[ospfd] Fix OSPF route refcount leak
[jleu-quagga.git] / ripngd / ripng_interface.c
blobd70c299d3e784c4b4bd3eb641c33111df9008fe6
1 /*
2 * Interface related function for RIPng.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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.
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.
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.
23 #include <zebra.h>
25 #include "linklist.h"
26 #include "if.h"
27 #include "prefix.h"
28 #include "memory.h"
29 #include "network.h"
30 #include "filter.h"
31 #include "log.h"
32 #include "stream.h"
33 #include "zclient.h"
34 #include "command.h"
35 #include "table.h"
36 #include "thread.h"
37 #include "privs.h"
39 #include "ripngd/ripngd.h"
40 #include "ripngd/ripng_debug.h"
42 /* If RFC2133 definition is used. */
43 #ifndef IPV6_JOIN_GROUP
44 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
45 #endif
46 #ifndef IPV6_LEAVE_GROUP
47 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
48 #endif
50 extern struct zebra_privs_t ripngd_privs;
52 /* Static utility function. */
53 static void ripng_enable_apply (struct interface *);
54 static void ripng_passive_interface_apply (struct interface *);
55 static int ripng_enable_if_lookup (const char *);
56 static int ripng_enable_network_lookup2 (struct connected *);
57 static void ripng_enable_apply_all (void);
59 /* Join to the all rip routers multicast group. */
60 static int
61 ripng_multicast_join (struct interface *ifp)
63 int ret;
64 struct ipv6_mreq mreq;
65 int save_errno;
67 if (if_is_up (ifp) && if_is_multicast (ifp)) {
68 memset (&mreq, 0, sizeof (mreq));
69 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
70 mreq.ipv6mr_interface = ifp->ifindex;
73 * NetBSD 1.6.2 requires root to join groups on gif(4).
74 * While this is bogus, privs are available and easy to use
75 * for this call as a workaround.
77 if (ripngd_privs.change (ZPRIVS_RAISE))
78 zlog_err ("ripng_multicast_join: could not raise privs");
80 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
81 (char *) &mreq, sizeof (mreq));
82 save_errno = errno;
84 if (ripngd_privs.change (ZPRIVS_LOWER))
85 zlog_err ("ripng_multicast_join: could not lower privs");
87 if (ret < 0 && save_errno == EADDRINUSE)
90 * Group is already joined. This occurs due to sloppy group
91 * management, in particular declining to leave the group on
92 * an interface that has just gone down.
94 zlog_warn ("ripng join on %s EADDRINUSE (ignoring)\n", ifp->name);
95 return 0; /* not an error */
98 if (ret < 0)
99 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s",
100 safe_strerror (save_errno));
102 if (IS_RIPNG_DEBUG_EVENT)
103 zlog_debug ("RIPng %s join to all-rip-routers multicast group", ifp->name);
105 if (ret < 0)
106 return -1;
108 return 0;
111 /* Leave from the all rip routers multicast group. */
112 static int
113 ripng_multicast_leave (struct interface *ifp)
115 int ret;
116 struct ipv6_mreq mreq;
118 if (if_is_up (ifp) && if_is_multicast (ifp)) {
119 memset (&mreq, 0, sizeof (mreq));
120 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
121 mreq.ipv6mr_interface = ifp->ifindex;
123 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
124 (char *) &mreq, sizeof (mreq));
125 if (ret < 0)
126 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", safe_strerror (errno));
128 if (IS_RIPNG_DEBUG_EVENT)
129 zlog_debug ("RIPng %s leave from all-rip-routers multicast group",
130 ifp->name);
132 if (ret < 0)
133 return -1;
136 return 0;
139 /* How many link local IPv6 address could be used on the interface ? */
140 static int
141 ripng_if_ipv6_lladdress_check (struct interface *ifp)
143 struct listnode *nn;
144 struct connected *connected;
145 int count = 0;
147 for (ALL_LIST_ELEMENTS_RO (ifp->connected, nn, connected))
149 struct prefix *p;
150 p = connected->address;
152 if ((p->family == AF_INET6) &&
153 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
154 count++;
157 return count;
160 /* Check max mtu size. */
161 static unsigned int
162 ripng_check_max_mtu (void)
164 struct listnode *node;
165 struct interface *ifp;
166 unsigned int mtu;
168 mtu = 0;
169 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
170 if (mtu < ifp->mtu6)
171 mtu = ifp->mtu6;
173 return mtu;
176 static int
177 ripng_if_down (struct interface *ifp)
179 struct route_node *rp;
180 struct ripng_info *rinfo;
181 struct ripng_interface *ri;
183 if (ripng)
185 for (rp = route_top (ripng->table); rp; rp = route_next (rp))
186 if ((rinfo = rp->info) != NULL)
188 /* Routes got through this interface. */
189 if (rinfo->ifindex == ifp->ifindex
190 && rinfo->type == ZEBRA_ROUTE_RIPNG
191 && rinfo->sub_type == RIPNG_ROUTE_RTE)
193 ripng_zebra_ipv6_delete ((struct prefix_ipv6 *) &rp->p,
194 &rinfo->nexthop,
195 rinfo->ifindex);
197 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
198 (struct prefix_ipv6 *)&rp->p,
199 rinfo->ifindex);
201 else
203 /* All redistributed routes got through this interface,
204 * but the static and system ones are kept. */
205 if ((rinfo->ifindex == ifp->ifindex) &&
206 (rinfo->type != ZEBRA_ROUTE_STATIC) &&
207 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
208 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
209 (struct prefix_ipv6 *) &rp->p,
210 rinfo->ifindex);
215 ri = ifp->info;
217 if (ri->running)
219 if (IS_RIPNG_DEBUG_EVENT)
220 zlog_debug ("turn off %s", ifp->name);
222 /* Leave from multicast group. */
223 ripng_multicast_leave (ifp);
225 ri->running = 0;
228 return 0;
231 /* Inteface link up message processing. */
233 ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length)
235 struct stream *s;
236 struct interface *ifp;
238 /* zebra_interface_state_read() updates interface structure in iflist. */
239 s = zclient->ibuf;
240 ifp = zebra_interface_state_read (s);
242 if (ifp == NULL)
243 return 0;
245 if (IS_RIPNG_DEBUG_ZEBRA)
246 zlog_debug ("interface up %s index %d flags %ld metric %d mtu %d",
247 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
249 /* Check if this interface is RIPng enabled or not. */
250 ripng_enable_apply (ifp);
252 /* Check for a passive interface. */
253 ripng_passive_interface_apply (ifp);
255 /* Apply distribute list to the all interface. */
256 ripng_distribute_update_interface (ifp);
258 return 0;
261 /* Inteface link down message processing. */
263 ripng_interface_down (int command, struct zclient *zclient,
264 zebra_size_t length)
266 struct stream *s;
267 struct interface *ifp;
269 /* zebra_interface_state_read() updates interface structure in iflist. */
270 s = zclient->ibuf;
271 ifp = zebra_interface_state_read (s);
273 if (ifp == NULL)
274 return 0;
276 ripng_if_down (ifp);
278 if (IS_RIPNG_DEBUG_ZEBRA)
279 zlog_debug ("interface down %s index %d flags %ld metric %d mtu %d",
280 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
282 return 0;
285 /* Inteface addition message from zebra. */
287 ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length)
289 struct interface *ifp;
291 ifp = zebra_interface_add_read (zclient->ibuf);
293 if (IS_RIPNG_DEBUG_ZEBRA)
294 zlog_debug ("RIPng interface add %s index %d flags %ld metric %d mtu %d",
295 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
297 /* Check is this interface is RIP enabled or not.*/
298 ripng_enable_apply (ifp);
300 /* Apply distribute list to the interface. */
301 ripng_distribute_update_interface (ifp);
303 /* Check interface routemap. */
304 ripng_if_rmap_update_interface (ifp);
306 return 0;
310 ripng_interface_delete (int command, struct zclient *zclient,
311 zebra_size_t length)
313 struct interface *ifp;
314 struct stream *s;
316 s = zclient->ibuf;
317 /* zebra_interface_state_read() updates interface structure in iflist */
318 ifp = zebra_interface_state_read(s);
320 if (ifp == NULL)
321 return 0;
323 if (if_is_up (ifp)) {
324 ripng_if_down(ifp);
327 zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
328 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
330 /* To support pseudo interface do not free interface structure. */
331 /* if_delete(ifp); */
332 ifp->ifindex = IFINDEX_INTERNAL;
334 return 0;
337 void
338 ripng_interface_clean (void)
340 struct listnode *node, *nnode;
341 struct interface *ifp;
342 struct ripng_interface *ri;
344 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
346 ri = ifp->info;
348 ri->enable_network = 0;
349 ri->enable_interface = 0;
350 ri->running = 0;
352 if (ri->t_wakeup)
354 thread_cancel (ri->t_wakeup);
355 ri->t_wakeup = NULL;
360 void
361 ripng_interface_reset (void)
363 struct listnode *node;
364 struct interface *ifp;
365 struct ripng_interface *ri;
367 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
369 ri = ifp->info;
371 ri->enable_network = 0;
372 ri->enable_interface = 0;
373 ri->running = 0;
375 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
376 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
378 ri->list[RIPNG_FILTER_IN] = NULL;
379 ri->list[RIPNG_FILTER_OUT] = NULL;
381 ri->prefix[RIPNG_FILTER_IN] = NULL;
382 ri->prefix[RIPNG_FILTER_OUT] = NULL;
384 if (ri->t_wakeup)
386 thread_cancel (ri->t_wakeup);
387 ri->t_wakeup = NULL;
390 ri->passive = 0;
394 static void
395 ripng_apply_address_add (struct connected *ifc) {
396 struct prefix_ipv6 address;
397 struct prefix *p;
399 if (!ripng)
400 return;
402 if (! if_is_up(ifc->ifp))
403 return;
405 p = ifc->address;
407 memset (&address, 0, sizeof (address));
408 address.family = p->family;
409 address.prefix = p->u.prefix6;
410 address.prefixlen = p->prefixlen;
411 apply_mask_ipv6(&address);
413 /* Check if this interface is RIP enabled or not
414 or Check if this address's prefix is RIP enabled */
415 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
416 (ripng_enable_network_lookup2(ifc) >= 0))
417 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
418 &address, ifc->ifp->ifindex, NULL);
423 ripng_interface_address_add (int command, struct zclient *zclient,
424 zebra_size_t length)
426 struct connected *c;
427 struct prefix *p;
429 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
430 zclient->ibuf);
432 if (c == NULL)
433 return 0;
435 p = c->address;
437 if (p->family == AF_INET6)
439 struct ripng_interface *ri = c->ifp->info;
441 if (IS_RIPNG_DEBUG_ZEBRA)
442 zlog_debug ("RIPng connected address %s/%d add",
443 inet6_ntoa(p->u.prefix6),
444 p->prefixlen);
446 /* Check is this prefix needs to be redistributed. */
447 ripng_apply_address_add(c);
449 /* Let's try once again whether the interface could be activated */
450 if (!ri->running) {
451 /* Check if this interface is RIP enabled or not.*/
452 ripng_enable_apply (c->ifp);
454 /* Apply distribute list to the interface. */
455 ripng_distribute_update_interface (c->ifp);
457 /* Check interface routemap. */
458 ripng_if_rmap_update_interface (c->ifp);
463 return 0;
466 static void
467 ripng_apply_address_del (struct connected *ifc) {
468 struct prefix_ipv6 address;
469 struct prefix *p;
471 if (!ripng)
472 return;
474 if (! if_is_up(ifc->ifp))
475 return;
477 p = ifc->address;
479 memset (&address, 0, sizeof (address));
480 address.family = p->family;
481 address.prefix = p->u.prefix6;
482 address.prefixlen = p->prefixlen;
483 apply_mask_ipv6(&address);
485 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
486 &address, ifc->ifp->ifindex);
490 ripng_interface_address_delete (int command, struct zclient *zclient,
491 zebra_size_t length)
493 struct connected *ifc;
494 struct prefix *p;
495 char buf[INET6_ADDRSTRLEN];
497 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
498 zclient->ibuf);
500 if (ifc)
502 p = ifc->address;
504 if (p->family == AF_INET6)
506 if (IS_RIPNG_DEBUG_ZEBRA)
507 zlog_debug ("RIPng connected address %s/%d delete",
508 inet_ntop (AF_INET6, &p->u.prefix6, buf,
509 INET6_ADDRSTRLEN),
510 p->prefixlen);
512 /* Check wether this prefix needs to be removed. */
513 ripng_apply_address_del(ifc);
515 connected_free (ifc);
518 return 0;
521 /* RIPng enable interface vector. */
522 vector ripng_enable_if;
524 /* RIPng enable network table. */
525 struct route_table *ripng_enable_network;
527 /* Lookup RIPng enable network. */
528 /* Check wether the interface has at least a connected prefix that
529 * is within the ripng_enable_network table. */
530 static int
531 ripng_enable_network_lookup_if (struct interface *ifp)
533 struct listnode *node;
534 struct connected *connected;
535 struct prefix_ipv6 address;
537 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
539 struct prefix *p;
540 struct route_node *node;
542 p = connected->address;
544 if (p->family == AF_INET6)
546 address.family = AF_INET6;
547 address.prefix = p->u.prefix6;
548 address.prefixlen = IPV6_MAX_BITLEN;
550 node = route_node_match (ripng_enable_network,
551 (struct prefix *)&address);
552 if (node)
554 route_unlock_node (node);
555 return 1;
559 return -1;
562 /* Check wether connected is within the ripng_enable_network table. */
563 static int
564 ripng_enable_network_lookup2 (struct connected *connected)
566 struct prefix_ipv6 address;
567 struct prefix *p;
569 p = connected->address;
571 if (p->family == AF_INET6) {
572 struct route_node *node;
574 address.family = p->family;
575 address.prefix = p->u.prefix6;
576 address.prefixlen = IPV6_MAX_BITLEN;
578 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
579 node = route_node_match (ripng_enable_network,
580 (struct prefix *)&address);
582 if (node) {
583 route_unlock_node (node);
584 return 1;
588 return -1;
591 /* Add RIPng enable network. */
592 static int
593 ripng_enable_network_add (struct prefix *p)
595 struct route_node *node;
597 node = route_node_get (ripng_enable_network, p);
599 if (node->info)
601 route_unlock_node (node);
602 return -1;
604 else
605 node->info = (char *) "enabled";
607 /* XXX: One should find a better solution than a generic one */
608 ripng_enable_apply_all();
610 return 1;
613 /* Delete RIPng enable network. */
614 static int
615 ripng_enable_network_delete (struct prefix *p)
617 struct route_node *node;
619 node = route_node_lookup (ripng_enable_network, p);
620 if (node)
622 node->info = NULL;
624 /* Unlock info lock. */
625 route_unlock_node (node);
627 /* Unlock lookup lock. */
628 route_unlock_node (node);
630 return 1;
632 return -1;
635 /* Lookup function. */
636 static int
637 ripng_enable_if_lookup (const char *ifname)
639 unsigned int i;
640 char *str;
642 for (i = 0; i < vector_active (ripng_enable_if); i++)
643 if ((str = vector_slot (ripng_enable_if, i)) != NULL)
644 if (strcmp (str, ifname) == 0)
645 return i;
646 return -1;
649 /* Add interface to ripng_enable_if. */
650 static int
651 ripng_enable_if_add (const char *ifname)
653 int ret;
655 ret = ripng_enable_if_lookup (ifname);
656 if (ret >= 0)
657 return -1;
659 vector_set (ripng_enable_if, strdup (ifname));
661 ripng_enable_apply_all();
663 return 1;
666 /* Delete interface from ripng_enable_if. */
667 static int
668 ripng_enable_if_delete (const char *ifname)
670 int index;
671 char *str;
673 index = ripng_enable_if_lookup (ifname);
674 if (index < 0)
675 return -1;
677 str = vector_slot (ripng_enable_if, index);
678 free (str);
679 vector_unset (ripng_enable_if, index);
681 ripng_enable_apply_all();
683 return 1;
686 /* Wake up interface. */
687 static int
688 ripng_interface_wakeup (struct thread *t)
690 struct interface *ifp;
691 struct ripng_interface *ri;
693 /* Get interface. */
694 ifp = THREAD_ARG (t);
696 ri = ifp->info;
697 ri->t_wakeup = NULL;
699 /* Join to multicast group. */
700 if (ripng_multicast_join (ifp) < 0) {
701 zlog_err ("multicast join failed, interface %s not running", ifp->name);
702 return 0;
705 /* Set running flag. */
706 ri->running = 1;
708 /* Send RIP request to the interface. */
709 ripng_request (ifp);
711 return 0;
714 static void
715 ripng_connect_set (struct interface *ifp, int set)
717 struct listnode *node, *nnode;
718 struct connected *connected;
719 struct prefix_ipv6 address;
721 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
723 struct prefix *p;
724 p = connected->address;
726 if (p->family != AF_INET6)
727 continue;
729 address.family = AF_INET6;
730 address.prefix = p->u.prefix6;
731 address.prefixlen = p->prefixlen;
732 apply_mask_ipv6 (&address);
734 if (set) {
735 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
736 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
737 (ripng_enable_network_lookup2(connected) >= 0))
738 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
739 &address, connected->ifp->ifindex, NULL);
740 } else {
741 ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
742 &address, connected->ifp->ifindex);
743 if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
744 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
745 &address, connected->ifp->ifindex, NULL);
750 /* Check RIPng is enabed on this interface. */
751 void
752 ripng_enable_apply (struct interface *ifp)
754 int ret;
755 struct ripng_interface *ri = NULL;
757 /* Check interface. */
758 if (! if_is_up (ifp))
759 return;
761 ri = ifp->info;
763 /* Is this interface a candidate for RIPng ? */
764 ret = ripng_enable_network_lookup_if (ifp);
766 /* If the interface is matched. */
767 if (ret > 0)
768 ri->enable_network = 1;
769 else
770 ri->enable_network = 0;
772 /* Check interface name configuration. */
773 ret = ripng_enable_if_lookup (ifp->name);
774 if (ret >= 0)
775 ri->enable_interface = 1;
776 else
777 ri->enable_interface = 0;
779 /* any candidate interface MUST have a link-local IPv6 address */
780 if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
781 (ri->enable_network || ri->enable_interface)) {
782 ri->enable_network = 0;
783 ri->enable_interface = 0;
784 zlog_warn("Interface %s does not have any link-local address",
785 ifp->name);
788 /* Update running status of the interface. */
789 if (ri->enable_network || ri->enable_interface)
792 if (IS_RIPNG_DEBUG_EVENT)
793 zlog_debug ("RIPng turn on %s", ifp->name);
795 /* Add interface wake up thread. */
796 if (! ri->t_wakeup)
797 ri->t_wakeup = thread_add_timer (master, ripng_interface_wakeup,
798 ifp, 1);
800 ripng_connect_set (ifp, 1);
803 else
805 if (ri->running)
807 /* Might as well clean up the route table as well
808 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
810 ripng_if_down(ifp);
812 ripng_connect_set (ifp, 0);
817 /* Set distribute list to all interfaces. */
818 static void
819 ripng_enable_apply_all (void)
821 struct interface *ifp;
822 struct listnode *node;
824 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
825 ripng_enable_apply (ifp);
828 /* Clear all network and neighbor configuration */
829 void
830 ripng_clean_network ()
832 unsigned int i;
833 char *str;
834 struct route_node *rn;
836 /* ripng_enable_network */
837 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
838 if (rn->info) {
839 rn->info = NULL;
840 route_unlock_node(rn);
843 /* ripng_enable_if */
844 for (i = 0; i < vector_active (ripng_enable_if); i++)
845 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
846 free (str);
847 vector_slot (ripng_enable_if, i) = NULL;
851 /* Vector to store passive-interface name. */
852 vector Vripng_passive_interface;
854 /* Utility function for looking up passive interface settings. */
855 static int
856 ripng_passive_interface_lookup (const char *ifname)
858 unsigned int i;
859 char *str;
861 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
862 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
863 if (strcmp (str, ifname) == 0)
864 return i;
865 return -1;
868 void
869 ripng_passive_interface_apply (struct interface *ifp)
871 int ret;
872 struct ripng_interface *ri;
874 ri = ifp->info;
876 ret = ripng_passive_interface_lookup (ifp->name);
877 if (ret < 0)
878 ri->passive = 0;
879 else
880 ri->passive = 1;
883 static void
884 ripng_passive_interface_apply_all (void)
886 struct interface *ifp;
887 struct listnode *node;
889 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
890 ripng_passive_interface_apply (ifp);
893 /* Passive interface. */
894 static int
895 ripng_passive_interface_set (struct vty *vty, const char *ifname)
897 if (ripng_passive_interface_lookup (ifname) >= 0)
898 return CMD_WARNING;
900 vector_set (Vripng_passive_interface, strdup (ifname));
902 ripng_passive_interface_apply_all ();
904 return CMD_SUCCESS;
907 static int
908 ripng_passive_interface_unset (struct vty *vty, const char *ifname)
910 int i;
911 char *str;
913 i = ripng_passive_interface_lookup (ifname);
914 if (i < 0)
915 return CMD_WARNING;
917 str = vector_slot (Vripng_passive_interface, i);
918 free (str);
919 vector_unset (Vripng_passive_interface, i);
921 ripng_passive_interface_apply_all ();
923 return CMD_SUCCESS;
926 /* Free all configured RIP passive-interface settings. */
927 void
928 ripng_passive_interface_clean (void)
930 unsigned int i;
931 char *str;
933 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
934 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
936 free (str);
937 vector_slot (Vripng_passive_interface, i) = NULL;
939 ripng_passive_interface_apply_all ();
942 /* Write RIPng enable network and interface to the vty. */
944 ripng_network_write (struct vty *vty, int config_mode)
946 unsigned int i;
947 const char *ifname;
948 struct route_node *node;
949 char buf[BUFSIZ];
951 /* Write enable network. */
952 for (node = route_top (ripng_enable_network); node; node = route_next (node))
953 if (node->info)
955 struct prefix *p = &node->p;
956 vty_out (vty, "%s%s/%d%s",
957 config_mode ? " network " : " ",
958 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
959 p->prefixlen,
960 VTY_NEWLINE);
964 /* Write enable interface. */
965 for (i = 0; i < vector_active (ripng_enable_if); i++)
966 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
967 vty_out (vty, "%s%s%s",
968 config_mode ? " network " : " ",
969 ifname,
970 VTY_NEWLINE);
972 /* Write passive interface. */
973 if (config_mode)
974 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
975 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
976 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
978 return 0;
981 /* RIPng enable on specified interface or matched network. */
982 DEFUN (ripng_network,
983 ripng_network_cmd,
984 "network IF_OR_ADDR",
985 "RIPng enable on specified interface or network.\n"
986 "Interface or address")
988 int ret;
989 struct prefix p;
991 ret = str2prefix (argv[0], &p);
993 /* Given string is IPv6 network or interface name. */
994 if (ret)
995 ret = ripng_enable_network_add (&p);
996 else
997 ret = ripng_enable_if_add (argv[0]);
999 if (ret < 0)
1001 vty_out (vty, "There is same network configuration %s%s", argv[0],
1002 VTY_NEWLINE);
1003 return CMD_WARNING;
1006 return CMD_SUCCESS;
1009 /* RIPng enable on specified interface or matched network. */
1010 DEFUN (no_ripng_network,
1011 no_ripng_network_cmd,
1012 "no network IF_OR_ADDR",
1013 NO_STR
1014 "RIPng enable on specified interface or network.\n"
1015 "Interface or address")
1017 int ret;
1018 struct prefix p;
1020 ret = str2prefix (argv[0], &p);
1022 /* Given string is interface name. */
1023 if (ret)
1024 ret = ripng_enable_network_delete (&p);
1025 else
1026 ret = ripng_enable_if_delete (argv[0]);
1028 if (ret < 0)
1030 vty_out (vty, "can't find network %s%s", argv[0],
1031 VTY_NEWLINE);
1032 return CMD_WARNING;
1035 return CMD_SUCCESS;
1038 DEFUN (ipv6_ripng_split_horizon,
1039 ipv6_ripng_split_horizon_cmd,
1040 "ipv6 ripng split-horizon",
1041 IPV6_STR
1042 "Routing Information Protocol\n"
1043 "Perform split horizon\n")
1045 struct interface *ifp;
1046 struct ripng_interface *ri;
1048 ifp = vty->index;
1049 ri = ifp->info;
1051 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1052 return CMD_SUCCESS;
1055 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1056 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1057 "ipv6 ripng split-horizon poisoned-reverse",
1058 IPV6_STR
1059 "Routing Information Protocol\n"
1060 "Perform split horizon\n"
1061 "With poisoned-reverse\n")
1063 struct interface *ifp;
1064 struct ripng_interface *ri;
1066 ifp = vty->index;
1067 ri = ifp->info;
1069 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1070 return CMD_SUCCESS;
1073 DEFUN (no_ipv6_ripng_split_horizon,
1074 no_ipv6_ripng_split_horizon_cmd,
1075 "no ipv6 ripng split-horizon",
1076 NO_STR
1077 IPV6_STR
1078 "Routing Information Protocol\n"
1079 "Perform split horizon\n")
1081 struct interface *ifp;
1082 struct ripng_interface *ri;
1084 ifp = vty->index;
1085 ri = ifp->info;
1087 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1088 return CMD_SUCCESS;
1091 ALIAS (no_ipv6_ripng_split_horizon,
1092 no_ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1093 "no ipv6 ripng split-horizon poisoned-reverse",
1094 NO_STR
1095 IPV6_STR
1096 "Routing Information Protocol\n"
1097 "Perform split horizon\n"
1098 "With poisoned-reverse\n")
1100 DEFUN (ripng_passive_interface,
1101 ripng_passive_interface_cmd,
1102 "passive-interface IFNAME",
1103 "Suppress routing updates on an interface\n"
1104 "Interface name\n")
1106 return ripng_passive_interface_set (vty, argv[0]);
1109 DEFUN (no_ripng_passive_interface,
1110 no_ripng_passive_interface_cmd,
1111 "no passive-interface IFNAME",
1112 NO_STR
1113 "Suppress routing updates on an interface\n"
1114 "Interface name\n")
1116 return ripng_passive_interface_unset (vty, argv[0]);
1119 static struct ripng_interface *
1120 ri_new (void)
1122 struct ripng_interface *ri;
1123 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
1125 /* Set default split-horizon behavior. If the interface is Frame
1126 Relay or SMDS is enabled, the default value for split-horizon is
1127 off. But currently Zebra does detect Frame Relay or SMDS
1128 interface. So all interface is set to split horizon. */
1129 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1130 ri->split_horizon = ri->split_horizon_default;
1132 return ri;
1135 static int
1136 ripng_if_new_hook (struct interface *ifp)
1138 ifp->info = ri_new ();
1139 return 0;
1142 /* Called when interface structure deleted. */
1143 static int
1144 ripng_if_delete_hook (struct interface *ifp)
1146 XFREE (MTYPE_IF, ifp->info);
1147 ifp->info = NULL;
1148 return 0;
1151 /* Configuration write function for ripngd. */
1152 static int
1153 interface_config_write (struct vty *vty)
1155 struct listnode *node;
1156 struct interface *ifp;
1157 struct ripng_interface *ri;
1158 int write = 0;
1160 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1162 ri = ifp->info;
1164 /* Do not display the interface if there is no
1165 * configuration about it.
1167 if ((!ifp->desc) &&
1168 (ri->split_horizon == ri->split_horizon_default))
1169 continue;
1171 vty_out (vty, "interface %s%s", ifp->name,
1172 VTY_NEWLINE);
1173 if (ifp->desc)
1174 vty_out (vty, " description %s%s", ifp->desc,
1175 VTY_NEWLINE);
1177 /* Split horizon. */
1178 if (ri->split_horizon != ri->split_horizon_default)
1180 switch (ri->split_horizon) {
1181 case RIPNG_SPLIT_HORIZON:
1182 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1183 break;
1184 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1185 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1186 VTY_NEWLINE);
1187 break;
1188 case RIPNG_NO_SPLIT_HORIZON:
1189 default:
1190 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1191 break;
1195 vty_out (vty, "!%s", VTY_NEWLINE);
1197 write++;
1199 return write;
1202 /* ripngd's interface node. */
1203 static struct cmd_node interface_node =
1205 INTERFACE_NODE,
1206 "%s(config-if)# ",
1207 1 /* VTYSH */
1210 /* Initialization of interface. */
1211 void
1212 ripng_if_init ()
1214 /* Interface initialize. */
1215 iflist = list_new ();
1216 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
1217 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
1219 /* RIPng enable network init. */
1220 ripng_enable_network = route_table_init ();
1222 /* RIPng enable interface init. */
1223 ripng_enable_if = vector_init (1);
1225 /* RIPng passive interface. */
1226 Vripng_passive_interface = vector_init (1);
1228 /* Install interface node. */
1229 install_node (&interface_node, interface_config_write);
1231 /* Install commands. */
1232 install_element (CONFIG_NODE, &interface_cmd);
1233 install_element (CONFIG_NODE, &no_interface_cmd);
1234 install_default (INTERFACE_NODE);
1235 install_element (INTERFACE_NODE, &interface_desc_cmd);
1236 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1238 install_element (RIPNG_NODE, &ripng_network_cmd);
1239 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1240 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1241 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
1243 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1244 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1245 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1246 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_poisoned_reverse_cmd);