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