3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sbin/routed/rdisc.c,v 1.5.2.1 2000/08/14 17:00:04 sheldonh Exp $
34 * $DragonFly: src/sbin/routed/rdisc.c,v 1.4 2005/03/16 21:21:34 cpressey Exp $
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_icmp.h>
42 #if !defined(sgi) && !defined(__NetBSD__)
43 static char sccsid
[] __attribute__((unused
)) = "@(#)rdisc.c 8.1 (Berkeley) x/y/95";
44 #elif defined(__NetBSD__)
48 /* router advertisement ICMP packet */
50 u_int8_t icmp_type
; /* type of message */
51 u_int8_t icmp_code
; /* type sub code */
52 u_int16_t icmp_cksum
; /* ones complement cksum of struct */
53 u_int8_t icmp_ad_num
; /* # of following router addresses */
54 u_int8_t icmp_ad_asize
; /* 2--words in each advertisement */
55 u_int16_t icmp_ad_life
; /* seconds of validity */
62 /* router solicitation ICMP packet */
64 u_int8_t icmp_type
; /* type of message */
65 u_int8_t icmp_code
; /* type sub code */
66 u_int16_t icmp_cksum
; /* ones complement cksum of struct */
77 int rdisc_sock
= -1; /* router-discovery raw socket */
78 struct interface
*rdisc_sock_mcast
; /* current multicast interface */
80 struct timeval rdisc_timer
;
81 int rdisc_ok
; /* using solicited route */
84 #define MAX_ADS 16 /* at least one per interface */
85 struct dr
{ /* accumulated advertisements */
86 struct interface
*dr_ifp
;
87 naddr dr_gate
; /* gateway */
88 time_t dr_ts
; /* when received */
89 time_t dr_life
; /* lifetime in host byte order */
90 n_long dr_recv_pref
; /* received but biased preference */
91 n_long dr_pref
; /* preference adjusted by metric */
92 } *cur_drp
, drs
[MAX_ADS
];
94 /* convert between signed, balanced around zero,
95 * and unsigned zero-based preferences */
96 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel)
97 #define UNSIGN_PREF(p) SIGN_PREF(p)
98 /* adjust unsigned preference by interface metric,
99 * without driving it to infinity */
100 #define PREF(p, ifp) ((int)(p) <= (ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
101 : (p) - ((ifp)->int_metric))
103 static void rdisc_sort(void);
106 /* dump an ICMP Router Discovery Advertisement Message
109 trace_rdisc(const char *act
,
112 struct interface
*ifp
,
120 if (!TRACEPACKETS
|| ftrace
== 0)
125 if (p
->icmp
.icmp_type
== ICMP_ROUTERADVERT
) {
126 fprintf(ftrace
, "%s Router Ad"
127 " from %s to %s via %s life=%d\n",
128 act
, naddr_ntoa(from
), naddr_ntoa(to
),
129 ifp
? ifp
->int_name
: "?",
130 ntohs(p
->ad
.icmp_ad_life
));
134 wp
= &p
->ad
.icmp_ad_info
[0].icmp_ad_addr
;
135 lim
= &wp
[(len
- sizeof(p
->ad
)) / sizeof(*wp
)];
136 for (i
= 0; i
< p
->ad
.icmp_ad_num
&& wp
<= lim
; i
++) {
137 fprintf(ftrace
, "\t%s preference=%d",
138 naddr_ntoa(wp
[0]), (int)ntohl(wp
[1]));
139 wp
+= p
->ad
.icmp_ad_asize
;
144 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
145 act
, naddr_ntoa(from
), naddr_ntoa(to
),
146 ifp
? ifp
->int_name
: "?",
147 (int)ntohl(p
->so
.icmp_so_rsvd
));
151 /* prepare Router Discovery socket.
156 if (rdisc_sock
< 0) {
157 rdisc_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
159 BADERR(1,"rdisc_sock = socket()");
160 fix_sock(rdisc_sock
,"rdisc_sock");
166 /* Pick multicast group for router-discovery socket
169 set_rdisc_mg(struct interface
*ifp
,
170 int on
) /* 0=turn it off */
174 if (rdisc_sock
< 0) {
175 /* Create the raw socket so that we can hear at least
176 * broadcast router discovery packets.
178 if ((ifp
->int_state
& IS_NO_RDISC
) == IS_NO_RDISC
184 if (!(ifp
->int_if_flags
& IFF_MULTICAST
)) {
185 ifp
->int_state
&= ~(IS_ALL_HOSTS
| IS_ALL_ROUTERS
);
190 if (ifp
->int_if_flags
& IFF_POINTOPOINT
)
193 memset(&m
, 0, sizeof(m
));
194 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
198 || (ifp
->int_state
& IS_NO_ADV_IN
)
200 /* stop listening to advertisements
202 if (ifp
->int_state
& IS_ALL_HOSTS
) {
203 m
.imr_multiaddr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
204 if (setsockopt(rdisc_sock
, IPPROTO_IP
,
207 LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
208 ifp
->int_state
&= ~IS_ALL_HOSTS
;
211 } else if (!(ifp
->int_state
& IS_ALL_HOSTS
)) {
212 /* start listening to advertisements
214 m
.imr_multiaddr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
215 if (setsockopt(rdisc_sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
216 &m
, sizeof(m
)) < 0) {
217 LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
219 ifp
->int_state
|= IS_ALL_HOSTS
;
224 || (ifp
->int_state
& IS_NO_ADV_OUT
)
226 /* stop listening to solicitations
228 if (ifp
->int_state
& IS_ALL_ROUTERS
) {
229 m
.imr_multiaddr
.s_addr
=htonl(INADDR_ALLROUTERS_GROUP
);
230 if (setsockopt(rdisc_sock
, IPPROTO_IP
,
233 LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
234 ifp
->int_state
&= ~IS_ALL_ROUTERS
;
237 } else if (!(ifp
->int_state
& IS_ALL_ROUTERS
)) {
238 /* start hearing solicitations
240 m
.imr_multiaddr
.s_addr
=htonl(INADDR_ALLROUTERS_GROUP
);
241 if (setsockopt(rdisc_sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
242 &m
, sizeof(m
)) < 0) {
243 LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
245 ifp
->int_state
|= IS_ALL_ROUTERS
;
251 /* start supplying routes
256 struct interface
*ifp
;
262 trace_act("start supplying routes");
264 /* Forget discovered routes.
266 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
267 drp
->dr_recv_pref
= 0;
275 /* Do not start advertising until we have heard some RIP routes */
276 LIM_SEC(rdisc_timer
, now
.tv_sec
+MIN_WAITTIME
);
278 /* Switch router discovery multicast groups from soliciting
281 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
282 if (ifp
->int_state
& IS_BROKE
)
284 ifp
->int_rdisc_cnt
= 0;
285 ifp
->int_rdisc_timer
.tv_usec
= rdisc_timer
.tv_usec
;
286 ifp
->int_rdisc_timer
.tv_sec
= now
.tv_sec
+MIN_WAITTIME
;
287 set_rdisc_mg(ifp
, 1);
290 /* get rid of any redirects */
295 /* age discovered routes and find the best one
298 rdisc_age(naddr bad_gate
)
304 /* If only advertising, then do only that. */
306 /* If switching from client to server, get rid of old
315 /* If we are being told about a bad router,
316 * then age the discovered default route, and if there is
317 * no alternative, solicit a replacement.
320 /* Look for the bad discovered default route.
321 * Age it and note its interface.
323 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
327 /* When we find the bad router, then age the route
328 * to at most SUPPLY_INTERVAL.
329 * This is contrary to RFC 1256, but defends against
332 if (drp
->dr_gate
== bad_gate
) {
333 sec
= (now
.tv_sec
- drp
->dr_life
335 if (drp
->dr_ts
> sec
) {
336 trace_act("age 0.0.0.0 --> %s via %s",
337 naddr_ntoa(drp
->dr_gate
),
338 drp
->dr_ifp
->int_name
);
349 /* Delete old redirected routes to keep the kernel table small,
350 * and to prevent black holes. Check that the kernel table
351 * matches the daemon table (i.e. has the default route).
352 * But only if RIP is not running and we are not dealing with
353 * a bad gateway, since otherwise age() will be called.
355 if (rip_sock
< 0 && bad_gate
== 0)
360 /* Zap all routes discovered via an interface that has gone bad
361 * This should only be called when !(ifp->int_state & IS_ALIAS)
364 if_bad_rdisc(struct interface
*ifp
)
368 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
369 if (drp
->dr_ifp
!= ifp
)
371 drp
->dr_recv_pref
= 0;
376 /* make a note to re-solicit, turn RIP on or off, etc. */
377 rdisc_timer
.tv_sec
= 0;
381 /* mark an interface ok for router discovering.
384 if_ok_rdisc(struct interface
*ifp
)
386 set_rdisc_mg(ifp
, 1);
388 ifp
->int_rdisc_cnt
= 0;
389 ifp
->int_rdisc_timer
.tv_sec
= now
.tv_sec
+ (supplier
391 : MAX_SOLICITATION_DELAY
);
392 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
393 rdisc_timer
= ifp
->int_rdisc_timer
;
397 /* get rid of a dead discovered router
400 del_rdisc(struct dr
*drp
)
402 struct interface
*ifp
;
407 del_redirects(gate
= drp
->dr_gate
, 0);
412 /* Count the other discovered routes on the interface.
416 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
418 && drp
->dr_ifp
== ifp
)
422 /* If that was the last good discovered router on the interface,
423 * then solicit a new one.
424 * This is contrary to RFC 1256, but defends against black holes.
427 trace_act("discovered router %s via %s"
428 " is bad--have %d remaining",
429 naddr_ntoa(gate
), ifp
->int_name
, i
);
430 } else if (ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
) {
431 trace_act("last discovered router %s via %s"
432 " is bad--re-solicit",
433 naddr_ntoa(gate
), ifp
->int_name
);
434 ifp
->int_rdisc_cnt
= 0;
435 ifp
->int_rdisc_timer
.tv_sec
= 0;
438 trace_act("last discovered router %s via %s"
439 " is bad--wait to solicit",
440 naddr_ntoa(gate
), ifp
->int_name
);
445 /* Find the best discovered route,
446 * and discard stale routers.
451 struct dr
*drp
, *new_drp
;
454 struct interface
*ifp
;
459 /* Find the best discovered route.
462 for (drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
467 /* Get rid of expired discovered routers.
469 if (drp
->dr_ts
+ drp
->dr_life
<= now
.tv_sec
) {
474 LIM_SEC(rdisc_timer
, drp
->dr_ts
+drp
->dr_life
+1);
476 /* Update preference with possibly changed interface
479 drp
->dr_pref
= PREF(drp
->dr_recv_pref
, ifp
);
481 /* Prefer the current route to prevent thrashing.
482 * Prefer shorter lifetimes to speed the detection of
484 * Avoid sick interfaces.
487 || (!((new_st
^ drp
->dr_ifp
->int_state
) & IS_SICK
)
488 && (new_pref
< drp
->dr_pref
489 || (new_pref
== drp
->dr_pref
491 || (new_drp
!= cur_drp
492 && new_drp
->dr_life
> drp
->dr_life
)))))
493 || ((new_st
& IS_SICK
)
494 && !(drp
->dr_ifp
->int_state
& IS_SICK
))) {
496 new_st
= drp
->dr_ifp
->int_state
;
497 new_pref
= drp
->dr_pref
;
501 /* switch to a better default route
503 if (new_drp
!= cur_drp
) {
504 rt
= rtget(RIP_DEFAULT
, 0);
506 /* Stop using discovered routes if they are all bad
509 trace_act("turn off Router Discovery client");
513 && (rt
->rt_state
& RS_RDISC
)) {
514 new = rt
->rt_spares
[0];
515 new.rts_metric
= HOPCNT_INFINITY
;
516 new.rts_time
= now
.tv_sec
- GARBAGE_TIME
;
517 rtchange(rt
, rt
->rt_state
& ~RS_RDISC
,
524 trace_act("turn on Router Discovery client"
526 naddr_ntoa(new_drp
->dr_gate
),
527 new_drp
->dr_ifp
->int_name
);
531 trace_act("switch Router Discovery from"
532 " %s via %s to %s via %s",
533 naddr_ntoa(cur_drp
->dr_gate
),
534 cur_drp
->dr_ifp
->int_name
,
535 naddr_ntoa(new_drp
->dr_gate
),
536 new_drp
->dr_ifp
->int_name
);
539 memset(&new, 0, sizeof(new));
540 new.rts_ifp
= new_drp
->dr_ifp
;
541 new.rts_gate
= new_drp
->dr_gate
;
542 new.rts_router
= new_drp
->dr_gate
;
543 new.rts_metric
= HOPCNT_INFINITY
-1;
544 new.rts_time
= now
.tv_sec
;
546 rtchange(rt
, rt
->rt_state
| RS_RDISC
, &new, 0);
548 rtadd(RIP_DEFAULT
, 0, RS_RDISC
, &new);
555 /* turn RIP on or off */
556 if (!rdisc_ok
|| rip_interfaces
> 1) {
564 /* handle a single address in an advertisement
569 n_long pref
, /* signed and in network order */
570 u_short life
, /* in host byte order */
571 struct interface
*ifp
)
573 static struct msg_limit bad_gate
;
574 struct dr
*drp
, *new_drp
;
577 if (gate
== RIP_DEFAULT
578 || !check_dst(gate
)) {
579 msglim(&bad_gate
, from
,"router %s advertising bad gateway %s",
585 /* ignore pointers to ourself and routes via unreachable networks
587 if (ifwithaddr(gate
, 1, 0) != 0) {
588 trace_pkt(" discard Router Discovery Ad pointing at us");
591 if (!on_net(gate
, ifp
->int_net
, ifp
->int_mask
)) {
592 trace_pkt(" discard Router Discovery Ad"
593 " toward unreachable net");
597 /* Convert preference to an unsigned value
598 * and later bias it by the metric of the interface.
600 pref
= UNSIGN_PREF(ntohl(pref
));
602 if (pref
== 0 || life
< MinMaxAdvertiseInterval
) {
607 for (new_drp
= 0, drp
= drs
; drp
< &drs
[MAX_ADS
]; drp
++) {
608 /* accept new info for a familiar entry
610 if (drp
->dr_gate
== gate
) {
616 continue; /* do not worry about dead ads */
618 if (drp
->dr_ts
== 0) {
619 new_drp
= drp
; /* use unused entry */
621 } else if (new_drp
== 0) {
622 /* look for an entry worse than the new one to
625 if ((!(ifp
->int_state
& IS_SICK
)
626 && (drp
->dr_ifp
->int_state
& IS_SICK
))
627 || (pref
> drp
->dr_pref
628 && !((ifp
->int_state
^ drp
->dr_ifp
->int_state
)
632 } else if (new_drp
->dr_ts
!= 0) {
633 /* look for the least valuable entry to reuse
635 if ((!(new_drp
->dr_ifp
->int_state
& IS_SICK
)
636 && (drp
->dr_ifp
->int_state
& IS_SICK
))
637 || (new_drp
->dr_pref
> drp
->dr_pref
638 && !((new_drp
->dr_ifp
->int_state
639 ^ drp
->dr_ifp
->int_state
)
645 /* forget it if all of the current entries are better */
649 new_drp
->dr_ifp
= ifp
;
650 new_drp
->dr_gate
= gate
;
651 new_drp
->dr_ts
= now
.tv_sec
;
652 new_drp
->dr_life
= life
;
653 new_drp
->dr_recv_pref
= pref
;
654 /* bias functional preference by metric of the interface */
655 new_drp
->dr_pref
= PREF(pref
,ifp
);
657 /* after hearing a good advertisement, stop asking
659 if (!(ifp
->int_state
& IS_SICK
))
660 ifp
->int_rdisc_cnt
= MAX_SOLICITATIONS
;
664 /* Compute the IP checksum
665 * This assumes the packet is less than 32K long.
672 int nwords
= len
>> 1;
674 while (nwords
-- != 0)
680 /* end-around-carry */
681 sum
= (sum
>> 16) + (sum
& 0xffff);
687 /* Send a router discovery advertisement or solicitation ICMP packet.
690 send_rdisc(union ad_u
*p
,
692 struct interface
*ifp
,
693 naddr dst
, /* 0 or unicast destination */
694 int type
) /* 0=unicast, 1=bcast, 2=mcast */
696 struct sockaddr_in in
;
702 memset(&in
, 0, sizeof(in
));
703 in
.sin_addr
.s_addr
= dst
;
704 in
.sin_family
= AF_INET
;
706 in
.sin_len
= sizeof(in
);
708 flags
= MSG_DONTROUTE
;
711 case 0: /* unicast */
716 case 1: /* broadcast */
717 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
718 msg
= "Send pt-to-pt";
719 in
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
721 msg
= "Send broadcast";
722 in
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
726 case 2: /* multicast */
727 msg
= "Send multicast";
728 if (ifp
->int_state
& IS_DUP
) {
729 trace_act("abort multicast output via %s"
730 " with duplicate address",
734 if (rdisc_sock_mcast
!= ifp
) {
735 /* select the right interface. */
737 /* Do not specify the primary interface explicitly
738 * if we have the multicast point-to-point kernel
739 * bug, since the kernel will do the wrong thing
740 * if the local address of a point-to-point link
741 * is the same as the address of an ordinary
744 if (ifp
->int_addr
== myaddr
) {
748 tgt_mcast
= ifp
->int_addr
;
749 if (0 > setsockopt(rdisc_sock
,
750 IPPROTO_IP
, IP_MULTICAST_IF
,
751 &tgt_mcast
, sizeof(tgt_mcast
))) {
752 LOGERR("setsockopt(rdisc_sock,"
754 rdisc_sock_mcast
= 0;
757 rdisc_sock_mcast
= ifp
;
766 trace_rdisc(msg
, ifp
->int_addr
, in
.sin_addr
.s_addr
, ifp
,
769 if (0 > sendto(rdisc_sock
, p
, p_size
, flags
,
770 (struct sockaddr
*)&in
, sizeof(in
))) {
771 if (ifp
== 0 || !(ifp
->int_state
& IS_BROKE
))
772 msglog("sendto(%s%s%s): %s",
773 ifp
!= 0 ? ifp
->int_name
: "",
774 ifp
!= 0 ? ", " : "",
775 inet_ntoa(in
.sin_addr
),
783 /* Send an advertisement
786 send_adv(struct interface
*ifp
,
787 naddr dst
, /* 0 or unicast destination */
788 int type
) /* 0=unicast, 1=bcast, 2=mcast */
794 memset(&u
, 0, sizeof(u
.ad
));
796 u
.ad
.icmp_type
= ICMP_ROUTERADVERT
;
797 u
.ad
.icmp_ad_num
= 1;
798 u
.ad
.icmp_ad_asize
= sizeof(u
.ad
.icmp_ad_info
[0])/4;
800 u
.ad
.icmp_ad_life
= stopint
? 0 : htons(ifp
->int_rdisc_int
*3);
802 /* Convert the configured preference to an unsigned value,
803 * bias it by the interface metric, and then send it as a
804 * signed, network byte order value.
806 pref
= UNSIGN_PREF(ifp
->int_rdisc_pref
);
807 u
.ad
.icmp_ad_info
[0].icmp_ad_pref
= htonl(SIGN_PREF(PREF(pref
, ifp
)));
809 u
.ad
.icmp_ad_info
[0].icmp_ad_addr
= ifp
->int_addr
;
811 u
.ad
.icmp_cksum
= in_cksum((u_short
*)&u
.ad
, sizeof(u
.ad
));
813 send_rdisc(&u
, sizeof(u
.ad
), ifp
, dst
, type
);
817 /* Advertise for Router Discovery
822 struct interface
*ifp
;
827 rdisc_timer
.tv_sec
= now
.tv_sec
+ NEVER
;
829 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
830 if (0 != (ifp
->int_state
& (IS_NO_ADV_OUT
| IS_BROKE
)))
833 if (!timercmp(&ifp
->int_rdisc_timer
, &now
, >)
835 send_adv(ifp
, htonl(INADDR_ALLHOSTS_GROUP
),
836 (ifp
->int_state
&IS_BCAST_RDISC
) ? 1 : 2);
837 ifp
->int_rdisc_cnt
++;
839 intvl_random(&ifp
->int_rdisc_timer
,
840 (ifp
->int_rdisc_int
*3)/4,
842 if (ifp
->int_rdisc_cnt
< MAX_INITIAL_ADVERTS
843 && (ifp
->int_rdisc_timer
.tv_sec
844 > MAX_INITIAL_ADVERT_INTERVAL
)) {
845 ifp
->int_rdisc_timer
.tv_sec
846 = MAX_INITIAL_ADVERT_INTERVAL
;
848 timevaladd(&ifp
->int_rdisc_timer
, &now
);
851 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
852 rdisc_timer
= ifp
->int_rdisc_timer
;
857 /* Solicit for Router Discovery
862 struct interface
*ifp
;
869 rdisc_timer
.tv_sec
= now
.tv_sec
+ NEVER
;
871 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
872 if (0 != (ifp
->int_state
& (IS_NO_SOL_OUT
| IS_BROKE
))
873 || ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
)
876 if (!timercmp(&ifp
->int_rdisc_timer
, &now
, >)) {
877 memset(&u
, 0, sizeof(u
.so
));
878 u
.so
.icmp_type
= ICMP_ROUTERSOLICIT
;
879 u
.so
.icmp_cksum
= in_cksum((u_short
*)&u
.so
,
881 send_rdisc(&u
, sizeof(u
.so
), ifp
,
882 htonl(INADDR_ALLROUTERS_GROUP
),
883 ((ifp
->int_state
&IS_BCAST_RDISC
) ? 1 : 2));
885 if (++ifp
->int_rdisc_cnt
>= MAX_SOLICITATIONS
)
888 ifp
->int_rdisc_timer
.tv_sec
= SOLICITATION_INTERVAL
;
889 ifp
->int_rdisc_timer
.tv_usec
= 0;
890 timevaladd(&ifp
->int_rdisc_timer
, &now
);
893 if (timercmp(&rdisc_timer
, &ifp
->int_rdisc_timer
, >))
894 rdisc_timer
= ifp
->int_rdisc_timer
;
899 /* check the IP header of a possible Router Discovery ICMP packet */
900 static struct interface
* /* 0 if bad */
901 ck_icmp(const char *act
,
903 struct interface
*ifp
,
911 if (p
->icmp
.icmp_type
== ICMP_ROUTERADVERT
) {
912 type
= "advertisement";
913 } else if (p
->icmp
.icmp_type
== ICMP_ROUTERSOLICIT
) {
914 type
= "solicitation";
919 if (p
->icmp
.icmp_code
!= 0) {
920 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
921 type
, p
->icmp
.icmp_code
,
922 naddr_ntoa(from
), naddr_ntoa(to
));
926 trace_rdisc(act
, from
, to
, ifp
, p
, len
);
929 trace_pkt("unknown interface for router-discovery %s"
931 type
, naddr_ntoa(from
), naddr_ntoa(to
));
937 /* read packets from the router discovery socket
942 static struct msg_limit bad_asize
, bad_len
;
943 #ifdef USE_PASSIFNAME
944 static struct msg_limit bad_name
;
946 struct sockaddr_in from
;
947 int n
, fromlen
, cc
, hlen
;
949 #ifdef USE_PASSIFNAME
950 char ifname
[IFNAMSIZ
];
960 struct interface
*ifp
;
964 fromlen
= sizeof(from
);
965 cc
= recvfrom(rdisc_sock
, &buf
, sizeof(buf
), 0,
966 (struct sockaddr
*)&from
,
969 if (cc
< 0 && errno
!= EWOULDBLOCK
)
970 LOGERR("recvfrom(rdisc_sock)");
973 if (fromlen
!= sizeof(struct sockaddr_in
))
974 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
976 #ifdef USE_PASSIFNAME
977 if ((cc
-= sizeof(buf
.ifname
)) < 0)
978 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
979 cc
+sizeof(buf
.ifname
));
982 hlen
= buf
.pkt
.ip
.ip_hl
<< 2;
983 if (cc
< hlen
+ ICMP_MINLEN
)
985 p
= (union ad_u
*)&buf
.pkt
.b
[hlen
];
988 #ifdef USE_PASSIFNAME
989 ifp
= ifwithname(buf
.ifname
, 0);
991 msglim(&bad_name
, from
.sin_addr
.s_addr
,
992 "impossible rdisc if_ name %.*s",
993 IFNAMSIZ
, buf
.ifname
);
995 /* If we could tell the interface on which a packet from
996 * address 0 arrived, we could deal with such solicitations.
998 ifp
= ((from
.sin_addr
.s_addr
== 0)
999 ? 0 : iflookup(from
.sin_addr
.s_addr
));
1001 ifp
= ck_icmp("Recv", from
.sin_addr
.s_addr
, ifp
,
1002 buf
.pkt
.ip
.ip_dst
.s_addr
, p
, cc
);
1005 if (ifwithaddr(from
.sin_addr
.s_addr
, 0, 0)) {
1007 "discard our own Router Discovery message");
1011 switch (p
->icmp
.icmp_type
) {
1012 case ICMP_ROUTERADVERT
:
1013 if (p
->ad
.icmp_ad_asize
*4
1014 < (int)sizeof(p
->ad
.icmp_ad_info
[0])) {
1015 msglim(&bad_asize
, from
.sin_addr
.s_addr
,
1016 "intolerable rdisc address size=%d",
1017 p
->ad
.icmp_ad_asize
);
1020 if (p
->ad
.icmp_ad_num
== 0) {
1021 trace_pkt(" empty?");
1024 if (cc
!= (int)(sizeof(p
->ad
)
1025 - sizeof(p
->ad
.icmp_ad_info
)
1026 + (p
->ad
.icmp_ad_num
1027 * sizeof(p
->ad
.icmp_ad_info
[0])))) {
1028 msglim(&bad_len
, from
.sin_addr
.s_addr
,
1029 "rdisc length %d does not match ad_num"
1030 " %d", cc
, p
->ad
.icmp_ad_num
);
1035 if (ifp
->int_state
& IS_NO_ADV_IN
)
1038 wp
= &p
->ad
.icmp_ad_info
[0].icmp_ad_addr
;
1039 for (n
= 0; n
< p
->ad
.icmp_ad_num
; n
++) {
1040 parse_ad(from
.sin_addr
.s_addr
,
1042 ntohs(p
->ad
.icmp_ad_life
),
1044 wp
+= p
->ad
.icmp_ad_asize
;
1049 case ICMP_ROUTERSOLICIT
:
1052 if (ifp
->int_state
& IS_NO_ADV_OUT
)
1058 * We should handle messages from address 0.
1061 /* Respond with a point-to-point advertisement */
1062 send_adv(ifp
, from
.sin_addr
.s_addr
, 0);