ripng_interface.c 29 KB

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