xref: /csrg-svn/sys/netinet/ip_input.c (revision 4801)
1*4801Swnj /* ip_input.c 1.12 81/11/08 */
24571Swnj 
34495Swnj #include "../h/param.h"
44543Swnj #include "../h/systm.h"
54640Swnj #include "../h/clock.h"
64640Swnj #include "../h/mbuf.h"
7*4801Swnj #include "../net/inet_cksum.h"
8*4801Swnj #include "../net/inet.h"
9*4801Swnj #include "../net/inet_systm.h"
10*4801Swnj #include "../net/imp.h"
11*4801Swnj #include "../net/ip.h"			/* belongs before inet.h */
12*4801Swnj #include "../net/ip_icmp.h"
13*4801Swnj #include "../net/tcp.h"
144495Swnj 
15*4801Swnj /*
16*4801Swnj  * Ip initialization.
17*4801Swnj  */
18*4801Swnj ip_init()
19*4801Swnj {
204495Swnj 
21*4801Swnj 	ipq.next = ipq.prev = &ipq;
22*4801Swnj 	ip_id = time & 0xffff;
23*4801Swnj }
24*4801Swnj 
25*4801Swnj int	ipcksum = 1;
26*4801Swnj 
274640Swnj struct	ip *ip_reass();
284640Swnj 
294640Swnj /*
304640Swnj  * Ip input routines.
314640Swnj  */
324640Swnj 
334640Swnj /*
344640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
354640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
364640Swnj  * Process options.  Pass to next level.
374640Swnj  */
384640Swnj ip_input(m0)
394640Swnj 	struct mbuf *m0;
404495Swnj {
414689Swnj 	register struct ip *ip;		/* known to be r11 in CKSUM below */
424689Swnj 	register struct mbuf *m = m0;
434640Swnj 	register int i;
444689Swnj 	register struct ipq *q;
454495Swnj 	register struct ipq *fp;
464495Swnj 	int hlen;
474495Swnj 
484495Swnj COUNT(IP_INPUT);
494640Swnj 	/*
504640Swnj 	 * Check header and byteswap.
514640Swnj 	 */
524640Swnj 	ip = mtod(m, struct ip *);
534640Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
544640Swnj 		printf("ip hdr ovflo\n");
554640Swnj 		m_freem(m);
564495Swnj 		return;
574495Swnj 	}
584689Swnj 	CKSUM_IPCHK(m, ip, r11, hlen);
594689Swnj 	if (ip->ip_sum) {
604689Swnj 		printf("ip_sum %x\n", ip->ip_sum);
614495Swnj 		netstat.ip_badsum++;
62*4801Swnj 		if (ipcksum) {
634640Swnj 			m_freem(m);
644640Swnj 			return;
654495Swnj 		}
664495Swnj 	}
674640Swnj 	ip->ip_len = ntohs(ip->ip_len);
684640Swnj 	ip->ip_id = ntohs(ip->ip_id);
694640Swnj 	ip->ip_off = ntohs(ip->ip_off);
704495Swnj 
714543Swnj 	/*
724640Swnj 	 * Check that the amount of data in the buffers
734640Swnj 	 * is as at least much as the IP header would have us expect.
744640Swnj 	 * Trim mbufs if longer than we expect.
754640Swnj 	 * Drop packet if shorter than we expect.
764543Swnj 	 */
774640Swnj 	i = 0;
784640Swnj 	for (; m != NULL; m = m->m_next)
794495Swnj 		i += m->m_len;
804640Swnj 	m = m0;
814640Swnj 	if (i != ip->ip_len) {
824640Swnj 		if (i < ip->ip_len) {
834640Swnj 			printf("ip_input: short packet\n");
844640Swnj 			m_freem(m);
854640Swnj 			return;
864640Swnj 		}
874640Swnj 		m_adj(m, ip->ip_len - i);
884495Swnj 	}
894495Swnj 
904640Swnj 	/*
914640Swnj 	 * Process options and, if not destined for us,
924640Swnj 	 * ship it on.
934640Swnj 	 */
944543Swnj 	if (hlen > sizeof (struct ip))
954640Swnj 		ip_dooptions(ip, hlen);
964640Swnj 	if (ip->ip_dst.s_addr != n_lhost.s_addr) {
974640Swnj 		if (--ip->ip_ttl == 0) {
984640Swnj 			icmp_error(ip, ICMP_TIMXCEED);
994543Swnj 			return;
1004495Swnj 		}
1014640Swnj 		ip_output(dtom(ip));
1024640Swnj 		return;
1034543Swnj 	}
1044495Swnj 
1054640Swnj 	/*
1064640Swnj 	 * Look for queue of fragments
1074640Swnj 	 * of this datagram.
1084640Swnj 	 */
1094640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1104640Swnj 		if (ip->ip_id == fp->ipq_id &&
1114640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1124640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1134640Swnj 		    ip->ip_p == fp->ipq_p)
1144640Swnj 			goto found;
1154640Swnj 	fp = 0;
1164640Swnj found:
1174495Swnj 
1184640Swnj 	/*
1194640Swnj 	 * Adjust ip_len to not reflect header,
1204640Swnj 	 * set ip_mff if more fragments are expected,
1214640Swnj 	 * convert offset of this to bytes.
1224640Swnj 	 */
1234640Swnj 	ip->ip_len -= hlen;
1244640Swnj 	ip->ip_mff = 0;
1254640Swnj 	if (ip->ip_off & IP_MF)
1264640Swnj 		ip->ip_mff = 1;
1274640Swnj 	ip->ip_off <<= 3;
1284495Swnj 
1294640Swnj 	/*
1304640Swnj 	 * If datagram marked as having more fragments
1314640Swnj 	 * or if this is not the first fragment,
1324640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1334640Swnj 	 */
1344640Swnj 	if (ip->ip_mff || ip->ip_off) {
1354640Swnj 		ip = ip_reass(ip, fp);
1364640Swnj 		if (ip == 0)
1374640Swnj 			return;
1384640Swnj 		hlen = ip->ip_hl << 2;
1394640Swnj 		m = dtom(ip);
1404640Swnj 	} else
1414640Swnj 		if (fp)
1424640Swnj 			(void) ip_freef(fp);
1434495Swnj 
1444640Swnj 	/*
1454640Swnj 	 * Switch out to protocol specific routine.
1464640Swnj 	 * SHOULD GO THROUGH PROTOCOL SWITCH TABLE
1474640Swnj 	 */
1484640Swnj 	switch (ip->ip_p) {
1494495Swnj 
1504640Swnj 	case IPPROTO_ICMP:
1514640Swnj 		icmp_input(m);
1524640Swnj 		break;
1534495Swnj 
1544640Swnj 	case IPPROTO_TCP:
1554640Swnj 		if (hlen > sizeof (struct ip))
1564640Swnj 			ip_stripoptions(ip, hlen);
1574640Swnj 		tcp_input(m);
1584640Swnj 		break;
1594495Swnj 
1604640Swnj 	case IPPROTO_UDP:
1614640Swnj 		if (hlen > sizeof (struct ip))
1624640Swnj 			ip_stripoptions(ip, hlen);
1634640Swnj 		udp_input(m);
1644640Swnj 		break;
1654495Swnj 
1664640Swnj 	default:
167*4801Swnj 		ri_input(m);
1684640Swnj 		break;
1694640Swnj 	}
1704640Swnj }
1714495Swnj 
1724640Swnj /*
1734640Swnj  * Take incoming datagram fragment and try to
1744640Swnj  * reassamble it into whole datagram.  If a chain for
1754640Swnj  * reassembly of this datagram already exists, then it
1764640Swnj  * is given as fp; otherwise have to make a chain.
1774640Swnj  */
1784640Swnj struct ip *
1794640Swnj ip_reass(ip, fp)
1804640Swnj 	register struct ip *ip;
1814640Swnj 	register struct ipq *fp;
1824640Swnj {
1834640Swnj 	register struct mbuf *m = dtom(ip);
1844640Swnj 	register struct ip *q;
1854640Swnj 	struct mbuf *t;
1864640Swnj 	int hlen = ip->ip_hl << 2;
1874640Swnj 	int i, next;
1884543Swnj 
1894640Swnj 	/*
1904640Swnj 	 * Presence of header sizes in mbufs
1914640Swnj 	 * would confuse code below.
1924640Swnj 	 */
1934640Swnj 	m->m_off += hlen;
1944640Swnj 	m->m_len -= hlen;
1954495Swnj 
1964640Swnj 	/*
1974640Swnj 	 * If first fragment to arrive, create a reassembly queue.
1984640Swnj 	 */
1994640Swnj 	if (fp == 0) {
2004640Swnj 		if ((t = m_get(1)) == NULL)
2014640Swnj 			goto dropfrag;
2024640Swnj 		t->m_off = MMINOFF;
2034640Swnj 		fp = mtod(t, struct ipq *);
2044640Swnj 		insque(fp, &ipq);
2054640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2064640Swnj 		fp->ipq_p = ip->ip_p;
2074640Swnj 		fp->ipq_id = ip->ip_id;
2084640Swnj 		fp->ipq_next = fp->ipq_prev = (struct ip *)fp;
2094640Swnj 		fp->ipq_src = ip->ip_src;
2104640Swnj 		fp->ipq_dst = ip->ip_dst;
2114640Swnj 	}
2124495Swnj 
2134640Swnj 	/*
2144640Swnj 	 * Find a segment which begins after this one does.
2154640Swnj 	 */
2164640Swnj 	for (q = fp->ipq_next; q != (struct ip *)fp; q = q->ip_next)
2174640Swnj 		if (q->ip_off > ip->ip_off)
2184640Swnj 			break;
2194495Swnj 
2204640Swnj 	/*
2214640Swnj 	 * If there is a preceding segment, it may provide some of
2224640Swnj 	 * our data already.  If so, drop the data from the incoming
2234640Swnj 	 * segment.  If it provides all of our data, drop us.
2244640Swnj 	 */
2254640Swnj 	if (q->ip_prev != (struct ip *)fp) {
2264640Swnj 		i = q->ip_prev->ip_off + q->ip_prev->ip_len - ip->ip_off;
2274640Swnj 		if (i > 0) {
2284640Swnj 			if (i >= ip->ip_len)
2294640Swnj 				goto dropfrag;
2304640Swnj 			m_adj(dtom(ip), i);
2314640Swnj 			ip->ip_off += i;
2324640Swnj 			ip->ip_len -= i;
2334640Swnj 		}
2344640Swnj 	}
2354543Swnj 
2364640Swnj 	/*
2374640Swnj 	 * While we overlap succeeding segments trim them or,
2384640Swnj 	 * if they are completely covered, dequeue them.
2394640Swnj 	 */
2404640Swnj 	while (q != (struct ip *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2414640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2424640Swnj 		if (i < q->ip_len) {
2434640Swnj 			q->ip_len -= i;
2444640Swnj 			m_adj(dtom(q), i);
2454640Swnj 			break;
2464495Swnj 		}
2474640Swnj 		q = q->ip_next;
2484640Swnj 		m_freem(dtom(q->ip_prev));
2494640Swnj 		ip_deq(q->ip_prev);
2504543Swnj 	}
2514495Swnj 
2524640Swnj 	/*
2534640Swnj 	 * Stick new segment in its place;
2544640Swnj 	 * check for complete reassembly.
2554640Swnj 	 */
2564640Swnj 	ip_enq(ip, q->ip_prev);
2574640Swnj 	next = 0;
2584640Swnj 	for (q = fp->ipq_next; q != (struct ip *)fp; q = q->ip_next) {
2594640Swnj 		if (q->ip_off != next)
2604640Swnj 			return (0);
2614640Swnj 		next += q->ip_len;
2624640Swnj 	}
2634640Swnj 	if (q->ip_prev->ip_mff)
2644640Swnj 		return (0);
2654495Swnj 
2664640Swnj 	/*
2674640Swnj 	 * Reassembly is complete; concatenate fragments.
2684640Swnj 	 */
2694640Swnj 	q = fp->ipq_next;
2704640Swnj 	m = dtom(q);
2714640Swnj 	t = m->m_next;
2724640Swnj 	m->m_next = 0;
2734640Swnj 	m_cat(m, t);
2744640Swnj 	while ((q = q->ip_next) != (struct ip *)fp)
2754640Swnj 		m_cat(m, dtom(q));
2764495Swnj 
2774640Swnj 	/*
2784640Swnj 	 * Create header for new ip packet by
2794640Swnj 	 * modifying header of first packet;
2804640Swnj 	 * dequeue and discard fragment reassembly header.
2814640Swnj 	 * Make header visible.
2824640Swnj 	 */
2834640Swnj 	ip = fp->ipq_next;
2844640Swnj 	ip->ip_len = next;
2854640Swnj 	ip->ip_src = fp->ipq_src;
2864640Swnj 	ip->ip_dst = fp->ipq_dst;
2874640Swnj 	remque(fp);
2884640Swnj 	m_free(dtom(fp));
2894640Swnj 	m = dtom(ip);
2904640Swnj 	m->m_len += sizeof (struct ip);
2914640Swnj 	m->m_off -= sizeof (struct ip);
2924640Swnj 	return (ip);
2934495Swnj 
2944640Swnj dropfrag:
2954640Swnj 	m_freem(m);
2964640Swnj 	return (0);
2974495Swnj }
2984495Swnj 
2994640Swnj /*
3004640Swnj  * Free a fragment reassembly header and all
3014640Swnj  * associated datagrams.
3024640Swnj  */
3034640Swnj struct ipq *
3044640Swnj ip_freef(fp)
3054640Swnj 	struct ipq *fp;
3064495Swnj {
3074495Swnj 	register struct ip *q;
3084640Swnj 	struct mbuf *m;
3094495Swnj 
3104640Swnj 	for (q = fp->ipq_next; q != (struct ip *)fp; q = q->ip_next)
3114640Swnj 		m_freem(dtom(q));
3124640Swnj 	m = dtom(fp);
3134640Swnj 	fp = fp->next;
3144640Swnj 	remque(fp->prev);
3154640Swnj 	m_free(m);
3164640Swnj 	return (fp);
3174495Swnj }
3184495Swnj 
3194640Swnj /*
3204640Swnj  * Put an ip fragment on a reassembly chain.
3214640Swnj  * Like insque, but pointers in middle of structure.
3224640Swnj  */
3234640Swnj ip_enq(p, prev)
3244640Swnj 	register struct ip *p;
3254640Swnj 	register struct ip *prev;
3264495Swnj {
3274640Swnj COUNT(IP_ENQ);
3284495Swnj 
3294640Swnj 	p->ip_prev = prev;
3304640Swnj 	p->ip_next = prev->ip_next;
3314640Swnj 	prev->ip_next->ip_prev = p;
3324640Swnj 	prev->ip_next = p;
3334495Swnj }
3344495Swnj 
3354640Swnj /*
3364640Swnj  * To ip_enq as remque is to insque.
3374640Swnj  */
3384640Swnj ip_deq(p)
3394640Swnj 	register struct ip *p;
3404640Swnj {
3414640Swnj COUNT(IP_DEQ);
3424495Swnj 
3434640Swnj 	p->ip_prev->ip_next = p->ip_next;
3444640Swnj 	p->ip_next->ip_prev = p->ip_prev;
3454495Swnj }
3464495Swnj 
3474640Swnj /*
3484640Swnj  * IP timer processing;
3494640Swnj  * if a timer expires on a reassembly
3504640Swnj  * queue, discard it.
3514640Swnj  */
352*4801Swnj ip_slowtimo()
3534495Swnj {
3544640Swnj 	register struct ip *q;
3554495Swnj 	register struct ipq *fp;
3564640Swnj 	int s = splnet();
357*4801Swnj COUNT(IP_SLOWTIMO);
3584495Swnj 
3594644Swnj 	for (fp = ipq.next; fp != &ipq; )
3604640Swnj 		if (--fp->ipq_ttl == 0)
3614640Swnj 			fp = ip_freef(fp);
3624640Swnj 		else
3634640Swnj 			fp = fp->next;
3644640Swnj 	splx(s);
3654495Swnj }
3664495Swnj 
367*4801Swnj ip_drain()
368*4801Swnj {
369*4801Swnj 
370*4801Swnj }
3714640Swnj /*
3724640Swnj  * Do option processing on a datagram,
3734640Swnj  * possibly discarding it if bad options
3744640Swnj  * are encountered.
3754640Swnj  */
3764640Swnj ip_dooptions(ip)
3774640Swnj 	struct ip *ip;
3784495Swnj {
3794640Swnj 	register u_char *cp;
3804640Swnj 	int opt, optlen, cnt, s;
381*4801Swnj 	struct ip_addr *sp;
382*4801Swnj 	register struct ip_timestamp *ipt;
383*4801Swnj 	int x;
3844495Swnj 
3854640Swnj 	cp = (u_char *)(ip + 1);
3864640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
3874640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3884640Swnj 		opt = cp[0];
3894640Swnj 		if (opt == IPOPT_EOL)
3904640Swnj 			break;
3914640Swnj 		if (opt == IPOPT_NOP)
3924640Swnj 			optlen = 1;
3934640Swnj 		else
3944640Swnj 			optlen = cp[1];
3954640Swnj 		switch (opt) {
3964495Swnj 
3974640Swnj 		default:
3984640Swnj 			break;
3994495Swnj 
4004640Swnj 		case IPOPT_LSRR:
4014640Swnj 		case IPOPT_SSRR:
402*4801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4034640Swnj 				break;
404*4801Swnj 			sp = (struct ip_addr *)(cp + cp[2]);
4054640Swnj 			if (n_lhost.s_addr == *(u_long *)sp) {
4064640Swnj 				if (opt == IPOPT_SSRR) {
407*4801Swnj 					/* MAKE SURE *SP DIRECTLY ACCESSIBLE */
4084640Swnj 				}
4094640Swnj 				ip->ip_dst = *sp;
4104640Swnj 				*sp = n_lhost;
4114640Swnj 				cp[2] += 4;
4124640Swnj 			}
4134640Swnj 			break;
4144495Swnj 
4154640Swnj 		case IPOPT_TS:
416*4801Swnj 			ipt = (struct ip_timestamp *)cp;
417*4801Swnj 			if (ipt->ipt_len < 5)
4184640Swnj 				goto bad;
419*4801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
420*4801Swnj 				if (++ipt->ipt_oflw == 0)
4214640Swnj 					goto bad;
4224495Swnj 				break;
4234640Swnj 			}
424*4801Swnj 			sp = (struct ip_addr *)(cp+cp[2]);
425*4801Swnj 			switch (ipt->ipt_flg) {
4264495Swnj 
4274640Swnj 			case IPOPT_TS_TSONLY:
4284640Swnj 				break;
4294640Swnj 
4304640Swnj 			case IPOPT_TS_TSANDADDR:
431*4801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4324640Swnj 					goto bad;
433*4801Swnj 				*(struct ip_addr *)sp++ = n_lhost;
4344640Swnj 				break;
4354640Swnj 
4364640Swnj 			case IPOPT_TS_PRESPEC:
4374640Swnj 				if (*(u_long *)sp != n_lhost.s_addr)
4384640Swnj 					break;
439*4801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4404640Swnj 					goto bad;
441*4801Swnj 				ipt->ipt_ptr += 4;
4424640Swnj 				break;
4434640Swnj 
4444495Swnj 			default:
4454640Swnj 				goto bad;
4464495Swnj 			}
447*4801Swnj 			*(n_time *)sp = ip_time();
448*4801Swnj 			ipt->ipt_ptr += 4;
4494640Swnj 		}
4504495Swnj 	}
4514640Swnj 	return (0);
4524640Swnj bad:
4534640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
4544640Swnj 	return (-1);
4554495Swnj }
4564495Swnj 
4574640Swnj /*
4584640Swnj  * Strip out IP options, e.g. before passing
4594640Swnj  * to higher level protocol in the kernel.
4604640Swnj  */
4614640Swnj ip_stripoptions(ip)
4624640Swnj 	struct ip *ip;
4634495Swnj {
4644640Swnj 	register int i;
4654640Swnj 	register struct mbuf *m;
4664640Swnj 	char *op;
4674640Swnj 	int olen;
4684640Swnj COUNT(IP_OPT);
4694640Swnj 
4704640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
4714640Swnj 	op = (caddr_t)ip + olen;
4724640Swnj 	m = dtom(++ip);
4734640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
4744640Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, i);
4754640Swnj 	m->m_len -= i;
4764495Swnj }
477