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