ip_icmp.c revision 1.79 1 /* $NetBSD: ip_icmp.c,v 1.79 2003/11/11 20:25:26 jonathan 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.79 2003/11/11 20:25:26 jonathan 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/icmp_var.h>
130
131 #ifdef IPSEC
132 #include <netinet6/ipsec.h>
133 #include <netkey/key.h>
134 #endif
135
136 #ifdef FAST_IPSEC
137 #include <netipsec/ipsec.h>
138 #include <netipsec/key.h>
139 #endif /* FAST_IPSEC*/
140
141 #include <machine/stdarg.h>
142
143 /*
144 * ICMP routines: error generation, receive packet processing, and
145 * routines to turnaround packets back to the originator, and
146 * host table maintenance routines.
147 */
148
149 int icmpmaskrepl = 0;
150 #ifdef ICMPPRINTFS
151 int icmpprintfs = 0;
152 #endif
153 int icmpreturndatabytes = 8;
154
155 /*
156 * List of callbacks to notify when Path MTU changes are made.
157 */
158 struct icmp_mtudisc_callback {
159 LIST_ENTRY(icmp_mtudisc_callback) mc_list;
160 void (*mc_func) __P((struct in_addr));
161 };
162
163 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
164 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
165
166 #if 0
167 static int ip_next_mtu __P((int, int));
168 #else
169 /*static*/ int ip_next_mtu __P((int, int));
170 #endif
171
172 extern int icmperrppslim;
173 static int icmperrpps_count = 0;
174 static struct timeval icmperrppslim_last;
175 static int icmp_rediraccept = 1;
176 static int icmp_redirtimeout = 600;
177 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
178
179 static void icmp_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
180 static void icmp_redirect_timeout __P((struct rtentry *, struct rttimer *));
181
182 static int icmp_ratelimit __P((const struct in_addr *, const int, const int));
183
184
185 void
186 icmp_init()
187 {
188 /*
189 * This is only useful if the user initializes redirtimeout to
190 * something other than zero.
191 */
192 if (icmp_redirtimeout != 0) {
193 icmp_redirect_timeout_q =
194 rt_timer_queue_create(icmp_redirtimeout);
195 }
196 }
197
198 /*
199 * Register a Path MTU Discovery callback.
200 */
201 void
202 icmp_mtudisc_callback_register(func)
203 void (*func) __P((struct in_addr));
204 {
205 struct icmp_mtudisc_callback *mc;
206
207 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
208 mc = LIST_NEXT(mc, mc_list)) {
209 if (mc->mc_func == func)
210 return;
211 }
212
213 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
214 if (mc == NULL)
215 panic("icmp_mtudisc_callback_register");
216
217 mc->mc_func = func;
218 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
219 }
220
221 /*
222 * Generate an error packet of type error
223 * in response to bad packet ip.
224 */
225 void
226 icmp_error(n, type, code, dest, destifp)
227 struct mbuf *n;
228 int type, code;
229 n_long dest;
230 struct ifnet *destifp;
231 {
232 struct ip *oip = mtod(n, struct ip *), *nip;
233 unsigned oiplen = oip->ip_hl << 2;
234 struct icmp *icp;
235 struct mbuf *m;
236 unsigned icmplen, mblen;
237
238 #ifdef ICMPPRINTFS
239 if (icmpprintfs)
240 printf("icmp_error(%x, %d, %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 *)((caddr_t)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 && destifp)
331 icp->icmp_nextmtu = htons(destifp->if_mtu);
332 }
333
334 icp->icmp_code = code;
335 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
336 nip = &icp->icmp_ip;
337
338 /*
339 * Now, copy old ip header (without options)
340 * in front of icmp message.
341 */
342 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
343 panic("icmp len");
344 m->m_data -= sizeof(struct ip);
345 m->m_len += sizeof(struct ip);
346 m->m_pkthdr.len = m->m_len;
347 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
348 nip = mtod(m, struct ip *);
349 /* ip_v set in ip_output */
350 nip->ip_hl = sizeof(struct ip) >> 2;
351 nip->ip_tos = 0;
352 nip->ip_len = htons(m->m_len);
353 /* ip_id set in ip_output */
354 nip->ip_off = htons(0);
355 /* ip_ttl set in icmp_reflect */
356 nip->ip_p = IPPROTO_ICMP;
357 nip->ip_src = oip->ip_src;
358 nip->ip_dst = oip->ip_dst;
359 icmp_reflect(m);
360
361 freeit:
362 m_freem(n);
363 }
364
365 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
366 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
367 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
368 struct sockaddr_in icmpmask = { 8, 0 };
369
370 /*
371 * Process a received ICMP message.
372 */
373 void
374 #if __STDC__
375 icmp_input(struct mbuf *m, ...)
376 #else
377 icmp_input(m, va_alist)
378 struct mbuf *m;
379 va_dcl
380 #endif
381 {
382 int proto;
383 struct icmp *icp;
384 struct ip *ip = mtod(m, struct ip *);
385 int icmplen;
386 int i;
387 struct in_ifaddr *ia;
388 void *(*ctlfunc) __P((int, struct sockaddr *, void *));
389 int code;
390 int hlen;
391 va_list ap;
392 struct rtentry *rt;
393
394 va_start(ap, m);
395 hlen = va_arg(ap, int);
396 proto = va_arg(ap, int);
397 va_end(ap);
398
399 /*
400 * Locate icmp structure in mbuf, and check
401 * that not corrupted and of at least minimum length.
402 */
403 icmplen = ntohs(ip->ip_len) - hlen;
404 #ifdef ICMPPRINTFS
405 if (icmpprintfs)
406 printf("icmp_input from %x to %x, len %d\n",
407 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
408 icmplen);
409 #endif
410 if (icmplen < ICMP_MINLEN) {
411 icmpstat.icps_tooshort++;
412 goto freeit;
413 }
414 i = hlen + min(icmplen, ICMP_ADVLENMIN);
415 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
416 icmpstat.icps_tooshort++;
417 return;
418 }
419 ip = mtod(m, struct ip *);
420 m->m_len -= hlen;
421 m->m_data += hlen;
422 icp = mtod(m, struct icmp *);
423 /* Don't need to assert alignment, here. */
424 if (in_cksum(m, icmplen)) {
425 icmpstat.icps_checksum++;
426 goto freeit;
427 }
428 m->m_len += hlen;
429 m->m_data -= hlen;
430
431 #ifdef ICMPPRINTFS
432 /*
433 * Message type specific processing.
434 */
435 if (icmpprintfs)
436 printf("icmp_input, type %d code %d\n", icp->icmp_type,
437 icp->icmp_code);
438 #endif
439 if (icp->icmp_type > ICMP_MAXTYPE)
440 goto raw;
441 icmpstat.icps_inhist[icp->icmp_type]++;
442 code = icp->icmp_code;
443 switch (icp->icmp_type) {
444
445 case ICMP_UNREACH:
446 switch (code) {
447 case ICMP_UNREACH_NET:
448 case ICMP_UNREACH_HOST:
449 case ICMP_UNREACH_PROTOCOL:
450 case ICMP_UNREACH_PORT:
451 case ICMP_UNREACH_SRCFAIL:
452 code += PRC_UNREACH_NET;
453 break;
454
455 case ICMP_UNREACH_NEEDFRAG:
456 code = PRC_MSGSIZE;
457 break;
458
459 case ICMP_UNREACH_NET_UNKNOWN:
460 case ICMP_UNREACH_NET_PROHIB:
461 case ICMP_UNREACH_TOSNET:
462 code = PRC_UNREACH_NET;
463 break;
464
465 case ICMP_UNREACH_HOST_UNKNOWN:
466 case ICMP_UNREACH_ISOLATED:
467 case ICMP_UNREACH_HOST_PROHIB:
468 case ICMP_UNREACH_TOSHOST:
469 code = PRC_UNREACH_HOST;
470 break;
471
472 default:
473 goto badcode;
474 }
475 goto deliver;
476
477 case ICMP_TIMXCEED:
478 if (code > 1)
479 goto badcode;
480 code += PRC_TIMXCEED_INTRANS;
481 goto deliver;
482
483 case ICMP_PARAMPROB:
484 if (code > 1)
485 goto badcode;
486 code = PRC_PARAMPROB;
487 goto deliver;
488
489 case ICMP_SOURCEQUENCH:
490 if (code)
491 goto badcode;
492 code = PRC_QUENCH;
493 goto deliver;
494
495 deliver:
496 /*
497 * Problem with datagram; advise higher level routines.
498 */
499 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
500 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
501 icmpstat.icps_badlen++;
502 goto freeit;
503 }
504 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
505 goto badcode;
506 #ifdef ICMPPRINTFS
507 if (icmpprintfs)
508 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
509 #endif
510 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
511 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
512 if (ctlfunc)
513 (void) (*ctlfunc)(code, sintosa(&icmpsrc),
514 &icp->icmp_ip);
515 break;
516
517 badcode:
518 icmpstat.icps_badcode++;
519 break;
520
521 case ICMP_ECHO:
522 icp->icmp_type = ICMP_ECHOREPLY;
523 goto reflect;
524
525 case ICMP_TSTAMP:
526 if (icmplen < ICMP_TSLEN) {
527 icmpstat.icps_badlen++;
528 break;
529 }
530 icp->icmp_type = ICMP_TSTAMPREPLY;
531 icp->icmp_rtime = iptime();
532 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
533 goto reflect;
534
535 case ICMP_MASKREQ:
536 if (icmpmaskrepl == 0)
537 break;
538 /*
539 * We are not able to respond with all ones broadcast
540 * unless we receive it over a point-to-point interface.
541 */
542 if (icmplen < ICMP_MASKLEN) {
543 icmpstat.icps_badlen++;
544 break;
545 }
546 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
547 in_nullhost(ip->ip_dst))
548 icmpdst.sin_addr = ip->ip_src;
549 else
550 icmpdst.sin_addr = ip->ip_dst;
551 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
552 m->m_pkthdr.rcvif));
553 if (ia == 0)
554 break;
555 icp->icmp_type = ICMP_MASKREPLY;
556 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
557 if (in_nullhost(ip->ip_src)) {
558 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
559 ip->ip_src = ia->ia_broadaddr.sin_addr;
560 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
561 ip->ip_src = ia->ia_dstaddr.sin_addr;
562 }
563 reflect:
564 icmpstat.icps_reflect++;
565 icmpstat.icps_outhist[icp->icmp_type]++;
566 icmp_reflect(m);
567 return;
568
569 case ICMP_REDIRECT:
570 if (code > 3)
571 goto badcode;
572 if (icmp_rediraccept == 0)
573 goto freeit;
574 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
575 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
576 icmpstat.icps_badlen++;
577 break;
578 }
579 /*
580 * Short circuit routing redirects to force
581 * immediate change in the kernel's routing
582 * tables. The message is also handed to anyone
583 * listening on a raw socket (e.g. the routing
584 * daemon for use in updating its tables).
585 */
586 icmpgw.sin_addr = ip->ip_src;
587 icmpdst.sin_addr = icp->icmp_gwaddr;
588 #ifdef ICMPPRINTFS
589 if (icmpprintfs)
590 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
591 icp->icmp_gwaddr);
592 #endif
593 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
594 rt = NULL;
595 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
596 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
597 sintosa(&icmpgw), (struct rtentry **)&rt);
598 if (rt != NULL && icmp_redirtimeout != 0) {
599 i = rt_timer_add(rt, icmp_redirect_timeout,
600 icmp_redirect_timeout_q);
601 if (i)
602 log(LOG_ERR, "ICMP: redirect failed to "
603 "register timeout for route to %x, "
604 "code %d\n",
605 icp->icmp_ip.ip_dst.s_addr, i);
606 }
607 if (rt != NULL)
608 rtfree(rt);
609
610 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
611 #if defined(IPSEC) || defined(FAST_IPSEC)
612 key_sa_routechange((struct sockaddr *)&icmpsrc);
613 #endif
614 break;
615
616 /*
617 * No kernel processing for the following;
618 * just fall through to send to raw listener.
619 */
620 case ICMP_ECHOREPLY:
621 case ICMP_ROUTERADVERT:
622 case ICMP_ROUTERSOLICIT:
623 case ICMP_TSTAMPREPLY:
624 case ICMP_IREQREPLY:
625 case ICMP_MASKREPLY:
626 default:
627 break;
628 }
629
630 raw:
631 rip_input(m, hlen, proto);
632 return;
633
634 freeit:
635 m_freem(m);
636 return;
637 }
638
639 /*
640 * Reflect the ip packet back to the source
641 */
642 void
643 icmp_reflect(m)
644 struct mbuf *m;
645 {
646 struct ip *ip = mtod(m, struct ip *);
647 struct in_ifaddr *ia;
648 struct ifaddr *ifa;
649 struct sockaddr_in *sin = 0;
650 struct in_addr t;
651 struct mbuf *opts = 0;
652 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
653
654 if (!in_canforward(ip->ip_src) &&
655 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
656 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
657 m_freem(m); /* Bad return address */
658 goto done; /* ip_output() will check for broadcast */
659 }
660 t = ip->ip_dst;
661 ip->ip_dst = ip->ip_src;
662 /*
663 * If the incoming packet was addressed directly to us, use
664 * dst as the src for the reply. Otherwise (broadcast or
665 * anonymous), use an address which corresponds to the
666 * incoming interface, with a preference for the address which
667 * corresponds to the route to the destination of the ICMP.
668 */
669
670 /* Look for packet addressed to us */
671 INADDR_TO_IA(t, ia);
672
673 /* look for packet sent to broadcast address */
674 if (ia == NULL && (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
675 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
676 if (ifa->ifa_addr->sa_family != AF_INET)
677 continue;
678 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
679 ia = ifatoia(ifa);
680 break;
681 }
682 }
683 }
684
685 if (ia)
686 sin = &ia->ia_addr;
687
688 icmpdst.sin_addr = t;
689
690 /*
691 * if the packet is addressed somewhere else, compute the
692 * source address for packets routed back to the source, and
693 * use that, if it's an address on the interface which
694 * received the packet
695 */
696 if (sin == (struct sockaddr_in *)0) {
697 struct sockaddr_in sin_dst;
698 struct route icmproute;
699 int errornum;
700
701 sin_dst.sin_family = AF_INET;
702 sin_dst.sin_len = sizeof(struct sockaddr_in);
703 sin_dst.sin_addr = ip->ip_dst;
704 bzero(&icmproute, sizeof(icmproute));
705 errornum = 0;
706 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum);
707 /* errornum is never used */
708 if (icmproute.ro_rt)
709 RTFREE(icmproute.ro_rt);
710 /* check to make sure sin is a source address on rcvif */
711 if (sin) {
712 t = sin->sin_addr;
713 sin = (struct sockaddr_in *)0;
714 INADDR_TO_IA(t, ia);
715 while (ia) {
716 if (ia->ia_ifp == m->m_pkthdr.rcvif) {
717 sin = &ia->ia_addr;
718 break;
719 }
720 NEXT_IA_WITH_SAME_ADDR(ia);
721 }
722 }
723 }
724
725 /*
726 * if it was not addressed to us, but the route doesn't go out
727 * the source interface, pick an address on the source
728 * interface. This can happen when routing is asymmetric, or
729 * when the incoming packet was encapsulated
730 */
731 if (sin == (struct sockaddr_in *)0) {
732 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
733 if (ifa->ifa_addr->sa_family != AF_INET)
734 continue;
735 sin = &(ifatoia(ifa)->ia_addr);
736 break;
737 }
738 }
739
740 /*
741 * The following happens if the packet was not addressed to us,
742 * and was received on an interface with no IP address:
743 * We find the first AF_INET address on the first non-loopback
744 * interface.
745 */
746 if (sin == (struct sockaddr_in *)0)
747 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
748 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
749 continue;
750 sin = &ia->ia_addr;
751 break;
752 }
753
754 /*
755 * If we still didn't find an address, punt. We could have an
756 * interface up (and receiving packets) with no address.
757 */
758 if (sin == (struct sockaddr_in *)0) {
759 m_freem(m);
760 goto done;
761 }
762
763 ip->ip_src = sin->sin_addr;
764 ip->ip_ttl = MAXTTL;
765
766 if (optlen > 0) {
767 u_char *cp;
768 int opt, cnt;
769 u_int len;
770
771 /*
772 * Retrieve any source routing from the incoming packet;
773 * add on any record-route or timestamp options.
774 */
775 cp = (u_char *) (ip + 1);
776 if ((opts = ip_srcroute()) == 0 &&
777 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
778 MCLAIM(opts, m->m_owner);
779 opts->m_len = sizeof(struct in_addr);
780 *mtod(opts, struct in_addr *) = zeroin_addr;
781 }
782 if (opts) {
783 #ifdef ICMPPRINTFS
784 if (icmpprintfs)
785 printf("icmp_reflect optlen %d rt %d => ",
786 optlen, opts->m_len);
787 #endif
788 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
789 opt = cp[IPOPT_OPTVAL];
790 if (opt == IPOPT_EOL)
791 break;
792 if (opt == IPOPT_NOP)
793 len = 1;
794 else {
795 if (cnt < IPOPT_OLEN + sizeof(*cp))
796 break;
797 len = cp[IPOPT_OLEN];
798 if (len < IPOPT_OLEN + sizeof(*cp) ||
799 len > cnt)
800 break;
801 }
802 /*
803 * Should check for overflow, but it "can't happen"
804 */
805 if (opt == IPOPT_RR || opt == IPOPT_TS ||
806 opt == IPOPT_SECURITY) {
807 bcopy((caddr_t)cp,
808 mtod(opts, caddr_t) + opts->m_len, len);
809 opts->m_len += len;
810 }
811 }
812 /* Terminate & pad, if necessary */
813 if ((cnt = opts->m_len % 4) != 0) {
814 for (; cnt < 4; cnt++) {
815 *(mtod(opts, caddr_t) + opts->m_len) =
816 IPOPT_EOL;
817 opts->m_len++;
818 }
819 }
820 #ifdef ICMPPRINTFS
821 if (icmpprintfs)
822 printf("%d\n", opts->m_len);
823 #endif
824 }
825 /*
826 * Now strip out original options by copying rest of first
827 * mbuf's data back, and adjust the IP length.
828 */
829 ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
830 ip->ip_hl = sizeof(struct ip) >> 2;
831 m->m_len -= optlen;
832 if (m->m_flags & M_PKTHDR)
833 m->m_pkthdr.len -= optlen;
834 optlen += sizeof(struct ip);
835 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
836 (unsigned)(m->m_len - sizeof(struct ip)));
837 }
838 m->m_flags &= ~(M_BCAST|M_MCAST);
839
840 /*
841 * Clear any in-bound checksum flags for this packet.
842 */
843 m->m_pkthdr.csum_flags = 0;
844
845 icmp_send(m, opts);
846 done:
847 if (opts)
848 (void)m_free(opts);
849 }
850
851 /*
852 * Send an icmp packet back to the ip level,
853 * after supplying a checksum.
854 */
855 void
856 icmp_send(m, opts)
857 struct mbuf *m;
858 struct mbuf *opts;
859 {
860 struct ip *ip = mtod(m, struct ip *);
861 int hlen;
862 struct icmp *icp;
863
864 hlen = ip->ip_hl << 2;
865 m->m_data += hlen;
866 m->m_len -= hlen;
867 icp = mtod(m, struct icmp *);
868 icp->icmp_cksum = 0;
869 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
870 m->m_data -= hlen;
871 m->m_len += hlen;
872 #ifdef ICMPPRINTFS
873 if (icmpprintfs)
874 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
875 #endif
876 (void) ip_output(m, opts, NULL, 0,
877 (struct ip_moptions *)NULL, (struct socket *)NULL);
878 }
879
880 n_time
881 iptime()
882 {
883 struct timeval atv;
884 u_long t;
885
886 microtime(&atv);
887 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
888 return (htonl(t));
889 }
890
891 int
892 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
893 int *name;
894 u_int namelen;
895 void *oldp;
896 size_t *oldlenp;
897 void *newp;
898 size_t newlen;
899 {
900 int arg, error;
901
902 /* All sysctl names at this level are terminal. */
903 if (namelen != 1)
904 return (ENOTDIR);
905
906 switch (name[0])
907 {
908 case ICMPCTL_MASKREPL:
909 error = sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl);
910 break;
911 case ICMPCTL_RETURNDATABYTES:
912 arg = icmpreturndatabytes;
913 error = sysctl_int(oldp, oldlenp, newp, newlen, &arg);
914 if (error)
915 break;
916 if ((arg >= 8) || (arg <= 512))
917 icmpreturndatabytes = arg;
918 else
919 error = EINVAL;
920 break;
921 case ICMPCTL_ERRPPSLIMIT:
922 error = sysctl_int(oldp, oldlenp, newp, newlen, &icmperrppslim);
923 break;
924 case ICMPCTL_REDIRACCEPT:
925 error = sysctl_int(oldp, oldlenp, newp, newlen,
926 &icmp_rediraccept);
927 break;
928 case ICMPCTL_REDIRTIMEOUT:
929 error = sysctl_int(oldp, oldlenp, newp, newlen,
930 &icmp_redirtimeout);
931 if (icmp_redirect_timeout_q != NULL) {
932 if (icmp_redirtimeout == 0) {
933 rt_timer_queue_destroy(icmp_redirect_timeout_q,
934 TRUE);
935 icmp_redirect_timeout_q = NULL;
936 } else {
937 rt_timer_queue_change(icmp_redirect_timeout_q,
938 icmp_redirtimeout);
939 }
940 } else if (icmp_redirtimeout > 0) {
941 icmp_redirect_timeout_q =
942 rt_timer_queue_create(icmp_redirtimeout);
943 }
944 return (error);
945 default:
946 error = ENOPROTOOPT;
947 break;
948 }
949 return error;
950 }
951
952 /* Table of common MTUs: */
953
954 static const u_int mtu_table[] = {
955 65535, 65280, 32000, 17914, 9180, 8166,
956 4352, 2002, 1492, 1006, 508, 296, 68, 0
957 };
958
959 void
960 icmp_mtudisc(icp, faddr)
961 struct icmp *icp;
962 struct in_addr faddr;
963 {
964 struct icmp_mtudisc_callback *mc;
965 struct sockaddr *dst = sintosa(&icmpsrc);
966 struct rtentry *rt;
967 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */
968 int error;
969
970 rt = rtalloc1(dst, 1);
971 if (rt == 0)
972 return;
973
974 /* If we didn't get a host route, allocate one */
975
976 if ((rt->rt_flags & RTF_HOST) == 0) {
977 struct rtentry *nrt;
978
979 error = rtrequest((int) RTM_ADD, dst,
980 (struct sockaddr *) rt->rt_gateway,
981 (struct sockaddr *) 0,
982 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
983 if (error) {
984 rtfree(rt);
985 return;
986 }
987 nrt->rt_rmx = rt->rt_rmx;
988 rtfree(rt);
989 rt = nrt;
990 }
991 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
992 if (error) {
993 rtfree(rt);
994 return;
995 }
996
997 if (mtu == 0) {
998 int i = 0;
999
1000 mtu = ntohs(icp->icmp_ip.ip_len);
1001 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
1002 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
1003 mtu -= (icp->icmp_ip.ip_hl << 2);
1004
1005 /* If we still can't guess a value, try the route */
1006
1007 if (mtu == 0) {
1008 mtu = rt->rt_rmx.rmx_mtu;
1009
1010 /* If no route mtu, default to the interface mtu */
1011
1012 if (mtu == 0)
1013 mtu = rt->rt_ifp->if_mtu;
1014 }
1015
1016 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
1017 if (mtu > mtu_table[i]) {
1018 mtu = mtu_table[i];
1019 break;
1020 }
1021 }
1022
1023 /*
1024 * XXX: RTV_MTU is overloaded, since the admin can set it
1025 * to turn off PMTU for a route, and the kernel can
1026 * set it to indicate a serious problem with PMTU
1027 * on a route. We should be using a separate flag
1028 * for the kernel to indicate this.
1029 */
1030
1031 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1032 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
1033 rt->rt_rmx.rmx_locks |= RTV_MTU;
1034 else if (rt->rt_rmx.rmx_mtu > mtu ||
1035 rt->rt_rmx.rmx_mtu == 0) {
1036 icmpstat.icps_pmtuchg++;
1037 rt->rt_rmx.rmx_mtu = mtu;
1038 }
1039 }
1040
1041 if (rt)
1042 rtfree(rt);
1043
1044 /*
1045 * Notify protocols that the MTU for this destination
1046 * has changed.
1047 */
1048 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
1049 mc = LIST_NEXT(mc, mc_list))
1050 (*mc->mc_func)(faddr);
1051 }
1052
1053 /*
1054 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1055 * given current value MTU. If DIR is less than zero, a larger plateau
1056 * is returned; otherwise, a smaller value is returned.
1057 */
1058 int
1059 ip_next_mtu(mtu, dir) /* XXX */
1060 int mtu;
1061 int dir;
1062 {
1063 int i;
1064
1065 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
1066 if (mtu >= mtu_table[i])
1067 break;
1068 }
1069
1070 if (dir < 0) {
1071 if (i == 0) {
1072 return 0;
1073 } else {
1074 return mtu_table[i - 1];
1075 }
1076 } else {
1077 if (mtu_table[i] == 0) {
1078 return 0;
1079 } else if (mtu > mtu_table[i]) {
1080 return mtu_table[i];
1081 } else {
1082 return mtu_table[i + 1];
1083 }
1084 }
1085 }
1086
1087 static void
1088 icmp_mtudisc_timeout(rt, r)
1089 struct rtentry *rt;
1090 struct rttimer *r;
1091 {
1092 if (rt == NULL)
1093 panic("icmp_mtudisc_timeout: bad route to timeout");
1094 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1095 (RTF_DYNAMIC | RTF_HOST)) {
1096 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
1097 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1098 } else {
1099 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1100 rt->rt_rmx.rmx_mtu = 0;
1101 }
1102 }
1103 }
1104
1105 static void
1106 icmp_redirect_timeout(rt, r)
1107 struct rtentry *rt;
1108 struct rttimer *r;
1109 {
1110 if (rt == NULL)
1111 panic("icmp_redirect_timeout: bad route to timeout");
1112 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1113 (RTF_DYNAMIC | RTF_HOST)) {
1114 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
1115 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1116 }
1117 }
1118
1119 /*
1120 * Perform rate limit check.
1121 * Returns 0 if it is okay to send the icmp packet.
1122 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
1123 * limitation.
1124 *
1125 * XXX per-destination/type check necessary?
1126 */
1127 static int
1128 icmp_ratelimit(dst, type, code)
1129 const struct in_addr *dst;
1130 const int type; /* not used at this moment */
1131 const int code; /* not used at this moment */
1132 {
1133
1134 /* PPS limit */
1135 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
1136 icmperrppslim)) {
1137 /* The packet is subject to rate limit */
1138 return 1;
1139 }
1140
1141 /*okay to send*/
1142 return 0;
1143 }
1144