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