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