zebra_rib.c 84 KB

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