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 EVENT_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;
460 XFREE(MTYPE_TMP
, ri
->bfd
.profile
);
462 rip_interface_clean(ri
);
465 int rip_if_down(struct interface
*ifp
)
468 struct route_node
*rp
;
469 struct rip_info
*rinfo
;
470 struct rip_interface
*ri
= NULL
;
471 struct list
*list
= NULL
;
472 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
476 EVENT_OFF(ri
->t_wakeup
);
480 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
481 if ((list
= rp
->info
) != NULL
)
482 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
484 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
485 rip_ecmp_delete(rip
, rinfo
);
488 if (IS_RIP_DEBUG_EVENT
)
489 zlog_debug("turn off %s", ifp
->name
);
491 /* Leave from multicast group. */
492 rip_multicast_leave(ifp
, rip
->sock
);
501 static void rip_apply_address_add(struct connected
*ifc
)
503 struct rip_interface
*ri
= ifc
->ifp
->info
;
504 struct rip
*rip
= ri
->rip
;
505 struct prefix_ipv4 address
;
512 if (!if_is_up(ifc
->ifp
))
517 memset(&address
, 0, sizeof(address
));
518 memset(&nh
, 0, sizeof(nh
));
520 address
.family
= p
->family
;
521 address
.prefix
= p
->u
.prefix4
;
522 address
.prefixlen
= p
->prefixlen
;
523 apply_mask_ipv4(&address
);
525 nh
.ifindex
= ifc
->ifp
->ifindex
;
526 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
528 /* Check if this interface is RIP enabled or not
529 or Check if this address's prefix is RIP enabled */
530 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
531 || (rip_enable_network_lookup2(ifc
) >= 0))
532 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
533 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
537 int rip_interface_address_add(ZAPI_CALLBACK_ARGS
)
539 struct connected
*ifc
;
542 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
543 zclient
->ibuf
, vrf_id
);
550 if (p
->family
== AF_INET
) {
551 if (IS_RIP_DEBUG_ZEBRA
)
552 zlog_debug("connected address %pFX is added", p
);
554 rip_enable_apply(ifc
->ifp
);
555 /* Check if this prefix needs to be redistributed */
556 rip_apply_address_add(ifc
);
558 hook_call(rip_ifaddr_add
, ifc
);
564 static void rip_apply_address_del(struct connected
*ifc
)
566 struct rip_interface
*ri
= ifc
->ifp
->info
;
567 struct rip
*rip
= ri
->rip
;
568 struct prefix_ipv4 address
;
574 if (!if_is_up(ifc
->ifp
))
579 memset(&address
, 0, sizeof(address
));
580 address
.family
= p
->family
;
581 address
.prefix
= p
->u
.prefix4
;
582 address
.prefixlen
= p
->prefixlen
;
583 apply_mask_ipv4(&address
);
585 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
586 &address
, ifc
->ifp
->ifindex
);
589 int rip_interface_address_delete(ZAPI_CALLBACK_ARGS
)
591 struct connected
*ifc
;
594 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
595 zclient
->ibuf
, vrf_id
);
599 if (p
->family
== AF_INET
) {
600 if (IS_RIP_DEBUG_ZEBRA
)
601 zlog_debug("connected address %pFX is deleted",
604 hook_call(rip_ifaddr_del
, ifc
);
606 /* Chech whether this prefix needs to be removed */
607 rip_apply_address_del(ifc
);
610 connected_free(&ifc
);
616 /* Check interface is enabled by network statement. */
617 /* Check whether the interface has at least a connected prefix that
618 * is within the ripng_enable_network table. */
619 static int rip_enable_network_lookup_if(struct interface
*ifp
)
621 struct rip_interface
*ri
= ifp
->info
;
622 struct rip
*rip
= ri
->rip
;
623 struct listnode
*node
, *nnode
;
624 struct connected
*connected
;
625 struct prefix_ipv4 address
;
630 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
632 struct route_node
*n
;
634 p
= connected
->address
;
636 if (p
->family
== AF_INET
) {
637 address
.family
= AF_INET
;
638 address
.prefix
= p
->u
.prefix4
;
639 address
.prefixlen
= IPV4_MAX_BITLEN
;
641 n
= route_node_match(rip
->enable_network
,
642 (struct prefix
*)&address
);
644 route_unlock_node(n
);
652 /* Check whether connected is within the ripng_enable_network table. */
653 static int rip_enable_network_lookup2(struct connected
*connected
)
655 struct rip_interface
*ri
= connected
->ifp
->info
;
656 struct rip
*rip
= ri
->rip
;
657 struct prefix_ipv4 address
;
660 p
= connected
->address
;
662 if (p
->family
== AF_INET
) {
663 struct route_node
*node
;
665 address
.family
= p
->family
;
666 address
.prefix
= p
->u
.prefix4
;
667 address
.prefixlen
= IPV4_MAX_BITLEN
;
669 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
670 * rip->enable_network */
671 node
= route_node_match(rip
->enable_network
,
672 (struct prefix
*)&address
);
675 route_unlock_node(node
);
682 /* Add RIP enable network. */
683 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
685 struct route_node
*node
;
687 node
= route_node_get(rip
->enable_network
, p
);
690 route_unlock_node(node
);
691 return NB_ERR_INCONSISTENCY
;
693 node
->info
= (void *)1;
695 /* XXX: One should find a better solution than a generic one */
696 rip_enable_apply_all(rip
);
701 /* Delete RIP enable network. */
702 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
704 struct route_node
*node
;
706 node
= route_node_lookup(rip
->enable_network
, p
);
710 /* Unlock info lock. */
711 route_unlock_node(node
);
713 /* Unlock lookup lock. */
714 route_unlock_node(node
);
716 /* XXX: One should find a better solution than a generic one */
717 rip_enable_apply_all(rip
);
722 return NB_ERR_INCONSISTENCY
;
725 /* Check interface is enabled by ifname statement. */
726 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
734 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
735 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
736 if (strcmp(str
, ifname
) == 0)
741 /* Add interface to rip_enable_if. */
742 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
746 ret
= rip_enable_if_lookup(rip
, ifname
);
748 return NB_ERR_INCONSISTENCY
;
750 vector_set(rip
->enable_interface
,
751 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
753 rip_enable_apply_all(rip
); /* TODOVJ */
758 /* Delete interface from rip_enable_if. */
759 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
764 index
= rip_enable_if_lookup(rip
, ifname
);
766 return NB_ERR_INCONSISTENCY
;
768 str
= vector_slot(rip
->enable_interface
, index
);
769 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
770 vector_unset(rip
->enable_interface
, index
);
772 rip_enable_apply_all(rip
); /* TODOVJ */
777 /* Join to multicast group and send request to the interface. */
778 static void rip_interface_wakeup(struct event
*t
)
780 struct interface
*ifp
;
781 struct rip_interface
*ri
;
788 /* Join to multicast group. */
789 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
790 flog_err_sys(EC_LIB_SOCKET
,
791 "multicast join failed, interface %s not running",
796 /* Set running flag. */
799 /* Send RIP request to the interface. */
800 rip_request_interface(ifp
);
803 static void rip_connect_set(struct interface
*ifp
, int set
)
805 struct rip_interface
*ri
= ifp
->info
;
806 struct rip
*rip
= ri
->rip
;
807 struct listnode
*node
, *nnode
;
808 struct connected
*connected
;
809 struct prefix_ipv4 address
;
812 memset(&nh
, 0, sizeof(nh
));
814 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
816 p
= connected
->address
;
818 if (p
->family
!= AF_INET
)
821 address
.family
= AF_INET
;
822 address
.prefix
= p
->u
.prefix4
;
823 address
.prefixlen
= p
->prefixlen
;
824 apply_mask_ipv4(&address
);
826 nh
.ifindex
= connected
->ifp
->ifindex
;
827 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
829 /* Check once more whether this prefix is within a
830 * "network IF_OR_PREF" one */
831 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
833 || (rip_enable_network_lookup2(connected
) >= 0))
834 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
836 &address
, &nh
, 0, 0, 0);
838 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
839 RIP_ROUTE_INTERFACE
, &address
,
840 connected
->ifp
->ifindex
);
841 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
842 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
843 RIP_ROUTE_REDISTRIBUTE
,
844 &address
, &nh
, 0, 0, 0);
849 /* Update interface status. */
850 void rip_enable_apply(struct interface
*ifp
)
853 struct rip_interface
*ri
= NULL
;
855 /* Check interface. */
856 if (!if_is_operative(ifp
))
861 /* Check network configuration. */
862 ret
= rip_enable_network_lookup_if(ifp
);
864 /* If the interface is matched. */
866 ri
->enable_network
= 1;
868 ri
->enable_network
= 0;
870 /* Check interface name configuration. */
871 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
873 ri
->enable_interface
= 1;
875 ri
->enable_interface
= 0;
877 /* any interface MUST have an IPv4 address */
878 if (!rip_if_ipv4_address_check(ifp
)) {
879 ri
->enable_network
= 0;
880 ri
->enable_interface
= 0;
883 /* Update running status of the interface. */
884 if (ri
->enable_network
|| ri
->enable_interface
) {
885 if (IS_RIP_DEBUG_EVENT
)
886 zlog_debug("turn on %s", ifp
->name
);
888 /* Add interface wake up thread. */
889 event_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
891 rip_connect_set(ifp
, 1);
892 } else if (ri
->running
) {
893 /* Might as well clean up the route table as well
894 * rip_if_down sets to 0 ri->running, and displays "turn
899 rip_connect_set(ifp
, 0);
903 /* Apply network configuration to all interface. */
904 static void rip_enable_apply_all(struct rip
*rip
)
906 struct interface
*ifp
;
908 /* Check each interface. */
909 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
910 rip_enable_apply(ifp
);
913 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
915 struct prefix_ipv4 p
;
916 struct route_node
*node
;
918 memset(&p
, 0, sizeof(p
));
920 p
.prefix
= from
->sin_addr
;
921 p
.prefixlen
= IPV4_MAX_BITLEN
;
923 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
925 route_unlock_node(node
);
931 /* Add new RIP neighbor to the neighbor tree. */
932 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
934 struct route_node
*node
;
936 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
939 return NB_ERR_INCONSISTENCY
;
941 node
->info
= rip
->neighbor
;
946 /* Delete RIP neighbor from the neighbor tree. */
947 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
949 struct route_node
*node
;
951 /* Lock for look up. */
952 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
954 return NB_ERR_INCONSISTENCY
;
958 /* Unlock lookup lock. */
959 route_unlock_node(node
);
961 /* Unlock real neighbor information lock. */
962 route_unlock_node(node
);
967 /* Clear all network and neighbor configuration. */
968 void rip_clean_network(struct rip
*rip
)
972 struct route_node
*rn
;
974 /* rip->enable_network. */
975 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
978 route_unlock_node(rn
);
981 /* rip->enable_interface. */
982 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
983 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
984 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
985 vector_slot(rip
->enable_interface
, i
) = NULL
;
989 /* Utility function for looking up passive interface settings. */
990 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
995 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
996 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
997 if (strcmp(str
, ifname
) == 0)
1002 static void rip_passive_interface_apply(struct interface
*ifp
)
1005 struct rip_interface
*ri
;
1012 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1013 ? rip
->passive_default
1014 : !rip
->passive_default
);
1016 if (IS_RIP_DEBUG_ZEBRA
)
1017 zlog_debug("interface %s: passive = %d", ifp
->name
,
1021 static void rip_passive_interface_apply_all(struct rip
*rip
)
1023 struct interface
*ifp
;
1025 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1026 rip_passive_interface_apply(ifp
);
1029 /* Passive interface. */
1030 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1032 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1034 * Don't return an error, this can happen after changing
1035 * 'passive-default'.
1039 vector_set(rip
->passive_nondefault
,
1040 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1042 rip_passive_interface_apply_all(rip
);
1047 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1052 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1055 * Don't return an error, this can happen after changing
1056 * 'passive-default'.
1060 str
= vector_slot(rip
->passive_nondefault
, i
);
1061 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1062 vector_unset(rip
->passive_nondefault
, i
);
1064 rip_passive_interface_apply_all(rip
);
1069 /* Free all configured RIP passive-interface settings. */
1070 void rip_passive_nondefault_clean(struct rip
*rip
)
1075 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1076 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1077 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1078 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1080 rip_passive_interface_apply_all(rip
);
1083 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1087 struct route_node
*node
;
1089 /* Network type RIP enable interface statement. */
1090 for (node
= route_top(rip
->enable_network
); node
;
1091 node
= route_next(node
))
1093 vty_out(vty
, " %pFX\n", &node
->p
);
1095 /* Interface name RIP enable statement. */
1096 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1097 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1098 vty_out(vty
, " %s\n", ifname
);
1100 /* RIP neighbors listing. */
1101 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1103 vty_out(vty
, " %pI4\n", &node
->p
.u
.prefix4
);
1108 void rip_interface_sync(struct interface
*ifp
)
1110 struct rip_interface
*ri
;
1115 ri
->rip
= ifp
->vrf
->info
;
1118 /* Called when interface structure allocated. */
1119 static int rip_interface_new_hook(struct interface
*ifp
)
1121 ifp
->info
= rip_interface_new();
1122 rip_interface_sync(ifp
);
1127 /* Called when interface structure deleted. */
1128 static int rip_interface_delete_hook(struct interface
*ifp
)
1130 rip_interface_reset(ifp
->info
);
1131 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1135 /* Allocate and initialize interface vector. */
1136 void rip_if_init(void)
1138 /* Default initial size of interface vector. */
1139 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1140 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1142 /* Install interface node. */
1143 if_cmd_init_default();
1144 if_zapi_callbacks(rip_ifp_create
, rip_ifp_up
,
1145 rip_ifp_down
, rip_ifp_destroy
);