]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge pull request #7374 from sworleys/Revert-Revert-NHG-Dependents
[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_with_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 static int ripng_ifp_up(struct interface *ifp)
200 {
201 if (IS_RIPNG_DEBUG_ZEBRA) {
202 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
203
204 zlog_debug(
205 "interface up %s vrf %s(%u) index %d flags %llx metric %d mtu %d",
206 ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex,
207 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6);
208 }
209
210 ripng_interface_sync(ifp);
211
212 /* Check if this interface is RIPng enabled or not. */
213 ripng_enable_apply(ifp);
214
215 /* Check for a passive interface. */
216 ripng_passive_interface_apply(ifp);
217
218 /* Apply distribute list to the all interface. */
219 ripng_distribute_update_interface(ifp);
220
221 return 0;
222 }
223
224 /* Inteface link down message processing. */
225 static int ripng_ifp_down(struct interface *ifp)
226 {
227 ripng_interface_sync(ifp);
228 ripng_if_down(ifp);
229
230 if (IS_RIPNG_DEBUG_ZEBRA) {
231 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
232
233 zlog_debug(
234 "interface down %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
235 ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex,
236 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6);
237 }
238
239 return 0;
240 }
241
242 /* Inteface addition message from zebra. */
243 static int ripng_ifp_create(struct interface *ifp)
244 {
245 ripng_interface_sync(ifp);
246
247 if (IS_RIPNG_DEBUG_ZEBRA) {
248 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
249
250 zlog_debug(
251 "RIPng interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
252 ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex,
253 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6);
254 }
255
256 /* Check is this interface is RIP enabled or not.*/
257 ripng_enable_apply(ifp);
258
259 /* Apply distribute list to the interface. */
260 ripng_distribute_update_interface(ifp);
261
262 /* Check interface routemap. */
263 ripng_if_rmap_update_interface(ifp);
264
265 return 0;
266 }
267
268 static int ripng_ifp_destroy(struct interface *ifp)
269 {
270 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
271
272 ripng_interface_sync(ifp);
273 if (if_is_up(ifp)) {
274 ripng_if_down(ifp);
275 }
276
277 if (IS_RIPNG_DEBUG_ZEBRA)
278 zlog_debug(
279 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
280 ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex,
281 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6);
282
283 return 0;
284 }
285
286 /* VRF update for an interface. */
287 int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS)
288 {
289 struct interface *ifp;
290 vrf_id_t new_vrf_id;
291
292 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
293 &new_vrf_id);
294 if (!ifp)
295 return 0;
296
297 if (IS_RIPNG_DEBUG_ZEBRA) {
298 struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id);
299 struct vrf *nvrf = vrf_lookup_by_id(new_vrf_id);
300
301 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
302 ifp->name, VRF_LOGNAME(vrf), vrf_id,
303 VRF_LOGNAME(nvrf), new_vrf_id);
304 }
305
306 if_update_to_new_vrf(ifp, new_vrf_id);
307 ripng_interface_sync(ifp);
308
309 return 0;
310 }
311
312 void ripng_interface_clean(struct ripng *ripng)
313 {
314 struct interface *ifp;
315 struct ripng_interface *ri;
316
317 FOR_ALL_INTERFACES (ripng->vrf, ifp) {
318 ri = ifp->info;
319
320 ri->enable_network = 0;
321 ri->enable_interface = 0;
322 ri->running = 0;
323
324 thread_cancel(&ri->t_wakeup);
325 }
326 }
327
328 static void ripng_apply_address_add(struct connected *ifc)
329 {
330 struct ripng_interface *ri = ifc->ifp->info;
331 struct ripng *ripng = ri->ripng;
332 struct prefix_ipv6 address;
333 struct prefix *p;
334
335 if (!ripng)
336 return;
337
338 if (!if_is_up(ifc->ifp))
339 return;
340
341 p = ifc->address;
342
343 memset(&address, 0, sizeof(address));
344 address.family = p->family;
345 address.prefix = p->u.prefix6;
346 address.prefixlen = p->prefixlen;
347 apply_mask_ipv6(&address);
348
349 /* Check if this interface is RIP enabled or not
350 or Check if this address's prefix is RIP enabled */
351 if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0)
352 || (ripng_enable_network_lookup2(ifc) >= 0))
353 ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT,
354 RIPNG_ROUTE_INTERFACE, &address,
355 ifc->ifp->ifindex, NULL, 0);
356 }
357
358 int ripng_interface_address_add(ZAPI_CALLBACK_ARGS)
359 {
360 struct connected *c;
361 struct prefix *p;
362
363 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
364 zclient->ibuf, vrf_id);
365
366 if (c == NULL)
367 return 0;
368
369 p = c->address;
370
371 if (p->family == AF_INET6) {
372 struct ripng_interface *ri = c->ifp->info;
373
374 if (IS_RIPNG_DEBUG_ZEBRA)
375 zlog_debug("RIPng connected address %pFX add", p);
376
377 /* Check is this prefix needs to be redistributed. */
378 ripng_apply_address_add(c);
379
380 /* Let's try once again whether the interface could be activated
381 */
382 if (!ri->running) {
383 /* Check if this interface is RIP enabled or not.*/
384 ripng_enable_apply(c->ifp);
385
386 /* Apply distribute list to the interface. */
387 ripng_distribute_update_interface(c->ifp);
388
389 /* Check interface routemap. */
390 ripng_if_rmap_update_interface(c->ifp);
391 }
392 }
393
394 return 0;
395 }
396
397 static void ripng_apply_address_del(struct connected *ifc)
398 {
399 struct ripng_interface *ri = ifc->ifp->info;
400 struct ripng *ripng = ri->ripng;
401 struct prefix_ipv6 address;
402 struct prefix *p;
403
404 if (!ripng)
405 return;
406
407 if (!if_is_up(ifc->ifp))
408 return;
409
410 p = ifc->address;
411
412 memset(&address, 0, sizeof(address));
413 address.family = p->family;
414 address.prefix = p->u.prefix6;
415 address.prefixlen = p->prefixlen;
416 apply_mask_ipv6(&address);
417
418 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
419 RIPNG_ROUTE_INTERFACE, &address,
420 ifc->ifp->ifindex);
421 }
422
423 int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS)
424 {
425 struct connected *ifc;
426 struct prefix *p;
427
428 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
429 zclient->ibuf, vrf_id);
430
431 if (ifc) {
432 p = ifc->address;
433
434 if (p->family == AF_INET6) {
435 if (IS_RIPNG_DEBUG_ZEBRA)
436 zlog_debug(
437 "RIPng connected address %pFX delete",
438 p);
439
440 /* Check wether this prefix needs to be removed. */
441 ripng_apply_address_del(ifc);
442 }
443 connected_free(&ifc);
444 }
445
446 return 0;
447 }
448
449 /* Lookup RIPng enable network. */
450 /* Check wether the interface has at least a connected prefix that
451 * is within the ripng->enable_network table. */
452 static int ripng_enable_network_lookup_if(struct interface *ifp)
453 {
454 struct ripng_interface *ri = ifp->info;
455 struct ripng *ripng = ri->ripng;
456 struct listnode *node;
457 struct connected *connected;
458 struct prefix_ipv6 address;
459
460 if (!ripng)
461 return -1;
462
463 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
464 struct prefix *p;
465 struct agg_node *n;
466
467 p = connected->address;
468
469 if (p->family == AF_INET6) {
470 address.family = AF_INET6;
471 address.prefix = p->u.prefix6;
472 address.prefixlen = IPV6_MAX_BITLEN;
473
474 n = agg_node_match(ripng->enable_network,
475 (struct prefix *)&address);
476 if (n) {
477 agg_unlock_node(n);
478 return 1;
479 }
480 }
481 }
482 return -1;
483 }
484
485 /* Check wether connected is within the ripng->enable_network table. */
486 static int ripng_enable_network_lookup2(struct connected *connected)
487 {
488 struct ripng_interface *ri = connected->ifp->info;
489 struct ripng *ripng = ri->ripng;
490 struct prefix_ipv6 address;
491 struct prefix *p;
492
493 if (!ripng)
494 return -1;
495
496 p = connected->address;
497
498 if (p->family == AF_INET6) {
499 struct agg_node *node;
500
501 address.family = p->family;
502 address.prefix = p->u.prefix6;
503 address.prefixlen = IPV6_MAX_BITLEN;
504
505 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
506 * ripng->enable_network */
507 node = agg_node_match(ripng->enable_network,
508 (struct prefix *)&address);
509
510 if (node) {
511 agg_unlock_node(node);
512 return 1;
513 }
514 }
515
516 return -1;
517 }
518
519 /* Add RIPng enable network. */
520 int ripng_enable_network_add(struct ripng *ripng, struct prefix *p)
521 {
522 struct agg_node *node;
523
524 node = agg_node_get(ripng->enable_network, p);
525
526 if (node->info) {
527 agg_unlock_node(node);
528 return NB_ERR_INCONSISTENCY;
529 } else
530 node->info = (void *)1;
531
532 /* XXX: One should find a better solution than a generic one */
533 ripng_enable_apply_all(ripng);
534
535 return NB_OK;
536 }
537
538 /* Delete RIPng enable network. */
539 int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p)
540 {
541 struct agg_node *node;
542
543 node = agg_node_lookup(ripng->enable_network, p);
544 if (node) {
545 node->info = NULL;
546
547 /* Unlock info lock. */
548 agg_unlock_node(node);
549
550 /* Unlock lookup lock. */
551 agg_unlock_node(node);
552
553 return NB_OK;
554 }
555
556 return NB_ERR_INCONSISTENCY;
557 }
558
559 /* Lookup function. */
560 static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname)
561 {
562 unsigned int i;
563 char *str;
564
565 if (!ripng)
566 return -1;
567
568 for (i = 0; i < vector_active(ripng->enable_if); i++)
569 if ((str = vector_slot(ripng->enable_if, i)) != NULL)
570 if (strcmp(str, ifname) == 0)
571 return i;
572 return -1;
573 }
574
575 int ripng_enable_if_add(struct ripng *ripng, const char *ifname)
576 {
577 int ret;
578
579 ret = ripng_enable_if_lookup(ripng, ifname);
580 if (ret >= 0)
581 return NB_ERR_INCONSISTENCY;
582
583 vector_set(ripng->enable_if, strdup(ifname));
584
585 ripng_enable_apply_all(ripng);
586
587 return NB_OK;
588 }
589
590 int ripng_enable_if_delete(struct ripng *ripng, const char *ifname)
591 {
592 int index;
593 char *str;
594
595 index = ripng_enable_if_lookup(ripng, ifname);
596 if (index < 0)
597 return NB_ERR_INCONSISTENCY;
598
599 str = vector_slot(ripng->enable_if, index);
600 free(str);
601 vector_unset(ripng->enable_if, index);
602
603 ripng_enable_apply_all(ripng);
604
605 return NB_OK;
606 }
607
608 /* Wake up interface. */
609 static int ripng_interface_wakeup(struct thread *t)
610 {
611 struct interface *ifp;
612 struct ripng_interface *ri;
613
614 /* Get interface. */
615 ifp = THREAD_ARG(t);
616
617 ri = ifp->info;
618 ri->t_wakeup = NULL;
619
620 /* Join to multicast group. */
621 if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) {
622 flog_err_sys(EC_LIB_SOCKET,
623 "multicast join failed, interface %s not running",
624 ifp->name);
625 return 0;
626 }
627
628 /* Set running flag. */
629 ri->running = 1;
630
631 /* Send RIP request to the interface. */
632 ripng_request(ifp);
633
634 return 0;
635 }
636
637 static void ripng_connect_set(struct interface *ifp, int set)
638 {
639 struct ripng_interface *ri = ifp->info;
640 struct ripng *ripng = ri->ripng;
641 struct listnode *node, *nnode;
642 struct connected *connected;
643 struct prefix_ipv6 address;
644
645 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
646 struct prefix *p;
647 p = connected->address;
648
649 if (p->family != AF_INET6)
650 continue;
651
652 address.family = AF_INET6;
653 address.prefix = p->u.prefix6;
654 address.prefixlen = p->prefixlen;
655 apply_mask_ipv6(&address);
656
657 if (set) {
658 /* Check once more wether this prefix is within a
659 * "network IF_OR_PREF" one */
660 if ((ripng_enable_if_lookup(ripng, connected->ifp->name)
661 >= 0)
662 || (ripng_enable_network_lookup2(connected) >= 0))
663 ripng_redistribute_add(
664 ripng, ZEBRA_ROUTE_CONNECT,
665 RIPNG_ROUTE_INTERFACE, &address,
666 connected->ifp->ifindex, NULL, 0);
667 } else {
668 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
669 RIPNG_ROUTE_INTERFACE,
670 &address,
671 connected->ifp->ifindex);
672 if (ripng_redistribute_check(ripng,
673 ZEBRA_ROUTE_CONNECT))
674 ripng_redistribute_add(
675 ripng, ZEBRA_ROUTE_CONNECT,
676 RIPNG_ROUTE_REDISTRIBUTE, &address,
677 connected->ifp->ifindex, NULL, 0);
678 }
679 }
680 }
681
682 /* Check RIPng is enabed on this interface. */
683 void ripng_enable_apply(struct interface *ifp)
684 {
685 int ret;
686 struct ripng_interface *ri = NULL;
687
688 /* Check interface. */
689 if (!if_is_up(ifp))
690 return;
691
692 ri = ifp->info;
693
694 /* Is this interface a candidate for RIPng ? */
695 ret = ripng_enable_network_lookup_if(ifp);
696
697 /* If the interface is matched. */
698 if (ret > 0)
699 ri->enable_network = 1;
700 else
701 ri->enable_network = 0;
702
703 /* Check interface name configuration. */
704 ret = ripng_enable_if_lookup(ri->ripng, ifp->name);
705 if (ret >= 0)
706 ri->enable_interface = 1;
707 else
708 ri->enable_interface = 0;
709
710 /* any candidate interface MUST have a link-local IPv6 address */
711 if ((!ripng_if_ipv6_lladdress_check(ifp))
712 && (ri->enable_network || ri->enable_interface)) {
713 ri->enable_network = 0;
714 ri->enable_interface = 0;
715 zlog_warn("Interface %s does not have any link-local address",
716 ifp->name);
717 }
718
719 /* Update running status of the interface. */
720 if (ri->enable_network || ri->enable_interface) {
721 zlog_info("RIPng INTERFACE ON %s", ifp->name);
722
723 /* Add interface wake up thread. */
724 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
725 &ri->t_wakeup);
726
727 ripng_connect_set(ifp, 1);
728 } else {
729 if (ri->running) {
730 /* Might as well clean up the route table as well
731 * ripng_if_down sets to 0 ri->running, and displays
732 *"turn off %s"
733 **/
734 ripng_if_down(ifp);
735
736 ripng_connect_set(ifp, 0);
737 }
738 }
739 }
740
741 /* Set distribute list to all interfaces. */
742 static void ripng_enable_apply_all(struct ripng *ripng)
743 {
744 struct interface *ifp;
745
746 FOR_ALL_INTERFACES (ripng->vrf, ifp)
747 ripng_enable_apply(ifp);
748 }
749
750 /* Clear all network and neighbor configuration */
751 void ripng_clean_network(struct ripng *ripng)
752 {
753 unsigned int i;
754 char *str;
755 struct agg_node *rn;
756
757 /* ripng->enable_network */
758 for (rn = agg_route_top(ripng->enable_network); rn;
759 rn = agg_route_next(rn))
760 if (rn->info) {
761 rn->info = NULL;
762 agg_unlock_node(rn);
763 }
764
765 /* ripng->enable_if */
766 for (i = 0; i < vector_active(ripng->enable_if); i++)
767 if ((str = vector_slot(ripng->enable_if, i)) != NULL) {
768 free(str);
769 vector_slot(ripng->enable_if, i) = NULL;
770 }
771 }
772
773 /* Utility function for looking up passive interface settings. */
774 static int ripng_passive_interface_lookup(struct ripng *ripng,
775 const char *ifname)
776 {
777 unsigned int i;
778 char *str;
779
780 for (i = 0; i < vector_active(ripng->passive_interface); i++)
781 if ((str = vector_slot(ripng->passive_interface, i)) != NULL)
782 if (strcmp(str, ifname) == 0)
783 return i;
784 return -1;
785 }
786
787 void ripng_passive_interface_apply(struct interface *ifp)
788 {
789 int ret;
790 struct ripng_interface *ri;
791 struct ripng *ripng;
792
793 ri = ifp->info;
794 ripng = ri->ripng;
795 if (!ripng)
796 return;
797
798 ret = ripng_passive_interface_lookup(ripng, ifp->name);
799 if (ret < 0)
800 ri->passive = 0;
801 else
802 ri->passive = 1;
803 }
804
805 static void ripng_passive_interface_apply_all(struct ripng *ripng)
806 {
807 struct interface *ifp;
808
809 FOR_ALL_INTERFACES (ripng->vrf, ifp)
810 ripng_passive_interface_apply(ifp);
811 }
812
813 /* Passive interface. */
814 int ripng_passive_interface_set(struct ripng *ripng, const char *ifname)
815 {
816 if (ripng_passive_interface_lookup(ripng, ifname) >= 0)
817 return NB_ERR_INCONSISTENCY;
818
819 vector_set(ripng->passive_interface, strdup(ifname));
820
821 ripng_passive_interface_apply_all(ripng);
822
823 return NB_OK;
824 }
825
826 int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname)
827 {
828 int i;
829 char *str;
830
831 i = ripng_passive_interface_lookup(ripng, ifname);
832 if (i < 0)
833 return NB_ERR_INCONSISTENCY;
834
835 str = vector_slot(ripng->passive_interface, i);
836 free(str);
837 vector_unset(ripng->passive_interface, i);
838
839 ripng_passive_interface_apply_all(ripng);
840
841 return NB_OK;
842 }
843
844 /* Free all configured RIP passive-interface settings. */
845 void ripng_passive_interface_clean(struct ripng *ripng)
846 {
847 unsigned int i;
848 char *str;
849
850 for (i = 0; i < vector_active(ripng->passive_interface); i++)
851 if ((str = vector_slot(ripng->passive_interface, i)) != NULL) {
852 free(str);
853 vector_slot(ripng->passive_interface, i) = NULL;
854 }
855 ripng_passive_interface_apply_all(ripng);
856 }
857
858 /* Write RIPng enable network and interface to the vty. */
859 int ripng_network_write(struct vty *vty, struct ripng *ripng)
860 {
861 unsigned int i;
862 const char *ifname;
863 struct agg_node *node;
864
865 /* Write enable network. */
866 for (node = agg_route_top(ripng->enable_network); node;
867 node = agg_route_next(node))
868 if (node->info)
869 vty_out(vty, " %pRN\n", node);
870
871 /* Write enable interface. */
872 for (i = 0; i < vector_active(ripng->enable_if); i++)
873 if ((ifname = vector_slot(ripng->enable_if, i)) != NULL)
874 vty_out(vty, " %s\n", ifname);
875
876 return 0;
877 }
878
879 static struct ripng_interface *ri_new(void)
880 {
881 struct ripng_interface *ri;
882
883 ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface));
884
885 /* Set default split-horizon behavior. If the interface is Frame
886 Relay or SMDS is enabled, the default value for split-horizon is
887 off. But currently Zebra does detect Frame Relay or SMDS
888 interface. So all interface is set to split horizon. */
889 ri->split_horizon =
890 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE);
891
892 return ri;
893 }
894
895 void ripng_interface_sync(struct interface *ifp)
896 {
897 struct vrf *vrf;
898
899 vrf = vrf_lookup_by_id(ifp->vrf_id);
900 if (vrf) {
901 struct ripng_interface *ri;
902
903 ri = ifp->info;
904 if (ri)
905 ri->ripng = vrf->info;
906 }
907 }
908
909 static int ripng_if_new_hook(struct interface *ifp)
910 {
911 ifp->info = ri_new();
912 ripng_interface_sync(ifp);
913
914 return 0;
915 }
916
917 /* Called when interface structure deleted. */
918 static int ripng_if_delete_hook(struct interface *ifp)
919 {
920 XFREE(MTYPE_RIPNG_IF, ifp->info);
921 return 0;
922 }
923
924 /* Configuration write function for ripngd. */
925 static int interface_config_write(struct vty *vty)
926 {
927 struct vrf *vrf;
928 int write = 0;
929
930 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
931 struct interface *ifp;
932
933 FOR_ALL_INTERFACES (vrf, ifp) {
934 struct lyd_node *dnode;
935
936 dnode = yang_dnode_get(
937 running_config->dnode,
938 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
939 ifp->name, vrf->name);
940 if (dnode == NULL)
941 continue;
942
943 write = 1;
944 nb_cli_show_dnode_cmds(vty, dnode, false);
945 }
946 }
947
948 return write;
949 }
950
951 static int interface_config_write(struct vty *vty);
952 /* ripngd's interface node. */
953 static struct cmd_node interface_node = {
954 .name = "interface",
955 .node = INTERFACE_NODE,
956 .parent_node = CONFIG_NODE,
957 .prompt = "%s(config-if)# ",
958 .config_write = interface_config_write,
959 };
960
961 /* Initialization of interface. */
962 void ripng_if_init(void)
963 {
964 /* Interface initialize. */
965 hook_register_prio(if_add, 0, ripng_if_new_hook);
966 hook_register_prio(if_del, 0, ripng_if_delete_hook);
967
968 /* Install interface node. */
969 install_node(&interface_node);
970 if_cmd_init();
971 if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up,
972 ripng_ifp_down, ripng_ifp_destroy);
973 }