]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_interface.c
ospf6d: fix decimal area ID cli
[mirror_frr.git] / ripd / rip_interface.c
1 /* Interface related function for RIP.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 #include <zebra.h>
23
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"
38
39 #include "zebra/connected.h"
40
41 #include "ripd/ripd.h"
42 #include "ripd/rip_debug.h"
43 #include "ripd/rip_interface.h"
44
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);
52
53 const struct message ri_version_msg[] =
54 {
55 {RI_RIP_VERSION_1, "1"},
56 {RI_RIP_VERSION_2, "2"},
57 {RI_RIP_VERSION_1_AND_2, "1 2"},
58 {RI_RIP_VERSION_NONE, "none"},
59 };
60
61 extern struct zebra_privs_t ripd_privs;
62
63 /* RIP enabled network vector. */
64 vector rip_enable_interface;
65
66 /* RIP enabled interface table. */
67 struct route_table *rip_enable_network;
68
69 /* Vector to store passive-interface name. */
70 static int passive_default; /* are we in passive-interface default mode? */
71 vector Vrip_passive_nondefault;
72
73 /* Join to the RIP version 2 multicast group. */
74 static int
75 ipv4_multicast_join (int sock,
76 struct in_addr group,
77 struct in_addr ifa,
78 ifindex_t ifindex)
79 {
80 int ret;
81
82 ret = setsockopt_ipv4_multicast (sock,
83 IP_ADD_MEMBERSHIP,
84 ifa,
85 group.s_addr,
86 ifindex);
87
88 if (ret < 0)
89 zlog (NULL, LOG_INFO, "can't setsockopt IP_ADD_MEMBERSHIP %s",
90 safe_strerror (errno));
91
92 return ret;
93 }
94
95 /* Leave from the RIP version 2 multicast group. */
96 static int
97 ipv4_multicast_leave (int sock,
98 struct in_addr group,
99 struct in_addr ifa,
100 ifindex_t ifindex)
101 {
102 int ret;
103
104 ret = setsockopt_ipv4_multicast (sock,
105 IP_DROP_MEMBERSHIP,
106 ifa,
107 group.s_addr,
108 ifindex);
109
110 if (ret < 0)
111 zlog (NULL, LOG_INFO, "can't setsockopt IP_DROP_MEMBERSHIP");
112
113 return ret;
114 }
115
116 static void rip_interface_reset (struct rip_interface *);
117
118 /* Allocate new RIP's interface configuration. */
119 static struct rip_interface *
120 rip_interface_new (void)
121 {
122 struct rip_interface *ri;
123
124 ri = XCALLOC (MTYPE_RIP_INTERFACE, sizeof (struct rip_interface));
125
126 rip_interface_reset (ri);
127
128 return ri;
129 }
130
131 void
132 rip_interface_multicast_set (int sock, struct connected *connected)
133 {
134 struct in_addr addr;
135
136 assert (connected != NULL);
137
138 addr = CONNECTED_ID(connected)->u.prefix4;
139
140 if (setsockopt_ipv4_multicast_if (sock, addr, connected->ifp->ifindex) < 0)
141 {
142 zlog_warn ("Can't setsockopt IP_MULTICAST_IF on fd %d to "
143 "ifindex %d for interface %s",
144 sock, connected->ifp->ifindex,
145 connected->ifp->name);
146 }
147
148 return;
149 }
150
151 /* Send RIP request packet to specified interface. */
152 static void
153 rip_request_interface_send (struct interface *ifp, u_char version)
154 {
155 struct sockaddr_in to;
156
157 /* RIPv2 support multicast. */
158 if (version == RIPv2 && if_is_multicast (ifp))
159 {
160
161 if (IS_RIP_DEBUG_EVENT)
162 zlog_debug ("multicast request on %s", ifp->name);
163
164 rip_request_send (NULL, ifp, version, NULL);
165 return;
166 }
167
168 /* RIPv1 and non multicast interface. */
169 if (if_is_pointopoint (ifp) || if_is_broadcast (ifp))
170 {
171 struct listnode *cnode, *cnnode;
172 struct connected *connected;
173
174 if (IS_RIP_DEBUG_EVENT)
175 zlog_debug ("broadcast request to %s", ifp->name);
176
177 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, connected))
178 {
179 if (connected->address->family == AF_INET)
180 {
181 memset (&to, 0, sizeof (struct sockaddr_in));
182 to.sin_port = htons (RIP_PORT_DEFAULT);
183 if (connected->destination)
184 /* use specified broadcast or peer destination addr */
185 to.sin_addr = connected->destination->u.prefix4;
186 else if (connected->address->prefixlen < IPV4_MAX_PREFIXLEN)
187 /* calculate the appropriate broadcast address */
188 to.sin_addr.s_addr =
189 ipv4_broadcast_addr(connected->address->u.prefix4.s_addr,
190 connected->address->prefixlen);
191 else
192 /* do not know where to send the packet */
193 continue;
194
195 if (IS_RIP_DEBUG_EVENT)
196 zlog_debug ("SEND request to %s", inet_ntoa (to.sin_addr));
197
198 rip_request_send (&to, ifp, version, connected);
199 }
200 }
201 }
202 }
203
204 /* This will be executed when interface goes up. */
205 static void
206 rip_request_interface (struct interface *ifp)
207 {
208 struct rip_interface *ri;
209
210 /* In default ripd doesn't send RIP_REQUEST to the loopback interface. */
211 if (if_is_loopback (ifp))
212 return;
213
214 /* If interface is down, don't send RIP packet. */
215 if (! if_is_operative (ifp))
216 return;
217
218 /* Fetch RIP interface information. */
219 ri = ifp->info;
220
221
222 /* If there is no version configuration in the interface,
223 use rip's version setting. */
224 {
225 int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ?
226 rip->version_send : ri->ri_send);
227 if (vsend & RIPv1)
228 rip_request_interface_send (ifp, RIPv1);
229 if (vsend & RIPv2)
230 rip_request_interface_send (ifp, RIPv2);
231 }
232 }
233
234 #if 0
235 /* Send RIP request to the neighbor. */
236 static void
237 rip_request_neighbor (struct in_addr addr)
238 {
239 struct sockaddr_in to;
240
241 memset (&to, 0, sizeof (struct sockaddr_in));
242 to.sin_port = htons (RIP_PORT_DEFAULT);
243 to.sin_addr = addr;
244
245 rip_request_send (&to, NULL, rip->version_send, NULL);
246 }
247
248 /* Request routes at all interfaces. */
249 static void
250 rip_request_neighbor_all (void)
251 {
252 struct route_node *rp;
253
254 if (! rip)
255 return;
256
257 if (IS_RIP_DEBUG_EVENT)
258 zlog_debug ("request to the all neighbor");
259
260 /* Send request to all neighbor. */
261 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
262 if (rp->info)
263 rip_request_neighbor (rp->p.u.prefix4);
264 }
265 #endif
266
267 /* Multicast packet receive socket. */
268 static int
269 rip_multicast_join (struct interface *ifp, int sock)
270 {
271 struct listnode *cnode;
272 struct connected *ifc;
273
274 if (if_is_operative (ifp) && if_is_multicast (ifp))
275 {
276 if (IS_RIP_DEBUG_EVENT)
277 zlog_debug ("multicast join at %s", ifp->name);
278
279 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, ifc))
280 {
281 struct prefix_ipv4 *p;
282 struct in_addr group;
283
284 p = (struct prefix_ipv4 *) ifc->address;
285
286 if (p->family != AF_INET)
287 continue;
288
289 group.s_addr = htonl (INADDR_RIP_GROUP);
290 if (ipv4_multicast_join (sock, group, p->prefix, ifp->ifindex) < 0)
291 return -1;
292 else
293 return 0;
294 }
295 }
296 return 0;
297 }
298
299 /* Leave from multicast group. */
300 static void
301 rip_multicast_leave (struct interface *ifp, int sock)
302 {
303 struct listnode *cnode;
304 struct connected *connected;
305
306 if (if_is_up (ifp) && if_is_multicast (ifp))
307 {
308 if (IS_RIP_DEBUG_EVENT)
309 zlog_debug ("multicast leave from %s", ifp->name);
310
311 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
312 {
313 struct prefix_ipv4 *p;
314 struct in_addr group;
315
316 p = (struct prefix_ipv4 *) connected->address;
317
318 if (p->family != AF_INET)
319 continue;
320
321 group.s_addr = htonl (INADDR_RIP_GROUP);
322 if (ipv4_multicast_leave (sock, group, p->prefix, ifp->ifindex) == 0)
323 return;
324 }
325 }
326 }
327
328 /* Is there and address on interface that I could use ? */
329 static int
330 rip_if_ipv4_address_check (struct interface *ifp)
331 {
332 struct listnode *nn;
333 struct connected *connected;
334 int count = 0;
335
336 for (ALL_LIST_ELEMENTS_RO (ifp->connected, nn, connected))
337 {
338 struct prefix *p;
339
340 p = connected->address;
341
342 if (p->family == AF_INET)
343 count++;
344 }
345
346 return count;
347 }
348
349
350
351
352 /* Does this address belongs to me ? */
353 int
354 if_check_address (struct in_addr addr)
355 {
356 struct listnode *node;
357 struct interface *ifp;
358
359 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
360 {
361 struct listnode *cnode;
362 struct connected *connected;
363
364 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
365 {
366 struct prefix_ipv4 *p;
367
368 p = (struct prefix_ipv4 *) connected->address;
369
370 if (p->family != AF_INET)
371 continue;
372
373 if (IPV4_ADDR_CMP (&p->prefix, &addr) == 0)
374 return 1;
375 }
376 }
377 return 0;
378 }
379
380 /* Inteface link down message processing. */
381 int
382 rip_interface_down (int command, struct zclient *zclient, zebra_size_t length,
383 vrf_id_t vrf_id)
384 {
385 struct interface *ifp;
386 struct stream *s;
387
388 s = zclient->ibuf;
389
390 /* zebra_interface_state_read() updates interface structure in
391 iflist. */
392 ifp = zebra_interface_state_read (s, vrf_id);
393
394 if (ifp == NULL)
395 return 0;
396
397 rip_if_down(ifp);
398
399 if (IS_RIP_DEBUG_ZEBRA)
400 zlog_debug ("interface %s index %d flags %llx metric %d mtu %d is down",
401 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
402 ifp->metric, ifp->mtu);
403
404 return 0;
405 }
406
407 /* Inteface link up message processing */
408 int
409 rip_interface_up (int command, struct zclient *zclient, zebra_size_t length,
410 vrf_id_t vrf_id)
411 {
412 struct interface *ifp;
413
414 /* zebra_interface_state_read () updates interface structure in
415 iflist. */
416 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
417
418 if (ifp == NULL)
419 return 0;
420
421 if (IS_RIP_DEBUG_ZEBRA)
422 zlog_debug ("interface %s index %d flags %#llx metric %d mtu %d is up",
423 ifp->name, ifp->ifindex, (unsigned long long) ifp->flags,
424 ifp->metric, ifp->mtu);
425
426 /* Check if this interface is RIP enabled or not.*/
427 rip_enable_apply (ifp);
428
429 /* Check for a passive interface */
430 rip_passive_interface_apply (ifp);
431
432 /* Apply distribute list to the all interface. */
433 rip_distribute_update_interface (ifp);
434
435 return 0;
436 }
437
438 /* Inteface addition message from zebra. */
439 int
440 rip_interface_add (int command, struct zclient *zclient, zebra_size_t length,
441 vrf_id_t vrf_id)
442 {
443 struct interface *ifp;
444
445 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
446
447 if (IS_RIP_DEBUG_ZEBRA)
448 zlog_debug ("interface add %s index %d flags %#llx metric %d mtu %d",
449 ifp->name, ifp->ifindex, (unsigned long long) ifp->flags,
450 ifp->metric, ifp->mtu);
451
452 /* Check if this interface is RIP enabled or not.*/
453 rip_enable_apply (ifp);
454
455 /* Check for a passive interface */
456 rip_passive_interface_apply (ifp);
457
458 /* Apply distribute list to the all interface. */
459 rip_distribute_update_interface (ifp);
460
461 /* rip_request_neighbor_all (); */
462
463 /* Check interface routemap. */
464 rip_if_rmap_update_interface (ifp);
465
466 return 0;
467 }
468
469 int
470 rip_interface_delete (int command, struct zclient *zclient,
471 zebra_size_t length, vrf_id_t vrf_id)
472 {
473 struct interface *ifp;
474 struct stream *s;
475
476
477 s = zclient->ibuf;
478 /* zebra_interface_state_read() updates interface structure in iflist */
479 ifp = zebra_interface_state_read (s, vrf_id);
480
481 if (ifp == NULL)
482 return 0;
483
484 if (if_is_up (ifp)) {
485 rip_if_down(ifp);
486 }
487
488 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
489 ifp->name, ifp->ifindex, (unsigned long long) ifp->flags,
490 ifp->metric, ifp->mtu);
491
492 /* To support pseudo interface do not free interface structure. */
493 /* if_delete(ifp); */
494 ifp->ifindex = IFINDEX_DELETED;
495
496 return 0;
497 }
498
499 static void
500 rip_interface_clean (struct rip_interface *ri)
501 {
502 ri->enable_network = 0;
503 ri->enable_interface = 0;
504 ri->running = 0;
505
506 if (ri->t_wakeup)
507 {
508 thread_cancel (ri->t_wakeup);
509 ri->t_wakeup = NULL;
510 }
511 }
512
513 void
514 rip_interfaces_clean (void)
515 {
516 struct listnode *node;
517 struct interface *ifp;
518
519 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
520 rip_interface_clean (ifp->info);
521 }
522
523 static void
524 rip_interface_reset (struct rip_interface *ri)
525 {
526 /* Default authentication type is simple password for Cisco
527 compatibility. */
528 ri->auth_type = RIP_NO_AUTH;
529 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
530
531 /* Set default split-horizon behavior. If the interface is Frame
532 Relay or SMDS is enabled, the default value for split-horizon is
533 off. But currently Zebra does detect Frame Relay or SMDS
534 interface. So all interface is set to split horizon. */
535 ri->split_horizon_default = RIP_SPLIT_HORIZON;
536 ri->split_horizon = ri->split_horizon_default;
537
538 ri->ri_send = RI_RIP_UNSPEC;
539 ri->ri_receive = RI_RIP_UNSPEC;
540
541 ri->v2_broadcast = 0;
542
543 if (ri->auth_str)
544 {
545 free (ri->auth_str);
546 ri->auth_str = NULL;
547 }
548 if (ri->key_chain)
549 {
550 free (ri->key_chain);
551 ri->key_chain = NULL;
552 }
553
554 ri->list[RIP_FILTER_IN] = NULL;
555 ri->list[RIP_FILTER_OUT] = NULL;
556
557 ri->prefix[RIP_FILTER_IN] = NULL;
558 ri->prefix[RIP_FILTER_OUT] = NULL;
559
560 ri->recv_badpackets = 0;
561 ri->recv_badroutes = 0;
562 ri->sent_updates = 0;
563
564 ri->passive = 0;
565
566 rip_interface_clean (ri);
567 }
568
569 void
570 rip_interfaces_reset (void)
571 {
572 struct listnode *node;
573 struct interface *ifp;
574
575 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
576 rip_interface_reset (ifp->info);
577 }
578
579 int
580 rip_if_down(struct interface *ifp)
581 {
582 struct route_node *rp;
583 struct rip_info *rinfo;
584 struct rip_interface *ri = NULL;
585 struct list *list = NULL;
586 struct listnode *listnode = NULL, *nextnode = NULL;
587 if (rip)
588 for (rp = route_top (rip->table); rp; rp = route_next (rp))
589 if ((list = rp->info) != NULL)
590 for (ALL_LIST_ELEMENTS (list, listnode, nextnode, rinfo))
591 if (rinfo->ifindex == ifp->ifindex)
592 rip_ecmp_delete (rinfo);
593
594 ri = ifp->info;
595
596 if (ri->running)
597 {
598 if (IS_RIP_DEBUG_EVENT)
599 zlog_debug ("turn off %s", ifp->name);
600
601 /* Leave from multicast group. */
602 rip_multicast_leave (ifp, rip->sock);
603
604 ri->running = 0;
605 }
606
607 return 0;
608 }
609
610 /* Needed for stop RIP process. */
611 void
612 rip_if_down_all ()
613 {
614 struct interface *ifp;
615 struct listnode *node, *nnode;
616
617 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
618 rip_if_down (ifp);
619 }
620
621 static void
622 rip_apply_address_add (struct connected *ifc)
623 {
624 struct prefix_ipv4 address;
625 struct prefix *p;
626
627 if (!rip)
628 return;
629
630 if (! if_is_up(ifc->ifp))
631 return;
632
633 p = ifc->address;
634
635 memset (&address, 0, sizeof (address));
636 address.family = p->family;
637 address.prefix = p->u.prefix4;
638 address.prefixlen = p->prefixlen;
639 apply_mask_ipv4(&address);
640
641 /* Check if this interface is RIP enabled or not
642 or Check if this address's prefix is RIP enabled */
643 if ((rip_enable_if_lookup(ifc->ifp->name) >= 0) ||
644 (rip_enable_network_lookup2(ifc) >= 0))
645 rip_redistribute_add(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
646 &address, ifc->ifp->ifindex, NULL, 0, 0, 0);
647
648 }
649
650 int
651 rip_interface_address_add (int command, struct zclient *zclient,
652 zebra_size_t length, vrf_id_t vrf_id)
653 {
654 struct connected *ifc;
655 struct prefix *p;
656
657 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
658 zclient->ibuf, vrf_id);
659
660 if (ifc == NULL)
661 return 0;
662
663 p = ifc->address;
664
665 if (p->family == AF_INET)
666 {
667 if (IS_RIP_DEBUG_ZEBRA)
668 zlog_debug ("connected address %s/%d is added",
669 inet_ntoa (p->u.prefix4), p->prefixlen);
670
671 rip_enable_apply(ifc->ifp);
672 /* Check if this prefix needs to be redistributed */
673 rip_apply_address_add(ifc);
674
675 #ifdef HAVE_SNMP
676 rip_ifaddr_add (ifc->ifp, ifc);
677 #endif /* HAVE_SNMP */
678 }
679
680 return 0;
681 }
682
683 static void
684 rip_apply_address_del (struct connected *ifc) {
685 struct prefix_ipv4 address;
686 struct prefix *p;
687
688 if (!rip)
689 return;
690
691 if (! if_is_up(ifc->ifp))
692 return;
693
694 p = ifc->address;
695
696 memset (&address, 0, sizeof (address));
697 address.family = p->family;
698 address.prefix = p->u.prefix4;
699 address.prefixlen = p->prefixlen;
700 apply_mask_ipv4(&address);
701
702 rip_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
703 &address, ifc->ifp->ifindex);
704 }
705
706 int
707 rip_interface_address_delete (int command, struct zclient *zclient,
708 zebra_size_t length, vrf_id_t vrf_id)
709 {
710 struct connected *ifc;
711 struct prefix *p;
712
713 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
714 zclient->ibuf, vrf_id);
715
716 if (ifc)
717 {
718 p = ifc->address;
719 if (p->family == AF_INET)
720 {
721 if (IS_RIP_DEBUG_ZEBRA)
722 zlog_debug ("connected address %s/%d is deleted",
723 inet_ntoa (p->u.prefix4), p->prefixlen);
724
725 #ifdef HAVE_SNMP
726 rip_ifaddr_delete (ifc->ifp, ifc);
727 #endif /* HAVE_SNMP */
728
729 /* Chech wether this prefix needs to be removed */
730 rip_apply_address_del(ifc);
731
732 }
733
734 connected_free (ifc);
735
736 }
737
738 return 0;
739 }
740
741 /* Check interface is enabled by network statement. */
742 /* Check wether the interface has at least a connected prefix that
743 * is within the ripng_enable_network table. */
744 static int
745 rip_enable_network_lookup_if (struct interface *ifp)
746 {
747 struct listnode *node, *nnode;
748 struct connected *connected;
749 struct prefix_ipv4 address;
750
751 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
752 {
753 struct prefix *p;
754 struct route_node *node;
755
756 p = connected->address;
757
758 if (p->family == AF_INET)
759 {
760 address.family = AF_INET;
761 address.prefix = p->u.prefix4;
762 address.prefixlen = IPV4_MAX_BITLEN;
763
764 node = route_node_match (rip_enable_network,
765 (struct prefix *)&address);
766 if (node)
767 {
768 route_unlock_node (node);
769 return 1;
770 }
771 }
772 }
773 return -1;
774 }
775
776 /* Check wether connected is within the ripng_enable_network table. */
777 int
778 rip_enable_network_lookup2 (struct connected *connected)
779 {
780 struct prefix_ipv4 address;
781 struct prefix *p;
782
783 p = connected->address;
784
785 if (p->family == AF_INET) {
786 struct route_node *node;
787
788 address.family = p->family;
789 address.prefix = p->u.prefix4;
790 address.prefixlen = IPV4_MAX_BITLEN;
791
792 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within rip_enable_network */
793 node = route_node_match (rip_enable_network,
794 (struct prefix *)&address);
795
796 if (node) {
797 route_unlock_node (node);
798 return 1;
799 }
800 }
801
802 return -1;
803 }
804 /* Add RIP enable network. */
805 static int
806 rip_enable_network_add (struct prefix *p)
807 {
808 struct route_node *node;
809
810 node = route_node_get (rip_enable_network, p);
811
812 if (node->info)
813 {
814 route_unlock_node (node);
815 return -1;
816 }
817 else
818 node->info = (void *)1;
819
820 /* XXX: One should find a better solution than a generic one */
821 rip_enable_apply_all();
822
823 return 1;
824 }
825
826 /* Delete RIP enable network. */
827 static int
828 rip_enable_network_delete (struct prefix *p)
829 {
830 struct route_node *node;
831
832 node = route_node_lookup (rip_enable_network, p);
833 if (node)
834 {
835 node->info = NULL;
836
837 /* Unlock info lock. */
838 route_unlock_node (node);
839
840 /* Unlock lookup lock. */
841 route_unlock_node (node);
842
843 /* XXX: One should find a better solution than a generic one */
844 rip_enable_apply_all ();
845
846 return 1;
847 }
848 return -1;
849 }
850
851 /* Check interface is enabled by ifname statement. */
852 static int
853 rip_enable_if_lookup (const char *ifname)
854 {
855 unsigned int i;
856 char *str;
857
858 for (i = 0; i < vector_active (rip_enable_interface); i++)
859 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
860 if (strcmp (str, ifname) == 0)
861 return i;
862 return -1;
863 }
864
865 /* Add interface to rip_enable_if. */
866 static int
867 rip_enable_if_add (const char *ifname)
868 {
869 int ret;
870
871 ret = rip_enable_if_lookup (ifname);
872 if (ret >= 0)
873 return -1;
874
875 vector_set (rip_enable_interface, strdup (ifname));
876
877 rip_enable_apply_all(); /* TODOVJ */
878
879 return 1;
880 }
881
882 /* Delete interface from rip_enable_if. */
883 static int
884 rip_enable_if_delete (const char *ifname)
885 {
886 int index;
887 char *str;
888
889 index = rip_enable_if_lookup (ifname);
890 if (index < 0)
891 return -1;
892
893 str = vector_slot (rip_enable_interface, index);
894 free (str);
895 vector_unset (rip_enable_interface, index);
896
897 rip_enable_apply_all(); /* TODOVJ */
898
899 return 1;
900 }
901
902 /* Join to multicast group and send request to the interface. */
903 static int
904 rip_interface_wakeup (struct thread *t)
905 {
906 struct interface *ifp;
907 struct rip_interface *ri;
908
909 /* Get interface. */
910 ifp = THREAD_ARG (t);
911
912 ri = ifp->info;
913 ri->t_wakeup = NULL;
914
915 /* Join to multicast group. */
916 if (rip_multicast_join (ifp, rip->sock) < 0)
917 {
918 zlog_err ("multicast join failed, interface %s not running", ifp->name);
919 return 0;
920 }
921
922 /* Set running flag. */
923 ri->running = 1;
924
925 /* Send RIP request to the interface. */
926 rip_request_interface (ifp);
927
928 return 0;
929 }
930
931 static void
932 rip_connect_set (struct interface *ifp, int set)
933 {
934 struct listnode *node, *nnode;
935 struct connected *connected;
936 struct prefix_ipv4 address;
937
938 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
939 {
940 struct prefix *p;
941 p = connected->address;
942
943 if (p->family != AF_INET)
944 continue;
945
946 address.family = AF_INET;
947 address.prefix = p->u.prefix4;
948 address.prefixlen = p->prefixlen;
949 apply_mask_ipv4 (&address);
950
951 if (set) {
952 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
953 if ((rip_enable_if_lookup(connected->ifp->name) >= 0) ||
954 (rip_enable_network_lookup2(connected) >= 0))
955 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
956 &address, connected->ifp->ifindex,
957 NULL, 0, 0, 0);
958 } else
959 {
960 rip_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
961 &address, connected->ifp->ifindex);
962 if (rip_redistribute_check (ZEBRA_ROUTE_CONNECT))
963 rip_redistribute_add (ZEBRA_ROUTE_CONNECT, RIP_ROUTE_REDISTRIBUTE,
964 &address, connected->ifp->ifindex,
965 NULL, 0, 0, 0);
966 }
967 }
968 }
969
970 /* Update interface status. */
971 void
972 rip_enable_apply (struct interface *ifp)
973 {
974 int ret;
975 struct rip_interface *ri = NULL;
976
977 /* Check interface. */
978 if (! if_is_operative (ifp))
979 return;
980
981 ri = ifp->info;
982
983 /* Check network configuration. */
984 ret = rip_enable_network_lookup_if (ifp);
985
986 /* If the interface is matched. */
987 if (ret > 0)
988 ri->enable_network = 1;
989 else
990 ri->enable_network = 0;
991
992 /* Check interface name configuration. */
993 ret = rip_enable_if_lookup (ifp->name);
994 if (ret >= 0)
995 ri->enable_interface = 1;
996 else
997 ri->enable_interface = 0;
998
999 /* any interface MUST have an IPv4 address */
1000 if ( ! rip_if_ipv4_address_check (ifp) )
1001 {
1002 ri->enable_network = 0;
1003 ri->enable_interface = 0;
1004 }
1005
1006 /* Update running status of the interface. */
1007 if (ri->enable_network || ri->enable_interface)
1008 {
1009 {
1010 if (IS_RIP_DEBUG_EVENT)
1011 zlog_debug ("turn on %s", ifp->name);
1012
1013 /* Add interface wake up thread. */
1014 if (! ri->t_wakeup)
1015 ri->t_wakeup = thread_add_timer (master, rip_interface_wakeup,
1016 ifp, 1);
1017 rip_connect_set (ifp, 1);
1018 }
1019 }
1020 else
1021 {
1022 if (ri->running)
1023 {
1024 /* Might as well clean up the route table as well
1025 * rip_if_down sets to 0 ri->running, and displays "turn off %s"
1026 **/
1027 rip_if_down(ifp);
1028
1029 rip_connect_set (ifp, 0);
1030 }
1031 }
1032 }
1033
1034 /* Apply network configuration to all interface. */
1035 void
1036 rip_enable_apply_all ()
1037 {
1038 struct interface *ifp;
1039 struct listnode *node, *nnode;
1040
1041 /* Check each interface. */
1042 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
1043 rip_enable_apply (ifp);
1044 }
1045
1046 int
1047 rip_neighbor_lookup (struct sockaddr_in *from)
1048 {
1049 struct prefix_ipv4 p;
1050 struct route_node *node;
1051
1052 memset (&p, 0, sizeof (struct prefix_ipv4));
1053 p.family = AF_INET;
1054 p.prefix = from->sin_addr;
1055 p.prefixlen = IPV4_MAX_BITLEN;
1056
1057 node = route_node_lookup (rip->neighbor, (struct prefix *) &p);
1058 if (node)
1059 {
1060 route_unlock_node (node);
1061 return 1;
1062 }
1063 return 0;
1064 }
1065
1066 /* Add new RIP neighbor to the neighbor tree. */
1067 static int
1068 rip_neighbor_add (struct prefix_ipv4 *p)
1069 {
1070 struct route_node *node;
1071
1072 node = route_node_get (rip->neighbor, (struct prefix *) p);
1073
1074 if (node->info)
1075 return -1;
1076
1077 node->info = rip->neighbor;
1078
1079 return 0;
1080 }
1081
1082 /* Delete RIP neighbor from the neighbor tree. */
1083 static int
1084 rip_neighbor_delete (struct prefix_ipv4 *p)
1085 {
1086 struct route_node *node;
1087
1088 /* Lock for look up. */
1089 node = route_node_lookup (rip->neighbor, (struct prefix *) p);
1090 if (! node)
1091 return -1;
1092
1093 node->info = NULL;
1094
1095 /* Unlock lookup lock. */
1096 route_unlock_node (node);
1097
1098 /* Unlock real neighbor information lock. */
1099 route_unlock_node (node);
1100
1101 return 0;
1102 }
1103
1104 /* Clear all network and neighbor configuration. */
1105 void
1106 rip_clean_network ()
1107 {
1108 unsigned int i;
1109 char *str;
1110 struct route_node *rn;
1111
1112 /* rip_enable_network. */
1113 for (rn = route_top (rip_enable_network); rn; rn = route_next (rn))
1114 if (rn->info)
1115 {
1116 rn->info = NULL;
1117 route_unlock_node (rn);
1118 }
1119
1120 /* rip_enable_interface. */
1121 for (i = 0; i < vector_active (rip_enable_interface); i++)
1122 if ((str = vector_slot (rip_enable_interface, i)) != NULL)
1123 {
1124 free (str);
1125 vector_slot (rip_enable_interface, i) = NULL;
1126 }
1127 }
1128
1129 /* Utility function for looking up passive interface settings. */
1130 static int
1131 rip_passive_nondefault_lookup (const char *ifname)
1132 {
1133 unsigned int i;
1134 char *str;
1135
1136 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1137 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1138 if (strcmp (str, ifname) == 0)
1139 return i;
1140 return -1;
1141 }
1142
1143 void
1144 rip_passive_interface_apply (struct interface *ifp)
1145 {
1146 struct rip_interface *ri;
1147
1148 ri = ifp->info;
1149
1150 ri->passive = ((rip_passive_nondefault_lookup (ifp->name) < 0) ?
1151 passive_default : !passive_default);
1152
1153 if (IS_RIP_DEBUG_ZEBRA)
1154 zlog_debug ("interface %s: passive = %d",ifp->name,ri->passive);
1155 }
1156
1157 static void
1158 rip_passive_interface_apply_all (void)
1159 {
1160 struct interface *ifp;
1161 struct listnode *node, *nnode;
1162
1163 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
1164 rip_passive_interface_apply (ifp);
1165 }
1166
1167 /* Passive interface. */
1168 static int
1169 rip_passive_nondefault_set (struct vty *vty, const char *ifname)
1170 {
1171 if (rip_passive_nondefault_lookup (ifname) >= 0)
1172 return CMD_WARNING;
1173
1174 vector_set (Vrip_passive_nondefault, strdup (ifname));
1175
1176 rip_passive_interface_apply_all ();
1177
1178 return CMD_SUCCESS;
1179 }
1180
1181 static int
1182 rip_passive_nondefault_unset (struct vty *vty, const char *ifname)
1183 {
1184 int i;
1185 char *str;
1186
1187 i = rip_passive_nondefault_lookup (ifname);
1188 if (i < 0)
1189 return CMD_WARNING;
1190
1191 str = vector_slot (Vrip_passive_nondefault, i);
1192 free (str);
1193 vector_unset (Vrip_passive_nondefault, i);
1194
1195 rip_passive_interface_apply_all ();
1196
1197 return CMD_SUCCESS;
1198 }
1199
1200 /* Free all configured RIP passive-interface settings. */
1201 void
1202 rip_passive_nondefault_clean (void)
1203 {
1204 unsigned int i;
1205 char *str;
1206
1207 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
1208 if ((str = vector_slot (Vrip_passive_nondefault, i)) != NULL)
1209 {
1210 free (str);
1211 vector_slot (Vrip_passive_nondefault, i) = NULL;
1212 }
1213 rip_passive_interface_apply_all ();
1214 }
1215
1216 /* RIP enable network or interface configuration. */
1217 DEFUN (rip_network,
1218 rip_network_cmd,
1219 "network (A.B.C.D/M|WORD)",
1220 "Enable routing on an IP network\n"
1221 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1222 "Interface name\n")
1223 {
1224 int ret;
1225 struct prefix_ipv4 p;
1226
1227 ret = str2prefix_ipv4 (argv[0], &p);
1228
1229 if (ret)
1230 ret = rip_enable_network_add ((struct prefix *) &p);
1231 else
1232 ret = rip_enable_if_add (argv[0]);
1233
1234 if (ret < 0)
1235 {
1236 vty_out (vty, "There is a same network configuration %s%s", argv[0],
1237 VTY_NEWLINE);
1238 return CMD_WARNING;
1239 }
1240
1241 return CMD_SUCCESS;
1242 }
1243
1244 /* RIP enable network or interface configuration. */
1245 DEFUN (no_rip_network,
1246 no_rip_network_cmd,
1247 "no network (A.B.C.D/M|WORD)",
1248 NO_STR
1249 "Enable routing on an IP network\n"
1250 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1251 "Interface name\n")
1252 {
1253 int ret;
1254 struct prefix_ipv4 p;
1255
1256 ret = str2prefix_ipv4 (argv[0], &p);
1257
1258 if (ret)
1259 ret = rip_enable_network_delete ((struct prefix *) &p);
1260 else
1261 ret = rip_enable_if_delete (argv[0]);
1262
1263 if (ret < 0)
1264 {
1265 vty_out (vty, "Can't find network configuration %s%s", argv[0],
1266 VTY_NEWLINE);
1267 return CMD_WARNING;
1268 }
1269
1270 return CMD_SUCCESS;
1271 }
1272
1273 /* RIP neighbor configuration set. */
1274 DEFUN (rip_neighbor,
1275 rip_neighbor_cmd,
1276 "neighbor A.B.C.D",
1277 "Specify a neighbor router\n"
1278 "Neighbor address\n")
1279 {
1280 int ret;
1281 struct prefix_ipv4 p;
1282
1283 ret = str2prefix_ipv4 (argv[0], &p);
1284
1285 if (ret <= 0)
1286 {
1287 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1288 return CMD_WARNING;
1289 }
1290
1291 rip_neighbor_add (&p);
1292
1293 return CMD_SUCCESS;
1294 }
1295
1296 /* RIP neighbor configuration unset. */
1297 DEFUN (no_rip_neighbor,
1298 no_rip_neighbor_cmd,
1299 "no neighbor A.B.C.D",
1300 NO_STR
1301 "Specify a neighbor router\n"
1302 "Neighbor address\n")
1303 {
1304 int ret;
1305 struct prefix_ipv4 p;
1306
1307 ret = str2prefix_ipv4 (argv[0], &p);
1308
1309 if (ret <= 0)
1310 {
1311 vty_out (vty, "Please specify address by A.B.C.D%s", VTY_NEWLINE);
1312 return CMD_WARNING;
1313 }
1314
1315 rip_neighbor_delete (&p);
1316
1317 return CMD_SUCCESS;
1318 }
1319
1320 DEFUN (ip_rip_receive_version,
1321 ip_rip_receive_version_cmd,
1322 "ip rip receive version (1|2|none)",
1323 IP_STR
1324 "Routing Information Protocol\n"
1325 "Advertisement reception\n"
1326 "Version control\n"
1327 "RIP version 1\n"
1328 "RIP version 2\n"
1329 "None\n")
1330 {
1331 struct interface *ifp;
1332 struct rip_interface *ri;
1333
1334 ifp = (struct interface *)vty->index;
1335 ri = ifp->info;
1336
1337 switch (*argv[0])
1338 {
1339 case '1':
1340 ri->ri_receive = RI_RIP_VERSION_1;
1341 return CMD_SUCCESS;
1342 case '2':
1343 ri->ri_receive = RI_RIP_VERSION_2;
1344 return CMD_SUCCESS;
1345 case 'n':
1346 ri->ri_receive = RI_RIP_VERSION_NONE;
1347 return CMD_SUCCESS;
1348 default:
1349 break;
1350 }
1351
1352 return CMD_WARNING;
1353 }
1354
1355 DEFUN (ip_rip_receive_version_1,
1356 ip_rip_receive_version_1_cmd,
1357 "ip rip receive version 1 2",
1358 IP_STR
1359 "Routing Information Protocol\n"
1360 "Advertisement reception\n"
1361 "Version control\n"
1362 "RIP version 1\n"
1363 "RIP version 2\n")
1364 {
1365 struct interface *ifp;
1366 struct rip_interface *ri;
1367
1368 ifp = (struct interface *)vty->index;
1369 ri = ifp->info;
1370
1371 /* Version 1 and 2. */
1372 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1373 return CMD_SUCCESS;
1374 }
1375
1376 DEFUN (ip_rip_receive_version_2,
1377 ip_rip_receive_version_2_cmd,
1378 "ip rip receive version 2 1",
1379 IP_STR
1380 "Routing Information Protocol\n"
1381 "Advertisement reception\n"
1382 "Version control\n"
1383 "RIP version 2\n"
1384 "RIP version 1\n")
1385 {
1386 struct interface *ifp;
1387 struct rip_interface *ri;
1388
1389 ifp = (struct interface *)vty->index;
1390 ri = ifp->info;
1391
1392 /* Version 1 and 2. */
1393 ri->ri_receive = RI_RIP_VERSION_1_AND_2;
1394 return CMD_SUCCESS;
1395 }
1396
1397 DEFUN (no_ip_rip_receive_version,
1398 no_ip_rip_receive_version_cmd,
1399 "no ip rip receive version",
1400 NO_STR
1401 IP_STR
1402 "Routing Information Protocol\n"
1403 "Advertisement reception\n"
1404 "Version control\n")
1405 {
1406 struct interface *ifp;
1407 struct rip_interface *ri;
1408
1409 ifp = (struct interface *)vty->index;
1410 ri = ifp->info;
1411
1412 ri->ri_receive = RI_RIP_UNSPEC;
1413 return CMD_SUCCESS;
1414 }
1415
1416 ALIAS (no_ip_rip_receive_version,
1417 no_ip_rip_receive_version_num_cmd,
1418 "no ip rip receive version (1|2)",
1419 NO_STR
1420 IP_STR
1421 "Routing Information Protocol\n"
1422 "Advertisement reception\n"
1423 "Version control\n"
1424 "Version 1\n"
1425 "Version 2\n")
1426
1427 DEFUN (ip_rip_send_version,
1428 ip_rip_send_version_cmd,
1429 "ip rip send version (1|2)",
1430 IP_STR
1431 "Routing Information Protocol\n"
1432 "Advertisement transmission\n"
1433 "Version control\n"
1434 "RIP version 1\n"
1435 "RIP version 2\n")
1436 {
1437 struct interface *ifp;
1438 struct rip_interface *ri;
1439
1440 ifp = (struct interface *)vty->index;
1441 ri = ifp->info;
1442
1443 /* Version 1. */
1444 if (atoi (argv[0]) == 1)
1445 {
1446 ri->ri_send = RI_RIP_VERSION_1;
1447 return CMD_SUCCESS;
1448 }
1449 if (atoi (argv[0]) == 2)
1450 {
1451 ri->ri_send = RI_RIP_VERSION_2;
1452 return CMD_SUCCESS;
1453 }
1454 return CMD_WARNING;
1455 }
1456
1457 DEFUN (ip_rip_send_version_1,
1458 ip_rip_send_version_1_cmd,
1459 "ip rip send version 1 2",
1460 IP_STR
1461 "Routing Information Protocol\n"
1462 "Advertisement transmission\n"
1463 "Version control\n"
1464 "RIP version 1\n"
1465 "RIP version 2\n")
1466 {
1467 struct interface *ifp;
1468 struct rip_interface *ri;
1469
1470 ifp = (struct interface *)vty->index;
1471 ri = ifp->info;
1472
1473 /* Version 1 and 2. */
1474 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1475 return CMD_SUCCESS;
1476 }
1477
1478 DEFUN (ip_rip_send_version_2,
1479 ip_rip_send_version_2_cmd,
1480 "ip rip send version 2 1",
1481 IP_STR
1482 "Routing Information Protocol\n"
1483 "Advertisement transmission\n"
1484 "Version control\n"
1485 "RIP version 2\n"
1486 "RIP version 1\n")
1487 {
1488 struct interface *ifp;
1489 struct rip_interface *ri;
1490
1491 ifp = (struct interface *)vty->index;
1492 ri = ifp->info;
1493
1494 /* Version 1 and 2. */
1495 ri->ri_send = RI_RIP_VERSION_1_AND_2;
1496 return CMD_SUCCESS;
1497 }
1498
1499 DEFUN (no_ip_rip_send_version,
1500 no_ip_rip_send_version_cmd,
1501 "no ip rip send version",
1502 NO_STR
1503 IP_STR
1504 "Routing Information Protocol\n"
1505 "Advertisement transmission\n"
1506 "Version control\n")
1507 {
1508 struct interface *ifp;
1509 struct rip_interface *ri;
1510
1511 ifp = (struct interface *)vty->index;
1512 ri = ifp->info;
1513
1514 ri->ri_send = RI_RIP_UNSPEC;
1515 return CMD_SUCCESS;
1516 }
1517
1518 ALIAS (no_ip_rip_send_version,
1519 no_ip_rip_send_version_num_cmd,
1520 "no ip rip send version (1|2)",
1521 NO_STR
1522 IP_STR
1523 "Routing Information Protocol\n"
1524 "Advertisement transmission\n"
1525 "Version control\n"
1526 "Version 1\n"
1527 "Version 2\n")
1528
1529 DEFUN (ip_rip_v2_broadcast,
1530 ip_rip_v2_broadcast_cmd,
1531 "ip rip v2-broadcast",
1532 IP_STR
1533 "Routing Information Protocol\n"
1534 "Send ip broadcast v2 update\n")
1535 {
1536 struct interface *ifp;
1537 struct rip_interface *ri;
1538
1539 ifp = (struct interface *)vty->index;
1540 ri = ifp->info;
1541
1542 ri->v2_broadcast = 1;
1543 return CMD_SUCCESS;
1544 }
1545
1546 DEFUN (no_ip_rip_v2_broadcast,
1547 no_ip_rip_v2_broadcast_cmd,
1548 "no ip rip v2-broadcast",
1549 NO_STR
1550 IP_STR
1551 "Routing Information Protocol\n"
1552 "Send ip broadcast v2 update\n")
1553 {
1554 struct interface *ifp;
1555 struct rip_interface *ri;
1556
1557 ifp = (struct interface *)vty->index;
1558 ri = ifp->info;
1559
1560 ri->v2_broadcast = 0;
1561 return CMD_SUCCESS;
1562 }
1563
1564 DEFUN (ip_rip_authentication_mode,
1565 ip_rip_authentication_mode_cmd,
1566 "ip rip authentication mode (md5|text)",
1567 IP_STR
1568 "Routing Information Protocol\n"
1569 "Authentication control\n"
1570 "Authentication mode\n"
1571 "Keyed message digest\n"
1572 "Clear text authentication\n")
1573 {
1574 struct interface *ifp;
1575 struct rip_interface *ri;
1576 int auth_type;
1577
1578 ifp = (struct interface *)vty->index;
1579 ri = ifp->info;
1580
1581 if ( (argc < 1) || (argc > 2) )
1582 {
1583 vty_out (vty, "incorrect argument count%s", VTY_NEWLINE);
1584 return CMD_WARNING;
1585 }
1586
1587 if (strncmp ("md5", argv[0], strlen (argv[0])) == 0)
1588 auth_type = RIP_AUTH_MD5;
1589 else if (strncmp ("text", argv[0], strlen (argv[0])) == 0)
1590 auth_type = RIP_AUTH_SIMPLE_PASSWORD;
1591 else
1592 {
1593 vty_out (vty, "mode should be md5 or text%s", VTY_NEWLINE);
1594 return CMD_WARNING;
1595 }
1596
1597 if (argc == 1)
1598 {
1599 ri->auth_type = auth_type;
1600 return CMD_SUCCESS;
1601 }
1602
1603 if ( (argc == 2) && (auth_type != RIP_AUTH_MD5) )
1604 {
1605 vty_out (vty, "auth length argument only valid for md5%s", VTY_NEWLINE);
1606 return CMD_WARNING;
1607 }
1608
1609 if (strncmp ("r", argv[1], 1) == 0)
1610 ri->md5_auth_len = RIP_AUTH_MD5_SIZE;
1611 else if (strncmp ("o", argv[1], 1) == 0)
1612 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1613 else
1614 return CMD_WARNING;
1615
1616 ri->auth_type = auth_type;
1617
1618 return CMD_SUCCESS;
1619 }
1620
1621 ALIAS (ip_rip_authentication_mode,
1622 ip_rip_authentication_mode_authlen_cmd,
1623 "ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1624 IP_STR
1625 "Routing Information Protocol\n"
1626 "Authentication control\n"
1627 "Authentication mode\n"
1628 "Keyed message digest\n"
1629 "Clear text authentication\n"
1630 "MD5 authentication data length\n"
1631 "RFC compatible\n"
1632 "Old ripd compatible\n")
1633
1634 DEFUN (no_ip_rip_authentication_mode,
1635 no_ip_rip_authentication_mode_cmd,
1636 "no ip rip authentication mode",
1637 NO_STR
1638 IP_STR
1639 "Routing Information Protocol\n"
1640 "Authentication control\n"
1641 "Authentication mode\n")
1642 {
1643 struct interface *ifp;
1644 struct rip_interface *ri;
1645
1646 ifp = (struct interface *)vty->index;
1647 ri = ifp->info;
1648
1649 ri->auth_type = RIP_NO_AUTH;
1650 ri->md5_auth_len = RIP_AUTH_MD5_COMPAT_SIZE;
1651
1652 return CMD_SUCCESS;
1653 }
1654
1655 ALIAS (no_ip_rip_authentication_mode,
1656 no_ip_rip_authentication_mode_type_cmd,
1657 "no ip rip authentication mode (md5|text)",
1658 NO_STR
1659 IP_STR
1660 "Routing Information Protocol\n"
1661 "Authentication control\n"
1662 "Authentication mode\n"
1663 "Keyed message digest\n"
1664 "Clear text authentication\n")
1665
1666 ALIAS (no_ip_rip_authentication_mode,
1667 no_ip_rip_authentication_mode_type_authlen_cmd,
1668 "no ip rip authentication mode (md5|text) auth-length (rfc|old-ripd)",
1669 NO_STR
1670 IP_STR
1671 "Routing Information Protocol\n"
1672 "Authentication control\n"
1673 "Authentication mode\n"
1674 "Keyed message digest\n"
1675 "Clear text authentication\n"
1676 "MD5 authentication data length\n"
1677 "RFC compatible\n"
1678 "Old ripd compatible\n")
1679
1680 DEFUN (ip_rip_authentication_string,
1681 ip_rip_authentication_string_cmd,
1682 "ip rip authentication string LINE",
1683 IP_STR
1684 "Routing Information Protocol\n"
1685 "Authentication control\n"
1686 "Authentication string\n"
1687 "Authentication string\n")
1688 {
1689 struct interface *ifp;
1690 struct rip_interface *ri;
1691
1692 ifp = (struct interface *)vty->index;
1693 ri = ifp->info;
1694
1695 if (strlen (argv[0]) > 16)
1696 {
1697 vty_out (vty, "%% RIPv2 authentication string must be shorter than 16%s",
1698 VTY_NEWLINE);
1699 return CMD_WARNING;
1700 }
1701
1702 if (ri->key_chain)
1703 {
1704 vty_out (vty, "%% key-chain configuration exists%s", VTY_NEWLINE);
1705 return CMD_WARNING;
1706 }
1707
1708 if (ri->auth_str)
1709 free (ri->auth_str);
1710
1711 ri->auth_str = strdup (argv[0]);
1712
1713 return CMD_SUCCESS;
1714 }
1715
1716 DEFUN (no_ip_rip_authentication_string,
1717 no_ip_rip_authentication_string_cmd,
1718 "no ip rip authentication string",
1719 NO_STR
1720 IP_STR
1721 "Routing Information Protocol\n"
1722 "Authentication control\n"
1723 "Authentication string\n")
1724 {
1725 struct interface *ifp;
1726 struct rip_interface *ri;
1727
1728 ifp = (struct interface *)vty->index;
1729 ri = ifp->info;
1730
1731 if (ri->auth_str)
1732 free (ri->auth_str);
1733
1734 ri->auth_str = NULL;
1735
1736 return CMD_SUCCESS;
1737 }
1738
1739 ALIAS (no_ip_rip_authentication_string,
1740 no_ip_rip_authentication_string2_cmd,
1741 "no ip rip authentication string LINE",
1742 NO_STR
1743 IP_STR
1744 "Routing Information Protocol\n"
1745 "Authentication control\n"
1746 "Authentication string\n"
1747 "Authentication string\n")
1748
1749 DEFUN (ip_rip_authentication_key_chain,
1750 ip_rip_authentication_key_chain_cmd,
1751 "ip rip authentication key-chain LINE",
1752 IP_STR
1753 "Routing Information Protocol\n"
1754 "Authentication control\n"
1755 "Authentication key-chain\n"
1756 "name of key-chain\n")
1757 {
1758 struct interface *ifp;
1759 struct rip_interface *ri;
1760
1761 ifp = (struct interface *) vty->index;
1762 ri = ifp->info;
1763
1764 if (ri->auth_str)
1765 {
1766 vty_out (vty, "%% authentication string configuration exists%s",
1767 VTY_NEWLINE);
1768 return CMD_WARNING;
1769 }
1770
1771 if (ri->key_chain)
1772 free (ri->key_chain);
1773
1774 ri->key_chain = strdup (argv[0]);
1775
1776 return CMD_SUCCESS;
1777 }
1778
1779 DEFUN (no_ip_rip_authentication_key_chain,
1780 no_ip_rip_authentication_key_chain_cmd,
1781 "no ip rip authentication key-chain",
1782 NO_STR
1783 IP_STR
1784 "Routing Information Protocol\n"
1785 "Authentication control\n"
1786 "Authentication key-chain\n")
1787 {
1788 struct interface *ifp;
1789 struct rip_interface *ri;
1790
1791 ifp = (struct interface *) vty->index;
1792 ri = ifp->info;
1793
1794 if (ri->key_chain)
1795 free (ri->key_chain);
1796
1797 ri->key_chain = NULL;
1798
1799 return CMD_SUCCESS;
1800 }
1801
1802 ALIAS (no_ip_rip_authentication_key_chain,
1803 no_ip_rip_authentication_key_chain2_cmd,
1804 "no ip rip authentication key-chain LINE",
1805 NO_STR
1806 IP_STR
1807 "Routing Information Protocol\n"
1808 "Authentication control\n"
1809 "Authentication key-chain\n"
1810 "name of key-chain\n")
1811
1812 /* CHANGED: ip rip split-horizon
1813 Cisco and Zebra's command is
1814 ip split-horizon
1815 */
1816 DEFUN (ip_rip_split_horizon,
1817 ip_rip_split_horizon_cmd,
1818 "ip rip split-horizon",
1819 IP_STR
1820 "Routing Information Protocol\n"
1821 "Perform split horizon\n")
1822 {
1823 struct interface *ifp;
1824 struct rip_interface *ri;
1825
1826 ifp = vty->index;
1827 ri = ifp->info;
1828
1829 ri->split_horizon = RIP_SPLIT_HORIZON;
1830 return CMD_SUCCESS;
1831 }
1832
1833 DEFUN (ip_rip_split_horizon_poisoned_reverse,
1834 ip_rip_split_horizon_poisoned_reverse_cmd,
1835 "ip rip split-horizon poisoned-reverse",
1836 IP_STR
1837 "Routing Information Protocol\n"
1838 "Perform split horizon\n"
1839 "With poisoned-reverse\n")
1840 {
1841 struct interface *ifp;
1842 struct rip_interface *ri;
1843
1844 ifp = vty->index;
1845 ri = ifp->info;
1846
1847 ri->split_horizon = RIP_SPLIT_HORIZON_POISONED_REVERSE;
1848 return CMD_SUCCESS;
1849 }
1850
1851 /* CHANGED: no ip rip split-horizon
1852 Cisco and Zebra's command is
1853 no ip split-horizon
1854 */
1855 DEFUN (no_ip_rip_split_horizon,
1856 no_ip_rip_split_horizon_cmd,
1857 "no ip rip split-horizon",
1858 NO_STR
1859 IP_STR
1860 "Routing Information Protocol\n"
1861 "Perform split horizon\n")
1862 {
1863 struct interface *ifp;
1864 struct rip_interface *ri;
1865
1866 ifp = vty->index;
1867 ri = ifp->info;
1868
1869 ri->split_horizon = RIP_NO_SPLIT_HORIZON;
1870 return CMD_SUCCESS;
1871 }
1872
1873 DEFUN (no_ip_rip_split_horizon_poisoned_reverse,
1874 no_ip_rip_split_horizon_poisoned_reverse_cmd,
1875 "no ip rip split-horizon poisoned-reverse",
1876 NO_STR
1877 IP_STR
1878 "Routing Information Protocol\n"
1879 "Perform split horizon\n"
1880 "With poisoned-reverse\n")
1881 {
1882 struct interface *ifp;
1883 struct rip_interface *ri;
1884
1885 ifp = vty->index;
1886 ri = ifp->info;
1887
1888 switch( ri->split_horizon )
1889 {
1890 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1891 ri->split_horizon = RIP_SPLIT_HORIZON;
1892 default:
1893 break;
1894 }
1895
1896 return CMD_SUCCESS;
1897 }
1898
1899 DEFUN (rip_passive_interface,
1900 rip_passive_interface_cmd,
1901 "passive-interface (IFNAME|default)",
1902 "Suppress routing updates on an interface\n"
1903 "Interface name\n"
1904 "default for all interfaces\n")
1905 {
1906 const char *ifname = argv[0];
1907
1908 if (!strcmp(ifname,"default")) {
1909 passive_default = 1;
1910 rip_passive_nondefault_clean();
1911 return CMD_SUCCESS;
1912 }
1913 if (passive_default)
1914 return rip_passive_nondefault_unset (vty, ifname);
1915 else
1916 return rip_passive_nondefault_set (vty, ifname);
1917 }
1918
1919 DEFUN (no_rip_passive_interface,
1920 no_rip_passive_interface_cmd,
1921 "no passive-interface (IFNAME|default)",
1922 NO_STR
1923 "Suppress routing updates on an interface\n"
1924 "Interface name\n"
1925 "default for all interfaces\n")
1926 {
1927 const char *ifname = argv[0];
1928
1929 if (!strcmp(ifname,"default")) {
1930 passive_default = 0;
1931 rip_passive_nondefault_clean();
1932 return CMD_SUCCESS;
1933 }
1934 if (passive_default)
1935 return rip_passive_nondefault_set (vty, ifname);
1936 else
1937 return rip_passive_nondefault_unset (vty, ifname);
1938 }
1939
1940 /* Write rip configuration of each interface. */
1941 static int
1942 rip_interface_config_write (struct vty *vty)
1943 {
1944 struct listnode *node;
1945 struct interface *ifp;
1946
1947 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
1948 {
1949 struct rip_interface *ri;
1950
1951 if (ifp->ifindex == IFINDEX_DELETED)
1952 continue;
1953
1954 ri = ifp->info;
1955
1956 /* Do not display the interface if there is no
1957 * configuration about it.
1958 **/
1959 if ((!ifp->desc) &&
1960 (ri->split_horizon == ri->split_horizon_default) &&
1961 (ri->ri_send == RI_RIP_UNSPEC) &&
1962 (ri->ri_receive == RI_RIP_UNSPEC) &&
1963 (ri->auth_type != RIP_AUTH_MD5) &&
1964 (!ri->v2_broadcast) &&
1965 (ri->md5_auth_len != RIP_AUTH_MD5_SIZE) &&
1966 (!ri->auth_str) &&
1967 (!ri->key_chain) )
1968 continue;
1969
1970 vty_out (vty, "interface %s%s", ifp->name,
1971 VTY_NEWLINE);
1972
1973 if (ifp->desc)
1974 vty_out (vty, " description %s%s", ifp->desc,
1975 VTY_NEWLINE);
1976
1977 /* Split horizon. */
1978 if (ri->split_horizon != ri->split_horizon_default)
1979 {
1980 switch (ri->split_horizon) {
1981 case RIP_SPLIT_HORIZON:
1982 vty_out (vty, " ip rip split-horizon%s", VTY_NEWLINE);
1983 break;
1984 case RIP_SPLIT_HORIZON_POISONED_REVERSE:
1985 vty_out (vty, " ip rip split-horizon poisoned-reverse%s",
1986 VTY_NEWLINE);
1987 break;
1988 case RIP_NO_SPLIT_HORIZON:
1989 default:
1990 vty_out (vty, " no ip rip split-horizon%s", VTY_NEWLINE);
1991 break;
1992 }
1993 }
1994
1995 /* RIP version setting. */
1996 if (ri->ri_send != RI_RIP_UNSPEC)
1997 vty_out (vty, " ip rip send version %s%s",
1998 lookup (ri_version_msg, ri->ri_send),
1999 VTY_NEWLINE);
2000
2001 if (ri->ri_receive != RI_RIP_UNSPEC)
2002 vty_out (vty, " ip rip receive version %s%s",
2003 lookup (ri_version_msg, ri->ri_receive),
2004 VTY_NEWLINE);
2005
2006 if (ri->v2_broadcast)
2007 vty_out (vty, " ip rip v2-broadcast%s", VTY_NEWLINE);
2008
2009 /* RIP authentication. */
2010 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
2011 vty_out (vty, " ip rip authentication mode text%s", VTY_NEWLINE);
2012
2013 if (ri->auth_type == RIP_AUTH_MD5)
2014 {
2015 vty_out (vty, " ip rip authentication mode md5");
2016 if (ri->md5_auth_len == RIP_AUTH_MD5_COMPAT_SIZE)
2017 vty_out (vty, " auth-length old-ripd");
2018 else
2019 vty_out (vty, " auth-length rfc");
2020 vty_out (vty, "%s", VTY_NEWLINE);
2021 }
2022
2023 if (ri->auth_str)
2024 vty_out (vty, " ip rip authentication string %s%s",
2025 ri->auth_str, VTY_NEWLINE);
2026
2027 if (ri->key_chain)
2028 vty_out (vty, " ip rip authentication key-chain %s%s",
2029 ri->key_chain, VTY_NEWLINE);
2030
2031 vty_out (vty, "!%s", VTY_NEWLINE);
2032 }
2033 return 0;
2034 }
2035
2036 int
2037 config_write_rip_network (struct vty *vty, int config_mode)
2038 {
2039 unsigned int i;
2040 char *ifname;
2041 struct route_node *node;
2042
2043 /* Network type RIP enable interface statement. */
2044 for (node = route_top (rip_enable_network); node; node = route_next (node))
2045 if (node->info)
2046 vty_out (vty, "%s%s/%d%s",
2047 config_mode ? " network " : " ",
2048 inet_ntoa (node->p.u.prefix4),
2049 node->p.prefixlen,
2050 VTY_NEWLINE);
2051
2052 /* Interface name RIP enable statement. */
2053 for (i = 0; i < vector_active (rip_enable_interface); i++)
2054 if ((ifname = vector_slot (rip_enable_interface, i)) != NULL)
2055 vty_out (vty, "%s%s%s",
2056 config_mode ? " network " : " ",
2057 ifname,
2058 VTY_NEWLINE);
2059
2060 /* RIP neighbors listing. */
2061 for (node = route_top (rip->neighbor); node; node = route_next (node))
2062 if (node->info)
2063 vty_out (vty, "%s%s%s",
2064 config_mode ? " neighbor " : " ",
2065 inet_ntoa (node->p.u.prefix4),
2066 VTY_NEWLINE);
2067
2068 /* RIP passive interface listing. */
2069 if (config_mode) {
2070 if (passive_default)
2071 vty_out (vty, " passive-interface default%s", VTY_NEWLINE);
2072 for (i = 0; i < vector_active (Vrip_passive_nondefault); i++)
2073 if ((ifname = vector_slot (Vrip_passive_nondefault, i)) != NULL)
2074 vty_out (vty, " %spassive-interface %s%s",
2075 (passive_default ? "no " : ""), ifname, VTY_NEWLINE);
2076 }
2077
2078 return 0;
2079 }
2080
2081 static struct cmd_node interface_node =
2082 {
2083 INTERFACE_NODE,
2084 "%s(config-if)# ",
2085 1,
2086 };
2087
2088 /* Called when interface structure allocated. */
2089 static int
2090 rip_interface_new_hook (struct interface *ifp)
2091 {
2092 ifp->info = rip_interface_new ();
2093 return 0;
2094 }
2095
2096 /* Called when interface structure deleted. */
2097 static int
2098 rip_interface_delete_hook (struct interface *ifp)
2099 {
2100 XFREE (MTYPE_RIP_INTERFACE, ifp->info);
2101 ifp->info = NULL;
2102 return 0;
2103 }
2104
2105 /* Allocate and initialize interface vector. */
2106 void
2107 rip_if_init (void)
2108 {
2109 /* Default initial size of interface vector. */
2110 if_add_hook (IF_NEW_HOOK, rip_interface_new_hook);
2111 if_add_hook (IF_DELETE_HOOK, rip_interface_delete_hook);
2112
2113 /* RIP network init. */
2114 rip_enable_interface = vector_init (1);
2115 rip_enable_network = route_table_init ();
2116
2117 /* RIP passive interface. */
2118 Vrip_passive_nondefault = vector_init (1);
2119
2120 /* Install interface node. */
2121 install_node (&interface_node, rip_interface_config_write);
2122
2123 /* Install commands. */
2124 install_element (CONFIG_NODE, &interface_cmd);
2125 install_element (CONFIG_NODE, &no_interface_cmd);
2126 install_default (INTERFACE_NODE);
2127 install_element (INTERFACE_NODE, &interface_desc_cmd);
2128 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
2129 install_element (RIP_NODE, &rip_network_cmd);
2130 install_element (RIP_NODE, &no_rip_network_cmd);
2131 install_element (RIP_NODE, &rip_neighbor_cmd);
2132 install_element (RIP_NODE, &no_rip_neighbor_cmd);
2133
2134 install_element (RIP_NODE, &rip_passive_interface_cmd);
2135 install_element (RIP_NODE, &no_rip_passive_interface_cmd);
2136
2137 install_element (INTERFACE_NODE, &ip_rip_send_version_cmd);
2138 install_element (INTERFACE_NODE, &ip_rip_send_version_1_cmd);
2139 install_element (INTERFACE_NODE, &ip_rip_send_version_2_cmd);
2140 install_element (INTERFACE_NODE, &no_ip_rip_send_version_cmd);
2141 install_element (INTERFACE_NODE, &no_ip_rip_send_version_num_cmd);
2142
2143 install_element (INTERFACE_NODE, &ip_rip_receive_version_cmd);
2144 install_element (INTERFACE_NODE, &ip_rip_receive_version_1_cmd);
2145 install_element (INTERFACE_NODE, &ip_rip_receive_version_2_cmd);
2146 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_cmd);
2147 install_element (INTERFACE_NODE, &no_ip_rip_receive_version_num_cmd);
2148
2149 install_element (INTERFACE_NODE, &ip_rip_v2_broadcast_cmd);
2150 install_element (INTERFACE_NODE, &no_ip_rip_v2_broadcast_cmd);
2151
2152 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_cmd);
2153 install_element (INTERFACE_NODE, &ip_rip_authentication_mode_authlen_cmd);
2154 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_cmd);
2155 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_cmd);
2156 install_element (INTERFACE_NODE, &no_ip_rip_authentication_mode_type_authlen_cmd);
2157
2158 install_element (INTERFACE_NODE, &ip_rip_authentication_key_chain_cmd);
2159 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain_cmd);
2160 install_element (INTERFACE_NODE, &no_ip_rip_authentication_key_chain2_cmd);
2161
2162 install_element (INTERFACE_NODE, &ip_rip_authentication_string_cmd);
2163 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string_cmd);
2164 install_element (INTERFACE_NODE, &no_ip_rip_authentication_string2_cmd);
2165
2166 install_element (INTERFACE_NODE, &ip_rip_split_horizon_cmd);
2167 install_element (INTERFACE_NODE, &ip_rip_split_horizon_poisoned_reverse_cmd);
2168 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_cmd);
2169 install_element (INTERFACE_NODE, &no_ip_rip_split_horizon_poisoned_reverse_cmd);
2170 }