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