zebra_rib.c 85 KB

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