]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/netinet/ip_icmp.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / netinet / ip_icmp.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
55 */
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/mbuf.h>
60#include <sys/protosw.h>
61#include <sys/socket.h>
62#include <sys/time.h>
63#include <sys/kernel.h>
64#include <sys/sysctl.h>
65
66#include <net/if.h>
67#include <net/route.h>
68
69#define _IP_VHL
70#include <netinet/in.h>
71#include <netinet/in_systm.h>
72#include <netinet/in_var.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h>
75#include <netinet/ip_var.h>
76#include <netinet/icmp_var.h>
77
78#if IPSEC
79#include <netinet6/ipsec.h>
80#include <netkey/key.h>
81#endif
82
83#if defined(NFAITH) && NFAITH > 0
84#include "faith.h"
85#include <net/if_types.h>
86#endif
87
88/*
89 * ICMP routines: error generation, receive packet processing, and
90 * routines to turnaround packets back to the originator, and
91 * host table maintenance routines.
92 */
93
94static struct icmpstat icmpstat;
95SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD,
96 &icmpstat, icmpstat, "");
97
98static int icmpmaskrepl = 0;
99SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
100 &icmpmaskrepl, 0, "");
101
102#if ICMP_BANDLIM
103
104/*
105 * ICMP error-response bandwidth limiting sysctl. If not enabled, sysctl
106 * variable content is -1 and read-only.
107 */
108
109static int icmplim = 100;
110SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
111 &icmplim, 0, "");
112#else
113
114static int icmplim = -1;
115SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RD,
116 &icmplim, 0, "");
117
118#endif
119
120/*
121 * ICMP broadcast echo sysctl
122 */
123
124static int icmpbmcastecho = 0;
125SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, &icmpbmcastecho,
126 0, "");
127
128
129#if ICMPPRINTFS
130int icmpprintfs = 0;
131#endif
132
133static void icmp_reflect __P((struct mbuf *));
134static void icmp_send __P((struct mbuf *, struct mbuf *));
135int ip_next_mtu __P((int, int));
136
137extern struct protosw inetsw[];
138
139/*
140 * Generate an error packet of type error
141 * in response to bad packet ip.
142 */
143void
144icmp_error(n, type, code, dest, destifp)
145 struct mbuf *n;
146 int type, code;
147 n_long dest;
148 struct ifnet *destifp;
149{
150 register struct ip *oip = mtod(n, struct ip *), *nip;
151 register unsigned oiplen = IP_VHL_HL(oip->ip_vhl) << 2;
152 register struct icmp *icp;
153 register struct mbuf *m;
154 unsigned icmplen;
155
156#if ICMPPRINTFS
157 if (icmpprintfs)
158 printf("icmp_error(%p, %x, %d)\n", oip, type, code);
159#endif
160 if (type != ICMP_REDIRECT)
161 icmpstat.icps_error++;
162 /*
163 * Don't send error if the original packet was encrypted.
164 * Don't send error if not the first fragment of message.
165 * Don't error if the old packet protocol was ICMP
166 * error message, only known informational types.
167 */
168 if (n->m_flags & M_DECRYPTED)
169 goto freeit;
170 if (oip->ip_off &~ (IP_MF|IP_DF))
171 goto freeit;
172 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
173 n->m_len >= oiplen + ICMP_MINLEN &&
174 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
175 icmpstat.icps_oldicmp++;
176 goto freeit;
177 }
178 /* Don't send error in response to a multicast or broadcast packet */
179 if (n->m_flags & (M_BCAST|M_MCAST))
180 goto freeit;
181 /*
182 * First, formulate icmp message
183 */
184 m = m_gethdr(M_DONTWAIT, MT_HEADER);
185 if (m == NULL)
186 goto freeit;
187 icmplen = oiplen + min(8, oip->ip_len);
188 m->m_len = icmplen + ICMP_MINLEN;
189 MH_ALIGN(m, m->m_len);
190 icp = mtod(m, struct icmp *);
191 if ((u_int)type > ICMP_MAXTYPE)
192 panic("icmp_error");
193 icmpstat.icps_outhist[type]++;
194 icp->icmp_type = type;
195 if (type == ICMP_REDIRECT)
196 icp->icmp_gwaddr.s_addr = dest;
197 else {
198 icp->icmp_void = 0;
199 /*
200 * The following assignments assume an overlay with the
201 * zeroed icmp_void field.
202 */
203 if (type == ICMP_PARAMPROB) {
204 icp->icmp_pptr = code;
205 code = 0;
206 } else if (type == ICMP_UNREACH &&
207 code == ICMP_UNREACH_NEEDFRAG && destifp) {
208 icp->icmp_nextmtu = htons(destifp->if_mtu);
209 }
210 }
211
212 icp->icmp_code = code;
213 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
214 nip = &icp->icmp_ip;
215 nip->ip_len = htons((u_short)(nip->ip_len + oiplen));
216
217 /*
218 * Now, copy old ip header (without options)
219 * in front of icmp message.
220 */
221 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
222 panic("icmp len");
223 m->m_data -= sizeof(struct ip);
224 m->m_len += sizeof(struct ip);
225 m->m_pkthdr.len = m->m_len;
226 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
227 m->m_pkthdr.aux = NULL; /* for IPsec */
228 nip = mtod(m, struct ip *);
229 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
230 nip->ip_len = m->m_len;
231 nip->ip_vhl = IP_VHL_BORING;
232 nip->ip_p = IPPROTO_ICMP;
233 nip->ip_tos = 0;
234 icmp_reflect(m);
235
236freeit:
237 m_freem(n);
238}
239
240static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
241static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
242static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
243
244/*
245 * Process a received ICMP message.
246 */
247void
248icmp_input(m, hlen)
249 register struct mbuf *m;
250 int hlen;
251{
252 register struct icmp *icp;
253 register struct ip *ip = mtod(m, struct ip *);
254 int icmplen = ip->ip_len;
255 register int i;
256 struct in_ifaddr *ia;
257 void (*ctlfunc) __P((int, struct sockaddr *, void *));
258 int code;
259
260 /*
261 * Locate icmp structure in mbuf, and check
262 * that not corrupted and of at least minimum length.
263 */
264#if ICMPPRINTFS
265 if (icmpprintfs) {
266 char buf[4 * sizeof "123"];
267 strcpy(buf, inet_ntoa(ip->ip_src));
268 printf("icmp_input from %s to %s, len %d\n",
269 buf, inet_ntoa(ip->ip_dst), icmplen);
270 }
271#endif
272 if (icmplen < ICMP_MINLEN) {
273 icmpstat.icps_tooshort++;
274 goto freeit;
275 }
276 i = hlen + min(icmplen, ICMP_ADVLENMIN);
277 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
278 icmpstat.icps_tooshort++;
279 return;
280 }
281 ip = mtod(m, struct ip *);
282 m->m_len -= hlen;
283 m->m_data += hlen;
284 icp = mtod(m, struct icmp *);
285 if (in_cksum(m, icmplen)) {
286 icmpstat.icps_checksum++;
287 goto freeit;
288 }
289 m->m_len += hlen;
290 m->m_data -= hlen;
291
292#if defined(NFAITH) && 0 < NFAITH
293 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
294 /*
295 * Deliver very specific ICMP type only.
296 */
297 switch (icp->icmp_type) {
298 case ICMP_UNREACH:
299 case ICMP_TIMXCEED:
300 break;
301 default:
302 goto freeit;
303 }
304 }
305#endif
306
307#if ICMPPRINTFS
308 if (icmpprintfs)
309 printf("icmp_input, type %d code %d\n", icp->icmp_type,
310 icp->icmp_code);
311#endif
312
313#if IPSEC
314 /* drop it if it does not match the policy */
315 /* XXX Is there meaning of check in here ? */
316 if (ipsec4_in_reject(m, NULL)) {
317 ipsecstat.in_polvio++;
318 goto freeit;
319 }
320#endif
321
322 /*
323 * Message type specific processing.
324 */
325 if (icp->icmp_type > ICMP_MAXTYPE)
326 goto raw;
327 icmpstat.icps_inhist[icp->icmp_type]++;
328 code = icp->icmp_code;
329 switch (icp->icmp_type) {
330
331 case ICMP_UNREACH:
332 switch (code) {
333 case ICMP_UNREACH_NET:
334 case ICMP_UNREACH_HOST:
335 case ICMP_UNREACH_PROTOCOL:
336 case ICMP_UNREACH_PORT:
337 case ICMP_UNREACH_SRCFAIL:
338 code += PRC_UNREACH_NET;
339 break;
340
341 case ICMP_UNREACH_NEEDFRAG:
342 code = PRC_MSGSIZE;
343 break;
344
345 case ICMP_UNREACH_NET_UNKNOWN:
346 case ICMP_UNREACH_NET_PROHIB:
347 case ICMP_UNREACH_TOSNET:
348 code = PRC_UNREACH_NET;
349 break;
350
351 case ICMP_UNREACH_HOST_UNKNOWN:
352 case ICMP_UNREACH_ISOLATED:
353 case ICMP_UNREACH_HOST_PROHIB:
354 case ICMP_UNREACH_TOSHOST:
355 code = PRC_UNREACH_HOST;
356 break;
357
358 case ICMP_UNREACH_FILTER_PROHIB:
359 case ICMP_UNREACH_HOST_PRECEDENCE:
360 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
361 code = PRC_UNREACH_PORT;
362 break;
363
364 default:
365 goto badcode;
366 }
367 goto deliver;
368
369 case ICMP_TIMXCEED:
370 if (code > 1)
371 goto badcode;
372 code += PRC_TIMXCEED_INTRANS;
373 goto deliver;
374
375 case ICMP_PARAMPROB:
376 if (code > 1)
377 goto badcode;
378 code = PRC_PARAMPROB;
379 goto deliver;
380
381 case ICMP_SOURCEQUENCH:
382 if (code)
383 goto badcode;
384 code = PRC_QUENCH;
385 deliver:
386 /*
387 * Problem with datagram; advise higher level routines.
388 */
389 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
390 IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
391 icmpstat.icps_badlen++;
392 goto freeit;
393 }
394 NTOHS(icp->icmp_ip.ip_len);
395 /* Discard ICMP's in response to multicast packets */
396 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
397 goto badcode;
398#if ICMPPRINTFS
399 if (icmpprintfs)
400 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
401#endif
402 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
403#if 1
404 /*
405 * MTU discovery:
406 * If we got a needfrag and there is a host route to the
407 * original destination, and the MTU is not locked, then
408 * set the MTU in the route to the suggested new value
409 * (if given) and then notify as usual. The ULPs will
410 * notice that the MTU has changed and adapt accordingly.
411 * If no new MTU was suggested, then we guess a new one
412 * less than the current value. If the new MTU is
413 * unreasonably small (arbitrarily set at 296), then
414 * we reset the MTU to the interface value and enable the
415 * lock bit, indicating that we are no longer doing MTU
416 * discovery.
417 */
418 if (code == PRC_MSGSIZE) {
419 struct rtentry *rt;
420 int mtu;
421
422 rt = rtalloc1((struct sockaddr *)&icmpsrc, 0,
423 RTF_CLONING | RTF_PRCLONING);
424 if (rt && (rt->rt_flags & RTF_HOST)
425 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
426 mtu = ntohs(icp->icmp_nextmtu);
427 if (!mtu)
428 mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu,
429 1);
430#if DEBUG_MTUDISC
431 printf("MTU for %s reduced to %d\n",
432 inet_ntoa(icmpsrc.sin_addr), mtu);
433#endif
434 if (mtu < 296) {
435 /* rt->rt_rmx.rmx_mtu =
436 rt->rt_ifp->if_mtu; */
437 rt->rt_rmx.rmx_locks |= RTV_MTU;
438 } else if (rt->rt_rmx.rmx_mtu > mtu) {
439 rt->rt_rmx.rmx_mtu = mtu;
440 }
441 }
442 if (rt)
443 RTFREE(rt);
444 }
445
446#endif
447 /*
448 * XXX if the packet contains [IPv4 AH TCP], we can't make a
449 * notification to TCP layer.
450 */
451 ctlfunc = ip_protox[icp->icmp_ip.ip_p]->pr_ctlinput;
452 if (ctlfunc)
453 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
454 (void *)&icp->icmp_ip);
455 break;
456
457 badcode:
458 icmpstat.icps_badcode++;
459 break;
460
461 case ICMP_ECHO:
462 if (!icmpbmcastecho
463 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
464 icmpstat.icps_bmcastecho++;
465 break;
466 }
467 icp->icmp_type = ICMP_ECHOREPLY;
468 goto reflect;
469
470 case ICMP_TSTAMP:
471 if (!icmpbmcastecho
472 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
473 icmpstat.icps_bmcasttstamp++;
474 break;
475 }
476 if (icmplen < ICMP_TSLEN) {
477 icmpstat.icps_badlen++;
478 break;
479 }
480 icp->icmp_type = ICMP_TSTAMPREPLY;
481 icp->icmp_rtime = iptime();
482 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
483 goto reflect;
484
485 case ICMP_MASKREQ:
486#define satosin(sa) ((struct sockaddr_in *)(sa))
487 if (icmpmaskrepl == 0)
488 break;
489 /*
490 * We are not able to respond with all ones broadcast
491 * unless we receive it over a point-to-point interface.
492 */
493 if (icmplen < ICMP_MASKLEN)
494 break;
495 switch (ip->ip_dst.s_addr) {
496
497 case INADDR_BROADCAST:
498 case INADDR_ANY:
499 icmpdst.sin_addr = ip->ip_src;
500 break;
501
502 default:
503 icmpdst.sin_addr = ip->ip_dst;
504 }
505 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
506 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
507 if (ia == 0)
508 break;
509 if (ia->ia_ifp == 0)
510 break;
511 icp->icmp_type = ICMP_MASKREPLY;
512 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
513 if (ip->ip_src.s_addr == 0) {
514 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
515 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
516 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
517 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
518 }
519reflect:
520 ip->ip_len += hlen; /* since ip_input deducts this */
521 icmpstat.icps_reflect++;
522 icmpstat.icps_outhist[icp->icmp_type]++;
523 icmp_reflect(m);
524 return;
525
526 case ICMP_REDIRECT:
527 if (code > 3)
528 goto badcode;
529 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
530 IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
531 icmpstat.icps_badlen++;
532 break;
533 }
534 /*
535 * Short circuit routing redirects to force
536 * immediate change in the kernel's routing
537 * tables. The message is also handed to anyone
538 * listening on a raw socket (e.g. the routing
539 * daemon for use in updating its tables).
540 */
541 icmpgw.sin_addr = ip->ip_src;
542 icmpdst.sin_addr = icp->icmp_gwaddr;
543#if ICMPPRINTFS
544 if (icmpprintfs) {
545 char buf[4 * sizeof "123"];
546 strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
547
548 printf("redirect dst %s to %s\n",
549 buf, inet_ntoa(icp->icmp_gwaddr));
550 }
551#endif
552 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
553 rtredirect((struct sockaddr *)&icmpsrc,
554 (struct sockaddr *)&icmpdst,
555 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
556 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
557 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
558#if IPSEC
559 key_sa_routechange((struct sockaddr *)&icmpsrc);
560#endif
561 break;
562
563 /*
564 * No kernel processing for the following;
565 * just fall through to send to raw listener.
566 */
567 case ICMP_ECHOREPLY:
568 case ICMP_ROUTERADVERT:
569 case ICMP_ROUTERSOLICIT:
570 case ICMP_TSTAMPREPLY:
571 case ICMP_IREQREPLY:
572 case ICMP_MASKREPLY:
573 default:
574 break;
575 }
576
577raw:
578 rip_input(m, hlen);
579 return;
580
581freeit:
582 m_freem(m);
583}
584
585/*
586 * Reflect the ip packet back to the source
587 */
588static void
589icmp_reflect(m)
590 struct mbuf *m;
591{
592 register struct ip *ip = mtod(m, struct ip *);
593 register struct in_ifaddr *ia;
594 struct in_addr t;
595 struct mbuf *opts = 0;
596 int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
597
598 if (!in_canforward(ip->ip_src) &&
599 ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
600 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
601 m_freem(m); /* Bad return address */
602 goto done; /* Ip_output() will check for broadcast */
603 }
604 t = ip->ip_dst;
605 ip->ip_dst = ip->ip_src;
606 /*
607 * If the incoming packet was addressed directly to us,
608 * use dst as the src for the reply. Otherwise (broadcast
609 * or anonymous), use the address which corresponds
610 * to the incoming interface.
611 */
612 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
613 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
614 break;
615 if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) &&
616 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
617 break;
618 }
619 icmpdst.sin_addr = t;
620 if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif)
621 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
622 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
623 /*
624 * The following happens if the packet was not addressed to us,
625 * and was received on an interface with no IP address.
626 */
627 if (ia == (struct in_ifaddr *)0)
628 ia = in_ifaddrhead.tqh_first;
629 t = IA_SIN(ia)->sin_addr;
630 ip->ip_src = t;
631 ip->ip_ttl = MAXTTL;
632
633 if (optlen > 0) {
634 register u_char *cp;
635 int opt, cnt;
636 u_int len;
637
638 /*
639 * Retrieve any source routing from the incoming packet;
640 * add on any record-route or timestamp options.
641 */
642 cp = (u_char *) (ip + 1);
643 if ((opts = ip_srcroute()) == 0 &&
644 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
645 opts->m_len = sizeof(struct in_addr);
646 mtod(opts, struct in_addr *)->s_addr = 0;
647 }
648 if (opts) {
649#if ICMPPRINTFS
650 if (icmpprintfs)
651 printf("icmp_reflect optlen %d rt %d => ",
652 optlen, opts->m_len);
653#endif
654 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
655 opt = cp[IPOPT_OPTVAL];
656 if (opt == IPOPT_EOL)
657 break;
658 if (opt == IPOPT_NOP)
659 len = 1;
660 else {
661 if (cnt < IPOPT_OLEN + sizeof(*cp))
662 break;
663 len = cp[IPOPT_OLEN];
664 if (len < IPOPT_OLEN + sizeof(*cp) ||
665 len > cnt)
666 break;
667 }
668 /*
669 * Should check for overflow, but it "can't happen"
670 */
671 if (opt == IPOPT_RR || opt == IPOPT_TS ||
672 opt == IPOPT_SECURITY) {
673 bcopy((caddr_t)cp,
674 mtod(opts, caddr_t) + opts->m_len, len);
675 opts->m_len += len;
676 }
677 }
678 /* Terminate & pad, if necessary */
679 cnt = opts->m_len % 4;
680 if (cnt) {
681 for (; cnt < 4; cnt++) {
682 *(mtod(opts, caddr_t) + opts->m_len) =
683 IPOPT_EOL;
684 opts->m_len++;
685 }
686 }
687#if ICMPPRINTFS
688 if (icmpprintfs)
689 printf("%d\n", opts->m_len);
690#endif
691 }
692 /*
693 * Now strip out original options by copying rest of first
694 * mbuf's data back, and adjust the IP length.
695 */
696 ip->ip_len -= optlen;
697 ip->ip_vhl = IP_VHL_BORING;
698 m->m_len -= optlen;
699 if (m->m_flags & M_PKTHDR)
700 m->m_pkthdr.len -= optlen;
701 optlen += sizeof(struct ip);
702 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
703 (unsigned)(m->m_len - sizeof(struct ip)));
704 }
705 m->m_flags &= ~(M_BCAST|M_MCAST);
706 icmp_send(m, opts);
707done:
708 if (opts)
709 (void)m_free(opts);
710}
711
712/*
713 * Send an icmp packet back to the ip level,
714 * after supplying a checksum.
715 */
716static void
717icmp_send(m, opts)
718 register struct mbuf *m;
719 struct mbuf *opts;
720{
721 register struct ip *ip = mtod(m, struct ip *);
722 register int hlen;
723 register struct icmp *icp;
724 struct route ro;
725
726 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
727 m->m_data += hlen;
728 m->m_len -= hlen;
729 icp = mtod(m, struct icmp *);
730 icp->icmp_cksum = 0;
731 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
732 m->m_data -= hlen;
733 m->m_len += hlen;
734 m->m_pkthdr.rcvif = (struct ifnet *)0;
735 m->m_pkthdr.aux = NULL;
736 m->m_pkthdr.csum_data = 0;
737 m->m_pkthdr.csum_flags = 0;
738#if ICMPPRINTFS
739 if (icmpprintfs) {
740 char buf[4 * sizeof "123"];
741 strcpy(buf, inet_ntoa(ip->ip_dst));
742 printf("icmp_send dst %s src %s\n",
743 buf, inet_ntoa(ip->ip_src));
744 }
745#endif
746 bzero(&ro, sizeof ro);
747
748#ifdef IPSEC
749 ipsec_setsocket(m, NULL);
750#endif /*IPSEC*/
751 (void) ip_output(m, opts, &ro, 0, NULL);
752 if (ro.ro_rt)
753 RTFREE(ro.ro_rt);
754}
755
756n_time
757iptime()
758{
759 struct timeval atv;
760 u_long t;
761
762 microtime(&atv);
763 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
764 return (htonl(t));
765}
766
767#if 1
768/*
769 * Return the next larger or smaller MTU plateau (table from RFC 1191)
770 * given current value MTU. If DIR is less than zero, a larger plateau
771 * is returned; otherwise, a smaller value is returned.
772 */
773/* static */ int
774ip_next_mtu(mtu, dir)
775 int mtu;
776 int dir;
777{
778 static int mtutab[] = {
779 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
780 68, 0
781 };
782 int i;
783
784 for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
785 if (mtu >= mtutab[i])
786 break;
787 }
788
789 if (dir < 0) {
790 if (i == 0) {
791 return 0;
792 } else {
793 return mtutab[i - 1];
794 }
795 } else {
796 if (mtutab[i] == 0) {
797 return 0;
798 } else if(mtu > mtutab[i]) {
799 return mtutab[i];
800 } else {
801 return mtutab[i + 1];
802 }
803 }
804}
805#endif
806
807#if ICMP_BANDLIM
808
809/*
810 * badport_bandlim() - check for ICMP bandwidth limit
811 *
812 * Return 0 if it is ok to send an ICMP error response, -1 if we have
813 * hit our bandwidth limit and it is not ok.
814 *
815 * If icmplim is <= 0, the feature is disabled and 0 is returned.
816 *
817 * For now we separate the TCP and UDP subsystems w/ different 'which'
818 * values. We may eventually remove this separation (and simplify the
819 * code further).
820 *
821 * Note that the printing of the error message is delayed so we can
822 * properly print the icmp error rate that the system was trying to do
823 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing
824 * the 'final' error, but it doesn't make sense to solve the printing
825 * delay with more complex code.
826 */
827
828int
829badport_bandlim(int which)
830{
831 static int lticks[2];
832 static int lpackets[2];
833 int dticks;
834
835 /*
836 * Return ok status if feature disabled or argument out of
837 * ranage.
838 */
839
840 if (icmplim <= 0 || which >= 2 || which < 0)
841 return(0);
842 dticks = ticks - lticks[which];
843
844 /*
845 * reset stats when cumulative dt exceeds one second.
846 */
847
848 if ((unsigned int)dticks > hz) {
849 if (lpackets[which] > icmplim) {
850 printf("icmp-response bandwidth limit %d/%d pps\n",
851 lpackets[which],
852 icmplim
853 );
854 }
855 lticks[which] = ticks;
856 lpackets[which] = 0;
857 }
858
859 /*
860 * bump packet count
861 */
862
863 if (++lpackets[which] > icmplim) {
864 return(-1);
865 }
866 return(0);
867}
868
869#endif
870
871