]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge pull request #3502 from donaldsharp/socket_to_me_baby
[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 /* Static utility function. */
53 static void ripng_enable_apply(struct interface *);
54 static void ripng_passive_interface_apply(struct interface *);
55 static int ripng_enable_if_lookup(const char *);
56 static int ripng_enable_network_lookup2(struct connected *);
57 static void ripng_enable_apply_all(void);
58
59 /* Join to the all rip routers multicast group. */
60 static int ripng_multicast_join(struct interface *ifp)
61 {
62 int ret;
63 struct ipv6_mreq mreq;
64 int save_errno;
65
66 if (if_is_multicast(ifp)) {
67 memset(&mreq, 0, sizeof(mreq));
68 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
69 mreq.ipv6mr_interface = ifp->ifindex;
70
71 /*
72 * NetBSD 1.6.2 requires root to join groups on gif(4).
73 * While this is bogus, privs are available and easy to use
74 * for this call as a workaround.
75 */
76 frr_elevate_privs(&ripngd_privs) {
77
78 ret = setsockopt(ripng->sock, IPPROTO_IPV6,
79 IPV6_JOIN_GROUP,
80 (char *)&mreq, sizeof(mreq));
81 save_errno = errno;
82
83 }
84
85 if (ret < 0 && save_errno == EADDRINUSE) {
86 /*
87 * Group is already joined. This occurs due to sloppy
88 * group
89 * management, in particular declining to leave the
90 * group on
91 * an interface that has just gone down.
92 */
93 zlog_warn("ripng join on %s EADDRINUSE (ignoring)\n",
94 ifp->name);
95 return 0; /* not an error */
96 }
97
98 if (ret < 0)
99 zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s",
100 safe_strerror(save_errno));
101
102 if (IS_RIPNG_DEBUG_EVENT)
103 zlog_debug(
104 "RIPng %s join to all-rip-routers multicast group",
105 ifp->name);
106
107 if (ret < 0)
108 return -1;
109 }
110 return 0;
111 }
112
113 /* Leave from the all rip routers multicast group. */
114 static int ripng_multicast_leave(struct interface *ifp)
115 {
116 int ret;
117 struct ipv6_mreq mreq;
118
119 if (if_is_multicast(ifp)) {
120 memset(&mreq, 0, sizeof(mreq));
121 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
122 mreq.ipv6mr_interface = ifp->ifindex;
123
124 ret = setsockopt(ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
125 (char *)&mreq, sizeof(mreq));
126 if (ret < 0)
127 zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s\n",
128 safe_strerror(errno));
129
130 if (IS_RIPNG_DEBUG_EVENT)
131 zlog_debug(
132 "RIPng %s leave from all-rip-routers multicast group",
133 ifp->name);
134
135 if (ret < 0)
136 return -1;
137 }
138
139 return 0;
140 }
141
142 /* How many link local IPv6 address could be used on the interface ? */
143 static int ripng_if_ipv6_lladdress_check(struct interface *ifp)
144 {
145 struct listnode *nn;
146 struct connected *connected;
147 int count = 0;
148
149 for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, connected)) {
150 struct prefix *p;
151 p = connected->address;
152
153 if ((p->family == AF_INET6)
154 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
155 count++;
156 }
157
158 return count;
159 }
160
161 static int ripng_if_down(struct interface *ifp)
162 {
163 struct agg_node *rp;
164 struct ripng_info *rinfo;
165 struct ripng_interface *ri;
166 struct list *list = NULL;
167 struct listnode *listnode = NULL, *nextnode = NULL;
168
169 if (ripng)
170 for (rp = agg_route_top(ripng->table); rp;
171 rp = agg_route_next(rp))
172 if ((list = rp->info) != NULL)
173 for (ALL_LIST_ELEMENTS(list, listnode, nextnode,
174 rinfo))
175 if (rinfo->ifindex == ifp->ifindex)
176 ripng_ecmp_delete(rinfo);
177
178 ri = ifp->info;
179
180 if (ri->running) {
181 if (IS_RIPNG_DEBUG_EVENT)
182 zlog_debug("turn off %s", ifp->name);
183
184 /* Leave from multicast group. */
185 ripng_multicast_leave(ifp);
186
187 ri->running = 0;
188 }
189
190 return 0;
191 }
192
193 /* Inteface link up message processing. */
194 int ripng_interface_up(int command, struct zclient *zclient,
195 zebra_size_t length, vrf_id_t vrf_id)
196 {
197 struct stream *s;
198 struct interface *ifp;
199
200 /* zebra_interface_state_read() updates interface structure in iflist.
201 */
202 s = zclient->ibuf;
203 ifp = zebra_interface_state_read(s, vrf_id);
204
205 if (ifp == NULL)
206 return 0;
207
208 if (IS_RIPNG_DEBUG_ZEBRA)
209 zlog_debug(
210 "interface up %s index %d flags %llx metric %d mtu %d",
211 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
212 ifp->metric, ifp->mtu6);
213
214 /* Check if this interface is RIPng enabled or not. */
215 ripng_enable_apply(ifp);
216
217 /* Check for a passive interface. */
218 ripng_passive_interface_apply(ifp);
219
220 /* Apply distribute list to the all interface. */
221 ripng_distribute_update_interface(ifp);
222
223 return 0;
224 }
225
226 /* Inteface link down message processing. */
227 int ripng_interface_down(int command, struct zclient *zclient,
228 zebra_size_t length, vrf_id_t vrf_id)
229 {
230 struct stream *s;
231 struct interface *ifp;
232
233 /* zebra_interface_state_read() updates interface structure in iflist.
234 */
235 s = zclient->ibuf;
236 ifp = zebra_interface_state_read(s, vrf_id);
237
238 if (ifp == NULL)
239 return 0;
240
241 ripng_if_down(ifp);
242
243 if (IS_RIPNG_DEBUG_ZEBRA)
244 zlog_debug(
245 "interface down %s index %d flags %#llx metric %d mtu %d",
246 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
247 ifp->metric, ifp->mtu6);
248
249 return 0;
250 }
251
252 /* Inteface addition message from zebra. */
253 int ripng_interface_add(int command, struct zclient *zclient,
254 zebra_size_t length, vrf_id_t vrf_id)
255 {
256 struct interface *ifp;
257
258 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
259
260 if (IS_RIPNG_DEBUG_ZEBRA)
261 zlog_debug(
262 "RIPng interface add %s index %d flags %#llx metric %d mtu %d",
263 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
264 ifp->metric, ifp->mtu6);
265
266 /* Check is this interface is RIP enabled or not.*/
267 ripng_enable_apply(ifp);
268
269 /* Apply distribute list to the interface. */
270 ripng_distribute_update_interface(ifp);
271
272 /* Check interface routemap. */
273 ripng_if_rmap_update_interface(ifp);
274
275 return 0;
276 }
277
278 int ripng_interface_delete(int command, struct zclient *zclient,
279 zebra_size_t length, vrf_id_t vrf_id)
280 {
281 struct interface *ifp;
282 struct stream *s;
283
284 s = zclient->ibuf;
285 /* zebra_interface_state_read() updates interface structure in iflist
286 */
287 ifp = zebra_interface_state_read(s, vrf_id);
288
289 if (ifp == NULL)
290 return 0;
291
292 if (if_is_up(ifp)) {
293 ripng_if_down(ifp);
294 }
295
296 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
297 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
298 ifp->metric, ifp->mtu6);
299
300 /* To support pseudo interface do not free interface structure. */
301 /* if_delete(ifp); */
302 if_set_index(ifp, IFINDEX_INTERNAL);
303
304 return 0;
305 }
306
307 void ripng_interface_clean(void)
308 {
309 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
310 struct interface *ifp;
311 struct ripng_interface *ri;
312
313 FOR_ALL_INTERFACES (vrf, ifp) {
314 ri = ifp->info;
315
316 ri->enable_network = 0;
317 ri->enable_interface = 0;
318 ri->running = 0;
319
320 if (ri->t_wakeup) {
321 thread_cancel(ri->t_wakeup);
322 ri->t_wakeup = NULL;
323 }
324 }
325 }
326
327 static void ripng_apply_address_add(struct connected *ifc)
328 {
329 struct prefix_ipv6 address;
330 struct prefix *p;
331
332 if (!ripng)
333 return;
334
335 if (!if_is_up(ifc->ifp))
336 return;
337
338 p = ifc->address;
339
340 memset(&address, 0, sizeof(address));
341 address.family = p->family;
342 address.prefix = p->u.prefix6;
343 address.prefixlen = p->prefixlen;
344 apply_mask_ipv6(&address);
345
346 /* Check if this interface is RIP enabled or not
347 or Check if this address's prefix is RIP enabled */
348 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0)
349 || (ripng_enable_network_lookup2(ifc) >= 0))
350 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT,
351 RIPNG_ROUTE_INTERFACE, &address,
352 ifc->ifp->ifindex, NULL, 0);
353 }
354
355 int ripng_interface_address_add(int command, struct zclient *zclient,
356 zebra_size_t length, vrf_id_t vrf_id)
357 {
358 struct connected *c;
359 struct prefix *p;
360
361 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
362 zclient->ibuf, vrf_id);
363
364 if (c == NULL)
365 return 0;
366
367 p = c->address;
368
369 if (p->family == AF_INET6) {
370 struct ripng_interface *ri = c->ifp->info;
371
372 if (IS_RIPNG_DEBUG_ZEBRA)
373 zlog_debug("RIPng connected address %s/%d add",
374 inet6_ntoa(p->u.prefix6), p->prefixlen);
375
376 /* Check is this prefix needs to be redistributed. */
377 ripng_apply_address_add(c);
378
379 /* Let's try once again whether the interface could be activated
380 */
381 if (!ri->running) {
382 /* Check if this interface is RIP enabled or not.*/
383 ripng_enable_apply(c->ifp);
384
385 /* Apply distribute list to the interface. */
386 ripng_distribute_update_interface(c->ifp);
387
388 /* Check interface routemap. */
389 ripng_if_rmap_update_interface(c->ifp);
390 }
391 }
392
393 return 0;
394 }
395
396 static void ripng_apply_address_del(struct connected *ifc)
397 {
398 struct prefix_ipv6 address;
399 struct prefix *p;
400
401 if (!ripng)
402 return;
403
404 if (!if_is_up(ifc->ifp))
405 return;
406
407 p = ifc->address;
408
409 memset(&address, 0, sizeof(address));
410 address.family = p->family;
411 address.prefix = p->u.prefix6;
412 address.prefixlen = p->prefixlen;
413 apply_mask_ipv6(&address);
414
415 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
416 &address, ifc->ifp->ifindex);
417 }
418
419 int ripng_interface_address_delete(int command, struct zclient *zclient,
420 zebra_size_t length, vrf_id_t vrf_id)
421 {
422 struct connected *ifc;
423 struct prefix *p;
424 char buf[INET6_ADDRSTRLEN];
425
426 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
427 zclient->ibuf, vrf_id);
428
429 if (ifc) {
430 p = ifc->address;
431
432 if (p->family == AF_INET6) {
433 if (IS_RIPNG_DEBUG_ZEBRA)
434 zlog_debug(
435 "RIPng connected address %s/%d delete",
436 inet_ntop(AF_INET6, &p->u.prefix6, buf,
437 INET6_ADDRSTRLEN),
438 p->prefixlen);
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 /* RIPng enable interface vector. */
450 vector ripng_enable_if;
451
452 /* RIPng enable network table. */
453 struct agg_table *ripng_enable_network;
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 listnode *node;
461 struct connected *connected;
462 struct prefix_ipv6 address;
463
464 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
465 struct prefix *p;
466 struct agg_node *n;
467
468 p = connected->address;
469
470 if (p->family == AF_INET6) {
471 address.family = AF_INET6;
472 address.prefix = p->u.prefix6;
473 address.prefixlen = IPV6_MAX_BITLEN;
474
475 n = agg_node_match(ripng_enable_network,
476 (struct prefix *)&address);
477 if (n) {
478 agg_unlock_node(n);
479 return 1;
480 }
481 }
482 }
483 return -1;
484 }
485
486 /* Check wether connected is within the ripng_enable_network table. */
487 static int ripng_enable_network_lookup2(struct connected *connected)
488 {
489 struct prefix_ipv6 address;
490 struct prefix *p;
491
492 p = connected->address;
493
494 if (p->family == AF_INET6) {
495 struct agg_node *node;
496
497 address.family = p->family;
498 address.prefix = p->u.prefix6;
499 address.prefixlen = IPV6_MAX_BITLEN;
500
501 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
502 * ripng_enable_network */
503 node = agg_node_match(ripng_enable_network,
504 (struct prefix *)&address);
505
506 if (node) {
507 agg_unlock_node(node);
508 return 1;
509 }
510 }
511
512 return -1;
513 }
514
515 /* Add RIPng enable network. */
516 int ripng_enable_network_add(struct prefix *p)
517 {
518 struct agg_node *node;
519
520 node = agg_node_get(ripng_enable_network, p);
521
522 if (node->info) {
523 agg_unlock_node(node);
524 return NB_ERR_INCONSISTENCY;
525 } else
526 node->info = (void *)1;
527
528 /* XXX: One should find a better solution than a generic one */
529 ripng_enable_apply_all();
530
531 return NB_OK;
532 }
533
534 /* Delete RIPng enable network. */
535 int ripng_enable_network_delete(struct prefix *p)
536 {
537 struct agg_node *node;
538
539 node = agg_node_lookup(ripng_enable_network, p);
540 if (node) {
541 node->info = NULL;
542
543 /* Unlock info lock. */
544 agg_unlock_node(node);
545
546 /* Unlock lookup lock. */
547 agg_unlock_node(node);
548
549 return NB_OK;
550 }
551
552 return NB_ERR_INCONSISTENCY;
553 }
554
555 /* Lookup function. */
556 static int ripng_enable_if_lookup(const char *ifname)
557 {
558 unsigned int i;
559 char *str;
560
561 for (i = 0; i < vector_active(ripng_enable_if); i++)
562 if ((str = vector_slot(ripng_enable_if, i)) != NULL)
563 if (strcmp(str, ifname) == 0)
564 return i;
565 return -1;
566 }
567
568 /* Add interface to ripng_enable_if. */
569 int ripng_enable_if_add(const char *ifname)
570 {
571 int ret;
572
573 ret = ripng_enable_if_lookup(ifname);
574 if (ret >= 0)
575 return NB_ERR_INCONSISTENCY;
576
577 vector_set(ripng_enable_if, strdup(ifname));
578
579 ripng_enable_apply_all();
580
581 return NB_OK;
582 }
583
584 /* Delete interface from ripng_enable_if. */
585 int ripng_enable_if_delete(const char *ifname)
586 {
587 int index;
588 char *str;
589
590 index = ripng_enable_if_lookup(ifname);
591 if (index < 0)
592 return NB_ERR_INCONSISTENCY;
593
594 str = vector_slot(ripng_enable_if, index);
595 free(str);
596 vector_unset(ripng_enable_if, index);
597
598 ripng_enable_apply_all();
599
600 return NB_OK;
601 }
602
603 /* Wake up interface. */
604 static int ripng_interface_wakeup(struct thread *t)
605 {
606 struct interface *ifp;
607 struct ripng_interface *ri;
608
609 /* Get interface. */
610 ifp = THREAD_ARG(t);
611
612 ri = ifp->info;
613 ri->t_wakeup = NULL;
614
615 /* Join to multicast group. */
616 if (ripng_multicast_join(ifp) < 0) {
617 flog_err_sys(EC_LIB_SOCKET,
618 "multicast join failed, interface %s not running",
619 ifp->name);
620 return 0;
621 }
622
623 /* Set running flag. */
624 ri->running = 1;
625
626 /* Send RIP request to the interface. */
627 ripng_request(ifp);
628
629 return 0;
630 }
631
632 static void ripng_connect_set(struct interface *ifp, int set)
633 {
634 struct listnode *node, *nnode;
635 struct connected *connected;
636 struct prefix_ipv6 address;
637
638 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
639 struct prefix *p;
640 p = connected->address;
641
642 if (p->family != AF_INET6)
643 continue;
644
645 address.family = AF_INET6;
646 address.prefix = p->u.prefix6;
647 address.prefixlen = p->prefixlen;
648 apply_mask_ipv6(&address);
649
650 if (set) {
651 /* Check once more wether this prefix is within a
652 * "network IF_OR_PREF" one */
653 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0)
654 || (ripng_enable_network_lookup2(connected) >= 0))
655 ripng_redistribute_add(
656 ZEBRA_ROUTE_CONNECT,
657 RIPNG_ROUTE_INTERFACE, &address,
658 connected->ifp->ifindex, NULL, 0);
659 } else {
660 ripng_redistribute_delete(
661 ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
662 &address, connected->ifp->ifindex);
663 if (ripng_redistribute_check(ZEBRA_ROUTE_CONNECT))
664 ripng_redistribute_add(
665 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(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(void)
733 {
734 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
735 struct interface *ifp;
736
737 FOR_ALL_INTERFACES (vrf, ifp)
738 ripng_enable_apply(ifp);
739 }
740
741 /* Clear all network and neighbor configuration */
742 void ripng_clean_network()
743 {
744 unsigned int i;
745 char *str;
746 struct agg_node *rn;
747
748 /* ripng_enable_network */
749 for (rn = agg_route_top(ripng_enable_network); rn;
750 rn = agg_route_next(rn))
751 if (rn->info) {
752 rn->info = NULL;
753 agg_unlock_node(rn);
754 }
755
756 /* ripng_enable_if */
757 for (i = 0; i < vector_active(ripng_enable_if); i++)
758 if ((str = vector_slot(ripng_enable_if, i)) != NULL) {
759 free(str);
760 vector_slot(ripng_enable_if, i) = NULL;
761 }
762 }
763
764 /* Vector to store passive-interface name. */
765 vector Vripng_passive_interface;
766
767 /* Utility function for looking up passive interface settings. */
768 static int ripng_passive_interface_lookup(const char *ifname)
769 {
770 unsigned int i;
771 char *str;
772
773 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
774 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL)
775 if (strcmp(str, ifname) == 0)
776 return i;
777 return -1;
778 }
779
780 void ripng_passive_interface_apply(struct interface *ifp)
781 {
782 int ret;
783 struct ripng_interface *ri;
784
785 ri = ifp->info;
786
787 ret = ripng_passive_interface_lookup(ifp->name);
788 if (ret < 0)
789 ri->passive = 0;
790 else
791 ri->passive = 1;
792 }
793
794 static void ripng_passive_interface_apply_all(void)
795 {
796 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
797 struct interface *ifp;
798
799 FOR_ALL_INTERFACES (vrf, ifp)
800 ripng_passive_interface_apply(ifp);
801 }
802
803 /* Passive interface. */
804 int ripng_passive_interface_set(const char *ifname)
805 {
806 if (ripng_passive_interface_lookup(ifname) >= 0)
807 return NB_ERR_INCONSISTENCY;
808
809 vector_set(Vripng_passive_interface, strdup(ifname));
810
811 ripng_passive_interface_apply_all();
812
813 return NB_OK;
814 }
815
816 int ripng_passive_interface_unset(const char *ifname)
817 {
818 int i;
819 char *str;
820
821 i = ripng_passive_interface_lookup(ifname);
822 if (i < 0)
823 return NB_ERR_INCONSISTENCY;
824
825 str = vector_slot(Vripng_passive_interface, i);
826 free(str);
827 vector_unset(Vripng_passive_interface, i);
828
829 ripng_passive_interface_apply_all();
830
831 return NB_OK;
832 }
833
834 /* Free all configured RIP passive-interface settings. */
835 void ripng_passive_interface_clean(void)
836 {
837 unsigned int i;
838 char *str;
839
840 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
841 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL) {
842 free(str);
843 vector_slot(Vripng_passive_interface, i) = NULL;
844 }
845 ripng_passive_interface_apply_all();
846 }
847
848 /* Write RIPng enable network and interface to the vty. */
849 int ripng_network_write(struct vty *vty)
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 ri = XCALLOC(MTYPE_IF, sizeof(struct ripng_interface));
878
879 /* Set default split-horizon behavior. If the interface is Frame
880 Relay or SMDS is enabled, the default value for split-horizon is
881 off. But currently Zebra does detect Frame Relay or SMDS
882 interface. So all interface is set to split horizon. */
883 ri->split_horizon =
884 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE);
885
886 return ri;
887 }
888
889 static int ripng_if_new_hook(struct interface *ifp)
890 {
891 ifp->info = ri_new();
892 return 0;
893 }
894
895 /* Called when interface structure deleted. */
896 static int ripng_if_delete_hook(struct interface *ifp)
897 {
898 XFREE(MTYPE_IF, ifp->info);
899 ifp->info = NULL;
900 return 0;
901 }
902
903 /* Configuration write function for ripngd. */
904 static int interface_config_write(struct vty *vty)
905 {
906 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
907 struct interface *ifp;
908 int write = 0;
909
910 FOR_ALL_INTERFACES (vrf, ifp) {
911 struct lyd_node *dnode;
912
913 dnode = yang_dnode_get(
914 running_config->dnode,
915 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
916 ifp->name, vrf->name);
917 if (dnode == NULL)
918 continue;
919
920 write = 1;
921 nb_cli_show_dnode_cmds(vty, dnode, false);
922 }
923
924 return write;
925 }
926
927 /* ripngd's interface node. */
928 static struct cmd_node interface_node = {
929 INTERFACE_NODE, "%s(config-if)# ", 1 /* VTYSH */
930 };
931
932 /* Initialization of interface. */
933 void ripng_if_init()
934 {
935 /* Interface initialize. */
936 hook_register_prio(if_add, 0, ripng_if_new_hook);
937 hook_register_prio(if_del, 0, ripng_if_delete_hook);
938
939 /* RIPng enable network init. */
940 ripng_enable_network = agg_table_init();
941
942 /* RIPng enable interface init. */
943 ripng_enable_if = vector_init(1);
944
945 /* RIPng passive interface. */
946 Vripng_passive_interface = vector_init(1);
947
948 /* Install interface node. */
949 install_node(&interface_node, interface_config_write);
950 if_cmd_init();
951 }