ip_icmp.c revision 1.113 1 /* $NetBSD: ip_icmp.c,v 1.113 2007/08/27 05:39:44 dyoung Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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 project 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 PROJECT 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 PROJECT 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
32 /*-
33 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Jason R. Thorpe of Zembu Labs, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 /*
73 * Copyright (c) 1982, 1986, 1988, 1993
74 * The Regents of the University of California. All rights reserved.
75 *
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
78 * are met:
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. Neither the name of the University nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
87 *
88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 * SUCH DAMAGE.
99 *
100 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
101 */
102
103 #include <sys/cdefs.h>
104 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.113 2007/08/27 05:39:44 dyoung Exp $");
105
106 #include "opt_ipsec.h"
107
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/malloc.h>
111 #include <sys/mbuf.h>
112 #include <sys/protosw.h>
113 #include <sys/socket.h>
114 #include <sys/time.h>
115 #include <sys/kernel.h>
116 #include <sys/syslog.h>
117 #include <sys/sysctl.h>
118
119 #include <net/if.h>
120 #include <net/route.h>
121
122 #include <netinet/in.h>
123 #include <netinet/in_systm.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip.h>
126 #include <netinet/ip_icmp.h>
127 #include <netinet/ip_var.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/in_proto.h>
130 #include <netinet/icmp_var.h>
131
132 #ifdef IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netkey/key.h>
135 #endif
136
137 #ifdef FAST_IPSEC
138 #include <netipsec/ipsec.h>
139 #include <netipsec/key.h>
140 #endif /* FAST_IPSEC*/
141
142 #include <machine/stdarg.h>
143
144 /*
145 * ICMP routines: error generation, receive packet processing, and
146 * routines to turnaround packets back to the originator, and
147 * host table maintenance routines.
148 */
149
150 int icmpmaskrepl = 0;
151 #ifdef ICMPPRINTFS
152 int icmpprintfs = 0;
153 #endif
154 int icmpreturndatabytes = 8;
155
156 struct icmpstat icmpstat;
157
158 /*
159 * List of callbacks to notify when Path MTU changes are made.
160 */
161 struct icmp_mtudisc_callback {
162 LIST_ENTRY(icmp_mtudisc_callback) mc_list;
163 void (*mc_func)(struct in_addr);
164 };
165
166 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
167 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
168
169 #if 0
170 static u_int ip_next_mtu(u_int, int);
171 #else
172 /*static*/ u_int ip_next_mtu(u_int, int);
173 #endif
174
175 extern int icmperrppslim;
176 static int icmperrpps_count = 0;
177 static struct timeval icmperrppslim_last;
178 static int icmp_rediraccept = 1;
179 static int icmp_redirtimeout = 600;
180 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
181
182 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *);
183 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *);
184
185 static int icmp_ratelimit(const struct in_addr *, const int, const int);
186
187
188 void
189 icmp_init(void)
190 {
191 /*
192 * This is only useful if the user initializes redirtimeout to
193 * something other than zero.
194 */
195 if (icmp_redirtimeout != 0) {
196 icmp_redirect_timeout_q =
197 rt_timer_queue_create(icmp_redirtimeout);
198 }
199 }
200
201 /*
202 * Register a Path MTU Discovery callback.
203 */
204 void
205 icmp_mtudisc_callback_register(void (*func)(struct in_addr))
206 {
207 struct icmp_mtudisc_callback *mc;
208
209 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
210 mc = LIST_NEXT(mc, mc_list)) {
211 if (mc->mc_func == func)
212 return;
213 }
214
215 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
216 if (mc == NULL)
217 panic("icmp_mtudisc_callback_register");
218
219 mc->mc_func = func;
220 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
221 }
222
223 /*
224 * Generate an error packet of type error
225 * in response to bad packet ip.
226 */
227 void
228 icmp_error(struct mbuf *n, int type, int code, n_long dest,
229 int destmtu)
230 {
231 struct ip *oip = mtod(n, struct ip *), *nip;
232 unsigned oiplen = oip->ip_hl << 2;
233 struct icmp *icp;
234 struct mbuf *m;
235 struct m_tag *mtag;
236 unsigned icmplen, mblen;
237
238 #ifdef ICMPPRINTFS
239 if (icmpprintfs)
240 printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, code);
241 #endif
242 if (type != ICMP_REDIRECT)
243 icmpstat.icps_error++;
244 /*
245 * Don't send error if the original packet was encrypted.
246 * Don't send error if not the first fragment of message.
247 * Don't error if the old packet protocol was ICMP
248 * error message, only known informational types.
249 */
250 if (n->m_flags & M_DECRYPTED)
251 goto freeit;
252 if (oip->ip_off &~ htons(IP_MF|IP_DF))
253 goto freeit;
254 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
255 n->m_len >= oiplen + ICMP_MINLEN &&
256 !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type)) {
257 icmpstat.icps_oldicmp++;
258 goto freeit;
259 }
260 /* Don't send error in response to a multicast or broadcast packet */
261 if (n->m_flags & (M_BCAST|M_MCAST))
262 goto freeit;
263
264 /*
265 * First, do a rate limitation check.
266 */
267 if (icmp_ratelimit(&oip->ip_src, type, code)) {
268 /* XXX stat */
269 goto freeit;
270 }
271
272 /*
273 * Now, formulate icmp message
274 */
275 icmplen = oiplen + min(icmpreturndatabytes,
276 ntohs(oip->ip_len) - oiplen);
277 /*
278 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
279 */
280 mblen = 0;
281 for (m = n; m && (mblen < icmplen); m = m->m_next)
282 mblen += m->m_len;
283 icmplen = min(mblen, icmplen);
284
285 /*
286 * As we are not required to return everything we have,
287 * we return whatever we can return at ease.
288 *
289 * Note that ICMP datagrams longer than 576 octets are out of spec
290 * according to RFC1812; the limit on icmpreturndatabytes below in
291 * icmp_sysctl will keep things below that limit.
292 */
293
294 KASSERT(ICMP_MINLEN <= MCLBYTES);
295
296 if (icmplen + ICMP_MINLEN > MCLBYTES)
297 icmplen = MCLBYTES - ICMP_MINLEN;
298
299 m = m_gethdr(M_DONTWAIT, MT_HEADER);
300 if (m && (icmplen + ICMP_MINLEN > MHLEN)) {
301 MCLGET(m, M_DONTWAIT);
302 if ((m->m_flags & M_EXT) == 0) {
303 m_freem(m);
304 m = NULL;
305 }
306 }
307 if (m == NULL)
308 goto freeit;
309 MCLAIM(m, n->m_owner);
310 m->m_len = icmplen + ICMP_MINLEN;
311 if ((m->m_flags & M_EXT) == 0)
312 MH_ALIGN(m, m->m_len);
313 icp = mtod(m, struct icmp *);
314 if ((u_int)type > ICMP_MAXTYPE)
315 panic("icmp_error");
316 icmpstat.icps_outhist[type]++;
317 icp->icmp_type = type;
318 if (type == ICMP_REDIRECT)
319 icp->icmp_gwaddr.s_addr = dest;
320 else {
321 icp->icmp_void = 0;
322 /*
323 * The following assignments assume an overlay with the
324 * zeroed icmp_void field.
325 */
326 if (type == ICMP_PARAMPROB) {
327 icp->icmp_pptr = code;
328 code = 0;
329 } else if (type == ICMP_UNREACH &&
330 code == ICMP_UNREACH_NEEDFRAG && destmtu)
331 icp->icmp_nextmtu = htons(destmtu);
332 }
333
334 icp->icmp_code = code;
335 m_copydata(n, 0, icmplen, (void *)&icp->icmp_ip);
336
337 /*
338 * Now, copy old ip header (without options)
339 * in front of icmp message.
340 */
341 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
342 panic("icmp len");
343 m->m_data -= sizeof(struct ip);
344 m->m_len += sizeof(struct ip);
345 m->m_pkthdr.len = m->m_len;
346 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
347 nip = mtod(m, struct ip *);
348 /* ip_v set in ip_output */
349 nip->ip_hl = sizeof(struct ip) >> 2;
350 nip->ip_tos = 0;
351 nip->ip_len = htons(m->m_len);
352 /* ip_id set in ip_output */
353 nip->ip_off = htons(0);
354 /* ip_ttl set in icmp_reflect */
355 nip->ip_p = IPPROTO_ICMP;
356 nip->ip_src = oip->ip_src;
357 nip->ip_dst = oip->ip_dst;
358 /* move PF_GENERATED m_tag to new packet, if it exists */
359 mtag = m_tag_find(n, PACKET_TAG_PF_GENERATED, NULL);
360 if (mtag != NULL) {
361 m_tag_unlink(n, mtag);
362 m_tag_prepend(m, mtag);
363 }
364 icmp_reflect(m);
365
366 freeit:
367 m_freem(n);
368 }
369
370 struct sockaddr_in icmpsrc = {
371 .sin_len = sizeof (struct sockaddr_in),
372 .sin_family = AF_INET,
373 };
374 static struct sockaddr_in icmpdst = {
375 .sin_len = sizeof (struct sockaddr_in),
376 .sin_family = AF_INET,
377 };
378 static struct sockaddr_in icmpgw = {
379 .sin_len = sizeof (struct sockaddr_in),
380 .sin_family = AF_INET,
381 };
382 struct sockaddr_in icmpmask = {
383 .sin_len = 8,
384 .sin_family = 0,
385 };
386
387 /*
388 * Process a received ICMP message.
389 */
390 void
391 icmp_input(struct mbuf *m, ...)
392 {
393 int proto;
394 struct icmp *icp;
395 struct ip *ip = mtod(m, struct ip *);
396 int icmplen;
397 int i;
398 struct in_ifaddr *ia;
399 void *(*ctlfunc)(int, const struct sockaddr *, void *);
400 int code;
401 int hlen;
402 va_list ap;
403 struct rtentry *rt;
404
405 va_start(ap, m);
406 hlen = va_arg(ap, int);
407 proto = va_arg(ap, int);
408 va_end(ap);
409
410 /*
411 * Locate icmp structure in mbuf, and check
412 * that not corrupted and of at least minimum length.
413 */
414 icmplen = ntohs(ip->ip_len) - hlen;
415 #ifdef ICMPPRINTFS
416 if (icmpprintfs) {
417 printf("icmp_input from `%s' to ", inet_ntoa(ip->ip_src));
418 printf("`%s', len %d\n", inet_ntoa(ip->ip_dst), icmplen);
419 }
420 #endif
421 if (icmplen < ICMP_MINLEN) {
422 icmpstat.icps_tooshort++;
423 goto freeit;
424 }
425 i = hlen + min(icmplen, ICMP_ADVLENMIN);
426 if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == 0) {
427 icmpstat.icps_tooshort++;
428 return;
429 }
430 ip = mtod(m, struct ip *);
431 m->m_len -= hlen;
432 m->m_data += hlen;
433 icp = mtod(m, struct icmp *);
434 /* Don't need to assert alignment, here. */
435 if (in_cksum(m, icmplen)) {
436 icmpstat.icps_checksum++;
437 goto freeit;
438 }
439 m->m_len += hlen;
440 m->m_data -= hlen;
441
442 #ifdef ICMPPRINTFS
443 /*
444 * Message type specific processing.
445 */
446 if (icmpprintfs)
447 printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type,
448 icp->icmp_code);
449 #endif
450 if (icp->icmp_type > ICMP_MAXTYPE)
451 goto raw;
452 icmpstat.icps_inhist[icp->icmp_type]++;
453 code = icp->icmp_code;
454 switch (icp->icmp_type) {
455
456 case ICMP_UNREACH:
457 switch (code) {
458 case ICMP_UNREACH_NET:
459 code = PRC_UNREACH_NET;
460 break;
461
462 case ICMP_UNREACH_HOST:
463 code = PRC_UNREACH_HOST;
464 break;
465
466 case ICMP_UNREACH_PROTOCOL:
467 code = PRC_UNREACH_PROTOCOL;
468 break;
469
470 case ICMP_UNREACH_PORT:
471 code = PRC_UNREACH_PORT;
472 break;
473
474 case ICMP_UNREACH_SRCFAIL:
475 code = PRC_UNREACH_SRCFAIL;
476 break;
477
478 case ICMP_UNREACH_NEEDFRAG:
479 code = PRC_MSGSIZE;
480 break;
481
482 case ICMP_UNREACH_NET_UNKNOWN:
483 case ICMP_UNREACH_NET_PROHIB:
484 case ICMP_UNREACH_TOSNET:
485 code = PRC_UNREACH_NET;
486 break;
487
488 case ICMP_UNREACH_HOST_UNKNOWN:
489 case ICMP_UNREACH_ISOLATED:
490 case ICMP_UNREACH_HOST_PROHIB:
491 case ICMP_UNREACH_TOSHOST:
492 code = PRC_UNREACH_HOST;
493 break;
494
495 default:
496 goto badcode;
497 }
498 goto deliver;
499
500 case ICMP_TIMXCEED:
501 if (code > 1)
502 goto badcode;
503 code += PRC_TIMXCEED_INTRANS;
504 goto deliver;
505
506 case ICMP_PARAMPROB:
507 if (code > 1)
508 goto badcode;
509 code = PRC_PARAMPROB;
510 goto deliver;
511
512 case ICMP_SOURCEQUENCH:
513 if (code)
514 goto badcode;
515 code = PRC_QUENCH;
516 goto deliver;
517
518 deliver:
519 /*
520 * Problem with datagram; advise higher level routines.
521 */
522 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
523 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
524 icmpstat.icps_badlen++;
525 goto freeit;
526 }
527 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
528 goto badcode;
529 #ifdef ICMPPRINTFS
530 if (icmpprintfs)
531 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
532 #endif
533 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
534 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
535 if (ctlfunc)
536 (void) (*ctlfunc)(code, sintosa(&icmpsrc),
537 &icp->icmp_ip);
538 break;
539
540 badcode:
541 icmpstat.icps_badcode++;
542 break;
543
544 case ICMP_ECHO:
545 icp->icmp_type = ICMP_ECHOREPLY;
546 goto reflect;
547
548 case ICMP_TSTAMP:
549 if (icmplen < ICMP_TSLEN) {
550 icmpstat.icps_badlen++;
551 break;
552 }
553 icp->icmp_type = ICMP_TSTAMPREPLY;
554 icp->icmp_rtime = iptime();
555 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
556 goto reflect;
557
558 case ICMP_MASKREQ:
559 if (icmpmaskrepl == 0)
560 break;
561 /*
562 * We are not able to respond with all ones broadcast
563 * unless we receive it over a point-to-point interface.
564 */
565 if (icmplen < ICMP_MASKLEN) {
566 icmpstat.icps_badlen++;
567 break;
568 }
569 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
570 in_nullhost(ip->ip_dst))
571 icmpdst.sin_addr = ip->ip_src;
572 else
573 icmpdst.sin_addr = ip->ip_dst;
574 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
575 m->m_pkthdr.rcvif));
576 if (ia == 0)
577 break;
578 icp->icmp_type = ICMP_MASKREPLY;
579 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
580 if (in_nullhost(ip->ip_src)) {
581 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
582 ip->ip_src = ia->ia_broadaddr.sin_addr;
583 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
584 ip->ip_src = ia->ia_dstaddr.sin_addr;
585 }
586 reflect:
587 icmpstat.icps_reflect++;
588 icmpstat.icps_outhist[icp->icmp_type]++;
589 icmp_reflect(m);
590 return;
591
592 case ICMP_REDIRECT:
593 if (code > 3)
594 goto badcode;
595 if (icmp_rediraccept == 0)
596 goto freeit;
597 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
598 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
599 icmpstat.icps_badlen++;
600 break;
601 }
602 /*
603 * Short circuit routing redirects to force
604 * immediate change in the kernel's routing
605 * tables. The message is also handed to anyone
606 * listening on a raw socket (e.g. the routing
607 * daemon for use in updating its tables).
608 */
609 icmpgw.sin_addr = ip->ip_src;
610 icmpdst.sin_addr = icp->icmp_gwaddr;
611 #ifdef ICMPPRINTFS
612 if (icmpprintfs) {
613 printf("redirect dst `%s' to `%s'\n",
614 inet_ntoa(icp->icmp_ip.ip_dst),
615 inet_ntoa(icp->icmp_gwaddr));
616 }
617 #endif
618 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
619 rt = NULL;
620 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
621 NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt);
622 if (rt != NULL && icmp_redirtimeout != 0) {
623 i = rt_timer_add(rt, icmp_redirect_timeout,
624 icmp_redirect_timeout_q);
625 if (i)
626 log(LOG_ERR, "ICMP: redirect failed to "
627 "register timeout for route to %x, "
628 "code %d\n",
629 icp->icmp_ip.ip_dst.s_addr, i);
630 }
631 if (rt != NULL)
632 rtfree(rt);
633
634 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
635 #if defined(IPSEC) || defined(FAST_IPSEC)
636 key_sa_routechange((struct sockaddr *)&icmpsrc);
637 #endif
638 break;
639
640 /*
641 * No kernel processing for the following;
642 * just fall through to send to raw listener.
643 */
644 case ICMP_ECHOREPLY:
645 case ICMP_ROUTERADVERT:
646 case ICMP_ROUTERSOLICIT:
647 case ICMP_TSTAMPREPLY:
648 case ICMP_IREQREPLY:
649 case ICMP_MASKREPLY:
650 default:
651 break;
652 }
653
654 raw:
655 rip_input(m, hlen, proto);
656 return;
657
658 freeit:
659 m_freem(m);
660 return;
661 }
662
663 /*
664 * Reflect the ip packet back to the source
665 */
666 void
667 icmp_reflect(struct mbuf *m)
668 {
669 struct ip *ip = mtod(m, struct ip *);
670 struct in_ifaddr *ia;
671 struct ifaddr *ifa;
672 struct sockaddr_in *sin = 0;
673 struct in_addr t;
674 struct mbuf *opts = 0;
675 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
676
677 if (!in_canforward(ip->ip_src) &&
678 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
679 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
680 m_freem(m); /* Bad return address */
681 goto done; /* ip_output() will check for broadcast */
682 }
683 t = ip->ip_dst;
684 ip->ip_dst = ip->ip_src;
685 /*
686 * If the incoming packet was addressed directly to us, use
687 * dst as the src for the reply. Otherwise (broadcast or
688 * anonymous), use an address which corresponds to the
689 * incoming interface, with a preference for the address which
690 * corresponds to the route to the destination of the ICMP.
691 */
692
693 /* Look for packet addressed to us */
694 INADDR_TO_IA(t, ia);
695
696 /* look for packet sent to broadcast address */
697 if (ia == NULL && m->m_pkthdr.rcvif &&
698 (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
699 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
700 if (ifa->ifa_addr->sa_family != AF_INET)
701 continue;
702 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
703 ia = ifatoia(ifa);
704 break;
705 }
706 }
707 }
708
709 if (ia)
710 sin = &ia->ia_addr;
711
712 icmpdst.sin_addr = t;
713
714 /*
715 * if the packet is addressed somewhere else, compute the
716 * source address for packets routed back to the source, and
717 * use that, if it's an address on the interface which
718 * received the packet
719 */
720 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) {
721 struct sockaddr_in sin_dst;
722 struct route icmproute;
723 int errornum;
724
725 sin_dst.sin_family = AF_INET;
726 sin_dst.sin_len = sizeof(struct sockaddr_in);
727 sin_dst.sin_addr = ip->ip_dst;
728 memset(&icmproute, 0, sizeof(icmproute));
729 errornum = 0;
730 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum);
731 /* errornum is never used */
732 rtcache_free(&icmproute);
733 /* check to make sure sin is a source address on rcvif */
734 if (sin) {
735 t = sin->sin_addr;
736 sin = (struct sockaddr_in *)0;
737 INADDR_TO_IA(t, ia);
738 while (ia) {
739 if (ia->ia_ifp == m->m_pkthdr.rcvif) {
740 sin = &ia->ia_addr;
741 break;
742 }
743 NEXT_IA_WITH_SAME_ADDR(ia);
744 }
745 }
746 }
747
748 /*
749 * if it was not addressed to us, but the route doesn't go out
750 * the source interface, pick an address on the source
751 * interface. This can happen when routing is asymmetric, or
752 * when the incoming packet was encapsulated
753 */
754 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) {
755 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
756 if (ifa->ifa_addr->sa_family != AF_INET)
757 continue;
758 sin = &(ifatoia(ifa)->ia_addr);
759 break;
760 }
761 }
762
763 /*
764 * The following happens if the packet was not addressed to us,
765 * and was received on an interface with no IP address:
766 * We find the first AF_INET address on the first non-loopback
767 * interface.
768 */
769 if (sin == (struct sockaddr_in *)0)
770 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
771 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
772 continue;
773 sin = &ia->ia_addr;
774 break;
775 }
776
777 /*
778 * If we still didn't find an address, punt. We could have an
779 * interface up (and receiving packets) with no address.
780 */
781 if (sin == (struct sockaddr_in *)0) {
782 m_freem(m);
783 goto done;
784 }
785
786 ip->ip_src = sin->sin_addr;
787 ip->ip_ttl = MAXTTL;
788
789 if (optlen > 0) {
790 u_char *cp;
791 int opt, cnt;
792 u_int len;
793
794 /*
795 * Retrieve any source routing from the incoming packet;
796 * add on any record-route or timestamp options.
797 */
798 cp = (u_char *) (ip + 1);
799 if ((opts = ip_srcroute()) == 0 &&
800 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
801 MCLAIM(opts, m->m_owner);
802 opts->m_len = sizeof(struct in_addr);
803 *mtod(opts, struct in_addr *) = zeroin_addr;
804 }
805 if (opts) {
806 #ifdef ICMPPRINTFS
807 if (icmpprintfs)
808 printf("icmp_reflect optlen %d rt %d => ",
809 optlen, opts->m_len);
810 #endif
811 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
812 opt = cp[IPOPT_OPTVAL];
813 if (opt == IPOPT_EOL)
814 break;
815 if (opt == IPOPT_NOP)
816 len = 1;
817 else {
818 if (cnt < IPOPT_OLEN + sizeof(*cp))
819 break;
820 len = cp[IPOPT_OLEN];
821 if (len < IPOPT_OLEN + sizeof(*cp) ||
822 len > cnt)
823 break;
824 }
825 /*
826 * Should check for overflow, but it "can't happen"
827 */
828 if (opt == IPOPT_RR || opt == IPOPT_TS ||
829 opt == IPOPT_SECURITY) {
830 memmove(mtod(opts, char *) + opts->m_len,
831 cp, len);
832 opts->m_len += len;
833 }
834 }
835 /* Terminate & pad, if necessary */
836 if ((cnt = opts->m_len % 4) != 0) {
837 for (; cnt < 4; cnt++) {
838 *(mtod(opts, char *) + opts->m_len) =
839 IPOPT_EOL;
840 opts->m_len++;
841 }
842 }
843 #ifdef ICMPPRINTFS
844 if (icmpprintfs)
845 printf("%d\n", opts->m_len);
846 #endif
847 }
848 /*
849 * Now strip out original options by copying rest of first
850 * mbuf's data back, and adjust the IP length.
851 */
852 ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
853 ip->ip_hl = sizeof(struct ip) >> 2;
854 m->m_len -= optlen;
855 if (m->m_flags & M_PKTHDR)
856 m->m_pkthdr.len -= optlen;
857 optlen += sizeof(struct ip);
858 memmove(ip + 1, (char *)ip + optlen,
859 (unsigned)(m->m_len - sizeof(struct ip)));
860 }
861 m_tag_delete_nonpersistent(m);
862 m->m_flags &= ~(M_BCAST|M_MCAST);
863
864 /*
865 * Clear any in-bound checksum flags for this packet.
866 */
867 if (m->m_flags & M_PKTHDR)
868 m->m_pkthdr.csum_flags = 0;
869
870 icmp_send(m, opts);
871 done:
872 if (opts)
873 (void)m_free(opts);
874 }
875
876 /*
877 * Send an icmp packet back to the ip level,
878 * after supplying a checksum.
879 */
880 void
881 icmp_send(struct mbuf *m, struct mbuf *opts)
882 {
883 struct ip *ip = mtod(m, struct ip *);
884 int hlen;
885 struct icmp *icp;
886
887 hlen = ip->ip_hl << 2;
888 m->m_data += hlen;
889 m->m_len -= hlen;
890 icp = mtod(m, struct icmp *);
891 icp->icmp_cksum = 0;
892 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
893 m->m_data -= hlen;
894 m->m_len += hlen;
895 #ifdef ICMPPRINTFS
896 if (icmpprintfs) {
897 printf("icmp_send to destination `%s' from `%s'\n",
898 inet_ntoa(ip->ip_dst), inet_ntoa(ip->ip_src));
899 }
900 #endif
901 (void) ip_output(m, opts, NULL, 0,
902 (struct ip_moptions *)NULL, (struct socket *)NULL);
903 }
904
905 n_time
906 iptime(void)
907 {
908 struct timeval atv;
909 u_long t;
910
911 microtime(&atv);
912 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
913 return (htonl(t));
914 }
915
916 /*
917 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures
918 * that the new value is in the correct range.
919 */
920 static int
921 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS)
922 {
923 int error, t;
924 struct sysctlnode node;
925
926 node = *rnode;
927 node.sysctl_data = &t;
928 t = icmpreturndatabytes;
929 error = sysctl_lookup(SYSCTLFN_CALL(&node));
930 if (error || newp == NULL)
931 return (error);
932
933 if (t < 8 || t > 512)
934 return (EINVAL);
935 icmpreturndatabytes = t;
936
937 return (0);
938 }
939
940 /*
941 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that
942 * the given value is not less than zero and then resets the timeout
943 * queue.
944 */
945 static int
946 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS)
947 {
948 int error, tmp;
949 struct sysctlnode node;
950
951 node = *rnode;
952 node.sysctl_data = &tmp;
953 tmp = icmp_redirtimeout;
954 error = sysctl_lookup(SYSCTLFN_CALL(&node));
955 if (error || newp == NULL)
956 return (error);
957 if (tmp < 0)
958 return (EINVAL);
959 icmp_redirtimeout = tmp;
960
961 /*
962 * was it a *defined* side-effect that anyone even *reading*
963 * this value causes these things to happen?
964 */
965 if (icmp_redirect_timeout_q != NULL) {
966 if (icmp_redirtimeout == 0) {
967 rt_timer_queue_destroy(icmp_redirect_timeout_q,
968 true);
969 icmp_redirect_timeout_q = NULL;
970 } else {
971 rt_timer_queue_change(icmp_redirect_timeout_q,
972 icmp_redirtimeout);
973 }
974 } else if (icmp_redirtimeout > 0) {
975 icmp_redirect_timeout_q =
976 rt_timer_queue_create(icmp_redirtimeout);
977 }
978
979 return (0);
980 }
981
982 SYSCTL_SETUP(sysctl_net_inet_icmp_setup, "sysctl net.inet.icmp subtree setup")
983 {
984
985 sysctl_createv(clog, 0, NULL, NULL,
986 CTLFLAG_PERMANENT,
987 CTLTYPE_NODE, "net", NULL,
988 NULL, 0, NULL, 0,
989 CTL_NET, CTL_EOL);
990 sysctl_createv(clog, 0, NULL, NULL,
991 CTLFLAG_PERMANENT,
992 CTLTYPE_NODE, "inet", NULL,
993 NULL, 0, NULL, 0,
994 CTL_NET, PF_INET, CTL_EOL);
995 sysctl_createv(clog, 0, NULL, NULL,
996 CTLFLAG_PERMANENT,
997 CTLTYPE_NODE, "icmp",
998 SYSCTL_DESCR("ICMPv4 related settings"),
999 NULL, 0, NULL, 0,
1000 CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL);
1001
1002 sysctl_createv(clog, 0, NULL, NULL,
1003 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1004 CTLTYPE_INT, "maskrepl",
1005 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
1006 NULL, 0, &icmpmaskrepl, 0,
1007 CTL_NET, PF_INET, IPPROTO_ICMP,
1008 ICMPCTL_MASKREPL, CTL_EOL);
1009 sysctl_createv(clog, 0, NULL, NULL,
1010 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1011 CTLTYPE_INT, "returndatabytes",
1012 SYSCTL_DESCR("Number of bytes to return in an ICMP "
1013 "error message"),
1014 sysctl_net_inet_icmp_returndatabytes, 0,
1015 &icmpreturndatabytes, 0,
1016 CTL_NET, PF_INET, IPPROTO_ICMP,
1017 ICMPCTL_RETURNDATABYTES, CTL_EOL);
1018 sysctl_createv(clog, 0, NULL, NULL,
1019 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1020 CTLTYPE_INT, "errppslimit",
1021 SYSCTL_DESCR("Maximum number of outgoing ICMP error "
1022 "messages per second"),
1023 NULL, 0, &icmperrppslim, 0,
1024 CTL_NET, PF_INET, IPPROTO_ICMP,
1025 ICMPCTL_ERRPPSLIMIT, CTL_EOL);
1026 sysctl_createv(clog, 0, NULL, NULL,
1027 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1028 CTLTYPE_INT, "rediraccept",
1029 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
1030 NULL, 0, &icmp_rediraccept, 0,
1031 CTL_NET, PF_INET, IPPROTO_ICMP,
1032 ICMPCTL_REDIRACCEPT, CTL_EOL);
1033 sysctl_createv(clog, 0, NULL, NULL,
1034 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1035 CTLTYPE_INT, "redirtimeout",
1036 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
1037 "routes"),
1038 sysctl_net_inet_icmp_redirtimeout, 0,
1039 &icmp_redirtimeout, 0,
1040 CTL_NET, PF_INET, IPPROTO_ICMP,
1041 ICMPCTL_REDIRTIMEOUT, CTL_EOL);
1042 sysctl_createv(clog, 0, NULL, NULL,
1043 CTLFLAG_PERMANENT,
1044 CTLTYPE_STRUCT, "stats",
1045 SYSCTL_DESCR("ICMP statistics"),
1046 NULL, 0, &icmpstat, sizeof(icmpstat),
1047 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS,
1048 CTL_EOL);
1049 }
1050
1051 /* Table of common MTUs: */
1052
1053 static const u_int mtu_table[] = {
1054 65535, 65280, 32000, 17914, 9180, 8166,
1055 4352, 2002, 1492, 1006, 508, 296, 68, 0
1056 };
1057
1058 void
1059 icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
1060 {
1061 struct icmp_mtudisc_callback *mc;
1062 struct sockaddr *dst = sintosa(&icmpsrc);
1063 struct rtentry *rt;
1064 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */
1065 int error;
1066
1067 rt = rtalloc1(dst, 1);
1068 if (rt == 0)
1069 return;
1070
1071 /* If we didn't get a host route, allocate one */
1072
1073 if ((rt->rt_flags & RTF_HOST) == 0) {
1074 struct rtentry *nrt;
1075
1076 error = rtrequest((int) RTM_ADD, dst,
1077 (struct sockaddr *) rt->rt_gateway,
1078 (struct sockaddr *) 0,
1079 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
1080 if (error) {
1081 rtfree(rt);
1082 return;
1083 }
1084 nrt->rt_rmx = rt->rt_rmx;
1085 rtfree(rt);
1086 rt = nrt;
1087 }
1088 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
1089 if (error) {
1090 rtfree(rt);
1091 return;
1092 }
1093
1094 if (mtu == 0) {
1095 int i = 0;
1096
1097 mtu = ntohs(icp->icmp_ip.ip_len);
1098 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
1099 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
1100 mtu -= (icp->icmp_ip.ip_hl << 2);
1101
1102 /* If we still can't guess a value, try the route */
1103
1104 if (mtu == 0) {
1105 mtu = rt->rt_rmx.rmx_mtu;
1106
1107 /* If no route mtu, default to the interface mtu */
1108
1109 if (mtu == 0)
1110 mtu = rt->rt_ifp->if_mtu;
1111 }
1112
1113 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
1114 if (mtu > mtu_table[i]) {
1115 mtu = mtu_table[i];
1116 break;
1117 }
1118 }
1119
1120 /*
1121 * XXX: RTV_MTU is overloaded, since the admin can set it
1122 * to turn off PMTU for a route, and the kernel can
1123 * set it to indicate a serious problem with PMTU
1124 * on a route. We should be using a separate flag
1125 * for the kernel to indicate this.
1126 */
1127
1128 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1129 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
1130 rt->rt_rmx.rmx_locks |= RTV_MTU;
1131 else if (rt->rt_rmx.rmx_mtu > mtu ||
1132 rt->rt_rmx.rmx_mtu == 0) {
1133 icmpstat.icps_pmtuchg++;
1134 rt->rt_rmx.rmx_mtu = mtu;
1135 }
1136 }
1137
1138 if (rt)
1139 rtfree(rt);
1140
1141 /*
1142 * Notify protocols that the MTU for this destination
1143 * has changed.
1144 */
1145 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
1146 mc = LIST_NEXT(mc, mc_list))
1147 (*mc->mc_func)(faddr);
1148 }
1149
1150 /*
1151 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1152 * given current value MTU. If DIR is less than zero, a larger plateau
1153 * is returned; otherwise, a smaller value is returned.
1154 */
1155 u_int
1156 ip_next_mtu(u_int mtu, int dir) /* XXX */
1157 {
1158 int i;
1159
1160 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
1161 if (mtu >= mtu_table[i])
1162 break;
1163 }
1164
1165 if (dir < 0) {
1166 if (i == 0) {
1167 return 0;
1168 } else {
1169 return mtu_table[i - 1];
1170 }
1171 } else {
1172 if (mtu_table[i] == 0) {
1173 return 0;
1174 } else if (mtu > mtu_table[i]) {
1175 return mtu_table[i];
1176 } else {
1177 return mtu_table[i + 1];
1178 }
1179 }
1180 }
1181
1182 static void
1183 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
1184 {
1185 if (rt == NULL)
1186 panic("icmp_mtudisc_timeout: bad route to timeout");
1187 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1188 (RTF_DYNAMIC | RTF_HOST)) {
1189 rtrequest((int) RTM_DELETE, rt_getkey(rt),
1190 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1191 } else {
1192 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1193 rt->rt_rmx.rmx_mtu = 0;
1194 }
1195 }
1196 }
1197
1198 static void
1199 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
1200 {
1201 if (rt == NULL)
1202 panic("icmp_redirect_timeout: bad route to timeout");
1203 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1204 (RTF_DYNAMIC | RTF_HOST)) {
1205 rtrequest((int) RTM_DELETE, rt_getkey(rt),
1206 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1207 }
1208 }
1209
1210 /*
1211 * Perform rate limit check.
1212 * Returns 0 if it is okay to send the icmp packet.
1213 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
1214 * limitation.
1215 *
1216 * XXX per-destination/type check necessary?
1217 */
1218 static int
1219 icmp_ratelimit(const struct in_addr *dst, const int type,
1220 const int code)
1221 {
1222
1223 /* PPS limit */
1224 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
1225 icmperrppslim)) {
1226 /* The packet is subject to rate limit */
1227 return 1;
1228 }
1229
1230 /* okay to send */
1231 return 0;
1232 }
1233