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