zebra_rib.c 86 KB

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