xref: /csrg-svn/sys/netinet/ip_input.c (revision 4923)
1*4923Swnj /* ip_input.c 1.15 81/11/18 */
24571Swnj 
34495Swnj #include "../h/param.h"
44543Swnj #include "../h/systm.h"
54640Swnj #include "../h/clock.h"
64640Swnj #include "../h/mbuf.h"
74898Swnj #include "../h/protosw.h"
8*4923Swnj #include "../h/socket.h"
94801Swnj #include "../net/inet.h"
104801Swnj #include "../net/inet_systm.h"
114801Swnj #include "../net/imp.h"
124801Swnj #include "../net/ip.h"			/* belongs before inet.h */
134898Swnj #include "../net/ip_var.h"
144801Swnj #include "../net/ip_icmp.h"
154801Swnj #include "../net/tcp.h"
164495Swnj 
174898Swnj u_char	ip_protox[IPPROTO_MAX];
184898Swnj 
194801Swnj /*
204801Swnj  * Ip initialization.
214801Swnj  */
224801Swnj ip_init()
234801Swnj {
244898Swnj 	register struct protosw *pr;
254898Swnj 	register int i;
264495Swnj 
274898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
284898Swnj 	if (pr == 0)
294898Swnj 		panic("ip_init");
304898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
314898Swnj 		ip_protox[i] = pr - protosw;
324898Swnj 	for (pr = protosw; pr <= protoswLAST; pr++)
334898Swnj 		if (pr->pr_family == PF_INET &&
344898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
354898Swnj 			ip_protox[pr->pr_protocol] = pr - protosw;
364801Swnj 	ipq.next = ipq.prev = &ipq;
374801Swnj 	ip_id = time & 0xffff;
384801Swnj }
394801Swnj 
404898Swnj u_char	ipcksum = 1;
414640Swnj struct	ip *ip_reass();
424640Swnj 
434640Swnj /*
444640Swnj  * Ip input routines.
454640Swnj  */
464640Swnj 
474640Swnj /*
484640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
494640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
504640Swnj  * Process options.  Pass to next level.
514640Swnj  */
524640Swnj ip_input(m0)
534640Swnj 	struct mbuf *m0;
544495Swnj {
55*4923Swnj 	register struct ip *ip;
564689Swnj 	register struct mbuf *m = m0;
574640Swnj 	register int i;
584495Swnj 	register struct ipq *fp;
594495Swnj 	int hlen;
604495Swnj 
614495Swnj COUNT(IP_INPUT);
624640Swnj 	/*
634640Swnj 	 * Check header and byteswap.
644640Swnj 	 */
654640Swnj 	ip = mtod(m, struct ip *);
664640Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
674640Swnj 		printf("ip hdr ovflo\n");
684640Swnj 		m_freem(m);
694495Swnj 		return;
704495Swnj 	}
71*4923Swnj 	ip->ip_sum = inet_cksum(m, hlen);
724689Swnj 	if (ip->ip_sum) {
734689Swnj 		printf("ip_sum %x\n", ip->ip_sum);
74*4923Swnj 		ipstat.ips_badsum++;
754801Swnj 		if (ipcksum) {
764640Swnj 			m_freem(m);
774640Swnj 			return;
784495Swnj 		}
794495Swnj 	}
804907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
814640Swnj 	ip->ip_id = ntohs(ip->ip_id);
824640Swnj 	ip->ip_off = ntohs(ip->ip_off);
834495Swnj 
844543Swnj 	/*
854640Swnj 	 * Check that the amount of data in the buffers
864640Swnj 	 * is as at least much as the IP header would have us expect.
874640Swnj 	 * Trim mbufs if longer than we expect.
884640Swnj 	 * Drop packet if shorter than we expect.
894543Swnj 	 */
904640Swnj 	i = 0;
914640Swnj 	for (; m != NULL; m = m->m_next)
924495Swnj 		i += m->m_len;
934640Swnj 	m = m0;
944640Swnj 	if (i != ip->ip_len) {
954640Swnj 		if (i < ip->ip_len) {
964640Swnj 			printf("ip_input: short packet\n");
974640Swnj 			m_freem(m);
984640Swnj 			return;
994640Swnj 		}
1004640Swnj 		m_adj(m, ip->ip_len - i);
1014495Swnj 	}
1024495Swnj 
1034640Swnj 	/*
1044640Swnj 	 * Process options and, if not destined for us,
1054640Swnj 	 * ship it on.
1064640Swnj 	 */
1074543Swnj 	if (hlen > sizeof (struct ip))
1084907Swnj 		ip_dooptions(ip);
1094640Swnj 	if (ip->ip_dst.s_addr != n_lhost.s_addr) {
1104640Swnj 		if (--ip->ip_ttl == 0) {
1114907Swnj 			icmp_error(ip, ICMP_TIMXCEED, 0);
1124543Swnj 			return;
1134495Swnj 		}
1144640Swnj 		ip_output(dtom(ip));
1154640Swnj 		return;
1164543Swnj 	}
1174495Swnj 
1184640Swnj 	/*
1194640Swnj 	 * Look for queue of fragments
1204640Swnj 	 * of this datagram.
1214640Swnj 	 */
1224640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1234640Swnj 		if (ip->ip_id == fp->ipq_id &&
1244640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1254640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1264640Swnj 		    ip->ip_p == fp->ipq_p)
1274640Swnj 			goto found;
1284640Swnj 	fp = 0;
1294640Swnj found:
1304495Swnj 
1314640Swnj 	/*
1324640Swnj 	 * Adjust ip_len to not reflect header,
1334640Swnj 	 * set ip_mff if more fragments are expected,
1344640Swnj 	 * convert offset of this to bytes.
1354640Swnj 	 */
1364640Swnj 	ip->ip_len -= hlen;
1374898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1384640Swnj 	if (ip->ip_off & IP_MF)
1394898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1404640Swnj 	ip->ip_off <<= 3;
1414495Swnj 
1424640Swnj 	/*
1434640Swnj 	 * If datagram marked as having more fragments
1444640Swnj 	 * or if this is not the first fragment,
1454640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1464640Swnj 	 */
1474898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1484898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1494640Swnj 		if (ip == 0)
1504640Swnj 			return;
1514640Swnj 		hlen = ip->ip_hl << 2;
1524640Swnj 		m = dtom(ip);
1534640Swnj 	} else
1544640Swnj 		if (fp)
1554640Swnj 			(void) ip_freef(fp);
1564898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
1574640Swnj }
1584495Swnj 
1594640Swnj /*
1604640Swnj  * Take incoming datagram fragment and try to
1614640Swnj  * reassamble it into whole datagram.  If a chain for
1624640Swnj  * reassembly of this datagram already exists, then it
1634640Swnj  * is given as fp; otherwise have to make a chain.
1644640Swnj  */
1654640Swnj struct ip *
1664640Swnj ip_reass(ip, fp)
1674898Swnj 	register struct ipasfrag *ip;
1684640Swnj 	register struct ipq *fp;
1694640Swnj {
1704640Swnj 	register struct mbuf *m = dtom(ip);
1714898Swnj 	register struct ipasfrag *q;
1724640Swnj 	struct mbuf *t;
1734640Swnj 	int hlen = ip->ip_hl << 2;
1744640Swnj 	int i, next;
1754543Swnj 
1764640Swnj 	/*
1774640Swnj 	 * Presence of header sizes in mbufs
1784640Swnj 	 * would confuse code below.
1794640Swnj 	 */
1804640Swnj 	m->m_off += hlen;
1814640Swnj 	m->m_len -= hlen;
1824495Swnj 
1834640Swnj 	/*
1844640Swnj 	 * If first fragment to arrive, create a reassembly queue.
1854640Swnj 	 */
1864640Swnj 	if (fp == 0) {
1874640Swnj 		if ((t = m_get(1)) == NULL)
1884640Swnj 			goto dropfrag;
1894640Swnj 		t->m_off = MMINOFF;
1904640Swnj 		fp = mtod(t, struct ipq *);
1914640Swnj 		insque(fp, &ipq);
1924640Swnj 		fp->ipq_ttl = IPFRAGTTL;
1934640Swnj 		fp->ipq_p = ip->ip_p;
1944640Swnj 		fp->ipq_id = ip->ip_id;
1954898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
1964898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
1974898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
1984640Swnj 	}
1994495Swnj 
2004640Swnj 	/*
2014640Swnj 	 * Find a segment which begins after this one does.
2024640Swnj 	 */
2034898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2044640Swnj 		if (q->ip_off > ip->ip_off)
2054640Swnj 			break;
2064495Swnj 
2074640Swnj 	/*
2084640Swnj 	 * If there is a preceding segment, it may provide some of
2094640Swnj 	 * our data already.  If so, drop the data from the incoming
2104640Swnj 	 * segment.  If it provides all of our data, drop us.
2114640Swnj 	 */
2124898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2134898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2144640Swnj 		if (i > 0) {
2154640Swnj 			if (i >= ip->ip_len)
2164640Swnj 				goto dropfrag;
2174640Swnj 			m_adj(dtom(ip), i);
2184640Swnj 			ip->ip_off += i;
2194640Swnj 			ip->ip_len -= i;
2204640Swnj 		}
2214640Swnj 	}
2224543Swnj 
2234640Swnj 	/*
2244640Swnj 	 * While we overlap succeeding segments trim them or,
2254640Swnj 	 * if they are completely covered, dequeue them.
2264640Swnj 	 */
2274898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2284640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2294640Swnj 		if (i < q->ip_len) {
2304640Swnj 			q->ip_len -= i;
2314640Swnj 			m_adj(dtom(q), i);
2324640Swnj 			break;
2334495Swnj 		}
2344898Swnj 		q = q->ipf_next;
2354898Swnj 		m_freem(dtom(q->ipf_prev));
2364898Swnj 		ip_deq(q->ipf_prev);
2374543Swnj 	}
2384495Swnj 
2394640Swnj 	/*
2404640Swnj 	 * Stick new segment in its place;
2414640Swnj 	 * check for complete reassembly.
2424640Swnj 	 */
2434898Swnj 	ip_enq(ip, q->ipf_prev);
2444640Swnj 	next = 0;
2454898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
2464640Swnj 		if (q->ip_off != next)
2474640Swnj 			return (0);
2484640Swnj 		next += q->ip_len;
2494640Swnj 	}
2504898Swnj 	if (q->ipf_prev->ipf_mff)
2514640Swnj 		return (0);
2524495Swnj 
2534640Swnj 	/*
2544640Swnj 	 * Reassembly is complete; concatenate fragments.
2554640Swnj 	 */
2564640Swnj 	q = fp->ipq_next;
2574640Swnj 	m = dtom(q);
2584640Swnj 	t = m->m_next;
2594640Swnj 	m->m_next = 0;
2604640Swnj 	m_cat(m, t);
2614898Swnj 	while ((q = q->ipf_next) != (struct ipasfrag *)fp)
2624640Swnj 		m_cat(m, dtom(q));
2634495Swnj 
2644640Swnj 	/*
2654640Swnj 	 * Create header for new ip packet by
2664640Swnj 	 * modifying header of first packet;
2674640Swnj 	 * dequeue and discard fragment reassembly header.
2684640Swnj 	 * Make header visible.
2694640Swnj 	 */
2704640Swnj 	ip = fp->ipq_next;
2714640Swnj 	ip->ip_len = next;
2724898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
2734898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
2744640Swnj 	remque(fp);
2754907Swnj 	(void) m_free(dtom(fp));
2764640Swnj 	m = dtom(ip);
2774898Swnj 	m->m_len += sizeof (struct ipasfrag);
2784898Swnj 	m->m_off -= sizeof (struct ipasfrag);
2794898Swnj 	return ((struct ip *)ip);
2804495Swnj 
2814640Swnj dropfrag:
2824640Swnj 	m_freem(m);
2834640Swnj 	return (0);
2844495Swnj }
2854495Swnj 
2864640Swnj /*
2874640Swnj  * Free a fragment reassembly header and all
2884640Swnj  * associated datagrams.
2894640Swnj  */
2904640Swnj struct ipq *
2914640Swnj ip_freef(fp)
2924640Swnj 	struct ipq *fp;
2934495Swnj {
2944898Swnj 	register struct ipasfrag *q;
2954640Swnj 	struct mbuf *m;
2964495Swnj 
2974898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2984640Swnj 		m_freem(dtom(q));
2994640Swnj 	m = dtom(fp);
3004640Swnj 	fp = fp->next;
3014640Swnj 	remque(fp->prev);
3024907Swnj 	(void) m_free(m);
3034640Swnj 	return (fp);
3044495Swnj }
3054495Swnj 
3064640Swnj /*
3074640Swnj  * Put an ip fragment on a reassembly chain.
3084640Swnj  * Like insque, but pointers in middle of structure.
3094640Swnj  */
3104640Swnj ip_enq(p, prev)
3114898Swnj 	register struct ipasfrag *p, *prev;
3124495Swnj {
3134640Swnj COUNT(IP_ENQ);
3144495Swnj 
3154898Swnj 	p->ipf_prev = prev;
3164898Swnj 	p->ipf_next = prev->ipf_next;
3174898Swnj 	prev->ipf_next->ipf_prev = p;
3184898Swnj 	prev->ipf_next = p;
3194495Swnj }
3204495Swnj 
3214640Swnj /*
3224640Swnj  * To ip_enq as remque is to insque.
3234640Swnj  */
3244640Swnj ip_deq(p)
3254898Swnj 	register struct ipasfrag *p;
3264640Swnj {
3274640Swnj COUNT(IP_DEQ);
3284495Swnj 
3294898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3304898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3314495Swnj }
3324495Swnj 
3334640Swnj /*
3344640Swnj  * IP timer processing;
3354640Swnj  * if a timer expires on a reassembly
3364640Swnj  * queue, discard it.
3374640Swnj  */
3384801Swnj ip_slowtimo()
3394495Swnj {
3404495Swnj 	register struct ipq *fp;
3414640Swnj 	int s = splnet();
3424801Swnj COUNT(IP_SLOWTIMO);
3434495Swnj 
3444644Swnj 	for (fp = ipq.next; fp != &ipq; )
3454640Swnj 		if (--fp->ipq_ttl == 0)
3464640Swnj 			fp = ip_freef(fp);
3474640Swnj 		else
3484640Swnj 			fp = fp->next;
3494640Swnj 	splx(s);
3504495Swnj }
3514495Swnj 
3524801Swnj ip_drain()
3534801Swnj {
3544801Swnj 
3554801Swnj }
356*4923Swnj 
3574640Swnj /*
3584640Swnj  * Do option processing on a datagram,
3594640Swnj  * possibly discarding it if bad options
3604640Swnj  * are encountered.
3614640Swnj  */
3624640Swnj ip_dooptions(ip)
3634640Swnj 	struct ip *ip;
3644495Swnj {
3654640Swnj 	register u_char *cp;
3664907Swnj 	int opt, optlen, cnt;
367*4923Swnj 	struct in_addr *sin;
3684801Swnj 	register struct ip_timestamp *ipt;
3694495Swnj 
3704640Swnj 	cp = (u_char *)(ip + 1);
3714640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
3724640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3734640Swnj 		opt = cp[0];
3744640Swnj 		if (opt == IPOPT_EOL)
3754640Swnj 			break;
3764640Swnj 		if (opt == IPOPT_NOP)
3774640Swnj 			optlen = 1;
3784640Swnj 		else
3794640Swnj 			optlen = cp[1];
3804640Swnj 		switch (opt) {
3814495Swnj 
3824640Swnj 		default:
3834640Swnj 			break;
3844495Swnj 
3854640Swnj 		case IPOPT_LSRR:
3864640Swnj 		case IPOPT_SSRR:
3874801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
3884640Swnj 				break;
389*4923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
390*4923Swnj 			if (n_lhost.s_addr == *(u_long *)sin) {
3914640Swnj 				if (opt == IPOPT_SSRR) {
3924801Swnj 					/* MAKE SURE *SP DIRECTLY ACCESSIBLE */
3934640Swnj 				}
394*4923Swnj 				ip->ip_dst = *sin;
395*4923Swnj 				*sin = n_lhost;
3964640Swnj 				cp[2] += 4;
3974640Swnj 			}
3984640Swnj 			break;
3994495Swnj 
4004640Swnj 		case IPOPT_TS:
4014801Swnj 			ipt = (struct ip_timestamp *)cp;
4024801Swnj 			if (ipt->ipt_len < 5)
4034640Swnj 				goto bad;
4044801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
4054801Swnj 				if (++ipt->ipt_oflw == 0)
4064640Swnj 					goto bad;
4074495Swnj 				break;
4084640Swnj 			}
409*4923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
4104801Swnj 			switch (ipt->ipt_flg) {
4114495Swnj 
4124640Swnj 			case IPOPT_TS_TSONLY:
4134640Swnj 				break;
4144640Swnj 
4154640Swnj 			case IPOPT_TS_TSANDADDR:
4164801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4174640Swnj 					goto bad;
418*4923Swnj 				*(struct in_addr *)sin++ = n_lhost;
4194640Swnj 				break;
4204640Swnj 
4214640Swnj 			case IPOPT_TS_PRESPEC:
422*4923Swnj 				if (*(u_long *)sin != n_lhost.s_addr)
4234640Swnj 					break;
4244801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4254640Swnj 					goto bad;
4264801Swnj 				ipt->ipt_ptr += 4;
4274640Swnj 				break;
4284640Swnj 
4294495Swnj 			default:
4304640Swnj 				goto bad;
4314495Swnj 			}
432*4923Swnj 			*(n_time *)sin = iptime();
4334801Swnj 			ipt->ipt_ptr += 4;
4344640Swnj 		}
4354495Swnj 	}
4364907Swnj 	return;
4374640Swnj bad:
4384640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
4394907Swnj 	return;
4404495Swnj }
4414495Swnj 
4424640Swnj /*
4434640Swnj  * Strip out IP options, e.g. before passing
4444640Swnj  * to higher level protocol in the kernel.
4454640Swnj  */
4464640Swnj ip_stripoptions(ip)
4474640Swnj 	struct ip *ip;
4484495Swnj {
4494640Swnj 	register int i;
4504640Swnj 	register struct mbuf *m;
4514640Swnj 	int olen;
4524640Swnj COUNT(IP_OPT);
4534640Swnj 
4544640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
4554640Swnj 	m = dtom(++ip);
4564640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
4574907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
4584640Swnj 	m->m_len -= i;
4594495Swnj }
460