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