blob: 7437f70fa5d6e0af6c080c655db8a9f04bf21240 [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"
37
38#include "ripngd/ripngd.h"
39#include "ripngd/ripng_debug.h"
40
41/* If RFC2133 definition is used. */
42#ifndef IPV6_JOIN_GROUP
43#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
44#endif
45#ifndef IPV6_LEAVE_GROUP
46#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
47#endif
48
49/* Static utility function. */
50static void ripng_enable_apply (struct interface *);
51static void ripng_passive_interface_apply (struct interface *);
hassoa94434b2003-05-25 17:10:12 +000052int ripng_enable_if_lookup (char *ifname);
53int ripng_enable_network_lookup2 (struct connected *connected);
54void ripng_enable_apply_all ();
paul718e3742002-12-13 20:15:29 +000055
56/* Join to the all rip routers multicast group. */
57int
58ripng_multicast_join (struct interface *ifp)
59{
60 int ret;
61 struct ipv6_mreq mreq;
62
hassoa94434b2003-05-25 17:10:12 +000063 if (if_is_up (ifp) && if_is_multicast (ifp)) {
64 memset (&mreq, 0, sizeof (mreq));
65 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
66 mreq.ipv6mr_interface = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +000067
hassoa94434b2003-05-25 17:10:12 +000068 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
69 (char *) &mreq, sizeof (mreq));
gdtddf1c262004-01-04 01:02:55 +000070
71 if (ret < 0 && errno == EADDRINUSE)
72 {
73 /*
74 * Group is already joined. This occurs due to sloppy group
75 * management, in particular declining to leave the group on
76 * an interface that has just gone down.
77 */
78 zlog_warn ("ripng join on %s EADDRINUSE (ignoring)\n", ifp->name);
79 return 0; /* not an error */
80 }
81
hassoa94434b2003-05-25 17:10:12 +000082 if (ret < 0)
83 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +000084
hassoa94434b2003-05-25 17:10:12 +000085 if (IS_RIPNG_DEBUG_EVENT)
86 zlog_info ("RIPng %s join to all-rip-routers multicast group", ifp->name);
paul718e3742002-12-13 20:15:29 +000087
hassoa94434b2003-05-25 17:10:12 +000088 if (ret < 0)
89 return -1;
90 }
91 return 0;
paul718e3742002-12-13 20:15:29 +000092}
93
94/* Leave from the all rip routers multicast group. */
95int
96ripng_multicast_leave (struct interface *ifp)
97{
98 int ret;
99 struct ipv6_mreq mreq;
100
hassoa94434b2003-05-25 17:10:12 +0000101 if (if_is_up (ifp) && if_is_multicast (ifp)) {
102 memset (&mreq, 0, sizeof (mreq));
103 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
104 mreq.ipv6mr_interface = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +0000105
hassoa94434b2003-05-25 17:10:12 +0000106 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
107 (char *) &mreq, sizeof (mreq));
108 if (ret < 0)
109 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000110
hassoa94434b2003-05-25 17:10:12 +0000111 if (IS_RIPNG_DEBUG_EVENT)
112 zlog_info ("RIPng %s leave from all-rip-routers multicast group",
113 ifp->name);
paul718e3742002-12-13 20:15:29 +0000114
hassoa94434b2003-05-25 17:10:12 +0000115 if (ret < 0)
116 return -1;
117 }
118
119 return 0;
120}
121
122/* How many link local IPv6 address could be used on the interface ? */
123int
124ripng_if_ipv6_lladdress_check (struct interface *ifp)
125{
126 struct listnode *nn;
127 struct connected *connected;
128 int count = 0;
129
130 for (nn = listhead (ifp->connected); nn; nextnode (nn))
131 if ((connected = getdata (nn)) != NULL) {
132 struct prefix *p;
133 p = connected->address;
134
135 if ((p->family == AF_INET6) &&
136 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
137 count++;
138 }
139
140 return count;
paul718e3742002-12-13 20:15:29 +0000141}
142
143/* Check max mtu size. */
144int
145ripng_check_max_mtu ()
146{
147 listnode node;
148 struct interface *ifp;
149 int mtu;
150
151 mtu = 0;
152 for (node = listhead (iflist); node; nextnode (node))
153 {
154 ifp = getdata (node);
155 if (mtu < ifp->mtu)
156 mtu = ifp->mtu;
157 }
158 return mtu;
159}
160
161int
162ripng_if_down (struct interface *ifp)
163{
164 struct route_node *rp;
165 struct ripng_info *rinfo;
166 struct ripng_interface *ri;
167
hassoa94434b2003-05-25 17:10:12 +0000168 if (ripng)
paul718e3742002-12-13 20:15:29 +0000169 {
170 for (rp = route_top (ripng->table); rp; rp = route_next (rp))
171 if ((rinfo = rp->info) != NULL)
172 {
173 /* Routes got through this interface. */
174 if (rinfo->ifindex == ifp->ifindex
175 && rinfo->type == ZEBRA_ROUTE_RIPNG
176 && rinfo->sub_type == RIPNG_ROUTE_RTE)
177 {
178 ripng_zebra_ipv6_delete ((struct prefix_ipv6 *) &rp->p,
179 &rinfo->nexthop,
180 rinfo->ifindex);
181
hassoa94434b2003-05-25 17:10:12 +0000182 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
183 (struct prefix_ipv6 *)&rp->p,
184 rinfo->ifindex);
paul718e3742002-12-13 20:15:29 +0000185 }
186 else
187 {
hassoa94434b2003-05-25 17:10:12 +0000188 /* All redistributed routes got through this interface,
189 * but the static and system ones are kept. */
190 if ((rinfo->ifindex == ifp->ifindex) &&
191 (rinfo->type != ZEBRA_ROUTE_STATIC) &&
192 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
paul718e3742002-12-13 20:15:29 +0000193 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
194 (struct prefix_ipv6 *) &rp->p,
195 rinfo->ifindex);
196 }
197 }
198 }
199
200 ri = ifp->info;
201
hassoa94434b2003-05-25 17:10:12 +0000202 if (ri->running)
paul718e3742002-12-13 20:15:29 +0000203 {
204 if (IS_RIPNG_DEBUG_EVENT)
205 zlog_info ("turn off %s", ifp->name);
206
207 /* Leave from multicast group. */
208 ripng_multicast_leave (ifp);
209
210 ri->running = 0;
211 }
212
213 return 0;
214}
215
216/* Inteface link up message processing. */
217int
218ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length)
219{
220 struct stream *s;
221 struct interface *ifp;
222
223 /* zebra_interface_state_read() updates interface structure in iflist. */
224 s = zclient->ibuf;
225 ifp = zebra_interface_state_read (s);
226
227 if (ifp == NULL)
228 return 0;
229
230 if (IS_RIPNG_DEBUG_ZEBRA)
231 zlog_info ("interface up %s index %d flags %ld metric %d mtu %d",
232 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
233
234 /* Check if this interface is RIPng enabled or not. */
235 ripng_enable_apply (ifp);
236
237 /* Check for a passive interface. */
238 ripng_passive_interface_apply (ifp);
239
240 /* Apply distribute list to the all interface. */
241 ripng_distribute_update_interface (ifp);
242
243 return 0;
244}
245
246/* Inteface link down message processing. */
247int
248ripng_interface_down (int command, struct zclient *zclient,
249 zebra_size_t length)
250{
251 struct stream *s;
252 struct interface *ifp;
253
254 /* zebra_interface_state_read() updates interface structure in iflist. */
255 s = zclient->ibuf;
256 ifp = zebra_interface_state_read (s);
257
258 if (ifp == NULL)
259 return 0;
260
261 ripng_if_down (ifp);
262
263 if (IS_RIPNG_DEBUG_ZEBRA)
264 zlog_info ("interface down %s index %d flags %ld metric %d mtu %d",
265 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
266
267 return 0;
268}
269
270/* Inteface addition message from zebra. */
271int
272ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length)
273{
274 struct interface *ifp;
275
276 ifp = zebra_interface_add_read (zclient->ibuf);
277
278 if (IS_RIPNG_DEBUG_ZEBRA)
279 zlog_info ("RIPng interface add %s index %d flags %ld metric %d mtu %d",
280 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
281
282 /* Check is this interface is RIP enabled or not.*/
283 ripng_enable_apply (ifp);
284
285 /* Apply distribute list to the interface. */
286 ripng_distribute_update_interface (ifp);
287
288 /* Check interface routemap. */
289 ripng_if_rmap_update_interface (ifp);
290
291 return 0;
292}
293
294int
295ripng_interface_delete (int command, struct zclient *zclient,
296 zebra_size_t length)
297{
hassoa94434b2003-05-25 17:10:12 +0000298 struct interface *ifp;
299 struct stream *s;
300
301 s = zclient->ibuf;
302 /* zebra_interface_state_read() updates interface structure in iflist */
303 ifp = zebra_interface_state_read(s);
304
305 if (ifp == NULL)
306 return 0;
307
308 if (if_is_up (ifp)) {
309 ripng_if_down(ifp);
310 }
311
312 zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
313 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
314
315 /* To support pseudo interface do not free interface structure. */
316 /* if_delete(ifp); */
317
paul718e3742002-12-13 20:15:29 +0000318 return 0;
319}
320
hassoa94434b2003-05-25 17:10:12 +0000321void
322ripng_interface_clean ()
323{
324 listnode node;
325 struct interface *ifp;
326 struct ripng_interface *ri;
327
328 for (node = listhead (iflist); node; nextnode (node))
329 {
330 ifp = getdata (node);
331 ri = ifp->info;
332
333 ri->enable_network = 0;
334 ri->enable_interface = 0;
335 ri->running = 0;
336
337 if (ri->t_wakeup)
338 {
339 thread_cancel (ri->t_wakeup);
340 ri->t_wakeup = NULL;
341 }
342 }
343}
344
345void
346ripng_interface_reset () {
347 listnode node;
348 struct interface *ifp;
349 struct ripng_interface *ri;
350
351 for (node = listhead (iflist); node; nextnode (node))
352 {
353 ifp = getdata (node);
354 ri = ifp->info;
355
356 ri->enable_network = 0;
357 ri->enable_interface = 0;
358 ri->running = 0;
359
360 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
361 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
362
363 ri->list[RIPNG_FILTER_IN] = NULL;
364 ri->list[RIPNG_FILTER_OUT] = NULL;
365
366 ri->prefix[RIPNG_FILTER_IN] = NULL;
367 ri->prefix[RIPNG_FILTER_OUT] = NULL;
368
369 if (ri->t_wakeup)
370 {
371 thread_cancel (ri->t_wakeup);
372 ri->t_wakeup = NULL;
373 }
374
375 ri->passive = 0;
376 }
377}
378
379static void
380ripng_apply_address_add (struct connected *ifc) {
381 struct prefix_ipv6 address;
382 struct prefix *p;
383
384 if (!ripng)
385 return;
386
387 if (! if_is_up(ifc->ifp))
388 return;
389
390 p = ifc->address;
391
392 memset (&address, 0, sizeof (address));
393 address.family = p->family;
394 address.prefix = p->u.prefix6;
395 address.prefixlen = p->prefixlen;
396 apply_mask_ipv6(&address);
397
398 /* Check if this interface is RIP enabled or not
399 or Check if this address's prefix is RIP enabled */
400 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
401 (ripng_enable_network_lookup2(ifc) >= 0))
402 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
403 &address, ifc->ifp->ifindex, NULL);
404
405}
406
paul718e3742002-12-13 20:15:29 +0000407int
408ripng_interface_address_add (int command, struct zclient *zclient,
409 zebra_size_t length)
410{
411 struct connected *c;
412 struct prefix *p;
paul718e3742002-12-13 20:15:29 +0000413
414 c = zebra_interface_address_add_read (zclient->ibuf);
415
416 if (c == NULL)
417 return 0;
418
419 p = c->address;
420
421 if (p->family == AF_INET6)
422 {
423 if (IS_RIPNG_DEBUG_ZEBRA)
424 zlog_info ("RIPng connected address %s/%d add",
hassoa94434b2003-05-25 17:10:12 +0000425 inet6_ntop(&p->u.prefix6),
paul718e3742002-12-13 20:15:29 +0000426 p->prefixlen);
427
hassoa94434b2003-05-25 17:10:12 +0000428 /* Check is this prefix needs to be redistributed. */
429 ripng_apply_address_add(c);
430
431 /* Let's try once again whether the interface could be activated */
432 if (c->ifp) {
433 struct ripng_interface *ri = c->ifp->info;
434
435 if (!ri->running) {
436 /* Check if this interface is RIP enabled or not.*/
437 ripng_enable_apply (c->ifp);
438
439 /* Apply distribute list to the interface. */
440 ripng_distribute_update_interface (c->ifp);
441
442 /* Check interface routemap. */
443 ripng_if_rmap_update_interface (c->ifp);
444 }
445 }
446
paul718e3742002-12-13 20:15:29 +0000447 }
448
449 return 0;
450}
451
hassoa94434b2003-05-25 17:10:12 +0000452static void
453ripng_apply_address_del (struct connected *ifc) {
454 struct prefix_ipv6 address;
455 struct prefix *p;
456
457 if (!ripng)
458 return;
459
460 if (! if_is_up(ifc->ifp))
461 return;
462
463 p = ifc->address;
464
465 memset (&address, 0, sizeof (address));
466 address.family = p->family;
467 address.prefix = p->u.prefix6;
468 address.prefixlen = p->prefixlen;
469 apply_mask_ipv6(&address);
470
471 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
472 &address, ifc->ifp->ifindex);
473}
474
paul718e3742002-12-13 20:15:29 +0000475int
476ripng_interface_address_delete (int command, struct zclient *zclient,
477 zebra_size_t length)
478{
479 struct connected *ifc;
480 struct prefix *p;
481 char buf[INET6_ADDRSTRLEN];
482
483 ifc = zebra_interface_address_delete_read (zclient->ibuf);
484
485 if (ifc)
486 {
487 p = ifc->address;
488
489 if (p->family == AF_INET6)
490 {
491 if (IS_RIPNG_DEBUG_ZEBRA)
492 zlog_info ("RIPng connected address %s/%d delete",
493 inet_ntop (AF_INET6, &p->u.prefix6, buf,
494 INET6_ADDRSTRLEN),
495 p->prefixlen);
496
hassoa94434b2003-05-25 17:10:12 +0000497 /* Check wether this prefix needs to be removed. */
498 ripng_apply_address_del(ifc);
paul718e3742002-12-13 20:15:29 +0000499 }
500 connected_free (ifc);
501 }
502
503 return 0;
504}
505
506/* RIPng enable interface vector. */
507vector ripng_enable_if;
508
509/* RIPng enable network table. */
510struct route_table *ripng_enable_network;
511
512/* Lookup RIPng enable network. */
hassoa94434b2003-05-25 17:10:12 +0000513/* Check wether the interface has at least a connected prefix that
514 * is within the ripng_enable_network table. */
paul718e3742002-12-13 20:15:29 +0000515int
hassoa94434b2003-05-25 17:10:12 +0000516ripng_enable_network_lookup_if (struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000517{
518 listnode listnode;
519 struct connected *connected;
hassoa94434b2003-05-25 17:10:12 +0000520 struct prefix_ipv6 address;
paul718e3742002-12-13 20:15:29 +0000521
522 for (listnode = listhead (ifp->connected); listnode; nextnode (listnode))
523 if ((connected = getdata (listnode)) != NULL)
524 {
525 struct prefix *p;
526 struct route_node *node;
527
528 p = connected->address;
529
530 if (p->family == AF_INET6)
531 {
hassoa94434b2003-05-25 17:10:12 +0000532 address.family = AF_INET6;
533 address.prefix = p->u.prefix6;
534 address.prefixlen = IPV6_MAX_BITLEN;
535
536 node = route_node_match (ripng_enable_network,
537 (struct prefix *)&address);
paul718e3742002-12-13 20:15:29 +0000538 if (node)
539 {
540 route_unlock_node (node);
541 return 1;
542 }
543 }
544 }
545 return -1;
546}
547
hassoa94434b2003-05-25 17:10:12 +0000548/* Check wether connected is within the ripng_enable_network table. */
549int
550ripng_enable_network_lookup2 (struct connected *connected)
551{
552 struct prefix_ipv6 address;
553 struct prefix *p;
554
555 p = connected->address;
556
557 if (p->family == AF_INET6) {
558 struct route_node *node;
559
560 address.family = p->family;
561 address.prefix = p->u.prefix6;
562 address.prefixlen = IPV6_MAX_BITLEN;
563
564 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
565 node = route_node_match (ripng_enable_network,
566 (struct prefix *)&address);
567
568 if (node) {
569 route_unlock_node (node);
570 return 1;
571 }
572 }
573
574 return -1;
575}
576
paul718e3742002-12-13 20:15:29 +0000577/* Add RIPng enable network. */
578int
579ripng_enable_network_add (struct prefix *p)
580{
581 struct route_node *node;
582
583 node = route_node_get (ripng_enable_network, p);
584
585 if (node->info)
586 {
587 route_unlock_node (node);
588 return -1;
589 }
590 else
591 node->info = "enabled";
592
hassoa94434b2003-05-25 17:10:12 +0000593 /* XXX: One should find a better solution than a generic one */
594 ripng_enable_apply_all();
595
paul718e3742002-12-13 20:15:29 +0000596 return 1;
597}
598
599/* Delete RIPng enable network. */
600int
601ripng_enable_network_delete (struct prefix *p)
602{
603 struct route_node *node;
604
605 node = route_node_lookup (ripng_enable_network, p);
606 if (node)
607 {
608 node->info = NULL;
609
610 /* Unlock info lock. */
611 route_unlock_node (node);
612
613 /* Unlock lookup lock. */
614 route_unlock_node (node);
615
616 return 1;
617 }
618 return -1;
619}
620
621/* Lookup function. */
622int
623ripng_enable_if_lookup (char *ifname)
624{
625 int i;
626 char *str;
627
628 for (i = 0; i < vector_max (ripng_enable_if); i++)
629 if ((str = vector_slot (ripng_enable_if, i)) != NULL)
630 if (strcmp (str, ifname) == 0)
631 return i;
632 return -1;
633}
634
635/* Add interface to ripng_enable_if. */
636int
637ripng_enable_if_add (char *ifname)
638{
639 int ret;
640
641 ret = ripng_enable_if_lookup (ifname);
642 if (ret >= 0)
643 return -1;
644
645 vector_set (ripng_enable_if, strdup (ifname));
646
hassoa94434b2003-05-25 17:10:12 +0000647 ripng_enable_apply_all();
648
paul718e3742002-12-13 20:15:29 +0000649 return 1;
650}
651
652/* Delete interface from ripng_enable_if. */
653int
654ripng_enable_if_delete (char *ifname)
655{
656 int index;
657 char *str;
658
659 index = ripng_enable_if_lookup (ifname);
660 if (index < 0)
661 return -1;
662
663 str = vector_slot (ripng_enable_if, index);
664 free (str);
665 vector_unset (ripng_enable_if, index);
666
hassoa94434b2003-05-25 17:10:12 +0000667 ripng_enable_apply_all();
668
paul718e3742002-12-13 20:15:29 +0000669 return 1;
670}
671
672/* Wake up interface. */
673int
674ripng_interface_wakeup (struct thread *t)
675{
676 struct interface *ifp;
677 struct ripng_interface *ri;
678
679 /* Get interface. */
680 ifp = THREAD_ARG (t);
681
682 ri = ifp->info;
683 ri->t_wakeup = NULL;
684
685 /* Join to multicast group. */
hassoa94434b2003-05-25 17:10:12 +0000686 if (ripng_multicast_join (ifp) < 0) {
687 zlog_err ("multicast join failed, interface %s not running", ifp->name);
688 return 0;
689 }
690
691 /* Set running flag. */
692 ri->running = 1;
paul718e3742002-12-13 20:15:29 +0000693
694 /* Send RIP request to the interface. */
695 ripng_request (ifp);
696
697 return 0;
698}
699
hassoa94434b2003-05-25 17:10:12 +0000700int ripng_redistribute_check (int);
701
702void
703ripng_connect_set (struct interface *ifp, int set)
704{
705 struct listnode *nn;
706 struct connected *connected;
707 struct prefix_ipv6 address;
708
709 for (nn = listhead (ifp->connected); nn; nextnode (nn))
710 if ((connected = getdata (nn)) != NULL) {
711 struct prefix *p;
712 p = connected->address;
713
714 if (p->family != AF_INET6)
715 continue;
716
717 address.family = AF_INET6;
718 address.prefix = p->u.prefix6;
719 address.prefixlen = p->prefixlen;
720 apply_mask_ipv6 (&address);
721
722 if (set) {
723 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
724 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
725 (ripng_enable_network_lookup2(connected) >= 0))
726 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
727 &address, connected->ifp->ifindex, NULL);
728 } else {
729 ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
730 &address, connected->ifp->ifindex);
731 if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
732 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
733 &address, connected->ifp->ifindex, NULL);
734 }
735 }
736}
737
paul718e3742002-12-13 20:15:29 +0000738/* Check RIPng is enabed on this interface. */
739void
740ripng_enable_apply (struct interface *ifp)
741{
742 int ret;
743 struct ripng_interface *ri = NULL;
744
745 /* Check interface. */
paul718e3742002-12-13 20:15:29 +0000746 if (! if_is_up (ifp))
747 return;
748
749 ri = ifp->info;
750
hassoa94434b2003-05-25 17:10:12 +0000751 /* Is this interface a candidate for RIPng ? */
752 ret = ripng_enable_network_lookup_if (ifp);
paul718e3742002-12-13 20:15:29 +0000753
754 /* If the interface is matched. */
755 if (ret > 0)
756 ri->enable_network = 1;
757 else
758 ri->enable_network = 0;
759
760 /* Check interface name configuration. */
761 ret = ripng_enable_if_lookup (ifp->name);
762 if (ret >= 0)
763 ri->enable_interface = 1;
764 else
765 ri->enable_interface = 0;
766
hassoa94434b2003-05-25 17:10:12 +0000767 /* any candidate interface MUST have a link-local IPv6 address */
768 if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
769 (ri->enable_network || ri->enable_interface)) {
770 ri->enable_network = 0;
771 ri->enable_interface = 0;
772 zlog_warn("Interface %s does not have any link-local address",
773 ifp->name);
774 }
775
paul718e3742002-12-13 20:15:29 +0000776 /* Update running status of the interface. */
777 if (ri->enable_network || ri->enable_interface)
778 {
paul718e3742002-12-13 20:15:29 +0000779 {
780 if (IS_RIPNG_DEBUG_EVENT)
781 zlog_info ("RIPng turn on %s", ifp->name);
782
783 /* Add interface wake up thread. */
784 if (! ri->t_wakeup)
785 ri->t_wakeup = thread_add_timer (master, ripng_interface_wakeup,
786 ifp, 1);
paul718e3742002-12-13 20:15:29 +0000787
hassoa94434b2003-05-25 17:10:12 +0000788 ripng_connect_set (ifp, 1);
paul718e3742002-12-13 20:15:29 +0000789 }
790 }
791 else
792 {
793 if (ri->running)
794 {
hassoa94434b2003-05-25 17:10:12 +0000795 /* Might as well clean up the route table as well
796 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
797 **/
798 ripng_if_down(ifp);
paul718e3742002-12-13 20:15:29 +0000799
hassoa94434b2003-05-25 17:10:12 +0000800 ripng_connect_set (ifp, 0);
paul718e3742002-12-13 20:15:29 +0000801 }
802 }
803}
804
805/* Set distribute list to all interfaces. */
hassoa94434b2003-05-25 17:10:12 +0000806void
paul718e3742002-12-13 20:15:29 +0000807ripng_enable_apply_all ()
808{
809 struct interface *ifp;
810 listnode node;
811
812 for (node = listhead (iflist); node; nextnode (node))
813 {
814 ifp = getdata (node);
815 ripng_enable_apply (ifp);
816 }
817}
818
hassoa94434b2003-05-25 17:10:12 +0000819/* Clear all network and neighbor configuration */
820void
821ripng_clean_network ()
822{
823 int i;
824 char *str;
825 struct route_node *rn;
826
827 /* ripng_enable_network */
828 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
829 if (rn->info) {
830 rn->info = NULL;
831 route_unlock_node(rn);
832 }
833
834 /* ripng_enable_if */
835 for (i = 0; i < vector_max (ripng_enable_if); i++)
836 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
837 free (str);
838 vector_slot (ripng_enable_if, i) = NULL;
839 }
840}
841
paul718e3742002-12-13 20:15:29 +0000842/* Vector to store passive-interface name. */
843vector Vripng_passive_interface;
844
845/* Utility function for looking up passive interface settings. */
846int
847ripng_passive_interface_lookup (char *ifname)
848{
849 int i;
850 char *str;
851
852 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
853 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
854 if (strcmp (str, ifname) == 0)
855 return i;
856 return -1;
857}
858
859void
860ripng_passive_interface_apply (struct interface *ifp)
861{
862 int ret;
863 struct ripng_interface *ri;
864
865 ri = ifp->info;
866
867 ret = ripng_passive_interface_lookup (ifp->name);
868 if (ret < 0)
869 ri->passive = 0;
870 else
871 ri->passive = 1;
872}
873
874void
875ripng_passive_interface_apply_all (void)
876{
877 struct interface *ifp;
878 listnode node;
879
880 for (node = listhead (iflist); node; nextnode (node))
881 {
882 ifp = getdata (node);
883 ripng_passive_interface_apply (ifp);
884 }
885}
886
887/* Passive interface. */
888int
889ripng_passive_interface_set (struct vty *vty, char *ifname)
890{
891 if (ripng_passive_interface_lookup (ifname) >= 0)
892 return CMD_WARNING;
893
894 vector_set (Vripng_passive_interface, strdup (ifname));
895
896 ripng_passive_interface_apply_all ();
897
898 return CMD_SUCCESS;
899}
900
901int
902ripng_passive_interface_unset (struct vty *vty, char *ifname)
903{
904 int i;
905 char *str;
906
907 i = ripng_passive_interface_lookup (ifname);
908 if (i < 0)
909 return CMD_WARNING;
910
911 str = vector_slot (Vripng_passive_interface, i);
912 free (str);
913 vector_unset (Vripng_passive_interface, i);
914
915 ripng_passive_interface_apply_all ();
916
917 return CMD_SUCCESS;
918}
919
920/* Free all configured RIP passive-interface settings. */
921void
922ripng_passive_interface_clean (void)
923{
924 int i;
925 char *str;
926
927 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
928 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
929 {
930 free (str);
931 vector_slot (Vripng_passive_interface, i) = NULL;
932 }
933 ripng_passive_interface_apply_all ();
934}
935
936/* Write RIPng enable network and interface to the vty. */
937int
hassoa94434b2003-05-25 17:10:12 +0000938ripng_network_write (struct vty *vty, int config_mode)
paul718e3742002-12-13 20:15:29 +0000939{
940 int i;
paul718e3742002-12-13 20:15:29 +0000941 char *ifname;
942 struct route_node *node;
943 char buf[BUFSIZ];
944
945 /* Write enable network. */
946 for (node = route_top (ripng_enable_network); node; node = route_next (node))
947 if (node->info)
948 {
949 struct prefix *p = &node->p;
hassoa94434b2003-05-25 17:10:12 +0000950 vty_out (vty, "%s%s/%d%s",
951 config_mode ? " network " : " ",
paul718e3742002-12-13 20:15:29 +0000952 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
953 p->prefixlen,
954 VTY_NEWLINE);
955
956 }
957
958 /* Write enable interface. */
959 for (i = 0; i < vector_max (ripng_enable_if); i++)
hassoa94434b2003-05-25 17:10:12 +0000960 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
961 vty_out (vty, "%s%s%s",
962 config_mode ? " network " : " ",
963 ifname,
paul718e3742002-12-13 20:15:29 +0000964 VTY_NEWLINE);
965
966 /* Write passive interface. */
hassoa94434b2003-05-25 17:10:12 +0000967 if (config_mode)
968 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
969 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
970 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000971
972 return 0;
973}
974
975/* RIPng enable on specified interface or matched network. */
976DEFUN (ripng_network,
977 ripng_network_cmd,
978 "network IF_OR_ADDR",
979 "RIPng enable on specified interface or network.\n"
980 "Interface or address")
981{
982 int ret;
983 struct prefix p;
984
985 ret = str2prefix (argv[0], &p);
986
987 /* Given string is IPv6 network or interface name. */
988 if (ret)
989 ret = ripng_enable_network_add (&p);
990 else
991 ret = ripng_enable_if_add (argv[0]);
992
993 if (ret < 0)
994 {
995 vty_out (vty, "There is same network configuration %s%s", argv[0],
996 VTY_NEWLINE);
997 return CMD_WARNING;
998 }
999
paul718e3742002-12-13 20:15:29 +00001000 return CMD_SUCCESS;
1001}
1002
1003/* RIPng enable on specified interface or matched network. */
1004DEFUN (no_ripng_network,
1005 no_ripng_network_cmd,
1006 "no network IF_OR_ADDR",
1007 NO_STR
1008 "RIPng enable on specified interface or network.\n"
1009 "Interface or address")
1010{
1011 int ret;
1012 struct prefix p;
1013
1014 ret = str2prefix (argv[0], &p);
1015
1016 /* Given string is interface name. */
1017 if (ret)
1018 ret = ripng_enable_network_delete (&p);
1019 else
1020 ret = ripng_enable_if_delete (argv[0]);
1021
1022 if (ret < 0)
1023 {
1024 vty_out (vty, "can't find network %s%s", argv[0],
1025 VTY_NEWLINE);
1026 return CMD_WARNING;
1027 }
1028
paul718e3742002-12-13 20:15:29 +00001029 return CMD_SUCCESS;
1030}
1031
hassoa94434b2003-05-25 17:10:12 +00001032DEFUN (ipv6_ripng_split_horizon,
1033 ipv6_ripng_split_horizon_cmd,
1034 "ipv6 ripng split-horizon",
1035 IPV6_STR
1036 "Routing Information Protocol\n"
1037 "Perform split horizon\n")
1038{
1039 struct interface *ifp;
1040 struct ripng_interface *ri;
1041
1042 ifp = vty->index;
1043 ri = ifp->info;
1044
1045 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1046 return CMD_SUCCESS;
1047}
1048
1049DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1050 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1051 "ipv6 ripng split-horizon poisoned-reverse",
1052 IPV6_STR
1053 "Routing Information Protocol\n"
1054 "Perform split horizon\n"
1055 "With poisoned-reverse\n")
1056{
1057 struct interface *ifp;
1058 struct ripng_interface *ri;
1059
1060 ifp = vty->index;
1061 ri = ifp->info;
1062
1063 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1064 return CMD_SUCCESS;
1065}
1066
1067DEFUN (no_ipv6_ripng_split_horizon,
1068 no_ipv6_ripng_split_horizon_cmd,
1069 "no ipv6 ripng split-horizon",
1070 NO_STR
1071 IPV6_STR
1072 "Routing Information Protocol\n"
1073 "Perform split horizon\n")
1074{
1075 struct interface *ifp;
1076 struct ripng_interface *ri;
1077
1078 ifp = vty->index;
1079 ri = ifp->info;
1080
1081 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1082 return CMD_SUCCESS;
1083}
1084
1085ALIAS (no_ipv6_ripng_split_horizon,
1086 no_ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1087 "no ipv6 ripng split-horizon poisoned-reverse",
1088 NO_STR
1089 IPV6_STR
1090 "Routing Information Protocol\n"
1091 "Perform split horizon\n"
1092 "With poisoned-reverse\n")
1093
paul718e3742002-12-13 20:15:29 +00001094DEFUN (ripng_passive_interface,
1095 ripng_passive_interface_cmd,
1096 "passive-interface IFNAME",
1097 "Suppress routing updates on an interface\n"
1098 "Interface name\n")
1099{
1100 return ripng_passive_interface_set (vty, argv[0]);
1101}
1102
1103DEFUN (no_ripng_passive_interface,
1104 no_ripng_passive_interface_cmd,
1105 "no passive-interface IFNAME",
1106 NO_STR
1107 "Suppress routing updates on an interface\n"
1108 "Interface name\n")
1109{
1110 return ripng_passive_interface_unset (vty, argv[0]);
1111}
1112
1113struct ripng_interface *
1114ri_new ()
1115{
1116 struct ripng_interface *ri;
1117 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
hassoa94434b2003-05-25 17:10:12 +00001118
1119 /* Set default split-horizon behavior. If the interface is Frame
1120 Relay or SMDS is enabled, the default value for split-horizon is
1121 off. But currently Zebra does detect Frame Relay or SMDS
1122 interface. So all interface is set to split horizon. */
1123 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1124 ri->split_horizon = ri->split_horizon_default;
1125
paul718e3742002-12-13 20:15:29 +00001126 return ri;
1127}
1128
1129int
1130ripng_if_new_hook (struct interface *ifp)
1131{
1132 ifp->info = ri_new ();
1133 return 0;
1134}
1135
hassoa94434b2003-05-25 17:10:12 +00001136/* Called when interface structure deleted. */
1137int
1138ripng_if_delete_hook (struct interface *ifp)
1139{
1140 XFREE (MTYPE_IF, ifp->info);
1141 ifp->info = NULL;
1142 return 0;
1143}
1144
paul718e3742002-12-13 20:15:29 +00001145/* Configuration write function for ripngd. */
1146int
1147interface_config_write (struct vty *vty)
1148{
1149 listnode node;
1150 struct interface *ifp;
1151 struct ripng_interface *ri;
1152 int write = 0;
1153
1154 for (node = listhead (iflist); node; nextnode (node))
1155 {
1156 ifp = getdata (node);
1157 ri = ifp->info;
1158
hassoa94434b2003-05-25 17:10:12 +00001159 /* Do not display the interface if there is no
1160 * configuration about it.
1161 **/
1162 if ((!ifp->desc) &&
1163 (ri->split_horizon == ri->split_horizon_default))
1164 continue;
1165
paul718e3742002-12-13 20:15:29 +00001166 vty_out (vty, "interface %s%s", ifp->name,
1167 VTY_NEWLINE);
1168 if (ifp->desc)
1169 vty_out (vty, " description %s%s", ifp->desc,
1170 VTY_NEWLINE);
1171
hassoa94434b2003-05-25 17:10:12 +00001172 /* Split horizon. */
1173 if (ri->split_horizon != ri->split_horizon_default)
1174 {
1175 switch (ri->split_horizon) {
1176 case RIPNG_SPLIT_HORIZON:
1177 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1178 break;
1179 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1180 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1181 VTY_NEWLINE);
1182 break;
1183 case RIPNG_NO_SPLIT_HORIZON:
1184 default:
1185 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1186 break;
1187 }
1188 }
1189
paul718e3742002-12-13 20:15:29 +00001190 vty_out (vty, "!%s", VTY_NEWLINE);
1191
1192 write++;
1193 }
1194 return write;
1195}
1196
1197/* ripngd's interface node. */
1198struct cmd_node interface_node =
1199{
1200 INTERFACE_NODE,
1201 "%s(config-if)# ",
hassoa94434b2003-05-25 17:10:12 +00001202 1 /* VTYSH */
paul718e3742002-12-13 20:15:29 +00001203};
1204
1205/* Initialization of interface. */
1206void
1207ripng_if_init ()
1208{
1209 /* Interface initialize. */
1210 iflist = list_new ();
1211 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
hassoa94434b2003-05-25 17:10:12 +00001212 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
paul718e3742002-12-13 20:15:29 +00001213
1214 /* RIPng enable network init. */
1215 ripng_enable_network = route_table_init ();
1216
1217 /* RIPng enable interface init. */
1218 ripng_enable_if = vector_init (1);
1219
1220 /* RIPng passive interface. */
1221 Vripng_passive_interface = vector_init (1);
1222
1223 /* Install interface node. */
1224 install_node (&interface_node, interface_config_write);
hassoa94434b2003-05-25 17:10:12 +00001225
1226 /* Install commands. */
paul718e3742002-12-13 20:15:29 +00001227 install_element (CONFIG_NODE, &interface_cmd);
paul32d24632003-05-23 09:25:20 +00001228 install_element (CONFIG_NODE, &no_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001229 install_default (INTERFACE_NODE);
paul718e3742002-12-13 20:15:29 +00001230 install_element (INTERFACE_NODE, &interface_desc_cmd);
1231 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1232
1233 install_element (RIPNG_NODE, &ripng_network_cmd);
1234 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1235 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1236 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001237
1238 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1239 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1240 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1241 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_poisoned_reverse_cmd);
paul718e3742002-12-13 20:15:29 +00001242}