ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / ripd / rip_interface.c
blob4f80bf42ead47d78d211788855aed518997db541
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 %llx metric %d mtu %d is down",
406 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
407 ifp->metric, ifp->mtu);
409 return 0;
412 /* Inteface link up message processing */
414 rip_interface_up (int command, struct zclient *zclient, zebra_size_t length)
416 struct interface *ifp;
418 /* zebra_interface_state_read () updates interface structure in
419 iflist. */
420 ifp = zebra_interface_state_read (zclient->ibuf);
422 if (ifp == NULL)
423 return 0;
425 if (IS_RIP_DEBUG_ZEBRA)
426 zlog_debug ("interface %s index %d flags %lld metric %d mtu %d is up",
427 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
429 /* Check if this interface is RIP enabled or not.*/
430 rip_enable_apply (ifp);
432 /* Check for a passive interface */
433 rip_passive_interface_apply (ifp);
435 /* Apply distribute list to the all interface. */
436 rip_distribute_update_interface (ifp);
438 return 0;
441 /* Inteface addition message from zebra. */
443 rip_interface_add (int command, struct zclient *zclient, zebra_size_t length)
445 struct interface *ifp;
447 ifp = zebra_interface_add_read (zclient->ibuf);
449 if (IS_RIP_DEBUG_ZEBRA)
450 zlog_debug ("interface add %s index %d flags %lld metric %d mtu %d",
451 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
453 /* Check if this interface is RIP enabled or not.*/
454 rip_enable_apply (ifp);
456 /* Check for a passive interface */
457 rip_passive_interface_apply (ifp);
459 /* Apply distribute list to the all interface. */
460 rip_distribute_update_interface (ifp);
462 /* rip_request_neighbor_all (); */
464 /* Check interface routemap. */
465 rip_if_rmap_update_interface (ifp);
467 return 0;
471 rip_interface_delete (int command, struct zclient *zclient,
472 zebra_size_t length)
474 struct interface *ifp;
475 struct stream *s;
478 s = zclient->ibuf;
479 /* zebra_interface_state_read() updates interface structure in iflist */
480 ifp = zebra_interface_state_read(s);
482 if (ifp == NULL)
483 return 0;
485 if (if_is_up (ifp)) {
486 rip_if_down(ifp);
489 zlog_info("interface delete %s index %d flags %lld metric %d mtu %d",
490 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
492 /* To support pseudo interface do not free interface structure. */
493 /* if_delete(ifp); */
494 ifp->ifindex = IFINDEX_INTERNAL;
496 return 0;
499 void
500 rip_interface_clean (void)
502 struct listnode *node;
503 struct interface *ifp;
504 struct rip_interface *ri;
506 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
508 ri = ifp->info;
510 ri->enable_network = 0;
511 ri->enable_interface = 0;
512 ri->running = 0;
514 if (ri->t_wakeup)
516 thread_cancel (ri->t_wakeup);
517 ri->t_wakeup = NULL;
522 void
523 rip_interface_reset (void)
525 struct listnode *node;
526 struct interface *ifp;
527 struct rip_interface *ri;
529 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
531 ri = ifp->info;
533 ri->enable_network = 0;
534 ri->enable_interface = 0;
535 ri->running = 0;
537 ri->ri_send = RI_RIP_UNSPEC;
538 ri->ri_receive = RI_RIP_UNSPEC;
540 ri->auth_type = RIP_NO_AUTH;
542 if (ri->auth_str)
544 free (ri->auth_str);
545 ri->auth_str = NULL;
547 if (ri->key_chain)
549 free (ri->key_chain);
550 ri->key_chain = NULL;
553 ri->split_horizon = RIP_NO_SPLIT_HORIZON;
554 ri->split_horizon_default = RIP_NO_SPLIT_HORIZON;
556 ri->list[RIP_FILTER_IN] = NULL;
557 ri->list[RIP_FILTER_OUT] = NULL;
559 ri->prefix[RIP_FILTER_IN] = NULL;
560 ri->prefix[RIP_FILTER_OUT] = NULL;
562 if (ri->t_wakeup)
564 thread_cancel (ri->t_wakeup);
565 ri->t_wakeup = NULL;
568 ri->recv_badpackets = 0;
569 ri->recv_badroutes = 0;
570 ri->sent_updates = 0;
572 ri->passive = 0;
577 rip_if_down(struct interface *ifp)
579 struct route_node *rp;
580 struct rip_info *rinfo;
581 struct rip_interface *ri = NULL;
582 if (rip)
584 for (rp = route_top (rip->table); rp; rp = route_next (rp))
585 if ((rinfo = rp->info) != NULL)
587 /* Routes got through this interface. */
588 if (rinfo->ifindex == ifp->ifindex &&
589 rinfo->type == ZEBRA_ROUTE_RIP &&
590 rinfo->sub_type == RIP_ROUTE_RTE)
592 rip_zebra_ipv4_delete ((struct prefix_ipv4 *) &rp->p,
593 &rinfo->nexthop,
594 rinfo->metric);
596 rip_redistribute_delete (rinfo->type,rinfo->sub_type,
597 (struct prefix_ipv4 *)&rp->p,
598 rinfo->ifindex);
600 else
602 /* All redistributed routes but static and system */
603 if ((rinfo->ifindex == ifp->ifindex) &&
604 /* (rinfo->type != ZEBRA_ROUTE_STATIC) && */
605 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
606 rip_redistribute_delete (rinfo->type,rinfo->sub_type,
607 (struct prefix_ipv4 *)&rp->p,
608 rinfo->ifindex);
613 ri = ifp->info;
615 if (ri->running)
617 if (IS_RIP_DEBUG_EVENT)
618 zlog_debug ("turn off %s", ifp->name);
620 /* Leave from multicast group. */
621 rip_multicast_leave (ifp, rip->sock);
623 ri->running = 0;
626 return 0;
629 /* Needed for stop RIP process. */
630 void
631 rip_if_down_all ()
633 struct interface *ifp;
634 struct listnode *node, *nnode;
636 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
637 rip_if_down (ifp);
640 static void
641 rip_apply_address_add (struct connected *ifc)
643 struct prefix_ipv4 address;
644 struct prefix *p;
646 if (!rip)
647 return;
649 if (! if_is_up(ifc->ifp))
650 return;
652 p = ifc->address;
654 memset (&address, 0, sizeof (address));
655 address.family = p->family;
656 address.prefix = p->u.prefix4;
657 address.prefixlen = p->prefixlen;
658 apply_mask_ipv4(&address);
660 /* Check if this interface is RIP enabled or not
661 or Check if this address's prefix is RIP enabled */
662 if ((rip_enable_if_lookup(ifc->ifp->name) >= 0) ||
663 (rip_enable_network_lookup2(ifc) >= 0))
664 rip_redistribute_add(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
665 &address, ifc->ifp->ifindex, NULL, 0, 0);
670 rip_interface_address_add (int command, struct zclient *zclient,
671 zebra_size_t length)
673 struct connected *ifc;
674 struct prefix *p;
676 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
677 zclient->ibuf);
679 if (ifc == NULL)
680 return 0;
682 p = ifc->address;
684 if (p->family == AF_INET)
686 if (IS_RIP_DEBUG_ZEBRA)
687 zlog_debug ("connected address %s/%d is added",
688 inet_ntoa (p->u.prefix4), p->prefixlen);
690 rip_enable_apply(ifc->ifp);
691 /* Check if this prefix needs to be redistributed */
692 rip_apply_address_add(ifc);
694 #ifdef HAVE_SNMP
695 rip_ifaddr_add (ifc->ifp, ifc);
696 #endif /* HAVE_SNMP */
699 return 0;
702 static void
703 rip_apply_address_del (struct connected *ifc) {
704 struct prefix_ipv4 address;
705 struct prefix *p;
707 if (!rip)
708 return;
710 if (! if_is_up(ifc->ifp))
711 return;
713 p = ifc->address;
715 memset (&address, 0, sizeof (address));
716 address.family = p->family;
717 address.prefix = p->u.prefix4;
718 address.prefixlen = p->prefixlen;
719 apply_mask_ipv4(&address);
721 rip_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
722 &address, ifc->ifp->ifindex);
726 rip_interface_address_delete (int command, struct zclient *zclient,
727 zebra_size_t length)
729 struct connected *ifc;
730 struct prefix *p;
732 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
733 zclient->ibuf);
735 if (ifc)
737 p = ifc->address;
738 if (p->family == AF_INET)
740 if (IS_RIP_DEBUG_ZEBRA)
741 zlog_debug ("connected address %s/%d is deleted",
742 inet_ntoa (p->u.prefix4), p->prefixlen);
744 #ifdef HAVE_SNMP
745 rip_ifaddr_delete (ifc->ifp, ifc);
746 #endif /* HAVE_SNMP */
748 /* Chech wether this prefix needs to be removed */
749 rip_apply_address_del(ifc);
753 connected_free (ifc);
757 return 0;
760 /* Check interface is enabled by network statement. */
761 /* Check wether the interface has at least a connected prefix that
762 * is within the ripng_enable_network table. */
763 static int
764 rip_enable_network_lookup_if (struct interface *ifp)
766 struct listnode *node, *nnode;
767 struct connected *connected;
768 struct prefix_ipv4 address;
770 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
772 struct prefix *p;
773 struct route_node *node;
775 p = connected->address;
777 if (p->family == AF_INET)
779 address.family = AF_INET;
780 address.prefix = p->u.prefix4;
781 address.prefixlen = IPV4_MAX_BITLEN;
783 node = route_node_match (rip_enable_network,
784 (struct prefix *)&address);
785 if (node)
787 route_unlock_node (node);
788 return 1;
792 return -1;
795 /* Check wether connected is within the ripng_enable_network table. */
797 rip_enable_network_lookup2 (struct connected *connected)
799 struct prefix_ipv4 address;
800 struct prefix *p;
802 p = connected->address;
804 if (p->family == AF_INET) {
805 struct route_node *node;
807 address.family = p->family;
808 address.prefix = p->u.prefix4;
809 address.prefixlen = IPV4_MAX_BITLEN;
811 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within rip_enable_network */
812 node = route_node_match (rip_enable_network,
813 (struct prefix *)&address);
815 if (node) {
816 route_unlock_node (node);
817 return 1;
821 return -1;
823 /* Add RIP enable network. */
824 static int
825 rip_enable_network_add (struct prefix *p)
827 struct route_node *node;
829 node = route_node_get (rip_enable_network, p);
831 if (node->info)
833 route_unlock_node (node);
834 return -1;
836 else
837 node->info = (char *) "enabled";
839 /* XXX: One should find a better solution than a generic one */
840 rip_enable_apply_all();
842 return 1;
845 /* Delete RIP enable network. */
846 static int
847 rip_enable_network_delete (struct prefix *p)
849 struct route_node *node;
851 node = route_node_lookup (rip_enable_network, p);
852 if (node)
854 node->info = NULL;
856 /* Unlock info lock. */
857 route_unlock_node (node);
859 /* Unlock lookup lock. */
860 route_unlock_node (node);
862 /* XXX: One should find a better solution than a generic one */
863 rip_enable_apply_all ();
865 return 1;
867 return -1;
870 /* Check interface is enabled by ifname statement. */
871 static int
872 rip_enable_if_lookup (const char *ifname)
874 unsigned int i;
875 char *str;
877 for (i = 0; i < vector_active (rip_enable_interface); i++)
878 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
879 if (strcmp (str, ifname) == 0)
880 return i;
881 return -1;
884 /* Add interface to rip_enable_if. */
885 static int
886 rip_enable_if_add (const char *ifname)
888 int ret;
890 ret = rip_enable_if_lookup (ifname);
891 if (ret >= 0)
892 return -1;
894 vector_set (rip_enable_interface, strdup (ifname));
896 rip_enable_apply_all(); /* TODOVJ */
898 return 1;
901 /* Delete interface from rip_enable_if. */
902 static int
903 rip_enable_if_delete (const char *ifname)
905 int index;
906 char *str;
908 index = rip_enable_if_lookup (ifname);
909 if (index < 0)
910 return -1;
912 str = vector_slot (rip_enable_interface, index);
913 free (str);
914 vector_unset (rip_enable_interface, index);
916 rip_enable_apply_all(); /* TODOVJ */
918 return 1;
921 /* Join to multicast group and send request to the interface. */
922 static int
923 rip_interface_wakeup (struct thread *t)
925 struct interface *ifp;
926 struct rip_interface *ri;
928 /* Get interface. */
929 ifp = THREAD_ARG (t);
931 ri = ifp->info;
932 ri->t_wakeup = NULL;
934 /* Join to multicast group. */
935 if (rip_multicast_join (ifp, rip->sock) < 0)
937 zlog_err ("multicast join failed, interface %s not running", ifp->name);
938 return 0;
941 /* Set running flag. */
942 ri->running = 1;
944 /* Send RIP request to the interface. */
945 rip_request_interface (ifp);
947 return 0;
950 int rip_redistribute_check (int);
952 static void
953 rip_connect_set (struct interface *ifp, int set)
955 struct listnode *node, *nnode;
956 struct connected *connected;
957 struct prefix_ipv4 address;
959 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
961 struct prefix *p;
962 p = connected->address;
964 if (p->family != AF_INET)
965 continue;
967 address.family = AF_INET;
968 address.prefix = p->u.prefix4;
969 address.prefixlen = p->prefixlen;
970 apply_mask_ipv4 (&address);
972 if (set) {
973 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
974 if ((rip_enable_if_lookup(connected->ifp->name) >= 0) ||
975 (rip_enable_network_lookup2(connected) >= 0))
976 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
977 &address, connected->ifp->ifindex,
978 NULL, 0, 0);
979 } else
981 rip_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
982 &address, connected->ifp->ifindex);
983 if (rip_redistribute_check (ZEBRA_ROUTE_CONNECT))
984 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_REDISTRIBUTE,
985 &address, connected->ifp->ifindex,
986 NULL, 0, 0);
991 /* Update interface status. */
992 void
993 rip_enable_apply (struct interface *ifp)
995 int ret;
996 struct rip_interface *ri = NULL;
998 /* Check interface. */
999 if (! if_is_operative (ifp))
1000 return;
1002 ri = ifp->info;
1004 /* Check network configuration. */
1005 ret = rip_enable_network_lookup_if (ifp);
1007 /* If the interface is matched. */
1008 if (ret > 0)
1009 ri->enable_network = 1;
1010 else
1011 ri->enable_network = 0;
1013 /* Check interface name configuration. */
1014 ret = rip_enable_if_lookup (ifp->name);
1015 if (ret >= 0)
1016 ri->enable_interface = 1;
1017 else
1018 ri->enable_interface = 0;
1020 /* any interface MUST have an IPv4 address */
1021 if ( ! rip_if_ipv4_address_check (ifp) )
1023 ri->enable_network = 0;
1024 ri->enable_interface = 0;
1027 /* Update running status of the interface. */
1028 if (ri->enable_network || ri->enable_interface)
1031 if (IS_RIP_DEBUG_EVENT)
1032 zlog_debug ("turn on %s", ifp->name);
1034 /* Add interface wake up thread. */
1035 if (! ri->t_wakeup)
1036 ri->t_wakeup = thread_add_timer (master, rip_interface_wakeup,
1037 ifp, 1);
1038 rip_connect_set (ifp, 1);
1041 else
1043 if (ri->running)
1045 /* Might as well clean up the route table as well
1046 * rip_if_down sets to 0 ri->running, and displays "turn off %s"
1047 **/
1048 rip_if_down(ifp);
1050 rip_connect_set (ifp, 0);
1055 /* Apply network configuration to all interface. */
1056 void
1057 rip_enable_apply_all ()
1059 struct interface *ifp;
1060 struct listnode *node, *nnode;
1062 /* Check each interface. */
1063 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
1064 rip_enable_apply (ifp);
1068 rip_neighbor_lookup (struct sockaddr_in *from)
1070 struct prefix_ipv4 p;
1071 struct route_node *node;
1073 memset (&p, 0, sizeof (struct prefix_ipv4));
1074 p.family = AF_INET;
1075 p.prefix = from->sin_addr;
1076 p.prefixlen = IPV4_MAX_BITLEN;
1078 node = route_node_lookup (rip->neighbor, (struct prefix *) &p);
1079 if (node)
1081 route_unlock_node (node);
1082 return 1;
1084 return 0;
1087 /* Add new RIP neighbor to the neighbor tree. */
1088 static int
1089 rip_neighbor_add (struct prefix_ipv4 *p)
1091 struct route_node *node;
1093 node = route_node_get (rip->neighbor, (struct prefix *) p);
1095 if (node->info)
1096 return -1;
1098 node->info = rip->neighbor;
1100 return 0;
1103 /* Delete RIP neighbor from the neighbor tree. */
1104 static int
1105 rip_neighbor_delete (struct prefix_ipv4 *p)
1107 struct route_node *node;
1109 /* Lock for look up. */
1110 node = route_node_lookup (rip->neighbor, (struct prefix *) p);
1111 if (! node)
1112 return -1;
1114 node->info = NULL;
1116 /* Unlock lookup lock. */
1117 route_unlock_node (node);
1119 /* Unlock real neighbor information lock. */
1120 route_unlock_node (node);
1122 return 0;
1125 /* Clear all network and neighbor configuration. */
1126 void
1127 rip_clean_network ()
1129 unsigned int i;
1130 char *str;
1131 struct route_node *rn;
1133 /* rip_enable_network. */
1134 for (rn = route_top (rip_enable_network); rn; rn = route_next (rn))
1135 if (rn->info)
1137 rn->info = NULL;
1138 route_unlock_node (rn);
1141 /* rip_enable_interface. */
1142 for (i = 0; i < vector_active (rip_enable_interface); i++)
1143 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
1145 free (str);
1146 vector_slot (rip_enable_interface, i) = NULL;
1150 /* Utility function for looking up passive interface settings. */
1151 static int
1152 rip_passive_nondefault_lookup (const char *ifname)
1154 unsigned int i;
1155 char *str;
1157 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1158 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1159 if (strcmp (str, ifname) == 0)
1160 return i;
1161 return -1;
1164 void
1165 rip_passive_interface_apply (struct interface *ifp)
1167 struct rip_interface *ri;
1169 ri = ifp->info;
1171 ri->passive = ((rip_passive_nondefault_lookup (ifp->name) < 0) ?
1172 passive_default : !passive_default);
1174 if (IS_RIP_DEBUG_ZEBRA)
1175 zlog_debug ("interface %s: passive = %d",ifp->name,ri->passive);
1178 static void
1179 rip_passive_interface_apply_all (void)
1181 struct interface *ifp;
1182 struct listnode *node, *nnode;
1184 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
1185 rip_passive_interface_apply (ifp);
1188 /* Passive interface. */
1189 static int
1190 rip_passive_nondefault_set (struct vty *vty, const char *ifname)
1192 if (rip_passive_nondefault_lookup (ifname) >= 0)
1193 return CMD_WARNING;
1195 vector_set (Vrip_passive_nondefault, strdup (ifname));
1197 rip_passive_interface_apply_all ();
1199 return CMD_SUCCESS;
1202 static int
1203 rip_passive_nondefault_unset (struct vty *vty, const char *ifname)
1205 int i;
1206 char *str;
1208 i = rip_passive_nondefault_lookup (ifname);
1209 if (i < 0)
1210 return CMD_WARNING;
1212 str = vector_slot (Vrip_passive_nondefault, i);
1213 free (str);
1214 vector_unset (Vrip_passive_nondefault, i);
1216 rip_passive_interface_apply_all ();
1218 return CMD_SUCCESS;
1221 /* Free all configured RIP passive-interface settings. */
1222 void
1223 rip_passive_nondefault_clean (void)
1225 unsigned int i;
1226 char *str;
1228 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1229 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1231 free (str);
1232 vector_slot (Vrip_passive_nondefault, i) = NULL;
1234 rip_passive_interface_apply_all ();
1237 /* RIP enable network or interface configuration. */
1238 DEFUN (rip_network,
1239 rip_network_cmd,
1240 "network (A.B.C.D/M|WORD)",
1241 "Enable routing on an IP network\n"
1242 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1243 "Interface name\n")
1245 int ret;
1246 struct prefix_ipv4 p;
1248 ret = str2prefix_ipv4 (argv[0], &p);
1250 if (ret)
1251 ret = rip_enable_network_add ((struct prefix *) &p);
1252 else
1253 ret = rip_enable_if_add (argv[0]);
1255 if (ret < 0)
1257 vty_out (vty, "There is a same network configuration %s%s", argv[0],
1258 VTY_NEWLINE);
1259 return CMD_WARNING;
1262 return CMD_SUCCESS;
1265 /* RIP enable network or interface configuration. */
1266 DEFUN (no_rip_network,
1267 no_rip_network_cmd,
1268 "no network (A.B.C.D/M|WORD)",
1269 NO_STR
1270 "Enable routing on an IP network\n"
1271 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1272 "Interface name\n")
1274 int ret;
1275 struct prefix_ipv4 p;
1277 ret = str2prefix_ipv4 (argv[0], &p);
1279 if (ret)
1280 ret = rip_enable_network_delete ((struct prefix *) &p);
1281 else
1282 ret = rip_enable_if_delete (argv[0]);
1284 if (ret < 0)
1286 vty_out (vty, "Can't find network configuration %s%s", argv[0],
1287 VTY_NEWLINE);
1288 return CMD_WARNING;
1291 return CMD_SUCCESS;
1294 /* RIP neighbor configuration set. */
1295 DEFUN (rip_neighbor,
1296 rip_neighbor_cmd,
1297 "neighbor A.B.C.D",
1298 "Specify a neighbor router\n"
1299 "Neighbor address\n")
1301 int ret;
1302 struct prefix_ipv4 p;
1304 ret = str2prefix_ipv4 (argv[0], &p);
1306 if (ret <= 0)
1308 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1309 return CMD_WARNING;
1312 rip_neighbor_add (&p);
1314 return CMD_SUCCESS;
1317 /* RIP neighbor configuration unset. */
1318 DEFUN (no_rip_neighbor,
1319 no_rip_neighbor_cmd,
1320 "no neighbor A.B.C.D",
1321 NO_STR
1322 "Specify a neighbor router\n"
1323 "Neighbor address\n")
1325 int ret;
1326 struct prefix_ipv4 p;
1328 ret = str2prefix_ipv4 (argv[0], &p);
1330 if (ret <= 0)
1332 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1333 return CMD_WARNING;
1336 rip_neighbor_delete (&p);
1338 return CMD_SUCCESS;
1341 DEFUN (ip_rip_receive_version,
1342 ip_rip_receive_version_cmd,
1343 "ip rip receive version (1|2)",
1344 IP_STR
1345 "Routing Information Protocol\n"
1346 "Advertisement reception\n"
1347 "Version control\n"
1348 "RIP version 1\n"
1349 "RIP version 2\n")
1351 struct interface *ifp;
1352 struct rip_interface *ri;
1354 ifp = (struct interface *)vty->index;
1355 ri = ifp->info;
1357 /* Version 1. */
1358 if (atoi (argv[0]) == 1)
1360 ri->ri_receive = RI_RIP_VERSION_1;
1361 return CMD_SUCCESS;
1363 if (atoi (argv[0]) == 2)
1365 ri->ri_receive = RI_RIP_VERSION_2;
1366 return CMD_SUCCESS;
1368 return CMD_WARNING;
1371 DEFUN (ip_rip_receive_version_1,
1372 ip_rip_receive_version_1_cmd,
1373 "ip rip receive version 1 2",
1374 IP_STR
1375 "Routing Information Protocol\n"
1376 "Advertisement reception\n"
1377 "Version control\n"
1378 "RIP version 1\n"
1379 "RIP version 2\n")
1381 struct interface *ifp;
1382 struct rip_interface *ri;
1384 ifp = (struct interface *)vty->index;
1385 ri = ifp->info;
1387 /* Version 1 and 2. */
1388 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1389 return CMD_SUCCESS;
1392 DEFUN (ip_rip_receive_version_2,
1393 ip_rip_receive_version_2_cmd,
1394 "ip rip receive version 2 1",
1395 IP_STR
1396 "Routing Information Protocol\n"
1397 "Advertisement reception\n"
1398 "Version control\n"
1399 "RIP version 2\n"
1400 "RIP version 1\n")
1402 struct interface *ifp;
1403 struct rip_interface *ri;
1405 ifp = (struct interface *)vty->index;
1406 ri = ifp->info;
1408 /* Version 1 and 2. */
1409 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1410 return CMD_SUCCESS;
1413 DEFUN (no_ip_rip_receive_version,
1414 no_ip_rip_receive_version_cmd,
1415 "no ip rip receive version",
1416 NO_STR
1417 IP_STR
1418 "Routing Information Protocol\n"
1419 "Advertisement reception\n"
1420 "Version control\n")
1422 struct interface *ifp;
1423 struct rip_interface *ri;
1425 ifp = (struct interface *)vty->index;
1426 ri = ifp->info;
1428 ri->ri_receive = RI_RIP_UNSPEC;
1429 return CMD_SUCCESS;
1432 ALIAS (no_ip_rip_receive_version,
1433 no_ip_rip_receive_version_num_cmd,
1434 "no ip rip receive version (1|2)",
1435 NO_STR
1436 IP_STR
1437 "Routing Information Protocol\n"
1438 "Advertisement reception\n"
1439 "Version control\n"
1440 "Version 1\n"
1441 "Version 2\n")
1443 DEFUN (ip_rip_send_version,
1444 ip_rip_send_version_cmd,
1445 "ip rip send version (1|2)",
1446 IP_STR
1447 "Routing Information Protocol\n"
1448 "Advertisement transmission\n"
1449 "Version control\n"
1450 "RIP version 1\n"
1451 "RIP version 2\n")
1453 struct interface *ifp;
1454 struct rip_interface *ri;
1456 ifp = (struct interface *)vty->index;
1457 ri = ifp->info;
1459 /* Version 1. */
1460 if (atoi (argv[0]) == 1)
1462 ri->ri_send = RI_RIP_VERSION_1;
1463 return CMD_SUCCESS;
1465 if (atoi (argv[0]) == 2)
1467 ri->ri_send = RI_RIP_VERSION_2;
1468 return CMD_SUCCESS;
1470 return CMD_WARNING;
1473 DEFUN (ip_rip_send_version_1,
1474 ip_rip_send_version_1_cmd,
1475 "ip rip send version 1 2",
1476 IP_STR
1477 "Routing Information Protocol\n"
1478 "Advertisement transmission\n"
1479 "Version control\n"
1480 "RIP version 1\n"
1481 "RIP version 2\n")
1483 struct interface *ifp;
1484 struct rip_interface *ri;
1486 ifp = (struct interface *)vty->index;
1487 ri = ifp->info;
1489 /* Version 1 and 2. */
1490 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1491 return CMD_SUCCESS;
1494 DEFUN (ip_rip_send_version_2,
1495 ip_rip_send_version_2_cmd,
1496 "ip rip send version 2 1",
1497 IP_STR
1498 "Routing Information Protocol\n"
1499 "Advertisement transmission\n"
1500 "Version control\n"
1501 "RIP version 2\n"
1502 "RIP version 1\n")
1504 struct interface *ifp;
1505 struct rip_interface *ri;
1507 ifp = (struct interface *)vty->index;
1508 ri = ifp->info;
1510 /* Version 1 and 2. */
1511 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1512 return CMD_SUCCESS;
1515 DEFUN (no_ip_rip_send_version,
1516 no_ip_rip_send_version_cmd,
1517 "no ip rip send version",
1518 NO_STR
1519 IP_STR
1520 "Routing Information Protocol\n"
1521 "Advertisement transmission\n"
1522 "Version control\n")
1524 struct interface *ifp;
1525 struct rip_interface *ri;
1527 ifp = (struct interface *)vty->index;
1528 ri = ifp->info;
1530 ri->ri_send = RI_RIP_UNSPEC;
1531 return CMD_SUCCESS;
1534 ALIAS (no_ip_rip_send_version,
1535 no_ip_rip_send_version_num_cmd,
1536 "no ip rip send version (1|2)",
1537 NO_STR
1538 IP_STR
1539 "Routing Information Protocol\n"
1540 "Advertisement transmission\n"
1541 "Version control\n"
1542 "Version 1\n"
1543 "Version 2\n")
1545 DEFUN (ip_rip_authentication_mode,
1546 ip_rip_authentication_mode_cmd,
1547 "ip rip authentication mode (md5|text)",
1548 IP_STR
1549 "Routing Information Protocol\n"
1550 "Authentication control\n"
1551 "Authentication mode\n"
1552 "Keyed message digest\n"
1553 "Clear text authentication\n")
1555 struct interface *ifp;
1556 struct rip_interface *ri;
1557 int auth_type;
1559 ifp = (struct interface *)vty->index;
1560 ri = ifp->info;
1562 if ( (argc < 1) || (argc > 2) )
1564 vty_out (vty, "incorrect argument count%s", VTY_NEWLINE);
1565 return CMD_WARNING;
1568 if (strncmp ("md5", argv[0], strlen (argv[0])) == 0)
1569 auth_type = RIP_AUTH_MD5;
1570 else if (strncmp ("text", argv[0], strlen (argv[0])) == 0)
1571 auth_type = RIP_AUTH_SIMPLE_PASSWORD;
1572 else
1574 vty_out (vty, "mode should be md5 or text%s", VTY_NEWLINE);
1575 return CMD_WARNING;
1578 if (argc == 1)
1580 ri->auth_type = auth_type;
1581 return CMD_SUCCESS;
1584 if ( (argc == 2) && (auth_type != RIP_AUTH_MD5) )
1586 vty_out (vty, "auth length argument only valid for md5%s", VTY_NEWLINE);
1587 return CMD_WARNING;
1590 if (strncmp ("r", argv[1], 1) == 0)
1591 ri->md5_auth_len = RIP_AUTH_MD5_SIZE;
1592 else if (strncmp ("o", argv[1], 1) == 0)
1593 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1594 else
1595 return CMD_WARNING;
1597 ri->auth_type = auth_type;
1599 return CMD_SUCCESS;
1602 ALIAS (ip_rip_authentication_mode,
1603 ip_rip_authentication_mode_authlen_cmd,
1604 "ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1605 IP_STR
1606 "Routing Information Protocol\n"
1607 "Authentication control\n"
1608 "Authentication mode\n"
1609 "Keyed message digest\n"
1610 "Clear text authentication\n"
1611 "MD5 authentication data length\n"
1612 "RFC compatible\n"
1613 "Old ripd compatible\n")
1615 DEFUN (no_ip_rip_authentication_mode,
1616 no_ip_rip_authentication_mode_cmd,
1617 "no ip rip authentication mode",
1618 NO_STR
1619 IP_STR
1620 "Routing Information Protocol\n"
1621 "Authentication control\n"
1622 "Authentication mode\n")
1624 struct interface *ifp;
1625 struct rip_interface *ri;
1627 ifp = (struct interface *)vty->index;
1628 ri = ifp->info;
1630 ri->auth_type = RIP_NO_AUTH;
1631 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1633 return CMD_SUCCESS;
1636 ALIAS (no_ip_rip_authentication_mode,
1637 no_ip_rip_authentication_mode_type_cmd,
1638 "no ip rip authentication mode (md5|text)",
1639 NO_STR
1640 IP_STR
1641 "Routing Information Protocol\n"
1642 "Authentication control\n"
1643 "Authentication mode\n"
1644 "Keyed message digest\n"
1645 "Clear text authentication\n")
1647 ALIAS (no_ip_rip_authentication_mode,
1648 no_ip_rip_authentication_mode_type_authlen_cmd,
1649 "no ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1650 NO_STR
1651 IP_STR
1652 "Routing Information Protocol\n"
1653 "Authentication control\n"
1654 "Authentication mode\n"
1655 "Keyed message digest\n"
1656 "Clear text authentication\n"
1657 "MD5 authentication data length\n"
1658 "RFC compatible\n"
1659 "Old ripd compatible\n")
1661 DEFUN (ip_rip_authentication_string,
1662 ip_rip_authentication_string_cmd,
1663 "ip rip authentication string LINE",
1664 IP_STR
1665 "Routing Information Protocol\n"
1666 "Authentication control\n"
1667 "Authentication string\n"
1668 "Authentication string\n")
1670 struct interface *ifp;
1671 struct rip_interface *ri;
1673 ifp = (struct interface *)vty->index;
1674 ri = ifp->info;
1676 if (strlen (argv[0]) > 16)
1678 vty_out (vty, "%% RIPv2 authentication string must be shorter than 16%s",
1679 VTY_NEWLINE);
1680 return CMD_WARNING;
1683 if (ri->key_chain)
1685 vty_out (vty, "%% key-chain configuration exists%s", VTY_NEWLINE);
1686 return CMD_WARNING;
1689 if (ri->auth_str)
1690 free (ri->auth_str);
1692 ri->auth_str = strdup (argv[0]);
1694 return CMD_SUCCESS;
1697 DEFUN (no_ip_rip_authentication_string,
1698 no_ip_rip_authentication_string_cmd,
1699 "no ip rip authentication string",
1700 NO_STR
1701 IP_STR
1702 "Routing Information Protocol\n"
1703 "Authentication control\n"
1704 "Authentication string\n")
1706 struct interface *ifp;
1707 struct rip_interface *ri;
1709 ifp = (struct interface *)vty->index;
1710 ri = ifp->info;
1712 if (ri->auth_str)
1713 free (ri->auth_str);
1715 ri->auth_str = NULL;
1717 return CMD_SUCCESS;
1720 ALIAS (no_ip_rip_authentication_string,
1721 no_ip_rip_authentication_string2_cmd,
1722 "no ip rip authentication string LINE",
1723 NO_STR
1724 IP_STR
1725 "Routing Information Protocol\n"
1726 "Authentication control\n"
1727 "Authentication string\n"
1728 "Authentication string\n")
1730 DEFUN (ip_rip_authentication_key_chain,
1731 ip_rip_authentication_key_chain_cmd,
1732 "ip rip authentication key-chain LINE",
1733 IP_STR
1734 "Routing Information Protocol\n"
1735 "Authentication control\n"
1736 "Authentication key-chain\n"
1737 "name of key-chain\n")
1739 struct interface *ifp;
1740 struct rip_interface *ri;
1742 ifp = (struct interface *) vty->index;
1743 ri = ifp->info;
1745 if (ri->auth_str)
1747 vty_out (vty, "%% authentication string configuration exists%s",
1748 VTY_NEWLINE);
1749 return CMD_WARNING;
1752 if (ri->key_chain)
1753 free (ri->key_chain);
1755 ri->key_chain = strdup (argv[0]);
1757 return CMD_SUCCESS;
1760 DEFUN (no_ip_rip_authentication_key_chain,
1761 no_ip_rip_authentication_key_chain_cmd,
1762 "no ip rip authentication key-chain",
1763 NO_STR
1764 IP_STR
1765 "Routing Information Protocol\n"
1766 "Authentication control\n"
1767 "Authentication key-chain\n")
1769 struct interface *ifp;
1770 struct rip_interface *ri;
1772 ifp = (struct interface *) vty->index;
1773 ri = ifp->info;
1775 if (ri->key_chain)
1776 free (ri->key_chain);
1778 ri->key_chain = NULL;
1780 return CMD_SUCCESS;
1783 ALIAS (no_ip_rip_authentication_key_chain,
1784 no_ip_rip_authentication_key_chain2_cmd,
1785 "no ip rip authentication key-chain LINE",
1786 NO_STR
1787 IP_STR
1788 "Routing Information Protocol\n"
1789 "Authentication control\n"
1790 "Authentication key-chain\n"
1791 "name of key-chain\n")
1793 /* CHANGED: ip rip split-horizon
1794 Cisco and Zebra's command is
1795 ip split-horizon
1797 DEFUN (ip_rip_split_horizon,
1798 ip_rip_split_horizon_cmd,
1799 "ip rip split-horizon",
1800 IP_STR
1801 "Routing Information Protocol\n"
1802 "Perform split horizon\n")
1804 struct interface *ifp;
1805 struct rip_interface *ri;
1807 ifp = vty->index;
1808 ri = ifp->info;
1810 ri->split_horizon = RIP_SPLIT_HORIZON;
1811 return CMD_SUCCESS;
1814 DEFUN (ip_rip_split_horizon_poisoned_reverse,
1815 ip_rip_split_horizon_poisoned_reverse_cmd,
1816 "ip rip split-horizon poisoned-reverse",
1817 IP_STR
1818 "Routing Information Protocol\n"
1819 "Perform split horizon\n"
1820 "With poisoned-reverse\n")
1822 struct interface *ifp;
1823 struct rip_interface *ri;
1825 ifp = vty->index;
1826 ri = ifp->info;
1828 ri->split_horizon = RIP_SPLIT_HORIZON_POISONED_REVERSE;
1829 return CMD_SUCCESS;
1832 /* CHANGED: no ip rip split-horizon
1833 Cisco and Zebra's command is
1834 no ip split-horizon
1836 DEFUN (no_ip_rip_split_horizon,
1837 no_ip_rip_split_horizon_cmd,
1838 "no ip rip split-horizon",
1839 NO_STR
1840 IP_STR
1841 "Routing Information Protocol\n"
1842 "Perform split horizon\n")
1844 struct interface *ifp;
1845 struct rip_interface *ri;
1847 ifp = vty->index;
1848 ri = ifp->info;
1850 ri->split_horizon = RIP_NO_SPLIT_HORIZON;
1851 return CMD_SUCCESS;
1854 DEFUN (no_ip_rip_split_horizon_poisoned_reverse,
1855 no_ip_rip_split_horizon_poisoned_reverse_cmd,
1856 "no ip rip split-horizon poisoned-reverse",
1857 NO_STR
1858 IP_STR
1859 "Routing Information Protocol\n"
1860 "Perform split horizon\n"
1861 "With poisoned-reverse\n")
1863 struct interface *ifp;
1864 struct rip_interface *ri;
1866 ifp = vty->index;
1867 ri = ifp->info;
1869 switch( ri->split_horizon )
1871 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1872 ri->split_horizon = RIP_SPLIT_HORIZON;
1873 default:
1874 break;
1877 return CMD_SUCCESS;
1880 DEFUN (rip_passive_interface,
1881 rip_passive_interface_cmd,
1882 "passive-interface (IFNAME|default)",
1883 "Suppress routing updates on an interface\n"
1884 "Interface name\n"
1885 "default for all interfaces\n")
1887 const char *ifname = argv[0];
1889 if (!strcmp(ifname,"default")) {
1890 passive_default = 1;
1891 rip_passive_nondefault_clean();
1892 return CMD_SUCCESS;
1894 if (passive_default)
1895 return rip_passive_nondefault_unset (vty, ifname);
1896 else
1897 return rip_passive_nondefault_set (vty, ifname);
1900 DEFUN (no_rip_passive_interface,
1901 no_rip_passive_interface_cmd,
1902 "no passive-interface (IFNAME|default)",
1903 NO_STR
1904 "Suppress routing updates on an interface\n"
1905 "Interface name\n"
1906 "default for all interfaces\n")
1908 const char *ifname = argv[0];
1910 if (!strcmp(ifname,"default")) {
1911 passive_default = 0;
1912 rip_passive_nondefault_clean();
1913 return CMD_SUCCESS;
1915 if (passive_default)
1916 return rip_passive_nondefault_set (vty, ifname);
1917 else
1918 return rip_passive_nondefault_unset (vty, ifname);
1921 /* Write rip configuration of each interface. */
1922 static int
1923 rip_interface_config_write (struct vty *vty)
1925 struct listnode *node;
1926 struct interface *ifp;
1928 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1930 struct rip_interface *ri;
1932 ri = ifp->info;
1934 /* Do not display the interface if there is no
1935 * configuration about it.
1937 if ((!ifp->desc) &&
1938 (ri->split_horizon == ri->split_horizon_default) &&
1939 (ri->ri_send == RI_RIP_UNSPEC) &&
1940 (ri->ri_receive == RI_RIP_UNSPEC) &&
1941 (ri->auth_type != RIP_AUTH_MD5) &&
1942 (ri->md5_auth_len != RIP_AUTH_MD5_SIZE) &&
1943 (!ri->auth_str) &&
1944 (!ri->key_chain) )
1945 continue;
1947 vty_out (vty, "interface %s%s", ifp->name,
1948 VTY_NEWLINE);
1950 if (ifp->desc)
1951 vty_out (vty, " description %s%s", ifp->desc,
1952 VTY_NEWLINE);
1954 /* Split horizon. */
1955 if (ri->split_horizon != ri->split_horizon_default)
1957 switch (ri->split_horizon) {
1958 case RIP_SPLIT_HORIZON:
1959 vty_out (vty, " ip rip split-horizon%s", VTY_NEWLINE);
1960 break;
1961 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1962 vty_out (vty, " ip rip split-horizon poisoned-reverse%s",
1963 VTY_NEWLINE);
1964 break;
1965 case RIP_NO_SPLIT_HORIZON:
1966 default:
1967 vty_out (vty, " no ip rip split-horizon%s", VTY_NEWLINE);
1968 break;
1972 /* RIP version setting. */
1973 if (ri->ri_send != RI_RIP_UNSPEC)
1974 vty_out (vty, " ip rip send version %s%s",
1975 lookup (ri_version_msg, ri->ri_send),
1976 VTY_NEWLINE);
1978 if (ri->ri_receive != RI_RIP_UNSPEC)
1979 vty_out (vty, " ip rip receive version %s%s",
1980 lookup (ri_version_msg, ri->ri_receive),
1981 VTY_NEWLINE);
1983 /* RIP authentication. */
1984 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
1985 vty_out (vty, " ip rip authentication mode text%s", VTY_NEWLINE);
1987 if (ri->auth_type == RIP_AUTH_MD5)
1989 vty_out (vty, " ip rip authentication mode md5");
1990 if (ri->md5_auth_len == RIP_AUTH_MD5_COMPAT_SIZE)
1991 vty_out (vty, " auth-length old-ripd");
1992 else
1993 vty_out (vty, " auth-length rfc");
1994 vty_out (vty, "%s", VTY_NEWLINE);
1997 if (ri->auth_str)
1998 vty_out (vty, " ip rip authentication string %s%s",
1999 ri->auth_str, VTY_NEWLINE);
2001 if (ri->key_chain)
2002 vty_out (vty, " ip rip authentication key-chain %s%s",
2003 ri->key_chain, VTY_NEWLINE);
2005 vty_out (vty, "!%s", VTY_NEWLINE);
2007 return 0;
2011 config_write_rip_network (struct vty *vty, int config_mode)
2013 unsigned int i;
2014 char *ifname;
2015 struct route_node *node;
2017 /* Network type RIP enable interface statement. */
2018 for (node = route_top (rip_enable_network); node; node = route_next (node))
2019 if (node->info)
2020 vty_out (vty, "%s%s/%d%s",
2021 config_mode ? " network " : " ",
2022 inet_ntoa (node->p.u.prefix4),
2023 node->p.prefixlen,
2024 VTY_NEWLINE);
2026 /* Interface name RIP enable statement. */
2027 for (i = 0; i < vector_active (rip_enable_interface); i++)
2028 if ((ifname = vector_slot (rip_enable_interface, i)) != NULL)
2029 vty_out (vty, "%s%s%s",
2030 config_mode ? " network " : " ",
2031 ifname,
2032 VTY_NEWLINE);
2034 /* RIP neighbors listing. */
2035 for (node = route_top (rip->neighbor); node; node = route_next (node))
2036 if (node->info)
2037 vty_out (vty, "%s%s%s",
2038 config_mode ? " neighbor " : " ",
2039 inet_ntoa (node->p.u.prefix4),
2040 VTY_NEWLINE);
2042 /* RIP passive interface listing. */
2043 if (config_mode) {
2044 if (passive_default)
2045 vty_out (vty, " passive-interface default%s", VTY_NEWLINE);
2046 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
2047 if ((ifname = vector_slot (Vrip_passive_nondefault, i)) != NULL)
2048 vty_out (vty, " %spassive-interface %s%s",
2049 (passive_default ? "no " : ""), ifname, VTY_NEWLINE);
2052 return 0;
2055 static struct cmd_node interface_node =
2057 INTERFACE_NODE,
2058 "%s(config-if)# ",
2062 /* Called when interface structure allocated. */
2063 static int
2064 rip_interface_new_hook (struct interface *ifp)
2066 ifp->info = rip_interface_new ();
2067 return 0;
2070 /* Called when interface structure deleted. */
2071 static int
2072 rip_interface_delete_hook (struct interface *ifp)
2074 XFREE (MTYPE_RIP_INTERFACE, ifp->info);
2075 ifp->info = NULL;
2076 return 0;
2079 /* Allocate and initialize interface vector. */
2080 void
2081 rip_if_init (void)
2083 /* Default initial size of interface vector. */
2084 if_init();
2085 if_add_hook (IF_NEW_HOOK, rip_interface_new_hook);
2086 if_add_hook (IF_DELETE_HOOK, rip_interface_delete_hook);
2088 /* RIP network init. */
2089 rip_enable_interface = vector_init (1);
2090 rip_enable_network = route_table_init ();
2092 /* RIP passive interface. */
2093 Vrip_passive_nondefault = vector_init (1);
2095 /* Install interface node. */
2096 install_node (&interface_node, rip_interface_config_write);
2098 /* Install commands. */
2099 install_element (CONFIG_NODE, &interface_cmd);
2100 install_element (CONFIG_NODE, &no_interface_cmd);
2101 install_default (INTERFACE_NODE);
2102 install_element (INTERFACE_NODE, &interface_desc_cmd);
2103 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
2104 install_element (RIP_NODE, &rip_network_cmd);
2105 install_element (RIP_NODE, &no_rip_network_cmd);
2106 install_element (RIP_NODE, &rip_neighbor_cmd);
2107 install_element (RIP_NODE, &no_rip_neighbor_cmd);
2109 install_element (RIP_NODE, &rip_passive_interface_cmd);
2110 install_element (RIP_NODE, &no_rip_passive_interface_cmd);
2112 install_element (INTERFACE_NODE, &ip_rip_send_version_cmd);
2113 install_element (INTERFACE_NODE, &ip_rip_send_version_1_cmd);
2114 install_element (INTERFACE_NODE, &ip_rip_send_version_2_cmd);
2115 install_element (INTERFACE_NODE, &no_ip_rip_send_version_cmd);
2116 install_element (INTERFACE_NODE, &no_ip_rip_send_version_num_cmd);
2118 install_element (INTERFACE_NODE, &ip_rip_receive_version_cmd);
2119 install_element (INTERFACE_NODE, &ip_rip_receive_version_1_cmd);
2120 install_element (INTERFACE_NODE, &ip_rip_receive_version_2_cmd);
2121 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_cmd);
2122 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_num_cmd);
2124 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_cmd);
2125 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_authlen_cmd);
2126 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_cmd);
2127 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_cmd);
2128 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_authlen_cmd);
2130 install_element (INTERFACE_NODE, &ip_rip_authentication_key_chain_cmd);
2131 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain_cmd);
2132 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain2_cmd);
2134 install_element (INTERFACE_NODE, &ip_rip_authentication_string_cmd);
2135 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string_cmd);
2136 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string2_cmd);
2138 install_element (INTERFACE_NODE, &ip_rip_split_horizon_cmd);
2139 install_element (INTERFACE_NODE, &ip_rip_split_horizon_poisoned_reverse_cmd);
2140 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_cmd);
2141 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_poisoned_reverse_cmd);