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 /* 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 ripng_multicast_join(struct interface
*ifp
)
63 struct ipv6_mreq mreq
;
66 if (if_is_multicast(ifp
)) {
67 memset(&mreq
, 0, sizeof(mreq
));
68 inet_pton(AF_INET6
, RIPNG_GROUP
, &mreq
.ipv6mr_multiaddr
);
69 mreq
.ipv6mr_interface
= ifp
->ifindex
;
72 * NetBSD 1.6.2 requires root to join groups on gif(4).
73 * While this is bogus, privs are available and easy to use
74 * for this call as a workaround.
76 frr_elevate_privs(&ripngd_privs
) {
78 ret
= setsockopt(ripng
->sock
, IPPROTO_IPV6
,
80 (char *)&mreq
, sizeof(mreq
));
85 if (ret
< 0 && save_errno
== EADDRINUSE
) {
87 * Group is already joined. This occurs due to sloppy
89 * management, in particular declining to leave the
91 * an interface that has just gone down.
93 zlog_warn("ripng join on %s EADDRINUSE (ignoring)\n",
95 return 0; /* not an error */
99 zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s",
100 safe_strerror(save_errno
));
102 if (IS_RIPNG_DEBUG_EVENT
)
104 "RIPng %s join to all-rip-routers multicast group",
113 /* Leave from the all rip routers multicast group. */
114 static int ripng_multicast_leave(struct interface
*ifp
)
117 struct ipv6_mreq mreq
;
119 if (if_is_multicast(ifp
)) {
120 memset(&mreq
, 0, sizeof(mreq
));
121 inet_pton(AF_INET6
, RIPNG_GROUP
, &mreq
.ipv6mr_multiaddr
);
122 mreq
.ipv6mr_interface
= ifp
->ifindex
;
124 ret
= setsockopt(ripng
->sock
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
,
125 (char *)&mreq
, sizeof(mreq
));
127 zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s\n",
128 safe_strerror(errno
));
130 if (IS_RIPNG_DEBUG_EVENT
)
132 "RIPng %s leave from all-rip-routers multicast group",
142 /* How many link local IPv6 address could be used on the interface ? */
143 static int ripng_if_ipv6_lladdress_check(struct interface
*ifp
)
146 struct connected
*connected
;
149 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
151 p
= connected
->address
;
153 if ((p
->family
== AF_INET6
)
154 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
161 static int ripng_if_down(struct interface
*ifp
)
164 struct ripng_info
*rinfo
;
165 struct ripng_interface
*ri
;
166 struct list
*list
= NULL
;
167 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
170 for (rp
= agg_route_top(ripng
->table
); rp
;
171 rp
= agg_route_next(rp
))
172 if ((list
= rp
->info
) != NULL
)
173 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
175 if (rinfo
->ifindex
== ifp
->ifindex
)
176 ripng_ecmp_delete(rinfo
);
181 if (IS_RIPNG_DEBUG_EVENT
)
182 zlog_debug("turn off %s", ifp
->name
);
184 /* Leave from multicast group. */
185 ripng_multicast_leave(ifp
);
193 /* Inteface link up message processing. */
194 int ripng_interface_up(int command
, struct zclient
*zclient
,
195 zebra_size_t length
, vrf_id_t vrf_id
)
198 struct interface
*ifp
;
200 /* zebra_interface_state_read() updates interface structure in iflist.
203 ifp
= zebra_interface_state_read(s
, vrf_id
);
208 if (IS_RIPNG_DEBUG_ZEBRA
)
210 "interface up %s index %d flags %llx metric %d mtu %d",
211 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
212 ifp
->metric
, ifp
->mtu6
);
214 /* Check if this interface is RIPng enabled or not. */
215 ripng_enable_apply(ifp
);
217 /* Check for a passive interface. */
218 ripng_passive_interface_apply(ifp
);
220 /* Apply distribute list to the all interface. */
221 ripng_distribute_update_interface(ifp
);
226 /* Inteface link down message processing. */
227 int ripng_interface_down(int command
, struct zclient
*zclient
,
228 zebra_size_t length
, vrf_id_t vrf_id
)
231 struct interface
*ifp
;
233 /* zebra_interface_state_read() updates interface structure in iflist.
236 ifp
= zebra_interface_state_read(s
, vrf_id
);
243 if (IS_RIPNG_DEBUG_ZEBRA
)
245 "interface down %s index %d flags %#llx metric %d mtu %d",
246 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
247 ifp
->metric
, ifp
->mtu6
);
252 /* Inteface addition message from zebra. */
253 int ripng_interface_add(int command
, struct zclient
*zclient
,
254 zebra_size_t length
, vrf_id_t vrf_id
)
256 struct interface
*ifp
;
258 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
260 if (IS_RIPNG_DEBUG_ZEBRA
)
262 "RIPng interface add %s index %d flags %#llx metric %d mtu %d",
263 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
264 ifp
->metric
, ifp
->mtu6
);
266 /* Check is this interface is RIP enabled or not.*/
267 ripng_enable_apply(ifp
);
269 /* Apply distribute list to the interface. */
270 ripng_distribute_update_interface(ifp
);
272 /* Check interface routemap. */
273 ripng_if_rmap_update_interface(ifp
);
278 int ripng_interface_delete(int command
, struct zclient
*zclient
,
279 zebra_size_t length
, vrf_id_t vrf_id
)
281 struct interface
*ifp
;
285 /* zebra_interface_state_read() updates interface structure in iflist
287 ifp
= zebra_interface_state_read(s
, vrf_id
);
296 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
297 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
298 ifp
->metric
, ifp
->mtu6
);
300 /* To support pseudo interface do not free interface structure. */
301 /* if_delete(ifp); */
302 if_set_index(ifp
, IFINDEX_INTERNAL
);
307 void ripng_interface_clean(void)
309 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
310 struct interface
*ifp
;
311 struct ripng_interface
*ri
;
313 FOR_ALL_INTERFACES (vrf
, ifp
) {
316 ri
->enable_network
= 0;
317 ri
->enable_interface
= 0;
321 thread_cancel(ri
->t_wakeup
);
327 static void ripng_apply_address_add(struct connected
*ifc
)
329 struct prefix_ipv6 address
;
335 if (!if_is_up(ifc
->ifp
))
340 memset(&address
, 0, sizeof(address
));
341 address
.family
= p
->family
;
342 address
.prefix
= p
->u
.prefix6
;
343 address
.prefixlen
= p
->prefixlen
;
344 apply_mask_ipv6(&address
);
346 /* Check if this interface is RIP enabled or not
347 or Check if this address's prefix is RIP enabled */
348 if ((ripng_enable_if_lookup(ifc
->ifp
->name
) >= 0)
349 || (ripng_enable_network_lookup2(ifc
) >= 0))
350 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT
,
351 RIPNG_ROUTE_INTERFACE
, &address
,
352 ifc
->ifp
->ifindex
, NULL
, 0);
355 int ripng_interface_address_add(int command
, struct zclient
*zclient
,
356 zebra_size_t length
, vrf_id_t vrf_id
)
361 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
362 zclient
->ibuf
, vrf_id
);
369 if (p
->family
== AF_INET6
) {
370 struct ripng_interface
*ri
= c
->ifp
->info
;
372 if (IS_RIPNG_DEBUG_ZEBRA
)
373 zlog_debug("RIPng connected address %s/%d add",
374 inet6_ntoa(p
->u
.prefix6
), p
->prefixlen
);
376 /* Check is this prefix needs to be redistributed. */
377 ripng_apply_address_add(c
);
379 /* Let's try once again whether the interface could be activated
382 /* Check if this interface is RIP enabled or not.*/
383 ripng_enable_apply(c
->ifp
);
385 /* Apply distribute list to the interface. */
386 ripng_distribute_update_interface(c
->ifp
);
388 /* Check interface routemap. */
389 ripng_if_rmap_update_interface(c
->ifp
);
396 static void ripng_apply_address_del(struct connected
*ifc
)
398 struct prefix_ipv6 address
;
404 if (!if_is_up(ifc
->ifp
))
409 memset(&address
, 0, sizeof(address
));
410 address
.family
= p
->family
;
411 address
.prefix
= p
->u
.prefix6
;
412 address
.prefixlen
= p
->prefixlen
;
413 apply_mask_ipv6(&address
);
415 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT
, RIPNG_ROUTE_INTERFACE
,
416 &address
, ifc
->ifp
->ifindex
);
419 int ripng_interface_address_delete(int command
, struct zclient
*zclient
,
420 zebra_size_t length
, vrf_id_t vrf_id
)
422 struct connected
*ifc
;
424 char buf
[INET6_ADDRSTRLEN
];
426 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
427 zclient
->ibuf
, vrf_id
);
432 if (p
->family
== AF_INET6
) {
433 if (IS_RIPNG_DEBUG_ZEBRA
)
435 "RIPng connected address %s/%d delete",
436 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
,
440 /* Check wether this prefix needs to be removed. */
441 ripng_apply_address_del(ifc
);
449 /* RIPng enable interface vector. */
450 vector ripng_enable_if
;
452 /* RIPng enable network table. */
453 struct agg_table
*ripng_enable_network
;
455 /* Lookup RIPng enable network. */
456 /* Check wether the interface has at least a connected prefix that
457 * is within the ripng_enable_network table. */
458 static int ripng_enable_network_lookup_if(struct interface
*ifp
)
460 struct listnode
*node
;
461 struct connected
*connected
;
462 struct prefix_ipv6 address
;
464 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
468 p
= connected
->address
;
470 if (p
->family
== AF_INET6
) {
471 address
.family
= AF_INET6
;
472 address
.prefix
= p
->u
.prefix6
;
473 address
.prefixlen
= IPV6_MAX_BITLEN
;
475 n
= agg_node_match(ripng_enable_network
,
476 (struct prefix
*)&address
);
486 /* Check wether connected is within the ripng_enable_network table. */
487 static int ripng_enable_network_lookup2(struct connected
*connected
)
489 struct prefix_ipv6 address
;
492 p
= connected
->address
;
494 if (p
->family
== AF_INET6
) {
495 struct agg_node
*node
;
497 address
.family
= p
->family
;
498 address
.prefix
= p
->u
.prefix6
;
499 address
.prefixlen
= IPV6_MAX_BITLEN
;
501 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
502 * ripng_enable_network */
503 node
= agg_node_match(ripng_enable_network
,
504 (struct prefix
*)&address
);
507 agg_unlock_node(node
);
515 /* Add RIPng enable network. */
516 int ripng_enable_network_add(struct prefix
*p
)
518 struct agg_node
*node
;
520 node
= agg_node_get(ripng_enable_network
, p
);
523 agg_unlock_node(node
);
524 return NB_ERR_INCONSISTENCY
;
526 node
->info
= (void *)1;
528 /* XXX: One should find a better solution than a generic one */
529 ripng_enable_apply_all();
534 /* Delete RIPng enable network. */
535 int ripng_enable_network_delete(struct prefix
*p
)
537 struct agg_node
*node
;
539 node
= agg_node_lookup(ripng_enable_network
, p
);
543 /* Unlock info lock. */
544 agg_unlock_node(node
);
546 /* Unlock lookup lock. */
547 agg_unlock_node(node
);
552 return NB_ERR_INCONSISTENCY
;
555 /* Lookup function. */
556 static int ripng_enable_if_lookup(const char *ifname
)
561 for (i
= 0; i
< vector_active(ripng_enable_if
); i
++)
562 if ((str
= vector_slot(ripng_enable_if
, i
)) != NULL
)
563 if (strcmp(str
, ifname
) == 0)
568 /* Add interface to ripng_enable_if. */
569 int ripng_enable_if_add(const char *ifname
)
573 ret
= ripng_enable_if_lookup(ifname
);
575 return NB_ERR_INCONSISTENCY
;
577 vector_set(ripng_enable_if
, strdup(ifname
));
579 ripng_enable_apply_all();
584 /* Delete interface from ripng_enable_if. */
585 int ripng_enable_if_delete(const char *ifname
)
590 index
= ripng_enable_if_lookup(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();
603 /* Wake up interface. */
604 static int ripng_interface_wakeup(struct thread
*t
)
606 struct interface
*ifp
;
607 struct ripng_interface
*ri
;
615 /* Join to multicast group. */
616 if (ripng_multicast_join(ifp
) < 0) {
617 flog_err_sys(EC_LIB_SOCKET
,
618 "multicast join failed, interface %s not running",
623 /* Set running flag. */
626 /* Send RIP request to the interface. */
632 static void ripng_connect_set(struct interface
*ifp
, int set
)
634 struct listnode
*node
, *nnode
;
635 struct connected
*connected
;
636 struct prefix_ipv6 address
;
638 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
640 p
= connected
->address
;
642 if (p
->family
!= AF_INET6
)
645 address
.family
= AF_INET6
;
646 address
.prefix
= p
->u
.prefix6
;
647 address
.prefixlen
= p
->prefixlen
;
648 apply_mask_ipv6(&address
);
651 /* Check once more wether this prefix is within a
652 * "network IF_OR_PREF" one */
653 if ((ripng_enable_if_lookup(connected
->ifp
->name
) >= 0)
654 || (ripng_enable_network_lookup2(connected
) >= 0))
655 ripng_redistribute_add(
657 RIPNG_ROUTE_INTERFACE
, &address
,
658 connected
->ifp
->ifindex
, NULL
, 0);
660 ripng_redistribute_delete(
661 ZEBRA_ROUTE_CONNECT
, RIPNG_ROUTE_INTERFACE
,
662 &address
, connected
->ifp
->ifindex
);
663 if (ripng_redistribute_check(ZEBRA_ROUTE_CONNECT
))
664 ripng_redistribute_add(
666 RIPNG_ROUTE_REDISTRIBUTE
, &address
,
667 connected
->ifp
->ifindex
, NULL
, 0);
672 /* Check RIPng is enabed on this interface. */
673 void ripng_enable_apply(struct interface
*ifp
)
676 struct ripng_interface
*ri
= NULL
;
678 /* Check interface. */
684 /* Is this interface a candidate for RIPng ? */
685 ret
= ripng_enable_network_lookup_if(ifp
);
687 /* If the interface is matched. */
689 ri
->enable_network
= 1;
691 ri
->enable_network
= 0;
693 /* Check interface name configuration. */
694 ret
= ripng_enable_if_lookup(ifp
->name
);
696 ri
->enable_interface
= 1;
698 ri
->enable_interface
= 0;
700 /* any candidate interface MUST have a link-local IPv6 address */
701 if ((!ripng_if_ipv6_lladdress_check(ifp
))
702 && (ri
->enable_network
|| ri
->enable_interface
)) {
703 ri
->enable_network
= 0;
704 ri
->enable_interface
= 0;
705 zlog_warn("Interface %s does not have any link-local address",
709 /* Update running status of the interface. */
710 if (ri
->enable_network
|| ri
->enable_interface
) {
711 zlog_info("RIPng INTERFACE ON %s", ifp
->name
);
713 /* Add interface wake up thread. */
714 thread_add_timer(master
, ripng_interface_wakeup
, ifp
, 1,
717 ripng_connect_set(ifp
, 1);
720 /* Might as well clean up the route table as well
721 * ripng_if_down sets to 0 ri->running, and displays
726 ripng_connect_set(ifp
, 0);
731 /* Set distribute list to all interfaces. */
732 static void ripng_enable_apply_all(void)
734 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
735 struct interface
*ifp
;
737 FOR_ALL_INTERFACES (vrf
, ifp
)
738 ripng_enable_apply(ifp
);
741 /* Clear all network and neighbor configuration */
742 void ripng_clean_network()
748 /* ripng_enable_network */
749 for (rn
= agg_route_top(ripng_enable_network
); rn
;
750 rn
= agg_route_next(rn
))
756 /* ripng_enable_if */
757 for (i
= 0; i
< vector_active(ripng_enable_if
); i
++)
758 if ((str
= vector_slot(ripng_enable_if
, i
)) != NULL
) {
760 vector_slot(ripng_enable_if
, i
) = NULL
;
764 /* Vector to store passive-interface name. */
765 vector Vripng_passive_interface
;
767 /* Utility function for looking up passive interface settings. */
768 static int ripng_passive_interface_lookup(const char *ifname
)
773 for (i
= 0; i
< vector_active(Vripng_passive_interface
); i
++)
774 if ((str
= vector_slot(Vripng_passive_interface
, i
)) != NULL
)
775 if (strcmp(str
, ifname
) == 0)
780 void ripng_passive_interface_apply(struct interface
*ifp
)
783 struct ripng_interface
*ri
;
787 ret
= ripng_passive_interface_lookup(ifp
->name
);
794 static void ripng_passive_interface_apply_all(void)
796 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
797 struct interface
*ifp
;
799 FOR_ALL_INTERFACES (vrf
, ifp
)
800 ripng_passive_interface_apply(ifp
);
803 /* Passive interface. */
804 int ripng_passive_interface_set(const char *ifname
)
806 if (ripng_passive_interface_lookup(ifname
) >= 0)
807 return NB_ERR_INCONSISTENCY
;
809 vector_set(Vripng_passive_interface
, strdup(ifname
));
811 ripng_passive_interface_apply_all();
816 int ripng_passive_interface_unset(const char *ifname
)
821 i
= ripng_passive_interface_lookup(ifname
);
823 return NB_ERR_INCONSISTENCY
;
825 str
= vector_slot(Vripng_passive_interface
, i
);
827 vector_unset(Vripng_passive_interface
, i
);
829 ripng_passive_interface_apply_all();
834 /* Free all configured RIP passive-interface settings. */
835 void ripng_passive_interface_clean(void)
840 for (i
= 0; i
< vector_active(Vripng_passive_interface
); i
++)
841 if ((str
= vector_slot(Vripng_passive_interface
, i
)) != NULL
) {
843 vector_slot(Vripng_passive_interface
, i
) = NULL
;
845 ripng_passive_interface_apply_all();
848 /* Write RIPng enable network and interface to the vty. */
849 int ripng_network_write(struct vty
*vty
)
853 struct agg_node
*node
;
856 /* Write enable network. */
857 for (node
= agg_route_top(ripng_enable_network
); node
;
858 node
= agg_route_next(node
))
860 struct prefix
*p
= &node
->p
;
861 vty_out(vty
, " %s/%d\n",
862 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
866 /* Write enable interface. */
867 for (i
= 0; i
< vector_active(ripng_enable_if
); i
++)
868 if ((ifname
= vector_slot(ripng_enable_if
, i
)) != NULL
)
869 vty_out(vty
, " %s\n", ifname
);
874 static struct ripng_interface
*ri_new(void)
876 struct ripng_interface
*ri
;
877 ri
= XCALLOC(MTYPE_IF
, sizeof(struct ripng_interface
));
879 /* Set default split-horizon behavior. If the interface is Frame
880 Relay or SMDS is enabled, the default value for split-horizon is
881 off. But currently Zebra does detect Frame Relay or SMDS
882 interface. So all interface is set to split horizon. */
884 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE
);
889 static int ripng_if_new_hook(struct interface
*ifp
)
891 ifp
->info
= ri_new();
895 /* Called when interface structure deleted. */
896 static int ripng_if_delete_hook(struct interface
*ifp
)
898 XFREE(MTYPE_IF
, ifp
->info
);
903 /* Configuration write function for ripngd. */
904 static int interface_config_write(struct vty
*vty
)
906 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
907 struct interface
*ifp
;
910 FOR_ALL_INTERFACES (vrf
, ifp
) {
911 struct lyd_node
*dnode
;
913 dnode
= yang_dnode_get(
914 running_config
->dnode
,
915 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
916 ifp
->name
, vrf
->name
);
921 nb_cli_show_dnode_cmds(vty
, dnode
, false);
927 /* ripngd's interface node. */
928 static struct cmd_node interface_node
= {
929 INTERFACE_NODE
, "%s(config-if)# ", 1 /* VTYSH */
932 /* Initialization of interface. */
935 /* Interface initialize. */
936 hook_register_prio(if_add
, 0, ripng_if_new_hook
);
937 hook_register_prio(if_del
, 0, ripng_if_delete_hook
);
939 /* RIPng enable network init. */
940 ripng_enable_network
= agg_table_init();
942 /* RIPng enable interface init. */
943 ripng_enable_if
= vector_init(1);
945 /* RIPng passive interface. */
946 Vripng_passive_interface
= vector_init(1);
948 /* Install interface node. */
949 install_node(&interface_node
, interface_config_write
);