]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge pull request #630 from opensourcerouting/zebra-lbl-unicast-issues
[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%s",
920 config_mode ? " network " : " ",
921 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
922 p->prefixlen,
923 VTY_NEWLINE);
924
925 }
926
927 /* Write enable interface. */
928 for (i = 0; i < vector_active (ripng_enable_if); i++)
929 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
930 vty_out (vty, "%s%s%s",
931 config_mode ? " network " : " ",
932 ifname,
933 VTY_NEWLINE);
934
935 /* Write passive interface. */
936 if (config_mode)
937 for (i = 0; i < vector_active (Vripng_passive_interface); i++)
938 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
939 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
940
941 return 0;
942 }
943
944 /* RIPng enable on specified interface or matched network. */
945 DEFUN (ripng_network,
946 ripng_network_cmd,
947 "network IF_OR_ADDR",
948 "RIPng enable on specified interface or network.\n"
949 "Interface or address\n")
950 {
951 int idx_if_or_addr = 1;
952 int ret;
953 struct prefix p;
954
955 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
956
957 /* Given string is IPv6 network or interface name. */
958 if (ret)
959 ret = ripng_enable_network_add (&p);
960 else
961 ret = ripng_enable_if_add (argv[idx_if_or_addr]->arg);
962
963 if (ret < 0)
964 {
965 vty_out (vty, "There is same network configuration %s%s", argv[idx_if_or_addr]->arg,
966 VTY_NEWLINE);
967 return CMD_WARNING;
968 }
969
970 return CMD_SUCCESS;
971 }
972
973 /* RIPng enable on specified interface or matched network. */
974 DEFUN (no_ripng_network,
975 no_ripng_network_cmd,
976 "no network IF_OR_ADDR",
977 NO_STR
978 "RIPng enable on specified interface or network.\n"
979 "Interface or address\n")
980 {
981 int idx_if_or_addr = 2;
982 int ret;
983 struct prefix p;
984
985 ret = str2prefix (argv[idx_if_or_addr]->arg, &p);
986
987 /* Given string is interface name. */
988 if (ret)
989 ret = ripng_enable_network_delete (&p);
990 else
991 ret = ripng_enable_if_delete (argv[idx_if_or_addr]->arg);
992
993 if (ret < 0)
994 {
995 vty_out (vty, "can't find network %s%s", argv[idx_if_or_addr]->arg,
996 VTY_NEWLINE);
997 return CMD_WARNING;
998 }
999
1000 return CMD_SUCCESS;
1001 }
1002
1003 DEFUN (ipv6_ripng_split_horizon,
1004 ipv6_ripng_split_horizon_cmd,
1005 "ipv6 ripng split-horizon",
1006 IPV6_STR
1007 "Routing Information Protocol\n"
1008 "Perform split horizon\n")
1009 {
1010 VTY_DECLVAR_CONTEXT(interface, ifp);
1011 struct ripng_interface *ri;
1012
1013 ri = ifp->info;
1014
1015 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1016 return CMD_SUCCESS;
1017 }
1018
1019 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1020 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1021 "ipv6 ripng split-horizon poisoned-reverse",
1022 IPV6_STR
1023 "Routing Information Protocol\n"
1024 "Perform split horizon\n"
1025 "With poisoned-reverse\n")
1026 {
1027 VTY_DECLVAR_CONTEXT(interface, ifp);
1028 struct ripng_interface *ri;
1029
1030 ri = ifp->info;
1031
1032 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1033 return CMD_SUCCESS;
1034 }
1035
1036 DEFUN (no_ipv6_ripng_split_horizon,
1037 no_ipv6_ripng_split_horizon_cmd,
1038 "no ipv6 ripng split-horizon [poisoned-reverse]",
1039 NO_STR
1040 IPV6_STR
1041 "Routing Information Protocol\n"
1042 "Perform split horizon\n"
1043 "With poisoned-reverse\n")
1044 {
1045 VTY_DECLVAR_CONTEXT(interface, ifp);
1046 struct ripng_interface *ri;
1047
1048 ri = ifp->info;
1049
1050 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1051 return CMD_SUCCESS;
1052 }
1053
1054 DEFUN (ripng_passive_interface,
1055 ripng_passive_interface_cmd,
1056 "passive-interface IFNAME",
1057 "Suppress routing updates on an interface\n"
1058 "Interface name\n")
1059 {
1060 int idx_ifname = 1;
1061 return ripng_passive_interface_set (vty, argv[idx_ifname]->arg);
1062 }
1063
1064 DEFUN (no_ripng_passive_interface,
1065 no_ripng_passive_interface_cmd,
1066 "no passive-interface IFNAME",
1067 NO_STR
1068 "Suppress routing updates on an interface\n"
1069 "Interface name\n")
1070 {
1071 int idx_ifname = 2;
1072 return ripng_passive_interface_unset (vty, argv[idx_ifname]->arg);
1073 }
1074
1075 static struct ripng_interface *
1076 ri_new (void)
1077 {
1078 struct ripng_interface *ri;
1079 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
1080
1081 /* Set default split-horizon behavior. If the interface is Frame
1082 Relay or SMDS is enabled, the default value for split-horizon is
1083 off. But currently Zebra does detect Frame Relay or SMDS
1084 interface. So all interface is set to split horizon. */
1085 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1086 ri->split_horizon = ri->split_horizon_default;
1087
1088 return ri;
1089 }
1090
1091 static int
1092 ripng_if_new_hook (struct interface *ifp)
1093 {
1094 ifp->info = ri_new ();
1095 return 0;
1096 }
1097
1098 /* Called when interface structure deleted. */
1099 static int
1100 ripng_if_delete_hook (struct interface *ifp)
1101 {
1102 XFREE (MTYPE_IF, ifp->info);
1103 ifp->info = NULL;
1104 return 0;
1105 }
1106
1107 /* Configuration write function for ripngd. */
1108 static int
1109 interface_config_write (struct vty *vty)
1110 {
1111 struct listnode *node;
1112 struct interface *ifp;
1113 struct ripng_interface *ri;
1114 int write = 0;
1115
1116 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
1117 {
1118 ri = ifp->info;
1119
1120 /* Do not display the interface if there is no
1121 * configuration about it.
1122 **/
1123 if ((!ifp->desc) &&
1124 (ri->split_horizon == ri->split_horizon_default))
1125 continue;
1126
1127 vty_out (vty, "interface %s%s", ifp->name,
1128 VTY_NEWLINE);
1129 if (ifp->desc)
1130 vty_out (vty, " description %s%s", ifp->desc,
1131 VTY_NEWLINE);
1132
1133 /* Split horizon. */
1134 if (ri->split_horizon != ri->split_horizon_default)
1135 {
1136 switch (ri->split_horizon) {
1137 case RIPNG_SPLIT_HORIZON:
1138 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1139 break;
1140 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1141 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1142 VTY_NEWLINE);
1143 break;
1144 case RIPNG_NO_SPLIT_HORIZON:
1145 default:
1146 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1147 break;
1148 }
1149 }
1150
1151 vty_out (vty, "!%s", VTY_NEWLINE);
1152
1153 write++;
1154 }
1155 return write;
1156 }
1157
1158 /* ripngd's interface node. */
1159 static struct cmd_node interface_node =
1160 {
1161 INTERFACE_NODE,
1162 "%s(config-if)# ",
1163 1 /* VTYSH */
1164 };
1165
1166 /* Initialization of interface. */
1167 void
1168 ripng_if_init ()
1169 {
1170 /* Interface initialize. */
1171 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
1172 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
1173
1174 /* RIPng enable network init. */
1175 ripng_enable_network = route_table_init ();
1176
1177 /* RIPng enable interface init. */
1178 ripng_enable_if = vector_init (1);
1179
1180 /* RIPng passive interface. */
1181 Vripng_passive_interface = vector_init (1);
1182
1183 /* Install interface node. */
1184 install_node (&interface_node, interface_config_write);
1185 if_cmd_init ();
1186
1187 install_element (RIPNG_NODE, &ripng_network_cmd);
1188 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1189 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1190 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
1191
1192 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1193 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1194 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1195 }