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