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