]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge pull request #2848 from donaldsharp/more_init
[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 "table.h"
35 #include "thread.h"
36 #include "privs.h"
37 #include "vrf.h"
38 #include "lib_errors.h"
39
40 #include "ripngd/ripngd.h"
41 #include "ripngd/ripng_debug.h"
42
43 /* If RFC2133 definition is used. */
44 #ifndef IPV6_JOIN_GROUP
45 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
46 #endif
47 #ifndef IPV6_LEAVE_GROUP
48 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
49 #endif
50
51 /* Static utility function. */
52 static void ripng_enable_apply(struct interface *);
53 static void ripng_passive_interface_apply(struct interface *);
54 static int ripng_enable_if_lookup(const char *);
55 static int ripng_enable_network_lookup2(struct connected *);
56 static void ripng_enable_apply_all(void);
57
58 /* Join to the all rip routers multicast group. */
59 static int ripng_multicast_join(struct interface *ifp)
60 {
61 int ret;
62 struct ipv6_mreq mreq;
63 int save_errno;
64
65 if (if_is_multicast(ifp)) {
66 memset(&mreq, 0, sizeof(mreq));
67 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
68 mreq.ipv6mr_interface = ifp->ifindex;
69
70 /*
71 * NetBSD 1.6.2 requires root to join groups on gif(4).
72 * While this is bogus, privs are available and easy to use
73 * for this call as a workaround.
74 */
75 frr_elevate_privs(&ripngd_privs) {
76
77 ret = setsockopt(ripng->sock, IPPROTO_IPV6,
78 IPV6_JOIN_GROUP,
79 (char *)&mreq, sizeof(mreq));
80 save_errno = errno;
81
82 }
83
84 if (ret < 0 && save_errno == EADDRINUSE) {
85 /*
86 * Group is already joined. This occurs due to sloppy
87 * group
88 * management, in particular declining to leave the
89 * group on
90 * an interface that has just gone down.
91 */
92 zlog_warn("ripng join on %s EADDRINUSE (ignoring)\n",
93 ifp->name);
94 return 0; /* not an error */
95 }
96
97 if (ret < 0)
98 zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s",
99 safe_strerror(save_errno));
100
101 if (IS_RIPNG_DEBUG_EVENT)
102 zlog_debug(
103 "RIPng %s join to all-rip-routers multicast group",
104 ifp->name);
105
106 if (ret < 0)
107 return -1;
108 }
109 return 0;
110 }
111
112 /* Leave from the all rip routers multicast group. */
113 static int ripng_multicast_leave(struct interface *ifp)
114 {
115 int ret;
116 struct ipv6_mreq mreq;
117
118 if (if_is_multicast(ifp)) {
119 memset(&mreq, 0, sizeof(mreq));
120 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
121 mreq.ipv6mr_interface = ifp->ifindex;
122
123 ret = setsockopt(ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
124 (char *)&mreq, sizeof(mreq));
125 if (ret < 0)
126 zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s\n",
127 safe_strerror(errno));
128
129 if (IS_RIPNG_DEBUG_EVENT)
130 zlog_debug(
131 "RIPng %s leave from all-rip-routers multicast group",
132 ifp->name);
133
134 if (ret < 0)
135 return -1;
136 }
137
138 return 0;
139 }
140
141 /* How many link local IPv6 address could be used on the interface ? */
142 static int ripng_if_ipv6_lladdress_check(struct interface *ifp)
143 {
144 struct listnode *nn;
145 struct connected *connected;
146 int count = 0;
147
148 for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, connected)) {
149 struct prefix *p;
150 p = connected->address;
151
152 if ((p->family == AF_INET6)
153 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
154 count++;
155 }
156
157 return count;
158 }
159
160 static int ripng_if_down(struct interface *ifp)
161 {
162 struct route_node *rp;
163 struct ripng_info *rinfo;
164 struct ripng_interface *ri;
165 struct list *list = NULL;
166 struct listnode *listnode = NULL, *nextnode = NULL;
167
168 if (ripng)
169 for (rp = route_top(ripng->table); rp; rp = route_next(rp))
170 if ((list = rp->info) != NULL)
171 for (ALL_LIST_ELEMENTS(list, listnode, nextnode,
172 rinfo))
173 if (rinfo->ifindex == ifp->ifindex)
174 ripng_ecmp_delete(rinfo);
175
176 ri = ifp->info;
177
178 if (ri->running) {
179 if (IS_RIPNG_DEBUG_EVENT)
180 zlog_debug("turn off %s", ifp->name);
181
182 /* Leave from multicast group. */
183 ripng_multicast_leave(ifp);
184
185 ri->running = 0;
186 }
187
188 return 0;
189 }
190
191 /* Inteface link up message processing. */
192 int ripng_interface_up(int command, struct zclient *zclient,
193 zebra_size_t length, vrf_id_t vrf_id)
194 {
195 struct stream *s;
196 struct interface *ifp;
197
198 /* zebra_interface_state_read() updates interface structure in iflist.
199 */
200 s = zclient->ibuf;
201 ifp = zebra_interface_state_read(s, vrf_id);
202
203 if (ifp == NULL)
204 return 0;
205
206 if (IS_RIPNG_DEBUG_ZEBRA)
207 zlog_debug(
208 "interface up %s index %d flags %llx metric %d mtu %d",
209 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
210 ifp->metric, ifp->mtu6);
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 int ripng_interface_down(int command, struct zclient *zclient,
226 zebra_size_t length, vrf_id_t vrf_id)
227 {
228 struct stream *s;
229 struct interface *ifp;
230
231 /* zebra_interface_state_read() updates interface structure in iflist.
232 */
233 s = zclient->ibuf;
234 ifp = zebra_interface_state_read(s, vrf_id);
235
236 if (ifp == NULL)
237 return 0;
238
239 ripng_if_down(ifp);
240
241 if (IS_RIPNG_DEBUG_ZEBRA)
242 zlog_debug(
243 "interface down %s index %d flags %#llx metric %d mtu %d",
244 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
245 ifp->metric, ifp->mtu6);
246
247 return 0;
248 }
249
250 /* Inteface addition message from zebra. */
251 int ripng_interface_add(int command, struct zclient *zclient,
252 zebra_size_t length, vrf_id_t vrf_id)
253 {
254 struct interface *ifp;
255
256 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
257
258 if (IS_RIPNG_DEBUG_ZEBRA)
259 zlog_debug(
260 "RIPng interface add %s index %d flags %#llx metric %d mtu %d",
261 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
262 ifp->metric, ifp->mtu6);
263
264 /* Check is this interface is RIP enabled or not.*/
265 ripng_enable_apply(ifp);
266
267 /* Apply distribute list to the interface. */
268 ripng_distribute_update_interface(ifp);
269
270 /* Check interface routemap. */
271 ripng_if_rmap_update_interface(ifp);
272
273 return 0;
274 }
275
276 int ripng_interface_delete(int command, struct zclient *zclient,
277 zebra_size_t length, vrf_id_t vrf_id)
278 {
279 struct interface *ifp;
280 struct stream *s;
281
282 s = zclient->ibuf;
283 /* zebra_interface_state_read() updates interface structure in iflist
284 */
285 ifp = zebra_interface_state_read(s, vrf_id);
286
287 if (ifp == NULL)
288 return 0;
289
290 if (if_is_up(ifp)) {
291 ripng_if_down(ifp);
292 }
293
294 zlog_info("interface delete %s index %d flags %#llx metric %d mtu %d",
295 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
296 ifp->metric, ifp->mtu6);
297
298 /* To support pseudo interface do not free interface structure. */
299 /* if_delete(ifp); */
300 if_set_index(ifp, IFINDEX_INTERNAL);
301
302 return 0;
303 }
304
305 void ripng_interface_clean(void)
306 {
307 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
308 struct interface *ifp;
309 struct ripng_interface *ri;
310
311 FOR_ALL_INTERFACES (vrf, ifp) {
312 ri = ifp->info;
313
314 ri->enable_network = 0;
315 ri->enable_interface = 0;
316 ri->running = 0;
317
318 if (ri->t_wakeup) {
319 thread_cancel(ri->t_wakeup);
320 ri->t_wakeup = NULL;
321 }
322 }
323 }
324
325 void ripng_interface_reset(void)
326 {
327 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
328 struct interface *ifp;
329 struct ripng_interface *ri;
330
331 FOR_ALL_INTERFACES (vrf, ifp) {
332 ri = ifp->info;
333
334 ri->enable_network = 0;
335 ri->enable_interface = 0;
336 ri->running = 0;
337
338 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
339 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
340
341 ri->list[RIPNG_FILTER_IN] = NULL;
342 ri->list[RIPNG_FILTER_OUT] = NULL;
343
344 ri->prefix[RIPNG_FILTER_IN] = NULL;
345 ri->prefix[RIPNG_FILTER_OUT] = NULL;
346
347 if (ri->t_wakeup) {
348 thread_cancel(ri->t_wakeup);
349 ri->t_wakeup = NULL;
350 }
351
352 ri->passive = 0;
353 }
354 }
355
356 static void ripng_apply_address_add(struct connected *ifc)
357 {
358 struct prefix_ipv6 address;
359 struct prefix *p;
360
361 if (!ripng)
362 return;
363
364 if (!if_is_up(ifc->ifp))
365 return;
366
367 p = ifc->address;
368
369 memset(&address, 0, sizeof(address));
370 address.family = p->family;
371 address.prefix = p->u.prefix6;
372 address.prefixlen = p->prefixlen;
373 apply_mask_ipv6(&address);
374
375 /* Check if this interface is RIP enabled or not
376 or Check if this address's prefix is RIP enabled */
377 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0)
378 || (ripng_enable_network_lookup2(ifc) >= 0))
379 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT,
380 RIPNG_ROUTE_INTERFACE, &address,
381 ifc->ifp->ifindex, NULL, 0);
382 }
383
384 int ripng_interface_address_add(int command, struct zclient *zclient,
385 zebra_size_t length, vrf_id_t vrf_id)
386 {
387 struct connected *c;
388 struct prefix *p;
389
390 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
391 zclient->ibuf, vrf_id);
392
393 if (c == NULL)
394 return 0;
395
396 p = c->address;
397
398 if (p->family == AF_INET6) {
399 struct ripng_interface *ri = c->ifp->info;
400
401 if (IS_RIPNG_DEBUG_ZEBRA)
402 zlog_debug("RIPng connected address %s/%d add",
403 inet6_ntoa(p->u.prefix6), p->prefixlen);
404
405 /* Check is this prefix needs to be redistributed. */
406 ripng_apply_address_add(c);
407
408 /* Let's try once again whether the interface could be activated
409 */
410 if (!ri->running) {
411 /* Check if this interface is RIP enabled or not.*/
412 ripng_enable_apply(c->ifp);
413
414 /* Apply distribute list to the interface. */
415 ripng_distribute_update_interface(c->ifp);
416
417 /* Check interface routemap. */
418 ripng_if_rmap_update_interface(c->ifp);
419 }
420 }
421
422 return 0;
423 }
424
425 static void ripng_apply_address_del(struct connected *ifc)
426 {
427 struct prefix_ipv6 address;
428 struct prefix *p;
429
430 if (!ripng)
431 return;
432
433 if (!if_is_up(ifc->ifp))
434 return;
435
436 p = ifc->address;
437
438 memset(&address, 0, sizeof(address));
439 address.family = p->family;
440 address.prefix = p->u.prefix6;
441 address.prefixlen = p->prefixlen;
442 apply_mask_ipv6(&address);
443
444 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
445 &address, ifc->ifp->ifindex);
446 }
447
448 int ripng_interface_address_delete(int command, struct zclient *zclient,
449 zebra_size_t length, vrf_id_t vrf_id)
450 {
451 struct connected *ifc;
452 struct prefix *p;
453 char buf[INET6_ADDRSTRLEN];
454
455 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
456 zclient->ibuf, vrf_id);
457
458 if (ifc) {
459 p = ifc->address;
460
461 if (p->family == AF_INET6) {
462 if (IS_RIPNG_DEBUG_ZEBRA)
463 zlog_debug(
464 "RIPng connected address %s/%d delete",
465 inet_ntop(AF_INET6, &p->u.prefix6, buf,
466 INET6_ADDRSTRLEN),
467 p->prefixlen);
468
469 /* Check wether this prefix needs to be removed. */
470 ripng_apply_address_del(ifc);
471 }
472 connected_free(ifc);
473 }
474
475 return 0;
476 }
477
478 /* RIPng enable interface vector. */
479 vector ripng_enable_if;
480
481 /* RIPng enable network table. */
482 struct route_table *ripng_enable_network;
483
484 /* Lookup RIPng enable network. */
485 /* Check wether the interface has at least a connected prefix that
486 * is within the ripng_enable_network table. */
487 static int ripng_enable_network_lookup_if(struct interface *ifp)
488 {
489 struct listnode *node;
490 struct connected *connected;
491 struct prefix_ipv6 address;
492
493 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
494 struct prefix *p;
495 struct route_node *n;
496
497 p = connected->address;
498
499 if (p->family == AF_INET6) {
500 address.family = AF_INET6;
501 address.prefix = p->u.prefix6;
502 address.prefixlen = IPV6_MAX_BITLEN;
503
504 n = route_node_match(ripng_enable_network,
505 (struct prefix *)&address);
506 if (n) {
507 route_unlock_node(n);
508 return 1;
509 }
510 }
511 }
512 return -1;
513 }
514
515 /* Check wether connected is within the ripng_enable_network table. */
516 static int ripng_enable_network_lookup2(struct connected *connected)
517 {
518 struct prefix_ipv6 address;
519 struct prefix *p;
520
521 p = connected->address;
522
523 if (p->family == AF_INET6) {
524 struct route_node *node;
525
526 address.family = p->family;
527 address.prefix = p->u.prefix6;
528 address.prefixlen = IPV6_MAX_BITLEN;
529
530 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
531 * ripng_enable_network */
532 node = route_node_match(ripng_enable_network,
533 (struct prefix *)&address);
534
535 if (node) {
536 route_unlock_node(node);
537 return 1;
538 }
539 }
540
541 return -1;
542 }
543
544 /* Add RIPng enable network. */
545 static int ripng_enable_network_add(struct prefix *p)
546 {
547 struct route_node *node;
548
549 node = route_node_get(ripng_enable_network, p);
550
551 if (node->info) {
552 route_unlock_node(node);
553 return -1;
554 } else
555 node->info = (void *)1;
556
557 /* XXX: One should find a better solution than a generic one */
558 ripng_enable_apply_all();
559
560 return 1;
561 }
562
563 /* Delete RIPng enable network. */
564 static int ripng_enable_network_delete(struct prefix *p)
565 {
566 struct route_node *node;
567
568 node = route_node_lookup(ripng_enable_network, p);
569 if (node) {
570 node->info = NULL;
571
572 /* Unlock info lock. */
573 route_unlock_node(node);
574
575 /* Unlock lookup lock. */
576 route_unlock_node(node);
577
578 return 1;
579 }
580 return -1;
581 }
582
583 /* Lookup function. */
584 static int ripng_enable_if_lookup(const char *ifname)
585 {
586 unsigned int i;
587 char *str;
588
589 for (i = 0; i < vector_active(ripng_enable_if); i++)
590 if ((str = vector_slot(ripng_enable_if, i)) != NULL)
591 if (strcmp(str, ifname) == 0)
592 return i;
593 return -1;
594 }
595
596 /* Add interface to ripng_enable_if. */
597 static int ripng_enable_if_add(const char *ifname)
598 {
599 int ret;
600
601 ret = ripng_enable_if_lookup(ifname);
602 if (ret >= 0)
603 return -1;
604
605 vector_set(ripng_enable_if, strdup(ifname));
606
607 ripng_enable_apply_all();
608
609 return 1;
610 }
611
612 /* Delete interface from ripng_enable_if. */
613 static int ripng_enable_if_delete(const char *ifname)
614 {
615 int index;
616 char *str;
617
618 index = ripng_enable_if_lookup(ifname);
619 if (index < 0)
620 return -1;
621
622 str = vector_slot(ripng_enable_if, index);
623 free(str);
624 vector_unset(ripng_enable_if, index);
625
626 ripng_enable_apply_all();
627
628 return 1;
629 }
630
631 /* Wake up interface. */
632 static int ripng_interface_wakeup(struct thread *t)
633 {
634 struct interface *ifp;
635 struct ripng_interface *ri;
636
637 /* Get interface. */
638 ifp = THREAD_ARG(t);
639
640 ri = ifp->info;
641 ri->t_wakeup = NULL;
642
643 /* Join to multicast group. */
644 if (ripng_multicast_join(ifp) < 0) {
645 flog_err_sys(LIB_ERR_SOCKET,
646 "multicast join failed, interface %s not running",
647 ifp->name);
648 return 0;
649 }
650
651 /* Set running flag. */
652 ri->running = 1;
653
654 /* Send RIP request to the interface. */
655 ripng_request(ifp);
656
657 return 0;
658 }
659
660 static void ripng_connect_set(struct interface *ifp, int set)
661 {
662 struct listnode *node, *nnode;
663 struct connected *connected;
664 struct prefix_ipv6 address;
665
666 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
667 struct prefix *p;
668 p = connected->address;
669
670 if (p->family != AF_INET6)
671 continue;
672
673 address.family = AF_INET6;
674 address.prefix = p->u.prefix6;
675 address.prefixlen = p->prefixlen;
676 apply_mask_ipv6(&address);
677
678 if (set) {
679 /* Check once more wether this prefix is within a
680 * "network IF_OR_PREF" one */
681 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0)
682 || (ripng_enable_network_lookup2(connected) >= 0))
683 ripng_redistribute_add(
684 ZEBRA_ROUTE_CONNECT,
685 RIPNG_ROUTE_INTERFACE, &address,
686 connected->ifp->ifindex, NULL, 0);
687 } else {
688 ripng_redistribute_delete(
689 ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
690 &address, connected->ifp->ifindex);
691 if (ripng_redistribute_check(ZEBRA_ROUTE_CONNECT))
692 ripng_redistribute_add(
693 ZEBRA_ROUTE_CONNECT,
694 RIPNG_ROUTE_REDISTRIBUTE, &address,
695 connected->ifp->ifindex, NULL, 0);
696 }
697 }
698 }
699
700 /* Check RIPng is enabed on this interface. */
701 void ripng_enable_apply(struct interface *ifp)
702 {
703 int ret;
704 struct ripng_interface *ri = NULL;
705
706 /* Check interface. */
707 if (!if_is_up(ifp))
708 return;
709
710 ri = ifp->info;
711
712 /* Is this interface a candidate for RIPng ? */
713 ret = ripng_enable_network_lookup_if(ifp);
714
715 /* If the interface is matched. */
716 if (ret > 0)
717 ri->enable_network = 1;
718 else
719 ri->enable_network = 0;
720
721 /* Check interface name configuration. */
722 ret = ripng_enable_if_lookup(ifp->name);
723 if (ret >= 0)
724 ri->enable_interface = 1;
725 else
726 ri->enable_interface = 0;
727
728 /* any candidate interface MUST have a link-local IPv6 address */
729 if ((!ripng_if_ipv6_lladdress_check(ifp))
730 && (ri->enable_network || ri->enable_interface)) {
731 ri->enable_network = 0;
732 ri->enable_interface = 0;
733 zlog_warn("Interface %s does not have any link-local address",
734 ifp->name);
735 }
736
737 /* Update running status of the interface. */
738 if (ri->enable_network || ri->enable_interface) {
739 zlog_info("RIPng INTERFACE ON %s", ifp->name);
740
741 /* Add interface wake up thread. */
742 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
743 &ri->t_wakeup);
744
745 ripng_connect_set(ifp, 1);
746 } else {
747 if (ri->running) {
748 /* Might as well clean up the route table as well
749 * ripng_if_down sets to 0 ri->running, and displays
750 *"turn off %s"
751 **/
752 ripng_if_down(ifp);
753
754 ripng_connect_set(ifp, 0);
755 }
756 }
757 }
758
759 /* Set distribute list to all interfaces. */
760 static void ripng_enable_apply_all(void)
761 {
762 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
763 struct interface *ifp;
764
765 FOR_ALL_INTERFACES (vrf, ifp)
766 ripng_enable_apply(ifp);
767 }
768
769 /* Clear all network and neighbor configuration */
770 void ripng_clean_network()
771 {
772 unsigned int i;
773 char *str;
774 struct route_node *rn;
775
776 /* ripng_enable_network */
777 for (rn = route_top(ripng_enable_network); rn; rn = route_next(rn))
778 if (rn->info) {
779 rn->info = NULL;
780 route_unlock_node(rn);
781 }
782
783 /* ripng_enable_if */
784 for (i = 0; i < vector_active(ripng_enable_if); i++)
785 if ((str = vector_slot(ripng_enable_if, i)) != NULL) {
786 free(str);
787 vector_slot(ripng_enable_if, i) = NULL;
788 }
789 }
790
791 /* Vector to store passive-interface name. */
792 vector Vripng_passive_interface;
793
794 /* Utility function for looking up passive interface settings. */
795 static int ripng_passive_interface_lookup(const char *ifname)
796 {
797 unsigned int i;
798 char *str;
799
800 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
801 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL)
802 if (strcmp(str, ifname) == 0)
803 return i;
804 return -1;
805 }
806
807 void ripng_passive_interface_apply(struct interface *ifp)
808 {
809 int ret;
810 struct ripng_interface *ri;
811
812 ri = ifp->info;
813
814 ret = ripng_passive_interface_lookup(ifp->name);
815 if (ret < 0)
816 ri->passive = 0;
817 else
818 ri->passive = 1;
819 }
820
821 static void ripng_passive_interface_apply_all(void)
822 {
823 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
824 struct interface *ifp;
825
826 FOR_ALL_INTERFACES (vrf, ifp)
827 ripng_passive_interface_apply(ifp);
828 }
829
830 /* Passive interface. */
831 static int ripng_passive_interface_set(struct vty *vty, const char *ifname)
832 {
833 if (ripng_passive_interface_lookup(ifname) >= 0)
834 return CMD_WARNING_CONFIG_FAILED;
835
836 vector_set(Vripng_passive_interface, strdup(ifname));
837
838 ripng_passive_interface_apply_all();
839
840 return CMD_SUCCESS;
841 }
842
843 static int ripng_passive_interface_unset(struct vty *vty, const char *ifname)
844 {
845 int i;
846 char *str;
847
848 i = ripng_passive_interface_lookup(ifname);
849 if (i < 0)
850 return CMD_WARNING_CONFIG_FAILED;
851
852 str = vector_slot(Vripng_passive_interface, i);
853 free(str);
854 vector_unset(Vripng_passive_interface, i);
855
856 ripng_passive_interface_apply_all();
857
858 return CMD_SUCCESS;
859 }
860
861 /* Free all configured RIP passive-interface settings. */
862 void ripng_passive_interface_clean(void)
863 {
864 unsigned int i;
865 char *str;
866
867 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
868 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL) {
869 free(str);
870 vector_slot(Vripng_passive_interface, i) = NULL;
871 }
872 ripng_passive_interface_apply_all();
873 }
874
875 /* Write RIPng enable network and interface to the vty. */
876 int ripng_network_write(struct vty *vty, int config_mode)
877 {
878 unsigned int i;
879 const char *ifname;
880 struct route_node *node;
881 char buf[BUFSIZ];
882
883 /* Write enable network. */
884 for (node = route_top(ripng_enable_network); node;
885 node = route_next(node))
886 if (node->info) {
887 struct prefix *p = &node->p;
888 vty_out(vty, "%s%s/%d\n",
889 config_mode ? " network " : " ",
890 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
891 p->prefixlen);
892 }
893
894 /* Write enable interface. */
895 for (i = 0; i < vector_active(ripng_enable_if); i++)
896 if ((ifname = vector_slot(ripng_enable_if, i)) != NULL)
897 vty_out(vty, "%s%s\n",
898 config_mode ? " network " : " ", ifname);
899
900 /* Write passive interface. */
901 if (config_mode)
902 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
903 if ((ifname = vector_slot(Vripng_passive_interface, i))
904 != NULL)
905 vty_out(vty, " passive-interface %s\n", ifname);
906
907 return 0;
908 }
909
910 /* RIPng enable on specified interface or matched network. */
911 DEFUN (ripng_network,
912 ripng_network_cmd,
913 "network IF_OR_ADDR",
914 "RIPng enable on specified interface or network.\n"
915 "Interface or address\n")
916 {
917 int idx_if_or_addr = 1;
918 int ret;
919 struct prefix p;
920
921 ret = str2prefix(argv[idx_if_or_addr]->arg, &p);
922
923 /* Given string is IPv6 network or interface name. */
924 if (ret)
925 ret = ripng_enable_network_add(&p);
926 else
927 ret = ripng_enable_if_add(argv[idx_if_or_addr]->arg);
928
929 if (ret < 0) {
930 vty_out(vty, "There is same network configuration %s\n",
931 argv[idx_if_or_addr]->arg);
932 return CMD_WARNING_CONFIG_FAILED;
933 }
934
935 return CMD_SUCCESS;
936 }
937
938 /* RIPng enable on specified interface or matched network. */
939 DEFUN (no_ripng_network,
940 no_ripng_network_cmd,
941 "no network IF_OR_ADDR",
942 NO_STR
943 "RIPng enable on specified interface or network.\n"
944 "Interface or address\n")
945 {
946 int idx_if_or_addr = 2;
947 int ret;
948 struct prefix p;
949
950 ret = str2prefix(argv[idx_if_or_addr]->arg, &p);
951
952 /* Given string is interface name. */
953 if (ret)
954 ret = ripng_enable_network_delete(&p);
955 else
956 ret = ripng_enable_if_delete(argv[idx_if_or_addr]->arg);
957
958 if (ret < 0) {
959 vty_out(vty, "can't find network %s\n",
960 argv[idx_if_or_addr]->arg);
961 return CMD_WARNING_CONFIG_FAILED;
962 }
963
964 return CMD_SUCCESS;
965 }
966
967 DEFUN (ipv6_ripng_split_horizon,
968 ipv6_ripng_split_horizon_cmd,
969 "ipv6 ripng split-horizon",
970 IPV6_STR
971 "Routing Information Protocol\n"
972 "Perform split horizon\n")
973 {
974 VTY_DECLVAR_CONTEXT(interface, ifp);
975 struct ripng_interface *ri;
976
977 ri = ifp->info;
978
979 ri->split_horizon = RIPNG_SPLIT_HORIZON;
980 return CMD_SUCCESS;
981 }
982
983 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
984 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
985 "ipv6 ripng split-horizon poisoned-reverse",
986 IPV6_STR
987 "Routing Information Protocol\n"
988 "Perform split horizon\n"
989 "With poisoned-reverse\n")
990 {
991 VTY_DECLVAR_CONTEXT(interface, ifp);
992 struct ripng_interface *ri;
993
994 ri = ifp->info;
995
996 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
997 return CMD_SUCCESS;
998 }
999
1000 DEFUN (no_ipv6_ripng_split_horizon,
1001 no_ipv6_ripng_split_horizon_cmd,
1002 "no ipv6 ripng split-horizon [poisoned-reverse]",
1003 NO_STR
1004 IPV6_STR
1005 "Routing Information Protocol\n"
1006 "Perform split horizon\n"
1007 "With poisoned-reverse\n")
1008 {
1009 VTY_DECLVAR_CONTEXT(interface, ifp);
1010 struct ripng_interface *ri;
1011
1012 ri = ifp->info;
1013
1014 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1015 return CMD_SUCCESS;
1016 }
1017
1018 DEFUN (ripng_passive_interface,
1019 ripng_passive_interface_cmd,
1020 "passive-interface IFNAME",
1021 "Suppress routing updates on an interface\n"
1022 "Interface name\n")
1023 {
1024 int idx_ifname = 1;
1025 return ripng_passive_interface_set(vty, argv[idx_ifname]->arg);
1026 }
1027
1028 DEFUN (no_ripng_passive_interface,
1029 no_ripng_passive_interface_cmd,
1030 "no passive-interface IFNAME",
1031 NO_STR
1032 "Suppress routing updates on an interface\n"
1033 "Interface name\n")
1034 {
1035 int idx_ifname = 2;
1036 return ripng_passive_interface_unset(vty, argv[idx_ifname]->arg);
1037 }
1038
1039 static struct ripng_interface *ri_new(void)
1040 {
1041 struct ripng_interface *ri;
1042 ri = XCALLOC(MTYPE_IF, sizeof(struct ripng_interface));
1043
1044 /* Set default split-horizon behavior. If the interface is Frame
1045 Relay or SMDS is enabled, the default value for split-horizon is
1046 off. But currently Zebra does detect Frame Relay or SMDS
1047 interface. So all interface is set to split horizon. */
1048 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1049 ri->split_horizon = ri->split_horizon_default;
1050
1051 return ri;
1052 }
1053
1054 static int ripng_if_new_hook(struct interface *ifp)
1055 {
1056 ifp->info = ri_new();
1057 return 0;
1058 }
1059
1060 /* Called when interface structure deleted. */
1061 static int ripng_if_delete_hook(struct interface *ifp)
1062 {
1063 XFREE(MTYPE_IF, ifp->info);
1064 ifp->info = NULL;
1065 return 0;
1066 }
1067
1068 /* Configuration write function for ripngd. */
1069 static int interface_config_write(struct vty *vty)
1070 {
1071 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
1072 struct interface *ifp;
1073 struct ripng_interface *ri;
1074 int write = 0;
1075
1076 FOR_ALL_INTERFACES (vrf, ifp) {
1077 ri = ifp->info;
1078
1079 /* Do not display the interface if there is no
1080 * configuration about it.
1081 **/
1082 if ((!ifp->desc)
1083 && (ri->split_horizon == ri->split_horizon_default))
1084 continue;
1085
1086 vty_frame(vty, "interface %s\n", ifp->name);
1087 if (ifp->desc)
1088 vty_out(vty, " description %s\n", ifp->desc);
1089
1090 /* Split horizon. */
1091 if (ri->split_horizon != ri->split_horizon_default) {
1092 switch (ri->split_horizon) {
1093 case RIPNG_SPLIT_HORIZON:
1094 vty_out(vty, " ipv6 ripng split-horizon\n");
1095 break;
1096 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1097 vty_out(vty,
1098 " ipv6 ripng split-horizon poisoned-reverse\n");
1099 break;
1100 case RIPNG_NO_SPLIT_HORIZON:
1101 default:
1102 vty_out(vty, " no ipv6 ripng split-horizon\n");
1103 break;
1104 }
1105 }
1106
1107 vty_endframe(vty, "!\n");
1108
1109 write++;
1110 }
1111 return write;
1112 }
1113
1114 /* ripngd's interface node. */
1115 static struct cmd_node interface_node = {
1116 INTERFACE_NODE, "%s(config-if)# ", 1 /* VTYSH */
1117 };
1118
1119 /* Initialization of interface. */
1120 void ripng_if_init()
1121 {
1122 /* Interface initialize. */
1123 hook_register_prio(if_add, 0, ripng_if_new_hook);
1124 hook_register_prio(if_del, 0, ripng_if_delete_hook);
1125
1126 /* RIPng enable network init. */
1127 ripng_enable_network = route_table_init();
1128
1129 /* RIPng enable interface init. */
1130 ripng_enable_if = vector_init(1);
1131
1132 /* RIPng passive interface. */
1133 Vripng_passive_interface = vector_init(1);
1134
1135 /* Install interface node. */
1136 install_node(&interface_node, interface_config_write);
1137 if_cmd_init();
1138
1139 install_element(RIPNG_NODE, &ripng_network_cmd);
1140 install_element(RIPNG_NODE, &no_ripng_network_cmd);
1141 install_element(RIPNG_NODE, &ripng_passive_interface_cmd);
1142 install_element(RIPNG_NODE, &no_ripng_passive_interface_cmd);
1143
1144 install_element(INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1145 install_element(INTERFACE_NODE,
1146 &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1147 install_element(INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1148 }