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