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
);
530 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
532 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
534 ri
->list
[RIP_FILTER_IN
] = NULL
;
535 ri
->list
[RIP_FILTER_OUT
] = NULL
;
537 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
538 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
540 ri
->recv_badpackets
= 0;
541 ri
->recv_badroutes
= 0;
542 ri
->sent_updates
= 0;
546 rip_interface_clean(ri
);
549 int rip_if_down(struct interface
*ifp
)
552 struct route_node
*rp
;
553 struct rip_info
*rinfo
;
554 struct rip_interface
*ri
= NULL
;
555 struct list
*list
= NULL
;
556 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
561 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
562 if ((list
= rp
->info
) != NULL
)
563 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
565 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
566 rip_ecmp_delete(rip
, rinfo
);
569 if (IS_RIP_DEBUG_EVENT
)
570 zlog_debug("turn off %s", ifp
->name
);
572 /* Leave from multicast group. */
573 rip_multicast_leave(ifp
, rip
->sock
);
582 static void rip_apply_address_add(struct connected
*ifc
)
584 struct rip_interface
*ri
= ifc
->ifp
->info
;
585 struct rip
*rip
= ri
->rip
;
586 struct prefix_ipv4 address
;
593 if (!if_is_up(ifc
->ifp
))
598 memset(&address
, 0, sizeof(address
));
599 memset(&nh
, 0, sizeof(nh
));
601 address
.family
= p
->family
;
602 address
.prefix
= p
->u
.prefix4
;
603 address
.prefixlen
= p
->prefixlen
;
604 apply_mask_ipv4(&address
);
606 nh
.ifindex
= ifc
->ifp
->ifindex
;
607 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
609 /* Check if this interface is RIP enabled or not
610 or Check if this address's prefix is RIP enabled */
611 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
612 || (rip_enable_network_lookup2(ifc
) >= 0))
613 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
614 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
618 int rip_interface_address_add(int command
, struct zclient
*zclient
,
619 zebra_size_t length
, vrf_id_t vrf_id
)
621 struct connected
*ifc
;
624 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
625 zclient
->ibuf
, vrf_id
);
632 if (p
->family
== AF_INET
) {
633 if (IS_RIP_DEBUG_ZEBRA
)
634 zlog_debug("connected address %s/%d is added",
635 inet_ntoa(p
->u
.prefix4
), p
->prefixlen
);
637 rip_enable_apply(ifc
->ifp
);
638 /* Check if this prefix needs to be redistributed */
639 rip_apply_address_add(ifc
);
641 hook_call(rip_ifaddr_add
, ifc
);
647 static void rip_apply_address_del(struct connected
*ifc
)
649 struct rip_interface
*ri
= ifc
->ifp
->info
;
650 struct rip
*rip
= ri
->rip
;
651 struct prefix_ipv4 address
;
657 if (!if_is_up(ifc
->ifp
))
662 memset(&address
, 0, sizeof(address
));
663 address
.family
= p
->family
;
664 address
.prefix
= p
->u
.prefix4
;
665 address
.prefixlen
= p
->prefixlen
;
666 apply_mask_ipv4(&address
);
668 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
669 &address
, ifc
->ifp
->ifindex
);
672 int rip_interface_address_delete(int command
, struct zclient
*zclient
,
673 zebra_size_t length
, vrf_id_t vrf_id
)
675 struct connected
*ifc
;
678 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
679 zclient
->ibuf
, vrf_id
);
683 if (p
->family
== AF_INET
) {
684 if (IS_RIP_DEBUG_ZEBRA
)
685 zlog_debug("connected address %s/%d is deleted",
686 inet_ntoa(p
->u
.prefix4
),
689 hook_call(rip_ifaddr_del
, ifc
);
691 /* Chech wether this prefix needs to be removed */
692 rip_apply_address_del(ifc
);
701 /* Check interface is enabled by network statement. */
702 /* Check wether the interface has at least a connected prefix that
703 * is within the ripng_enable_network table. */
704 static int rip_enable_network_lookup_if(struct interface
*ifp
)
706 struct rip_interface
*ri
= ifp
->info
;
707 struct rip
*rip
= ri
->rip
;
708 struct listnode
*node
, *nnode
;
709 struct connected
*connected
;
710 struct prefix_ipv4 address
;
715 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
717 struct route_node
*n
;
719 p
= connected
->address
;
721 if (p
->family
== AF_INET
) {
722 address
.family
= AF_INET
;
723 address
.prefix
= p
->u
.prefix4
;
724 address
.prefixlen
= IPV4_MAX_BITLEN
;
726 n
= route_node_match(rip
->enable_network
,
727 (struct prefix
*)&address
);
729 route_unlock_node(n
);
737 /* Check wether connected is within the ripng_enable_network table. */
738 static int rip_enable_network_lookup2(struct connected
*connected
)
740 struct rip_interface
*ri
= connected
->ifp
->info
;
741 struct rip
*rip
= ri
->rip
;
742 struct prefix_ipv4 address
;
745 p
= connected
->address
;
747 if (p
->family
== AF_INET
) {
748 struct route_node
*node
;
750 address
.family
= p
->family
;
751 address
.prefix
= p
->u
.prefix4
;
752 address
.prefixlen
= IPV4_MAX_BITLEN
;
754 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
755 * rip->enable_network */
756 node
= route_node_match(rip
->enable_network
,
757 (struct prefix
*)&address
);
760 route_unlock_node(node
);
767 /* Add RIP enable network. */
768 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
770 struct route_node
*node
;
772 node
= route_node_get(rip
->enable_network
, p
);
775 route_unlock_node(node
);
776 return NB_ERR_INCONSISTENCY
;
778 node
->info
= (void *)1;
780 /* XXX: One should find a better solution than a generic one */
781 rip_enable_apply_all(rip
);
786 /* Delete RIP enable network. */
787 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
789 struct route_node
*node
;
791 node
= route_node_lookup(rip
->enable_network
, p
);
795 /* Unlock info lock. */
796 route_unlock_node(node
);
798 /* Unlock lookup lock. */
799 route_unlock_node(node
);
801 /* XXX: One should find a better solution than a generic one */
802 rip_enable_apply_all(rip
);
807 return NB_ERR_INCONSISTENCY
;
810 /* Check interface is enabled by ifname statement. */
811 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
819 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
820 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
821 if (strcmp(str
, ifname
) == 0)
826 /* Add interface to rip_enable_if. */
827 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
831 ret
= rip_enable_if_lookup(rip
, ifname
);
833 return NB_ERR_INCONSISTENCY
;
835 vector_set(rip
->enable_interface
,
836 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
838 rip_enable_apply_all(rip
); /* TODOVJ */
843 /* Delete interface from rip_enable_if. */
844 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
849 index
= rip_enable_if_lookup(rip
, ifname
);
851 return NB_ERR_INCONSISTENCY
;
853 str
= vector_slot(rip
->enable_interface
, index
);
854 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
855 vector_unset(rip
->enable_interface
, index
);
857 rip_enable_apply_all(rip
); /* TODOVJ */
862 /* Join to multicast group and send request to the interface. */
863 static int rip_interface_wakeup(struct thread
*t
)
865 struct interface
*ifp
;
866 struct rip_interface
*ri
;
874 /* Join to multicast group. */
875 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
876 flog_err_sys(EC_LIB_SOCKET
,
877 "multicast join failed, interface %s not running",
882 /* Set running flag. */
885 /* Send RIP request to the interface. */
886 rip_request_interface(ifp
);
891 static void rip_connect_set(struct interface
*ifp
, int set
)
893 struct rip_interface
*ri
= ifp
->info
;
894 struct rip
*rip
= ri
->rip
;
895 struct listnode
*node
, *nnode
;
896 struct connected
*connected
;
897 struct prefix_ipv4 address
;
900 memset(&nh
, 0, sizeof(nh
));
902 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
904 p
= connected
->address
;
906 if (p
->family
!= AF_INET
)
909 address
.family
= AF_INET
;
910 address
.prefix
= p
->u
.prefix4
;
911 address
.prefixlen
= p
->prefixlen
;
912 apply_mask_ipv4(&address
);
914 nh
.ifindex
= connected
->ifp
->ifindex
;
915 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
917 /* Check once more wether this prefix is within a
918 * "network IF_OR_PREF" one */
919 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
921 || (rip_enable_network_lookup2(connected
) >= 0))
922 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
924 &address
, &nh
, 0, 0, 0);
926 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
927 RIP_ROUTE_INTERFACE
, &address
,
928 connected
->ifp
->ifindex
);
929 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
930 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
931 RIP_ROUTE_REDISTRIBUTE
,
932 &address
, &nh
, 0, 0, 0);
937 /* Update interface status. */
938 void rip_enable_apply(struct interface
*ifp
)
941 struct rip_interface
*ri
= NULL
;
943 /* Check interface. */
944 if (!if_is_operative(ifp
))
949 /* Check network configuration. */
950 ret
= rip_enable_network_lookup_if(ifp
);
952 /* If the interface is matched. */
954 ri
->enable_network
= 1;
956 ri
->enable_network
= 0;
958 /* Check interface name configuration. */
959 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
961 ri
->enable_interface
= 1;
963 ri
->enable_interface
= 0;
965 /* any interface MUST have an IPv4 address */
966 if (!rip_if_ipv4_address_check(ifp
)) {
967 ri
->enable_network
= 0;
968 ri
->enable_interface
= 0;
971 /* Update running status of the interface. */
972 if (ri
->enable_network
|| ri
->enable_interface
) {
973 if (IS_RIP_DEBUG_EVENT
)
974 zlog_debug("turn on %s", ifp
->name
);
976 /* Add interface wake up thread. */
977 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
979 rip_connect_set(ifp
, 1);
980 } else if (ri
->running
) {
981 /* Might as well clean up the route table as well
982 * rip_if_down sets to 0 ri->running, and displays "turn
987 rip_connect_set(ifp
, 0);
991 /* Apply network configuration to all interface. */
992 static void rip_enable_apply_all(struct rip
*rip
)
994 struct interface
*ifp
;
996 /* Check each interface. */
997 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
998 rip_enable_apply(ifp
);
1001 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
1003 struct prefix_ipv4 p
;
1004 struct route_node
*node
;
1006 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1008 p
.prefix
= from
->sin_addr
;
1009 p
.prefixlen
= IPV4_MAX_BITLEN
;
1011 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
1013 route_unlock_node(node
);
1019 /* Add new RIP neighbor to the neighbor tree. */
1020 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
1022 struct route_node
*node
;
1024 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
1027 return NB_ERR_INCONSISTENCY
;
1029 node
->info
= rip
->neighbor
;
1034 /* Delete RIP neighbor from the neighbor tree. */
1035 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
1037 struct route_node
*node
;
1039 /* Lock for look up. */
1040 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
1042 return NB_ERR_INCONSISTENCY
;
1046 /* Unlock lookup lock. */
1047 route_unlock_node(node
);
1049 /* Unlock real neighbor information lock. */
1050 route_unlock_node(node
);
1055 /* Clear all network and neighbor configuration. */
1056 void rip_clean_network(struct rip
*rip
)
1060 struct route_node
*rn
;
1062 /* rip->enable_network. */
1063 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
1066 route_unlock_node(rn
);
1069 /* rip->enable_interface. */
1070 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1071 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
1072 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1073 vector_slot(rip
->enable_interface
, i
) = NULL
;
1077 /* Utility function for looking up passive interface settings. */
1078 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
1083 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1084 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
1085 if (strcmp(str
, ifname
) == 0)
1090 static void rip_passive_interface_apply(struct interface
*ifp
)
1093 struct rip_interface
*ri
;
1100 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1101 ? rip
->passive_default
1102 : !rip
->passive_default
);
1104 if (IS_RIP_DEBUG_ZEBRA
)
1105 zlog_debug("interface %s: passive = %d", ifp
->name
,
1109 static void rip_passive_interface_apply_all(struct rip
*rip
)
1111 struct interface
*ifp
;
1113 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1114 rip_passive_interface_apply(ifp
);
1117 /* Passive interface. */
1118 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1120 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1122 * Don't return an error, this can happen after changing
1123 * 'passive-default'.
1127 vector_set(rip
->passive_nondefault
,
1128 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1130 rip_passive_interface_apply_all(rip
);
1135 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1140 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1143 * Don't return an error, this can happen after changing
1144 * 'passive-default'.
1148 str
= vector_slot(rip
->passive_nondefault
, i
);
1149 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1150 vector_unset(rip
->passive_nondefault
, i
);
1152 rip_passive_interface_apply_all(rip
);
1157 /* Free all configured RIP passive-interface settings. */
1158 void rip_passive_nondefault_clean(struct rip
*rip
)
1163 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1164 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1165 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1166 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1168 rip_passive_interface_apply_all(rip
);
1171 /* Write rip configuration of each interface. */
1172 static int rip_interface_config_write(struct vty
*vty
)
1177 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1178 struct interface
*ifp
;
1180 FOR_ALL_INTERFACES (vrf
, ifp
) {
1181 struct lyd_node
*dnode
;
1183 dnode
= yang_dnode_get(
1184 running_config
->dnode
,
1185 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1186 ifp
->name
, vrf
->name
);
1191 nb_cli_show_dnode_cmds(vty
, dnode
, false);
1198 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1202 struct route_node
*node
;
1204 /* Network type RIP enable interface statement. */
1205 for (node
= route_top(rip
->enable_network
); node
;
1206 node
= route_next(node
))
1208 vty_out(vty
, " %s/%u\n",
1209 inet_ntoa(node
->p
.u
.prefix4
),
1212 /* Interface name RIP enable statement. */
1213 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1214 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1215 vty_out(vty
, " %s\n", ifname
);
1217 /* RIP neighbors listing. */
1218 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1220 vty_out(vty
, " %s\n", inet_ntoa(node
->p
.u
.prefix4
));
1225 static struct cmd_node interface_node
= {
1226 INTERFACE_NODE
, "%s(config-if)# ", 1,
1229 void rip_interface_sync(struct interface
*ifp
)
1233 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1235 struct rip_interface
*ri
;
1239 ri
->rip
= vrf
->info
;
1243 /* Called when interface structure allocated. */
1244 static int rip_interface_new_hook(struct interface
*ifp
)
1246 ifp
->info
= rip_interface_new();
1247 rip_interface_sync(ifp
);
1252 /* Called when interface structure deleted. */
1253 static int rip_interface_delete_hook(struct interface
*ifp
)
1255 rip_interface_reset(ifp
->info
);
1256 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1261 /* Allocate and initialize interface vector. */
1262 void rip_if_init(void)
1264 /* Default initial size of interface vector. */
1265 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1266 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1268 /* Install interface node. */
1269 install_node(&interface_node
, rip_interface_config_write
);