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_MTYPE_STATIC(RIPD
, RIP_INTERFACE
, "RIP interface")
47 DEFINE_MTYPE(RIPD
, RIP_INTERFACE_STRING
, "RIP Interface String")
48 DEFINE_HOOK(rip_ifaddr_add
, (struct connected
* ifc
), (ifc
))
49 DEFINE_HOOK(rip_ifaddr_del
, (struct connected
* ifc
), (ifc
))
51 /* static prototypes */
52 static void rip_enable_apply(struct interface
*);
53 static void rip_passive_interface_apply(struct interface
*);
54 static int rip_if_down(struct interface
*ifp
);
55 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
);
56 static int rip_enable_network_lookup2(struct connected
*connected
);
57 static void rip_enable_apply_all(struct rip
*rip
);
59 const struct message ri_version_msg
[] = {{RI_RIP_VERSION_1
, "1"},
60 {RI_RIP_VERSION_2
, "2"},
61 {RI_RIP_VERSION_1_AND_2
, "1 2"},
62 {RI_RIP_VERSION_NONE
, "none"},
65 /* Join to the RIP version 2 multicast group. */
66 static int ipv4_multicast_join(int sock
, struct in_addr group
,
67 struct in_addr ifa
, ifindex_t ifindex
)
71 ret
= setsockopt_ipv4_multicast(sock
, IP_ADD_MEMBERSHIP
, ifa
,
72 group
.s_addr
, ifindex
);
75 zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s",
76 safe_strerror(errno
));
81 /* Leave from the RIP version 2 multicast group. */
82 static int ipv4_multicast_leave(int sock
, struct in_addr group
,
83 struct in_addr ifa
, ifindex_t ifindex
)
87 ret
= setsockopt_ipv4_multicast(sock
, IP_DROP_MEMBERSHIP
, ifa
,
88 group
.s_addr
, ifindex
);
91 zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");
96 static void rip_interface_reset(struct rip_interface
*);
98 /* Allocate new RIP's interface configuration. */
99 static struct rip_interface
*rip_interface_new(void)
101 struct rip_interface
*ri
;
103 ri
= XCALLOC(MTYPE_RIP_INTERFACE
, sizeof(struct rip_interface
));
105 rip_interface_reset(ri
);
110 void rip_interface_multicast_set(int sock
, struct connected
*connected
)
114 assert(connected
!= NULL
);
116 addr
= CONNECTED_ID(connected
)->u
.prefix4
;
118 if (setsockopt_ipv4_multicast_if(sock
, addr
, connected
->ifp
->ifindex
)
121 "Can't setsockopt IP_MULTICAST_IF on fd %d to "
122 "ifindex %d for interface %s",
123 sock
, connected
->ifp
->ifindex
, connected
->ifp
->name
);
129 /* Send RIP request packet to specified interface. */
130 static void rip_request_interface_send(struct interface
*ifp
, uint8_t version
)
132 struct sockaddr_in to
;
134 /* RIPv2 support multicast. */
135 if (version
== RIPv2
&& if_is_multicast(ifp
)) {
137 if (IS_RIP_DEBUG_EVENT
)
138 zlog_debug("multicast request on %s", ifp
->name
);
140 rip_request_send(NULL
, ifp
, version
, NULL
);
144 /* RIPv1 and non multicast interface. */
145 if (if_is_pointopoint(ifp
) || if_is_broadcast(ifp
)) {
146 struct listnode
*cnode
, *cnnode
;
147 struct connected
*connected
;
149 if (IS_RIP_DEBUG_EVENT
)
150 zlog_debug("broadcast request to %s", ifp
->name
);
152 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
,
154 if (connected
->address
->family
!= AF_INET
)
157 memset(&to
, 0, sizeof(struct sockaddr_in
));
158 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
159 if (connected
->destination
)
160 /* use specified broadcast or peer
161 * destination addr */
162 to
.sin_addr
= connected
->destination
->u
.prefix4
;
163 else if (connected
->address
->prefixlen
164 < IPV4_MAX_PREFIXLEN
)
165 /* calculate the appropriate broadcast
167 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
168 connected
->address
->u
.prefix4
.s_addr
,
169 connected
->address
->prefixlen
);
171 /* do not know where to send the packet
175 if (IS_RIP_DEBUG_EVENT
)
176 zlog_debug("SEND request to %s",
177 inet_ntoa(to
.sin_addr
));
179 rip_request_send(&to
, ifp
, version
, connected
);
184 /* This will be executed when interface goes up. */
185 static void rip_request_interface(struct interface
*ifp
)
187 struct rip_interface
*ri
;
190 /* In default ripd doesn't send RIP_REQUEST to the loopback interface.
192 if (if_is_loopback(ifp
))
195 /* If interface is down, don't send RIP packet. */
196 if (!if_is_operative(ifp
))
199 /* Fetch RIP interface information. */
202 /* If there is no version configuration in the interface,
203 use rip's version setting. */
204 vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? ri
->rip
->version_send
207 rip_request_interface_send(ifp
, RIPv1
);
209 rip_request_interface_send(ifp
, RIPv2
);
213 /* Send RIP request to the neighbor. */
215 rip_request_neighbor (struct in_addr addr
)
217 struct sockaddr_in to
;
219 memset (&to
, 0, sizeof (struct sockaddr_in
));
220 to
.sin_port
= htons (RIP_PORT_DEFAULT
);
223 rip_request_send (&to
, NULL
, rip
->version_send
, NULL
);
226 /* Request routes at all interfaces. */
228 rip_request_neighbor_all (void)
230 struct route_node
*rp
;
235 if (IS_RIP_DEBUG_EVENT
)
236 zlog_debug ("request to the all neighbor");
238 /* Send request to all neighbor. */
239 for (rp
= route_top (rip
->neighbor
); rp
; rp
= route_next (rp
))
241 rip_request_neighbor (rp
->p
.u
.prefix4
);
245 /* Multicast packet receive socket. */
246 static int rip_multicast_join(struct interface
*ifp
, int sock
)
248 struct listnode
*cnode
;
249 struct connected
*ifc
;
251 if (if_is_operative(ifp
) && if_is_multicast(ifp
)) {
252 if (IS_RIP_DEBUG_EVENT
)
253 zlog_debug("multicast join at %s", ifp
->name
);
255 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
256 struct prefix_ipv4
*p
;
257 struct in_addr group
;
259 p
= (struct prefix_ipv4
*)ifc
->address
;
261 if (p
->family
!= AF_INET
)
264 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
265 if (ipv4_multicast_join(sock
, group
, p
->prefix
,
276 /* Leave from multicast group. */
277 static void rip_multicast_leave(struct interface
*ifp
, int sock
)
279 struct listnode
*cnode
;
280 struct connected
*connected
;
282 if (if_is_up(ifp
) && if_is_multicast(ifp
)) {
283 if (IS_RIP_DEBUG_EVENT
)
284 zlog_debug("multicast leave from %s", ifp
->name
);
286 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
287 struct prefix_ipv4
*p
;
288 struct in_addr group
;
290 p
= (struct prefix_ipv4
*)connected
->address
;
292 if (p
->family
!= AF_INET
)
295 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
296 if (ipv4_multicast_leave(sock
, group
, p
->prefix
,
304 /* Is there and address on interface that I could use ? */
305 static int rip_if_ipv4_address_check(struct interface
*ifp
)
308 struct connected
*connected
;
311 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
314 p
= connected
->address
;
316 if (p
->family
== AF_INET
)
324 /* Does this address belongs to me ? */
325 int if_check_address(struct rip
*rip
, struct in_addr addr
)
327 struct interface
*ifp
;
329 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
330 struct listnode
*cnode
;
331 struct connected
*connected
;
333 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
334 struct prefix_ipv4
*p
;
336 p
= (struct prefix_ipv4
*)connected
->address
;
338 if (p
->family
!= AF_INET
)
341 if (IPV4_ADDR_CMP(&p
->prefix
, &addr
) == 0)
348 /* Inteface link down message processing. */
349 int rip_interface_down(ZAPI_CALLBACK_ARGS
)
351 struct interface
*ifp
;
356 /* zebra_interface_state_read() updates interface structure in
358 ifp
= zebra_interface_state_read(s
, vrf_id
);
363 rip_interface_sync(ifp
);
366 if (IS_RIP_DEBUG_ZEBRA
)
368 "interface %s vrf %u index %d flags %llx metric %d mtu %d is down",
369 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
370 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
375 /* Inteface link up message processing */
376 int rip_interface_up(ZAPI_CALLBACK_ARGS
)
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(ZAPI_CALLBACK_ARGS
)
410 struct interface
*ifp
;
412 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
413 rip_interface_sync(ifp
);
415 if (IS_RIP_DEBUG_ZEBRA
)
417 "interface add %s vrf %u index %d flags %#llx metric %d mtu %d",
418 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
419 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
421 /* Check if this interface is RIP enabled or not.*/
422 rip_enable_apply(ifp
);
424 /* Check for a passive interface */
425 rip_passive_interface_apply(ifp
);
427 /* Apply distribute list to the all interface. */
428 rip_distribute_update_interface(ifp
);
430 /* rip_request_neighbor_all (); */
432 /* Check interface routemap. */
433 rip_if_rmap_update_interface(ifp
);
438 int rip_interface_delete(ZAPI_CALLBACK_ARGS
)
440 struct interface
*ifp
;
445 /* zebra_interface_state_read() updates interface structure in iflist */
446 ifp
= zebra_interface_state_read(s
, vrf_id
);
451 rip_interface_sync(ifp
);
457 "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d",
458 ifp
->name
, ifp
->vrf_id
, ifp
->ifindex
,
459 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
461 /* To support pseudo interface do not free interface structure. */
462 /* if_delete(ifp); */
463 if_set_index(ifp
, IFINDEX_INTERNAL
);
468 /* VRF update for an interface. */
469 int rip_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
471 struct interface
*ifp
;
474 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
479 if (IS_RIP_DEBUG_ZEBRA
)
480 zlog_debug("interface %s VRF change vrf_id %u new vrf id %u",
481 ifp
->name
, vrf_id
, new_vrf_id
);
483 if_update_to_new_vrf(ifp
, new_vrf_id
);
484 rip_interface_sync(ifp
);
489 static void rip_interface_clean(struct rip_interface
*ri
)
491 ri
->enable_network
= 0;
492 ri
->enable_interface
= 0;
496 thread_cancel(ri
->t_wakeup
);
501 void rip_interfaces_clean(struct rip
*rip
)
503 struct interface
*ifp
;
505 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
506 rip_interface_clean(ifp
->info
);
509 static void rip_interface_reset(struct rip_interface
*ri
)
511 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
513 ri
->md5_auth_len
= yang_get_default_enum(
514 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
516 /* Set default split-horizon behavior. If the interface is Frame
517 Relay or SMDS is enabled, the default value for split-horizon is
518 off. But currently Zebra does detect Frame Relay or SMDS
519 interface. So all interface is set to split horizon. */
521 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
523 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
524 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
525 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
527 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
529 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
531 ri
->list
[RIP_FILTER_IN
] = NULL
;
532 ri
->list
[RIP_FILTER_OUT
] = NULL
;
534 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
535 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
537 ri
->recv_badpackets
= 0;
538 ri
->recv_badroutes
= 0;
539 ri
->sent_updates
= 0;
543 rip_interface_clean(ri
);
546 int rip_if_down(struct interface
*ifp
)
549 struct route_node
*rp
;
550 struct rip_info
*rinfo
;
551 struct rip_interface
*ri
= NULL
;
552 struct list
*list
= NULL
;
553 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
558 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
559 if ((list
= rp
->info
) != NULL
)
560 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
562 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
563 rip_ecmp_delete(rip
, rinfo
);
566 if (IS_RIP_DEBUG_EVENT
)
567 zlog_debug("turn off %s", ifp
->name
);
569 /* Leave from multicast group. */
570 rip_multicast_leave(ifp
, rip
->sock
);
579 static void rip_apply_address_add(struct connected
*ifc
)
581 struct rip_interface
*ri
= ifc
->ifp
->info
;
582 struct rip
*rip
= ri
->rip
;
583 struct prefix_ipv4 address
;
590 if (!if_is_up(ifc
->ifp
))
595 memset(&address
, 0, sizeof(address
));
596 memset(&nh
, 0, sizeof(nh
));
598 address
.family
= p
->family
;
599 address
.prefix
= p
->u
.prefix4
;
600 address
.prefixlen
= p
->prefixlen
;
601 apply_mask_ipv4(&address
);
603 nh
.ifindex
= ifc
->ifp
->ifindex
;
604 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
606 /* Check if this interface is RIP enabled or not
607 or Check if this address's prefix is RIP enabled */
608 if ((rip_enable_if_lookup(rip
, ifc
->ifp
->name
) >= 0)
609 || (rip_enable_network_lookup2(ifc
) >= 0))
610 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
611 RIP_ROUTE_INTERFACE
, &address
, &nh
, 0, 0,
615 int rip_interface_address_add(ZAPI_CALLBACK_ARGS
)
617 struct connected
*ifc
;
620 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
621 zclient
->ibuf
, vrf_id
);
628 if (p
->family
== AF_INET
) {
629 if (IS_RIP_DEBUG_ZEBRA
)
630 zlog_debug("connected address %s/%d is added",
631 inet_ntoa(p
->u
.prefix4
), p
->prefixlen
);
633 rip_enable_apply(ifc
->ifp
);
634 /* Check if this prefix needs to be redistributed */
635 rip_apply_address_add(ifc
);
637 hook_call(rip_ifaddr_add
, ifc
);
643 static void rip_apply_address_del(struct connected
*ifc
)
645 struct rip_interface
*ri
= ifc
->ifp
->info
;
646 struct rip
*rip
= ri
->rip
;
647 struct prefix_ipv4 address
;
653 if (!if_is_up(ifc
->ifp
))
658 memset(&address
, 0, sizeof(address
));
659 address
.family
= p
->family
;
660 address
.prefix
= p
->u
.prefix4
;
661 address
.prefixlen
= p
->prefixlen
;
662 apply_mask_ipv4(&address
);
664 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
665 &address
, ifc
->ifp
->ifindex
);
668 int rip_interface_address_delete(ZAPI_CALLBACK_ARGS
)
670 struct connected
*ifc
;
673 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
674 zclient
->ibuf
, vrf_id
);
678 if (p
->family
== AF_INET
) {
679 if (IS_RIP_DEBUG_ZEBRA
)
680 zlog_debug("connected address %s/%d is deleted",
681 inet_ntoa(p
->u
.prefix4
),
684 hook_call(rip_ifaddr_del
, ifc
);
686 /* Chech wether this prefix needs to be removed */
687 rip_apply_address_del(ifc
);
696 /* Check interface is enabled by network statement. */
697 /* Check wether the interface has at least a connected prefix that
698 * is within the ripng_enable_network table. */
699 static int rip_enable_network_lookup_if(struct interface
*ifp
)
701 struct rip_interface
*ri
= ifp
->info
;
702 struct rip
*rip
= ri
->rip
;
703 struct listnode
*node
, *nnode
;
704 struct connected
*connected
;
705 struct prefix_ipv4 address
;
710 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
712 struct route_node
*n
;
714 p
= connected
->address
;
716 if (p
->family
== AF_INET
) {
717 address
.family
= AF_INET
;
718 address
.prefix
= p
->u
.prefix4
;
719 address
.prefixlen
= IPV4_MAX_BITLEN
;
721 n
= route_node_match(rip
->enable_network
,
722 (struct prefix
*)&address
);
724 route_unlock_node(n
);
732 /* Check wether connected is within the ripng_enable_network table. */
733 static int rip_enable_network_lookup2(struct connected
*connected
)
735 struct rip_interface
*ri
= connected
->ifp
->info
;
736 struct rip
*rip
= ri
->rip
;
737 struct prefix_ipv4 address
;
740 p
= connected
->address
;
742 if (p
->family
== AF_INET
) {
743 struct route_node
*node
;
745 address
.family
= p
->family
;
746 address
.prefix
= p
->u
.prefix4
;
747 address
.prefixlen
= IPV4_MAX_BITLEN
;
749 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
750 * rip->enable_network */
751 node
= route_node_match(rip
->enable_network
,
752 (struct prefix
*)&address
);
755 route_unlock_node(node
);
762 /* Add RIP enable network. */
763 int rip_enable_network_add(struct rip
*rip
, struct prefix
*p
)
765 struct route_node
*node
;
767 node
= route_node_get(rip
->enable_network
, p
);
770 route_unlock_node(node
);
771 return NB_ERR_INCONSISTENCY
;
773 node
->info
= (void *)1;
775 /* XXX: One should find a better solution than a generic one */
776 rip_enable_apply_all(rip
);
781 /* Delete RIP enable network. */
782 int rip_enable_network_delete(struct rip
*rip
, struct prefix
*p
)
784 struct route_node
*node
;
786 node
= route_node_lookup(rip
->enable_network
, p
);
790 /* Unlock info lock. */
791 route_unlock_node(node
);
793 /* Unlock lookup lock. */
794 route_unlock_node(node
);
796 /* XXX: One should find a better solution than a generic one */
797 rip_enable_apply_all(rip
);
802 return NB_ERR_INCONSISTENCY
;
805 /* Check interface is enabled by ifname statement. */
806 static int rip_enable_if_lookup(struct rip
*rip
, const char *ifname
)
814 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
815 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
816 if (strcmp(str
, ifname
) == 0)
821 /* Add interface to rip_enable_if. */
822 int rip_enable_if_add(struct rip
*rip
, const char *ifname
)
826 ret
= rip_enable_if_lookup(rip
, ifname
);
828 return NB_ERR_INCONSISTENCY
;
830 vector_set(rip
->enable_interface
,
831 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
833 rip_enable_apply_all(rip
); /* TODOVJ */
838 /* Delete interface from rip_enable_if. */
839 int rip_enable_if_delete(struct rip
*rip
, const char *ifname
)
844 index
= rip_enable_if_lookup(rip
, ifname
);
846 return NB_ERR_INCONSISTENCY
;
848 str
= vector_slot(rip
->enable_interface
, index
);
849 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
850 vector_unset(rip
->enable_interface
, index
);
852 rip_enable_apply_all(rip
); /* TODOVJ */
857 /* Join to multicast group and send request to the interface. */
858 static int rip_interface_wakeup(struct thread
*t
)
860 struct interface
*ifp
;
861 struct rip_interface
*ri
;
869 /* Join to multicast group. */
870 if (rip_multicast_join(ifp
, ri
->rip
->sock
) < 0) {
871 flog_err_sys(EC_LIB_SOCKET
,
872 "multicast join failed, interface %s not running",
877 /* Set running flag. */
880 /* Send RIP request to the interface. */
881 rip_request_interface(ifp
);
886 static void rip_connect_set(struct interface
*ifp
, int set
)
888 struct rip_interface
*ri
= ifp
->info
;
889 struct rip
*rip
= ri
->rip
;
890 struct listnode
*node
, *nnode
;
891 struct connected
*connected
;
892 struct prefix_ipv4 address
;
895 memset(&nh
, 0, sizeof(nh
));
897 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
899 p
= connected
->address
;
901 if (p
->family
!= AF_INET
)
904 address
.family
= AF_INET
;
905 address
.prefix
= p
->u
.prefix4
;
906 address
.prefixlen
= p
->prefixlen
;
907 apply_mask_ipv4(&address
);
909 nh
.ifindex
= connected
->ifp
->ifindex
;
910 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
912 /* Check once more wether this prefix is within a
913 * "network IF_OR_PREF" one */
914 if ((rip_enable_if_lookup(rip
, connected
->ifp
->name
)
916 || (rip_enable_network_lookup2(connected
) >= 0))
917 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
919 &address
, &nh
, 0, 0, 0);
921 rip_redistribute_delete(rip
, ZEBRA_ROUTE_CONNECT
,
922 RIP_ROUTE_INTERFACE
, &address
,
923 connected
->ifp
->ifindex
);
924 if (rip_redistribute_check(rip
, ZEBRA_ROUTE_CONNECT
))
925 rip_redistribute_add(rip
, ZEBRA_ROUTE_CONNECT
,
926 RIP_ROUTE_REDISTRIBUTE
,
927 &address
, &nh
, 0, 0, 0);
932 /* Update interface status. */
933 void rip_enable_apply(struct interface
*ifp
)
936 struct rip_interface
*ri
= NULL
;
938 /* Check interface. */
939 if (!if_is_operative(ifp
))
944 /* Check network configuration. */
945 ret
= rip_enable_network_lookup_if(ifp
);
947 /* If the interface is matched. */
949 ri
->enable_network
= 1;
951 ri
->enable_network
= 0;
953 /* Check interface name configuration. */
954 ret
= rip_enable_if_lookup(ri
->rip
, ifp
->name
);
956 ri
->enable_interface
= 1;
958 ri
->enable_interface
= 0;
960 /* any interface MUST have an IPv4 address */
961 if (!rip_if_ipv4_address_check(ifp
)) {
962 ri
->enable_network
= 0;
963 ri
->enable_interface
= 0;
966 /* Update running status of the interface. */
967 if (ri
->enable_network
|| ri
->enable_interface
) {
968 if (IS_RIP_DEBUG_EVENT
)
969 zlog_debug("turn on %s", ifp
->name
);
971 /* Add interface wake up thread. */
972 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
974 rip_connect_set(ifp
, 1);
975 } else if (ri
->running
) {
976 /* Might as well clean up the route table as well
977 * rip_if_down sets to 0 ri->running, and displays "turn
982 rip_connect_set(ifp
, 0);
986 /* Apply network configuration to all interface. */
987 static void rip_enable_apply_all(struct rip
*rip
)
989 struct interface
*ifp
;
991 /* Check each interface. */
992 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
993 rip_enable_apply(ifp
);
996 int rip_neighbor_lookup(struct rip
*rip
, struct sockaddr_in
*from
)
998 struct prefix_ipv4 p
;
999 struct route_node
*node
;
1001 memset(&p
, 0, sizeof(struct prefix_ipv4
));
1003 p
.prefix
= from
->sin_addr
;
1004 p
.prefixlen
= IPV4_MAX_BITLEN
;
1006 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
1008 route_unlock_node(node
);
1014 /* Add new RIP neighbor to the neighbor tree. */
1015 int rip_neighbor_add(struct rip
*rip
, struct prefix_ipv4
*p
)
1017 struct route_node
*node
;
1019 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
1022 return NB_ERR_INCONSISTENCY
;
1024 node
->info
= rip
->neighbor
;
1029 /* Delete RIP neighbor from the neighbor tree. */
1030 int rip_neighbor_delete(struct rip
*rip
, struct prefix_ipv4
*p
)
1032 struct route_node
*node
;
1034 /* Lock for look up. */
1035 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
1037 return NB_ERR_INCONSISTENCY
;
1041 /* Unlock lookup lock. */
1042 route_unlock_node(node
);
1044 /* Unlock real neighbor information lock. */
1045 route_unlock_node(node
);
1050 /* Clear all network and neighbor configuration. */
1051 void rip_clean_network(struct rip
*rip
)
1055 struct route_node
*rn
;
1057 /* rip->enable_network. */
1058 for (rn
= route_top(rip
->enable_network
); rn
; rn
= route_next(rn
))
1061 route_unlock_node(rn
);
1064 /* rip->enable_interface. */
1065 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1066 if ((str
= vector_slot(rip
->enable_interface
, i
)) != NULL
) {
1067 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1068 vector_slot(rip
->enable_interface
, i
) = NULL
;
1072 /* Utility function for looking up passive interface settings. */
1073 static int rip_passive_nondefault_lookup(struct rip
*rip
, const char *ifname
)
1078 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1079 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
)
1080 if (strcmp(str
, ifname
) == 0)
1085 static void rip_passive_interface_apply(struct interface
*ifp
)
1088 struct rip_interface
*ri
;
1095 ri
->passive
= ((rip_passive_nondefault_lookup(rip
, ifp
->name
) < 0)
1096 ? rip
->passive_default
1097 : !rip
->passive_default
);
1099 if (IS_RIP_DEBUG_ZEBRA
)
1100 zlog_debug("interface %s: passive = %d", ifp
->name
,
1104 static void rip_passive_interface_apply_all(struct rip
*rip
)
1106 struct interface
*ifp
;
1108 FOR_ALL_INTERFACES (rip
->vrf
, ifp
)
1109 rip_passive_interface_apply(ifp
);
1112 /* Passive interface. */
1113 int rip_passive_nondefault_set(struct rip
*rip
, const char *ifname
)
1115 if (rip_passive_nondefault_lookup(rip
, ifname
) >= 0)
1117 * Don't return an error, this can happen after changing
1118 * 'passive-default'.
1122 vector_set(rip
->passive_nondefault
,
1123 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1125 rip_passive_interface_apply_all(rip
);
1130 int rip_passive_nondefault_unset(struct rip
*rip
, const char *ifname
)
1135 i
= rip_passive_nondefault_lookup(rip
, ifname
);
1138 * Don't return an error, this can happen after changing
1139 * 'passive-default'.
1143 str
= vector_slot(rip
->passive_nondefault
, i
);
1144 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1145 vector_unset(rip
->passive_nondefault
, i
);
1147 rip_passive_interface_apply_all(rip
);
1152 /* Free all configured RIP passive-interface settings. */
1153 void rip_passive_nondefault_clean(struct rip
*rip
)
1158 for (i
= 0; i
< vector_active(rip
->passive_nondefault
); i
++)
1159 if ((str
= vector_slot(rip
->passive_nondefault
, i
)) != NULL
) {
1160 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1161 vector_slot(rip
->passive_nondefault
, i
) = NULL
;
1163 rip_passive_interface_apply_all(rip
);
1166 /* Write rip configuration of each interface. */
1167 static int rip_interface_config_write(struct vty
*vty
)
1172 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1173 struct interface
*ifp
;
1175 FOR_ALL_INTERFACES (vrf
, ifp
) {
1176 struct lyd_node
*dnode
;
1178 dnode
= yang_dnode_get(
1179 running_config
->dnode
,
1180 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1181 ifp
->name
, vrf
->name
);
1186 nb_cli_show_dnode_cmds(vty
, dnode
, false);
1193 int rip_show_network_config(struct vty
*vty
, struct rip
*rip
)
1197 struct route_node
*node
;
1199 /* Network type RIP enable interface statement. */
1200 for (node
= route_top(rip
->enable_network
); node
;
1201 node
= route_next(node
))
1203 vty_out(vty
, " %s/%u\n",
1204 inet_ntoa(node
->p
.u
.prefix4
),
1207 /* Interface name RIP enable statement. */
1208 for (i
= 0; i
< vector_active(rip
->enable_interface
); i
++)
1209 if ((ifname
= vector_slot(rip
->enable_interface
, i
)) != NULL
)
1210 vty_out(vty
, " %s\n", ifname
);
1212 /* RIP neighbors listing. */
1213 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1215 vty_out(vty
, " %s\n", inet_ntoa(node
->p
.u
.prefix4
));
1220 static struct cmd_node interface_node
= {
1221 INTERFACE_NODE
, "%s(config-if)# ", 1,
1224 void rip_interface_sync(struct interface
*ifp
)
1228 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
1230 struct rip_interface
*ri
;
1234 ri
->rip
= vrf
->info
;
1238 /* Called when interface structure allocated. */
1239 static int rip_interface_new_hook(struct interface
*ifp
)
1241 ifp
->info
= rip_interface_new();
1242 rip_interface_sync(ifp
);
1247 /* Called when interface structure deleted. */
1248 static int rip_interface_delete_hook(struct interface
*ifp
)
1250 rip_interface_reset(ifp
->info
);
1251 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1256 /* Allocate and initialize interface vector. */
1257 void rip_if_init(void)
1259 /* Default initial size of interface vector. */
1260 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1261 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1263 /* Install interface node. */
1264 install_node(&interface_node
, rip_interface_config_write
);