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