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