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