1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Interface related function for RIP.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
10 #include "sockunion.h"
22 #include "lib_errors.h"
23 #include "northbound_cli.h"
25 #include "zebra/connected.h"
27 #include "ripd/ripd.h"
28 #include "ripd/rip_debug.h"
29 #include "ripd/rip_interface.h"
31 DEFINE_MTYPE_STATIC(RIPD
, RIP_INTERFACE
, "RIP interface");
32 DEFINE_MTYPE(RIPD
, RIP_INTERFACE_STRING
, "RIP Interface String");
33 DEFINE_HOOK(rip_ifaddr_add
, (struct connected
* ifc
), (ifc
));
34 DEFINE_HOOK(rip_ifaddr_del
, (struct connected
* ifc
), (ifc
));
36 /* static prototypes */
37 static void rip_enable_apply(struct interface
*);
38 static void rip_passive_interface_apply(struct interface
*);
39 static int rip_if_down(struct interface
*ifp
);
40 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
);
41 static int rip_enable_network_lookup2(struct connected
*connected
);
42 static void rip_enable_apply_all(struct rip
*rip
);
44 const struct message ri_version_msg
[] = {{RI_RIP_VERSION_1
, "1"},
45 {RI_RIP_VERSION_2
, "2"},
46 {RI_RIP_VERSION_1_AND_2
, "1 2"},
47 {RI_RIP_VERSION_NONE
, "none"},
50 /* Join to the RIP version 2 multicast group. */
51 static int ipv4_multicast_join(int sock
, struct in_addr group
,
52 struct in_addr ifa
, ifindex_t ifindex
)
56 ret
= setsockopt_ipv4_multicast(sock
, IP_ADD_MEMBERSHIP
, ifa
,
57 group
.s_addr
, ifindex
);
60 zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s",
61 safe_strerror(errno
));
66 /* Leave from the RIP version 2 multicast group. */
67 static int ipv4_multicast_leave(int sock
, struct in_addr group
,
68 struct in_addr ifa
, ifindex_t ifindex
)
72 ret
= setsockopt_ipv4_multicast(sock
, IP_DROP_MEMBERSHIP
, ifa
,
73 group
.s_addr
, ifindex
);
76 zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");
81 static void rip_interface_reset(struct rip_interface
*);
83 /* Allocate new RIP's interface configuration. */
84 static struct rip_interface
*rip_interface_new(void)
86 struct rip_interface
*ri
;
88 ri
= XCALLOC(MTYPE_RIP_INTERFACE
, sizeof(struct rip_interface
));
90 rip_interface_reset(ri
);
95 void rip_interface_multicast_set(int sock
, struct connected
*connected
)
99 assert(connected
!= NULL
);
101 addr
= CONNECTED_ID(connected
)->u
.prefix4
;
103 if (setsockopt_ipv4_multicast_if(sock
, addr
, connected
->ifp
->ifindex
)
106 "Can't setsockopt IP_MULTICAST_IF on fd %d to ifindex %d for interface %s",
107 sock
, connected
->ifp
->ifindex
, connected
->ifp
->name
);
113 /* Send RIP request packet to specified interface. */
114 static void rip_request_interface_send(struct interface
*ifp
, uint8_t version
)
116 struct sockaddr_in to
;
118 /* RIPv2 support multicast. */
119 if (version
== RIPv2
&& if_is_multicast(ifp
)) {
121 if (IS_RIP_DEBUG_EVENT
)
122 zlog_debug("multicast request on %s", ifp
->name
);
124 rip_request_send(NULL
, ifp
, version
, NULL
);
128 /* RIPv1 and non multicast interface. */
129 if (if_is_pointopoint(ifp
) || if_is_broadcast(ifp
)) {
130 struct listnode
*cnode
, *cnnode
;
131 struct connected
*connected
;
133 if (IS_RIP_DEBUG_EVENT
)
134 zlog_debug("broadcast request to %s", ifp
->name
);
136 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
,
138 if (connected
->address
->family
!= AF_INET
)
141 memset(&to
, 0, sizeof(struct sockaddr_in
));
142 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
143 if (connected
->destination
)
144 /* use specified broadcast or peer
145 * destination addr */
146 to
.sin_addr
= connected
->destination
->u
.prefix4
;
147 else if (connected
->address
->prefixlen
149 /* calculate the appropriate broadcast
151 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
152 connected
->address
->u
.prefix4
.s_addr
,
153 connected
->address
->prefixlen
);
155 /* do not know where to send the packet
159 if (IS_RIP_DEBUG_EVENT
)
160 zlog_debug("SEND request to %pI4",
163 rip_request_send(&to
, ifp
, version
, connected
);
168 /* This will be executed when interface goes up. */
169 static void rip_request_interface(struct interface
*ifp
)
171 struct rip_interface
*ri
;
174 /* In default ripd doesn't send RIP_REQUEST to the loopback interface.
176 if (if_is_loopback(ifp
))
179 /* If interface is down, don't send RIP packet. */
180 if (!if_is_operative(ifp
))
183 /* Fetch RIP interface information. */
186 /* If there is no version configuration in the interface,
187 use rip's version setting. */
188 vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? ri
->rip
->version_send
191 rip_request_interface_send(ifp
, RIPv1
);
193 rip_request_interface_send(ifp
, RIPv2
);
196 /* Multicast packet receive socket. */
197 static int rip_multicast_join(struct interface
*ifp
, int sock
)
199 struct listnode
*cnode
;
200 struct connected
*ifc
;
202 if (if_is_operative(ifp
) && if_is_multicast(ifp
)) {
203 if (IS_RIP_DEBUG_EVENT
)
204 zlog_debug("multicast join at %s", ifp
->name
);
206 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
207 struct prefix_ipv4
*p
;
208 struct in_addr group
;
210 p
= (struct prefix_ipv4
*)ifc
->address
;
212 if (p
->family
!= AF_INET
)
215 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
216 if (ipv4_multicast_join(sock
, group
, p
->prefix
,
227 /* Leave from multicast group. */
228 static void rip_multicast_leave(struct interface
*ifp
, int sock
)
230 struct listnode
*cnode
;
231 struct connected
*connected
;
233 if (if_is_up(ifp
) && if_is_multicast(ifp
)) {
234 if (IS_RIP_DEBUG_EVENT
)
235 zlog_debug("multicast leave from %s", ifp
->name
);
237 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
238 struct prefix_ipv4
*p
;
239 struct in_addr group
;
241 p
= (struct prefix_ipv4
*)connected
->address
;
243 if (p
->family
!= AF_INET
)
246 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
247 if (ipv4_multicast_leave(sock
, group
, p
->prefix
,
255 /* Is there and address on interface that I could use ? */
256 static int rip_if_ipv4_address_check(struct interface
*ifp
)
259 struct connected
*connected
;
262 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
265 p
= connected
->address
;
267 if (p
->family
== AF_INET
)
275 /* Does this address belongs to me ? */
276 int if_check_address(struct rip
*rip
, struct in_addr addr
)
278 struct interface
*ifp
;
280 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
281 struct listnode
*cnode
;
282 struct connected
*connected
;
284 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
285 struct prefix_ipv4
*p
;
287 p
= (struct prefix_ipv4
*)connected
->address
;
289 if (p
->family
!= AF_INET
)
292 if (IPV4_ADDR_CMP(&p
->prefix
, &addr
) == 0)
299 /* Interface link down message processing. */
300 static int rip_ifp_down(struct interface
*ifp
)
302 rip_interface_sync(ifp
);
305 if (IS_RIP_DEBUG_ZEBRA
)
307 "interface %s vrf %s(%u) index %d flags %llx metric %d mtu %d is down",
308 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
309 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
310 ifp
->metric
, ifp
->mtu
);
315 /* Interface link up message processing */
316 static int rip_ifp_up(struct interface
*ifp
)
318 if (IS_RIP_DEBUG_ZEBRA
)
320 "interface %s vrf %s(%u) index %d flags %#llx metric %d mtu %d is up",
321 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
322 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
323 ifp
->metric
, ifp
->mtu
);
325 rip_interface_sync(ifp
);
327 /* Check if this interface is RIP enabled or not.*/
328 rip_enable_apply(ifp
);
330 /* Check for a passive interface */
331 rip_passive_interface_apply(ifp
);
333 /* Apply distribute list to the all interface. */
334 rip_distribute_update_interface(ifp
);
339 /* Interface addition message from zebra. */
340 static int rip_ifp_create(struct interface
*ifp
)
342 rip_interface_sync(ifp
);
344 if (IS_RIP_DEBUG_ZEBRA
)
346 "interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
347 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
348 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
349 ifp
->metric
, ifp
->mtu
);
351 /* Check if this interface is RIP enabled or not.*/
352 rip_enable_apply(ifp
);
354 /* Check for a passive interface */
355 rip_passive_interface_apply(ifp
);
357 /* Apply distribute list to the all interface. */
358 rip_distribute_update_interface(ifp
);
360 /* rip_request_neighbor_all (); */
362 /* Check interface routemap. */
363 rip_if_rmap_update_interface(ifp
);
368 static int rip_ifp_destroy(struct interface
*ifp
)
370 rip_interface_sync(ifp
);
375 if (IS_RIP_DEBUG_ZEBRA
)
377 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
378 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
379 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
380 ifp
->metric
, ifp
->mtu
);
385 /* VRF update for an interface. */
386 int rip_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
388 struct interface
*ifp
;
391 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
396 if (IS_RIP_DEBUG_ZEBRA
) {
397 struct vrf
*nvrf
= vrf_lookup_by_id(new_vrf_id
);
399 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
400 ifp
->name
, ifp
->vrf
->name
, vrf_id
, VRF_LOGNAME(nvrf
),
404 if_update_to_new_vrf(ifp
, new_vrf_id
);
405 rip_interface_sync(ifp
);
410 static void rip_interface_clean(struct rip_interface
*ri
)
412 ri
->enable_network
= 0;
413 ri
->enable_interface
= 0;
416 THREAD_OFF(ri
->t_wakeup
);
419 void rip_interfaces_clean(struct rip
*rip
)
421 struct interface
*ifp
;
423 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
424 rip_interface_clean(ifp
->info
);
427 static void rip_interface_reset(struct rip_interface
*ri
)
429 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
431 ri
->md5_auth_len
= yang_get_default_enum(
432 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
434 /* Set default split-horizon behavior. If the interface is Frame
435 Relay or SMDS is enabled, the default value for split-horizon is
436 off. But currently Zebra does detect Frame Relay or SMDS
437 interface. So all interface is set to split horizon. */
439 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
441 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
442 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
443 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
445 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
447 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
449 ri
->list
[RIP_FILTER_IN
] = NULL
;
450 ri
->list
[RIP_FILTER_OUT
] = NULL
;
452 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
453 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
455 ri
->recv_badpackets
= 0;
456 ri
->recv_badroutes
= 0;
457 ri
->sent_updates
= 0;
461 rip_interface_clean(ri
);
464 int rip_if_down(struct interface
*ifp
)
467 struct route_node
*rp
;
468 struct rip_info
*rinfo
;
469 struct rip_interface
*ri
= NULL
;
470 struct list
*list
= NULL
;
471 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
475 THREAD_OFF(ri
->t_wakeup
);
479 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
480 if ((list
= rp
->info
) != NULL
)
481 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
483 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
484 rip_ecmp_delete(rip
, rinfo
);
487 if (IS_RIP_DEBUG_EVENT
)
488 zlog_debug("turn off %s", ifp
->name
);
490 /* Leave from multicast group. */
491 rip_multicast_leave(ifp
, rip
->sock
);
500 static void rip_apply_address_add(struct connected
*ifc
)
502 struct rip_interface
*ri
= ifc
->ifp
->info
;
503 struct rip
*rip
= ri
->rip
;
504 struct prefix_ipv4 address
;
511 if (!if_is_up(ifc
->ifp
))
516 memset(&address
, 0, sizeof(address
));
517 memset(&nh
, 0, sizeof(nh
));
519 address
.family
= p
->family
;
520 address
.prefix
= p
->u
.prefix4
;
521 address
.prefixlen
= p
->prefixlen
;
522 apply_mask_ipv4(&address
);
524 nh
.ifindex
= ifc
->ifp
->ifindex
;
525 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
527 /* Check if this interface is RIP enabled or not
528 or Check if this address's prefix is RIP enabled */
529 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
530 || (rip_enable_network_lookup2(ifc
) >= 0))
531 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
532 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
536 int rip_interface_address_add(ZAPI_CALLBACK_ARGS
)
538 struct connected
*ifc
;
541 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
542 zclient
->ibuf
, vrf_id
);
549 if (p
->family
== AF_INET
) {
550 if (IS_RIP_DEBUG_ZEBRA
)
551 zlog_debug("connected address %pFX is added", p
);
553 rip_enable_apply(ifc
->ifp
);
554 /* Check if this prefix needs to be redistributed */
555 rip_apply_address_add(ifc
);
557 hook_call(rip_ifaddr_add
, ifc
);
563 static void rip_apply_address_del(struct connected
*ifc
)
565 struct rip_interface
*ri
= ifc
->ifp
->info
;
566 struct rip
*rip
= ri
->rip
;
567 struct prefix_ipv4 address
;
573 if (!if_is_up(ifc
->ifp
))
578 memset(&address
, 0, sizeof(address
));
579 address
.family
= p
->family
;
580 address
.prefix
= p
->u
.prefix4
;
581 address
.prefixlen
= p
->prefixlen
;
582 apply_mask_ipv4(&address
);
584 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
585 &address
, ifc
->ifp
->ifindex
);
588 int rip_interface_address_delete(ZAPI_CALLBACK_ARGS
)
590 struct connected
*ifc
;
593 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
594 zclient
->ibuf
, vrf_id
);
598 if (p
->family
== AF_INET
) {
599 if (IS_RIP_DEBUG_ZEBRA
)
600 zlog_debug("connected address %pFX is deleted",
603 hook_call(rip_ifaddr_del
, ifc
);
605 /* Chech whether this prefix needs to be removed */
606 rip_apply_address_del(ifc
);
609 connected_free(&ifc
);
615 /* Check interface is enabled by network statement. */
616 /* Check whether the interface has at least a connected prefix that
617 * is within the ripng_enable_network table. */
618 static int rip_enable_network_lookup_if(struct interface
*ifp
)
620 struct rip_interface
*ri
= ifp
->info
;
621 struct rip
*rip
= ri
->rip
;
622 struct listnode
*node
, *nnode
;
623 struct connected
*connected
;
624 struct prefix_ipv4 address
;
629 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
631 struct route_node
*n
;
633 p
= connected
->address
;
635 if (p
->family
== AF_INET
) {
636 address
.family
= AF_INET
;
637 address
.prefix
= p
->u
.prefix4
;
638 address
.prefixlen
= IPV4_MAX_BITLEN
;
640 n
= route_node_match(rip
->enable_network
,
641 (struct prefix
*)&address
);
643 route_unlock_node(n
);
651 /* Check whether connected is within the ripng_enable_network table. */
652 static int rip_enable_network_lookup2(struct connected
*connected
)
654 struct rip_interface
*ri
= connected
->ifp
->info
;
655 struct rip
*rip
= ri
->rip
;
656 struct prefix_ipv4 address
;
659 p
= connected
->address
;
661 if (p
->family
== AF_INET
) {
662 struct route_node
*node
;
664 address
.family
= p
->family
;
665 address
.prefix
= p
->u
.prefix4
;
666 address
.prefixlen
= IPV4_MAX_BITLEN
;
668 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
669 * rip->enable_network */
670 node
= route_node_match(rip
->enable_network
,
671 (struct prefix
*)&address
);
674 route_unlock_node(node
);
681 /* Add RIP enable network. */
682 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
684 struct route_node
*node
;
686 node
= route_node_get(rip
->enable_network
, p
);
689 route_unlock_node(node
);
690 return NB_ERR_INCONSISTENCY
;
692 node
->info
= (void *)1;
694 /* XXX: One should find a better solution than a generic one */
695 rip_enable_apply_all(rip
);
700 /* Delete RIP enable network. */
701 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
703 struct route_node
*node
;
705 node
= route_node_lookup(rip
->enable_network
, p
);
709 /* Unlock info lock. */
710 route_unlock_node(node
);
712 /* Unlock lookup lock. */
713 route_unlock_node(node
);
715 /* XXX: One should find a better solution than a generic one */
716 rip_enable_apply_all(rip
);
721 return NB_ERR_INCONSISTENCY
;
724 /* Check interface is enabled by ifname statement. */
725 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
733 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
734 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
735 if (strcmp(str
, ifname
) == 0)
740 /* Add interface to rip_enable_if. */
741 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
745 ret
= rip_enable_if_lookup(rip
, ifname
);
747 return NB_ERR_INCONSISTENCY
;
749 vector_set(rip
->enable_interface
,
750 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
752 rip_enable_apply_all(rip
); /* TODOVJ */
757 /* Delete interface from rip_enable_if. */
758 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
763 index
= rip_enable_if_lookup(rip
, ifname
);
765 return NB_ERR_INCONSISTENCY
;
767 str
= vector_slot(rip
->enable_interface
, index
);
768 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
769 vector_unset(rip
->enable_interface
, index
);
771 rip_enable_apply_all(rip
); /* TODOVJ */
776 /* Join to multicast group and send request to the interface. */
777 static void rip_interface_wakeup(struct thread
*t
)
779 struct interface
*ifp
;
780 struct rip_interface
*ri
;
787 /* Join to multicast group. */
788 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
789 flog_err_sys(EC_LIB_SOCKET
,
790 "multicast join failed, interface %s not running",
795 /* Set running flag. */
798 /* Send RIP request to the interface. */
799 rip_request_interface(ifp
);
802 static void rip_connect_set(struct interface
*ifp
, int set
)
804 struct rip_interface
*ri
= ifp
->info
;
805 struct rip
*rip
= ri
->rip
;
806 struct listnode
*node
, *nnode
;
807 struct connected
*connected
;
808 struct prefix_ipv4 address
;
811 memset(&nh
, 0, sizeof(nh
));
813 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
815 p
= connected
->address
;
817 if (p
->family
!= AF_INET
)
820 address
.family
= AF_INET
;
821 address
.prefix
= p
->u
.prefix4
;
822 address
.prefixlen
= p
->prefixlen
;
823 apply_mask_ipv4(&address
);
825 nh
.ifindex
= connected
->ifp
->ifindex
;
826 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
828 /* Check once more whether this prefix is within a
829 * "network IF_OR_PREF" one */
830 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
832 || (rip_enable_network_lookup2(connected
) >= 0))
833 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
835 &address
, &nh
, 0, 0, 0);
837 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
838 RIP_ROUTE_INTERFACE
, &address
,
839 connected
->ifp
->ifindex
);
840 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
841 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
842 RIP_ROUTE_REDISTRIBUTE
,
843 &address
, &nh
, 0, 0, 0);
848 /* Update interface status. */
849 void rip_enable_apply(struct interface
*ifp
)
852 struct rip_interface
*ri
= NULL
;
854 /* Check interface. */
855 if (!if_is_operative(ifp
))
860 /* Check network configuration. */
861 ret
= rip_enable_network_lookup_if(ifp
);
863 /* If the interface is matched. */
865 ri
->enable_network
= 1;
867 ri
->enable_network
= 0;
869 /* Check interface name configuration. */
870 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
872 ri
->enable_interface
= 1;
874 ri
->enable_interface
= 0;
876 /* any interface MUST have an IPv4 address */
877 if (!rip_if_ipv4_address_check(ifp
)) {
878 ri
->enable_network
= 0;
879 ri
->enable_interface
= 0;
882 /* Update running status of the interface. */
883 if (ri
->enable_network
|| ri
->enable_interface
) {
884 if (IS_RIP_DEBUG_EVENT
)
885 zlog_debug("turn on %s", ifp
->name
);
887 /* Add interface wake up thread. */
888 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
890 rip_connect_set(ifp
, 1);
891 } else if (ri
->running
) {
892 /* Might as well clean up the route table as well
893 * rip_if_down sets to 0 ri->running, and displays "turn
898 rip_connect_set(ifp
, 0);
902 /* Apply network configuration to all interface. */
903 static void rip_enable_apply_all(struct rip
*rip
)
905 struct interface
*ifp
;
907 /* Check each interface. */
908 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
909 rip_enable_apply(ifp
);
912 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
914 struct prefix_ipv4 p
;
915 struct route_node
*node
;
917 memset(&p
, 0, sizeof(p
));
919 p
.prefix
= from
->sin_addr
;
920 p
.prefixlen
= IPV4_MAX_BITLEN
;
922 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
924 route_unlock_node(node
);
930 /* Add new RIP neighbor to the neighbor tree. */
931 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
933 struct route_node
*node
;
935 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
938 return NB_ERR_INCONSISTENCY
;
940 node
->info
= rip
->neighbor
;
945 /* Delete RIP neighbor from the neighbor tree. */
946 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
948 struct route_node
*node
;
950 /* Lock for look up. */
951 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
953 return NB_ERR_INCONSISTENCY
;
957 /* Unlock lookup lock. */
958 route_unlock_node(node
);
960 /* Unlock real neighbor information lock. */
961 route_unlock_node(node
);
966 /* Clear all network and neighbor configuration. */
967 void rip_clean_network(struct rip
*rip
)
971 struct route_node
*rn
;
973 /* rip->enable_network. */
974 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
977 route_unlock_node(rn
);
980 /* rip->enable_interface. */
981 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
982 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
983 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
984 vector_slot(rip
->enable_interface
, i
) = NULL
;
988 /* Utility function for looking up passive interface settings. */
989 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
994 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
995 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
996 if (strcmp(str
, ifname
) == 0)
1001 static void rip_passive_interface_apply(struct interface
*ifp
)
1004 struct rip_interface
*ri
;
1011 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1012 ? rip
->passive_default
1013 : !rip
->passive_default
);
1015 if (IS_RIP_DEBUG_ZEBRA
)
1016 zlog_debug("interface %s: passive = %d", ifp
->name
,
1020 static void rip_passive_interface_apply_all(struct rip
*rip
)
1022 struct interface
*ifp
;
1024 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1025 rip_passive_interface_apply(ifp
);
1028 /* Passive interface. */
1029 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1031 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1033 * Don't return an error, this can happen after changing
1034 * 'passive-default'.
1038 vector_set(rip
->passive_nondefault
,
1039 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1041 rip_passive_interface_apply_all(rip
);
1046 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1051 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1054 * Don't return an error, this can happen after changing
1055 * 'passive-default'.
1059 str
= vector_slot(rip
->passive_nondefault
, i
);
1060 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1061 vector_unset(rip
->passive_nondefault
, i
);
1063 rip_passive_interface_apply_all(rip
);
1068 /* Free all configured RIP passive-interface settings. */
1069 void rip_passive_nondefault_clean(struct rip
*rip
)
1074 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1075 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1076 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1077 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1079 rip_passive_interface_apply_all(rip
);
1082 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1086 struct route_node
*node
;
1088 /* Network type RIP enable interface statement. */
1089 for (node
= route_top(rip
->enable_network
); node
;
1090 node
= route_next(node
))
1092 vty_out(vty
, " %pFX\n", &node
->p
);
1094 /* Interface name RIP enable statement. */
1095 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1096 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1097 vty_out(vty
, " %s\n", ifname
);
1099 /* RIP neighbors listing. */
1100 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1102 vty_out(vty
, " %pI4\n", &node
->p
.u
.prefix4
);
1107 void rip_interface_sync(struct interface
*ifp
)
1109 struct rip_interface
*ri
;
1113 ri
->rip
= ifp
->vrf
->info
;
1116 /* Called when interface structure allocated. */
1117 static int rip_interface_new_hook(struct interface
*ifp
)
1119 ifp
->info
= rip_interface_new();
1120 rip_interface_sync(ifp
);
1125 /* Called when interface structure deleted. */
1126 static int rip_interface_delete_hook(struct interface
*ifp
)
1128 rip_interface_reset(ifp
->info
);
1129 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1133 /* Allocate and initialize interface vector. */
1134 void rip_if_init(void)
1136 /* Default initial size of interface vector. */
1137 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1138 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1140 /* Install interface node. */
1141 if_cmd_init_default();
1142 if_zapi_callbacks(rip_ifp_create
, rip_ifp_up
,
1143 rip_ifp_down
, rip_ifp_destroy
);