xref: /csrg-svn/sys/netinet/ip_input.c (revision 5084)
1*5084Swnj /* ip_input.c 1.19 81/11/26 */
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"
9*5084Swnj #include "../net/in.h"
10*5084Swnj #include "../net/in_systm.h"
114951Swnj #include "../net/if.h"
12*5084Swnj #include "../net/ip.h"			/* belongs before in.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 
274951Swnj COUNT(IP_INIT);
284898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
294898Swnj 	if (pr == 0)
304898Swnj 		panic("ip_init");
314898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
324898Swnj 		ip_protox[i] = pr - protosw;
334898Swnj 	for (pr = protosw; pr <= protoswLAST; pr++)
344898Swnj 		if (pr->pr_family == PF_INET &&
354898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
364898Swnj 			ip_protox[pr->pr_protocol] = pr - protosw;
374801Swnj 	ipq.next = ipq.prev = &ipq;
384801Swnj 	ip_id = time & 0xffff;
394801Swnj }
404801Swnj 
414898Swnj u_char	ipcksum = 1;
424640Swnj struct	ip *ip_reass();
434640Swnj 
444640Swnj /*
454640Swnj  * Ip input routines.
464640Swnj  */
474640Swnj 
484640Swnj /*
494640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
504640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
514640Swnj  * Process options.  Pass to next level.
524640Swnj  */
53*5084Swnj ipintr()
544495Swnj {
554923Swnj 	register struct ip *ip;
56*5084Swnj 	register struct mbuf *m;
57*5084Swnj 	struct mbuf *m0;
584640Swnj 	register int i;
594495Swnj 	register struct ipq *fp;
60*5084Swnj 	int hlen, s;
614495Swnj 
62*5084Swnj COUNT(IPINTR);
63*5084Swnj next:
644640Swnj 	/*
65*5084Swnj 	 * Get next datagram off input queue and get IP header
66*5084Swnj 	 * in first mbuf.
674640Swnj 	 */
68*5084Swnj 	s = splimp();
69*5084Swnj 	IF_DEQUEUE(&ipintrq, m);
70*5084Swnj 	splx(s);
71*5084Swnj 	if (m == 0)
72*5084Swnj 		return;
735046Swnj 	if (m->m_len < sizeof (struct ip) &&
745046Swnj 	    m_pullup(m, sizeof (struct ip)) == 0)
755046Swnj 		goto bad;
764640Swnj 	ip = mtod(m, struct ip *);
775046Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len &&
785046Swnj 	    m_pullup(m, hlen) == 0)
794951Swnj 		goto bad;
804951Swnj 	if (ipcksum)
81*5084Swnj 		if ((ip->ip_sum = in_cksum(m, hlen)) != 0xffff) {
824951Swnj 			printf("ip_sum %x\n", ip->ip_sum);
834951Swnj 			ipstat.ips_badsum++;
844951Swnj 			goto bad;
854495Swnj 		}
864951Swnj 
874951Swnj 	/*
884951Swnj 	 * Convert fields to host representation.
894951Swnj 	 */
904907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
914640Swnj 	ip->ip_id = ntohs(ip->ip_id);
924951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
934495Swnj 
944543Swnj 	/*
954640Swnj 	 * Check that the amount of data in the buffers
964640Swnj 	 * is as at least much as the IP header would have us expect.
974640Swnj 	 * Trim mbufs if longer than we expect.
984640Swnj 	 * Drop packet if shorter than we expect.
994543Swnj 	 */
1004640Swnj 	i = 0;
101*5084Swnj 	for (m0 = m; m != NULL; m = m->m_next)
1024495Swnj 		i += m->m_len;
1034640Swnj 	m = m0;
1044640Swnj 	if (i != ip->ip_len) {
1055046Swnj 		if (i < ip->ip_len)
1064951Swnj 			goto bad;
1074640Swnj 		m_adj(m, ip->ip_len - i);
1084495Swnj 	}
1094495Swnj 
1104640Swnj 	/*
1114640Swnj 	 * Process options and, if not destined for us,
1124640Swnj 	 * ship it on.
1134640Swnj 	 */
1144543Swnj 	if (hlen > sizeof (struct ip))
1154907Swnj 		ip_dooptions(ip);
116*5084Swnj 	if (ifnet && ip->ip_dst.s_addr != ifnet->if_addr.s_addr &&
1175045Swnj 	    if_ifwithaddr(ip->ip_dst) == 0) {
1184640Swnj 		if (--ip->ip_ttl == 0) {
1194907Swnj 			icmp_error(ip, ICMP_TIMXCEED, 0);
120*5084Swnj 			goto next;
1214495Swnj 		}
122*5084Swnj 		ip_output(dtom(ip), (struct mbuf *)0);
123*5084Swnj 		goto next;
1244543Swnj 	}
1254495Swnj 
1264640Swnj 	/*
1274640Swnj 	 * Look for queue of fragments
1284640Swnj 	 * of this datagram.
1294640Swnj 	 */
1304640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1314640Swnj 		if (ip->ip_id == fp->ipq_id &&
1324640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1334640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1344640Swnj 		    ip->ip_p == fp->ipq_p)
1354640Swnj 			goto found;
1364640Swnj 	fp = 0;
1374640Swnj found:
1384495Swnj 
1394640Swnj 	/*
1404640Swnj 	 * Adjust ip_len to not reflect header,
1414640Swnj 	 * set ip_mff if more fragments are expected,
1424640Swnj 	 * convert offset of this to bytes.
1434640Swnj 	 */
1444640Swnj 	ip->ip_len -= hlen;
1454898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1464640Swnj 	if (ip->ip_off & IP_MF)
1474898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1484640Swnj 	ip->ip_off <<= 3;
1494495Swnj 
1504640Swnj 	/*
1514640Swnj 	 * If datagram marked as having more fragments
1524640Swnj 	 * or if this is not the first fragment,
1534640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1544640Swnj 	 */
1554898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1564898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1574640Swnj 		if (ip == 0)
158*5084Swnj 			goto next;
1594640Swnj 		hlen = ip->ip_hl << 2;
1604640Swnj 		m = dtom(ip);
1614640Swnj 	} else
1624640Swnj 		if (fp)
1634640Swnj 			(void) ip_freef(fp);
1644951Swnj 
1654951Swnj 	/*
1664951Swnj 	 * Switch out to protocol's input routine.
1674951Swnj 	 */
1684898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
169*5084Swnj 	goto next;
1704951Swnj bad:
1714951Swnj 	m_freem(m);
172*5084Swnj 	goto next;
1734640Swnj }
1744495Swnj 
1754640Swnj /*
1764640Swnj  * Take incoming datagram fragment and try to
1774951Swnj  * reassemble it into whole datagram.  If a chain for
1784640Swnj  * reassembly of this datagram already exists, then it
1794640Swnj  * is given as fp; otherwise have to make a chain.
1804640Swnj  */
1814640Swnj struct ip *
1824640Swnj ip_reass(ip, fp)
1834898Swnj 	register struct ipasfrag *ip;
1844640Swnj 	register struct ipq *fp;
1854640Swnj {
1864640Swnj 	register struct mbuf *m = dtom(ip);
1874898Swnj 	register struct ipasfrag *q;
1884640Swnj 	struct mbuf *t;
1894640Swnj 	int hlen = ip->ip_hl << 2;
1904640Swnj 	int i, next;
1914951Swnj COUNT(IP_REASS);
1924543Swnj 
1934640Swnj 	/*
1944640Swnj 	 * Presence of header sizes in mbufs
1954640Swnj 	 * would confuse code below.
1964640Swnj 	 */
1974640Swnj 	m->m_off += hlen;
1984640Swnj 	m->m_len -= hlen;
1994495Swnj 
2004640Swnj 	/*
2014640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2024640Swnj 	 */
2034640Swnj 	if (fp == 0) {
2044640Swnj 		if ((t = m_get(1)) == NULL)
2054640Swnj 			goto dropfrag;
2064640Swnj 		t->m_off = MMINOFF;
2074640Swnj 		fp = mtod(t, struct ipq *);
2084640Swnj 		insque(fp, &ipq);
2094640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2104640Swnj 		fp->ipq_p = ip->ip_p;
2114640Swnj 		fp->ipq_id = ip->ip_id;
2124898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2134898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2144898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2154640Swnj 	}
2164495Swnj 
2174640Swnj 	/*
2184640Swnj 	 * Find a segment which begins after this one does.
2194640Swnj 	 */
2204898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2214640Swnj 		if (q->ip_off > ip->ip_off)
2224640Swnj 			break;
2234495Swnj 
2244640Swnj 	/*
2254640Swnj 	 * If there is a preceding segment, it may provide some of
2264640Swnj 	 * our data already.  If so, drop the data from the incoming
2274640Swnj 	 * segment.  If it provides all of our data, drop us.
2284640Swnj 	 */
2294898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2304898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2314640Swnj 		if (i > 0) {
2324640Swnj 			if (i >= ip->ip_len)
2334640Swnj 				goto dropfrag;
2344640Swnj 			m_adj(dtom(ip), i);
2354640Swnj 			ip->ip_off += i;
2364640Swnj 			ip->ip_len -= i;
2374640Swnj 		}
2384640Swnj 	}
2394543Swnj 
2404640Swnj 	/*
2414640Swnj 	 * While we overlap succeeding segments trim them or,
2424640Swnj 	 * if they are completely covered, dequeue them.
2434640Swnj 	 */
2444898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2454640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2464640Swnj 		if (i < q->ip_len) {
2474640Swnj 			q->ip_len -= i;
2484640Swnj 			m_adj(dtom(q), i);
2494640Swnj 			break;
2504495Swnj 		}
2514898Swnj 		q = q->ipf_next;
2524898Swnj 		m_freem(dtom(q->ipf_prev));
2534898Swnj 		ip_deq(q->ipf_prev);
2544543Swnj 	}
2554495Swnj 
2564640Swnj 	/*
2574640Swnj 	 * Stick new segment in its place;
2584640Swnj 	 * check for complete reassembly.
2594640Swnj 	 */
2604898Swnj 	ip_enq(ip, q->ipf_prev);
2614640Swnj 	next = 0;
2624898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
2634640Swnj 		if (q->ip_off != next)
2644640Swnj 			return (0);
2654640Swnj 		next += q->ip_len;
2664640Swnj 	}
2674898Swnj 	if (q->ipf_prev->ipf_mff)
2684640Swnj 		return (0);
2694495Swnj 
2704640Swnj 	/*
2714640Swnj 	 * Reassembly is complete; concatenate fragments.
2724640Swnj 	 */
2734640Swnj 	q = fp->ipq_next;
2744640Swnj 	m = dtom(q);
2754640Swnj 	t = m->m_next;
2764640Swnj 	m->m_next = 0;
2774640Swnj 	m_cat(m, t);
2784898Swnj 	while ((q = q->ipf_next) != (struct ipasfrag *)fp)
2794640Swnj 		m_cat(m, dtom(q));
2804495Swnj 
2814640Swnj 	/*
2824640Swnj 	 * Create header for new ip packet by
2834640Swnj 	 * modifying header of first packet;
2844640Swnj 	 * dequeue and discard fragment reassembly header.
2854640Swnj 	 * Make header visible.
2864640Swnj 	 */
2874640Swnj 	ip = fp->ipq_next;
2884640Swnj 	ip->ip_len = next;
2894898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
2904898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
2914640Swnj 	remque(fp);
2924907Swnj 	(void) m_free(dtom(fp));
2934640Swnj 	m = dtom(ip);
2944898Swnj 	m->m_len += sizeof (struct ipasfrag);
2954898Swnj 	m->m_off -= sizeof (struct ipasfrag);
2964898Swnj 	return ((struct ip *)ip);
2974495Swnj 
2984640Swnj dropfrag:
2994640Swnj 	m_freem(m);
3004640Swnj 	return (0);
3014495Swnj }
3024495Swnj 
3034640Swnj /*
3044640Swnj  * Free a fragment reassembly header and all
3054640Swnj  * associated datagrams.
3064640Swnj  */
3074640Swnj struct ipq *
3084640Swnj ip_freef(fp)
3094640Swnj 	struct ipq *fp;
3104495Swnj {
3114898Swnj 	register struct ipasfrag *q;
3124640Swnj 	struct mbuf *m;
3134951Swnj COUNT(IP_FREEF);
3144495Swnj 
3154898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3164640Swnj 		m_freem(dtom(q));
3174640Swnj 	m = dtom(fp);
3184640Swnj 	fp = fp->next;
3194640Swnj 	remque(fp->prev);
3204907Swnj 	(void) m_free(m);
3214640Swnj 	return (fp);
3224495Swnj }
3234495Swnj 
3244640Swnj /*
3254640Swnj  * Put an ip fragment on a reassembly chain.
3264640Swnj  * Like insque, but pointers in middle of structure.
3274640Swnj  */
3284640Swnj ip_enq(p, prev)
3294898Swnj 	register struct ipasfrag *p, *prev;
3304495Swnj {
3314951Swnj 
3324640Swnj COUNT(IP_ENQ);
3334898Swnj 	p->ipf_prev = prev;
3344898Swnj 	p->ipf_next = prev->ipf_next;
3354898Swnj 	prev->ipf_next->ipf_prev = p;
3364898Swnj 	prev->ipf_next = p;
3374495Swnj }
3384495Swnj 
3394640Swnj /*
3404640Swnj  * To ip_enq as remque is to insque.
3414640Swnj  */
3424640Swnj ip_deq(p)
3434898Swnj 	register struct ipasfrag *p;
3444640Swnj {
3454951Swnj 
3464640Swnj COUNT(IP_DEQ);
3474898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3484898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3494495Swnj }
3504495Swnj 
3514640Swnj /*
3524640Swnj  * IP timer processing;
3534640Swnj  * if a timer expires on a reassembly
3544640Swnj  * queue, discard it.
3554640Swnj  */
3564801Swnj ip_slowtimo()
3574495Swnj {
3584495Swnj 	register struct ipq *fp;
3594640Swnj 	int s = splnet();
3604951Swnj 
3614801Swnj COUNT(IP_SLOWTIMO);
3624644Swnj 	for (fp = ipq.next; fp != &ipq; )
3634640Swnj 		if (--fp->ipq_ttl == 0)
3644640Swnj 			fp = ip_freef(fp);
3654640Swnj 		else
3664640Swnj 			fp = fp->next;
3674640Swnj 	splx(s);
3684495Swnj }
3694495Swnj 
3704951Swnj /*
3714951Swnj  * Drain off all datagram fragments.
3724951Swnj  */
3734801Swnj ip_drain()
3744801Swnj {
3754801Swnj 
3764951Swnj COUNT(IP_DRAIN);
3774951Swnj 	while (ipq.next != &ipq)
3784951Swnj 		(void) ip_freef(ipq.next);
3794801Swnj }
3804923Swnj 
3814640Swnj /*
3824640Swnj  * Do option processing on a datagram,
3834640Swnj  * possibly discarding it if bad options
3844640Swnj  * are encountered.
3854640Swnj  */
3864640Swnj ip_dooptions(ip)
3874640Swnj 	struct ip *ip;
3884495Swnj {
3894640Swnj 	register u_char *cp;
3904907Swnj 	int opt, optlen, cnt;
3914923Swnj 	struct in_addr *sin;
3924801Swnj 	register struct ip_timestamp *ipt;
3934951Swnj 	register struct ifnet *ifp;
3944951Swnj 	struct in_addr t;
3954495Swnj 
3964951Swnj COUNT(IP_DOOPTIONS);
3974640Swnj 	cp = (u_char *)(ip + 1);
3984640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
3994640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4004640Swnj 		opt = cp[0];
4014640Swnj 		if (opt == IPOPT_EOL)
4024640Swnj 			break;
4034640Swnj 		if (opt == IPOPT_NOP)
4044640Swnj 			optlen = 1;
4054640Swnj 		else
4064640Swnj 			optlen = cp[1];
4074640Swnj 		switch (opt) {
4084495Swnj 
4094640Swnj 		default:
4104640Swnj 			break;
4114495Swnj 
4124951Swnj 		/*
4134951Swnj 		 * Source routing with record.
4144951Swnj 		 * Find interface with current destination address.
4154951Swnj 		 * If none on this machine then drop if strictly routed,
4164951Swnj 		 * or do nothing if loosely routed.
4174951Swnj 		 * Record interface address and bring up next address
4184951Swnj 		 * component.  If strictly routed make sure next
4194951Swnj 		 * address on directly accessible net.
4204951Swnj 		 */
4214640Swnj 		case IPOPT_LSRR:
4224801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4234640Swnj 				break;
4244923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4254951Swnj 			ifp = if_ifwithaddr(*sin);
4264951Swnj 			if (ifp == 0) {
4274951Swnj 				if (opt == IPOPT_SSRR)
4284951Swnj 					goto bad;
4294951Swnj 				break;
4304640Swnj 			}
4314951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4324951Swnj 			cp[2] += 4;
4334951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4344951Swnj 				break;
4354951Swnj 			ip->ip_dst = sin[1];
4364951Swnj 			if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0)
4374951Swnj 				goto bad;
4384640Swnj 			break;
4394495Swnj 
4404640Swnj 		case IPOPT_TS:
4414801Swnj 			ipt = (struct ip_timestamp *)cp;
4424801Swnj 			if (ipt->ipt_len < 5)
4434640Swnj 				goto bad;
4444801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
4454801Swnj 				if (++ipt->ipt_oflw == 0)
4464640Swnj 					goto bad;
4474495Swnj 				break;
4484640Swnj 			}
4494923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
4504801Swnj 			switch (ipt->ipt_flg) {
4514495Swnj 
4524640Swnj 			case IPOPT_TS_TSONLY:
4534640Swnj 				break;
4544640Swnj 
4554640Swnj 			case IPOPT_TS_TSANDADDR:
4564801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4574640Swnj 					goto bad;
4584951Swnj 				/* stamp with ``first'' interface address */
4594951Swnj 				*sin++ = ifnet->if_addr;
4604640Swnj 				break;
4614640Swnj 
4624640Swnj 			case IPOPT_TS_PRESPEC:
4634951Swnj 				if (if_ifwithaddr(*sin) == 0)
4644951Swnj 					continue;
4654801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4664640Swnj 					goto bad;
4674801Swnj 				ipt->ipt_ptr += 4;
4684640Swnj 				break;
4694640Swnj 
4704495Swnj 			default:
4714640Swnj 				goto bad;
4724495Swnj 			}
4734923Swnj 			*(n_time *)sin = iptime();
4744801Swnj 			ipt->ipt_ptr += 4;
4754640Swnj 		}
4764495Swnj 	}
4774907Swnj 	return;
4784640Swnj bad:
4794640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
4804907Swnj 	return;
4814495Swnj }
4824495Swnj 
4834640Swnj /*
4844951Swnj  * Strip out IP options, at higher
4854951Swnj  * level protocol in the kernel.
4864951Swnj  * Second argument is buffer to which options
4874951Swnj  * will be moved, and return value is their length.
4884640Swnj  */
4894951Swnj ip_stripoptions(ip, cp)
4904640Swnj 	struct ip *ip;
4914951Swnj 	char *cp;
4924495Swnj {
4934640Swnj 	register int i;
4944640Swnj 	register struct mbuf *m;
4954640Swnj 	int olen;
4964951Swnj COUNT(IP_STRIPOPTIONS);
4974640Swnj 
4984640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
4994951Swnj 	m = dtom(ip);
5004951Swnj 	ip++;
5014951Swnj 	if (cp)
5024951Swnj 		bcopy((caddr_t)ip, cp, (unsigned)olen);
5034640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5044907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5054640Swnj 	m->m_len -= i;
5064495Swnj }
507