zebra_rib.c 83 KB

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