[cleanup] Make command nodes static
[jleu-quagga.git] / ripd / rip_interface.c
blob827663ac7bebd8932675d1ec7e6d5d1e82838b2f
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
9 * later version.
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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include <zebra.h>
24 #include "command.h"
25 #include "if.h"
26 #include "sockunion.h"
27 #include "prefix.h"
28 #include "memory.h"
29 #include "network.h"
30 #include "table.h"
31 #include "log.h"
32 #include "stream.h"
33 #include "thread.h"
34 #include "zclient.h"
35 #include "filter.h"
36 #include "sockopt.h"
37 #include "privs.h"
39 #include "zebra/connected.h"
41 #include "ripd/ripd.h"
42 #include "ripd/rip_debug.h"
43 #include "ripd/rip_interface.h"
45 /* static prototypes */
46 static void rip_enable_apply (struct interface *);
47 static void rip_passive_interface_apply (struct interface *);
48 static int rip_if_down(struct interface *ifp);
49 static int rip_enable_if_lookup (const char *ifname);
50 static int rip_enable_network_lookup2 (struct connected *connected);
51 static void rip_enable_apply_all (void);
53 struct message ri_version_msg[] =
55 {RI_RIP_VERSION_1, "1"},
56 {RI_RIP_VERSION_2, "2"},
57 {RI_RIP_VERSION_1_AND_2, "1 2"},
60 extern struct zebra_privs_t ripd_privs;
62 /* RIP enabled network vector. */
63 vector rip_enable_interface;
65 /* RIP enabled interface table. */
66 struct route_table *rip_enable_network;
68 /* Vector to store passive-interface name. */
69 static int passive_default; /* are we in passive-interface default mode? */
70 vector Vrip_passive_nondefault;
72 /* Join to the RIP version 2 multicast group. */
73 static int
74 ipv4_multicast_join (int sock,
75 struct in_addr group,
76 struct in_addr ifa,
77 unsigned int ifindex)
79 int ret;
81 ret = setsockopt_multicast_ipv4 (sock,
82 IP_ADD_MEMBERSHIP,
83 ifa,
84 group.s_addr,
85 ifindex);
87 if (ret < 0)
88 zlog (NULL, LOG_INFO, "can't setsockopt IP_ADD_MEMBERSHIP %s",
89 safe_strerror (errno));
91 return ret;
94 /* Leave from the RIP version 2 multicast group. */
95 static int
96 ipv4_multicast_leave (int sock,
97 struct in_addr group,
98 struct in_addr ifa,
99 unsigned int ifindex)
101 int ret;
103 ret = setsockopt_multicast_ipv4 (sock,
104 IP_DROP_MEMBERSHIP,
105 ifa,
106 group.s_addr,
107 ifindex);
109 if (ret < 0)
110 zlog (NULL, LOG_INFO, "can't setsockopt IP_DROP_MEMBERSHIP");
112 return ret;
115 /* Allocate new RIP's interface configuration. */
116 static struct rip_interface *
117 rip_interface_new (void)
119 struct rip_interface *ri;
121 ri = XCALLOC (MTYPE_RIP_INTERFACE, sizeof (struct rip_interface));
123 /* Default authentication type is simple password for Cisco
124 compatibility. */
125 ri->auth_type = RIP_NO_AUTH;
126 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
128 /* Set default split-horizon behavior. If the interface is Frame
129 Relay or SMDS is enabled, the default value for split-horizon is
130 off. But currently Zebra does detect Frame Relay or SMDS
131 interface. So all interface is set to split horizon. */
132 ri->split_horizon_default = RIP_SPLIT_HORIZON;
133 ri->split_horizon = ri->split_horizon_default;
135 return ri;
138 void
139 rip_interface_multicast_set (int sock, struct connected *connected)
141 struct in_addr addr;
143 assert (connected != NULL);
145 addr = CONNECTED_ID(connected)->u.prefix4;
147 if (setsockopt_multicast_ipv4 (sock, IP_MULTICAST_IF, addr, 0,
148 connected->ifp->ifindex) < 0)
150 zlog_warn ("Can't setsockopt IP_MULTICAST_IF on fd %d to "
151 "source address %s for interface %s",
152 sock, inet_ntoa(addr),
153 connected->ifp->name);
156 return;
159 /* Send RIP request packet to specified interface. */
160 static void
161 rip_request_interface_send (struct interface *ifp, u_char version)
163 struct sockaddr_in to;
165 /* RIPv2 support multicast. */
166 if (version == RIPv2 && if_is_multicast (ifp))
169 if (IS_RIP_DEBUG_EVENT)
170 zlog_debug ("multicast request on %s", ifp->name);
172 rip_request_send (NULL, ifp, version, NULL);
173 return;
176 /* RIPv1 and non multicast interface. */
177 if (if_is_pointopoint (ifp) || if_is_broadcast (ifp))
179 struct listnode *cnode, *cnnode;
180 struct connected *connected;
182 if (IS_RIP_DEBUG_EVENT)
183 zlog_debug ("broadcast request to %s", ifp->name);
185 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, connected))
187 if (connected->address->family == AF_INET)
189 memset (&to, 0, sizeof (struct sockaddr_in));
190 to.sin_port = htons (RIP_PORT_DEFAULT);
191 if (connected->destination)
192 /* use specified broadcast or peer destination addr */
193 to.sin_addr = connected->destination->u.prefix4;
194 else if (connected->address->prefixlen < IPV4_MAX_PREFIXLEN)
195 /* calculate the appropriate broadcast address */
196 to.sin_addr.s_addr =
197 ipv4_broadcast_addr(connected->address->u.prefix4.s_addr,
198 connected->address->prefixlen);
199 else
200 /* do not know where to send the packet */
201 continue;
203 if (IS_RIP_DEBUG_EVENT)
204 zlog_debug ("SEND request to %s", inet_ntoa (to.sin_addr));
206 rip_request_send (&to, ifp, version, connected);
212 /* This will be executed when interface goes up. */
213 static void
214 rip_request_interface (struct interface *ifp)
216 struct rip_interface *ri;
218 /* In default ripd doesn't send RIP_REQUEST to the loopback interface. */
219 if (if_is_loopback (ifp))
220 return;
222 /* If interface is down, don't send RIP packet. */
223 if (! if_is_operative (ifp))
224 return;
226 /* Fetch RIP interface information. */
227 ri = ifp->info;
230 /* If there is no version configuration in the interface,
231 use rip's version setting. */
233 int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ?
234 rip->version_send : ri->ri_send);
235 if (vsend & RIPv1)
236 rip_request_interface_send (ifp, RIPv1);
237 if (vsend & RIPv2)
238 rip_request_interface_send (ifp, RIPv2);
242 /* Send RIP request to the neighbor. */
243 static void
244 rip_request_neighbor (struct in_addr addr)
246 struct sockaddr_in to;
248 memset (&to, 0, sizeof (struct sockaddr_in));
249 to.sin_port = htons (RIP_PORT_DEFAULT);
250 to.sin_addr = addr;
252 rip_request_send (&to, NULL, rip->version_send, NULL);
255 /* Request routes at all interfaces. */
256 static void
257 rip_request_neighbor_all (void)
259 struct route_node *rp;
261 if (! rip)
262 return;
264 if (IS_RIP_DEBUG_EVENT)
265 zlog_debug ("request to the all neighbor");
267 /* Send request to all neighbor. */
268 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
269 if (rp->info)
270 rip_request_neighbor (rp->p.u.prefix4);
273 /* Multicast packet receive socket. */
274 static int
275 rip_multicast_join (struct interface *ifp, int sock)
277 struct listnode *cnode;
278 struct connected *ifc;
280 if (if_is_operative (ifp) && if_is_multicast (ifp))
282 if (IS_RIP_DEBUG_EVENT)
283 zlog_debug ("multicast join at %s", ifp->name);
285 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, ifc))
287 struct prefix_ipv4 *p;
288 struct in_addr group;
290 p = (struct prefix_ipv4 *) ifc->address;
292 if (p->family != AF_INET)
293 continue;
295 group.s_addr = htonl (INADDR_RIP_GROUP);
296 if (ipv4_multicast_join (sock, group, p->prefix, ifp->ifindex) < 0)
297 return -1;
298 else
299 return 0;
302 return 0;
305 /* Leave from multicast group. */
306 static void
307 rip_multicast_leave (struct interface *ifp, int sock)
309 struct listnode *cnode;
310 struct connected *connected;
312 if (if_is_up (ifp) && if_is_multicast (ifp))
314 if (IS_RIP_DEBUG_EVENT)
315 zlog_debug ("multicast leave from %s", ifp->name);
317 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
319 struct prefix_ipv4 *p;
320 struct in_addr group;
322 p = (struct prefix_ipv4 *) connected->address;
324 if (p->family != AF_INET)
325 continue;
327 group.s_addr = htonl (INADDR_RIP_GROUP);
328 if (ipv4_multicast_leave (sock, group, p->prefix, ifp->ifindex) == 0)
329 return;
334 /* Is there and address on interface that I could use ? */
335 static int
336 rip_if_ipv4_address_check (struct interface *ifp)
338 struct listnode *nn;
339 struct connected *connected;
340 int count = 0;
342 for (ALL_LIST_ELEMENTS_RO (ifp->connected, nn, connected))
344 struct prefix *p;
346 p = connected->address;
348 if (p->family == AF_INET)
349 count++;
352 return count;
358 /* Does this address belongs to me ? */
360 if_check_address (struct in_addr addr)
362 struct listnode *node;
363 struct interface *ifp;
365 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
367 struct listnode *cnode;
368 struct connected *connected;
370 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
372 struct prefix_ipv4 *p;
374 p = (struct prefix_ipv4 *) connected->address;
376 if (p->family != AF_INET)
377 continue;
379 if (IPV4_ADDR_CMP (&p->prefix, &addr) == 0)
380 return 1;
383 return 0;
386 /* Inteface link down message processing. */
388 rip_interface_down (int command, struct zclient *zclient, zebra_size_t length)
390 struct interface *ifp;
391 struct stream *s;
393 s = zclient->ibuf;
395 /* zebra_interface_state_read() updates interface structure in
396 iflist. */
397 ifp = zebra_interface_state_read(s);
399 if (ifp == NULL)
400 return 0;
402 rip_if_down(ifp);
404 if (IS_RIP_DEBUG_ZEBRA)
405 zlog_debug ("interface %s index %d flags %ld metric %d mtu %d is down",
406 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
408 return 0;
411 /* Inteface link up message processing */
413 rip_interface_up (int command, struct zclient *zclient, zebra_size_t length)
415 struct interface *ifp;
417 /* zebra_interface_state_read () updates interface structure in
418 iflist. */
419 ifp = zebra_interface_state_read (zclient->ibuf);
421 if (ifp == NULL)
422 return 0;
424 if (IS_RIP_DEBUG_ZEBRA)
425 zlog_debug ("interface %s index %d flags %ld metric %d mtu %d is up",
426 ifp->name, ifp->ifindex, ifp->flags, 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 return 0;
440 /* Inteface addition message from zebra. */
442 rip_interface_add (int command, struct zclient *zclient, zebra_size_t length)
444 struct interface *ifp;
446 ifp = zebra_interface_add_read (zclient->ibuf);
448 if (IS_RIP_DEBUG_ZEBRA)
449 zlog_debug ("interface add %s index %d flags %ld metric %d mtu %d",
450 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
452 /* Check if this interface is RIP enabled or not.*/
453 rip_enable_apply (ifp);
455 /* Check for a passive interface */
456 rip_passive_interface_apply (ifp);
458 /* Apply distribute list to the all interface. */
459 rip_distribute_update_interface (ifp);
461 /* rip_request_neighbor_all (); */
463 /* Check interface routemap. */
464 rip_if_rmap_update_interface (ifp);
466 return 0;
470 rip_interface_delete (int command, struct zclient *zclient,
471 zebra_size_t length)
473 struct interface *ifp;
474 struct stream *s;
477 s = zclient->ibuf;
478 /* zebra_interface_state_read() updates interface structure in iflist */
479 ifp = zebra_interface_state_read(s);
481 if (ifp == NULL)
482 return 0;
484 if (if_is_up (ifp)) {
485 rip_if_down(ifp);
488 zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
489 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
491 /* To support pseudo interface do not free interface structure. */
492 /* if_delete(ifp); */
493 ifp->ifindex = IFINDEX_INTERNAL;
495 return 0;
498 void
499 rip_interface_clean (void)
501 struct listnode *node;
502 struct interface *ifp;
503 struct rip_interface *ri;
505 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
507 ri = ifp->info;
509 ri->enable_network = 0;
510 ri->enable_interface = 0;
511 ri->running = 0;
513 if (ri->t_wakeup)
515 thread_cancel (ri->t_wakeup);
516 ri->t_wakeup = NULL;
521 void
522 rip_interface_reset (void)
524 struct listnode *node;
525 struct interface *ifp;
526 struct rip_interface *ri;
528 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
530 ri = ifp->info;
532 ri->enable_network = 0;
533 ri->enable_interface = 0;
534 ri->running = 0;
536 ri->ri_send = RI_RIP_UNSPEC;
537 ri->ri_receive = RI_RIP_UNSPEC;
539 ri->auth_type = RIP_NO_AUTH;
541 if (ri->auth_str)
543 free (ri->auth_str);
544 ri->auth_str = NULL;
546 if (ri->key_chain)
548 free (ri->key_chain);
549 ri->key_chain = NULL;
552 ri->split_horizon = RIP_NO_SPLIT_HORIZON;
553 ri->split_horizon_default = RIP_NO_SPLIT_HORIZON;
555 ri->list[RIP_FILTER_IN] = NULL;
556 ri->list[RIP_FILTER_OUT] = NULL;
558 ri->prefix[RIP_FILTER_IN] = NULL;
559 ri->prefix[RIP_FILTER_OUT] = NULL;
561 if (ri->t_wakeup)
563 thread_cancel (ri->t_wakeup);
564 ri->t_wakeup = NULL;
567 ri->recv_badpackets = 0;
568 ri->recv_badroutes = 0;
569 ri->sent_updates = 0;
571 ri->passive = 0;
576 rip_if_down(struct interface *ifp)
578 struct route_node *rp;
579 struct rip_info *rinfo;
580 struct rip_interface *ri = NULL;
581 if (rip)
583 for (rp = route_top (rip->table); rp; rp = route_next (rp))
584 if ((rinfo = rp->info) != NULL)
586 /* Routes got through this interface. */
587 if (rinfo->ifindex == ifp->ifindex &&
588 rinfo->type == ZEBRA_ROUTE_RIP &&
589 rinfo->sub_type == RIP_ROUTE_RTE)
591 rip_zebra_ipv4_delete ((struct prefix_ipv4 *) &rp->p,
592 &rinfo->nexthop,
593 rinfo->metric);
595 rip_redistribute_delete (rinfo->type,rinfo->sub_type,
596 (struct prefix_ipv4 *)&rp->p,
597 rinfo->ifindex);
599 else
601 /* All redistributed routes but static and system */
602 if ((rinfo->ifindex == ifp->ifindex) &&
603 /* (rinfo->type != ZEBRA_ROUTE_STATIC) && */
604 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
605 rip_redistribute_delete (rinfo->type,rinfo->sub_type,
606 (struct prefix_ipv4 *)&rp->p,
607 rinfo->ifindex);
612 ri = ifp->info;
614 if (ri->running)
616 if (IS_RIP_DEBUG_EVENT)
617 zlog_debug ("turn off %s", ifp->name);
619 /* Leave from multicast group. */
620 rip_multicast_leave (ifp, rip->sock);
622 ri->running = 0;
625 return 0;
628 /* Needed for stop RIP process. */
629 void
630 rip_if_down_all ()
632 struct interface *ifp;
633 struct listnode *node, *nnode;
635 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
636 rip_if_down (ifp);
639 static void
640 rip_apply_address_add (struct connected *ifc)
642 struct prefix_ipv4 address;
643 struct prefix *p;
645 if (!rip)
646 return;
648 if (! if_is_up(ifc->ifp))
649 return;
651 p = ifc->address;
653 memset (&address, 0, sizeof (address));
654 address.family = p->family;
655 address.prefix = p->u.prefix4;
656 address.prefixlen = p->prefixlen;
657 apply_mask_ipv4(&address);
659 /* Check if this interface is RIP enabled or not
660 or Check if this address's prefix is RIP enabled */
661 if ((rip_enable_if_lookup(ifc->ifp->name) >= 0) ||
662 (rip_enable_network_lookup2(ifc) >= 0))
663 rip_redistribute_add(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
664 &address, ifc->ifp->ifindex, NULL, 0, 0);
669 rip_interface_address_add (int command, struct zclient *zclient,
670 zebra_size_t length)
672 struct connected *ifc;
673 struct prefix *p;
675 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
676 zclient->ibuf);
678 if (ifc == NULL)
679 return 0;
681 p = ifc->address;
683 if (p->family == AF_INET)
685 if (IS_RIP_DEBUG_ZEBRA)
686 zlog_debug ("connected address %s/%d is added",
687 inet_ntoa (p->u.prefix4), p->prefixlen);
689 rip_enable_apply(ifc->ifp);
690 /* Check if this prefix needs to be redistributed */
691 rip_apply_address_add(ifc);
693 #ifdef HAVE_SNMP
694 rip_ifaddr_add (ifc->ifp, ifc);
695 #endif /* HAVE_SNMP */
698 return 0;
701 static void
702 rip_apply_address_del (struct connected *ifc) {
703 struct prefix_ipv4 address;
704 struct prefix *p;
706 if (!rip)
707 return;
709 if (! if_is_up(ifc->ifp))
710 return;
712 p = ifc->address;
714 memset (&address, 0, sizeof (address));
715 address.family = p->family;
716 address.prefix = p->u.prefix4;
717 address.prefixlen = p->prefixlen;
718 apply_mask_ipv4(&address);
720 rip_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
721 &address, ifc->ifp->ifindex);
725 rip_interface_address_delete (int command, struct zclient *zclient,
726 zebra_size_t length)
728 struct connected *ifc;
729 struct prefix *p;
731 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
732 zclient->ibuf);
734 if (ifc)
736 p = ifc->address;
737 if (p->family == AF_INET)
739 if (IS_RIP_DEBUG_ZEBRA)
740 zlog_debug ("connected address %s/%d is deleted",
741 inet_ntoa (p->u.prefix4), p->prefixlen);
743 #ifdef HAVE_SNMP
744 rip_ifaddr_delete (ifc->ifp, ifc);
745 #endif /* HAVE_SNMP */
747 /* Chech wether this prefix needs to be removed */
748 rip_apply_address_del(ifc);
752 connected_free (ifc);
756 return 0;
759 /* Check interface is enabled by network statement. */
760 /* Check wether the interface has at least a connected prefix that
761 * is within the ripng_enable_network table. */
762 static int
763 rip_enable_network_lookup_if (struct interface *ifp)
765 struct listnode *node, *nnode;
766 struct connected *connected;
767 struct prefix_ipv4 address;
769 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
771 struct prefix *p;
772 struct route_node *node;
774 p = connected->address;
776 if (p->family == AF_INET)
778 address.family = AF_INET;
779 address.prefix = p->u.prefix4;
780 address.prefixlen = IPV4_MAX_BITLEN;
782 node = route_node_match (rip_enable_network,
783 (struct prefix *)&address);
784 if (node)
786 route_unlock_node (node);
787 return 1;
791 return -1;
794 /* Check wether connected is within the ripng_enable_network table. */
796 rip_enable_network_lookup2 (struct connected *connected)
798 struct prefix_ipv4 address;
799 struct prefix *p;
801 p = connected->address;
803 if (p->family == AF_INET) {
804 struct route_node *node;
806 address.family = p->family;
807 address.prefix = p->u.prefix4;
808 address.prefixlen = IPV4_MAX_BITLEN;
810 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within rip_enable_network */
811 node = route_node_match (rip_enable_network,
812 (struct prefix *)&address);
814 if (node) {
815 route_unlock_node (node);
816 return 1;
820 return -1;
822 /* Add RIP enable network. */
823 static int
824 rip_enable_network_add (struct prefix *p)
826 struct route_node *node;
828 node = route_node_get (rip_enable_network, p);
830 if (node->info)
832 route_unlock_node (node);
833 return -1;
835 else
836 node->info = (char *) "enabled";
838 /* XXX: One should find a better solution than a generic one */
839 rip_enable_apply_all();
841 return 1;
844 /* Delete RIP enable network. */
845 static int
846 rip_enable_network_delete (struct prefix *p)
848 struct route_node *node;
850 node = route_node_lookup (rip_enable_network, p);
851 if (node)
853 node->info = NULL;
855 /* Unlock info lock. */
856 route_unlock_node (node);
858 /* Unlock lookup lock. */
859 route_unlock_node (node);
861 /* XXX: One should find a better solution than a generic one */
862 rip_enable_apply_all ();
864 return 1;
866 return -1;
869 /* Check interface is enabled by ifname statement. */
870 static int
871 rip_enable_if_lookup (const char *ifname)
873 unsigned int i;
874 char *str;
876 for (i = 0; i < vector_active (rip_enable_interface); i++)
877 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
878 if (strcmp (str, ifname) == 0)
879 return i;
880 return -1;
883 /* Add interface to rip_enable_if. */
884 static int
885 rip_enable_if_add (const char *ifname)
887 int ret;
889 ret = rip_enable_if_lookup (ifname);
890 if (ret >= 0)
891 return -1;
893 vector_set (rip_enable_interface, strdup (ifname));
895 rip_enable_apply_all(); /* TODOVJ */
897 return 1;
900 /* Delete interface from rip_enable_if. */
901 static int
902 rip_enable_if_delete (const char *ifname)
904 int index;
905 char *str;
907 index = rip_enable_if_lookup (ifname);
908 if (index < 0)
909 return -1;
911 str = vector_slot (rip_enable_interface, index);
912 free (str);
913 vector_unset (rip_enable_interface, index);
915 rip_enable_apply_all(); /* TODOVJ */
917 return 1;
920 /* Join to multicast group and send request to the interface. */
921 static int
922 rip_interface_wakeup (struct thread *t)
924 struct interface *ifp;
925 struct rip_interface *ri;
927 /* Get interface. */
928 ifp = THREAD_ARG (t);
930 ri = ifp->info;
931 ri->t_wakeup = NULL;
933 /* Join to multicast group. */
934 if (rip_multicast_join (ifp, rip->sock) < 0)
936 zlog_err ("multicast join failed, interface %s not running", ifp->name);
937 return 0;
940 /* Set running flag. */
941 ri->running = 1;
943 /* Send RIP request to the interface. */
944 rip_request_interface (ifp);
946 return 0;
949 int rip_redistribute_check (int);
951 static void
952 rip_connect_set (struct interface *ifp, int set)
954 struct listnode *node, *nnode;
955 struct connected *connected;
956 struct prefix_ipv4 address;
958 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
960 struct prefix *p;
961 p = connected->address;
963 if (p->family != AF_INET)
964 continue;
966 address.family = AF_INET;
967 address.prefix = p->u.prefix4;
968 address.prefixlen = p->prefixlen;
969 apply_mask_ipv4 (&address);
971 if (set) {
972 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
973 if ((rip_enable_if_lookup(connected->ifp->name) >= 0) ||
974 (rip_enable_network_lookup2(connected) >= 0))
975 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
976 &address, connected->ifp->ifindex,
977 NULL, 0, 0);
978 } else
980 rip_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
981 &address, connected->ifp->ifindex);
982 if (rip_redistribute_check (ZEBRA_ROUTE_CONNECT))
983 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_REDISTRIBUTE,
984 &address, connected->ifp->ifindex,
985 NULL, 0, 0);
990 /* Update interface status. */
991 void
992 rip_enable_apply (struct interface *ifp)
994 int ret;
995 struct rip_interface *ri = NULL;
997 /* Check interface. */
998 if (! if_is_operative (ifp))
999 return;
1001 ri = ifp->info;
1003 /* Check network configuration. */
1004 ret = rip_enable_network_lookup_if (ifp);
1006 /* If the interface is matched. */
1007 if (ret > 0)
1008 ri->enable_network = 1;
1009 else
1010 ri->enable_network = 0;
1012 /* Check interface name configuration. */
1013 ret = rip_enable_if_lookup (ifp->name);
1014 if (ret >= 0)
1015 ri->enable_interface = 1;
1016 else
1017 ri->enable_interface = 0;
1019 /* any interface MUST have an IPv4 address */
1020 if ( ! rip_if_ipv4_address_check (ifp) )
1022 ri->enable_network = 0;
1023 ri->enable_interface = 0;
1026 /* Update running status of the interface. */
1027 if (ri->enable_network || ri->enable_interface)
1030 if (IS_RIP_DEBUG_EVENT)
1031 zlog_debug ("turn on %s", ifp->name);
1033 /* Add interface wake up thread. */
1034 if (! ri->t_wakeup)
1035 ri->t_wakeup = thread_add_timer (master, rip_interface_wakeup,
1036 ifp, 1);
1037 rip_connect_set (ifp, 1);
1040 else
1042 if (ri->running)
1044 /* Might as well clean up the route table as well
1045 * rip_if_down sets to 0 ri->running, and displays "turn off %s"
1046 **/
1047 rip_if_down(ifp);
1049 rip_connect_set (ifp, 0);
1054 /* Apply network configuration to all interface. */
1055 void
1056 rip_enable_apply_all ()
1058 struct interface *ifp;
1059 struct listnode *node, *nnode;
1061 /* Check each interface. */
1062 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
1063 rip_enable_apply (ifp);
1067 rip_neighbor_lookup (struct sockaddr_in *from)
1069 struct prefix_ipv4 p;
1070 struct route_node *node;
1072 memset (&p, 0, sizeof (struct prefix_ipv4));
1073 p.family = AF_INET;
1074 p.prefix = from->sin_addr;
1075 p.prefixlen = IPV4_MAX_BITLEN;
1077 node = route_node_lookup (rip->neighbor, (struct prefix *) &p);
1078 if (node)
1080 route_unlock_node (node);
1081 return 1;
1083 return 0;
1086 /* Add new RIP neighbor to the neighbor tree. */
1087 static int
1088 rip_neighbor_add (struct prefix_ipv4 *p)
1090 struct route_node *node;
1092 node = route_node_get (rip->neighbor, (struct prefix *) p);
1094 if (node->info)
1095 return -1;
1097 node->info = rip->neighbor;
1099 return 0;
1102 /* Delete RIP neighbor from the neighbor tree. */
1103 static int
1104 rip_neighbor_delete (struct prefix_ipv4 *p)
1106 struct route_node *node;
1108 /* Lock for look up. */
1109 node = route_node_lookup (rip->neighbor, (struct prefix *) p);
1110 if (! node)
1111 return -1;
1113 node->info = NULL;
1115 /* Unlock lookup lock. */
1116 route_unlock_node (node);
1118 /* Unlock real neighbor information lock. */
1119 route_unlock_node (node);
1121 return 0;
1124 /* Clear all network and neighbor configuration. */
1125 void
1126 rip_clean_network ()
1128 unsigned int i;
1129 char *str;
1130 struct route_node *rn;
1132 /* rip_enable_network. */
1133 for (rn = route_top (rip_enable_network); rn; rn = route_next (rn))
1134 if (rn->info)
1136 rn->info = NULL;
1137 route_unlock_node (rn);
1140 /* rip_enable_interface. */
1141 for (i = 0; i < vector_active (rip_enable_interface); i++)
1142 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
1144 free (str);
1145 vector_slot (rip_enable_interface, i) = NULL;
1149 /* Utility function for looking up passive interface settings. */
1150 static int
1151 rip_passive_nondefault_lookup (const char *ifname)
1153 unsigned int i;
1154 char *str;
1156 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1157 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1158 if (strcmp (str, ifname) == 0)
1159 return i;
1160 return -1;
1163 void
1164 rip_passive_interface_apply (struct interface *ifp)
1166 struct rip_interface *ri;
1168 ri = ifp->info;
1170 ri->passive = ((rip_passive_nondefault_lookup (ifp->name) < 0) ?
1171 passive_default : !passive_default);
1173 if (IS_RIP_DEBUG_ZEBRA)
1174 zlog_debug ("interface %s: passive = %d",ifp->name,ri->passive);
1177 static void
1178 rip_passive_interface_apply_all (void)
1180 struct interface *ifp;
1181 struct listnode *node, *nnode;
1183 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
1184 rip_passive_interface_apply (ifp);
1187 /* Passive interface. */
1188 static int
1189 rip_passive_nondefault_set (struct vty *vty, const char *ifname)
1191 if (rip_passive_nondefault_lookup (ifname) >= 0)
1192 return CMD_WARNING;
1194 vector_set (Vrip_passive_nondefault, strdup (ifname));
1196 rip_passive_interface_apply_all ();
1198 return CMD_SUCCESS;
1201 static int
1202 rip_passive_nondefault_unset (struct vty *vty, const char *ifname)
1204 int i;
1205 char *str;
1207 i = rip_passive_nondefault_lookup (ifname);
1208 if (i < 0)
1209 return CMD_WARNING;
1211 str = vector_slot (Vrip_passive_nondefault, i);
1212 free (str);
1213 vector_unset (Vrip_passive_nondefault, i);
1215 rip_passive_interface_apply_all ();
1217 return CMD_SUCCESS;
1220 /* Free all configured RIP passive-interface settings. */
1221 void
1222 rip_passive_nondefault_clean (void)
1224 unsigned int i;
1225 char *str;
1227 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1228 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1230 free (str);
1231 vector_slot (Vrip_passive_nondefault, i) = NULL;
1233 rip_passive_interface_apply_all ();
1236 /* RIP enable network or interface configuration. */
1237 DEFUN (rip_network,
1238 rip_network_cmd,
1239 "network (A.B.C.D/M|WORD)",
1240 "Enable routing on an IP network\n"
1241 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1242 "Interface name\n")
1244 int ret;
1245 struct prefix_ipv4 p;
1247 ret = str2prefix_ipv4 (argv[0], &p);
1249 if (ret)
1250 ret = rip_enable_network_add ((struct prefix *) &p);
1251 else
1252 ret = rip_enable_if_add (argv[0]);
1254 if (ret < 0)
1256 vty_out (vty, "There is a same network configuration %s%s", argv[0],
1257 VTY_NEWLINE);
1258 return CMD_WARNING;
1261 return CMD_SUCCESS;
1264 /* RIP enable network or interface configuration. */
1265 DEFUN (no_rip_network,
1266 no_rip_network_cmd,
1267 "no network (A.B.C.D/M|WORD)",
1268 NO_STR
1269 "Enable routing on an IP network\n"
1270 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1271 "Interface name\n")
1273 int ret;
1274 struct prefix_ipv4 p;
1276 ret = str2prefix_ipv4 (argv[0], &p);
1278 if (ret)
1279 ret = rip_enable_network_delete ((struct prefix *) &p);
1280 else
1281 ret = rip_enable_if_delete (argv[0]);
1283 if (ret < 0)
1285 vty_out (vty, "Can't find network configuration %s%s", argv[0],
1286 VTY_NEWLINE);
1287 return CMD_WARNING;
1290 return CMD_SUCCESS;
1293 /* RIP neighbor configuration set. */
1294 DEFUN (rip_neighbor,
1295 rip_neighbor_cmd,
1296 "neighbor A.B.C.D",
1297 "Specify a neighbor router\n"
1298 "Neighbor address\n")
1300 int ret;
1301 struct prefix_ipv4 p;
1303 ret = str2prefix_ipv4 (argv[0], &p);
1305 if (ret <= 0)
1307 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1308 return CMD_WARNING;
1311 rip_neighbor_add (&p);
1313 return CMD_SUCCESS;
1316 /* RIP neighbor configuration unset. */
1317 DEFUN (no_rip_neighbor,
1318 no_rip_neighbor_cmd,
1319 "no neighbor A.B.C.D",
1320 NO_STR
1321 "Specify a neighbor router\n"
1322 "Neighbor address\n")
1324 int ret;
1325 struct prefix_ipv4 p;
1327 ret = str2prefix_ipv4 (argv[0], &p);
1329 if (ret <= 0)
1331 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1332 return CMD_WARNING;
1335 rip_neighbor_delete (&p);
1337 return CMD_SUCCESS;
1340 DEFUN (ip_rip_receive_version,
1341 ip_rip_receive_version_cmd,
1342 "ip rip receive version (1|2)",
1343 IP_STR
1344 "Routing Information Protocol\n"
1345 "Advertisement reception\n"
1346 "Version control\n"
1347 "RIP version 1\n"
1348 "RIP version 2\n")
1350 struct interface *ifp;
1351 struct rip_interface *ri;
1353 ifp = (struct interface *)vty->index;
1354 ri = ifp->info;
1356 /* Version 1. */
1357 if (atoi (argv[0]) == 1)
1359 ri->ri_receive = RI_RIP_VERSION_1;
1360 return CMD_SUCCESS;
1362 if (atoi (argv[0]) == 2)
1364 ri->ri_receive = RI_RIP_VERSION_2;
1365 return CMD_SUCCESS;
1367 return CMD_WARNING;
1370 DEFUN (ip_rip_receive_version_1,
1371 ip_rip_receive_version_1_cmd,
1372 "ip rip receive version 1 2",
1373 IP_STR
1374 "Routing Information Protocol\n"
1375 "Advertisement reception\n"
1376 "Version control\n"
1377 "RIP version 1\n"
1378 "RIP version 2\n")
1380 struct interface *ifp;
1381 struct rip_interface *ri;
1383 ifp = (struct interface *)vty->index;
1384 ri = ifp->info;
1386 /* Version 1 and 2. */
1387 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1388 return CMD_SUCCESS;
1391 DEFUN (ip_rip_receive_version_2,
1392 ip_rip_receive_version_2_cmd,
1393 "ip rip receive version 2 1",
1394 IP_STR
1395 "Routing Information Protocol\n"
1396 "Advertisement reception\n"
1397 "Version control\n"
1398 "RIP version 2\n"
1399 "RIP version 1\n")
1401 struct interface *ifp;
1402 struct rip_interface *ri;
1404 ifp = (struct interface *)vty->index;
1405 ri = ifp->info;
1407 /* Version 1 and 2. */
1408 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1409 return CMD_SUCCESS;
1412 DEFUN (no_ip_rip_receive_version,
1413 no_ip_rip_receive_version_cmd,
1414 "no ip rip receive version",
1415 NO_STR
1416 IP_STR
1417 "Routing Information Protocol\n"
1418 "Advertisement reception\n"
1419 "Version control\n")
1421 struct interface *ifp;
1422 struct rip_interface *ri;
1424 ifp = (struct interface *)vty->index;
1425 ri = ifp->info;
1427 ri->ri_receive = RI_RIP_UNSPEC;
1428 return CMD_SUCCESS;
1431 ALIAS (no_ip_rip_receive_version,
1432 no_ip_rip_receive_version_num_cmd,
1433 "no ip rip receive version (1|2)",
1434 NO_STR
1435 IP_STR
1436 "Routing Information Protocol\n"
1437 "Advertisement reception\n"
1438 "Version control\n"
1439 "Version 1\n"
1440 "Version 2\n")
1442 DEFUN (ip_rip_send_version,
1443 ip_rip_send_version_cmd,
1444 "ip rip send version (1|2)",
1445 IP_STR
1446 "Routing Information Protocol\n"
1447 "Advertisement transmission\n"
1448 "Version control\n"
1449 "RIP version 1\n"
1450 "RIP version 2\n")
1452 struct interface *ifp;
1453 struct rip_interface *ri;
1455 ifp = (struct interface *)vty->index;
1456 ri = ifp->info;
1458 /* Version 1. */
1459 if (atoi (argv[0]) == 1)
1461 ri->ri_send = RI_RIP_VERSION_1;
1462 return CMD_SUCCESS;
1464 if (atoi (argv[0]) == 2)
1466 ri->ri_send = RI_RIP_VERSION_2;
1467 return CMD_SUCCESS;
1469 return CMD_WARNING;
1472 DEFUN (ip_rip_send_version_1,
1473 ip_rip_send_version_1_cmd,
1474 "ip rip send version 1 2",
1475 IP_STR
1476 "Routing Information Protocol\n"
1477 "Advertisement transmission\n"
1478 "Version control\n"
1479 "RIP version 1\n"
1480 "RIP version 2\n")
1482 struct interface *ifp;
1483 struct rip_interface *ri;
1485 ifp = (struct interface *)vty->index;
1486 ri = ifp->info;
1488 /* Version 1 and 2. */
1489 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1490 return CMD_SUCCESS;
1493 DEFUN (ip_rip_send_version_2,
1494 ip_rip_send_version_2_cmd,
1495 "ip rip send version 2 1",
1496 IP_STR
1497 "Routing Information Protocol\n"
1498 "Advertisement transmission\n"
1499 "Version control\n"
1500 "RIP version 2\n"
1501 "RIP version 1\n")
1503 struct interface *ifp;
1504 struct rip_interface *ri;
1506 ifp = (struct interface *)vty->index;
1507 ri = ifp->info;
1509 /* Version 1 and 2. */
1510 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1511 return CMD_SUCCESS;
1514 DEFUN (no_ip_rip_send_version,
1515 no_ip_rip_send_version_cmd,
1516 "no ip rip send version",
1517 NO_STR
1518 IP_STR
1519 "Routing Information Protocol\n"
1520 "Advertisement transmission\n"
1521 "Version control\n")
1523 struct interface *ifp;
1524 struct rip_interface *ri;
1526 ifp = (struct interface *)vty->index;
1527 ri = ifp->info;
1529 ri->ri_send = RI_RIP_UNSPEC;
1530 return CMD_SUCCESS;
1533 ALIAS (no_ip_rip_send_version,
1534 no_ip_rip_send_version_num_cmd,
1535 "no ip rip send version (1|2)",
1536 NO_STR
1537 IP_STR
1538 "Routing Information Protocol\n"
1539 "Advertisement transmission\n"
1540 "Version control\n"
1541 "Version 1\n"
1542 "Version 2\n")
1544 DEFUN (ip_rip_authentication_mode,
1545 ip_rip_authentication_mode_cmd,
1546 "ip rip authentication mode (md5|text)",
1547 IP_STR
1548 "Routing Information Protocol\n"
1549 "Authentication control\n"
1550 "Authentication mode\n"
1551 "Keyed message digest\n"
1552 "Clear text authentication\n")
1554 struct interface *ifp;
1555 struct rip_interface *ri;
1556 int auth_type;
1558 ifp = (struct interface *)vty->index;
1559 ri = ifp->info;
1561 if ( (argc < 1) || (argc > 2) )
1563 vty_out (vty, "incorrect argument count%s", VTY_NEWLINE);
1564 return CMD_WARNING;
1567 if (strncmp ("md5", argv[0], strlen (argv[0])) == 0)
1568 auth_type = RIP_AUTH_MD5;
1569 else if (strncmp ("text", argv[0], strlen (argv[0])) == 0)
1570 auth_type = RIP_AUTH_SIMPLE_PASSWORD;
1571 else
1573 vty_out (vty, "mode should be md5 or text%s", VTY_NEWLINE);
1574 return CMD_WARNING;
1577 if (argc == 1)
1579 ri->auth_type = auth_type;
1580 return CMD_SUCCESS;
1583 if ( (argc == 2) && (auth_type != RIP_AUTH_MD5) )
1585 vty_out (vty, "auth length argument only valid for md5%s", VTY_NEWLINE);
1586 return CMD_WARNING;
1589 if (strncmp ("r", argv[1], 1) == 0)
1590 ri->md5_auth_len = RIP_AUTH_MD5_SIZE;
1591 else if (strncmp ("o", argv[1], 1) == 0)
1592 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1593 else
1594 return CMD_WARNING;
1596 ri->auth_type = auth_type;
1598 return CMD_SUCCESS;
1601 ALIAS (ip_rip_authentication_mode,
1602 ip_rip_authentication_mode_authlen_cmd,
1603 "ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1604 IP_STR
1605 "Routing Information Protocol\n"
1606 "Authentication control\n"
1607 "Authentication mode\n"
1608 "Keyed message digest\n"
1609 "Clear text authentication\n"
1610 "MD5 authentication data length\n"
1611 "RFC compatible\n"
1612 "Old ripd compatible\n")
1614 DEFUN (no_ip_rip_authentication_mode,
1615 no_ip_rip_authentication_mode_cmd,
1616 "no ip rip authentication mode",
1617 NO_STR
1618 IP_STR
1619 "Routing Information Protocol\n"
1620 "Authentication control\n"
1621 "Authentication mode\n")
1623 struct interface *ifp;
1624 struct rip_interface *ri;
1626 ifp = (struct interface *)vty->index;
1627 ri = ifp->info;
1629 ri->auth_type = RIP_NO_AUTH;
1630 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1632 return CMD_SUCCESS;
1635 ALIAS (no_ip_rip_authentication_mode,
1636 no_ip_rip_authentication_mode_type_cmd,
1637 "no ip rip authentication mode (md5|text)",
1638 NO_STR
1639 IP_STR
1640 "Routing Information Protocol\n"
1641 "Authentication control\n"
1642 "Authentication mode\n"
1643 "Keyed message digest\n"
1644 "Clear text authentication\n")
1646 ALIAS (no_ip_rip_authentication_mode,
1647 no_ip_rip_authentication_mode_type_authlen_cmd,
1648 "no ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1649 NO_STR
1650 IP_STR
1651 "Routing Information Protocol\n"
1652 "Authentication control\n"
1653 "Authentication mode\n"
1654 "Keyed message digest\n"
1655 "Clear text authentication\n"
1656 "MD5 authentication data length\n"
1657 "RFC compatible\n"
1658 "Old ripd compatible\n")
1660 DEFUN (ip_rip_authentication_string,
1661 ip_rip_authentication_string_cmd,
1662 "ip rip authentication string LINE",
1663 IP_STR
1664 "Routing Information Protocol\n"
1665 "Authentication control\n"
1666 "Authentication string\n"
1667 "Authentication string\n")
1669 struct interface *ifp;
1670 struct rip_interface *ri;
1672 ifp = (struct interface *)vty->index;
1673 ri = ifp->info;
1675 if (strlen (argv[0]) > 16)
1677 vty_out (vty, "%% RIPv2 authentication string must be shorter than 16%s",
1678 VTY_NEWLINE);
1679 return CMD_WARNING;
1682 if (ri->key_chain)
1684 vty_out (vty, "%% key-chain configuration exists%s", VTY_NEWLINE);
1685 return CMD_WARNING;
1688 if (ri->auth_str)
1689 free (ri->auth_str);
1691 ri->auth_str = strdup (argv[0]);
1693 return CMD_SUCCESS;
1696 DEFUN (no_ip_rip_authentication_string,
1697 no_ip_rip_authentication_string_cmd,
1698 "no ip rip authentication string",
1699 NO_STR
1700 IP_STR
1701 "Routing Information Protocol\n"
1702 "Authentication control\n"
1703 "Authentication string\n")
1705 struct interface *ifp;
1706 struct rip_interface *ri;
1708 ifp = (struct interface *)vty->index;
1709 ri = ifp->info;
1711 if (ri->auth_str)
1712 free (ri->auth_str);
1714 ri->auth_str = NULL;
1716 return CMD_SUCCESS;
1719 ALIAS (no_ip_rip_authentication_string,
1720 no_ip_rip_authentication_string2_cmd,
1721 "no ip rip authentication string LINE",
1722 NO_STR
1723 IP_STR
1724 "Routing Information Protocol\n"
1725 "Authentication control\n"
1726 "Authentication string\n"
1727 "Authentication string\n")
1729 DEFUN (ip_rip_authentication_key_chain,
1730 ip_rip_authentication_key_chain_cmd,
1731 "ip rip authentication key-chain LINE",
1732 IP_STR
1733 "Routing Information Protocol\n"
1734 "Authentication control\n"
1735 "Authentication key-chain\n"
1736 "name of key-chain\n")
1738 struct interface *ifp;
1739 struct rip_interface *ri;
1741 ifp = (struct interface *) vty->index;
1742 ri = ifp->info;
1744 if (ri->auth_str)
1746 vty_out (vty, "%% authentication string configuration exists%s",
1747 VTY_NEWLINE);
1748 return CMD_WARNING;
1751 if (ri->key_chain)
1752 free (ri->key_chain);
1754 ri->key_chain = strdup (argv[0]);
1756 return CMD_SUCCESS;
1759 DEFUN (no_ip_rip_authentication_key_chain,
1760 no_ip_rip_authentication_key_chain_cmd,
1761 "no ip rip authentication key-chain",
1762 NO_STR
1763 IP_STR
1764 "Routing Information Protocol\n"
1765 "Authentication control\n"
1766 "Authentication key-chain\n")
1768 struct interface *ifp;
1769 struct rip_interface *ri;
1771 ifp = (struct interface *) vty->index;
1772 ri = ifp->info;
1774 if (ri->key_chain)
1775 free (ri->key_chain);
1777 ri->key_chain = NULL;
1779 return CMD_SUCCESS;
1782 ALIAS (no_ip_rip_authentication_key_chain,
1783 no_ip_rip_authentication_key_chain2_cmd,
1784 "no ip rip authentication key-chain LINE",
1785 NO_STR
1786 IP_STR
1787 "Routing Information Protocol\n"
1788 "Authentication control\n"
1789 "Authentication key-chain\n"
1790 "name of key-chain\n")
1792 /* CHANGED: ip rip split-horizon
1793 Cisco and Zebra's command is
1794 ip split-horizon
1796 DEFUN (ip_rip_split_horizon,
1797 ip_rip_split_horizon_cmd,
1798 "ip rip split-horizon",
1799 IP_STR
1800 "Routing Information Protocol\n"
1801 "Perform split horizon\n")
1803 struct interface *ifp;
1804 struct rip_interface *ri;
1806 ifp = vty->index;
1807 ri = ifp->info;
1809 ri->split_horizon = RIP_SPLIT_HORIZON;
1810 return CMD_SUCCESS;
1813 DEFUN (ip_rip_split_horizon_poisoned_reverse,
1814 ip_rip_split_horizon_poisoned_reverse_cmd,
1815 "ip rip split-horizon poisoned-reverse",
1816 IP_STR
1817 "Routing Information Protocol\n"
1818 "Perform split horizon\n"
1819 "With poisoned-reverse\n")
1821 struct interface *ifp;
1822 struct rip_interface *ri;
1824 ifp = vty->index;
1825 ri = ifp->info;
1827 ri->split_horizon = RIP_SPLIT_HORIZON_POISONED_REVERSE;
1828 return CMD_SUCCESS;
1831 /* CHANGED: no ip rip split-horizon
1832 Cisco and Zebra's command is
1833 no ip split-horizon
1835 DEFUN (no_ip_rip_split_horizon,
1836 no_ip_rip_split_horizon_cmd,
1837 "no ip rip split-horizon",
1838 NO_STR
1839 IP_STR
1840 "Routing Information Protocol\n"
1841 "Perform split horizon\n")
1843 struct interface *ifp;
1844 struct rip_interface *ri;
1846 ifp = vty->index;
1847 ri = ifp->info;
1849 ri->split_horizon = RIP_NO_SPLIT_HORIZON;
1850 return CMD_SUCCESS;
1853 DEFUN (no_ip_rip_split_horizon_poisoned_reverse,
1854 no_ip_rip_split_horizon_poisoned_reverse_cmd,
1855 "no ip rip split-horizon poisoned-reverse",
1856 NO_STR
1857 IP_STR
1858 "Routing Information Protocol\n"
1859 "Perform split horizon\n"
1860 "With poisoned-reverse\n")
1862 struct interface *ifp;
1863 struct rip_interface *ri;
1865 ifp = vty->index;
1866 ri = ifp->info;
1868 switch( ri->split_horizon )
1870 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1871 ri->split_horizon = RIP_SPLIT_HORIZON;
1872 default:
1873 break;
1876 return CMD_SUCCESS;
1879 DEFUN (rip_passive_interface,
1880 rip_passive_interface_cmd,
1881 "passive-interface (IFNAME|default)",
1882 "Suppress routing updates on an interface\n"
1883 "Interface name\n"
1884 "default for all interfaces\n")
1886 const char *ifname = argv[0];
1888 if (!strcmp(ifname,"default")) {
1889 passive_default = 1;
1890 rip_passive_nondefault_clean();
1891 return CMD_SUCCESS;
1893 if (passive_default)
1894 return rip_passive_nondefault_unset (vty, ifname);
1895 else
1896 return rip_passive_nondefault_set (vty, ifname);
1899 DEFUN (no_rip_passive_interface,
1900 no_rip_passive_interface_cmd,
1901 "no passive-interface (IFNAME|default)",
1902 NO_STR
1903 "Suppress routing updates on an interface\n"
1904 "Interface name\n"
1905 "default for all interfaces\n")
1907 const char *ifname = argv[0];
1909 if (!strcmp(ifname,"default")) {
1910 passive_default = 0;
1911 rip_passive_nondefault_clean();
1912 return CMD_SUCCESS;
1914 if (passive_default)
1915 return rip_passive_nondefault_set (vty, ifname);
1916 else
1917 return rip_passive_nondefault_unset (vty, ifname);
1920 /* Write rip configuration of each interface. */
1921 static int
1922 rip_interface_config_write (struct vty *vty)
1924 struct listnode *node;
1925 struct interface *ifp;
1927 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1929 struct rip_interface *ri;
1931 ri = ifp->info;
1933 /* Do not display the interface if there is no
1934 * configuration about it.
1936 if ((!ifp->desc) &&
1937 (ri->split_horizon == ri->split_horizon_default) &&
1938 (ri->ri_send == RI_RIP_UNSPEC) &&
1939 (ri->ri_receive == RI_RIP_UNSPEC) &&
1940 (ri->auth_type != RIP_AUTH_MD5) &&
1941 (ri->md5_auth_len != RIP_AUTH_MD5_SIZE) &&
1942 (!ri->auth_str) &&
1943 (!ri->key_chain) )
1944 continue;
1946 vty_out (vty, "interface %s%s", ifp->name,
1947 VTY_NEWLINE);
1949 if (ifp->desc)
1950 vty_out (vty, " description %s%s", ifp->desc,
1951 VTY_NEWLINE);
1953 /* Split horizon. */
1954 if (ri->split_horizon != ri->split_horizon_default)
1956 switch (ri->split_horizon) {
1957 case RIP_SPLIT_HORIZON:
1958 vty_out (vty, " ip rip split-horizon%s", VTY_NEWLINE);
1959 break;
1960 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1961 vty_out (vty, " ip rip split-horizon poisoned-reverse%s",
1962 VTY_NEWLINE);
1963 break;
1964 case RIP_NO_SPLIT_HORIZON:
1965 default:
1966 vty_out (vty, " no ip rip split-horizon%s", VTY_NEWLINE);
1967 break;
1971 /* RIP version setting. */
1972 if (ri->ri_send != RI_RIP_UNSPEC)
1973 vty_out (vty, " ip rip send version %s%s",
1974 lookup (ri_version_msg, ri->ri_send),
1975 VTY_NEWLINE);
1977 if (ri->ri_receive != RI_RIP_UNSPEC)
1978 vty_out (vty, " ip rip receive version %s%s",
1979 lookup (ri_version_msg, ri->ri_receive),
1980 VTY_NEWLINE);
1982 /* RIP authentication. */
1983 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
1984 vty_out (vty, " ip rip authentication mode text%s", VTY_NEWLINE);
1986 if (ri->auth_type == RIP_AUTH_MD5)
1988 vty_out (vty, " ip rip authentication mode md5");
1989 if (ri->md5_auth_len == RIP_AUTH_MD5_COMPAT_SIZE)
1990 vty_out (vty, " auth-length old-ripd");
1991 else
1992 vty_out (vty, " auth-length rfc");
1993 vty_out (vty, "%s", VTY_NEWLINE);
1996 if (ri->auth_str)
1997 vty_out (vty, " ip rip authentication string %s%s",
1998 ri->auth_str, VTY_NEWLINE);
2000 if (ri->key_chain)
2001 vty_out (vty, " ip rip authentication key-chain %s%s",
2002 ri->key_chain, VTY_NEWLINE);
2004 vty_out (vty, "!%s", VTY_NEWLINE);
2006 return 0;
2010 config_write_rip_network (struct vty *vty, int config_mode)
2012 unsigned int i;
2013 char *ifname;
2014 struct route_node *node;
2016 /* Network type RIP enable interface statement. */
2017 for (node = route_top (rip_enable_network); node; node = route_next (node))
2018 if (node->info)
2019 vty_out (vty, "%s%s/%d%s",
2020 config_mode ? " network " : " ",
2021 inet_ntoa (node->p.u.prefix4),
2022 node->p.prefixlen,
2023 VTY_NEWLINE);
2025 /* Interface name RIP enable statement. */
2026 for (i = 0; i < vector_active (rip_enable_interface); i++)
2027 if ((ifname = vector_slot (rip_enable_interface, i)) != NULL)
2028 vty_out (vty, "%s%s%s",
2029 config_mode ? " network " : " ",
2030 ifname,
2031 VTY_NEWLINE);
2033 /* RIP neighbors listing. */
2034 for (node = route_top (rip->neighbor); node; node = route_next (node))
2035 if (node->info)
2036 vty_out (vty, "%s%s%s",
2037 config_mode ? " neighbor " : " ",
2038 inet_ntoa (node->p.u.prefix4),
2039 VTY_NEWLINE);
2041 /* RIP passive interface listing. */
2042 if (config_mode) {
2043 if (passive_default)
2044 vty_out (vty, " passive-interface default%s", VTY_NEWLINE);
2045 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
2046 if ((ifname = vector_slot (Vrip_passive_nondefault, i)) != NULL)
2047 vty_out (vty, " %spassive-interface %s%s",
2048 (passive_default ? "no " : ""), ifname, VTY_NEWLINE);
2051 return 0;
2054 static struct cmd_node interface_node =
2056 INTERFACE_NODE,
2057 "%s(config-if)# ",
2061 /* Called when interface structure allocated. */
2062 static int
2063 rip_interface_new_hook (struct interface *ifp)
2065 ifp->info = rip_interface_new ();
2066 return 0;
2069 /* Called when interface structure deleted. */
2070 static int
2071 rip_interface_delete_hook (struct interface *ifp)
2073 XFREE (MTYPE_RIP_INTERFACE, ifp->info);
2074 ifp->info = NULL;
2075 return 0;
2078 /* Allocate and initialize interface vector. */
2079 void
2080 rip_if_init (void)
2082 /* Default initial size of interface vector. */
2083 if_init();
2084 if_add_hook (IF_NEW_HOOK, rip_interface_new_hook);
2085 if_add_hook (IF_DELETE_HOOK, rip_interface_delete_hook);
2087 /* RIP network init. */
2088 rip_enable_interface = vector_init (1);
2089 rip_enable_network = route_table_init ();
2091 /* RIP passive interface. */
2092 Vrip_passive_nondefault = vector_init (1);
2094 /* Install interface node. */
2095 install_node (&interface_node, rip_interface_config_write);
2097 /* Install commands. */
2098 install_element (CONFIG_NODE, &interface_cmd);
2099 install_element (CONFIG_NODE, &no_interface_cmd);
2100 install_default (INTERFACE_NODE);
2101 install_element (INTERFACE_NODE, &interface_desc_cmd);
2102 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
2103 install_element (RIP_NODE, &rip_network_cmd);
2104 install_element (RIP_NODE, &no_rip_network_cmd);
2105 install_element (RIP_NODE, &rip_neighbor_cmd);
2106 install_element (RIP_NODE, &no_rip_neighbor_cmd);
2108 install_element (RIP_NODE, &rip_passive_interface_cmd);
2109 install_element (RIP_NODE, &no_rip_passive_interface_cmd);
2111 install_element (INTERFACE_NODE, &ip_rip_send_version_cmd);
2112 install_element (INTERFACE_NODE, &ip_rip_send_version_1_cmd);
2113 install_element (INTERFACE_NODE, &ip_rip_send_version_2_cmd);
2114 install_element (INTERFACE_NODE, &no_ip_rip_send_version_cmd);
2115 install_element (INTERFACE_NODE, &no_ip_rip_send_version_num_cmd);
2117 install_element (INTERFACE_NODE, &ip_rip_receive_version_cmd);
2118 install_element (INTERFACE_NODE, &ip_rip_receive_version_1_cmd);
2119 install_element (INTERFACE_NODE, &ip_rip_receive_version_2_cmd);
2120 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_cmd);
2121 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_num_cmd);
2123 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_cmd);
2124 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_authlen_cmd);
2125 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_cmd);
2126 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_cmd);
2127 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_authlen_cmd);
2129 install_element (INTERFACE_NODE, &ip_rip_authentication_key_chain_cmd);
2130 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain_cmd);
2131 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain2_cmd);
2133 install_element (INTERFACE_NODE, &ip_rip_authentication_string_cmd);
2134 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string_cmd);
2135 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string2_cmd);
2137 install_element (INTERFACE_NODE, &ip_rip_split_horizon_cmd);
2138 install_element (INTERFACE_NODE, &ip_rip_split_horizon_poisoned_reverse_cmd);
2139 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_cmd);
2140 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_poisoned_reverse_cmd);