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
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "agg_table.h"
38 #include "lib_errors.h"
39 #include "northbound_cli.h"
41 #include "ripngd/ripngd.h"
42 #include "ripngd/ripng_debug.h"
44 /* If RFC2133 definition is used. */
45 #ifndef IPV6_JOIN_GROUP
46 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
48 #ifndef IPV6_LEAVE_GROUP
49 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
52 DEFINE_MTYPE_STATIC(RIPNGD
, RIPNG_IF
, "ripng interface");
54 /* Static utility function. */
55 static void ripng_enable_apply(struct interface
*);
56 static void ripng_passive_interface_apply(struct interface
*);
57 static int ripng_enable_if_lookup(struct ripng
*ripng
, const char *ifname
);
58 static int ripng_enable_network_lookup2(struct connected
*);
59 static void ripng_enable_apply_all(struct ripng
*ripng
);
61 /* Join to the all rip routers multicast group. */
62 static int ripng_multicast_join(struct interface
*ifp
, int sock
)
65 struct ipv6_mreq mreq
;
68 if (if_is_multicast(ifp
)) {
69 memset(&mreq
, 0, sizeof(mreq
));
70 inet_pton(AF_INET6
, RIPNG_GROUP
, &mreq
.ipv6mr_multiaddr
);
71 mreq
.ipv6mr_interface
= ifp
->ifindex
;
74 * NetBSD 1.6.2 requires root to join groups on gif(4).
75 * While this is bogus, privs are available and easy to use
76 * for this call as a workaround.
78 frr_with_privs(&ripngd_privs
) {
80 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
81 (char *)&mreq
, sizeof(mreq
));
86 if (ret
< 0 && save_errno
== EADDRINUSE
) {
88 * Group is already joined. This occurs due to sloppy
90 * management, in particular declining to leave the
92 * an interface that has just gone down.
94 zlog_warn("ripng join on %s EADDRINUSE (ignoring)",
96 return 0; /* not an error */
100 zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s",
101 safe_strerror(save_errno
));
103 if (IS_RIPNG_DEBUG_EVENT
)
105 "RIPng %s join to all-rip-routers multicast group",
114 /* Leave from the all rip routers multicast group. */
115 static int ripng_multicast_leave(struct interface
*ifp
, int sock
)
118 struct ipv6_mreq mreq
;
120 if (if_is_multicast(ifp
)) {
121 memset(&mreq
, 0, sizeof(mreq
));
122 inet_pton(AF_INET6
, RIPNG_GROUP
, &mreq
.ipv6mr_multiaddr
);
123 mreq
.ipv6mr_interface
= ifp
->ifindex
;
125 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
,
126 (char *)&mreq
, sizeof(mreq
));
128 zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s",
129 safe_strerror(errno
));
131 if (IS_RIPNG_DEBUG_EVENT
)
133 "RIPng %s leave from all-rip-routers multicast group",
143 /* How many link local IPv6 address could be used on the interface ? */
144 static int ripng_if_ipv6_lladdress_check(struct interface
*ifp
)
147 struct connected
*connected
;
150 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
152 p
= connected
->address
;
154 if ((p
->family
== AF_INET6
)
155 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
162 static int ripng_if_down(struct interface
*ifp
)
165 struct ripng_info
*rinfo
;
166 struct ripng_interface
*ri
;
168 struct list
*list
= NULL
;
169 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
173 THREAD_OFF(ri
->t_wakeup
);
178 for (rp
= agg_route_top(ripng
->table
); rp
;
179 rp
= agg_route_next(rp
))
180 if ((list
= rp
->info
) != NULL
)
181 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
183 if (rinfo
->ifindex
== ifp
->ifindex
)
184 ripng_ecmp_delete(ripng
, rinfo
);
188 if (IS_RIPNG_DEBUG_EVENT
)
189 zlog_debug("turn off %s", ifp
->name
);
191 /* Leave from multicast group. */
193 ripng_multicast_leave(ifp
, ripng
->sock
);
201 /* Interface link up message processing. */
202 static int ripng_ifp_up(struct interface
*ifp
)
204 if (IS_RIPNG_DEBUG_ZEBRA
)
206 "interface up %s vrf %s(%u) index %d flags %llx metric %d mtu %d",
207 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
208 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
209 ifp
->metric
, ifp
->mtu6
);
211 ripng_interface_sync(ifp
);
213 /* Check if this interface is RIPng enabled or not. */
214 ripng_enable_apply(ifp
);
216 /* Check for a passive interface. */
217 ripng_passive_interface_apply(ifp
);
219 /* Apply distribute list to the all interface. */
220 ripng_distribute_update_interface(ifp
);
225 /* Interface link down message processing. */
226 static int ripng_ifp_down(struct interface
*ifp
)
228 ripng_interface_sync(ifp
);
231 if (IS_RIPNG_DEBUG_ZEBRA
)
233 "interface down %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
234 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
235 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
236 ifp
->metric
, ifp
->mtu6
);
241 /* Interface addition message from zebra. */
242 static int ripng_ifp_create(struct interface
*ifp
)
244 ripng_interface_sync(ifp
);
246 if (IS_RIPNG_DEBUG_ZEBRA
)
248 "RIPng interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
249 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
250 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
251 ifp
->metric
, ifp
->mtu6
);
253 /* Check is this interface is RIP enabled or not.*/
254 ripng_enable_apply(ifp
);
256 /* Apply distribute list to the interface. */
257 ripng_distribute_update_interface(ifp
);
259 /* Check interface routemap. */
260 ripng_if_rmap_update_interface(ifp
);
265 static int ripng_ifp_destroy(struct interface
*ifp
)
267 ripng_interface_sync(ifp
);
272 if (IS_RIPNG_DEBUG_ZEBRA
)
274 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
275 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
276 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
277 ifp
->metric
, ifp
->mtu6
);
282 /* VRF update for an interface. */
283 int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
285 struct interface
*ifp
;
288 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
293 if (IS_RIPNG_DEBUG_ZEBRA
) {
294 struct vrf
*nvrf
= vrf_lookup_by_id(new_vrf_id
);
296 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
297 ifp
->name
, ifp
->vrf
->name
, vrf_id
, VRF_LOGNAME(nvrf
),
301 if_update_to_new_vrf(ifp
, new_vrf_id
);
302 ripng_interface_sync(ifp
);
307 void ripng_interface_clean(struct ripng
*ripng
)
309 struct interface
*ifp
;
310 struct ripng_interface
*ri
;
312 FOR_ALL_INTERFACES (ripng
->vrf
, ifp
) {
315 ri
->enable_network
= 0;
316 ri
->enable_interface
= 0;
319 THREAD_OFF(ri
->t_wakeup
);
323 static void ripng_apply_address_add(struct connected
*ifc
)
325 struct ripng_interface
*ri
= ifc
->ifp
->info
;
326 struct ripng
*ripng
= ri
->ripng
;
327 struct prefix_ipv6 address
;
333 if (!if_is_up(ifc
->ifp
))
338 memset(&address
, 0, sizeof(address
));
339 address
.family
= p
->family
;
340 address
.prefix
= p
->u
.prefix6
;
341 address
.prefixlen
= p
->prefixlen
;
342 apply_mask_ipv6(&address
);
344 /* Check if this interface is RIP enabled or not
345 or Check if this address's prefix is RIP enabled */
346 if ((ripng_enable_if_lookup(ripng
, ifc
->ifp
->name
) >= 0)
347 || (ripng_enable_network_lookup2(ifc
) >= 0))
348 ripng_redistribute_add(ripng
, ZEBRA_ROUTE_CONNECT
,
349 RIPNG_ROUTE_INTERFACE
, &address
,
350 ifc
->ifp
->ifindex
, NULL
, 0);
353 int ripng_interface_address_add(ZAPI_CALLBACK_ARGS
)
358 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
359 zclient
->ibuf
, vrf_id
);
366 if (p
->family
== AF_INET6
) {
367 struct ripng_interface
*ri
= c
->ifp
->info
;
369 if (IS_RIPNG_DEBUG_ZEBRA
)
370 zlog_debug("RIPng connected address %pFX add", p
);
372 /* Check is this prefix needs to be redistributed. */
373 ripng_apply_address_add(c
);
375 /* Let's try once again whether the interface could be activated
378 /* Check if this interface is RIP enabled or not.*/
379 ripng_enable_apply(c
->ifp
);
381 /* Apply distribute list to the interface. */
382 ripng_distribute_update_interface(c
->ifp
);
384 /* Check interface routemap. */
385 ripng_if_rmap_update_interface(c
->ifp
);
392 static void ripng_apply_address_del(struct connected
*ifc
)
394 struct ripng_interface
*ri
= ifc
->ifp
->info
;
395 struct ripng
*ripng
= ri
->ripng
;
396 struct prefix_ipv6 address
;
402 if (!if_is_up(ifc
->ifp
))
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 ripng_redistribute_delete(ripng
, ZEBRA_ROUTE_CONNECT
,
414 RIPNG_ROUTE_INTERFACE
, &address
,
418 int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS
)
420 struct connected
*ifc
;
423 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
424 zclient
->ibuf
, vrf_id
);
429 if (p
->family
== AF_INET6
) {
430 if (IS_RIPNG_DEBUG_ZEBRA
)
432 "RIPng connected address %pFX delete",
435 /* Check whether this prefix needs to be removed. */
436 ripng_apply_address_del(ifc
);
438 connected_free(&ifc
);
444 /* Lookup RIPng enable network. */
445 /* Check whether the interface has at least a connected prefix that
446 * is within the ripng->enable_network table. */
447 static int ripng_enable_network_lookup_if(struct interface
*ifp
)
449 struct ripng_interface
*ri
= ifp
->info
;
450 struct ripng
*ripng
= ri
->ripng
;
451 struct listnode
*node
;
452 struct connected
*connected
;
453 struct prefix_ipv6 address
;
458 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
462 p
= connected
->address
;
464 if (p
->family
== AF_INET6
) {
465 address
.family
= AF_INET6
;
466 address
.prefix
= p
->u
.prefix6
;
467 address
.prefixlen
= IPV6_MAX_BITLEN
;
469 n
= agg_node_match(ripng
->enable_network
,
470 (struct prefix
*)&address
);
480 /* Check whether connected is within the ripng->enable_network table. */
481 static int ripng_enable_network_lookup2(struct connected
*connected
)
483 struct ripng_interface
*ri
= connected
->ifp
->info
;
484 struct ripng
*ripng
= ri
->ripng
;
485 struct prefix_ipv6 address
;
491 p
= connected
->address
;
493 if (p
->family
== AF_INET6
) {
494 struct agg_node
*node
;
496 address
.family
= p
->family
;
497 address
.prefix
= p
->u
.prefix6
;
498 address
.prefixlen
= IPV6_MAX_BITLEN
;
500 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
501 * ripng->enable_network */
502 node
= agg_node_match(ripng
->enable_network
,
503 (struct prefix
*)&address
);
506 agg_unlock_node(node
);
514 /* Add RIPng enable network. */
515 int ripng_enable_network_add(struct ripng
*ripng
, struct prefix
*p
)
517 struct agg_node
*node
;
519 node
= agg_node_get(ripng
->enable_network
, p
);
522 agg_unlock_node(node
);
523 return NB_ERR_INCONSISTENCY
;
525 node
->info
= (void *)1;
527 /* XXX: One should find a better solution than a generic one */
528 ripng_enable_apply_all(ripng
);
533 /* Delete RIPng enable network. */
534 int ripng_enable_network_delete(struct ripng
*ripng
, struct prefix
*p
)
536 struct agg_node
*node
;
538 node
= agg_node_lookup(ripng
->enable_network
, p
);
542 /* Unlock info lock. */
543 agg_unlock_node(node
);
545 /* Unlock lookup lock. */
546 agg_unlock_node(node
);
551 return NB_ERR_INCONSISTENCY
;
554 /* Lookup function. */
555 static int ripng_enable_if_lookup(struct ripng
*ripng
, const char *ifname
)
563 for (i
= 0; i
< vector_active(ripng
->enable_if
); i
++)
564 if ((str
= vector_slot(ripng
->enable_if
, i
)) != NULL
)
565 if (strcmp(str
, ifname
) == 0)
570 int ripng_enable_if_add(struct ripng
*ripng
, const char *ifname
)
574 ret
= ripng_enable_if_lookup(ripng
, ifname
);
576 return NB_ERR_INCONSISTENCY
;
578 vector_set(ripng
->enable_if
, strdup(ifname
));
580 ripng_enable_apply_all(ripng
);
585 int ripng_enable_if_delete(struct ripng
*ripng
, const char *ifname
)
590 index
= ripng_enable_if_lookup(ripng
, ifname
);
592 return NB_ERR_INCONSISTENCY
;
594 str
= vector_slot(ripng
->enable_if
, index
);
596 vector_unset(ripng
->enable_if
, index
);
598 ripng_enable_apply_all(ripng
);
603 /* Wake up interface. */
604 static void ripng_interface_wakeup(struct thread
*t
)
606 struct interface
*ifp
;
607 struct ripng_interface
*ri
;
614 /* Join to multicast group. */
615 if (ripng_multicast_join(ifp
, ri
->ripng
->sock
) < 0) {
616 flog_err_sys(EC_LIB_SOCKET
,
617 "multicast join failed, interface %s not running",
622 /* Set running flag. */
625 /* Send RIP request to the interface. */
629 static void ripng_connect_set(struct interface
*ifp
, int set
)
631 struct ripng_interface
*ri
= ifp
->info
;
632 struct ripng
*ripng
= ri
->ripng
;
633 struct listnode
*node
, *nnode
;
634 struct connected
*connected
;
635 struct prefix_ipv6 address
;
637 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
639 p
= connected
->address
;
641 if (p
->family
!= AF_INET6
)
644 address
.family
= AF_INET6
;
645 address
.prefix
= p
->u
.prefix6
;
646 address
.prefixlen
= p
->prefixlen
;
647 apply_mask_ipv6(&address
);
650 /* Check once more whether this prefix is within a
651 * "network IF_OR_PREF" one */
652 if ((ripng_enable_if_lookup(ripng
, connected
->ifp
->name
)
654 || (ripng_enable_network_lookup2(connected
) >= 0))
655 ripng_redistribute_add(
656 ripng
, ZEBRA_ROUTE_CONNECT
,
657 RIPNG_ROUTE_INTERFACE
, &address
,
658 connected
->ifp
->ifindex
, NULL
, 0);
660 ripng_redistribute_delete(ripng
, ZEBRA_ROUTE_CONNECT
,
661 RIPNG_ROUTE_INTERFACE
,
663 connected
->ifp
->ifindex
);
664 if (ripng_redistribute_check(ripng
,
665 ZEBRA_ROUTE_CONNECT
))
666 ripng_redistribute_add(
667 ripng
, ZEBRA_ROUTE_CONNECT
,
668 RIPNG_ROUTE_REDISTRIBUTE
, &address
,
669 connected
->ifp
->ifindex
, NULL
, 0);
674 /* Check RIPng is enabed on this interface. */
675 void ripng_enable_apply(struct interface
*ifp
)
678 struct ripng_interface
*ri
= NULL
;
680 /* Check interface. */
686 /* Is this interface a candidate for RIPng ? */
687 ret
= ripng_enable_network_lookup_if(ifp
);
689 /* If the interface is matched. */
691 ri
->enable_network
= 1;
693 ri
->enable_network
= 0;
695 /* Check interface name configuration. */
696 ret
= ripng_enable_if_lookup(ri
->ripng
, ifp
->name
);
698 ri
->enable_interface
= 1;
700 ri
->enable_interface
= 0;
702 /* any candidate interface MUST have a link-local IPv6 address */
703 if ((!ripng_if_ipv6_lladdress_check(ifp
))
704 && (ri
->enable_network
|| ri
->enable_interface
)) {
705 ri
->enable_network
= 0;
706 ri
->enable_interface
= 0;
707 zlog_warn("Interface %s does not have any link-local address",
711 /* Update running status of the interface. */
712 if (ri
->enable_network
|| ri
->enable_interface
) {
713 zlog_info("RIPng INTERFACE ON %s", ifp
->name
);
715 /* Add interface wake up thread. */
716 thread_add_timer(master
, ripng_interface_wakeup
, ifp
, 1,
719 ripng_connect_set(ifp
, 1);
722 /* Might as well clean up the route table as well
723 * ripng_if_down sets to 0 ri->running, and displays
728 ripng_connect_set(ifp
, 0);
733 /* Set distribute list to all interfaces. */
734 static void ripng_enable_apply_all(struct ripng
*ripng
)
736 struct interface
*ifp
;
738 FOR_ALL_INTERFACES (ripng
->vrf
, ifp
)
739 ripng_enable_apply(ifp
);
742 /* Clear all network and neighbor configuration */
743 void ripng_clean_network(struct ripng
*ripng
)
749 /* ripng->enable_network */
750 for (rn
= agg_route_top(ripng
->enable_network
); rn
;
751 rn
= agg_route_next(rn
))
757 /* ripng->enable_if */
758 for (i
= 0; i
< vector_active(ripng
->enable_if
); i
++)
759 if ((str
= vector_slot(ripng
->enable_if
, i
)) != NULL
) {
761 vector_slot(ripng
->enable_if
, i
) = NULL
;
765 /* Utility function for looking up passive interface settings. */
766 static int ripng_passive_interface_lookup(struct ripng
*ripng
,
772 for (i
= 0; i
< vector_active(ripng
->passive_interface
); i
++)
773 if ((str
= vector_slot(ripng
->passive_interface
, i
)) != NULL
)
774 if (strcmp(str
, ifname
) == 0)
779 void ripng_passive_interface_apply(struct interface
*ifp
)
782 struct ripng_interface
*ri
;
790 ret
= ripng_passive_interface_lookup(ripng
, ifp
->name
);
797 static void ripng_passive_interface_apply_all(struct ripng
*ripng
)
799 struct interface
*ifp
;
801 FOR_ALL_INTERFACES (ripng
->vrf
, ifp
)
802 ripng_passive_interface_apply(ifp
);
805 /* Passive interface. */
806 int ripng_passive_interface_set(struct ripng
*ripng
, const char *ifname
)
808 if (ripng_passive_interface_lookup(ripng
, ifname
) >= 0)
809 return NB_ERR_INCONSISTENCY
;
811 vector_set(ripng
->passive_interface
, strdup(ifname
));
813 ripng_passive_interface_apply_all(ripng
);
818 int ripng_passive_interface_unset(struct ripng
*ripng
, const char *ifname
)
823 i
= ripng_passive_interface_lookup(ripng
, ifname
);
825 return NB_ERR_INCONSISTENCY
;
827 str
= vector_slot(ripng
->passive_interface
, i
);
829 vector_unset(ripng
->passive_interface
, i
);
831 ripng_passive_interface_apply_all(ripng
);
836 /* Free all configured RIP passive-interface settings. */
837 void ripng_passive_interface_clean(struct ripng
*ripng
)
842 for (i
= 0; i
< vector_active(ripng
->passive_interface
); i
++)
843 if ((str
= vector_slot(ripng
->passive_interface
, i
)) != NULL
) {
845 vector_slot(ripng
->passive_interface
, i
) = NULL
;
847 ripng_passive_interface_apply_all(ripng
);
850 /* Write RIPng enable network and interface to the vty. */
851 int ripng_network_write(struct vty
*vty
, struct ripng
*ripng
)
855 struct agg_node
*node
;
857 /* Write enable network. */
858 for (node
= agg_route_top(ripng
->enable_network
); node
;
859 node
= agg_route_next(node
))
861 vty_out(vty
, " %pRN\n", node
);
863 /* Write enable interface. */
864 for (i
= 0; i
< vector_active(ripng
->enable_if
); i
++)
865 if ((ifname
= vector_slot(ripng
->enable_if
, i
)) != NULL
)
866 vty_out(vty
, " %s\n", ifname
);
871 static struct ripng_interface
*ri_new(void)
873 struct ripng_interface
*ri
;
875 ri
= XCALLOC(MTYPE_RIPNG_IF
, sizeof(struct ripng_interface
));
877 /* Set default split-horizon behavior. If the interface is Frame
878 Relay or SMDS is enabled, the default value for split-horizon is
879 off. But currently Zebra does detect Frame Relay or SMDS
880 interface. So all interface is set to split horizon. */
882 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE
);
887 void ripng_interface_sync(struct interface
*ifp
)
889 struct ripng_interface
*ri
;
893 ri
->ripng
= ifp
->vrf
->info
;
896 static int ripng_if_new_hook(struct interface
*ifp
)
898 ifp
->info
= ri_new();
899 ripng_interface_sync(ifp
);
904 /* Called when interface structure deleted. */
905 static int ripng_if_delete_hook(struct interface
*ifp
)
907 XFREE(MTYPE_RIPNG_IF
, ifp
->info
);
911 /* Initialization of interface. */
912 void ripng_if_init(void)
914 /* Interface initialize. */
915 hook_register_prio(if_add
, 0, ripng_if_new_hook
);
916 hook_register_prio(if_del
, 0, ripng_if_delete_hook
);
918 /* Install interface node. */
919 if_cmd_init_default();
920 if_zapi_callbacks(ripng_ifp_create
, ripng_ifp_up
,
921 ripng_ifp_down
, ripng_ifp_destroy
);