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. 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
29 * $FreeBSD: src/sbin/routed/rdisc.c,v 1.5.2.1 2000/08/14 17:00:04 sheldonh Exp $
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/ip_icmp.h>
37 #if !defined(sgi) && !defined(__NetBSD__)
38 static char sccsid[] __attribute__((unused)) = "@(#)rdisc.c 8.1 (Berkeley) x/y/95";
39 #elif defined(__NetBSD__)
43 /* router advertisement ICMP packet */
45 u_int8_t icmp_type; /* type of message */
46 u_int8_t icmp_code; /* type sub code */
47 u_int16_t icmp_cksum; /* ones complement cksum of struct */
48 u_int8_t icmp_ad_num; /* # of following router addresses */
49 u_int8_t icmp_ad_asize; /* 2--words in each advertisement */
50 u_int16_t icmp_ad_life; /* seconds of validity */
57 /* router solicitation ICMP packet */
59 u_int8_t icmp_type; /* type of message */
60 u_int8_t icmp_code; /* type sub code */
61 u_int16_t icmp_cksum; /* ones complement cksum of struct */
72 int rdisc_sock = -1; /* router-discovery raw socket */
73 struct interface *rdisc_sock_mcast; /* current multicast interface */
75 struct timeval rdisc_timer;
76 int rdisc_ok; /* using solicited route */
79 #define MAX_ADS 16 /* at least one per interface */
80 struct dr { /* accumulated advertisements */
81 struct interface *dr_ifp;
82 naddr dr_gate; /* gateway */
83 time_t dr_ts; /* when received */
84 time_t dr_life; /* lifetime in host byte order */
85 n_long dr_recv_pref; /* received but biased preference */
86 n_long dr_pref; /* preference adjusted by metric */
87 } *cur_drp, drs[MAX_ADS];
89 /* convert between signed, balanced around zero,
90 * and unsigned zero-based preferences */
91 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel)
92 #define UNSIGN_PREF(p) SIGN_PREF(p)
93 /* adjust unsigned preference by interface metric,
94 * without driving it to infinity */
95 #define PREF(p, ifp) ((int)(p) <= (ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
96 : (p) - ((ifp)->int_metric))
98 static void rdisc_sort(void);
101 /* dump an ICMP Router Discovery Advertisement Message
104 trace_rdisc(const char *act,
107 struct interface *ifp,
115 if (!TRACEPACKETS || ftrace == 0)
120 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
121 fprintf(ftrace, "%s Router Ad"
122 " from %s to %s via %s life=%d\n",
123 act, naddr_ntoa(from), naddr_ntoa(to),
124 ifp ? ifp->int_name : "?",
125 ntohs(p->ad.icmp_ad_life));
129 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
130 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
131 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
132 fprintf(ftrace, "\t%s preference=%d",
133 naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
134 wp += p->ad.icmp_ad_asize;
139 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
140 act, naddr_ntoa(from), naddr_ntoa(to),
141 ifp ? ifp->int_name : "?",
142 (int)ntohl(p->so.icmp_so_rsvd));
146 /* prepare Router Discovery socket.
151 if (rdisc_sock < 0) {
152 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
154 BADERR(1,"rdisc_sock = socket()");
155 fix_sock(rdisc_sock,"rdisc_sock");
161 /* Pick multicast group for router-discovery socket
164 set_rdisc_mg(struct interface *ifp,
165 int on) /* 0=turn it off */
169 if (rdisc_sock < 0) {
170 /* Create the raw socket so that we can hear at least
171 * broadcast router discovery packets.
173 if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC
179 if (!(ifp->int_if_flags & IFF_MULTICAST)) {
180 ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
185 if (ifp->int_if_flags & IFF_POINTOPOINT)
188 memset(&m, 0, sizeof(m));
189 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
193 || (ifp->int_state & IS_NO_ADV_IN)
195 /* stop listening to advertisements
197 if (ifp->int_state & IS_ALL_HOSTS) {
198 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
199 if (setsockopt(rdisc_sock, IPPROTO_IP,
202 LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
203 ifp->int_state &= ~IS_ALL_HOSTS;
206 } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
207 /* start listening to advertisements
209 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
210 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
211 &m, sizeof(m)) < 0) {
212 LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
214 ifp->int_state |= IS_ALL_HOSTS;
219 || (ifp->int_state & IS_NO_ADV_OUT)
221 /* stop listening to solicitations
223 if (ifp->int_state & IS_ALL_ROUTERS) {
224 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
225 if (setsockopt(rdisc_sock, IPPROTO_IP,
228 LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
229 ifp->int_state &= ~IS_ALL_ROUTERS;
232 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
233 /* start hearing solicitations
235 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
236 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
237 &m, sizeof(m)) < 0) {
238 LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
240 ifp->int_state |= IS_ALL_ROUTERS;
246 /* start supplying routes
251 struct interface *ifp;
257 trace_act("start supplying routes");
259 /* Forget discovered routes.
261 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
262 drp->dr_recv_pref = 0;
270 /* Do not start advertising until we have heard some RIP routes */
271 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
273 /* Switch router discovery multicast groups from soliciting
276 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
277 if (ifp->int_state & IS_BROKE)
279 ifp->int_rdisc_cnt = 0;
280 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
281 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
282 set_rdisc_mg(ifp, 1);
285 /* get rid of any redirects */
290 /* age discovered routes and find the best one
293 rdisc_age(naddr bad_gate)
299 /* If only advertising, then do only that. */
301 /* If switching from client to server, get rid of old
310 /* If we are being told about a bad router,
311 * then age the discovered default route, and if there is
312 * no alternative, solicit a replacement.
315 /* Look for the bad discovered default route.
316 * Age it and note its interface.
318 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
322 /* When we find the bad router, then age the route
323 * to at most SUPPLY_INTERVAL.
324 * This is contrary to RFC 1256, but defends against
327 if (drp->dr_gate == bad_gate) {
328 sec = (now.tv_sec - drp->dr_life
330 if (drp->dr_ts > sec) {
331 trace_act("age 0.0.0.0 --> %s via %s",
332 naddr_ntoa(drp->dr_gate),
333 drp->dr_ifp->int_name);
344 /* Delete old redirected routes to keep the kernel table small,
345 * and to prevent black holes. Check that the kernel table
346 * matches the daemon table (i.e. has the default route).
347 * But only if RIP is not running and we are not dealing with
348 * a bad gateway, since otherwise age() will be called.
350 if (rip_sock < 0 && bad_gate == 0)
355 /* Zap all routes discovered via an interface that has gone bad
356 * This should only be called when !(ifp->int_state & IS_ALIAS)
359 if_bad_rdisc(struct interface *ifp)
363 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
364 if (drp->dr_ifp != ifp)
366 drp->dr_recv_pref = 0;
371 /* make a note to re-solicit, turn RIP on or off, etc. */
372 rdisc_timer.tv_sec = 0;
376 /* mark an interface ok for router discovering.
379 if_ok_rdisc(struct interface *ifp)
381 set_rdisc_mg(ifp, 1);
383 ifp->int_rdisc_cnt = 0;
384 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
386 : MAX_SOLICITATION_DELAY);
387 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
388 rdisc_timer = ifp->int_rdisc_timer;
392 /* get rid of a dead discovered router
395 del_rdisc(struct dr *drp)
397 struct interface *ifp;
402 del_redirects(gate = drp->dr_gate, 0);
407 /* Count the other discovered routes on the interface.
411 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
413 && drp->dr_ifp == ifp)
417 /* If that was the last good discovered router on the interface,
418 * then solicit a new one.
419 * This is contrary to RFC 1256, but defends against black holes.
422 trace_act("discovered router %s via %s"
423 " is bad--have %d remaining",
424 naddr_ntoa(gate), ifp->int_name, i);
425 } else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
426 trace_act("last discovered router %s via %s"
427 " is bad--re-solicit",
428 naddr_ntoa(gate), ifp->int_name);
429 ifp->int_rdisc_cnt = 0;
430 ifp->int_rdisc_timer.tv_sec = 0;
433 trace_act("last discovered router %s via %s"
434 " is bad--wait to solicit",
435 naddr_ntoa(gate), ifp->int_name);
440 /* Find the best discovered route,
441 * and discard stale routers.
446 struct dr *drp, *new_drp;
449 struct interface *ifp;
454 /* Find the best discovered route.
457 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
462 /* Get rid of expired discovered routers.
464 if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
469 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
471 /* Update preference with possibly changed interface
474 drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
476 /* Prefer the current route to prevent thrashing.
477 * Prefer shorter lifetimes to speed the detection of
479 * Avoid sick interfaces.
482 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
483 && (new_pref < drp->dr_pref
484 || (new_pref == drp->dr_pref
486 || (new_drp != cur_drp
487 && new_drp->dr_life > drp->dr_life)))))
488 || ((new_st & IS_SICK)
489 && !(drp->dr_ifp->int_state & IS_SICK))) {
491 new_st = drp->dr_ifp->int_state;
492 new_pref = drp->dr_pref;
496 /* switch to a better default route
498 if (new_drp != cur_drp) {
499 rt = rtget(RIP_DEFAULT, 0);
501 /* Stop using discovered routes if they are all bad
503 if (new_drp == NULL) {
504 trace_act("turn off Router Discovery client");
508 && (rt->rt_state & RS_RDISC)) {
509 new = rt->rt_spares[0];
510 new.rts_metric = HOPCNT_INFINITY;
511 new.rts_time = now.tv_sec - GARBAGE_TIME;
512 rtchange(rt, rt->rt_state & ~RS_RDISC,
518 if (cur_drp == NULL) {
519 trace_act("turn on Router Discovery client"
521 naddr_ntoa(new_drp->dr_gate),
522 new_drp->dr_ifp->int_name);
526 trace_act("switch Router Discovery from"
527 " %s via %s to %s via %s",
528 naddr_ntoa(cur_drp->dr_gate),
529 cur_drp->dr_ifp->int_name,
530 naddr_ntoa(new_drp->dr_gate),
531 new_drp->dr_ifp->int_name);
534 memset(&new, 0, sizeof(new));
535 new.rts_ifp = new_drp->dr_ifp;
536 new.rts_gate = new_drp->dr_gate;
537 new.rts_router = new_drp->dr_gate;
538 new.rts_metric = HOPCNT_INFINITY-1;
539 new.rts_time = now.tv_sec;
541 rtchange(rt, rt->rt_state | RS_RDISC, &new, 0);
543 rtadd(RIP_DEFAULT, 0, RS_RDISC, &new);
550 /* turn RIP on or off */
551 if (!rdisc_ok || rip_interfaces > 1) {
559 /* handle a single address in an advertisement
564 n_long pref, /* signed and in network order */
565 u_short life, /* in host byte order */
566 struct interface *ifp)
568 static struct msg_limit bad_gate;
569 struct dr *drp, *new_drp;
572 if (gate == RIP_DEFAULT
573 || !check_dst(gate)) {
574 msglim(&bad_gate, from,"router %s advertising bad gateway %s",
580 /* ignore pointers to ourself and routes via unreachable networks
582 if (ifwithaddr(gate, 1, 0) != 0) {
583 trace_pkt(" discard Router Discovery Ad pointing at us");
586 if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
587 trace_pkt(" discard Router Discovery Ad"
588 " toward unreachable net");
592 /* Convert preference to an unsigned value
593 * and later bias it by the metric of the interface.
595 pref = UNSIGN_PREF(ntohl(pref));
597 if (pref == 0 || life < MinMaxAdvertiseInterval) {
602 for (new_drp = NULL, drp = drs; drp < &drs[MAX_ADS]; drp++) {
603 /* accept new info for a familiar entry
605 if (drp->dr_gate == gate) {
611 continue; /* do not worry about dead ads */
613 if (drp->dr_ts == 0) {
614 new_drp = drp; /* use unused entry */
616 } else if (new_drp == NULL) {
617 /* look for an entry worse than the new one to
620 if ((!(ifp->int_state & IS_SICK)
621 && (drp->dr_ifp->int_state & IS_SICK))
622 || (pref > drp->dr_pref
623 && !((ifp->int_state ^ drp->dr_ifp->int_state)
627 } else if (new_drp->dr_ts != 0) {
628 /* look for the least valuable entry to reuse
630 if ((!(new_drp->dr_ifp->int_state & IS_SICK)
631 && (drp->dr_ifp->int_state & IS_SICK))
632 || (new_drp->dr_pref > drp->dr_pref
633 && !((new_drp->dr_ifp->int_state
634 ^ drp->dr_ifp->int_state)
640 /* forget it if all of the current entries are better */
644 new_drp->dr_ifp = ifp;
645 new_drp->dr_gate = gate;
646 new_drp->dr_ts = now.tv_sec;
647 new_drp->dr_life = life;
648 new_drp->dr_recv_pref = pref;
649 /* bias functional preference by metric of the interface */
650 new_drp->dr_pref = PREF(pref,ifp);
652 /* after hearing a good advertisement, stop asking
654 if (!(ifp->int_state & IS_SICK))
655 ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
659 /* Compute the IP checksum
660 * This assumes the packet is less than 32K long.
667 int nwords = len >> 1;
669 while (nwords-- != 0)
675 /* end-around-carry */
676 sum = (sum >> 16) + (sum & 0xffff);
682 /* Send a router discovery advertisement or solicitation ICMP packet.
685 send_rdisc(union ad_u *p,
687 struct interface *ifp,
688 naddr dst, /* 0 or unicast destination */
689 int type) /* 0=unicast, 1=bcast, 2=mcast */
691 struct sockaddr_in in;
697 memset(&in, 0, sizeof(in));
698 in.sin_addr.s_addr = dst;
699 in.sin_family = AF_INET;
701 in.sin_len = sizeof(in);
703 flags = MSG_DONTROUTE;
706 case 0: /* unicast */
711 case 1: /* broadcast */
712 if (ifp->int_if_flags & IFF_POINTOPOINT) {
713 msg = "Send pt-to-pt";
714 in.sin_addr.s_addr = ifp->int_dstaddr;
716 msg = "Send broadcast";
717 in.sin_addr.s_addr = ifp->int_brdaddr;
721 case 2: /* multicast */
722 msg = "Send multicast";
723 if (ifp->int_state & IS_DUP) {
724 trace_act("abort multicast output via %s"
725 " with duplicate address",
729 if (rdisc_sock_mcast != ifp) {
730 /* select the right interface. */
732 /* Do not specify the primary interface explicitly
733 * if we have the multicast point-to-point kernel
734 * bug, since the kernel will do the wrong thing
735 * if the local address of a point-to-point link
736 * is the same as the address of an ordinary
739 if (ifp->int_addr == myaddr) {
743 tgt_mcast = ifp->int_addr;
744 if (0 > setsockopt(rdisc_sock,
745 IPPROTO_IP, IP_MULTICAST_IF,
746 &tgt_mcast, sizeof(tgt_mcast))) {
747 LOGERR("setsockopt(rdisc_sock,"
749 rdisc_sock_mcast = NULL;
752 rdisc_sock_mcast = ifp;
761 trace_rdisc(msg, ifp->int_addr, in.sin_addr.s_addr, ifp,
764 if (0 > sendto(rdisc_sock, p, p_size, flags,
765 (struct sockaddr *)&in, sizeof(in))) {
766 if (ifp == NULL || !(ifp->int_state & IS_BROKE))
767 msglog("sendto(%s%s%s): %s",
768 ifp != NULL ? ifp->int_name : "",
769 ifp != NULL ? ", " : "",
770 inet_ntoa(in.sin_addr),
778 /* Send an advertisement
781 send_adv(struct interface *ifp,
782 naddr dst, /* 0 or unicast destination */
783 int type) /* 0=unicast, 1=bcast, 2=mcast */
789 memset(&u, 0, sizeof(u.ad));
791 u.ad.icmp_type = ICMP_ROUTERADVERT;
792 u.ad.icmp_ad_num = 1;
793 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
795 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
797 /* Convert the configured preference to an unsigned value,
798 * bias it by the interface metric, and then send it as a
799 * signed, network byte order value.
801 pref = UNSIGN_PREF(ifp->int_rdisc_pref);
802 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp)));
804 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
806 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
808 send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
812 /* Advertise for Router Discovery
817 struct interface *ifp;
822 rdisc_timer.tv_sec = now.tv_sec + NEVER;
824 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
825 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
828 if (!timercmp(&ifp->int_rdisc_timer, &now, >)
830 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
831 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
832 ifp->int_rdisc_cnt++;
834 intvl_random(&ifp->int_rdisc_timer,
835 (ifp->int_rdisc_int*3)/4,
837 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
838 && (ifp->int_rdisc_timer.tv_sec
839 > MAX_INITIAL_ADVERT_INTERVAL)) {
840 ifp->int_rdisc_timer.tv_sec
841 = MAX_INITIAL_ADVERT_INTERVAL;
843 timevaladd(&ifp->int_rdisc_timer, &now);
846 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
847 rdisc_timer = ifp->int_rdisc_timer;
852 /* Solicit for Router Discovery
857 struct interface *ifp;
864 rdisc_timer.tv_sec = now.tv_sec + NEVER;
866 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
867 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
868 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
871 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
872 memset(&u, 0, sizeof(u.so));
873 u.so.icmp_type = ICMP_ROUTERSOLICIT;
874 u.so.icmp_cksum = in_cksum((u_short*)&u.so,
876 send_rdisc(&u, sizeof(u.so), ifp,
877 htonl(INADDR_ALLROUTERS_GROUP),
878 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
880 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
883 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
884 ifp->int_rdisc_timer.tv_usec = 0;
885 timevaladd(&ifp->int_rdisc_timer, &now);
888 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
889 rdisc_timer = ifp->int_rdisc_timer;
894 /* check the IP header of a possible Router Discovery ICMP packet */
895 static struct interface * /* 0 if bad */
896 ck_icmp(const char *act,
898 struct interface *ifp,
906 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
907 type = "advertisement";
908 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
909 type = "solicitation";
914 if (p->icmp.icmp_code != 0) {
915 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
916 type, p->icmp.icmp_code,
917 naddr_ntoa(from), naddr_ntoa(to));
921 trace_rdisc(act, from, to, ifp, p, len);
924 trace_pkt("unknown interface for router-discovery %s"
926 type, naddr_ntoa(from), naddr_ntoa(to));
932 /* read packets from the router discovery socket
937 static struct msg_limit bad_asize, bad_len;
938 #ifdef USE_PASSIFNAME
939 static struct msg_limit bad_name;
941 struct sockaddr_in from;
945 #ifdef USE_PASSIFNAME
946 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);