ripng_interface.c 30 KB

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