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