ip_icmp.c revision 1.32 1 /* $NetBSD: ip_icmp.c,v 1.32 1999/01/11 22:35:06 thorpej 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_id);
187 HTONS(oip->ip_off);
188 HTONS(oip->ip_len);
189 icp->icmp_code = code;
190 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
191 nip = &icp->icmp_ip;
192 nip->ip_len = htons((u_int16_t)(nip->ip_len + oiplen));
193
194 /*
195 * Now, copy old ip header (without options)
196 * in front of icmp message.
197 */
198 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
199 panic("icmp len");
200 m->m_data -= sizeof(struct ip);
201 m->m_len += sizeof(struct ip);
202 m->m_pkthdr.len = m->m_len;
203 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
204 nip = mtod(m, struct ip *);
205 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
206 nip->ip_len = m->m_len;
207 nip->ip_hl = sizeof(struct ip) >> 2;
208 nip->ip_p = IPPROTO_ICMP;
209 nip->ip_tos = 0;
210 icmp_reflect(m);
211
212 freeit:
213 m_freem(n);
214 }
215
216 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
217 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
218 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
219 struct sockaddr_in icmpmask = { 8, 0 };
220
221 /*
222 * Process a received ICMP message.
223 */
224 void
225 #if __STDC__
226 icmp_input(struct mbuf *m, ...)
227 #else
228 icmp_input(m, va_alist)
229 struct mbuf *m;
230 va_dcl
231 #endif
232 {
233 register struct icmp *icp;
234 register struct ip *ip = mtod(m, struct ip *);
235 int icmplen = ip->ip_len;
236 register int i;
237 struct in_ifaddr *ia;
238 void *(*ctlfunc) __P((int, struct sockaddr *, void *));
239 int code;
240 extern u_char ip_protox[];
241 int hlen;
242 va_list ap;
243
244 va_start(ap, m);
245 hlen = va_arg(ap, int);
246 va_end(ap);
247
248 /*
249 * Locate icmp structure in mbuf, and check
250 * that not corrupted and of at least minimum length.
251 */
252 #ifdef ICMPPRINTFS
253 if (icmpprintfs)
254 printf("icmp_input from %x to %x, len %d\n",
255 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
256 icmplen);
257 #endif
258 if (icmplen < ICMP_MINLEN) {
259 icmpstat.icps_tooshort++;
260 goto freeit;
261 }
262 i = hlen + min(icmplen, ICMP_ADVLENMIN);
263 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
264 icmpstat.icps_tooshort++;
265 return;
266 }
267 ip = mtod(m, struct ip *);
268 m->m_len -= hlen;
269 m->m_data += hlen;
270 icp = mtod(m, struct icmp *);
271 if (in_cksum(m, icmplen)) {
272 icmpstat.icps_checksum++;
273 goto freeit;
274 }
275 m->m_len += hlen;
276 m->m_data -= hlen;
277
278 #ifdef ICMPPRINTFS
279 /*
280 * Message type specific processing.
281 */
282 if (icmpprintfs)
283 printf("icmp_input, type %d code %d\n", icp->icmp_type,
284 icp->icmp_code);
285 #endif
286 if (icp->icmp_type > ICMP_MAXTYPE)
287 goto raw;
288 icmpstat.icps_inhist[icp->icmp_type]++;
289 code = icp->icmp_code;
290 switch (icp->icmp_type) {
291
292 case ICMP_UNREACH:
293 switch (code) {
294 case ICMP_UNREACH_NET:
295 case ICMP_UNREACH_HOST:
296 case ICMP_UNREACH_PROTOCOL:
297 case ICMP_UNREACH_PORT:
298 case ICMP_UNREACH_SRCFAIL:
299 code += PRC_UNREACH_NET;
300 break;
301
302 case ICMP_UNREACH_NEEDFRAG:
303 code = PRC_MSGSIZE;
304 break;
305
306 case ICMP_UNREACH_NET_UNKNOWN:
307 case ICMP_UNREACH_NET_PROHIB:
308 case ICMP_UNREACH_TOSNET:
309 code = PRC_UNREACH_NET;
310 break;
311
312 case ICMP_UNREACH_HOST_UNKNOWN:
313 case ICMP_UNREACH_ISOLATED:
314 case ICMP_UNREACH_HOST_PROHIB:
315 case ICMP_UNREACH_TOSHOST:
316 code = PRC_UNREACH_HOST;
317 break;
318
319 default:
320 goto badcode;
321 }
322 goto deliver;
323
324 case ICMP_TIMXCEED:
325 if (code > 1)
326 goto badcode;
327 code += PRC_TIMXCEED_INTRANS;
328 goto deliver;
329
330 case ICMP_PARAMPROB:
331 if (code > 1)
332 goto badcode;
333 code = PRC_PARAMPROB;
334 goto deliver;
335
336 case ICMP_SOURCEQUENCH:
337 if (code)
338 goto badcode;
339 code = PRC_QUENCH;
340 goto deliver;
341
342 deliver:
343 /*
344 * Problem with datagram; advise higher level routines.
345 */
346 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
347 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
348 icmpstat.icps_badlen++;
349 goto freeit;
350 }
351 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
352 goto badcode;
353 NTOHS(icp->icmp_ip.ip_len);
354 #ifdef ICMPPRINTFS
355 if (icmpprintfs)
356 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
357 #endif
358 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
359 if (code == PRC_MSGSIZE && ip_mtudisc)
360 icmp_mtudisc(icp);
361 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
362 if (ctlfunc)
363 (*ctlfunc)(code, sintosa(&icmpsrc), &icp->icmp_ip);
364 break;
365
366 badcode:
367 icmpstat.icps_badcode++;
368 break;
369
370 case ICMP_ECHO:
371 icp->icmp_type = ICMP_ECHOREPLY;
372 goto reflect;
373
374 case ICMP_TSTAMP:
375 if (icmplen < ICMP_TSLEN) {
376 icmpstat.icps_badlen++;
377 break;
378 }
379 icp->icmp_type = ICMP_TSTAMPREPLY;
380 icp->icmp_rtime = iptime();
381 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
382 goto reflect;
383
384 case ICMP_MASKREQ:
385 if (icmpmaskrepl == 0)
386 break;
387 /*
388 * We are not able to respond with all ones broadcast
389 * unless we receive it over a point-to-point interface.
390 */
391 if (icmplen < ICMP_MASKLEN) {
392 icmpstat.icps_badlen++;
393 break;
394 }
395 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
396 in_nullhost(ip->ip_dst))
397 icmpdst.sin_addr = ip->ip_src;
398 else
399 icmpdst.sin_addr = ip->ip_dst;
400 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
401 m->m_pkthdr.rcvif));
402 if (ia == 0)
403 break;
404 icp->icmp_type = ICMP_MASKREPLY;
405 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
406 if (in_nullhost(ip->ip_src)) {
407 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
408 ip->ip_src = ia->ia_broadaddr.sin_addr;
409 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
410 ip->ip_src = ia->ia_dstaddr.sin_addr;
411 }
412 reflect:
413 ip->ip_len += hlen; /* since ip_input deducts this */
414 icmpstat.icps_reflect++;
415 icmpstat.icps_outhist[icp->icmp_type]++;
416 icmp_reflect(m);
417 return;
418
419 case ICMP_REDIRECT:
420 if (code > 3)
421 goto badcode;
422 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
423 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
424 icmpstat.icps_badlen++;
425 break;
426 }
427 /*
428 * Short circuit routing redirects to force
429 * immediate change in the kernel's routing
430 * tables. The message is also handed to anyone
431 * listening on a raw socket (e.g. the routing
432 * daemon for use in updating its tables).
433 */
434 icmpgw.sin_addr = ip->ip_src;
435 icmpdst.sin_addr = icp->icmp_gwaddr;
436 #ifdef ICMPPRINTFS
437 if (icmpprintfs)
438 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
439 icp->icmp_gwaddr);
440 #endif
441 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
442 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
443 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
444 sintosa(&icmpgw), (struct rtentry **)0);
445 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
446 break;
447
448 /*
449 * No kernel processing for the following;
450 * just fall through to send to raw listener.
451 */
452 case ICMP_ECHOREPLY:
453 case ICMP_ROUTERADVERT:
454 case ICMP_ROUTERSOLICIT:
455 case ICMP_TSTAMPREPLY:
456 case ICMP_IREQREPLY:
457 case ICMP_MASKREPLY:
458 default:
459 break;
460 }
461
462 raw:
463 rip_input(m);
464 return;
465
466 freeit:
467 m_freem(m);
468 }
469
470 /*
471 * Reflect the ip packet back to the source
472 */
473 void
474 icmp_reflect(m)
475 struct mbuf *m;
476 {
477 register struct ip *ip = mtod(m, struct ip *);
478 register struct in_ifaddr *ia;
479 register struct ifaddr *ifa;
480 struct in_addr t;
481 struct mbuf *opts = 0;
482 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
483
484 if (!in_canforward(ip->ip_src) &&
485 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
486 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
487 m_freem(m); /* Bad return address */
488 goto done; /* ip_output() will check for broadcast */
489 }
490 t = ip->ip_dst;
491 ip->ip_dst = ip->ip_src;
492 /*
493 * If the incoming packet was addressed directly to us,
494 * use dst as the src for the reply. Otherwise (broadcast
495 * or anonymous), use the address which corresponds
496 * to the incoming interface.
497 */
498 INADDR_TO_IA(t, ia);
499 if (ia == NULL && (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
500 for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
501 ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
502 if (ifa->ifa_addr->sa_family != AF_INET)
503 continue;
504 ia = ifatoia(ifa);
505 if (in_hosteq(t, ia->ia_broadaddr.sin_addr))
506 break;
507 }
508 }
509
510 icmpdst.sin_addr = t;
511 if (ia == (struct in_ifaddr *)0)
512 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
513 m->m_pkthdr.rcvif));
514 /*
515 * The following happens if the packet was not addressed to us,
516 * and was received on an interface with no IP address:
517 * We find the first AF_INET address on the first non-loopback
518 * interface.
519 */
520 if (ia == (struct in_ifaddr *)0)
521 for (ia = in_ifaddr.tqh_first; ia != NULL;
522 ia = ia->ia_list.tqe_next) {
523 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
524 continue;
525 break;
526 }
527 t = ia->ia_addr.sin_addr;
528 ip->ip_src = t;
529 ip->ip_ttl = MAXTTL;
530
531 if (optlen > 0) {
532 register u_char *cp;
533 int opt, cnt;
534 u_int len;
535
536 /*
537 * Retrieve any source routing from the incoming packet;
538 * add on any record-route or timestamp options.
539 */
540 cp = (u_char *) (ip + 1);
541 if ((opts = ip_srcroute()) == 0 &&
542 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
543 opts->m_len = sizeof(struct in_addr);
544 *mtod(opts, struct in_addr *) = zeroin_addr;
545 }
546 if (opts) {
547 #ifdef ICMPPRINTFS
548 if (icmpprintfs)
549 printf("icmp_reflect optlen %d rt %d => ",
550 optlen, opts->m_len);
551 #endif
552 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
553 opt = cp[IPOPT_OPTVAL];
554 if (opt == IPOPT_EOL)
555 break;
556 if (opt == IPOPT_NOP)
557 len = 1;
558 else {
559 len = cp[IPOPT_OLEN];
560 if (len <= 0 || len > cnt)
561 break;
562 }
563 /*
564 * Should check for overflow, but it "can't happen"
565 */
566 if (opt == IPOPT_RR || opt == IPOPT_TS ||
567 opt == IPOPT_SECURITY) {
568 bcopy((caddr_t)cp,
569 mtod(opts, caddr_t) + opts->m_len, len);
570 opts->m_len += len;
571 }
572 }
573 /* Terminate & pad, if necessary */
574 if ((cnt = opts->m_len % 4) != 0) {
575 for (; cnt < 4; cnt++) {
576 *(mtod(opts, caddr_t) + opts->m_len) =
577 IPOPT_EOL;
578 opts->m_len++;
579 }
580 }
581 #ifdef ICMPPRINTFS
582 if (icmpprintfs)
583 printf("%d\n", opts->m_len);
584 #endif
585 }
586 /*
587 * Now strip out original options by copying rest of first
588 * mbuf's data back, and adjust the IP length.
589 */
590 ip->ip_len -= optlen;
591 ip->ip_hl = sizeof(struct ip) >> 2;
592 m->m_len -= optlen;
593 if (m->m_flags & M_PKTHDR)
594 m->m_pkthdr.len -= optlen;
595 optlen += sizeof(struct ip);
596 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
597 (unsigned)(m->m_len - sizeof(struct ip)));
598 }
599 m->m_flags &= ~(M_BCAST|M_MCAST);
600 icmp_send(m, opts);
601 done:
602 if (opts)
603 (void)m_free(opts);
604 }
605
606 /*
607 * Send an icmp packet back to the ip level,
608 * after supplying a checksum.
609 */
610 void
611 icmp_send(m, opts)
612 register struct mbuf *m;
613 struct mbuf *opts;
614 {
615 register struct ip *ip = mtod(m, struct ip *);
616 register int hlen;
617 register struct icmp *icp;
618
619 hlen = ip->ip_hl << 2;
620 m->m_data += hlen;
621 m->m_len -= hlen;
622 icp = mtod(m, struct icmp *);
623 icp->icmp_cksum = 0;
624 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
625 m->m_data -= hlen;
626 m->m_len += hlen;
627 #ifdef ICMPPRINTFS
628 if (icmpprintfs)
629 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
630 #endif
631 (void) ip_output(m, opts, NULL, 0, NULL);
632 }
633
634 n_time
635 iptime()
636 {
637 struct timeval atv;
638 u_long t;
639
640 microtime(&atv);
641 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
642 return (htonl(t));
643 }
644
645 int
646 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
647 int *name;
648 u_int namelen;
649 void *oldp;
650 size_t *oldlenp;
651 void *newp;
652 size_t newlen;
653 {
654
655 /* All sysctl names at this level are terminal. */
656 if (namelen != 1)
657 return (ENOTDIR);
658
659 switch (name[0]) {
660 case ICMPCTL_MASKREPL:
661 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
662 default:
663 return (ENOPROTOOPT);
664 }
665 /* NOTREACHED */
666 }
667
668 static void
669 icmp_mtudisc(icp)
670 struct icmp *icp;
671 {
672 struct rtentry *rt;
673 struct sockaddr *dst = sintosa(&icmpsrc);
674 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */
675 int error;
676
677 /* Table of common MTUs: */
678
679 static u_long mtu_table[] = {65535, 65280, 32000, 17914, 9180, 8166,
680 4352, 2002, 1492, 1006, 508, 296, 68, 0};
681
682 rt = rtalloc1(dst, 1);
683 if (rt == 0)
684 return;
685
686 /* If we didn't get a host route, allocate one */
687
688 if ((rt->rt_flags & RTF_HOST) == 0) {
689 struct rtentry *nrt;
690
691 error = rtrequest((int) RTM_ADD, dst,
692 (struct sockaddr *) rt->rt_gateway,
693 (struct sockaddr *) 0,
694 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
695 if (error) {
696 rtfree(rt);
697 rtfree(nrt);
698 return;
699 }
700 nrt->rt_rmx = rt->rt_rmx;
701 rtfree(rt);
702 rt = nrt;
703 }
704 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
705 if (error) {
706 rtfree(rt);
707 return;
708 }
709
710 if (mtu == 0) {
711 int i = 0;
712
713 mtu = icp->icmp_ip.ip_len; /* NTOHS happened in deliver: */
714 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
715 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
716 mtu -= (icp->icmp_ip.ip_hl << 2);
717
718 /* If we still can't guess a value, try the route */
719
720 if (mtu == 0) {
721 mtu = rt->rt_rmx.rmx_mtu;
722
723 /* If no route mtu, default to the interface mtu */
724
725 if (mtu == 0)
726 mtu = rt->rt_ifp->if_mtu;
727 }
728
729 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
730 if (mtu > mtu_table[i]) {
731 mtu = mtu_table[i];
732 break;
733 }
734 }
735
736 /*
737 * XXX: RTV_MTU is overloaded, since the admin can set it
738 * to turn off PMTU for a route, and the kernel can
739 * set it to indicate a serious problem with PMTU
740 * on a route. We should be using a separate flag
741 * for the kernel to indicate this.
742 */
743
744 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
745 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
746 rt->rt_rmx.rmx_locks |= RTV_MTU;
747 else if (rt->rt_rmx.rmx_mtu > mtu ||
748 rt->rt_rmx.rmx_mtu == 0)
749 rt->rt_rmx.rmx_mtu = mtu;
750 }
751
752 if (rt)
753 rtfree(rt);
754 }
755
756
757 static void
758 icmp_mtudisc_timeout(rt, r)
759 struct rtentry *rt;
760 struct rttimer *r;
761 {
762 if (rt == NULL)
763 panic("icmp_mtudisc_timeout: bad route to timeout");
764 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
765 (RTF_DYNAMIC | RTF_HOST)) {
766 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
767 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
768 } else {
769 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
770 rt->rt_rmx.rmx_mtu = 0;
771 }
772 }
773 }
774