rdisc.c revision 1.1 1 1.1 thorpej /*
2 1.1 thorpej * Copyright (c) 1995
3 1.1 thorpej * The Regents of the University of California. All rights reserved.
4 1.1 thorpej *
5 1.1 thorpej * Redistribution and use in source and binary forms, with or without
6 1.1 thorpej * modification, are permitted provided that the following conditions
7 1.1 thorpej * are met:
8 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
9 1.1 thorpej * notice, this list of conditions and the following disclaimer.
10 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
12 1.1 thorpej * documentation and/or other materials provided with the distribution.
13 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
14 1.1 thorpej * must display the following acknowledgement:
15 1.1 thorpej * This product includes software developed by the University of
16 1.1 thorpej * California, Berkeley and its contributors.
17 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
18 1.1 thorpej * may be used to endorse or promote products derived from this software
19 1.1 thorpej * without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #if !defined(lint) && !defined(sgi)
35 1.1 thorpej static char sccsid[] = "@(#)rdisc.c 8.1 (Berkeley) x/y/95";
36 1.1 thorpej #endif /* not lint */
37 1.1 thorpej
38 1.1 thorpej #ident "$Revision: 1.1 $"
39 1.1 thorpej
40 1.1 thorpej #include "defs.h"
41 1.1 thorpej #include <netinet/in_systm.h>
42 1.1 thorpej #include <netinet/ip.h>
43 1.1 thorpej #include <netinet/ip_icmp.h>
44 1.1 thorpej
45 1.1 thorpej /* router advertisement ICMP packet */
46 1.1 thorpej struct icmp_ad {
47 1.1 thorpej u_char icmp_type; /* type of message */
48 1.1 thorpej u_char icmp_code; /* type sub code */
49 1.1 thorpej u_short icmp_cksum; /* ones complement cksum of struct */
50 1.1 thorpej u_char icmp_ad_num; /* # of following router addresses */
51 1.1 thorpej u_char icmp_ad_asize; /* 2--words in each advertisement */
52 1.1 thorpej u_short icmp_ad_life; /* seconds of validity */
53 1.1 thorpej struct icmp_ad_info {
54 1.1 thorpej n_long icmp_ad_addr;
55 1.1 thorpej n_long icmp_ad_pref;
56 1.1 thorpej } icmp_ad_info[1];
57 1.1 thorpej };
58 1.1 thorpej
59 1.1 thorpej /* router solicitation ICMP packet */
60 1.1 thorpej struct icmp_so {
61 1.1 thorpej u_char icmp_type; /* type of message */
62 1.1 thorpej u_char icmp_code; /* type sub code */
63 1.1 thorpej u_short icmp_cksum; /* ones complement cksum of struct */
64 1.1 thorpej n_long icmp_so_rsvd;
65 1.1 thorpej };
66 1.1 thorpej
67 1.1 thorpej union ad_u {
68 1.1 thorpej struct icmp icmp;
69 1.1 thorpej struct icmp_ad ad;
70 1.1 thorpej struct icmp_so so;
71 1.1 thorpej };
72 1.1 thorpej
73 1.1 thorpej
74 1.1 thorpej int rdisc_sock = -1; /* router-discovery raw socket */
75 1.1 thorpej struct interface *rdisc_sock_mcast; /* current multicast interface */
76 1.1 thorpej
77 1.1 thorpej struct timeval rdisc_timer;
78 1.1 thorpej int rdisc_ok; /* using solicited route */
79 1.1 thorpej
80 1.1 thorpej
81 1.1 thorpej #define MAX_ADS 5
82 1.1 thorpej struct dr { /* accumulated advertisements */
83 1.1 thorpej struct interface *dr_ifp;
84 1.1 thorpej naddr dr_gate; /* gateway */
85 1.1 thorpej time_t dr_ts; /* when received */
86 1.1 thorpej time_t dr_life; /* lifetime */
87 1.1 thorpej n_long dr_recv_pref; /* received but biased preference */
88 1.1 thorpej n_long dr_pref; /* preference adjusted by metric */
89 1.1 thorpej } *cur_drp, drs[MAX_ADS];
90 1.1 thorpej
91 1.1 thorpej /* adjust preference by interface metric without driving it to infinity */
92 1.1 thorpej #define PREF(p, ifp) ((p) <= (ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
93 1.1 thorpej : (p) - ((ifp)->int_metric))
94 1.1 thorpej
95 1.1 thorpej static void rdisc_sort(void);
96 1.1 thorpej
97 1.1 thorpej
98 1.1 thorpej /* dump an ICMP Router Discovery Advertisement Message
99 1.1 thorpej */
100 1.1 thorpej static void
101 1.1 thorpej trace_rdisc(char *act,
102 1.1 thorpej naddr from,
103 1.1 thorpej naddr to,
104 1.1 thorpej struct interface *ifp,
105 1.1 thorpej union ad_u *p,
106 1.1 thorpej u_int len)
107 1.1 thorpej {
108 1.1 thorpej int i;
109 1.1 thorpej n_long *wp, *lim;
110 1.1 thorpej
111 1.1 thorpej
112 1.1 thorpej if (!TRACEPACKETS || ftrace == 0)
113 1.1 thorpej return;
114 1.1 thorpej
115 1.1 thorpej lastlog();
116 1.1 thorpej
117 1.1 thorpej if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
118 1.1 thorpej (void)fprintf(ftrace, "%s Router Ad"
119 1.1 thorpej " from %s to %s via %s life=%d\n",
120 1.1 thorpej act, naddr_ntoa(from), naddr_ntoa(to),
121 1.1 thorpej ifp ? ifp->int_name : "?",
122 1.1 thorpej ntohs(p->ad.icmp_ad_life));
123 1.1 thorpej if (!TRACECONTENTS)
124 1.1 thorpej return;
125 1.1 thorpej
126 1.1 thorpej wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
127 1.1 thorpej lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
128 1.1 thorpej for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
129 1.1 thorpej (void)fprintf(ftrace, "\t%s preference=%#x",
130 1.1 thorpej naddr_ntoa(wp[0]), ntohl(wp[1]));
131 1.1 thorpej wp += p->ad.icmp_ad_asize;
132 1.1 thorpej }
133 1.1 thorpej (void)fputc('\n',ftrace);
134 1.1 thorpej
135 1.1 thorpej } else {
136 1.1 thorpej trace_act("%s Router Solic. from %s to %s via %s"
137 1.1 thorpej " value=%#x\n",
138 1.1 thorpej act, naddr_ntoa(from), naddr_ntoa(to),
139 1.1 thorpej ifp ? ifp->int_name : "?",
140 1.1 thorpej ntohl(p->so.icmp_so_rsvd));
141 1.1 thorpej }
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej
145 1.1 thorpej /* Pick multicast group for router-discovery socket
146 1.1 thorpej */
147 1.1 thorpej void
148 1.1 thorpej set_rdisc_mg(struct interface *ifp,
149 1.1 thorpej int on) { /* 0=turn it off */
150 1.1 thorpej struct ip_mreq m;
151 1.1 thorpej
152 1.1 thorpej if (rdisc_sock == -1
153 1.1 thorpej || !(ifp->int_if_flags & IFF_MULTICAST)
154 1.1 thorpej || (ifp->int_state & IS_ALIAS)) {
155 1.1 thorpej ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
156 1.1 thorpej return;
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej #ifdef MCAST_PPP_BUG
160 1.1 thorpej if (ifp->int_if_flags & IFF_POINTOPOINT)
161 1.1 thorpej return;
162 1.1 thorpej #endif
163 1.1 thorpej bzero(&m, sizeof(m));
164 1.1 thorpej m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
165 1.1 thorpej ? ifp->int_dstaddr
166 1.1 thorpej : ifp->int_addr);
167 1.1 thorpej if (supplier
168 1.1 thorpej || (ifp->int_state & IS_NO_ADV_IN)
169 1.1 thorpej || !on) {
170 1.1 thorpej /* stop listening to advertisements */
171 1.1 thorpej if (ifp->int_state & IS_ALL_HOSTS) {
172 1.1 thorpej m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
173 1.1 thorpej if (setsockopt(rdisc_sock, IPPROTO_IP,
174 1.1 thorpej IP_DROP_MEMBERSHIP,
175 1.1 thorpej &m, sizeof(m)) < 0)
176 1.1 thorpej LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
177 1.1 thorpej ifp->int_state &= ~IS_ALL_HOSTS;
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej } else if (!(ifp->int_state & IS_ALL_HOSTS)) {
181 1.1 thorpej /* start listening to advertisements */
182 1.1 thorpej m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
183 1.1 thorpej if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
184 1.1 thorpej &m, sizeof(m)) < 0) {
185 1.1 thorpej LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
186 1.1 thorpej } else {
187 1.1 thorpej ifp->int_state |= IS_ALL_HOSTS;
188 1.1 thorpej }
189 1.1 thorpej }
190 1.1 thorpej
191 1.1 thorpej if (!supplier
192 1.1 thorpej || (ifp->int_state & IS_NO_ADV_OUT)
193 1.1 thorpej || !on) {
194 1.1 thorpej /* stop listening to solicitations */
195 1.1 thorpej if (ifp->int_state & IS_ALL_ROUTERS) {
196 1.1 thorpej m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
197 1.1 thorpej if (setsockopt(rdisc_sock, IPPROTO_IP,
198 1.1 thorpej IP_DROP_MEMBERSHIP,
199 1.1 thorpej &m, sizeof(m)) < 0)
200 1.1 thorpej LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
201 1.1 thorpej ifp->int_state &= ~IS_ALL_ROUTERS;
202 1.1 thorpej }
203 1.1 thorpej
204 1.1 thorpej } else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
205 1.1 thorpej /* start hearing solicitations */
206 1.1 thorpej m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
207 1.1 thorpej if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
208 1.1 thorpej &m, sizeof(m)) < 0) {
209 1.1 thorpej LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
210 1.1 thorpej } else {
211 1.1 thorpej ifp->int_state |= IS_ALL_ROUTERS;
212 1.1 thorpej }
213 1.1 thorpej }
214 1.1 thorpej }
215 1.1 thorpej
216 1.1 thorpej
217 1.1 thorpej /* start supplying routes
218 1.1 thorpej */
219 1.1 thorpej void
220 1.1 thorpej set_supplier(void)
221 1.1 thorpej {
222 1.1 thorpej struct interface *ifp;
223 1.1 thorpej struct dr *drp;
224 1.1 thorpej
225 1.1 thorpej if (supplier_set)
226 1.1 thorpej return;
227 1.1 thorpej
228 1.1 thorpej trace_act("start suppying routes\n");
229 1.1 thorpej
230 1.1 thorpej /* Forget discovered routes.
231 1.1 thorpej */
232 1.1 thorpej for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
233 1.1 thorpej drp->dr_recv_pref = 0;
234 1.1 thorpej drp->dr_life = 0;
235 1.1 thorpej }
236 1.1 thorpej rdisc_age(0);
237 1.1 thorpej
238 1.1 thorpej supplier_set = 1;
239 1.1 thorpej supplier = 1;
240 1.1 thorpej
241 1.1 thorpej /* Do not start advertising until we have heard some RIP routes */
242 1.1 thorpej LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
243 1.1 thorpej
244 1.1 thorpej /* Switch router discovery multicast groups from soliciting
245 1.1 thorpej * to advertising.
246 1.1 thorpej */
247 1.1 thorpej for (ifp = ifnet; ifp; ifp = ifp->int_next) {
248 1.1 thorpej if (ifp->int_state & IS_BROKE)
249 1.1 thorpej continue;
250 1.1 thorpej ifp->int_rdisc_cnt = 0;
251 1.1 thorpej ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
252 1.1 thorpej ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
253 1.1 thorpej set_rdisc_mg(ifp, 1);
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej /* get rid of any redirects */
257 1.1 thorpej del_redirects(0,0);
258 1.1 thorpej }
259 1.1 thorpej
260 1.1 thorpej
261 1.1 thorpej /* age discovered routes and find the best one
262 1.1 thorpej */
263 1.1 thorpej void
264 1.1 thorpej rdisc_age(naddr bad_gate)
265 1.1 thorpej {
266 1.1 thorpej time_t sec;
267 1.1 thorpej struct dr *drp;
268 1.1 thorpej
269 1.1 thorpej
270 1.1 thorpej /* If only adverising, then do only that. */
271 1.1 thorpej if (supplier) {
272 1.1 thorpej /* if switching from client to server, get rid of old
273 1.1 thorpej * default routes.
274 1.1 thorpej */
275 1.1 thorpej if (cur_drp != 0)
276 1.1 thorpej rdisc_sort();
277 1.1 thorpej rdisc_adv();
278 1.1 thorpej return;
279 1.1 thorpej }
280 1.1 thorpej
281 1.1 thorpej /* If we are being told about a bad router,
282 1.1 thorpej * then age the discovered default route, and if there is
283 1.1 thorpej * no alternative, solicite a replacement.
284 1.1 thorpej */
285 1.1 thorpej if (bad_gate != 0) {
286 1.1 thorpej /* Look for the bad discovered default route.
287 1.1 thorpej * Age it and note its interface.
288 1.1 thorpej */
289 1.1 thorpej for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
290 1.1 thorpej if (drp->dr_ts == 0)
291 1.1 thorpej continue;
292 1.1 thorpej
293 1.1 thorpej /* When we find the bad router, then age the route
294 1.1 thorpej * to at most SUPPLY_INTERVAL.
295 1.1 thorpej * This is contrary to RFC 1256, but defends against
296 1.1 thorpej * black holes.
297 1.1 thorpej */
298 1.1 thorpej if (drp->dr_gate == bad_gate) {
299 1.1 thorpej sec = (now.tv_sec - drp->dr_life
300 1.1 thorpej + SUPPLY_INTERVAL);
301 1.1 thorpej if (drp->dr_ts > sec) {
302 1.1 thorpej trace_act("age 0.0.0.0 --> %s"
303 1.1 thorpej " via %s\n",
304 1.1 thorpej naddr_ntoa(drp->dr_gate),
305 1.1 thorpej drp->dr_ifp->int_name);
306 1.1 thorpej drp->dr_ts = sec;
307 1.1 thorpej }
308 1.1 thorpej break;
309 1.1 thorpej }
310 1.1 thorpej }
311 1.1 thorpej }
312 1.1 thorpej
313 1.1 thorpej /* delete old redirected routes to keep the kernel table small
314 1.1 thorpej */
315 1.1 thorpej sec = (cur_drp == 0) ? MaxMaxAdvertiseInterval : cur_drp->dr_life;
316 1.1 thorpej del_redirects(bad_gate, now.tv_sec-sec);
317 1.1 thorpej
318 1.1 thorpej rdisc_sol();
319 1.1 thorpej
320 1.1 thorpej rdisc_sort();
321 1.1 thorpej }
322 1.1 thorpej
323 1.1 thorpej
324 1.1 thorpej /* Zap all routes discovered via an interface that has gone bad
325 1.1 thorpej * This should only be called when !(ifp->int_state & IS_ALIAS)
326 1.1 thorpej */
327 1.1 thorpej void
328 1.1 thorpej if_bad_rdisc(struct interface *ifp)
329 1.1 thorpej {
330 1.1 thorpej struct dr *drp;
331 1.1 thorpej
332 1.1 thorpej for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
333 1.1 thorpej if (drp->dr_ifp != ifp)
334 1.1 thorpej continue;
335 1.1 thorpej drp->dr_recv_pref = 0;
336 1.1 thorpej drp->dr_life = 0;
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej rdisc_sort();
340 1.1 thorpej }
341 1.1 thorpej
342 1.1 thorpej
343 1.1 thorpej /* mark an interface ok for router discovering.
344 1.1 thorpej */
345 1.1 thorpej void
346 1.1 thorpej if_ok_rdisc(struct interface *ifp)
347 1.1 thorpej {
348 1.1 thorpej set_rdisc_mg(ifp, 1);
349 1.1 thorpej
350 1.1 thorpej ifp->int_rdisc_cnt = 0;
351 1.1 thorpej ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
352 1.1 thorpej ? MIN_WAITTIME
353 1.1 thorpej : MAX_SOLICITATION_DELAY);
354 1.1 thorpej if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
355 1.1 thorpej rdisc_timer = ifp->int_rdisc_timer;
356 1.1 thorpej }
357 1.1 thorpej
358 1.1 thorpej
359 1.1 thorpej /* get rid of a dead discovered router
360 1.1 thorpej */
361 1.1 thorpej static void
362 1.1 thorpej del_rdisc(struct dr *drp)
363 1.1 thorpej {
364 1.1 thorpej struct interface *ifp;
365 1.1 thorpej int i;
366 1.1 thorpej
367 1.1 thorpej
368 1.1 thorpej del_redirects(drp->dr_gate, 0);
369 1.1 thorpej drp->dr_ts = 0;
370 1.1 thorpej drp->dr_life = 0;
371 1.1 thorpej
372 1.1 thorpej
373 1.1 thorpej /* Count the other discovered routes on the interface.
374 1.1 thorpej */
375 1.1 thorpej i = 0;
376 1.1 thorpej ifp = drp->dr_ifp;
377 1.1 thorpej for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
378 1.1 thorpej if (drp->dr_ts != 0
379 1.1 thorpej && drp->dr_ifp == ifp)
380 1.1 thorpej i++;
381 1.1 thorpej }
382 1.1 thorpej
383 1.1 thorpej /* If that was the last good discovered router on the interface,
384 1.1 thorpej * then solicit a new one.
385 1.1 thorpej * This is contrary to RFC 1256, but defends against black holes.
386 1.1 thorpej */
387 1.1 thorpej if (i == 0
388 1.1 thorpej && ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
389 1.1 thorpej trace_act("discovered route is bad"
390 1.1 thorpej "--re-solicit routers via %s\n", ifp->int_name);
391 1.1 thorpej ifp->int_rdisc_cnt = 0;
392 1.1 thorpej ifp->int_rdisc_timer.tv_sec = 0;
393 1.1 thorpej rdisc_sol();
394 1.1 thorpej }
395 1.1 thorpej }
396 1.1 thorpej
397 1.1 thorpej
398 1.1 thorpej /* Find the best discovered route,
399 1.1 thorpej * and discard stale routers.
400 1.1 thorpej */
401 1.1 thorpej static void
402 1.1 thorpej rdisc_sort(void)
403 1.1 thorpej {
404 1.1 thorpej struct dr *drp, *new_drp;
405 1.1 thorpej struct rt_entry *rt;
406 1.1 thorpej struct interface *ifp;
407 1.1 thorpej u_int new_st;
408 1.1 thorpej n_long new_pref;
409 1.1 thorpej
410 1.1 thorpej
411 1.1 thorpej /* Find the best discovered route.
412 1.1 thorpej */
413 1.1 thorpej new_drp = 0;
414 1.1 thorpej for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
415 1.1 thorpej if (drp->dr_ts == 0)
416 1.1 thorpej continue;
417 1.1 thorpej ifp = drp->dr_ifp;
418 1.1 thorpej
419 1.1 thorpej /* Get rid of expired discovered routers.
420 1.1 thorpej */
421 1.1 thorpej if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
422 1.1 thorpej del_rdisc(drp);
423 1.1 thorpej continue;
424 1.1 thorpej }
425 1.1 thorpej
426 1.1 thorpej LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
427 1.1 thorpej
428 1.1 thorpej /* Update preference with possibly changed interface
429 1.1 thorpej * metric.
430 1.1 thorpej */
431 1.1 thorpej drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
432 1.1 thorpej
433 1.1 thorpej /* Prefer the current route to prevent thrashing.
434 1.1 thorpej * Prefer shorter lifetimes to speed the detection of
435 1.1 thorpej * bad routers.
436 1.1 thorpej * Avoid sick interfaces.
437 1.1 thorpej */
438 1.1 thorpej if (new_drp == 0
439 1.1 thorpej || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
440 1.1 thorpej && (new_pref < drp->dr_pref
441 1.1 thorpej || (new_pref == drp->dr_pref
442 1.1 thorpej && (drp == cur_drp
443 1.1 thorpej || (new_drp != cur_drp
444 1.1 thorpej && new_drp->dr_life > drp->dr_life)))))
445 1.1 thorpej || ((new_st & IS_SICK)
446 1.1 thorpej && !(drp->dr_ifp->int_state & IS_SICK))) {
447 1.1 thorpej new_drp = drp;
448 1.1 thorpej new_st = drp->dr_ifp->int_state;
449 1.1 thorpej new_pref = drp->dr_pref;
450 1.1 thorpej }
451 1.1 thorpej }
452 1.1 thorpej
453 1.1 thorpej /* switch to a better default route
454 1.1 thorpej */
455 1.1 thorpej if (new_drp != cur_drp) {
456 1.1 thorpej rt = rtget(RIP_DEFAULT, 0);
457 1.1 thorpej
458 1.1 thorpej /* Stop using discovered routes if they are all bad
459 1.1 thorpej */
460 1.1 thorpej if (new_drp == 0) {
461 1.1 thorpej trace_act("turn off Router Discovery client\n");
462 1.1 thorpej rdisc_ok = 0;
463 1.1 thorpej
464 1.1 thorpej if (rt != 0
465 1.1 thorpej && (rt->rt_state & RS_RDISC)) {
466 1.1 thorpej rtchange(rt, rt->rt_state & ~RS_RDISC,
467 1.1 thorpej rt->rt_gate, rt->rt_router,
468 1.1 thorpej HOPCNT_INFINITY, 0, rt->rt_ifp,
469 1.1 thorpej now.tv_sec - GARBAGE_TIME, 0);
470 1.1 thorpej rtswitch(rt, 0);
471 1.1 thorpej }
472 1.1 thorpej
473 1.1 thorpej /* turn on RIP if permitted */
474 1.1 thorpej rip_on(0);
475 1.1 thorpej
476 1.1 thorpej } else {
477 1.1 thorpej if (cur_drp == 0) {
478 1.1 thorpej trace_act("turn on Router Discovery client"
479 1.1 thorpej " using %s via %s\n",
480 1.1 thorpej naddr_ntoa(new_drp->dr_gate),
481 1.1 thorpej new_drp->dr_ifp->int_name);
482 1.1 thorpej
483 1.1 thorpej rdisc_ok = 1;
484 1.1 thorpej
485 1.1 thorpej } else {
486 1.1 thorpej trace_act("switch Router Discovery from"
487 1.1 thorpej " %s via %s to %s via %s\n",
488 1.1 thorpej naddr_ntoa(cur_drp->dr_gate),
489 1.1 thorpej cur_drp->dr_ifp->int_name,
490 1.1 thorpej naddr_ntoa(new_drp->dr_gate),
491 1.1 thorpej new_drp->dr_ifp->int_name);
492 1.1 thorpej }
493 1.1 thorpej
494 1.1 thorpej if (rt != 0) {
495 1.1 thorpej rtchange(rt, rt->rt_state | RS_RDISC,
496 1.1 thorpej new_drp->dr_gate, new_drp->dr_gate,
497 1.1 thorpej 0,0, new_drp->dr_ifp,
498 1.1 thorpej now.tv_sec, 0);
499 1.1 thorpej } else {
500 1.1 thorpej rtadd(RIP_DEFAULT, 0,
501 1.1 thorpej new_drp->dr_gate, new_drp->dr_gate,
502 1.1 thorpej 0, 0, RS_RDISC, new_drp->dr_ifp);
503 1.1 thorpej }
504 1.1 thorpej
505 1.1 thorpej /* Now turn off RIP and delete RIP routes,
506 1.1 thorpej * which might otherwise include the default
507 1.1 thorpej * we just modified.
508 1.1 thorpej */
509 1.1 thorpej rip_off();
510 1.1 thorpej }
511 1.1 thorpej
512 1.1 thorpej cur_drp = new_drp;
513 1.1 thorpej }
514 1.1 thorpej }
515 1.1 thorpej
516 1.1 thorpej
517 1.1 thorpej /* handle a single address in an advertisement
518 1.1 thorpej */
519 1.1 thorpej static void
520 1.1 thorpej parse_ad(naddr from,
521 1.1 thorpej naddr gate,
522 1.1 thorpej n_long pref,
523 1.1 thorpej u_short life,
524 1.1 thorpej struct interface *ifp)
525 1.1 thorpej {
526 1.1 thorpej static naddr bad_gate;
527 1.1 thorpej struct dr *drp, *new_drp;
528 1.1 thorpej
529 1.1 thorpej
530 1.1 thorpej if (gate == RIP_DEFAULT
531 1.1 thorpej || !check_dst(gate)) {
532 1.1 thorpej if (bad_gate != from) {
533 1.1 thorpej msglog("router %s advertising bad gateway %s",
534 1.1 thorpej naddr_ntoa(from),
535 1.1 thorpej naddr_ntoa(gate));
536 1.1 thorpej bad_gate = from;
537 1.1 thorpej }
538 1.1 thorpej return;
539 1.1 thorpej }
540 1.1 thorpej
541 1.1 thorpej /* ignore pointers to ourself and routes via unreachable networks
542 1.1 thorpej */
543 1.1 thorpej if (ifwithaddr(gate, 1, 0) != 0) {
544 1.1 thorpej trace_pkt("\tdiscard our own Router Discovery Ad\n");
545 1.1 thorpej return;
546 1.1 thorpej }
547 1.1 thorpej if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
548 1.1 thorpej trace_pkt("\tdiscard Router Discovery Ad"
549 1.1 thorpej " from unreachable net\n");
550 1.1 thorpej return;
551 1.1 thorpej }
552 1.1 thorpej
553 1.1 thorpej /* Convert preference to an unsigned value
554 1.1 thorpej * and later bias it by the metric of the interface.
555 1.1 thorpej */
556 1.1 thorpej pref = ntohl(pref) ^ MIN_PreferenceLevel;
557 1.1 thorpej
558 1.1 thorpej if (pref == 0 || life == 0) {
559 1.1 thorpej pref = 0;
560 1.1 thorpej life = 0;
561 1.1 thorpej }
562 1.1 thorpej
563 1.1 thorpej for (new_drp = 0, drp = drs; drp < &drs[MAX_ADS]; drp++) {
564 1.1 thorpej /* accept new info for a familiar entry
565 1.1 thorpej */
566 1.1 thorpej if (drp->dr_gate == gate) {
567 1.1 thorpej new_drp = drp;
568 1.1 thorpej break;
569 1.1 thorpej }
570 1.1 thorpej
571 1.1 thorpej if (life == 0)
572 1.1 thorpej continue; /* do not worry about dead ads */
573 1.1 thorpej
574 1.1 thorpej if (drp->dr_ts == 0) {
575 1.1 thorpej new_drp = drp; /* use unused entry */
576 1.1 thorpej
577 1.1 thorpej } else if (new_drp == 0) {
578 1.1 thorpej /* look for an entry worse than the new one to
579 1.1 thorpej * reuse.
580 1.1 thorpej */
581 1.1 thorpej if ((!(ifp->int_state & IS_SICK)
582 1.1 thorpej && (drp->dr_ifp->int_state & IS_SICK))
583 1.1 thorpej || (pref > drp->dr_pref
584 1.1 thorpej && !((ifp->int_state ^ drp->dr_ifp->int_state)
585 1.1 thorpej & IS_SICK)))
586 1.1 thorpej new_drp = drp;
587 1.1 thorpej
588 1.1 thorpej } else if (new_drp->dr_ts != 0) {
589 1.1 thorpej /* look for the least valueable entry to reuse
590 1.1 thorpej */
591 1.1 thorpej if ((!(new_drp->dr_ifp->int_state & IS_SICK)
592 1.1 thorpej && (drp->dr_ifp->int_state & IS_SICK))
593 1.1 thorpej || (new_drp->dr_pref > drp->dr_pref
594 1.1 thorpej && !((new_drp->dr_ifp->int_state
595 1.1 thorpej ^ drp->dr_ifp->int_state)
596 1.1 thorpej & IS_SICK)))
597 1.1 thorpej new_drp = drp;
598 1.1 thorpej }
599 1.1 thorpej }
600 1.1 thorpej
601 1.1 thorpej /* forget it if all of the current entries are better */
602 1.1 thorpej if (new_drp == 0)
603 1.1 thorpej return;
604 1.1 thorpej
605 1.1 thorpej new_drp->dr_ifp = ifp;
606 1.1 thorpej new_drp->dr_gate = gate;
607 1.1 thorpej new_drp->dr_ts = now.tv_sec;
608 1.1 thorpej new_drp->dr_life = ntohs(life);
609 1.1 thorpej new_drp->dr_recv_pref = pref;
610 1.1 thorpej /* bias functional preference by metric of the interface */
611 1.1 thorpej new_drp->dr_pref = PREF(pref,ifp);
612 1.1 thorpej
613 1.1 thorpej /* after hearing a good advertisement, stop asking
614 1.1 thorpej */
615 1.1 thorpej if (!(ifp->int_state & IS_SICK))
616 1.1 thorpej ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
617 1.1 thorpej }
618 1.1 thorpej
619 1.1 thorpej
620 1.1 thorpej /* Compute the IP checksum
621 1.1 thorpej * This assumes the packet is less than 32K long.
622 1.1 thorpej */
623 1.1 thorpej static u_short
624 1.1 thorpej in_cksum(u_short *p,
625 1.1 thorpej u_int len)
626 1.1 thorpej {
627 1.1 thorpej u_int sum = 0;
628 1.1 thorpej int nwords = len >> 1;
629 1.1 thorpej
630 1.1 thorpej while (nwords-- != 0)
631 1.1 thorpej sum += *p++;
632 1.1 thorpej
633 1.1 thorpej if (len & 1)
634 1.1 thorpej sum += *(u_char *)p;
635 1.1 thorpej
636 1.1 thorpej /* end-around-carry */
637 1.1 thorpej sum = (sum >> 16) + (sum & 0xffff);
638 1.1 thorpej sum += (sum >> 16);
639 1.1 thorpej return (~sum);
640 1.1 thorpej }
641 1.1 thorpej
642 1.1 thorpej
643 1.1 thorpej /* Send a router discovery advertisement or solicitation ICMP packet.
644 1.1 thorpej */
645 1.1 thorpej static void
646 1.1 thorpej send_rdisc(union ad_u *p,
647 1.1 thorpej int p_size,
648 1.1 thorpej struct interface *ifp,
649 1.1 thorpej naddr dst, /* 0 or unicast destination */
650 1.1 thorpej int type) /* 0=unicast, 1=bcast, 2=mcast */
651 1.1 thorpej {
652 1.1 thorpej struct sockaddr_in sin;
653 1.1 thorpej int flags;
654 1.1 thorpej char *msg;
655 1.1 thorpej naddr tgt_mcast;
656 1.1 thorpej
657 1.1 thorpej
658 1.1 thorpej bzero(&sin, sizeof(sin));
659 1.1 thorpej sin.sin_addr.s_addr = dst;
660 1.1 thorpej flags = MSG_DONTROUTE;
661 1.1 thorpej
662 1.1 thorpej switch (type) {
663 1.1 thorpej case 0: /* unicast */
664 1.1 thorpej msg = "Send";
665 1.1 thorpej break;
666 1.1 thorpej
667 1.1 thorpej case 1: /* broadcast */
668 1.1 thorpej if (ifp->int_if_flags & IFF_POINTOPOINT) {
669 1.1 thorpej msg = "Send pt-to-pt";
670 1.1 thorpej sin.sin_addr.s_addr = ifp->int_dstaddr;
671 1.1 thorpej } else {
672 1.1 thorpej msg = "Send broadcast";
673 1.1 thorpej sin.sin_addr.s_addr = ifp->int_brdaddr;
674 1.1 thorpej }
675 1.1 thorpej break;
676 1.1 thorpej
677 1.1 thorpej case 2: /* multicast */
678 1.1 thorpej msg = "Send multicast";
679 1.1 thorpej if (ifp->int_state & IS_DUP) {
680 1.1 thorpej trace_act("abort multicast output via %s"
681 1.1 thorpej " with duplicate address\n",
682 1.1 thorpej ifp->int_name);
683 1.1 thorpej return;
684 1.1 thorpej }
685 1.1 thorpej if (rdisc_sock_mcast != ifp) {
686 1.1 thorpej /* select the right interface. */
687 1.1 thorpej #ifdef MCAST_PPP_BUG
688 1.1 thorpej /* Do not specifiy the primary interface explicitly
689 1.1 thorpej * if we have the multicast point-to-point kernel
690 1.1 thorpej * bug, since the kernel will do the wrong thing
691 1.1 thorpej * if the local address of a point-to-point link
692 1.1 thorpej * is the same as the address of an ordinary
693 1.1 thorpej * interface.
694 1.1 thorpej */
695 1.1 thorpej if (ifp->int_addr == myaddr) {
696 1.1 thorpej tgt_mcast = 0;
697 1.1 thorpej } else
698 1.1 thorpej #endif
699 1.1 thorpej tgt_mcast = ifp->int_addr;
700 1.1 thorpej if (0 > setsockopt(rdisc_sock,
701 1.1 thorpej IPPROTO_IP, IP_MULTICAST_IF,
702 1.1 thorpej &tgt_mcast, sizeof(tgt_mcast))) {
703 1.1 thorpej LOGERR("setsockopt(rdisc_sock,"
704 1.1 thorpej "IP_MULTICAST_IF)");
705 1.1 thorpej rdisc_sock_mcast = 0;
706 1.1 thorpej return;
707 1.1 thorpej }
708 1.1 thorpej rdisc_sock_mcast = ifp;
709 1.1 thorpej }
710 1.1 thorpej flags = 0;
711 1.1 thorpej break;
712 1.1 thorpej }
713 1.1 thorpej
714 1.1 thorpej trace_rdisc(msg, ifp->int_addr, sin.sin_addr.s_addr, ifp,
715 1.1 thorpej p, p_size);
716 1.1 thorpej
717 1.1 thorpej if (0 > sendto(rdisc_sock, p, p_size, flags,
718 1.1 thorpej (struct sockaddr *)&sin, sizeof(sin))) {
719 1.1 thorpej if (ifp == 0 || !(ifp->int_state & IS_BROKE))
720 1.1 thorpej msglog("sendto(%s%s%s): %s",
721 1.1 thorpej ifp != 0 ? ifp->int_name : "",
722 1.1 thorpej ifp != 0 ? ", " : "",
723 1.1 thorpej inet_ntoa(sin.sin_addr),
724 1.1 thorpej strerror(errno));
725 1.1 thorpej if (ifp != 0)
726 1.1 thorpej if_sick(ifp);
727 1.1 thorpej }
728 1.1 thorpej }
729 1.1 thorpej
730 1.1 thorpej
731 1.1 thorpej /* Send an advertisement
732 1.1 thorpej */
733 1.1 thorpej static void
734 1.1 thorpej send_adv(struct interface *ifp,
735 1.1 thorpej naddr dst, /* 0 or unicast destination */
736 1.1 thorpej int type) /* 0=unicast, 1=bcast, 2=mcast */
737 1.1 thorpej {
738 1.1 thorpej union ad_u u;
739 1.1 thorpej n_long pref;
740 1.1 thorpej
741 1.1 thorpej
742 1.1 thorpej bzero(&u,sizeof(u.ad));
743 1.1 thorpej
744 1.1 thorpej u.ad.icmp_type = ICMP_ROUTERADVERT;
745 1.1 thorpej u.ad.icmp_ad_num = 1;
746 1.1 thorpej u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
747 1.1 thorpej
748 1.1 thorpej u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
749 1.1 thorpej pref = ifp->int_rdisc_pref ^ MIN_PreferenceLevel;
750 1.1 thorpej pref = PREF(pref, ifp) ^ MIN_PreferenceLevel;
751 1.1 thorpej u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(pref);
752 1.1 thorpej
753 1.1 thorpej u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
754 1.1 thorpej
755 1.1 thorpej u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
756 1.1 thorpej
757 1.1 thorpej send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
758 1.1 thorpej }
759 1.1 thorpej
760 1.1 thorpej
761 1.1 thorpej /* Advertise for Router Discovery
762 1.1 thorpej */
763 1.1 thorpej void
764 1.1 thorpej rdisc_adv(void)
765 1.1 thorpej {
766 1.1 thorpej struct interface *ifp;
767 1.1 thorpej
768 1.1 thorpej
769 1.1 thorpej rdisc_timer.tv_sec = now.tv_sec + NEVER;
770 1.1 thorpej
771 1.1 thorpej for (ifp = ifnet; ifp; ifp = ifp->int_next) {
772 1.1 thorpej if (0 != (ifp->int_state & (IS_NO_ADV_OUT
773 1.1 thorpej | IS_PASSIVE
774 1.1 thorpej | IS_ALIAS
775 1.1 thorpej | IS_BROKE)))
776 1.1 thorpej continue;
777 1.1 thorpej
778 1.1 thorpej if (!timercmp(&ifp->int_rdisc_timer, &now, >)
779 1.1 thorpej || stopint) {
780 1.1 thorpej send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
781 1.1 thorpej (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
782 1.1 thorpej ifp->int_rdisc_cnt++;
783 1.1 thorpej
784 1.1 thorpej intvl_random(&ifp->int_rdisc_timer,
785 1.1 thorpej (ifp->int_rdisc_int*3)/4,
786 1.1 thorpej ifp->int_rdisc_int);
787 1.1 thorpej if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
788 1.1 thorpej && (ifp->int_rdisc_timer.tv_sec
789 1.1 thorpej > MAX_INITIAL_ADVERT_INTERVAL)) {
790 1.1 thorpej ifp->int_rdisc_timer.tv_sec
791 1.1 thorpej = MAX_INITIAL_ADVERT_INTERVAL;
792 1.1 thorpej }
793 1.1 thorpej timevaladd(&ifp->int_rdisc_timer, &now);
794 1.1 thorpej }
795 1.1 thorpej
796 1.1 thorpej if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
797 1.1 thorpej rdisc_timer = ifp->int_rdisc_timer;
798 1.1 thorpej }
799 1.1 thorpej }
800 1.1 thorpej
801 1.1 thorpej
802 1.1 thorpej /* Solicit for Router Discovery
803 1.1 thorpej */
804 1.1 thorpej void
805 1.1 thorpej rdisc_sol(void)
806 1.1 thorpej {
807 1.1 thorpej struct interface *ifp;
808 1.1 thorpej union ad_u u;
809 1.1 thorpej
810 1.1 thorpej
811 1.1 thorpej rdisc_timer.tv_sec = now.tv_sec + NEVER;
812 1.1 thorpej
813 1.1 thorpej for (ifp = ifnet; ifp; ifp = ifp->int_next) {
814 1.1 thorpej if (0 != (ifp->int_state & (IS_NO_SOL_OUT
815 1.1 thorpej | IS_PASSIVE
816 1.1 thorpej | IS_ALIAS
817 1.1 thorpej | IS_BROKE))
818 1.1 thorpej || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
819 1.1 thorpej continue;
820 1.1 thorpej
821 1.1 thorpej if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
822 1.1 thorpej bzero(&u,sizeof(u.so));
823 1.1 thorpej u.so.icmp_type = ICMP_ROUTERSOLICIT;
824 1.1 thorpej u.so.icmp_cksum = in_cksum((u_short*)&u.so,
825 1.1 thorpej sizeof(u.so));
826 1.1 thorpej send_rdisc(&u, sizeof(u.so), ifp,
827 1.1 thorpej htonl(INADDR_ALLROUTERS_GROUP),
828 1.1 thorpej ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
829 1.1 thorpej
830 1.1 thorpej if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
831 1.1 thorpej continue;
832 1.1 thorpej
833 1.1 thorpej ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
834 1.1 thorpej ifp->int_rdisc_timer.tv_usec = 0;
835 1.1 thorpej timevaladd(&ifp->int_rdisc_timer, &now);
836 1.1 thorpej }
837 1.1 thorpej
838 1.1 thorpej if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
839 1.1 thorpej rdisc_timer = ifp->int_rdisc_timer;
840 1.1 thorpej }
841 1.1 thorpej }
842 1.1 thorpej
843 1.1 thorpej
844 1.1 thorpej /* check the IP header of a possible Router Discovery ICMP packet */
845 1.1 thorpej static struct interface * /* 0 if bad */
846 1.1 thorpej ck_icmp(char *act,
847 1.1 thorpej naddr from,
848 1.1 thorpej naddr to,
849 1.1 thorpej union ad_u *p,
850 1.1 thorpej u_int len)
851 1.1 thorpej {
852 1.1 thorpej struct interface *ifp;
853 1.1 thorpej char *type;
854 1.1 thorpej
855 1.1 thorpej
856 1.1 thorpej /* If we could tell the interface on which a packet from address 0
857 1.1 thorpej * arrived, we could deal with such solicitations.
858 1.1 thorpej */
859 1.1 thorpej
860 1.1 thorpej ifp = ((from == 0) ? 0 : iflookup(from));
861 1.1 thorpej
862 1.1 thorpej if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
863 1.1 thorpej type = "advertisement";
864 1.1 thorpej } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
865 1.1 thorpej type = "solicitation";
866 1.1 thorpej } else {
867 1.1 thorpej return 0;
868 1.1 thorpej }
869 1.1 thorpej
870 1.1 thorpej if (p->icmp.icmp_code != 0) {
871 1.1 thorpej trace_pkt("unrecognized ICMP Router"
872 1.1 thorpej " %s code=%d from %s to %s\n",
873 1.1 thorpej type, p->icmp.icmp_code,
874 1.1 thorpej naddr_ntoa(from), naddr_ntoa(to));
875 1.1 thorpej return 0;
876 1.1 thorpej }
877 1.1 thorpej
878 1.1 thorpej trace_rdisc(act, from, to, ifp, p, len);
879 1.1 thorpej
880 1.1 thorpej if (ifp == 0)
881 1.1 thorpej trace_pkt("unknown interface for router-discovery %s"
882 1.1 thorpej " from %s to %s",
883 1.1 thorpej type, naddr_ntoa(from), naddr_ntoa(to));
884 1.1 thorpej
885 1.1 thorpej return ifp;
886 1.1 thorpej }
887 1.1 thorpej
888 1.1 thorpej
889 1.1 thorpej /* read packets from the router discovery socket
890 1.1 thorpej */
891 1.1 thorpej void
892 1.1 thorpej read_d(void)
893 1.1 thorpej {
894 1.1 thorpej static naddr bad_asize, bad_len;
895 1.1 thorpej struct sockaddr_in from;
896 1.1 thorpej int n, fromlen, cc, hlen;
897 1.1 thorpej union {
898 1.1 thorpej struct ip ip;
899 1.1 thorpej u_short s[512/2];
900 1.1 thorpej u_char b[512];
901 1.1 thorpej } pkt;
902 1.1 thorpej union ad_u *p;
903 1.1 thorpej n_long *wp;
904 1.1 thorpej struct interface *ifp;
905 1.1 thorpej
906 1.1 thorpej
907 1.1 thorpej for (;;) {
908 1.1 thorpej fromlen = sizeof(from);
909 1.1 thorpej cc = recvfrom(rdisc_sock, &pkt, sizeof(pkt), 0,
910 1.1 thorpej (struct sockaddr*)&from,
911 1.1 thorpej &fromlen);
912 1.1 thorpej if (cc <= 0) {
913 1.1 thorpej if (cc < 0 && errno != EWOULDBLOCK)
914 1.1 thorpej LOGERR("recvfrom(rdisc_sock)");
915 1.1 thorpej break;
916 1.1 thorpej }
917 1.1 thorpej if (fromlen != sizeof(struct sockaddr_in))
918 1.1 thorpej logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
919 1.1 thorpej fromlen);
920 1.1 thorpej
921 1.1 thorpej hlen = pkt.ip.ip_hl << 2;
922 1.1 thorpej if (cc < hlen + ICMP_MINLEN)
923 1.1 thorpej continue;
924 1.1 thorpej p = (union ad_u *)&pkt.b[hlen];
925 1.1 thorpej cc -= hlen;
926 1.1 thorpej
927 1.1 thorpej ifp = ck_icmp("Recv",
928 1.1 thorpej from.sin_addr.s_addr, pkt.ip.ip_dst.s_addr,
929 1.1 thorpej p, cc);
930 1.1 thorpej if (ifp == 0)
931 1.1 thorpej continue;
932 1.1 thorpej if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
933 1.1 thorpej trace_pkt("\tdiscard our own Router Discovery msg\n");
934 1.1 thorpej continue;
935 1.1 thorpej }
936 1.1 thorpej
937 1.1 thorpej switch (p->icmp.icmp_type) {
938 1.1 thorpej case ICMP_ROUTERADVERT:
939 1.1 thorpej if (p->ad.icmp_ad_asize*4
940 1.1 thorpej < sizeof(p->ad.icmp_ad_info[0])) {
941 1.1 thorpej if (bad_asize != from.sin_addr.s_addr) {
942 1.1 thorpej msglog("intolerable rdisc address"
943 1.1 thorpej " size=%d",
944 1.1 thorpej p->ad.icmp_ad_asize);
945 1.1 thorpej bad_asize = from.sin_addr.s_addr;
946 1.1 thorpej }
947 1.1 thorpej continue;
948 1.1 thorpej }
949 1.1 thorpej if (p->ad.icmp_ad_num == 0) {
950 1.1 thorpej trace_pkt("\tempty?\n");
951 1.1 thorpej continue;
952 1.1 thorpej }
953 1.1 thorpej if (cc != (sizeof(p->ad) - sizeof(p->ad.icmp_ad_info)
954 1.1 thorpej + (p->ad.icmp_ad_num
955 1.1 thorpej * sizeof(p->ad.icmp_ad_info[0])))) {
956 1.1 thorpej if (bad_len != from.sin_addr.s_addr) {
957 1.1 thorpej msglog("rdisc length %d does not"
958 1.1 thorpej " match ad_num %d",
959 1.1 thorpej cc, p->ad.icmp_ad_num);
960 1.1 thorpej bad_len = from.sin_addr.s_addr;
961 1.1 thorpej }
962 1.1 thorpej continue;
963 1.1 thorpej }
964 1.1 thorpej if (supplier)
965 1.1 thorpej continue;
966 1.1 thorpej if (ifp->int_state & IS_NO_ADV_IN)
967 1.1 thorpej continue;
968 1.1 thorpej
969 1.1 thorpej wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
970 1.1 thorpej for (n = 0; n < p->ad.icmp_ad_num; n++) {
971 1.1 thorpej parse_ad(from.sin_addr.s_addr,
972 1.1 thorpej wp[0], wp[1],
973 1.1 thorpej ntohs(p->ad.icmp_ad_life),
974 1.1 thorpej ifp);
975 1.1 thorpej wp += p->ad.icmp_ad_asize;
976 1.1 thorpej }
977 1.1 thorpej break;
978 1.1 thorpej
979 1.1 thorpej
980 1.1 thorpej case ICMP_ROUTERSOLICIT:
981 1.1 thorpej if (!supplier)
982 1.1 thorpej continue;
983 1.1 thorpej if (ifp->int_state & IS_NO_ADV_OUT)
984 1.1 thorpej continue;
985 1.1 thorpej
986 1.1 thorpej /* XXX
987 1.1 thorpej * We should handle messages from address 0.
988 1.1 thorpej */
989 1.1 thorpej
990 1.1 thorpej /* Respond with a point-to-point advertisement */
991 1.1 thorpej send_adv(ifp, from.sin_addr.s_addr, 0);
992 1.1 thorpej break;
993 1.1 thorpej }
994 1.1 thorpej }
995 1.1 thorpej
996 1.1 thorpej rdisc_sort();
997 1.1 thorpej }
998