zebra_rib.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  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 "zebra/rib.h"
  31. #include "zebra/rt.h"
  32. #include "zebra/zserv.h"
  33. #include "zebra/redistribute.h"
  34. #include "zebra/debug.h"
  35. /* Default rtm_table for all clients */
  36. extern struct zebra_t zebrad;
  37. /* Each route type's string and default distance value. */
  38. struct
  39. {
  40. int key;
  41. int distance;
  42. } route_info[] =
  43. {
  44. {ZEBRA_ROUTE_SYSTEM, 0},
  45. {ZEBRA_ROUTE_KERNEL, 0},
  46. {ZEBRA_ROUTE_CONNECT, 0},
  47. {ZEBRA_ROUTE_STATIC, 1},
  48. {ZEBRA_ROUTE_RIP, 120},
  49. {ZEBRA_ROUTE_RIPNG, 120},
  50. {ZEBRA_ROUTE_OSPF, 110},
  51. {ZEBRA_ROUTE_OSPF6, 110},
  52. {ZEBRA_ROUTE_ISIS, 115},
  53. {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */}
  54. };
  55. /* Vector for routing table. */
  56. vector vrf_vector;
  57. /* Allocate new VRF. */
  58. struct vrf *
  59. vrf_alloc (char *name)
  60. {
  61. struct vrf *vrf;
  62. vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
  63. /* Put name. */
  64. if (name)
  65. vrf->name = XSTRDUP (MTYPE_VRF_NAME, name);
  66. /* Allocate routing table and static table. */
  67. vrf->table[AFI_IP][SAFI_UNICAST] = route_table_init ();
  68. vrf->table[AFI_IP6][SAFI_UNICAST] = route_table_init ();
  69. vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
  70. vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
  71. return vrf;
  72. }
  73. /* Free VRF. */
  74. void
  75. vrf_free (struct vrf *vrf)
  76. {
  77. if (vrf->name)
  78. XFREE (MTYPE_VRF_NAME, vrf->name);
  79. XFREE (MTYPE_VRF, vrf);
  80. }
  81. /* Lookup VRF by identifier. */
  82. struct vrf *
  83. vrf_lookup (u_int32_t id)
  84. {
  85. return vector_lookup (vrf_vector, id);
  86. }
  87. /* Lookup VRF by name. */
  88. struct vrf *
  89. vrf_lookup_by_name (char *name)
  90. {
  91. int i;
  92. struct vrf *vrf;
  93. for (i = 0; i < vector_max (vrf_vector); i++)
  94. if ((vrf = vector_slot (vrf_vector, i)) != NULL)
  95. if (vrf->name && name && strcmp (vrf->name, name) == 0)
  96. return vrf;
  97. return NULL;
  98. }
  99. /* Initialize VRF. */
  100. void
  101. vrf_init ()
  102. {
  103. struct vrf *default_table;
  104. /* Allocate VRF vector. */
  105. vrf_vector = vector_init (1);
  106. /* Allocate default main table. */
  107. default_table = vrf_alloc ("Default-IP-Routing-Table");
  108. /* Default table index must be 0. */
  109. vector_set_index (vrf_vector, 0, default_table);
  110. }
  111. /* Lookup route table. */
  112. struct route_table *
  113. vrf_table (afi_t afi, safi_t safi, u_int32_t id)
  114. {
  115. struct vrf *vrf;
  116. vrf = vrf_lookup (id);
  117. if (! vrf)
  118. return NULL;
  119. return vrf->table[afi][safi];
  120. }
  121. /* Lookup static route table. */
  122. struct route_table *
  123. vrf_static_table (afi_t afi, safi_t safi, u_int32_t id)
  124. {
  125. struct vrf *vrf;
  126. vrf = vrf_lookup (id);
  127. if (! vrf)
  128. return NULL;
  129. return vrf->stable[afi][safi];
  130. }
  131. /* Add nexthop to the end of the list. */
  132. void
  133. nexthop_add (struct rib *rib, struct nexthop *nexthop)
  134. {
  135. struct nexthop *last;
  136. for (last = rib->nexthop; last && last->next; last = last->next)
  137. ;
  138. if (last)
  139. last->next = nexthop;
  140. else
  141. rib->nexthop = nexthop;
  142. nexthop->prev = last;
  143. rib->nexthop_num++;
  144. }
  145. /* Delete specified nexthop from the list. */
  146. void
  147. nexthop_delete (struct rib *rib, struct nexthop *nexthop)
  148. {
  149. if (nexthop->next)
  150. nexthop->next->prev = nexthop->prev;
  151. if (nexthop->prev)
  152. nexthop->prev->next = nexthop->next;
  153. else
  154. rib->nexthop = nexthop->next;
  155. rib->nexthop_num--;
  156. }
  157. /* Free nexthop. */
  158. void
  159. nexthop_free (struct nexthop *nexthop)
  160. {
  161. if (nexthop->ifname)
  162. XFREE (0, nexthop->ifname);
  163. XFREE (MTYPE_NEXTHOP, nexthop);
  164. }
  165. struct nexthop *
  166. nexthop_ifindex_add (struct rib *rib, unsigned int ifindex)
  167. {
  168. struct nexthop *nexthop;
  169. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  170. memset (nexthop, 0, sizeof (struct nexthop));
  171. nexthop->type = NEXTHOP_TYPE_IFINDEX;
  172. nexthop->ifindex = ifindex;
  173. nexthop_add (rib, nexthop);
  174. return nexthop;
  175. }
  176. struct nexthop *
  177. nexthop_ifname_add (struct rib *rib, char *ifname)
  178. {
  179. struct nexthop *nexthop;
  180. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  181. memset (nexthop, 0, sizeof (struct nexthop));
  182. nexthop->type = NEXTHOP_TYPE_IFNAME;
  183. nexthop->ifname = XSTRDUP (0, ifname);
  184. nexthop_add (rib, nexthop);
  185. return nexthop;
  186. }
  187. struct nexthop *
  188. nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4)
  189. {
  190. struct nexthop *nexthop;
  191. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  192. memset (nexthop, 0, sizeof (struct nexthop));
  193. nexthop->type = NEXTHOP_TYPE_IPV4;
  194. nexthop->gate.ipv4 = *ipv4;
  195. nexthop_add (rib, nexthop);
  196. return nexthop;
  197. }
  198. struct nexthop *
  199. nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
  200. unsigned int ifindex)
  201. {
  202. struct nexthop *nexthop;
  203. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  204. memset (nexthop, 0, sizeof (struct nexthop));
  205. nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
  206. nexthop->gate.ipv4 = *ipv4;
  207. nexthop->ifindex = ifindex;
  208. nexthop_add (rib, nexthop);
  209. return nexthop;
  210. }
  211. #ifdef HAVE_IPV6
  212. struct nexthop *
  213. nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
  214. {
  215. struct nexthop *nexthop;
  216. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  217. memset (nexthop, 0, sizeof (struct nexthop));
  218. nexthop->type = NEXTHOP_TYPE_IPV6;
  219. nexthop->gate.ipv6 = *ipv6;
  220. nexthop_add (rib, nexthop);
  221. return nexthop;
  222. }
  223. struct nexthop *
  224. nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6,
  225. char *ifname)
  226. {
  227. struct nexthop *nexthop;
  228. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  229. memset (nexthop, 0, sizeof (struct nexthop));
  230. nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME;
  231. nexthop->gate.ipv6 = *ipv6;
  232. nexthop->ifname = XSTRDUP (0, ifname);
  233. nexthop_add (rib, nexthop);
  234. return nexthop;
  235. }
  236. struct nexthop *
  237. nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
  238. unsigned int ifindex)
  239. {
  240. struct nexthop *nexthop;
  241. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  242. memset (nexthop, 0, sizeof (struct nexthop));
  243. nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
  244. nexthop->gate.ipv6 = *ipv6;
  245. nexthop->ifindex = ifindex;
  246. nexthop_add (rib, nexthop);
  247. return nexthop;
  248. }
  249. #endif /* HAVE_IPV6 */
  250. struct nexthop *
  251. nexthop_blackhole_add (struct rib *rib)
  252. {
  253. struct nexthop *nexthop;
  254. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  255. memset (nexthop, 0, sizeof (struct nexthop));
  256. nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
  257. SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
  258. nexthop_add (rib, nexthop);
  259. return nexthop;
  260. }
  261. /* If force flag is not set, do not modify falgs at all for uninstall
  262. the route from FIB. */
  263. int
  264. nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
  265. struct route_node *top)
  266. {
  267. struct prefix_ipv4 p;
  268. struct route_table *table;
  269. struct route_node *rn;
  270. struct rib *match;
  271. struct nexthop *newhop;
  272. if (nexthop->type == NEXTHOP_TYPE_IPV4)
  273. nexthop->ifindex = 0;
  274. if (set)
  275. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  276. /* Make lookup prefix. */
  277. memset (&p, 0, sizeof (struct prefix_ipv4));
  278. p.family = AF_INET;
  279. p.prefixlen = IPV4_MAX_PREFIXLEN;
  280. p.prefix = nexthop->gate.ipv4;
  281. /* Lookup table. */
  282. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  283. if (! table)
  284. return 0;
  285. rn = route_node_match (table, (struct prefix *) &p);
  286. while (rn)
  287. {
  288. route_unlock_node (rn);
  289. /* If lookup self prefix return immidiately. */
  290. if (rn == top)
  291. return 0;
  292. /* Pick up selected route. */
  293. for (match = rn->info; match; match = match->next)
  294. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  295. break;
  296. /* If there is no selected route or matched route is EGP, go up
  297. tree. */
  298. if (! match
  299. || match->type == ZEBRA_ROUTE_BGP)
  300. {
  301. do {
  302. rn = rn->parent;
  303. } while (rn && rn->info == NULL);
  304. if (rn)
  305. route_lock_node (rn);
  306. }
  307. else
  308. {
  309. if (match->type == ZEBRA_ROUTE_CONNECT)
  310. {
  311. /* Directly point connected route. */
  312. newhop = match->nexthop;
  313. if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
  314. nexthop->ifindex = newhop->ifindex;
  315. return 1;
  316. }
  317. else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
  318. {
  319. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  320. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
  321. && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
  322. {
  323. if (set)
  324. {
  325. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  326. nexthop->rtype = newhop->type;
  327. if (newhop->type == NEXTHOP_TYPE_IPV4 ||
  328. newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  329. nexthop->rgate.ipv4 = newhop->gate.ipv4;
  330. if (newhop->type == NEXTHOP_TYPE_IFINDEX
  331. || newhop->type == NEXTHOP_TYPE_IFNAME
  332. || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  333. nexthop->rifindex = newhop->ifindex;
  334. }
  335. return 1;
  336. }
  337. return 0;
  338. }
  339. else
  340. {
  341. return 0;
  342. }
  343. }
  344. }
  345. return 0;
  346. }
  347. #ifdef HAVE_IPV6
  348. /* If force flag is not set, do not modify falgs at all for uninstall
  349. the route from FIB. */
  350. int
  351. nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
  352. struct route_node *top)
  353. {
  354. struct prefix_ipv6 p;
  355. struct route_table *table;
  356. struct route_node *rn;
  357. struct rib *match;
  358. struct nexthop *newhop;
  359. if (nexthop->type == NEXTHOP_TYPE_IPV6)
  360. nexthop->ifindex = 0;
  361. if (set)
  362. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  363. /* Make lookup prefix. */
  364. memset (&p, 0, sizeof (struct prefix_ipv6));
  365. p.family = AF_INET6;
  366. p.prefixlen = IPV6_MAX_PREFIXLEN;
  367. p.prefix = nexthop->gate.ipv6;
  368. /* Lookup table. */
  369. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  370. if (! table)
  371. return 0;
  372. rn = route_node_match (table, (struct prefix *) &p);
  373. while (rn)
  374. {
  375. route_unlock_node (rn);
  376. /* If lookup self prefix return immidiately. */
  377. if (rn == top)
  378. return 0;
  379. /* Pick up selected route. */
  380. for (match = rn->info; match; match = match->next)
  381. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  382. break;
  383. /* If there is no selected route or matched route is EGP, go up
  384. tree. */
  385. if (! match
  386. || match->type == ZEBRA_ROUTE_BGP)
  387. {
  388. do {
  389. rn = rn->parent;
  390. } while (rn && rn->info == NULL);
  391. if (rn)
  392. route_lock_node (rn);
  393. }
  394. else
  395. {
  396. if (match->type == ZEBRA_ROUTE_CONNECT)
  397. {
  398. /* Directly point connected route. */
  399. newhop = match->nexthop;
  400. if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
  401. nexthop->ifindex = newhop->ifindex;
  402. return 1;
  403. }
  404. else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
  405. {
  406. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  407. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
  408. && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
  409. {
  410. if (set)
  411. {
  412. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
  413. nexthop->rtype = newhop->type;
  414. if (newhop->type == NEXTHOP_TYPE_IPV6
  415. || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  416. || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  417. nexthop->rgate.ipv6 = newhop->gate.ipv6;
  418. if (newhop->type == NEXTHOP_TYPE_IFINDEX
  419. || newhop->type == NEXTHOP_TYPE_IFNAME
  420. || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  421. || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  422. nexthop->rifindex = newhop->ifindex;
  423. }
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. else
  429. {
  430. return 0;
  431. }
  432. }
  433. }
  434. return 0;
  435. }
  436. #endif /* HAVE_IPV6 */
  437. struct rib *
  438. rib_match_ipv4 (struct in_addr addr)
  439. {
  440. struct prefix_ipv4 p;
  441. struct route_table *table;
  442. struct route_node *rn;
  443. struct rib *match;
  444. struct nexthop *newhop;
  445. /* Lookup table. */
  446. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  447. if (! table)
  448. return 0;
  449. memset (&p, 0, sizeof (struct prefix_ipv4));
  450. p.family = AF_INET;
  451. p.prefixlen = IPV4_MAX_PREFIXLEN;
  452. p.prefix = addr;
  453. rn = route_node_match (table, (struct prefix *) &p);
  454. while (rn)
  455. {
  456. route_unlock_node (rn);
  457. /* Pick up selected route. */
  458. for (match = rn->info; match; match = match->next)
  459. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  460. break;
  461. /* If there is no selected route or matched route is EGP, go up
  462. tree. */
  463. if (! match
  464. || match->type == ZEBRA_ROUTE_BGP)
  465. {
  466. do {
  467. rn = rn->parent;
  468. } while (rn && rn->info == NULL);
  469. if (rn)
  470. route_lock_node (rn);
  471. }
  472. else
  473. {
  474. if (match->type == ZEBRA_ROUTE_CONNECT)
  475. /* Directly point connected route. */
  476. return match;
  477. else
  478. {
  479. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  480. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
  481. return match;
  482. return NULL;
  483. }
  484. }
  485. }
  486. return NULL;
  487. }
  488. struct rib *
  489. rib_lookup_ipv4 (struct prefix_ipv4 *p)
  490. {
  491. struct route_table *table;
  492. struct route_node *rn;
  493. struct rib *match;
  494. struct nexthop *nexthop;
  495. /* Lookup table. */
  496. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  497. if (! table)
  498. return 0;
  499. rn = route_node_lookup (table, (struct prefix *) p);
  500. /* No route for this prefix. */
  501. if (! rn)
  502. return NULL;
  503. /* Unlock node. */
  504. route_unlock_node (rn);
  505. /* Pick up selected route. */
  506. for (match = rn->info; match; match = match->next)
  507. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  508. break;
  509. if (! match || match->type == ZEBRA_ROUTE_BGP)
  510. return NULL;
  511. if (match->type == ZEBRA_ROUTE_CONNECT)
  512. return match;
  513. for (nexthop = match->nexthop; nexthop; nexthop = nexthop->next)
  514. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  515. return match;
  516. return NULL;
  517. }
  518. #ifdef HAVE_IPV6
  519. struct rib *
  520. rib_match_ipv6 (struct in6_addr *addr)
  521. {
  522. struct prefix_ipv6 p;
  523. struct route_table *table;
  524. struct route_node *rn;
  525. struct rib *match;
  526. struct nexthop *newhop;
  527. /* Lookup table. */
  528. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  529. if (! table)
  530. return 0;
  531. memset (&p, 0, sizeof (struct prefix_ipv6));
  532. p.family = AF_INET6;
  533. p.prefixlen = IPV6_MAX_PREFIXLEN;
  534. IPV6_ADDR_COPY (&p.prefix, addr);
  535. rn = route_node_match (table, (struct prefix *) &p);
  536. while (rn)
  537. {
  538. route_unlock_node (rn);
  539. /* Pick up selected route. */
  540. for (match = rn->info; match; match = match->next)
  541. if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
  542. break;
  543. /* If there is no selected route or matched route is EGP, go up
  544. tree. */
  545. if (! match
  546. || match->type == ZEBRA_ROUTE_BGP)
  547. {
  548. do {
  549. rn = rn->parent;
  550. } while (rn && rn->info == NULL);
  551. if (rn)
  552. route_lock_node (rn);
  553. }
  554. else
  555. {
  556. if (match->type == ZEBRA_ROUTE_CONNECT)
  557. /* Directly point connected route. */
  558. return match;
  559. else
  560. {
  561. for (newhop = match->nexthop; newhop; newhop = newhop->next)
  562. if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
  563. return match;
  564. return NULL;
  565. }
  566. }
  567. }
  568. return NULL;
  569. }
  570. #endif /* HAVE_IPV6 */
  571. int
  572. nexthop_active_check (struct route_node *rn, struct rib *rib,
  573. struct nexthop *nexthop, int set)
  574. {
  575. struct interface *ifp;
  576. switch (nexthop->type)
  577. {
  578. case NEXTHOP_TYPE_IFINDEX:
  579. ifp = if_lookup_by_index (nexthop->ifindex);
  580. if (ifp && if_is_up (ifp))
  581. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  582. else
  583. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  584. break;
  585. case NEXTHOP_TYPE_IFNAME:
  586. case NEXTHOP_TYPE_IPV6_IFNAME:
  587. ifp = if_lookup_by_name (nexthop->ifname);
  588. if (ifp && if_is_up (ifp))
  589. {
  590. if (set)
  591. nexthop->ifindex = ifp->ifindex;
  592. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  593. }
  594. else
  595. {
  596. if (set)
  597. nexthop->ifindex = 0;
  598. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  599. }
  600. break;
  601. case NEXTHOP_TYPE_IPV4:
  602. case NEXTHOP_TYPE_IPV4_IFINDEX:
  603. if (nexthop_active_ipv4 (rib, nexthop, set, rn))
  604. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  605. else
  606. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  607. break;
  608. #ifdef HAVE_IPV6
  609. case NEXTHOP_TYPE_IPV6:
  610. if (nexthop_active_ipv6 (rib, nexthop, set, rn))
  611. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  612. else
  613. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  614. break;
  615. case NEXTHOP_TYPE_IPV6_IFINDEX:
  616. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
  617. {
  618. ifp = if_lookup_by_index (nexthop->ifindex);
  619. if (ifp && if_is_up (ifp))
  620. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  621. else
  622. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  623. }
  624. else
  625. {
  626. if (nexthop_active_ipv6 (rib, nexthop, set, rn))
  627. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  628. else
  629. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  630. }
  631. break;
  632. #endif /* HAVE_IPV6 */
  633. case NEXTHOP_TYPE_BLACKHOLE:
  634. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  635. break;
  636. default:
  637. break;
  638. }
  639. return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  640. }
  641. int
  642. nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
  643. {
  644. struct nexthop *nexthop;
  645. int active;
  646. rib->nexthop_active_num = 0;
  647. UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
  648. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  649. {
  650. active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
  651. nexthop_active_check (rn, rib, nexthop, set);
  652. if ((MULTIPATH_NUM == 0 || rib->nexthop_active_num < MULTIPATH_NUM)
  653. && active != CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  654. SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
  655. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  656. rib->nexthop_active_num++;
  657. }
  658. return rib->nexthop_active_num;
  659. }
  660. #define RIB_SYSTEM_ROUTE(R) \
  661. ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
  662. void
  663. newrib_free (struct rib *rib)
  664. {
  665. struct nexthop *nexthop;
  666. struct nexthop *next;
  667. for (nexthop = rib->nexthop; nexthop; nexthop = next)
  668. {
  669. next = nexthop->next;
  670. nexthop_free (nexthop);
  671. }
  672. XFREE (MTYPE_RIB, rib);
  673. }
  674. void
  675. rib_install_kernel (struct route_node *rn, struct rib *rib)
  676. {
  677. int ret = 0;
  678. struct nexthop *nexthop;
  679. switch (PREFIX_FAMILY (&rn->p))
  680. {
  681. case AF_INET:
  682. ret = kernel_add_ipv4 (&rn->p, rib);
  683. break;
  684. #ifdef HAVE_IPV6
  685. case AF_INET6:
  686. ret = kernel_add_ipv6 (&rn->p, rib);
  687. break;
  688. #endif /* HAVE_IPV6 */
  689. }
  690. if (ret < 0)
  691. {
  692. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  693. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  694. }
  695. }
  696. /* Uninstall the route from kernel. */
  697. int
  698. rib_uninstall_kernel (struct route_node *rn, struct rib *rib)
  699. {
  700. int ret = 0;
  701. struct nexthop *nexthop;
  702. switch (PREFIX_FAMILY (&rn->p))
  703. {
  704. case AF_INET:
  705. ret = kernel_delete_ipv4 (&rn->p, rib);
  706. break;
  707. #ifdef HAVE_IPV6
  708. case AF_INET6:
  709. ret = kernel_delete_ipv6 (&rn->p, rib);
  710. break;
  711. #endif /* HAVE_IPV6 */
  712. }
  713. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  714. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  715. return ret;
  716. }
  717. /* Uninstall the route from kernel. */
  718. void
  719. rib_uninstall (struct route_node *rn, struct rib *rib)
  720. {
  721. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  722. {
  723. redistribute_delete (&rn->p, rib);
  724. if (! RIB_SYSTEM_ROUTE (rib))
  725. rib_uninstall_kernel (rn, rib);
  726. UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED);
  727. }
  728. }
  729. /* Core function for processing routing information base. */
  730. void
  731. rib_process (struct route_node *rn, struct rib *del)
  732. {
  733. struct rib *rib;
  734. struct rib *next;
  735. struct rib *fib = NULL;
  736. struct rib *select = NULL;
  737. int installed = 0;
  738. struct nexthop *nexthop = NULL;
  739. for (rib = rn->info; rib; rib = next)
  740. {
  741. next = rib->next;
  742. /* Currently installed rib. */
  743. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  744. fib = rib;
  745. /* Skip unreachable nexthop. */
  746. if (! nexthop_active_update (rn, rib, 0))
  747. continue;
  748. /* Infinit distance. */
  749. if (rib->distance == DISTANCE_INFINITY)
  750. continue;
  751. /* Newly selected rib. */
  752. if (! select || rib->distance < select->distance
  753. || rib->type == ZEBRA_ROUTE_CONNECT)
  754. select = rib;
  755. }
  756. /* Deleted route check. */
  757. if (del && CHECK_FLAG (del->flags, ZEBRA_FLAG_SELECTED))
  758. fib = del;
  759. /* Same route is selected. */
  760. if (select && select == fib)
  761. {
  762. if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
  763. {
  764. redistribute_delete (&rn->p, select);
  765. if (! RIB_SYSTEM_ROUTE (select))
  766. rib_uninstall_kernel (rn, select);
  767. /* Set real nexthop. */
  768. nexthop_active_update (rn, select, 1);
  769. if (! RIB_SYSTEM_ROUTE (select))
  770. rib_install_kernel (rn, select);
  771. redistribute_add (&rn->p, select);
  772. }
  773. else if (! RIB_SYSTEM_ROUTE (select))
  774. {
  775. /* Housekeeping code to deal with
  776. race conditions in kernel with linux
  777. netlink reporting interface up before IPv4 or IPv6 protocol
  778. is ready to add routes.
  779. This makes sure the routes are IN the kernel.
  780. */
  781. for (nexthop = select->nexthop; nexthop; nexthop = nexthop->next)
  782. {
  783. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  784. installed = 1;
  785. }
  786. if (! installed) rib_install_kernel (rn, select);
  787. }
  788. return;
  789. }
  790. /* Uninstall old rib from forwarding table. */
  791. if (fib)
  792. {
  793. redistribute_delete (&rn->p, fib);
  794. if (! RIB_SYSTEM_ROUTE (fib))
  795. rib_uninstall_kernel (rn, fib);
  796. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  797. /* Set real nexthop. */
  798. nexthop_active_update (rn, fib, 1);
  799. }
  800. /* Install new rib into forwarding table. */
  801. if (select)
  802. {
  803. /* Set real nexthop. */
  804. nexthop_active_update (rn, select, 1);
  805. if (! RIB_SYSTEM_ROUTE (select))
  806. rib_install_kernel (rn, select);
  807. SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
  808. redistribute_add (&rn->p, select);
  809. }
  810. }
  811. /* Add RIB to head of the route node. */
  812. void
  813. rib_addnode (struct route_node *rn, struct rib *rib)
  814. {
  815. struct rib *head;
  816. head = rn->info;
  817. if (head)
  818. head->prev = rib;
  819. rib->next = head;
  820. rn->info = rib;
  821. }
  822. void
  823. rib_delnode (struct route_node *rn, struct rib *rib)
  824. {
  825. if (rib->next)
  826. rib->next->prev = rib->prev;
  827. if (rib->prev)
  828. rib->prev->next = rib->next;
  829. else
  830. rn->info = rib->next;
  831. }
  832. int
  833. rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  834. struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
  835. u_int32_t metric, u_char distance)
  836. {
  837. struct rib *rib;
  838. struct rib *same = NULL;
  839. struct route_table *table;
  840. struct route_node *rn;
  841. struct nexthop *nexthop;
  842. /* Lookup table. */
  843. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  844. if (! table)
  845. return 0;
  846. /* Make it sure prefixlen is applied to the prefix. */
  847. apply_mask_ipv4 (p);
  848. /* Set default distance by route type. */
  849. if (distance == 0)
  850. {
  851. distance = route_info[type].distance;
  852. /* iBGP distance is 200. */
  853. if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
  854. distance = 200;
  855. }
  856. /* Lookup route node.*/
  857. rn = route_node_get (table, (struct prefix *) p);
  858. /* If same type of route are installed, treat it as a implicit
  859. withdraw. */
  860. for (rib = rn->info; rib; rib = rib->next)
  861. {
  862. if (rib->type == ZEBRA_ROUTE_CONNECT)
  863. {
  864. nexthop = rib->nexthop;
  865. /* Duplicate connected route comes in. */
  866. if (rib->type == type
  867. && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX
  868. && nexthop->ifindex == ifindex)
  869. {
  870. rib->refcnt++;
  871. return 0 ;
  872. }
  873. }
  874. else if (rib->type == type)
  875. {
  876. same = rib;
  877. rib_delnode (rn, same);
  878. route_unlock_node (rn);
  879. break;
  880. }
  881. }
  882. /* Allocate new rib structure. */
  883. rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
  884. memset (rib, 0, sizeof (struct rib));
  885. rib->type = type;
  886. rib->distance = distance;
  887. rib->flags = flags;
  888. rib->metric = metric;
  889. rib->table = vrf_id;
  890. rib->nexthop_num = 0;
  891. rib->uptime = time (NULL);
  892. /* Nexthop settings. */
  893. if (gate)
  894. {
  895. if (ifindex)
  896. nexthop_ipv4_ifindex_add (rib, gate, ifindex);
  897. else
  898. nexthop_ipv4_add (rib, gate);
  899. }
  900. else
  901. nexthop_ifindex_add (rib, ifindex);
  902. /* If this route is kernel route, set FIB flag to the route. */
  903. if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
  904. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  905. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  906. /* Link new rib to node.*/
  907. rib_addnode (rn, rib);
  908. /* Process this route node. */
  909. rib_process (rn, same);
  910. /* Free implicit route.*/
  911. if (same)
  912. newrib_free (same);
  913. return 0;
  914. }
  915. int
  916. rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib)
  917. {
  918. struct route_table *table;
  919. struct route_node *rn;
  920. struct rib *same;
  921. struct nexthop *nexthop;
  922. /* Lookup table. */
  923. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  924. if (! table)
  925. return 0;
  926. /* Make it sure prefixlen is applied to the prefix. */
  927. apply_mask_ipv4 (p);
  928. /* Set default distance by route type. */
  929. if (rib->distance == 0)
  930. {
  931. rib->distance = route_info[rib->type].distance;
  932. /* iBGP distance is 200. */
  933. if (rib->type == ZEBRA_ROUTE_BGP
  934. && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
  935. rib->distance = 200;
  936. }
  937. /* Lookup route node.*/
  938. rn = route_node_get (table, (struct prefix *) p);
  939. /* If same type of route are installed, treat it as a implicit
  940. withdraw. */
  941. for (same = rn->info; same; same = same->next)
  942. {
  943. if (same->type == rib->type && same->table == rib->table
  944. && same->type != ZEBRA_ROUTE_CONNECT)
  945. {
  946. rib_delnode (rn, same);
  947. route_unlock_node (rn);
  948. break;
  949. }
  950. }
  951. /* If this route is kernel route, set FIB flag to the route. */
  952. if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
  953. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  954. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  955. /* Link new rib to node.*/
  956. rib_addnode (rn, rib);
  957. /* Process this route node. */
  958. rib_process (rn, same);
  959. /* Free implicit route.*/
  960. if (same)
  961. newrib_free (same);
  962. return 0;
  963. }
  964. int
  965. rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
  966. struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id)
  967. {
  968. struct route_table *table;
  969. struct route_node *rn;
  970. struct rib *rib;
  971. struct rib *fib = NULL;
  972. struct rib *same = NULL;
  973. struct nexthop *nexthop;
  974. char buf1[BUFSIZ];
  975. char buf2[BUFSIZ];
  976. /* Lookup table. */
  977. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  978. if (! table)
  979. return 0;
  980. /* Apply mask. */
  981. apply_mask_ipv4 (p);
  982. if (IS_ZEBRA_DEBUG_KERNEL && gate)
  983. zlog_info ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
  984. inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ),
  985. p->prefixlen,
  986. inet_ntoa (*gate),
  987. ifindex);
  988. /* Lookup route node. */
  989. rn = route_node_lookup (table, (struct prefix *) p);
  990. if (! rn)
  991. {
  992. if (IS_ZEBRA_DEBUG_KERNEL)
  993. {
  994. if (gate)
  995. zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
  996. inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ),
  997. p->prefixlen,
  998. inet_ntop (AF_INET, gate, buf2, BUFSIZ),
  999. ifindex);
  1000. else
  1001. zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
  1002. inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ),
  1003. p->prefixlen,
  1004. ifindex);
  1005. }
  1006. return ZEBRA_ERR_RTNOEXIST;
  1007. }
  1008. /* Lookup same type route. */
  1009. for (rib = rn->info; rib; rib = rib->next)
  1010. {
  1011. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1012. fib = rib;
  1013. if (rib->type == ZEBRA_ROUTE_CONNECT)
  1014. {
  1015. nexthop = rib->nexthop;
  1016. if (rib->type == type
  1017. && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX
  1018. && nexthop->ifindex == ifindex)
  1019. {
  1020. if (rib->refcnt)
  1021. {
  1022. rib->refcnt--;
  1023. route_unlock_node (rn);
  1024. route_unlock_node (rn);
  1025. return 0;
  1026. }
  1027. same = rib;
  1028. break;
  1029. }
  1030. }
  1031. else if (gate)
  1032. {
  1033. nexthop = rib->nexthop;
  1034. /* Make sure that the route found has the same gateway. */
  1035. if (rib->type == type
  1036. && nexthop &&
  1037. (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate) ||
  1038. IPV4_ADDR_SAME (&nexthop->rgate.ipv4, gate)) )
  1039. {
  1040. same = rib;
  1041. break;
  1042. }
  1043. }
  1044. else
  1045. {
  1046. if (rib->type == type)
  1047. {
  1048. same = rib;
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. /* If same type of route can't be found and this message is from
  1054. kernel. */
  1055. if (! same)
  1056. {
  1057. if (fib && type == ZEBRA_ROUTE_KERNEL)
  1058. {
  1059. /* Unset flags. */
  1060. for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
  1061. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1062. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  1063. }
  1064. else
  1065. {
  1066. if (IS_ZEBRA_DEBUG_KERNEL)
  1067. {
  1068. if (gate)
  1069. zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
  1070. inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ),
  1071. p->prefixlen,
  1072. inet_ntop (AF_INET, gate, buf2, BUFSIZ),
  1073. ifindex,
  1074. type);
  1075. else
  1076. zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
  1077. inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ),
  1078. p->prefixlen,
  1079. ifindex,
  1080. type);
  1081. }
  1082. route_unlock_node (rn);
  1083. return ZEBRA_ERR_RTNOEXIST;
  1084. }
  1085. }
  1086. if (same)
  1087. rib_delnode (rn, same);
  1088. /* Process changes. */
  1089. rib_process (rn, same);
  1090. if (same)
  1091. {
  1092. newrib_free (same);
  1093. route_unlock_node (rn);
  1094. }
  1095. route_unlock_node (rn);
  1096. return 0;
  1097. }
  1098. /* Install static route into rib. */
  1099. void
  1100. static_install_ipv4 (struct prefix *p, struct static_ipv4 *si)
  1101. {
  1102. struct rib *rib;
  1103. struct route_node *rn;
  1104. struct route_table *table;
  1105. /* Lookup table. */
  1106. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  1107. if (! table)
  1108. return;
  1109. /* Lookup existing route */
  1110. rn = route_node_get (table, p);
  1111. for (rib = rn->info; rib; rib = rib->next)
  1112. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  1113. break;
  1114. if (rib)
  1115. {
  1116. /* Same distance static route is there. Update it with new
  1117. nexthop. */
  1118. route_unlock_node (rn);
  1119. switch (si->type)
  1120. {
  1121. case STATIC_IPV4_GATEWAY:
  1122. nexthop_ipv4_add (rib, &si->gate.ipv4);
  1123. break;
  1124. case STATIC_IPV4_IFNAME:
  1125. nexthop_ifname_add (rib, si->gate.ifname);
  1126. break;
  1127. case STATIC_IPV4_BLACKHOLE:
  1128. nexthop_blackhole_add (rib);
  1129. break;
  1130. }
  1131. rib_process (rn, NULL);
  1132. }
  1133. else
  1134. {
  1135. /* This is new static route. */
  1136. rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
  1137. memset (rib, 0, sizeof (struct rib));
  1138. rib->type = ZEBRA_ROUTE_STATIC;
  1139. rib->distance = si->distance;
  1140. rib->metric = 0;
  1141. rib->nexthop_num = 0;
  1142. switch (si->type)
  1143. {
  1144. case STATIC_IPV4_GATEWAY:
  1145. nexthop_ipv4_add (rib, &si->gate.ipv4);
  1146. break;
  1147. case STATIC_IPV4_IFNAME:
  1148. nexthop_ifname_add (rib, si->gate.ifname);
  1149. break;
  1150. case STATIC_IPV4_BLACKHOLE:
  1151. nexthop_blackhole_add (rib);
  1152. break;
  1153. }
  1154. /* Save the flags of this static routes (reject, blackhole) */
  1155. rib->flags = si->flags;
  1156. /* Link this rib to the tree. */
  1157. rib_addnode (rn, rib);
  1158. /* Process this prefix. */
  1159. rib_process (rn, NULL);
  1160. }
  1161. }
  1162. int
  1163. static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si)
  1164. {
  1165. if (nexthop->type == NEXTHOP_TYPE_IPV4
  1166. && si->type == STATIC_IPV4_GATEWAY
  1167. && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4))
  1168. return 1;
  1169. if (nexthop->type == NEXTHOP_TYPE_IFNAME
  1170. && si->type == STATIC_IPV4_IFNAME
  1171. && strcmp (nexthop->ifname, si->gate.ifname) == 0)
  1172. return 1;
  1173. if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
  1174. && si->type == STATIC_IPV4_BLACKHOLE)
  1175. return 1;
  1176. return 0;;
  1177. }
  1178. /* Uninstall static route from RIB. */
  1179. void
  1180. static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si)
  1181. {
  1182. struct route_node *rn;
  1183. struct rib *rib;
  1184. struct nexthop *nexthop;
  1185. struct route_table *table;
  1186. /* Lookup table. */
  1187. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  1188. if (! table)
  1189. return;
  1190. /* Lookup existing route with type and distance. */
  1191. rn = route_node_lookup (table, p);
  1192. if (! rn)
  1193. return;
  1194. for (rib = rn->info; rib; rib = rib->next)
  1195. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  1196. break;
  1197. if (! rib)
  1198. {
  1199. route_unlock_node (rn);
  1200. return;
  1201. }
  1202. /* Lookup nexthop. */
  1203. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1204. if (static_ipv4_nexthop_same (nexthop, si))
  1205. break;
  1206. /* Can't find nexthop. */
  1207. if (! nexthop)
  1208. {
  1209. route_unlock_node (rn);
  1210. return;
  1211. }
  1212. /* Check nexthop. */
  1213. if (rib->nexthop_num == 1)
  1214. {
  1215. rib_delnode (rn, rib);
  1216. rib_process (rn, rib);
  1217. newrib_free (rib);
  1218. route_unlock_node (rn);
  1219. }
  1220. else
  1221. {
  1222. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  1223. rib_uninstall (rn, rib);
  1224. nexthop_delete (rib, nexthop);
  1225. nexthop_free (nexthop);
  1226. rib_process (rn, rib);
  1227. }
  1228. /* Unlock node. */
  1229. route_unlock_node (rn);
  1230. }
  1231. /* Add static route into static route configuration. */
  1232. int
  1233. static_add_ipv4 (struct prefix *p, struct in_addr *gate, char *ifname,
  1234. u_char flags, u_char distance, u_int32_t vrf_id)
  1235. {
  1236. u_char type = 0;
  1237. struct route_node *rn;
  1238. struct static_ipv4 *si;
  1239. struct static_ipv4 *pp;
  1240. struct static_ipv4 *cp;
  1241. struct static_ipv4 *update = NULL;
  1242. struct route_table *stable;
  1243. /* Lookup table. */
  1244. stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
  1245. if (! stable)
  1246. return -1;
  1247. /* Lookup static route prefix. */
  1248. rn = route_node_get (stable, p);
  1249. /* Make flags. */
  1250. if (gate)
  1251. type = STATIC_IPV4_GATEWAY;
  1252. else if (ifname)
  1253. type = STATIC_IPV4_IFNAME;
  1254. else
  1255. type = STATIC_IPV4_BLACKHOLE;
  1256. /* Do nothing if there is a same static route. */
  1257. for (si = rn->info; si; si = si->next)
  1258. {
  1259. if (type == si->type
  1260. && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
  1261. && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
  1262. {
  1263. if (distance == si->distance)
  1264. {
  1265. route_unlock_node (rn);
  1266. return 0;
  1267. }
  1268. else
  1269. update = si;
  1270. }
  1271. }
  1272. /* Distance chaged. */
  1273. if (update)
  1274. static_delete_ipv4 (p, gate, ifname, update->distance, vrf_id);
  1275. /* Make new static route structure. */
  1276. si = XMALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4));
  1277. memset (si, 0, sizeof (struct static_ipv4));
  1278. si->type = type;
  1279. si->distance = distance;
  1280. si->flags = flags;
  1281. if (gate)
  1282. si->gate.ipv4 = *gate;
  1283. if (ifname)
  1284. si->gate.ifname = XSTRDUP (0, ifname);
  1285. /* Add new static route information to the tree with sort by
  1286. distance value and gateway address. */
  1287. for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
  1288. {
  1289. if (si->distance < cp->distance)
  1290. break;
  1291. if (si->distance > cp->distance)
  1292. continue;
  1293. if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
  1294. {
  1295. if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr))
  1296. break;
  1297. if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr))
  1298. continue;
  1299. }
  1300. }
  1301. /* Make linked list. */
  1302. if (pp)
  1303. pp->next = si;
  1304. else
  1305. rn->info = si;
  1306. if (cp)
  1307. cp->prev = si;
  1308. si->prev = pp;
  1309. si->next = cp;
  1310. /* Install into rib. */
  1311. static_install_ipv4 (p, si);
  1312. return 1;
  1313. }
  1314. /* Delete static route from static route configuration. */
  1315. int
  1316. static_delete_ipv4 (struct prefix *p, struct in_addr *gate, char *ifname,
  1317. u_char distance, u_int32_t vrf_id)
  1318. {
  1319. u_char type = 0;
  1320. struct route_node *rn;
  1321. struct static_ipv4 *si;
  1322. struct route_table *stable;
  1323. /* Lookup table. */
  1324. stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
  1325. if (! stable)
  1326. return -1;
  1327. /* Lookup static route prefix. */
  1328. rn = route_node_lookup (stable, p);
  1329. if (! rn)
  1330. return 0;
  1331. /* Make flags. */
  1332. if (gate)
  1333. type = STATIC_IPV4_GATEWAY;
  1334. else if (ifname)
  1335. type = STATIC_IPV4_IFNAME;
  1336. else
  1337. type = STATIC_IPV4_BLACKHOLE;
  1338. /* Find same static route is the tree */
  1339. for (si = rn->info; si; si = si->next)
  1340. if (type == si->type
  1341. && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
  1342. && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
  1343. break;
  1344. /* Can't find static route. */
  1345. if (! si)
  1346. {
  1347. route_unlock_node (rn);
  1348. return 0;
  1349. }
  1350. /* Install into rib. */
  1351. static_uninstall_ipv4 (p, si);
  1352. /* Unlink static route from linked list. */
  1353. if (si->prev)
  1354. si->prev->next = si->next;
  1355. else
  1356. rn->info = si->next;
  1357. if (si->next)
  1358. si->next->prev = si->prev;
  1359. route_unlock_node (rn);
  1360. /* Free static route configuration. */
  1361. if (ifname)
  1362. XFREE (0, si->gate.ifname);
  1363. XFREE (MTYPE_STATIC_IPV4, si);
  1364. route_unlock_node (rn);
  1365. return 1;
  1366. }
  1367. #ifdef HAVE_IPV6
  1368. int
  1369. rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
  1370. struct in6_addr *gate, unsigned int ifindex, int table)
  1371. {
  1372. if (type == ZEBRA_ROUTE_CONNECT && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)) {
  1373. #if defined (MUSICA) || defined (LINUX)
  1374. /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
  1375. if (p->prefixlen == 96)
  1376. return 0;
  1377. #endif /* MUSICA */
  1378. return 1;
  1379. }
  1380. if (type == ZEBRA_ROUTE_KERNEL && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)
  1381. && p->prefixlen == 96 && gate && IN6_IS_ADDR_UNSPECIFIED (gate))
  1382. {
  1383. kernel_delete_ipv6_old (p, gate, ifindex, 0, table);
  1384. return 1;
  1385. }
  1386. return 0;
  1387. }
  1388. int
  1389. rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  1390. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id)
  1391. {
  1392. struct rib *rib;
  1393. struct rib *same = NULL;
  1394. struct route_table *table;
  1395. struct route_node *rn;
  1396. struct nexthop *nexthop;
  1397. int distance;
  1398. u_int32_t metric = 0;
  1399. /* Lookup table. */
  1400. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  1401. if (! table)
  1402. return 0;
  1403. /* Make sure mask is applied. */
  1404. apply_mask_ipv6 (p);
  1405. /* Set default distance by route type. */
  1406. distance = route_info[type].distance;
  1407. if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
  1408. distance = 200;
  1409. /* Filter bogus route. */
  1410. if (rib_bogus_ipv6 (type, p, gate, ifindex, 0))
  1411. return 0;
  1412. /* Lookup route node.*/
  1413. rn = route_node_get (table, (struct prefix *) p);
  1414. /* If same type of route are installed, treat it as a implicit
  1415. withdraw. */
  1416. for (rib = rn->info; rib; rib = rib->next)
  1417. {
  1418. if (rib->type == ZEBRA_ROUTE_CONNECT)
  1419. {
  1420. nexthop = rib->nexthop;
  1421. if (rib->type == type
  1422. && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX
  1423. && nexthop->ifindex == ifindex)
  1424. {
  1425. rib->refcnt++;
  1426. return 0;
  1427. }
  1428. }
  1429. else if (rib->type == type)
  1430. {
  1431. same = rib;
  1432. rib_delnode (rn, same);
  1433. route_unlock_node (rn);
  1434. break;
  1435. }
  1436. }
  1437. /* Allocate new rib structure. */
  1438. rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
  1439. memset (rib, 0, sizeof (struct rib));
  1440. rib->type = type;
  1441. rib->distance = distance;
  1442. rib->flags = flags;
  1443. rib->metric = metric;
  1444. rib->table = vrf_id;
  1445. rib->nexthop_num = 0;
  1446. rib->uptime = time (NULL);
  1447. /* Nexthop settings. */
  1448. if (gate)
  1449. {
  1450. if (ifindex)
  1451. nexthop_ipv6_ifindex_add (rib, gate, ifindex);
  1452. else
  1453. nexthop_ipv6_add (rib, gate);
  1454. }
  1455. else
  1456. nexthop_ifindex_add (rib, ifindex);
  1457. /* If this route is kernel route, set FIB flag to the route. */
  1458. if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
  1459. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1460. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1461. /* Link new rib to node.*/
  1462. rib_addnode (rn, rib);
  1463. /* Process this route node. */
  1464. rib_process (rn, same);
  1465. /* Free implicit route.*/
  1466. if (same)
  1467. newrib_free (same);
  1468. return 0;
  1469. }
  1470. int
  1471. rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
  1472. struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id)
  1473. {
  1474. struct route_table *table;
  1475. struct route_node *rn;
  1476. struct rib *rib;
  1477. struct rib *fib = NULL;
  1478. struct rib *same = NULL;
  1479. struct nexthop *nexthop;
  1480. char buf1[BUFSIZ];
  1481. char buf2[BUFSIZ];
  1482. /* Apply mask. */
  1483. apply_mask_ipv6 (p);
  1484. /* Lookup table. */
  1485. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  1486. if (! table)
  1487. return 0;
  1488. /* Lookup route node. */
  1489. rn = route_node_lookup (table, (struct prefix *) p);
  1490. if (! rn)
  1491. {
  1492. if (IS_ZEBRA_DEBUG_KERNEL)
  1493. {
  1494. if (gate)
  1495. zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
  1496. inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ),
  1497. p->prefixlen,
  1498. inet_ntop (AF_INET6, gate, buf2, BUFSIZ),
  1499. ifindex);
  1500. else
  1501. zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
  1502. inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ),
  1503. p->prefixlen,
  1504. ifindex);
  1505. }
  1506. return ZEBRA_ERR_RTNOEXIST;
  1507. }
  1508. /* Lookup same type route. */
  1509. for (rib = rn->info; rib; rib = rib->next)
  1510. {
  1511. if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1512. fib = rib;
  1513. if (rib->type == ZEBRA_ROUTE_CONNECT)
  1514. {
  1515. nexthop = rib->nexthop;
  1516. if (rib->type == type
  1517. && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX
  1518. && nexthop->ifindex == ifindex)
  1519. {
  1520. if (rib->refcnt)
  1521. {
  1522. rib->refcnt--;
  1523. route_unlock_node (rn);
  1524. route_unlock_node (rn);
  1525. return 0;
  1526. }
  1527. same = rib;
  1528. break;
  1529. }
  1530. }
  1531. else
  1532. {
  1533. if (rib->type == type)
  1534. {
  1535. same = rib;
  1536. break;
  1537. }
  1538. }
  1539. }
  1540. /* If same type of route can't be found and this message is from
  1541. kernel. */
  1542. if (! same)
  1543. {
  1544. if (fib && type == ZEBRA_ROUTE_KERNEL)
  1545. {
  1546. /* Unset flags. */
  1547. for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
  1548. UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1549. UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
  1550. }
  1551. else
  1552. {
  1553. if (IS_ZEBRA_DEBUG_KERNEL)
  1554. {
  1555. if (gate)
  1556. zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
  1557. inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ),
  1558. p->prefixlen,
  1559. inet_ntop (AF_INET6, gate, buf2, BUFSIZ),
  1560. ifindex,
  1561. type);
  1562. else
  1563. zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
  1564. inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ),
  1565. p->prefixlen,
  1566. ifindex,
  1567. type);
  1568. }
  1569. route_unlock_node (rn);
  1570. return ZEBRA_ERR_RTNOEXIST;
  1571. }
  1572. }
  1573. if (same)
  1574. rib_delnode (rn, same);
  1575. /* Process changes. */
  1576. rib_process (rn, same);
  1577. if (same)
  1578. {
  1579. newrib_free (same);
  1580. route_unlock_node (rn);
  1581. }
  1582. route_unlock_node (rn);
  1583. return 0;
  1584. }
  1585. /* Install static route into rib. */
  1586. void
  1587. static_install_ipv6 (struct prefix *p, struct static_ipv6 *si)
  1588. {
  1589. struct rib *rib;
  1590. struct route_table *table;
  1591. struct route_node *rn;
  1592. /* Lookup table. */
  1593. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  1594. if (! table)
  1595. return;
  1596. /* Lookup existing route */
  1597. rn = route_node_get (table, p);
  1598. for (rib = rn->info; rib; rib = rib->next)
  1599. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  1600. break;
  1601. if (rib)
  1602. {
  1603. /* Same distance static route is there. Update it with new
  1604. nexthop. */
  1605. route_unlock_node (rn);
  1606. switch (si->type)
  1607. {
  1608. case STATIC_IPV6_GATEWAY:
  1609. nexthop_ipv6_add (rib, &si->ipv6);
  1610. break;
  1611. case STATIC_IPV6_IFNAME:
  1612. nexthop_ifname_add (rib, si->ifname);
  1613. break;
  1614. case STATIC_IPV6_GATEWAY_IFNAME:
  1615. nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
  1616. break;
  1617. }
  1618. rib_process (rn, NULL);
  1619. }
  1620. else
  1621. {
  1622. /* This is new static route. */
  1623. rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
  1624. memset (rib, 0, sizeof (struct rib));
  1625. rib->type = ZEBRA_ROUTE_STATIC;
  1626. rib->distance = si->distance;
  1627. rib->metric = 0;
  1628. rib->nexthop_num = 0;
  1629. switch (si->type)
  1630. {
  1631. case STATIC_IPV6_GATEWAY:
  1632. nexthop_ipv6_add (rib, &si->ipv6);
  1633. break;
  1634. case STATIC_IPV6_IFNAME:
  1635. nexthop_ifname_add (rib, si->ifname);
  1636. break;
  1637. case STATIC_IPV6_GATEWAY_IFNAME:
  1638. nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
  1639. break;
  1640. }
  1641. /* Save the flags of this static routes (reject, blackhole) */
  1642. rib->flags = si->flags;
  1643. /* Link this rib to the tree. */
  1644. rib_addnode (rn, rib);
  1645. /* Process this prefix. */
  1646. rib_process (rn, NULL);
  1647. }
  1648. }
  1649. int
  1650. static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si)
  1651. {
  1652. if (nexthop->type == NEXTHOP_TYPE_IPV6
  1653. && si->type == STATIC_IPV6_GATEWAY
  1654. && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6))
  1655. return 1;
  1656. if (nexthop->type == NEXTHOP_TYPE_IFNAME
  1657. && si->type == STATIC_IPV6_IFNAME
  1658. && strcmp (nexthop->ifname, si->ifname) == 0)
  1659. return 1;
  1660. if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1661. && si->type == STATIC_IPV6_GATEWAY_IFNAME
  1662. && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)
  1663. && strcmp (nexthop->ifname, si->ifname) == 0)
  1664. return 1;
  1665. return 0;;
  1666. }
  1667. void
  1668. static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si)
  1669. {
  1670. struct route_table *table;
  1671. struct route_node *rn;
  1672. struct rib *rib;
  1673. struct nexthop *nexthop;
  1674. /* Lookup table. */
  1675. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  1676. if (! table)
  1677. return;
  1678. /* Lookup existing route with type and distance. */
  1679. rn = route_node_lookup (table, (struct prefix *) p);
  1680. if (! rn)
  1681. return;
  1682. for (rib = rn->info; rib; rib = rib->next)
  1683. if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
  1684. break;
  1685. if (! rib)
  1686. {
  1687. route_unlock_node (rn);
  1688. return;
  1689. }
  1690. /* Lookup nexthop. */
  1691. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1692. if (static_ipv6_nexthop_same (nexthop, si))
  1693. break;
  1694. /* Can't find nexthop. */
  1695. if (! nexthop)
  1696. {
  1697. route_unlock_node (rn);
  1698. return;
  1699. }
  1700. /* Check nexthop. */
  1701. if (rib->nexthop_num == 1)
  1702. {
  1703. rib_delnode (rn, rib);
  1704. rib_process (rn, rib);
  1705. newrib_free (rib);
  1706. route_unlock_node (rn);
  1707. }
  1708. else
  1709. {
  1710. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  1711. rib_uninstall (rn, rib);
  1712. nexthop_delete (rib, nexthop);
  1713. nexthop_free (nexthop);
  1714. rib_process (rn, rib);
  1715. }
  1716. /* Unlock node. */
  1717. route_unlock_node (rn);
  1718. }
  1719. /* Add static route into static route configuration. */
  1720. int
  1721. static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  1722. char *ifname, u_char flags, u_char distance, u_int32_t vrf_id)
  1723. {
  1724. struct route_node *rn;
  1725. struct static_ipv6 *si;
  1726. struct static_ipv6 *pp;
  1727. struct static_ipv6 *cp;
  1728. struct route_table *stable;
  1729. /* Lookup table. */
  1730. stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  1731. if (! stable)
  1732. return -1;
  1733. /* Lookup static route prefix. */
  1734. rn = route_node_get (stable, p);
  1735. /* Do nothing if there is a same static route. */
  1736. for (si = rn->info; si; si = si->next)
  1737. {
  1738. if (distance == si->distance
  1739. && type == si->type
  1740. && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
  1741. && (! ifname || strcmp (ifname, si->ifname) == 0))
  1742. {
  1743. route_unlock_node (rn);
  1744. return 0;
  1745. }
  1746. }
  1747. /* Make new static route structure. */
  1748. si = XMALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6));
  1749. memset (si, 0, sizeof (struct static_ipv6));
  1750. si->type = type;
  1751. si->distance = distance;
  1752. si->flags = flags;
  1753. switch (type)
  1754. {
  1755. case STATIC_IPV6_GATEWAY:
  1756. si->ipv6 = *gate;
  1757. break;
  1758. case STATIC_IPV6_IFNAME:
  1759. si->ifname = XSTRDUP (0, ifname);
  1760. break;
  1761. case STATIC_IPV6_GATEWAY_IFNAME:
  1762. si->ipv6 = *gate;
  1763. si->ifname = XSTRDUP (0, ifname);
  1764. break;
  1765. }
  1766. /* Add new static route information to the tree with sort by
  1767. distance value and gateway address. */
  1768. for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
  1769. {
  1770. if (si->distance < cp->distance)
  1771. break;
  1772. if (si->distance > cp->distance)
  1773. continue;
  1774. }
  1775. /* Make linked list. */
  1776. if (pp)
  1777. pp->next = si;
  1778. else
  1779. rn->info = si;
  1780. if (cp)
  1781. cp->prev = si;
  1782. si->prev = pp;
  1783. si->next = cp;
  1784. /* Install into rib. */
  1785. static_install_ipv6 (p, si);
  1786. return 1;
  1787. }
  1788. /* Delete static route from static route configuration. */
  1789. int
  1790. static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
  1791. char *ifname, u_char distance, u_int32_t vrf_id)
  1792. {
  1793. struct route_node *rn;
  1794. struct static_ipv6 *si;
  1795. struct route_table *stable;
  1796. /* Lookup table. */
  1797. stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  1798. if (! stable)
  1799. return -1;
  1800. /* Lookup static route prefix. */
  1801. rn = route_node_lookup (stable, p);
  1802. if (! rn)
  1803. return 0;
  1804. /* Find same static route is the tree */
  1805. for (si = rn->info; si; si = si->next)
  1806. if (distance == si->distance
  1807. && type == si->type
  1808. && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
  1809. && (! ifname || strcmp (ifname, si->ifname) == 0))
  1810. break;
  1811. /* Can't find static route. */
  1812. if (! si)
  1813. {
  1814. route_unlock_node (rn);
  1815. return 0;
  1816. }
  1817. /* Install into rib. */
  1818. static_uninstall_ipv6 (p, si);
  1819. /* Unlink static route from linked list. */
  1820. if (si->prev)
  1821. si->prev->next = si->next;
  1822. else
  1823. rn->info = si->next;
  1824. if (si->next)
  1825. si->next->prev = si->prev;
  1826. /* Free static route configuration. */
  1827. if (ifname)
  1828. XFREE (0, si->ifname);
  1829. XFREE (MTYPE_STATIC_IPV6, si);
  1830. return 1;
  1831. }
  1832. #endif /* HAVE_IPV6 */
  1833. /* RIB update function. */
  1834. void
  1835. rib_update ()
  1836. {
  1837. struct route_node *rn;
  1838. struct route_table *table;
  1839. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  1840. if (table)
  1841. for (rn = route_top (table); rn; rn = route_next (rn))
  1842. rib_process (rn, NULL);
  1843. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  1844. if (table)
  1845. for (rn = route_top (table); rn; rn = route_next (rn))
  1846. rib_process (rn, NULL);
  1847. }
  1848. /* Interface goes up. */
  1849. void
  1850. rib_if_up (struct interface *ifp)
  1851. {
  1852. rib_update ();
  1853. }
  1854. /* Interface goes down. */
  1855. void
  1856. rib_if_down (struct interface *ifp)
  1857. {
  1858. rib_update ();
  1859. }
  1860. /* Remove all routes which comes from non main table. */
  1861. void
  1862. rib_weed_table (struct route_table *table)
  1863. {
  1864. struct route_node *rn;
  1865. struct rib *rib;
  1866. struct rib *next;
  1867. if (table)
  1868. for (rn = route_top (table); rn; rn = route_next (rn))
  1869. for (rib = rn->info; rib; rib = next)
  1870. {
  1871. next = rib->next;
  1872. if (rib->table != zebrad.rtm_table_default &&
  1873. rib->table != RT_TABLE_MAIN)
  1874. {
  1875. rib_delnode (rn, rib);
  1876. newrib_free (rib);
  1877. route_unlock_node (rn);
  1878. }
  1879. }
  1880. }
  1881. /* Delete all routes from non main table. */
  1882. void
  1883. rib_weed_tables ()
  1884. {
  1885. rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  1886. rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  1887. }
  1888. /* Delete self installed routes after zebra is relaunched. */
  1889. void
  1890. rib_sweep_table (struct route_table *table)
  1891. {
  1892. struct route_node *rn;
  1893. struct rib *rib;
  1894. struct rib *next;
  1895. int ret = 0;
  1896. if (table)
  1897. for (rn = route_top (table); rn; rn = route_next (rn))
  1898. for (rib = rn->info; rib; rib = next)
  1899. {
  1900. next = rib->next;
  1901. if (rib->type == ZEBRA_ROUTE_KERNEL &&
  1902. CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
  1903. {
  1904. ret = rib_uninstall_kernel (rn, rib);
  1905. if (! ret)
  1906. {
  1907. rib_delnode (rn, rib);
  1908. newrib_free (rib);
  1909. route_unlock_node (rn);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. /* Sweep all RIB tables. */
  1915. void
  1916. rib_sweep_route ()
  1917. {
  1918. rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  1919. rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  1920. }
  1921. /* Close RIB and clean up kernel routes. */
  1922. void
  1923. rib_close_table (struct route_table *table)
  1924. {
  1925. struct route_node *rn;
  1926. struct rib *rib;
  1927. if (table)
  1928. for (rn = route_top (table); rn; rn = route_next (rn))
  1929. for (rib = rn->info; rib; rib = rib->next)
  1930. if (! RIB_SYSTEM_ROUTE (rib)
  1931. && CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
  1932. rib_uninstall_kernel (rn, rib);
  1933. }
  1934. /* Close all RIB tables. */
  1935. void
  1936. rib_close ()
  1937. {
  1938. rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
  1939. rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
  1940. }
  1941. /* Routing information base initialize. */
  1942. void
  1943. rib_init ()
  1944. {
  1945. /* VRF initialization. */
  1946. vrf_init ();
  1947. }