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 /* router advertisement ICMP packet */
43 u_int8_t icmp_type; /* type of message */
44 u_int8_t icmp_code; /* type sub code */
45 u_int16_t icmp_cksum; /* ones complement cksum of struct */
46 u_int8_t icmp_ad_num; /* # of following router addresses */
47 u_int8_t icmp_ad_asize; /* 2--words in each advertisement */
48 u_int16_t icmp_ad_life; /* seconds of validity */
55 /* router solicitation ICMP packet */
57 u_int8_t icmp_type; /* type of message */
58 u_int8_t icmp_code; /* type sub code */
59 u_int16_t icmp_cksum; /* ones complement cksum of struct */
70 int rdisc_sock = -1; /* router-discovery raw socket */
71 static const struct interface *rdisc_sock_mcast; /* current multicast interface */
73 struct timeval rdisc_timer;
74 int rdisc_ok; /* using solicited route */
77 #define MAX_ADS 16 /* at least one per interface */
78 struct dr { /* accumulated advertisements */
79 struct interface *dr_ifp;
80 naddr dr_gate; /* gateway */
81 time_t dr_ts; /* when received */
82 time_t dr_life; /* lifetime in host byte order */
83 n_long dr_recv_pref; /* received but biased preference */
84 n_long dr_pref; /* preference adjusted by metric */
86 static const struct dr *cur_drp;
87 static struct dr 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+(ifp)->int_adj_outmetric)\
96 ? ((p) != 0 ? 1 : 0) \
97 : (p) - ((ifp)->int_metric+(ifp)->int_adj_outmetric))
99 static void rdisc_sort(void);
102 /* dump an ICMP Router Discovery Advertisement Message
105 trace_rdisc(const char *act,
108 struct interface *ifp,
116 if (!TRACEPACKETS || ftrace == NULL)
121 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
122 (void)fprintf(ftrace, "%s Router Ad"
123 " from %s to %s via %s life=%d\n",
124 act, naddr_ntoa(from), naddr_ntoa(to),
125 ifp ? ifp->int_name : "?",
126 ntohs(p->ad.icmp_ad_life));
130 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
131 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
132 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
133 (void)fprintf(ftrace, "\t%s preference=%d",
134 naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
135 wp += p->ad.icmp_ad_asize;
137 (void)fputc('\n',ftrace);
140 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
141 act, naddr_ntoa(from), naddr_ntoa(to),
142 ifp ? ifp->int_name : "?",
143 (int)ntohl(p->so.icmp_so_rsvd));
147 /* prepare Router Discovery socket.
152 if (rdisc_sock < 0) {
153 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
155 BADERR(1,"rdisc_sock = socket()");
156 fix_sock(rdisc_sock,"rdisc_sock");
162 /* Pick multicast group for router-discovery socket
165 set_rdisc_mg(struct interface *ifp,
166 int on) /* 0=turn it off */
169 struct sockaddr_in *sin;
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);
188 memset(&gr, 0, sizeof(gr));
189 gr.gr_interface = ifp->int_index;
190 sin = (struct sockaddr_in *)&gr.gr_group;
191 sin->sin_family = AF_INET;
193 sin->sin_len = sizeof(struct sockaddr_in);
197 || (ifp->int_state & IS_NO_ADV_IN)
199 /* stop listening to advertisements
201 if (ifp->int_state & IS_ALL_HOSTS) {
202 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
203 if (setsockopt(rdisc_sock, IPPROTO_IP,
205 &gr, sizeof(gr)) < 0)
206 LOGERR("MCAST_LEAVE_GROUP ALLHOSTS");
207 ifp->int_state &= ~IS_ALL_HOSTS;
210 } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
211 /* start listening to advertisements
213 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
214 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP,
215 &gr, sizeof(gr)) < 0) {
216 LOGERR("MCAST_JOIN_GROUP ALLHOSTS");
218 ifp->int_state |= IS_ALL_HOSTS;
223 || (ifp->int_state & IS_NO_ADV_OUT)
225 /* stop listening to solicitations
227 if (ifp->int_state & IS_ALL_ROUTERS) {
228 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP);
229 if (setsockopt(rdisc_sock, IPPROTO_IP,
231 &gr, sizeof(gr)) < 0)
232 LOGERR("MCAST_LEAVE_GROUP ALLROUTERS");
233 ifp->int_state &= ~IS_ALL_ROUTERS;
236 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
237 /* start hearing solicitations
239 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP);
240 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP,
241 &gr, sizeof(gr)) < 0) {
242 LOGERR("MCAST_JOIN_GROUP ALLROUTERS");
244 ifp->int_state |= IS_ALL_ROUTERS;
250 /* start supplying routes
255 struct interface *ifp;
261 trace_act("start supplying routes");
263 /* Forget discovered routes.
265 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
266 drp->dr_recv_pref = 0;
274 /* Do not start advertising until we have heard some RIP routes */
275 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
277 /* Switch router discovery multicast groups from soliciting
280 LIST_FOREACH(ifp, &ifnet, int_list) {
281 if (ifp->int_state & IS_BROKE)
283 ifp->int_rdisc_cnt = 0;
284 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
285 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
286 set_rdisc_mg(ifp, 1);
289 /* get rid of any redirects */
294 /* age discovered routes and find the best one
297 rdisc_age(naddr bad_gate)
303 /* If only advertising, then do only that. */
305 /* If switching from client to server, get rid of old
314 /* If we are being told about a bad router,
315 * then age the discovered default route, and if there is
316 * no alternative, solicit a replacement.
319 /* Look for the bad discovered default route.
320 * Age it and note its interface.
322 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
326 /* When we find the bad router, then age the route
327 * to at most SUPPLY_INTERVAL.
328 * This is contrary to RFC 1256, but defends against
331 if (drp->dr_gate == bad_gate) {
332 sec = (now.tv_sec - drp->dr_life
334 if (drp->dr_ts > sec) {
335 trace_act("age 0.0.0.0 --> %s via %s",
336 naddr_ntoa(drp->dr_gate),
337 drp->dr_ifp->int_name);
348 /* Delete old redirected routes to keep the kernel table small,
349 * and to prevent black holes. Check that the kernel table
350 * matches the daemon table (i.e. has the default route).
351 * But only if RIP is not running and we are not dealing with
352 * a bad gateway, since otherwise age() will be called.
354 if (rip_sock < 0 && bad_gate == 0)
359 /* Zap all routes discovered via an interface that has gone bad
360 * This should only be called when !(ifp->int_state & IS_ALIAS)
363 if_bad_rdisc(struct interface *ifp)
367 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
368 if (drp->dr_ifp != ifp)
370 drp->dr_recv_pref = 0;
375 /* make a note to re-solicit, turn RIP on or off, etc. */
376 rdisc_timer.tv_sec = 0;
380 /* mark an interface ok for router discovering.
383 if_ok_rdisc(struct interface *ifp)
385 set_rdisc_mg(ifp, 1);
387 ifp->int_rdisc_cnt = 0;
388 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
390 : MAX_SOLICITATION_DELAY);
391 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
392 rdisc_timer = ifp->int_rdisc_timer;
396 /* get rid of a dead discovered router
399 del_rdisc(struct dr *drp)
401 struct interface *ifp;
406 del_redirects(gate = drp->dr_gate, 0);
411 /* Count the other discovered routes on the interface.
415 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
417 && drp->dr_ifp == ifp)
421 /* If that was the last good discovered router on the interface,
422 * then solicit a new one.
423 * This is contrary to RFC 1256, but defends against black holes.
426 trace_act("discovered router %s via %s"
427 " is bad--have %d remaining",
428 naddr_ntoa(gate), ifp->int_name, i);
429 } else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
430 trace_act("last discovered router %s via %s"
431 " is bad--re-solicit",
432 naddr_ntoa(gate), ifp->int_name);
433 ifp->int_rdisc_cnt = 0;
434 ifp->int_rdisc_timer.tv_sec = 0;
437 trace_act("last discovered router %s via %s"
438 " is bad--wait to solicit",
439 naddr_ntoa(gate), ifp->int_name);
444 /* Find the best discovered route,
445 * and discard stale routers.
450 struct dr *drp, *new_drp;
453 struct interface *ifp;
458 /* Find the best discovered route.
461 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
466 /* Get rid of expired discovered routers.
468 if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
473 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
475 /* Update preference with possibly changed interface
478 drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
480 /* Prefer the current route to prevent thrashing.
481 * Prefer shorter lifetimes to speed the detection of
483 * Avoid sick interfaces.
486 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
487 && (new_pref < drp->dr_pref
488 || (new_pref == drp->dr_pref
490 || (new_drp != cur_drp
491 && new_drp->dr_life > drp->dr_life)))))
492 || ((new_st & IS_SICK)
493 && !(drp->dr_ifp->int_state & IS_SICK))) {
495 new_st = drp->dr_ifp->int_state;
496 new_pref = drp->dr_pref;
500 /* switch to a better default route
502 if (new_drp != cur_drp) {
503 rt = rtget(RIP_DEFAULT, 0);
505 /* Stop using discovered routes if they are all bad
507 if (new_drp == NULL) {
508 trace_act("turn off Router Discovery client");
512 && (rt->rt_state & RS_RDISC)) {
513 new = rt->rt_spares[0];
514 new.rts_metric = HOPCNT_INFINITY;
515 new.rts_time = now.tv_sec - GARBAGE_TIME;
516 rtchange(rt, rt->rt_state & ~RS_RDISC,
522 if (cur_drp == NULL) {
523 trace_act("turn on Router Discovery client"
525 naddr_ntoa(new_drp->dr_gate),
526 new_drp->dr_ifp->int_name);
530 trace_act("switch Router Discovery from"
531 " %s via %s to %s via %s",
532 naddr_ntoa(cur_drp->dr_gate),
533 cur_drp->dr_ifp->int_name,
534 naddr_ntoa(new_drp->dr_gate),
535 new_drp->dr_ifp->int_name);
538 memset(&new, 0, sizeof(new));
539 new.rts_ifp = new_drp->dr_ifp;
540 new.rts_gate = new_drp->dr_gate;
541 new.rts_router = new_drp->dr_gate;
542 new.rts_metric = HOPCNT_INFINITY-1;
543 new.rts_time = now.tv_sec;
545 rtchange(rt, rt->rt_state | RS_RDISC, &new, 0);
547 rtadd(RIP_DEFAULT, 0, RS_RDISC, &new);
554 /* turn RIP on or off */
555 if (!rdisc_ok || rip_interfaces > 1) {
563 /* handle a single address in an advertisement
568 n_long pref, /* signed and in network order */
569 u_short life, /* in host byte order */
570 struct interface *ifp)
572 static struct msg_limit bad_gate;
573 struct dr *drp, *new_drp;
576 if (gate == RIP_DEFAULT
577 || !check_dst(gate)) {
578 msglim(&bad_gate, from,"router %s advertising bad gateway %s",
584 /* ignore pointers to ourself and routes via unreachable networks
586 if (ifwithaddr(gate, 1, 0) != NULL) {
587 trace_pkt(" discard Router Discovery Ad pointing at us");
590 if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
591 trace_pkt(" discard Router Discovery Ad"
592 " toward unreachable net");
596 /* Convert preference to an unsigned value
597 * and later bias it by the metric of the interface.
599 pref = UNSIGN_PREF(ntohl(pref));
601 if (pref == 0 || life < MinMaxAdvertiseInterval) {
606 for (new_drp = NULL, drp = drs; drp < &drs[MAX_ADS]; drp++) {
607 /* accept new info for a familiar entry
609 if (drp->dr_gate == gate) {
615 continue; /* do not worry about dead ads */
617 if (drp->dr_ts == 0) {
618 new_drp = drp; /* use unused entry */
620 } else if (new_drp == NULL) {
621 /* look for an entry worse than the new one to
624 if ((!(ifp->int_state & IS_SICK)
625 && (drp->dr_ifp->int_state & IS_SICK))
626 || (pref > drp->dr_pref
627 && !((ifp->int_state ^ drp->dr_ifp->int_state)
631 } else if (new_drp->dr_ts != 0) {
632 /* look for the least valuable entry to reuse
634 if ((!(new_drp->dr_ifp->int_state & IS_SICK)
635 && (drp->dr_ifp->int_state & IS_SICK))
636 || (new_drp->dr_pref > drp->dr_pref
637 && !((new_drp->dr_ifp->int_state
638 ^ drp->dr_ifp->int_state)
644 /* forget it if all of the current entries are better */
648 new_drp->dr_ifp = ifp;
649 new_drp->dr_gate = gate;
650 new_drp->dr_ts = now.tv_sec;
651 new_drp->dr_life = life;
652 new_drp->dr_recv_pref = pref;
653 /* bias functional preference by metric of the interface */
654 new_drp->dr_pref = PREF(pref,ifp);
656 /* after hearing a good advertisement, stop asking
658 if (!(ifp->int_state & IS_SICK))
659 ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
663 /* Compute the IP checksum
664 * This assumes the packet is less than 32K long.
671 int nwords = len >> 1;
673 while (nwords-- != 0)
679 /* end-around-carry */
680 sum = (sum >> 16) + (sum & 0xffff);
686 /* Send a router discovery advertisement or solicitation ICMP packet.
689 send_rdisc(union ad_u *p,
691 struct interface *ifp,
692 naddr dst, /* 0 or unicast destination */
693 int type) /* 0=unicast, 1=bcast, 2=mcast */
695 struct sockaddr_in rsin;
700 memset(&rsin, 0, sizeof(rsin));
701 rsin.sin_addr.s_addr = dst;
702 rsin.sin_family = AF_INET;
704 rsin.sin_len = sizeof(rsin);
706 flags = MSG_DONTROUTE;
709 case 0: /* unicast */
714 case 1: /* broadcast */
715 if (ifp->int_if_flags & IFF_POINTOPOINT) {
716 msg = "Send pt-to-pt";
717 rsin.sin_addr.s_addr = ifp->int_dstaddr;
719 msg = "Send broadcast";
720 rsin.sin_addr.s_addr = ifp->int_brdaddr;
724 case 2: /* multicast */
725 msg = "Send multicast";
726 if (ifp->int_state & IS_DUP) {
727 trace_act("abort multicast output via %s"
728 " with duplicate address",
732 if (rdisc_sock_mcast != ifp) {
733 /* select the right interface. */
734 struct ip_mreqn mreqn;
736 memset(&mreqn, 0, sizeof(struct ip_mreqn));
737 mreqn.imr_ifindex = ifp->int_index;
738 if (0 > setsockopt(rdisc_sock,
739 IPPROTO_IP, IP_MULTICAST_IF,
742 LOGERR("setsockopt(rdisc_sock,"
744 rdisc_sock_mcast = NULL;
747 rdisc_sock_mcast = ifp;
756 trace_rdisc(msg, (ifp ? ifp->int_addr : 0), rsin.sin_addr.s_addr, ifp,
759 if (0 > sendto(rdisc_sock, p, p_size, flags,
760 (struct sockaddr *)&rsin, sizeof(rsin))) {
761 if (ifp == NULL || !(ifp->int_state & IS_BROKE))
762 msglog("sendto(%s%s%s): %s",
763 ifp != NULL ? ifp->int_name : "",
764 ifp != NULL ? ", " : "",
765 inet_ntoa(rsin.sin_addr),
773 /* Send an advertisement
776 send_adv(struct interface *ifp,
777 naddr dst, /* 0 or unicast destination */
778 int type) /* 0=unicast, 1=bcast, 2=mcast */
784 memset(&u, 0, sizeof(u.ad));
786 u.ad.icmp_type = ICMP_ROUTERADVERT;
787 u.ad.icmp_ad_num = 1;
788 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
790 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
792 /* Convert the configured preference to an unsigned value,
793 * bias it by the interface metric, and then send it as a
794 * signed, network byte order value.
796 pref = UNSIGN_PREF(ifp->int_rdisc_pref);
797 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp)));
799 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
801 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
803 send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
807 /* Advertise for Router Discovery
812 struct interface *ifp;
817 rdisc_timer.tv_sec = now.tv_sec + NEVER;
819 LIST_FOREACH(ifp, &ifnet, int_list) {
820 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
823 if (!timercmp(&ifp->int_rdisc_timer, &now, >)
825 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
826 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
827 ifp->int_rdisc_cnt++;
829 intvl_random(&ifp->int_rdisc_timer,
830 (ifp->int_rdisc_int*3)/4,
832 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
833 && (ifp->int_rdisc_timer.tv_sec
834 > MAX_INITIAL_ADVERT_INTERVAL)) {
835 ifp->int_rdisc_timer.tv_sec
836 = MAX_INITIAL_ADVERT_INTERVAL;
838 timevaladd(&ifp->int_rdisc_timer, &now);
841 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
842 rdisc_timer = ifp->int_rdisc_timer;
847 /* Solicit for Router Discovery
852 struct interface *ifp;
859 rdisc_timer.tv_sec = now.tv_sec + NEVER;
861 LIST_FOREACH(ifp, &ifnet, int_list) {
862 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
863 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
866 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
867 memset(&u, 0, sizeof(u.so));
868 u.so.icmp_type = ICMP_ROUTERSOLICIT;
869 u.so.icmp_cksum = in_cksum((u_short*)&u.so,
871 send_rdisc(&u, sizeof(u.so), ifp,
872 htonl(INADDR_ALLROUTERS_GROUP),
873 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
875 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
878 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
879 ifp->int_rdisc_timer.tv_usec = 0;
880 timevaladd(&ifp->int_rdisc_timer, &now);
883 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
884 rdisc_timer = ifp->int_rdisc_timer;
889 /* check the IP header of a possible Router Discovery ICMP packet */
890 static struct interface * /* 0 if bad */
891 ck_icmp(const char *act,
893 struct interface *ifp,
901 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
902 type = "advertisement";
903 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
904 type = "solicitation";
909 if (p->icmp.icmp_code != 0) {
910 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
911 type, p->icmp.icmp_code,
912 naddr_ntoa(from), naddr_ntoa(to));
916 trace_rdisc(act, from, to, ifp, p, len);
919 trace_pkt("unknown interface for router-discovery %s"
921 type, naddr_ntoa(from), naddr_ntoa(to));
927 /* read packets from the router discovery socket
932 static struct msg_limit bad_asize, bad_len;
933 #ifdef USE_PASSIFNAME
934 static struct msg_limit bad_name;
936 struct sockaddr_in from;
937 int n, fromlen, cc, hlen;
939 #ifdef USE_PASSIFNAME
940 char ifname[IFNAMSIZ];
949 struct interface *ifp;
953 fromlen = sizeof(from);
954 cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0,
955 (struct sockaddr*)&from,
958 if (cc < 0 && errno != EWOULDBLOCK)
959 LOGERR("recvfrom(rdisc_sock)");
962 if (fromlen != sizeof(struct sockaddr_in))
963 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
965 #ifdef USE_PASSIFNAME
966 if ((cc -= sizeof(buf.ifname)) < 0)
967 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
968 cc+sizeof(buf.ifname));
971 hlen = buf.pkt.ip.ip_hl << 2;
972 if (cc < hlen + ICMP_MINLEN)
974 p = (union ad_u *)&buf.pkt.b[hlen];
977 #ifdef USE_PASSIFNAME
978 ifp = ifwithname(buf.ifname, 0);
980 msglim(&bad_name, from.sin_addr.s_addr,
981 "impossible rdisc if_ name %.*s",
982 IFNAMSIZ, buf.ifname);
984 /* If we could tell the interface on which a packet from
985 * address 0 arrived, we could deal with such solicitations.
987 ifp = ((from.sin_addr.s_addr == 0)
988 ? 0 : iflookup(from.sin_addr.s_addr));
990 ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
991 buf.pkt.ip.ip_dst.s_addr, p, cc);
994 if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
996 "discard our own Router Discovery message");
1000 switch (p->icmp.icmp_type) {
1001 case ICMP_ROUTERADVERT:
1002 if (p->ad.icmp_ad_asize*4
1003 < (int)sizeof(p->ad.icmp_ad_info[0])) {
1004 msglim(&bad_asize, from.sin_addr.s_addr,
1005 "intolerable rdisc address size=%d",
1006 p->ad.icmp_ad_asize);
1009 if (p->ad.icmp_ad_num == 0) {
1010 trace_pkt(" empty?");
1013 if (cc != (int)(sizeof(p->ad)
1014 - sizeof(p->ad.icmp_ad_info)
1015 + (p->ad.icmp_ad_num
1016 * sizeof(p->ad.icmp_ad_info[0])))) {
1017 msglim(&bad_len, from.sin_addr.s_addr,
1018 "rdisc length %d does not match ad_num"
1019 " %d", cc, p->ad.icmp_ad_num);
1024 if (ifp->int_state & IS_NO_ADV_IN)
1027 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
1028 for (n = 0; n < p->ad.icmp_ad_num; n++) {
1029 parse_ad(from.sin_addr.s_addr,
1031 ntohs(p->ad.icmp_ad_life),
1033 wp += p->ad.icmp_ad_asize;
1038 case ICMP_ROUTERSOLICIT:
1041 if (ifp->int_state & IS_NO_ADV_OUT)
1047 * We should handle messages from address 0.
1050 /* Respond with a point-to-point advertisement */
1051 send_adv(ifp, from.sin_addr.s_addr, 0);