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