]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
bgpd: encode_label call, remove unnecessary braces
[mirror_frr.git] / ripngd / ripng_interface.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Interface related function for RIPng.
4 * Copyright (C) 1998 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #include "linklist.h"
10 #include "if.h"
11 #include "prefix.h"
12 #include "memory.h"
13 #include "network.h"
14 #include "filter.h"
15 #include "log.h"
16 #include "stream.h"
17 #include "zclient.h"
18 #include "command.h"
19 #include "agg_table.h"
20 #include "frrevent.h"
21 #include "privs.h"
22 #include "vrf.h"
23 #include "lib_errors.h"
24 #include "northbound_cli.h"
25
26 #include "ripngd/ripngd.h"
27 #include "ripngd/ripng_debug.h"
28
29 /* If RFC2133 definition is used. */
30 #ifndef IPV6_JOIN_GROUP
31 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
32 #endif
33 #ifndef IPV6_LEAVE_GROUP
34 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
35 #endif
36
37 DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_IF, "ripng interface");
38
39 /* Static utility function. */
40 static void ripng_enable_apply(struct interface *);
41 static void ripng_passive_interface_apply(struct interface *);
42 static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname);
43 static int ripng_enable_network_lookup2(struct connected *);
44 static void ripng_enable_apply_all(struct ripng *ripng);
45
46 /* Join to the all rip routers multicast group. */
47 static int ripng_multicast_join(struct interface *ifp, int sock)
48 {
49 int ret;
50 struct ipv6_mreq mreq;
51 int save_errno;
52
53 if (if_is_multicast(ifp)) {
54 memset(&mreq, 0, sizeof(mreq));
55 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
56 mreq.ipv6mr_interface = ifp->ifindex;
57
58 /*
59 * NetBSD 1.6.2 requires root to join groups on gif(4).
60 * While this is bogus, privs are available and easy to use
61 * for this call as a workaround.
62 */
63 frr_with_privs(&ripngd_privs) {
64
65 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
66 (char *)&mreq, sizeof(mreq));
67 save_errno = errno;
68
69 }
70
71 if (ret < 0 && save_errno == EADDRINUSE) {
72 /*
73 * Group is already joined. This occurs due to sloppy
74 * group
75 * management, in particular declining to leave the
76 * group on
77 * an interface that has just gone down.
78 */
79 zlog_warn("ripng join on %s EADDRINUSE (ignoring)",
80 ifp->name);
81 return 0; /* not an error */
82 }
83
84 if (ret < 0)
85 zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s",
86 safe_strerror(save_errno));
87
88 if (IS_RIPNG_DEBUG_EVENT)
89 zlog_debug(
90 "RIPng %s join to all-rip-routers multicast group",
91 ifp->name);
92
93 if (ret < 0)
94 return -1;
95 }
96 return 0;
97 }
98
99 /* Leave from the all rip routers multicast group. */
100 static int ripng_multicast_leave(struct interface *ifp, int sock)
101 {
102 int ret;
103 struct ipv6_mreq mreq;
104
105 if (if_is_multicast(ifp)) {
106 memset(&mreq, 0, sizeof(mreq));
107 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
108 mreq.ipv6mr_interface = ifp->ifindex;
109
110 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
111 (char *)&mreq, sizeof(mreq));
112 if (ret < 0)
113 zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s",
114 safe_strerror(errno));
115
116 if (IS_RIPNG_DEBUG_EVENT)
117 zlog_debug(
118 "RIPng %s leave from all-rip-routers multicast group",
119 ifp->name);
120
121 if (ret < 0)
122 return -1;
123 }
124
125 return 0;
126 }
127
128 /* How many link local IPv6 address could be used on the interface ? */
129 static int ripng_if_ipv6_lladdress_check(struct interface *ifp)
130 {
131 struct listnode *nn;
132 struct connected *connected;
133 int count = 0;
134
135 for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, connected)) {
136 struct prefix *p;
137 p = connected->address;
138
139 if ((p->family == AF_INET6)
140 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
141 count++;
142 }
143
144 return count;
145 }
146
147 static int ripng_if_down(struct interface *ifp)
148 {
149 struct agg_node *rp;
150 struct ripng_info *rinfo;
151 struct ripng_interface *ri;
152 struct ripng *ripng;
153 struct list *list = NULL;
154 struct listnode *listnode = NULL, *nextnode = NULL;
155
156 ri = ifp->info;
157
158 EVENT_OFF(ri->t_wakeup);
159
160 ripng = ri->ripng;
161
162 if (ripng)
163 for (rp = agg_route_top(ripng->table); rp;
164 rp = agg_route_next(rp))
165 if ((list = rp->info) != NULL)
166 for (ALL_LIST_ELEMENTS(list, listnode, nextnode,
167 rinfo))
168 if (rinfo->ifindex == ifp->ifindex)
169 ripng_ecmp_delete(ripng, rinfo);
170
171
172 if (ri->running) {
173 if (IS_RIPNG_DEBUG_EVENT)
174 zlog_debug("turn off %s", ifp->name);
175
176 /* Leave from multicast group. */
177 if (ripng)
178 ripng_multicast_leave(ifp, ripng->sock);
179
180 ri->running = 0;
181 }
182
183 return 0;
184 }
185
186 /* Interface link up message processing. */
187 static int ripng_ifp_up(struct interface *ifp)
188 {
189 if (IS_RIPNG_DEBUG_ZEBRA)
190 zlog_debug(
191 "interface up %s vrf %s(%u) index %d flags %llx metric %d mtu %d",
192 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
193 ifp->ifindex, (unsigned long long)ifp->flags,
194 ifp->metric, ifp->mtu6);
195
196 ripng_interface_sync(ifp);
197
198 /* Check if this interface is RIPng enabled or not. */
199 ripng_enable_apply(ifp);
200
201 /* Check for a passive interface. */
202 ripng_passive_interface_apply(ifp);
203
204 /* Apply distribute list to the all interface. */
205 ripng_distribute_update_interface(ifp);
206
207 return 0;
208 }
209
210 /* Interface link down message processing. */
211 static int ripng_ifp_down(struct interface *ifp)
212 {
213 ripng_interface_sync(ifp);
214 ripng_if_down(ifp);
215
216 if (IS_RIPNG_DEBUG_ZEBRA)
217 zlog_debug(
218 "interface down %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
219 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
220 ifp->ifindex, (unsigned long long)ifp->flags,
221 ifp->metric, ifp->mtu6);
222
223 return 0;
224 }
225
226 /* Interface addition message from zebra. */
227 static int ripng_ifp_create(struct interface *ifp)
228 {
229 ripng_interface_sync(ifp);
230
231 if (IS_RIPNG_DEBUG_ZEBRA)
232 zlog_debug(
233 "RIPng interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
234 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
235 ifp->ifindex, (unsigned long long)ifp->flags,
236 ifp->metric, ifp->mtu6);
237
238 /* Check is this interface is RIP enabled or not.*/
239 ripng_enable_apply(ifp);
240
241 /* Apply distribute list to the interface. */
242 ripng_distribute_update_interface(ifp);
243
244 /* Check interface routemap. */
245 ripng_if_rmap_update_interface(ifp);
246
247 return 0;
248 }
249
250 static int ripng_ifp_destroy(struct interface *ifp)
251 {
252 ripng_interface_sync(ifp);
253 if (if_is_up(ifp)) {
254 ripng_if_down(ifp);
255 }
256
257 if (IS_RIPNG_DEBUG_ZEBRA)
258 zlog_debug(
259 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
260 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
261 ifp->ifindex, (unsigned long long)ifp->flags,
262 ifp->metric, ifp->mtu6);
263
264 return 0;
265 }
266
267 /* VRF update for an interface. */
268 int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS)
269 {
270 struct interface *ifp;
271 vrf_id_t new_vrf_id;
272
273 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
274 &new_vrf_id);
275 if (!ifp)
276 return 0;
277
278 if (IS_RIPNG_DEBUG_ZEBRA) {
279 struct vrf *nvrf = vrf_lookup_by_id(new_vrf_id);
280
281 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
282 ifp->name, ifp->vrf->name, vrf_id, VRF_LOGNAME(nvrf),
283 new_vrf_id);
284 }
285
286 if_update_to_new_vrf(ifp, new_vrf_id);
287 ripng_interface_sync(ifp);
288
289 return 0;
290 }
291
292 void ripng_interface_clean(struct ripng *ripng)
293 {
294 struct interface *ifp;
295 struct ripng_interface *ri;
296
297 FOR_ALL_INTERFACES (ripng->vrf, ifp) {
298 ri = ifp->info;
299
300 ri->enable_network = 0;
301 ri->enable_interface = 0;
302 ri->running = 0;
303
304 EVENT_OFF(ri->t_wakeup);
305 }
306 }
307
308 static void ripng_apply_address_add(struct connected *ifc)
309 {
310 struct ripng_interface *ri = ifc->ifp->info;
311 struct ripng *ripng = ri->ripng;
312 struct prefix_ipv6 address;
313 struct prefix *p;
314
315 if (!ripng)
316 return;
317
318 if (!if_is_up(ifc->ifp))
319 return;
320
321 p = ifc->address;
322
323 memset(&address, 0, sizeof(address));
324 address.family = p->family;
325 address.prefix = p->u.prefix6;
326 address.prefixlen = p->prefixlen;
327 apply_mask_ipv6(&address);
328
329 /* Check if this interface is RIP enabled or not
330 or Check if this address's prefix is RIP enabled */
331 if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0)
332 || (ripng_enable_network_lookup2(ifc) >= 0))
333 ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT,
334 RIPNG_ROUTE_INTERFACE, &address,
335 ifc->ifp->ifindex, NULL, 0);
336 }
337
338 int ripng_interface_address_add(ZAPI_CALLBACK_ARGS)
339 {
340 struct connected *c;
341 struct prefix *p;
342
343 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
344 zclient->ibuf, vrf_id);
345
346 if (c == NULL)
347 return 0;
348
349 p = c->address;
350
351 if (p->family == AF_INET6) {
352 struct ripng_interface *ri = c->ifp->info;
353
354 if (IS_RIPNG_DEBUG_ZEBRA)
355 zlog_debug("RIPng connected address %pFX add", p);
356
357 /* Check is this prefix needs to be redistributed. */
358 ripng_apply_address_add(c);
359
360 /* Let's try once again whether the interface could be activated
361 */
362 if (!ri->running) {
363 /* Check if this interface is RIP enabled or not.*/
364 ripng_enable_apply(c->ifp);
365
366 /* Apply distribute list to the interface. */
367 ripng_distribute_update_interface(c->ifp);
368
369 /* Check interface routemap. */
370 ripng_if_rmap_update_interface(c->ifp);
371 }
372 }
373
374 return 0;
375 }
376
377 static void ripng_apply_address_del(struct connected *ifc)
378 {
379 struct ripng_interface *ri = ifc->ifp->info;
380 struct ripng *ripng = ri->ripng;
381 struct prefix_ipv6 address;
382 struct prefix *p;
383
384 if (!ripng)
385 return;
386
387 if (!if_is_up(ifc->ifp))
388 return;
389
390 p = ifc->address;
391
392 memset(&address, 0, sizeof(address));
393 address.family = p->family;
394 address.prefix = p->u.prefix6;
395 address.prefixlen = p->prefixlen;
396 apply_mask_ipv6(&address);
397
398 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
399 RIPNG_ROUTE_INTERFACE, &address,
400 ifc->ifp->ifindex);
401 }
402
403 int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS)
404 {
405 struct connected *ifc;
406 struct prefix *p;
407
408 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
409 zclient->ibuf, vrf_id);
410
411 if (ifc) {
412 p = ifc->address;
413
414 if (p->family == AF_INET6) {
415 if (IS_RIPNG_DEBUG_ZEBRA)
416 zlog_debug(
417 "RIPng connected address %pFX delete",
418 p);
419
420 /* Check whether this prefix needs to be removed. */
421 ripng_apply_address_del(ifc);
422 }
423 connected_free(&ifc);
424 }
425
426 return 0;
427 }
428
429 /* Lookup RIPng enable network. */
430 /* Check whether the interface has at least a connected prefix that
431 * is within the ripng->enable_network table. */
432 static int ripng_enable_network_lookup_if(struct interface *ifp)
433 {
434 struct ripng_interface *ri = ifp->info;
435 struct ripng *ripng = ri->ripng;
436 struct listnode *node;
437 struct connected *connected;
438 struct prefix_ipv6 address;
439
440 if (!ripng)
441 return -1;
442
443 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
444 struct prefix *p;
445 struct agg_node *n;
446
447 p = connected->address;
448
449 if (p->family == AF_INET6) {
450 address.family = AF_INET6;
451 address.prefix = p->u.prefix6;
452 address.prefixlen = IPV6_MAX_BITLEN;
453
454 n = agg_node_match(ripng->enable_network,
455 (struct prefix *)&address);
456 if (n) {
457 agg_unlock_node(n);
458 return 1;
459 }
460 }
461 }
462 return -1;
463 }
464
465 /* Check whether connected is within the ripng->enable_network table. */
466 static int ripng_enable_network_lookup2(struct connected *connected)
467 {
468 struct ripng_interface *ri = connected->ifp->info;
469 struct ripng *ripng = ri->ripng;
470 struct prefix_ipv6 address;
471 struct prefix *p;
472
473 if (!ripng)
474 return -1;
475
476 p = connected->address;
477
478 if (p->family == AF_INET6) {
479 struct agg_node *node;
480
481 address.family = p->family;
482 address.prefix = p->u.prefix6;
483 address.prefixlen = IPV6_MAX_BITLEN;
484
485 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
486 * ripng->enable_network */
487 node = agg_node_match(ripng->enable_network,
488 (struct prefix *)&address);
489
490 if (node) {
491 agg_unlock_node(node);
492 return 1;
493 }
494 }
495
496 return -1;
497 }
498
499 /* Add RIPng enable network. */
500 int ripng_enable_network_add(struct ripng *ripng, struct prefix *p)
501 {
502 struct agg_node *node;
503
504 node = agg_node_get(ripng->enable_network, p);
505
506 if (node->info) {
507 agg_unlock_node(node);
508 return NB_ERR_INCONSISTENCY;
509 } else
510 node->info = (void *)1;
511
512 /* XXX: One should find a better solution than a generic one */
513 ripng_enable_apply_all(ripng);
514
515 return NB_OK;
516 }
517
518 /* Delete RIPng enable network. */
519 int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p)
520 {
521 struct agg_node *node;
522
523 node = agg_node_lookup(ripng->enable_network, p);
524 if (node) {
525 node->info = NULL;
526
527 /* Unlock info lock. */
528 agg_unlock_node(node);
529
530 /* Unlock lookup lock. */
531 agg_unlock_node(node);
532
533 return NB_OK;
534 }
535
536 return NB_ERR_INCONSISTENCY;
537 }
538
539 /* Lookup function. */
540 static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname)
541 {
542 unsigned int i;
543 char *str;
544
545 if (!ripng)
546 return -1;
547
548 for (i = 0; i < vector_active(ripng->enable_if); i++)
549 if ((str = vector_slot(ripng->enable_if, i)) != NULL)
550 if (strcmp(str, ifname) == 0)
551 return i;
552 return -1;
553 }
554
555 int ripng_enable_if_add(struct ripng *ripng, const char *ifname)
556 {
557 int ret;
558
559 ret = ripng_enable_if_lookup(ripng, ifname);
560 if (ret >= 0)
561 return NB_ERR_INCONSISTENCY;
562
563 vector_set(ripng->enable_if, strdup(ifname));
564
565 ripng_enable_apply_all(ripng);
566
567 return NB_OK;
568 }
569
570 int ripng_enable_if_delete(struct ripng *ripng, const char *ifname)
571 {
572 int index;
573 char *str;
574
575 index = ripng_enable_if_lookup(ripng, ifname);
576 if (index < 0)
577 return NB_ERR_INCONSISTENCY;
578
579 str = vector_slot(ripng->enable_if, index);
580 free(str);
581 vector_unset(ripng->enable_if, index);
582
583 ripng_enable_apply_all(ripng);
584
585 return NB_OK;
586 }
587
588 /* Wake up interface. */
589 static void ripng_interface_wakeup(struct event *t)
590 {
591 struct interface *ifp;
592 struct ripng_interface *ri;
593
594 /* Get interface. */
595 ifp = EVENT_ARG(t);
596
597 ri = ifp->info;
598
599 /* Join to multicast group. */
600 if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) {
601 flog_err_sys(EC_LIB_SOCKET,
602 "multicast join failed, interface %s not running",
603 ifp->name);
604 return;
605 }
606
607 /* Set running flag. */
608 ri->running = 1;
609
610 /* Send RIP request to the interface. */
611 ripng_request(ifp);
612 }
613
614 static void ripng_connect_set(struct interface *ifp, int set)
615 {
616 struct ripng_interface *ri = ifp->info;
617 struct ripng *ripng = ri->ripng;
618 struct listnode *node, *nnode;
619 struct connected *connected;
620 struct prefix_ipv6 address;
621
622 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
623 struct prefix *p;
624 p = connected->address;
625
626 if (p->family != AF_INET6)
627 continue;
628
629 address.family = AF_INET6;
630 address.prefix = p->u.prefix6;
631 address.prefixlen = p->prefixlen;
632 apply_mask_ipv6(&address);
633
634 if (set) {
635 /* Check once more whether this prefix is within a
636 * "network IF_OR_PREF" one */
637 if ((ripng_enable_if_lookup(ripng, connected->ifp->name)
638 >= 0)
639 || (ripng_enable_network_lookup2(connected) >= 0))
640 ripng_redistribute_add(
641 ripng, ZEBRA_ROUTE_CONNECT,
642 RIPNG_ROUTE_INTERFACE, &address,
643 connected->ifp->ifindex, NULL, 0);
644 } else {
645 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
646 RIPNG_ROUTE_INTERFACE,
647 &address,
648 connected->ifp->ifindex);
649 if (ripng_redistribute_check(ripng,
650 ZEBRA_ROUTE_CONNECT))
651 ripng_redistribute_add(
652 ripng, ZEBRA_ROUTE_CONNECT,
653 RIPNG_ROUTE_REDISTRIBUTE, &address,
654 connected->ifp->ifindex, NULL, 0);
655 }
656 }
657 }
658
659 /* Check RIPng is enabed on this interface. */
660 void ripng_enable_apply(struct interface *ifp)
661 {
662 int ret;
663 struct ripng_interface *ri = NULL;
664
665 /* Check interface. */
666 if (!if_is_up(ifp))
667 return;
668
669 ri = ifp->info;
670
671 /* Is this interface a candidate for RIPng ? */
672 ret = ripng_enable_network_lookup_if(ifp);
673
674 /* If the interface is matched. */
675 if (ret > 0)
676 ri->enable_network = 1;
677 else
678 ri->enable_network = 0;
679
680 /* Check interface name configuration. */
681 ret = ripng_enable_if_lookup(ri->ripng, ifp->name);
682 if (ret >= 0)
683 ri->enable_interface = 1;
684 else
685 ri->enable_interface = 0;
686
687 /* any candidate interface MUST have a link-local IPv6 address */
688 if ((!ripng_if_ipv6_lladdress_check(ifp))
689 && (ri->enable_network || ri->enable_interface)) {
690 ri->enable_network = 0;
691 ri->enable_interface = 0;
692 zlog_warn("Interface %s does not have any link-local address",
693 ifp->name);
694 }
695
696 /* Update running status of the interface. */
697 if (ri->enable_network || ri->enable_interface) {
698 zlog_info("RIPng INTERFACE ON %s", ifp->name);
699
700 /* Add interface wake up thread. */
701 event_add_timer(master, ripng_interface_wakeup, ifp, 1,
702 &ri->t_wakeup);
703
704 ripng_connect_set(ifp, 1);
705 } else {
706 if (ri->running) {
707 /* Might as well clean up the route table as well
708 * ripng_if_down sets to 0 ri->running, and displays
709 *"turn off %s"
710 **/
711 ripng_if_down(ifp);
712
713 ripng_connect_set(ifp, 0);
714 }
715 }
716 }
717
718 /* Set distribute list to all interfaces. */
719 static void ripng_enable_apply_all(struct ripng *ripng)
720 {
721 struct interface *ifp;
722
723 FOR_ALL_INTERFACES (ripng->vrf, ifp)
724 ripng_enable_apply(ifp);
725 }
726
727 /* Clear all network and neighbor configuration */
728 void ripng_clean_network(struct ripng *ripng)
729 {
730 unsigned int i;
731 char *str;
732 struct agg_node *rn;
733
734 /* ripng->enable_network */
735 for (rn = agg_route_top(ripng->enable_network); rn;
736 rn = agg_route_next(rn))
737 if (rn->info) {
738 rn->info = NULL;
739 agg_unlock_node(rn);
740 }
741
742 /* ripng->enable_if */
743 for (i = 0; i < vector_active(ripng->enable_if); i++)
744 if ((str = vector_slot(ripng->enable_if, i)) != NULL) {
745 free(str);
746 vector_slot(ripng->enable_if, i) = NULL;
747 }
748 }
749
750 /* Utility function for looking up passive interface settings. */
751 static int ripng_passive_interface_lookup(struct ripng *ripng,
752 const char *ifname)
753 {
754 unsigned int i;
755 char *str;
756
757 for (i = 0; i < vector_active(ripng->passive_interface); i++)
758 if ((str = vector_slot(ripng->passive_interface, i)) != NULL)
759 if (strcmp(str, ifname) == 0)
760 return i;
761 return -1;
762 }
763
764 void ripng_passive_interface_apply(struct interface *ifp)
765 {
766 int ret;
767 struct ripng_interface *ri;
768 struct ripng *ripng;
769
770 ri = ifp->info;
771 ripng = ri->ripng;
772 if (!ripng)
773 return;
774
775 ret = ripng_passive_interface_lookup(ripng, ifp->name);
776 if (ret < 0)
777 ri->passive = 0;
778 else
779 ri->passive = 1;
780 }
781
782 static void ripng_passive_interface_apply_all(struct ripng *ripng)
783 {
784 struct interface *ifp;
785
786 FOR_ALL_INTERFACES (ripng->vrf, ifp)
787 ripng_passive_interface_apply(ifp);
788 }
789
790 /* Passive interface. */
791 int ripng_passive_interface_set(struct ripng *ripng, const char *ifname)
792 {
793 if (ripng_passive_interface_lookup(ripng, ifname) >= 0)
794 return NB_ERR_INCONSISTENCY;
795
796 vector_set(ripng->passive_interface, strdup(ifname));
797
798 ripng_passive_interface_apply_all(ripng);
799
800 return NB_OK;
801 }
802
803 int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname)
804 {
805 int i;
806 char *str;
807
808 i = ripng_passive_interface_lookup(ripng, ifname);
809 if (i < 0)
810 return NB_ERR_INCONSISTENCY;
811
812 str = vector_slot(ripng->passive_interface, i);
813 free(str);
814 vector_unset(ripng->passive_interface, i);
815
816 ripng_passive_interface_apply_all(ripng);
817
818 return NB_OK;
819 }
820
821 /* Free all configured RIP passive-interface settings. */
822 void ripng_passive_interface_clean(struct ripng *ripng)
823 {
824 unsigned int i;
825 char *str;
826
827 for (i = 0; i < vector_active(ripng->passive_interface); i++)
828 if ((str = vector_slot(ripng->passive_interface, i)) != NULL) {
829 free(str);
830 vector_slot(ripng->passive_interface, i) = NULL;
831 }
832 ripng_passive_interface_apply_all(ripng);
833 }
834
835 /* Write RIPng enable network and interface to the vty. */
836 int ripng_network_write(struct vty *vty, struct ripng *ripng)
837 {
838 unsigned int i;
839 const char *ifname;
840 struct agg_node *node;
841
842 /* Write enable network. */
843 for (node = agg_route_top(ripng->enable_network); node;
844 node = agg_route_next(node))
845 if (node->info)
846 vty_out(vty, " %pRN\n", node);
847
848 /* Write enable interface. */
849 for (i = 0; i < vector_active(ripng->enable_if); i++)
850 if ((ifname = vector_slot(ripng->enable_if, i)) != NULL)
851 vty_out(vty, " %s\n", ifname);
852
853 return 0;
854 }
855
856 static struct ripng_interface *ri_new(void)
857 {
858 struct ripng_interface *ri;
859
860 ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface));
861
862 /* Set default split-horizon behavior. If the interface is Frame
863 Relay or SMDS is enabled, the default value for split-horizon is
864 off. But currently Zebra does detect Frame Relay or SMDS
865 interface. So all interface is set to split horizon. */
866 ri->split_horizon =
867 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE);
868
869 return ri;
870 }
871
872 void ripng_interface_sync(struct interface *ifp)
873 {
874 struct ripng_interface *ri;
875
876 ri = ifp->info;
877 if (ri)
878 ri->ripng = ifp->vrf->info;
879 }
880
881 static int ripng_if_new_hook(struct interface *ifp)
882 {
883 ifp->info = ri_new();
884 ripng_interface_sync(ifp);
885
886 return 0;
887 }
888
889 /* Called when interface structure deleted. */
890 static int ripng_if_delete_hook(struct interface *ifp)
891 {
892 XFREE(MTYPE_RIPNG_IF, ifp->info);
893 return 0;
894 }
895
896 /* Initialization of interface. */
897 void ripng_if_init(void)
898 {
899 /* Interface initialize. */
900 hook_register_prio(if_add, 0, ripng_if_new_hook);
901 hook_register_prio(if_del, 0, ripng_if_delete_hook);
902
903 /* Install interface node. */
904 if_cmd_init_default();
905 if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up,
906 ripng_ifp_down, ripng_ifp_destroy);
907 }