zebra_rib.c 79 KB

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