]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge branch 'frr/pull/567'
[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 {
759 if (IS_RIPNG_DEBUG_EVENT)
760 zlog_debug ("RIPng turn on %s", ifp->name);
761
762 /* Add interface wake up thread. */
763 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
764 &ri->t_wakeup);
765
766 ripng_connect_set (ifp, 1);
767 }
768 }
769 else
770 {
771 if (ri->running)
772 {
773 /* Might as well clean up the route table as well
774 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
775 **/
776 ripng_if_down(ifp);
777
778 ripng_connect_set (ifp, 0);
779 }
780 }
781 }
782
783 /* Set distribute list to all interfaces. */
784 static void
785 ripng_enable_apply_all (void)
786 {
787 struct interface *ifp;
788 struct listnode *node;
789
790 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
791 ripng_enable_apply (ifp);
792 }
793
794 /* Clear all network and neighbor configuration */
795 void
796 ripng_clean_network ()
797 {
798 unsigned int i;
799 char *str;
800 struct route_node *rn;
801
802 /* ripng_enable_network */
803 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
804 if (rn->info) {
805 rn->info = NULL;
806 route_unlock_node(rn);
807 }
808
809 /* ripng_enable_if */
810 for (i = 0; i < vector_active (ripng_enable_if); i++)
811 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
812 free (str);
813 vector_slot (ripng_enable_if, i) = NULL;
814 }
815 }
816
817 /* Vector to store passive-interface name. */
818 vector Vripng_passive_interface;
819
820 /* Utility function for looking up passive interface settings. */
821 static int
822 ripng_passive_interface_lookup (const char *ifname)
823 {
824 unsigned int i;
825 char *str;
826
827 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
828 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
829 if (strcmp (str, ifname) == 0)
830 return i;
831 return -1;
832 }
833
834 void
835 ripng_passive_interface_apply (struct interface *ifp)
836 {
837 int ret;
838 struct ripng_interface *ri;
839
840 ri = ifp->info;
841
842 ret = ripng_passive_interface_lookup (ifp->name);
843 if (ret < 0)
844 ri->passive = 0;
845 else
846 ri->passive = 1;
847 }
848
849 static void
850 ripng_passive_interface_apply_all (void)
851 {
852 struct interface *ifp;
853 struct listnode *node;
854
855 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
856 ripng_passive_interface_apply (ifp);
857 }
858
859 /* Passive interface. */
860 static int
861 ripng_passive_interface_set (struct vty *vty, const char *ifname)
862 {
863 if (ripng_passive_interface_lookup (ifname) >= 0)
864 return CMD_WARNING;
865
866 vector_set (Vripng_passive_interface, strdup (ifname));
867
868 ripng_passive_interface_apply_all ();
869
870 return CMD_SUCCESS;
871 }
872
873 static int
874 ripng_passive_interface_unset (struct vty *vty, const char *ifname)
875 {
876 int i;
877 char *str;
878
879 i = ripng_passive_interface_lookup (ifname);
880 if (i < 0)
881 return CMD_WARNING;
882
883 str = vector_slot (Vripng_passive_interface, i);
884 free (str);
885 vector_unset (Vripng_passive_interface, i);
886
887 ripng_passive_interface_apply_all ();
888
889 return CMD_SUCCESS;
890 }
891
892 /* Free all configured RIP passive-interface settings. */
893 void
894 ripng_passive_interface_clean (void)
895 {
896 unsigned int i;
897 char *str;
898
899 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
900 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
901 {
902 free (str);
903 vector_slot (Vripng_passive_interface, i) = NULL;
904 }
905 ripng_passive_interface_apply_all ();
906 }
907
908 /* Write RIPng enable network and interface to the vty. */
909 int
910 ripng_network_write (struct vty *vty, int config_mode)
911 {
912 unsigned int i;
913 const char *ifname;
914 struct route_node *node;
915 char buf[BUFSIZ];
916
917 /* Write enable network. */
918 for (node = route_top (ripng_enable_network); node; node = route_next (node))
919 if (node->info)
920 {
921 struct prefix *p = &node->p;
922 vty_out (vty, "%s%s/%d%s",
923 config_mode ? " network " : " ",
924 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
925 p->prefixlen,
926 VTY_NEWLINE);
927
928 }
929
930 /* Write enable interface. */
931 for (i = 0; i < vector_active (ripng_enable_if); i++)
932 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
933 vty_out (vty, "%s%s%s",
934 config_mode ? " network " : " ",
935 ifname,
936 VTY_NEWLINE);
937
938 /* Write passive interface. */
939 if (config_mode)
940 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
941 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
942 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
943
944 return 0;
945 }
946
947 /* RIPng enable on specified interface or matched network. */
948 DEFUN (ripng_network,
949 ripng_network_cmd,
950 "network IF_OR_ADDR",
951 "RIPng enable on specified interface or network.\n"
952 "Interface or address")
953 {
954 int idx_if_or_addr = 1;
955 int ret;
956 struct prefix p;
957
958 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
959
960 /* Given string is IPv6 network or interface name. */
961 if (ret)
962 ret = ripng_enable_network_add (&p);
963 else
964 ret = ripng_enable_if_add (argv[idx_if_or_addr]->arg);
965
966 if (ret < 0)
967 {
968 vty_out (vty, "There is same network configuration %s%s", argv[idx_if_or_addr]->arg,
969 VTY_NEWLINE);
970 return CMD_WARNING;
971 }
972
973 return CMD_SUCCESS;
974 }
975
976 /* RIPng enable on specified interface or matched network. */
977 DEFUN (no_ripng_network,
978 no_ripng_network_cmd,
979 "no network IF_OR_ADDR",
980 NO_STR
981 "RIPng enable on specified interface or network.\n"
982 "Interface or address")
983 {
984 int idx_if_or_addr = 2;
985 int ret;
986 struct prefix p;
987
988 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
989
990 /* Given string is interface name. */
991 if (ret)
992 ret = ripng_enable_network_delete (&p);
993 else
994 ret = ripng_enable_if_delete (argv[idx_if_or_addr]->arg);
995
996 if (ret < 0)
997 {
998 vty_out (vty, "can't find network %s%s", argv[idx_if_or_addr]->arg,
999 VTY_NEWLINE);
1000 return CMD_WARNING;
1001 }
1002
1003 return CMD_SUCCESS;
1004 }
1005
1006 DEFUN (ipv6_ripng_split_horizon,
1007 ipv6_ripng_split_horizon_cmd,
1008 "ipv6 ripng split-horizon",
1009 IPV6_STR
1010 "Routing Information Protocol\n"
1011 "Perform split horizon\n")
1012 {
1013 VTY_DECLVAR_CONTEXT(interface, ifp);
1014 struct ripng_interface *ri;
1015
1016 ri = ifp->info;
1017
1018 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1019 return CMD_SUCCESS;
1020 }
1021
1022 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1023 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1024 "ipv6 ripng split-horizon poisoned-reverse",
1025 IPV6_STR
1026 "Routing Information Protocol\n"
1027 "Perform split horizon\n"
1028 "With poisoned-reverse\n")
1029 {
1030 VTY_DECLVAR_CONTEXT(interface, ifp);
1031 struct ripng_interface *ri;
1032
1033 ri = ifp->info;
1034
1035 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1036 return CMD_SUCCESS;
1037 }
1038
1039 DEFUN (no_ipv6_ripng_split_horizon,
1040 no_ipv6_ripng_split_horizon_cmd,
1041 "no ipv6 ripng split-horizon [poisoned-reverse]",
1042 NO_STR
1043 IPV6_STR
1044 "Routing Information Protocol\n"
1045 "Perform split horizon\n"
1046 "With poisoned-reverse\n")
1047 {
1048 VTY_DECLVAR_CONTEXT(interface, ifp);
1049 struct ripng_interface *ri;
1050
1051 ri = ifp->info;
1052
1053 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1054 return CMD_SUCCESS;
1055 }
1056
1057 DEFUN (ripng_passive_interface,
1058 ripng_passive_interface_cmd,
1059 "passive-interface IFNAME",
1060 "Suppress routing updates on an interface\n"
1061 "Interface name\n")
1062 {
1063 int idx_ifname = 1;
1064 return ripng_passive_interface_set (vty, argv[idx_ifname]->arg);
1065 }
1066
1067 DEFUN (no_ripng_passive_interface,
1068 no_ripng_passive_interface_cmd,
1069 "no passive-interface IFNAME",
1070 NO_STR
1071 "Suppress routing updates on an interface\n"
1072 "Interface name\n")
1073 {
1074 int idx_ifname = 2;
1075 return ripng_passive_interface_unset (vty, argv[idx_ifname]->arg);
1076 }
1077
1078 static struct ripng_interface *
1079 ri_new (void)
1080 {
1081 struct ripng_interface *ri;
1082 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
1083
1084 /* Set default split-horizon behavior. If the interface is Frame
1085 Relay or SMDS is enabled, the default value for split-horizon is
1086 off. But currently Zebra does detect Frame Relay or SMDS
1087 interface. So all interface is set to split horizon. */
1088 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1089 ri->split_horizon = ri->split_horizon_default;
1090
1091 return ri;
1092 }
1093
1094 static int
1095 ripng_if_new_hook (struct interface *ifp)
1096 {
1097 ifp->info = ri_new ();
1098 return 0;
1099 }
1100
1101 /* Called when interface structure deleted. */
1102 static int
1103 ripng_if_delete_hook (struct interface *ifp)
1104 {
1105 XFREE (MTYPE_IF, ifp->info);
1106 ifp->info = NULL;
1107 return 0;
1108 }
1109
1110 /* Configuration write function for ripngd. */
1111 static int
1112 interface_config_write (struct vty *vty)
1113 {
1114 struct listnode *node;
1115 struct interface *ifp;
1116 struct ripng_interface *ri;
1117 int write = 0;
1118
1119 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
1120 {
1121 ri = ifp->info;
1122
1123 /* Do not display the interface if there is no
1124 * configuration about it.
1125 **/
1126 if ((!ifp->desc) &&
1127 (ri->split_horizon == ri->split_horizon_default))
1128 continue;
1129
1130 vty_out (vty, "interface %s%s", ifp->name,
1131 VTY_NEWLINE);
1132 if (ifp->desc)
1133 vty_out (vty, " description %s%s", ifp->desc,
1134 VTY_NEWLINE);
1135
1136 /* Split horizon. */
1137 if (ri->split_horizon != ri->split_horizon_default)
1138 {
1139 switch (ri->split_horizon) {
1140 case RIPNG_SPLIT_HORIZON:
1141 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1142 break;
1143 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1144 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1145 VTY_NEWLINE);
1146 break;
1147 case RIPNG_NO_SPLIT_HORIZON:
1148 default:
1149 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1150 break;
1151 }
1152 }
1153
1154 vty_out (vty, "!%s", VTY_NEWLINE);
1155
1156 write++;
1157 }
1158 return write;
1159 }
1160
1161 /* ripngd's interface node. */
1162 static struct cmd_node interface_node =
1163 {
1164 INTERFACE_NODE,
1165 "%s(config-if)# ",
1166 1 /* VTYSH */
1167 };
1168
1169 /* Initialization of interface. */
1170 void
1171 ripng_if_init ()
1172 {
1173 /* Interface initialize. */
1174 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
1175 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
1176
1177 /* RIPng enable network init. */
1178 ripng_enable_network = route_table_init ();
1179
1180 /* RIPng enable interface init. */
1181 ripng_enable_if = vector_init (1);
1182
1183 /* RIPng passive interface. */
1184 Vripng_passive_interface = vector_init (1);
1185
1186 /* Install interface node. */
1187 install_node (&interface_node, interface_config_write);
1188 if_cmd_init ();
1189
1190 install_element (RIPNG_NODE, &ripng_network_cmd);
1191 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1192 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1193 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
1194
1195 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1196 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1197 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1198 }