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