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