zebra_rib.c 87 KB

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