ripng_interface.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * Interface related function for RIPng.
  3. * Copyright (C) 1998 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "linklist.h"
  24. #include "if.h"
  25. #include "prefix.h"
  26. #include "memory.h"
  27. #include "network.h"
  28. #include "filter.h"
  29. #include "log.h"
  30. #include "stream.h"
  31. #include "zclient.h"
  32. #include "command.h"
  33. #include "table.h"
  34. #include "thread.h"
  35. #include "privs.h"
  36. #include "ripngd/ripngd.h"
  37. #include "ripngd/ripng_debug.h"
  38. /* If RFC2133 definition is used. */
  39. #ifndef IPV6_JOIN_GROUP
  40. #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
  41. #endif
  42. #ifndef IPV6_LEAVE_GROUP
  43. #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
  44. #endif
  45. extern struct zebra_privs_t ripngd_privs;
  46. /* Static utility function. */
  47. static void ripng_enable_apply (struct interface *);
  48. static void ripng_passive_interface_apply (struct interface *);
  49. int ripng_enable_if_lookup (char *ifname);
  50. int ripng_enable_network_lookup2 (struct connected *connected);
  51. void ripng_enable_apply_all ();
  52. /* Join to the all rip routers multicast group. */
  53. int
  54. ripng_multicast_join (struct interface *ifp)
  55. {
  56. int ret;
  57. struct ipv6_mreq mreq;
  58. if (if_is_up (ifp) && if_is_multicast (ifp)) {
  59. memset (&mreq, 0, sizeof (mreq));
  60. inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
  61. mreq.ipv6mr_interface = ifp->ifindex;
  62. /*
  63. * NetBSD 1.6.2 requires root to join groups on gif(4).
  64. * While this is bogus, privs are available and easy to use
  65. * for this call as a workaround.
  66. */
  67. if (ripngd_privs.change (ZPRIVS_RAISE))
  68. zlog_err ("ripng_multicast_join: could not raise privs");
  69. ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  70. (char *) &mreq, sizeof (mreq));
  71. if (ripngd_privs.change (ZPRIVS_LOWER))
  72. zlog_err ("ripng_multicast_join: could not lower privs");
  73. if (ret < 0 && errno == EADDRINUSE)
  74. {
  75. /*
  76. * Group is already joined. This occurs due to sloppy group
  77. * management, in particular declining to leave the group on
  78. * an interface that has just gone down.
  79. */
  80. zlog_warn ("ripng join on %s EADDRINUSE (ignoring)\n", ifp->name);
  81. return 0; /* not an error */
  82. }
  83. if (ret < 0)
  84. zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
  85. if (IS_RIPNG_DEBUG_EVENT)
  86. zlog_info ("RIPng %s join to all-rip-routers multicast group", ifp->name);
  87. if (ret < 0)
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. /* Leave from the all rip routers multicast group. */
  93. int
  94. ripng_multicast_leave (struct interface *ifp)
  95. {
  96. int ret;
  97. struct ipv6_mreq mreq;
  98. if (if_is_up (ifp) && if_is_multicast (ifp)) {
  99. memset (&mreq, 0, sizeof (mreq));
  100. inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
  101. mreq.ipv6mr_interface = ifp->ifindex;
  102. ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  103. (char *) &mreq, sizeof (mreq));
  104. if (ret < 0)
  105. zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", strerror (errno));
  106. if (IS_RIPNG_DEBUG_EVENT)
  107. zlog_info ("RIPng %s leave from all-rip-routers multicast group",
  108. ifp->name);
  109. if (ret < 0)
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. /* How many link local IPv6 address could be used on the interface ? */
  115. int
  116. ripng_if_ipv6_lladdress_check (struct interface *ifp)
  117. {
  118. struct listnode *nn;
  119. struct connected *connected;
  120. int count = 0;
  121. for (nn = listhead (ifp->connected); nn; nextnode (nn))
  122. if ((connected = getdata (nn)) != NULL) {
  123. struct prefix *p;
  124. p = connected->address;
  125. if ((p->family == AF_INET6) &&
  126. IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
  127. count++;
  128. }
  129. return count;
  130. }
  131. /* Check max mtu size. */
  132. int
  133. ripng_check_max_mtu ()
  134. {
  135. listnode node;
  136. struct interface *ifp;
  137. int mtu;
  138. mtu = 0;
  139. for (node = listhead (iflist); node; nextnode (node))
  140. {
  141. ifp = getdata (node);
  142. if (mtu < ifp->mtu6)
  143. mtu = ifp->mtu6;
  144. }
  145. return mtu;
  146. }
  147. int
  148. ripng_if_down (struct interface *ifp)
  149. {
  150. struct route_node *rp;
  151. struct ripng_info *rinfo;
  152. struct ripng_interface *ri;
  153. if (ripng)
  154. {
  155. for (rp = route_top (ripng->table); rp; rp = route_next (rp))
  156. if ((rinfo = rp->info) != NULL)
  157. {
  158. /* Routes got through this interface. */
  159. if (rinfo->ifindex == ifp->ifindex
  160. && rinfo->type == ZEBRA_ROUTE_RIPNG
  161. && rinfo->sub_type == RIPNG_ROUTE_RTE)
  162. {
  163. ripng_zebra_ipv6_delete ((struct prefix_ipv6 *) &rp->p,
  164. &rinfo->nexthop,
  165. rinfo->ifindex);
  166. ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
  167. (struct prefix_ipv6 *)&rp->p,
  168. rinfo->ifindex);
  169. }
  170. else
  171. {
  172. /* All redistributed routes got through this interface,
  173. * but the static and system ones are kept. */
  174. if ((rinfo->ifindex == ifp->ifindex) &&
  175. (rinfo->type != ZEBRA_ROUTE_STATIC) &&
  176. (rinfo->type != ZEBRA_ROUTE_SYSTEM))
  177. ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
  178. (struct prefix_ipv6 *) &rp->p,
  179. rinfo->ifindex);
  180. }
  181. }
  182. }
  183. ri = ifp->info;
  184. if (ri->running)
  185. {
  186. if (IS_RIPNG_DEBUG_EVENT)
  187. zlog_info ("turn off %s", ifp->name);
  188. /* Leave from multicast group. */
  189. ripng_multicast_leave (ifp);
  190. ri->running = 0;
  191. }
  192. return 0;
  193. }
  194. /* Inteface link up message processing. */
  195. int
  196. ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length)
  197. {
  198. struct stream *s;
  199. struct interface *ifp;
  200. /* zebra_interface_state_read() updates interface structure in iflist. */
  201. s = zclient->ibuf;
  202. ifp = zebra_interface_state_read (s);
  203. if (ifp == NULL)
  204. return 0;
  205. if (IS_RIPNG_DEBUG_ZEBRA)
  206. zlog_info ("interface up %s index %d flags %ld metric %d mtu %d",
  207. ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
  208. /* Check if this interface is RIPng enabled or not. */
  209. ripng_enable_apply (ifp);
  210. /* Check for a passive interface. */
  211. ripng_passive_interface_apply (ifp);
  212. /* Apply distribute list to the all interface. */
  213. ripng_distribute_update_interface (ifp);
  214. return 0;
  215. }
  216. /* Inteface link down message processing. */
  217. int
  218. ripng_interface_down (int command, struct zclient *zclient,
  219. zebra_size_t length)
  220. {
  221. struct stream *s;
  222. struct interface *ifp;
  223. /* zebra_interface_state_read() updates interface structure in iflist. */
  224. s = zclient->ibuf;
  225. ifp = zebra_interface_state_read (s);
  226. if (ifp == NULL)
  227. return 0;
  228. ripng_if_down (ifp);
  229. if (IS_RIPNG_DEBUG_ZEBRA)
  230. zlog_info ("interface down %s index %d flags %ld metric %d mtu %d",
  231. ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
  232. return 0;
  233. }
  234. /* Inteface addition message from zebra. */
  235. int
  236. ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length)
  237. {
  238. struct interface *ifp;
  239. ifp = zebra_interface_add_read (zclient->ibuf);
  240. if (IS_RIPNG_DEBUG_ZEBRA)
  241. zlog_info ("RIPng interface add %s index %d flags %ld metric %d mtu %d",
  242. ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
  243. /* Check is this interface is RIP enabled or not.*/
  244. ripng_enable_apply (ifp);
  245. /* Apply distribute list to the interface. */
  246. ripng_distribute_update_interface (ifp);
  247. /* Check interface routemap. */
  248. ripng_if_rmap_update_interface (ifp);
  249. return 0;
  250. }
  251. int
  252. ripng_interface_delete (int command, struct zclient *zclient,
  253. zebra_size_t length)
  254. {
  255. struct interface *ifp;
  256. struct stream *s;
  257. s = zclient->ibuf;
  258. /* zebra_interface_state_read() updates interface structure in iflist */
  259. ifp = zebra_interface_state_read(s);
  260. if (ifp == NULL)
  261. return 0;
  262. if (if_is_up (ifp)) {
  263. ripng_if_down(ifp);
  264. }
  265. zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
  266. ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
  267. /* To support pseudo interface do not free interface structure. */
  268. /* if_delete(ifp); */
  269. return 0;
  270. }
  271. void
  272. ripng_interface_clean ()
  273. {
  274. listnode node;
  275. struct interface *ifp;
  276. struct ripng_interface *ri;
  277. for (node = listhead (iflist); node; nextnode (node))
  278. {
  279. ifp = getdata (node);
  280. ri = ifp->info;
  281. ri->enable_network = 0;
  282. ri->enable_interface = 0;
  283. ri->running = 0;
  284. if (ri->t_wakeup)
  285. {
  286. thread_cancel (ri->t_wakeup);
  287. ri->t_wakeup = NULL;
  288. }
  289. }
  290. }
  291. void
  292. ripng_interface_reset () {
  293. listnode node;
  294. struct interface *ifp;
  295. struct ripng_interface *ri;
  296. for (node = listhead (iflist); node; nextnode (node))
  297. {
  298. ifp = getdata (node);
  299. ri = ifp->info;
  300. ri->enable_network = 0;
  301. ri->enable_interface = 0;
  302. ri->running = 0;
  303. ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
  304. ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
  305. ri->list[RIPNG_FILTER_IN] = NULL;
  306. ri->list[RIPNG_FILTER_OUT] = NULL;
  307. ri->prefix[RIPNG_FILTER_IN] = NULL;
  308. ri->prefix[RIPNG_FILTER_OUT] = NULL;
  309. if (ri->t_wakeup)
  310. {
  311. thread_cancel (ri->t_wakeup);
  312. ri->t_wakeup = NULL;
  313. }
  314. ri->passive = 0;
  315. }
  316. }
  317. static void
  318. ripng_apply_address_add (struct connected *ifc) {
  319. struct prefix_ipv6 address;
  320. struct prefix *p;
  321. if (!ripng)
  322. return;
  323. if (! if_is_up(ifc->ifp))
  324. return;
  325. p = ifc->address;
  326. memset (&address, 0, sizeof (address));
  327. address.family = p->family;
  328. address.prefix = p->u.prefix6;
  329. address.prefixlen = p->prefixlen;
  330. apply_mask_ipv6(&address);
  331. /* Check if this interface is RIP enabled or not
  332. or Check if this address's prefix is RIP enabled */
  333. if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
  334. (ripng_enable_network_lookup2(ifc) >= 0))
  335. ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
  336. &address, ifc->ifp->ifindex, NULL);
  337. }
  338. int
  339. ripng_interface_address_add (int command, struct zclient *zclient,
  340. zebra_size_t length)
  341. {
  342. struct connected *c;
  343. struct prefix *p;
  344. c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
  345. zclient->ibuf);
  346. if (c == NULL)
  347. return 0;
  348. p = c->address;
  349. if (p->family == AF_INET6)
  350. {
  351. if (IS_RIPNG_DEBUG_ZEBRA)
  352. zlog_info ("RIPng connected address %s/%d add",
  353. inet6_ntop(&p->u.prefix6),
  354. p->prefixlen);
  355. /* Check is this prefix needs to be redistributed. */
  356. ripng_apply_address_add(c);
  357. /* Let's try once again whether the interface could be activated */
  358. if (c->ifp) {
  359. struct ripng_interface *ri = c->ifp->info;
  360. if (!ri->running) {
  361. /* Check if this interface is RIP enabled or not.*/
  362. ripng_enable_apply (c->ifp);
  363. /* Apply distribute list to the interface. */
  364. ripng_distribute_update_interface (c->ifp);
  365. /* Check interface routemap. */
  366. ripng_if_rmap_update_interface (c->ifp);
  367. }
  368. }
  369. }
  370. return 0;
  371. }
  372. static void
  373. ripng_apply_address_del (struct connected *ifc) {
  374. struct prefix_ipv6 address;
  375. struct prefix *p;
  376. if (!ripng)
  377. return;
  378. if (! if_is_up(ifc->ifp))
  379. return;
  380. p = ifc->address;
  381. memset (&address, 0, sizeof (address));
  382. address.family = p->family;
  383. address.prefix = p->u.prefix6;
  384. address.prefixlen = p->prefixlen;
  385. apply_mask_ipv6(&address);
  386. ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
  387. &address, ifc->ifp->ifindex);
  388. }
  389. int
  390. ripng_interface_address_delete (int command, struct zclient *zclient,
  391. zebra_size_t length)
  392. {
  393. struct connected *ifc;
  394. struct prefix *p;
  395. char buf[INET6_ADDRSTRLEN];
  396. ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
  397. zclient->ibuf);
  398. if (ifc)
  399. {
  400. p = ifc->address;
  401. if (p->family == AF_INET6)
  402. {
  403. if (IS_RIPNG_DEBUG_ZEBRA)
  404. zlog_info ("RIPng connected address %s/%d delete",
  405. inet_ntop (AF_INET6, &p->u.prefix6, buf,
  406. INET6_ADDRSTRLEN),
  407. p->prefixlen);
  408. /* Check wether this prefix needs to be removed. */
  409. ripng_apply_address_del(ifc);
  410. }
  411. connected_free (ifc);
  412. }
  413. return 0;
  414. }
  415. /* RIPng enable interface vector. */
  416. vector ripng_enable_if;
  417. /* RIPng enable network table. */
  418. struct route_table *ripng_enable_network;
  419. /* Lookup RIPng enable network. */
  420. /* Check wether the interface has at least a connected prefix that
  421. * is within the ripng_enable_network table. */
  422. int
  423. ripng_enable_network_lookup_if (struct interface *ifp)
  424. {
  425. listnode listnode;
  426. struct connected *connected;
  427. struct prefix_ipv6 address;
  428. for (listnode = listhead (ifp->connected); listnode; nextnode (listnode))
  429. if ((connected = getdata (listnode)) != NULL)
  430. {
  431. struct prefix *p;
  432. struct route_node *node;
  433. p = connected->address;
  434. if (p->family == AF_INET6)
  435. {
  436. address.family = AF_INET6;
  437. address.prefix = p->u.prefix6;
  438. address.prefixlen = IPV6_MAX_BITLEN;
  439. node = route_node_match (ripng_enable_network,
  440. (struct prefix *)&address);
  441. if (node)
  442. {
  443. route_unlock_node (node);
  444. return 1;
  445. }
  446. }
  447. }
  448. return -1;
  449. }
  450. /* Check wether connected is within the ripng_enable_network table. */
  451. int
  452. ripng_enable_network_lookup2 (struct connected *connected)
  453. {
  454. struct prefix_ipv6 address;
  455. struct prefix *p;
  456. p = connected->address;
  457. if (p->family == AF_INET6) {
  458. struct route_node *node;
  459. address.family = p->family;
  460. address.prefix = p->u.prefix6;
  461. address.prefixlen = IPV6_MAX_BITLEN;
  462. /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
  463. node = route_node_match (ripng_enable_network,
  464. (struct prefix *)&address);
  465. if (node) {
  466. route_unlock_node (node);
  467. return 1;
  468. }
  469. }
  470. return -1;
  471. }
  472. /* Add RIPng enable network. */
  473. int
  474. ripng_enable_network_add (struct prefix *p)
  475. {
  476. struct route_node *node;
  477. node = route_node_get (ripng_enable_network, p);
  478. if (node->info)
  479. {
  480. route_unlock_node (node);
  481. return -1;
  482. }
  483. else
  484. node->info = "enabled";
  485. /* XXX: One should find a better solution than a generic one */
  486. ripng_enable_apply_all();
  487. return 1;
  488. }
  489. /* Delete RIPng enable network. */
  490. int
  491. ripng_enable_network_delete (struct prefix *p)
  492. {
  493. struct route_node *node;
  494. node = route_node_lookup (ripng_enable_network, p);
  495. if (node)
  496. {
  497. node->info = NULL;
  498. /* Unlock info lock. */
  499. route_unlock_node (node);
  500. /* Unlock lookup lock. */
  501. route_unlock_node (node);
  502. return 1;
  503. }
  504. return -1;
  505. }
  506. /* Lookup function. */
  507. int
  508. ripng_enable_if_lookup (char *ifname)
  509. {
  510. int i;
  511. char *str;
  512. for (i = 0; i < vector_max (ripng_enable_if); i++)
  513. if ((str = vector_slot (ripng_enable_if, i)) != NULL)
  514. if (strcmp (str, ifname) == 0)
  515. return i;
  516. return -1;
  517. }
  518. /* Add interface to ripng_enable_if. */
  519. int
  520. ripng_enable_if_add (char *ifname)
  521. {
  522. int ret;
  523. ret = ripng_enable_if_lookup (ifname);
  524. if (ret >= 0)
  525. return -1;
  526. vector_set (ripng_enable_if, strdup (ifname));
  527. ripng_enable_apply_all();
  528. return 1;
  529. }
  530. /* Delete interface from ripng_enable_if. */
  531. int
  532. ripng_enable_if_delete (char *ifname)
  533. {
  534. int index;
  535. char *str;
  536. index = ripng_enable_if_lookup (ifname);
  537. if (index < 0)
  538. return -1;
  539. str = vector_slot (ripng_enable_if, index);
  540. free (str);
  541. vector_unset (ripng_enable_if, index);
  542. ripng_enable_apply_all();
  543. return 1;
  544. }
  545. /* Wake up interface. */
  546. int
  547. ripng_interface_wakeup (struct thread *t)
  548. {
  549. struct interface *ifp;
  550. struct ripng_interface *ri;
  551. /* Get interface. */
  552. ifp = THREAD_ARG (t);
  553. ri = ifp->info;
  554. ri->t_wakeup = NULL;
  555. /* Join to multicast group. */
  556. if (ripng_multicast_join (ifp) < 0) {
  557. zlog_err ("multicast join failed, interface %s not running", ifp->name);
  558. return 0;
  559. }
  560. /* Set running flag. */
  561. ri->running = 1;
  562. /* Send RIP request to the interface. */
  563. ripng_request (ifp);
  564. return 0;
  565. }
  566. int ripng_redistribute_check (int);
  567. void
  568. ripng_connect_set (struct interface *ifp, int set)
  569. {
  570. struct listnode *nn;
  571. struct connected *connected;
  572. struct prefix_ipv6 address;
  573. for (nn = listhead (ifp->connected); nn; nextnode (nn))
  574. if ((connected = getdata (nn)) != NULL) {
  575. struct prefix *p;
  576. p = connected->address;
  577. if (p->family != AF_INET6)
  578. continue;
  579. address.family = AF_INET6;
  580. address.prefix = p->u.prefix6;
  581. address.prefixlen = p->prefixlen;
  582. apply_mask_ipv6 (&address);
  583. if (set) {
  584. /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
  585. if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
  586. (ripng_enable_network_lookup2(connected) >= 0))
  587. ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
  588. &address, connected->ifp->ifindex, NULL);
  589. } else {
  590. ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
  591. &address, connected->ifp->ifindex);
  592. if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
  593. ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
  594. &address, connected->ifp->ifindex, NULL);
  595. }
  596. }
  597. }
  598. /* Check RIPng is enabed on this interface. */
  599. void
  600. ripng_enable_apply (struct interface *ifp)
  601. {
  602. int ret;
  603. struct ripng_interface *ri = NULL;
  604. /* Check interface. */
  605. if (! if_is_up (ifp))
  606. return;
  607. ri = ifp->info;
  608. /* Is this interface a candidate for RIPng ? */
  609. ret = ripng_enable_network_lookup_if (ifp);
  610. /* If the interface is matched. */
  611. if (ret > 0)
  612. ri->enable_network = 1;
  613. else
  614. ri->enable_network = 0;
  615. /* Check interface name configuration. */
  616. ret = ripng_enable_if_lookup (ifp->name);
  617. if (ret >= 0)
  618. ri->enable_interface = 1;
  619. else
  620. ri->enable_interface = 0;
  621. /* any candidate interface MUST have a link-local IPv6 address */
  622. if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
  623. (ri->enable_network || ri->enable_interface)) {
  624. ri->enable_network = 0;
  625. ri->enable_interface = 0;
  626. zlog_warn("Interface %s does not have any link-local address",
  627. ifp->name);
  628. }
  629. /* Update running status of the interface. */
  630. if (ri->enable_network || ri->enable_interface)
  631. {
  632. {
  633. if (IS_RIPNG_DEBUG_EVENT)
  634. zlog_info ("RIPng turn on %s", ifp->name);
  635. /* Add interface wake up thread. */
  636. if (! ri->t_wakeup)
  637. ri->t_wakeup = thread_add_timer (master, ripng_interface_wakeup,
  638. ifp, 1);
  639. ripng_connect_set (ifp, 1);
  640. }
  641. }
  642. else
  643. {
  644. if (ri->running)
  645. {
  646. /* Might as well clean up the route table as well
  647. * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
  648. **/
  649. ripng_if_down(ifp);
  650. ripng_connect_set (ifp, 0);
  651. }
  652. }
  653. }
  654. /* Set distribute list to all interfaces. */
  655. void
  656. ripng_enable_apply_all ()
  657. {
  658. struct interface *ifp;
  659. listnode node;
  660. for (node = listhead (iflist); node; nextnode (node))
  661. {
  662. ifp = getdata (node);
  663. ripng_enable_apply (ifp);
  664. }
  665. }
  666. /* Clear all network and neighbor configuration */
  667. void
  668. ripng_clean_network ()
  669. {
  670. int i;
  671. char *str;
  672. struct route_node *rn;
  673. /* ripng_enable_network */
  674. for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
  675. if (rn->info) {
  676. rn->info = NULL;
  677. route_unlock_node(rn);
  678. }
  679. /* ripng_enable_if */
  680. for (i = 0; i < vector_max (ripng_enable_if); i++)
  681. if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
  682. free (str);
  683. vector_slot (ripng_enable_if, i) = NULL;
  684. }
  685. }
  686. /* Vector to store passive-interface name. */
  687. vector Vripng_passive_interface;
  688. /* Utility function for looking up passive interface settings. */
  689. int
  690. ripng_passive_interface_lookup (char *ifname)
  691. {
  692. int i;
  693. char *str;
  694. for (i = 0; i < vector_max (Vripng_passive_interface); i++)
  695. if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
  696. if (strcmp (str, ifname) == 0)
  697. return i;
  698. return -1;
  699. }
  700. void
  701. ripng_passive_interface_apply (struct interface *ifp)
  702. {
  703. int ret;
  704. struct ripng_interface *ri;
  705. ri = ifp->info;
  706. ret = ripng_passive_interface_lookup (ifp->name);
  707. if (ret < 0)
  708. ri->passive = 0;
  709. else
  710. ri->passive = 1;
  711. }
  712. void
  713. ripng_passive_interface_apply_all (void)
  714. {
  715. struct interface *ifp;
  716. listnode node;
  717. for (node = listhead (iflist); node; nextnode (node))
  718. {
  719. ifp = getdata (node);
  720. ripng_passive_interface_apply (ifp);
  721. }
  722. }
  723. /* Passive interface. */
  724. int
  725. ripng_passive_interface_set (struct vty *vty, char *ifname)
  726. {
  727. if (ripng_passive_interface_lookup (ifname) >= 0)
  728. return CMD_WARNING;
  729. vector_set (Vripng_passive_interface, strdup (ifname));
  730. ripng_passive_interface_apply_all ();
  731. return CMD_SUCCESS;
  732. }
  733. int
  734. ripng_passive_interface_unset (struct vty *vty, char *ifname)
  735. {
  736. int i;
  737. char *str;
  738. i = ripng_passive_interface_lookup (ifname);
  739. if (i < 0)
  740. return CMD_WARNING;
  741. str = vector_slot (Vripng_passive_interface, i);
  742. free (str);
  743. vector_unset (Vripng_passive_interface, i);
  744. ripng_passive_interface_apply_all ();
  745. return CMD_SUCCESS;
  746. }
  747. /* Free all configured RIP passive-interface settings. */
  748. void
  749. ripng_passive_interface_clean (void)
  750. {
  751. int i;
  752. char *str;
  753. for (i = 0; i < vector_max (Vripng_passive_interface); i++)
  754. if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
  755. {
  756. free (str);
  757. vector_slot (Vripng_passive_interface, i) = NULL;
  758. }
  759. ripng_passive_interface_apply_all ();
  760. }
  761. /* Write RIPng enable network and interface to the vty. */
  762. int
  763. ripng_network_write (struct vty *vty, int config_mode)
  764. {
  765. int i;
  766. char *ifname;
  767. struct route_node *node;
  768. char buf[BUFSIZ];
  769. /* Write enable network. */
  770. for (node = route_top (ripng_enable_network); node; node = route_next (node))
  771. if (node->info)
  772. {
  773. struct prefix *p = &node->p;
  774. vty_out (vty, "%s%s/%d%s",
  775. config_mode ? " network " : " ",
  776. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  777. p->prefixlen,
  778. VTY_NEWLINE);
  779. }
  780. /* Write enable interface. */
  781. for (i = 0; i < vector_max (ripng_enable_if); i++)
  782. if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
  783. vty_out (vty, "%s%s%s",
  784. config_mode ? " network " : " ",
  785. ifname,
  786. VTY_NEWLINE);
  787. /* Write passive interface. */
  788. if (config_mode)
  789. for (i = 0; i < vector_max (Vripng_passive_interface); i++)
  790. if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
  791. vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
  792. return 0;
  793. }
  794. /* RIPng enable on specified interface or matched network. */
  795. DEFUN (ripng_network,
  796. ripng_network_cmd,
  797. "network IF_OR_ADDR",
  798. "RIPng enable on specified interface or network.\n"
  799. "Interface or address")
  800. {
  801. int ret;
  802. struct prefix p;
  803. ret = str2prefix (argv[0], &p);
  804. /* Given string is IPv6 network or interface name. */
  805. if (ret)
  806. ret = ripng_enable_network_add (&p);
  807. else
  808. ret = ripng_enable_if_add (argv[0]);
  809. if (ret < 0)
  810. {
  811. vty_out (vty, "There is same network configuration %s%s", argv[0],
  812. VTY_NEWLINE);
  813. return CMD_WARNING;
  814. }
  815. return CMD_SUCCESS;
  816. }
  817. /* RIPng enable on specified interface or matched network. */
  818. DEFUN (no_ripng_network,
  819. no_ripng_network_cmd,
  820. "no network IF_OR_ADDR",
  821. NO_STR
  822. "RIPng enable on specified interface or network.\n"
  823. "Interface or address")
  824. {
  825. int ret;
  826. struct prefix p;
  827. ret = str2prefix (argv[0], &p);
  828. /* Given string is interface name. */
  829. if (ret)
  830. ret = ripng_enable_network_delete (&p);
  831. else
  832. ret = ripng_enable_if_delete (argv[0]);
  833. if (ret < 0)
  834. {
  835. vty_out (vty, "can't find network %s%s", argv[0],
  836. VTY_NEWLINE);
  837. return CMD_WARNING;
  838. }
  839. return CMD_SUCCESS;
  840. }
  841. DEFUN (ipv6_ripng_split_horizon,
  842. ipv6_ripng_split_horizon_cmd,
  843. "ipv6 ripng split-horizon",
  844. IPV6_STR
  845. "Routing Information Protocol\n"
  846. "Perform split horizon\n")
  847. {
  848. struct interface *ifp;
  849. struct ripng_interface *ri;
  850. ifp = vty->index;
  851. ri = ifp->info;
  852. ri->split_horizon = RIPNG_SPLIT_HORIZON;
  853. return CMD_SUCCESS;
  854. }
  855. DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
  856. ipv6_ripng_split_horizon_poisoned_reverse_cmd,
  857. "ipv6 ripng split-horizon poisoned-reverse",
  858. IPV6_STR
  859. "Routing Information Protocol\n"
  860. "Perform split horizon\n"
  861. "With poisoned-reverse\n")
  862. {
  863. struct interface *ifp;
  864. struct ripng_interface *ri;
  865. ifp = vty->index;
  866. ri = ifp->info;
  867. ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
  868. return CMD_SUCCESS;
  869. }
  870. DEFUN (no_ipv6_ripng_split_horizon,
  871. no_ipv6_ripng_split_horizon_cmd,
  872. "no ipv6 ripng split-horizon",
  873. NO_STR
  874. IPV6_STR
  875. "Routing Information Protocol\n"
  876. "Perform split horizon\n")
  877. {
  878. struct interface *ifp;
  879. struct ripng_interface *ri;
  880. ifp = vty->index;
  881. ri = ifp->info;
  882. ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
  883. return CMD_SUCCESS;
  884. }
  885. ALIAS (no_ipv6_ripng_split_horizon,
  886. no_ipv6_ripng_split_horizon_poisoned_reverse_cmd,
  887. "no ipv6 ripng split-horizon poisoned-reverse",
  888. NO_STR
  889. IPV6_STR
  890. "Routing Information Protocol\n"
  891. "Perform split horizon\n"
  892. "With poisoned-reverse\n")
  893. DEFUN (ripng_passive_interface,
  894. ripng_passive_interface_cmd,
  895. "passive-interface IFNAME",
  896. "Suppress routing updates on an interface\n"
  897. "Interface name\n")
  898. {
  899. return ripng_passive_interface_set (vty, argv[0]);
  900. }
  901. DEFUN (no_ripng_passive_interface,
  902. no_ripng_passive_interface_cmd,
  903. "no passive-interface IFNAME",
  904. NO_STR
  905. "Suppress routing updates on an interface\n"
  906. "Interface name\n")
  907. {
  908. return ripng_passive_interface_unset (vty, argv[0]);
  909. }
  910. struct ripng_interface *
  911. ri_new ()
  912. {
  913. struct ripng_interface *ri;
  914. ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
  915. /* Set default split-horizon behavior. If the interface is Frame
  916. Relay or SMDS is enabled, the default value for split-horizon is
  917. off. But currently Zebra does detect Frame Relay or SMDS
  918. interface. So all interface is set to split horizon. */
  919. ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
  920. ri->split_horizon = ri->split_horizon_default;
  921. return ri;
  922. }
  923. int
  924. ripng_if_new_hook (struct interface *ifp)
  925. {
  926. ifp->info = ri_new ();
  927. return 0;
  928. }
  929. /* Called when interface structure deleted. */
  930. int
  931. ripng_if_delete_hook (struct interface *ifp)
  932. {
  933. XFREE (MTYPE_IF, ifp->info);
  934. ifp->info = NULL;
  935. return 0;
  936. }
  937. /* Configuration write function for ripngd. */
  938. int
  939. interface_config_write (struct vty *vty)
  940. {
  941. listnode node;
  942. struct interface *ifp;
  943. struct ripng_interface *ri;
  944. int write = 0;
  945. for (node = listhead (iflist); node; nextnode (node))
  946. {
  947. ifp = getdata (node);
  948. ri = ifp->info;
  949. /* Do not display the interface if there is no
  950. * configuration about it.
  951. **/
  952. if ((!ifp->desc) &&
  953. (ri->split_horizon == ri->split_horizon_default))
  954. continue;
  955. vty_out (vty, "interface %s%s", ifp->name,
  956. VTY_NEWLINE);
  957. if (ifp->desc)
  958. vty_out (vty, " description %s%s", ifp->desc,
  959. VTY_NEWLINE);
  960. /* Split horizon. */
  961. if (ri->split_horizon != ri->split_horizon_default)
  962. {
  963. switch (ri->split_horizon) {
  964. case RIPNG_SPLIT_HORIZON:
  965. vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
  966. break;
  967. case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
  968. vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
  969. VTY_NEWLINE);
  970. break;
  971. case RIPNG_NO_SPLIT_HORIZON:
  972. default:
  973. vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
  974. break;
  975. }
  976. }
  977. vty_out (vty, "!%s", VTY_NEWLINE);
  978. write++;
  979. }
  980. return write;
  981. }
  982. /* ripngd's interface node. */
  983. struct cmd_node interface_node =
  984. {
  985. INTERFACE_NODE,
  986. "%s(config-if)# ",
  987. 1 /* VTYSH */
  988. };
  989. /* Initialization of interface. */
  990. void
  991. ripng_if_init ()
  992. {
  993. /* Interface initialize. */
  994. iflist = list_new ();
  995. if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
  996. if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
  997. /* RIPng enable network init. */
  998. ripng_enable_network = route_table_init ();
  999. /* RIPng enable interface init. */
  1000. ripng_enable_if = vector_init (1);
  1001. /* RIPng passive interface. */
  1002. Vripng_passive_interface = vector_init (1);
  1003. /* Install interface node. */
  1004. install_node (&interface_node, interface_config_write);
  1005. /* Install commands. */
  1006. install_element (CONFIG_NODE, &interface_cmd);
  1007. install_element (CONFIG_NODE, &no_interface_cmd);
  1008. install_default (INTERFACE_NODE);
  1009. install_element (INTERFACE_NODE, &interface_desc_cmd);
  1010. install_element (INTERFACE_NODE, &no_interface_desc_cmd);
  1011. install_element (RIPNG_NODE, &ripng_network_cmd);
  1012. install_element (RIPNG_NODE, &no_ripng_network_cmd);
  1013. install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
  1014. install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
  1015. install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
  1016. install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
  1017. install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
  1018. install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_poisoned_reverse_cmd);
  1019. }