]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_interface.c
ripd: fix interface wakeup after shutdown
[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
bf8d3d6a 52DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_IF, "ripng interface");
eaf58ba9 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
b3d6bc6e 324 thread_cancel(&ri->t_wakeup);
d62a17ae 325 }
326}
a94434b6 327
d62a17ae 328static void ripng_apply_address_add(struct connected *ifc)
329{
5c84b9a5
RW
330 struct ripng_interface *ri = ifc->ifp->info;
331 struct ripng *ripng = ri->ripng;
d62a17ae 332 struct prefix_ipv6 address;
333 struct prefix *p;
334
335 if (!ripng)
336 return;
337
338 if (!if_is_up(ifc->ifp))
339 return;
340
341 p = ifc->address;
342
343 memset(&address, 0, sizeof(address));
344 address.family = p->family;
345 address.prefix = p->u.prefix6;
346 address.prefixlen = p->prefixlen;
347 apply_mask_ipv6(&address);
348
349 /* Check if this interface is RIP enabled or not
350 or Check if this address's prefix is RIP enabled */
5c84b9a5 351 if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0)
d62a17ae 352 || (ripng_enable_network_lookup2(ifc) >= 0))
5c84b9a5 353 ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 354 RIPNG_ROUTE_INTERFACE, &address,
355 ifc->ifp->ifindex, NULL, 0);
a94434b6 356}
357
121f9dee 358int ripng_interface_address_add(ZAPI_CALLBACK_ARGS)
718e3744 359{
d62a17ae 360 struct connected *c;
361 struct prefix *p;
718e3744 362
d62a17ae 363 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
364 zclient->ibuf, vrf_id);
718e3744 365
d62a17ae 366 if (c == NULL)
367 return 0;
718e3744 368
d62a17ae 369 p = c->address;
718e3744 370
d62a17ae 371 if (p->family == AF_INET6) {
372 struct ripng_interface *ri = c->ifp->info;
a94434b6 373
d62a17ae 374 if (IS_RIPNG_DEBUG_ZEBRA)
e0259674 375 zlog_debug("RIPng connected address %pFX add", p);
a94434b6 376
d62a17ae 377 /* Check is this prefix needs to be redistributed. */
378 ripng_apply_address_add(c);
a94434b6 379
d62a17ae 380 /* Let's try once again whether the interface could be activated
381 */
382 if (!ri->running) {
383 /* Check if this interface is RIP enabled or not.*/
384 ripng_enable_apply(c->ifp);
a94434b6 385
d62a17ae 386 /* Apply distribute list to the interface. */
387 ripng_distribute_update_interface(c->ifp);
718e3744 388
d62a17ae 389 /* Check interface routemap. */
390 ripng_if_rmap_update_interface(c->ifp);
391 }
392 }
393
394 return 0;
718e3744 395}
396
d62a17ae 397static void ripng_apply_address_del(struct connected *ifc)
398{
5c84b9a5
RW
399 struct ripng_interface *ri = ifc->ifp->info;
400 struct ripng *ripng = ri->ripng;
d62a17ae 401 struct prefix_ipv6 address;
402 struct prefix *p;
a94434b6 403
d62a17ae 404 if (!ripng)
405 return;
a94434b6 406
d62a17ae 407 if (!if_is_up(ifc->ifp))
408 return;
a94434b6 409
d62a17ae 410 p = ifc->address;
a94434b6 411
d62a17ae 412 memset(&address, 0, sizeof(address));
413 address.family = p->family;
414 address.prefix = p->u.prefix6;
415 address.prefixlen = p->prefixlen;
416 apply_mask_ipv6(&address);
a94434b6 417
5c84b9a5
RW
418 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
419 RIPNG_ROUTE_INTERFACE, &address,
420 ifc->ifp->ifindex);
a94434b6 421}
422
121f9dee 423int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS)
718e3744 424{
d62a17ae 425 struct connected *ifc;
426 struct prefix *p;
d62a17ae 427
428 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
429 zclient->ibuf, vrf_id);
430
431 if (ifc) {
432 p = ifc->address;
433
434 if (p->family == AF_INET6) {
435 if (IS_RIPNG_DEBUG_ZEBRA)
436 zlog_debug(
e0259674
DS
437 "RIPng connected address %pFX delete",
438 p);
d62a17ae 439
440 /* Check wether this prefix needs to be removed. */
441 ripng_apply_address_del(ifc);
442 }
721c0857 443 connected_free(&ifc);
718e3744 444 }
718e3744 445
d62a17ae 446 return 0;
718e3744 447}
6b0655a2 448
718e3744 449/* Lookup RIPng enable network. */
a94434b6 450/* Check wether the interface has at least a connected prefix that
29b94d58 451 * is within the ripng->enable_network table. */
d62a17ae 452static int ripng_enable_network_lookup_if(struct interface *ifp)
718e3744 453{
5c84b9a5
RW
454 struct ripng_interface *ri = ifp->info;
455 struct ripng *ripng = ri->ripng;
d62a17ae 456 struct listnode *node;
457 struct connected *connected;
458 struct prefix_ipv6 address;
459
29b94d58
RW
460 if (!ripng)
461 return -1;
462
d62a17ae 463 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
464 struct prefix *p;
fe08ba7e 465 struct agg_node *n;
d62a17ae 466
467 p = connected->address;
468
469 if (p->family == AF_INET6) {
470 address.family = AF_INET6;
471 address.prefix = p->u.prefix6;
472 address.prefixlen = IPV6_MAX_BITLEN;
473
29b94d58 474 n = agg_node_match(ripng->enable_network,
fe08ba7e 475 (struct prefix *)&address);
ee6f7757 476 if (n) {
fe08ba7e 477 agg_unlock_node(n);
d62a17ae 478 return 1;
479 }
480 }
481 }
482 return -1;
718e3744 483}
484
29b94d58 485/* Check wether connected is within the ripng->enable_network table. */
d62a17ae 486static int ripng_enable_network_lookup2(struct connected *connected)
a94434b6 487{
5c84b9a5
RW
488 struct ripng_interface *ri = connected->ifp->info;
489 struct ripng *ripng = ri->ripng;
d62a17ae 490 struct prefix_ipv6 address;
491 struct prefix *p;
a94434b6 492
29b94d58
RW
493 if (!ripng)
494 return -1;
495
d62a17ae 496 p = connected->address;
a94434b6 497
d62a17ae 498 if (p->family == AF_INET6) {
fe08ba7e 499 struct agg_node *node;
a94434b6 500
d62a17ae 501 address.family = p->family;
502 address.prefix = p->u.prefix6;
503 address.prefixlen = IPV6_MAX_BITLEN;
a94434b6 504
d62a17ae 505 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
29b94d58
RW
506 * ripng->enable_network */
507 node = agg_node_match(ripng->enable_network,
fe08ba7e 508 (struct prefix *)&address);
a94434b6 509
d62a17ae 510 if (node) {
fe08ba7e 511 agg_unlock_node(node);
d62a17ae 512 return 1;
513 }
514 }
a94434b6 515
d62a17ae 516 return -1;
a94434b6 517}
518
718e3744 519/* Add RIPng enable network. */
5c84b9a5 520int ripng_enable_network_add(struct ripng *ripng, struct prefix *p)
718e3744 521{
fe08ba7e 522 struct agg_node *node;
718e3744 523
29b94d58 524 node = agg_node_get(ripng->enable_network, p);
718e3744 525
d62a17ae 526 if (node->info) {
fe08ba7e 527 agg_unlock_node(node);
cc48702b 528 return NB_ERR_INCONSISTENCY;
d62a17ae 529 } else
530 node->info = (void *)1;
718e3744 531
d62a17ae 532 /* XXX: One should find a better solution than a generic one */
5c84b9a5 533 ripng_enable_apply_all(ripng);
a94434b6 534
cc48702b 535 return NB_OK;
718e3744 536}
537
538/* Delete RIPng enable network. */
5c84b9a5 539int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p)
718e3744 540{
fe08ba7e 541 struct agg_node *node;
718e3744 542
29b94d58 543 node = agg_node_lookup(ripng->enable_network, p);
d62a17ae 544 if (node) {
545 node->info = NULL;
718e3744 546
d62a17ae 547 /* Unlock info lock. */
fe08ba7e 548 agg_unlock_node(node);
718e3744 549
d62a17ae 550 /* Unlock lookup lock. */
fe08ba7e 551 agg_unlock_node(node);
718e3744 552
cc48702b 553 return NB_OK;
d62a17ae 554 }
cc48702b
RW
555
556 return NB_ERR_INCONSISTENCY;
718e3744 557}
558
559/* Lookup function. */
5c84b9a5 560static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname)
718e3744 561{
d62a17ae 562 unsigned int i;
563 char *str;
564
b0ba762f
RW
565 if (!ripng)
566 return -1;
567
568 for (i = 0; i < vector_active(ripng->enable_if); i++)
569 if ((str = vector_slot(ripng->enable_if, i)) != NULL)
d62a17ae 570 if (strcmp(str, ifname) == 0)
571 return i;
572 return -1;
718e3744 573}
574
5c84b9a5 575int ripng_enable_if_add(struct ripng *ripng, const char *ifname)
718e3744 576{
d62a17ae 577 int ret;
718e3744 578
5c84b9a5 579 ret = ripng_enable_if_lookup(ripng, ifname);
d62a17ae 580 if (ret >= 0)
cc48702b 581 return NB_ERR_INCONSISTENCY;
718e3744 582
b0ba762f 583 vector_set(ripng->enable_if, strdup(ifname));
718e3744 584
5c84b9a5 585 ripng_enable_apply_all(ripng);
a94434b6 586
cc48702b 587 return NB_OK;
718e3744 588}
589
5c84b9a5 590int ripng_enable_if_delete(struct ripng *ripng, const char *ifname)
718e3744 591{
d62a17ae 592 int index;
593 char *str;
718e3744 594
5c84b9a5 595 index = ripng_enable_if_lookup(ripng, ifname);
d62a17ae 596 if (index < 0)
cc48702b 597 return NB_ERR_INCONSISTENCY;
718e3744 598
b0ba762f 599 str = vector_slot(ripng->enable_if, index);
d62a17ae 600 free(str);
b0ba762f 601 vector_unset(ripng->enable_if, index);
718e3744 602
5c84b9a5 603 ripng_enable_apply_all(ripng);
a94434b6 604
cc48702b 605 return NB_OK;
718e3744 606}
607
608/* Wake up interface. */
d62a17ae 609static int ripng_interface_wakeup(struct thread *t)
718e3744 610{
d62a17ae 611 struct interface *ifp;
612 struct ripng_interface *ri;
718e3744 613
d62a17ae 614 /* Get interface. */
615 ifp = THREAD_ARG(t);
718e3744 616
d62a17ae 617 ri = ifp->info;
618 ri->t_wakeup = NULL;
718e3744 619
d62a17ae 620 /* Join to multicast group. */
5c84b9a5 621 if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) {
450971aa 622 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
623 "multicast join failed, interface %s not running",
624 ifp->name);
d62a17ae 625 return 0;
626 }
627
628 /* Set running flag. */
629 ri->running = 1;
718e3744 630
d62a17ae 631 /* Send RIP request to the interface. */
632 ripng_request(ifp);
718e3744 633
d62a17ae 634 return 0;
718e3744 635}
636
d62a17ae 637static void ripng_connect_set(struct interface *ifp, int set)
a94434b6 638{
5c84b9a5
RW
639 struct ripng_interface *ri = ifp->info;
640 struct ripng *ripng = ri->ripng;
d62a17ae 641 struct listnode *node, *nnode;
642 struct connected *connected;
643 struct prefix_ipv6 address;
644
645 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
646 struct prefix *p;
647 p = connected->address;
648
649 if (p->family != AF_INET6)
650 continue;
651
652 address.family = AF_INET6;
653 address.prefix = p->u.prefix6;
654 address.prefixlen = p->prefixlen;
655 apply_mask_ipv6(&address);
656
657 if (set) {
658 /* Check once more wether this prefix is within a
659 * "network IF_OR_PREF" one */
5c84b9a5
RW
660 if ((ripng_enable_if_lookup(ripng, connected->ifp->name)
661 >= 0)
d62a17ae 662 || (ripng_enable_network_lookup2(connected) >= 0))
663 ripng_redistribute_add(
5c84b9a5 664 ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 665 RIPNG_ROUTE_INTERFACE, &address,
666 connected->ifp->ifindex, NULL, 0);
667 } else {
5c84b9a5
RW
668 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
669 RIPNG_ROUTE_INTERFACE,
670 &address,
671 connected->ifp->ifindex);
672 if (ripng_redistribute_check(ripng,
673 ZEBRA_ROUTE_CONNECT))
d62a17ae 674 ripng_redistribute_add(
5c84b9a5 675 ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 676 RIPNG_ROUTE_REDISTRIBUTE, &address,
677 connected->ifp->ifindex, NULL, 0);
678 }
679 }
a94434b6 680}
681
718e3744 682/* Check RIPng is enabed on this interface. */
d62a17ae 683void ripng_enable_apply(struct interface *ifp)
718e3744 684{
d62a17ae 685 int ret;
686 struct ripng_interface *ri = NULL;
687
688 /* Check interface. */
689 if (!if_is_up(ifp))
690 return;
691
692 ri = ifp->info;
693
694 /* Is this interface a candidate for RIPng ? */
695 ret = ripng_enable_network_lookup_if(ifp);
696
697 /* If the interface is matched. */
698 if (ret > 0)
699 ri->enable_network = 1;
700 else
701 ri->enable_network = 0;
702
703 /* Check interface name configuration. */
5c84b9a5 704 ret = ripng_enable_if_lookup(ri->ripng, ifp->name);
d62a17ae 705 if (ret >= 0)
706 ri->enable_interface = 1;
707 else
708 ri->enable_interface = 0;
709
710 /* any candidate interface MUST have a link-local IPv6 address */
711 if ((!ripng_if_ipv6_lladdress_check(ifp))
712 && (ri->enable_network || ri->enable_interface)) {
713 ri->enable_network = 0;
714 ri->enable_interface = 0;
715 zlog_warn("Interface %s does not have any link-local address",
716 ifp->name);
717 }
718
719 /* Update running status of the interface. */
720 if (ri->enable_network || ri->enable_interface) {
721 zlog_info("RIPng INTERFACE ON %s", ifp->name);
722
723 /* Add interface wake up thread. */
724 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
725 &ri->t_wakeup);
726
727 ripng_connect_set(ifp, 1);
728 } else {
729 if (ri->running) {
730 /* Might as well clean up the route table as well
731 * ripng_if_down sets to 0 ri->running, and displays
732 *"turn off %s"
733 **/
734 ripng_if_down(ifp);
735
736 ripng_connect_set(ifp, 0);
737 }
718e3744 738 }
718e3744 739}
740
741/* Set distribute list to all interfaces. */
5c84b9a5 742static void ripng_enable_apply_all(struct ripng *ripng)
718e3744 743{
d62a17ae 744 struct interface *ifp;
718e3744 745
dde7b15b 746 FOR_ALL_INTERFACES (ripng->vrf, ifp)
d62a17ae 747 ripng_enable_apply(ifp);
718e3744 748}
6b0655a2 749
a94434b6 750/* Clear all network and neighbor configuration */
5c84b9a5 751void ripng_clean_network(struct ripng *ripng)
a94434b6 752{
d62a17ae 753 unsigned int i;
754 char *str;
fe08ba7e 755 struct agg_node *rn;
d62a17ae 756
29b94d58
RW
757 /* ripng->enable_network */
758 for (rn = agg_route_top(ripng->enable_network); rn;
fe08ba7e 759 rn = agg_route_next(rn))
d62a17ae 760 if (rn->info) {
761 rn->info = NULL;
fe08ba7e 762 agg_unlock_node(rn);
d62a17ae 763 }
764
b0ba762f
RW
765 /* ripng->enable_if */
766 for (i = 0; i < vector_active(ripng->enable_if); i++)
767 if ((str = vector_slot(ripng->enable_if, i)) != NULL) {
d62a17ae 768 free(str);
b0ba762f 769 vector_slot(ripng->enable_if, i) = NULL;
d62a17ae 770 }
a94434b6 771}
6b0655a2 772
718e3744 773/* Utility function for looking up passive interface settings. */
5c84b9a5
RW
774static int ripng_passive_interface_lookup(struct ripng *ripng,
775 const char *ifname)
718e3744 776{
d62a17ae 777 unsigned int i;
778 char *str;
779
0c32404f
RW
780 for (i = 0; i < vector_active(ripng->passive_interface); i++)
781 if ((str = vector_slot(ripng->passive_interface, i)) != NULL)
d62a17ae 782 if (strcmp(str, ifname) == 0)
783 return i;
784 return -1;
718e3744 785}
786
d62a17ae 787void ripng_passive_interface_apply(struct interface *ifp)
718e3744 788{
d62a17ae 789 int ret;
790 struct ripng_interface *ri;
5c84b9a5 791 struct ripng *ripng;
718e3744 792
d62a17ae 793 ri = ifp->info;
5c84b9a5
RW
794 ripng = ri->ripng;
795 if (!ripng)
796 return;
718e3744 797
5c84b9a5 798 ret = ripng_passive_interface_lookup(ripng, ifp->name);
d62a17ae 799 if (ret < 0)
800 ri->passive = 0;
801 else
802 ri->passive = 1;
718e3744 803}
804
5c84b9a5 805static void ripng_passive_interface_apply_all(struct ripng *ripng)
718e3744 806{
d62a17ae 807 struct interface *ifp;
718e3744 808
dde7b15b 809 FOR_ALL_INTERFACES (ripng->vrf, ifp)
d62a17ae 810 ripng_passive_interface_apply(ifp);
718e3744 811}
812
813/* Passive interface. */
5c84b9a5 814int ripng_passive_interface_set(struct ripng *ripng, const char *ifname)
718e3744 815{
5c84b9a5 816 if (ripng_passive_interface_lookup(ripng, ifname) >= 0)
22e8c7ae 817 return NB_ERR_INCONSISTENCY;
718e3744 818
0c32404f 819 vector_set(ripng->passive_interface, strdup(ifname));
718e3744 820
5c84b9a5 821 ripng_passive_interface_apply_all(ripng);
718e3744 822
22e8c7ae 823 return NB_OK;
718e3744 824}
825
5c84b9a5 826int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname)
718e3744 827{
d62a17ae 828 int i;
829 char *str;
718e3744 830
5c84b9a5 831 i = ripng_passive_interface_lookup(ripng, ifname);
d62a17ae 832 if (i < 0)
22e8c7ae 833 return NB_ERR_INCONSISTENCY;
718e3744 834
0c32404f 835 str = vector_slot(ripng->passive_interface, i);
d62a17ae 836 free(str);
0c32404f 837 vector_unset(ripng->passive_interface, i);
718e3744 838
5c84b9a5 839 ripng_passive_interface_apply_all(ripng);
718e3744 840
22e8c7ae 841 return NB_OK;
718e3744 842}
843
844/* Free all configured RIP passive-interface settings. */
5c84b9a5 845void ripng_passive_interface_clean(struct ripng *ripng)
718e3744 846{
d62a17ae 847 unsigned int i;
848 char *str;
849
0c32404f
RW
850 for (i = 0; i < vector_active(ripng->passive_interface); i++)
851 if ((str = vector_slot(ripng->passive_interface, i)) != NULL) {
d62a17ae 852 free(str);
0c32404f 853 vector_slot(ripng->passive_interface, i) = NULL;
d62a17ae 854 }
5c84b9a5 855 ripng_passive_interface_apply_all(ripng);
718e3744 856}
857
858/* Write RIPng enable network and interface to the vty. */
5c84b9a5 859int ripng_network_write(struct vty *vty, struct ripng *ripng)
718e3744 860{
d62a17ae 861 unsigned int i;
862 const char *ifname;
fe08ba7e 863 struct agg_node *node;
d62a17ae 864
865 /* Write enable network. */
29b94d58 866 for (node = agg_route_top(ripng->enable_network); node;
fe08ba7e 867 node = agg_route_next(node))
26a3ffd6
DS
868 if (node->info)
869 vty_out(vty, " %pRN\n", node);
d62a17ae 870
871 /* Write enable interface. */
b0ba762f
RW
872 for (i = 0; i < vector_active(ripng->enable_if); i++)
873 if ((ifname = vector_slot(ripng->enable_if, i)) != NULL)
22e8c7ae 874 vty_out(vty, " %s\n", ifname);
d62a17ae 875
876 return 0;
718e3744 877}
878
d62a17ae 879static struct ripng_interface *ri_new(void)
718e3744 880{
d62a17ae 881 struct ripng_interface *ri;
5c84b9a5 882
eaf58ba9 883 ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface));
a94434b6 884
d62a17ae 885 /* Set default split-horizon behavior. If the interface is Frame
886 Relay or SMDS is enabled, the default value for split-horizon is
887 off. But currently Zebra does detect Frame Relay or SMDS
888 interface. So all interface is set to split horizon. */
d406db4c
RW
889 ri->split_horizon =
890 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE);
a94434b6 891
d62a17ae 892 return ri;
718e3744 893}
894
dde7b15b
RW
895void ripng_interface_sync(struct interface *ifp)
896{
897 struct vrf *vrf;
898
a36898e7 899 vrf = vrf_lookup_by_id(ifp->vrf_id);
dde7b15b
RW
900 if (vrf) {
901 struct ripng_interface *ri;
902
903 ri = ifp->info;
904 if (ri)
905 ri->ripng = vrf->info;
906 }
907}
908
d62a17ae 909static int ripng_if_new_hook(struct interface *ifp)
718e3744 910{
d62a17ae 911 ifp->info = ri_new();
dde7b15b
RW
912 ripng_interface_sync(ifp);
913
d62a17ae 914 return 0;
718e3744 915}
916
a94434b6 917/* Called when interface structure deleted. */
d62a17ae 918static int ripng_if_delete_hook(struct interface *ifp)
a94434b6 919{
eaf58ba9 920 XFREE(MTYPE_RIPNG_IF, ifp->info);
d62a17ae 921 return 0;
a94434b6 922}
923
718e3744 924/* Configuration write function for ripngd. */
d62a17ae 925static int interface_config_write(struct vty *vty)
718e3744 926{
dde7b15b 927 struct vrf *vrf;
d62a17ae 928 int write = 0;
929
dde7b15b
RW
930 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
931 struct interface *ifp;
d62a17ae 932
dde7b15b
RW
933 FOR_ALL_INTERFACES (vrf, ifp) {
934 struct lyd_node *dnode;
d62a17ae 935
3bb513c3 936 dnode = yang_dnode_getf(
dde7b15b
RW
937 running_config->dnode,
938 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
939 ifp->name, vrf->name);
940 if (dnode == NULL)
941 continue;
d62a17ae 942
dde7b15b
RW
943 write = 1;
944 nb_cli_show_dnode_cmds(vty, dnode, false);
945 }
a94434b6 946 }
d406db4c 947
d62a17ae 948 return write;
718e3744 949}
950
612c2c15 951static int interface_config_write(struct vty *vty);
718e3744 952/* ripngd's interface node. */
d62a17ae 953static struct cmd_node interface_node = {
f4b8291f 954 .name = "interface",
62b346ee 955 .node = INTERFACE_NODE,
24389580 956 .parent_node = CONFIG_NODE,
62b346ee 957 .prompt = "%s(config-if)# ",
612c2c15 958 .config_write = interface_config_write,
718e3744 959};
960
961/* Initialization of interface. */
4d762f26 962void ripng_if_init(void)
718e3744 963{
d62a17ae 964 /* Interface initialize. */
ce19a04a
DL
965 hook_register_prio(if_add, 0, ripng_if_new_hook);
966 hook_register_prio(if_del, 0, ripng_if_delete_hook);
718e3744 967
d62a17ae 968 /* Install interface node. */
612c2c15 969 install_node(&interface_node);
d62a17ae 970 if_cmd_init();
138c5a74
DS
971 if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up,
972 ripng_ifp_down, ripng_ifp_destroy);
718e3744 973}