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_bfd.h"
29 #include "ripd/rip_debug.h"
30 #include "ripd/rip_interface.h"
32 DEFINE_MTYPE_STATIC(RIPD
, RIP_INTERFACE
, "RIP interface");
33 DEFINE_MTYPE(RIPD
, RIP_INTERFACE_STRING
, "RIP Interface String");
34 DEFINE_HOOK(rip_ifaddr_add
, (struct connected
* ifc
), (ifc
));
35 DEFINE_HOOK(rip_ifaddr_del
, (struct connected
* ifc
), (ifc
));
37 /* static prototypes */
38 static void rip_enable_apply(struct interface
*);
39 static void rip_passive_interface_apply(struct interface
*);
40 static int rip_if_down(struct interface
*ifp
);
41 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
);
42 static int rip_enable_network_lookup2(struct connected
*connected
);
43 static void rip_enable_apply_all(struct rip
*rip
);
45 const struct message ri_version_msg
[] = {{RI_RIP_VERSION_1
, "1"},
46 {RI_RIP_VERSION_2
, "2"},
47 {RI_RIP_VERSION_1_AND_2
, "1 2"},
48 {RI_RIP_VERSION_NONE
, "none"},
51 /* Join to the RIP version 2 multicast group. */
52 static int ipv4_multicast_join(int sock
, struct in_addr group
,
53 struct in_addr ifa
, ifindex_t ifindex
)
57 ret
= setsockopt_ipv4_multicast(sock
, IP_ADD_MEMBERSHIP
, ifa
,
58 group
.s_addr
, ifindex
);
61 zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s",
62 safe_strerror(errno
));
67 /* Leave from the RIP version 2 multicast group. */
68 static int ipv4_multicast_leave(int sock
, struct in_addr group
,
69 struct in_addr ifa
, ifindex_t ifindex
)
73 ret
= setsockopt_ipv4_multicast(sock
, IP_DROP_MEMBERSHIP
, ifa
,
74 group
.s_addr
, ifindex
);
77 zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");
82 static void rip_interface_reset(struct rip_interface
*);
84 /* Allocate new RIP's interface configuration. */
85 static struct rip_interface
*rip_interface_new(void)
87 struct rip_interface
*ri
;
89 ri
= XCALLOC(MTYPE_RIP_INTERFACE
, sizeof(struct rip_interface
));
91 rip_interface_reset(ri
);
96 void rip_interface_multicast_set(int sock
, struct connected
*connected
)
100 assert(connected
!= NULL
);
102 addr
= CONNECTED_ID(connected
)->u
.prefix4
;
104 if (setsockopt_ipv4_multicast_if(sock
, addr
, connected
->ifp
->ifindex
)
107 "Can't setsockopt IP_MULTICAST_IF on fd %d to ifindex %d for interface %s",
108 sock
, connected
->ifp
->ifindex
, connected
->ifp
->name
);
114 /* Send RIP request packet to specified interface. */
115 static void rip_request_interface_send(struct interface
*ifp
, uint8_t version
)
117 struct sockaddr_in to
;
119 /* RIPv2 support multicast. */
120 if (version
== RIPv2
&& if_is_multicast(ifp
)) {
122 if (IS_RIP_DEBUG_EVENT
)
123 zlog_debug("multicast request on %s", ifp
->name
);
125 rip_request_send(NULL
, ifp
, version
, NULL
);
129 /* RIPv1 and non multicast interface. */
130 if (if_is_pointopoint(ifp
) || if_is_broadcast(ifp
)) {
131 struct listnode
*cnode
, *cnnode
;
132 struct connected
*connected
;
134 if (IS_RIP_DEBUG_EVENT
)
135 zlog_debug("broadcast request to %s", ifp
->name
);
137 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
,
139 if (connected
->address
->family
!= AF_INET
)
142 memset(&to
, 0, sizeof(struct sockaddr_in
));
143 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
144 if (connected
->destination
)
145 /* use specified broadcast or peer
146 * destination addr */
147 to
.sin_addr
= connected
->destination
->u
.prefix4
;
148 else if (connected
->address
->prefixlen
150 /* calculate the appropriate broadcast
152 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
153 connected
->address
->u
.prefix4
.s_addr
,
154 connected
->address
->prefixlen
);
156 /* do not know where to send the packet
160 if (IS_RIP_DEBUG_EVENT
)
161 zlog_debug("SEND request to %pI4",
164 rip_request_send(&to
, ifp
, version
, connected
);
169 /* This will be executed when interface goes up. */
170 static void rip_request_interface(struct interface
*ifp
)
172 struct rip_interface
*ri
;
175 /* In default ripd doesn't send RIP_REQUEST to the loopback interface.
177 if (if_is_loopback(ifp
))
180 /* If interface is down, don't send RIP packet. */
181 if (!if_is_operative(ifp
))
184 /* Fetch RIP interface information. */
187 /* If there is no version configuration in the interface,
188 use rip's version setting. */
189 vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? ri
->rip
->version_send
192 rip_request_interface_send(ifp
, RIPv1
);
194 rip_request_interface_send(ifp
, RIPv2
);
197 /* Multicast packet receive socket. */
198 static int rip_multicast_join(struct interface
*ifp
, int sock
)
200 struct listnode
*cnode
;
201 struct connected
*ifc
;
203 if (if_is_operative(ifp
) && if_is_multicast(ifp
)) {
204 if (IS_RIP_DEBUG_EVENT
)
205 zlog_debug("multicast join at %s", ifp
->name
);
207 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
208 struct prefix_ipv4
*p
;
209 struct in_addr group
;
211 p
= (struct prefix_ipv4
*)ifc
->address
;
213 if (p
->family
!= AF_INET
)
216 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
217 if (ipv4_multicast_join(sock
, group
, p
->prefix
,
228 /* Leave from multicast group. */
229 static void rip_multicast_leave(struct interface
*ifp
, int sock
)
231 struct listnode
*cnode
;
232 struct connected
*connected
;
234 if (if_is_up(ifp
) && if_is_multicast(ifp
)) {
235 if (IS_RIP_DEBUG_EVENT
)
236 zlog_debug("multicast leave from %s", ifp
->name
);
238 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
239 struct prefix_ipv4
*p
;
240 struct in_addr group
;
242 p
= (struct prefix_ipv4
*)connected
->address
;
244 if (p
->family
!= AF_INET
)
247 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
248 if (ipv4_multicast_leave(sock
, group
, p
->prefix
,
256 /* Is there and address on interface that I could use ? */
257 static int rip_if_ipv4_address_check(struct interface
*ifp
)
260 struct connected
*connected
;
263 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
266 p
= connected
->address
;
268 if (p
->family
== AF_INET
)
276 /* Does this address belongs to me ? */
277 int if_check_address(struct rip
*rip
, struct in_addr addr
)
279 struct interface
*ifp
;
281 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
282 struct listnode
*cnode
;
283 struct connected
*connected
;
285 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
286 struct prefix_ipv4
*p
;
288 p
= (struct prefix_ipv4
*)connected
->address
;
290 if (p
->family
!= AF_INET
)
293 if (IPV4_ADDR_CMP(&p
->prefix
, &addr
) == 0)
300 /* Interface link down message processing. */
301 static int rip_ifp_down(struct interface
*ifp
)
303 rip_interface_sync(ifp
);
306 if (IS_RIP_DEBUG_ZEBRA
)
308 "interface %s vrf %s(%u) index %d flags %llx metric %d mtu %d is down",
309 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
310 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
311 ifp
->metric
, ifp
->mtu
);
316 /* Interface link up message processing */
317 static int rip_ifp_up(struct interface
*ifp
)
319 if (IS_RIP_DEBUG_ZEBRA
)
321 "interface %s vrf %s(%u) index %d flags %#llx metric %d mtu %d is up",
322 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
323 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
324 ifp
->metric
, ifp
->mtu
);
326 rip_interface_sync(ifp
);
328 /* Check if this interface is RIP enabled or not.*/
329 rip_enable_apply(ifp
);
331 /* Check for a passive interface */
332 rip_passive_interface_apply(ifp
);
334 /* Apply distribute list to the all interface. */
335 rip_distribute_update_interface(ifp
);
340 /* Interface addition message from zebra. */
341 static int rip_ifp_create(struct interface
*ifp
)
343 rip_interface_sync(ifp
);
345 if (IS_RIP_DEBUG_ZEBRA
)
347 "interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
348 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
349 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
350 ifp
->metric
, ifp
->mtu
);
352 /* Check if this interface is RIP enabled or not.*/
353 rip_enable_apply(ifp
);
355 /* Check for a passive interface */
356 rip_passive_interface_apply(ifp
);
358 /* Apply distribute list to the all interface. */
359 rip_distribute_update_interface(ifp
);
361 /* rip_request_neighbor_all (); */
363 /* Check interface routemap. */
364 rip_if_rmap_update_interface(ifp
);
369 static int rip_ifp_destroy(struct interface
*ifp
)
371 rip_interface_sync(ifp
);
376 if (IS_RIP_DEBUG_ZEBRA
)
378 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
379 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
380 ifp
->ifindex
, (unsigned long long)ifp
->flags
,
381 ifp
->metric
, ifp
->mtu
);
386 /* VRF update for an interface. */
387 int rip_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
389 struct interface
*ifp
;
392 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
397 if (IS_RIP_DEBUG_ZEBRA
) {
398 struct vrf
*nvrf
= vrf_lookup_by_id(new_vrf_id
);
400 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
401 ifp
->name
, ifp
->vrf
->name
, vrf_id
, VRF_LOGNAME(nvrf
),
405 if_update_to_new_vrf(ifp
, new_vrf_id
);
406 rip_interface_sync(ifp
);
411 static void rip_interface_clean(struct rip_interface
*ri
)
413 ri
->enable_network
= 0;
414 ri
->enable_interface
= 0;
417 EVENT_OFF(ri
->t_wakeup
);
420 void rip_interfaces_clean(struct rip
*rip
)
422 struct interface
*ifp
;
424 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
425 rip_interface_clean(ifp
->info
);
428 static void rip_interface_reset(struct rip_interface
*ri
)
430 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
432 ri
->md5_auth_len
= yang_get_default_enum(
433 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
435 /* Set default split-horizon behavior. If the interface is Frame
436 Relay or SMDS is enabled, the default value for split-horizon is
437 off. But currently Zebra does detect Frame Relay or SMDS
438 interface. So all interface is set to split horizon. */
440 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
442 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
443 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
444 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
446 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
448 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
450 ri
->list
[RIP_FILTER_IN
] = NULL
;
451 ri
->list
[RIP_FILTER_OUT
] = NULL
;
453 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
454 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
456 ri
->recv_badpackets
= 0;
457 ri
->recv_badroutes
= 0;
458 ri
->sent_updates
= 0;
461 XFREE(MTYPE_RIP_BFD_PROFILE
, ri
->bfd
.profile
);
463 rip_interface_clean(ri
);
466 int rip_if_down(struct interface
*ifp
)
469 struct route_node
*rp
;
470 struct rip_info
*rinfo
;
471 struct rip_interface
*ri
= NULL
;
472 struct list
*list
= NULL
;
473 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
477 EVENT_OFF(ri
->t_wakeup
);
481 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
482 if ((list
= rp
->info
) != NULL
)
483 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
485 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
486 rip_ecmp_delete(rip
, rinfo
);
489 if (IS_RIP_DEBUG_EVENT
)
490 zlog_debug("turn off %s", ifp
->name
);
492 /* Leave from multicast group. */
493 rip_multicast_leave(ifp
, rip
->sock
);
502 static void rip_apply_address_add(struct connected
*ifc
)
504 struct rip_interface
*ri
= ifc
->ifp
->info
;
505 struct rip
*rip
= ri
->rip
;
506 struct prefix_ipv4 address
;
513 if (!if_is_up(ifc
->ifp
))
518 memset(&address
, 0, sizeof(address
));
519 memset(&nh
, 0, sizeof(nh
));
521 address
.family
= p
->family
;
522 address
.prefix
= p
->u
.prefix4
;
523 address
.prefixlen
= p
->prefixlen
;
524 apply_mask_ipv4(&address
);
526 nh
.ifindex
= ifc
->ifp
->ifindex
;
527 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
529 /* Check if this interface is RIP enabled or not
530 or Check if this address's prefix is RIP enabled */
531 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
532 || (rip_enable_network_lookup2(ifc
) >= 0))
533 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
534 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
538 int rip_interface_address_add(ZAPI_CALLBACK_ARGS
)
540 struct connected
*ifc
;
543 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
544 zclient
->ibuf
, vrf_id
);
551 if (p
->family
== AF_INET
) {
552 if (IS_RIP_DEBUG_ZEBRA
)
553 zlog_debug("connected address %pFX is added", p
);
555 rip_enable_apply(ifc
->ifp
);
556 /* Check if this prefix needs to be redistributed */
557 rip_apply_address_add(ifc
);
559 hook_call(rip_ifaddr_add
, ifc
);
565 static void rip_apply_address_del(struct connected
*ifc
)
567 struct rip_interface
*ri
= ifc
->ifp
->info
;
568 struct rip
*rip
= ri
->rip
;
569 struct prefix_ipv4 address
;
575 if (!if_is_up(ifc
->ifp
))
580 memset(&address
, 0, sizeof(address
));
581 address
.family
= p
->family
;
582 address
.prefix
= p
->u
.prefix4
;
583 address
.prefixlen
= p
->prefixlen
;
584 apply_mask_ipv4(&address
);
586 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
587 &address
, ifc
->ifp
->ifindex
);
590 int rip_interface_address_delete(ZAPI_CALLBACK_ARGS
)
592 struct connected
*ifc
;
595 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
596 zclient
->ibuf
, vrf_id
);
600 if (p
->family
== AF_INET
) {
601 if (IS_RIP_DEBUG_ZEBRA
)
602 zlog_debug("connected address %pFX is deleted",
605 hook_call(rip_ifaddr_del
, ifc
);
607 /* Chech whether this prefix needs to be removed */
608 rip_apply_address_del(ifc
);
611 connected_free(&ifc
);
617 /* Check interface is enabled by network statement. */
618 /* Check whether the interface has at least a connected prefix that
619 * is within the ripng_enable_network table. */
620 static int rip_enable_network_lookup_if(struct interface
*ifp
)
622 struct rip_interface
*ri
= ifp
->info
;
623 struct rip
*rip
= ri
->rip
;
624 struct listnode
*node
, *nnode
;
625 struct connected
*connected
;
626 struct prefix_ipv4 address
;
631 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
633 struct route_node
*n
;
635 p
= connected
->address
;
637 if (p
->family
== AF_INET
) {
638 address
.family
= AF_INET
;
639 address
.prefix
= p
->u
.prefix4
;
640 address
.prefixlen
= IPV4_MAX_BITLEN
;
642 n
= route_node_match(rip
->enable_network
,
643 (struct prefix
*)&address
);
645 route_unlock_node(n
);
653 /* Check whether connected is within the ripng_enable_network table. */
654 static int rip_enable_network_lookup2(struct connected
*connected
)
656 struct rip_interface
*ri
= connected
->ifp
->info
;
657 struct rip
*rip
= ri
->rip
;
658 struct prefix_ipv4 address
;
661 p
= connected
->address
;
663 if (p
->family
== AF_INET
) {
664 struct route_node
*node
;
666 address
.family
= p
->family
;
667 address
.prefix
= p
->u
.prefix4
;
668 address
.prefixlen
= IPV4_MAX_BITLEN
;
670 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
671 * rip->enable_network */
672 node
= route_node_match(rip
->enable_network
,
673 (struct prefix
*)&address
);
676 route_unlock_node(node
);
683 /* Add RIP enable network. */
684 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
686 struct route_node
*node
;
688 node
= route_node_get(rip
->enable_network
, p
);
691 route_unlock_node(node
);
692 return NB_ERR_INCONSISTENCY
;
694 node
->info
= (void *)1;
696 /* XXX: One should find a better solution than a generic one */
697 rip_enable_apply_all(rip
);
702 /* Delete RIP enable network. */
703 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
705 struct route_node
*node
;
707 node
= route_node_lookup(rip
->enable_network
, p
);
711 /* Unlock info lock. */
712 route_unlock_node(node
);
714 /* Unlock lookup lock. */
715 route_unlock_node(node
);
717 /* XXX: One should find a better solution than a generic one */
718 rip_enable_apply_all(rip
);
723 return NB_ERR_INCONSISTENCY
;
726 /* Check interface is enabled by ifname statement. */
727 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
735 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
736 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
737 if (strcmp(str
, ifname
) == 0)
742 /* Add interface to rip_enable_if. */
743 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
747 ret
= rip_enable_if_lookup(rip
, ifname
);
749 return NB_ERR_INCONSISTENCY
;
751 vector_set(rip
->enable_interface
,
752 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
754 rip_enable_apply_all(rip
); /* TODOVJ */
759 /* Delete interface from rip_enable_if. */
760 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
765 index
= rip_enable_if_lookup(rip
, ifname
);
767 return NB_ERR_INCONSISTENCY
;
769 str
= vector_slot(rip
->enable_interface
, index
);
770 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
771 vector_unset(rip
->enable_interface
, index
);
773 rip_enable_apply_all(rip
); /* TODOVJ */
778 /* Join to multicast group and send request to the interface. */
779 static void rip_interface_wakeup(struct event
*t
)
781 struct interface
*ifp
;
782 struct rip_interface
*ri
;
789 /* Join to multicast group. */
790 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
791 flog_err_sys(EC_LIB_SOCKET
,
792 "multicast join failed, interface %s not running",
797 /* Set running flag. */
800 /* Send RIP request to the interface. */
801 rip_request_interface(ifp
);
804 static void rip_connect_set(struct interface
*ifp
, int set
)
806 struct rip_interface
*ri
= ifp
->info
;
807 struct rip
*rip
= ri
->rip
;
808 struct listnode
*node
, *nnode
;
809 struct connected
*connected
;
810 struct prefix_ipv4 address
;
813 memset(&nh
, 0, sizeof(nh
));
815 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
817 p
= connected
->address
;
819 if (p
->family
!= AF_INET
)
822 address
.family
= AF_INET
;
823 address
.prefix
= p
->u
.prefix4
;
824 address
.prefixlen
= p
->prefixlen
;
825 apply_mask_ipv4(&address
);
827 nh
.ifindex
= connected
->ifp
->ifindex
;
828 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
830 /* Check once more whether this prefix is within a
831 * "network IF_OR_PREF" one */
832 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
834 || (rip_enable_network_lookup2(connected
) >= 0))
835 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
837 &address
, &nh
, 0, 0, 0);
839 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
840 RIP_ROUTE_INTERFACE
, &address
,
841 connected
->ifp
->ifindex
);
842 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
843 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
844 RIP_ROUTE_REDISTRIBUTE
,
845 &address
, &nh
, 0, 0, 0);
850 /* Update interface status. */
851 void rip_enable_apply(struct interface
*ifp
)
854 struct rip_interface
*ri
= NULL
;
856 /* Check interface. */
857 if (!if_is_operative(ifp
))
862 /* Check network configuration. */
863 ret
= rip_enable_network_lookup_if(ifp
);
865 /* If the interface is matched. */
867 ri
->enable_network
= 1;
869 ri
->enable_network
= 0;
871 /* Check interface name configuration. */
872 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
874 ri
->enable_interface
= 1;
876 ri
->enable_interface
= 0;
878 /* any interface MUST have an IPv4 address */
879 if (!rip_if_ipv4_address_check(ifp
)) {
880 ri
->enable_network
= 0;
881 ri
->enable_interface
= 0;
884 /* Update running status of the interface. */
885 if (ri
->enable_network
|| ri
->enable_interface
) {
886 if (IS_RIP_DEBUG_EVENT
)
887 zlog_debug("turn on %s", ifp
->name
);
889 /* Add interface wake up thread. */
890 event_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
892 rip_connect_set(ifp
, 1);
893 } else if (ri
->running
) {
894 /* Might as well clean up the route table as well
895 * rip_if_down sets to 0 ri->running, and displays "turn
900 rip_connect_set(ifp
, 0);
904 /* Apply network configuration to all interface. */
905 static void rip_enable_apply_all(struct rip
*rip
)
907 struct interface
*ifp
;
909 /* Check each interface. */
910 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
911 rip_enable_apply(ifp
);
914 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
916 struct prefix_ipv4 p
;
917 struct route_node
*node
;
919 memset(&p
, 0, sizeof(p
));
921 p
.prefix
= from
->sin_addr
;
922 p
.prefixlen
= IPV4_MAX_BITLEN
;
924 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
926 route_unlock_node(node
);
932 /* Add new RIP neighbor to the neighbor tree. */
933 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
935 struct route_node
*node
;
937 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
940 return NB_ERR_INCONSISTENCY
;
942 node
->info
= rip
->neighbor
;
947 /* Delete RIP neighbor from the neighbor tree. */
948 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
950 struct route_node
*node
;
952 /* Lock for look up. */
953 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
955 return NB_ERR_INCONSISTENCY
;
959 /* Unlock lookup lock. */
960 route_unlock_node(node
);
962 /* Unlock real neighbor information lock. */
963 route_unlock_node(node
);
968 /* Clear all network and neighbor configuration. */
969 void rip_clean_network(struct rip
*rip
)
973 struct route_node
*rn
;
975 /* rip->enable_network. */
976 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
979 route_unlock_node(rn
);
982 /* rip->enable_interface. */
983 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
984 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
985 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
986 vector_slot(rip
->enable_interface
, i
) = NULL
;
990 /* Utility function for looking up passive interface settings. */
991 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
996 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
997 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
998 if (strcmp(str
, ifname
) == 0)
1003 static void rip_passive_interface_apply(struct interface
*ifp
)
1006 struct rip_interface
*ri
;
1013 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1014 ? rip
->passive_default
1015 : !rip
->passive_default
);
1017 if (IS_RIP_DEBUG_ZEBRA
)
1018 zlog_debug("interface %s: passive = %d", ifp
->name
,
1022 static void rip_passive_interface_apply_all(struct rip
*rip
)
1024 struct interface
*ifp
;
1026 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1027 rip_passive_interface_apply(ifp
);
1030 /* Passive interface. */
1031 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1033 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1035 * Don't return an error, this can happen after changing
1036 * 'passive-default'.
1040 vector_set(rip
->passive_nondefault
,
1041 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1043 rip_passive_interface_apply_all(rip
);
1048 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1053 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1056 * Don't return an error, this can happen after changing
1057 * 'passive-default'.
1061 str
= vector_slot(rip
->passive_nondefault
, i
);
1062 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1063 vector_unset(rip
->passive_nondefault
, i
);
1065 rip_passive_interface_apply_all(rip
);
1070 /* Free all configured RIP passive-interface settings. */
1071 void rip_passive_nondefault_clean(struct rip
*rip
)
1076 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1077 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1078 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1079 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1081 rip_passive_interface_apply_all(rip
);
1084 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1088 struct route_node
*node
;
1090 /* Network type RIP enable interface statement. */
1091 for (node
= route_top(rip
->enable_network
); node
;
1092 node
= route_next(node
))
1094 vty_out(vty
, " %pFX\n", &node
->p
);
1096 /* Interface name RIP enable statement. */
1097 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1098 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1099 vty_out(vty
, " %s\n", ifname
);
1101 /* RIP neighbors listing. */
1102 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1104 vty_out(vty
, " %pI4\n", &node
->p
.u
.prefix4
);
1109 void rip_interface_sync(struct interface
*ifp
)
1111 struct rip_interface
*ri
;
1115 ri
->rip
= ifp
->vrf
->info
;
1120 /* Called when interface structure allocated. */
1121 static int rip_interface_new_hook(struct interface
*ifp
)
1123 ifp
->info
= rip_interface_new();
1124 rip_interface_sync(ifp
);
1129 /* Called when interface structure deleted. */
1130 static int rip_interface_delete_hook(struct interface
*ifp
)
1132 rip_interface_reset(ifp
->info
);
1133 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1137 /* Allocate and initialize interface vector. */
1138 void rip_if_init(void)
1140 /* Default initial size of interface vector. */
1141 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1142 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1144 /* Install interface node. */
1145 if_cmd_init_default();
1146 if_zapi_callbacks(rip_ifp_create
, rip_ifp_up
,
1147 rip_ifp_down
, rip_ifp_destroy
);