zebra_rib.c 84 KB

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