blob: b4299eb07af9025bd4fe520189bc895347c82c35 [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));
70 if (ret < 0)
71 zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +000072
hassoa94434b2003-05-25 17:10:12 +000073 if (IS_RIPNG_DEBUG_EVENT)
74 zlog_info ("RIPng %s join to all-rip-routers multicast group", ifp->name);
paul718e3742002-12-13 20:15:29 +000075
hassoa94434b2003-05-25 17:10:12 +000076 if (ret < 0)
77 return -1;
78 }
79 return 0;
paul718e3742002-12-13 20:15:29 +000080}
81
82/* Leave from the all rip routers multicast group. */
83int
84ripng_multicast_leave (struct interface *ifp)
85{
86 int ret;
87 struct ipv6_mreq mreq;
88
hassoa94434b2003-05-25 17:10:12 +000089 if (if_is_up (ifp) && if_is_multicast (ifp)) {
90 memset (&mreq, 0, sizeof (mreq));
91 inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr);
92 mreq.ipv6mr_interface = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +000093
hassoa94434b2003-05-25 17:10:12 +000094 ret = setsockopt (ripng->sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
95 (char *) &mreq, sizeof (mreq));
96 if (ret < 0)
97 zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s\n", strerror (errno));
paul718e3742002-12-13 20:15:29 +000098
hassoa94434b2003-05-25 17:10:12 +000099 if (IS_RIPNG_DEBUG_EVENT)
100 zlog_info ("RIPng %s leave from all-rip-routers multicast group",
101 ifp->name);
paul718e3742002-12-13 20:15:29 +0000102
hassoa94434b2003-05-25 17:10:12 +0000103 if (ret < 0)
104 return -1;
105 }
106
107 return 0;
108}
109
110/* How many link local IPv6 address could be used on the interface ? */
111int
112ripng_if_ipv6_lladdress_check (struct interface *ifp)
113{
114 struct listnode *nn;
115 struct connected *connected;
116 int count = 0;
117
118 for (nn = listhead (ifp->connected); nn; nextnode (nn))
119 if ((connected = getdata (nn)) != NULL) {
120 struct prefix *p;
121 p = connected->address;
122
123 if ((p->family == AF_INET6) &&
124 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
125 count++;
126 }
127
128 return count;
paul718e3742002-12-13 20:15:29 +0000129}
130
131/* Check max mtu size. */
132int
133ripng_check_max_mtu ()
134{
135 listnode node;
136 struct interface *ifp;
137 int mtu;
138
139 mtu = 0;
140 for (node = listhead (iflist); node; nextnode (node))
141 {
142 ifp = getdata (node);
143 if (mtu < ifp->mtu)
144 mtu = ifp->mtu;
145 }
146 return mtu;
147}
148
149int
150ripng_if_down (struct interface *ifp)
151{
152 struct route_node *rp;
153 struct ripng_info *rinfo;
154 struct ripng_interface *ri;
155
hassoa94434b2003-05-25 17:10:12 +0000156 if (ripng)
paul718e3742002-12-13 20:15:29 +0000157 {
158 for (rp = route_top (ripng->table); rp; rp = route_next (rp))
159 if ((rinfo = rp->info) != NULL)
160 {
161 /* Routes got through this interface. */
162 if (rinfo->ifindex == ifp->ifindex
163 && rinfo->type == ZEBRA_ROUTE_RIPNG
164 && rinfo->sub_type == RIPNG_ROUTE_RTE)
165 {
166 ripng_zebra_ipv6_delete ((struct prefix_ipv6 *) &rp->p,
167 &rinfo->nexthop,
168 rinfo->ifindex);
169
hassoa94434b2003-05-25 17:10:12 +0000170 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
171 (struct prefix_ipv6 *)&rp->p,
172 rinfo->ifindex);
paul718e3742002-12-13 20:15:29 +0000173 }
174 else
175 {
hassoa94434b2003-05-25 17:10:12 +0000176 /* All redistributed routes got through this interface,
177 * but the static and system ones are kept. */
178 if ((rinfo->ifindex == ifp->ifindex) &&
179 (rinfo->type != ZEBRA_ROUTE_STATIC) &&
180 (rinfo->type != ZEBRA_ROUTE_SYSTEM))
paul718e3742002-12-13 20:15:29 +0000181 ripng_redistribute_delete (rinfo->type, rinfo->sub_type,
182 (struct prefix_ipv6 *) &rp->p,
183 rinfo->ifindex);
184 }
185 }
186 }
187
188 ri = ifp->info;
189
hassoa94434b2003-05-25 17:10:12 +0000190 if (ri->running)
paul718e3742002-12-13 20:15:29 +0000191 {
192 if (IS_RIPNG_DEBUG_EVENT)
193 zlog_info ("turn off %s", ifp->name);
194
195 /* Leave from multicast group. */
196 ripng_multicast_leave (ifp);
197
198 ri->running = 0;
199 }
200
201 return 0;
202}
203
204/* Inteface link up message processing. */
205int
206ripng_interface_up (int command, struct zclient *zclient, zebra_size_t length)
207{
208 struct stream *s;
209 struct interface *ifp;
210
211 /* zebra_interface_state_read() updates interface structure in iflist. */
212 s = zclient->ibuf;
213 ifp = zebra_interface_state_read (s);
214
215 if (ifp == NULL)
216 return 0;
217
218 if (IS_RIPNG_DEBUG_ZEBRA)
219 zlog_info ("interface up %s index %d flags %ld metric %d mtu %d",
220 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
221
222 /* Check if this interface is RIPng enabled or not. */
223 ripng_enable_apply (ifp);
224
225 /* Check for a passive interface. */
226 ripng_passive_interface_apply (ifp);
227
228 /* Apply distribute list to the all interface. */
229 ripng_distribute_update_interface (ifp);
230
231 return 0;
232}
233
234/* Inteface link down message processing. */
235int
236ripng_interface_down (int command, struct zclient *zclient,
237 zebra_size_t length)
238{
239 struct stream *s;
240 struct interface *ifp;
241
242 /* zebra_interface_state_read() updates interface structure in iflist. */
243 s = zclient->ibuf;
244 ifp = zebra_interface_state_read (s);
245
246 if (ifp == NULL)
247 return 0;
248
249 ripng_if_down (ifp);
250
251 if (IS_RIPNG_DEBUG_ZEBRA)
252 zlog_info ("interface down %s index %d flags %ld metric %d mtu %d",
253 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
254
255 return 0;
256}
257
258/* Inteface addition message from zebra. */
259int
260ripng_interface_add (int command, struct zclient *zclient, zebra_size_t length)
261{
262 struct interface *ifp;
263
264 ifp = zebra_interface_add_read (zclient->ibuf);
265
266 if (IS_RIPNG_DEBUG_ZEBRA)
267 zlog_info ("RIPng interface add %s index %d flags %ld metric %d mtu %d",
268 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
269
270 /* Check is this interface is RIP enabled or not.*/
271 ripng_enable_apply (ifp);
272
273 /* Apply distribute list to the interface. */
274 ripng_distribute_update_interface (ifp);
275
276 /* Check interface routemap. */
277 ripng_if_rmap_update_interface (ifp);
278
279 return 0;
280}
281
282int
283ripng_interface_delete (int command, struct zclient *zclient,
284 zebra_size_t length)
285{
hassoa94434b2003-05-25 17:10:12 +0000286 struct interface *ifp;
287 struct stream *s;
288
289 s = zclient->ibuf;
290 /* zebra_interface_state_read() updates interface structure in iflist */
291 ifp = zebra_interface_state_read(s);
292
293 if (ifp == NULL)
294 return 0;
295
296 if (if_is_up (ifp)) {
297 ripng_if_down(ifp);
298 }
299
300 zlog_info("interface delete %s index %d flags %ld metric %d mtu %d",
301 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
302
303 /* To support pseudo interface do not free interface structure. */
304 /* if_delete(ifp); */
305
paul718e3742002-12-13 20:15:29 +0000306 return 0;
307}
308
hassoa94434b2003-05-25 17:10:12 +0000309void
310ripng_interface_clean ()
311{
312 listnode node;
313 struct interface *ifp;
314 struct ripng_interface *ri;
315
316 for (node = listhead (iflist); node; nextnode (node))
317 {
318 ifp = getdata (node);
319 ri = ifp->info;
320
321 ri->enable_network = 0;
322 ri->enable_interface = 0;
323 ri->running = 0;
324
325 if (ri->t_wakeup)
326 {
327 thread_cancel (ri->t_wakeup);
328 ri->t_wakeup = NULL;
329 }
330 }
331}
332
333void
334ripng_interface_reset () {
335 listnode node;
336 struct interface *ifp;
337 struct ripng_interface *ri;
338
339 for (node = listhead (iflist); node; nextnode (node))
340 {
341 ifp = getdata (node);
342 ri = ifp->info;
343
344 ri->enable_network = 0;
345 ri->enable_interface = 0;
346 ri->running = 0;
347
348 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
349 ri->split_horizon_default = RIPNG_NO_SPLIT_HORIZON;
350
351 ri->list[RIPNG_FILTER_IN] = NULL;
352 ri->list[RIPNG_FILTER_OUT] = NULL;
353
354 ri->prefix[RIPNG_FILTER_IN] = NULL;
355 ri->prefix[RIPNG_FILTER_OUT] = NULL;
356
357 if (ri->t_wakeup)
358 {
359 thread_cancel (ri->t_wakeup);
360 ri->t_wakeup = NULL;
361 }
362
363 ri->passive = 0;
364 }
365}
366
367static void
368ripng_apply_address_add (struct connected *ifc) {
369 struct prefix_ipv6 address;
370 struct prefix *p;
371
372 if (!ripng)
373 return;
374
375 if (! if_is_up(ifc->ifp))
376 return;
377
378 p = ifc->address;
379
380 memset (&address, 0, sizeof (address));
381 address.family = p->family;
382 address.prefix = p->u.prefix6;
383 address.prefixlen = p->prefixlen;
384 apply_mask_ipv6(&address);
385
386 /* Check if this interface is RIP enabled or not
387 or Check if this address's prefix is RIP enabled */
388 if ((ripng_enable_if_lookup(ifc->ifp->name) >= 0) ||
389 (ripng_enable_network_lookup2(ifc) >= 0))
390 ripng_redistribute_add(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
391 &address, ifc->ifp->ifindex, NULL);
392
393}
394
paul718e3742002-12-13 20:15:29 +0000395int
396ripng_interface_address_add (int command, struct zclient *zclient,
397 zebra_size_t length)
398{
399 struct connected *c;
400 struct prefix *p;
paul718e3742002-12-13 20:15:29 +0000401
402 c = zebra_interface_address_add_read (zclient->ibuf);
403
404 if (c == NULL)
405 return 0;
406
407 p = c->address;
408
409 if (p->family == AF_INET6)
410 {
411 if (IS_RIPNG_DEBUG_ZEBRA)
412 zlog_info ("RIPng connected address %s/%d add",
hassoa94434b2003-05-25 17:10:12 +0000413 inet6_ntop(&p->u.prefix6),
paul718e3742002-12-13 20:15:29 +0000414 p->prefixlen);
415
hassoa94434b2003-05-25 17:10:12 +0000416 /* Check is this prefix needs to be redistributed. */
417 ripng_apply_address_add(c);
418
419 /* Let's try once again whether the interface could be activated */
420 if (c->ifp) {
421 struct ripng_interface *ri = c->ifp->info;
422
423 if (!ri->running) {
424 /* Check if this interface is RIP enabled or not.*/
425 ripng_enable_apply (c->ifp);
426
427 /* Apply distribute list to the interface. */
428 ripng_distribute_update_interface (c->ifp);
429
430 /* Check interface routemap. */
431 ripng_if_rmap_update_interface (c->ifp);
432 }
433 }
434
paul718e3742002-12-13 20:15:29 +0000435 }
436
437 return 0;
438}
439
hassoa94434b2003-05-25 17:10:12 +0000440static void
441ripng_apply_address_del (struct connected *ifc) {
442 struct prefix_ipv6 address;
443 struct prefix *p;
444
445 if (!ripng)
446 return;
447
448 if (! if_is_up(ifc->ifp))
449 return;
450
451 p = ifc->address;
452
453 memset (&address, 0, sizeof (address));
454 address.family = p->family;
455 address.prefix = p->u.prefix6;
456 address.prefixlen = p->prefixlen;
457 apply_mask_ipv6(&address);
458
459 ripng_redistribute_delete(ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
460 &address, ifc->ifp->ifindex);
461}
462
paul718e3742002-12-13 20:15:29 +0000463int
464ripng_interface_address_delete (int command, struct zclient *zclient,
465 zebra_size_t length)
466{
467 struct connected *ifc;
468 struct prefix *p;
469 char buf[INET6_ADDRSTRLEN];
470
471 ifc = zebra_interface_address_delete_read (zclient->ibuf);
472
473 if (ifc)
474 {
475 p = ifc->address;
476
477 if (p->family == AF_INET6)
478 {
479 if (IS_RIPNG_DEBUG_ZEBRA)
480 zlog_info ("RIPng connected address %s/%d delete",
481 inet_ntop (AF_INET6, &p->u.prefix6, buf,
482 INET6_ADDRSTRLEN),
483 p->prefixlen);
484
hassoa94434b2003-05-25 17:10:12 +0000485 /* Check wether this prefix needs to be removed. */
486 ripng_apply_address_del(ifc);
paul718e3742002-12-13 20:15:29 +0000487 }
488 connected_free (ifc);
489 }
490
491 return 0;
492}
493
494/* RIPng enable interface vector. */
495vector ripng_enable_if;
496
497/* RIPng enable network table. */
498struct route_table *ripng_enable_network;
499
500/* Lookup RIPng enable network. */
hassoa94434b2003-05-25 17:10:12 +0000501/* Check wether the interface has at least a connected prefix that
502 * is within the ripng_enable_network table. */
paul718e3742002-12-13 20:15:29 +0000503int
hassoa94434b2003-05-25 17:10:12 +0000504ripng_enable_network_lookup_if (struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000505{
506 listnode listnode;
507 struct connected *connected;
hassoa94434b2003-05-25 17:10:12 +0000508 struct prefix_ipv6 address;
paul718e3742002-12-13 20:15:29 +0000509
510 for (listnode = listhead (ifp->connected); listnode; nextnode (listnode))
511 if ((connected = getdata (listnode)) != NULL)
512 {
513 struct prefix *p;
514 struct route_node *node;
515
516 p = connected->address;
517
518 if (p->family == AF_INET6)
519 {
hassoa94434b2003-05-25 17:10:12 +0000520 address.family = AF_INET6;
521 address.prefix = p->u.prefix6;
522 address.prefixlen = IPV6_MAX_BITLEN;
523
524 node = route_node_match (ripng_enable_network,
525 (struct prefix *)&address);
paul718e3742002-12-13 20:15:29 +0000526 if (node)
527 {
528 route_unlock_node (node);
529 return 1;
530 }
531 }
532 }
533 return -1;
534}
535
hassoa94434b2003-05-25 17:10:12 +0000536/* Check wether connected is within the ripng_enable_network table. */
537int
538ripng_enable_network_lookup2 (struct connected *connected)
539{
540 struct prefix_ipv6 address;
541 struct prefix *p;
542
543 p = connected->address;
544
545 if (p->family == AF_INET6) {
546 struct route_node *node;
547
548 address.family = p->family;
549 address.prefix = p->u.prefix6;
550 address.prefixlen = IPV6_MAX_BITLEN;
551
552 /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within ripng_enable_network */
553 node = route_node_match (ripng_enable_network,
554 (struct prefix *)&address);
555
556 if (node) {
557 route_unlock_node (node);
558 return 1;
559 }
560 }
561
562 return -1;
563}
564
paul718e3742002-12-13 20:15:29 +0000565/* Add RIPng enable network. */
566int
567ripng_enable_network_add (struct prefix *p)
568{
569 struct route_node *node;
570
571 node = route_node_get (ripng_enable_network, p);
572
573 if (node->info)
574 {
575 route_unlock_node (node);
576 return -1;
577 }
578 else
579 node->info = "enabled";
580
hassoa94434b2003-05-25 17:10:12 +0000581 /* XXX: One should find a better solution than a generic one */
582 ripng_enable_apply_all();
583
paul718e3742002-12-13 20:15:29 +0000584 return 1;
585}
586
587/* Delete RIPng enable network. */
588int
589ripng_enable_network_delete (struct prefix *p)
590{
591 struct route_node *node;
592
593 node = route_node_lookup (ripng_enable_network, p);
594 if (node)
595 {
596 node->info = NULL;
597
598 /* Unlock info lock. */
599 route_unlock_node (node);
600
601 /* Unlock lookup lock. */
602 route_unlock_node (node);
603
604 return 1;
605 }
606 return -1;
607}
608
609/* Lookup function. */
610int
611ripng_enable_if_lookup (char *ifname)
612{
613 int i;
614 char *str;
615
616 for (i = 0; i < vector_max (ripng_enable_if); i++)
617 if ((str = vector_slot (ripng_enable_if, i)) != NULL)
618 if (strcmp (str, ifname) == 0)
619 return i;
620 return -1;
621}
622
623/* Add interface to ripng_enable_if. */
624int
625ripng_enable_if_add (char *ifname)
626{
627 int ret;
628
629 ret = ripng_enable_if_lookup (ifname);
630 if (ret >= 0)
631 return -1;
632
633 vector_set (ripng_enable_if, strdup (ifname));
634
hassoa94434b2003-05-25 17:10:12 +0000635 ripng_enable_apply_all();
636
paul718e3742002-12-13 20:15:29 +0000637 return 1;
638}
639
640/* Delete interface from ripng_enable_if. */
641int
642ripng_enable_if_delete (char *ifname)
643{
644 int index;
645 char *str;
646
647 index = ripng_enable_if_lookup (ifname);
648 if (index < 0)
649 return -1;
650
651 str = vector_slot (ripng_enable_if, index);
652 free (str);
653 vector_unset (ripng_enable_if, index);
654
hassoa94434b2003-05-25 17:10:12 +0000655 ripng_enable_apply_all();
656
paul718e3742002-12-13 20:15:29 +0000657 return 1;
658}
659
660/* Wake up interface. */
661int
662ripng_interface_wakeup (struct thread *t)
663{
664 struct interface *ifp;
665 struct ripng_interface *ri;
666
667 /* Get interface. */
668 ifp = THREAD_ARG (t);
669
670 ri = ifp->info;
671 ri->t_wakeup = NULL;
672
673 /* Join to multicast group. */
hassoa94434b2003-05-25 17:10:12 +0000674 if (ripng_multicast_join (ifp) < 0) {
675 zlog_err ("multicast join failed, interface %s not running", ifp->name);
676 return 0;
677 }
678
679 /* Set running flag. */
680 ri->running = 1;
paul718e3742002-12-13 20:15:29 +0000681
682 /* Send RIP request to the interface. */
683 ripng_request (ifp);
684
685 return 0;
686}
687
hassoa94434b2003-05-25 17:10:12 +0000688int ripng_redistribute_check (int);
689
690void
691ripng_connect_set (struct interface *ifp, int set)
692{
693 struct listnode *nn;
694 struct connected *connected;
695 struct prefix_ipv6 address;
696
697 for (nn = listhead (ifp->connected); nn; nextnode (nn))
698 if ((connected = getdata (nn)) != NULL) {
699 struct prefix *p;
700 p = connected->address;
701
702 if (p->family != AF_INET6)
703 continue;
704
705 address.family = AF_INET6;
706 address.prefix = p->u.prefix6;
707 address.prefixlen = p->prefixlen;
708 apply_mask_ipv6 (&address);
709
710 if (set) {
711 /* Check once more wether this prefix is within a "network IF_OR_PREF" one */
712 if ((ripng_enable_if_lookup(connected->ifp->name) >= 0) ||
713 (ripng_enable_network_lookup2(connected) >= 0))
714 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
715 &address, connected->ifp->ifindex, NULL);
716 } else {
717 ripng_redistribute_delete (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_INTERFACE,
718 &address, connected->ifp->ifindex);
719 if (ripng_redistribute_check (ZEBRA_ROUTE_CONNECT))
720 ripng_redistribute_add (ZEBRA_ROUTE_CONNECT, RIPNG_ROUTE_REDISTRIBUTE,
721 &address, connected->ifp->ifindex, NULL);
722 }
723 }
724}
725
paul718e3742002-12-13 20:15:29 +0000726/* Check RIPng is enabed on this interface. */
727void
728ripng_enable_apply (struct interface *ifp)
729{
730 int ret;
731 struct ripng_interface *ri = NULL;
732
733 /* Check interface. */
paul718e3742002-12-13 20:15:29 +0000734 if (! if_is_up (ifp))
735 return;
736
737 ri = ifp->info;
738
hassoa94434b2003-05-25 17:10:12 +0000739 /* Is this interface a candidate for RIPng ? */
740 ret = ripng_enable_network_lookup_if (ifp);
paul718e3742002-12-13 20:15:29 +0000741
742 /* If the interface is matched. */
743 if (ret > 0)
744 ri->enable_network = 1;
745 else
746 ri->enable_network = 0;
747
748 /* Check interface name configuration. */
749 ret = ripng_enable_if_lookup (ifp->name);
750 if (ret >= 0)
751 ri->enable_interface = 1;
752 else
753 ri->enable_interface = 0;
754
hassoa94434b2003-05-25 17:10:12 +0000755 /* any candidate interface MUST have a link-local IPv6 address */
756 if ((! ripng_if_ipv6_lladdress_check (ifp)) &&
757 (ri->enable_network || ri->enable_interface)) {
758 ri->enable_network = 0;
759 ri->enable_interface = 0;
760 zlog_warn("Interface %s does not have any link-local address",
761 ifp->name);
762 }
763
paul718e3742002-12-13 20:15:29 +0000764 /* Update running status of the interface. */
765 if (ri->enable_network || ri->enable_interface)
766 {
paul718e3742002-12-13 20:15:29 +0000767 {
768 if (IS_RIPNG_DEBUG_EVENT)
769 zlog_info ("RIPng turn on %s", ifp->name);
770
771 /* Add interface wake up thread. */
772 if (! ri->t_wakeup)
773 ri->t_wakeup = thread_add_timer (master, ripng_interface_wakeup,
774 ifp, 1);
paul718e3742002-12-13 20:15:29 +0000775
hassoa94434b2003-05-25 17:10:12 +0000776 ripng_connect_set (ifp, 1);
paul718e3742002-12-13 20:15:29 +0000777 }
778 }
779 else
780 {
781 if (ri->running)
782 {
hassoa94434b2003-05-25 17:10:12 +0000783 /* Might as well clean up the route table as well
784 * ripng_if_down sets to 0 ri->running, and displays "turn off %s"
785 **/
786 ripng_if_down(ifp);
paul718e3742002-12-13 20:15:29 +0000787
hassoa94434b2003-05-25 17:10:12 +0000788 ripng_connect_set (ifp, 0);
paul718e3742002-12-13 20:15:29 +0000789 }
790 }
791}
792
793/* Set distribute list to all interfaces. */
hassoa94434b2003-05-25 17:10:12 +0000794void
paul718e3742002-12-13 20:15:29 +0000795ripng_enable_apply_all ()
796{
797 struct interface *ifp;
798 listnode node;
799
800 for (node = listhead (iflist); node; nextnode (node))
801 {
802 ifp = getdata (node);
803 ripng_enable_apply (ifp);
804 }
805}
806
hassoa94434b2003-05-25 17:10:12 +0000807/* Clear all network and neighbor configuration */
808void
809ripng_clean_network ()
810{
811 int i;
812 char *str;
813 struct route_node *rn;
814
815 /* ripng_enable_network */
816 for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn))
817 if (rn->info) {
818 rn->info = NULL;
819 route_unlock_node(rn);
820 }
821
822 /* ripng_enable_if */
823 for (i = 0; i < vector_max (ripng_enable_if); i++)
824 if ((str = vector_slot (ripng_enable_if, i)) != NULL) {
825 free (str);
826 vector_slot (ripng_enable_if, i) = NULL;
827 }
828}
829
paul718e3742002-12-13 20:15:29 +0000830/* Vector to store passive-interface name. */
831vector Vripng_passive_interface;
832
833/* Utility function for looking up passive interface settings. */
834int
835ripng_passive_interface_lookup (char *ifname)
836{
837 int i;
838 char *str;
839
840 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
841 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
842 if (strcmp (str, ifname) == 0)
843 return i;
844 return -1;
845}
846
847void
848ripng_passive_interface_apply (struct interface *ifp)
849{
850 int ret;
851 struct ripng_interface *ri;
852
853 ri = ifp->info;
854
855 ret = ripng_passive_interface_lookup (ifp->name);
856 if (ret < 0)
857 ri->passive = 0;
858 else
859 ri->passive = 1;
860}
861
862void
863ripng_passive_interface_apply_all (void)
864{
865 struct interface *ifp;
866 listnode node;
867
868 for (node = listhead (iflist); node; nextnode (node))
869 {
870 ifp = getdata (node);
871 ripng_passive_interface_apply (ifp);
872 }
873}
874
875/* Passive interface. */
876int
877ripng_passive_interface_set (struct vty *vty, char *ifname)
878{
879 if (ripng_passive_interface_lookup (ifname) >= 0)
880 return CMD_WARNING;
881
882 vector_set (Vripng_passive_interface, strdup (ifname));
883
884 ripng_passive_interface_apply_all ();
885
886 return CMD_SUCCESS;
887}
888
889int
890ripng_passive_interface_unset (struct vty *vty, char *ifname)
891{
892 int i;
893 char *str;
894
895 i = ripng_passive_interface_lookup (ifname);
896 if (i < 0)
897 return CMD_WARNING;
898
899 str = vector_slot (Vripng_passive_interface, i);
900 free (str);
901 vector_unset (Vripng_passive_interface, i);
902
903 ripng_passive_interface_apply_all ();
904
905 return CMD_SUCCESS;
906}
907
908/* Free all configured RIP passive-interface settings. */
909void
910ripng_passive_interface_clean (void)
911{
912 int i;
913 char *str;
914
915 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
916 if ((str = vector_slot (Vripng_passive_interface, i)) != NULL)
917 {
918 free (str);
919 vector_slot (Vripng_passive_interface, i) = NULL;
920 }
921 ripng_passive_interface_apply_all ();
922}
923
924/* Write RIPng enable network and interface to the vty. */
925int
hassoa94434b2003-05-25 17:10:12 +0000926ripng_network_write (struct vty *vty, int config_mode)
paul718e3742002-12-13 20:15:29 +0000927{
928 int i;
paul718e3742002-12-13 20:15:29 +0000929 char *ifname;
930 struct route_node *node;
931 char buf[BUFSIZ];
932
933 /* Write enable network. */
934 for (node = route_top (ripng_enable_network); node; node = route_next (node))
935 if (node->info)
936 {
937 struct prefix *p = &node->p;
hassoa94434b2003-05-25 17:10:12 +0000938 vty_out (vty, "%s%s/%d%s",
939 config_mode ? " network " : " ",
paul718e3742002-12-13 20:15:29 +0000940 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
941 p->prefixlen,
942 VTY_NEWLINE);
943
944 }
945
946 /* Write enable interface. */
947 for (i = 0; i < vector_max (ripng_enable_if); i++)
hassoa94434b2003-05-25 17:10:12 +0000948 if ((ifname = vector_slot (ripng_enable_if, i)) != NULL)
949 vty_out (vty, "%s%s%s",
950 config_mode ? " network " : " ",
951 ifname,
paul718e3742002-12-13 20:15:29 +0000952 VTY_NEWLINE);
953
954 /* Write passive interface. */
hassoa94434b2003-05-25 17:10:12 +0000955 if (config_mode)
956 for (i = 0; i < vector_max (Vripng_passive_interface); i++)
957 if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL)
958 vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000959
960 return 0;
961}
962
963/* RIPng enable on specified interface or matched network. */
964DEFUN (ripng_network,
965 ripng_network_cmd,
966 "network IF_OR_ADDR",
967 "RIPng enable on specified interface or network.\n"
968 "Interface or address")
969{
970 int ret;
971 struct prefix p;
972
973 ret = str2prefix (argv[0], &p);
974
975 /* Given string is IPv6 network or interface name. */
976 if (ret)
977 ret = ripng_enable_network_add (&p);
978 else
979 ret = ripng_enable_if_add (argv[0]);
980
981 if (ret < 0)
982 {
983 vty_out (vty, "There is same network configuration %s%s", argv[0],
984 VTY_NEWLINE);
985 return CMD_WARNING;
986 }
987
paul718e3742002-12-13 20:15:29 +0000988 return CMD_SUCCESS;
989}
990
991/* RIPng enable on specified interface or matched network. */
992DEFUN (no_ripng_network,
993 no_ripng_network_cmd,
994 "no network IF_OR_ADDR",
995 NO_STR
996 "RIPng enable on specified interface or network.\n"
997 "Interface or address")
998{
999 int ret;
1000 struct prefix p;
1001
1002 ret = str2prefix (argv[0], &p);
1003
1004 /* Given string is interface name. */
1005 if (ret)
1006 ret = ripng_enable_network_delete (&p);
1007 else
1008 ret = ripng_enable_if_delete (argv[0]);
1009
1010 if (ret < 0)
1011 {
1012 vty_out (vty, "can't find network %s%s", argv[0],
1013 VTY_NEWLINE);
1014 return CMD_WARNING;
1015 }
1016
paul718e3742002-12-13 20:15:29 +00001017 return CMD_SUCCESS;
1018}
1019
hassoa94434b2003-05-25 17:10:12 +00001020DEFUN (ipv6_ripng_split_horizon,
1021 ipv6_ripng_split_horizon_cmd,
1022 "ipv6 ripng split-horizon",
1023 IPV6_STR
1024 "Routing Information Protocol\n"
1025 "Perform split horizon\n")
1026{
1027 struct interface *ifp;
1028 struct ripng_interface *ri;
1029
1030 ifp = vty->index;
1031 ri = ifp->info;
1032
1033 ri->split_horizon = RIPNG_SPLIT_HORIZON;
1034 return CMD_SUCCESS;
1035}
1036
1037DEFUN (ipv6_ripng_split_horizon_poisoned_reverse,
1038 ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1039 "ipv6 ripng split-horizon poisoned-reverse",
1040 IPV6_STR
1041 "Routing Information Protocol\n"
1042 "Perform split horizon\n"
1043 "With poisoned-reverse\n")
1044{
1045 struct interface *ifp;
1046 struct ripng_interface *ri;
1047
1048 ifp = vty->index;
1049 ri = ifp->info;
1050
1051 ri->split_horizon = RIPNG_SPLIT_HORIZON_POISONED_REVERSE;
1052 return CMD_SUCCESS;
1053}
1054
1055DEFUN (no_ipv6_ripng_split_horizon,
1056 no_ipv6_ripng_split_horizon_cmd,
1057 "no ipv6 ripng split-horizon",
1058 NO_STR
1059 IPV6_STR
1060 "Routing Information Protocol\n"
1061 "Perform split horizon\n")
1062{
1063 struct interface *ifp;
1064 struct ripng_interface *ri;
1065
1066 ifp = vty->index;
1067 ri = ifp->info;
1068
1069 ri->split_horizon = RIPNG_NO_SPLIT_HORIZON;
1070 return CMD_SUCCESS;
1071}
1072
1073ALIAS (no_ipv6_ripng_split_horizon,
1074 no_ipv6_ripng_split_horizon_poisoned_reverse_cmd,
1075 "no ipv6 ripng split-horizon poisoned-reverse",
1076 NO_STR
1077 IPV6_STR
1078 "Routing Information Protocol\n"
1079 "Perform split horizon\n"
1080 "With poisoned-reverse\n")
1081
paul718e3742002-12-13 20:15:29 +00001082DEFUN (ripng_passive_interface,
1083 ripng_passive_interface_cmd,
1084 "passive-interface IFNAME",
1085 "Suppress routing updates on an interface\n"
1086 "Interface name\n")
1087{
1088 return ripng_passive_interface_set (vty, argv[0]);
1089}
1090
1091DEFUN (no_ripng_passive_interface,
1092 no_ripng_passive_interface_cmd,
1093 "no passive-interface IFNAME",
1094 NO_STR
1095 "Suppress routing updates on an interface\n"
1096 "Interface name\n")
1097{
1098 return ripng_passive_interface_unset (vty, argv[0]);
1099}
1100
1101struct ripng_interface *
1102ri_new ()
1103{
1104 struct ripng_interface *ri;
1105 ri = XCALLOC (MTYPE_IF, sizeof (struct ripng_interface));
hassoa94434b2003-05-25 17:10:12 +00001106
1107 /* Set default split-horizon behavior. If the interface is Frame
1108 Relay or SMDS is enabled, the default value for split-horizon is
1109 off. But currently Zebra does detect Frame Relay or SMDS
1110 interface. So all interface is set to split horizon. */
1111 ri->split_horizon_default = RIPNG_SPLIT_HORIZON;
1112 ri->split_horizon = ri->split_horizon_default;
1113
paul718e3742002-12-13 20:15:29 +00001114 return ri;
1115}
1116
1117int
1118ripng_if_new_hook (struct interface *ifp)
1119{
1120 ifp->info = ri_new ();
1121 return 0;
1122}
1123
hassoa94434b2003-05-25 17:10:12 +00001124/* Called when interface structure deleted. */
1125int
1126ripng_if_delete_hook (struct interface *ifp)
1127{
1128 XFREE (MTYPE_IF, ifp->info);
1129 ifp->info = NULL;
1130 return 0;
1131}
1132
paul718e3742002-12-13 20:15:29 +00001133/* Configuration write function for ripngd. */
1134int
1135interface_config_write (struct vty *vty)
1136{
1137 listnode node;
1138 struct interface *ifp;
1139 struct ripng_interface *ri;
1140 int write = 0;
1141
1142 for (node = listhead (iflist); node; nextnode (node))
1143 {
1144 ifp = getdata (node);
1145 ri = ifp->info;
1146
hassoa94434b2003-05-25 17:10:12 +00001147 /* Do not display the interface if there is no
1148 * configuration about it.
1149 **/
1150 if ((!ifp->desc) &&
1151 (ri->split_horizon == ri->split_horizon_default))
1152 continue;
1153
paul718e3742002-12-13 20:15:29 +00001154 vty_out (vty, "interface %s%s", ifp->name,
1155 VTY_NEWLINE);
1156 if (ifp->desc)
1157 vty_out (vty, " description %s%s", ifp->desc,
1158 VTY_NEWLINE);
1159
hassoa94434b2003-05-25 17:10:12 +00001160 /* Split horizon. */
1161 if (ri->split_horizon != ri->split_horizon_default)
1162 {
1163 switch (ri->split_horizon) {
1164 case RIPNG_SPLIT_HORIZON:
1165 vty_out (vty, " ipv6 ripng split-horizon%s", VTY_NEWLINE);
1166 break;
1167 case RIPNG_SPLIT_HORIZON_POISONED_REVERSE:
1168 vty_out (vty, " ipv6 ripng split-horizon poisoned-reverse%s",
1169 VTY_NEWLINE);
1170 break;
1171 case RIPNG_NO_SPLIT_HORIZON:
1172 default:
1173 vty_out (vty, " no ipv6 ripng split-horizon%s", VTY_NEWLINE);
1174 break;
1175 }
1176 }
1177
paul718e3742002-12-13 20:15:29 +00001178 vty_out (vty, "!%s", VTY_NEWLINE);
1179
1180 write++;
1181 }
1182 return write;
1183}
1184
1185/* ripngd's interface node. */
1186struct cmd_node interface_node =
1187{
1188 INTERFACE_NODE,
1189 "%s(config-if)# ",
hassoa94434b2003-05-25 17:10:12 +00001190 1 /* VTYSH */
paul718e3742002-12-13 20:15:29 +00001191};
1192
1193/* Initialization of interface. */
1194void
1195ripng_if_init ()
1196{
1197 /* Interface initialize. */
1198 iflist = list_new ();
1199 if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
hassoa94434b2003-05-25 17:10:12 +00001200 if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
paul718e3742002-12-13 20:15:29 +00001201
1202 /* RIPng enable network init. */
1203 ripng_enable_network = route_table_init ();
1204
1205 /* RIPng enable interface init. */
1206 ripng_enable_if = vector_init (1);
1207
1208 /* RIPng passive interface. */
1209 Vripng_passive_interface = vector_init (1);
1210
1211 /* Install interface node. */
1212 install_node (&interface_node, interface_config_write);
hassoa94434b2003-05-25 17:10:12 +00001213
1214 /* Install commands. */
paul718e3742002-12-13 20:15:29 +00001215 install_element (CONFIG_NODE, &interface_cmd);
paul32d24632003-05-23 09:25:20 +00001216 install_element (CONFIG_NODE, &no_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001217 install_default (INTERFACE_NODE);
paul718e3742002-12-13 20:15:29 +00001218 install_element (INTERFACE_NODE, &interface_desc_cmd);
1219 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1220
1221 install_element (RIPNG_NODE, &ripng_network_cmd);
1222 install_element (RIPNG_NODE, &no_ripng_network_cmd);
1223 install_element (RIPNG_NODE, &ripng_passive_interface_cmd);
1224 install_element (RIPNG_NODE, &no_ripng_passive_interface_cmd);
hassoa94434b2003-05-25 17:10:12 +00001225
1226 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_cmd);
1227 install_element (INTERFACE_NODE, &ipv6_ripng_split_horizon_poisoned_reverse_cmd);
1228 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_cmd);
1229 install_element (INTERFACE_NODE, &no_ipv6_ripng_split_horizon_poisoned_reverse_cmd);
paul718e3742002-12-13 20:15:29 +00001230}