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