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