]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_interface.c
Merge pull request #10447 from ton31337/fix/json_with_whitespaces
[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 171 ri = ifp->info;
29ceb848
IR
172
173 THREAD_OFF(ri->t_wakeup);
174
5c84b9a5
RW
175 ripng = ri->ripng;
176
d62a17ae 177 if (ripng)
fe08ba7e
DS
178 for (rp = agg_route_top(ripng->table); rp;
179 rp = agg_route_next(rp))
d62a17ae 180 if ((list = rp->info) != NULL)
181 for (ALL_LIST_ELEMENTS(list, listnode, nextnode,
182 rinfo))
183 if (rinfo->ifindex == ifp->ifindex)
5c84b9a5 184 ripng_ecmp_delete(ripng, rinfo);
d62a17ae 185
d62a17ae 186
187 if (ri->running) {
188 if (IS_RIPNG_DEBUG_EVENT)
189 zlog_debug("turn off %s", ifp->name);
190
191 /* Leave from multicast group. */
fa3bf3a2
MS
192 if (ripng)
193 ripng_multicast_leave(ifp, ripng->sock);
d62a17ae 194
195 ri->running = 0;
196 }
197
198 return 0;
718e3744 199}
200
201/* Inteface link up message processing. */
ddbf3e60 202static int ripng_ifp_up(struct interface *ifp)
718e3744 203{
096f7609 204 if (IS_RIPNG_DEBUG_ZEBRA)
d62a17ae 205 zlog_debug(
24c4ee4a 206 "interface up %s vrf %s(%u) index %d flags %llx metric %d mtu %d",
096f7609
IR
207 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
208 ifp->ifindex, (unsigned long long)ifp->flags,
209 ifp->metric, ifp->mtu6);
dde7b15b
RW
210
211 ripng_interface_sync(ifp);
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. */
b0b69e59 226static int ripng_ifp_down(struct interface *ifp)
718e3744 227{
dde7b15b 228 ripng_interface_sync(ifp);
d62a17ae 229 ripng_if_down(ifp);
718e3744 230
096f7609 231 if (IS_RIPNG_DEBUG_ZEBRA)
d62a17ae 232 zlog_debug(
24c4ee4a 233 "interface down %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
096f7609
IR
234 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
235 ifp->ifindex, (unsigned long long)ifp->flags,
236 ifp->metric, ifp->mtu6);
718e3744 237
d62a17ae 238 return 0;
718e3744 239}
240
241/* Inteface addition message from zebra. */
ef7bd2a3 242static int ripng_ifp_create(struct interface *ifp)
718e3744 243{
dde7b15b 244 ripng_interface_sync(ifp);
718e3744 245
096f7609 246 if (IS_RIPNG_DEBUG_ZEBRA)
d62a17ae 247 zlog_debug(
24c4ee4a 248 "RIPng interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
096f7609
IR
249 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
250 ifp->ifindex, (unsigned long long)ifp->flags,
251 ifp->metric, ifp->mtu6);
718e3744 252
d62a17ae 253 /* Check is this interface is RIP enabled or not.*/
254 ripng_enable_apply(ifp);
718e3744 255
d62a17ae 256 /* Apply distribute list to the interface. */
257 ripng_distribute_update_interface(ifp);
718e3744 258
d62a17ae 259 /* Check interface routemap. */
260 ripng_if_rmap_update_interface(ifp);
718e3744 261
d62a17ae 262 return 0;
718e3744 263}
264
3c3c3252 265static int ripng_ifp_destroy(struct interface *ifp)
718e3744 266{
dde7b15b 267 ripng_interface_sync(ifp);
d62a17ae 268 if (if_is_up(ifp)) {
269 ripng_if_down(ifp);
270 }
a94434b6 271
13c5f95e
DS
272 if (IS_RIPNG_DEBUG_ZEBRA)
273 zlog_debug(
274 "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d",
096f7609
IR
275 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
276 ifp->ifindex, (unsigned long long)ifp->flags,
277 ifp->metric, ifp->mtu6);
a94434b6 278
d62a17ae 279 return 0;
718e3744 280}
281
dde7b15b 282/* VRF update for an interface. */
121f9dee 283int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS)
dde7b15b
RW
284{
285 struct interface *ifp;
286 vrf_id_t new_vrf_id;
287
288 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
289 &new_vrf_id);
290 if (!ifp)
291 return 0;
292
24c4ee4a 293 if (IS_RIPNG_DEBUG_ZEBRA) {
24c4ee4a
DS
294 struct vrf *nvrf = vrf_lookup_by_id(new_vrf_id);
295
296 zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)",
096f7609
IR
297 ifp->name, ifp->vrf->name, vrf_id, VRF_LOGNAME(nvrf),
298 new_vrf_id);
24c4ee4a 299 }
dde7b15b 300
a36898e7 301 if_update_to_new_vrf(ifp, new_vrf_id);
dde7b15b
RW
302 ripng_interface_sync(ifp);
303
304 return 0;
305}
306
5c84b9a5 307void ripng_interface_clean(struct ripng *ripng)
a94434b6 308{
d62a17ae 309 struct interface *ifp;
310 struct ripng_interface *ri;
a94434b6 311
dde7b15b 312 FOR_ALL_INTERFACES (ripng->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
b3d6bc6e 319 thread_cancel(&ri->t_wakeup);
d62a17ae 320 }
321}
a94434b6 322
d62a17ae 323static void ripng_apply_address_add(struct connected *ifc)
324{
5c84b9a5
RW
325 struct ripng_interface *ri = ifc->ifp->info;
326 struct ripng *ripng = ri->ripng;
d62a17ae 327 struct prefix_ipv6 address;
328 struct prefix *p;
329
330 if (!ripng)
331 return;
332
333 if (!if_is_up(ifc->ifp))
334 return;
335
336 p = ifc->address;
337
338 memset(&address, 0, sizeof(address));
339 address.family = p->family;
340 address.prefix = p->u.prefix6;
341 address.prefixlen = p->prefixlen;
342 apply_mask_ipv6(&address);
343
344 /* Check if this interface is RIP enabled or not
345 or Check if this address's prefix is RIP enabled */
5c84b9a5 346 if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0)
d62a17ae 347 || (ripng_enable_network_lookup2(ifc) >= 0))
5c84b9a5 348 ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 349 RIPNG_ROUTE_INTERFACE, &address,
350 ifc->ifp->ifindex, NULL, 0);
a94434b6 351}
352
121f9dee 353int ripng_interface_address_add(ZAPI_CALLBACK_ARGS)
718e3744 354{
d62a17ae 355 struct connected *c;
356 struct prefix *p;
718e3744 357
d62a17ae 358 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
359 zclient->ibuf, vrf_id);
718e3744 360
d62a17ae 361 if (c == NULL)
362 return 0;
718e3744 363
d62a17ae 364 p = c->address;
718e3744 365
d62a17ae 366 if (p->family == AF_INET6) {
367 struct ripng_interface *ri = c->ifp->info;
a94434b6 368
d62a17ae 369 if (IS_RIPNG_DEBUG_ZEBRA)
e0259674 370 zlog_debug("RIPng connected address %pFX add", p);
a94434b6 371
d62a17ae 372 /* Check is this prefix needs to be redistributed. */
373 ripng_apply_address_add(c);
a94434b6 374
d62a17ae 375 /* Let's try once again whether the interface could be activated
376 */
377 if (!ri->running) {
378 /* Check if this interface is RIP enabled or not.*/
379 ripng_enable_apply(c->ifp);
a94434b6 380
d62a17ae 381 /* Apply distribute list to the interface. */
382 ripng_distribute_update_interface(c->ifp);
718e3744 383
d62a17ae 384 /* Check interface routemap. */
385 ripng_if_rmap_update_interface(c->ifp);
386 }
387 }
388
389 return 0;
718e3744 390}
391
d62a17ae 392static void ripng_apply_address_del(struct connected *ifc)
393{
5c84b9a5
RW
394 struct ripng_interface *ri = ifc->ifp->info;
395 struct ripng *ripng = ri->ripng;
d62a17ae 396 struct prefix_ipv6 address;
397 struct prefix *p;
a94434b6 398
d62a17ae 399 if (!ripng)
400 return;
a94434b6 401
d62a17ae 402 if (!if_is_up(ifc->ifp))
403 return;
a94434b6 404
d62a17ae 405 p = ifc->address;
a94434b6 406
d62a17ae 407 memset(&address, 0, sizeof(address));
408 address.family = p->family;
409 address.prefix = p->u.prefix6;
410 address.prefixlen = p->prefixlen;
411 apply_mask_ipv6(&address);
a94434b6 412
5c84b9a5
RW
413 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
414 RIPNG_ROUTE_INTERFACE, &address,
415 ifc->ifp->ifindex);
a94434b6 416}
417
121f9dee 418int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS)
718e3744 419{
d62a17ae 420 struct connected *ifc;
421 struct prefix *p;
d62a17ae 422
423 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
424 zclient->ibuf, vrf_id);
425
426 if (ifc) {
427 p = ifc->address;
428
429 if (p->family == AF_INET6) {
430 if (IS_RIPNG_DEBUG_ZEBRA)
431 zlog_debug(
e0259674
DS
432 "RIPng connected address %pFX delete",
433 p);
d62a17ae 434
435 /* Check wether this prefix needs to be removed. */
436 ripng_apply_address_del(ifc);
437 }
721c0857 438 connected_free(&ifc);
718e3744 439 }
718e3744 440
d62a17ae 441 return 0;
718e3744 442}
6b0655a2 443
718e3744 444/* Lookup RIPng enable network. */
a94434b6 445/* Check wether the interface has at least a connected prefix that
29b94d58 446 * is within the ripng->enable_network table. */
d62a17ae 447static int ripng_enable_network_lookup_if(struct interface *ifp)
718e3744 448{
5c84b9a5
RW
449 struct ripng_interface *ri = ifp->info;
450 struct ripng *ripng = ri->ripng;
d62a17ae 451 struct listnode *node;
452 struct connected *connected;
453 struct prefix_ipv6 address;
454
29b94d58
RW
455 if (!ripng)
456 return -1;
457
d62a17ae 458 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
459 struct prefix *p;
fe08ba7e 460 struct agg_node *n;
d62a17ae 461
462 p = connected->address;
463
464 if (p->family == AF_INET6) {
465 address.family = AF_INET6;
466 address.prefix = p->u.prefix6;
467 address.prefixlen = IPV6_MAX_BITLEN;
468
29b94d58 469 n = agg_node_match(ripng->enable_network,
fe08ba7e 470 (struct prefix *)&address);
ee6f7757 471 if (n) {
fe08ba7e 472 agg_unlock_node(n);
d62a17ae 473 return 1;
474 }
475 }
476 }
477 return -1;
718e3744 478}
479
29b94d58 480/* Check wether connected is within the ripng->enable_network table. */
d62a17ae 481static int ripng_enable_network_lookup2(struct connected *connected)
a94434b6 482{
5c84b9a5
RW
483 struct ripng_interface *ri = connected->ifp->info;
484 struct ripng *ripng = ri->ripng;
d62a17ae 485 struct prefix_ipv6 address;
486 struct prefix *p;
a94434b6 487
29b94d58
RW
488 if (!ripng)
489 return -1;
490
d62a17ae 491 p = connected->address;
a94434b6 492
d62a17ae 493 if (p->family == AF_INET6) {
fe08ba7e 494 struct agg_node *node;
a94434b6 495
d62a17ae 496 address.family = p->family;
497 address.prefix = p->u.prefix6;
498 address.prefixlen = IPV6_MAX_BITLEN;
a94434b6 499
d62a17ae 500 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within
29b94d58
RW
501 * ripng->enable_network */
502 node = agg_node_match(ripng->enable_network,
fe08ba7e 503 (struct prefix *)&address);
a94434b6 504
d62a17ae 505 if (node) {
fe08ba7e 506 agg_unlock_node(node);
d62a17ae 507 return 1;
508 }
509 }
a94434b6 510
d62a17ae 511 return -1;
a94434b6 512}
513
718e3744 514/* Add RIPng enable network. */
5c84b9a5 515int ripng_enable_network_add(struct ripng *ripng, struct prefix *p)
718e3744 516{
fe08ba7e 517 struct agg_node *node;
718e3744 518
29b94d58 519 node = agg_node_get(ripng->enable_network, p);
718e3744 520
d62a17ae 521 if (node->info) {
fe08ba7e 522 agg_unlock_node(node);
cc48702b 523 return NB_ERR_INCONSISTENCY;
d62a17ae 524 } else
525 node->info = (void *)1;
718e3744 526
d62a17ae 527 /* XXX: One should find a better solution than a generic one */
5c84b9a5 528 ripng_enable_apply_all(ripng);
a94434b6 529
cc48702b 530 return NB_OK;
718e3744 531}
532
533/* Delete RIPng enable network. */
5c84b9a5 534int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p)
718e3744 535{
fe08ba7e 536 struct agg_node *node;
718e3744 537
29b94d58 538 node = agg_node_lookup(ripng->enable_network, p);
d62a17ae 539 if (node) {
540 node->info = NULL;
718e3744 541
d62a17ae 542 /* Unlock info lock. */
fe08ba7e 543 agg_unlock_node(node);
718e3744 544
d62a17ae 545 /* Unlock lookup lock. */
fe08ba7e 546 agg_unlock_node(node);
718e3744 547
cc48702b 548 return NB_OK;
d62a17ae 549 }
cc48702b
RW
550
551 return NB_ERR_INCONSISTENCY;
718e3744 552}
553
554/* Lookup function. */
5c84b9a5 555static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname)
718e3744 556{
d62a17ae 557 unsigned int i;
558 char *str;
559
b0ba762f
RW
560 if (!ripng)
561 return -1;
562
563 for (i = 0; i < vector_active(ripng->enable_if); i++)
564 if ((str = vector_slot(ripng->enable_if, i)) != NULL)
d62a17ae 565 if (strcmp(str, ifname) == 0)
566 return i;
567 return -1;
718e3744 568}
569
5c84b9a5 570int ripng_enable_if_add(struct ripng *ripng, const char *ifname)
718e3744 571{
d62a17ae 572 int ret;
718e3744 573
5c84b9a5 574 ret = ripng_enable_if_lookup(ripng, ifname);
d62a17ae 575 if (ret >= 0)
cc48702b 576 return NB_ERR_INCONSISTENCY;
718e3744 577
b0ba762f 578 vector_set(ripng->enable_if, strdup(ifname));
718e3744 579
5c84b9a5 580 ripng_enable_apply_all(ripng);
a94434b6 581
cc48702b 582 return NB_OK;
718e3744 583}
584
5c84b9a5 585int ripng_enable_if_delete(struct ripng *ripng, const char *ifname)
718e3744 586{
d62a17ae 587 int index;
588 char *str;
718e3744 589
5c84b9a5 590 index = ripng_enable_if_lookup(ripng, ifname);
d62a17ae 591 if (index < 0)
cc48702b 592 return NB_ERR_INCONSISTENCY;
718e3744 593
b0ba762f 594 str = vector_slot(ripng->enable_if, index);
d62a17ae 595 free(str);
b0ba762f 596 vector_unset(ripng->enable_if, index);
718e3744 597
5c84b9a5 598 ripng_enable_apply_all(ripng);
a94434b6 599
cc48702b 600 return NB_OK;
718e3744 601}
602
603/* Wake up interface. */
cc9f21da 604static void ripng_interface_wakeup(struct thread *t)
718e3744 605{
d62a17ae 606 struct interface *ifp;
607 struct ripng_interface *ri;
718e3744 608
d62a17ae 609 /* Get interface. */
610 ifp = THREAD_ARG(t);
718e3744 611
d62a17ae 612 ri = ifp->info;
718e3744 613
d62a17ae 614 /* Join to multicast group. */
5c84b9a5 615 if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) {
450971aa 616 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
617 "multicast join failed, interface %s not running",
618 ifp->name);
cc9f21da 619 return;
d62a17ae 620 }
621
622 /* Set running flag. */
623 ri->running = 1;
718e3744 624
d62a17ae 625 /* Send RIP request to the interface. */
626 ripng_request(ifp);
718e3744 627}
628
d62a17ae 629static void ripng_connect_set(struct interface *ifp, int set)
a94434b6 630{
5c84b9a5
RW
631 struct ripng_interface *ri = ifp->info;
632 struct ripng *ripng = ri->ripng;
d62a17ae 633 struct listnode *node, *nnode;
634 struct connected *connected;
635 struct prefix_ipv6 address;
636
637 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
638 struct prefix *p;
639 p = connected->address;
640
641 if (p->family != AF_INET6)
642 continue;
643
644 address.family = AF_INET6;
645 address.prefix = p->u.prefix6;
646 address.prefixlen = p->prefixlen;
647 apply_mask_ipv6(&address);
648
649 if (set) {
650 /* Check once more wether this prefix is within a
651 * "network IF_OR_PREF" one */
5c84b9a5
RW
652 if ((ripng_enable_if_lookup(ripng, connected->ifp->name)
653 >= 0)
d62a17ae 654 || (ripng_enable_network_lookup2(connected) >= 0))
655 ripng_redistribute_add(
5c84b9a5 656 ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 657 RIPNG_ROUTE_INTERFACE, &address,
658 connected->ifp->ifindex, NULL, 0);
659 } else {
5c84b9a5
RW
660 ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT,
661 RIPNG_ROUTE_INTERFACE,
662 &address,
663 connected->ifp->ifindex);
664 if (ripng_redistribute_check(ripng,
665 ZEBRA_ROUTE_CONNECT))
d62a17ae 666 ripng_redistribute_add(
5c84b9a5 667 ripng, ZEBRA_ROUTE_CONNECT,
d62a17ae 668 RIPNG_ROUTE_REDISTRIBUTE, &address,
669 connected->ifp->ifindex, NULL, 0);
670 }
671 }
a94434b6 672}
673
718e3744 674/* Check RIPng is enabed on this interface. */
d62a17ae 675void ripng_enable_apply(struct interface *ifp)
718e3744 676{
d62a17ae 677 int ret;
678 struct ripng_interface *ri = NULL;
679
680 /* Check interface. */
681 if (!if_is_up(ifp))
682 return;
683
684 ri = ifp->info;
685
686 /* Is this interface a candidate for RIPng ? */
687 ret = ripng_enable_network_lookup_if(ifp);
688
689 /* If the interface is matched. */
690 if (ret > 0)
691 ri->enable_network = 1;
692 else
693 ri->enable_network = 0;
694
695 /* Check interface name configuration. */
5c84b9a5 696 ret = ripng_enable_if_lookup(ri->ripng, ifp->name);
d62a17ae 697 if (ret >= 0)
698 ri->enable_interface = 1;
699 else
700 ri->enable_interface = 0;
701
702 /* any candidate interface MUST have a link-local IPv6 address */
703 if ((!ripng_if_ipv6_lladdress_check(ifp))
704 && (ri->enable_network || ri->enable_interface)) {
705 ri->enable_network = 0;
706 ri->enable_interface = 0;
707 zlog_warn("Interface %s does not have any link-local address",
708 ifp->name);
709 }
710
711 /* Update running status of the interface. */
712 if (ri->enable_network || ri->enable_interface) {
713 zlog_info("RIPng INTERFACE ON %s", ifp->name);
714
715 /* Add interface wake up thread. */
716 thread_add_timer(master, ripng_interface_wakeup, ifp, 1,
717 &ri->t_wakeup);
718
719 ripng_connect_set(ifp, 1);
720 } else {
721 if (ri->running) {
722 /* Might as well clean up the route table as well
723 * ripng_if_down sets to 0 ri->running, and displays
724 *"turn off %s"
725 **/
726 ripng_if_down(ifp);
727
728 ripng_connect_set(ifp, 0);
729 }
718e3744 730 }
718e3744 731}
732
733/* Set distribute list to all interfaces. */
5c84b9a5 734static void ripng_enable_apply_all(struct ripng *ripng)
718e3744 735{
d62a17ae 736 struct interface *ifp;
718e3744 737
dde7b15b 738 FOR_ALL_INTERFACES (ripng->vrf, ifp)
d62a17ae 739 ripng_enable_apply(ifp);
718e3744 740}
6b0655a2 741
a94434b6 742/* Clear all network and neighbor configuration */
5c84b9a5 743void ripng_clean_network(struct ripng *ripng)
a94434b6 744{
d62a17ae 745 unsigned int i;
746 char *str;
fe08ba7e 747 struct agg_node *rn;
d62a17ae 748
29b94d58
RW
749 /* ripng->enable_network */
750 for (rn = agg_route_top(ripng->enable_network); rn;
fe08ba7e 751 rn = agg_route_next(rn))
d62a17ae 752 if (rn->info) {
753 rn->info = NULL;
fe08ba7e 754 agg_unlock_node(rn);
d62a17ae 755 }
756
b0ba762f
RW
757 /* ripng->enable_if */
758 for (i = 0; i < vector_active(ripng->enable_if); i++)
759 if ((str = vector_slot(ripng->enable_if, i)) != NULL) {
d62a17ae 760 free(str);
b0ba762f 761 vector_slot(ripng->enable_if, i) = NULL;
d62a17ae 762 }
a94434b6 763}
6b0655a2 764
718e3744 765/* Utility function for looking up passive interface settings. */
5c84b9a5
RW
766static int ripng_passive_interface_lookup(struct ripng *ripng,
767 const char *ifname)
718e3744 768{
d62a17ae 769 unsigned int i;
770 char *str;
771
0c32404f
RW
772 for (i = 0; i < vector_active(ripng->passive_interface); i++)
773 if ((str = vector_slot(ripng->passive_interface, i)) != NULL)
d62a17ae 774 if (strcmp(str, ifname) == 0)
775 return i;
776 return -1;
718e3744 777}
778
d62a17ae 779void ripng_passive_interface_apply(struct interface *ifp)
718e3744 780{
d62a17ae 781 int ret;
782 struct ripng_interface *ri;
5c84b9a5 783 struct ripng *ripng;
718e3744 784
d62a17ae 785 ri = ifp->info;
5c84b9a5
RW
786 ripng = ri->ripng;
787 if (!ripng)
788 return;
718e3744 789
5c84b9a5 790 ret = ripng_passive_interface_lookup(ripng, ifp->name);
d62a17ae 791 if (ret < 0)
792 ri->passive = 0;
793 else
794 ri->passive = 1;
718e3744 795}
796
5c84b9a5 797static void ripng_passive_interface_apply_all(struct ripng *ripng)
718e3744 798{
d62a17ae 799 struct interface *ifp;
718e3744 800
dde7b15b 801 FOR_ALL_INTERFACES (ripng->vrf, ifp)
d62a17ae 802 ripng_passive_interface_apply(ifp);
718e3744 803}
804
805/* Passive interface. */
5c84b9a5 806int ripng_passive_interface_set(struct ripng *ripng, const char *ifname)
718e3744 807{
5c84b9a5 808 if (ripng_passive_interface_lookup(ripng, ifname) >= 0)
22e8c7ae 809 return NB_ERR_INCONSISTENCY;
718e3744 810
0c32404f 811 vector_set(ripng->passive_interface, strdup(ifname));
718e3744 812
5c84b9a5 813 ripng_passive_interface_apply_all(ripng);
718e3744 814
22e8c7ae 815 return NB_OK;
718e3744 816}
817
5c84b9a5 818int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname)
718e3744 819{
d62a17ae 820 int i;
821 char *str;
718e3744 822
5c84b9a5 823 i = ripng_passive_interface_lookup(ripng, ifname);
d62a17ae 824 if (i < 0)
22e8c7ae 825 return NB_ERR_INCONSISTENCY;
718e3744 826
0c32404f 827 str = vector_slot(ripng->passive_interface, i);
d62a17ae 828 free(str);
0c32404f 829 vector_unset(ripng->passive_interface, i);
718e3744 830
5c84b9a5 831 ripng_passive_interface_apply_all(ripng);
718e3744 832
22e8c7ae 833 return NB_OK;
718e3744 834}
835
836/* Free all configured RIP passive-interface settings. */
5c84b9a5 837void ripng_passive_interface_clean(struct ripng *ripng)
718e3744 838{
d62a17ae 839 unsigned int i;
840 char *str;
841
0c32404f
RW
842 for (i = 0; i < vector_active(ripng->passive_interface); i++)
843 if ((str = vector_slot(ripng->passive_interface, i)) != NULL) {
d62a17ae 844 free(str);
0c32404f 845 vector_slot(ripng->passive_interface, i) = NULL;
d62a17ae 846 }
5c84b9a5 847 ripng_passive_interface_apply_all(ripng);
718e3744 848}
849
850/* Write RIPng enable network and interface to the vty. */
5c84b9a5 851int ripng_network_write(struct vty *vty, struct ripng *ripng)
718e3744 852{
d62a17ae 853 unsigned int i;
854 const char *ifname;
fe08ba7e 855 struct agg_node *node;
d62a17ae 856
857 /* Write enable network. */
29b94d58 858 for (node = agg_route_top(ripng->enable_network); node;
fe08ba7e 859 node = agg_route_next(node))
26a3ffd6
DS
860 if (node->info)
861 vty_out(vty, " %pRN\n", node);
d62a17ae 862
863 /* Write enable interface. */
b0ba762f
RW
864 for (i = 0; i < vector_active(ripng->enable_if); i++)
865 if ((ifname = vector_slot(ripng->enable_if, i)) != NULL)
22e8c7ae 866 vty_out(vty, " %s\n", ifname);
d62a17ae 867
868 return 0;
718e3744 869}
870
d62a17ae 871static struct ripng_interface *ri_new(void)
718e3744 872{
d62a17ae 873 struct ripng_interface *ri;
5c84b9a5 874
eaf58ba9 875 ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface));
a94434b6 876
d62a17ae 877 /* Set default split-horizon behavior. If the interface is Frame
878 Relay or SMDS is enabled, the default value for split-horizon is
879 off. But currently Zebra does detect Frame Relay or SMDS
880 interface. So all interface is set to split horizon. */
d406db4c
RW
881 ri->split_horizon =
882 yang_get_default_enum("%s/split-horizon", RIPNG_IFACE);
a94434b6 883
d62a17ae 884 return ri;
718e3744 885}
886
dde7b15b
RW
887void ripng_interface_sync(struct interface *ifp)
888{
096f7609 889 struct ripng_interface *ri;
dde7b15b 890
096f7609
IR
891 ri = ifp->info;
892 if (ri)
893 ri->ripng = ifp->vrf->info;
dde7b15b
RW
894}
895
d62a17ae 896static int ripng_if_new_hook(struct interface *ifp)
718e3744 897{
d62a17ae 898 ifp->info = ri_new();
dde7b15b
RW
899 ripng_interface_sync(ifp);
900
d62a17ae 901 return 0;
718e3744 902}
903
a94434b6 904/* Called when interface structure deleted. */
d62a17ae 905static int ripng_if_delete_hook(struct interface *ifp)
a94434b6 906{
eaf58ba9 907 XFREE(MTYPE_RIPNG_IF, ifp->info);
d62a17ae 908 return 0;
a94434b6 909}
910
718e3744 911/* Initialization of interface. */
4d762f26 912void ripng_if_init(void)
718e3744 913{
d62a17ae 914 /* Interface initialize. */
ce19a04a
DL
915 hook_register_prio(if_add, 0, ripng_if_new_hook);
916 hook_register_prio(if_del, 0, ripng_if_delete_hook);
718e3744 917
d62a17ae 918 /* Install interface node. */
104fd767 919 if_cmd_init_default();
138c5a74
DS
920 if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up,
921 ripng_ifp_down, ripng_ifp_destroy);
718e3744 922}