MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / zebra / zebra / irdp_interface.c
blob4f63f78fb51d9c85c6c053af217027c99f22bf1b
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 thread_master *master;
68 int in_cksum (void *ptr, int nbytes);
69 extern int irdp_sock;
70 int irdp_send_thread(struct thread *t_advert);
71 char *inet_2a(u_int32_t a, char *b);
72 void irdp_advert_off(struct interface *ifp);
75 char b1[16], b2[16], b3[16], b4[16]; /* For inet_2a */
77 struct prefix *irdp_get_prefix(struct interface *ifp)
79 listnode node;
80 struct connected *ifc;
82 if(ifp->connected)
83 for (node = listhead (ifp->connected); node; nextnode (node)) {
84 ifc = getdata (node);
85 return ifc->address;
87 return NULL;
90 /* Join to the add/leave multicast group. */
91 int if_group (struct interface *ifp,
92 int sock,
93 u_int32_t group,
94 int add_leave)
96 struct zebra_if *zi;
97 struct ip_mreq m;
98 struct prefix *p;
99 int ret;
101 zi = ifp->info;
103 bzero (&m, sizeof (m));
104 m.imr_multiaddr.s_addr = htonl (group);
105 p = irdp_get_prefix(ifp);
107 if(!p) {
108 zlog_warn ("IRDP: can't get address for %s", ifp->name);
109 return 1;
112 m.imr_interface = p->u.prefix4;
114 ret = setsockopt (sock, IPPROTO_IP, add_leave,
115 (char *) &m, sizeof (struct ip_mreq));
116 if (ret < 0)
117 zlog_warn ("IRDP: %s can't setsockopt %s: %s",
118 add_leave == IP_ADD_MEMBERSHIP? "join group":"leave group",
119 inet_2a(group, b1),
120 strerror (errno));
122 return ret;
125 int if_add_group (struct interface *ifp)
127 struct zebra_if *zi= ifp->info;
128 struct irdp_interface *irdp = &zi->irdp;
129 int ret;
131 ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_ADD_MEMBERSHIP);
132 if (ret < 0) {
133 return ret;
136 if(irdp->flags & IF_DEBUG_MISC )
137 zlog_warn("IRDP: Adding group %s for %s\n",
138 inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
139 ifp->name);
140 return 0;
142 int if_drop_group (struct interface *ifp)
144 struct zebra_if *zi= ifp->info;
145 struct irdp_interface *irdp = &zi->irdp;
146 int ret;
148 ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_DROP_MEMBERSHIP);
149 if (ret < 0)
150 return ret;
152 if(irdp->flags & IF_DEBUG_MISC)
153 zlog_warn("IRDP: Leaving group %s for %s\n",
154 inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
155 ifp->name);
156 return 0;
159 struct interface *get_iflist_ifp(int idx)
161 listnode node;
162 struct interface *ifp;
164 for (node = listhead (iflist); node; nextnode (node)) {
165 ifp = getdata (node);
166 if(ifp->ifindex == idx) return ifp;
168 return NULL;
171 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 struct Adv *Adv_new ()
186 struct Adv *new;
187 new = XMALLOC (MTYPE_TMP, sizeof (struct Adv));
188 memset (new, 0, sizeof (struct Adv));
189 return new;
192 void Adv_free (struct Adv *adv)
194 XFREE (MTYPE_TMP, adv);
197 void irdp_if_start(struct interface *ifp, int multicast, int set_defaults)
199 struct zebra_if *zi= ifp->info;
200 struct irdp_interface *irdp = &zi->irdp;
201 listnode node;
202 u_int32_t timer, seed;
204 if (irdp->flags & IF_ACTIVE ) {
205 zlog_warn("IRDP: Interface is already active %s\n", ifp->name);
206 return;
208 irdp->flags |= IF_ACTIVE;
210 if(!multicast)
211 irdp->flags |= IF_BROADCAST;
213 if_add_update(ifp);
215 if (! (ifp->flags & IFF_UP)) {
216 zlog_warn("IRDP: Interface is down %s\n", ifp->name);
219 /* Shall we cancel if_start if if_add_group fails? */
221 if( multicast) {
222 if_add_group(ifp);
224 if (! (ifp->flags & (IFF_MULTICAST|IFF_ALLMULTI))) {
225 zlog_warn("IRDP: Interface not multicast enabled %s\n", ifp->name);
229 if(set_defaults)
230 if_set_defaults(ifp);
232 irdp->irdp_sent = 0;
234 /* The spec suggests this for randomness */
236 seed = 0;
237 if( ifp->connected)
238 for (node = listhead (ifp->connected); node; nextnode (node))
240 struct connected *ifc = getdata (node);
241 seed = ifc->address->u.prefix4.s_addr;
244 srandom(seed);
245 timer = (random () % IRDP_DEFAULT_INTERVAL) + 1;
247 irdp->AdvPrefList = list_new();
248 irdp->AdvPrefList->del = (void *) Adv_free; /* Destructor */
251 /* And this for startup. Speed limit from 1991 :-). But it's OK*/
253 if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
254 timer > MAX_INITIAL_ADVERT_INTERVAL )
255 timer= MAX_INITIAL_ADVERT_INTERVAL;
258 if(irdp->flags & IF_DEBUG_MISC)
259 zlog_warn("IRDP: Init timer for %s set to %u\n",
260 ifp->name,
261 timer);
263 irdp->t_advertise = thread_add_timer(master,
264 irdp_send_thread,
265 ifp,
266 timer);
269 void irdp_if_stop(struct interface *ifp)
271 struct zebra_if *zi=ifp->info;
272 struct irdp_interface *irdp=&zi->irdp;
274 if (irdp == NULL) {
275 zlog_warn ("Interface %s structure is NULL", ifp->name);
276 return;
279 if (! (irdp->flags & IF_ACTIVE )) {
280 zlog_warn("Interface is not active %s\n", ifp->name);
281 return;
284 if(! (irdp->flags & IF_BROADCAST))
285 if_drop_group(ifp);
287 irdp_advert_off(ifp);
289 list_delete(irdp->AdvPrefList);
290 irdp->AdvPrefList=NULL;
292 irdp->flags = 0;
296 void irdp_if_shutdown(struct interface *ifp)
298 struct zebra_if *zi= ifp->info;
299 struct irdp_interface *irdp = &zi->irdp;
301 if (irdp->flags & IF_SHUTDOWN ) {
302 zlog_warn("IRDP: Interface is already shutdown %s\n", ifp->name);
303 return;
306 irdp->flags |= IF_SHUTDOWN;
307 irdp->flags &= ~IF_ACTIVE;
309 if(! (irdp->flags & IF_BROADCAST))
310 if_drop_group(ifp);
312 /* Tell the hosts we are out of service */
313 irdp_advert_off(ifp);
316 void irdp_if_no_shutdown(struct interface *ifp)
318 struct zebra_if *zi= ifp->info;
319 struct irdp_interface *irdp = &zi->irdp;
321 if (! (irdp->flags & IF_SHUTDOWN )) {
322 zlog_warn("IRDP: Interface is not shutdown %s\n", ifp->name);
323 return;
326 irdp->flags &= ~IF_SHUTDOWN;
328 irdp_if_start(ifp, irdp->flags & IF_BROADCAST? FALSE : TRUE, FALSE);
333 /* Write configuration to user */
335 void irdp_config_write (struct vty *vty, struct interface *ifp)
337 struct zebra_if *zi=ifp->info;
338 struct irdp_interface *irdp=&zi->irdp;
339 struct Adv *adv;
340 listnode node;
342 if(irdp->flags & IF_ACTIVE || irdp->flags & IF_SHUTDOWN) {
344 if( irdp->flags & IF_SHUTDOWN)
345 vty_out (vty, " ip irdp shutdown %s", VTY_NEWLINE);
347 if( irdp->flags & IF_BROADCAST)
348 vty_out (vty, " ip irdp broadcast%s", VTY_NEWLINE);
349 else
350 vty_out (vty, " ip irdp multicast%s", VTY_NEWLINE);
352 vty_out (vty, " ip irdp preference %ld%s",
353 irdp->Preference, VTY_NEWLINE);
355 for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) {
356 adv = getdata (node);
357 vty_out (vty, " ip irdp address %s preference %d%s",
358 inet_2a(adv->ip.s_addr, b1),
359 adv->pref,
360 VTY_NEWLINE);
364 vty_out (vty, " ip irdp holdtime %d%s",
365 irdp->Lifetime, VTY_NEWLINE);
367 vty_out (vty, " ip irdp minadvertinterval %ld%s",
368 irdp->MinAdvertInterval, VTY_NEWLINE);
370 vty_out (vty, " ip irdp maxadvertinterval %ld%s",
371 irdp->MaxAdvertInterval, VTY_NEWLINE);
377 DEFUN (ip_irdp_multicast,
378 ip_irdp_multicast_cmd,
379 "ip irdp multicast",
380 IP_STR
381 "ICMP Router discovery on this interface using multicast\n")
383 struct interface *ifp;
385 ifp = (struct interface *) vty->index;
386 if(!ifp) {
387 return CMD_WARNING;
390 irdp_if_start(ifp, TRUE, TRUE);
391 return CMD_SUCCESS;
394 DEFUN (ip_irdp_broadcast,
395 ip_irdp_broadcast_cmd,
396 "ip irdp broadcast",
397 IP_STR
398 "ICMP Router discovery on this interface using broadcast\n")
400 struct interface *ifp;
402 ifp = (struct interface *) vty->index;
403 if(!ifp) {
404 return CMD_WARNING;
407 irdp_if_start(ifp, FALSE, TRUE);
408 return CMD_SUCCESS;
411 DEFUN (ip_irdp_no,
412 ip_irdp_cmd_no,
413 "no ip irdp",
414 IP_STR
415 "Disable ICMP Router discovery on this interface\n")
417 struct interface *ifp;
419 ifp = (struct interface *) vty->index;
420 if(!ifp) {
421 return CMD_WARNING;
424 irdp_if_stop(ifp);
425 return CMD_SUCCESS;
428 DEFUN (ip_irdp_shutdown,
429 ip_irdp_shutdown_cmd,
430 "ip irdp shutdown",
431 IP_STR
432 "ICMP Router discovery shutdown on this interface\n")
434 struct interface *ifp;
436 ifp = (struct interface *) vty->index;
437 if(!ifp) {
438 return CMD_WARNING;
441 irdp_if_shutdown(ifp);
442 return CMD_SUCCESS;
445 DEFUN (ip_irdp_no_shutdown,
446 ip_irdp_no_shutdown_cmd,
447 "no ip irdp shutdown",
448 IP_STR
449 "ICMP Router discovery no shutdown on this interface\n")
451 struct interface *ifp;
453 ifp = (struct interface *) vty->index;
454 if(!ifp) {
455 return CMD_WARNING;
458 irdp_if_no_shutdown(ifp);
459 return CMD_SUCCESS;
462 DEFUN (ip_irdp_holdtime,
463 ip_irdp_holdtime_cmd,
464 "ip irdp holdtime <0-9000>",
465 IP_STR
466 "ICMP Router discovery on this interface\n"
467 "Set holdtime value\n"
468 "Holdtime value in seconds. Default is 1800 seconds\n")
470 struct interface *ifp;
471 struct zebra_if *zi;
472 struct irdp_interface *irdp;
473 ifp = (struct interface *) vty->index;
474 if(!ifp) {
475 return CMD_WARNING;
478 zi=ifp->info;
479 irdp=&zi->irdp;
481 irdp->Lifetime = atoi(argv[0]);
482 return CMD_SUCCESS;
485 DEFUN (ip_irdp_minadvertinterval,
486 ip_irdp_minadvertinterval_cmd,
487 "ip irdp minadvertinterval <3-1800>",
488 IP_STR
489 "ICMP Router discovery on this interface\n"
490 "Set minimum time between advertisement\n"
491 "Minimum advertisement interval in seconds\n")
493 struct interface *ifp;
494 struct zebra_if *zi;
495 struct irdp_interface *irdp;
496 ifp = (struct interface *) vty->index;
497 if(!ifp) {
498 return CMD_WARNING;
501 zi=ifp->info;
502 irdp=&zi->irdp;
504 if( atoi(argv[0]) <= irdp->MaxAdvertInterval) {
505 irdp->MinAdvertInterval = atoi(argv[0]);
507 return CMD_SUCCESS;
510 vty_out (vty, "ICMP warning maxadvertinterval is greater or equal than minadvertinterval%s",
511 VTY_NEWLINE);
513 vty_out (vty, "Please correct!%s",
514 VTY_NEWLINE);
515 return CMD_WARNING;
518 DEFUN (ip_irdp_maxadvertinterval,
519 ip_irdp_maxadvertinterval_cmd,
520 "ip irdp maxadvertinterval <4-1800>",
521 IP_STR
522 "ICMP Router discovery on this interface\n"
523 "Set maximum time between advertisement\n"
524 "Maximum advertisement interval in seconds\n")
526 struct interface *ifp;
527 struct zebra_if *zi;
528 struct irdp_interface *irdp;
529 ifp = (struct interface *) vty->index;
530 if(!ifp) {
531 return CMD_WARNING;
534 zi=ifp->info;
535 irdp=&zi->irdp;
538 if( irdp->MinAdvertInterval <= atoi(argv[0]) ) {
539 irdp->MaxAdvertInterval = atoi(argv[0]);
541 return CMD_SUCCESS;
544 vty_out (vty, "ICMP warning maxadvertinterval is greater or equal than minadvertinterval%s",
545 VTY_NEWLINE);
547 vty_out (vty, "Please correct!%s",
548 VTY_NEWLINE);
549 return CMD_WARNING;
552 DEFUN (ip_irdp_preference,
553 ip_irdp_preference_cmd,
555 /* DEFUN needs to be fixed for negative ranages...
556 Be positive for now. :-)
558 "ip irdp preference <-2147483648-2147483647>",
562 "ip irdp preference <0-2147483647>",
563 IP_STR
564 "ICMP Router discovery on this interface\n"
565 "Set default preference level for this interface\n"
566 "Preference level\n")
568 struct interface *ifp;
569 struct zebra_if *zi;
570 struct irdp_interface *irdp;
571 ifp = (struct interface *) vty->index;
572 if(!ifp) {
573 return CMD_WARNING;
576 zi=ifp->info;
577 irdp=&zi->irdp;
579 irdp->Preference = atoi(argv[0]);
580 return CMD_SUCCESS;
583 DEFUN (ip_irdp_address_preference,
584 ip_irdp_address_preference_cmd,
585 "ip irdp address A.B.C.D preference <0-2147483647>",
586 IP_STR
587 "Alter ICMP Router discovery preference this interface\n"
588 "Specify IRDP non-default preference to advertise\n"
589 "Set IRDP address for advertise\n"
590 "Preference level\n")
592 listnode node;
593 struct in_addr ip;
594 int pref;
595 int ret;
596 struct interface *ifp;
597 struct zebra_if *zi;
598 struct irdp_interface *irdp;
599 struct Adv *adv;
601 ifp = (struct interface *) vty->index;
602 if(!ifp) {
603 return CMD_WARNING;
606 zi=ifp->info;
607 irdp=&zi->irdp;
609 ret = inet_aton(argv[0], &ip);
610 if(!ret) return CMD_WARNING;
612 pref = atoi(argv[1]);
614 for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) {
615 adv = getdata (node);
616 if(adv->ip.s_addr == ip.s_addr) return CMD_SUCCESS;
619 adv = Adv_new();
620 adv->ip = ip;
621 adv->pref = pref;
622 listnode_add(irdp->AdvPrefList, adv);
624 return CMD_SUCCESS;
628 DEFUN (ip_irdp_address_preference_no,
629 ip_irdp_address_preference_cmd_no,
630 "no ip irdp address A.B.C.D preference <0-2147483647>",
631 IP_STR
632 "Alter ICMP Router discovery preference this interface\n"
633 "Removes IRDP non-default preference\n"
634 "Select IRDP address\n"
635 "Old preference level\n")
637 listnode node;
638 struct in_addr ip;
639 int pref;
640 int ret;
641 struct interface *ifp;
642 struct zebra_if *zi;
643 struct irdp_interface *irdp;
644 struct Adv *adv;
646 ifp = (struct interface *) vty->index;
647 if(!ifp) {
648 return CMD_WARNING;
651 zi=ifp->info;
652 irdp=&zi->irdp;
654 ret = inet_aton(argv[0], &ip);
655 if(!ret) return CMD_WARNING;
657 pref = atoi(argv[1]);
659 for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) {
660 adv = getdata (node);
661 if(adv->ip.s_addr == ip.s_addr ) {
662 listnode_delete(irdp->AdvPrefList, adv);
663 break;
667 return CMD_SUCCESS;
672 DEFUN (ip_irdp_debug_messages,
673 ip_irdp_debug_messages_cmd,
674 "ip irdp debug messages",
675 IP_STR
676 "ICMP Router discovery debug Averts. and Solicits (short)\n")
678 struct interface *ifp;
679 struct zebra_if *zi;
680 struct irdp_interface *irdp;
681 ifp = (struct interface *) vty->index;
682 if(!ifp) {
683 return CMD_WARNING;
686 zi=ifp->info;
687 irdp=&zi->irdp;
689 irdp->flags |= IF_DEBUG_MESSAGES;
691 return CMD_SUCCESS;
694 DEFUN (ip_irdp_debug_misc,
695 ip_irdp_debug_misc_cmd,
696 "ip irdp debug misc",
697 IP_STR
698 "ICMP Router discovery debug Averts. and Solicits (short)\n")
700 struct interface *ifp;
701 struct zebra_if *zi;
702 struct irdp_interface *irdp;
703 ifp = (struct interface *) vty->index;
704 if(!ifp) {
705 return CMD_WARNING;
708 zi=ifp->info;
709 irdp=&zi->irdp;
711 irdp->flags |= IF_DEBUG_MISC;
713 return CMD_SUCCESS;
716 DEFUN (ip_irdp_debug_packet,
717 ip_irdp_debug_packet_cmd,
718 "ip irdp debug packet",
719 IP_STR
720 "ICMP Router discovery debug Averts. and Solicits (short)\n")
722 struct interface *ifp;
723 struct zebra_if *zi;
724 struct irdp_interface *irdp;
725 ifp = (struct interface *) vty->index;
726 if(!ifp) {
727 return CMD_WARNING;
730 zi=ifp->info;
731 irdp=&zi->irdp;
733 irdp->flags |= IF_DEBUG_PACKET;
735 return CMD_SUCCESS;
739 DEFUN (ip_irdp_debug_disable,
740 ip_irdp_debug_disable_cmd,
741 "ip irdp debug disable",
742 IP_STR
743 "ICMP Router discovery debug Averts. and Solicits (short)\n")
745 struct interface *ifp;
746 struct zebra_if *zi;
747 struct irdp_interface *irdp;
748 ifp = (struct interface *) vty->index;
749 if(!ifp) {
750 return CMD_WARNING;
753 zi=ifp->info;
754 irdp=&zi->irdp;
756 irdp->flags &= ~IF_DEBUG_PACKET;
757 irdp->flags &= ~IF_DEBUG_MESSAGES;
758 irdp->flags &= ~IF_DEBUG_MISC;
760 return CMD_SUCCESS;
763 void
764 irdp_if_init ()
766 install_element (INTERFACE_NODE, &ip_irdp_broadcast_cmd);
767 install_element (INTERFACE_NODE, &ip_irdp_multicast_cmd);
768 install_element (INTERFACE_NODE, &ip_irdp_cmd_no);
769 install_element (INTERFACE_NODE, &ip_irdp_shutdown_cmd);
770 install_element (INTERFACE_NODE, &ip_irdp_no_shutdown_cmd);
771 install_element (INTERFACE_NODE, &ip_irdp_holdtime_cmd);
772 install_element (INTERFACE_NODE, &ip_irdp_maxadvertinterval_cmd);
773 install_element (INTERFACE_NODE, &ip_irdp_minadvertinterval_cmd);
774 install_element (INTERFACE_NODE, &ip_irdp_preference_cmd);
775 install_element (INTERFACE_NODE, &ip_irdp_address_preference_cmd);
776 install_element (INTERFACE_NODE, &ip_irdp_address_preference_cmd_no);
778 install_element (INTERFACE_NODE, &ip_irdp_debug_messages_cmd);
779 install_element (INTERFACE_NODE, &ip_irdp_debug_misc_cmd);
780 install_element (INTERFACE_NODE, &ip_irdp_debug_packet_cmd);
781 install_element (INTERFACE_NODE, &ip_irdp_debug_disable_cmd);
784 #endif /* HAVE_IRDP */