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