]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
*: ditch vty_outln(), part 1 of 2
[mirror_frr.git] / ripngd / ripng_interface.c
1 /*
2 * Interface related function for RIPng.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "linklist.h"
25 #include "if.h"
26 #include "prefix.h"
27 #include "memory.h"
28 #include "network.h"
29 #include "filter.h"
30 #include "log.h"
31 #include "stream.h"
32 #include "zclient.h"
33 #include "command.h"
34 #include "table.h"
35 #include "thread.h"
36 #include "privs.h"
37 #include "vrf.h"
38
39 #include "ripngd/ripngd.h"
40 #include "ripngd/ripng_debug.h"
41
42 /* If RFC2133 definition is used. */
43 #ifndef IPV6_JOIN_GROUP
44 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
45 #endif
46 #ifndef IPV6_LEAVE_GROUP
47 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
48 #endif
49
50 extern struct zebra_privs_t ripngd_privs;
51
52 /* Static utility function. */
53 static void ripng_enable_apply (struct interface *);
54 static void ripng_passive_interface_apply (struct interface *);
55 static int ripng_enable_if_lookup (const char *);
56 static int ripng_enable_network_lookup2 (struct connected *);
57 static void ripng_enable_apply_all (void);
58
59 /* Join to the all rip routers multicast group. */
60 static int
61 ripng_multicast_join (struct interface *ifp)
62 {
63 int ret;
64 struct ipv6_mreq mreq;
65 int save_errno;
66
67 if (if_is_multicast (ifp)) {
68 memset (&mreq, 0, sizeof (mreq));
69 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
70 mreq.ipv6mr_interface = ifp->ifindex;
71
72 /*
73 * NetBSD 1.6.2 requires root to join groups on gif(4).
74 * While this is bogus, privs are available and easy to use
75 * for this call as a workaround.
76 */
77 if (ripngd_privs.change (ZPRIVS_RAISE))
78 zlog_err ("ripng_multicast_join: could not raise privs");
79
80 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
81 (char *) &mreq, sizeof (mreq));
82 save_errno = errno;
83
84 if (ripngd_privs.change (ZPRIVS_LOWER))
85 zlog_err ("ripng_multicast_join: could not lower privs");
86
87 if (ret < 0 && save_errno == EADDRINUSE)
88 {
89 /*
90 * Group is already joined. This occurs due to sloppy group
91 * management, in particular declining to leave the group on
92 * an interface that has just gone down.
93 */
94 zlog_warn ("ripng join on %s EADDRINUSE (ignoring)\n", ifp->name);
95 return 0; /* not an error */
96 }
97
98 if (ret < 0)
99 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s",
100 safe_strerror (save_errno));
101
102 if (IS_RIPNG_DEBUG_EVENT)
103 zlog_debug ("RIPng %s join to all-rip-routers multicast group", ifp->name);
104
105 if (ret < 0)
106 return -1;
107 }
108 return 0;
109 }
110
111 /* Leave from the all rip routers multicast group. */
112 static int
113 ripng_multicast_leave (struct interface *ifp)
114 {
115 int ret;
116 struct ipv6_mreq mreq;
117
118 if (if_is_multicast (ifp)) {
119 memset (&mreq, 0, sizeof (mreq));
120 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
121 mreq.ipv6mr_interface = ifp->ifindex;
122
123 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
124 (char *) &mreq, sizeof (mreq));
125 if (ret < 0)
126 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", safe_strerror (errno));
127
128 if (IS_RIPNG_DEBUG_EVENT)
129 zlog_debug ("RIPng %s leave from all-rip-routers multicast group",
130 ifp->name);
131
132 if (ret < 0)
133 return -1;
134 }
135
136 return 0;
137 }
138
139 /* How many link local IPv6 address could be used on the interface ? */
140 static int
141 ripng_if_ipv6_lladdress_check (struct interface *ifp)
142 {
143 struct listnode *nn;
144 struct connected *connected;
145 int count = 0;
146
147 for (ALL_LIST_ELEMENTS_RO (ifp->connected, nn, connected))
148 {
149 struct prefix *p;
150 p = connected->address;
151
152 if ((p->family == AF_INET6) &&
153 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
154 count++;
155 }
156
157 return count;
158 }
159
160 static int
161 ripng_if_down (struct interface *ifp)
162 {
163 struct route_node *rp;
164 struct ripng_info *rinfo;
165 struct ripng_interface *ri;
166 struct list *list = NULL;
167 struct listnode *listnode = NULL, *nextnode = NULL;
168
169 if (ripng)
170 for (rp = route_top (ripng->table); rp; rp = route_next (rp))
171 if ((list = rp->info) != NULL)
172 for (ALL_LIST_ELEMENTS (list, listnode, nextnode, rinfo))
173 if (rinfo->ifindex == ifp->ifindex)
174 ripng_ecmp_delete (rinfo);
175
176 ri = ifp->info;
177
178 if (ri->running)
179 {
180 if (IS_RIPNG_DEBUG_EVENT)
181 zlog_debug ("turn off %s", ifp->name);
182
183 /* Leave from multicast group. */
184 ripng_multicast_leave (ifp);
185
186 ri->running = 0;
187 }
188
189 return 0;
190 }
191
192 /* Inteface link up message processing. */
193 int
194 ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length,
195 vrf_id_t vrf_id)
196 {
197 struct stream *s;
198 struct interface *ifp;
199
200 /* zebra_interface_state_read() updates interface structure in iflist. */
201 s = zclient->ibuf;
202 ifp = zebra_interface_state_read (s, vrf_id);
203
204 if (ifp == NULL)
205 return 0;
206
207 if (IS_RIPNG_DEBUG_ZEBRA)
208 zlog_debug ("interface up %s index %d flags %llx metric %d mtu %d",
209 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
210 ifp->metric, ifp->mtu6);
211
212 /* Check if this interface is RIPng enabled or not. */
213 ripng_enable_apply (ifp);
214
215 /* Check for a passive interface. */
216 ripng_passive_interface_apply (ifp);
217
218 /* Apply distribute list to the all interface. */
219 ripng_distribute_update_interface (ifp);
220
221 return 0;
222 }
223
224 /* Inteface link down message processing. */
225 int
226 ripng_interface_down (int command, struct zclient *zclient,
227 zebra_size_t length, vrf_id_t vrf_id)
228 {
229 struct stream *s;
230 struct interface *ifp;
231
232 /* zebra_interface_state_read() updates interface structure in iflist. */
233 s = zclient->ibuf;
234 ifp = zebra_interface_state_read (s, vrf_id);
235
236 if (ifp == NULL)
237 return 0;
238
239 ripng_if_down (ifp);
240
241 if (IS_RIPNG_DEBUG_ZEBRA)
242 zlog_debug ("interface down %s index %d flags %#llx metric %d mtu %d",
243 ifp->name, ifp->ifindex,
244 (unsigned long long) ifp->flags, ifp->metric, ifp->mtu6);
245
246 return 0;
247 }
248
249 /* Inteface addition message from zebra. */
250 int
251 ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length,
252 vrf_id_t vrf_id)
253 {
254 struct interface *ifp;
255
256 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
257
258 if (IS_RIPNG_DEBUG_ZEBRA)
259 zlog_debug ("RIPng interface add %s index %d flags %#llx metric %d mtu %d",
260 ifp->name, ifp->ifindex, (unsigned long long) ifp->flags,
261 ifp->metric, ifp->mtu6);
262
263 /* Check is this interface is RIP enabled or not.*/
264 ripng_enable_apply (ifp);
265
266 /* Apply distribute list to the interface. */
267 ripng_distribute_update_interface (ifp);
268
269 /* Check interface routemap. */
270 ripng_if_rmap_update_interface (ifp);
271
272 return 0;
273 }
274
275 int
276 ripng_interface_delete (int command, struct zclient *zclient,
277 zebra_size_t length, vrf_id_t vrf_id)
278 {
279 struct interface *ifp;
280 struct stream *s;
281
282 s = zclient->ibuf;
283 /* zebra_interface_state_read() updates interface structure in iflist */
284 ifp = zebra_interface_state_read (s, vrf_id);
285
286 if (ifp == NULL)
287 return 0;
288
289 if (if_is_up (ifp)) {
290 ripng_if_down(ifp);
291 }
292
293 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
294 ifp->name, ifp->ifindex, (unsigned long long) ifp->flags,
295 ifp->metric, ifp->mtu6);
296
297 /* To support pseudo interface do not free interface structure. */
298 /* if_delete(ifp); */
299 ifp->ifindex = IFINDEX_DELETED;
300
301 return 0;
302 }
303
304 void
305 ripng_interface_clean (void)
306 {
307 struct listnode *node, *nnode;
308 struct interface *ifp;
309 struct ripng_interface *ri;
310
311 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
312 {
313 ri = ifp->info;
314
315 ri->enable_network = 0;
316 ri->enable_interface = 0;
317 ri->running = 0;
318
319 if (ri->t_wakeup)
320 {
321 thread_cancel (ri->t_wakeup);
322 ri->t_wakeup = NULL;
323 }
324 }
325 }
326
327 void
328 ripng_interface_reset (void)
329 {
330 struct listnode *node;
331 struct interface *ifp;
332 struct ripng_interface *ri;
333
334 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
335 {
336 ri = ifp->info;
337
338 ri->enable_network = 0;
339 ri->enable_interface = 0;
340 ri->running = 0;
341
342 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
343 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
344
345 ri->list[RIPNG_FILTER_IN] = NULL;
346 ri->list[RIPNG_FILTER_OUT] = NULL;
347
348 ri->prefix[RIPNG_FILTER_IN] = NULL;
349 ri->prefix[RIPNG_FILTER_OUT] = NULL;
350
351 if (ri->t_wakeup)
352 {
353 thread_cancel (ri->t_wakeup);
354 ri->t_wakeup = NULL;
355 }
356
357 ri->passive = 0;
358 }
359 }
360
361 static void
362 ripng_apply_address_add (struct connected *ifc) {
363 struct prefix_ipv6 address;
364 struct prefix *p;
365
366 if (!ripng)
367 return;
368
369 if (! if_is_up(ifc->ifp))
370 return;
371
372 p = ifc->address;
373
374 memset (&address, 0, sizeof (address));
375 address.family = p->family;
376 address.prefix = p->u.prefix6;
377 address.prefixlen = p->prefixlen;
378 apply_mask_ipv6(&address);
379
380 /* Check if this interface is RIP enabled or not
381 or Check if this address's prefix is RIP enabled */
382 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
383 (ripng_enable_network_lookup2(ifc) >= 0))
384 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
385 &address, ifc->ifp->ifindex, NULL, 0);
386
387 }
388
389 int
390 ripng_interface_address_add (int command, struct zclient *zclient,
391 zebra_size_t length, vrf_id_t vrf_id)
392 {
393 struct connected *c;
394 struct prefix *p;
395
396 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
397 zclient->ibuf, vrf_id);
398
399 if (c == NULL)
400 return 0;
401
402 p = c->address;
403
404 if (p->family == AF_INET6)
405 {
406 struct ripng_interface *ri = c->ifp->info;
407
408 if (IS_RIPNG_DEBUG_ZEBRA)
409 zlog_debug ("RIPng connected address %s/%d add",
410 inet6_ntoa(p->u.prefix6),
411 p->prefixlen);
412
413 /* Check is this prefix needs to be redistributed. */
414 ripng_apply_address_add(c);
415
416 /* Let's try once again whether the interface could be activated */
417 if (!ri->running) {
418 /* Check if this interface is RIP enabled or not.*/
419 ripng_enable_apply (c->ifp);
420
421 /* Apply distribute list to the interface. */
422 ripng_distribute_update_interface (c->ifp);
423
424 /* Check interface routemap. */
425 ripng_if_rmap_update_interface (c->ifp);
426 }
427
428 }
429
430 return 0;
431 }
432
433 static void
434 ripng_apply_address_del (struct connected *ifc) {
435 struct prefix_ipv6 address;
436 struct prefix *p;
437
438 if (!ripng)
439 return;
440
441 if (! if_is_up(ifc->ifp))
442 return;
443
444 p = ifc->address;
445
446 memset (&address, 0, sizeof (address));
447 address.family = p->family;
448 address.prefix = p->u.prefix6;
449 address.prefixlen = p->prefixlen;
450 apply_mask_ipv6(&address);
451
452 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
453 &address, ifc->ifp->ifindex);
454 }
455
456 int
457 ripng_interface_address_delete (int command, struct zclient *zclient,
458 zebra_size_t length, vrf_id_t vrf_id)
459 {
460 struct connected *ifc;
461 struct prefix *p;
462 char buf[INET6_ADDRSTRLEN];
463
464 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
465 zclient->ibuf, vrf_id);
466
467 if (ifc)
468 {
469 p = ifc->address;
470
471 if (p->family == AF_INET6)
472 {
473 if (IS_RIPNG_DEBUG_ZEBRA)
474 zlog_debug ("RIPng connected address %s/%d delete",
475 inet_ntop (AF_INET6, &p->u.prefix6, buf,
476 INET6_ADDRSTRLEN),
477 p->prefixlen);
478
479 /* Check wether this prefix needs to be removed. */
480 ripng_apply_address_del(ifc);
481 }
482 connected_free (ifc);
483 }
484
485 return 0;
486 }
487
488 /* RIPng enable interface vector. */
489 vector ripng_enable_if;
490
491 /* RIPng enable network table. */
492 struct route_table *ripng_enable_network;
493
494 /* Lookup RIPng enable network. */
495 /* Check wether the interface has at least a connected prefix that
496 * is within the ripng_enable_network table. */
497 static int
498 ripng_enable_network_lookup_if (struct interface *ifp)
499 {
500 struct listnode *node;
501 struct connected *connected;
502 struct prefix_ipv6 address;
503
504 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
505 {
506 struct prefix *p;
507 struct route_node *node;
508
509 p = connected->address;
510
511 if (p->family == AF_INET6)
512 {
513 address.family = AF_INET6;
514 address.prefix = p->u.prefix6;
515 address.prefixlen = IPV6_MAX_BITLEN;
516
517 node = route_node_match (ripng_enable_network,
518 (struct prefix *)&address);
519 if (node)
520 {
521 route_unlock_node (node);
522 return 1;
523 }
524 }
525 }
526 return -1;
527 }
528
529 /* Check wether connected is within the ripng_enable_network table. */
530 static int
531 ripng_enable_network_lookup2 (struct connected *connected)
532 {
533 struct prefix_ipv6 address;
534 struct prefix *p;
535
536 p = connected->address;
537
538 if (p->family == AF_INET6) {
539 struct route_node *node;
540
541 address.family = p->family;
542 address.prefix = p->u.prefix6;
543 address.prefixlen = IPV6_MAX_BITLEN;
544
545 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
546 node = route_node_match (ripng_enable_network,
547 (struct prefix *)&address);
548
549 if (node) {
550 route_unlock_node (node);
551 return 1;
552 }
553 }
554
555 return -1;
556 }
557
558 /* Add RIPng enable network. */
559 static int
560 ripng_enable_network_add (struct prefix *p)
561 {
562 struct route_node *node;
563
564 node = route_node_get (ripng_enable_network, p);
565
566 if (node->info)
567 {
568 route_unlock_node (node);
569 return -1;
570 }
571 else
572 node->info = (void *)1;
573
574 /* XXX: One should find a better solution than a generic one */
575 ripng_enable_apply_all();
576
577 return 1;
578 }
579
580 /* Delete RIPng enable network. */
581 static int
582 ripng_enable_network_delete (struct prefix *p)
583 {
584 struct route_node *node;
585
586 node = route_node_lookup (ripng_enable_network, p);
587 if (node)
588 {
589 node->info = NULL;
590
591 /* Unlock info lock. */
592 route_unlock_node (node);
593
594 /* Unlock lookup lock. */
595 route_unlock_node (node);
596
597 return 1;
598 }
599 return -1;
600 }
601
602 /* Lookup function. */
603 static int
604 ripng_enable_if_lookup (const char *ifname)
605 {
606 unsigned int i;
607 char *str;
608
609 for (i = 0; i < vector_active (ripng_enable_if); i++)
610 if ((str = vector_slot (ripng_enable_if, i)) != NULL)
611 if (strcmp (str, ifname) == 0)
612 return i;
613 return -1;
614 }
615
616 /* Add interface to ripng_enable_if. */
617 static int
618 ripng_enable_if_add (const char *ifname)
619 {
620 int ret;
621
622 ret = ripng_enable_if_lookup (ifname);
623 if (ret >= 0)
624 return -1;
625
626 vector_set (ripng_enable_if, strdup (ifname));
627
628 ripng_enable_apply_all();
629
630 return 1;
631 }
632
633 /* Delete interface from ripng_enable_if. */
634 static int
635 ripng_enable_if_delete (const char *ifname)
636 {
637 int index;
638 char *str;
639
640 index = ripng_enable_if_lookup (ifname);
641 if (index < 0)
642 return -1;
643
644 str = vector_slot (ripng_enable_if, index);
645 free (str);
646 vector_unset (ripng_enable_if, index);
647
648 ripng_enable_apply_all();
649
650 return 1;
651 }
652
653 /* Wake up interface. */
654 static int
655 ripng_interface_wakeup (struct thread *t)
656 {
657 struct interface *ifp;
658 struct ripng_interface *ri;
659
660 /* Get interface. */
661 ifp = THREAD_ARG (t);
662
663 ri = ifp->info;
664 ri->t_wakeup = NULL;
665
666 /* Join to multicast group. */
667 if (ripng_multicast_join (ifp) < 0) {
668 zlog_err ("multicast join failed, interface %s not running", ifp->name);
669 return 0;
670 }
671
672 /* Set running flag. */
673 ri->running = 1;
674
675 /* Send RIP request to the interface. */
676 ripng_request (ifp);
677
678 return 0;
679 }
680
681 static void
682 ripng_connect_set (struct interface *ifp, int set)
683 {
684 struct listnode *node, *nnode;
685 struct connected *connected;
686 struct prefix_ipv6 address;
687
688 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
689 {
690 struct prefix *p;
691 p = connected->address;
692
693 if (p->family != AF_INET6)
694 continue;
695
696 address.family = AF_INET6;
697 address.prefix = p->u.prefix6;
698 address.prefixlen = p->prefixlen;
699 apply_mask_ipv6 (&address);
700
701 if (set) {
702 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
703 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
704 (ripng_enable_network_lookup2(connected) >= 0))
705 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
706 &address, connected->ifp->ifindex, NULL, 0);
707 } else {
708 ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
709 &address, connected->ifp->ifindex);
710 if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
711 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
712 &address, connected->ifp->ifindex, NULL, 0);
713 }
714 }
715 }
716
717 /* Check RIPng is enabed on this interface. */
718 void
719 ripng_enable_apply (struct interface *ifp)
720 {
721 int ret;
722 struct ripng_interface *ri = NULL;
723
724 /* Check interface. */
725 if (! if_is_up (ifp))
726 return;
727
728 ri = ifp->info;
729
730 /* Is this interface a candidate for RIPng ? */
731 ret = ripng_enable_network_lookup_if (ifp);
732
733 /* If the interface is matched. */
734 if (ret > 0)
735 ri->enable_network = 1;
736 else
737 ri->enable_network = 0;
738
739 /* Check interface name configuration. */
740 ret = ripng_enable_if_lookup (ifp->name);
741 if (ret >= 0)
742 ri->enable_interface = 1;
743 else
744 ri->enable_interface = 0;
745
746 /* any candidate interface MUST have a link-local IPv6 address */
747 if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
748 (ri->enable_network || ri->enable_interface)) {
749 ri->enable_network = 0;
750 ri->enable_interface = 0;
751 zlog_warn("Interface %s does not have any link-local address",
752 ifp->name);
753 }
754
755 /* Update running status of the interface. */
756 if (ri->enable_network || ri->enable_interface)
757 {
758 zlog_info ("RIPng INTERFACE ON %s", ifp->name);
759
760 /* Add interface wake up thread. */
761 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
762 &ri->t_wakeup);
763
764 ripng_connect_set (ifp, 1);
765 }
766 else
767 {
768 if (ri->running)
769 {
770 /* Might as well clean up the route table as well
771 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
772 **/
773 ripng_if_down(ifp);
774
775 ripng_connect_set (ifp, 0);
776 }
777 }
778 }
779
780 /* Set distribute list to all interfaces. */
781 static void
782 ripng_enable_apply_all (void)
783 {
784 struct interface *ifp;
785 struct listnode *node;
786
787 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
788 ripng_enable_apply (ifp);
789 }
790
791 /* Clear all network and neighbor configuration */
792 void
793 ripng_clean_network ()
794 {
795 unsigned int i;
796 char *str;
797 struct route_node *rn;
798
799 /* ripng_enable_network */
800 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
801 if (rn->info) {
802 rn->info = NULL;
803 route_unlock_node(rn);
804 }
805
806 /* ripng_enable_if */
807 for (i = 0; i < vector_active (ripng_enable_if); i++)
808 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
809 free (str);
810 vector_slot (ripng_enable_if, i) = NULL;
811 }
812 }
813
814 /* Vector to store passive-interface name. */
815 vector Vripng_passive_interface;
816
817 /* Utility function for looking up passive interface settings. */
818 static int
819 ripng_passive_interface_lookup (const char *ifname)
820 {
821 unsigned int i;
822 char *str;
823
824 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
825 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
826 if (strcmp (str, ifname) == 0)
827 return i;
828 return -1;
829 }
830
831 void
832 ripng_passive_interface_apply (struct interface *ifp)
833 {
834 int ret;
835 struct ripng_interface *ri;
836
837 ri = ifp->info;
838
839 ret = ripng_passive_interface_lookup (ifp->name);
840 if (ret < 0)
841 ri->passive = 0;
842 else
843 ri->passive = 1;
844 }
845
846 static void
847 ripng_passive_interface_apply_all (void)
848 {
849 struct interface *ifp;
850 struct listnode *node;
851
852 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
853 ripng_passive_interface_apply (ifp);
854 }
855
856 /* Passive interface. */
857 static int
858 ripng_passive_interface_set (struct vty *vty, const char *ifname)
859 {
860 if (ripng_passive_interface_lookup (ifname) >= 0)
861 return CMD_WARNING;
862
863 vector_set (Vripng_passive_interface, strdup (ifname));
864
865 ripng_passive_interface_apply_all ();
866
867 return CMD_SUCCESS;
868 }
869
870 static int
871 ripng_passive_interface_unset (struct vty *vty, const char *ifname)
872 {
873 int i;
874 char *str;
875
876 i = ripng_passive_interface_lookup (ifname);
877 if (i < 0)
878 return CMD_WARNING;
879
880 str = vector_slot (Vripng_passive_interface, i);
881 free (str);
882 vector_unset (Vripng_passive_interface, i);
883
884 ripng_passive_interface_apply_all ();
885
886 return CMD_SUCCESS;
887 }
888
889 /* Free all configured RIP passive-interface settings. */
890 void
891 ripng_passive_interface_clean (void)
892 {
893 unsigned int i;
894 char *str;
895
896 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
897 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
898 {
899 free (str);
900 vector_slot (Vripng_passive_interface, i) = NULL;
901 }
902 ripng_passive_interface_apply_all ();
903 }
904
905 /* Write RIPng enable network and interface to the vty. */
906 int
907 ripng_network_write (struct vty *vty, int config_mode)
908 {
909 unsigned int i;
910 const char *ifname;
911 struct route_node *node;
912 char buf[BUFSIZ];
913
914 /* Write enable network. */
915 for (node = route_top (ripng_enable_network); node; node = route_next (node))
916 if (node->info)
917 {
918 struct prefix *p = &node->p;
919 vty_out (vty, "%s%s/%d\n",
920 config_mode ? " network " : " ",
921 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
922 p->prefixlen);
923
924 }
925
926 /* Write enable interface. */
927 for (i = 0; i < vector_active (ripng_enable_if); i++)
928 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
929 vty_out (vty, "%s%s\n",
930 config_mode ? " network " : " ",
931 ifname);
932
933 /* Write passive interface. */
934 if (config_mode)
935 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
936 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
937 vty_out (vty, " passive-interface %s\n", ifname);
938
939 return 0;
940 }
941
942 /* RIPng enable on specified interface or matched network. */
943 DEFUN (ripng_network,
944 ripng_network_cmd,
945 "network IF_OR_ADDR",
946 "RIPng enable on specified interface or network.\n"
947 "Interface or address\n")
948 {
949 int idx_if_or_addr = 1;
950 int ret;
951 struct prefix p;
952
953 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
954
955 /* Given string is IPv6 network or interface name. */
956 if (ret)
957 ret = ripng_enable_network_add (&p);
958 else
959 ret = ripng_enable_if_add (argv[idx_if_or_addr]->arg);
960
961 if (ret < 0)
962 {
963 vty_out (vty, "There is same network configuration %s\n",
964 argv[idx_if_or_addr]->arg);
965 return CMD_WARNING;
966 }
967
968 return CMD_SUCCESS;
969 }
970
971 /* RIPng enable on specified interface or matched network. */
972 DEFUN (no_ripng_network,
973 no_ripng_network_cmd,
974 "no network IF_OR_ADDR",
975 NO_STR
976 "RIPng enable on specified interface or network.\n"
977 "Interface or address\n")
978 {
979 int idx_if_or_addr = 2;
980 int ret;
981 struct prefix p;
982
983 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
984
985 /* Given string is interface name. */
986 if (ret)
987 ret = ripng_enable_network_delete (&p);
988 else
989 ret = ripng_enable_if_delete (argv[idx_if_or_addr]->arg);
990
991 if (ret < 0)
992 {
993 vty_out (vty, "can't find network %s\n",argv[idx_if_or_addr]->arg);
994 return CMD_WARNING;
995 }
996
997 return CMD_SUCCESS;
998 }
999
1000 DEFUN (ipv6_ripng_split_horizon,
1001 ipv6_ripng_split_horizon_cmd,
1002 "ipv6 ripng split-horizon",
1003 IPV6_STR
1004 "Routing Information Protocol\n"
1005 "Perform split horizon\n")
1006 {
1007 VTY_DECLVAR_CONTEXT(interface, ifp);
1008 struct ripng_interface *ri;
1009
1010 ri = ifp->info;
1011
1012 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1013 return CMD_SUCCESS;
1014 }
1015
1016 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1017 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1018 "ipv6 ripng split-horizon poisoned-reverse",
1019 IPV6_STR
1020 "Routing Information Protocol\n"
1021 "Perform split horizon\n"
1022 "With poisoned-reverse\n")
1023 {
1024 VTY_DECLVAR_CONTEXT(interface, ifp);
1025 struct ripng_interface *ri;
1026
1027 ri = ifp->info;
1028
1029 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1030 return CMD_SUCCESS;
1031 }
1032
1033 DEFUN (no_ipv6_ripng_split_horizon,
1034 no_ipv6_ripng_split_horizon_cmd,
1035 "no ipv6 ripng split-horizon [poisoned-reverse]",
1036 NO_STR
1037 IPV6_STR
1038 "Routing Information Protocol\n"
1039 "Perform split horizon\n"
1040 "With poisoned-reverse\n")
1041 {
1042 VTY_DECLVAR_CONTEXT(interface, ifp);
1043 struct ripng_interface *ri;
1044
1045 ri = ifp->info;
1046
1047 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1048 return CMD_SUCCESS;
1049 }
1050
1051 DEFUN (ripng_passive_interface,
1052 ripng_passive_interface_cmd,
1053 "passive-interface IFNAME",
1054 "Suppress routing updates on an interface\n"
1055 "Interface name\n")
1056 {
1057 int idx_ifname = 1;
1058 return ripng_passive_interface_set (vty, argv[idx_ifname]->arg);
1059 }
1060
1061 DEFUN (no_ripng_passive_interface,
1062 no_ripng_passive_interface_cmd,
1063 "no passive-interface IFNAME",
1064 NO_STR
1065 "Suppress routing updates on an interface\n"
1066 "Interface name\n")
1067 {
1068 int idx_ifname = 2;
1069 return ripng_passive_interface_unset (vty, argv[idx_ifname]->arg);
1070 }
1071
1072 static struct ripng_interface *
1073 ri_new (void)
1074 {
1075 struct ripng_interface *ri;
1076 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
1077
1078 /* Set default split-horizon behavior. If the interface is Frame
1079 Relay or SMDS is enabled, the default value for split-horizon is
1080 off. But currently Zebra does detect Frame Relay or SMDS
1081 interface. So all interface is set to split horizon. */
1082 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1083 ri->split_horizon = ri->split_horizon_default;
1084
1085 return ri;
1086 }
1087
1088 static int
1089 ripng_if_new_hook (struct interface *ifp)
1090 {
1091 ifp->info = ri_new ();
1092 return 0;
1093 }
1094
1095 /* Called when interface structure deleted. */
1096 static int
1097 ripng_if_delete_hook (struct interface *ifp)
1098 {
1099 XFREE (MTYPE_IF, ifp->info);
1100 ifp->info = NULL;
1101 return 0;
1102 }
1103
1104 /* Configuration write function for ripngd. */
1105 static int
1106 interface_config_write (struct vty *vty)
1107 {
1108 struct listnode *node;
1109 struct interface *ifp;
1110 struct ripng_interface *ri;
1111 int write = 0;
1112
1113 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
1114 {
1115 ri = ifp->info;
1116
1117 /* Do not display the interface if there is no
1118 * configuration about it.
1119 **/
1120 if ((!ifp->desc) &&
1121 (ri->split_horizon == ri->split_horizon_default))
1122 continue;
1123
1124 vty_out (vty, "interface %s\n",ifp->name);
1125 if (ifp->desc)
1126 vty_out (vty, " description %s\n",ifp->desc);
1127
1128 /* Split horizon. */
1129 if (ri->split_horizon != ri->split_horizon_default)
1130 {
1131 switch (ri->split_horizon) {
1132 case RIPNG_SPLIT_HORIZON:
1133 vty_out (vty, " ipv6 ripng split-horizon\n");
1134 break;
1135 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1136 vty_out (vty," ipv6 ripng split-horizon poisoned-reverse\n");
1137 break;
1138 case RIPNG_NO_SPLIT_HORIZON:
1139 default:
1140 vty_out (vty, " no ipv6 ripng split-horizon\n");
1141 break;
1142 }
1143 }
1144
1145 vty_out (vty, "!\n");
1146
1147 write++;
1148 }
1149 return write;
1150 }
1151
1152 /* ripngd's interface node. */
1153 static struct cmd_node interface_node =
1154 {
1155 INTERFACE_NODE,
1156 "%s(config-if)# ",
1157 1 /* VTYSH */
1158 };
1159
1160 /* Initialization of interface. */
1161 void
1162 ripng_if_init ()
1163 {
1164 /* Interface initialize. */
1165 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
1166 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
1167
1168 /* RIPng enable network init. */
1169 ripng_enable_network = route_table_init ();
1170
1171 /* RIPng enable interface init. */
1172 ripng_enable_if = vector_init (1);
1173
1174 /* RIPng passive interface. */
1175 Vripng_passive_interface = vector_init (1);
1176
1177 /* Install interface node. */
1178 install_node (&interface_node, interface_config_write);
1179 if_cmd_init ();
1180
1181 install_element (RIPNG_NODE, &ripng_network_cmd);
1182 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1183 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1184 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
1185
1186 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1187 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1188 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1189 }