blob: bef7699ae43eaaaa34f4e9ff240a236b48763689 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
26#include "if.h"
27#include "prefix.h"
28#include "memory.h"
29#include "network.h"
30#include "filter.h"
31#include "log.h"
32#include "stream.h"
33#include "zclient.h"
34#include "command.h"
35#include "table.h"
36#include "thread.h"
gdt4d4653a2004-07-01 19:26:33 +000037#include "privs.h"
paul718e3742002-12-13 20:15:29 +000038
39#include "ripngd/ripngd.h"
40#include "ripngd/ripng_debug.h"
41
42/* If RFC2133 definition is used. */
43#ifndef IPV6_JOIN_GROUP
44#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
45#endif
46#ifndef IPV6_LEAVE_GROUP
47#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
48#endif
49
gdt4d4653a2004-07-01 19:26:33 +000050extern struct zebra_privs_t ripngd_privs;
51
paul718e3742002-12-13 20:15:29 +000052/* Static utility function. */
53static void ripng_enable_apply (struct interface *);
54static void ripng_passive_interface_apply (struct interface *);
hasso98b718a2004-10-11 12:57:57 +000055int ripng_enable_if_lookup (const char *ifname);
hassoa94434b2003-05-25 17:10:12 +000056int ripng_enable_network_lookup2 (struct connected *connected);
57void ripng_enable_apply_all ();
paul718e3742002-12-13 20:15:29 +000058
59/* Join to the all rip routers multicast group. */
60int
61ripng_multicast_join (struct interface *ifp)
62{
63 int ret;
64 struct ipv6_mreq mreq;
ajs656b4ee2005-01-30 18:08:12 +000065 int save_errno;
paul718e3742002-12-13 20:15:29 +000066
hassoa94434b2003-05-25 17:10:12 +000067 if (if_is_up (ifp) && if_is_multicast (ifp)) {
68 memset (&mreq, 0, sizeof (mreq));
69 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
70 mreq.ipv6mr_interface = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +000071
gdt4d4653a2004-07-01 19:26:33 +000072 /*
73 * NetBSD 1.6.2 requires root to join groups on gif(4).
74 * While this is bogus, privs are available and easy to use
75 * for this call as a workaround.
76 */
77 if (ripngd_privs.change (ZPRIVS_RAISE))
78 zlog_err ("ripng_multicast_join: could not raise privs");
79
hassoa94434b2003-05-25 17:10:12 +000080 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
81 (char *) &mreq, sizeof (mreq));
ajs656b4ee2005-01-30 18:08:12 +000082 save_errno = errno;
gdtddf1c262004-01-04 01:02:55 +000083
gdt4d4653a2004-07-01 19:26:33 +000084 if (ripngd_privs.change (ZPRIVS_LOWER))
85 zlog_err ("ripng_multicast_join: could not lower privs");
86
ajs656b4ee2005-01-30 18:08:12 +000087 if (ret < 0 && save_errno == EADDRINUSE)
gdtddf1c262004-01-04 01:02:55 +000088 {
89 /*
90 * Group is already joined. This occurs due to sloppy group
91 * management, in particular declining to leave the group on
92 * an interface that has just gone down.
93 */
94 zlog_warn ("ripng join on %s EADDRINUSE (ignoring)\n", ifp->name);
95 return 0; /* not an error */
96 }
97
hassoa94434b2003-05-25 17:10:12 +000098 if (ret < 0)
ajs656b4ee2005-01-30 18:08:12 +000099 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s",
100 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000101
hassoa94434b2003-05-25 17:10:12 +0000102 if (IS_RIPNG_DEBUG_EVENT)
ajs2e23ab22004-12-08 19:46:50 +0000103 zlog_debug ("RIPng %s join to all-rip-routers multicast group", ifp->name);
paul718e3742002-12-13 20:15:29 +0000104
hassoa94434b2003-05-25 17:10:12 +0000105 if (ret < 0)
106 return -1;
107 }
108 return 0;
paul718e3742002-12-13 20:15:29 +0000109}
110
111/* Leave from the all rip routers multicast group. */
112int
113ripng_multicast_leave (struct interface *ifp)
114{
115 int ret;
116 struct ipv6_mreq mreq;
117
hassoa94434b2003-05-25 17:10:12 +0000118 if (if_is_up (ifp) && if_is_multicast (ifp)) {
119 memset (&mreq, 0, sizeof (mreq));
120 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
121 mreq.ipv6mr_interface = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +0000122
hassoa94434b2003-05-25 17:10:12 +0000123 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
124 (char *) &mreq, sizeof (mreq));
125 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000126 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000127
hassoa94434b2003-05-25 17:10:12 +0000128 if (IS_RIPNG_DEBUG_EVENT)
ajs2e23ab22004-12-08 19:46:50 +0000129 zlog_debug ("RIPng %s leave from all-rip-routers multicast group",
hassoa94434b2003-05-25 17:10:12 +0000130 ifp->name);
paul718e3742002-12-13 20:15:29 +0000131
hassoa94434b2003-05-25 17:10:12 +0000132 if (ret < 0)
133 return -1;
134 }
135
136 return 0;
137}
138
139/* How many link local IPv6 address could be used on the interface ? */
140int
141ripng_if_ipv6_lladdress_check (struct interface *ifp)
142{
143 struct listnode *nn;
144 struct connected *connected;
145 int count = 0;
146
147 for (nn = listhead (ifp->connected); nn; nextnode (nn))
148 if ((connected = getdata (nn)) != NULL) {
149 struct prefix *p;
150 p = connected->address;
151
152 if ((p->family == AF_INET6) &&
153 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
154 count++;
155 }
156
157 return count;
paul718e3742002-12-13 20:15:29 +0000158}
159
160/* Check max mtu size. */
hasso98b718a2004-10-11 12:57:57 +0000161unsigned int
paul718e3742002-12-13 20:15:29 +0000162ripng_check_max_mtu ()
163{
hasso52dc7ee2004-09-23 19:18:23 +0000164 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000165 struct interface *ifp;
hasso98b718a2004-10-11 12:57:57 +0000166 unsigned int mtu;
paul718e3742002-12-13 20:15:29 +0000167
168 mtu = 0;
169 for (node = listhead (iflist); node; nextnode (node))
170 {
171 ifp = getdata (node);
hasso1203e1c2004-07-23 21:34:27 +0000172 if (mtu < ifp->mtu6)
173 mtu = ifp->mtu6;
paul718e3742002-12-13 20:15:29 +0000174 }
175 return mtu;
176}
177
178int
179ripng_if_down (struct interface *ifp)
180{
181 struct route_node *rp;
182 struct ripng_info *rinfo;
183 struct ripng_interface *ri;
184
hassoa94434b2003-05-25 17:10:12 +0000185 if (ripng)
paul718e3742002-12-13 20:15:29 +0000186 {
187 for (rp = route_top (ripng->table); rp; rp = route_next (rp))
188 if ((rinfo = rp->info) != NULL)
189 {
190 /* Routes got through this interface. */
191 if (rinfo->ifindex == ifp->ifindex
192 && rinfo->type == ZEBRA_ROUTE_RIPNG
193 && rinfo->sub_type == RIPNG_ROUTE_RTE)
194 {
195 ripng_zebra_ipv6_delete ((struct prefix_ipv6 *) &rp->p,
196 &rinfo->nexthop,
197 rinfo->ifindex);
198
hassoa94434b2003-05-25 17:10:12 +0000199 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
200 (struct prefix_ipv6 *)&rp->p,
201 rinfo->ifindex);
paul718e3742002-12-13 20:15:29 +0000202 }
203 else
204 {
hassoa94434b2003-05-25 17:10:12 +0000205 /* All redistributed routes got through this interface,
206 * but the static and system ones are kept. */
207 if ((rinfo->ifindex == ifp->ifindex) &&
208 (rinfo->type != ZEBRA_ROUTE_STATIC) &&
209 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
paul718e3742002-12-13 20:15:29 +0000210 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
211 (struct prefix_ipv6 *) &rp->p,
212 rinfo->ifindex);
213 }
214 }
215 }
216
217 ri = ifp->info;
218
hassoa94434b2003-05-25 17:10:12 +0000219 if (ri->running)
paul718e3742002-12-13 20:15:29 +0000220 {
221 if (IS_RIPNG_DEBUG_EVENT)
ajs2e23ab22004-12-08 19:46:50 +0000222 zlog_debug ("turn off %s", ifp->name);
paul718e3742002-12-13 20:15:29 +0000223
224 /* Leave from multicast group. */
225 ripng_multicast_leave (ifp);
226
227 ri->running = 0;
228 }
229
230 return 0;
231}
232
233/* Inteface link up message processing. */
234int
235ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length)
236{
237 struct stream *s;
238 struct interface *ifp;
239
240 /* zebra_interface_state_read() updates interface structure in iflist. */
241 s = zclient->ibuf;
242 ifp = zebra_interface_state_read (s);
243
244 if (ifp == NULL)
245 return 0;
246
247 if (IS_RIPNG_DEBUG_ZEBRA)
ajs2e23ab22004-12-08 19:46:50 +0000248 zlog_debug ("interface up %s index %d flags %ld metric %d mtu %d",
hasso1203e1c2004-07-23 21:34:27 +0000249 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000250
251 /* Check if this interface is RIPng enabled or not. */
252 ripng_enable_apply (ifp);
253
254 /* Check for a passive interface. */
255 ripng_passive_interface_apply (ifp);
256
257 /* Apply distribute list to the all interface. */
258 ripng_distribute_update_interface (ifp);
259
260 return 0;
261}
262
263/* Inteface link down message processing. */
264int
265ripng_interface_down (int command, struct zclient *zclient,
266 zebra_size_t length)
267{
268 struct stream *s;
269 struct interface *ifp;
270
271 /* zebra_interface_state_read() updates interface structure in iflist. */
272 s = zclient->ibuf;
273 ifp = zebra_interface_state_read (s);
274
275 if (ifp == NULL)
276 return 0;
277
278 ripng_if_down (ifp);
279
280 if (IS_RIPNG_DEBUG_ZEBRA)
ajs2e23ab22004-12-08 19:46:50 +0000281 zlog_debug ("interface down %s index %d flags %ld metric %d mtu %d",
hasso1203e1c2004-07-23 21:34:27 +0000282 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000283
284 return 0;
285}
286
287/* Inteface addition message from zebra. */
288int
289ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length)
290{
291 struct interface *ifp;
292
293 ifp = zebra_interface_add_read (zclient->ibuf);
294
295 if (IS_RIPNG_DEBUG_ZEBRA)
ajs2e23ab22004-12-08 19:46:50 +0000296 zlog_debug ("RIPng interface add %s index %d flags %ld metric %d mtu %d",
hasso1203e1c2004-07-23 21:34:27 +0000297 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000298
299 /* Check is this interface is RIP enabled or not.*/
300 ripng_enable_apply (ifp);
301
302 /* Apply distribute list to the interface. */
303 ripng_distribute_update_interface (ifp);
304
305 /* Check interface routemap. */
306 ripng_if_rmap_update_interface (ifp);
307
308 return 0;
309}
310
311int
312ripng_interface_delete (int command, struct zclient *zclient,
313 zebra_size_t length)
314{
hassoa94434b2003-05-25 17:10:12 +0000315 struct interface *ifp;
316 struct stream *s;
317
318 s = zclient->ibuf;
319 /* zebra_interface_state_read() updates interface structure in iflist */
320 ifp = zebra_interface_state_read(s);
321
322 if (ifp == NULL)
323 return 0;
324
325 if (if_is_up (ifp)) {
326 ripng_if_down(ifp);
327 }
328
329 zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
hasso1203e1c2004-07-23 21:34:27 +0000330 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu6);
hassoa94434b2003-05-25 17:10:12 +0000331
332 /* To support pseudo interface do not free interface structure. */
333 /* if_delete(ifp); */
334
paul718e3742002-12-13 20:15:29 +0000335 return 0;
336}
337
hassoa94434b2003-05-25 17:10:12 +0000338void
339ripng_interface_clean ()
340{
hasso52dc7ee2004-09-23 19:18:23 +0000341 struct listnode *node;
hassoa94434b2003-05-25 17:10:12 +0000342 struct interface *ifp;
343 struct ripng_interface *ri;
344
345 for (node = listhead (iflist); node; nextnode (node))
346 {
347 ifp = getdata (node);
348 ri = ifp->info;
349
350 ri->enable_network = 0;
351 ri->enable_interface = 0;
352 ri->running = 0;
353
354 if (ri->t_wakeup)
355 {
356 thread_cancel (ri->t_wakeup);
357 ri->t_wakeup = NULL;
358 }
359 }
360}
361
362void
363ripng_interface_reset () {
hasso52dc7ee2004-09-23 19:18:23 +0000364 struct listnode *node;
hassoa94434b2003-05-25 17:10:12 +0000365 struct interface *ifp;
366 struct ripng_interface *ri;
367
368 for (node = listhead (iflist); node; nextnode (node))
369 {
370 ifp = getdata (node);
371 ri = ifp->info;
372
373 ri->enable_network = 0;
374 ri->enable_interface = 0;
375 ri->running = 0;
376
377 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
378 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
379
380 ri->list[RIPNG_FILTER_IN] = NULL;
381 ri->list[RIPNG_FILTER_OUT] = NULL;
382
383 ri->prefix[RIPNG_FILTER_IN] = NULL;
384 ri->prefix[RIPNG_FILTER_OUT] = NULL;
385
386 if (ri->t_wakeup)
387 {
388 thread_cancel (ri->t_wakeup);
389 ri->t_wakeup = NULL;
390 }
391
392 ri->passive = 0;
393 }
394}
395
396static void
397ripng_apply_address_add (struct connected *ifc) {
398 struct prefix_ipv6 address;
399 struct prefix *p;
400
401 if (!ripng)
402 return;
403
404 if (! if_is_up(ifc->ifp))
405 return;
406
407 p = ifc->address;
408
409 memset (&address, 0, sizeof (address));
410 address.family = p->family;
411 address.prefix = p->u.prefix6;
412 address.prefixlen = p->prefixlen;
413 apply_mask_ipv6(&address);
414
415 /* Check if this interface is RIP enabled or not
416 or Check if this address's prefix is RIP enabled */
417 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
418 (ripng_enable_network_lookup2(ifc) >= 0))
419 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
420 &address, ifc->ifp->ifindex, NULL);
421
422}
423
paul718e3742002-12-13 20:15:29 +0000424int
425ripng_interface_address_add (int command, struct zclient *zclient,
426 zebra_size_t length)
427{
428 struct connected *c;
429 struct prefix *p;
paul718e3742002-12-13 20:15:29 +0000430
paul0a589352004-05-08 11:48:26 +0000431 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
432 zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000433
434 if (c == NULL)
435 return 0;
436
437 p = c->address;
438
439 if (p->family == AF_INET6)
440 {
441 if (IS_RIPNG_DEBUG_ZEBRA)
ajs2e23ab22004-12-08 19:46:50 +0000442 zlog_debug ("RIPng connected address %s/%d add",
hassoa94434b2003-05-25 17:10:12 +0000443 inet6_ntop(&p->u.prefix6),
paul718e3742002-12-13 20:15:29 +0000444 p->prefixlen);
445
hassoa94434b2003-05-25 17:10:12 +0000446 /* Check is this prefix needs to be redistributed. */
447 ripng_apply_address_add(c);
448
449 /* Let's try once again whether the interface could be activated */
450 if (c->ifp) {
451 struct ripng_interface *ri = c->ifp->info;
452
453 if (!ri->running) {
454 /* Check if this interface is RIP enabled or not.*/
455 ripng_enable_apply (c->ifp);
456
457 /* Apply distribute list to the interface. */
458 ripng_distribute_update_interface (c->ifp);
459
460 /* Check interface routemap. */
461 ripng_if_rmap_update_interface (c->ifp);
462 }
463 }
464
paul718e3742002-12-13 20:15:29 +0000465 }
466
467 return 0;
468}
469
hassoa94434b2003-05-25 17:10:12 +0000470static void
471ripng_apply_address_del (struct connected *ifc) {
472 struct prefix_ipv6 address;
473 struct prefix *p;
474
475 if (!ripng)
476 return;
477
478 if (! if_is_up(ifc->ifp))
479 return;
480
481 p = ifc->address;
482
483 memset (&address, 0, sizeof (address));
484 address.family = p->family;
485 address.prefix = p->u.prefix6;
486 address.prefixlen = p->prefixlen;
487 apply_mask_ipv6(&address);
488
489 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
490 &address, ifc->ifp->ifindex);
491}
492
paul718e3742002-12-13 20:15:29 +0000493int
494ripng_interface_address_delete (int command, struct zclient *zclient,
495 zebra_size_t length)
496{
497 struct connected *ifc;
498 struct prefix *p;
499 char buf[INET6_ADDRSTRLEN];
500
paul0a589352004-05-08 11:48:26 +0000501 ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
502 zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000503
504 if (ifc)
505 {
506 p = ifc->address;
507
508 if (p->family == AF_INET6)
509 {
510 if (IS_RIPNG_DEBUG_ZEBRA)
ajs2e23ab22004-12-08 19:46:50 +0000511 zlog_debug ("RIPng connected address %s/%d delete",
paul718e3742002-12-13 20:15:29 +0000512 inet_ntop (AF_INET6, &p->u.prefix6, buf,
513 INET6_ADDRSTRLEN),
514 p->prefixlen);
515
hassoa94434b2003-05-25 17:10:12 +0000516 /* Check wether this prefix needs to be removed. */
517 ripng_apply_address_del(ifc);
paul718e3742002-12-13 20:15:29 +0000518 }
519 connected_free (ifc);
520 }
521
522 return 0;
523}
524
525/* RIPng enable interface vector. */
526vector ripng_enable_if;
527
528/* RIPng enable network table. */
529struct route_table *ripng_enable_network;
530
531/* Lookup RIPng enable network. */
hassoa94434b2003-05-25 17:10:12 +0000532/* Check wether the interface has at least a connected prefix that
533 * is within the ripng_enable_network table. */
paul718e3742002-12-13 20:15:29 +0000534int
hassoa94434b2003-05-25 17:10:12 +0000535ripng_enable_network_lookup_if (struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000536{
hasso52dc7ee2004-09-23 19:18:23 +0000537 struct listnode *listnode;
paul718e3742002-12-13 20:15:29 +0000538 struct connected *connected;
hassoa94434b2003-05-25 17:10:12 +0000539 struct prefix_ipv6 address;
paul718e3742002-12-13 20:15:29 +0000540
541 for (listnode = listhead (ifp->connected); listnode; nextnode (listnode))
542 if ((connected = getdata (listnode)) != NULL)
543 {
544 struct prefix *p;
545 struct route_node *node;
546
547 p = connected->address;
548
549 if (p->family == AF_INET6)
550 {
hassoa94434b2003-05-25 17:10:12 +0000551 address.family = AF_INET6;
552 address.prefix = p->u.prefix6;
553 address.prefixlen = IPV6_MAX_BITLEN;
554
555 node = route_node_match (ripng_enable_network,
556 (struct prefix *)&address);
paul718e3742002-12-13 20:15:29 +0000557 if (node)
558 {
559 route_unlock_node (node);
560 return 1;
561 }
562 }
563 }
564 return -1;
565}
566
hassoa94434b2003-05-25 17:10:12 +0000567/* Check wether connected is within the ripng_enable_network table. */
568int
569ripng_enable_network_lookup2 (struct connected *connected)
570{
571 struct prefix_ipv6 address;
572 struct prefix *p;
573
574 p = connected->address;
575
576 if (p->family == AF_INET6) {
577 struct route_node *node;
578
579 address.family = p->family;
580 address.prefix = p->u.prefix6;
581 address.prefixlen = IPV6_MAX_BITLEN;
582
583 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
584 node = route_node_match (ripng_enable_network,
585 (struct prefix *)&address);
586
587 if (node) {
588 route_unlock_node (node);
589 return 1;
590 }
591 }
592
593 return -1;
594}
595
paul718e3742002-12-13 20:15:29 +0000596/* Add RIPng enable network. */
597int
598ripng_enable_network_add (struct prefix *p)
599{
600 struct route_node *node;
601
602 node = route_node_get (ripng_enable_network, p);
603
604 if (node->info)
605 {
606 route_unlock_node (node);
607 return -1;
608 }
609 else
hasso7a1d5832004-10-08 06:32:23 +0000610 node->info = (char *) "enabled";
paul718e3742002-12-13 20:15:29 +0000611
hassoa94434b2003-05-25 17:10:12 +0000612 /* XXX: One should find a better solution than a generic one */
613 ripng_enable_apply_all();
614
paul718e3742002-12-13 20:15:29 +0000615 return 1;
616}
617
618/* Delete RIPng enable network. */
619int
620ripng_enable_network_delete (struct prefix *p)
621{
622 struct route_node *node;
623
624 node = route_node_lookup (ripng_enable_network, p);
625 if (node)
626 {
627 node->info = NULL;
628
629 /* Unlock info lock. */
630 route_unlock_node (node);
631
632 /* Unlock lookup lock. */
633 route_unlock_node (node);
634
635 return 1;
636 }
637 return -1;
638}
639
640/* Lookup function. */
641int
hasso98b718a2004-10-11 12:57:57 +0000642ripng_enable_if_lookup (const char *ifname)
paul718e3742002-12-13 20:15:29 +0000643{
hasso7a1d5832004-10-08 06:32:23 +0000644 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000645 char *str;
646
647 for (i = 0; i < vector_max (ripng_enable_if); i++)
648 if ((str = vector_slot (ripng_enable_if, i)) != NULL)
649 if (strcmp (str, ifname) == 0)
650 return i;
651 return -1;
652}
653
654/* Add interface to ripng_enable_if. */
655int
hasso98b718a2004-10-11 12:57:57 +0000656ripng_enable_if_add (const char *ifname)
paul718e3742002-12-13 20:15:29 +0000657{
658 int ret;
659
660 ret = ripng_enable_if_lookup (ifname);
661 if (ret >= 0)
662 return -1;
663
664 vector_set (ripng_enable_if, strdup (ifname));
665
hassoa94434b2003-05-25 17:10:12 +0000666 ripng_enable_apply_all();
667
paul718e3742002-12-13 20:15:29 +0000668 return 1;
669}
670
671/* Delete interface from ripng_enable_if. */
672int
hasso98b718a2004-10-11 12:57:57 +0000673ripng_enable_if_delete (const char *ifname)
paul718e3742002-12-13 20:15:29 +0000674{
675 int index;
676 char *str;
677
678 index = ripng_enable_if_lookup (ifname);
679 if (index < 0)
680 return -1;
681
682 str = vector_slot (ripng_enable_if, index);
683 free (str);
684 vector_unset (ripng_enable_if, index);
685
hassoa94434b2003-05-25 17:10:12 +0000686 ripng_enable_apply_all();
687
paul718e3742002-12-13 20:15:29 +0000688 return 1;
689}
690
691/* Wake up interface. */
692int
693ripng_interface_wakeup (struct thread *t)
694{
695 struct interface *ifp;
696 struct ripng_interface *ri;
697
698 /* Get interface. */
699 ifp = THREAD_ARG (t);
700
701 ri = ifp->info;
702 ri->t_wakeup = NULL;
703
704 /* Join to multicast group. */
hassoa94434b2003-05-25 17:10:12 +0000705 if (ripng_multicast_join (ifp) < 0) {
706 zlog_err ("multicast join failed, interface %s not running", ifp->name);
707 return 0;
708 }
709
710 /* Set running flag. */
711 ri->running = 1;
paul718e3742002-12-13 20:15:29 +0000712
713 /* Send RIP request to the interface. */
714 ripng_request (ifp);
715
716 return 0;
717}
718
hassoa94434b2003-05-25 17:10:12 +0000719int ripng_redistribute_check (int);
720
721void
722ripng_connect_set (struct interface *ifp, int set)
723{
724 struct listnode *nn;
725 struct connected *connected;
726 struct prefix_ipv6 address;
727
728 for (nn = listhead (ifp->connected); nn; nextnode (nn))
729 if ((connected = getdata (nn)) != NULL) {
730 struct prefix *p;
731 p = connected->address;
732
733 if (p->family != AF_INET6)
734 continue;
735
736 address.family = AF_INET6;
737 address.prefix = p->u.prefix6;
738 address.prefixlen = p->prefixlen;
739 apply_mask_ipv6 (&address);
740
741 if (set) {
742 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
743 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
744 (ripng_enable_network_lookup2(connected) >= 0))
745 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
746 &address, connected->ifp->ifindex, NULL);
747 } else {
748 ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
749 &address, connected->ifp->ifindex);
750 if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
751 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
752 &address, connected->ifp->ifindex, NULL);
753 }
754 }
755}
756
paul718e3742002-12-13 20:15:29 +0000757/* Check RIPng is enabed on this interface. */
758void
759ripng_enable_apply (struct interface *ifp)
760{
761 int ret;
762 struct ripng_interface *ri = NULL;
763
764 /* Check interface. */
paul718e3742002-12-13 20:15:29 +0000765 if (! if_is_up (ifp))
766 return;
767
768 ri = ifp->info;
769
hassoa94434b2003-05-25 17:10:12 +0000770 /* Is this interface a candidate for RIPng ? */
771 ret = ripng_enable_network_lookup_if (ifp);
paul718e3742002-12-13 20:15:29 +0000772
773 /* If the interface is matched. */
774 if (ret > 0)
775 ri->enable_network = 1;
776 else
777 ri->enable_network = 0;
778
779 /* Check interface name configuration. */
780 ret = ripng_enable_if_lookup (ifp->name);
781 if (ret >= 0)
782 ri->enable_interface = 1;
783 else
784 ri->enable_interface = 0;
785
hassoa94434b2003-05-25 17:10:12 +0000786 /* any candidate interface MUST have a link-local IPv6 address */
787 if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
788 (ri->enable_network || ri->enable_interface)) {
789 ri->enable_network = 0;
790 ri->enable_interface = 0;
791 zlog_warn("Interface %s does not have any link-local address",
792 ifp->name);
793 }
794
paul718e3742002-12-13 20:15:29 +0000795 /* Update running status of the interface. */
796 if (ri->enable_network || ri->enable_interface)
797 {
paul718e3742002-12-13 20:15:29 +0000798 {
799 if (IS_RIPNG_DEBUG_EVENT)
ajs2e23ab22004-12-08 19:46:50 +0000800 zlog_debug ("RIPng turn on %s", ifp->name);
paul718e3742002-12-13 20:15:29 +0000801
802 /* Add interface wake up thread. */
803 if (! ri->t_wakeup)
804 ri->t_wakeup = thread_add_timer (master, ripng_interface_wakeup,
805 ifp, 1);
paul718e3742002-12-13 20:15:29 +0000806
hassoa94434b2003-05-25 17:10:12 +0000807 ripng_connect_set (ifp, 1);
paul718e3742002-12-13 20:15:29 +0000808 }
809 }
810 else
811 {
812 if (ri->running)
813 {
hassoa94434b2003-05-25 17:10:12 +0000814 /* Might as well clean up the route table as well
815 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
816 **/
817 ripng_if_down(ifp);
paul718e3742002-12-13 20:15:29 +0000818
hassoa94434b2003-05-25 17:10:12 +0000819 ripng_connect_set (ifp, 0);
paul718e3742002-12-13 20:15:29 +0000820 }
821 }
822}
823
824/* Set distribute list to all interfaces. */
hassoa94434b2003-05-25 17:10:12 +0000825void
paul718e3742002-12-13 20:15:29 +0000826ripng_enable_apply_all ()
827{
828 struct interface *ifp;
hasso52dc7ee2004-09-23 19:18:23 +0000829 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000830
831 for (node = listhead (iflist); node; nextnode (node))
832 {
833 ifp = getdata (node);
834 ripng_enable_apply (ifp);
835 }
836}
837
hassoa94434b2003-05-25 17:10:12 +0000838/* Clear all network and neighbor configuration */
839void
840ripng_clean_network ()
841{
hasso7a1d5832004-10-08 06:32:23 +0000842 unsigned int i;
hassoa94434b2003-05-25 17:10:12 +0000843 char *str;
844 struct route_node *rn;
845
846 /* ripng_enable_network */
847 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
848 if (rn->info) {
849 rn->info = NULL;
850 route_unlock_node(rn);
851 }
852
853 /* ripng_enable_if */
854 for (i = 0; i < vector_max (ripng_enable_if); i++)
855 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
856 free (str);
857 vector_slot (ripng_enable_if, i) = NULL;
858 }
859}
860
paul718e3742002-12-13 20:15:29 +0000861/* Vector to store passive-interface name. */
862vector Vripng_passive_interface;
863
864/* Utility function for looking up passive interface settings. */
865int
hasso98b718a2004-10-11 12:57:57 +0000866ripng_passive_interface_lookup (const char *ifname)
paul718e3742002-12-13 20:15:29 +0000867{
hasso7a1d5832004-10-08 06:32:23 +0000868 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000869 char *str;
870
871 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
872 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
873 if (strcmp (str, ifname) == 0)
874 return i;
875 return -1;
876}
877
878void
879ripng_passive_interface_apply (struct interface *ifp)
880{
881 int ret;
882 struct ripng_interface *ri;
883
884 ri = ifp->info;
885
886 ret = ripng_passive_interface_lookup (ifp->name);
887 if (ret < 0)
888 ri->passive = 0;
889 else
890 ri->passive = 1;
891}
892
893void
894ripng_passive_interface_apply_all (void)
895{
896 struct interface *ifp;
hasso52dc7ee2004-09-23 19:18:23 +0000897 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000898
899 for (node = listhead (iflist); node; nextnode (node))
900 {
901 ifp = getdata (node);
902 ripng_passive_interface_apply (ifp);
903 }
904}
905
906/* Passive interface. */
907int
hasso98b718a2004-10-11 12:57:57 +0000908ripng_passive_interface_set (struct vty *vty, const char *ifname)
paul718e3742002-12-13 20:15:29 +0000909{
910 if (ripng_passive_interface_lookup (ifname) >= 0)
911 return CMD_WARNING;
912
913 vector_set (Vripng_passive_interface, strdup (ifname));
914
915 ripng_passive_interface_apply_all ();
916
917 return CMD_SUCCESS;
918}
919
920int
hasso98b718a2004-10-11 12:57:57 +0000921ripng_passive_interface_unset (struct vty *vty, const char *ifname)
paul718e3742002-12-13 20:15:29 +0000922{
923 int i;
924 char *str;
925
926 i = ripng_passive_interface_lookup (ifname);
927 if (i < 0)
928 return CMD_WARNING;
929
930 str = vector_slot (Vripng_passive_interface, i);
931 free (str);
932 vector_unset (Vripng_passive_interface, i);
933
934 ripng_passive_interface_apply_all ();
935
936 return CMD_SUCCESS;
937}
938
939/* Free all configured RIP passive-interface settings. */
940void
941ripng_passive_interface_clean (void)
942{
hasso7a1d5832004-10-08 06:32:23 +0000943 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000944 char *str;
945
946 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
947 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
948 {
949 free (str);
950 vector_slot (Vripng_passive_interface, i) = NULL;
951 }
952 ripng_passive_interface_apply_all ();
953}
954
955/* Write RIPng enable network and interface to the vty. */
956int
hassoa94434b2003-05-25 17:10:12 +0000957ripng_network_write (struct vty *vty, int config_mode)
paul718e3742002-12-13 20:15:29 +0000958{
hasso7a1d5832004-10-08 06:32:23 +0000959 unsigned int i;
hasso98b718a2004-10-11 12:57:57 +0000960 const char *ifname;
paul718e3742002-12-13 20:15:29 +0000961 struct route_node *node;
962 char buf[BUFSIZ];
963
964 /* Write enable network. */
965 for (node = route_top (ripng_enable_network); node; node = route_next (node))
966 if (node->info)
967 {
968 struct prefix *p = &node->p;
hassoa94434b2003-05-25 17:10:12 +0000969 vty_out (vty, "%s%s/%d%s",
970 config_mode ? " network " : " ",
paul718e3742002-12-13 20:15:29 +0000971 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
972 p->prefixlen,
973 VTY_NEWLINE);
974
975 }
976
977 /* Write enable interface. */
978 for (i = 0; i < vector_max (ripng_enable_if); i++)
hassoa94434b2003-05-25 17:10:12 +0000979 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
980 vty_out (vty, "%s%s%s",
981 config_mode ? " network " : " ",
982 ifname,
paul718e3742002-12-13 20:15:29 +0000983 VTY_NEWLINE);
984
985 /* Write passive interface. */
hassoa94434b2003-05-25 17:10:12 +0000986 if (config_mode)
987 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
988 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
989 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000990
991 return 0;
992}
993
994/* RIPng enable on specified interface or matched network. */
995DEFUN (ripng_network,
996 ripng_network_cmd,
997 "network IF_OR_ADDR",
998 "RIPng enable on specified interface or network.\n"
999 "Interface or address")
1000{
1001 int ret;
1002 struct prefix p;
1003
1004 ret = str2prefix (argv[0], &p);
1005
1006 /* Given string is IPv6 network or interface name. */
1007 if (ret)
1008 ret = ripng_enable_network_add (&p);
1009 else
1010 ret = ripng_enable_if_add (argv[0]);
1011
1012 if (ret < 0)
1013 {
1014 vty_out (vty, "There is same network configuration %s%s", argv[0],
1015 VTY_NEWLINE);
1016 return CMD_WARNING;
1017 }
1018
paul718e3742002-12-13 20:15:29 +00001019 return CMD_SUCCESS;
1020}
1021
1022/* RIPng enable on specified interface or matched network. */
1023DEFUN (no_ripng_network,
1024 no_ripng_network_cmd,
1025 "no network IF_OR_ADDR",
1026 NO_STR
1027 "RIPng enable on specified interface or network.\n"
1028 "Interface or address")
1029{
1030 int ret;
1031 struct prefix p;
1032
1033 ret = str2prefix (argv[0], &p);
1034
1035 /* Given string is interface name. */
1036 if (ret)
1037 ret = ripng_enable_network_delete (&p);
1038 else
1039 ret = ripng_enable_if_delete (argv[0]);
1040
1041 if (ret < 0)
1042 {
1043 vty_out (vty, "can't find network %s%s", argv[0],
1044 VTY_NEWLINE);
1045 return CMD_WARNING;
1046 }
1047
paul718e3742002-12-13 20:15:29 +00001048 return CMD_SUCCESS;
1049}
1050
hassoa94434b2003-05-25 17:10:12 +00001051DEFUN (ipv6_ripng_split_horizon,
1052 ipv6_ripng_split_horizon_cmd,
1053 "ipv6 ripng split-horizon",
1054 IPV6_STR
1055 "Routing Information Protocol\n"
1056 "Perform split horizon\n")
1057{
1058 struct interface *ifp;
1059 struct ripng_interface *ri;
1060
1061 ifp = vty->index;
1062 ri = ifp->info;
1063
1064 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1065 return CMD_SUCCESS;
1066}
1067
1068DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1069 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1070 "ipv6 ripng split-horizon poisoned-reverse",
1071 IPV6_STR
1072 "Routing Information Protocol\n"
1073 "Perform split horizon\n"
1074 "With poisoned-reverse\n")
1075{
1076 struct interface *ifp;
1077 struct ripng_interface *ri;
1078
1079 ifp = vty->index;
1080 ri = ifp->info;
1081
1082 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1083 return CMD_SUCCESS;
1084}
1085
1086DEFUN (no_ipv6_ripng_split_horizon,
1087 no_ipv6_ripng_split_horizon_cmd,
1088 "no ipv6 ripng split-horizon",
1089 NO_STR
1090 IPV6_STR
1091 "Routing Information Protocol\n"
1092 "Perform split horizon\n")
1093{
1094 struct interface *ifp;
1095 struct ripng_interface *ri;
1096
1097 ifp = vty->index;
1098 ri = ifp->info;
1099
1100 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1101 return CMD_SUCCESS;
1102}
1103
1104ALIAS (no_ipv6_ripng_split_horizon,
1105 no_ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1106 "no ipv6 ripng split-horizon poisoned-reverse",
1107 NO_STR
1108 IPV6_STR
1109 "Routing Information Protocol\n"
1110 "Perform split horizon\n"
1111 "With poisoned-reverse\n")
1112
paul718e3742002-12-13 20:15:29 +00001113DEFUN (ripng_passive_interface,
1114 ripng_passive_interface_cmd,
1115 "passive-interface IFNAME",
1116 "Suppress routing updates on an interface\n"
1117 "Interface name\n")
1118{
1119 return ripng_passive_interface_set (vty, argv[0]);
1120}
1121
1122DEFUN (no_ripng_passive_interface,
1123 no_ripng_passive_interface_cmd,
1124 "no passive-interface IFNAME",
1125 NO_STR
1126 "Suppress routing updates on an interface\n"
1127 "Interface name\n")
1128{
1129 return ripng_passive_interface_unset (vty, argv[0]);
1130}
1131
1132struct ripng_interface *
1133ri_new ()
1134{
1135 struct ripng_interface *ri;
1136 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
hassoa94434b2003-05-25 17:10:12 +00001137
1138 /* Set default split-horizon behavior. If the interface is Frame
1139 Relay or SMDS is enabled, the default value for split-horizon is
1140 off. But currently Zebra does detect Frame Relay or SMDS
1141 interface. So all interface is set to split horizon. */
1142 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1143 ri->split_horizon = ri->split_horizon_default;
1144
paul718e3742002-12-13 20:15:29 +00001145 return ri;
1146}
1147
1148int
1149ripng_if_new_hook (struct interface *ifp)
1150{
1151 ifp->info = ri_new ();
1152 return 0;
1153}
1154
hassoa94434b2003-05-25 17:10:12 +00001155/* Called when interface structure deleted. */
1156int
1157ripng_if_delete_hook (struct interface *ifp)
1158{
1159 XFREE (MTYPE_IF, ifp->info);
1160 ifp->info = NULL;
1161 return 0;
1162}
1163
paul718e3742002-12-13 20:15:29 +00001164/* Configuration write function for ripngd. */
1165int
1166interface_config_write (struct vty *vty)
1167{
hasso52dc7ee2004-09-23 19:18:23 +00001168 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001169 struct interface *ifp;
1170 struct ripng_interface *ri;
1171 int write = 0;
1172
1173 for (node = listhead (iflist); node; nextnode (node))
1174 {
1175 ifp = getdata (node);
1176 ri = ifp->info;
1177
hassoa94434b2003-05-25 17:10:12 +00001178 /* Do not display the interface if there is no
1179 * configuration about it.
1180 **/
1181 if ((!ifp->desc) &&
1182 (ri->split_horizon == ri->split_horizon_default))
1183 continue;
1184
paul718e3742002-12-13 20:15:29 +00001185 vty_out (vty, "interface %s%s", ifp->name,
1186 VTY_NEWLINE);
1187 if (ifp->desc)
1188 vty_out (vty, " description %s%s", ifp->desc,
1189 VTY_NEWLINE);
1190
hassoa94434b2003-05-25 17:10:12 +00001191 /* Split horizon. */
1192 if (ri->split_horizon != ri->split_horizon_default)
1193 {
1194 switch (ri->split_horizon) {
1195 case RIPNG_SPLIT_HORIZON:
1196 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1197 break;
1198 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1199 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1200 VTY_NEWLINE);
1201 break;
1202 case RIPNG_NO_SPLIT_HORIZON:
1203 default:
1204 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1205 break;
1206 }
1207 }
1208
paul718e3742002-12-13 20:15:29 +00001209 vty_out (vty, "!%s", VTY_NEWLINE);
1210
1211 write++;
1212 }
1213 return write;
1214}
1215
1216/* ripngd's interface node. */
1217struct cmd_node interface_node =
1218{
1219 INTERFACE_NODE,
1220 "%s(config-if)# ",
hassoa94434b2003-05-25 17:10:12 +00001221 1 /* VTYSH */
paul718e3742002-12-13 20:15:29 +00001222};
1223
1224/* Initialization of interface. */
1225void
1226ripng_if_init ()
1227{
1228 /* Interface initialize. */
1229 iflist = list_new ();
1230 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
hassoa94434b2003-05-25 17:10:12 +00001231 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
paul718e3742002-12-13 20:15:29 +00001232
1233 /* RIPng enable network init. */
1234 ripng_enable_network = route_table_init ();
1235
1236 /* RIPng enable interface init. */
1237 ripng_enable_if = vector_init (1);
1238
1239 /* RIPng passive interface. */
1240 Vripng_passive_interface = vector_init (1);
1241
1242 /* Install interface node. */
1243 install_node (&interface_node, interface_config_write);
hassoa94434b2003-05-25 17:10:12 +00001244
1245 /* Install commands. */
paul718e3742002-12-13 20:15:29 +00001246 install_element (CONFIG_NODE, &interface_cmd);
paul32d24632003-05-23 09:25:20 +00001247 install_element (CONFIG_NODE, &no_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001248 install_default (INTERFACE_NODE);
paul718e3742002-12-13 20:15:29 +00001249 install_element (INTERFACE_NODE, &interface_desc_cmd);
1250 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1251
1252 install_element (RIPNG_NODE, &ripng_network_cmd);
1253 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1254 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1255 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001256
1257 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1258 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1259 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1260 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_poisoned_reverse_cmd);
paul718e3742002-12-13 20:15:29 +00001261}