1 /* $NetBSD: rdisc.c,v 1.16 2006/03/18 19:55:49 christos Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_icmp.h>
42 __RCSID("$NetBSD: rdisc.c,v 1.16 2006/03/18 19:55:49 christos Exp $");
43 #elif defined(__FreeBSD__)
46 __RCSID("Revision: 2.23 ");
47 #ident "Revision: 2.23 "
50 /* router advertisement ICMP packet */
52 u_int8_t icmp_type
; /* type of message */
53 u_int8_t icmp_code
; /* type sub code */
54 u_int16_t icmp_cksum
; /* ones complement cksum of struct */
55 u_int8_t icmp_ad_num
; /* # of following router addresses */
56 u_int8_t icmp_ad_asize
; /* 2--words in each advertisement */
57 u_int16_t icmp_ad_life
; /* seconds of validity */
64 /* router solicitation ICMP packet */
66 u_int8_t icmp_type
; /* type of message */
67 u_int8_t icmp_code
; /* type sub code */
68 u_int16_t icmp_cksum
; /* ones complement cksum of struct */
79 int rdisc_sock
= -1; /* router-discovery raw socket */
80 struct interface
*rdisc_sock_mcast
; /* current multicast interface */
82 struct timeval rdisc_timer
;
83 int rdisc_ok
; /* using solicited route */
86 #define MAX_ADS 16 /* at least one per interface */
87 struct dr
{ /* accumulated advertisements */
88 struct interface
*dr_ifp
;
89 naddr dr_gate
; /* gateway */
90 time_t dr_ts
; /* when received */
91 time_t dr_life
; /* lifetime in host byte order */
92 n_long dr_recv_pref
; /* received but biased preference */
93 n_long dr_pref
; /* preference adjusted by metric */
94 } *cur_drp
, drs
[MAX_ADS
];
96 /* convert between signed, balanced around zero,
97 * and unsigned zero-based preferences */
98 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel)
99 #define UNSIGN_PREF(p) SIGN_PREF(p)
100 /* adjust unsigned preference by interface metric,
101 * without driving it to infinity */
102 #define PREF(p, ifp) ((n_long)(p) <= (n_long)((ifp)->int_metric \
103 + (ifp)->int_adj_outmetric) \
104 ? ((p) != 0 ? 1 : 0) \
105 : (p) - ((ifp)->int_metric + (ifp)->int_adj_outmetric))
107 static void rdisc_sort(void);
110 /* dump an ICMP Router Discovery Advertisement Message
113 trace_rdisc(const char *act
,
116 struct interface
*ifp
,
124 if (!TRACEPACKETS
|| ftrace
== 0)
129 if (p
->icmp
.icmp_type
== ICMP_ROUTERADVERT
) {
130 (void)fprintf(ftrace
, "%s Router Ad"
131 " from %s to %s via %s life=%d\n",
132 act
, naddr_ntoa(from
), naddr_ntoa(to
),
133 ifp
? ifp
->int_name
: "?",
134 ntohs(p
->ad
.icmp_ad_life
));
138 wp
= &p
->ad
.icmp_ad_info
[0].icmp_ad_addr
;
139 lim
= &wp
[(len
- sizeof(p
->ad
)) / sizeof(*wp
)];
140 for (i
= 0; i
< p
->ad
.icmp_ad_num
&& wp
<= lim
; i
++) {
141 (void)fprintf(ftrace
, "\t%s preference=%d",
142 naddr_ntoa(wp
[0]), (int)ntohl(wp
[1]));
143 wp
+= p
->ad
.icmp_ad_asize
;
145 (void)fputc('\n',ftrace
);
148 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
149 act
, naddr_ntoa(from
), naddr_ntoa(to
),
150 ifp
? ifp
->int_name
: "?",
151 (int)ntohl(p
->so
.icmp_so_rsvd
));
155 /* prepare Router Discovery socket.
160 if (rdisc_sock
< 0) {
161 rdisc_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
163 BADERR(1,"rdisc_sock = socket()");
164 fix_sock(rdisc_sock
,"rdisc_sock");
170 /* Pick multicast group for router-discovery socket
173 set_rdisc_mg(struct interface
*ifp
,
174 int on
) /* 0=turn it off */
178 if (rdisc_sock
< 0) {
179 /* Create the raw socket so that we can hear at least
180 * broadcast router discovery packets.
182 if ((ifp
->int_state
& IS_NO_RDISC
) == IS_NO_RDISC
188 if (!(ifp
->int_if_flags
& IFF_MULTICAST
)) {
189 ifp
->int_state
&= ~(IS_ALL_HOSTS
| IS_ALL_ROUTERS
);
194 if (ifp
->int_if_flags
& IFF_POINTOPOINT
)
197 memset(&m
, 0, sizeof(m
));
199 m
.imr_interface
.s_addr
= htonl(ifp
->int_index
);
201 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
206 || (ifp
->int_state
& IS_NO_ADV_IN
)
208 /* stop listening to advertisements
210 if (ifp
->int_state
& IS_ALL_HOSTS
) {
211 m
.imr_multiaddr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
212 if (setsockopt(rdisc_sock
, IPPROTO_IP
,
215 LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
216 ifp
->int_state
&= ~IS_ALL_HOSTS
;
219 } else if (!(ifp
->int_state
& IS_ALL_HOSTS
)) {
220 /* start listening to advertisements
222 m
.imr_multiaddr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
223 if (setsockopt(rdisc_sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
224 &m
, sizeof(m
)) < 0) {
225 LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
227 ifp
->int_state
|= IS_ALL_HOSTS
;
232 || (ifp
->int_state
& IS_NO_ADV_OUT
)
234 /* stop listening to solicitations
236 if (ifp
->int_state
& IS_ALL_ROUTERS
) {
237 m
.imr_multiaddr
.s_addr
=htonl(INADDR_ALLROUTERS_GROUP
);
238 if (setsockopt(rdisc_sock
, IPPROTO_IP
,
241 LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
242 ifp
->int_state
&= ~IS_ALL_ROUTERS
;
245 } else if (!(ifp
->int_state
& IS_ALL_ROUTERS
)) {
246 /* start hearing solicitations
248 m
.imr_multiaddr
.s_addr
=htonl(INADDR_ALLROUTERS_GROUP
);
249 if (setsockopt(rdisc_sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
250 &m
, sizeof(m
)) < 0) {
251 LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
253 ifp
->int_state
|= IS_ALL_ROUTERS
;
259 /* start supplying routes
264 struct interface
*ifp
;
270 trace_act("start supplying routes");
272 /* Forget discovered routes.
274 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
275 drp
->dr_recv_pref
= 0;
283 /* Do not start advertising until we have heard some RIP routes */
284 LIM_SEC(rdisc_timer
, now
.tv_sec
+MIN_WAITTIME
);
286 /* Switch router discovery multicast groups from soliciting
289 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
290 if (ifp
->int_state
& IS_BROKE
)
292 ifp
->int_rdisc_cnt
= 0;
293 ifp
->int_rdisc_timer
.tv_usec
= rdisc_timer
.tv_usec
;
294 ifp
->int_rdisc_timer
.tv_sec
= now
.tv_sec
+MIN_WAITTIME
;
295 set_rdisc_mg(ifp
, 1);
298 /* get rid of any redirects */
303 /* age discovered routes and find the best one
306 rdisc_age(naddr bad_gate
)
312 /* If only advertising, then do only that. */
314 /* If switching from client to server, get rid of old
323 /* If we are being told about a bad router,
324 * then age the discovered default route, and if there is
325 * no alternative, solicit a replacement.
328 /* Look for the bad discovered default route.
329 * Age it and note its interface.
331 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
335 /* When we find the bad router, then age the route
336 * to at most SUPPLY_INTERVAL.
337 * This is contrary to RFC 1256, but defends against
340 if (drp
->dr_gate
== bad_gate
) {
341 sec
= (now
.tv_sec
- drp
->dr_life
343 if (drp
->dr_ts
> sec
) {
344 trace_act("age 0.0.0.0 --> %s via %s",
345 naddr_ntoa(drp
->dr_gate
),
346 drp
->dr_ifp
->int_name
);
357 /* Delete old redirected routes to keep the kernel table small,
358 * and to prevent black holes. Check that the kernel table
359 * matches the daemon table (i.e. has the default route).
360 * But only if RIP is not running and we are not dealing with
361 * a bad gateway, since otherwise age() will be called.
363 if (rip_sock
< 0 && bad_gate
== 0)
368 /* Zap all routes discovered via an interface that has gone bad
369 * This should only be called when !(ifp->int_state & IS_ALIAS)
372 if_bad_rdisc(struct interface
*ifp
)
376 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
377 if (drp
->dr_ifp
!= ifp
)
379 drp
->dr_recv_pref
= 0;
384 /* make a note to re-solicit, turn RIP on or off, etc. */
385 rdisc_timer
.tv_sec
= 0;
389 /* mark an interface ok for router discovering.
392 if_ok_rdisc(struct interface
*ifp
)
394 set_rdisc_mg(ifp
, 1);
396 ifp
->int_rdisc_cnt
= 0;
397 ifp
->int_rdisc_timer
.tv_sec
= now
.tv_sec
+ (supplier
399 : MAX_SOLICITATION_DELAY
);
400 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
401 rdisc_timer
= ifp
->int_rdisc_timer
;
405 /* get rid of a dead discovered router
408 del_rdisc(struct dr
*drp
)
410 struct interface
*ifp
;
415 del_redirects(gate
= drp
->dr_gate
, 0);
420 /* Count the other discovered routes on the interface.
424 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
426 && drp
->dr_ifp
== ifp
)
430 /* If that was the last good discovered router on the interface,
431 * then solicit a new one.
432 * This is contrary to RFC 1256, but defends against black holes.
435 trace_act("discovered router %s via %s"
436 " is bad--have %d remaining",
437 naddr_ntoa(gate
), ifp
->int_name
, i
);
438 } else if (ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
) {
439 trace_act("last discovered router %s via %s"
440 " is bad--re-solicit",
441 naddr_ntoa(gate
), ifp
->int_name
);
442 ifp
->int_rdisc_cnt
= 0;
443 ifp
->int_rdisc_timer
.tv_sec
= 0;
446 trace_act("last discovered router %s via %s"
447 " is bad--wait to solicit",
448 naddr_ntoa(gate
), ifp
->int_name
);
453 /* Find the best discovered route,
454 * and discard stale routers.
459 struct dr
*drp
, *new_drp
;
462 struct interface
*ifp
;
467 /* Find the best discovered route.
470 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
475 /* Get rid of expired discovered routers.
477 if (drp
->dr_ts
+ drp
->dr_life
<= now
.tv_sec
) {
482 LIM_SEC(rdisc_timer
, drp
->dr_ts
+drp
->dr_life
+1);
484 /* Update preference with possibly changed interface
487 drp
->dr_pref
= PREF(drp
->dr_recv_pref
, ifp
);
489 /* Prefer the current route to prevent thrashing.
490 * Prefer shorter lifetimes to speed the detection of
492 * Avoid sick interfaces.
495 || (!((new_st
^ drp
->dr_ifp
->int_state
) & IS_SICK
)
496 && (new_pref
< drp
->dr_pref
497 || (new_pref
== drp
->dr_pref
499 || (new_drp
!= cur_drp
500 && new_drp
->dr_life
> drp
->dr_life
)))))
501 || ((new_st
& IS_SICK
)
502 && !(drp
->dr_ifp
->int_state
& IS_SICK
))) {
504 new_st
= drp
->dr_ifp
->int_state
;
505 new_pref
= drp
->dr_pref
;
509 /* switch to a better default route
511 if (new_drp
!= cur_drp
) {
512 rt
= rtget(RIP_DEFAULT
, 0);
514 /* Stop using discovered routes if they are all bad
517 trace_act("turn off Router Discovery client");
521 && (rt
->rt_state
& RS_RDISC
)) {
522 new = rt
->rt_spares
[0];
523 new.rts_metric
= HOPCNT_INFINITY
;
524 new.rts_time
= now
.tv_sec
- GARBAGE_TIME
;
525 rtchange(rt
, rt
->rt_state
& ~RS_RDISC
,
532 trace_act("turn on Router Discovery client"
534 naddr_ntoa(new_drp
->dr_gate
),
535 new_drp
->dr_ifp
->int_name
);
539 trace_act("switch Router Discovery from"
540 " %s via %s to %s via %s",
541 naddr_ntoa(cur_drp
->dr_gate
),
542 cur_drp
->dr_ifp
->int_name
,
543 naddr_ntoa(new_drp
->dr_gate
),
544 new_drp
->dr_ifp
->int_name
);
547 memset(&new, 0, sizeof(new));
548 new.rts_ifp
= new_drp
->dr_ifp
;
549 new.rts_gate
= new_drp
->dr_gate
;
550 new.rts_router
= new_drp
->dr_gate
;
551 new.rts_metric
= HOPCNT_INFINITY
-1;
552 new.rts_time
= now
.tv_sec
;
554 rtchange(rt
, rt
->rt_state
| RS_RDISC
, &new, 0);
556 rtadd(RIP_DEFAULT
, 0, RS_RDISC
, &new);
563 /* turn RIP on or off */
564 if (!rdisc_ok
|| rip_interfaces
> 1) {
572 /* handle a single address in an advertisement
577 n_long pref
, /* signed and in network order */
578 u_short life
, /* in host byte order */
579 struct interface
*ifp
)
581 static struct msg_limit bad_gate
;
582 struct dr
*drp
, *new_drp
;
585 if (gate
== RIP_DEFAULT
586 || !check_dst(gate
)) {
587 msglim(&bad_gate
, from
,"router %s advertising bad gateway %s",
593 /* ignore pointers to ourself and routes via unreachable networks
595 if (ifwithaddr(gate
, 1, 0) != 0) {
596 trace_pkt(" discard Router Discovery Ad pointing at us");
599 if (!on_net(gate
, ifp
->int_net
, ifp
->int_mask
)) {
600 trace_pkt(" discard Router Discovery Ad"
601 " toward unreachable net");
605 /* Convert preference to an unsigned value
606 * and later bias it by the metric of the interface.
608 pref
= UNSIGN_PREF(ntohl(pref
));
610 if (pref
== 0 || life
< MinMaxAdvertiseInterval
) {
615 for (new_drp
= 0, drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
616 /* accept new info for a familiar entry
618 if (drp
->dr_gate
== gate
) {
624 continue; /* do not worry about dead ads */
626 if (drp
->dr_ts
== 0) {
627 new_drp
= drp
; /* use unused entry */
629 } else if (new_drp
== 0) {
630 /* look for an entry worse than the new one to
633 if ((!(ifp
->int_state
& IS_SICK
)
634 && (drp
->dr_ifp
->int_state
& IS_SICK
))
635 || (pref
> drp
->dr_pref
636 && !((ifp
->int_state
^ drp
->dr_ifp
->int_state
)
640 } else if (new_drp
->dr_ts
!= 0) {
641 /* look for the least valuable entry to reuse
643 if ((!(new_drp
->dr_ifp
->int_state
& IS_SICK
)
644 && (drp
->dr_ifp
->int_state
& IS_SICK
))
645 || (new_drp
->dr_pref
> drp
->dr_pref
646 && !((new_drp
->dr_ifp
->int_state
647 ^ drp
->dr_ifp
->int_state
)
653 /* forget it if all of the current entries are better */
657 new_drp
->dr_ifp
= ifp
;
658 new_drp
->dr_gate
= gate
;
659 new_drp
->dr_ts
= now
.tv_sec
;
660 new_drp
->dr_life
= life
;
661 new_drp
->dr_recv_pref
= pref
;
662 /* bias functional preference by metric of the interface */
663 new_drp
->dr_pref
= PREF(pref
,ifp
);
665 /* after hearing a good advertisement, stop asking
667 if (!(ifp
->int_state
& IS_SICK
))
668 ifp
->int_rdisc_cnt
= MAX_SOLICITATIONS
;
672 /* Compute the IP checksum
673 * This assumes the packet is less than 32K long.
680 int nwords
= len
>> 1;
682 while (nwords
-- != 0)
688 /* end-around-carry */
689 sum
= (sum
>> 16) + (sum
& 0xffff);
695 /* Send a router discovery advertisement or solicitation ICMP packet.
698 send_rdisc(union ad_u
*p
,
700 struct interface
*ifp
,
701 naddr dst
, /* 0 or unicast destination */
702 int type
) /* 0=unicast, 1=bcast, 2=mcast */
704 struct sockaddr_in rsin
;
710 memset(&rsin
, 0, sizeof(rsin
));
711 rsin
.sin_addr
.s_addr
= dst
;
712 rsin
.sin_family
= AF_INET
;
714 rsin
.sin_len
= sizeof(rsin
);
716 flags
= MSG_DONTROUTE
;
719 case 0: /* unicast */
724 case 1: /* broadcast */
725 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
726 msg
= "Send pt-to-pt";
727 rsin
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
729 msg
= "Send broadcast";
730 rsin
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
734 case 2: /* multicast */
735 msg
= "Send multicast";
736 if (ifp
->int_state
& IS_DUP
) {
737 trace_act("abort multicast output via %s"
738 " with duplicate address",
742 if (rdisc_sock_mcast
!= ifp
) {
743 /* select the right interface. */
745 /* specify ifindex */
746 tgt_mcast
= htonl(ifp
->int_index
);
749 /* Do not specify the primary interface explicitly
750 * if we have the multicast point-to-point kernel
751 * bug, since the kernel will do the wrong thing
752 * if the local address of a point-to-point link
753 * is the same as the address of an ordinary
756 if (ifp
->int_addr
== myaddr
) {
760 tgt_mcast
= ifp
->int_addr
;
762 if (0 > setsockopt(rdisc_sock
,
763 IPPROTO_IP
, IP_MULTICAST_IF
,
764 &tgt_mcast
, sizeof(tgt_mcast
))) {
765 LOGERR("setsockopt(rdisc_sock,"
767 rdisc_sock_mcast
= 0;
770 rdisc_sock_mcast
= ifp
;
779 trace_rdisc(msg
, ifp
? ifp
->int_addr
: 0, rsin
.sin_addr
.s_addr
, ifp
,
782 if (0 > sendto(rdisc_sock
, p
, p_size
, flags
,
783 (struct sockaddr
*)&rsin
, sizeof(rsin
))) {
784 if (ifp
== 0 || !(ifp
->int_state
& IS_BROKE
))
785 msglog("sendto(%s%s%s): %s",
786 ifp
!= 0 ? ifp
->int_name
: "",
787 ifp
!= 0 ? ", " : "",
788 inet_ntoa(rsin
.sin_addr
),
796 /* Send an advertisement
799 send_adv(struct interface
*ifp
,
800 naddr dst
, /* 0 or unicast destination */
801 int type
) /* 0=unicast, 1=bcast, 2=mcast */
807 memset(&u
, 0, sizeof(u
.ad
));
809 u
.ad
.icmp_type
= ICMP_ROUTERADVERT
;
810 u
.ad
.icmp_ad_num
= 1;
811 u
.ad
.icmp_ad_asize
= sizeof(u
.ad
.icmp_ad_info
[0])/4;
813 u
.ad
.icmp_ad_life
= stopint
? 0 : htons(ifp
->int_rdisc_int
*3);
815 /* Convert the configured preference to an unsigned value,
816 * bias it by the interface metric, and then send it as a
817 * signed, network byte order value.
819 pref
= UNSIGN_PREF(ifp
->int_rdisc_pref
);
820 u
.ad
.icmp_ad_info
[0].icmp_ad_pref
= htonl(SIGN_PREF(PREF(pref
, ifp
)));
822 u
.ad
.icmp_ad_info
[0].icmp_ad_addr
= ifp
->int_addr
;
824 u
.ad
.icmp_cksum
= in_cksum((u_short
*)&u
.ad
, sizeof(u
.ad
));
826 send_rdisc(&u
, sizeof(u
.ad
), ifp
, dst
, type
);
830 /* Advertise for Router Discovery
835 struct interface
*ifp
;
840 rdisc_timer
.tv_sec
= now
.tv_sec
+ NEVER
;
842 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
843 if (0 != (ifp
->int_state
& (IS_NO_ADV_OUT
| IS_BROKE
)))
846 if (!timercmp(&ifp
->int_rdisc_timer
, &now
, >)
848 send_adv(ifp
, htonl(INADDR_ALLHOSTS_GROUP
),
849 (ifp
->int_state
&IS_BCAST_RDISC
) ? 1 : 2);
850 ifp
->int_rdisc_cnt
++;
852 intvl_random(&ifp
->int_rdisc_timer
,
853 (ifp
->int_rdisc_int
*3)/4,
855 if (ifp
->int_rdisc_cnt
< MAX_INITIAL_ADVERTS
856 && (ifp
->int_rdisc_timer
.tv_sec
857 > MAX_INITIAL_ADVERT_INTERVAL
)) {
858 ifp
->int_rdisc_timer
.tv_sec
859 = MAX_INITIAL_ADVERT_INTERVAL
;
861 timevaladd(&ifp
->int_rdisc_timer
, &now
);
864 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
865 rdisc_timer
= ifp
->int_rdisc_timer
;
870 /* Solicit for Router Discovery
875 struct interface
*ifp
;
882 rdisc_timer
.tv_sec
= now
.tv_sec
+ NEVER
;
884 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
885 if (0 != (ifp
->int_state
& (IS_NO_SOL_OUT
| IS_BROKE
))
886 || ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
)
889 if (!timercmp(&ifp
->int_rdisc_timer
, &now
, >)) {
890 memset(&u
, 0, sizeof(u
.so
));
891 u
.so
.icmp_type
= ICMP_ROUTERSOLICIT
;
892 u
.so
.icmp_cksum
= in_cksum((u_short
*)&u
.so
,
894 send_rdisc(&u
, sizeof(u
.so
), ifp
,
895 htonl(INADDR_ALLROUTERS_GROUP
),
896 ((ifp
->int_state
&IS_BCAST_RDISC
) ? 1 : 2));
898 if (++ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
)
901 ifp
->int_rdisc_timer
.tv_sec
= SOLICITATION_INTERVAL
;
902 ifp
->int_rdisc_timer
.tv_usec
= 0;
903 timevaladd(&ifp
->int_rdisc_timer
, &now
);
906 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
907 rdisc_timer
= ifp
->int_rdisc_timer
;
912 /* check the IP header of a possible Router Discovery ICMP packet */
913 static struct interface
* /* 0 if bad */
914 ck_icmp(const char *act
,
916 struct interface
*ifp
,
924 if (p
->icmp
.icmp_type
== ICMP_ROUTERADVERT
) {
925 type
= "advertisement";
926 } else if (p
->icmp
.icmp_type
== ICMP_ROUTERSOLICIT
) {
927 type
= "solicitation";
932 if (p
->icmp
.icmp_code
!= 0) {
933 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
934 type
, p
->icmp
.icmp_code
,
935 naddr_ntoa(from
), naddr_ntoa(to
));
939 trace_rdisc(act
, from
, to
, ifp
, p
, len
);
942 trace_pkt("unknown interface for router-discovery %s"
944 type
, naddr_ntoa(from
), naddr_ntoa(to
));
950 /* read packets from the router discovery socket
955 static struct msg_limit bad_asize
, bad_len
;
956 #ifdef USE_PASSIFNAME
957 static struct msg_limit bad_name
;
959 struct sockaddr_in from
;
963 #ifdef USE_PASSIFNAME
964 char ifname
[IFNAMSIZ
];
974 struct interface
*ifp
;
978 fromlen
= sizeof(from
);
979 cc
= recvfrom(rdisc_sock
, &buf
, sizeof(buf
), 0,
980 (struct sockaddr
*)&from
,
983 if (cc
< 0 && errno
!= EWOULDBLOCK
)
984 LOGERR("recvfrom(rdisc_sock)");
987 if (fromlen
!= sizeof(struct sockaddr_in
))
988 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
990 #ifdef USE_PASSIFNAME
991 if ((cc
-= sizeof(buf
.ifname
)) < 0)
992 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
993 cc
+sizeof(buf
.ifname
));
996 hlen
= buf
.pkt
.ip
.ip_hl
<< 2;
997 if (cc
< hlen
+ ICMP_MINLEN
)
999 p
= (union ad_u
*)&buf
.pkt
.b
[hlen
];
1002 #ifdef USE_PASSIFNAME
1003 ifp
= ifwithname(buf
.ifname
, 0);
1005 msglim(&bad_name
, from
.sin_addr
.s_addr
,
1006 "impossible rdisc if_ name %.*s",
1007 IFNAMSIZ
, buf
.ifname
);
1009 /* If we could tell the interface on which a packet from
1010 * address 0 arrived, we could deal with such solicitations.
1012 ifp
= ((from
.sin_addr
.s_addr
== 0)
1013 ? 0 : iflookup(from
.sin_addr
.s_addr
));
1015 ifp
= ck_icmp("Recv", from
.sin_addr
.s_addr
, ifp
,
1016 buf
.pkt
.ip
.ip_dst
.s_addr
, p
, cc
);
1019 if (ifwithaddr(from
.sin_addr
.s_addr
, 0, 0)) {
1021 "discard our own Router Discovery message");
1025 switch (p
->icmp
.icmp_type
) {
1026 case ICMP_ROUTERADVERT
:
1027 if (p
->ad
.icmp_ad_asize
*4
1028 < (int)sizeof(p
->ad
.icmp_ad_info
[0])) {
1029 msglim(&bad_asize
, from
.sin_addr
.s_addr
,
1030 "intolerable rdisc address size=%d",
1031 p
->ad
.icmp_ad_asize
);
1034 if (p
->ad
.icmp_ad_num
== 0) {
1035 trace_pkt(" empty?");
1038 if (cc
!= (int)(sizeof(p
->ad
)
1039 - sizeof(p
->ad
.icmp_ad_info
)
1040 + (p
->ad
.icmp_ad_num
1041 * sizeof(p
->ad
.icmp_ad_info
[0])))) {
1042 msglim(&bad_len
, from
.sin_addr
.s_addr
,
1043 "rdisc length %d does not match ad_num"
1044 " %d", cc
, p
->ad
.icmp_ad_num
);
1049 if (ifp
->int_state
& IS_NO_ADV_IN
)
1052 wp
= &p
->ad
.icmp_ad_info
[0].icmp_ad_addr
;
1053 for (n
= 0; n
< p
->ad
.icmp_ad_num
; n
++) {
1054 parse_ad(from
.sin_addr
.s_addr
,
1056 ntohs(p
->ad
.icmp_ad_life
),
1058 wp
+= p
->ad
.icmp_ad_asize
;
1063 case ICMP_ROUTERSOLICIT
:
1066 if (ifp
->int_state
& IS_NO_ADV_OUT
)
1072 * We should handle messages from address 0.
1075 /* Respond with a point-to-point advertisement */
1076 send_adv(ifp
, from
.sin_addr
.s_addr
, 0);