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