zebra_rib.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406
  1. /* Routing Information Base.
  2. * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "prefix.h"
  23. #include "table.h"
  24. #include "memory.h"
  25. #include "str.h"
  26. #include "command.h"
  27. #include "if.h"
  28. #include "log.h"
  29. #include "sockunion.h"
  30. #include "linklist.h"
  31. #include "thread.h"
  32. #include "workqueue.h"
  33. #include "prefix.h"
  34. #include "routemap.h"
  35. #include "zebra/rib.h"
  36. #include "zebra/rt.h"
  37. #include "zebra/zserv.h"
  38. #include "zebra/redistribute.h"
  39. #include "zebra/debug.h"
  40. #include "zebra/zebra_fpm.h"
  41. /* Default rtm_table for all clients */
  42. extern struct zebra_t zebrad;
  43. /* Hold time for RIB process, should be very minimal.
  44. * it is useful to able to set it otherwise for testing, hence exported
  45. * as global here for test-rig code.
  46. */
  47. int rib_process_hold_time = 10;
  48. /* Each route type's string and default distance value. */
  49. static const struct
  50. {
  51. int key;
  52. int distance;
  53. } route_info[ZEBRA_ROUTE_MAX] =
  54. {
  55. [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
  56. [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
  57. [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
  58. [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
  59. [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
  60. [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
  61. [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
  62. [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
  63. [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
  64. [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
  65. [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 95},
  66. /* no entry/default: 150 */
  67. };
  68. /* Vector for routing table. */
  69. static vector vrf_vector;
  70. static void
  71. _rnode_zlog(const char *_func, struct route_node *rn, int priority,
  72. const char *msgfmt, ...)
  73. {
  74. char buf[INET6_ADDRSTRLEN + 4], *bptr;
  75. char msgbuf[512];
  76. va_list ap;
  77. va_start(ap, msgfmt);
  78. vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
  79. va_end(ap);
  80. if (rn)
  81. {
  82. rib_table_info_t *info = rn->table->info;
  83. inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
  84. bptr = buf + strlen(buf);
  85. snprintf(bptr, buf + sizeof(buf) - bptr, "/%d%s", rn->p.prefixlen,
  86. info->safi == SAFI_MULTICAST ? " (MRIB)" : "");
  87. }
  88. else
  89. {
  90. snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
  91. }
  92. zlog (NULL, priority, "%s: %s: %s", _func, buf, msgbuf);
  93. }
  94. #define rnode_debug(node, ...) \
  95. _rnode_zlog(__func__, node, LOG_DEBUG, __VA_ARGS__)
  96. #define rnode_info(node, ...) \
  97. _rnode_zlog(__func__, node, LOG_INFO, __VA_ARGS__)
  98. /*
  99. * vrf_table_create
  100. */
  101. static void
  102. vrf_table_create (struct vrf *vrf, afi_t afi, safi_t safi)
  103. {
  104. rib_table_info_t *info;
  105. struct route_table *table;
  106. assert (!vrf->table[afi][safi]);
  107. table = route_table_init ();
  108. vrf->table[afi][safi] = table;
  109. info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
  110. info->vrf = vrf;
  111. info->afi = afi;
  112. info->safi = safi;
  113. table->info = info;
  114. }
  115. /* Allocate new VRF. */
  116. static struct vrf *
  117. vrf_alloc (const char *name)
  118. {
  119. struct vrf *vrf;
  120. vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
  121. /* Put name. */
  122. if (name)
  123. vrf->name = XSTRDUP (MTYPE_VRF_NAME, name);
  124. /* Allocate routing table and static table. */
  125. vrf_table_create (vrf, AFI_IP, SAFI_UNICAST);
  126. vrf_table_create (vrf, AFI_IP6, SAFI_UNICAST);
  127. vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
  128. vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
  129. vrf_table_create (vrf, AFI_IP, SAFI_MULTICAST);
  130. vrf_table_create (vrf, AFI_IP6, SAFI_MULTICAST);
  131. vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
  132. vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
  133. return vrf;
  134. }
  135. /* Lookup VRF by identifier. */
  136. struct vrf *
  137. vrf_lookup (u_int32_t id)
  138. {
  139. return vector_lookup (vrf_vector, id);
  140. }
  141. /* Initialize VRF. */
  142. static void
  143. vrf_init (void)
  144. {
  145. struct vrf *default_table;
  146. /* Allocate VRF vector. */
  147. vrf_vector = vector_init (1);
  148. /* Allocate default main table. */
  149. default_table = vrf_alloc ("Default-IP-Routing-Table");
  150. /* Default table index must be 0. */
  151. vector_set_index (vrf_vector, 0, default_table);
  152. }
  153. /* Lookup route table. */
  154. struct route_table *
  155. vrf_table (afi_t afi, safi_t safi, u_int32_t id)
  156. {
  157. struct vrf *vrf;
  158. vrf = vrf_lookup (id);
  159. if (! vrf)
  160. return NULL;
  161. if( afi >= AFI_MAX || safi >= SAFI_MAX )
  162. return NULL;
  163. return vrf->table[afi][safi];
  164. }
  165. /* Lookup static route table. */
  166. struct route_table *
  167. vrf_static_table (afi_t afi, safi_t safi, u_int32_t id)
  168. {
  169. struct vrf *vrf;
  170. vrf = vrf_lookup (id);
  171. if (! vrf)
  172. return NULL;
  173. if( afi >= AFI_MAX || safi >= SAFI_MAX )
  174. return NULL;
  175. return vrf->stable[afi][safi];
  176. }
  177. /*
  178. * nexthop_type_to_str
  179. */
  180. const char *
  181. nexthop_type_to_str (enum nexthop_types_t nh_type)
  182. {
  183. static const char *desc[] = {
  184. "none",
  185. "Directly connected",
  186. "Interface route",
  187. "IPv4 nexthop",
  188. "IPv4 nexthop with ifindex",
  189. "IPv4 nexthop with ifname",
  190. "IPv6 nexthop",
  191. "IPv6 nexthop with ifindex",
  192. "IPv6 nexthop with ifname",
  193. "Null0 nexthop",
  194. };
  195. if (nh_type >= ZEBRA_NUM_OF (desc))
  196. return "<Invalid nh type>";
  197. return desc[nh_type];
  198. }
  199. /* Add nexthop to the end of a nexthop list. */
  200. static void
  201. _nexthop_add (struct nexthop **target, struct nexthop *nexthop)
  202. {
  203. struct nexthop *last;
  204. for (last = *target; last && last->next; last = last->next)
  205. ;
  206. if (last)
  207. last->next = nexthop;
  208. else
  209. *target = nexthop;
  210. nexthop->prev = last;
  211. }
  212. /* Add nexthop to the end of a rib node's nexthop list */
  213. static void
  214. nexthop_add (struct rib *rib, struct nexthop *nexthop)
  215. {
  216. _nexthop_add(&rib->nexthop, nexthop);
  217. rib->nexthop_num++;
  218. }
  219. /* Delete specified nexthop from the list. */
  220. static void
  221. nexthop_delete (struct rib *rib, struct nexthop *nexthop)
  222. {
  223. if (nexthop->next)
  224. nexthop->next->prev = nexthop->prev;
  225. if (nexthop->prev)
  226. nexthop->prev->next = nexthop->next;
  227. else
  228. rib->nexthop = nexthop->next;
  229. rib->nexthop_num--;
  230. }
  231. static void nexthops_free(struct nexthop *nexthop);
  232. /* Free nexthop. */
  233. static void
  234. nexthop_free (struct nexthop *nexthop)
  235. {
  236. if (nexthop->ifname)
  237. XFREE (0, nexthop->ifname);
  238. if (nexthop->resolved)
  239. nexthops_free(nexthop->resolved);
  240. XFREE (MTYPE_NEXTHOP, nexthop);
  241. }
  242. /* Frees a list of nexthops */
  243. static void
  244. nexthops_free (struct nexthop *nexthop)
  245. {
  246. struct nexthop *nh, *next;
  247. for (nh = nexthop; nh; nh = next)
  248. {
  249. next = nh->next;
  250. nexthop_free (nh);
  251. }
  252. }
  253. struct nexthop *
  254. nexthop_ifindex_add (struct rib *rib, unsigned int ifindex)
  255. {
  256. struct nexthop *nexthop;
  257. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  258. nexthop->type = NEXTHOP_TYPE_IFINDEX;
  259. nexthop->ifindex = ifindex;
  260. nexthop_add (rib, nexthop);
  261. return nexthop;
  262. }
  263. struct nexthop *
  264. nexthop_ifname_add (struct rib *rib, char *ifname)
  265. {
  266. struct nexthop *nexthop;
  267. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  268. nexthop->type = NEXTHOP_TYPE_IFNAME;
  269. nexthop->ifname = XSTRDUP (0, ifname);
  270. nexthop_add (rib, nexthop);
  271. return nexthop;
  272. }
  273. struct nexthop *
  274. nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
  275. {
  276. struct nexthop *nexthop;
  277. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  278. nexthop->type = NEXTHOP_TYPE_IPV4;
  279. nexthop->gate.ipv4 = *ipv4;
  280. if (src)
  281. nexthop->src.ipv4 = *src;
  282. nexthop_add (rib, nexthop);
  283. return nexthop;
  284. }
  285. struct nexthop *
  286. nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
  287. struct in_addr *src, unsigned int ifindex)
  288. {
  289. struct nexthop *nexthop;
  290. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  291. nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
  292. nexthop->gate.ipv4 = *ipv4;
  293. if (src)
  294. nexthop->src.ipv4 = *src;
  295. nexthop->ifindex = ifindex;
  296. nexthop_add (rib, nexthop);
  297. return nexthop;
  298. }
  299. #ifdef HAVE_IPV6
  300. struct nexthop *
  301. nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
  302. {
  303. struct nexthop *nexthop;
  304. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  305. nexthop->type = NEXTHOP_TYPE_IPV6;
  306. nexthop->gate.ipv6 = *ipv6;
  307. nexthop_add (rib, nexthop);
  308. return nexthop;
  309. }
  310. static struct nexthop *
  311. nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6,
  312. char *ifname)
  313. {
  314. struct nexthop *nexthop;
  315. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  316. nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME;
  317. nexthop->gate.ipv6 = *ipv6;
  318. nexthop->ifname = XSTRDUP (0, ifname);
  319. nexthop_add (rib, nexthop);
  320. return nexthop;
  321. }
  322. static struct nexthop *
  323. nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
  324. unsigned int ifindex)
  325. {
  326. struct nexthop *nexthop;
  327. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  328. nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
  329. nexthop->gate.ipv6 = *ipv6;
  330. nexthop->ifindex = ifindex;
  331. nexthop_add (rib, nexthop);
  332. return nexthop;
  333. }
  334. #endif /* HAVE_IPV6 */
  335. struct nexthop *
  336. nexthop_blackhole_add (struct rib *rib)
  337. {
  338. struct nexthop *nexthop;
  339. nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  340. nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
  341. SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
  342. nexthop_add (rib, nexthop);
  343. return nexthop;
  344. }
  345. /* This method checks whether a recursive nexthop has at
  346. * least one resolved nexthop in the fib.
  347. */
  348. int
  349. nexthop_has_fib_child(struct nexthop *nexthop)
  350. {
  351. struct nexthop *nh;
  352. if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  353. return 0;
  354. for (nh = nexthop->resolved; nh; nh = nh->next)
  355. if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB))
  356. return 1;
  357. return 0;
  358. }
  359. /* If force flag is not set, do not modify falgs at all for uninstall
  360. the route from FIB. */
  361. static int
  362. nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
  363. struct route_node *top)
  364. {
  365. struct prefix_ipv4 p;
  366. struct route_table *table;
  367. struct route_node *rn;
  368. struct rib *match;
  369. int resolved;
  370. struct nexthop *newhop;
  371. struct nexthop *resolved_hop;
  372. if (nexthop->type == NEXTHOP_TYPE_IPV4)
  373. nexthop->ifindex = 0;
  374. if (set)
  375. {
  376. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  377. nexthops_free(nexthop->resolved);
  378. nexthop->resolved = NULL;
  379. }
  380. /* Make lookup prefix. */
  381. memset (&p, 0, sizeof (struct prefix_ipv4));
  382. p.family = AF_INET;
  383. p.prefixlen = IPV4_MAX_PREFIXLEN;
  384. p.prefix = nexthop->gate.ipv4;
  385. /* Lookup table. */
  386. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  387. if (! table)
  388. return 0;
  389. rn = route_node_match (table, (struct prefix *) &p);
  390. while (rn)
  391. {
  392. route_unlock_node (rn);
  393. /* If lookup self prefix return immediately. */
  394. if (rn == top)
  395. return 0;
  396. /* Pick up selected route. */
  397. RNODE_FOREACH_RIB (rn, match)
  398. {
  399. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  400. continue;
  401. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  402. break;
  403. }
  404. /* If there is no selected route or matched route is EGP, go up
  405. tree. */
  406. if (! match
  407. || match->type == ZEBRA_ROUTE_BGP)
  408. {
  409. do {
  410. rn = rn->parent;
  411. } while (rn && rn->info == NULL);
  412. if (rn)
  413. route_lock_node (rn);
  414. }
  415. else
  416. {
  417. /* If the longest prefix match for the nexthop yields
  418. * a blackhole, mark it as inactive. */
  419. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
  420. || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
  421. return 0;
  422. if (match->type == ZEBRA_ROUTE_CONNECT)
  423. {
  424. /* Directly point connected route. */
  425. newhop = match->nexthop;
  426. if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
  427. nexthop->ifindex = newhop->ifindex;
  428. return 1;
  429. }
  430. else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
  431. {
  432. resolved = 0;
  433. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  434. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
  435. && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
  436. {
  437. if (set)
  438. {
  439. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  440. resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
  441. SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
  442. /* If the resolving route specifies a gateway, use it */
  443. if (newhop->type == NEXTHOP_TYPE_IPV4
  444. || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX
  445. || newhop->type == NEXTHOP_TYPE_IPV4_IFNAME)
  446. {
  447. resolved_hop->type = newhop->type;
  448. resolved_hop->gate.ipv4 = newhop->gate.ipv4;
  449. if (newhop->ifindex)
  450. {
  451. resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
  452. resolved_hop->ifindex = newhop->ifindex;
  453. }
  454. }
  455. /* If the resolving route is an interface route,
  456. * it means the gateway we are looking up is connected
  457. * to that interface. (The actual network is _not_ onlink).
  458. * Therefore, the resolved route should have the original
  459. * gateway as nexthop as it is directly connected.
  460. *
  461. * On Linux, we have to set the onlink netlink flag because
  462. * otherwise, the kernel won't accept the route. */
  463. if (newhop->type == NEXTHOP_TYPE_IFINDEX
  464. || newhop->type == NEXTHOP_TYPE_IFNAME)
  465. {
  466. resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
  467. resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
  468. resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
  469. resolved_hop->ifindex = newhop->ifindex;
  470. }
  471. _nexthop_add(&nexthop->resolved, resolved_hop);
  472. }
  473. resolved = 1;
  474. }
  475. return resolved;
  476. }
  477. else
  478. {
  479. return 0;
  480. }
  481. }
  482. }
  483. return 0;
  484. }
  485. #ifdef HAVE_IPV6
  486. /* If force flag is not set, do not modify falgs at all for uninstall
  487. the route from FIB. */
  488. static int
  489. nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
  490. struct route_node *top)
  491. {
  492. struct prefix_ipv6 p;
  493. struct route_table *table;
  494. struct route_node *rn;
  495. struct rib *match;
  496. int resolved;
  497. struct nexthop *newhop;
  498. struct nexthop *resolved_hop;
  499. if (nexthop->type == NEXTHOP_TYPE_IPV6)
  500. nexthop->ifindex = 0;
  501. if (set)
  502. {
  503. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  504. nexthops_free(nexthop->resolved);
  505. nexthop->resolved = NULL;
  506. }
  507. /* Make lookup prefix. */
  508. memset (&p, 0, sizeof (struct prefix_ipv6));
  509. p.family = AF_INET6;
  510. p.prefixlen = IPV6_MAX_PREFIXLEN;
  511. p.prefix = nexthop->gate.ipv6;
  512. /* Lookup table. */
  513. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  514. if (! table)
  515. return 0;
  516. rn = route_node_match (table, (struct prefix *) &p);
  517. while (rn)
  518. {
  519. route_unlock_node (rn);
  520. /* If lookup self prefix return immediately. */
  521. if (rn == top)
  522. return 0;
  523. /* Pick up selected route. */
  524. RNODE_FOREACH_RIB (rn, match)
  525. {
  526. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  527. continue;
  528. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  529. break;
  530. }
  531. /* If there is no selected route or matched route is EGP, go up
  532. tree. */
  533. if (! match
  534. || match->type == ZEBRA_ROUTE_BGP)
  535. {
  536. do {
  537. rn = rn->parent;
  538. } while (rn && rn->info == NULL);
  539. if (rn)
  540. route_lock_node (rn);
  541. }
  542. else
  543. {
  544. /* If the longest prefix match for the nexthop yields
  545. * a blackhole, mark it as inactive. */
  546. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
  547. || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
  548. return 0;
  549. if (match->type == ZEBRA_ROUTE_CONNECT)
  550. {
  551. /* Directly point connected route. */
  552. newhop = match->nexthop;
  553. if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
  554. nexthop->ifindex = newhop->ifindex;
  555. return 1;
  556. }
  557. else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
  558. {
  559. resolved = 0;
  560. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  561. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
  562. && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
  563. {
  564. if (set)
  565. {
  566. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  567. resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
  568. SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
  569. /* See nexthop_active_ipv4 for a description how the
  570. * resolved nexthop is constructed. */
  571. if (newhop->type == NEXTHOP_TYPE_IPV6
  572. || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  573. || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  574. {
  575. resolved_hop->type = newhop->type;
  576. resolved_hop->gate.ipv6 = newhop->gate.ipv6;
  577. if (newhop->ifindex)
  578. {
  579. resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
  580. resolved_hop->ifindex = newhop->ifindex;
  581. }
  582. }
  583. if (newhop->type == NEXTHOP_TYPE_IFINDEX
  584. || newhop->type == NEXTHOP_TYPE_IFNAME)
  585. {
  586. resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
  587. resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
  588. resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
  589. resolved_hop->ifindex = newhop->ifindex;
  590. }
  591. _nexthop_add(&nexthop->resolved, resolved_hop);
  592. }
  593. resolved = 1;
  594. }
  595. return resolved;
  596. }
  597. else
  598. {
  599. return 0;
  600. }
  601. }
  602. }
  603. return 0;
  604. }
  605. #endif /* HAVE_IPV6 */
  606. struct rib *
  607. rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp)
  608. {
  609. struct route_table *table;
  610. struct route_node *rn;
  611. struct rib *match;
  612. struct nexthop *newhop, *tnewhop;
  613. int recursing;
  614. /* Lookup table. */
  615. table = vrf_table (AFI_IP, safi, 0);
  616. if (! table)
  617. return 0;
  618. rn = route_node_match_ipv4 (table, &addr);
  619. while (rn)
  620. {
  621. route_unlock_node (rn);
  622. /* Pick up selected route. */
  623. RNODE_FOREACH_RIB (rn, match)
  624. {
  625. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  626. continue;
  627. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  628. break;
  629. }
  630. /* If there is no selected route or matched route is EGP, go up
  631. tree. */
  632. if (!match || (skip_bgp && (match->type == ZEBRA_ROUTE_BGP)))
  633. {
  634. do {
  635. rn = rn->parent;
  636. } while (rn && rn->info == NULL);
  637. if (rn)
  638. route_lock_node (rn);
  639. }
  640. else
  641. {
  642. if (match->type == ZEBRA_ROUTE_CONNECT)
  643. /* Directly point connected route. */
  644. return match;
  645. else
  646. {
  647. for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
  648. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
  649. return match;
  650. return NULL;
  651. }
  652. }
  653. }
  654. return NULL;
  655. }
  656. struct rib *
  657. rib_lookup_ipv4 (struct prefix_ipv4 *p)
  658. {
  659. struct route_table *table;
  660. struct route_node *rn;
  661. struct rib *match;
  662. struct nexthop *nexthop, *tnexthop;
  663. int recursing;
  664. /* Lookup table. */
  665. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  666. if (! table)
  667. return 0;
  668. rn = route_node_lookup (table, (struct prefix *) p);
  669. /* No route for this prefix. */
  670. if (! rn)
  671. return NULL;
  672. /* Unlock node. */
  673. route_unlock_node (rn);
  674. RNODE_FOREACH_RIB (rn, match)
  675. {
  676. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  677. continue;
  678. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  679. break;
  680. }
  681. if (! match || match->type == ZEBRA_ROUTE_BGP)
  682. return NULL;
  683. if (match->type == ZEBRA_ROUTE_CONNECT)
  684. return match;
  685. for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
  686. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  687. return match;
  688. return NULL;
  689. }
  690. /*
  691. * This clone function, unlike its original rib_lookup_ipv4(), checks
  692. * if specified IPv4 route record (prefix/mask -> gate) exists in
  693. * the whole RIB and has ZEBRA_FLAG_SELECTED set.
  694. *
  695. * Return values:
  696. * -1: error
  697. * 0: exact match found
  698. * 1: a match was found with a different gate
  699. * 2: connected route found
  700. * 3: no matches found
  701. */
  702. int
  703. rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate)
  704. {
  705. struct route_table *table;
  706. struct route_node *rn;
  707. struct rib *match;
  708. struct nexthop *nexthop, *tnexthop;
  709. int recursing;
  710. int nexthops_active;
  711. /* Lookup table. */
  712. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  713. if (! table)
  714. return ZEBRA_RIB_LOOKUP_ERROR;
  715. /* Scan the RIB table for exactly matching RIB entry. */
  716. rn = route_node_lookup (table, (struct prefix *) p);
  717. /* No route for this prefix. */
  718. if (! rn)
  719. return ZEBRA_RIB_NOTFOUND;
  720. /* Unlock node. */
  721. route_unlock_node (rn);
  722. /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
  723. RNODE_FOREACH_RIB (rn, match)
  724. {
  725. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  726. continue;
  727. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  728. break;
  729. }
  730. /* None such found :( */
  731. if (!match)
  732. return ZEBRA_RIB_NOTFOUND;
  733. if (match->type == ZEBRA_ROUTE_CONNECT)
  734. return ZEBRA_RIB_FOUND_CONNECTED;
  735. /* Ok, we have a cood candidate, let's check it's nexthop list... */
  736. nexthops_active = 0;
  737. for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
  738. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  739. {
  740. nexthops_active = 1;
  741. if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate))
  742. return ZEBRA_RIB_FOUND_EXACT;
  743. if (IS_ZEBRA_DEBUG_RIB)
  744. {
  745. char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
  746. inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
  747. inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN);
  748. zlog_debug ("%s: qgate == %s, %s == %s", __func__,
  749. qgate_buf, recursing ? "rgate" : "gate", gate_buf);
  750. }
  751. }
  752. if (nexthops_active)
  753. return ZEBRA_RIB_FOUND_NOGATE;
  754. return ZEBRA_RIB_NOTFOUND;
  755. }
  756. #ifdef HAVE_IPV6
  757. struct rib *
  758. rib_match_ipv6 (struct in6_addr *addr)
  759. {
  760. struct prefix_ipv6 p;
  761. struct route_table *table;
  762. struct route_node *rn;
  763. struct rib *match;
  764. struct nexthop *newhop, *tnewhop;
  765. int recursing;
  766. /* Lookup table. */
  767. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  768. if (! table)
  769. return 0;
  770. memset (&p, 0, sizeof (struct prefix_ipv6));
  771. p.family = AF_INET6;
  772. p.prefixlen = IPV6_MAX_PREFIXLEN;
  773. IPV6_ADDR_COPY (&p.prefix, addr);
  774. rn = route_node_match (table, (struct prefix *) &p);
  775. while (rn)
  776. {
  777. route_unlock_node (rn);
  778. /* Pick up selected route. */
  779. RNODE_FOREACH_RIB (rn, match)
  780. {
  781. if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
  782. continue;
  783. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  784. break;
  785. }
  786. /* If there is no selected route or matched route is EGP, go up
  787. tree. */
  788. if (! match
  789. || match->type == ZEBRA_ROUTE_BGP)
  790. {
  791. do {
  792. rn = rn->parent;
  793. } while (rn && rn->info == NULL);
  794. if (rn)
  795. route_lock_node (rn);
  796. }
  797. else
  798. {
  799. if (match->type == ZEBRA_ROUTE_CONNECT)
  800. /* Directly point connected route. */
  801. return match;
  802. else
  803. {
  804. for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
  805. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
  806. return match;
  807. return NULL;
  808. }
  809. }
  810. }
  811. return NULL;
  812. }
  813. #endif /* HAVE_IPV6 */
  814. #define RIB_SYSTEM_ROUTE(R) \
  815. ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
  816. /* This function verifies reachability of one given nexthop, which can be
  817. * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
  818. * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
  819. * nexthop->ifindex will be updated appropriately as well.
  820. * An existing route map can turn (otherwise active) nexthop into inactive, but
  821. * not vice versa.
  822. *
  823. * The return value is the final value of 'ACTIVE' flag.
  824. */
  825. static unsigned
  826. nexthop_active_check (struct route_node *rn, struct rib *rib,
  827. struct nexthop *nexthop, int set)
  828. {
  829. rib_table_info_t *info = rn->table->info;
  830. struct interface *ifp;
  831. route_map_result_t ret = RMAP_MATCH;
  832. extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1];
  833. struct route_map *rmap;
  834. int family;
  835. family = 0;
  836. switch (nexthop->type)
  837. {
  838. case NEXTHOP_TYPE_IFINDEX:
  839. ifp = if_lookup_by_index (nexthop->ifindex);
  840. if (ifp && if_is_operative(ifp))
  841. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  842. else
  843. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  844. break;
  845. case NEXTHOP_TYPE_IPV6_IFNAME:
  846. family = AFI_IP6;
  847. case NEXTHOP_TYPE_IFNAME:
  848. ifp = if_lookup_by_name (nexthop->ifname);
  849. if (ifp && if_is_operative(ifp))
  850. {
  851. if (set)
  852. nexthop->ifindex = ifp->ifindex;
  853. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  854. }
  855. else
  856. {
  857. if (set)
  858. nexthop->ifindex = 0;
  859. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  860. }
  861. break;
  862. case NEXTHOP_TYPE_IPV4:
  863. case NEXTHOP_TYPE_IPV4_IFINDEX:
  864. family = AFI_IP;
  865. if (nexthop_active_ipv4 (rib, nexthop, set, rn))
  866. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  867. else
  868. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  869. break;
  870. #ifdef HAVE_IPV6
  871. case NEXTHOP_TYPE_IPV6:
  872. family = AFI_IP6;
  873. if (nexthop_active_ipv6 (rib, nexthop, set, rn))
  874. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  875. else
  876. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  877. break;
  878. case NEXTHOP_TYPE_IPV6_IFINDEX:
  879. family = AFI_IP6;
  880. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
  881. {
  882. ifp = if_lookup_by_index (nexthop->ifindex);
  883. if (ifp && if_is_operative(ifp))
  884. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  885. else
  886. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  887. }
  888. else
  889. {
  890. if (nexthop_active_ipv6 (rib, nexthop, set, rn))
  891. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  892. else
  893. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  894. }
  895. break;
  896. #endif /* HAVE_IPV6 */
  897. case NEXTHOP_TYPE_BLACKHOLE:
  898. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  899. break;
  900. default:
  901. break;
  902. }
  903. if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  904. return 0;
  905. /* XXX: What exactly do those checks do? Do we support
  906. * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
  907. if (RIB_SYSTEM_ROUTE(rib) ||
  908. (family == AFI_IP && rn->p.family != AF_INET) ||
  909. (family == AFI_IP6 && rn->p.family != AF_INET6))
  910. return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  911. /* The original code didn't determine the family correctly
  912. * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
  913. * from the rib_table_info in those cases.
  914. * Possibly it may be better to use only the rib_table_info
  915. * in every case.
  916. */
  917. if (!family)
  918. family = info->afi;
  919. rmap = 0;
  920. if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX &&
  921. proto_rm[family][rib->type])
  922. rmap = route_map_lookup_by_name (proto_rm[family][rib->type]);
  923. if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX])
  924. rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]);
  925. if (rmap) {
  926. ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop);
  927. }
  928. if (ret == RMAP_DENYMATCH)
  929. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  930. return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  931. }
  932. /* Iterate over all nexthops of the given RIB entry and refresh their
  933. * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
  934. * nexthop is found to toggle the ACTIVE flag, the whole rib structure
  935. * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
  936. * transparently passed to nexthop_active_check().
  937. *
  938. * Return value is the new number of active nexthops.
  939. */
  940. static int
  941. nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
  942. {
  943. struct nexthop *nexthop;
  944. unsigned int prev_active, prev_index, new_active;
  945. rib->nexthop_active_num = 0;
  946. UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
  947. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  948. {
  949. prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  950. prev_index = nexthop->ifindex;
  951. if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
  952. rib->nexthop_active_num++;
  953. if (prev_active != new_active ||
  954. prev_index != nexthop->ifindex)
  955. SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
  956. }
  957. return rib->nexthop_active_num;
  958. }
  959. static void
  960. rib_install_kernel (struct route_node *rn, struct rib *rib)
  961. {
  962. int ret = 0;
  963. struct nexthop *nexthop, *tnexthop;
  964. int recursing;
  965. /*
  966. * Make sure we update the FPM any time we send new information to
  967. * the kernel.
  968. */
  969. zfpm_trigger_update (rn, "installing in kernel");
  970. switch (PREFIX_FAMILY (&rn->p))
  971. {
  972. case AF_INET:
  973. ret = kernel_add_ipv4 (&rn->p, rib);
  974. break;
  975. #ifdef HAVE_IPV6
  976. case AF_INET6:
  977. ret = kernel_add_ipv6 (&rn->p, rib);
  978. break;
  979. #endif /* HAVE_IPV6 */
  980. }
  981. /* This condition is never met, if we are using rt_socket.c */
  982. if (ret < 0)
  983. {
  984. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  985. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  986. }
  987. }
  988. /* Uninstall the route from kernel. */
  989. static int
  990. rib_uninstall_kernel (struct route_node *rn, struct rib *rib)
  991. {
  992. int ret = 0;
  993. struct nexthop *nexthop, *tnexthop;
  994. int recursing;
  995. /*
  996. * Make sure we update the FPM any time we send new information to
  997. * the kernel.
  998. */
  999. zfpm_trigger_update (rn, "uninstalling from kernel");
  1000. switch (PREFIX_FAMILY (&rn->p))
  1001. {
  1002. case AF_INET:
  1003. ret = kernel_delete_ipv4 (&rn->p, rib);
  1004. break;
  1005. #ifdef HAVE_IPV6
  1006. case AF_INET6:
  1007. ret = kernel_delete_ipv6 (&rn->p, rib);
  1008. break;
  1009. #endif /* HAVE_IPV6 */
  1010. }
  1011. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1012. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1013. return ret;
  1014. }
  1015. /* Uninstall the route from kernel. */
  1016. static void
  1017. rib_uninstall (struct route_node *rn, struct rib *rib)
  1018. {
  1019. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1020. {
  1021. zfpm_trigger_update (rn, "rib_uninstall");
  1022. redistribute_delete (&rn->p, rib);
  1023. if (! RIB_SYSTEM_ROUTE (rib))
  1024. rib_uninstall_kernel (rn, rib);
  1025. UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED);
  1026. }
  1027. }
  1028. static void rib_unlink (struct route_node *, struct rib *);
  1029. /*
  1030. * rib_can_delete_dest
  1031. *
  1032. * Returns TRUE if the given dest can be deleted from the table.
  1033. */
  1034. static int
  1035. rib_can_delete_dest (rib_dest_t *dest)
  1036. {
  1037. if (dest->routes)
  1038. {
  1039. return 0;
  1040. }
  1041. /*
  1042. * Don't delete the dest if we have to update the FPM about this
  1043. * prefix.
  1044. */
  1045. if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) ||
  1046. CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
  1047. return 0;
  1048. return 1;
  1049. }
  1050. /*
  1051. * rib_gc_dest
  1052. *
  1053. * Garbage collect the rib dest corresponding to the given route node
  1054. * if appropriate.
  1055. *
  1056. * Returns TRUE if the dest was deleted, FALSE otherwise.
  1057. */
  1058. int
  1059. rib_gc_dest (struct route_node *rn)
  1060. {
  1061. rib_dest_t *dest;
  1062. dest = rib_dest_from_rnode (rn);
  1063. if (!dest)
  1064. return 0;
  1065. if (!rib_can_delete_dest (dest))
  1066. return 0;
  1067. if (IS_ZEBRA_DEBUG_RIB)
  1068. rnode_debug (rn, "removing dest from table");
  1069. dest->rnode = NULL;
  1070. XFREE (MTYPE_RIB_DEST, dest);
  1071. rn->info = NULL;
  1072. /*
  1073. * Release the one reference that we keep on the route node.
  1074. */
  1075. route_unlock_node (rn);
  1076. return 1;
  1077. }
  1078. /* Core function for processing routing information base. */
  1079. static void
  1080. rib_process (struct route_node *rn)
  1081. {
  1082. struct rib *rib;
  1083. struct rib *next;
  1084. struct rib *fib = NULL;
  1085. struct rib *select = NULL;
  1086. struct rib *del = NULL;
  1087. int installed = 0;
  1088. struct nexthop *nexthop = NULL, *tnexthop;
  1089. int recursing;
  1090. rib_table_info_t *info;
  1091. assert (rn);
  1092. info = rn->table->info;
  1093. RNODE_FOREACH_RIB_SAFE (rn, rib, next)
  1094. {
  1095. /* Currently installed rib. */
  1096. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1097. {
  1098. assert (fib == NULL);
  1099. fib = rib;
  1100. }
  1101. /* Unlock removed routes, so they'll be freed, bar the FIB entry,
  1102. * which we need to do do further work with below.
  1103. */
  1104. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1105. {
  1106. if (rib != fib)
  1107. {
  1108. if (IS_ZEBRA_DEBUG_RIB)
  1109. rnode_debug (rn, "rn %p, removing rib %p", rn, rib);
  1110. rib_unlink (rn, rib);
  1111. }
  1112. else
  1113. del = rib;
  1114. continue;
  1115. }
  1116. /* Skip unreachable nexthop. */
  1117. if (! nexthop_active_update (rn, rib, 0))
  1118. continue;
  1119. if (info->safi == SAFI_MULTICAST)
  1120. continue;
  1121. /* Infinit distance. */
  1122. if (rib->distance == DISTANCE_INFINITY)
  1123. continue;
  1124. /* Newly selected rib, the common case. */
  1125. if (!select)
  1126. {
  1127. select = rib;
  1128. continue;
  1129. }
  1130. /* filter route selection in following order:
  1131. * - connected beats other types
  1132. * - lower distance beats higher
  1133. * - lower metric beats higher for equal distance
  1134. * - last, hence oldest, route wins tie break.
  1135. */
  1136. /* Connected routes. Pick the last connected
  1137. * route of the set of lowest metric connected routes.
  1138. */
  1139. if (rib->type == ZEBRA_ROUTE_CONNECT)
  1140. {
  1141. if (select->type != ZEBRA_ROUTE_CONNECT
  1142. || rib->metric <= select->metric)
  1143. select = rib;
  1144. continue;
  1145. }
  1146. else if (select->type == ZEBRA_ROUTE_CONNECT)
  1147. continue;
  1148. /* higher distance loses */
  1149. if (rib->distance > select->distance)
  1150. continue;
  1151. /* lower wins */
  1152. if (rib->distance < select->distance)
  1153. {
  1154. select = rib;
  1155. continue;
  1156. }
  1157. /* metric tie-breaks equal distance */
  1158. if (rib->metric <= select->metric)
  1159. select = rib;
  1160. } /* RNODE_FOREACH_RIB_SAFE */
  1161. /* After the cycle is finished, the following pointers will be set:
  1162. * select --- the winner RIB entry, if any was found, otherwise NULL
  1163. * fib --- the SELECTED RIB entry, if any, otherwise NULL
  1164. * del --- equal to fib, if fib is queued for deletion, NULL otherwise
  1165. * rib --- NULL
  1166. */
  1167. /* Same RIB entry is selected. Update FIB and finish. */
  1168. if (select && select == fib)
  1169. {
  1170. if (IS_ZEBRA_DEBUG_RIB)
  1171. rnode_debug (rn, "Updating existing route, select %p, fib %p",
  1172. select, fib);
  1173. if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
  1174. {
  1175. zfpm_trigger_update (rn, "updating existing route");
  1176. redistribute_delete (&rn->p, select);
  1177. if (! RIB_SYSTEM_ROUTE (select))
  1178. rib_uninstall_kernel (rn, select);
  1179. /* Set real nexthop. */
  1180. nexthop_active_update (rn, select, 1);
  1181. if (! RIB_SYSTEM_ROUTE (select))
  1182. rib_install_kernel (rn, select);
  1183. redistribute_add (&rn->p, select);
  1184. }
  1185. else if (! RIB_SYSTEM_ROUTE (select))
  1186. {
  1187. /* Housekeeping code to deal with
  1188. race conditions in kernel with linux
  1189. netlink reporting interface up before IPv4 or IPv6 protocol
  1190. is ready to add routes.
  1191. This makes sure the routes are IN the kernel.
  1192. */
  1193. for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing))
  1194. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  1195. {
  1196. installed = 1;
  1197. break;
  1198. }
  1199. if (! installed)
  1200. rib_install_kernel (rn, select);
  1201. }
  1202. goto end;
  1203. }
  1204. /* At this point we either haven't found the best RIB entry or it is
  1205. * different from what we currently intend to flag with SELECTED. In both
  1206. * cases, if a RIB block is present in FIB, it should be withdrawn.
  1207. */
  1208. if (fib)
  1209. {
  1210. if (IS_ZEBRA_DEBUG_RIB)
  1211. rnode_debug (rn, "Removing existing route, fib %p", fib);
  1212. zfpm_trigger_update (rn, "removing existing route");
  1213. redistribute_delete (&rn->p, fib);
  1214. if (! RIB_SYSTEM_ROUTE (fib))
  1215. rib_uninstall_kernel (rn, fib);
  1216. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  1217. /* Set real nexthop. */
  1218. nexthop_active_update (rn, fib, 1);
  1219. }
  1220. /* Regardless of some RIB entry being SELECTED or not before, now we can
  1221. * tell, that if a new winner exists, FIB is still not updated with this
  1222. * data, but ready to be.
  1223. */
  1224. if (select)
  1225. {
  1226. if (IS_ZEBRA_DEBUG_RIB)
  1227. rnode_debug (rn, "Adding route, select %p", select);
  1228. zfpm_trigger_update (rn, "new route selected");
  1229. /* Set real nexthop. */
  1230. nexthop_active_update (rn, select, 1);
  1231. if (! RIB_SYSTEM_ROUTE (select))
  1232. rib_install_kernel (rn, select);
  1233. SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
  1234. redistribute_add (&rn->p, select);
  1235. }
  1236. /* FIB route was removed, should be deleted */
  1237. if (del)
  1238. {
  1239. if (IS_ZEBRA_DEBUG_RIB)
  1240. rnode_debug (rn, "Deleting fib %p, rn %p", del, rn);
  1241. rib_unlink (rn, del);
  1242. }
  1243. end:
  1244. if (IS_ZEBRA_DEBUG_RIB_Q)
  1245. rnode_debug (rn, "rn %p dequeued", rn);
  1246. /*
  1247. * Check if the dest can be deleted now.
  1248. */
  1249. rib_gc_dest (rn);
  1250. }
  1251. /* Take a list of route_node structs and return 1, if there was a record
  1252. * picked from it and processed by rib_process(). Don't process more,
  1253. * than one RN record; operate only in the specified sub-queue.
  1254. */
  1255. static unsigned int
  1256. process_subq (struct list * subq, u_char qindex)
  1257. {
  1258. struct listnode *lnode = listhead (subq);
  1259. struct route_node *rnode;
  1260. if (!lnode)
  1261. return 0;
  1262. rnode = listgetdata (lnode);
  1263. rib_process (rnode);
  1264. if (rnode->info)
  1265. UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex));
  1266. #if 0
  1267. else
  1268. {
  1269. zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
  1270. __func__, rnode, rnode->lock);
  1271. zlog_backtrace(LOG_DEBUG);
  1272. }
  1273. #endif
  1274. route_unlock_node (rnode);
  1275. list_delete_node (subq, lnode);
  1276. return 1;
  1277. }
  1278. /* Dispatch the meta queue by picking, processing and unlocking the next RN from
  1279. * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
  1280. * is pointed to the meta queue structure.
  1281. */
  1282. static wq_item_status
  1283. meta_queue_process (struct work_queue *dummy, void *data)
  1284. {
  1285. struct meta_queue * mq = data;
  1286. unsigned i;
  1287. for (i = 0; i < MQ_SIZE; i++)
  1288. if (process_subq (mq->subq[i], i))
  1289. {
  1290. mq->size--;
  1291. break;
  1292. }
  1293. return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
  1294. }
  1295. /*
  1296. * Map from rib types to queue type (priority) in meta queue
  1297. */
  1298. static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = {
  1299. [ZEBRA_ROUTE_SYSTEM] = 4,
  1300. [ZEBRA_ROUTE_KERNEL] = 0,
  1301. [ZEBRA_ROUTE_CONNECT] = 0,
  1302. [ZEBRA_ROUTE_STATIC] = 1,
  1303. [ZEBRA_ROUTE_RIP] = 2,
  1304. [ZEBRA_ROUTE_RIPNG] = 2,
  1305. [ZEBRA_ROUTE_OSPF] = 2,
  1306. [ZEBRA_ROUTE_OSPF6] = 2,
  1307. [ZEBRA_ROUTE_ISIS] = 2,
  1308. [ZEBRA_ROUTE_BGP] = 3,
  1309. [ZEBRA_ROUTE_HSLS] = 4,
  1310. [ZEBRA_ROUTE_BABEL] = 2,
  1311. };
  1312. /* Look into the RN and queue it into one or more priority queues,
  1313. * increasing the size for each data push done.
  1314. */
  1315. static void
  1316. rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn)
  1317. {
  1318. struct rib *rib;
  1319. RNODE_FOREACH_RIB (rn, rib)
  1320. {
  1321. u_char qindex = meta_queue_map[rib->type];
  1322. /* Invariant: at this point we always have rn->info set. */
  1323. if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags,
  1324. RIB_ROUTE_QUEUED (qindex)))
  1325. {
  1326. if (IS_ZEBRA_DEBUG_RIB_Q)
  1327. rnode_debug (rn, "rn %p is already queued in sub-queue %u",
  1328. rn, qindex);
  1329. continue;
  1330. }
  1331. SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex));
  1332. listnode_add (mq->subq[qindex], rn);
  1333. route_lock_node (rn);
  1334. mq->size++;
  1335. if (IS_ZEBRA_DEBUG_RIB_Q)
  1336. rnode_debug (rn, "queued rn %p into sub-queue %u",
  1337. rn, qindex);
  1338. }
  1339. }
  1340. /* Add route_node to work queue and schedule processing */
  1341. static void
  1342. rib_queue_add (struct zebra_t *zebra, struct route_node *rn)
  1343. {
  1344. assert (zebra && rn);
  1345. /* Pointless to queue a route_node with no RIB entries to add or remove */
  1346. if (!rnode_to_ribs (rn))
  1347. {
  1348. zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
  1349. __func__, rn, rn->lock);
  1350. zlog_backtrace(LOG_DEBUG);
  1351. return;
  1352. }
  1353. if (IS_ZEBRA_DEBUG_RIB_Q)
  1354. rnode_info (rn, "work queue added");
  1355. assert (zebra);
  1356. if (zebra->ribq == NULL)
  1357. {
  1358. zlog_err ("%s: work_queue does not exist!", __func__);
  1359. return;
  1360. }
  1361. /*
  1362. * The RIB queue should normally be either empty or holding the only
  1363. * work_queue_item element. In the latter case this element would
  1364. * hold a pointer to the meta queue structure, which must be used to
  1365. * actually queue the route nodes to process. So create the MQ
  1366. * holder, if necessary, then push the work into it in any case.
  1367. * This semantics was introduced after 0.99.9 release.
  1368. */
  1369. if (!zebra->ribq->items->count)
  1370. work_queue_add (zebra->ribq, zebra->mq);
  1371. rib_meta_queue_add (zebra->mq, rn);
  1372. if (IS_ZEBRA_DEBUG_RIB_Q)
  1373. rnode_debug (rn, "rn %p queued", rn);
  1374. return;
  1375. }
  1376. /* Create new meta queue.
  1377. A destructor function doesn't seem to be necessary here.
  1378. */
  1379. static struct meta_queue *
  1380. meta_queue_new (void)
  1381. {
  1382. struct meta_queue *new;
  1383. unsigned i;
  1384. new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue));
  1385. assert(new);
  1386. for (i = 0; i < MQ_SIZE; i++)
  1387. {
  1388. new->subq[i] = list_new ();
  1389. assert(new->subq[i]);
  1390. }
  1391. return new;
  1392. }
  1393. /* initialise zebra rib work queue */
  1394. static void
  1395. rib_queue_init (struct zebra_t *zebra)
  1396. {
  1397. assert (zebra);
  1398. if (! (zebra->ribq = work_queue_new (zebra->master,
  1399. "route_node processing")))
  1400. {
  1401. zlog_err ("%s: could not initialise work queue!", __func__);
  1402. return;
  1403. }
  1404. /* fill in the work queue spec */
  1405. zebra->ribq->spec.workfunc = &meta_queue_process;
  1406. zebra->ribq->spec.errorfunc = NULL;
  1407. /* XXX: TODO: These should be runtime configurable via vty */
  1408. zebra->ribq->spec.max_retries = 3;
  1409. zebra->ribq->spec.hold = rib_process_hold_time;
  1410. if (!(zebra->mq = meta_queue_new ()))
  1411. {
  1412. zlog_err ("%s: could not initialise meta queue!", __func__);
  1413. return;
  1414. }
  1415. return;
  1416. }
  1417. /* RIB updates are processed via a queue of pointers to route_nodes.
  1418. *
  1419. * The queue length is bounded by the maximal size of the routing table,
  1420. * as a route_node will not be requeued, if already queued.
  1421. *
  1422. * RIBs are submitted via rib_addnode or rib_delnode which set minimal
  1423. * state, or static_install_ipv{4,6} (when an existing RIB is updated)
  1424. * and then submit route_node to queue for best-path selection later.
  1425. * Order of add/delete state changes are preserved for any given RIB.
  1426. *
  1427. * Deleted RIBs are reaped during best-path selection.
  1428. *
  1429. * rib_addnode
  1430. * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
  1431. * |-------->| | best RIB, if required
  1432. * | |
  1433. * static_install->|->rib_addqueue...... -> rib_process
  1434. * | |
  1435. * |-------->| |-> rib_unlink
  1436. * |-> set RIB_ENTRY_REMOVE |
  1437. * rib_delnode (RIB freed)
  1438. *
  1439. * The 'info' pointer of a route_node points to a rib_dest_t
  1440. * ('dest'). Queueing state for a route_node is kept on the dest. The
  1441. * dest is created on-demand by rib_link() and is kept around at least
  1442. * as long as there are ribs hanging off it (@see rib_gc_dest()).
  1443. *
  1444. * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
  1445. *
  1446. * - route_nodes: refcounted by:
  1447. * - dest attached to route_node:
  1448. * - managed by: rib_link/rib_gc_dest
  1449. * - route_node processing queue
  1450. * - managed by: rib_addqueue, rib_process.
  1451. *
  1452. */
  1453. /* Add RIB to head of the route node. */
  1454. static void
  1455. rib_link (struct route_node *rn, struct rib *rib)
  1456. {
  1457. struct rib *head;
  1458. rib_dest_t *dest;
  1459. assert (rib && rn);
  1460. if (IS_ZEBRA_DEBUG_RIB)
  1461. rnode_debug (rn, "rn %p, rib %p", rn, rib);
  1462. dest = rib_dest_from_rnode (rn);
  1463. if (!dest)
  1464. {
  1465. if (IS_ZEBRA_DEBUG_RIB)
  1466. rnode_debug (rn, "adding dest to table");
  1467. dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t));
  1468. route_lock_node (rn); /* rn route table reference */
  1469. rn->info = dest;
  1470. dest->rnode = rn;
  1471. }
  1472. head = dest->routes;
  1473. if (head)
  1474. {
  1475. head->prev = rib;
  1476. }
  1477. rib->next = head;
  1478. dest->routes = rib;
  1479. rib_queue_add (&zebrad, rn);
  1480. }
  1481. static void
  1482. rib_addnode (struct route_node *rn, struct rib *rib)
  1483. {
  1484. /* RIB node has been un-removed before route-node is processed.
  1485. * route_node must hence already be on the queue for processing..
  1486. */
  1487. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1488. {
  1489. if (IS_ZEBRA_DEBUG_RIB)
  1490. rnode_debug (rn, "rn %p, un-removed rib %p", rn, rib);
  1491. UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
  1492. return;
  1493. }
  1494. rib_link (rn, rib);
  1495. }
  1496. /*
  1497. * rib_unlink
  1498. *
  1499. * Detach a rib structure from a route_node.
  1500. *
  1501. * Note that a call to rib_unlink() should be followed by a call to
  1502. * rib_gc_dest() at some point. This allows a rib_dest_t that is no
  1503. * longer required to be deleted.
  1504. */
  1505. static void
  1506. rib_unlink (struct route_node *rn, struct rib *rib)
  1507. {
  1508. rib_dest_t *dest;
  1509. assert (rn && rib);
  1510. if (IS_ZEBRA_DEBUG_RIB)
  1511. rnode_debug (rn, "rn %p, rib %p", rn, rib);
  1512. dest = rib_dest_from_rnode (rn);
  1513. if (rib->next)
  1514. rib->next->prev = rib->prev;
  1515. if (rib->prev)
  1516. rib->prev->next = rib->next;
  1517. else
  1518. {
  1519. dest->routes = rib->next;
  1520. }
  1521. /* free RIB and nexthops */
  1522. nexthops_free(rib->nexthop);
  1523. XFREE (MTYPE_RIB, rib);
  1524. }
  1525. static void
  1526. rib_delnode (struct route_node *rn, struct rib *rib)
  1527. {
  1528. if (IS_ZEBRA_DEBUG_RIB)
  1529. rnode_debug (rn, "rn %p, rib %p, removing", rn, rib);
  1530. SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
  1531. rib_queue_add (&zebrad, rn);
  1532. }
  1533. int
  1534. rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  1535. struct in_addr *gate, struct in_addr *src,
  1536. unsigned int ifindex, u_int32_t vrf_id,
  1537. u_int32_t metric, u_char distance, safi_t safi)
  1538. {
  1539. struct rib *rib;
  1540. struct rib *same = NULL;
  1541. struct route_table *table;
  1542. struct route_node *rn;
  1543. struct nexthop *nexthop;
  1544. /* Lookup table. */
  1545. table = vrf_table (AFI_IP, safi, 0);
  1546. if (! table)
  1547. return 0;
  1548. /* Make it sure prefixlen is applied to the prefix. */
  1549. apply_mask_ipv4 (p);
  1550. /* Set default distance by route type. */
  1551. if (distance == 0)
  1552. {
  1553. if ((unsigned)type >= array_size(route_info))
  1554. distance = 150;
  1555. else
  1556. distance = route_info[type].distance;
  1557. /* iBGP distance is 200. */
  1558. if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
  1559. distance = 200;
  1560. }
  1561. /* Lookup route node.*/
  1562. rn = route_node_get (table, (struct prefix *) p);
  1563. /* If same type of route are installed, treat it as a implicit
  1564. withdraw. */
  1565. RNODE_FOREACH_RIB (rn, rib)
  1566. {
  1567. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1568. continue;
  1569. if (rib->type != type)
  1570. continue;
  1571. if (rib->type != ZEBRA_ROUTE_CONNECT)
  1572. {
  1573. same = rib;
  1574. break;
  1575. }
  1576. /* Duplicate connected route comes in. */
  1577. else if ((nexthop = rib->nexthop) &&
  1578. nexthop->type == NEXTHOP_TYPE_IFINDEX &&
  1579. nexthop->ifindex == ifindex &&
  1580. !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1581. {
  1582. rib->refcnt++;
  1583. return 0 ;
  1584. }
  1585. }
  1586. /* Allocate new rib structure. */
  1587. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  1588. rib->type = type;
  1589. rib->distance = distance;
  1590. rib->flags = flags;
  1591. rib->metric = metric;
  1592. rib->table = vrf_id;
  1593. rib->nexthop_num = 0;
  1594. rib->uptime = time (NULL);
  1595. /* Nexthop settings. */
  1596. if (gate)
  1597. {
  1598. if (ifindex)
  1599. nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
  1600. else
  1601. nexthop_ipv4_add (rib, gate, src);
  1602. }
  1603. else
  1604. nexthop_ifindex_add (rib, ifindex);
  1605. /* If this route is kernel route, set FIB flag to the route. */
  1606. if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
  1607. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1608. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1609. /* Link new rib to node.*/
  1610. if (IS_ZEBRA_DEBUG_RIB)
  1611. zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib);
  1612. rib_addnode (rn, rib);
  1613. /* Free implicit route.*/
  1614. if (same)
  1615. {
  1616. if (IS_ZEBRA_DEBUG_RIB)
  1617. zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib);
  1618. rib_delnode (rn, same);
  1619. }
  1620. route_unlock_node (rn);
  1621. return 0;
  1622. }
  1623. /* This function dumps the contents of a given RIB entry into
  1624. * standard debug log. Calling function name and IP prefix in
  1625. * question are passed as 1st and 2nd arguments.
  1626. */
  1627. void _rib_dump (const char * func,
  1628. union prefix46constptr pp, const struct rib * rib)
  1629. {
  1630. const struct prefix *p = pp.p;
  1631. char straddr[INET6_ADDRSTRLEN];
  1632. struct nexthop *nexthop, *tnexthop;
  1633. int recursing;
  1634. inet_ntop (p->family, &p->u.prefix, straddr, INET6_ADDRSTRLEN);
  1635. zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr, p->prefixlen);
  1636. zlog_debug
  1637. (
  1638. "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
  1639. func,
  1640. rib->refcnt,
  1641. (unsigned long) rib->uptime,
  1642. rib->type,
  1643. rib->table
  1644. );
  1645. zlog_debug
  1646. (
  1647. "%s: metric == %u, distance == %u, flags == %u, status == %u",
  1648. func,
  1649. rib->metric,
  1650. rib->distance,
  1651. rib->flags,
  1652. rib->status
  1653. );
  1654. zlog_debug
  1655. (
  1656. "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
  1657. func,
  1658. rib->nexthop_num,
  1659. rib->nexthop_active_num,
  1660. rib->nexthop_fib_num
  1661. );
  1662. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1663. {
  1664. inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
  1665. zlog_debug
  1666. (
  1667. "%s: %s %s with flags %s%s%s",
  1668. func,
  1669. (recursing ? " NH" : "NH"),
  1670. straddr,
  1671. (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
  1672. (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
  1673. (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
  1674. );
  1675. }
  1676. zlog_debug ("%s: dump complete", func);
  1677. }
  1678. /* This is an exported helper to rtm_read() to dump the strange
  1679. * RIB entry found by rib_lookup_ipv4_route()
  1680. */
  1681. void rib_lookup_and_dump (struct prefix_ipv4 * p)
  1682. {
  1683. struct route_table *table;
  1684. struct route_node *rn;
  1685. struct rib *rib;
  1686. char prefix_buf[INET_ADDRSTRLEN];
  1687. /* Lookup table. */
  1688. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  1689. if (! table)
  1690. {
  1691. zlog_err ("%s: vrf_table() returned NULL", __func__);
  1692. return;
  1693. }
  1694. inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN);
  1695. /* Scan the RIB table for exactly matching RIB entry. */
  1696. rn = route_node_lookup (table, (struct prefix *) p);
  1697. /* No route for this prefix. */
  1698. if (! rn)
  1699. {
  1700. zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen);
  1701. return;
  1702. }
  1703. /* Unlock node. */
  1704. route_unlock_node (rn);
  1705. /* let's go */
  1706. RNODE_FOREACH_RIB (rn, rib)
  1707. {
  1708. zlog_debug
  1709. (
  1710. "%s: rn %p, rib %p: %s, %s",
  1711. __func__,
  1712. rn,
  1713. rib,
  1714. (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
  1715. (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
  1716. );
  1717. rib_dump (p, rib);
  1718. }
  1719. }
  1720. /* Check if requested address assignment will fail due to another
  1721. * route being installed by zebra in FIB already. Take necessary
  1722. * actions, if needed: remove such a route from FIB and deSELECT
  1723. * corresponding RIB entry. Then put affected RN into RIBQ head.
  1724. */
  1725. void rib_lookup_and_pushup (struct prefix_ipv4 * p)
  1726. {
  1727. struct route_table *table;
  1728. struct route_node *rn;
  1729. struct rib *rib;
  1730. unsigned changed = 0;
  1731. if (NULL == (table = vrf_table (AFI_IP, SAFI_UNICAST, 0)))
  1732. {
  1733. zlog_err ("%s: vrf_table() returned NULL", __func__);
  1734. return;
  1735. }
  1736. /* No matches would be the simplest case. */
  1737. if (NULL == (rn = route_node_lookup (table, (struct prefix *) p)))
  1738. return;
  1739. /* Unlock node. */
  1740. route_unlock_node (rn);
  1741. /* Check all RIB entries. In case any changes have to be done, requeue
  1742. * the RN into RIBQ head. If the routing message about the new connected
  1743. * route (generated by the IP address we are going to assign very soon)
  1744. * comes before the RIBQ is processed, the new RIB entry will join
  1745. * RIBQ record already on head. This is necessary for proper revalidation
  1746. * of the rest of the RIB.
  1747. */
  1748. RNODE_FOREACH_RIB (rn, rib)
  1749. {
  1750. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) &&
  1751. ! RIB_SYSTEM_ROUTE (rib))
  1752. {
  1753. changed = 1;
  1754. if (IS_ZEBRA_DEBUG_RIB)
  1755. {
  1756. char buf[INET_ADDRSTRLEN];
  1757. inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN);
  1758. zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__, buf, p->prefixlen);
  1759. rib_dump (&rn->p, rib);
  1760. }
  1761. rib_uninstall (rn, rib);
  1762. }
  1763. }
  1764. if (changed)
  1765. rib_queue_add (&zebrad, rn);
  1766. }
  1767. int
  1768. rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
  1769. {
  1770. struct route_table *table;
  1771. struct route_node *rn;
  1772. struct rib *same;
  1773. struct nexthop *nexthop;
  1774. /* Lookup table. */
  1775. table = vrf_table (AFI_IP, safi, 0);
  1776. if (! table)
  1777. return 0;
  1778. /* Make it sure prefixlen is applied to the prefix. */
  1779. apply_mask_ipv4 (p);
  1780. /* Set default distance by route type. */
  1781. if (rib->distance == 0)
  1782. {
  1783. rib->distance = route_info[rib->type].distance;
  1784. /* iBGP distance is 200. */
  1785. if (rib->type == ZEBRA_ROUTE_BGP
  1786. && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
  1787. rib->distance = 200;
  1788. }
  1789. /* Lookup route node.*/
  1790. rn = route_node_get (table, (struct prefix *) p);
  1791. /* If same type of route are installed, treat it as a implicit
  1792. withdraw. */
  1793. RNODE_FOREACH_RIB (rn, same)
  1794. {
  1795. if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
  1796. continue;
  1797. if (same->type == rib->type && same->table == rib->table
  1798. && same->type != ZEBRA_ROUTE_CONNECT)
  1799. break;
  1800. }
  1801. /* If this route is kernel route, set FIB flag to the route. */
  1802. if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
  1803. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1804. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1805. /* Link new rib to node.*/
  1806. rib_addnode (rn, rib);
  1807. if (IS_ZEBRA_DEBUG_RIB)
  1808. {
  1809. zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
  1810. __func__, rn, rib);
  1811. rib_dump (p, rib);
  1812. }
  1813. /* Free implicit route.*/
  1814. if (same)
  1815. {
  1816. if (IS_ZEBRA_DEBUG_RIB)
  1817. {
  1818. zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
  1819. __func__, rn, same);
  1820. rib_dump (p, same);
  1821. }
  1822. rib_delnode (rn, same);
  1823. }
  1824. route_unlock_node (rn);
  1825. return 0;
  1826. }
  1827. /* XXX factor with rib_delete_ipv6 */
  1828. int
  1829. rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  1830. struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
  1831. {
  1832. struct route_table *table;
  1833. struct route_node *rn;
  1834. struct rib *rib;
  1835. struct rib *fib = NULL;
  1836. struct rib *same = NULL;
  1837. struct nexthop *nexthop, *tnexthop;
  1838. int recursing;
  1839. char buf1[INET_ADDRSTRLEN];
  1840. char buf2[INET_ADDRSTRLEN];
  1841. /* Lookup table. */
  1842. table = vrf_table (AFI_IP, safi, 0);
  1843. if (! table)
  1844. return 0;
  1845. /* Apply mask. */
  1846. apply_mask_ipv4 (p);
  1847. if (IS_ZEBRA_DEBUG_KERNEL)
  1848. {
  1849. if (gate)
  1850. zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
  1851. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1852. p->prefixlen,
  1853. inet_ntoa (*gate),
  1854. ifindex);
  1855. else
  1856. zlog_debug ("rib_delete_ipv4(): route delete %s/%d ifindex %d",
  1857. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1858. p->prefixlen,
  1859. ifindex);
  1860. }
  1861. /* Lookup route node. */
  1862. rn = route_node_lookup (table, (struct prefix *) p);
  1863. if (! rn)
  1864. {
  1865. if (IS_ZEBRA_DEBUG_KERNEL)
  1866. {
  1867. if (gate)
  1868. zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
  1869. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1870. p->prefixlen,
  1871. inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
  1872. ifindex);
  1873. else
  1874. zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
  1875. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1876. p->prefixlen,
  1877. ifindex);
  1878. }
  1879. return ZEBRA_ERR_RTNOEXIST;
  1880. }
  1881. /* Lookup same type route. */
  1882. RNODE_FOREACH_RIB (rn, rib)
  1883. {
  1884. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1885. continue;
  1886. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1887. fib = rib;
  1888. if (rib->type != type)
  1889. continue;
  1890. if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
  1891. nexthop->type == NEXTHOP_TYPE_IFINDEX)
  1892. {
  1893. if (nexthop->ifindex != ifindex)
  1894. continue;
  1895. if (rib->refcnt)
  1896. {
  1897. rib->refcnt--;
  1898. route_unlock_node (rn);
  1899. route_unlock_node (rn);
  1900. return 0;
  1901. }
  1902. same = rib;
  1903. break;
  1904. }
  1905. /* Make sure that the route found has the same gateway. */
  1906. else
  1907. {
  1908. if (gate == NULL)
  1909. {
  1910. same = rib;
  1911. break;
  1912. }
  1913. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1914. if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate))
  1915. {
  1916. same = rib;
  1917. break;
  1918. }
  1919. if (same)
  1920. break;
  1921. }
  1922. }
  1923. /* If same type of route can't be found and this message is from
  1924. kernel. */
  1925. if (! same)
  1926. {
  1927. if (fib && type == ZEBRA_ROUTE_KERNEL)
  1928. {
  1929. /* Unset flags. */
  1930. for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
  1931. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1932. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  1933. }
  1934. else
  1935. {
  1936. if (IS_ZEBRA_DEBUG_KERNEL)
  1937. {
  1938. if (gate)
  1939. zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
  1940. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1941. p->prefixlen,
  1942. inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
  1943. ifindex,
  1944. type);
  1945. else
  1946. zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
  1947. inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
  1948. p->prefixlen,
  1949. ifindex,
  1950. type);
  1951. }
  1952. route_unlock_node (rn);
  1953. return ZEBRA_ERR_RTNOEXIST;
  1954. }
  1955. }
  1956. if (same)
  1957. rib_delnode (rn, same);
  1958. route_unlock_node (rn);
  1959. return 0;
  1960. }
  1961. /* Install static route into rib. */
  1962. static void
  1963. static_install_ipv4 (struct prefix *p, struct static_ipv4 *si)
  1964. {
  1965. struct rib *rib;
  1966. struct route_node *rn;
  1967. struct route_table *table;
  1968. /* Lookup table. */
  1969. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  1970. if (! table)
  1971. return;
  1972. /* Lookup existing route */
  1973. rn = route_node_get (table, p);
  1974. RNODE_FOREACH_RIB (rn, rib)
  1975. {
  1976. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  1977. continue;
  1978. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  1979. break;
  1980. }
  1981. if (rib)
  1982. {
  1983. /* Same distance static route is there. Update it with new
  1984. nexthop. */
  1985. route_unlock_node (rn);
  1986. switch (si->type)
  1987. {
  1988. case STATIC_IPV4_GATEWAY:
  1989. nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
  1990. break;
  1991. case STATIC_IPV4_IFNAME:
  1992. nexthop_ifname_add (rib, si->gate.ifname);
  1993. break;
  1994. case STATIC_IPV4_BLACKHOLE:
  1995. nexthop_blackhole_add (rib);
  1996. break;
  1997. }
  1998. rib_queue_add (&zebrad, rn);
  1999. }
  2000. else
  2001. {
  2002. /* This is new static route. */
  2003. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  2004. rib->type = ZEBRA_ROUTE_STATIC;
  2005. rib->distance = si->distance;
  2006. rib->metric = 0;
  2007. rib->table = zebrad.rtm_table_default;
  2008. rib->nexthop_num = 0;
  2009. switch (si->type)
  2010. {
  2011. case STATIC_IPV4_GATEWAY:
  2012. nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
  2013. break;
  2014. case STATIC_IPV4_IFNAME:
  2015. nexthop_ifname_add (rib, si->gate.ifname);
  2016. break;
  2017. case STATIC_IPV4_BLACKHOLE:
  2018. nexthop_blackhole_add (rib);
  2019. break;
  2020. }
  2021. /* Save the flags of this static routes (reject, blackhole) */
  2022. rib->flags = si->flags;
  2023. /* Link this rib to the tree. */
  2024. rib_addnode (rn, rib);
  2025. }
  2026. }
  2027. static int
  2028. static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si)
  2029. {
  2030. if (nexthop->type == NEXTHOP_TYPE_IPV4
  2031. && si->type == STATIC_IPV4_GATEWAY
  2032. && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4))
  2033. return 1;
  2034. if (nexthop->type == NEXTHOP_TYPE_IFNAME
  2035. && si->type == STATIC_IPV4_IFNAME
  2036. && strcmp (nexthop->ifname, si->gate.ifname) == 0)
  2037. return 1;
  2038. if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
  2039. && si->type == STATIC_IPV4_BLACKHOLE)
  2040. return 1;
  2041. return 0;
  2042. }
  2043. /* Uninstall static route from RIB. */
  2044. static void
  2045. static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si)
  2046. {
  2047. struct route_node *rn;
  2048. struct rib *rib;
  2049. struct nexthop *nexthop;
  2050. struct route_table *table;
  2051. /* Lookup table. */
  2052. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  2053. if (! table)
  2054. return;
  2055. /* Lookup existing route with type and distance. */
  2056. rn = route_node_lookup (table, p);
  2057. if (! rn)
  2058. return;
  2059. RNODE_FOREACH_RIB (rn, rib)
  2060. {
  2061. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2062. continue;
  2063. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  2064. break;
  2065. }
  2066. if (! rib)
  2067. {
  2068. route_unlock_node (rn);
  2069. return;
  2070. }
  2071. /* Lookup nexthop. */
  2072. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  2073. if (static_ipv4_nexthop_same (nexthop, si))
  2074. break;
  2075. /* Can't find nexthop. */
  2076. if (! nexthop)
  2077. {
  2078. route_unlock_node (rn);
  2079. return;
  2080. }
  2081. /* Check nexthop. */
  2082. if (rib->nexthop_num == 1)
  2083. rib_delnode (rn, rib);
  2084. else
  2085. {
  2086. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  2087. rib_uninstall (rn, rib);
  2088. nexthop_delete (rib, nexthop);
  2089. nexthop_free (nexthop);
  2090. rib_queue_add (&zebrad, rn);
  2091. }
  2092. /* Unlock node. */
  2093. route_unlock_node (rn);
  2094. }
  2095. /* Add static route into static route configuration. */
  2096. int
  2097. static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  2098. u_char flags, u_char distance, u_int32_t vrf_id)
  2099. {
  2100. u_char type = 0;
  2101. struct route_node *rn;
  2102. struct static_ipv4 *si;
  2103. struct static_ipv4 *pp;
  2104. struct static_ipv4 *cp;
  2105. struct static_ipv4 *update = NULL;
  2106. struct route_table *stable;
  2107. /* Lookup table. */
  2108. stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
  2109. if (! stable)
  2110. return -1;
  2111. /* Lookup static route prefix. */
  2112. rn = route_node_get (stable, p);
  2113. /* Make flags. */
  2114. if (gate)
  2115. type = STATIC_IPV4_GATEWAY;
  2116. else if (ifname)
  2117. type = STATIC_IPV4_IFNAME;
  2118. else
  2119. type = STATIC_IPV4_BLACKHOLE;
  2120. /* Do nothing if there is a same static route. */
  2121. for (si = rn->info; si; si = si->next)
  2122. {
  2123. if (type == si->type
  2124. && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
  2125. && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
  2126. {
  2127. if (distance == si->distance)
  2128. {
  2129. route_unlock_node (rn);
  2130. return 0;
  2131. }
  2132. else
  2133. update = si;
  2134. }
  2135. }
  2136. /* Distance changed. */
  2137. if (update)
  2138. static_delete_ipv4 (p, gate, ifname, update->distance, vrf_id);
  2139. /* Make new static route structure. */
  2140. si = XCALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4));
  2141. si->type = type;
  2142. si->distance = distance;
  2143. si->flags = flags;
  2144. if (gate)
  2145. si->gate.ipv4 = *gate;
  2146. if (ifname)
  2147. si->gate.ifname = XSTRDUP (0, ifname);
  2148. /* Add new static route information to the tree with sort by
  2149. distance value and gateway address. */
  2150. for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
  2151. {
  2152. if (si->distance < cp->distance)
  2153. break;
  2154. if (si->distance > cp->distance)
  2155. continue;
  2156. if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
  2157. {
  2158. if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr))
  2159. break;
  2160. if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr))
  2161. continue;
  2162. }
  2163. }
  2164. /* Make linked list. */
  2165. if (pp)
  2166. pp->next = si;
  2167. else
  2168. rn->info = si;
  2169. if (cp)
  2170. cp->prev = si;
  2171. si->prev = pp;
  2172. si->next = cp;
  2173. /* Install into rib. */
  2174. static_install_ipv4 (p, si);
  2175. return 1;
  2176. }
  2177. /* Delete static route from static route configuration. */
  2178. int
  2179. static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
  2180. u_char distance, u_int32_t vrf_id)
  2181. {
  2182. u_char type = 0;
  2183. struct route_node *rn;
  2184. struct static_ipv4 *si;
  2185. struct route_table *stable;
  2186. /* Lookup table. */
  2187. stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
  2188. if (! stable)
  2189. return -1;
  2190. /* Lookup static route prefix. */
  2191. rn = route_node_lookup (stable, p);
  2192. if (! rn)
  2193. return 0;
  2194. /* Make flags. */
  2195. if (gate)
  2196. type = STATIC_IPV4_GATEWAY;
  2197. else if (ifname)
  2198. type = STATIC_IPV4_IFNAME;
  2199. else
  2200. type = STATIC_IPV4_BLACKHOLE;
  2201. /* Find same static route is the tree */
  2202. for (si = rn->info; si; si = si->next)
  2203. if (type == si->type
  2204. && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
  2205. && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
  2206. break;
  2207. /* Can't find static route. */
  2208. if (! si)
  2209. {
  2210. route_unlock_node (rn);
  2211. return 0;
  2212. }
  2213. /* Install into rib. */
  2214. static_uninstall_ipv4 (p, si);
  2215. /* Unlink static route from linked list. */
  2216. if (si->prev)
  2217. si->prev->next = si->next;
  2218. else
  2219. rn->info = si->next;
  2220. if (si->next)
  2221. si->next->prev = si->prev;
  2222. route_unlock_node (rn);
  2223. /* Free static route configuration. */
  2224. if (ifname)
  2225. XFREE (0, si->gate.ifname);
  2226. XFREE (MTYPE_STATIC_IPV4, si);
  2227. route_unlock_node (rn);
  2228. return 1;
  2229. }
  2230. #ifdef HAVE_IPV6
  2231. static int
  2232. rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
  2233. struct in6_addr *gate, unsigned int ifindex, int table)
  2234. {
  2235. if (type == ZEBRA_ROUTE_CONNECT && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)) {
  2236. #if defined (MUSICA) || defined (LINUX)
  2237. /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
  2238. if (p->prefixlen == 96)
  2239. return 0;
  2240. #endif /* MUSICA */
  2241. return 1;
  2242. }
  2243. if (type == ZEBRA_ROUTE_KERNEL && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)
  2244. && p->prefixlen == 96 && gate && IN6_IS_ADDR_UNSPECIFIED (gate))
  2245. {
  2246. kernel_delete_ipv6_old (p, gate, ifindex, 0, table);
  2247. return 1;
  2248. }
  2249. return 0;
  2250. }
  2251. int
  2252. rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  2253. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
  2254. u_int32_t metric, u_char distance, safi_t safi)
  2255. {
  2256. struct rib *rib;
  2257. struct rib *same = NULL;
  2258. struct route_table *table;
  2259. struct route_node *rn;
  2260. struct nexthop *nexthop;
  2261. /* Lookup table. */
  2262. table = vrf_table (AFI_IP6, safi, 0);
  2263. if (! table)
  2264. return 0;
  2265. /* Make sure mask is applied. */
  2266. apply_mask_ipv6 (p);
  2267. /* Set default distance by route type. */
  2268. if (!distance)
  2269. distance = route_info[type].distance;
  2270. if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
  2271. distance = 200;
  2272. /* Filter bogus route. */
  2273. if (rib_bogus_ipv6 (type, p, gate, ifindex, 0))
  2274. return 0;
  2275. /* Lookup route node.*/
  2276. rn = route_node_get (table, (struct prefix *) p);
  2277. /* If same type of route are installed, treat it as a implicit
  2278. withdraw. */
  2279. RNODE_FOREACH_RIB (rn, rib)
  2280. {
  2281. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2282. continue;
  2283. if (rib->type != type)
  2284. continue;
  2285. if (rib->type != ZEBRA_ROUTE_CONNECT)
  2286. {
  2287. same = rib;
  2288. break;
  2289. }
  2290. else if ((nexthop = rib->nexthop) &&
  2291. nexthop->type == NEXTHOP_TYPE_IFINDEX &&
  2292. nexthop->ifindex == ifindex)
  2293. {
  2294. rib->refcnt++;
  2295. return 0;
  2296. }
  2297. }
  2298. /* Allocate new rib structure. */
  2299. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  2300. rib->type = type;
  2301. rib->distance = distance;
  2302. rib->flags = flags;
  2303. rib->metric = metric;
  2304. rib->table = vrf_id;
  2305. rib->nexthop_num = 0;
  2306. rib->uptime = time (NULL);
  2307. /* Nexthop settings. */
  2308. if (gate)
  2309. {
  2310. if (ifindex)
  2311. nexthop_ipv6_ifindex_add (rib, gate, ifindex);
  2312. else
  2313. nexthop_ipv6_add (rib, gate);
  2314. }
  2315. else
  2316. nexthop_ifindex_add (rib, ifindex);
  2317. /* If this route is kernel route, set FIB flag to the route. */
  2318. if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
  2319. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  2320. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  2321. /* Link new rib to node.*/
  2322. rib_addnode (rn, rib);
  2323. if (IS_ZEBRA_DEBUG_RIB)
  2324. {
  2325. zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
  2326. __func__, rn, rib);
  2327. rib_dump (p, rib);
  2328. }
  2329. /* Free implicit route.*/
  2330. if (same)
  2331. {
  2332. if (IS_ZEBRA_DEBUG_RIB)
  2333. {
  2334. zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
  2335. __func__, rn, same);
  2336. rib_dump (p, same);
  2337. }
  2338. rib_delnode (rn, same);
  2339. }
  2340. route_unlock_node (rn);
  2341. return 0;
  2342. }
  2343. /* XXX factor with rib_delete_ipv6 */
  2344. int
  2345. rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  2346. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
  2347. {
  2348. struct route_table *table;
  2349. struct route_node *rn;
  2350. struct rib *rib;
  2351. struct rib *fib = NULL;
  2352. struct rib *same = NULL;
  2353. struct nexthop *nexthop, *tnexthop;
  2354. int recursing;
  2355. char buf1[INET6_ADDRSTRLEN];
  2356. char buf2[INET6_ADDRSTRLEN];
  2357. /* Apply mask. */
  2358. apply_mask_ipv6 (p);
  2359. /* Lookup table. */
  2360. table = vrf_table (AFI_IP6, safi, 0);
  2361. if (! table)
  2362. return 0;
  2363. /* Lookup route node. */
  2364. rn = route_node_lookup (table, (struct prefix *) p);
  2365. if (! rn)
  2366. {
  2367. if (IS_ZEBRA_DEBUG_KERNEL)
  2368. {
  2369. if (gate)
  2370. zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
  2371. inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
  2372. p->prefixlen,
  2373. inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
  2374. ifindex);
  2375. else
  2376. zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
  2377. inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
  2378. p->prefixlen,
  2379. ifindex);
  2380. }
  2381. return ZEBRA_ERR_RTNOEXIST;
  2382. }
  2383. /* Lookup same type route. */
  2384. RNODE_FOREACH_RIB (rn, rib)
  2385. {
  2386. if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
  2387. continue;
  2388. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  2389. fib = rib;
  2390. if (rib->type != type)
  2391. continue;
  2392. if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
  2393. nexthop->type == NEXTHOP_TYPE_IFINDEX)
  2394. {
  2395. if (nexthop->ifindex != ifindex)
  2396. continue;
  2397. if (rib->refcnt)
  2398. {
  2399. rib->refcnt--;
  2400. route_unlock_node (rn);
  2401. route_unlock_node (rn);
  2402. return 0;
  2403. }
  2404. same = rib;
  2405. break;
  2406. }
  2407. /* Make sure that the route found has the same gateway. */
  2408. else
  2409. {
  2410. if (gate == NULL)
  2411. {
  2412. same = rib;
  2413. break;
  2414. }
  2415. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  2416. if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
  2417. {
  2418. same = rib;
  2419. break;
  2420. }
  2421. if (same)
  2422. break;
  2423. }
  2424. }
  2425. /* If same type of route can't be found and this message is from
  2426. kernel. */
  2427. if (! same)
  2428. {
  2429. if (fib && type == ZEBRA_ROUTE_KERNEL)
  2430. {
  2431. /* Unset flags. */
  2432. for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
  2433. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  2434. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  2435. }
  2436. else
  2437. {
  2438. if (IS_ZEBRA_DEBUG_KERNEL)
  2439. {
  2440. if (gate)
  2441. zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
  2442. inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
  2443. p->prefixlen,
  2444. inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
  2445. ifindex,
  2446. type);
  2447. else
  2448. zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
  2449. inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
  2450. p->prefixlen,
  2451. ifindex,
  2452. type);
  2453. }
  2454. route_unlock_node (rn);
  2455. return ZEBRA_ERR_RTNOEXIST;
  2456. }
  2457. }
  2458. if (same)
  2459. rib_delnode (rn, same);
  2460. route_unlock_node (rn);
  2461. return 0;
  2462. }
  2463. /* Install static route into rib. */
  2464. static void
  2465. static_install_ipv6 (struct prefix *p, struct static_ipv6 *si)
  2466. {
  2467. struct rib *rib;
  2468. struct route_table *table;
  2469. struct route_node *rn;
  2470. /* Lookup table. */
  2471. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  2472. if (! table)
  2473. return;
  2474. /* Lookup existing route */
  2475. rn = route_node_get (table, p);
  2476. RNODE_FOREACH_RIB (rn, rib)
  2477. {
  2478. if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
  2479. continue;
  2480. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  2481. break;
  2482. }
  2483. if (rib)
  2484. {
  2485. /* Same distance static route is there. Update it with new
  2486. nexthop. */
  2487. route_unlock_node (rn);
  2488. switch (si->type)
  2489. {
  2490. case STATIC_IPV6_GATEWAY:
  2491. nexthop_ipv6_add (rib, &si->ipv6);
  2492. break;
  2493. case STATIC_IPV6_IFNAME:
  2494. nexthop_ifname_add (rib, si->ifname);
  2495. break;
  2496. case STATIC_IPV6_GATEWAY_IFNAME:
  2497. nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
  2498. break;
  2499. }
  2500. rib_queue_add (&zebrad, rn);
  2501. }
  2502. else
  2503. {
  2504. /* This is new static route. */
  2505. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  2506. rib->type = ZEBRA_ROUTE_STATIC;
  2507. rib->distance = si->distance;
  2508. rib->metric = 0;
  2509. rib->table = zebrad.rtm_table_default;
  2510. rib->nexthop_num = 0;
  2511. switch (si->type)
  2512. {
  2513. case STATIC_IPV6_GATEWAY:
  2514. nexthop_ipv6_add (rib, &si->ipv6);
  2515. break;
  2516. case STATIC_IPV6_IFNAME:
  2517. nexthop_ifname_add (rib, si->ifname);
  2518. break;
  2519. case STATIC_IPV6_GATEWAY_IFNAME:
  2520. nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
  2521. break;
  2522. }
  2523. /* Save the flags of this static routes (reject, blackhole) */
  2524. rib->flags = si->flags;
  2525. /* Link this rib to the tree. */
  2526. rib_addnode (rn, rib);
  2527. }
  2528. }
  2529. static int
  2530. static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si)
  2531. {
  2532. if (nexthop->type == NEXTHOP_TYPE_IPV6
  2533. && si->type == STATIC_IPV6_GATEWAY
  2534. && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6))
  2535. return 1;
  2536. if (nexthop->type == NEXTHOP_TYPE_IFNAME
  2537. && si->type == STATIC_IPV6_IFNAME
  2538. && strcmp (nexthop->ifname, si->ifname) == 0)
  2539. return 1;
  2540. if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  2541. && si->type == STATIC_IPV6_GATEWAY_IFNAME
  2542. && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)
  2543. && strcmp (nexthop->ifname, si->ifname) == 0)
  2544. return 1;
  2545. return 0;
  2546. }
  2547. static void
  2548. static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si)
  2549. {
  2550. struct route_table *table;
  2551. struct route_node *rn;
  2552. struct rib *rib;
  2553. struct nexthop *nexthop;
  2554. /* Lookup table. */
  2555. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  2556. if (! table)
  2557. return;
  2558. /* Lookup existing route with type and distance. */
  2559. rn = route_node_lookup (table, (struct prefix *) p);
  2560. if (! rn)
  2561. return;
  2562. RNODE_FOREACH_RIB (rn, rib)
  2563. {
  2564. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2565. continue;
  2566. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  2567. break;
  2568. }
  2569. if (! rib)
  2570. {
  2571. route_unlock_node (rn);
  2572. return;
  2573. }
  2574. /* Lookup nexthop. */
  2575. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  2576. if (static_ipv6_nexthop_same (nexthop, si))
  2577. break;
  2578. /* Can't find nexthop. */
  2579. if (! nexthop)
  2580. {
  2581. route_unlock_node (rn);
  2582. return;
  2583. }
  2584. /* Check nexthop. */
  2585. if (rib->nexthop_num == 1)
  2586. {
  2587. rib_delnode (rn, rib);
  2588. }
  2589. else
  2590. {
  2591. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  2592. rib_uninstall (rn, rib);
  2593. nexthop_delete (rib, nexthop);
  2594. nexthop_free (nexthop);
  2595. rib_queue_add (&zebrad, rn);
  2596. }
  2597. /* Unlock node. */
  2598. route_unlock_node (rn);
  2599. }
  2600. /* Add static route into static route configuration. */
  2601. int
  2602. static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  2603. const char *ifname, u_char flags, u_char distance,
  2604. u_int32_t vrf_id)
  2605. {
  2606. struct route_node *rn;
  2607. struct static_ipv6 *si;
  2608. struct static_ipv6 *pp;
  2609. struct static_ipv6 *cp;
  2610. struct route_table *stable;
  2611. /* Lookup table. */
  2612. stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  2613. if (! stable)
  2614. return -1;
  2615. if (!gate &&
  2616. (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME))
  2617. return -1;
  2618. if (!ifname &&
  2619. (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME))
  2620. return -1;
  2621. /* Lookup static route prefix. */
  2622. rn = route_node_get (stable, p);
  2623. /* Do nothing if there is a same static route. */
  2624. for (si = rn->info; si; si = si->next)
  2625. {
  2626. if (distance == si->distance
  2627. && type == si->type
  2628. && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
  2629. && (! ifname || strcmp (ifname, si->ifname) == 0))
  2630. {
  2631. route_unlock_node (rn);
  2632. return 0;
  2633. }
  2634. }
  2635. /* Make new static route structure. */
  2636. si = XCALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6));
  2637. si->type = type;
  2638. si->distance = distance;
  2639. si->flags = flags;
  2640. switch (type)
  2641. {
  2642. case STATIC_IPV6_GATEWAY:
  2643. si->ipv6 = *gate;
  2644. break;
  2645. case STATIC_IPV6_IFNAME:
  2646. si->ifname = XSTRDUP (0, ifname);
  2647. break;
  2648. case STATIC_IPV6_GATEWAY_IFNAME:
  2649. si->ipv6 = *gate;
  2650. si->ifname = XSTRDUP (0, ifname);
  2651. break;
  2652. }
  2653. /* Add new static route information to the tree with sort by
  2654. distance value and gateway address. */
  2655. for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
  2656. {
  2657. if (si->distance < cp->distance)
  2658. break;
  2659. if (si->distance > cp->distance)
  2660. continue;
  2661. }
  2662. /* Make linked list. */
  2663. if (pp)
  2664. pp->next = si;
  2665. else
  2666. rn->info = si;
  2667. if (cp)
  2668. cp->prev = si;
  2669. si->prev = pp;
  2670. si->next = cp;
  2671. /* Install into rib. */
  2672. static_install_ipv6 (p, si);
  2673. return 1;
  2674. }
  2675. /* Delete static route from static route configuration. */
  2676. int
  2677. static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  2678. const char *ifname, u_char distance, u_int32_t vrf_id)
  2679. {
  2680. struct route_node *rn;
  2681. struct static_ipv6 *si;
  2682. struct route_table *stable;
  2683. /* Lookup table. */
  2684. stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  2685. if (! stable)
  2686. return -1;
  2687. /* Lookup static route prefix. */
  2688. rn = route_node_lookup (stable, p);
  2689. if (! rn)
  2690. return 0;
  2691. /* Find same static route is the tree */
  2692. for (si = rn->info; si; si = si->next)
  2693. if (distance == si->distance
  2694. && type == si->type
  2695. && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
  2696. && (! ifname || strcmp (ifname, si->ifname) == 0))
  2697. break;
  2698. /* Can't find static route. */
  2699. if (! si)
  2700. {
  2701. route_unlock_node (rn);
  2702. return 0;
  2703. }
  2704. /* Install into rib. */
  2705. static_uninstall_ipv6 (p, si);
  2706. /* Unlink static route from linked list. */
  2707. if (si->prev)
  2708. si->prev->next = si->next;
  2709. else
  2710. rn->info = si->next;
  2711. if (si->next)
  2712. si->next->prev = si->prev;
  2713. /* Free static route configuration. */
  2714. if (ifname)
  2715. XFREE (0, si->ifname);
  2716. XFREE (MTYPE_STATIC_IPV6, si);
  2717. return 1;
  2718. }
  2719. #endif /* HAVE_IPV6 */
  2720. /* RIB update function. */
  2721. void
  2722. rib_update (void)
  2723. {
  2724. struct route_node *rn;
  2725. struct route_table *table;
  2726. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  2727. if (table)
  2728. for (rn = route_top (table); rn; rn = route_next (rn))
  2729. if (rnode_to_ribs (rn))
  2730. rib_queue_add (&zebrad, rn);
  2731. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  2732. if (table)
  2733. for (rn = route_top (table); rn; rn = route_next (rn))
  2734. if (rnode_to_ribs (rn))
  2735. rib_queue_add (&zebrad, rn);
  2736. }
  2737. /* Remove all routes which comes from non main table. */
  2738. static void
  2739. rib_weed_table (struct route_table *table)
  2740. {
  2741. struct route_node *rn;
  2742. struct rib *rib;
  2743. struct rib *next;
  2744. if (table)
  2745. for (rn = route_top (table); rn; rn = route_next (rn))
  2746. RNODE_FOREACH_RIB_SAFE (rn, rib, next)
  2747. {
  2748. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2749. continue;
  2750. if (rib->table != zebrad.rtm_table_default &&
  2751. rib->table != RT_TABLE_MAIN)
  2752. rib_delnode (rn, rib);
  2753. }
  2754. }
  2755. /* Delete all routes from non main table. */
  2756. void
  2757. rib_weed_tables (void)
  2758. {
  2759. rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  2760. rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  2761. }
  2762. /* Delete self installed routes after zebra is relaunched. */
  2763. static void
  2764. rib_sweep_table (struct route_table *table)
  2765. {
  2766. struct route_node *rn;
  2767. struct rib *rib;
  2768. struct rib *next;
  2769. int ret = 0;
  2770. if (table)
  2771. for (rn = route_top (table); rn; rn = route_next (rn))
  2772. RNODE_FOREACH_RIB_SAFE (rn, rib, next)
  2773. {
  2774. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2775. continue;
  2776. if (rib->type == ZEBRA_ROUTE_KERNEL &&
  2777. CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
  2778. {
  2779. ret = rib_uninstall_kernel (rn, rib);
  2780. if (! ret)
  2781. rib_delnode (rn, rib);
  2782. }
  2783. }
  2784. }
  2785. /* Sweep all RIB tables. */
  2786. void
  2787. rib_sweep_route (void)
  2788. {
  2789. rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  2790. rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  2791. }
  2792. /* Remove specific by protocol routes from 'table'. */
  2793. static unsigned long
  2794. rib_score_proto_table (u_char proto, struct route_table *table)
  2795. {
  2796. struct route_node *rn;
  2797. struct rib *rib;
  2798. struct rib *next;
  2799. unsigned long n = 0;
  2800. if (table)
  2801. for (rn = route_top (table); rn; rn = route_next (rn))
  2802. RNODE_FOREACH_RIB_SAFE (rn, rib, next)
  2803. {
  2804. if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
  2805. continue;
  2806. if (rib->type == proto)
  2807. {
  2808. rib_delnode (rn, rib);
  2809. n++;
  2810. }
  2811. }
  2812. return n;
  2813. }
  2814. /* Remove specific by protocol routes. */
  2815. unsigned long
  2816. rib_score_proto (u_char proto)
  2817. {
  2818. return rib_score_proto_table (proto, vrf_table (AFI_IP, SAFI_UNICAST, 0))
  2819. +rib_score_proto_table (proto, vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  2820. }
  2821. /* Close RIB and clean up kernel routes. */
  2822. static void
  2823. rib_close_table (struct route_table *table)
  2824. {
  2825. struct route_node *rn;
  2826. struct rib *rib;
  2827. if (table)
  2828. for (rn = route_top (table); rn; rn = route_next (rn))
  2829. RNODE_FOREACH_RIB (rn, rib)
  2830. {
  2831. if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  2832. continue;
  2833. zfpm_trigger_update (rn, NULL);
  2834. if (! RIB_SYSTEM_ROUTE (rib))
  2835. rib_uninstall_kernel (rn, rib);
  2836. }
  2837. }
  2838. /* Close all RIB tables. */
  2839. void
  2840. rib_close (void)
  2841. {
  2842. rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  2843. rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  2844. }
  2845. /* Routing information base initialize. */
  2846. void
  2847. rib_init (void)
  2848. {
  2849. rib_queue_init (&zebrad);
  2850. /* VRF initialization. */
  2851. vrf_init ();
  2852. }
  2853. /*
  2854. * vrf_id_get_next
  2855. *
  2856. * Get the first vrf id that is greater than the given vrf id if any.
  2857. *
  2858. * Returns TRUE if a vrf id was found, FALSE otherwise.
  2859. */
  2860. static inline int
  2861. vrf_id_get_next (uint32_t id, uint32_t *next_id_p)
  2862. {
  2863. while (++id < vector_active (vrf_vector))
  2864. {
  2865. if (vrf_lookup (id))
  2866. {
  2867. *next_id_p = id;
  2868. return 1;
  2869. }
  2870. }
  2871. return 0;
  2872. }
  2873. /*
  2874. * rib_tables_iter_next
  2875. *
  2876. * Returns the next table in the iteration.
  2877. */
  2878. struct route_table *
  2879. rib_tables_iter_next (rib_tables_iter_t *iter)
  2880. {
  2881. struct route_table *table;
  2882. /*
  2883. * Array that helps us go over all AFI/SAFI combinations via one
  2884. * index.
  2885. */
  2886. static struct {
  2887. afi_t afi;
  2888. safi_t safi;
  2889. } afi_safis[] = {
  2890. { AFI_IP, SAFI_UNICAST },
  2891. { AFI_IP, SAFI_MULTICAST },
  2892. { AFI_IP6, SAFI_UNICAST },
  2893. { AFI_IP6, SAFI_MULTICAST },
  2894. };
  2895. table = NULL;
  2896. switch (iter->state)
  2897. {
  2898. case RIB_TABLES_ITER_S_INIT:
  2899. iter->vrf_id = 0;
  2900. iter->afi_safi_ix = -1;
  2901. /* Fall through */
  2902. case RIB_TABLES_ITER_S_ITERATING:
  2903. iter->afi_safi_ix++;
  2904. while (1)
  2905. {
  2906. while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
  2907. {
  2908. table = vrf_table (afi_safis[iter->afi_safi_ix].afi,
  2909. afi_safis[iter->afi_safi_ix].safi,
  2910. iter->vrf_id);
  2911. if (table)
  2912. break;
  2913. iter->afi_safi_ix++;
  2914. }
  2915. /*
  2916. * Found another table in this vrf.
  2917. */
  2918. if (table)
  2919. break;
  2920. /*
  2921. * Done with all tables in the current vrf, go to the next
  2922. * one.
  2923. */
  2924. if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
  2925. break;
  2926. iter->afi_safi_ix = 0;
  2927. }
  2928. break;
  2929. case RIB_TABLES_ITER_S_DONE:
  2930. return NULL;
  2931. }
  2932. if (table)
  2933. iter->state = RIB_TABLES_ITER_S_ITERATING;
  2934. else
  2935. iter->state = RIB_TABLES_ITER_S_DONE;
  2936. return table;
  2937. }