isisd: change ISIS_METHOD to use C preprocessor
[jleu-quagga.git] / zebra / irdp_interface.c
blob8742b62b8cc8572a9a1af508ea55037af9636449
1 /*
3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
24 /*
25 * This work includes work with the following copywrite:
27 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
31 /*
32 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
33 * for reviewing and tests.
37 #include <zebra.h>
39 #ifdef HAVE_IRDP
41 #include "if.h"
42 #include "vty.h"
43 #include "sockunion.h"
44 #include "prefix.h"
45 #include "command.h"
46 #include "memory.h"
47 #include "stream.h"
48 #include "ioctl.h"
49 #include "connected.h"
50 #include "log.h"
51 #include "zclient.h"
52 #include "thread.h"
53 #include "zebra/interface.h"
54 #include "zebra/rtadv.h"
55 #include "zebra/rib.h"
56 #include "zebra/zserv.h"
57 #include "zebra/redistribute.h"
58 #include "zebra/irdp.h"
59 #include <netinet/ip_icmp.h>
60 #include "if.h"
61 #include "sockunion.h"
62 #include "log.h"
65 /* Master of threads. */
66 extern struct zebra_t zebrad;
68 extern int irdp_sock;
70 static const char *
71 inet_2a(u_int32_t a, char *b)
73 sprintf(b, "%u.%u.%u.%u",
74 (a ) & 0xFF,
75 (a>> 8) & 0xFF,
76 (a>>16) & 0xFF,
77 (a>>24) & 0xFF);
78 return b;
82 static struct prefix *
83 irdp_get_prefix(struct interface *ifp)
85 struct listnode *node;
86 struct connected *ifc;
88 if (ifp->connected)
89 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
90 return ifc->address;
92 return NULL;
95 /* Join to the add/leave multicast group. */
96 static int
97 if_group (struct interface *ifp,
98 int sock,
99 u_int32_t group,
100 int add_leave)
102 struct zebra_if *zi;
103 struct ip_mreq m;
104 struct prefix *p;
105 int ret;
106 char b1[INET_ADDRSTRLEN];
108 zi = ifp->info;
110 memset (&m, 0, sizeof (m));
111 m.imr_multiaddr.s_addr = htonl (group);
112 p = irdp_get_prefix(ifp);
114 if(!p) {
115 zlog_warn ("IRDP: can't get address for %s", ifp->name);
116 return 1;
119 m.imr_interface = p->u.prefix4;
121 ret = setsockopt (sock, IPPROTO_IP, add_leave,
122 (char *) &m, sizeof (struct ip_mreq));
123 if (ret < 0)
124 zlog_warn ("IRDP: %s can't setsockopt %s: %s",
125 add_leave == IP_ADD_MEMBERSHIP? "join group":"leave group",
126 inet_2a(group, b1),
127 safe_strerror (errno));
129 return ret;
132 static int
133 if_add_group (struct interface *ifp)
135 struct zebra_if *zi= ifp->info;
136 struct irdp_interface *irdp = &zi->irdp;
137 int ret;
138 char b1[INET_ADDRSTRLEN];
140 ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_ADD_MEMBERSHIP);
141 if (ret < 0) {
142 return ret;
145 if(irdp->flags & IF_DEBUG_MISC )
146 zlog_debug("IRDP: Adding group %s for %s",
147 inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
148 ifp->name);
149 return 0;
152 static int
153 if_drop_group (struct interface *ifp)
155 struct zebra_if *zi= ifp->info;
156 struct irdp_interface *irdp = &zi->irdp;
157 int ret;
158 char b1[INET_ADDRSTRLEN];
160 ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_DROP_MEMBERSHIP);
161 if (ret < 0)
162 return ret;
164 if(irdp->flags & IF_DEBUG_MISC)
165 zlog_debug("IRDP: Leaving group %s for %s",
166 inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
167 ifp->name);
168 return 0;
171 static void
172 if_set_defaults(struct interface *ifp)
174 struct zebra_if *zi=ifp->info;
175 struct irdp_interface *irdp=&zi->irdp;
177 irdp->MaxAdvertInterval = IRDP_MAXADVERTINTERVAL;
178 irdp->MinAdvertInterval = IRDP_MINADVERTINTERVAL;
179 irdp->Preference = IRDP_PREFERENCE;
180 irdp->Lifetime = IRDP_LIFETIME;
184 static struct Adv *Adv_new (void)
186 return XCALLOC (MTYPE_TMP, sizeof (struct Adv));
189 static void
190 Adv_free (struct Adv *adv)
192 XFREE (MTYPE_TMP, adv);
195 static void
196 irdp_if_start(struct interface *ifp, int multicast, int set_defaults)
198 struct zebra_if *zi= ifp->info;
199 struct irdp_interface *irdp = &zi->irdp;
200 struct listnode *node;
201 struct connected *ifc;
202 u_int32_t timer, seed;
204 if (irdp->flags & IF_ACTIVE ) {
205 zlog_warn("IRDP: Interface is already active %s", ifp->name);
206 return;
208 if ((irdp_sock < 0) && ((irdp_sock = irdp_sock_init()) < 0)) {
209 zlog_warn("IRDP: Cannot activate interface %s (cannot create "
210 "IRDP socket)", ifp->name);
211 return;
213 irdp->flags |= IF_ACTIVE;
215 if(!multicast)
216 irdp->flags |= IF_BROADCAST;
218 if_add_update(ifp);
220 if (! (ifp->flags & IFF_UP)) {
221 zlog_warn("IRDP: Interface is down %s", ifp->name);
224 /* Shall we cancel if_start if if_add_group fails? */
226 if( multicast) {
227 if_add_group(ifp);
229 if (! (ifp->flags & (IFF_MULTICAST|IFF_ALLMULTI))) {
230 zlog_warn("IRDP: Interface not multicast enabled %s", ifp->name);
234 if(set_defaults)
235 if_set_defaults(ifp);
237 irdp->irdp_sent = 0;
239 /* The spec suggests this for randomness */
241 seed = 0;
242 if( ifp->connected)
243 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
245 seed = ifc->address->u.prefix4.s_addr;
246 break;
249 srandom(seed);
250 timer = (random () % IRDP_DEFAULT_INTERVAL) + 1;
252 irdp->AdvPrefList = list_new();
253 irdp->AdvPrefList->del = (void (*)(void *)) Adv_free; /* Destructor */
256 /* And this for startup. Speed limit from 1991 :-). But it's OK*/
258 if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
259 timer > MAX_INITIAL_ADVERT_INTERVAL )
260 timer= MAX_INITIAL_ADVERT_INTERVAL;
263 if(irdp->flags & IF_DEBUG_MISC)
264 zlog_debug("IRDP: Init timer for %s set to %u",
265 ifp->name,
266 timer);
268 irdp->t_advertise = thread_add_timer(zebrad.master,
269 irdp_send_thread,
270 ifp,
271 timer);
274 static void
275 irdp_if_stop(struct interface *ifp)
277 struct zebra_if *zi=ifp->info;
278 struct irdp_interface *irdp=&zi->irdp;
280 if (irdp == NULL) {
281 zlog_warn ("Interface %s structure is NULL", ifp->name);
282 return;
285 if (! (irdp->flags & IF_ACTIVE )) {
286 zlog_warn("Interface is not active %s", ifp->name);
287 return;
290 if(! (irdp->flags & IF_BROADCAST))
291 if_drop_group(ifp);
293 irdp_advert_off(ifp);
295 list_delete(irdp->AdvPrefList);
296 irdp->AdvPrefList=NULL;
298 irdp->flags = 0;
302 static void
303 irdp_if_shutdown(struct interface *ifp)
305 struct zebra_if *zi= ifp->info;
306 struct irdp_interface *irdp = &zi->irdp;
308 if (irdp->flags & IF_SHUTDOWN ) {
309 zlog_warn("IRDP: Interface is already shutdown %s", ifp->name);
310 return;
313 irdp->flags |= IF_SHUTDOWN;
314 irdp->flags &= ~IF_ACTIVE;
316 if(! (irdp->flags & IF_BROADCAST))
317 if_drop_group(ifp);
319 /* Tell the hosts we are out of service */
320 irdp_advert_off(ifp);
323 static void
324 irdp_if_no_shutdown(struct interface *ifp)
326 struct zebra_if *zi= ifp->info;
327 struct irdp_interface *irdp = &zi->irdp;
329 if (! (irdp->flags & IF_SHUTDOWN )) {
330 zlog_warn("IRDP: Interface is not shutdown %s", ifp->name);
331 return;
334 irdp->flags &= ~IF_SHUTDOWN;
336 irdp_if_start(ifp, irdp->flags & IF_BROADCAST? FALSE : TRUE, FALSE);
341 /* Write configuration to user */
343 void irdp_config_write (struct vty *vty, struct interface *ifp)
345 struct zebra_if *zi=ifp->info;
346 struct irdp_interface *irdp=&zi->irdp;
347 struct Adv *adv;
348 struct listnode *node;
349 char b1[INET_ADDRSTRLEN];
351 if(irdp->flags & IF_ACTIVE || irdp->flags & IF_SHUTDOWN) {
353 if( irdp->flags & IF_SHUTDOWN)
354 vty_out (vty, " ip irdp shutdown %s", VTY_NEWLINE);
356 if( irdp->flags & IF_BROADCAST)
357 vty_out (vty, " ip irdp broadcast%s", VTY_NEWLINE);
358 else
359 vty_out (vty, " ip irdp multicast%s", VTY_NEWLINE);
361 vty_out (vty, " ip irdp preference %ld%s",
362 irdp->Preference, VTY_NEWLINE);
364 for (ALL_LIST_ELEMENTS_RO (irdp->AdvPrefList, node, adv))
365 vty_out (vty, " ip irdp address %s preference %d%s",
366 inet_2a(adv->ip.s_addr, b1),
367 adv->pref,
368 VTY_NEWLINE);
370 vty_out (vty, " ip irdp holdtime %d%s",
371 irdp->Lifetime, VTY_NEWLINE);
373 vty_out (vty, " ip irdp minadvertinterval %ld%s",
374 irdp->MinAdvertInterval, VTY_NEWLINE);
376 vty_out (vty, " ip irdp maxadvertinterval %ld%s",
377 irdp->MaxAdvertInterval, VTY_NEWLINE);
383 DEFUN (ip_irdp_multicast,
384 ip_irdp_multicast_cmd,
385 "ip irdp multicast",
386 IP_STR
387 "ICMP Router discovery on this interface using multicast\n")
389 struct interface *ifp;
391 ifp = (struct interface *) vty->index;
392 if(!ifp) {
393 return CMD_WARNING;
396 irdp_if_start(ifp, TRUE, TRUE);
397 return CMD_SUCCESS;
400 DEFUN (ip_irdp_broadcast,
401 ip_irdp_broadcast_cmd,
402 "ip irdp broadcast",
403 IP_STR
404 "ICMP Router discovery on this interface using broadcast\n")
406 struct interface *ifp;
408 ifp = (struct interface *) vty->index;
409 if(!ifp) {
410 return CMD_WARNING;
413 irdp_if_start(ifp, FALSE, TRUE);
414 return CMD_SUCCESS;
417 DEFUN (no_ip_irdp,
418 no_ip_irdp_cmd,
419 "no ip irdp",
420 NO_STR
421 IP_STR
422 "Disable ICMP Router discovery on this interface\n")
424 struct interface *ifp;
426 ifp = (struct interface *) vty->index;
427 if(!ifp) {
428 return CMD_WARNING;
431 irdp_if_stop(ifp);
432 return CMD_SUCCESS;
435 DEFUN (ip_irdp_shutdown,
436 ip_irdp_shutdown_cmd,
437 "ip irdp shutdown",
438 IP_STR
439 "ICMP Router discovery shutdown on this interface\n")
441 struct interface *ifp;
443 ifp = (struct interface *) vty->index;
444 if(!ifp) {
445 return CMD_WARNING;
448 irdp_if_shutdown(ifp);
449 return CMD_SUCCESS;
452 DEFUN (no_ip_irdp_shutdown,
453 no_ip_irdp_shutdown_cmd,
454 "no ip irdp shutdown",
455 NO_STR
456 IP_STR
457 "ICMP Router discovery no shutdown on this interface\n")
459 struct interface *ifp;
461 ifp = (struct interface *) vty->index;
462 if(!ifp) {
463 return CMD_WARNING;
466 irdp_if_no_shutdown(ifp);
467 return CMD_SUCCESS;
470 DEFUN (ip_irdp_holdtime,
471 ip_irdp_holdtime_cmd,
472 "ip irdp holdtime <0-9000>",
473 IP_STR
474 "ICMP Router discovery on this interface\n"
475 "Set holdtime value\n"
476 "Holdtime value in seconds. Default is 1800 seconds\n")
478 struct interface *ifp;
479 struct zebra_if *zi;
480 struct irdp_interface *irdp;
481 ifp = (struct interface *) vty->index;
482 if(!ifp) {
483 return CMD_WARNING;
486 zi=ifp->info;
487 irdp=&zi->irdp;
489 irdp->Lifetime = atoi(argv[0]);
490 return CMD_SUCCESS;
493 DEFUN (ip_irdp_minadvertinterval,
494 ip_irdp_minadvertinterval_cmd,
495 "ip irdp minadvertinterval <3-1800>",
496 IP_STR
497 "ICMP Router discovery on this interface\n"
498 "Set minimum time between advertisement\n"
499 "Minimum advertisement interval in seconds\n")
501 struct interface *ifp;
502 struct zebra_if *zi;
503 struct irdp_interface *irdp;
504 ifp = (struct interface *) vty->index;
505 if(!ifp) {
506 return CMD_WARNING;
509 zi=ifp->info;
510 irdp=&zi->irdp;
512 if( (unsigned) atoi(argv[0]) <= irdp->MaxAdvertInterval) {
513 irdp->MinAdvertInterval = atoi(argv[0]);
515 return CMD_SUCCESS;
518 vty_out (vty, "ICMP warning maxadvertinterval is greater or equal than minadvertinterval%s",
519 VTY_NEWLINE);
521 vty_out (vty, "Please correct!%s",
522 VTY_NEWLINE);
523 return CMD_WARNING;
526 DEFUN (ip_irdp_maxadvertinterval,
527 ip_irdp_maxadvertinterval_cmd,
528 "ip irdp maxadvertinterval <4-1800>",
529 IP_STR
530 "ICMP Router discovery on this interface\n"
531 "Set maximum time between advertisement\n"
532 "Maximum advertisement interval in seconds\n")
534 struct interface *ifp;
535 struct zebra_if *zi;
536 struct irdp_interface *irdp;
537 ifp = (struct interface *) vty->index;
538 if(!ifp) {
539 return CMD_WARNING;
542 zi=ifp->info;
543 irdp=&zi->irdp;
546 if( irdp->MinAdvertInterval <= (unsigned) atoi(argv[0]) ) {
547 irdp->MaxAdvertInterval = atoi(argv[0]);
549 return CMD_SUCCESS;
552 vty_out (vty, "ICMP warning maxadvertinterval is greater or equal than minadvertinterval%s",
553 VTY_NEWLINE);
555 vty_out (vty, "Please correct!%s",
556 VTY_NEWLINE);
557 return CMD_WARNING;
560 /* DEFUN needs to be fixed for negative ranages...
561 * "ip irdp preference <-2147483648-2147483647>",
562 * Be positive for now. :-)
565 DEFUN (ip_irdp_preference,
566 ip_irdp_preference_cmd,
567 "ip irdp preference <0-2147483647>",
568 IP_STR
569 "ICMP Router discovery on this interface\n"
570 "Set default preference level for this interface\n"
571 "Preference level\n")
573 struct interface *ifp;
574 struct zebra_if *zi;
575 struct irdp_interface *irdp;
576 ifp = (struct interface *) vty->index;
577 if(!ifp) {
578 return CMD_WARNING;
581 zi=ifp->info;
582 irdp=&zi->irdp;
584 irdp->Preference = atoi(argv[0]);
585 return CMD_SUCCESS;
588 DEFUN (ip_irdp_address_preference,
589 ip_irdp_address_preference_cmd,
590 "ip irdp address A.B.C.D preference <0-2147483647>",
591 IP_STR
592 "Alter ICMP Router discovery preference this interface\n"
593 "Specify IRDP non-default preference to advertise\n"
594 "Set IRDP address for advertise\n"
595 "Preference level\n")
597 struct listnode *node;
598 struct in_addr ip;
599 int pref;
600 int ret;
601 struct interface *ifp;
602 struct zebra_if *zi;
603 struct irdp_interface *irdp;
604 struct Adv *adv;
606 ifp = (struct interface *) vty->index;
607 if(!ifp) {
608 return CMD_WARNING;
611 zi=ifp->info;
612 irdp=&zi->irdp;
614 ret = inet_aton(argv[0], &ip);
615 if(!ret) return CMD_WARNING;
617 pref = atoi(argv[1]);
619 for (ALL_LIST_ELEMENTS_RO (irdp->AdvPrefList, node, adv))
620 if(adv->ip.s_addr == ip.s_addr)
621 return CMD_SUCCESS;
623 adv = Adv_new();
624 adv->ip = ip;
625 adv->pref = pref;
626 listnode_add(irdp->AdvPrefList, adv);
628 return CMD_SUCCESS;
632 DEFUN (no_ip_irdp_address_preference,
633 no_ip_irdp_address_preference_cmd,
634 "no ip irdp address A.B.C.D preference <0-2147483647>",
635 NO_STR
636 IP_STR
637 "Alter ICMP Router discovery preference this interface\n"
638 "Removes IRDP non-default preference\n"
639 "Select IRDP address\n"
640 "Old preference level\n")
642 struct listnode *node, *nnode;
643 struct in_addr ip;
644 int pref;
645 int ret;
646 struct interface *ifp;
647 struct zebra_if *zi;
648 struct irdp_interface *irdp;
649 struct Adv *adv;
651 ifp = (struct interface *) vty->index;
652 if(!ifp) {
653 return CMD_WARNING;
656 zi=ifp->info;
657 irdp=&zi->irdp;
659 ret = inet_aton(argv[0], &ip);
660 if (!ret)
661 return CMD_WARNING;
663 pref = atoi(argv[1]);
665 for (ALL_LIST_ELEMENTS (irdp->AdvPrefList, node, nnode, adv))
667 if(adv->ip.s_addr == ip.s_addr )
669 listnode_delete(irdp->AdvPrefList, adv);
670 break;
674 return CMD_SUCCESS;
677 DEFUN (ip_irdp_debug_messages,
678 ip_irdp_debug_messages_cmd,
679 "ip irdp debug messages",
680 IP_STR
681 "ICMP Router discovery debug Averts. and Solicits (short)\n")
683 struct interface *ifp;
684 struct zebra_if *zi;
685 struct irdp_interface *irdp;
686 ifp = (struct interface *) vty->index;
687 if(!ifp) {
688 return CMD_WARNING;
691 zi=ifp->info;
692 irdp=&zi->irdp;
694 irdp->flags |= IF_DEBUG_MESSAGES;
696 return CMD_SUCCESS;
699 DEFUN (ip_irdp_debug_misc,
700 ip_irdp_debug_misc_cmd,
701 "ip irdp debug misc",
702 IP_STR
703 "ICMP Router discovery debug Averts. and Solicits (short)\n")
705 struct interface *ifp;
706 struct zebra_if *zi;
707 struct irdp_interface *irdp;
708 ifp = (struct interface *) vty->index;
709 if(!ifp) {
710 return CMD_WARNING;
713 zi=ifp->info;
714 irdp=&zi->irdp;
716 irdp->flags |= IF_DEBUG_MISC;
718 return CMD_SUCCESS;
721 DEFUN (ip_irdp_debug_packet,
722 ip_irdp_debug_packet_cmd,
723 "ip irdp debug packet",
724 IP_STR
725 "ICMP Router discovery debug Averts. and Solicits (short)\n")
727 struct interface *ifp;
728 struct zebra_if *zi;
729 struct irdp_interface *irdp;
730 ifp = (struct interface *) vty->index;
731 if(!ifp) {
732 return CMD_WARNING;
735 zi=ifp->info;
736 irdp=&zi->irdp;
738 irdp->flags |= IF_DEBUG_PACKET;
740 return CMD_SUCCESS;
744 DEFUN (ip_irdp_debug_disable,
745 ip_irdp_debug_disable_cmd,
746 "ip irdp debug disable",
747 IP_STR
748 "ICMP Router discovery debug Averts. and Solicits (short)\n")
750 struct interface *ifp;
751 struct zebra_if *zi;
752 struct irdp_interface *irdp;
753 ifp = (struct interface *) vty->index;
754 if(!ifp) {
755 return CMD_WARNING;
758 zi=ifp->info;
759 irdp=&zi->irdp;
761 irdp->flags &= ~IF_DEBUG_PACKET;
762 irdp->flags &= ~IF_DEBUG_MESSAGES;
763 irdp->flags &= ~IF_DEBUG_MISC;
765 return CMD_SUCCESS;
768 void
769 irdp_init ()
771 install_element (INTERFACE_NODE, &ip_irdp_broadcast_cmd);
772 install_element (INTERFACE_NODE, &ip_irdp_multicast_cmd);
773 install_element (INTERFACE_NODE, &no_ip_irdp_cmd);
774 install_element (INTERFACE_NODE, &ip_irdp_shutdown_cmd);
775 install_element (INTERFACE_NODE, &no_ip_irdp_shutdown_cmd);
776 install_element (INTERFACE_NODE, &ip_irdp_holdtime_cmd);
777 install_element (INTERFACE_NODE, &ip_irdp_maxadvertinterval_cmd);
778 install_element (INTERFACE_NODE, &ip_irdp_minadvertinterval_cmd);
779 install_element (INTERFACE_NODE, &ip_irdp_preference_cmd);
780 install_element (INTERFACE_NODE, &ip_irdp_address_preference_cmd);
781 install_element (INTERFACE_NODE, &no_ip_irdp_address_preference_cmd);
783 install_element (INTERFACE_NODE, &ip_irdp_debug_messages_cmd);
784 install_element (INTERFACE_NODE, &ip_irdp_debug_misc_cmd);
785 install_element (INTERFACE_NODE, &ip_irdp_debug_packet_cmd);
786 install_element (INTERFACE_NODE, &ip_irdp_debug_disable_cmd);
789 #endif /* HAVE_IRDP */