zebra_rib.c 83 KB

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