1 /* Interface related function for RIP.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "sockunion.h"
37 #include "lib_errors.h"
38 #include "northbound_cli.h"
40 #include "zebra/connected.h"
42 #include "ripd/ripd.h"
43 #include "ripd/rip_debug.h"
44 #include "ripd/rip_interface.h"
46 DEFINE_HOOK(rip_ifaddr_add
, (struct connected
* ifc
), (ifc
))
47 DEFINE_HOOK(rip_ifaddr_del
, (struct connected
* ifc
), (ifc
))
49 /* static prototypes */
50 static void rip_enable_apply(struct interface
*);
51 static void rip_passive_interface_apply(struct interface
*);
52 static int rip_if_down(struct interface
*ifp
);
53 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
);
54 static int rip_enable_network_lookup2(struct connected
*connected
);
55 static void rip_enable_apply_all(struct rip
*rip
);
57 const struct message ri_version_msg
[] = {{RI_RIP_VERSION_1
, "1"},
58 {RI_RIP_VERSION_2
, "2"},
59 {RI_RIP_VERSION_1_AND_2
, "1 2"},
60 {RI_RIP_VERSION_NONE
, "none"},
63 /* Join to the RIP version 2 multicast group. */
64 static int ipv4_multicast_join(int sock
, struct in_addr group
,
65 struct in_addr ifa
, ifindex_t ifindex
)
69 ret
= setsockopt_ipv4_multicast(sock
, IP_ADD_MEMBERSHIP
, ifa
,
70 group
.s_addr
, ifindex
);
73 zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s",
74 safe_strerror(errno
));
79 /* Leave from the RIP version 2 multicast group. */
80 static int ipv4_multicast_leave(int sock
, struct in_addr group
,
81 struct in_addr ifa
, ifindex_t ifindex
)
85 ret
= setsockopt_ipv4_multicast(sock
, IP_DROP_MEMBERSHIP
, ifa
,
86 group
.s_addr
, ifindex
);
89 zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");
94 static void rip_interface_reset(struct rip_interface
*);
96 /* Allocate new RIP's interface configuration. */
97 static struct rip_interface
*rip_interface_new(void)
99 struct rip_interface
*ri
;
101 ri
= XCALLOC(MTYPE_RIP_INTERFACE
, sizeof(struct rip_interface
));
103 rip_interface_reset(ri
);
108 void rip_interface_multicast_set(int sock
, struct connected
*connected
)
112 assert(connected
!= NULL
);
114 addr
= CONNECTED_ID(connected
)->u
.prefix4
;
116 if (setsockopt_ipv4_multicast_if(sock
, addr
, connected
->ifp
->ifindex
)
119 "Can't setsockopt IP_MULTICAST_IF on fd %d to "
120 "ifindex %d for interface %s",
121 sock
, connected
->ifp
->ifindex
, connected
->ifp
->name
);
127 /* Send RIP request packet to specified interface. */
128 static void rip_request_interface_send(struct interface
*ifp
, uint8_t version
)
130 struct sockaddr_in to
;
132 /* RIPv2 support multicast. */
133 if (version
== RIPv2
&& if_is_multicast(ifp
)) {
135 if (IS_RIP_DEBUG_EVENT
)
136 zlog_debug("multicast request on %s", ifp
->name
);
138 rip_request_send(NULL
, ifp
, version
, NULL
);
142 /* RIPv1 and non multicast interface. */
143 if (if_is_pointopoint(ifp
) || if_is_broadcast(ifp
)) {
144 struct listnode
*cnode
, *cnnode
;
145 struct connected
*connected
;
147 if (IS_RIP_DEBUG_EVENT
)
148 zlog_debug("broadcast request to %s", ifp
->name
);
150 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
,
152 if (connected
->address
->family
!= AF_INET
)
155 memset(&to
, 0, sizeof(struct sockaddr_in
));
156 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
157 if (connected
->destination
)
158 /* use specified broadcast or peer
159 * destination addr */
160 to
.sin_addr
= connected
->destination
->u
.prefix4
;
161 else if (connected
->address
->prefixlen
162 < IPV4_MAX_PREFIXLEN
)
163 /* calculate the appropriate broadcast
165 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
166 connected
->address
->u
.prefix4
.s_addr
,
167 connected
->address
->prefixlen
);
169 /* do not know where to send the packet
173 if (IS_RIP_DEBUG_EVENT
)
174 zlog_debug("SEND request to %s",
175 inet_ntoa(to
.sin_addr
));
177 rip_request_send(&to
, ifp
, version
, connected
);
182 /* This will be executed when interface goes up. */
183 static void rip_request_interface(struct interface
*ifp
)
185 struct rip_interface
*ri
;
188 /* In default ripd doesn't send RIP_REQUEST to the loopback interface.
190 if (if_is_loopback(ifp
))
193 /* If interface is down, don't send RIP packet. */
194 if (!if_is_operative(ifp
))
197 /* Fetch RIP interface information. */
200 /* If there is no version configuration in the interface,
201 use rip's version setting. */
202 vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? ri
->rip
->version_send
205 rip_request_interface_send(ifp
, RIPv1
);
207 rip_request_interface_send(ifp
, RIPv2
);
211 /* Send RIP request to the neighbor. */
213 rip_request_neighbor (struct in_addr addr
)
215 struct sockaddr_in to
;
217 memset (&to
, 0, sizeof (struct sockaddr_in
));
218 to
.sin_port
= htons (RIP_PORT_DEFAULT
);
221 rip_request_send (&to
, NULL
, rip
->version_send
, NULL
);
224 /* Request routes at all interfaces. */
226 rip_request_neighbor_all (void)
228 struct route_node
*rp
;
233 if (IS_RIP_DEBUG_EVENT
)
234 zlog_debug ("request to the all neighbor");
236 /* Send request to all neighbor. */
237 for (rp
= route_top (rip
->neighbor
); rp
; rp
= route_next (rp
))
239 rip_request_neighbor (rp
->p
.u
.prefix4
);
243 /* Multicast packet receive socket. */
244 static int rip_multicast_join(struct interface
*ifp
, int sock
)
246 struct listnode
*cnode
;
247 struct connected
*ifc
;
249 if (if_is_operative(ifp
) && if_is_multicast(ifp
)) {
250 if (IS_RIP_DEBUG_EVENT
)
251 zlog_debug("multicast join at %s", ifp
->name
);
253 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
254 struct prefix_ipv4
*p
;
255 struct in_addr group
;
257 p
= (struct prefix_ipv4
*)ifc
->address
;
259 if (p
->family
!= AF_INET
)
262 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
263 if (ipv4_multicast_join(sock
, group
, p
->prefix
,
274 /* Leave from multicast group. */
275 static void rip_multicast_leave(struct interface
*ifp
, int sock
)
277 struct listnode
*cnode
;
278 struct connected
*connected
;
280 if (if_is_up(ifp
) && if_is_multicast(ifp
)) {
281 if (IS_RIP_DEBUG_EVENT
)
282 zlog_debug("multicast leave from %s", ifp
->name
);
284 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
285 struct prefix_ipv4
*p
;
286 struct in_addr group
;
288 p
= (struct prefix_ipv4
*)connected
->address
;
290 if (p
->family
!= AF_INET
)
293 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
294 if (ipv4_multicast_leave(sock
, group
, p
->prefix
,
302 /* Is there and address on interface that I could use ? */
303 static int rip_if_ipv4_address_check(struct interface
*ifp
)
306 struct connected
*connected
;
309 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
312 p
= connected
->address
;
314 if (p
->family
== AF_INET
)
322 /* Does this address belongs to me ? */
323 int if_check_address(struct rip
*rip
, struct in_addr addr
)
325 struct interface
*ifp
;
327 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
328 struct listnode
*cnode
;
329 struct connected
*connected
;
331 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
332 struct prefix_ipv4
*p
;
334 p
= (struct prefix_ipv4
*)connected
->address
;
336 if (p
->family
!= AF_INET
)
339 if (IPV4_ADDR_CMP(&p
->prefix
, &addr
) == 0)
346 /* Inteface link down message processing. */
347 int rip_interface_down(int command
, struct zclient
*zclient
,
348 zebra_size_t length
, vrf_id_t vrf_id
)
350 struct interface
*ifp
;
355 /* zebra_interface_state_read() updates interface structure in
357 ifp
= zebra_interface_state_read(s
, vrf_id
);
362 rip_interface_sync(ifp
);
365 if (IS_RIP_DEBUG_ZEBRA
)
367 "interface %s vrf %u index %d flags %llx metric %d mtu %d is down",
368 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
369 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
374 /* Inteface link up message processing */
375 int rip_interface_up(int command
, struct zclient
*zclient
, zebra_size_t length
,
378 struct interface
*ifp
;
380 /* zebra_interface_state_read () updates interface structure in
382 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
387 if (IS_RIP_DEBUG_ZEBRA
)
389 "interface %s vrf %u index %d flags %#llx metric %d mtu %d is up",
390 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
391 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
393 rip_interface_sync(ifp
);
395 /* Check if this interface is RIP enabled or not.*/
396 rip_enable_apply(ifp
);
398 /* Check for a passive interface */
399 rip_passive_interface_apply(ifp
);
401 /* Apply distribute list to the all interface. */
402 rip_distribute_update_interface(ifp
);
407 /* Inteface addition message from zebra. */
408 int rip_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
411 struct interface
*ifp
;
413 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
414 rip_interface_sync(ifp
);
416 if (IS_RIP_DEBUG_ZEBRA
)
418 "interface add %s vrf %u index %d flags %#llx metric %d mtu %d",
419 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
420 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
422 /* Check if this interface is RIP enabled or not.*/
423 rip_enable_apply(ifp
);
425 /* Check for a passive interface */
426 rip_passive_interface_apply(ifp
);
428 /* Apply distribute list to the all interface. */
429 rip_distribute_update_interface(ifp
);
431 /* rip_request_neighbor_all (); */
433 /* Check interface routemap. */
434 rip_if_rmap_update_interface(ifp
);
439 int rip_interface_delete(int command
, struct zclient
*zclient
,
440 zebra_size_t length
, vrf_id_t vrf_id
)
442 struct interface
*ifp
;
447 /* zebra_interface_state_read() updates interface structure in iflist */
448 ifp
= zebra_interface_state_read(s
, vrf_id
);
453 rip_interface_sync(ifp
);
459 "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d",
460 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
461 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
463 /* To support pseudo interface do not free interface structure. */
464 /* if_delete(ifp); */
465 if_set_index(ifp
, IFINDEX_INTERNAL
);
470 /* VRF update for an interface. */
471 int rip_interface_vrf_update(int command
, struct zclient
*zclient
,
472 zebra_size_t length
, vrf_id_t vrf_id
)
474 struct interface
*ifp
;
477 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
482 if (IS_RIP_DEBUG_ZEBRA
)
483 zlog_debug("interface %s VRF change vrf_id %u new vrf id %u",
484 ifp
->name
, vrf_id
, new_vrf_id
);
486 if_update_to_new_vrf(ifp
, new_vrf_id
);
487 rip_interface_sync(ifp
);
492 static void rip_interface_clean(struct rip_interface
*ri
)
494 ri
->enable_network
= 0;
495 ri
->enable_interface
= 0;
499 thread_cancel(ri
->t_wakeup
);
504 void rip_interfaces_clean(struct rip
*rip
)
506 struct interface
*ifp
;
508 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
509 rip_interface_clean(ifp
->info
);
512 static void rip_interface_reset(struct rip_interface
*ri
)
514 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
516 ri
->md5_auth_len
= yang_get_default_enum(
517 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
519 /* Set default split-horizon behavior. If the interface is Frame
520 Relay or SMDS is enabled, the default value for split-horizon is
521 off. But currently Zebra does detect Frame Relay or SMDS
522 interface. So all interface is set to split horizon. */
524 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
526 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
527 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
528 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
531 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
534 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
536 ri
->list
[RIP_FILTER_IN
] = NULL
;
537 ri
->list
[RIP_FILTER_OUT
] = NULL
;
539 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
540 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
542 ri
->recv_badpackets
= 0;
543 ri
->recv_badroutes
= 0;
544 ri
->sent_updates
= 0;
548 rip_interface_clean(ri
);
551 int rip_if_down(struct interface
*ifp
)
554 struct route_node
*rp
;
555 struct rip_info
*rinfo
;
556 struct rip_interface
*ri
= NULL
;
557 struct list
*list
= NULL
;
558 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
563 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
564 if ((list
= rp
->info
) != NULL
)
565 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
567 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
568 rip_ecmp_delete(rip
, rinfo
);
571 if (IS_RIP_DEBUG_EVENT
)
572 zlog_debug("turn off %s", ifp
->name
);
574 /* Leave from multicast group. */
575 rip_multicast_leave(ifp
, rip
->sock
);
584 static void rip_apply_address_add(struct connected
*ifc
)
586 struct rip_interface
*ri
= ifc
->ifp
->info
;
587 struct rip
*rip
= ri
->rip
;
588 struct prefix_ipv4 address
;
595 if (!if_is_up(ifc
->ifp
))
600 memset(&address
, 0, sizeof(address
));
601 memset(&nh
, 0, sizeof(nh
));
603 address
.family
= p
->family
;
604 address
.prefix
= p
->u
.prefix4
;
605 address
.prefixlen
= p
->prefixlen
;
606 apply_mask_ipv4(&address
);
608 nh
.ifindex
= ifc
->ifp
->ifindex
;
609 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
611 /* Check if this interface is RIP enabled or not
612 or Check if this address's prefix is RIP enabled */
613 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
614 || (rip_enable_network_lookup2(ifc
) >= 0))
615 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
616 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
620 int rip_interface_address_add(int command
, struct zclient
*zclient
,
621 zebra_size_t length
, vrf_id_t vrf_id
)
623 struct connected
*ifc
;
626 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
627 zclient
->ibuf
, vrf_id
);
634 if (p
->family
== AF_INET
) {
635 if (IS_RIP_DEBUG_ZEBRA
)
636 zlog_debug("connected address %s/%d is added",
637 inet_ntoa(p
->u
.prefix4
), p
->prefixlen
);
639 rip_enable_apply(ifc
->ifp
);
640 /* Check if this prefix needs to be redistributed */
641 rip_apply_address_add(ifc
);
643 hook_call(rip_ifaddr_add
, ifc
);
649 static void rip_apply_address_del(struct connected
*ifc
)
651 struct rip_interface
*ri
= ifc
->ifp
->info
;
652 struct rip
*rip
= ri
->rip
;
653 struct prefix_ipv4 address
;
659 if (!if_is_up(ifc
->ifp
))
664 memset(&address
, 0, sizeof(address
));
665 address
.family
= p
->family
;
666 address
.prefix
= p
->u
.prefix4
;
667 address
.prefixlen
= p
->prefixlen
;
668 apply_mask_ipv4(&address
);
670 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
671 &address
, ifc
->ifp
->ifindex
);
674 int rip_interface_address_delete(int command
, struct zclient
*zclient
,
675 zebra_size_t length
, vrf_id_t vrf_id
)
677 struct connected
*ifc
;
680 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
681 zclient
->ibuf
, vrf_id
);
685 if (p
->family
== AF_INET
) {
686 if (IS_RIP_DEBUG_ZEBRA
)
687 zlog_debug("connected address %s/%d is deleted",
688 inet_ntoa(p
->u
.prefix4
),
691 hook_call(rip_ifaddr_del
, ifc
);
693 /* Chech wether this prefix needs to be removed */
694 rip_apply_address_del(ifc
);
703 /* Check interface is enabled by network statement. */
704 /* Check wether the interface has at least a connected prefix that
705 * is within the ripng_enable_network table. */
706 static int rip_enable_network_lookup_if(struct interface
*ifp
)
708 struct rip_interface
*ri
= ifp
->info
;
709 struct rip
*rip
= ri
->rip
;
710 struct listnode
*node
, *nnode
;
711 struct connected
*connected
;
712 struct prefix_ipv4 address
;
717 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
719 struct route_node
*n
;
721 p
= connected
->address
;
723 if (p
->family
== AF_INET
) {
724 address
.family
= AF_INET
;
725 address
.prefix
= p
->u
.prefix4
;
726 address
.prefixlen
= IPV4_MAX_BITLEN
;
728 n
= route_node_match(rip
->enable_network
,
729 (struct prefix
*)&address
);
731 route_unlock_node(n
);
739 /* Check wether connected is within the ripng_enable_network table. */
740 static int rip_enable_network_lookup2(struct connected
*connected
)
742 struct rip_interface
*ri
= connected
->ifp
->info
;
743 struct rip
*rip
= ri
->rip
;
744 struct prefix_ipv4 address
;
747 p
= connected
->address
;
749 if (p
->family
== AF_INET
) {
750 struct route_node
*node
;
752 address
.family
= p
->family
;
753 address
.prefix
= p
->u
.prefix4
;
754 address
.prefixlen
= IPV4_MAX_BITLEN
;
756 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
757 * rip->enable_network */
758 node
= route_node_match(rip
->enable_network
,
759 (struct prefix
*)&address
);
762 route_unlock_node(node
);
769 /* Add RIP enable network. */
770 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
772 struct route_node
*node
;
774 node
= route_node_get(rip
->enable_network
, p
);
777 route_unlock_node(node
);
778 return NB_ERR_INCONSISTENCY
;
780 node
->info
= (void *)1;
782 /* XXX: One should find a better solution than a generic one */
783 rip_enable_apply_all(rip
);
788 /* Delete RIP enable network. */
789 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
791 struct route_node
*node
;
793 node
= route_node_lookup(rip
->enable_network
, p
);
797 /* Unlock info lock. */
798 route_unlock_node(node
);
800 /* Unlock lookup lock. */
801 route_unlock_node(node
);
803 /* XXX: One should find a better solution than a generic one */
804 rip_enable_apply_all(rip
);
809 return NB_ERR_INCONSISTENCY
;
812 /* Check interface is enabled by ifname statement. */
813 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
821 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
822 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
823 if (strcmp(str
, ifname
) == 0)
828 /* Add interface to rip_enable_if. */
829 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
833 ret
= rip_enable_if_lookup(rip
, ifname
);
835 return NB_ERR_INCONSISTENCY
;
837 vector_set(rip
->enable_interface
,
838 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
840 rip_enable_apply_all(rip
); /* TODOVJ */
845 /* Delete interface from rip_enable_if. */
846 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
851 index
= rip_enable_if_lookup(rip
, ifname
);
853 return NB_ERR_INCONSISTENCY
;
855 str
= vector_slot(rip
->enable_interface
, index
);
856 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
857 vector_unset(rip
->enable_interface
, index
);
859 rip_enable_apply_all(rip
); /* TODOVJ */
864 /* Join to multicast group and send request to the interface. */
865 static int rip_interface_wakeup(struct thread
*t
)
867 struct interface
*ifp
;
868 struct rip_interface
*ri
;
876 /* Join to multicast group. */
877 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
878 flog_err_sys(EC_LIB_SOCKET
,
879 "multicast join failed, interface %s not running",
884 /* Set running flag. */
887 /* Send RIP request to the interface. */
888 rip_request_interface(ifp
);
893 static void rip_connect_set(struct interface
*ifp
, int set
)
895 struct rip_interface
*ri
= ifp
->info
;
896 struct rip
*rip
= ri
->rip
;
897 struct listnode
*node
, *nnode
;
898 struct connected
*connected
;
899 struct prefix_ipv4 address
;
902 memset(&nh
, 0, sizeof(nh
));
904 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
906 p
= connected
->address
;
908 if (p
->family
!= AF_INET
)
911 address
.family
= AF_INET
;
912 address
.prefix
= p
->u
.prefix4
;
913 address
.prefixlen
= p
->prefixlen
;
914 apply_mask_ipv4(&address
);
916 nh
.ifindex
= connected
->ifp
->ifindex
;
917 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
919 /* Check once more wether this prefix is within a
920 * "network IF_OR_PREF" one */
921 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
923 || (rip_enable_network_lookup2(connected
) >= 0))
924 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
926 &address
, &nh
, 0, 0, 0);
928 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
929 RIP_ROUTE_INTERFACE
, &address
,
930 connected
->ifp
->ifindex
);
931 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
932 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
933 RIP_ROUTE_REDISTRIBUTE
,
934 &address
, &nh
, 0, 0, 0);
939 /* Update interface status. */
940 void rip_enable_apply(struct interface
*ifp
)
943 struct rip_interface
*ri
= NULL
;
945 /* Check interface. */
946 if (!if_is_operative(ifp
))
951 /* Check network configuration. */
952 ret
= rip_enable_network_lookup_if(ifp
);
954 /* If the interface is matched. */
956 ri
->enable_network
= 1;
958 ri
->enable_network
= 0;
960 /* Check interface name configuration. */
961 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
963 ri
->enable_interface
= 1;
965 ri
->enable_interface
= 0;
967 /* any interface MUST have an IPv4 address */
968 if (!rip_if_ipv4_address_check(ifp
)) {
969 ri
->enable_network
= 0;
970 ri
->enable_interface
= 0;
973 /* Update running status of the interface. */
974 if (ri
->enable_network
|| ri
->enable_interface
) {
975 if (IS_RIP_DEBUG_EVENT
)
976 zlog_debug("turn on %s", ifp
->name
);
978 /* Add interface wake up thread. */
979 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
981 rip_connect_set(ifp
, 1);
982 } else if (ri
->running
) {
983 /* Might as well clean up the route table as well
984 * rip_if_down sets to 0 ri->running, and displays "turn
989 rip_connect_set(ifp
, 0);
993 /* Apply network configuration to all interface. */
994 static void rip_enable_apply_all(struct rip
*rip
)
996 struct interface
*ifp
;
998 /* Check each interface. */
999 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1000 rip_enable_apply(ifp
);
1003 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
1005 struct prefix_ipv4 p
;
1006 struct route_node
*node
;
1008 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1010 p
.prefix
= from
->sin_addr
;
1011 p
.prefixlen
= IPV4_MAX_BITLEN
;
1013 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
1015 route_unlock_node(node
);
1021 /* Add new RIP neighbor to the neighbor tree. */
1022 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
1024 struct route_node
*node
;
1026 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
1029 return NB_ERR_INCONSISTENCY
;
1031 node
->info
= rip
->neighbor
;
1036 /* Delete RIP neighbor from the neighbor tree. */
1037 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
1039 struct route_node
*node
;
1041 /* Lock for look up. */
1042 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
1044 return NB_ERR_INCONSISTENCY
;
1048 /* Unlock lookup lock. */
1049 route_unlock_node(node
);
1051 /* Unlock real neighbor information lock. */
1052 route_unlock_node(node
);
1057 /* Clear all network and neighbor configuration. */
1058 void rip_clean_network(struct rip
*rip
)
1062 struct route_node
*rn
;
1064 /* rip->enable_network. */
1065 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
1068 route_unlock_node(rn
);
1071 /* rip->enable_interface. */
1072 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1073 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
1074 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1075 vector_slot(rip
->enable_interface
, i
) = NULL
;
1079 /* Utility function for looking up passive interface settings. */
1080 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
1085 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1086 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
1087 if (strcmp(str
, ifname
) == 0)
1092 static void rip_passive_interface_apply(struct interface
*ifp
)
1095 struct rip_interface
*ri
;
1102 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1103 ? rip
->passive_default
1104 : !rip
->passive_default
);
1106 if (IS_RIP_DEBUG_ZEBRA
)
1107 zlog_debug("interface %s: passive = %d", ifp
->name
,
1111 static void rip_passive_interface_apply_all(struct rip
*rip
)
1113 struct interface
*ifp
;
1115 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1116 rip_passive_interface_apply(ifp
);
1119 /* Passive interface. */
1120 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1122 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1124 * Don't return an error, this can happen after changing
1125 * 'passive-default'.
1129 vector_set(rip
->passive_nondefault
,
1130 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1132 rip_passive_interface_apply_all(rip
);
1137 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1142 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1145 * Don't return an error, this can happen after changing
1146 * 'passive-default'.
1150 str
= vector_slot(rip
->passive_nondefault
, i
);
1151 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1152 vector_unset(rip
->passive_nondefault
, i
);
1154 rip_passive_interface_apply_all(rip
);
1159 /* Free all configured RIP passive-interface settings. */
1160 void rip_passive_nondefault_clean(struct rip
*rip
)
1165 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1166 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1167 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1168 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1170 rip_passive_interface_apply_all(rip
);
1173 /* Write rip configuration of each interface. */
1174 static int rip_interface_config_write(struct vty
*vty
)
1179 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1180 struct interface
*ifp
;
1182 FOR_ALL_INTERFACES (vrf
, ifp
) {
1183 struct lyd_node
*dnode
;
1185 dnode
= yang_dnode_get(
1186 running_config
->dnode
,
1187 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1188 ifp
->name
, vrf
->name
);
1193 nb_cli_show_dnode_cmds(vty
, dnode
, false);
1200 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1204 struct route_node
*node
;
1206 /* Network type RIP enable interface statement. */
1207 for (node
= route_top(rip
->enable_network
); node
;
1208 node
= route_next(node
))
1210 vty_out(vty
, " %s/%u\n",
1211 inet_ntoa(node
->p
.u
.prefix4
),
1214 /* Interface name RIP enable statement. */
1215 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1216 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1217 vty_out(vty
, " %s\n", ifname
);
1219 /* RIP neighbors listing. */
1220 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1222 vty_out(vty
, " %s\n", inet_ntoa(node
->p
.u
.prefix4
));
1227 static struct cmd_node interface_node
= {
1228 INTERFACE_NODE
, "%s(config-if)# ", 1,
1231 void rip_interface_sync(struct interface
*ifp
)
1235 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1237 struct rip_interface
*ri
;
1241 ri
->rip
= vrf
->info
;
1245 /* Called when interface structure allocated. */
1246 static int rip_interface_new_hook(struct interface
*ifp
)
1248 ifp
->info
= rip_interface_new();
1249 rip_interface_sync(ifp
);
1254 /* Called when interface structure deleted. */
1255 static int rip_interface_delete_hook(struct interface
*ifp
)
1257 rip_interface_reset(ifp
->info
);
1258 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1263 /* Allocate and initialize interface vector. */
1264 void rip_if_init(void)
1266 /* Default initial size of interface vector. */
1267 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1268 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1270 /* Install interface node. */
1271 install_node(&interface_node
, rip_interface_config_write
);