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(ZAPI_CALLBACK_ARGS
)
349 struct interface
*ifp
;
354 /* zebra_interface_state_read() updates interface structure in
356 ifp
= zebra_interface_state_read(s
, vrf_id
);
361 rip_interface_sync(ifp
);
364 if (IS_RIP_DEBUG_ZEBRA
)
366 "interface %s vrf %u index %d flags %llx metric %d mtu %d is down",
367 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
368 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
373 /* Inteface link up message processing */
374 int rip_interface_up(ZAPI_CALLBACK_ARGS
)
376 struct interface
*ifp
;
378 /* zebra_interface_state_read () updates interface structure in
380 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
385 if (IS_RIP_DEBUG_ZEBRA
)
387 "interface %s vrf %u index %d flags %#llx metric %d mtu %d is up",
388 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
389 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
391 rip_interface_sync(ifp
);
393 /* Check if this interface is RIP enabled or not.*/
394 rip_enable_apply(ifp
);
396 /* Check for a passive interface */
397 rip_passive_interface_apply(ifp
);
399 /* Apply distribute list to the all interface. */
400 rip_distribute_update_interface(ifp
);
405 /* Inteface addition message from zebra. */
406 int rip_interface_add(ZAPI_CALLBACK_ARGS
)
408 struct interface
*ifp
;
410 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
411 rip_interface_sync(ifp
);
413 if (IS_RIP_DEBUG_ZEBRA
)
415 "interface add %s vrf %u index %d flags %#llx metric %d mtu %d",
416 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
417 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
419 /* Check if this interface is RIP enabled or not.*/
420 rip_enable_apply(ifp
);
422 /* Check for a passive interface */
423 rip_passive_interface_apply(ifp
);
425 /* Apply distribute list to the all interface. */
426 rip_distribute_update_interface(ifp
);
428 /* rip_request_neighbor_all (); */
430 /* Check interface routemap. */
431 rip_if_rmap_update_interface(ifp
);
436 int rip_interface_delete(ZAPI_CALLBACK_ARGS
)
438 struct interface
*ifp
;
443 /* zebra_interface_state_read() updates interface structure in iflist */
444 ifp
= zebra_interface_state_read(s
, vrf_id
);
449 rip_interface_sync(ifp
);
455 "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d",
456 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
457 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
459 /* To support pseudo interface do not free interface structure. */
460 /* if_delete(ifp); */
461 if_set_index(ifp
, IFINDEX_INTERNAL
);
466 /* VRF update for an interface. */
467 int rip_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
469 struct interface
*ifp
;
472 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
477 if (IS_RIP_DEBUG_ZEBRA
)
478 zlog_debug("interface %s VRF change vrf_id %u new vrf id %u",
479 ifp
->name
, vrf_id
, new_vrf_id
);
481 if_update_to_new_vrf(ifp
, new_vrf_id
);
482 rip_interface_sync(ifp
);
487 static void rip_interface_clean(struct rip_interface
*ri
)
489 ri
->enable_network
= 0;
490 ri
->enable_interface
= 0;
494 thread_cancel(ri
->t_wakeup
);
499 void rip_interfaces_clean(struct rip
*rip
)
501 struct interface
*ifp
;
503 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
504 rip_interface_clean(ifp
->info
);
507 static void rip_interface_reset(struct rip_interface
*ri
)
509 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
511 ri
->md5_auth_len
= yang_get_default_enum(
512 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
514 /* Set default split-horizon behavior. If the interface is Frame
515 Relay or SMDS is enabled, the default value for split-horizon is
516 off. But currently Zebra does detect Frame Relay or SMDS
517 interface. So all interface is set to split horizon. */
519 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
521 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
522 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
523 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
525 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
527 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
529 ri
->list
[RIP_FILTER_IN
] = NULL
;
530 ri
->list
[RIP_FILTER_OUT
] = NULL
;
532 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
533 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
535 ri
->recv_badpackets
= 0;
536 ri
->recv_badroutes
= 0;
537 ri
->sent_updates
= 0;
541 rip_interface_clean(ri
);
544 int rip_if_down(struct interface
*ifp
)
547 struct route_node
*rp
;
548 struct rip_info
*rinfo
;
549 struct rip_interface
*ri
= NULL
;
550 struct list
*list
= NULL
;
551 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
556 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
557 if ((list
= rp
->info
) != NULL
)
558 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
560 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
561 rip_ecmp_delete(rip
, rinfo
);
564 if (IS_RIP_DEBUG_EVENT
)
565 zlog_debug("turn off %s", ifp
->name
);
567 /* Leave from multicast group. */
568 rip_multicast_leave(ifp
, rip
->sock
);
577 static void rip_apply_address_add(struct connected
*ifc
)
579 struct rip_interface
*ri
= ifc
->ifp
->info
;
580 struct rip
*rip
= ri
->rip
;
581 struct prefix_ipv4 address
;
588 if (!if_is_up(ifc
->ifp
))
593 memset(&address
, 0, sizeof(address
));
594 memset(&nh
, 0, sizeof(nh
));
596 address
.family
= p
->family
;
597 address
.prefix
= p
->u
.prefix4
;
598 address
.prefixlen
= p
->prefixlen
;
599 apply_mask_ipv4(&address
);
601 nh
.ifindex
= ifc
->ifp
->ifindex
;
602 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
604 /* Check if this interface is RIP enabled or not
605 or Check if this address's prefix is RIP enabled */
606 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
607 || (rip_enable_network_lookup2(ifc
) >= 0))
608 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
609 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
613 int rip_interface_address_add(ZAPI_CALLBACK_ARGS
)
615 struct connected
*ifc
;
618 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
619 zclient
->ibuf
, vrf_id
);
626 if (p
->family
== AF_INET
) {
627 if (IS_RIP_DEBUG_ZEBRA
)
628 zlog_debug("connected address %s/%d is added",
629 inet_ntoa(p
->u
.prefix4
), p
->prefixlen
);
631 rip_enable_apply(ifc
->ifp
);
632 /* Check if this prefix needs to be redistributed */
633 rip_apply_address_add(ifc
);
635 hook_call(rip_ifaddr_add
, ifc
);
641 static void rip_apply_address_del(struct connected
*ifc
)
643 struct rip_interface
*ri
= ifc
->ifp
->info
;
644 struct rip
*rip
= ri
->rip
;
645 struct prefix_ipv4 address
;
651 if (!if_is_up(ifc
->ifp
))
656 memset(&address
, 0, sizeof(address
));
657 address
.family
= p
->family
;
658 address
.prefix
= p
->u
.prefix4
;
659 address
.prefixlen
= p
->prefixlen
;
660 apply_mask_ipv4(&address
);
662 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
663 &address
, ifc
->ifp
->ifindex
);
666 int rip_interface_address_delete(ZAPI_CALLBACK_ARGS
)
668 struct connected
*ifc
;
671 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
672 zclient
->ibuf
, vrf_id
);
676 if (p
->family
== AF_INET
) {
677 if (IS_RIP_DEBUG_ZEBRA
)
678 zlog_debug("connected address %s/%d is deleted",
679 inet_ntoa(p
->u
.prefix4
),
682 hook_call(rip_ifaddr_del
, ifc
);
684 /* Chech wether this prefix needs to be removed */
685 rip_apply_address_del(ifc
);
694 /* Check interface is enabled by network statement. */
695 /* Check wether the interface has at least a connected prefix that
696 * is within the ripng_enable_network table. */
697 static int rip_enable_network_lookup_if(struct interface
*ifp
)
699 struct rip_interface
*ri
= ifp
->info
;
700 struct rip
*rip
= ri
->rip
;
701 struct listnode
*node
, *nnode
;
702 struct connected
*connected
;
703 struct prefix_ipv4 address
;
708 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
710 struct route_node
*n
;
712 p
= connected
->address
;
714 if (p
->family
== AF_INET
) {
715 address
.family
= AF_INET
;
716 address
.prefix
= p
->u
.prefix4
;
717 address
.prefixlen
= IPV4_MAX_BITLEN
;
719 n
= route_node_match(rip
->enable_network
,
720 (struct prefix
*)&address
);
722 route_unlock_node(n
);
730 /* Check wether connected is within the ripng_enable_network table. */
731 static int rip_enable_network_lookup2(struct connected
*connected
)
733 struct rip_interface
*ri
= connected
->ifp
->info
;
734 struct rip
*rip
= ri
->rip
;
735 struct prefix_ipv4 address
;
738 p
= connected
->address
;
740 if (p
->family
== AF_INET
) {
741 struct route_node
*node
;
743 address
.family
= p
->family
;
744 address
.prefix
= p
->u
.prefix4
;
745 address
.prefixlen
= IPV4_MAX_BITLEN
;
747 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
748 * rip->enable_network */
749 node
= route_node_match(rip
->enable_network
,
750 (struct prefix
*)&address
);
753 route_unlock_node(node
);
760 /* Add RIP enable network. */
761 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
763 struct route_node
*node
;
765 node
= route_node_get(rip
->enable_network
, p
);
768 route_unlock_node(node
);
769 return NB_ERR_INCONSISTENCY
;
771 node
->info
= (void *)1;
773 /* XXX: One should find a better solution than a generic one */
774 rip_enable_apply_all(rip
);
779 /* Delete RIP enable network. */
780 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
782 struct route_node
*node
;
784 node
= route_node_lookup(rip
->enable_network
, p
);
788 /* Unlock info lock. */
789 route_unlock_node(node
);
791 /* Unlock lookup lock. */
792 route_unlock_node(node
);
794 /* XXX: One should find a better solution than a generic one */
795 rip_enable_apply_all(rip
);
800 return NB_ERR_INCONSISTENCY
;
803 /* Check interface is enabled by ifname statement. */
804 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
812 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
813 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
814 if (strcmp(str
, ifname
) == 0)
819 /* Add interface to rip_enable_if. */
820 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
824 ret
= rip_enable_if_lookup(rip
, ifname
);
826 return NB_ERR_INCONSISTENCY
;
828 vector_set(rip
->enable_interface
,
829 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
831 rip_enable_apply_all(rip
); /* TODOVJ */
836 /* Delete interface from rip_enable_if. */
837 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
842 index
= rip_enable_if_lookup(rip
, ifname
);
844 return NB_ERR_INCONSISTENCY
;
846 str
= vector_slot(rip
->enable_interface
, index
);
847 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
848 vector_unset(rip
->enable_interface
, index
);
850 rip_enable_apply_all(rip
); /* TODOVJ */
855 /* Join to multicast group and send request to the interface. */
856 static int rip_interface_wakeup(struct thread
*t
)
858 struct interface
*ifp
;
859 struct rip_interface
*ri
;
867 /* Join to multicast group. */
868 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
869 flog_err_sys(EC_LIB_SOCKET
,
870 "multicast join failed, interface %s not running",
875 /* Set running flag. */
878 /* Send RIP request to the interface. */
879 rip_request_interface(ifp
);
884 static void rip_connect_set(struct interface
*ifp
, int set
)
886 struct rip_interface
*ri
= ifp
->info
;
887 struct rip
*rip
= ri
->rip
;
888 struct listnode
*node
, *nnode
;
889 struct connected
*connected
;
890 struct prefix_ipv4 address
;
893 memset(&nh
, 0, sizeof(nh
));
895 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
897 p
= connected
->address
;
899 if (p
->family
!= AF_INET
)
902 address
.family
= AF_INET
;
903 address
.prefix
= p
->u
.prefix4
;
904 address
.prefixlen
= p
->prefixlen
;
905 apply_mask_ipv4(&address
);
907 nh
.ifindex
= connected
->ifp
->ifindex
;
908 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
910 /* Check once more wether this prefix is within a
911 * "network IF_OR_PREF" one */
912 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
914 || (rip_enable_network_lookup2(connected
) >= 0))
915 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
917 &address
, &nh
, 0, 0, 0);
919 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
920 RIP_ROUTE_INTERFACE
, &address
,
921 connected
->ifp
->ifindex
);
922 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
923 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
924 RIP_ROUTE_REDISTRIBUTE
,
925 &address
, &nh
, 0, 0, 0);
930 /* Update interface status. */
931 void rip_enable_apply(struct interface
*ifp
)
934 struct rip_interface
*ri
= NULL
;
936 /* Check interface. */
937 if (!if_is_operative(ifp
))
942 /* Check network configuration. */
943 ret
= rip_enable_network_lookup_if(ifp
);
945 /* If the interface is matched. */
947 ri
->enable_network
= 1;
949 ri
->enable_network
= 0;
951 /* Check interface name configuration. */
952 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
954 ri
->enable_interface
= 1;
956 ri
->enable_interface
= 0;
958 /* any interface MUST have an IPv4 address */
959 if (!rip_if_ipv4_address_check(ifp
)) {
960 ri
->enable_network
= 0;
961 ri
->enable_interface
= 0;
964 /* Update running status of the interface. */
965 if (ri
->enable_network
|| ri
->enable_interface
) {
966 if (IS_RIP_DEBUG_EVENT
)
967 zlog_debug("turn on %s", ifp
->name
);
969 /* Add interface wake up thread. */
970 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
972 rip_connect_set(ifp
, 1);
973 } else if (ri
->running
) {
974 /* Might as well clean up the route table as well
975 * rip_if_down sets to 0 ri->running, and displays "turn
980 rip_connect_set(ifp
, 0);
984 /* Apply network configuration to all interface. */
985 static void rip_enable_apply_all(struct rip
*rip
)
987 struct interface
*ifp
;
989 /* Check each interface. */
990 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
991 rip_enable_apply(ifp
);
994 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
996 struct prefix_ipv4 p
;
997 struct route_node
*node
;
999 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1001 p
.prefix
= from
->sin_addr
;
1002 p
.prefixlen
= IPV4_MAX_BITLEN
;
1004 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
1006 route_unlock_node(node
);
1012 /* Add new RIP neighbor to the neighbor tree. */
1013 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
1015 struct route_node
*node
;
1017 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
1020 return NB_ERR_INCONSISTENCY
;
1022 node
->info
= rip
->neighbor
;
1027 /* Delete RIP neighbor from the neighbor tree. */
1028 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
1030 struct route_node
*node
;
1032 /* Lock for look up. */
1033 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
1035 return NB_ERR_INCONSISTENCY
;
1039 /* Unlock lookup lock. */
1040 route_unlock_node(node
);
1042 /* Unlock real neighbor information lock. */
1043 route_unlock_node(node
);
1048 /* Clear all network and neighbor configuration. */
1049 void rip_clean_network(struct rip
*rip
)
1053 struct route_node
*rn
;
1055 /* rip->enable_network. */
1056 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
1059 route_unlock_node(rn
);
1062 /* rip->enable_interface. */
1063 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1064 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
1065 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1066 vector_slot(rip
->enable_interface
, i
) = NULL
;
1070 /* Utility function for looking up passive interface settings. */
1071 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
1076 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1077 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
1078 if (strcmp(str
, ifname
) == 0)
1083 static void rip_passive_interface_apply(struct interface
*ifp
)
1086 struct rip_interface
*ri
;
1093 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1094 ? rip
->passive_default
1095 : !rip
->passive_default
);
1097 if (IS_RIP_DEBUG_ZEBRA
)
1098 zlog_debug("interface %s: passive = %d", ifp
->name
,
1102 static void rip_passive_interface_apply_all(struct rip
*rip
)
1104 struct interface
*ifp
;
1106 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1107 rip_passive_interface_apply(ifp
);
1110 /* Passive interface. */
1111 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1113 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1115 * Don't return an error, this can happen after changing
1116 * 'passive-default'.
1120 vector_set(rip
->passive_nondefault
,
1121 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1123 rip_passive_interface_apply_all(rip
);
1128 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1133 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1136 * Don't return an error, this can happen after changing
1137 * 'passive-default'.
1141 str
= vector_slot(rip
->passive_nondefault
, i
);
1142 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1143 vector_unset(rip
->passive_nondefault
, i
);
1145 rip_passive_interface_apply_all(rip
);
1150 /* Free all configured RIP passive-interface settings. */
1151 void rip_passive_nondefault_clean(struct rip
*rip
)
1156 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1157 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1158 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1159 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1161 rip_passive_interface_apply_all(rip
);
1164 /* Write rip configuration of each interface. */
1165 static int rip_interface_config_write(struct vty
*vty
)
1170 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1171 struct interface
*ifp
;
1173 FOR_ALL_INTERFACES (vrf
, ifp
) {
1174 struct lyd_node
*dnode
;
1176 dnode
= yang_dnode_get(
1177 running_config
->dnode
,
1178 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1179 ifp
->name
, vrf
->name
);
1184 nb_cli_show_dnode_cmds(vty
, dnode
, false);
1191 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1195 struct route_node
*node
;
1197 /* Network type RIP enable interface statement. */
1198 for (node
= route_top(rip
->enable_network
); node
;
1199 node
= route_next(node
))
1201 vty_out(vty
, " %s/%u\n",
1202 inet_ntoa(node
->p
.u
.prefix4
),
1205 /* Interface name RIP enable statement. */
1206 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1207 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1208 vty_out(vty
, " %s\n", ifname
);
1210 /* RIP neighbors listing. */
1211 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1213 vty_out(vty
, " %s\n", inet_ntoa(node
->p
.u
.prefix4
));
1218 static struct cmd_node interface_node
= {
1219 INTERFACE_NODE
, "%s(config-if)# ", 1,
1222 void rip_interface_sync(struct interface
*ifp
)
1226 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1228 struct rip_interface
*ri
;
1232 ri
->rip
= vrf
->info
;
1236 /* Called when interface structure allocated. */
1237 static int rip_interface_new_hook(struct interface
*ifp
)
1239 ifp
->info
= rip_interface_new();
1240 rip_interface_sync(ifp
);
1245 /* Called when interface structure deleted. */
1246 static int rip_interface_delete_hook(struct interface
*ifp
)
1248 rip_interface_reset(ifp
->info
);
1249 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1254 /* Allocate and initialize interface vector. */
1255 void rip_if_init(void)
1257 /* Default initial size of interface vector. */
1258 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1259 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1261 /* Install interface node. */
1262 install_node(&interface_node
, rip_interface_config_write
);