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