zebra_rib.c 85 KB

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