Commit | Line | Data |
---|---|---|
15637ed4 | 1 | /* |
4cf615e1 JH |
2 | * Copyright (c) 1982, 1986, 1988, 1993 |
3 | * Regents of the University of California. All rights reserved. | |
15637ed4 RG |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
38e82238 | 33 | * from: @(#)ip_input.c 7.19 (Berkeley) 5/25/91 |
4cf615e1 | 34 | * $Id: ip_input.c,v 1.8 1994/01/04 17:47:13 ache Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
38 | #include "systm.h" | |
39 | #include "malloc.h" | |
40 | #include "mbuf.h" | |
41 | #include "domain.h" | |
42 | #include "protosw.h" | |
43 | #include "socket.h" | |
44 | #include "errno.h" | |
45 | #include "time.h" | |
46 | #include "kernel.h" | |
47 | ||
48 | #include "../net/if.h" | |
49 | #include "../net/route.h" | |
50 | ||
51 | #include "in.h" | |
52 | #include "in_systm.h" | |
53 | #include "ip.h" | |
54 | #include "in_pcb.h" | |
55 | #include "in_var.h" | |
56 | #include "ip_var.h" | |
57 | #include "ip_icmp.h" | |
58 | ||
4c45483e GW |
59 | static void ip_freef(struct ipq *); |
60 | static void ip_enq(struct ipasfrag *, struct ipasfrag *); | |
61 | static void ip_deq(struct ipasfrag *); | |
62 | static void save_rte(u_char *, struct in_addr); | |
63 | static void ip_forward(struct mbuf *, int); | |
fde1aeb2 | 64 | static struct ip *ip_reass(struct ipasfrag *, struct ipq *); |
15637ed4 RG |
65 | |
66 | /* | |
67 | * We need to save the IP options in case a protocol wants to respond | |
68 | * to an incoming packet over the same route if the packet got here | |
69 | * using IP source routing. This allows connection establishment and | |
70 | * maintenance when the remote end is on a network that is not known | |
71 | * to us. | |
72 | */ | |
73 | int ip_nhops = 0; | |
74 | static struct ip_srcrt { | |
75 | struct in_addr dst; /* final destination */ | |
76 | char nop; /* one NOP to align */ | |
77 | char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ | |
78 | struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; | |
79 | } ip_srcrt; | |
80 | ||
15637ed4 | 81 | extern int if_index; |
17b4c26b AC |
82 | #ifdef DIAGNOSTIC |
83 | extern int ipprintfs; | |
84 | #endif | |
15637ed4 RG |
85 | |
86 | /* | |
87 | * IP initialization: fill in IP protocol switch table. | |
88 | * All protocols not implemented in kernel go to raw IP protocol handler. | |
89 | */ | |
4c45483e | 90 | void |
15637ed4 RG |
91 | ip_init() |
92 | { | |
fde1aeb2 | 93 | register struct in_protosw *pr; |
15637ed4 RG |
94 | register int i; |
95 | ||
fde1aeb2 | 96 | pr = (struct in_protosw *)pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); |
15637ed4 RG |
97 | if (pr == 0) |
98 | panic("ip_init"); | |
99 | for (i = 0; i < IPPROTO_MAX; i++) | |
100 | ip_protox[i] = pr - inetsw; | |
fde1aeb2 GW |
101 | for (pr = (struct in_protosw *)inetdomain.dom_protosw; |
102 | pr < (struct in_protosw *)inetdomain.dom_protoswNPROTOSW; pr++) | |
15637ed4 RG |
103 | if (pr->pr_domain->dom_family == PF_INET && |
104 | pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) | |
105 | ip_protox[pr->pr_protocol] = pr - inetsw; | |
106 | ipq.next = ipq.prev = &ipq; | |
107 | ip_id = time.tv_sec & 0xffff; | |
108 | ipintrq.ifq_maxlen = ipqmaxlen; | |
109 | #ifdef GATEWAY | |
110 | i = (if_index + 1) * (if_index + 1) * sizeof (u_long); | |
111 | if ((ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK)) == 0) | |
112 | panic("no memory for ip_ifmatrix"); | |
113 | #endif | |
114 | } | |
115 | ||
15637ed4 RG |
116 | /* |
117 | * Ip input routine. Checksum and byte swap header. If fragmented | |
118 | * try to reassemble. Process options. Pass to next level. | |
119 | */ | |
4c45483e | 120 | void |
15637ed4 RG |
121 | ipintr() |
122 | { | |
123 | register struct ip *ip; | |
124 | register struct mbuf *m; | |
125 | register struct ipq *fp; | |
126 | register struct in_ifaddr *ia; | |
127 | int hlen, s; | |
128 | ||
129 | next: | |
130 | /* | |
131 | * Get next datagram off input queue and get IP header | |
132 | * in first mbuf. | |
133 | */ | |
134 | s = splimp(); | |
135 | IF_DEQUEUE(&ipintrq, m); | |
136 | splx(s); | |
137 | if (m == 0) | |
138 | return; | |
139 | #ifdef DIAGNOSTIC | |
140 | if ((m->m_flags & M_PKTHDR) == 0) | |
141 | panic("ipintr no HDR"); | |
142 | #endif | |
143 | /* | |
144 | * If no IP addresses have been set yet but the interfaces | |
145 | * are receiving, can't do anything with incoming packets yet. | |
146 | */ | |
147 | if (in_ifaddr == NULL) | |
148 | goto bad; | |
149 | ipstat.ips_total++; | |
150 | if (m->m_len < sizeof (struct ip) && | |
151 | (m = m_pullup(m, sizeof (struct ip))) == 0) { | |
152 | ipstat.ips_toosmall++; | |
153 | goto next; | |
154 | } | |
155 | ip = mtod(m, struct ip *); | |
156 | hlen = ip->ip_hl << 2; | |
157 | if (hlen < sizeof(struct ip)) { /* minimum header length */ | |
158 | ipstat.ips_badhlen++; | |
159 | goto bad; | |
160 | } | |
161 | if (hlen > m->m_len) { | |
162 | if ((m = m_pullup(m, hlen)) == 0) { | |
163 | ipstat.ips_badhlen++; | |
164 | goto next; | |
165 | } | |
166 | ip = mtod(m, struct ip *); | |
167 | } | |
168 | if (ip->ip_sum = in_cksum(m, hlen)) { | |
169 | ipstat.ips_badsum++; | |
170 | goto bad; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Convert fields to host representation. | |
175 | */ | |
176 | NTOHS(ip->ip_len); | |
177 | if (ip->ip_len < hlen) { | |
178 | ipstat.ips_badlen++; | |
179 | goto bad; | |
180 | } | |
181 | NTOHS(ip->ip_id); | |
182 | NTOHS(ip->ip_off); | |
183 | ||
184 | /* | |
185 | * Check that the amount of data in the buffers | |
186 | * is as at least much as the IP header would have us expect. | |
187 | * Trim mbufs if longer than we expect. | |
188 | * Drop packet if shorter than we expect. | |
189 | */ | |
190 | if (m->m_pkthdr.len < ip->ip_len) { | |
191 | ipstat.ips_tooshort++; | |
192 | goto bad; | |
193 | } | |
194 | if (m->m_pkthdr.len > ip->ip_len) { | |
195 | if (m->m_len == m->m_pkthdr.len) { | |
196 | m->m_len = ip->ip_len; | |
197 | m->m_pkthdr.len = ip->ip_len; | |
198 | } else | |
199 | m_adj(m, ip->ip_len - m->m_pkthdr.len); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Process options and, if not destined for us, | |
204 | * ship it on. ip_dooptions returns 1 when an | |
205 | * error was detected (causing an icmp message | |
206 | * to be sent and the original packet to be freed). | |
207 | */ | |
208 | ip_nhops = 0; /* for source routed packets */ | |
209 | if (hlen > sizeof (struct ip) && ip_dooptions(m)) | |
210 | goto next; | |
211 | ||
212 | /* | |
213 | * Check our list of addresses, to see if the packet is for us. | |
214 | */ | |
215 | for (ia = in_ifaddr; ia; ia = ia->ia_next) { | |
216 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
217 | ||
218 | if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) | |
219 | goto ours; | |
220 | if ( | |
221 | #ifdef DIRECTED_BROADCAST | |
222 | ia->ia_ifp == m->m_pkthdr.rcvif && | |
223 | #endif | |
224 | (ia->ia_ifp->if_flags & IFF_BROADCAST)) { | |
225 | u_long t; | |
226 | ||
227 | if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == | |
228 | ip->ip_dst.s_addr) | |
229 | goto ours; | |
230 | if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) | |
231 | goto ours; | |
232 | /* | |
233 | * Look for all-0's host part (old broadcast addr), | |
234 | * either for subnet or net. | |
235 | */ | |
236 | t = ntohl(ip->ip_dst.s_addr); | |
237 | if (t == ia->ia_subnet) | |
238 | goto ours; | |
239 | if (t == ia->ia_net) | |
240 | goto ours; | |
241 | } | |
242 | } | |
4cf615e1 JH |
243 | #ifdef MULTICAST |
244 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { | |
245 | struct in_multi *inm; | |
246 | #ifdef MROUTING | |
247 | extern struct socket *ip_mrouter; | |
248 | ||
249 | if (ip_mrouter) { | |
250 | /* | |
251 | * If we are acting as a multicast router, all | |
252 | * incoming multicast packets are passed to the | |
253 | * kernel-level multicast forwarding function. | |
254 | * The packet is returned (relatively) intact; if | |
255 | * ip_mforward() returns a non-zero value, the packet | |
256 | * must be discarded, else it may be accepted below. | |
257 | * | |
258 | * (The IP ident field is put in the same byte order | |
259 | * as expected when ip_mforward() is called from | |
260 | * ip_output().) | |
261 | */ | |
262 | ip->ip_id = htons(ip->ip_id); | |
263 | if (ip_mforward(ip, m->m_pkthdr.rcvif, m) != 0) { | |
264 | m_freem(m); | |
265 | goto next; | |
266 | } | |
267 | ip->ip_id = ntohs(ip->ip_id); | |
268 | ||
269 | /* | |
270 | * The process-level routing demon needs to receive | |
271 | * all multicast IGMP packets, whether or not this | |
272 | * host belongs to their destination groups. | |
273 | */ | |
274 | if (ip->ip_p == IPPROTO_IGMP) | |
275 | goto ours; | |
276 | } | |
277 | #endif | |
278 | /* | |
279 | * See if we belong to the destination multicast group on the | |
280 | * arrival interface. | |
281 | */ | |
282 | IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); | |
283 | if (inm == NULL) { | |
284 | m_freem(m); | |
285 | goto next; | |
286 | } | |
287 | goto ours; | |
288 | } | |
289 | #endif | |
15637ed4 RG |
290 | if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) |
291 | goto ours; | |
292 | if (ip->ip_dst.s_addr == INADDR_ANY) | |
293 | goto ours; | |
294 | ||
295 | /* | |
296 | * Not for us; forward if possible and desirable. | |
297 | */ | |
298 | if (ipforwarding == 0) { | |
299 | ipstat.ips_cantforward++; | |
300 | m_freem(m); | |
301 | } else | |
302 | ip_forward(m, 0); | |
303 | goto next; | |
304 | ||
305 | ours: | |
306 | /* | |
307 | * If offset or IP_MF are set, must reassemble. | |
308 | * Otherwise, nothing need be done. | |
309 | * (We could look in the reassembly queue to see | |
310 | * if the packet was previously fragmented, | |
311 | * but it's not worth the time; just let them time out.) | |
312 | */ | |
313 | if (ip->ip_off &~ IP_DF) { | |
314 | if (m->m_flags & M_EXT) { /* XXX */ | |
315 | if ((m = m_pullup(m, sizeof (struct ip))) == 0) { | |
316 | ipstat.ips_toosmall++; | |
317 | goto next; | |
318 | } | |
319 | ip = mtod(m, struct ip *); | |
320 | } | |
321 | /* | |
322 | * Look for queue of fragments | |
323 | * of this datagram. | |
324 | */ | |
325 | for (fp = ipq.next; fp != &ipq; fp = fp->next) | |
326 | if (ip->ip_id == fp->ipq_id && | |
327 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
328 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
329 | ip->ip_p == fp->ipq_p) | |
330 | goto found; | |
331 | fp = 0; | |
332 | found: | |
333 | ||
334 | /* | |
335 | * Adjust ip_len to not reflect header, | |
336 | * set ip_mff if more fragments are expected, | |
337 | * convert offset of this to bytes. | |
338 | */ | |
339 | ip->ip_len -= hlen; | |
340 | ((struct ipasfrag *)ip)->ipf_mff = 0; | |
341 | if (ip->ip_off & IP_MF) | |
342 | ((struct ipasfrag *)ip)->ipf_mff = 1; | |
343 | ip->ip_off <<= 3; | |
344 | ||
345 | /* | |
346 | * If datagram marked as having more fragments | |
347 | * or if this is not the first fragment, | |
348 | * attempt reassembly; if it succeeds, proceed. | |
349 | */ | |
350 | if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { | |
351 | ipstat.ips_fragments++; | |
352 | ip = ip_reass((struct ipasfrag *)ip, fp); | |
353 | if (ip == 0) | |
354 | goto next; | |
355 | else | |
356 | ipstat.ips_reassembled++; | |
357 | m = dtom(ip); | |
358 | } else | |
359 | if (fp) | |
360 | ip_freef(fp); | |
361 | } else | |
362 | ip->ip_len -= hlen; | |
363 | ||
364 | /* | |
365 | * Switch out to protocol's input routine. | |
366 | */ | |
367 | ipstat.ips_delivered++; | |
368 | (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); | |
369 | goto next; | |
370 | bad: | |
371 | m_freem(m); | |
372 | goto next; | |
373 | } | |
374 | ||
375 | /* | |
376 | * Take incoming datagram fragment and try to | |
377 | * reassemble it into whole datagram. If a chain for | |
378 | * reassembly of this datagram already exists, then it | |
379 | * is given as fp; otherwise have to make a chain. | |
380 | */ | |
fde1aeb2 | 381 | static struct ip * |
15637ed4 RG |
382 | ip_reass(ip, fp) |
383 | register struct ipasfrag *ip; | |
384 | register struct ipq *fp; | |
385 | { | |
386 | register struct mbuf *m = dtom(ip); | |
387 | register struct ipasfrag *q; | |
388 | struct mbuf *t; | |
389 | int hlen = ip->ip_hl << 2; | |
390 | int i, next; | |
391 | ||
392 | /* | |
393 | * Presence of header sizes in mbufs | |
394 | * would confuse code below. | |
395 | */ | |
396 | m->m_data += hlen; | |
397 | m->m_len -= hlen; | |
398 | ||
399 | /* | |
400 | * If first fragment to arrive, create a reassembly queue. | |
401 | */ | |
402 | if (fp == 0) { | |
403 | if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) | |
404 | goto dropfrag; | |
405 | fp = mtod(t, struct ipq *); | |
406 | insque(fp, &ipq); | |
407 | fp->ipq_ttl = IPFRAGTTL; | |
408 | fp->ipq_p = ip->ip_p; | |
409 | fp->ipq_id = ip->ip_id; | |
410 | fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; | |
411 | fp->ipq_src = ((struct ip *)ip)->ip_src; | |
412 | fp->ipq_dst = ((struct ip *)ip)->ip_dst; | |
413 | q = (struct ipasfrag *)fp; | |
414 | goto insert; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Find a segment which begins after this one does. | |
419 | */ | |
420 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) | |
421 | if (q->ip_off > ip->ip_off) | |
422 | break; | |
423 | ||
424 | /* | |
425 | * If there is a preceding segment, it may provide some of | |
426 | * our data already. If so, drop the data from the incoming | |
427 | * segment. If it provides all of our data, drop us. | |
428 | */ | |
429 | if (q->ipf_prev != (struct ipasfrag *)fp) { | |
430 | i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; | |
431 | if (i > 0) { | |
432 | if (i >= ip->ip_len) | |
433 | goto dropfrag; | |
434 | m_adj(dtom(ip), i); | |
435 | ip->ip_off += i; | |
436 | ip->ip_len -= i; | |
437 | } | |
438 | } | |
439 | ||
440 | /* | |
441 | * While we overlap succeeding segments trim them or, | |
442 | * if they are completely covered, dequeue them. | |
443 | */ | |
444 | while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { | |
445 | i = (ip->ip_off + ip->ip_len) - q->ip_off; | |
446 | if (i < q->ip_len) { | |
447 | q->ip_len -= i; | |
448 | q->ip_off += i; | |
449 | m_adj(dtom(q), i); | |
450 | break; | |
451 | } | |
452 | q = q->ipf_next; | |
453 | m_freem(dtom(q->ipf_prev)); | |
454 | ip_deq(q->ipf_prev); | |
455 | } | |
456 | ||
457 | insert: | |
458 | /* | |
459 | * Stick new segment in its place; | |
460 | * check for complete reassembly. | |
461 | */ | |
462 | ip_enq(ip, q->ipf_prev); | |
463 | next = 0; | |
464 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { | |
465 | if (q->ip_off != next) | |
466 | return (0); | |
467 | next += q->ip_len; | |
468 | } | |
469 | if (q->ipf_prev->ipf_mff) | |
470 | return (0); | |
471 | ||
472 | /* | |
473 | * Reassembly is complete; concatenate fragments. | |
474 | */ | |
475 | q = fp->ipq_next; | |
476 | m = dtom(q); | |
477 | t = m->m_next; | |
478 | m->m_next = 0; | |
479 | m_cat(m, t); | |
480 | q = q->ipf_next; | |
481 | while (q != (struct ipasfrag *)fp) { | |
482 | t = dtom(q); | |
483 | q = q->ipf_next; | |
484 | m_cat(m, t); | |
485 | } | |
486 | ||
487 | /* | |
488 | * Create header for new ip packet by | |
489 | * modifying header of first packet; | |
490 | * dequeue and discard fragment reassembly header. | |
491 | * Make header visible. | |
492 | */ | |
493 | ip = fp->ipq_next; | |
494 | ip->ip_len = next; | |
495 | ((struct ip *)ip)->ip_src = fp->ipq_src; | |
496 | ((struct ip *)ip)->ip_dst = fp->ipq_dst; | |
497 | remque(fp); | |
498 | (void) m_free(dtom(fp)); | |
499 | m = dtom(ip); | |
500 | m->m_len += (ip->ip_hl << 2); | |
501 | m->m_data -= (ip->ip_hl << 2); | |
502 | /* some debugging cruft by sklower, below, will go away soon */ | |
503 | if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ | |
504 | register int plen = 0; | |
505 | for (t = m; m; m = m->m_next) | |
506 | plen += m->m_len; | |
507 | t->m_pkthdr.len = plen; | |
508 | } | |
509 | return ((struct ip *)ip); | |
510 | ||
511 | dropfrag: | |
512 | ipstat.ips_fragdropped++; | |
513 | m_freem(m); | |
514 | return (0); | |
515 | } | |
516 | ||
517 | /* | |
518 | * Free a fragment reassembly header and all | |
519 | * associated datagrams. | |
520 | */ | |
4c45483e | 521 | static void |
15637ed4 RG |
522 | ip_freef(fp) |
523 | struct ipq *fp; | |
524 | { | |
525 | register struct ipasfrag *q, *p; | |
526 | ||
527 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { | |
528 | p = q->ipf_next; | |
529 | ip_deq(q); | |
530 | m_freem(dtom(q)); | |
531 | } | |
532 | remque(fp); | |
533 | (void) m_free(dtom(fp)); | |
534 | } | |
535 | ||
536 | /* | |
537 | * Put an ip fragment on a reassembly chain. | |
538 | * Like insque, but pointers in middle of structure. | |
539 | */ | |
4c45483e | 540 | static void |
15637ed4 RG |
541 | ip_enq(p, prev) |
542 | register struct ipasfrag *p, *prev; | |
543 | { | |
544 | ||
545 | p->ipf_prev = prev; | |
546 | p->ipf_next = prev->ipf_next; | |
547 | prev->ipf_next->ipf_prev = p; | |
548 | prev->ipf_next = p; | |
549 | } | |
550 | ||
551 | /* | |
552 | * To ip_enq as remque is to insque. | |
553 | */ | |
4c45483e | 554 | static void |
15637ed4 RG |
555 | ip_deq(p) |
556 | register struct ipasfrag *p; | |
557 | { | |
558 | ||
559 | p->ipf_prev->ipf_next = p->ipf_next; | |
560 | p->ipf_next->ipf_prev = p->ipf_prev; | |
561 | } | |
562 | ||
563 | /* | |
564 | * IP timer processing; | |
565 | * if a timer expires on a reassembly | |
566 | * queue, discard it. | |
567 | */ | |
4c45483e | 568 | void |
15637ed4 RG |
569 | ip_slowtimo() |
570 | { | |
571 | register struct ipq *fp; | |
572 | int s = splnet(); | |
573 | ||
574 | fp = ipq.next; | |
575 | if (fp == 0) { | |
576 | splx(s); | |
577 | return; | |
578 | } | |
579 | while (fp != &ipq) { | |
580 | --fp->ipq_ttl; | |
581 | fp = fp->next; | |
582 | if (fp->prev->ipq_ttl == 0) { | |
583 | ipstat.ips_fragtimeout++; | |
584 | ip_freef(fp->prev); | |
585 | } | |
586 | } | |
587 | splx(s); | |
588 | } | |
589 | ||
590 | /* | |
591 | * Drain off all datagram fragments. | |
592 | */ | |
4c45483e | 593 | void |
15637ed4 RG |
594 | ip_drain() |
595 | { | |
596 | ||
597 | while (ipq.next != &ipq) { | |
598 | ipstat.ips_fragdropped++; | |
599 | ip_freef(ipq.next); | |
600 | } | |
601 | } | |
602 | ||
603 | extern struct in_ifaddr *ifptoia(); | |
604 | struct in_ifaddr *ip_rtaddr(); | |
605 | ||
606 | /* | |
607 | * Do option processing on a datagram, | |
608 | * possibly discarding it if bad options are encountered, | |
609 | * or forwarding it if source-routed. | |
610 | * Returns 1 if packet has been forwarded/freed, | |
611 | * 0 if the packet should be processed further. | |
612 | */ | |
4c45483e | 613 | int |
15637ed4 RG |
614 | ip_dooptions(m) |
615 | struct mbuf *m; | |
616 | { | |
617 | register struct ip *ip = mtod(m, struct ip *); | |
618 | register u_char *cp; | |
619 | register struct ip_timestamp *ipt; | |
620 | register struct in_ifaddr *ia; | |
621 | int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; | |
622 | struct in_addr *sin; | |
623 | n_time ntime; | |
624 | ||
625 | cp = (u_char *)(ip + 1); | |
626 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
627 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
628 | opt = cp[IPOPT_OPTVAL]; | |
629 | if (opt == IPOPT_EOL) | |
630 | break; | |
631 | if (opt == IPOPT_NOP) | |
632 | optlen = 1; | |
633 | else { | |
634 | optlen = cp[IPOPT_OLEN]; | |
635 | if (optlen <= 0 || optlen > cnt) { | |
636 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
637 | goto bad; | |
638 | } | |
639 | } | |
640 | switch (opt) { | |
641 | ||
642 | default: | |
643 | break; | |
644 | ||
645 | /* | |
646 | * Source routing with record. | |
647 | * Find interface with current destination address. | |
648 | * If none on this machine then drop if strictly routed, | |
649 | * or do nothing if loosely routed. | |
650 | * Record interface address and bring up next address | |
651 | * component. If strictly routed make sure next | |
652 | * address is on directly accessible net. | |
653 | */ | |
654 | case IPOPT_LSRR: | |
655 | case IPOPT_SSRR: | |
656 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
657 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
658 | goto bad; | |
659 | } | |
660 | ipaddr.sin_addr = ip->ip_dst; | |
661 | ia = (struct in_ifaddr *) | |
662 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
663 | if (ia == 0) { | |
664 | if (opt == IPOPT_SSRR) { | |
665 | type = ICMP_UNREACH; | |
666 | code = ICMP_UNREACH_SRCFAIL; | |
667 | goto bad; | |
668 | } | |
669 | /* | |
670 | * Loose routing, and not at next destination | |
671 | * yet; nothing to do except forward. | |
672 | */ | |
673 | break; | |
674 | } | |
675 | off--; /* 0 origin */ | |
676 | if (off > optlen - sizeof(struct in_addr)) { | |
677 | /* | |
678 | * End of source route. Should be for us. | |
679 | */ | |
680 | save_rte(cp, ip->ip_src); | |
681 | break; | |
682 | } | |
683 | /* | |
684 | * locate outgoing interface | |
685 | */ | |
686 | bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, | |
687 | sizeof(ipaddr.sin_addr)); | |
688 | if (opt == IPOPT_SSRR) { | |
689 | #define INA struct in_ifaddr * | |
690 | #define SA struct sockaddr * | |
691 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) | |
692 | ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); | |
693 | } else | |
694 | ia = ip_rtaddr(ipaddr.sin_addr); | |
695 | if (ia == 0) { | |
696 | type = ICMP_UNREACH; | |
697 | code = ICMP_UNREACH_SRCFAIL; | |
698 | goto bad; | |
699 | } | |
700 | ip->ip_dst = ipaddr.sin_addr; | |
701 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
702 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
703 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
704 | forward = 1; | |
705 | break; | |
706 | ||
707 | case IPOPT_RR: | |
708 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
709 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
710 | goto bad; | |
711 | } | |
712 | /* | |
713 | * If no space remains, ignore. | |
714 | */ | |
715 | off--; /* 0 origin */ | |
716 | if (off > optlen - sizeof(struct in_addr)) | |
717 | break; | |
718 | bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, | |
719 | sizeof(ipaddr.sin_addr)); | |
720 | /* | |
721 | * locate outgoing interface; if we're the destination, | |
722 | * use the incoming interface (should be same). | |
723 | */ | |
724 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && | |
725 | (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { | |
726 | type = ICMP_UNREACH; | |
727 | code = ICMP_UNREACH_HOST; | |
728 | goto bad; | |
729 | } | |
730 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
731 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
732 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
733 | break; | |
734 | ||
735 | case IPOPT_TS: | |
736 | code = cp - (u_char *)ip; | |
737 | ipt = (struct ip_timestamp *)cp; | |
738 | if (ipt->ipt_len < 5) | |
739 | goto bad; | |
740 | if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { | |
741 | if (++ipt->ipt_oflw == 0) | |
742 | goto bad; | |
743 | break; | |
744 | } | |
745 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
746 | switch (ipt->ipt_flg) { | |
747 | ||
748 | case IPOPT_TS_TSONLY: | |
749 | break; | |
750 | ||
751 | case IPOPT_TS_TSANDADDR: | |
752 | if (ipt->ipt_ptr + sizeof(n_time) + | |
753 | sizeof(struct in_addr) > ipt->ipt_len) | |
754 | goto bad; | |
755 | ia = ifptoia(m->m_pkthdr.rcvif); | |
756 | bcopy((caddr_t)&IA_SIN(ia)->sin_addr, | |
757 | (caddr_t)sin, sizeof(struct in_addr)); | |
758 | ipt->ipt_ptr += sizeof(struct in_addr); | |
759 | break; | |
760 | ||
761 | case IPOPT_TS_PRESPEC: | |
762 | if (ipt->ipt_ptr + sizeof(n_time) + | |
763 | sizeof(struct in_addr) > ipt->ipt_len) | |
764 | goto bad; | |
765 | bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, | |
766 | sizeof(struct in_addr)); | |
767 | if (ifa_ifwithaddr((SA)&ipaddr) == 0) | |
768 | continue; | |
769 | ipt->ipt_ptr += sizeof(struct in_addr); | |
770 | break; | |
771 | ||
772 | default: | |
773 | goto bad; | |
774 | } | |
775 | ntime = iptime(); | |
776 | bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, | |
777 | sizeof(n_time)); | |
778 | ipt->ipt_ptr += sizeof(n_time); | |
779 | } | |
780 | } | |
781 | if (forward) { | |
782 | ip_forward(m, 1); | |
783 | return (1); | |
784 | } else | |
785 | return (0); | |
786 | bad: | |
2cb63509 GW |
787 | { |
788 | static struct in_addr fake; | |
789 | icmp_error(m, type, code, fake, 0); | |
790 | } | |
15637ed4 RG |
791 | return (1); |
792 | } | |
793 | ||
794 | /* | |
795 | * Given address of next destination (final or next hop), | |
796 | * return internet address info of interface to be used to get there. | |
797 | */ | |
798 | struct in_ifaddr * | |
799 | ip_rtaddr(dst) | |
800 | struct in_addr dst; | |
801 | { | |
802 | register struct sockaddr_in *sin; | |
803 | ||
804 | sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; | |
805 | ||
806 | if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { | |
807 | if (ipforward_rt.ro_rt) { | |
808 | RTFREE(ipforward_rt.ro_rt); | |
809 | ipforward_rt.ro_rt = 0; | |
810 | } | |
811 | sin->sin_family = AF_INET; | |
812 | sin->sin_len = sizeof(*sin); | |
813 | sin->sin_addr = dst; | |
814 | ||
815 | rtalloc(&ipforward_rt); | |
816 | } | |
817 | if (ipforward_rt.ro_rt == 0) | |
818 | return ((struct in_ifaddr *)0); | |
819 | return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); | |
820 | } | |
821 | ||
822 | /* | |
823 | * Save incoming source route for use in replies, | |
824 | * to be picked up later by ip_srcroute if the receiver is interested. | |
825 | */ | |
4c45483e | 826 | static void |
15637ed4 RG |
827 | save_rte(option, dst) |
828 | u_char *option; | |
829 | struct in_addr dst; | |
830 | { | |
831 | unsigned olen; | |
832 | ||
833 | olen = option[IPOPT_OLEN]; | |
834 | #ifdef DIAGNOSTIC | |
835 | if (ipprintfs) | |
836 | printf("save_rte: olen %d\n", olen); | |
837 | #endif | |
838 | if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) | |
839 | return; | |
840 | bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); | |
841 | ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); | |
842 | ip_srcrt.dst = dst; | |
843 | } | |
844 | ||
845 | /* | |
846 | * Retrieve incoming source route for use in replies, | |
847 | * in the same form used by setsockopt. | |
848 | * The first hop is placed before the options, will be removed later. | |
849 | */ | |
850 | struct mbuf * | |
851 | ip_srcroute() | |
852 | { | |
853 | register struct in_addr *p, *q; | |
854 | register struct mbuf *m; | |
855 | ||
856 | if (ip_nhops == 0) | |
857 | return ((struct mbuf *)0); | |
858 | m = m_get(M_DONTWAIT, MT_SOOPTS); | |
859 | if (m == 0) | |
860 | return ((struct mbuf *)0); | |
861 | ||
862 | #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) | |
863 | ||
864 | /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ | |
865 | m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + | |
866 | OPTSIZ; | |
867 | #ifdef DIAGNOSTIC | |
868 | if (ipprintfs) | |
869 | printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); | |
870 | #endif | |
871 | ||
872 | /* | |
873 | * First save first hop for return route | |
874 | */ | |
875 | p = &ip_srcrt.route[ip_nhops - 1]; | |
876 | *(mtod(m, struct in_addr *)) = *p--; | |
877 | #ifdef DIAGNOSTIC | |
878 | if (ipprintfs) | |
879 | printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); | |
880 | #endif | |
881 | ||
882 | /* | |
883 | * Copy option fields and padding (nop) to mbuf. | |
884 | */ | |
885 | ip_srcrt.nop = IPOPT_NOP; | |
886 | ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; | |
887 | bcopy((caddr_t)&ip_srcrt.nop, | |
888 | mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); | |
889 | q = (struct in_addr *)(mtod(m, caddr_t) + | |
890 | sizeof(struct in_addr) + OPTSIZ); | |
891 | #undef OPTSIZ | |
892 | /* | |
893 | * Record return path as an IP source route, | |
894 | * reversing the path (pointers are now aligned). | |
895 | */ | |
896 | while (p >= ip_srcrt.route) { | |
897 | #ifdef DIAGNOSTIC | |
898 | if (ipprintfs) | |
899 | printf(" %lx", ntohl(q->s_addr)); | |
900 | #endif | |
901 | *q++ = *p--; | |
902 | } | |
903 | /* | |
904 | * Last hop goes to final destination. | |
905 | */ | |
906 | *q = ip_srcrt.dst; | |
907 | #ifdef DIAGNOSTIC | |
908 | if (ipprintfs) | |
909 | printf(" %lx\n", ntohl(q->s_addr)); | |
910 | #endif | |
911 | return (m); | |
912 | } | |
913 | ||
914 | /* | |
915 | * Strip out IP options, at higher | |
916 | * level protocol in the kernel. | |
917 | * Second argument is buffer to which options | |
918 | * will be moved, and return value is their length. | |
919 | * XXX should be deleted; last arg currently ignored. | |
920 | */ | |
4c45483e | 921 | void |
15637ed4 RG |
922 | ip_stripoptions(m, mopt) |
923 | register struct mbuf *m; | |
924 | struct mbuf *mopt; | |
925 | { | |
926 | register int i; | |
927 | struct ip *ip = mtod(m, struct ip *); | |
928 | register caddr_t opts; | |
929 | int olen; | |
930 | ||
931 | olen = (ip->ip_hl<<2) - sizeof (struct ip); | |
932 | opts = (caddr_t)(ip + 1); | |
933 | i = m->m_len - (sizeof (struct ip) + olen); | |
934 | bcopy(opts + olen, opts, (unsigned)i); | |
935 | m->m_len -= olen; | |
936 | if (m->m_flags & M_PKTHDR) | |
937 | m->m_pkthdr.len -= olen; | |
938 | ip->ip_hl = sizeof(struct ip) >> 2; | |
939 | } | |
940 | ||
15637ed4 RG |
941 | /* |
942 | * Forward a packet. If some error occurs return the sender | |
943 | * an icmp packet. Note we can't always generate a meaningful | |
944 | * icmp message because icmp doesn't have a large enough repertoire | |
945 | * of codes and types. | |
946 | * | |
947 | * If not forwarding, just drop the packet. This could be confusing | |
948 | * if ipforwarding was zero but some routing protocol was advancing | |
949 | * us as a gateway to somewhere. However, we must let the routing | |
950 | * protocol deal with that. | |
951 | * | |
952 | * The srcrt parameter indicates whether the packet is being forwarded | |
953 | * via a source route. | |
954 | */ | |
4c45483e | 955 | static void |
15637ed4 RG |
956 | ip_forward(m, srcrt) |
957 | struct mbuf *m; | |
958 | int srcrt; | |
959 | { | |
960 | register struct ip *ip = mtod(m, struct ip *); | |
961 | register struct sockaddr_in *sin; | |
962 | register struct rtentry *rt; | |
4c45483e | 963 | int error, type = 0, code = 0; |
15637ed4 RG |
964 | struct mbuf *mcopy; |
965 | struct in_addr dest; | |
4c45483e | 966 | int mtu = 0; |
15637ed4 RG |
967 | |
968 | dest.s_addr = 0; | |
969 | #ifdef DIAGNOSTIC | |
970 | if (ipprintfs) | |
971 | printf("forward: src %x dst %x ttl %x\n", ip->ip_src, | |
972 | ip->ip_dst, ip->ip_ttl); | |
973 | #endif | |
974 | if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { | |
975 | ipstat.ips_cantforward++; | |
976 | m_freem(m); | |
977 | return; | |
978 | } | |
979 | HTONS(ip->ip_id); | |
980 | if (ip->ip_ttl <= IPTTLDEC) { | |
2cb63509 | 981 | icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); |
15637ed4 RG |
982 | return; |
983 | } | |
984 | ip->ip_ttl -= IPTTLDEC; | |
985 | ||
986 | sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; | |
987 | if ((rt = ipforward_rt.ro_rt) == 0 || | |
988 | ip->ip_dst.s_addr != sin->sin_addr.s_addr) { | |
989 | if (ipforward_rt.ro_rt) { | |
990 | RTFREE(ipforward_rt.ro_rt); | |
991 | ipforward_rt.ro_rt = 0; | |
992 | } | |
993 | sin->sin_family = AF_INET; | |
994 | sin->sin_len = sizeof(*sin); | |
995 | sin->sin_addr = ip->ip_dst; | |
996 | ||
997 | rtalloc(&ipforward_rt); | |
998 | if (ipforward_rt.ro_rt == 0) { | |
2cb63509 | 999 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); |
15637ed4 RG |
1000 | return; |
1001 | } | |
1002 | rt = ipforward_rt.ro_rt; | |
2cb63509 GW |
1003 | mtu = rt->rt_ifp->if_mtu; |
1004 | /* salt away if's mtu */ | |
15637ed4 RG |
1005 | } |
1006 | ||
1007 | /* | |
1008 | * Save at most 64 bytes of the packet in case | |
1009 | * we need to generate an ICMP message to the src. | |
1010 | */ | |
1011 | mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); | |
1012 | ||
1013 | #ifdef GATEWAY | |
1014 | ip_ifmatrix[rt->rt_ifp->if_index + | |
1015 | if_index * m->m_pkthdr.rcvif->if_index]++; | |
1016 | #endif | |
1017 | /* | |
1018 | * If forwarding packet using same interface that it came in on, | |
1019 | * perhaps should send a redirect to sender to shortcut a hop. | |
1020 | * Only send redirect if source is sending directly to us, | |
1021 | * and if packet was not source routed (or has any options). | |
1022 | * Also, don't send redirect if forwarding using a default route | |
1023 | * or a route modified by a redirect. | |
1024 | */ | |
1025 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
1026 | if (rt->rt_ifp == m->m_pkthdr.rcvif && | |
1027 | (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && | |
1028 | satosin(rt_key(rt))->sin_addr.s_addr != 0 && | |
1029 | ipsendredirects && !srcrt) { | |
1030 | struct in_ifaddr *ia; | |
1031 | u_long src = ntohl(ip->ip_src.s_addr); | |
1032 | u_long dst = ntohl(ip->ip_dst.s_addr); | |
1033 | ||
1034 | if ((ia = ifptoia(m->m_pkthdr.rcvif)) && | |
1035 | (src & ia->ia_subnetmask) == ia->ia_subnet) { | |
1036 | if (rt->rt_flags & RTF_GATEWAY) | |
1037 | dest = satosin(rt->rt_gateway)->sin_addr; | |
1038 | else | |
1039 | dest = ip->ip_dst; | |
1040 | /* | |
1041 | * If the destination is reached by a route to host, | |
1042 | * is on a subnet of a local net, or is directly | |
1043 | * on the attached net (!), use host redirect. | |
1044 | * (We may be the correct first hop for other subnets.) | |
1045 | */ | |
1046 | #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) | |
1047 | type = ICMP_REDIRECT; | |
1048 | if ((rt->rt_flags & RTF_HOST) || | |
1049 | (rt->rt_flags & RTF_GATEWAY) == 0) | |
1050 | code = ICMP_REDIRECT_HOST; | |
1051 | else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && | |
1052 | (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) | |
1053 | code = ICMP_REDIRECT_HOST; | |
1054 | else | |
1055 | code = ICMP_REDIRECT_NET; | |
1056 | #ifdef DIAGNOSTIC | |
1057 | if (ipprintfs) | |
1058 | printf("redirect (%d) to %x\n", code, dest.s_addr); | |
1059 | #endif | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); | |
1064 | if (error) | |
1065 | ipstat.ips_cantforward++; | |
1066 | else { | |
1067 | ipstat.ips_forward++; | |
1068 | if (type) | |
1069 | ipstat.ips_redirectsent++; | |
1070 | else { | |
1071 | if (mcopy) | |
1072 | m_freem(mcopy); | |
1073 | return; | |
1074 | } | |
1075 | } | |
1076 | if (mcopy == NULL) | |
1077 | return; | |
1078 | switch (error) { | |
1079 | ||
1080 | case 0: /* forwarded, but need redirect */ | |
1081 | /* type, code set above */ | |
1082 | break; | |
1083 | ||
1084 | case ENETUNREACH: /* shouldn't happen, checked above */ | |
1085 | case EHOSTUNREACH: | |
1086 | case ENETDOWN: | |
1087 | case EHOSTDOWN: | |
1088 | default: | |
1089 | type = ICMP_UNREACH; | |
1090 | code = ICMP_UNREACH_HOST; | |
1091 | break; | |
1092 | ||
1093 | case EMSGSIZE: | |
1094 | type = ICMP_UNREACH; | |
1095 | code = ICMP_UNREACH_NEEDFRAG; | |
1096 | ipstat.ips_cantfrag++; | |
1097 | break; | |
1098 | ||
1099 | case ENOBUFS: | |
1100 | type = ICMP_SOURCEQUENCH; | |
1101 | code = 0; | |
1102 | break; | |
1103 | } | |
2cb63509 | 1104 | icmp_error(mcopy, type, code, dest, mtu); |
15637ed4 | 1105 | } |