xref: /csrg-svn/sys/netinet/ip_input.c (revision 4951)
1*4951Swnj /* ip_input.c 1.16 81/11/20 */
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"
84923Swnj #include "../h/socket.h"
94801Swnj #include "../net/inet.h"
104801Swnj #include "../net/inet_systm.h"
11*4951Swnj #include "../net/if.h"
124801Swnj #include "../net/imp.h"
134801Swnj #include "../net/ip.h"			/* belongs before inet.h */
144898Swnj #include "../net/ip_var.h"
154801Swnj #include "../net/ip_icmp.h"
164801Swnj #include "../net/tcp.h"
174495Swnj 
184898Swnj u_char	ip_protox[IPPROTO_MAX];
194898Swnj 
204801Swnj /*
214801Swnj  * Ip initialization.
224801Swnj  */
234801Swnj ip_init()
244801Swnj {
254898Swnj 	register struct protosw *pr;
264898Swnj 	register int i;
274495Swnj 
28*4951Swnj COUNT(IP_INIT);
294898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
304898Swnj 	if (pr == 0)
314898Swnj 		panic("ip_init");
324898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
334898Swnj 		ip_protox[i] = pr - protosw;
344898Swnj 	for (pr = protosw; pr <= protoswLAST; pr++)
354898Swnj 		if (pr->pr_family == PF_INET &&
364898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
374898Swnj 			ip_protox[pr->pr_protocol] = pr - protosw;
384801Swnj 	ipq.next = ipq.prev = &ipq;
394801Swnj 	ip_id = time & 0xffff;
404801Swnj }
414801Swnj 
424898Swnj u_char	ipcksum = 1;
434640Swnj struct	ip *ip_reass();
444640Swnj 
454640Swnj /*
464640Swnj  * Ip input routines.
474640Swnj  */
484640Swnj 
494640Swnj /*
504640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
514640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
524640Swnj  * Process options.  Pass to next level.
534640Swnj  */
544640Swnj ip_input(m0)
554640Swnj 	struct mbuf *m0;
564495Swnj {
574923Swnj 	register struct ip *ip;
584689Swnj 	register struct mbuf *m = m0;
594640Swnj 	register int i;
604495Swnj 	register struct ipq *fp;
614495Swnj 	int hlen;
624495Swnj 
634495Swnj COUNT(IP_INPUT);
644640Swnj 	/*
654640Swnj 	 * Check header and byteswap.
664640Swnj 	 */
674640Swnj 	ip = mtod(m, struct ip *);
684640Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
694640Swnj 		printf("ip hdr ovflo\n");
70*4951Swnj 		goto bad;
714495Swnj 	}
72*4951Swnj 	if (ipcksum)
73*4951Swnj 		if (ip->ip_sum = inet_cksum(m, hlen)) {
74*4951Swnj 			printf("ip_sum %x\n", ip->ip_sum);
75*4951Swnj 			ipstat.ips_badsum++;
76*4951Swnj 			goto bad;
774495Swnj 		}
78*4951Swnj 
79*4951Swnj 	/*
80*4951Swnj 	 * Convert fields to host representation.
81*4951Swnj 	 */
824907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
834640Swnj 	ip->ip_id = ntohs(ip->ip_id);
84*4951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
854495Swnj 
864543Swnj 	/*
874640Swnj 	 * Check that the amount of data in the buffers
884640Swnj 	 * is as at least much as the IP header would have us expect.
894640Swnj 	 * Trim mbufs if longer than we expect.
904640Swnj 	 * Drop packet if shorter than we expect.
914543Swnj 	 */
924640Swnj 	i = 0;
934640Swnj 	for (; m != NULL; m = m->m_next)
944495Swnj 		i += m->m_len;
954640Swnj 	m = m0;
964640Swnj 	if (i != ip->ip_len) {
974640Swnj 		if (i < ip->ip_len) {
984640Swnj 			printf("ip_input: short packet\n");
99*4951Swnj 			goto bad;
1004640Swnj 		}
1014640Swnj 		m_adj(m, ip->ip_len - i);
1024495Swnj 	}
1034495Swnj 
1044640Swnj 	/*
1054640Swnj 	 * Process options and, if not destined for us,
1064640Swnj 	 * ship it on.
1074640Swnj 	 */
1084543Swnj 	if (hlen > sizeof (struct ip))
1094907Swnj 		ip_dooptions(ip);
1104640Swnj 	if (ip->ip_dst.s_addr != n_lhost.s_addr) {
1114640Swnj 		if (--ip->ip_ttl == 0) {
1124907Swnj 			icmp_error(ip, ICMP_TIMXCEED, 0);
1134543Swnj 			return;
1144495Swnj 		}
1154640Swnj 		ip_output(dtom(ip));
1164640Swnj 		return;
1174543Swnj 	}
1184495Swnj 
1194640Swnj 	/*
1204640Swnj 	 * Look for queue of fragments
1214640Swnj 	 * of this datagram.
1224640Swnj 	 */
1234640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1244640Swnj 		if (ip->ip_id == fp->ipq_id &&
1254640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1264640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1274640Swnj 		    ip->ip_p == fp->ipq_p)
1284640Swnj 			goto found;
1294640Swnj 	fp = 0;
1304640Swnj found:
1314495Swnj 
1324640Swnj 	/*
1334640Swnj 	 * Adjust ip_len to not reflect header,
1344640Swnj 	 * set ip_mff if more fragments are expected,
1354640Swnj 	 * convert offset of this to bytes.
1364640Swnj 	 */
1374640Swnj 	ip->ip_len -= hlen;
1384898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1394640Swnj 	if (ip->ip_off & IP_MF)
1404898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1414640Swnj 	ip->ip_off <<= 3;
1424495Swnj 
1434640Swnj 	/*
1444640Swnj 	 * If datagram marked as having more fragments
1454640Swnj 	 * or if this is not the first fragment,
1464640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1474640Swnj 	 */
1484898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1494898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1504640Swnj 		if (ip == 0)
1514640Swnj 			return;
1524640Swnj 		hlen = ip->ip_hl << 2;
1534640Swnj 		m = dtom(ip);
1544640Swnj 	} else
1554640Swnj 		if (fp)
1564640Swnj 			(void) ip_freef(fp);
157*4951Swnj 
158*4951Swnj 	/*
159*4951Swnj 	 * Switch out to protocol's input routine.
160*4951Swnj 	 */
1614898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
162*4951Swnj 	return;
163*4951Swnj bad:
164*4951Swnj 	m_freem(m);
1654640Swnj }
1664495Swnj 
1674640Swnj /*
1684640Swnj  * Take incoming datagram fragment and try to
169*4951Swnj  * reassemble it into whole datagram.  If a chain for
1704640Swnj  * reassembly of this datagram already exists, then it
1714640Swnj  * is given as fp; otherwise have to make a chain.
1724640Swnj  */
1734640Swnj struct ip *
1744640Swnj ip_reass(ip, fp)
1754898Swnj 	register struct ipasfrag *ip;
1764640Swnj 	register struct ipq *fp;
1774640Swnj {
1784640Swnj 	register struct mbuf *m = dtom(ip);
1794898Swnj 	register struct ipasfrag *q;
1804640Swnj 	struct mbuf *t;
1814640Swnj 	int hlen = ip->ip_hl << 2;
1824640Swnj 	int i, next;
183*4951Swnj COUNT(IP_REASS);
1844543Swnj 
1854640Swnj 	/*
1864640Swnj 	 * Presence of header sizes in mbufs
1874640Swnj 	 * would confuse code below.
1884640Swnj 	 */
1894640Swnj 	m->m_off += hlen;
1904640Swnj 	m->m_len -= hlen;
1914495Swnj 
1924640Swnj 	/*
1934640Swnj 	 * If first fragment to arrive, create a reassembly queue.
1944640Swnj 	 */
1954640Swnj 	if (fp == 0) {
1964640Swnj 		if ((t = m_get(1)) == NULL)
1974640Swnj 			goto dropfrag;
1984640Swnj 		t->m_off = MMINOFF;
1994640Swnj 		fp = mtod(t, struct ipq *);
2004640Swnj 		insque(fp, &ipq);
2014640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2024640Swnj 		fp->ipq_p = ip->ip_p;
2034640Swnj 		fp->ipq_id = ip->ip_id;
2044898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2054898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2064898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2074640Swnj 	}
2084495Swnj 
2094640Swnj 	/*
2104640Swnj 	 * Find a segment which begins after this one does.
2114640Swnj 	 */
2124898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2134640Swnj 		if (q->ip_off > ip->ip_off)
2144640Swnj 			break;
2154495Swnj 
2164640Swnj 	/*
2174640Swnj 	 * If there is a preceding segment, it may provide some of
2184640Swnj 	 * our data already.  If so, drop the data from the incoming
2194640Swnj 	 * segment.  If it provides all of our data, drop us.
2204640Swnj 	 */
2214898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2224898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2234640Swnj 		if (i > 0) {
2244640Swnj 			if (i >= ip->ip_len)
2254640Swnj 				goto dropfrag;
2264640Swnj 			m_adj(dtom(ip), i);
2274640Swnj 			ip->ip_off += i;
2284640Swnj 			ip->ip_len -= i;
2294640Swnj 		}
2304640Swnj 	}
2314543Swnj 
2324640Swnj 	/*
2334640Swnj 	 * While we overlap succeeding segments trim them or,
2344640Swnj 	 * if they are completely covered, dequeue them.
2354640Swnj 	 */
2364898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2374640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2384640Swnj 		if (i < q->ip_len) {
2394640Swnj 			q->ip_len -= i;
2404640Swnj 			m_adj(dtom(q), i);
2414640Swnj 			break;
2424495Swnj 		}
2434898Swnj 		q = q->ipf_next;
2444898Swnj 		m_freem(dtom(q->ipf_prev));
2454898Swnj 		ip_deq(q->ipf_prev);
2464543Swnj 	}
2474495Swnj 
2484640Swnj 	/*
2494640Swnj 	 * Stick new segment in its place;
2504640Swnj 	 * check for complete reassembly.
2514640Swnj 	 */
2524898Swnj 	ip_enq(ip, q->ipf_prev);
2534640Swnj 	next = 0;
2544898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
2554640Swnj 		if (q->ip_off != next)
2564640Swnj 			return (0);
2574640Swnj 		next += q->ip_len;
2584640Swnj 	}
2594898Swnj 	if (q->ipf_prev->ipf_mff)
2604640Swnj 		return (0);
2614495Swnj 
2624640Swnj 	/*
2634640Swnj 	 * Reassembly is complete; concatenate fragments.
2644640Swnj 	 */
2654640Swnj 	q = fp->ipq_next;
2664640Swnj 	m = dtom(q);
2674640Swnj 	t = m->m_next;
2684640Swnj 	m->m_next = 0;
2694640Swnj 	m_cat(m, t);
2704898Swnj 	while ((q = q->ipf_next) != (struct ipasfrag *)fp)
2714640Swnj 		m_cat(m, dtom(q));
2724495Swnj 
2734640Swnj 	/*
2744640Swnj 	 * Create header for new ip packet by
2754640Swnj 	 * modifying header of first packet;
2764640Swnj 	 * dequeue and discard fragment reassembly header.
2774640Swnj 	 * Make header visible.
2784640Swnj 	 */
2794640Swnj 	ip = fp->ipq_next;
2804640Swnj 	ip->ip_len = next;
2814898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
2824898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
2834640Swnj 	remque(fp);
2844907Swnj 	(void) m_free(dtom(fp));
2854640Swnj 	m = dtom(ip);
2864898Swnj 	m->m_len += sizeof (struct ipasfrag);
2874898Swnj 	m->m_off -= sizeof (struct ipasfrag);
2884898Swnj 	return ((struct ip *)ip);
2894495Swnj 
2904640Swnj dropfrag:
2914640Swnj 	m_freem(m);
2924640Swnj 	return (0);
2934495Swnj }
2944495Swnj 
2954640Swnj /*
2964640Swnj  * Free a fragment reassembly header and all
2974640Swnj  * associated datagrams.
2984640Swnj  */
2994640Swnj struct ipq *
3004640Swnj ip_freef(fp)
3014640Swnj 	struct ipq *fp;
3024495Swnj {
3034898Swnj 	register struct ipasfrag *q;
3044640Swnj 	struct mbuf *m;
305*4951Swnj COUNT(IP_FREEF);
3064495Swnj 
3074898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3084640Swnj 		m_freem(dtom(q));
3094640Swnj 	m = dtom(fp);
3104640Swnj 	fp = fp->next;
3114640Swnj 	remque(fp->prev);
3124907Swnj 	(void) m_free(m);
3134640Swnj 	return (fp);
3144495Swnj }
3154495Swnj 
3164640Swnj /*
3174640Swnj  * Put an ip fragment on a reassembly chain.
3184640Swnj  * Like insque, but pointers in middle of structure.
3194640Swnj  */
3204640Swnj ip_enq(p, prev)
3214898Swnj 	register struct ipasfrag *p, *prev;
3224495Swnj {
323*4951Swnj 
3244640Swnj COUNT(IP_ENQ);
3254898Swnj 	p->ipf_prev = prev;
3264898Swnj 	p->ipf_next = prev->ipf_next;
3274898Swnj 	prev->ipf_next->ipf_prev = p;
3284898Swnj 	prev->ipf_next = p;
3294495Swnj }
3304495Swnj 
3314640Swnj /*
3324640Swnj  * To ip_enq as remque is to insque.
3334640Swnj  */
3344640Swnj ip_deq(p)
3354898Swnj 	register struct ipasfrag *p;
3364640Swnj {
337*4951Swnj 
3384640Swnj COUNT(IP_DEQ);
3394898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3404898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3414495Swnj }
3424495Swnj 
3434640Swnj /*
3444640Swnj  * IP timer processing;
3454640Swnj  * if a timer expires on a reassembly
3464640Swnj  * queue, discard it.
3474640Swnj  */
3484801Swnj ip_slowtimo()
3494495Swnj {
3504495Swnj 	register struct ipq *fp;
3514640Swnj 	int s = splnet();
352*4951Swnj 
3534801Swnj COUNT(IP_SLOWTIMO);
3544644Swnj 	for (fp = ipq.next; fp != &ipq; )
3554640Swnj 		if (--fp->ipq_ttl == 0)
3564640Swnj 			fp = ip_freef(fp);
3574640Swnj 		else
3584640Swnj 			fp = fp->next;
3594640Swnj 	splx(s);
3604495Swnj }
3614495Swnj 
362*4951Swnj /*
363*4951Swnj  * Drain off all datagram fragments.
364*4951Swnj  */
3654801Swnj ip_drain()
3664801Swnj {
3674801Swnj 
368*4951Swnj COUNT(IP_DRAIN);
369*4951Swnj 	while (ipq.next != &ipq)
370*4951Swnj 		(void) ip_freef(ipq.next);
3714801Swnj }
3724923Swnj 
3734640Swnj /*
3744640Swnj  * Do option processing on a datagram,
3754640Swnj  * possibly discarding it if bad options
3764640Swnj  * are encountered.
3774640Swnj  */
3784640Swnj ip_dooptions(ip)
3794640Swnj 	struct ip *ip;
3804495Swnj {
3814640Swnj 	register u_char *cp;
3824907Swnj 	int opt, optlen, cnt;
3834923Swnj 	struct in_addr *sin;
3844801Swnj 	register struct ip_timestamp *ipt;
385*4951Swnj 	register struct ifnet *ifp;
386*4951Swnj 	struct in_addr t;
3874495Swnj 
388*4951Swnj COUNT(IP_DOOPTIONS);
3894640Swnj 	cp = (u_char *)(ip + 1);
3904640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
3914640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3924640Swnj 		opt = cp[0];
3934640Swnj 		if (opt == IPOPT_EOL)
3944640Swnj 			break;
3954640Swnj 		if (opt == IPOPT_NOP)
3964640Swnj 			optlen = 1;
3974640Swnj 		else
3984640Swnj 			optlen = cp[1];
3994640Swnj 		switch (opt) {
4004495Swnj 
4014640Swnj 		default:
4024640Swnj 			break;
4034495Swnj 
404*4951Swnj 		/*
405*4951Swnj 		 * Source routing with record.
406*4951Swnj 		 * Find interface with current destination address.
407*4951Swnj 		 * If none on this machine then drop if strictly routed,
408*4951Swnj 		 * or do nothing if loosely routed.
409*4951Swnj 		 * Record interface address and bring up next address
410*4951Swnj 		 * component.  If strictly routed make sure next
411*4951Swnj 		 * address on directly accessible net.
412*4951Swnj 		 */
4134640Swnj 		case IPOPT_LSRR:
4144801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4154640Swnj 				break;
4164923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
417*4951Swnj 			ifp = if_ifwithaddr(*sin);
418*4951Swnj 			if (ifp == 0) {
419*4951Swnj 				if (opt == IPOPT_SSRR)
420*4951Swnj 					goto bad;
421*4951Swnj 				break;
4224640Swnj 			}
423*4951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
424*4951Swnj 			cp[2] += 4;
425*4951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
426*4951Swnj 				break;
427*4951Swnj 			ip->ip_dst = sin[1];
428*4951Swnj 			if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0)
429*4951Swnj 				goto bad;
4304640Swnj 			break;
4314495Swnj 
4324640Swnj 		case IPOPT_TS:
4334801Swnj 			ipt = (struct ip_timestamp *)cp;
4344801Swnj 			if (ipt->ipt_len < 5)
4354640Swnj 				goto bad;
4364801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
4374801Swnj 				if (++ipt->ipt_oflw == 0)
4384640Swnj 					goto bad;
4394495Swnj 				break;
4404640Swnj 			}
4414923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
4424801Swnj 			switch (ipt->ipt_flg) {
4434495Swnj 
4444640Swnj 			case IPOPT_TS_TSONLY:
4454640Swnj 				break;
4464640Swnj 
4474640Swnj 			case IPOPT_TS_TSANDADDR:
4484801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4494640Swnj 					goto bad;
450*4951Swnj 				/* stamp with ``first'' interface address */
451*4951Swnj 				*sin++ = ifnet->if_addr;
4524640Swnj 				break;
4534640Swnj 
4544640Swnj 			case IPOPT_TS_PRESPEC:
455*4951Swnj 				if (if_ifwithaddr(*sin) == 0)
456*4951Swnj 					continue;
4574801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4584640Swnj 					goto bad;
4594801Swnj 				ipt->ipt_ptr += 4;
4604640Swnj 				break;
4614640Swnj 
4624495Swnj 			default:
4634640Swnj 				goto bad;
4644495Swnj 			}
4654923Swnj 			*(n_time *)sin = iptime();
4664801Swnj 			ipt->ipt_ptr += 4;
4674640Swnj 		}
4684495Swnj 	}
4694907Swnj 	return;
4704640Swnj bad:
4714640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
4724907Swnj 	return;
4734495Swnj }
4744495Swnj 
4754640Swnj /*
476*4951Swnj  * Strip out IP options, at higher
477*4951Swnj  * level protocol in the kernel.
478*4951Swnj  * Second argument is buffer to which options
479*4951Swnj  * will be moved, and return value is their length.
4804640Swnj  */
481*4951Swnj ip_stripoptions(ip, cp)
4824640Swnj 	struct ip *ip;
483*4951Swnj 	char *cp;
4844495Swnj {
4854640Swnj 	register int i;
4864640Swnj 	register struct mbuf *m;
4874640Swnj 	int olen;
488*4951Swnj COUNT(IP_STRIPOPTIONS);
4894640Swnj 
4904640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
491*4951Swnj 	m = dtom(ip);
492*4951Swnj 	ip++;
493*4951Swnj 	if (cp)
494*4951Swnj 		bcopy((caddr_t)ip, cp, (unsigned)olen);
4954640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
4964907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
4974640Swnj 	m->m_len -= i;
4984495Swnj }
499