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(const char *ifname
);
54 static int rip_enable_network_lookup2(struct connected
*connected
);
55 static void rip_enable_apply_all(void);
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 /* RIP enabled network vector. */
64 vector rip_enable_interface
;
66 /* RIP enabled interface table. */
67 struct route_table
*rip_enable_network
;
69 /* Vector to store passive-interface name. */
70 vector Vrip_passive_nondefault
;
72 /* Join to the RIP version 2 multicast group. */
73 static int ipv4_multicast_join(int sock
, struct in_addr group
,
74 struct in_addr ifa
, ifindex_t ifindex
)
78 ret
= setsockopt_ipv4_multicast(sock
, IP_ADD_MEMBERSHIP
, ifa
,
79 group
.s_addr
, ifindex
);
82 zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s",
83 safe_strerror(errno
));
88 /* Leave from the RIP version 2 multicast group. */
89 static int ipv4_multicast_leave(int sock
, struct in_addr group
,
90 struct in_addr ifa
, ifindex_t ifindex
)
94 ret
= setsockopt_ipv4_multicast(sock
, IP_DROP_MEMBERSHIP
, ifa
,
95 group
.s_addr
, ifindex
);
98 zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");
103 static void rip_interface_reset(struct rip_interface
*);
105 /* Allocate new RIP's interface configuration. */
106 static struct rip_interface
*rip_interface_new(void)
108 struct rip_interface
*ri
;
110 ri
= XCALLOC(MTYPE_RIP_INTERFACE
, sizeof(struct rip_interface
));
112 rip_interface_reset(ri
);
117 void rip_interface_multicast_set(int sock
, struct connected
*connected
)
121 assert(connected
!= NULL
);
123 addr
= CONNECTED_ID(connected
)->u
.prefix4
;
125 if (setsockopt_ipv4_multicast_if(sock
, addr
, connected
->ifp
->ifindex
)
128 "Can't setsockopt IP_MULTICAST_IF on fd %d to "
129 "ifindex %d for interface %s",
130 sock
, connected
->ifp
->ifindex
, connected
->ifp
->name
);
136 /* Send RIP request packet to specified interface. */
137 static void rip_request_interface_send(struct interface
*ifp
, uint8_t version
)
139 struct sockaddr_in to
;
141 /* RIPv2 support multicast. */
142 if (version
== RIPv2
&& if_is_multicast(ifp
)) {
144 if (IS_RIP_DEBUG_EVENT
)
145 zlog_debug("multicast request on %s", ifp
->name
);
147 rip_request_send(NULL
, ifp
, version
, NULL
);
151 /* RIPv1 and non multicast interface. */
152 if (if_is_pointopoint(ifp
) || if_is_broadcast(ifp
)) {
153 struct listnode
*cnode
, *cnnode
;
154 struct connected
*connected
;
156 if (IS_RIP_DEBUG_EVENT
)
157 zlog_debug("broadcast request to %s", ifp
->name
);
159 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
,
161 if (connected
->address
->family
!= AF_INET
)
164 memset(&to
, 0, sizeof(struct sockaddr_in
));
165 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
166 if (connected
->destination
)
167 /* use specified broadcast or peer
168 * destination addr */
169 to
.sin_addr
= connected
->destination
->u
.prefix4
;
170 else if (connected
->address
->prefixlen
171 < IPV4_MAX_PREFIXLEN
)
172 /* calculate the appropriate broadcast
174 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
175 connected
->address
->u
.prefix4
.s_addr
,
176 connected
->address
->prefixlen
);
178 /* do not know where to send the packet
182 if (IS_RIP_DEBUG_EVENT
)
183 zlog_debug("SEND request to %s",
184 inet_ntoa(to
.sin_addr
));
186 rip_request_send(&to
, ifp
, version
, connected
);
191 /* This will be executed when interface goes up. */
192 static void rip_request_interface(struct interface
*ifp
)
194 struct rip_interface
*ri
;
197 /* In default ripd doesn't send RIP_REQUEST to the loopback interface.
199 if (if_is_loopback(ifp
))
202 /* If interface is down, don't send RIP packet. */
203 if (!if_is_operative(ifp
))
206 /* Fetch RIP interface information. */
209 /* If there is no version configuration in the interface,
210 use rip's version setting. */
211 vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? rip
->version_send
214 rip_request_interface_send(ifp
, RIPv1
);
216 rip_request_interface_send(ifp
, RIPv2
);
220 /* Send RIP request to the neighbor. */
222 rip_request_neighbor (struct in_addr addr
)
224 struct sockaddr_in to
;
226 memset (&to
, 0, sizeof (struct sockaddr_in
));
227 to
.sin_port
= htons (RIP_PORT_DEFAULT
);
230 rip_request_send (&to
, NULL
, rip
->version_send
, NULL
);
233 /* Request routes at all interfaces. */
235 rip_request_neighbor_all (void)
237 struct route_node
*rp
;
242 if (IS_RIP_DEBUG_EVENT
)
243 zlog_debug ("request to the all neighbor");
245 /* Send request to all neighbor. */
246 for (rp
= route_top (rip
->neighbor
); rp
; rp
= route_next (rp
))
248 rip_request_neighbor (rp
->p
.u
.prefix4
);
252 /* Multicast packet receive socket. */
253 static int rip_multicast_join(struct interface
*ifp
, int sock
)
255 struct listnode
*cnode
;
256 struct connected
*ifc
;
258 if (if_is_operative(ifp
) && if_is_multicast(ifp
)) {
259 if (IS_RIP_DEBUG_EVENT
)
260 zlog_debug("multicast join at %s", ifp
->name
);
262 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
263 struct prefix_ipv4
*p
;
264 struct in_addr group
;
266 p
= (struct prefix_ipv4
*)ifc
->address
;
268 if (p
->family
!= AF_INET
)
271 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
272 if (ipv4_multicast_join(sock
, group
, p
->prefix
,
283 /* Leave from multicast group. */
284 static void rip_multicast_leave(struct interface
*ifp
, int sock
)
286 struct listnode
*cnode
;
287 struct connected
*connected
;
289 if (if_is_up(ifp
) && if_is_multicast(ifp
)) {
290 if (IS_RIP_DEBUG_EVENT
)
291 zlog_debug("multicast leave from %s", ifp
->name
);
293 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
294 struct prefix_ipv4
*p
;
295 struct in_addr group
;
297 p
= (struct prefix_ipv4
*)connected
->address
;
299 if (p
->family
!= AF_INET
)
302 group
.s_addr
= htonl(INADDR_RIP_GROUP
);
303 if (ipv4_multicast_leave(sock
, group
, p
->prefix
,
311 /* Is there and address on interface that I could use ? */
312 static int rip_if_ipv4_address_check(struct interface
*ifp
)
315 struct connected
*connected
;
318 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, nn
, connected
)) {
321 p
= connected
->address
;
323 if (p
->family
== AF_INET
)
331 /* Does this address belongs to me ? */
332 int if_check_address(struct in_addr addr
)
334 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
335 struct interface
*ifp
;
337 FOR_ALL_INTERFACES (vrf
, ifp
) {
338 struct listnode
*cnode
;
339 struct connected
*connected
;
341 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
342 struct prefix_ipv4
*p
;
344 p
= (struct prefix_ipv4
*)connected
->address
;
346 if (p
->family
!= AF_INET
)
349 if (IPV4_ADDR_CMP(&p
->prefix
, &addr
) == 0)
356 /* Inteface link down message processing. */
357 int rip_interface_down(int command
, struct zclient
*zclient
,
358 zebra_size_t length
, vrf_id_t vrf_id
)
360 struct interface
*ifp
;
365 /* zebra_interface_state_read() updates interface structure in
367 ifp
= zebra_interface_state_read(s
, vrf_id
);
374 if (IS_RIP_DEBUG_ZEBRA
)
376 "interface %s index %d flags %llx metric %d mtu %d is down",
377 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
378 ifp
->metric
, ifp
->mtu
);
383 /* Inteface link up message processing */
384 int rip_interface_up(int command
, struct zclient
*zclient
, zebra_size_t length
,
387 struct interface
*ifp
;
389 /* zebra_interface_state_read () updates interface structure in
391 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
396 if (IS_RIP_DEBUG_ZEBRA
)
398 "interface %s index %d flags %#llx metric %d mtu %d is up",
399 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
400 ifp
->metric
, ifp
->mtu
);
402 /* Check if this interface is RIP enabled or not.*/
403 rip_enable_apply(ifp
);
405 /* Check for a passive interface */
406 rip_passive_interface_apply(ifp
);
408 /* Apply distribute list to the all interface. */
409 rip_distribute_update_interface(ifp
);
414 /* Inteface addition message from zebra. */
415 int rip_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
418 struct interface
*ifp
;
420 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
422 if (IS_RIP_DEBUG_ZEBRA
)
424 "interface add %s index %d flags %#llx metric %d mtu %d",
425 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
426 ifp
->metric
, ifp
->mtu
);
428 /* Check if this interface is RIP enabled or not.*/
429 rip_enable_apply(ifp
);
431 /* Check for a passive interface */
432 rip_passive_interface_apply(ifp
);
434 /* Apply distribute list to the all interface. */
435 rip_distribute_update_interface(ifp
);
437 /* rip_request_neighbor_all (); */
439 /* Check interface routemap. */
440 rip_if_rmap_update_interface(ifp
);
445 int rip_interface_delete(int command
, struct zclient
*zclient
,
446 zebra_size_t length
, vrf_id_t vrf_id
)
448 struct interface
*ifp
;
453 /* zebra_interface_state_read() updates interface structure in iflist */
454 ifp
= zebra_interface_state_read(s
, vrf_id
);
463 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
464 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
465 ifp
->metric
, ifp
->mtu
);
467 /* To support pseudo interface do not free interface structure. */
468 /* if_delete(ifp); */
469 if_set_index(ifp
, IFINDEX_INTERNAL
);
474 static void rip_interface_clean(struct rip_interface
*ri
)
476 ri
->enable_network
= 0;
477 ri
->enable_interface
= 0;
481 thread_cancel(ri
->t_wakeup
);
486 void rip_interfaces_clean(void)
488 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
489 struct interface
*ifp
;
491 FOR_ALL_INTERFACES (vrf
, ifp
)
492 rip_interface_clean(ifp
->info
);
495 static void rip_interface_reset(struct rip_interface
*ri
)
497 ri
->auth_type
= yang_get_default_enum("%s/authentication-scheme/mode",
499 ri
->md5_auth_len
= yang_get_default_enum(
500 "%s/authentication-scheme/md5-auth-length", RIP_IFACE
);
502 /* Set default split-horizon behavior. If the interface is Frame
503 Relay or SMDS is enabled, the default value for split-horizon is
504 off. But currently Zebra does detect Frame Relay or SMDS
505 interface. So all interface is set to split horizon. */
507 yang_get_default_enum("%s/split-horizon", RIP_IFACE
);
509 ri
->ri_send
= yang_get_default_enum("%s/version-send", RIP_IFACE
);
510 ri
->ri_receive
= yang_get_default_enum("%s/version-receive", RIP_IFACE
);
511 ri
->v2_broadcast
= yang_get_default_bool("%s/v2-broadcast", RIP_IFACE
);
514 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->auth_str
);
517 XFREE(MTYPE_RIP_INTERFACE_STRING
, ri
->key_chain
);
519 ri
->list
[RIP_FILTER_IN
] = NULL
;
520 ri
->list
[RIP_FILTER_OUT
] = NULL
;
522 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
523 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
525 ri
->recv_badpackets
= 0;
526 ri
->recv_badroutes
= 0;
527 ri
->sent_updates
= 0;
531 rip_interface_clean(ri
);
534 int rip_if_down(struct interface
*ifp
)
536 struct route_node
*rp
;
537 struct rip_info
*rinfo
;
538 struct rip_interface
*ri
= NULL
;
539 struct list
*list
= NULL
;
540 struct listnode
*listnode
= NULL
, *nextnode
= NULL
;
542 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
543 if ((list
= rp
->info
) != NULL
)
544 for (ALL_LIST_ELEMENTS(list
, listnode
, nextnode
,
546 if (rinfo
->nh
.ifindex
== ifp
->ifindex
)
547 rip_ecmp_delete(rinfo
);
552 if (IS_RIP_DEBUG_EVENT
)
553 zlog_debug("turn off %s", ifp
->name
);
555 /* Leave from multicast group. */
556 rip_multicast_leave(ifp
, rip
->sock
);
565 /* Needed for stop RIP process. */
566 void rip_if_down_all(void)
568 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
569 struct interface
*ifp
;
571 FOR_ALL_INTERFACES (vrf
, ifp
)
575 static void rip_apply_address_add(struct connected
*ifc
)
577 struct prefix_ipv4 address
;
584 if (!if_is_up(ifc
->ifp
))
589 memset(&address
, 0, sizeof(address
));
590 memset(&nh
, 0, sizeof(nh
));
592 address
.family
= p
->family
;
593 address
.prefix
= p
->u
.prefix4
;
594 address
.prefixlen
= p
->prefixlen
;
595 apply_mask_ipv4(&address
);
597 nh
.ifindex
= ifc
->ifp
->ifindex
;
598 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
600 /* Check if this interface is RIP enabled or not
601 or Check if this address's prefix is RIP enabled */
602 if ((rip_enable_if_lookup(ifc
->ifp
->name
) >= 0)
603 || (rip_enable_network_lookup2(ifc
) >= 0))
604 rip_redistribute_add(ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
605 &address
, &nh
, 0, 0, 0);
608 int rip_interface_address_add(int command
, struct zclient
*zclient
,
609 zebra_size_t length
, vrf_id_t vrf_id
)
611 struct connected
*ifc
;
614 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
615 zclient
->ibuf
, vrf_id
);
622 if (p
->family
== AF_INET
) {
623 if (IS_RIP_DEBUG_ZEBRA
)
624 zlog_debug("connected address %s/%d is added",
625 inet_ntoa(p
->u
.prefix4
), p
->prefixlen
);
627 rip_enable_apply(ifc
->ifp
);
628 /* Check if this prefix needs to be redistributed */
629 rip_apply_address_add(ifc
);
631 hook_call(rip_ifaddr_add
, ifc
);
637 static void rip_apply_address_del(struct connected
*ifc
)
639 struct prefix_ipv4 address
;
645 if (!if_is_up(ifc
->ifp
))
650 memset(&address
, 0, sizeof(address
));
651 address
.family
= p
->family
;
652 address
.prefix
= p
->u
.prefix4
;
653 address
.prefixlen
= p
->prefixlen
;
654 apply_mask_ipv4(&address
);
656 rip_redistribute_delete(ZEBRA_ROUTE_CONNECT
, RIP_ROUTE_INTERFACE
,
657 &address
, ifc
->ifp
->ifindex
);
660 int rip_interface_address_delete(int command
, struct zclient
*zclient
,
661 zebra_size_t length
, vrf_id_t vrf_id
)
663 struct connected
*ifc
;
666 ifc
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
667 zclient
->ibuf
, vrf_id
);
671 if (p
->family
== AF_INET
) {
672 if (IS_RIP_DEBUG_ZEBRA
)
673 zlog_debug("connected address %s/%d is deleted",
674 inet_ntoa(p
->u
.prefix4
),
677 hook_call(rip_ifaddr_del
, ifc
);
679 /* Chech wether this prefix needs to be removed */
680 rip_apply_address_del(ifc
);
689 /* Check interface is enabled by network statement. */
690 /* Check wether the interface has at least a connected prefix that
691 * is within the ripng_enable_network table. */
692 static int rip_enable_network_lookup_if(struct interface
*ifp
)
694 struct listnode
*node
, *nnode
;
695 struct connected
*connected
;
696 struct prefix_ipv4 address
;
698 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
700 struct route_node
*n
;
702 p
= connected
->address
;
704 if (p
->family
== AF_INET
) {
705 address
.family
= AF_INET
;
706 address
.prefix
= p
->u
.prefix4
;
707 address
.prefixlen
= IPV4_MAX_BITLEN
;
709 n
= route_node_match(rip_enable_network
,
710 (struct prefix
*)&address
);
712 route_unlock_node(n
);
720 /* Check wether connected is within the ripng_enable_network table. */
721 int rip_enable_network_lookup2(struct connected
*connected
)
723 struct prefix_ipv4 address
;
726 p
= connected
->address
;
728 if (p
->family
== AF_INET
) {
729 struct route_node
*node
;
731 address
.family
= p
->family
;
732 address
.prefix
= p
->u
.prefix4
;
733 address
.prefixlen
= IPV4_MAX_BITLEN
;
735 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
736 * rip_enable_network */
737 node
= route_node_match(rip_enable_network
,
738 (struct prefix
*)&address
);
741 route_unlock_node(node
);
748 /* Add RIP enable network. */
749 int rip_enable_network_add(struct prefix
*p
)
751 struct route_node
*node
;
753 node
= route_node_get(rip_enable_network
, p
);
756 route_unlock_node(node
);
757 return NB_ERR_INCONSISTENCY
;
759 node
->info
= (void *)1;
761 /* XXX: One should find a better solution than a generic one */
762 rip_enable_apply_all();
767 /* Delete RIP enable network. */
768 int rip_enable_network_delete(struct prefix
*p
)
770 struct route_node
*node
;
772 node
= route_node_lookup(rip_enable_network
, p
);
776 /* Unlock info lock. */
777 route_unlock_node(node
);
779 /* Unlock lookup lock. */
780 route_unlock_node(node
);
782 /* XXX: One should find a better solution than a generic one */
783 rip_enable_apply_all();
788 return NB_ERR_INCONSISTENCY
;
791 /* Check interface is enabled by ifname statement. */
792 static int rip_enable_if_lookup(const char *ifname
)
797 for (i
= 0; i
< vector_active(rip_enable_interface
); i
++)
798 if ((str
= vector_slot(rip_enable_interface
, i
)) != NULL
)
799 if (strcmp(str
, ifname
) == 0)
804 /* Add interface to rip_enable_if. */
805 int rip_enable_if_add(const char *ifname
)
809 ret
= rip_enable_if_lookup(ifname
);
811 return NB_ERR_INCONSISTENCY
;
813 vector_set(rip_enable_interface
,
814 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
816 rip_enable_apply_all(); /* TODOVJ */
821 /* Delete interface from rip_enable_if. */
822 int rip_enable_if_delete(const char *ifname
)
827 index
= rip_enable_if_lookup(ifname
);
829 return NB_ERR_INCONSISTENCY
;
831 str
= vector_slot(rip_enable_interface
, index
);
832 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
833 vector_unset(rip_enable_interface
, index
);
835 rip_enable_apply_all(); /* TODOVJ */
840 /* Join to multicast group and send request to the interface. */
841 static int rip_interface_wakeup(struct thread
*t
)
843 struct interface
*ifp
;
844 struct rip_interface
*ri
;
852 /* Join to multicast group. */
853 if (rip_multicast_join(ifp
, rip
->sock
) < 0) {
854 flog_err_sys(EC_LIB_SOCKET
,
855 "multicast join failed, interface %s not running",
860 /* Set running flag. */
863 /* Send RIP request to the interface. */
864 rip_request_interface(ifp
);
869 static void rip_connect_set(struct interface
*ifp
, int set
)
871 struct listnode
*node
, *nnode
;
872 struct connected
*connected
;
873 struct prefix_ipv4 address
;
876 memset(&nh
, 0, sizeof(nh
));
878 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
880 p
= connected
->address
;
882 if (p
->family
!= AF_INET
)
885 address
.family
= AF_INET
;
886 address
.prefix
= p
->u
.prefix4
;
887 address
.prefixlen
= p
->prefixlen
;
888 apply_mask_ipv4(&address
);
890 nh
.ifindex
= connected
->ifp
->ifindex
;
891 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
893 /* Check once more wether this prefix is within a
894 * "network IF_OR_PREF" one */
895 if ((rip_enable_if_lookup(connected
->ifp
->name
) >= 0)
896 || (rip_enable_network_lookup2(connected
) >= 0))
897 rip_redistribute_add(ZEBRA_ROUTE_CONNECT
,
899 &address
, &nh
, 0, 0, 0);
901 rip_redistribute_delete(ZEBRA_ROUTE_CONNECT
,
902 RIP_ROUTE_INTERFACE
, &address
,
903 connected
->ifp
->ifindex
);
904 if (rip_redistribute_check(ZEBRA_ROUTE_CONNECT
))
905 rip_redistribute_add(ZEBRA_ROUTE_CONNECT
,
906 RIP_ROUTE_REDISTRIBUTE
,
907 &address
, &nh
, 0, 0, 0);
912 /* Update interface status. */
913 void rip_enable_apply(struct interface
*ifp
)
916 struct rip_interface
*ri
= NULL
;
918 /* Check interface. */
919 if (!if_is_operative(ifp
))
924 /* Check network configuration. */
925 ret
= rip_enable_network_lookup_if(ifp
);
927 /* If the interface is matched. */
929 ri
->enable_network
= 1;
931 ri
->enable_network
= 0;
933 /* Check interface name configuration. */
934 ret
= rip_enable_if_lookup(ifp
->name
);
936 ri
->enable_interface
= 1;
938 ri
->enable_interface
= 0;
940 /* any interface MUST have an IPv4 address */
941 if (!rip_if_ipv4_address_check(ifp
)) {
942 ri
->enable_network
= 0;
943 ri
->enable_interface
= 0;
946 /* Update running status of the interface. */
947 if (ri
->enable_network
|| ri
->enable_interface
) {
948 if (IS_RIP_DEBUG_EVENT
)
949 zlog_debug("turn on %s", ifp
->name
);
951 /* Add interface wake up thread. */
952 thread_add_timer(master
, rip_interface_wakeup
, ifp
, 1,
954 rip_connect_set(ifp
, 1);
955 } else if (ri
->running
) {
956 /* Might as well clean up the route table as well
957 * rip_if_down sets to 0 ri->running, and displays "turn
962 rip_connect_set(ifp
, 0);
966 /* Apply network configuration to all interface. */
967 void rip_enable_apply_all(void)
969 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
970 struct interface
*ifp
;
972 /* Check each interface. */
973 FOR_ALL_INTERFACES (vrf
, ifp
)
974 rip_enable_apply(ifp
);
977 int rip_neighbor_lookup(struct sockaddr_in
*from
)
979 struct prefix_ipv4 p
;
980 struct route_node
*node
;
982 memset(&p
, 0, sizeof(struct prefix_ipv4
));
984 p
.prefix
= from
->sin_addr
;
985 p
.prefixlen
= IPV4_MAX_BITLEN
;
987 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)&p
);
989 route_unlock_node(node
);
995 /* Add new RIP neighbor to the neighbor tree. */
996 int rip_neighbor_add(struct prefix_ipv4
*p
)
998 struct route_node
*node
;
1000 node
= route_node_get(rip
->neighbor
, (struct prefix
*)p
);
1003 return NB_ERR_INCONSISTENCY
;
1005 node
->info
= rip
->neighbor
;
1010 /* Delete RIP neighbor from the neighbor tree. */
1011 int rip_neighbor_delete(struct prefix_ipv4
*p
)
1013 struct route_node
*node
;
1015 /* Lock for look up. */
1016 node
= route_node_lookup(rip
->neighbor
, (struct prefix
*)p
);
1018 return NB_ERR_INCONSISTENCY
;
1022 /* Unlock lookup lock. */
1023 route_unlock_node(node
);
1025 /* Unlock real neighbor information lock. */
1026 route_unlock_node(node
);
1031 /* Clear all network and neighbor configuration. */
1032 void rip_clean_network(void)
1036 struct route_node
*rn
;
1038 /* rip_enable_network. */
1039 for (rn
= route_top(rip_enable_network
); rn
; rn
= route_next(rn
))
1042 route_unlock_node(rn
);
1045 /* rip_enable_interface. */
1046 for (i
= 0; i
< vector_active(rip_enable_interface
); i
++)
1047 if ((str
= vector_slot(rip_enable_interface
, i
)) != NULL
) {
1048 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1049 vector_slot(rip_enable_interface
, i
) = NULL
;
1053 /* Utility function for looking up passive interface settings. */
1054 static int rip_passive_nondefault_lookup(const char *ifname
)
1059 for (i
= 0; i
< vector_active(Vrip_passive_nondefault
); i
++)
1060 if ((str
= vector_slot(Vrip_passive_nondefault
, i
)) != NULL
)
1061 if (strcmp(str
, ifname
) == 0)
1066 void rip_passive_interface_apply(struct interface
*ifp
)
1068 struct rip_interface
*ri
;
1075 ri
->passive
= ((rip_passive_nondefault_lookup(ifp
->name
) < 0)
1076 ? rip
->passive_default
1077 : !rip
->passive_default
);
1079 if (IS_RIP_DEBUG_ZEBRA
)
1080 zlog_debug("interface %s: passive = %d", ifp
->name
,
1084 static void rip_passive_interface_apply_all(void)
1086 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1087 struct interface
*ifp
;
1089 FOR_ALL_INTERFACES (vrf
, ifp
)
1090 rip_passive_interface_apply(ifp
);
1093 /* Passive interface. */
1094 int rip_passive_nondefault_set(const char *ifname
)
1096 if (rip_passive_nondefault_lookup(ifname
) >= 0)
1098 * Don't return an error, this can happen after changing
1099 * 'passive-default'.
1103 vector_set(Vrip_passive_nondefault
,
1104 XSTRDUP(MTYPE_RIP_INTERFACE_STRING
, ifname
));
1106 rip_passive_interface_apply_all();
1111 int rip_passive_nondefault_unset(const char *ifname
)
1116 i
= rip_passive_nondefault_lookup(ifname
);
1119 * Don't return an error, this can happen after changing
1120 * 'passive-default'.
1124 str
= vector_slot(Vrip_passive_nondefault
, i
);
1125 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1126 vector_unset(Vrip_passive_nondefault
, i
);
1128 rip_passive_interface_apply_all();
1133 /* Free all configured RIP passive-interface settings. */
1134 void rip_passive_nondefault_clean(void)
1139 for (i
= 0; i
< vector_active(Vrip_passive_nondefault
); i
++)
1140 if ((str
= vector_slot(Vrip_passive_nondefault
, i
)) != NULL
) {
1141 XFREE(MTYPE_RIP_INTERFACE_STRING
, str
);
1142 vector_slot(Vrip_passive_nondefault
, i
) = NULL
;
1144 rip_passive_interface_apply_all();
1147 /* Write rip configuration of each interface. */
1148 static int rip_interface_config_write(struct vty
*vty
)
1150 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1151 struct interface
*ifp
;
1154 FOR_ALL_INTERFACES (vrf
, ifp
) {
1155 struct lyd_node
*dnode
;
1157 dnode
= yang_dnode_get(
1158 running_config
->dnode
,
1159 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1160 ifp
->name
, vrf
->name
);
1165 nb_cli_show_dnode_cmds(vty
, dnode
, false);
1171 int rip_show_network_config(struct vty
*vty
)
1175 struct route_node
*node
;
1177 /* Network type RIP enable interface statement. */
1178 for (node
= route_top(rip_enable_network
); node
;
1179 node
= route_next(node
))
1181 vty_out(vty
, " %s/%u\n",
1182 inet_ntoa(node
->p
.u
.prefix4
),
1185 /* Interface name RIP enable statement. */
1186 for (i
= 0; i
< vector_active(rip_enable_interface
); i
++)
1187 if ((ifname
= vector_slot(rip_enable_interface
, i
)) != NULL
)
1188 vty_out(vty
, " %s\n", ifname
);
1190 /* RIP neighbors listing. */
1191 for (node
= route_top(rip
->neighbor
); node
; node
= route_next(node
))
1193 vty_out(vty
, " %s\n", inet_ntoa(node
->p
.u
.prefix4
));
1198 static struct cmd_node interface_node
= {
1199 INTERFACE_NODE
, "%s(config-if)# ", 1,
1202 /* Called when interface structure allocated. */
1203 static int rip_interface_new_hook(struct interface
*ifp
)
1205 ifp
->info
= rip_interface_new();
1209 /* Called when interface structure deleted. */
1210 static int rip_interface_delete_hook(struct interface
*ifp
)
1212 rip_interface_reset(ifp
->info
);
1213 XFREE(MTYPE_RIP_INTERFACE
, ifp
->info
);
1218 /* Allocate and initialize interface vector. */
1219 void rip_if_init(void)
1221 /* Default initial size of interface vector. */
1222 hook_register_prio(if_add
, 0, rip_interface_new_hook
);
1223 hook_register_prio(if_del
, 0, rip_interface_delete_hook
);
1225 /* RIP network init. */
1226 rip_enable_interface
= vector_init(1);
1227 rip_enable_network
= route_table_init();
1229 /* RIP passive interface. */
1230 Vrip_passive_nondefault
= vector_init(1);
1232 /* Install interface node. */
1233 install_node(&interface_node
, rip_interface_config_write
);