ip_icmp.c revision 1.36 1 /* $NetBSD: ip_icmp.c,v 1.36 1999/03/30 19:02:56 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
73 */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/time.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84
85 #include <vm/vm.h>
86 #include <sys/sysctl.h>
87
88 #include <net/if.h>
89 #include <net/route.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip_icmp.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/icmp_var.h>
98
99 #include <machine/stdarg.h>
100
101 /*
102 * ICMP routines: error generation, receive packet processing, and
103 * routines to turnaround packets back to the originator, and
104 * host table maintenance routines.
105 */
106
107 int icmpmaskrepl = 0;
108 #ifdef ICMPPRINTFS
109 int icmpprintfs = 0;
110 #endif
111
112 extern struct protosw inetsw[];
113
114 static void icmp_mtudisc __P((struct icmp *));
115 static void icmp_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
116
117 /*
118 * Generate an error packet of type error
119 * in response to bad packet ip.
120 */
121 void
122 icmp_error(n, type, code, dest, destifp)
123 struct mbuf *n;
124 int type, code;
125 n_long dest;
126 struct ifnet *destifp;
127 {
128 register struct ip *oip = mtod(n, struct ip *), *nip;
129 register unsigned oiplen = oip->ip_hl << 2;
130 register struct icmp *icp;
131 register struct mbuf *m;
132 unsigned icmplen;
133
134 #ifdef ICMPPRINTFS
135 if (icmpprintfs)
136 printf("icmp_error(%x, %d, %d)\n", oip, type, code);
137 #endif
138 if (type != ICMP_REDIRECT)
139 icmpstat.icps_error++;
140 /*
141 * Don't send error if not the first fragment of message.
142 * Don't error if the old packet protocol was ICMP
143 * error message, only known informational types.
144 */
145 if (oip->ip_off &~ (IP_MF|IP_DF))
146 goto freeit;
147 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
148 n->m_len >= oiplen + ICMP_MINLEN &&
149 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
150 icmpstat.icps_oldicmp++;
151 goto freeit;
152 }
153 /* Don't send error in response to a multicast or broadcast packet */
154 if (n->m_flags & (M_BCAST|M_MCAST))
155 goto freeit;
156 /*
157 * First, formulate icmp message
158 */
159 m = m_gethdr(M_DONTWAIT, MT_HEADER);
160 if (m == NULL)
161 goto freeit;
162 icmplen = oiplen + min(8, oip->ip_len - oiplen);
163 m->m_len = icmplen + ICMP_MINLEN;
164 MH_ALIGN(m, m->m_len);
165 icp = mtod(m, struct icmp *);
166 if ((u_int)type > ICMP_MAXTYPE)
167 panic("icmp_error");
168 icmpstat.icps_outhist[type]++;
169 icp->icmp_type = type;
170 if (type == ICMP_REDIRECT)
171 icp->icmp_gwaddr.s_addr = dest;
172 else {
173 icp->icmp_void = 0;
174 /*
175 * The following assignments assume an overlay with the
176 * zeroed icmp_void field.
177 */
178 if (type == ICMP_PARAMPROB) {
179 icp->icmp_pptr = code;
180 code = 0;
181 } else if (type == ICMP_UNREACH &&
182 code == ICMP_UNREACH_NEEDFRAG && destifp)
183 icp->icmp_nextmtu = htons(destifp->if_mtu);
184 }
185
186 HTONS(oip->ip_off);
187 HTONS(oip->ip_len);
188 icp->icmp_code = code;
189 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
190 nip = &icp->icmp_ip;
191
192 /*
193 * Now, copy old ip header (without options)
194 * in front of icmp message.
195 */
196 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
197 panic("icmp len");
198 m->m_data -= sizeof(struct ip);
199 m->m_len += sizeof(struct ip);
200 m->m_pkthdr.len = m->m_len;
201 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
202 nip = mtod(m, struct ip *);
203 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
204 nip->ip_len = m->m_len;
205 nip->ip_hl = sizeof(struct ip) >> 2;
206 nip->ip_p = IPPROTO_ICMP;
207 nip->ip_tos = 0;
208 icmp_reflect(m);
209
210 freeit:
211 m_freem(n);
212 }
213
214 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
215 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
216 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
217 struct sockaddr_in icmpmask = { 8, 0 };
218
219 /*
220 * Process a received ICMP message.
221 */
222 void
223 #if __STDC__
224 icmp_input(struct mbuf *m, ...)
225 #else
226 icmp_input(m, va_alist)
227 struct mbuf *m;
228 va_dcl
229 #endif
230 {
231 register struct icmp *icp;
232 register struct ip *ip = mtod(m, struct ip *);
233 int icmplen;
234 register int i;
235 struct in_ifaddr *ia;
236 void *(*ctlfunc) __P((int, struct sockaddr *, void *));
237 int code;
238 extern u_char ip_protox[];
239 int hlen;
240 va_list ap;
241
242 va_start(ap, m);
243 hlen = va_arg(ap, int);
244 va_end(ap);
245
246 /*
247 * Locate icmp structure in mbuf, and check
248 * that not corrupted and of at least minimum length.
249 */
250 icmplen = ip->ip_len - hlen;
251 #ifdef ICMPPRINTFS
252 if (icmpprintfs)
253 printf("icmp_input from %x to %x, len %d\n",
254 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
255 icmplen);
256 #endif
257 if (icmplen < ICMP_MINLEN) {
258 icmpstat.icps_tooshort++;
259 goto freeit;
260 }
261 i = hlen + min(icmplen, ICMP_ADVLENMIN);
262 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
263 icmpstat.icps_tooshort++;
264 return;
265 }
266 ip = mtod(m, struct ip *);
267 m->m_len -= hlen;
268 m->m_data += hlen;
269 icp = mtod(m, struct icmp *);
270 if (in_cksum(m, icmplen)) {
271 icmpstat.icps_checksum++;
272 goto freeit;
273 }
274 m->m_len += hlen;
275 m->m_data -= hlen;
276
277 #ifdef ICMPPRINTFS
278 /*
279 * Message type specific processing.
280 */
281 if (icmpprintfs)
282 printf("icmp_input, type %d code %d\n", icp->icmp_type,
283 icp->icmp_code);
284 #endif
285 if (icp->icmp_type > ICMP_MAXTYPE)
286 goto raw;
287 icmpstat.icps_inhist[icp->icmp_type]++;
288 code = icp->icmp_code;
289 switch (icp->icmp_type) {
290
291 case ICMP_UNREACH:
292 switch (code) {
293 case ICMP_UNREACH_NET:
294 case ICMP_UNREACH_HOST:
295 case ICMP_UNREACH_PROTOCOL:
296 case ICMP_UNREACH_PORT:
297 case ICMP_UNREACH_SRCFAIL:
298 code += PRC_UNREACH_NET;
299 break;
300
301 case ICMP_UNREACH_NEEDFRAG:
302 code = PRC_MSGSIZE;
303 break;
304
305 case ICMP_UNREACH_NET_UNKNOWN:
306 case ICMP_UNREACH_NET_PROHIB:
307 case ICMP_UNREACH_TOSNET:
308 code = PRC_UNREACH_NET;
309 break;
310
311 case ICMP_UNREACH_HOST_UNKNOWN:
312 case ICMP_UNREACH_ISOLATED:
313 case ICMP_UNREACH_HOST_PROHIB:
314 case ICMP_UNREACH_TOSHOST:
315 code = PRC_UNREACH_HOST;
316 break;
317
318 default:
319 goto badcode;
320 }
321 goto deliver;
322
323 case ICMP_TIMXCEED:
324 if (code > 1)
325 goto badcode;
326 code += PRC_TIMXCEED_INTRANS;
327 goto deliver;
328
329 case ICMP_PARAMPROB:
330 if (code > 1)
331 goto badcode;
332 code = PRC_PARAMPROB;
333 goto deliver;
334
335 case ICMP_SOURCEQUENCH:
336 if (code)
337 goto badcode;
338 code = PRC_QUENCH;
339 goto deliver;
340
341 deliver:
342 /*
343 * Problem with datagram; advise higher level routines.
344 */
345 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
346 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
347 icmpstat.icps_badlen++;
348 goto freeit;
349 }
350 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
351 goto badcode;
352 NTOHS(icp->icmp_ip.ip_len);
353 #ifdef ICMPPRINTFS
354 if (icmpprintfs)
355 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
356 #endif
357 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
358 if (code == PRC_MSGSIZE && ip_mtudisc)
359 icmp_mtudisc(icp);
360 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
361 if (ctlfunc)
362 (*ctlfunc)(code, sintosa(&icmpsrc), &icp->icmp_ip);
363 break;
364
365 badcode:
366 icmpstat.icps_badcode++;
367 break;
368
369 case ICMP_ECHO:
370 icp->icmp_type = ICMP_ECHOREPLY;
371 goto reflect;
372
373 case ICMP_TSTAMP:
374 if (icmplen < ICMP_TSLEN) {
375 icmpstat.icps_badlen++;
376 break;
377 }
378 icp->icmp_type = ICMP_TSTAMPREPLY;
379 icp->icmp_rtime = iptime();
380 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
381 goto reflect;
382
383 case ICMP_MASKREQ:
384 if (icmpmaskrepl == 0)
385 break;
386 /*
387 * We are not able to respond with all ones broadcast
388 * unless we receive it over a point-to-point interface.
389 */
390 if (icmplen < ICMP_MASKLEN) {
391 icmpstat.icps_badlen++;
392 break;
393 }
394 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
395 in_nullhost(ip->ip_dst))
396 icmpdst.sin_addr = ip->ip_src;
397 else
398 icmpdst.sin_addr = ip->ip_dst;
399 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
400 m->m_pkthdr.rcvif));
401 if (ia == 0)
402 break;
403 icp->icmp_type = ICMP_MASKREPLY;
404 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
405 if (in_nullhost(ip->ip_src)) {
406 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
407 ip->ip_src = ia->ia_broadaddr.sin_addr;
408 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
409 ip->ip_src = ia->ia_dstaddr.sin_addr;
410 }
411 reflect:
412 icmpstat.icps_reflect++;
413 icmpstat.icps_outhist[icp->icmp_type]++;
414 icmp_reflect(m);
415 return;
416
417 case ICMP_REDIRECT:
418 if (code > 3)
419 goto badcode;
420 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
421 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
422 icmpstat.icps_badlen++;
423 break;
424 }
425 /*
426 * Short circuit routing redirects to force
427 * immediate change in the kernel's routing
428 * tables. The message is also handed to anyone
429 * listening on a raw socket (e.g. the routing
430 * daemon for use in updating its tables).
431 */
432 icmpgw.sin_addr = ip->ip_src;
433 icmpdst.sin_addr = icp->icmp_gwaddr;
434 #ifdef ICMPPRINTFS
435 if (icmpprintfs)
436 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
437 icp->icmp_gwaddr);
438 #endif
439 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
440 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
441 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
442 sintosa(&icmpgw), (struct rtentry **)0);
443 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
444 break;
445
446 /*
447 * No kernel processing for the following;
448 * just fall through to send to raw listener.
449 */
450 case ICMP_ECHOREPLY:
451 case ICMP_ROUTERADVERT:
452 case ICMP_ROUTERSOLICIT:
453 case ICMP_TSTAMPREPLY:
454 case ICMP_IREQREPLY:
455 case ICMP_MASKREPLY:
456 default:
457 break;
458 }
459
460 raw:
461 rip_input(m);
462 return;
463
464 freeit:
465 m_freem(m);
466 }
467
468 /*
469 * Reflect the ip packet back to the source
470 */
471 void
472 icmp_reflect(m)
473 struct mbuf *m;
474 {
475 register struct ip *ip = mtod(m, struct ip *);
476 register struct in_ifaddr *ia;
477 register struct ifaddr *ifa;
478 struct in_addr t;
479 struct mbuf *opts = 0;
480 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
481
482 if (!in_canforward(ip->ip_src) &&
483 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
484 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
485 m_freem(m); /* Bad return address */
486 goto done; /* ip_output() will check for broadcast */
487 }
488 t = ip->ip_dst;
489 ip->ip_dst = ip->ip_src;
490 /*
491 * If the incoming packet was addressed directly to us,
492 * use dst as the src for the reply. Otherwise (broadcast
493 * or anonymous), use the address which corresponds
494 * to the incoming interface.
495 */
496 INADDR_TO_IA(t, ia);
497 if (ia == NULL && (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
498 for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
499 ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
500 if (ifa->ifa_addr->sa_family != AF_INET)
501 continue;
502 ia = ifatoia(ifa);
503 if (in_hosteq(t, ia->ia_broadaddr.sin_addr))
504 break;
505 }
506 }
507
508 icmpdst.sin_addr = t;
509 if (ia == (struct in_ifaddr *)0)
510 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
511 m->m_pkthdr.rcvif));
512 /*
513 * The following happens if the packet was not addressed to us,
514 * and was received on an interface with no IP address:
515 * We find the first AF_INET address on the first non-loopback
516 * interface.
517 */
518 if (ia == (struct in_ifaddr *)0)
519 for (ia = in_ifaddr.tqh_first; ia != NULL;
520 ia = ia->ia_list.tqe_next) {
521 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
522 continue;
523 break;
524 }
525 /*
526 * If we still didn't find an address, punt. We could have an
527 * interface up (and receiving packets) with no address.
528 */
529 if (ia == (struct in_ifaddr *)0) {
530 m_freem(m);
531 goto done;
532 }
533
534 t = ia->ia_addr.sin_addr;
535 ip->ip_src = t;
536 ip->ip_ttl = MAXTTL;
537
538 if (optlen > 0) {
539 register u_char *cp;
540 int opt, cnt;
541 u_int len;
542
543 /*
544 * Retrieve any source routing from the incoming packet;
545 * add on any record-route or timestamp options.
546 */
547 cp = (u_char *) (ip + 1);
548 if ((opts = ip_srcroute()) == 0 &&
549 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
550 opts->m_len = sizeof(struct in_addr);
551 *mtod(opts, struct in_addr *) = zeroin_addr;
552 }
553 if (opts) {
554 #ifdef ICMPPRINTFS
555 if (icmpprintfs)
556 printf("icmp_reflect optlen %d rt %d => ",
557 optlen, opts->m_len);
558 #endif
559 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
560 opt = cp[IPOPT_OPTVAL];
561 if (opt == IPOPT_EOL)
562 break;
563 if (opt == IPOPT_NOP)
564 len = 1;
565 else {
566 len = cp[IPOPT_OLEN];
567 if (len <= 0 || len > cnt)
568 break;
569 }
570 /*
571 * Should check for overflow, but it "can't happen"
572 */
573 if (opt == IPOPT_RR || opt == IPOPT_TS ||
574 opt == IPOPT_SECURITY) {
575 bcopy((caddr_t)cp,
576 mtod(opts, caddr_t) + opts->m_len, len);
577 opts->m_len += len;
578 }
579 }
580 /* Terminate & pad, if necessary */
581 if ((cnt = opts->m_len % 4) != 0) {
582 for (; cnt < 4; cnt++) {
583 *(mtod(opts, caddr_t) + opts->m_len) =
584 IPOPT_EOL;
585 opts->m_len++;
586 }
587 }
588 #ifdef ICMPPRINTFS
589 if (icmpprintfs)
590 printf("%d\n", opts->m_len);
591 #endif
592 }
593 /*
594 * Now strip out original options by copying rest of first
595 * mbuf's data back, and adjust the IP length.
596 */
597 ip->ip_len -= optlen;
598 ip->ip_hl = sizeof(struct ip) >> 2;
599 m->m_len -= optlen;
600 if (m->m_flags & M_PKTHDR)
601 m->m_pkthdr.len -= optlen;
602 optlen += sizeof(struct ip);
603 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
604 (unsigned)(m->m_len - sizeof(struct ip)));
605 }
606 m->m_flags &= ~(M_BCAST|M_MCAST);
607 icmp_send(m, opts);
608 done:
609 if (opts)
610 (void)m_free(opts);
611 }
612
613 /*
614 * Send an icmp packet back to the ip level,
615 * after supplying a checksum.
616 */
617 void
618 icmp_send(m, opts)
619 register struct mbuf *m;
620 struct mbuf *opts;
621 {
622 register struct ip *ip = mtod(m, struct ip *);
623 register int hlen;
624 register struct icmp *icp;
625
626 hlen = ip->ip_hl << 2;
627 m->m_data += hlen;
628 m->m_len -= hlen;
629 icp = mtod(m, struct icmp *);
630 icp->icmp_cksum = 0;
631 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
632 m->m_data -= hlen;
633 m->m_len += hlen;
634 #ifdef ICMPPRINTFS
635 if (icmpprintfs)
636 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
637 #endif
638 (void) ip_output(m, opts, NULL, 0, NULL);
639 }
640
641 n_time
642 iptime()
643 {
644 struct timeval atv;
645 u_long t;
646
647 microtime(&atv);
648 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
649 return (htonl(t));
650 }
651
652 int
653 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
654 int *name;
655 u_int namelen;
656 void *oldp;
657 size_t *oldlenp;
658 void *newp;
659 size_t newlen;
660 {
661
662 /* All sysctl names at this level are terminal. */
663 if (namelen != 1)
664 return (ENOTDIR);
665
666 switch (name[0]) {
667 case ICMPCTL_MASKREPL:
668 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
669 default:
670 return (ENOPROTOOPT);
671 }
672 /* NOTREACHED */
673 }
674
675 static void
676 icmp_mtudisc(icp)
677 struct icmp *icp;
678 {
679 struct rtentry *rt;
680 struct sockaddr *dst = sintosa(&icmpsrc);
681 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */
682 int error;
683
684 /* Table of common MTUs: */
685
686 static u_long mtu_table[] = {65535, 65280, 32000, 17914, 9180, 8166,
687 4352, 2002, 1492, 1006, 508, 296, 68, 0};
688
689 rt = rtalloc1(dst, 1);
690 if (rt == 0)
691 return;
692
693 /* If we didn't get a host route, allocate one */
694
695 if ((rt->rt_flags & RTF_HOST) == 0) {
696 struct rtentry *nrt;
697
698 error = rtrequest((int) RTM_ADD, dst,
699 (struct sockaddr *) rt->rt_gateway,
700 (struct sockaddr *) 0,
701 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
702 if (error) {
703 rtfree(rt);
704 rtfree(nrt);
705 return;
706 }
707 nrt->rt_rmx = rt->rt_rmx;
708 rtfree(rt);
709 rt = nrt;
710 }
711 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
712 if (error) {
713 rtfree(rt);
714 return;
715 }
716
717 if (mtu == 0) {
718 int i = 0;
719
720 mtu = icp->icmp_ip.ip_len; /* NTOHS happened in deliver: */
721 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
722 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
723 mtu -= (icp->icmp_ip.ip_hl << 2);
724
725 /* If we still can't guess a value, try the route */
726
727 if (mtu == 0) {
728 mtu = rt->rt_rmx.rmx_mtu;
729
730 /* If no route mtu, default to the interface mtu */
731
732 if (mtu == 0)
733 mtu = rt->rt_ifp->if_mtu;
734 }
735
736 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
737 if (mtu > mtu_table[i]) {
738 mtu = mtu_table[i];
739 break;
740 }
741 }
742
743 /*
744 * XXX: RTV_MTU is overloaded, since the admin can set it
745 * to turn off PMTU for a route, and the kernel can
746 * set it to indicate a serious problem with PMTU
747 * on a route. We should be using a separate flag
748 * for the kernel to indicate this.
749 */
750
751 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
752 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
753 rt->rt_rmx.rmx_locks |= RTV_MTU;
754 else if (rt->rt_rmx.rmx_mtu > mtu ||
755 rt->rt_rmx.rmx_mtu == 0)
756 rt->rt_rmx.rmx_mtu = mtu;
757 }
758
759 if (rt)
760 rtfree(rt);
761 }
762
763
764 static void
765 icmp_mtudisc_timeout(rt, r)
766 struct rtentry *rt;
767 struct rttimer *r;
768 {
769 if (rt == NULL)
770 panic("icmp_mtudisc_timeout: bad route to timeout");
771 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
772 (RTF_DYNAMIC | RTF_HOST)) {
773 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
774 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
775 } else {
776 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
777 rt->rt_rmx.rmx_mtu = 0;
778 }
779 }
780 }
781