]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
ripngd: retrofit the 'passive-interface' command to the new northbound model
[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 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 NB_ERR_INCONSISTENCY;
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 NB_OK;
562 }
563
564 /* Delete RIPng enable network. */
565 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 NB_OK;
580 }
581
582 return NB_ERR_INCONSISTENCY;
583 }
584
585 /* Lookup function. */
586 static int ripng_enable_if_lookup(const char *ifname)
587 {
588 unsigned int i;
589 char *str;
590
591 for (i = 0; i < vector_active(ripng_enable_if); i++)
592 if ((str = vector_slot(ripng_enable_if, i)) != NULL)
593 if (strcmp(str, ifname) == 0)
594 return i;
595 return -1;
596 }
597
598 /* Add interface to ripng_enable_if. */
599 int ripng_enable_if_add(const char *ifname)
600 {
601 int ret;
602
603 ret = ripng_enable_if_lookup(ifname);
604 if (ret >= 0)
605 return NB_ERR_INCONSISTENCY;
606
607 vector_set(ripng_enable_if, strdup(ifname));
608
609 ripng_enable_apply_all();
610
611 return NB_OK;
612 }
613
614 /* Delete interface from ripng_enable_if. */
615 int ripng_enable_if_delete(const char *ifname)
616 {
617 int index;
618 char *str;
619
620 index = ripng_enable_if_lookup(ifname);
621 if (index < 0)
622 return NB_ERR_INCONSISTENCY;
623
624 str = vector_slot(ripng_enable_if, index);
625 free(str);
626 vector_unset(ripng_enable_if, index);
627
628 ripng_enable_apply_all();
629
630 return NB_OK;
631 }
632
633 /* Wake up interface. */
634 static int ripng_interface_wakeup(struct thread *t)
635 {
636 struct interface *ifp;
637 struct ripng_interface *ri;
638
639 /* Get interface. */
640 ifp = THREAD_ARG(t);
641
642 ri = ifp->info;
643 ri->t_wakeup = NULL;
644
645 /* Join to multicast group. */
646 if (ripng_multicast_join(ifp) < 0) {
647 flog_err_sys(EC_LIB_SOCKET,
648 "multicast join failed, interface %s not running",
649 ifp->name);
650 return 0;
651 }
652
653 /* Set running flag. */
654 ri->running = 1;
655
656 /* Send RIP request to the interface. */
657 ripng_request(ifp);
658
659 return 0;
660 }
661
662 static void ripng_connect_set(struct interface *ifp, int set)
663 {
664 struct listnode *node, *nnode;
665 struct connected *connected;
666 struct prefix_ipv6 address;
667
668 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
669 struct prefix *p;
670 p = connected->address;
671
672 if (p->family != AF_INET6)
673 continue;
674
675 address.family = AF_INET6;
676 address.prefix = p->u.prefix6;
677 address.prefixlen = p->prefixlen;
678 apply_mask_ipv6(&address);
679
680 if (set) {
681 /* Check once more wether this prefix is within a
682 * "network IF_OR_PREF" one */
683 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0)
684 || (ripng_enable_network_lookup2(connected) >= 0))
685 ripng_redistribute_add(
686 ZEBRA_ROUTE_CONNECT,
687 RIPNG_ROUTE_INTERFACE, &address,
688 connected->ifp->ifindex, NULL, 0);
689 } else {
690 ripng_redistribute_delete(
691 ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
692 &address, connected->ifp->ifindex);
693 if (ripng_redistribute_check(ZEBRA_ROUTE_CONNECT))
694 ripng_redistribute_add(
695 ZEBRA_ROUTE_CONNECT,
696 RIPNG_ROUTE_REDISTRIBUTE, &address,
697 connected->ifp->ifindex, NULL, 0);
698 }
699 }
700 }
701
702 /* Check RIPng is enabed on this interface. */
703 void ripng_enable_apply(struct interface *ifp)
704 {
705 int ret;
706 struct ripng_interface *ri = NULL;
707
708 /* Check interface. */
709 if (!if_is_up(ifp))
710 return;
711
712 ri = ifp->info;
713
714 /* Is this interface a candidate for RIPng ? */
715 ret = ripng_enable_network_lookup_if(ifp);
716
717 /* If the interface is matched. */
718 if (ret > 0)
719 ri->enable_network = 1;
720 else
721 ri->enable_network = 0;
722
723 /* Check interface name configuration. */
724 ret = ripng_enable_if_lookup(ifp->name);
725 if (ret >= 0)
726 ri->enable_interface = 1;
727 else
728 ri->enable_interface = 0;
729
730 /* any candidate interface MUST have a link-local IPv6 address */
731 if ((!ripng_if_ipv6_lladdress_check(ifp))
732 && (ri->enable_network || ri->enable_interface)) {
733 ri->enable_network = 0;
734 ri->enable_interface = 0;
735 zlog_warn("Interface %s does not have any link-local address",
736 ifp->name);
737 }
738
739 /* Update running status of the interface. */
740 if (ri->enable_network || ri->enable_interface) {
741 zlog_info("RIPng INTERFACE ON %s", ifp->name);
742
743 /* Add interface wake up thread. */
744 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
745 &ri->t_wakeup);
746
747 ripng_connect_set(ifp, 1);
748 } else {
749 if (ri->running) {
750 /* Might as well clean up the route table as well
751 * ripng_if_down sets to 0 ri->running, and displays
752 *"turn off %s"
753 **/
754 ripng_if_down(ifp);
755
756 ripng_connect_set(ifp, 0);
757 }
758 }
759 }
760
761 /* Set distribute list to all interfaces. */
762 static void ripng_enable_apply_all(void)
763 {
764 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
765 struct interface *ifp;
766
767 FOR_ALL_INTERFACES (vrf, ifp)
768 ripng_enable_apply(ifp);
769 }
770
771 /* Clear all network and neighbor configuration */
772 void ripng_clean_network()
773 {
774 unsigned int i;
775 char *str;
776 struct agg_node *rn;
777
778 /* ripng_enable_network */
779 for (rn = agg_route_top(ripng_enable_network); rn;
780 rn = agg_route_next(rn))
781 if (rn->info) {
782 rn->info = NULL;
783 agg_unlock_node(rn);
784 }
785
786 /* ripng_enable_if */
787 for (i = 0; i < vector_active(ripng_enable_if); i++)
788 if ((str = vector_slot(ripng_enable_if, i)) != NULL) {
789 free(str);
790 vector_slot(ripng_enable_if, i) = NULL;
791 }
792 }
793
794 /* Vector to store passive-interface name. */
795 vector Vripng_passive_interface;
796
797 /* Utility function for looking up passive interface settings. */
798 static int ripng_passive_interface_lookup(const char *ifname)
799 {
800 unsigned int i;
801 char *str;
802
803 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
804 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL)
805 if (strcmp(str, ifname) == 0)
806 return i;
807 return -1;
808 }
809
810 void ripng_passive_interface_apply(struct interface *ifp)
811 {
812 int ret;
813 struct ripng_interface *ri;
814
815 ri = ifp->info;
816
817 ret = ripng_passive_interface_lookup(ifp->name);
818 if (ret < 0)
819 ri->passive = 0;
820 else
821 ri->passive = 1;
822 }
823
824 static void ripng_passive_interface_apply_all(void)
825 {
826 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
827 struct interface *ifp;
828
829 FOR_ALL_INTERFACES (vrf, ifp)
830 ripng_passive_interface_apply(ifp);
831 }
832
833 /* Passive interface. */
834 int ripng_passive_interface_set(const char *ifname)
835 {
836 if (ripng_passive_interface_lookup(ifname) >= 0)
837 return NB_ERR_INCONSISTENCY;
838
839 vector_set(Vripng_passive_interface, strdup(ifname));
840
841 ripng_passive_interface_apply_all();
842
843 return NB_OK;
844 }
845
846 int ripng_passive_interface_unset(const char *ifname)
847 {
848 int i;
849 char *str;
850
851 i = ripng_passive_interface_lookup(ifname);
852 if (i < 0)
853 return NB_ERR_INCONSISTENCY;
854
855 str = vector_slot(Vripng_passive_interface, i);
856 free(str);
857 vector_unset(Vripng_passive_interface, i);
858
859 ripng_passive_interface_apply_all();
860
861 return NB_OK;
862 }
863
864 /* Free all configured RIP passive-interface settings. */
865 void ripng_passive_interface_clean(void)
866 {
867 unsigned int i;
868 char *str;
869
870 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
871 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL) {
872 free(str);
873 vector_slot(Vripng_passive_interface, i) = NULL;
874 }
875 ripng_passive_interface_apply_all();
876 }
877
878 /* Write RIPng enable network and interface to the vty. */
879 int ripng_network_write(struct vty *vty)
880 {
881 unsigned int i;
882 const char *ifname;
883 struct agg_node *node;
884 char buf[BUFSIZ];
885
886 /* Write enable network. */
887 for (node = agg_route_top(ripng_enable_network); node;
888 node = agg_route_next(node))
889 if (node->info) {
890 struct prefix *p = &node->p;
891 vty_out(vty, " %s/%d\n",
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\n", ifname);
900
901 return 0;
902 }
903
904 DEFUN (ipv6_ripng_split_horizon,
905 ipv6_ripng_split_horizon_cmd,
906 "ipv6 ripng split-horizon",
907 IPV6_STR
908 "Routing Information Protocol\n"
909 "Perform split horizon\n")
910 {
911 VTY_DECLVAR_CONTEXT(interface, ifp);
912 struct ripng_interface *ri;
913
914 ri = ifp->info;
915
916 ri->split_horizon = RIPNG_SPLIT_HORIZON;
917 return CMD_SUCCESS;
918 }
919
920 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
921 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
922 "ipv6 ripng split-horizon poisoned-reverse",
923 IPV6_STR
924 "Routing Information Protocol\n"
925 "Perform split horizon\n"
926 "With poisoned-reverse\n")
927 {
928 VTY_DECLVAR_CONTEXT(interface, ifp);
929 struct ripng_interface *ri;
930
931 ri = ifp->info;
932
933 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
934 return CMD_SUCCESS;
935 }
936
937 DEFUN (no_ipv6_ripng_split_horizon,
938 no_ipv6_ripng_split_horizon_cmd,
939 "no ipv6 ripng split-horizon [poisoned-reverse]",
940 NO_STR
941 IPV6_STR
942 "Routing Information Protocol\n"
943 "Perform split horizon\n"
944 "With poisoned-reverse\n")
945 {
946 VTY_DECLVAR_CONTEXT(interface, ifp);
947 struct ripng_interface *ri;
948
949 ri = ifp->info;
950
951 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
952 return CMD_SUCCESS;
953 }
954
955 static struct ripng_interface *ri_new(void)
956 {
957 struct ripng_interface *ri;
958 ri = XCALLOC(MTYPE_IF, sizeof(struct ripng_interface));
959
960 /* Set default split-horizon behavior. If the interface is Frame
961 Relay or SMDS is enabled, the default value for split-horizon is
962 off. But currently Zebra does detect Frame Relay or SMDS
963 interface. So all interface is set to split horizon. */
964 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
965 ri->split_horizon = ri->split_horizon_default;
966
967 return ri;
968 }
969
970 static int ripng_if_new_hook(struct interface *ifp)
971 {
972 ifp->info = ri_new();
973 return 0;
974 }
975
976 /* Called when interface structure deleted. */
977 static int ripng_if_delete_hook(struct interface *ifp)
978 {
979 XFREE(MTYPE_IF, ifp->info);
980 ifp->info = NULL;
981 return 0;
982 }
983
984 /* Configuration write function for ripngd. */
985 static int interface_config_write(struct vty *vty)
986 {
987 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
988 struct interface *ifp;
989 struct ripng_interface *ri;
990 int write = 0;
991
992 FOR_ALL_INTERFACES (vrf, ifp) {
993 ri = ifp->info;
994
995 /* Do not display the interface if there is no
996 * configuration about it.
997 **/
998 if ((!ifp->desc)
999 && (ri->split_horizon == ri->split_horizon_default))
1000 continue;
1001
1002 vty_frame(vty, "interface %s\n", ifp->name);
1003 if (ifp->desc)
1004 vty_out(vty, " description %s\n", ifp->desc);
1005
1006 /* Split horizon. */
1007 if (ri->split_horizon != ri->split_horizon_default) {
1008 switch (ri->split_horizon) {
1009 case RIPNG_SPLIT_HORIZON:
1010 vty_out(vty, " ipv6 ripng split-horizon\n");
1011 break;
1012 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1013 vty_out(vty,
1014 " ipv6 ripng split-horizon poisoned-reverse\n");
1015 break;
1016 case RIPNG_NO_SPLIT_HORIZON:
1017 default:
1018 vty_out(vty, " no ipv6 ripng split-horizon\n");
1019 break;
1020 }
1021 }
1022
1023 vty_endframe(vty, "!\n");
1024
1025 write++;
1026 }
1027 return write;
1028 }
1029
1030 /* ripngd's interface node. */
1031 static struct cmd_node interface_node = {
1032 INTERFACE_NODE, "%s(config-if)# ", 1 /* VTYSH */
1033 };
1034
1035 /* Initialization of interface. */
1036 void ripng_if_init()
1037 {
1038 /* Interface initialize. */
1039 hook_register_prio(if_add, 0, ripng_if_new_hook);
1040 hook_register_prio(if_del, 0, ripng_if_delete_hook);
1041
1042 /* RIPng enable network init. */
1043 ripng_enable_network = agg_table_init();
1044
1045 /* RIPng enable interface init. */
1046 ripng_enable_if = vector_init(1);
1047
1048 /* RIPng passive interface. */
1049 Vripng_passive_interface = vector_init(1);
1050
1051 /* Install interface node. */
1052 install_node(&interface_node, interface_config_write);
1053 if_cmd_init();
1054
1055 install_element(INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1056 install_element(INTERFACE_NODE,
1057 &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1058 install_element(INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1059 }