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