ip_icmp.c revision 1.19 1 /* $NetBSD: ip_icmp.c,v 1.19 1996/02/13 23:42:22 christos 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 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47
48 #include <vm/vm.h>
49 #include <sys/sysctl.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/icmp_var.h>
61
62 #include <machine/stdarg.h>
63
64 /*
65 * ICMP routines: error generation, receive packet processing, and
66 * routines to turnaround packets back to the originator, and
67 * host table maintenance routines.
68 */
69
70 int icmpmaskrepl = 0;
71 #ifdef ICMPPRINTFS
72 int icmpprintfs = 0;
73 #endif
74
75 extern struct protosw inetsw[];
76
77 /*
78 * Generate an error packet of type error
79 * in response to bad packet ip.
80 */
81 void
82 icmp_error(n, type, code, dest, destifp)
83 struct mbuf *n;
84 int type, code;
85 n_long dest;
86 struct ifnet *destifp;
87 {
88 register struct ip *oip = mtod(n, struct ip *), *nip;
89 register unsigned oiplen = oip->ip_hl << 2;
90 register struct icmp *icp;
91 register struct mbuf *m;
92 unsigned icmplen;
93
94 #ifdef ICMPPRINTFS
95 if (icmpprintfs)
96 printf("icmp_error(%x, %d, %d)\n", oip, type, code);
97 #endif
98 if (type != ICMP_REDIRECT)
99 icmpstat.icps_error++;
100 /*
101 * Don't send error if not the first fragment of message.
102 * Don't error if the old packet protocol was ICMP
103 * error message, only known informational types.
104 */
105 if (oip->ip_off &~ (IP_MF|IP_DF))
106 goto freeit;
107 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
108 n->m_len >= oiplen + ICMP_MINLEN &&
109 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
110 icmpstat.icps_oldicmp++;
111 goto freeit;
112 }
113 /* Don't send error in response to a multicast or broadcast packet */
114 if (n->m_flags & (M_BCAST|M_MCAST))
115 goto freeit;
116 /*
117 * First, formulate icmp message
118 */
119 m = m_gethdr(M_DONTWAIT, MT_HEADER);
120 if (m == NULL)
121 goto freeit;
122 icmplen = oiplen + min(8, oip->ip_len);
123 m->m_len = icmplen + ICMP_MINLEN;
124 MH_ALIGN(m, m->m_len);
125 icp = mtod(m, struct icmp *);
126 if ((u_int)type > ICMP_MAXTYPE)
127 panic("icmp_error");
128 icmpstat.icps_outhist[type]++;
129 icp->icmp_type = type;
130 if (type == ICMP_REDIRECT)
131 icp->icmp_gwaddr.s_addr = dest;
132 else {
133 icp->icmp_void = 0;
134 /*
135 * The following assignments assume an overlay with the
136 * zeroed icmp_void field.
137 */
138 if (type == ICMP_PARAMPROB) {
139 icp->icmp_pptr = code;
140 code = 0;
141 } else if (type == ICMP_UNREACH &&
142 code == ICMP_UNREACH_NEEDFRAG && destifp)
143 icp->icmp_nextmtu = htons(destifp->if_mtu);
144 }
145
146 icp->icmp_code = code;
147 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
148 nip = &icp->icmp_ip;
149 nip->ip_len = htons((u_int16_t)(nip->ip_len + oiplen));
150
151 /*
152 * Now, copy old ip header (without options)
153 * in front of icmp message.
154 */
155 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
156 panic("icmp len");
157 m->m_data -= sizeof(struct ip);
158 m->m_len += sizeof(struct ip);
159 m->m_pkthdr.len = m->m_len;
160 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
161 nip = mtod(m, struct ip *);
162 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
163 nip->ip_len = m->m_len;
164 nip->ip_hl = sizeof(struct ip) >> 2;
165 nip->ip_p = IPPROTO_ICMP;
166 nip->ip_tos = 0;
167 icmp_reflect(m);
168
169 freeit:
170 m_freem(n);
171 }
172
173 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
174 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
175 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
176 struct sockaddr_in icmpmask = { 8, 0 };
177
178 /*
179 * Process a received ICMP message.
180 */
181 void
182 #if __STDC__
183 icmp_input(struct mbuf *m, ...)
184 #else
185 icmp_input(m, va_alist)
186 struct mbuf *m;
187 va_dcl
188 #endif
189 {
190 register struct icmp *icp;
191 register struct ip *ip = mtod(m, struct ip *);
192 int icmplen = ip->ip_len;
193 register int i;
194 struct in_ifaddr *ia;
195 void *(*ctlfunc) __P((int, struct sockaddr *, void *));
196 int code;
197 extern u_char ip_protox[];
198 int hlen;
199 va_list ap;
200
201 va_start(ap, m);
202 hlen = va_arg(ap, int);
203 va_end(ap);
204
205 /*
206 * Locate icmp structure in mbuf, and check
207 * that not corrupted and of at least minimum length.
208 */
209 #ifdef ICMPPRINTFS
210 if (icmpprintfs)
211 printf("icmp_input from %x to %x, len %d\n",
212 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
213 icmplen);
214 #endif
215 if (icmplen < ICMP_MINLEN) {
216 icmpstat.icps_tooshort++;
217 goto freeit;
218 }
219 i = hlen + min(icmplen, ICMP_ADVLENMIN);
220 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
221 icmpstat.icps_tooshort++;
222 return;
223 }
224 ip = mtod(m, struct ip *);
225 m->m_len -= hlen;
226 m->m_data += hlen;
227 icp = mtod(m, struct icmp *);
228 if (in_cksum(m, icmplen)) {
229 icmpstat.icps_checksum++;
230 goto freeit;
231 }
232 m->m_len += hlen;
233 m->m_data -= hlen;
234
235 #ifdef ICMPPRINTFS
236 /*
237 * Message type specific processing.
238 */
239 if (icmpprintfs)
240 printf("icmp_input, type %d code %d\n", icp->icmp_type,
241 icp->icmp_code);
242 #endif
243 if (icp->icmp_type > ICMP_MAXTYPE)
244 goto raw;
245 icmpstat.icps_inhist[icp->icmp_type]++;
246 code = icp->icmp_code;
247 switch (icp->icmp_type) {
248
249 case ICMP_UNREACH:
250 switch (code) {
251 case ICMP_UNREACH_NET:
252 case ICMP_UNREACH_HOST:
253 case ICMP_UNREACH_PROTOCOL:
254 case ICMP_UNREACH_PORT:
255 case ICMP_UNREACH_SRCFAIL:
256 code += PRC_UNREACH_NET;
257 break;
258
259 case ICMP_UNREACH_NEEDFRAG:
260 code = PRC_MSGSIZE;
261 break;
262
263 case ICMP_UNREACH_NET_UNKNOWN:
264 case ICMP_UNREACH_NET_PROHIB:
265 case ICMP_UNREACH_TOSNET:
266 code = PRC_UNREACH_NET;
267 break;
268
269 case ICMP_UNREACH_HOST_UNKNOWN:
270 case ICMP_UNREACH_ISOLATED:
271 case ICMP_UNREACH_HOST_PROHIB:
272 case ICMP_UNREACH_TOSHOST:
273 code = PRC_UNREACH_HOST;
274 break;
275
276 default:
277 goto badcode;
278 }
279 goto deliver;
280
281 case ICMP_TIMXCEED:
282 if (code > 1)
283 goto badcode;
284 code += PRC_TIMXCEED_INTRANS;
285 goto deliver;
286
287 case ICMP_PARAMPROB:
288 if (code > 1)
289 goto badcode;
290 code = PRC_PARAMPROB;
291 goto deliver;
292
293 case ICMP_SOURCEQUENCH:
294 if (code)
295 goto badcode;
296 code = PRC_QUENCH;
297 deliver:
298 /*
299 * Problem with datagram; advise higher level routines.
300 */
301 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
302 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
303 icmpstat.icps_badlen++;
304 goto freeit;
305 }
306 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
307 goto badcode;
308 NTOHS(icp->icmp_ip.ip_len);
309 #ifdef ICMPPRINTFS
310 if (icmpprintfs)
311 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
312 #endif
313 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
314 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
315 if (ctlfunc)
316 (*ctlfunc)(code, sintosa(&icmpsrc), &icp->icmp_ip);
317 break;
318
319 badcode:
320 icmpstat.icps_badcode++;
321 break;
322
323 case ICMP_ECHO:
324 icp->icmp_type = ICMP_ECHOREPLY;
325 goto reflect;
326
327 case ICMP_TSTAMP:
328 if (icmplen < ICMP_TSLEN) {
329 icmpstat.icps_badlen++;
330 break;
331 }
332 icp->icmp_type = ICMP_TSTAMPREPLY;
333 icp->icmp_rtime = iptime();
334 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
335 goto reflect;
336
337 case ICMP_MASKREQ:
338 if (icmpmaskrepl == 0)
339 break;
340 /*
341 * We are not able to respond with all ones broadcast
342 * unless we receive it over a point-to-point interface.
343 */
344 if (icmplen < ICMP_MASKLEN)
345 break;
346 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
347 ip->ip_dst.s_addr == INADDR_ANY)
348 icmpdst.sin_addr = ip->ip_src;
349 else
350 icmpdst.sin_addr = ip->ip_dst;
351 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
352 m->m_pkthdr.rcvif));
353 if (ia == 0)
354 break;
355 icp->icmp_type = ICMP_MASKREPLY;
356 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
357 if (ip->ip_src.s_addr == 0) {
358 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
359 ip->ip_src = ia->ia_broadaddr.sin_addr;
360 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
361 ip->ip_src = ia->ia_dstaddr.sin_addr;
362 }
363 reflect:
364 ip->ip_len += hlen; /* since ip_input deducts this */
365 icmpstat.icps_reflect++;
366 icmpstat.icps_outhist[icp->icmp_type]++;
367 icmp_reflect(m);
368 return;
369
370 case ICMP_REDIRECT:
371 if (code > 3)
372 goto badcode;
373 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
374 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
375 icmpstat.icps_badlen++;
376 break;
377 }
378 /*
379 * Short circuit routing redirects to force
380 * immediate change in the kernel's routing
381 * tables. The message is also handed to anyone
382 * listening on a raw socket (e.g. the routing
383 * daemon for use in updating its tables).
384 */
385 icmpgw.sin_addr = ip->ip_src;
386 icmpdst.sin_addr = icp->icmp_gwaddr;
387 #ifdef ICMPPRINTFS
388 if (icmpprintfs)
389 printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
390 icp->icmp_gwaddr);
391 #endif
392 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
393 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
394 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
395 sintosa(&icmpgw), (struct rtentry **)0);
396 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
397 break;
398
399 /*
400 * No kernel processing for the following;
401 * just fall through to send to raw listener.
402 */
403 case ICMP_ECHOREPLY:
404 case ICMP_ROUTERADVERT:
405 case ICMP_ROUTERSOLICIT:
406 case ICMP_TSTAMPREPLY:
407 case ICMP_IREQREPLY:
408 case ICMP_MASKREPLY:
409 default:
410 break;
411 }
412
413 raw:
414 rip_input(m);
415 return;
416
417 freeit:
418 m_freem(m);
419 }
420
421 /*
422 * Reflect the ip packet back to the source
423 */
424 void
425 icmp_reflect(m)
426 struct mbuf *m;
427 {
428 register struct ip *ip = mtod(m, struct ip *);
429 register struct in_ifaddr *ia;
430 struct in_addr t;
431 struct mbuf *opts = 0;
432 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
433
434 if (!in_canforward(ip->ip_src) &&
435 ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
436 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
437 m_freem(m); /* Bad return address */
438 goto done; /* ip_output() will check for broadcast */
439 }
440 t = ip->ip_dst;
441 ip->ip_dst = ip->ip_src;
442 /*
443 * If the incoming packet was addressed directly to us,
444 * use dst as the src for the reply. Otherwise (broadcast
445 * or anonymous), use the address which corresponds
446 * to the incoming interface.
447 */
448 for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) {
449 if (t.s_addr == ia->ia_addr.sin_addr.s_addr)
450 break;
451 if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
452 t.s_addr == ia->ia_broadaddr.sin_addr.s_addr)
453 break;
454 }
455 icmpdst.sin_addr = t;
456 if (ia == (struct in_ifaddr *)0)
457 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
458 m->m_pkthdr.rcvif));
459 /*
460 * The following happens if the packet was not addressed to us,
461 * and was received on an interface with no IP address.
462 */
463 if (ia == (struct in_ifaddr *)0)
464 ia = in_ifaddr.tqh_first;
465 t = ia->ia_addr.sin_addr;
466 ip->ip_src = t;
467 ip->ip_ttl = MAXTTL;
468
469 if (optlen > 0) {
470 register u_char *cp;
471 int opt, cnt;
472 u_int len;
473
474 /*
475 * Retrieve any source routing from the incoming packet;
476 * add on any record-route or timestamp options.
477 */
478 cp = (u_char *) (ip + 1);
479 if ((opts = ip_srcroute()) == 0 &&
480 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
481 opts->m_len = sizeof(struct in_addr);
482 mtod(opts, struct in_addr *)->s_addr = 0;
483 }
484 if (opts) {
485 #ifdef ICMPPRINTFS
486 if (icmpprintfs)
487 printf("icmp_reflect optlen %d rt %d => ",
488 optlen, opts->m_len);
489 #endif
490 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
491 opt = cp[IPOPT_OPTVAL];
492 if (opt == IPOPT_EOL)
493 break;
494 if (opt == IPOPT_NOP)
495 len = 1;
496 else {
497 len = cp[IPOPT_OLEN];
498 if (len <= 0 || len > cnt)
499 break;
500 }
501 /*
502 * Should check for overflow, but it "can't happen"
503 */
504 if (opt == IPOPT_RR || opt == IPOPT_TS ||
505 opt == IPOPT_SECURITY) {
506 bcopy((caddr_t)cp,
507 mtod(opts, caddr_t) + opts->m_len, len);
508 opts->m_len += len;
509 }
510 }
511 /* Terminate & pad, if necessary */
512 if ((cnt = opts->m_len % 4) != 0) {
513 for (; cnt < 4; cnt++) {
514 *(mtod(opts, caddr_t) + opts->m_len) =
515 IPOPT_EOL;
516 opts->m_len++;
517 }
518 }
519 #ifdef ICMPPRINTFS
520 if (icmpprintfs)
521 printf("%d\n", opts->m_len);
522 #endif
523 }
524 /*
525 * Now strip out original options by copying rest of first
526 * mbuf's data back, and adjust the IP length.
527 */
528 ip->ip_len -= optlen;
529 ip->ip_hl = sizeof(struct ip) >> 2;
530 m->m_len -= optlen;
531 if (m->m_flags & M_PKTHDR)
532 m->m_pkthdr.len -= optlen;
533 optlen += sizeof(struct ip);
534 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
535 (unsigned)(m->m_len - sizeof(struct ip)));
536 }
537 m->m_flags &= ~(M_BCAST|M_MCAST);
538 icmp_send(m, opts);
539 done:
540 if (opts)
541 (void)m_free(opts);
542 }
543
544 /*
545 * Send an icmp packet back to the ip level,
546 * after supplying a checksum.
547 */
548 void
549 icmp_send(m, opts)
550 register struct mbuf *m;
551 struct mbuf *opts;
552 {
553 register struct ip *ip = mtod(m, struct ip *);
554 register int hlen;
555 register struct icmp *icp;
556
557 hlen = ip->ip_hl << 2;
558 m->m_data += hlen;
559 m->m_len -= hlen;
560 icp = mtod(m, struct icmp *);
561 icp->icmp_cksum = 0;
562 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
563 m->m_data -= hlen;
564 m->m_len += hlen;
565 #ifdef ICMPPRINTFS
566 if (icmpprintfs)
567 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
568 #endif
569 (void) ip_output(m, opts, NULL, 0, NULL);
570 }
571
572 n_time
573 iptime()
574 {
575 struct timeval atv;
576 u_long t;
577
578 microtime(&atv);
579 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
580 return (htonl(t));
581 }
582
583 int
584 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
585 int *name;
586 u_int namelen;
587 void *oldp;
588 size_t *oldlenp;
589 void *newp;
590 size_t newlen;
591 {
592
593 /* All sysctl names at this level are terminal. */
594 if (namelen != 1)
595 return (ENOTDIR);
596
597 switch (name[0]) {
598 case ICMPCTL_MASKREPL:
599 return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
600 default:
601 return (ENOPROTOOPT);
602 }
603 /* NOTREACHED */
604 }
605