2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
41 #elif defined(__FreeBSD__)
44 __RCSID("$Revision: 2.27 $");
45 #ident "$Revision: 2.27 $"
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 static const 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 */
93 static const struct dr *cur_drp;
94 static struct dr 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) ((int)(p) <= ((ifp)->int_metric+(ifp)->int_adj_outmetric)\
103 ? ((p) != 0 ? 1 : 0) \
104 : (p) - ((ifp)->int_metric+(ifp)->int_adj_outmetric))
106 static void rdisc_sort(void);
109 /* dump an ICMP Router Discovery Advertisement Message
112 trace_rdisc(const char *act,
115 struct interface *ifp,
123 if (!TRACEPACKETS || ftrace == NULL)
128 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
129 (void)fprintf(ftrace, "%s Router Ad"
130 " from %s to %s via %s life=%d\n",
131 act, naddr_ntoa(from), naddr_ntoa(to),
132 ifp ? ifp->int_name : "?",
133 ntohs(p->ad.icmp_ad_life));
137 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
138 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
139 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
140 (void)fprintf(ftrace, "\t%s preference=%d",
141 naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
142 wp += p->ad.icmp_ad_asize;
144 (void)fputc('\n',ftrace);
147 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
148 act, naddr_ntoa(from), naddr_ntoa(to),
149 ifp ? ifp->int_name : "?",
150 (int)ntohl(p->so.icmp_so_rsvd));
154 /* prepare Router Discovery socket.
159 if (rdisc_sock < 0) {
160 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
162 BADERR(1,"rdisc_sock = socket()");
163 fix_sock(rdisc_sock,"rdisc_sock");
169 /* Pick multicast group for router-discovery socket
172 set_rdisc_mg(struct interface *ifp,
173 int on) /* 0=turn it off */
176 struct sockaddr_in *sin;
180 if (rdisc_sock < 0) {
181 /* Create the raw socket so that we can hear at least
182 * broadcast router discovery packets.
184 if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC
190 if (!(ifp->int_if_flags & IFF_MULTICAST)) {
191 ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
195 memset(&gr, 0, sizeof(gr));
196 gr.gr_interface = ifp->int_index;
197 sin = (struct sockaddr_in *)&gr.gr_group;
198 sin->sin_family = AF_INET;
200 sin->sin_len = sizeof(struct sockaddr_in);
204 || (ifp->int_state & IS_NO_ADV_IN)
206 /* stop listening to advertisements
208 if (ifp->int_state & IS_ALL_HOSTS) {
209 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
210 if (setsockopt(rdisc_sock, IPPROTO_IP,
212 &gr, sizeof(gr)) < 0)
213 LOGERR("MCAST_LEAVE_GROUP ALLHOSTS");
214 ifp->int_state &= ~IS_ALL_HOSTS;
217 } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
218 /* start listening to advertisements
220 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
221 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP,
222 &gr, sizeof(gr)) < 0) {
223 LOGERR("MCAST_JOIN_GROUP ALLHOSTS");
225 ifp->int_state |= IS_ALL_HOSTS;
230 || (ifp->int_state & IS_NO_ADV_OUT)
232 /* stop listening to solicitations
234 if (ifp->int_state & IS_ALL_ROUTERS) {
235 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP);
236 if (setsockopt(rdisc_sock, IPPROTO_IP,
238 &gr, sizeof(gr)) < 0)
239 LOGERR("MCAST_LEAVE_GROUP ALLROUTERS");
240 ifp->int_state &= ~IS_ALL_ROUTERS;
243 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
244 /* start hearing solicitations
246 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP);
247 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP,
248 &gr, sizeof(gr)) < 0) {
249 LOGERR("MCAST_JOIN_GROUP ALLROUTERS");
251 ifp->int_state |= IS_ALL_ROUTERS;
257 /* start supplying routes
262 struct interface *ifp;
268 trace_act("start supplying routes");
270 /* Forget discovered routes.
272 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
273 drp->dr_recv_pref = 0;
281 /* Do not start advertising until we have heard some RIP routes */
282 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
284 /* Switch router discovery multicast groups from soliciting
287 LIST_FOREACH(ifp, &ifnet, int_list) {
288 if (ifp->int_state & IS_BROKE)
290 ifp->int_rdisc_cnt = 0;
291 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
292 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
293 set_rdisc_mg(ifp, 1);
296 /* get rid of any redirects */
301 /* age discovered routes and find the best one
304 rdisc_age(naddr bad_gate)
310 /* If only advertising, then do only that. */
312 /* If switching from client to server, get rid of old
321 /* If we are being told about a bad router,
322 * then age the discovered default route, and if there is
323 * no alternative, solicit a replacement.
326 /* Look for the bad discovered default route.
327 * Age it and note its interface.
329 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
333 /* When we find the bad router, then age the route
334 * to at most SUPPLY_INTERVAL.
335 * This is contrary to RFC 1256, but defends against
338 if (drp->dr_gate == bad_gate) {
339 sec = (now.tv_sec - drp->dr_life
341 if (drp->dr_ts > sec) {
342 trace_act("age 0.0.0.0 --> %s via %s",
343 naddr_ntoa(drp->dr_gate),
344 drp->dr_ifp->int_name);
355 /* Delete old redirected routes to keep the kernel table small,
356 * and to prevent black holes. Check that the kernel table
357 * matches the daemon table (i.e. has the default route).
358 * But only if RIP is not running and we are not dealing with
359 * a bad gateway, since otherwise age() will be called.
361 if (rip_sock < 0 && bad_gate == 0)
366 /* Zap all routes discovered via an interface that has gone bad
367 * This should only be called when !(ifp->int_state & IS_ALIAS)
370 if_bad_rdisc(struct interface *ifp)
374 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
375 if (drp->dr_ifp != ifp)
377 drp->dr_recv_pref = 0;
382 /* make a note to re-solicit, turn RIP on or off, etc. */
383 rdisc_timer.tv_sec = 0;
387 /* mark an interface ok for router discovering.
390 if_ok_rdisc(struct interface *ifp)
392 set_rdisc_mg(ifp, 1);
394 ifp->int_rdisc_cnt = 0;
395 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
397 : MAX_SOLICITATION_DELAY);
398 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
399 rdisc_timer = ifp->int_rdisc_timer;
403 /* get rid of a dead discovered router
406 del_rdisc(struct dr *drp)
408 struct interface *ifp;
413 del_redirects(gate = drp->dr_gate, 0);
418 /* Count the other discovered routes on the interface.
422 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
424 && drp->dr_ifp == ifp)
428 /* If that was the last good discovered router on the interface,
429 * then solicit a new one.
430 * This is contrary to RFC 1256, but defends against black holes.
433 trace_act("discovered router %s via %s"
434 " is bad--have %d remaining",
435 naddr_ntoa(gate), ifp->int_name, i);
436 } else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
437 trace_act("last discovered router %s via %s"
438 " is bad--re-solicit",
439 naddr_ntoa(gate), ifp->int_name);
440 ifp->int_rdisc_cnt = 0;
441 ifp->int_rdisc_timer.tv_sec = 0;
444 trace_act("last discovered router %s via %s"
445 " is bad--wait to solicit",
446 naddr_ntoa(gate), ifp->int_name);
451 /* Find the best discovered route,
452 * and discard stale routers.
457 struct dr *drp, *new_drp;
460 struct interface *ifp;
465 /* Find the best discovered route.
468 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
473 /* Get rid of expired discovered routers.
475 if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
480 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
482 /* Update preference with possibly changed interface
485 drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
487 /* Prefer the current route to prevent thrashing.
488 * Prefer shorter lifetimes to speed the detection of
490 * Avoid sick interfaces.
493 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
494 && (new_pref < drp->dr_pref
495 || (new_pref == drp->dr_pref
497 || (new_drp != cur_drp
498 && new_drp->dr_life > drp->dr_life)))))
499 || ((new_st & IS_SICK)
500 && !(drp->dr_ifp->int_state & IS_SICK))) {
502 new_st = drp->dr_ifp->int_state;
503 new_pref = drp->dr_pref;
507 /* switch to a better default route
509 if (new_drp != cur_drp) {
510 rt = rtget(RIP_DEFAULT, 0);
512 /* Stop using discovered routes if they are all bad
514 if (new_drp == NULL) {
515 trace_act("turn off Router Discovery client");
519 && (rt->rt_state & RS_RDISC)) {
520 new = rt->rt_spares[0];
521 new.rts_metric = HOPCNT_INFINITY;
522 new.rts_time = now.tv_sec - GARBAGE_TIME;
523 rtchange(rt, rt->rt_state & ~RS_RDISC,
529 if (cur_drp == NULL) {
530 trace_act("turn on Router Discovery client"
532 naddr_ntoa(new_drp->dr_gate),
533 new_drp->dr_ifp->int_name);
537 trace_act("switch Router Discovery from"
538 " %s via %s to %s via %s",
539 naddr_ntoa(cur_drp->dr_gate),
540 cur_drp->dr_ifp->int_name,
541 naddr_ntoa(new_drp->dr_gate),
542 new_drp->dr_ifp->int_name);
545 memset(&new, 0, sizeof(new));
546 new.rts_ifp = new_drp->dr_ifp;
547 new.rts_gate = new_drp->dr_gate;
548 new.rts_router = new_drp->dr_gate;
549 new.rts_metric = HOPCNT_INFINITY-1;
550 new.rts_time = now.tv_sec;
552 rtchange(rt, rt->rt_state | RS_RDISC, &new, 0);
554 rtadd(RIP_DEFAULT, 0, RS_RDISC, &new);
561 /* turn RIP on or off */
562 if (!rdisc_ok || rip_interfaces > 1) {
570 /* handle a single address in an advertisement
575 n_long pref, /* signed and in network order */
576 u_short life, /* in host byte order */
577 struct interface *ifp)
579 static struct msg_limit bad_gate;
580 struct dr *drp, *new_drp;
583 if (gate == RIP_DEFAULT
584 || !check_dst(gate)) {
585 msglim(&bad_gate, from,"router %s advertising bad gateway %s",
591 /* ignore pointers to ourself and routes via unreachable networks
593 if (ifwithaddr(gate, 1, 0) != NULL) {
594 trace_pkt(" discard Router Discovery Ad pointing at us");
597 if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
598 trace_pkt(" discard Router Discovery Ad"
599 " toward unreachable net");
603 /* Convert preference to an unsigned value
604 * and later bias it by the metric of the interface.
606 pref = UNSIGN_PREF(ntohl(pref));
608 if (pref == 0 || life < MinMaxAdvertiseInterval) {
613 for (new_drp = NULL, drp = drs; drp < &drs[MAX_ADS]; drp++) {
614 /* accept new info for a familiar entry
616 if (drp->dr_gate == gate) {
622 continue; /* do not worry about dead ads */
624 if (drp->dr_ts == 0) {
625 new_drp = drp; /* use unused entry */
627 } else if (new_drp == NULL) {
628 /* look for an entry worse than the new one to
631 if ((!(ifp->int_state & IS_SICK)
632 && (drp->dr_ifp->int_state & IS_SICK))
633 || (pref > drp->dr_pref
634 && !((ifp->int_state ^ drp->dr_ifp->int_state)
638 } else if (new_drp->dr_ts != 0) {
639 /* look for the least valuable entry to reuse
641 if ((!(new_drp->dr_ifp->int_state & IS_SICK)
642 && (drp->dr_ifp->int_state & IS_SICK))
643 || (new_drp->dr_pref > drp->dr_pref
644 && !((new_drp->dr_ifp->int_state
645 ^ drp->dr_ifp->int_state)
651 /* forget it if all of the current entries are better */
655 new_drp->dr_ifp = ifp;
656 new_drp->dr_gate = gate;
657 new_drp->dr_ts = now.tv_sec;
658 new_drp->dr_life = life;
659 new_drp->dr_recv_pref = pref;
660 /* bias functional preference by metric of the interface */
661 new_drp->dr_pref = PREF(pref,ifp);
663 /* after hearing a good advertisement, stop asking
665 if (!(ifp->int_state & IS_SICK))
666 ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
670 /* Compute the IP checksum
671 * This assumes the packet is less than 32K long.
678 int nwords = len >> 1;
680 while (nwords-- != 0)
686 /* end-around-carry */
687 sum = (sum >> 16) + (sum & 0xffff);
693 /* Send a router discovery advertisement or solicitation ICMP packet.
696 send_rdisc(union ad_u *p,
698 struct interface *ifp,
699 naddr dst, /* 0 or unicast destination */
700 int type) /* 0=unicast, 1=bcast, 2=mcast */
702 struct sockaddr_in rsin;
707 memset(&rsin, 0, sizeof(rsin));
708 rsin.sin_addr.s_addr = dst;
709 rsin.sin_family = AF_INET;
711 rsin.sin_len = sizeof(rsin);
713 flags = MSG_DONTROUTE;
716 case 0: /* unicast */
721 case 1: /* broadcast */
722 if (ifp->int_if_flags & IFF_POINTOPOINT) {
723 msg = "Send pt-to-pt";
724 rsin.sin_addr.s_addr = ifp->int_dstaddr;
726 msg = "Send broadcast";
727 rsin.sin_addr.s_addr = ifp->int_brdaddr;
731 case 2: /* multicast */
732 msg = "Send multicast";
733 if (ifp->int_state & IS_DUP) {
734 trace_act("abort multicast output via %s"
735 " with duplicate address",
739 if (rdisc_sock_mcast != ifp) {
740 /* select the right interface. */
741 struct ip_mreqn mreqn;
743 memset(&mreqn, 0, sizeof(struct ip_mreqn));
744 mreqn.imr_ifindex = ifp->int_index;
745 if (0 > setsockopt(rdisc_sock,
746 IPPROTO_IP, IP_MULTICAST_IF,
749 LOGERR("setsockopt(rdisc_sock,"
751 rdisc_sock_mcast = NULL;
754 rdisc_sock_mcast = ifp;
763 trace_rdisc(msg, (ifp ? ifp->int_addr : 0), rsin.sin_addr.s_addr, ifp,
766 if (0 > sendto(rdisc_sock, p, p_size, flags,
767 (struct sockaddr *)&rsin, sizeof(rsin))) {
768 if (ifp == NULL || !(ifp->int_state & IS_BROKE))
769 msglog("sendto(%s%s%s): %s",
770 ifp != NULL ? ifp->int_name : "",
771 ifp != NULL ? ", " : "",
772 inet_ntoa(rsin.sin_addr),
780 /* Send an advertisement
783 send_adv(struct interface *ifp,
784 naddr dst, /* 0 or unicast destination */
785 int type) /* 0=unicast, 1=bcast, 2=mcast */
791 memset(&u, 0, sizeof(u.ad));
793 u.ad.icmp_type = ICMP_ROUTERADVERT;
794 u.ad.icmp_ad_num = 1;
795 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
797 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
799 /* Convert the configured preference to an unsigned value,
800 * bias it by the interface metric, and then send it as a
801 * signed, network byte order value.
803 pref = UNSIGN_PREF(ifp->int_rdisc_pref);
804 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp)));
806 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
808 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
810 send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
814 /* Advertise for Router Discovery
819 struct interface *ifp;
824 rdisc_timer.tv_sec = now.tv_sec + NEVER;
826 LIST_FOREACH(ifp, &ifnet, int_list) {
827 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
830 if (!timercmp(&ifp->int_rdisc_timer, &now, >)
832 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
833 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
834 ifp->int_rdisc_cnt++;
836 intvl_random(&ifp->int_rdisc_timer,
837 (ifp->int_rdisc_int*3)/4,
839 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
840 && (ifp->int_rdisc_timer.tv_sec
841 > MAX_INITIAL_ADVERT_INTERVAL)) {
842 ifp->int_rdisc_timer.tv_sec
843 = MAX_INITIAL_ADVERT_INTERVAL;
845 timevaladd(&ifp->int_rdisc_timer, &now);
848 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
849 rdisc_timer = ifp->int_rdisc_timer;
854 /* Solicit for Router Discovery
859 struct interface *ifp;
866 rdisc_timer.tv_sec = now.tv_sec + NEVER;
868 LIST_FOREACH(ifp, &ifnet, int_list) {
869 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
870 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
873 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
874 memset(&u, 0, sizeof(u.so));
875 u.so.icmp_type = ICMP_ROUTERSOLICIT;
876 u.so.icmp_cksum = in_cksum((u_short*)&u.so,
878 send_rdisc(&u, sizeof(u.so), ifp,
879 htonl(INADDR_ALLROUTERS_GROUP),
880 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
882 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
885 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
886 ifp->int_rdisc_timer.tv_usec = 0;
887 timevaladd(&ifp->int_rdisc_timer, &now);
890 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
891 rdisc_timer = ifp->int_rdisc_timer;
896 /* check the IP header of a possible Router Discovery ICMP packet */
897 static struct interface * /* 0 if bad */
898 ck_icmp(const char *act,
900 struct interface *ifp,
908 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
909 type = "advertisement";
910 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
911 type = "solicitation";
916 if (p->icmp.icmp_code != 0) {
917 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
918 type, p->icmp.icmp_code,
919 naddr_ntoa(from), naddr_ntoa(to));
923 trace_rdisc(act, from, to, ifp, p, len);
926 trace_pkt("unknown interface for router-discovery %s"
928 type, naddr_ntoa(from), naddr_ntoa(to));
934 /* read packets from the router discovery socket
939 static struct msg_limit bad_asize, bad_len;
940 #ifdef USE_PASSIFNAME
941 static struct msg_limit bad_name;
943 struct sockaddr_in from;
944 int n, fromlen, cc, hlen;
946 #ifdef USE_PASSIFNAME
947 char ifname[IFNAMSIZ];
956 struct interface *ifp;
960 fromlen = sizeof(from);
961 cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0,
962 (struct sockaddr*)&from,
965 if (cc < 0 && errno != EWOULDBLOCK)
966 LOGERR("recvfrom(rdisc_sock)");
969 if (fromlen != sizeof(struct sockaddr_in))
970 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
972 #ifdef USE_PASSIFNAME
973 if ((cc -= sizeof(buf.ifname)) < 0)
974 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
975 cc+sizeof(buf.ifname));
978 hlen = buf.pkt.ip.ip_hl << 2;
979 if (cc < hlen + ICMP_MINLEN)
981 p = (union ad_u *)&buf.pkt.b[hlen];
984 #ifdef USE_PASSIFNAME
985 ifp = ifwithname(buf.ifname, 0);
987 msglim(&bad_name, from.sin_addr.s_addr,
988 "impossible rdisc if_ name %.*s",
989 IFNAMSIZ, buf.ifname);
991 /* If we could tell the interface on which a packet from
992 * address 0 arrived, we could deal with such solicitations.
994 ifp = ((from.sin_addr.s_addr == 0)
995 ? 0 : iflookup(from.sin_addr.s_addr));
997 ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
998 buf.pkt.ip.ip_dst.s_addr, p, cc);
1001 if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
1003 "discard our own Router Discovery message");
1007 switch (p->icmp.icmp_type) {
1008 case ICMP_ROUTERADVERT:
1009 if (p->ad.icmp_ad_asize*4
1010 < (int)sizeof(p->ad.icmp_ad_info[0])) {
1011 msglim(&bad_asize, from.sin_addr.s_addr,
1012 "intolerable rdisc address size=%d",
1013 p->ad.icmp_ad_asize);
1016 if (p->ad.icmp_ad_num == 0) {
1017 trace_pkt(" empty?");
1020 if (cc != (int)(sizeof(p->ad)
1021 - sizeof(p->ad.icmp_ad_info)
1022 + (p->ad.icmp_ad_num
1023 * sizeof(p->ad.icmp_ad_info[0])))) {
1024 msglim(&bad_len, from.sin_addr.s_addr,
1025 "rdisc length %d does not match ad_num"
1026 " %d", cc, p->ad.icmp_ad_num);
1031 if (ifp->int_state & IS_NO_ADV_IN)
1034 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
1035 for (n = 0; n < p->ad.icmp_ad_num; n++) {
1036 parse_ad(from.sin_addr.s_addr,
1038 ntohs(p->ad.icmp_ad_life),
1040 wp += p->ad.icmp_ad_asize;
1045 case ICMP_ROUTERSOLICIT:
1048 if (ifp->int_state & IS_NO_ADV_OUT)
1054 * We should handle messages from address 0.
1057 /* Respond with a point-to-point advertisement */
1058 send_adv(ifp, from.sin_addr.s_addr, 0);