xref: /original-bsd/sys/netinet/ip_input.c (revision f662c168)
1 /*	ip_input.c	1.33	82/03/19	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/clock.h"
6 #include "../h/mbuf.h"
7 #include "../h/protosw.h"
8 #include "../h/socket.h"
9 #include "../net/in.h"
10 #include "../net/in_systm.h"
11 #include "../net/if.h"
12 #include "../net/ip.h"			/* belongs before in.h */
13 #include "../net/ip_var.h"
14 #include "../net/ip_icmp.h"
15 #include "../net/tcp.h"
16 
17 u_char	ip_protox[IPPROTO_MAX];
18 int	ipqmaxlen = IFQ_MAXLEN;
19 
20 /*
21  * IP initialization: fill in IP protocol switch table.
22  * All protocols not implemented in kernel go to raw IP protocol handler.
23  */
24 ip_init()
25 {
26 	register struct protosw *pr;
27 	register int i;
28 
29 COUNT(IP_INIT);
30 	pr = pffindproto(PF_INET, IPPROTO_RAW);
31 	if (pr == 0)
32 		panic("ip_init");
33 	for (i = 0; i < IPPROTO_MAX; i++)
34 		ip_protox[i] = pr - protosw;
35 	for (pr = protosw; pr <= protoswLAST; pr++)
36 		if (pr->pr_family == PF_INET &&
37 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
38 			ip_protox[pr->pr_protocol] = pr - protosw;
39 	ipq.next = ipq.prev = &ipq;
40 	ip_id = time & 0xffff;
41 	ipintrq.ifq_maxlen = ipqmaxlen;
42 }
43 
44 u_char	ipcksum = 1;
45 struct	ip *ip_reass();
46 
47 /*
48  * Ip input routine.  Checksum and byte swap header.  If fragmented
49  * try to reassamble.  If complete and fragment queue exists, discard.
50  * Process options.  Pass to next level.
51  */
52 ipintr()
53 {
54 	register struct ip *ip;
55 	register struct mbuf *m;
56 	struct mbuf *m0, *mopt;
57 	register int i;
58 	register struct ipq *fp;
59 	int hlen, s;
60 
61 COUNT(IPINTR);
62 next:
63 	/*
64 	 * Get next datagram off input queue and get IP header
65 	 * in first mbuf.
66 	 */
67 	s = splimp();
68 	IF_DEQUEUE(&ipintrq, m);
69 	splx(s);
70 	if (m == 0)
71 		return;
72 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) &&
73 	    (m = m_pullup(m, sizeof (struct ip))) == 0)
74 		return;
75 	ip = mtod(m, struct ip *);
76 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
77 		if ((m = m_pullup(m, hlen)) == 0)
78 			return;
79 		ip = mtod(m, struct ip *);
80 	}
81 	if (ipcksum)
82 		if (ip->ip_sum = in_cksum(m, hlen)) {
83 			printf("ip_sum %x\n", ip->ip_sum);	/* XXX */
84 			ipstat.ips_badsum++;
85 			goto bad;
86 		}
87 
88 #if vax
89 	/*
90 	 * Convert fields to host representation.
91 	 */
92 	ip->ip_len = ntohs((u_short)ip->ip_len);
93 	ip->ip_id = ntohs(ip->ip_id);
94 	ip->ip_off = ntohs((u_short)ip->ip_off);
95 #endif
96 
97 	/*
98 	 * Check that the amount of data in the buffers
99 	 * is as at least much as the IP header would have us expect.
100 	 * Trim mbufs if longer than we expect.
101 	 * Drop packet if shorter than we expect.
102 	 */
103 	i = 0;
104 	m0 = m;
105 	for (; m != NULL; m = m->m_next) {
106 		if (m->m_free) panic("ipinput already free");
107 		i += m->m_len;
108 	}
109 	m = m0;
110 	if (i != ip->ip_len) {
111 		if (i < ip->ip_len) {
112 			ipstat.ips_tooshort++;
113 			goto bad;
114 		}
115 		m_adj(m, ip->ip_len - i);
116 	}
117 
118 	/*
119 	 * Process options and, if not destined for us,
120 	 * ship it on.
121 	 */
122 	if (hlen > sizeof (struct ip))
123 		ip_dooptions(ip);
124 	if (ifnet && ip->ip_dst.s_addr != ifnet->if_addr.s_addr &&
125 	    if_ifwithaddr(ip->ip_dst) == 0) {
126 
127 		goto bad;
128 #ifdef notdef
129 		printf("ip->ip_dst %x ip->ip_ttl %x\n",
130 		    ip->ip_dst, ip->ip_ttl);
131 		if (--ip->ip_ttl == 0) {
132 			icmp_error(ip, ICMP_TIMXCEED, 0);
133 			goto next;
134 		}
135 		mopt = m_get(M_DONTWAIT);
136 		if (mopt == 0)
137 			goto bad;
138 		ip_stripoptions(ip, mopt);
139 		/* 0 here means no directed broadcast */
140 		(void) ip_output(m0, mopt, 0);
141 		goto next;
142 #endif
143 	}
144 
145 	/*
146 	 * Look for queue of fragments
147 	 * of this datagram.
148 	 */
149 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
150 		if (ip->ip_id == fp->ipq_id &&
151 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
152 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
153 		    ip->ip_p == fp->ipq_p)
154 			goto found;
155 	fp = 0;
156 found:
157 
158 	/*
159 	 * Adjust ip_len to not reflect header,
160 	 * set ip_mff if more fragments are expected,
161 	 * convert offset of this to bytes.
162 	 */
163 	ip->ip_len -= hlen;
164 	((struct ipasfrag *)ip)->ipf_mff = 0;
165 	if (ip->ip_off & IP_MF)
166 		((struct ipasfrag *)ip)->ipf_mff = 1;
167 	ip->ip_off <<= 3;
168 
169 	/*
170 	 * If datagram marked as having more fragments
171 	 * or if this is not the first fragment,
172 	 * attempt reassembly; if it succeeds, proceed.
173 	 */
174 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
175 		ip = ip_reass((struct ipasfrag *)ip, fp);
176 		if (ip == 0)
177 			goto next;
178 		hlen = ip->ip_hl << 2;
179 		m = dtom(ip);
180 	} else
181 		if (fp)
182 			(void) ip_freef(fp);
183 
184 	/*
185 	 * Switch out to protocol's input routine.
186 	 */
187 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
188 	goto next;
189 bad:
190 	m_freem(m);
191 	goto next;
192 }
193 
194 /*
195  * Take incoming datagram fragment and try to
196  * reassemble it into whole datagram.  If a chain for
197  * reassembly of this datagram already exists, then it
198  * is given as fp; otherwise have to make a chain.
199  */
200 struct ip *
201 ip_reass(ip, fp)
202 	register struct ipasfrag *ip;
203 	register struct ipq *fp;
204 {
205 	register struct mbuf *m = dtom(ip);
206 	register struct ipasfrag *q;
207 	struct mbuf *t;
208 	int hlen = ip->ip_hl << 2;
209 	int i, next;
210 COUNT(IP_REASS);
211 
212 	/*
213 	 * Presence of header sizes in mbufs
214 	 * would confuse code below.
215 	 */
216 	m->m_off += hlen;
217 	m->m_len -= hlen;
218 
219 	/*
220 	 * If first fragment to arrive, create a reassembly queue.
221 	 */
222 	if (fp == 0) {
223 		if ((t = m_get(M_WAIT)) == NULL)
224 			goto dropfrag;
225 		t->m_off = MMINOFF;
226 		fp = mtod(t, struct ipq *);
227 		insque(fp, &ipq);
228 		fp->ipq_ttl = IPFRAGTTL;
229 		fp->ipq_p = ip->ip_p;
230 		fp->ipq_id = ip->ip_id;
231 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
232 		fp->ipq_src = ((struct ip *)ip)->ip_src;
233 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
234 		q = (struct ipasfrag *)fp;
235 		goto insert;
236 	}
237 
238 	/*
239 	 * Find a segment which begins after this one does.
240 	 */
241 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
242 		if (q->ip_off > ip->ip_off)
243 			break;
244 
245 	/*
246 	 * If there is a preceding segment, it may provide some of
247 	 * our data already.  If so, drop the data from the incoming
248 	 * segment.  If it provides all of our data, drop us.
249 	 */
250 	if (q->ipf_prev != (struct ipasfrag *)fp) {
251 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
252 		if (i > 0) {
253 			if (i >= ip->ip_len)
254 				goto dropfrag;
255 			m_adj(dtom(ip), i);
256 			ip->ip_off += i;
257 			ip->ip_len -= i;
258 		}
259 	}
260 
261 	/*
262 	 * While we overlap succeeding segments trim them or,
263 	 * if they are completely covered, dequeue them.
264 	 */
265 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
266 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
267 		if (i < q->ip_len) {
268 			q->ip_len -= i;
269 			q->ip_off += i;
270 			m_adj(dtom(q), i);
271 			break;
272 		}
273 		q = q->ipf_next;
274 		m_freem(dtom(q->ipf_prev));
275 		ip_deq(q->ipf_prev);
276 	}
277 
278 insert:
279 	/*
280 	 * Stick new segment in its place;
281 	 * check for complete reassembly.
282 	 */
283 	ip_enq(ip, q->ipf_prev);
284 	next = 0;
285 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
286 		if (q->ip_off != next)
287 			return (0);
288 		next += q->ip_len;
289 	}
290 	if (q->ipf_prev->ipf_mff)
291 		return (0);
292 
293 	/*
294 	 * Reassembly is complete; concatenate fragments.
295 	 */
296 	q = fp->ipq_next;
297 	m = dtom(q);
298 	t = m->m_next;
299 	m->m_next = 0;
300 	m_cat(m, t);
301 	while ((q = q->ipf_next) != (struct ipasfrag *)fp)
302 		m_cat(m, dtom(q));
303 
304 	/*
305 	 * Create header for new ip packet by
306 	 * modifying header of first packet;
307 	 * dequeue and discard fragment reassembly header.
308 	 * Make header visible.
309 	 */
310 	ip = fp->ipq_next;
311 	ip->ip_len = next;
312 	((struct ip *)ip)->ip_src = fp->ipq_src;
313 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
314 	remque(fp);
315 	(void) m_free(dtom(fp));
316 	m = dtom(ip);
317 	m->m_len += sizeof (struct ipasfrag);
318 	m->m_off -= sizeof (struct ipasfrag);
319 	return ((struct ip *)ip);
320 
321 dropfrag:
322 	m_freem(m);
323 	return (0);
324 }
325 
326 /*
327  * Free a fragment reassembly header and all
328  * associated datagrams.
329  */
330 struct ipq *
331 ip_freef(fp)
332 	struct ipq *fp;
333 {
334 	register struct ipasfrag *q;
335 	struct mbuf *m;
336 COUNT(IP_FREEF);
337 
338 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
339 		m_freem(dtom(q));
340 	m = dtom(fp);
341 	fp = fp->next;
342 	remque(fp->prev);
343 	(void) m_free(m);
344 	return (fp);
345 }
346 
347 /*
348  * Put an ip fragment on a reassembly chain.
349  * Like insque, but pointers in middle of structure.
350  */
351 ip_enq(p, prev)
352 	register struct ipasfrag *p, *prev;
353 {
354 
355 COUNT(IP_ENQ);
356 	p->ipf_prev = prev;
357 	p->ipf_next = prev->ipf_next;
358 	prev->ipf_next->ipf_prev = p;
359 	prev->ipf_next = p;
360 }
361 
362 /*
363  * To ip_enq as remque is to insque.
364  */
365 ip_deq(p)
366 	register struct ipasfrag *p;
367 {
368 
369 COUNT(IP_DEQ);
370 	p->ipf_prev->ipf_next = p->ipf_next;
371 	p->ipf_next->ipf_prev = p->ipf_prev;
372 }
373 
374 /*
375  * IP timer processing;
376  * if a timer expires on a reassembly
377  * queue, discard it.
378  */
379 ip_slowtimo()
380 {
381 	register struct ipq *fp;
382 	int s = splnet();
383 
384 COUNT(IP_SLOWTIMO);
385 	fp = ipq.next;
386 	if (fp == 0) {
387 		splx(s);
388 		return;
389 	}
390 	while (fp != &ipq)
391 		if (--fp->ipq_ttl == 0)
392 			fp = ip_freef(fp);
393 		else
394 			fp = fp->next;
395 	splx(s);
396 }
397 
398 /*
399  * Drain off all datagram fragments.
400  */
401 ip_drain()
402 {
403 
404 COUNT(IP_DRAIN);
405 	while (ipq.next != &ipq)
406 		(void) ip_freef(ipq.next);
407 }
408 
409 /*
410  * Do option processing on a datagram,
411  * possibly discarding it if bad options
412  * are encountered.
413  */
414 ip_dooptions(ip)
415 	struct ip *ip;
416 {
417 	register u_char *cp;
418 	int opt, optlen, cnt;
419 	struct in_addr *sin;
420 	register struct ip_timestamp *ipt;
421 	register struct ifnet *ifp;
422 	struct in_addr t;
423 
424 COUNT(IP_DOOPTIONS);
425 	cp = (u_char *)(ip + 1);
426 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
427 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
428 		opt = cp[0];
429 		if (opt == IPOPT_EOL)
430 			break;
431 		if (opt == IPOPT_NOP)
432 			optlen = 1;
433 		else
434 			optlen = cp[1];
435 		switch (opt) {
436 
437 		default:
438 			break;
439 
440 		/*
441 		 * Source routing with record.
442 		 * Find interface with current destination address.
443 		 * If none on this machine then drop if strictly routed,
444 		 * or do nothing if loosely routed.
445 		 * Record interface address and bring up next address
446 		 * component.  If strictly routed make sure next
447 		 * address on directly accessible net.
448 		 */
449 		case IPOPT_LSRR:
450 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
451 				break;
452 			sin = (struct in_addr *)(cp + cp[2]);
453 			ifp = if_ifwithaddr(*sin);
454 			if (ifp == 0) {
455 				if (opt == IPOPT_SSRR)
456 					goto bad;
457 				break;
458 			}
459 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
460 			cp[2] += 4;
461 			if (cp[2] > optlen - (sizeof (long) - 1))
462 				break;
463 			ip->ip_dst = sin[1];
464 			if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0)
465 				goto bad;
466 			break;
467 
468 		case IPOPT_TS:
469 			ipt = (struct ip_timestamp *)cp;
470 			if (ipt->ipt_len < 5)
471 				goto bad;
472 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
473 				if (++ipt->ipt_oflw == 0)
474 					goto bad;
475 				break;
476 			}
477 			sin = (struct in_addr *)(cp+cp[2]);
478 			switch (ipt->ipt_flg) {
479 
480 			case IPOPT_TS_TSONLY:
481 				break;
482 
483 			case IPOPT_TS_TSANDADDR:
484 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
485 					goto bad;
486 				/* stamp with ``first'' interface address */
487 				*sin++ = ifnet->if_addr;
488 				break;
489 
490 			case IPOPT_TS_PRESPEC:
491 				if (if_ifwithaddr(*sin) == 0)
492 					continue;
493 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
494 					goto bad;
495 				ipt->ipt_ptr += 4;
496 				break;
497 
498 			default:
499 				goto bad;
500 			}
501 			*(n_time *)sin = iptime();
502 			ipt->ipt_ptr += 4;
503 		}
504 	}
505 	return;
506 bad:
507 	/* SHOULD FORCE ICMP MESSAGE */
508 	return;
509 }
510 
511 /*
512  * Strip out IP options, at higher
513  * level protocol in the kernel.
514  * Second argument is buffer to which options
515  * will be moved, and return value is their length.
516  */
517 ip_stripoptions(ip, mopt)
518 	struct ip *ip;
519 	struct mbuf *mopt;
520 {
521 	register int i;
522 	register struct mbuf *m;
523 	int olen;
524 COUNT(IP_STRIPOPTIONS);
525 
526 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
527 	m = dtom(ip);
528 	ip++;
529 	if (mopt) {
530 		mopt->m_len = olen;
531 		mopt->m_off = MMINOFF;
532 		bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen);
533 	}
534 	i = m->m_len - (sizeof (struct ip) + olen);
535 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
536 	m->m_len -= olen;
537 }
538