]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_interface.c
Merge pull request #1051 from donaldsharp/plists
[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
39 #include "ripngd/ripngd.h"
40 #include "ripngd/ripng_debug.h"
41
42 /* If RFC2133 definition is used. */
43 #ifndef IPV6_JOIN_GROUP
44 #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
45 #endif
46 #ifndef IPV6_LEAVE_GROUP
47 #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
48 #endif
49
50 extern struct zebra_privs_t ripngd_privs;
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 if (ripngd_privs.change(ZPRIVS_RAISE))
77 zlog_err("ripng_multicast_join: could not raise privs");
78
79 ret = setsockopt(ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
80 (char *)&mreq, sizeof(mreq));
81 save_errno = errno;
82
83 if (ripngd_privs.change(ZPRIVS_LOWER))
84 zlog_err("ripng_multicast_join: could not lower privs");
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)\n",
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)
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(ripng->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\n",
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 route_node *rp;
165 struct ripng_info *rinfo;
166 struct ripng_interface *ri;
167 struct list *list = NULL;
168 struct listnode *listnode = NULL, *nextnode = NULL;
169
170 if (ripng)
171 for (rp = route_top(ripng->table); rp; rp = 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 ifp->ifindex = IFINDEX_DELETED;
303
304 return 0;
305 }
306
307 void ripng_interface_clean(void)
308 {
309 struct listnode *node, *nnode;
310 struct interface *ifp;
311 struct ripng_interface *ri;
312
313 for (ALL_LIST_ELEMENTS(vrf_iflist(VRF_DEFAULT), node, nnode, 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 void ripng_interface_reset(void)
328 {
329 struct listnode *node;
330 struct interface *ifp;
331 struct ripng_interface *ri;
332
333 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
334 ri = ifp->info;
335
336 ri->enable_network = 0;
337 ri->enable_interface = 0;
338 ri->running = 0;
339
340 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
341 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
342
343 ri->list[RIPNG_FILTER_IN] = NULL;
344 ri->list[RIPNG_FILTER_OUT] = NULL;
345
346 ri->prefix[RIPNG_FILTER_IN] = NULL;
347 ri->prefix[RIPNG_FILTER_OUT] = NULL;
348
349 if (ri->t_wakeup) {
350 thread_cancel(ri->t_wakeup);
351 ri->t_wakeup = NULL;
352 }
353
354 ri->passive = 0;
355 }
356 }
357
358 static void ripng_apply_address_add(struct connected *ifc)
359 {
360 struct prefix_ipv6 address;
361 struct prefix *p;
362
363 if (!ripng)
364 return;
365
366 if (!if_is_up(ifc->ifp))
367 return;
368
369 p = ifc->address;
370
371 memset(&address, 0, sizeof(address));
372 address.family = p->family;
373 address.prefix = p->u.prefix6;
374 address.prefixlen = p->prefixlen;
375 apply_mask_ipv6(&address);
376
377 /* Check if this interface is RIP enabled or not
378 or Check if this address's prefix is RIP enabled */
379 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0)
380 || (ripng_enable_network_lookup2(ifc) >= 0))
381 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT,
382 RIPNG_ROUTE_INTERFACE, &address,
383 ifc->ifp->ifindex, NULL, 0);
384 }
385
386 int ripng_interface_address_add(int command, struct zclient *zclient,
387 zebra_size_t length, vrf_id_t vrf_id)
388 {
389 struct connected *c;
390 struct prefix *p;
391
392 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
393 zclient->ibuf, vrf_id);
394
395 if (c == NULL)
396 return 0;
397
398 p = c->address;
399
400 if (p->family == AF_INET6) {
401 struct ripng_interface *ri = c->ifp->info;
402
403 if (IS_RIPNG_DEBUG_ZEBRA)
404 zlog_debug("RIPng connected address %s/%d add",
405 inet6_ntoa(p->u.prefix6), p->prefixlen);
406
407 /* Check is this prefix needs to be redistributed. */
408 ripng_apply_address_add(c);
409
410 /* Let's try once again whether the interface could be activated
411 */
412 if (!ri->running) {
413 /* Check if this interface is RIP enabled or not.*/
414 ripng_enable_apply(c->ifp);
415
416 /* Apply distribute list to the interface. */
417 ripng_distribute_update_interface(c->ifp);
418
419 /* Check interface routemap. */
420 ripng_if_rmap_update_interface(c->ifp);
421 }
422 }
423
424 return 0;
425 }
426
427 static void ripng_apply_address_del(struct connected *ifc)
428 {
429 struct prefix_ipv6 address;
430 struct prefix *p;
431
432 if (!ripng)
433 return;
434
435 if (!if_is_up(ifc->ifp))
436 return;
437
438 p = ifc->address;
439
440 memset(&address, 0, sizeof(address));
441 address.family = p->family;
442 address.prefix = p->u.prefix6;
443 address.prefixlen = p->prefixlen;
444 apply_mask_ipv6(&address);
445
446 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
447 &address, ifc->ifp->ifindex);
448 }
449
450 int ripng_interface_address_delete(int command, struct zclient *zclient,
451 zebra_size_t length, vrf_id_t vrf_id)
452 {
453 struct connected *ifc;
454 struct prefix *p;
455 char buf[INET6_ADDRSTRLEN];
456
457 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
458 zclient->ibuf, vrf_id);
459
460 if (ifc) {
461 p = ifc->address;
462
463 if (p->family == AF_INET6) {
464 if (IS_RIPNG_DEBUG_ZEBRA)
465 zlog_debug(
466 "RIPng connected address %s/%d delete",
467 inet_ntop(AF_INET6, &p->u.prefix6, buf,
468 INET6_ADDRSTRLEN),
469 p->prefixlen);
470
471 /* Check wether this prefix needs to be removed. */
472 ripng_apply_address_del(ifc);
473 }
474 connected_free(ifc);
475 }
476
477 return 0;
478 }
479
480 /* RIPng enable interface vector. */
481 vector ripng_enable_if;
482
483 /* RIPng enable network table. */
484 struct route_table *ripng_enable_network;
485
486 /* Lookup RIPng enable network. */
487 /* Check wether the interface has at least a connected prefix that
488 * is within the ripng_enable_network table. */
489 static int ripng_enable_network_lookup_if(struct interface *ifp)
490 {
491 struct listnode *node;
492 struct connected *connected;
493 struct prefix_ipv6 address;
494
495 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
496 struct prefix *p;
497 struct route_node *node;
498
499 p = connected->address;
500
501 if (p->family == AF_INET6) {
502 address.family = AF_INET6;
503 address.prefix = p->u.prefix6;
504 address.prefixlen = IPV6_MAX_BITLEN;
505
506 node = route_node_match(ripng_enable_network,
507 (struct prefix *)&address);
508 if (node) {
509 route_unlock_node(node);
510 return 1;
511 }
512 }
513 }
514 return -1;
515 }
516
517 /* Check wether connected is within the ripng_enable_network table. */
518 static int ripng_enable_network_lookup2(struct connected *connected)
519 {
520 struct prefix_ipv6 address;
521 struct prefix *p;
522
523 p = connected->address;
524
525 if (p->family == AF_INET6) {
526 struct route_node *node;
527
528 address.family = p->family;
529 address.prefix = p->u.prefix6;
530 address.prefixlen = IPV6_MAX_BITLEN;
531
532 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
533 * ripng_enable_network */
534 node = route_node_match(ripng_enable_network,
535 (struct prefix *)&address);
536
537 if (node) {
538 route_unlock_node(node);
539 return 1;
540 }
541 }
542
543 return -1;
544 }
545
546 /* Add RIPng enable network. */
547 static int ripng_enable_network_add(struct prefix *p)
548 {
549 struct route_node *node;
550
551 node = route_node_get(ripng_enable_network, p);
552
553 if (node->info) {
554 route_unlock_node(node);
555 return -1;
556 } else
557 node->info = (void *)1;
558
559 /* XXX: One should find a better solution than a generic one */
560 ripng_enable_apply_all();
561
562 return 1;
563 }
564
565 /* Delete RIPng enable network. */
566 static int ripng_enable_network_delete(struct prefix *p)
567 {
568 struct route_node *node;
569
570 node = route_node_lookup(ripng_enable_network, p);
571 if (node) {
572 node->info = NULL;
573
574 /* Unlock info lock. */
575 route_unlock_node(node);
576
577 /* Unlock lookup lock. */
578 route_unlock_node(node);
579
580 return 1;
581 }
582 return -1;
583 }
584
585 /* Lookup function. */
586 static int ripng_enable_if_lookup(const char *ifname)
587 {
588 unsigned int i;
589 char *str;
590
591 for (i = 0; i < vector_active(ripng_enable_if); i++)
592 if ((str = vector_slot(ripng_enable_if, i)) != NULL)
593 if (strcmp(str, ifname) == 0)
594 return i;
595 return -1;
596 }
597
598 /* Add interface to ripng_enable_if. */
599 static int ripng_enable_if_add(const char *ifname)
600 {
601 int ret;
602
603 ret = ripng_enable_if_lookup(ifname);
604 if (ret >= 0)
605 return -1;
606
607 vector_set(ripng_enable_if, strdup(ifname));
608
609 ripng_enable_apply_all();
610
611 return 1;
612 }
613
614 /* Delete interface from ripng_enable_if. */
615 static int ripng_enable_if_delete(const char *ifname)
616 {
617 int index;
618 char *str;
619
620 index = ripng_enable_if_lookup(ifname);
621 if (index < 0)
622 return -1;
623
624 str = vector_slot(ripng_enable_if, index);
625 free(str);
626 vector_unset(ripng_enable_if, index);
627
628 ripng_enable_apply_all();
629
630 return 1;
631 }
632
633 /* Wake up interface. */
634 static int ripng_interface_wakeup(struct thread *t)
635 {
636 struct interface *ifp;
637 struct ripng_interface *ri;
638
639 /* Get interface. */
640 ifp = THREAD_ARG(t);
641
642 ri = ifp->info;
643 ri->t_wakeup = NULL;
644
645 /* Join to multicast group. */
646 if (ripng_multicast_join(ifp) < 0) {
647 zlog_err("multicast join failed, interface %s not running",
648 ifp->name);
649 return 0;
650 }
651
652 /* Set running flag. */
653 ri->running = 1;
654
655 /* Send RIP request to the interface. */
656 ripng_request(ifp);
657
658 return 0;
659 }
660
661 static void ripng_connect_set(struct interface *ifp, int set)
662 {
663 struct listnode *node, *nnode;
664 struct connected *connected;
665 struct prefix_ipv6 address;
666
667 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
668 struct prefix *p;
669 p = connected->address;
670
671 if (p->family != AF_INET6)
672 continue;
673
674 address.family = AF_INET6;
675 address.prefix = p->u.prefix6;
676 address.prefixlen = p->prefixlen;
677 apply_mask_ipv6(&address);
678
679 if (set) {
680 /* Check once more wether this prefix is within a
681 * "network IF_OR_PREF" one */
682 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0)
683 || (ripng_enable_network_lookup2(connected) >= 0))
684 ripng_redistribute_add(
685 ZEBRA_ROUTE_CONNECT,
686 RIPNG_ROUTE_INTERFACE, &address,
687 connected->ifp->ifindex, NULL, 0);
688 } else {
689 ripng_redistribute_delete(
690 ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
691 &address, connected->ifp->ifindex);
692 if (ripng_redistribute_check(ZEBRA_ROUTE_CONNECT))
693 ripng_redistribute_add(
694 ZEBRA_ROUTE_CONNECT,
695 RIPNG_ROUTE_REDISTRIBUTE, &address,
696 connected->ifp->ifindex, NULL, 0);
697 }
698 }
699 }
700
701 /* Check RIPng is enabed on this interface. */
702 void ripng_enable_apply(struct interface *ifp)
703 {
704 int ret;
705 struct ripng_interface *ri = NULL;
706
707 /* Check interface. */
708 if (!if_is_up(ifp))
709 return;
710
711 ri = ifp->info;
712
713 /* Is this interface a candidate for RIPng ? */
714 ret = ripng_enable_network_lookup_if(ifp);
715
716 /* If the interface is matched. */
717 if (ret > 0)
718 ri->enable_network = 1;
719 else
720 ri->enable_network = 0;
721
722 /* Check interface name configuration. */
723 ret = ripng_enable_if_lookup(ifp->name);
724 if (ret >= 0)
725 ri->enable_interface = 1;
726 else
727 ri->enable_interface = 0;
728
729 /* any candidate interface MUST have a link-local IPv6 address */
730 if ((!ripng_if_ipv6_lladdress_check(ifp))
731 && (ri->enable_network || ri->enable_interface)) {
732 ri->enable_network = 0;
733 ri->enable_interface = 0;
734 zlog_warn("Interface %s does not have any link-local address",
735 ifp->name);
736 }
737
738 /* Update running status of the interface. */
739 if (ri->enable_network || ri->enable_interface) {
740 zlog_info("RIPng INTERFACE ON %s", ifp->name);
741
742 /* Add interface wake up thread. */
743 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
744 &ri->t_wakeup);
745
746 ripng_connect_set(ifp, 1);
747 } else {
748 if (ri->running) {
749 /* Might as well clean up the route table as well
750 * ripng_if_down sets to 0 ri->running, and displays
751 *"turn off %s"
752 **/
753 ripng_if_down(ifp);
754
755 ripng_connect_set(ifp, 0);
756 }
757 }
758 }
759
760 /* Set distribute list to all interfaces. */
761 static void ripng_enable_apply_all(void)
762 {
763 struct interface *ifp;
764 struct listnode *node;
765
766 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp))
767 ripng_enable_apply(ifp);
768 }
769
770 /* Clear all network and neighbor configuration */
771 void ripng_clean_network()
772 {
773 unsigned int i;
774 char *str;
775 struct route_node *rn;
776
777 /* ripng_enable_network */
778 for (rn = route_top(ripng_enable_network); rn; rn = route_next(rn))
779 if (rn->info) {
780 rn->info = NULL;
781 route_unlock_node(rn);
782 }
783
784 /* ripng_enable_if */
785 for (i = 0; i < vector_active(ripng_enable_if); i++)
786 if ((str = vector_slot(ripng_enable_if, i)) != NULL) {
787 free(str);
788 vector_slot(ripng_enable_if, i) = NULL;
789 }
790 }
791
792 /* Vector to store passive-interface name. */
793 vector Vripng_passive_interface;
794
795 /* Utility function for looking up passive interface settings. */
796 static int ripng_passive_interface_lookup(const char *ifname)
797 {
798 unsigned int i;
799 char *str;
800
801 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
802 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL)
803 if (strcmp(str, ifname) == 0)
804 return i;
805 return -1;
806 }
807
808 void ripng_passive_interface_apply(struct interface *ifp)
809 {
810 int ret;
811 struct ripng_interface *ri;
812
813 ri = ifp->info;
814
815 ret = ripng_passive_interface_lookup(ifp->name);
816 if (ret < 0)
817 ri->passive = 0;
818 else
819 ri->passive = 1;
820 }
821
822 static void ripng_passive_interface_apply_all(void)
823 {
824 struct interface *ifp;
825 struct listnode *node;
826
827 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp))
828 ripng_passive_interface_apply(ifp);
829 }
830
831 /* Passive interface. */
832 static int ripng_passive_interface_set(struct vty *vty, const char *ifname)
833 {
834 if (ripng_passive_interface_lookup(ifname) >= 0)
835 return CMD_WARNING_CONFIG_FAILED;
836
837 vector_set(Vripng_passive_interface, strdup(ifname));
838
839 ripng_passive_interface_apply_all();
840
841 return CMD_SUCCESS;
842 }
843
844 static int ripng_passive_interface_unset(struct vty *vty, const char *ifname)
845 {
846 int i;
847 char *str;
848
849 i = ripng_passive_interface_lookup(ifname);
850 if (i < 0)
851 return CMD_WARNING_CONFIG_FAILED;
852
853 str = vector_slot(Vripng_passive_interface, i);
854 free(str);
855 vector_unset(Vripng_passive_interface, i);
856
857 ripng_passive_interface_apply_all();
858
859 return CMD_SUCCESS;
860 }
861
862 /* Free all configured RIP passive-interface settings. */
863 void ripng_passive_interface_clean(void)
864 {
865 unsigned int i;
866 char *str;
867
868 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
869 if ((str = vector_slot(Vripng_passive_interface, i)) != NULL) {
870 free(str);
871 vector_slot(Vripng_passive_interface, i) = NULL;
872 }
873 ripng_passive_interface_apply_all();
874 }
875
876 /* Write RIPng enable network and interface to the vty. */
877 int ripng_network_write(struct vty *vty, int config_mode)
878 {
879 unsigned int i;
880 const char *ifname;
881 struct route_node *node;
882 char buf[BUFSIZ];
883
884 /* Write enable network. */
885 for (node = route_top(ripng_enable_network); node;
886 node = route_next(node))
887 if (node->info) {
888 struct prefix *p = &node->p;
889 vty_out(vty, "%s%s/%d\n",
890 config_mode ? " network " : " ",
891 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
892 p->prefixlen);
893 }
894
895 /* Write enable interface. */
896 for (i = 0; i < vector_active(ripng_enable_if); i++)
897 if ((ifname = vector_slot(ripng_enable_if, i)) != NULL)
898 vty_out(vty, "%s%s\n",
899 config_mode ? " network " : " ", ifname);
900
901 /* Write passive interface. */
902 if (config_mode)
903 for (i = 0; i < vector_active(Vripng_passive_interface); i++)
904 if ((ifname = vector_slot(Vripng_passive_interface, i))
905 != NULL)
906 vty_out(vty, " passive-interface %s\n", ifname);
907
908 return 0;
909 }
910
911 /* RIPng enable on specified interface or matched network. */
912 DEFUN (ripng_network,
913 ripng_network_cmd,
914 "network IF_OR_ADDR",
915 "RIPng enable on specified interface or network.\n"
916 "Interface or address\n")
917 {
918 int idx_if_or_addr = 1;
919 int ret;
920 struct prefix p;
921
922 ret = str2prefix(argv[idx_if_or_addr]->arg, &p);
923
924 /* Given string is IPv6 network or interface name. */
925 if (ret)
926 ret = ripng_enable_network_add(&p);
927 else
928 ret = ripng_enable_if_add(argv[idx_if_or_addr]->arg);
929
930 if (ret < 0) {
931 vty_out(vty, "There is same network configuration %s\n",
932 argv[idx_if_or_addr]->arg);
933 return CMD_WARNING_CONFIG_FAILED;
934 }
935
936 return CMD_SUCCESS;
937 }
938
939 /* RIPng enable on specified interface or matched network. */
940 DEFUN (no_ripng_network,
941 no_ripng_network_cmd,
942 "no network IF_OR_ADDR",
943 NO_STR
944 "RIPng enable on specified interface or network.\n"
945 "Interface or address\n")
946 {
947 int idx_if_or_addr = 2;
948 int ret;
949 struct prefix p;
950
951 ret = str2prefix(argv[idx_if_or_addr]->arg, &p);
952
953 /* Given string is interface name. */
954 if (ret)
955 ret = ripng_enable_network_delete(&p);
956 else
957 ret = ripng_enable_if_delete(argv[idx_if_or_addr]->arg);
958
959 if (ret < 0) {
960 vty_out(vty, "can't find network %s\n",
961 argv[idx_if_or_addr]->arg);
962 return CMD_WARNING_CONFIG_FAILED;
963 }
964
965 return CMD_SUCCESS;
966 }
967
968 DEFUN (ipv6_ripng_split_horizon,
969 ipv6_ripng_split_horizon_cmd,
970 "ipv6 ripng split-horizon",
971 IPV6_STR
972 "Routing Information Protocol\n"
973 "Perform split horizon\n")
974 {
975 VTY_DECLVAR_CONTEXT(interface, ifp);
976 struct ripng_interface *ri;
977
978 ri = ifp->info;
979
980 ri->split_horizon = RIPNG_SPLIT_HORIZON;
981 return CMD_SUCCESS;
982 }
983
984 DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
985 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
986 "ipv6 ripng split-horizon poisoned-reverse",
987 IPV6_STR
988 "Routing Information Protocol\n"
989 "Perform split horizon\n"
990 "With poisoned-reverse\n")
991 {
992 VTY_DECLVAR_CONTEXT(interface, ifp);
993 struct ripng_interface *ri;
994
995 ri = ifp->info;
996
997 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
998 return CMD_SUCCESS;
999 }
1000
1001 DEFUN (no_ipv6_ripng_split_horizon,
1002 no_ipv6_ripng_split_horizon_cmd,
1003 "no ipv6 ripng split-horizon [poisoned-reverse]",
1004 NO_STR
1005 IPV6_STR
1006 "Routing Information Protocol\n"
1007 "Perform split horizon\n"
1008 "With poisoned-reverse\n")
1009 {
1010 VTY_DECLVAR_CONTEXT(interface, ifp);
1011 struct ripng_interface *ri;
1012
1013 ri = ifp->info;
1014
1015 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1016 return CMD_SUCCESS;
1017 }
1018
1019 DEFUN (ripng_passive_interface,
1020 ripng_passive_interface_cmd,
1021 "passive-interface IFNAME",
1022 "Suppress routing updates on an interface\n"
1023 "Interface name\n")
1024 {
1025 int idx_ifname = 1;
1026 return ripng_passive_interface_set(vty, argv[idx_ifname]->arg);
1027 }
1028
1029 DEFUN (no_ripng_passive_interface,
1030 no_ripng_passive_interface_cmd,
1031 "no passive-interface IFNAME",
1032 NO_STR
1033 "Suppress routing updates on an interface\n"
1034 "Interface name\n")
1035 {
1036 int idx_ifname = 2;
1037 return ripng_passive_interface_unset(vty, argv[idx_ifname]->arg);
1038 }
1039
1040 static struct ripng_interface *ri_new(void)
1041 {
1042 struct ripng_interface *ri;
1043 ri = XCALLOC(MTYPE_IF, sizeof(struct ripng_interface));
1044
1045 /* Set default split-horizon behavior. If the interface is Frame
1046 Relay or SMDS is enabled, the default value for split-horizon is
1047 off. But currently Zebra does detect Frame Relay or SMDS
1048 interface. So all interface is set to split horizon. */
1049 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1050 ri->split_horizon = ri->split_horizon_default;
1051
1052 return ri;
1053 }
1054
1055 static int ripng_if_new_hook(struct interface *ifp)
1056 {
1057 ifp->info = ri_new();
1058 return 0;
1059 }
1060
1061 /* Called when interface structure deleted. */
1062 static int ripng_if_delete_hook(struct interface *ifp)
1063 {
1064 XFREE(MTYPE_IF, ifp->info);
1065 ifp->info = NULL;
1066 return 0;
1067 }
1068
1069 /* Configuration write function for ripngd. */
1070 static int interface_config_write(struct vty *vty)
1071 {
1072 struct listnode *node;
1073 struct interface *ifp;
1074 struct ripng_interface *ri;
1075 int write = 0;
1076
1077 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
1078 ri = ifp->info;
1079
1080 /* Do not display the interface if there is no
1081 * configuration about it.
1082 **/
1083 if ((!ifp->desc)
1084 && (ri->split_horizon == ri->split_horizon_default))
1085 continue;
1086
1087 vty_frame(vty, "interface %s\n", ifp->name);
1088 if (ifp->desc)
1089 vty_out(vty, " description %s\n", ifp->desc);
1090
1091 /* Split horizon. */
1092 if (ri->split_horizon != ri->split_horizon_default) {
1093 switch (ri->split_horizon) {
1094 case RIPNG_SPLIT_HORIZON:
1095 vty_out(vty, " ipv6 ripng split-horizon\n");
1096 break;
1097 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1098 vty_out(vty,
1099 " ipv6 ripng split-horizon poisoned-reverse\n");
1100 break;
1101 case RIPNG_NO_SPLIT_HORIZON:
1102 default:
1103 vty_out(vty, " no ipv6 ripng split-horizon\n");
1104 break;
1105 }
1106 }
1107
1108 vty_endframe(vty, "!\n");
1109
1110 write++;
1111 }
1112 return write;
1113 }
1114
1115 /* ripngd's interface node. */
1116 static struct cmd_node interface_node = {
1117 INTERFACE_NODE, "%s(config-if)# ", 1 /* VTYSH */
1118 };
1119
1120 /* Initialization of interface. */
1121 void ripng_if_init()
1122 {
1123 /* Interface initialize. */
1124 hook_register_prio(if_add, 0, ripng_if_new_hook);
1125 hook_register_prio(if_del, 0, ripng_if_delete_hook);
1126
1127 /* RIPng enable network init. */
1128 ripng_enable_network = route_table_init();
1129
1130 /* RIPng enable interface init. */
1131 ripng_enable_if = vector_init(1);
1132
1133 /* RIPng passive interface. */
1134 Vripng_passive_interface = vector_init(1);
1135
1136 /* Install interface node. */
1137 install_node(&interface_node, interface_config_write);
1138 if_cmd_init();
1139
1140 install_element(RIPNG_NODE, &ripng_network_cmd);
1141 install_element(RIPNG_NODE, &no_ripng_network_cmd);
1142 install_element(RIPNG_NODE, &ripng_passive_interface_cmd);
1143 install_element(RIPNG_NODE, &no_ripng_passive_interface_cmd);
1144
1145 install_element(INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1146 install_element(INTERFACE_NODE,
1147 &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1148 install_element(INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1149 }