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 acknowledgement:
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
34 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
35 static char sccsid[] = "@(#)rdisc.c 8.1 (Berkeley) x/y/95";
36 #elif defined(__NetBSD__)
37 static char rcsid[] = "$NetBSD$";
39 #ident "$Revision: 1.20 $"
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/ip_icmp.h>
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 */
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 */
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 /* adjust preference by interface metric without driving it to infinity */
93 #define PREF(p, ifp) ((p) <= (ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
94 : (p) - ((ifp)->int_metric))
96 static void rdisc_sort(void);
99 /* dump an ICMP Router Discovery Advertisement Message
102 trace_rdisc(char *act,
105 struct interface *ifp,
113 if (!TRACEPACKETS || ftrace == 0)
118 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
119 (void)fprintf(ftrace, "%s Router Ad"
120 " from %s to %s via %s life=%d\n",
121 act, naddr_ntoa(from), naddr_ntoa(to),
122 ifp ? ifp->int_name : "?",
123 ntohs(p->ad.icmp_ad_life));
127 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
128 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
129 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
130 (void)fprintf(ftrace, "\t%s preference=%d",
131 naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
132 wp += p->ad.icmp_ad_asize;
134 (void)fputc('\n',ftrace);
137 trace_act("%s Router Solic. from %s to %s via %s value=%#x",
138 act, naddr_ntoa(from), naddr_ntoa(to),
139 ifp ? ifp->int_name : "?",
140 ntohl(p->so.icmp_so_rsvd));
144 /* prepare Router Discovery socket.
149 if (rdisc_sock < 0) {
150 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
152 BADERR(1,"rdisc_sock = socket()");
153 fix_sock(rdisc_sock,"rdisc_sock");
159 /* Pick multicast group for router-discovery socket
162 set_rdisc_mg(struct interface *ifp,
163 int on) /* 0=turn it off */
167 if (rdisc_sock < 0) {
168 /* Create the raw socket so that we can hear at least
169 * broadcast router discovery packets.
171 if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC
177 if (!(ifp->int_if_flags & IFF_MULTICAST)) {
178 ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
183 if (ifp->int_if_flags & IFF_POINTOPOINT)
186 bzero(&m, sizeof(m));
187 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
191 || (ifp->int_state & IS_NO_ADV_IN)
193 /* stop listening to advertisements
195 if (ifp->int_state & IS_ALL_HOSTS) {
196 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
197 if (setsockopt(rdisc_sock, IPPROTO_IP,
200 LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
201 ifp->int_state &= ~IS_ALL_HOSTS;
204 } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
205 /* start listening to advertisements
207 m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
208 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
209 &m, sizeof(m)) < 0) {
210 LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
212 ifp->int_state |= IS_ALL_HOSTS;
217 || (ifp->int_state & IS_NO_ADV_OUT)
219 /* stop listening to solicitations
221 if (ifp->int_state & IS_ALL_ROUTERS) {
222 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
223 if (setsockopt(rdisc_sock, IPPROTO_IP,
226 LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
227 ifp->int_state &= ~IS_ALL_ROUTERS;
230 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
231 /* start hearing solicitations
233 m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
234 if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
235 &m, sizeof(m)) < 0) {
236 LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
238 ifp->int_state |= IS_ALL_ROUTERS;
244 /* start supplying routes
249 struct interface *ifp;
255 trace_act("start suppying routes");
257 /* Forget discovered routes.
259 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
260 drp->dr_recv_pref = 0;
268 /* Do not start advertising until we have heard some RIP routes */
269 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
271 /* Switch router discovery multicast groups from soliciting
274 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
275 if (ifp->int_state & IS_BROKE)
277 ifp->int_rdisc_cnt = 0;
278 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
279 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
280 set_rdisc_mg(ifp, 1);
283 /* get rid of any redirects */
288 /* age discovered routes and find the best one
291 rdisc_age(naddr bad_gate)
297 /* If only adverising, then do only that. */
299 /* if switching from client to server, get rid of old
308 /* If we are being told about a bad router,
309 * then age the discovered default route, and if there is
310 * no alternative, solicite a replacement.
313 /* Look for the bad discovered default route.
314 * Age it and note its interface.
316 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
320 /* When we find the bad router, then age the route
321 * to at most SUPPLY_INTERVAL.
322 * This is contrary to RFC 1256, but defends against
325 if (drp->dr_gate == bad_gate) {
326 sec = (now.tv_sec - drp->dr_life
328 if (drp->dr_ts > sec) {
329 trace_act("age 0.0.0.0 --> %s via %s",
330 naddr_ntoa(drp->dr_gate),
331 drp->dr_ifp->int_name);
339 /* delete old redirected routes to keep the kernel table small
341 sec = (cur_drp == 0) ? MaxMaxAdvertiseInterval : cur_drp->dr_life;
342 del_redirects(bad_gate, now.tv_sec-sec);
350 /* Zap all routes discovered via an interface that has gone bad
351 * This should only be called when !(ifp->int_state & IS_ALIAS)
354 if_bad_rdisc(struct interface *ifp)
358 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
359 if (drp->dr_ifp != ifp)
361 drp->dr_recv_pref = 0;
369 /* mark an interface ok for router discovering.
372 if_ok_rdisc(struct interface *ifp)
374 set_rdisc_mg(ifp, 1);
376 ifp->int_rdisc_cnt = 0;
377 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
379 : MAX_SOLICITATION_DELAY);
380 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
381 rdisc_timer = ifp->int_rdisc_timer;
385 /* get rid of a dead discovered router
388 del_rdisc(struct dr *drp)
390 struct interface *ifp;
394 del_redirects(drp->dr_gate, 0);
399 /* Count the other discovered routes on the interface.
403 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
405 && drp->dr_ifp == ifp)
409 /* If that was the last good discovered router on the interface,
410 * then solicit a new one.
411 * This is contrary to RFC 1256, but defends against black holes.
414 && ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
415 trace_act("discovered route is bad--re-solicit routers via %s",
417 ifp->int_rdisc_cnt = 0;
418 ifp->int_rdisc_timer.tv_sec = 0;
424 /* Find the best discovered route,
425 * and discard stale routers.
430 struct dr *drp, *new_drp;
432 struct interface *ifp;
437 /* Find the best discovered route.
440 for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
445 /* Get rid of expired discovered routers.
447 if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
452 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
454 /* Update preference with possibly changed interface
457 drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
459 /* Prefer the current route to prevent thrashing.
460 * Prefer shorter lifetimes to speed the detection of
462 * Avoid sick interfaces.
465 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
466 && (new_pref < drp->dr_pref
467 || (new_pref == drp->dr_pref
469 || (new_drp != cur_drp
470 && new_drp->dr_life > drp->dr_life)))))
471 || ((new_st & IS_SICK)
472 && !(drp->dr_ifp->int_state & IS_SICK))) {
474 new_st = drp->dr_ifp->int_state;
475 new_pref = drp->dr_pref;
479 /* switch to a better default route
481 if (new_drp != cur_drp) {
482 rt = rtget(RIP_DEFAULT, 0);
484 /* Stop using discovered routes if they are all bad
487 trace_act("turn off Router Discovery client");
491 && (rt->rt_state & RS_RDISC)) {
492 rtchange(rt, rt->rt_state & ~RS_RDISC,
493 rt->rt_gate, rt->rt_router,
494 HOPCNT_INFINITY, 0, rt->rt_ifp,
495 now.tv_sec - GARBAGE_TIME, 0);
499 /* turn on RIP if permitted */
504 trace_act("turn on Router Discovery client"
506 naddr_ntoa(new_drp->dr_gate),
507 new_drp->dr_ifp->int_name);
512 trace_act("switch Router Discovery from"
513 " %s via %s to %s via %s",
514 naddr_ntoa(cur_drp->dr_gate),
515 cur_drp->dr_ifp->int_name,
516 naddr_ntoa(new_drp->dr_gate),
517 new_drp->dr_ifp->int_name);
521 rtchange(rt, rt->rt_state | RS_RDISC,
522 new_drp->dr_gate, new_drp->dr_gate,
523 0,0, new_drp->dr_ifp,
526 rtadd(RIP_DEFAULT, 0,
527 new_drp->dr_gate, new_drp->dr_gate,
528 HOPCNT_INFINITY-1, 0,
529 RS_RDISC, new_drp->dr_ifp);
532 /* Now turn off RIP and delete RIP routes,
533 * which might otherwise include the default
544 /* handle a single address in an advertisement
551 struct interface *ifp)
553 static struct msg_limit bad_gate;
554 struct dr *drp, *new_drp;
557 if (gate == RIP_DEFAULT
558 || !check_dst(gate)) {
559 msglim(&bad_gate, from,"router %s advertising bad gateway %s",
565 /* ignore pointers to ourself and routes via unreachable networks
567 if (ifwithaddr(gate, 1, 0) != 0) {
568 trace_pkt(" discard Router Discovery Ad pointing at us");
571 if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
572 trace_pkt(" discard Router Discovery Ad"
573 " toward unreachable net");
577 /* Convert preference to an unsigned value
578 * and later bias it by the metric of the interface.
580 pref = ntohl(pref) ^ MIN_PreferenceLevel;
582 if (pref == 0 || life == 0) {
587 for (new_drp = 0, drp = drs; drp < &drs[MAX_ADS]; drp++) {
588 /* accept new info for a familiar entry
590 if (drp->dr_gate == gate) {
596 continue; /* do not worry about dead ads */
598 if (drp->dr_ts == 0) {
599 new_drp = drp; /* use unused entry */
601 } else if (new_drp == 0) {
602 /* look for an entry worse than the new one to
605 if ((!(ifp->int_state & IS_SICK)
606 && (drp->dr_ifp->int_state & IS_SICK))
607 || (pref > drp->dr_pref
608 && !((ifp->int_state ^ drp->dr_ifp->int_state)
612 } else if (new_drp->dr_ts != 0) {
613 /* look for the least valueable entry to reuse
615 if ((!(new_drp->dr_ifp->int_state & IS_SICK)
616 && (drp->dr_ifp->int_state & IS_SICK))
617 || (new_drp->dr_pref > drp->dr_pref
618 && !((new_drp->dr_ifp->int_state
619 ^ drp->dr_ifp->int_state)
625 /* forget it if all of the current entries are better */
629 new_drp->dr_ifp = ifp;
630 new_drp->dr_gate = gate;
631 new_drp->dr_ts = now.tv_sec;
632 new_drp->dr_life = ntohs(life);
633 new_drp->dr_recv_pref = pref;
634 /* bias functional preference by metric of the interface */
635 new_drp->dr_pref = PREF(pref,ifp);
637 /* after hearing a good advertisement, stop asking
639 if (!(ifp->int_state & IS_SICK))
640 ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
644 /* Compute the IP checksum
645 * This assumes the packet is less than 32K long.
652 int nwords = len >> 1;
654 while (nwords-- != 0)
660 /* end-around-carry */
661 sum = (sum >> 16) + (sum & 0xffff);
667 /* Send a router discovery advertisement or solicitation ICMP packet.
670 send_rdisc(union ad_u *p,
672 struct interface *ifp,
673 naddr dst, /* 0 or unicast destination */
674 int type) /* 0=unicast, 1=bcast, 2=mcast */
676 struct sockaddr_in sin;
682 bzero(&sin, sizeof(sin));
683 sin.sin_addr.s_addr = dst;
684 sin.sin_family = AF_INET;
686 sin.sin_len = sizeof(sin);
688 flags = MSG_DONTROUTE;
691 case 0: /* unicast */
696 case 1: /* broadcast */
697 if (ifp->int_if_flags & IFF_POINTOPOINT) {
698 msg = "Send pt-to-pt";
699 sin.sin_addr.s_addr = ifp->int_dstaddr;
701 msg = "Send broadcast";
702 sin.sin_addr.s_addr = ifp->int_brdaddr;
706 case 2: /* multicast */
707 msg = "Send multicast";
708 if (ifp->int_state & IS_DUP) {
709 trace_act("abort multicast output via %s"
710 " with duplicate address",
714 if (rdisc_sock_mcast != ifp) {
715 /* select the right interface. */
717 /* Do not specifiy the primary interface explicitly
718 * if we have the multicast point-to-point kernel
719 * bug, since the kernel will do the wrong thing
720 * if the local address of a point-to-point link
721 * is the same as the address of an ordinary
724 if (ifp->int_addr == myaddr) {
728 tgt_mcast = ifp->int_addr;
729 if (0 > setsockopt(rdisc_sock,
730 IPPROTO_IP, IP_MULTICAST_IF,
731 &tgt_mcast, sizeof(tgt_mcast))) {
732 LOGERR("setsockopt(rdisc_sock,"
734 rdisc_sock_mcast = 0;
737 rdisc_sock_mcast = ifp;
746 trace_rdisc(msg, ifp->int_addr, sin.sin_addr.s_addr, ifp,
749 if (0 > sendto(rdisc_sock, p, p_size, flags,
750 (struct sockaddr *)&sin, sizeof(sin))) {
751 if (ifp == 0 || !(ifp->int_state & IS_BROKE))
752 msglog("sendto(%s%s%s): %s",
753 ifp != 0 ? ifp->int_name : "",
754 ifp != 0 ? ", " : "",
755 inet_ntoa(sin.sin_addr),
763 /* Send an advertisement
766 send_adv(struct interface *ifp,
767 naddr dst, /* 0 or unicast destination */
768 int type) /* 0=unicast, 1=bcast, 2=mcast */
774 bzero(&u,sizeof(u.ad));
776 u.ad.icmp_type = ICMP_ROUTERADVERT;
777 u.ad.icmp_ad_num = 1;
778 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
780 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
781 pref = ifp->int_rdisc_pref ^ MIN_PreferenceLevel;
782 pref = PREF(pref, ifp) ^ MIN_PreferenceLevel;
783 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(pref);
785 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
787 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
789 send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
793 /* Advertise for Router Discovery
798 struct interface *ifp;
803 rdisc_timer.tv_sec = now.tv_sec + NEVER;
805 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
806 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
809 if (!timercmp(&ifp->int_rdisc_timer, &now, >)
811 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
812 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
813 ifp->int_rdisc_cnt++;
815 intvl_random(&ifp->int_rdisc_timer,
816 (ifp->int_rdisc_int*3)/4,
818 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
819 && (ifp->int_rdisc_timer.tv_sec
820 > MAX_INITIAL_ADVERT_INTERVAL)) {
821 ifp->int_rdisc_timer.tv_sec
822 = MAX_INITIAL_ADVERT_INTERVAL;
824 timevaladd(&ifp->int_rdisc_timer, &now);
827 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
828 rdisc_timer = ifp->int_rdisc_timer;
833 /* Solicit for Router Discovery
838 struct interface *ifp;
845 rdisc_timer.tv_sec = now.tv_sec + NEVER;
847 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
848 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
849 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
852 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
853 bzero(&u,sizeof(u.so));
854 u.so.icmp_type = ICMP_ROUTERSOLICIT;
855 u.so.icmp_cksum = in_cksum((u_short*)&u.so,
857 send_rdisc(&u, sizeof(u.so), ifp,
858 htonl(INADDR_ALLROUTERS_GROUP),
859 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
861 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
864 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
865 ifp->int_rdisc_timer.tv_usec = 0;
866 timevaladd(&ifp->int_rdisc_timer, &now);
869 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
870 rdisc_timer = ifp->int_rdisc_timer;
875 /* check the IP header of a possible Router Discovery ICMP packet */
876 static struct interface * /* 0 if bad */
879 struct interface *ifp,
887 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
888 type = "advertisement";
889 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
890 type = "solicitation";
895 if (p->icmp.icmp_code != 0) {
896 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
897 type, p->icmp.icmp_code,
898 naddr_ntoa(from), naddr_ntoa(to));
902 trace_rdisc(act, from, to, ifp, p, len);
905 trace_pkt("unknown interface for router-discovery %s"
907 type, naddr_ntoa(from), naddr_ntoa(to));
913 /* read packets from the router discovery socket
918 static struct msg_limit bad_asize, bad_len;
919 #ifdef USE_PASSIFNAME
920 static struct msg_limit bad_name;
922 struct sockaddr_in from;
923 int n, fromlen, cc, hlen;
925 #ifdef USE_PASSIFNAME
926 char ifname[IFNAMSIZ];
936 struct interface *ifp;
940 fromlen = sizeof(from);
941 cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0,
942 (struct sockaddr*)&from,
945 if (cc < 0 && errno != EWOULDBLOCK)
946 LOGERR("recvfrom(rdisc_sock)");
949 if (fromlen != sizeof(struct sockaddr_in))
950 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
952 #ifdef USE_PASSIFNAME
953 if ((cc -= sizeof(buf.ifname)) < 0)
954 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
955 cc+sizeof(buf.ifname));
958 hlen = buf.pkt.ip.ip_hl << 2;
959 if (cc < hlen + ICMP_MINLEN)
961 p = (union ad_u *)&buf.pkt.b[hlen];
964 #ifdef USE_PASSIFNAME
965 ifp = ifwithname(buf.ifname, 0);
967 msglim(&bad_name, from.sin_addr.s_addr,
968 "impossible rdisc if_ name %.*s",
969 IFNAMSIZ, buf.ifname);
971 /* If we could tell the interface on which a packet from
972 * address 0 arrived, we could deal with such solicitations.
974 ifp = ((from.sin_addr.s_addr == 0)
975 ? 0 : iflookup(from.sin_addr.s_addr));
977 ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
978 buf.pkt.ip.ip_dst.s_addr, p, cc);
981 if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
983 "discard our own Router Discovery message");
987 switch (p->icmp.icmp_type) {
988 case ICMP_ROUTERADVERT:
989 if (p->ad.icmp_ad_asize*4
990 < sizeof(p->ad.icmp_ad_info[0])) {
991 msglim(&bad_asize, from.sin_addr.s_addr,
992 "intolerable rdisc address size=%d",
993 p->ad.icmp_ad_asize);
996 if (p->ad.icmp_ad_num == 0) {
997 trace_pkt(" empty?");
1000 if (cc != (sizeof(p->ad) - sizeof(p->ad.icmp_ad_info)
1001 + (p->ad.icmp_ad_num
1002 * sizeof(p->ad.icmp_ad_info[0])))) {
1003 msglim(&bad_len, from.sin_addr.s_addr,
1004 "rdisc length %d does not match ad_num"
1005 " %d", cc, p->ad.icmp_ad_num);
1010 if (ifp->int_state & IS_NO_ADV_IN)
1013 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
1014 for (n = 0; n < p->ad.icmp_ad_num; n++) {
1015 parse_ad(from.sin_addr.s_addr,
1017 ntohs(p->ad.icmp_ad_life),
1019 wp += p->ad.icmp_ad_asize;
1024 case ICMP_ROUTERSOLICIT:
1027 if (ifp->int_state & IS_NO_ADV_OUT)
1031 * We should handle messages from address 0.
1034 /* Respond with a point-to-point advertisement */
1035 send_adv(ifp, from.sin_addr.s_addr, 0);