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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/ip_icmp.h>
39 #elif defined(__FreeBSD__)
42 __RCSID("$Revision: 2.27 $");
43 #ident "$Revision: 2.27 $"
46 /* router advertisement ICMP packet */
48 u_int8_t icmp_type; /* type of message */
49 u_int8_t icmp_code; /* type sub code */
50 u_int16_t icmp_cksum; /* ones complement cksum of struct */
51 u_int8_t icmp_ad_num; /* # of following router addresses */
52 u_int8_t icmp_ad_asize; /* 2--words in each advertisement */
53 u_int16_t icmp_ad_life; /* seconds of validity */
60 /* router solicitation ICMP packet */
62 u_int8_t icmp_type; /* type of message */
63 u_int8_t icmp_code; /* type sub code */
64 u_int16_t icmp_cksum; /* ones complement cksum of struct */
75 int rdisc_sock = -1; /* router-discovery raw socket */
76 struct interface *rdisc_sock_mcast; /* current multicast interface */
78 struct timeval rdisc_timer;
79 int rdisc_ok; /* using solicited route */
82 #define MAX_ADS 16 /* at least one per interface */
83 struct dr { /* accumulated advertisements */
84 struct interface *dr_ifp;
85 naddr dr_gate; /* gateway */
86 time_t dr_ts; /* when received */
87 time_t dr_life; /* lifetime in host byte order */
88 n_long dr_recv_pref; /* received but biased preference */
89 n_long dr_pref; /* preference adjusted by metric */
90 } *cur_drp, drs[MAX_ADS];
92 /* convert between signed, balanced around zero,
93 * and unsigned zero-based preferences */
94 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel)
95 #define UNSIGN_PREF(p) SIGN_PREF(p)
96 /* adjust unsigned preference by interface metric,
97 * without driving it to infinity */
98 #define PREF(p, ifp) ((int)(p) <= ((ifp)->int_metric+(ifp)->int_adj_outmetric)\
99 ? ((p) != 0 ? 1 : 0) \
100 : (p) - ((ifp)->int_metric+(ifp)->int_adj_outmetric))
102 static void rdisc_sort(void);
105 /* dump an ICMP Router Discovery Advertisement Message
108 trace_rdisc(const char *act,
111 struct interface *ifp,
119 if (!TRACEPACKETS || ftrace == 0)
124 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
125 (void)fprintf(ftrace, "%s Router Ad"
126 " from %s to %s via %s life=%d\n",
127 act, naddr_ntoa(from), naddr_ntoa(to),
128 ifp ? ifp->int_name : "?",
129 ntohs(p->ad.icmp_ad_life));
133 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
134 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
135 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
136 (void)fprintf(ftrace, "\t%s preference=%d",
137 naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
138 wp += p->ad.icmp_ad_asize;
140 (void)fputc('\n',ftrace);
143 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
144 act, naddr_ntoa(from), naddr_ntoa(to),
145 ifp ? ifp->int_name : "?",
146 (int)ntohl(p->so.icmp_so_rsvd));
150 /* prepare Router Discovery socket.
155 if (rdisc_sock < 0) {
156 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
158 BADERR(1,"rdisc_sock = socket()");
159 fix_sock(rdisc_sock,"rdisc_sock");
165 /* Pick multicast group for router-discovery socket
168 set_rdisc_mg(struct interface *ifp,
169 int on) /* 0=turn it off */
173 if (rdisc_sock < 0) {
174 /* Create the raw socket so that we can hear at least
175 * broadcast router discovery packets.
177 if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC
183 if (!(ifp->int_if_flags & IFF_MULTICAST)) {
184 ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
189 if (ifp->int_if_flags & IFF_POINTOPOINT)
192 memset(&m, 0, sizeof(m));
194 m.imr_interface.s_addr = htonl(ifp->int_index);
196 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
201 || (ifp->int_state & IS_NO_ADV_IN)
203 /* stop listening to advertisements
205 if (ifp->int_state & IS_ALL_HOSTS) {
206 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
207 if (setsockopt(rdisc_sock, IPPROTO_IP,
210 LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
211 ifp->int_state &= ~IS_ALL_HOSTS;
214 } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
215 /* start listening to advertisements
217 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
218 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
219 &m, sizeof(m)) < 0) {
220 LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
222 ifp->int_state |= IS_ALL_HOSTS;
227 || (ifp->int_state & IS_NO_ADV_OUT)
229 /* stop listening to solicitations
231 if (ifp->int_state & IS_ALL_ROUTERS) {
232 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
233 if (setsockopt(rdisc_sock, IPPROTO_IP,
236 LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
237 ifp->int_state &= ~IS_ALL_ROUTERS;
240 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
241 /* start hearing solicitations
243 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
244 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
245 &m, sizeof(m)) < 0) {
246 LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
248 ifp->int_state |= IS_ALL_ROUTERS;
254 /* start supplying routes
259 struct interface *ifp;
265 trace_act("start supplying routes");
267 /* Forget discovered routes.
269 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
270 drp->dr_recv_pref = 0;
278 /* Do not start advertising until we have heard some RIP routes */
279 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
281 /* Switch router discovery multicast groups from soliciting
284 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
285 if (ifp->int_state & IS_BROKE)
287 ifp->int_rdisc_cnt = 0;
288 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
289 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
290 set_rdisc_mg(ifp, 1);
293 /* get rid of any redirects */
298 /* age discovered routes and find the best one
301 rdisc_age(naddr bad_gate)
307 /* If only advertising, then do only that. */
309 /* If switching from client to server, get rid of old
318 /* If we are being told about a bad router,
319 * then age the discovered default route, and if there is
320 * no alternative, solicit a replacement.
323 /* Look for the bad discovered default route.
324 * Age it and note its interface.
326 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
330 /* When we find the bad router, then age the route
331 * to at most SUPPLY_INTERVAL.
332 * This is contrary to RFC 1256, but defends against
335 if (drp->dr_gate == bad_gate) {
336 sec = (now.tv_sec - drp->dr_life
338 if (drp->dr_ts > sec) {
339 trace_act("age 0.0.0.0 --> %s via %s",
340 naddr_ntoa(drp->dr_gate),
341 drp->dr_ifp->int_name);
352 /* Delete old redirected routes to keep the kernel table small,
353 * and to prevent black holes. Check that the kernel table
354 * matches the daemon table (i.e. has the default route).
355 * But only if RIP is not running and we are not dealing with
356 * a bad gateway, since otherwise age() will be called.
358 if (rip_sock < 0 && bad_gate == 0)
363 /* Zap all routes discovered via an interface that has gone bad
364 * This should only be called when !(ifp->int_state & IS_ALIAS)
367 if_bad_rdisc(struct interface *ifp)
371 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
372 if (drp->dr_ifp != ifp)
374 drp->dr_recv_pref = 0;
379 /* make a note to re-solicit, turn RIP on or off, etc. */
380 rdisc_timer.tv_sec = 0;
384 /* mark an interface ok for router discovering.
387 if_ok_rdisc(struct interface *ifp)
389 set_rdisc_mg(ifp, 1);
391 ifp->int_rdisc_cnt = 0;
392 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
394 : MAX_SOLICITATION_DELAY);
395 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
396 rdisc_timer = ifp->int_rdisc_timer;
400 /* get rid of a dead discovered router
403 del_rdisc(struct dr *drp)
405 struct interface *ifp;
410 del_redirects(gate = drp->dr_gate, 0);
415 /* Count the other discovered routes on the interface.
419 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
421 && drp->dr_ifp == ifp)
425 /* If that was the last good discovered router on the interface,
426 * then solicit a new one.
427 * This is contrary to RFC 1256, but defends against black holes.
430 trace_act("discovered router %s via %s"
431 " is bad--have %d remaining",
432 naddr_ntoa(gate), ifp->int_name, i);
433 } else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
434 trace_act("last discovered router %s via %s"
435 " is bad--re-solicit",
436 naddr_ntoa(gate), ifp->int_name);
437 ifp->int_rdisc_cnt = 0;
438 ifp->int_rdisc_timer.tv_sec = 0;
441 trace_act("last discovered router %s via %s"
442 " is bad--wait to solicit",
443 naddr_ntoa(gate), ifp->int_name);
448 /* Find the best discovered route,
449 * and discard stale routers.
454 struct dr *drp, *new_drp;
457 struct interface *ifp;
462 /* Find the best discovered route.
465 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
470 /* Get rid of expired discovered routers.
472 if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
477 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
479 /* Update preference with possibly changed interface
482 drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
484 /* Prefer the current route to prevent thrashing.
485 * Prefer shorter lifetimes to speed the detection of
487 * Avoid sick interfaces.
490 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
491 && (new_pref < drp->dr_pref
492 || (new_pref == drp->dr_pref
494 || (new_drp != cur_drp
495 && new_drp->dr_life > drp->dr_life)))))
496 || ((new_st & IS_SICK)
497 && !(drp->dr_ifp->int_state & IS_SICK))) {
499 new_st = drp->dr_ifp->int_state;
500 new_pref = drp->dr_pref;
504 /* switch to a better default route
506 if (new_drp != cur_drp) {
507 rt = rtget(RIP_DEFAULT, 0);
509 /* Stop using discovered routes if they are all bad
512 trace_act("turn off Router Discovery client");
516 && (rt->rt_state & RS_RDISC)) {
517 new = rt->rt_spares[0];
518 new.rts_metric = HOPCNT_INFINITY;
519 new.rts_time = now.tv_sec - GARBAGE_TIME;
520 rtchange(rt, rt->rt_state & ~RS_RDISC,
527 trace_act("turn on Router Discovery client"
529 naddr_ntoa(new_drp->dr_gate),
530 new_drp->dr_ifp->int_name);
534 trace_act("switch Router Discovery from"
535 " %s via %s to %s via %s",
536 naddr_ntoa(cur_drp->dr_gate),
537 cur_drp->dr_ifp->int_name,
538 naddr_ntoa(new_drp->dr_gate),
539 new_drp->dr_ifp->int_name);
542 memset(&new, 0, sizeof(new));
543 new.rts_ifp = new_drp->dr_ifp;
544 new.rts_gate = new_drp->dr_gate;
545 new.rts_router = new_drp->dr_gate;
546 new.rts_metric = HOPCNT_INFINITY-1;
547 new.rts_time = now.tv_sec;
549 rtchange(rt, rt->rt_state | RS_RDISC, &new, 0);
551 rtadd(RIP_DEFAULT, 0, RS_RDISC, &new);
558 /* turn RIP on or off */
559 if (!rdisc_ok || rip_interfaces > 1) {
567 /* handle a single address in an advertisement
572 n_long pref, /* signed and in network order */
573 u_short life, /* in host byte order */
574 struct interface *ifp)
576 static struct msg_limit bad_gate;
577 struct dr *drp, *new_drp;
580 if (gate == RIP_DEFAULT
581 || !check_dst(gate)) {
582 msglim(&bad_gate, from,"router %s advertising bad gateway %s",
588 /* ignore pointers to ourself and routes via unreachable networks
590 if (ifwithaddr(gate, 1, 0) != 0) {
591 trace_pkt(" discard Router Discovery Ad pointing at us");
594 if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
595 trace_pkt(" discard Router Discovery Ad"
596 " toward unreachable net");
600 /* Convert preference to an unsigned value
601 * and later bias it by the metric of the interface.
603 pref = UNSIGN_PREF(ntohl(pref));
605 if (pref == 0 || life < MinMaxAdvertiseInterval) {
610 for (new_drp = 0, drp = drs; drp < &drs[MAX_ADS]; drp++) {
611 /* accept new info for a familiar entry
613 if (drp->dr_gate == gate) {
619 continue; /* do not worry about dead ads */
621 if (drp->dr_ts == 0) {
622 new_drp = drp; /* use unused entry */
624 } else if (new_drp == 0) {
625 /* look for an entry worse than the new one to
628 if ((!(ifp->int_state & IS_SICK)
629 && (drp->dr_ifp->int_state & IS_SICK))
630 || (pref > drp->dr_pref
631 && !((ifp->int_state ^ drp->dr_ifp->int_state)
635 } else if (new_drp->dr_ts != 0) {
636 /* look for the least valuable entry to reuse
638 if ((!(new_drp->dr_ifp->int_state & IS_SICK)
639 && (drp->dr_ifp->int_state & IS_SICK))
640 || (new_drp->dr_pref > drp->dr_pref
641 && !((new_drp->dr_ifp->int_state
642 ^ drp->dr_ifp->int_state)
648 /* forget it if all of the current entries are better */
652 new_drp->dr_ifp = ifp;
653 new_drp->dr_gate = gate;
654 new_drp->dr_ts = now.tv_sec;
655 new_drp->dr_life = life;
656 new_drp->dr_recv_pref = pref;
657 /* bias functional preference by metric of the interface */
658 new_drp->dr_pref = PREF(pref,ifp);
660 /* after hearing a good advertisement, stop asking
662 if (!(ifp->int_state & IS_SICK))
663 ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
667 /* Compute the IP checksum
668 * This assumes the packet is less than 32K long.
675 int nwords = len >> 1;
677 while (nwords-- != 0)
683 /* end-around-carry */
684 sum = (sum >> 16) + (sum & 0xffff);
690 /* Send a router discovery advertisement or solicitation ICMP packet.
693 send_rdisc(union ad_u *p,
695 struct interface *ifp,
696 naddr dst, /* 0 or unicast destination */
697 int type) /* 0=unicast, 1=bcast, 2=mcast */
699 struct sockaddr_in rsin;
705 memset(&rsin, 0, sizeof(rsin));
706 rsin.sin_addr.s_addr = dst;
707 rsin.sin_family = AF_INET;
709 rsin.sin_len = sizeof(rsin);
711 flags = MSG_DONTROUTE;
714 case 0: /* unicast */
719 case 1: /* broadcast */
720 if (ifp->int_if_flags & IFF_POINTOPOINT) {
721 msg = "Send pt-to-pt";
722 rsin.sin_addr.s_addr = ifp->int_dstaddr;
724 msg = "Send broadcast";
725 rsin.sin_addr.s_addr = ifp->int_brdaddr;
729 case 2: /* multicast */
730 msg = "Send multicast";
731 if (ifp->int_state & IS_DUP) {
732 trace_act("abort multicast output via %s"
733 " with duplicate address",
737 if (rdisc_sock_mcast != ifp) {
738 /* select the right interface. */
740 /* specify ifindex */
741 tgt_mcast = htonl(ifp->int_index);
744 /* Do not specify the primary interface explicitly
745 * if we have the multicast point-to-point kernel
746 * bug, since the kernel will do the wrong thing
747 * if the local address of a point-to-point link
748 * is the same as the address of an ordinary
751 if (ifp->int_addr == myaddr) {
755 tgt_mcast = ifp->int_addr;
757 if (0 > setsockopt(rdisc_sock,
758 IPPROTO_IP, IP_MULTICAST_IF,
759 &tgt_mcast, sizeof(tgt_mcast))) {
760 LOGERR("setsockopt(rdisc_sock,"
762 rdisc_sock_mcast = 0;
765 rdisc_sock_mcast = ifp;
774 trace_rdisc(msg, ifp->int_addr, rsin.sin_addr.s_addr, ifp,
777 if (0 > sendto(rdisc_sock, p, p_size, flags,
778 (struct sockaddr *)&rsin, sizeof(rsin))) {
779 if (ifp == 0 || !(ifp->int_state & IS_BROKE))
780 msglog("sendto(%s%s%s): %s",
781 ifp != 0 ? ifp->int_name : "",
782 ifp != 0 ? ", " : "",
783 inet_ntoa(rsin.sin_addr),
791 /* Send an advertisement
794 send_adv(struct interface *ifp,
795 naddr dst, /* 0 or unicast destination */
796 int type) /* 0=unicast, 1=bcast, 2=mcast */
802 memset(&u, 0, sizeof(u.ad));
804 u.ad.icmp_type = ICMP_ROUTERADVERT;
805 u.ad.icmp_ad_num = 1;
806 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
808 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
810 /* Convert the configured preference to an unsigned value,
811 * bias it by the interface metric, and then send it as a
812 * signed, network byte order value.
814 pref = UNSIGN_PREF(ifp->int_rdisc_pref);
815 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp)));
817 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
819 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
821 send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
825 /* Advertise for Router Discovery
830 struct interface *ifp;
835 rdisc_timer.tv_sec = now.tv_sec + NEVER;
837 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
838 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
841 if (!timercmp(&ifp->int_rdisc_timer, &now, >)
843 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
844 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
845 ifp->int_rdisc_cnt++;
847 intvl_random(&ifp->int_rdisc_timer,
848 (ifp->int_rdisc_int*3)/4,
850 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
851 && (ifp->int_rdisc_timer.tv_sec
852 > MAX_INITIAL_ADVERT_INTERVAL)) {
853 ifp->int_rdisc_timer.tv_sec
854 = MAX_INITIAL_ADVERT_INTERVAL;
856 timevaladd(&ifp->int_rdisc_timer, &now);
859 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
860 rdisc_timer = ifp->int_rdisc_timer;
865 /* Solicit for Router Discovery
870 struct interface *ifp;
877 rdisc_timer.tv_sec = now.tv_sec + NEVER;
879 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
880 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
881 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
884 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
885 memset(&u, 0, sizeof(u.so));
886 u.so.icmp_type = ICMP_ROUTERSOLICIT;
887 u.so.icmp_cksum = in_cksum((u_short*)&u.so,
889 send_rdisc(&u, sizeof(u.so), ifp,
890 htonl(INADDR_ALLROUTERS_GROUP),
891 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
893 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
896 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
897 ifp->int_rdisc_timer.tv_usec = 0;
898 timevaladd(&ifp->int_rdisc_timer, &now);
901 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
902 rdisc_timer = ifp->int_rdisc_timer;
907 /* check the IP header of a possible Router Discovery ICMP packet */
908 static struct interface * /* 0 if bad */
909 ck_icmp(const char *act,
911 struct interface *ifp,
919 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
920 type = "advertisement";
921 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
922 type = "solicitation";
927 if (p->icmp.icmp_code != 0) {
928 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
929 type, p->icmp.icmp_code,
930 naddr_ntoa(from), naddr_ntoa(to));
934 trace_rdisc(act, from, to, ifp, p, len);
937 trace_pkt("unknown interface for router-discovery %s"
939 type, naddr_ntoa(from), naddr_ntoa(to));
945 /* read packets from the router discovery socket
950 static struct msg_limit bad_asize, bad_len;
951 #ifdef USE_PASSIFNAME
952 static struct msg_limit bad_name;
954 struct sockaddr_in from;
955 int n, fromlen, cc, hlen;
957 #ifdef USE_PASSIFNAME
958 char ifname[IFNAMSIZ];
968 struct interface *ifp;
972 fromlen = sizeof(from);
973 cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0,
974 (struct sockaddr*)&from,
977 if (cc < 0 && errno != EWOULDBLOCK)
978 LOGERR("recvfrom(rdisc_sock)");
981 if (fromlen != sizeof(struct sockaddr_in))
982 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
984 #ifdef USE_PASSIFNAME
985 if ((cc -= sizeof(buf.ifname)) < 0)
986 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
987 cc+sizeof(buf.ifname));
990 hlen = buf.pkt.ip.ip_hl << 2;
991 if (cc < hlen + ICMP_MINLEN)
993 p = (union ad_u *)&buf.pkt.b[hlen];
996 #ifdef USE_PASSIFNAME
997 ifp = ifwithname(buf.ifname, 0);
999 msglim(&bad_name, from.sin_addr.s_addr,
1000 "impossible rdisc if_ name %.*s",
1001 IFNAMSIZ, buf.ifname);
1003 /* If we could tell the interface on which a packet from
1004 * address 0 arrived, we could deal with such solicitations.
1006 ifp = ((from.sin_addr.s_addr == 0)
1007 ? 0 : iflookup(from.sin_addr.s_addr));
1009 ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
1010 buf.pkt.ip.ip_dst.s_addr, p, cc);
1013 if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
1015 "discard our own Router Discovery message");
1019 switch (p->icmp.icmp_type) {
1020 case ICMP_ROUTERADVERT:
1021 if (p->ad.icmp_ad_asize*4
1022 < (int)sizeof(p->ad.icmp_ad_info[0])) {
1023 msglim(&bad_asize, from.sin_addr.s_addr,
1024 "intolerable rdisc address size=%d",
1025 p->ad.icmp_ad_asize);
1028 if (p->ad.icmp_ad_num == 0) {
1029 trace_pkt(" empty?");
1032 if (cc != (int)(sizeof(p->ad)
1033 - sizeof(p->ad.icmp_ad_info)
1034 + (p->ad.icmp_ad_num
1035 * sizeof(p->ad.icmp_ad_info[0])))) {
1036 msglim(&bad_len, from.sin_addr.s_addr,
1037 "rdisc length %d does not match ad_num"
1038 " %d", cc, p->ad.icmp_ad_num);
1043 if (ifp->int_state & IS_NO_ADV_IN)
1046 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
1047 for (n = 0; n < p->ad.icmp_ad_num; n++) {
1048 parse_ad(from.sin_addr.s_addr,
1050 ntohs(p->ad.icmp_ad_life),
1052 wp += p->ad.icmp_ad_asize;
1057 case ICMP_ROUTERSOLICIT:
1060 if (ifp->int_state & IS_NO_ADV_OUT)
1066 * We should handle messages from address 0.
1069 /* Respond with a point-to-point advertisement */
1070 send_adv(ifp, from.sin_addr.s_addr, 0);