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