zebra_rib.c 84 KB

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