Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988 Regents of the University of California. | |
3 | * All rights reserved. | |
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 |
17b4c26b | 34 | * $Id: ip_input.c,v 1.7 1993/12/19 00:52:43 wollman 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 | } | |
243 | if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) | |
244 | goto ours; | |
245 | if (ip->ip_dst.s_addr == INADDR_ANY) | |
246 | goto ours; | |
247 | ||
248 | /* | |
249 | * Not for us; forward if possible and desirable. | |
250 | */ | |
251 | if (ipforwarding == 0) { | |
252 | ipstat.ips_cantforward++; | |
253 | m_freem(m); | |
254 | } else | |
255 | ip_forward(m, 0); | |
256 | goto next; | |
257 | ||
258 | ours: | |
259 | /* | |
260 | * If offset or IP_MF are set, must reassemble. | |
261 | * Otherwise, nothing need be done. | |
262 | * (We could look in the reassembly queue to see | |
263 | * if the packet was previously fragmented, | |
264 | * but it's not worth the time; just let them time out.) | |
265 | */ | |
266 | if (ip->ip_off &~ IP_DF) { | |
267 | if (m->m_flags & M_EXT) { /* XXX */ | |
268 | if ((m = m_pullup(m, sizeof (struct ip))) == 0) { | |
269 | ipstat.ips_toosmall++; | |
270 | goto next; | |
271 | } | |
272 | ip = mtod(m, struct ip *); | |
273 | } | |
274 | /* | |
275 | * Look for queue of fragments | |
276 | * of this datagram. | |
277 | */ | |
278 | for (fp = ipq.next; fp != &ipq; fp = fp->next) | |
279 | if (ip->ip_id == fp->ipq_id && | |
280 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
281 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
282 | ip->ip_p == fp->ipq_p) | |
283 | goto found; | |
284 | fp = 0; | |
285 | found: | |
286 | ||
287 | /* | |
288 | * Adjust ip_len to not reflect header, | |
289 | * set ip_mff if more fragments are expected, | |
290 | * convert offset of this to bytes. | |
291 | */ | |
292 | ip->ip_len -= hlen; | |
293 | ((struct ipasfrag *)ip)->ipf_mff = 0; | |
294 | if (ip->ip_off & IP_MF) | |
295 | ((struct ipasfrag *)ip)->ipf_mff = 1; | |
296 | ip->ip_off <<= 3; | |
297 | ||
298 | /* | |
299 | * If datagram marked as having more fragments | |
300 | * or if this is not the first fragment, | |
301 | * attempt reassembly; if it succeeds, proceed. | |
302 | */ | |
303 | if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { | |
304 | ipstat.ips_fragments++; | |
305 | ip = ip_reass((struct ipasfrag *)ip, fp); | |
306 | if (ip == 0) | |
307 | goto next; | |
308 | else | |
309 | ipstat.ips_reassembled++; | |
310 | m = dtom(ip); | |
311 | } else | |
312 | if (fp) | |
313 | ip_freef(fp); | |
314 | } else | |
315 | ip->ip_len -= hlen; | |
316 | ||
317 | /* | |
318 | * Switch out to protocol's input routine. | |
319 | */ | |
320 | ipstat.ips_delivered++; | |
321 | (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); | |
322 | goto next; | |
323 | bad: | |
324 | m_freem(m); | |
325 | goto next; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Take incoming datagram fragment and try to | |
330 | * reassemble it into whole datagram. If a chain for | |
331 | * reassembly of this datagram already exists, then it | |
332 | * is given as fp; otherwise have to make a chain. | |
333 | */ | |
fde1aeb2 | 334 | static struct ip * |
15637ed4 RG |
335 | ip_reass(ip, fp) |
336 | register struct ipasfrag *ip; | |
337 | register struct ipq *fp; | |
338 | { | |
339 | register struct mbuf *m = dtom(ip); | |
340 | register struct ipasfrag *q; | |
341 | struct mbuf *t; | |
342 | int hlen = ip->ip_hl << 2; | |
343 | int i, next; | |
344 | ||
345 | /* | |
346 | * Presence of header sizes in mbufs | |
347 | * would confuse code below. | |
348 | */ | |
349 | m->m_data += hlen; | |
350 | m->m_len -= hlen; | |
351 | ||
352 | /* | |
353 | * If first fragment to arrive, create a reassembly queue. | |
354 | */ | |
355 | if (fp == 0) { | |
356 | if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) | |
357 | goto dropfrag; | |
358 | fp = mtod(t, struct ipq *); | |
359 | insque(fp, &ipq); | |
360 | fp->ipq_ttl = IPFRAGTTL; | |
361 | fp->ipq_p = ip->ip_p; | |
362 | fp->ipq_id = ip->ip_id; | |
363 | fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; | |
364 | fp->ipq_src = ((struct ip *)ip)->ip_src; | |
365 | fp->ipq_dst = ((struct ip *)ip)->ip_dst; | |
366 | q = (struct ipasfrag *)fp; | |
367 | goto insert; | |
368 | } | |
369 | ||
370 | /* | |
371 | * Find a segment which begins after this one does. | |
372 | */ | |
373 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) | |
374 | if (q->ip_off > ip->ip_off) | |
375 | break; | |
376 | ||
377 | /* | |
378 | * If there is a preceding segment, it may provide some of | |
379 | * our data already. If so, drop the data from the incoming | |
380 | * segment. If it provides all of our data, drop us. | |
381 | */ | |
382 | if (q->ipf_prev != (struct ipasfrag *)fp) { | |
383 | i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; | |
384 | if (i > 0) { | |
385 | if (i >= ip->ip_len) | |
386 | goto dropfrag; | |
387 | m_adj(dtom(ip), i); | |
388 | ip->ip_off += i; | |
389 | ip->ip_len -= i; | |
390 | } | |
391 | } | |
392 | ||
393 | /* | |
394 | * While we overlap succeeding segments trim them or, | |
395 | * if they are completely covered, dequeue them. | |
396 | */ | |
397 | while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { | |
398 | i = (ip->ip_off + ip->ip_len) - q->ip_off; | |
399 | if (i < q->ip_len) { | |
400 | q->ip_len -= i; | |
401 | q->ip_off += i; | |
402 | m_adj(dtom(q), i); | |
403 | break; | |
404 | } | |
405 | q = q->ipf_next; | |
406 | m_freem(dtom(q->ipf_prev)); | |
407 | ip_deq(q->ipf_prev); | |
408 | } | |
409 | ||
410 | insert: | |
411 | /* | |
412 | * Stick new segment in its place; | |
413 | * check for complete reassembly. | |
414 | */ | |
415 | ip_enq(ip, q->ipf_prev); | |
416 | next = 0; | |
417 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { | |
418 | if (q->ip_off != next) | |
419 | return (0); | |
420 | next += q->ip_len; | |
421 | } | |
422 | if (q->ipf_prev->ipf_mff) | |
423 | return (0); | |
424 | ||
425 | /* | |
426 | * Reassembly is complete; concatenate fragments. | |
427 | */ | |
428 | q = fp->ipq_next; | |
429 | m = dtom(q); | |
430 | t = m->m_next; | |
431 | m->m_next = 0; | |
432 | m_cat(m, t); | |
433 | q = q->ipf_next; | |
434 | while (q != (struct ipasfrag *)fp) { | |
435 | t = dtom(q); | |
436 | q = q->ipf_next; | |
437 | m_cat(m, t); | |
438 | } | |
439 | ||
440 | /* | |
441 | * Create header for new ip packet by | |
442 | * modifying header of first packet; | |
443 | * dequeue and discard fragment reassembly header. | |
444 | * Make header visible. | |
445 | */ | |
446 | ip = fp->ipq_next; | |
447 | ip->ip_len = next; | |
448 | ((struct ip *)ip)->ip_src = fp->ipq_src; | |
449 | ((struct ip *)ip)->ip_dst = fp->ipq_dst; | |
450 | remque(fp); | |
451 | (void) m_free(dtom(fp)); | |
452 | m = dtom(ip); | |
453 | m->m_len += (ip->ip_hl << 2); | |
454 | m->m_data -= (ip->ip_hl << 2); | |
455 | /* some debugging cruft by sklower, below, will go away soon */ | |
456 | if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ | |
457 | register int plen = 0; | |
458 | for (t = m; m; m = m->m_next) | |
459 | plen += m->m_len; | |
460 | t->m_pkthdr.len = plen; | |
461 | } | |
462 | return ((struct ip *)ip); | |
463 | ||
464 | dropfrag: | |
465 | ipstat.ips_fragdropped++; | |
466 | m_freem(m); | |
467 | return (0); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Free a fragment reassembly header and all | |
472 | * associated datagrams. | |
473 | */ | |
4c45483e | 474 | static void |
15637ed4 RG |
475 | ip_freef(fp) |
476 | struct ipq *fp; | |
477 | { | |
478 | register struct ipasfrag *q, *p; | |
479 | ||
480 | for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { | |
481 | p = q->ipf_next; | |
482 | ip_deq(q); | |
483 | m_freem(dtom(q)); | |
484 | } | |
485 | remque(fp); | |
486 | (void) m_free(dtom(fp)); | |
487 | } | |
488 | ||
489 | /* | |
490 | * Put an ip fragment on a reassembly chain. | |
491 | * Like insque, but pointers in middle of structure. | |
492 | */ | |
4c45483e | 493 | static void |
15637ed4 RG |
494 | ip_enq(p, prev) |
495 | register struct ipasfrag *p, *prev; | |
496 | { | |
497 | ||
498 | p->ipf_prev = prev; | |
499 | p->ipf_next = prev->ipf_next; | |
500 | prev->ipf_next->ipf_prev = p; | |
501 | prev->ipf_next = p; | |
502 | } | |
503 | ||
504 | /* | |
505 | * To ip_enq as remque is to insque. | |
506 | */ | |
4c45483e | 507 | static void |
15637ed4 RG |
508 | ip_deq(p) |
509 | register struct ipasfrag *p; | |
510 | { | |
511 | ||
512 | p->ipf_prev->ipf_next = p->ipf_next; | |
513 | p->ipf_next->ipf_prev = p->ipf_prev; | |
514 | } | |
515 | ||
516 | /* | |
517 | * IP timer processing; | |
518 | * if a timer expires on a reassembly | |
519 | * queue, discard it. | |
520 | */ | |
4c45483e | 521 | void |
15637ed4 RG |
522 | ip_slowtimo() |
523 | { | |
524 | register struct ipq *fp; | |
525 | int s = splnet(); | |
526 | ||
527 | fp = ipq.next; | |
528 | if (fp == 0) { | |
529 | splx(s); | |
530 | return; | |
531 | } | |
532 | while (fp != &ipq) { | |
533 | --fp->ipq_ttl; | |
534 | fp = fp->next; | |
535 | if (fp->prev->ipq_ttl == 0) { | |
536 | ipstat.ips_fragtimeout++; | |
537 | ip_freef(fp->prev); | |
538 | } | |
539 | } | |
540 | splx(s); | |
541 | } | |
542 | ||
543 | /* | |
544 | * Drain off all datagram fragments. | |
545 | */ | |
4c45483e | 546 | void |
15637ed4 RG |
547 | ip_drain() |
548 | { | |
549 | ||
550 | while (ipq.next != &ipq) { | |
551 | ipstat.ips_fragdropped++; | |
552 | ip_freef(ipq.next); | |
553 | } | |
554 | } | |
555 | ||
556 | extern struct in_ifaddr *ifptoia(); | |
557 | struct in_ifaddr *ip_rtaddr(); | |
558 | ||
559 | /* | |
560 | * Do option processing on a datagram, | |
561 | * possibly discarding it if bad options are encountered, | |
562 | * or forwarding it if source-routed. | |
563 | * Returns 1 if packet has been forwarded/freed, | |
564 | * 0 if the packet should be processed further. | |
565 | */ | |
4c45483e | 566 | int |
15637ed4 RG |
567 | ip_dooptions(m) |
568 | struct mbuf *m; | |
569 | { | |
570 | register struct ip *ip = mtod(m, struct ip *); | |
571 | register u_char *cp; | |
572 | register struct ip_timestamp *ipt; | |
573 | register struct in_ifaddr *ia; | |
574 | int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; | |
575 | struct in_addr *sin; | |
576 | n_time ntime; | |
577 | ||
578 | cp = (u_char *)(ip + 1); | |
579 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
580 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
581 | opt = cp[IPOPT_OPTVAL]; | |
582 | if (opt == IPOPT_EOL) | |
583 | break; | |
584 | if (opt == IPOPT_NOP) | |
585 | optlen = 1; | |
586 | else { | |
587 | optlen = cp[IPOPT_OLEN]; | |
588 | if (optlen <= 0 || optlen > cnt) { | |
589 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
590 | goto bad; | |
591 | } | |
592 | } | |
593 | switch (opt) { | |
594 | ||
595 | default: | |
596 | break; | |
597 | ||
598 | /* | |
599 | * Source routing with record. | |
600 | * Find interface with current destination address. | |
601 | * If none on this machine then drop if strictly routed, | |
602 | * or do nothing if loosely routed. | |
603 | * Record interface address and bring up next address | |
604 | * component. If strictly routed make sure next | |
605 | * address is on directly accessible net. | |
606 | */ | |
607 | case IPOPT_LSRR: | |
608 | case IPOPT_SSRR: | |
609 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
610 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
611 | goto bad; | |
612 | } | |
613 | ipaddr.sin_addr = ip->ip_dst; | |
614 | ia = (struct in_ifaddr *) | |
615 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
616 | if (ia == 0) { | |
617 | if (opt == IPOPT_SSRR) { | |
618 | type = ICMP_UNREACH; | |
619 | code = ICMP_UNREACH_SRCFAIL; | |
620 | goto bad; | |
621 | } | |
622 | /* | |
623 | * Loose routing, and not at next destination | |
624 | * yet; nothing to do except forward. | |
625 | */ | |
626 | break; | |
627 | } | |
628 | off--; /* 0 origin */ | |
629 | if (off > optlen - sizeof(struct in_addr)) { | |
630 | /* | |
631 | * End of source route. Should be for us. | |
632 | */ | |
633 | save_rte(cp, ip->ip_src); | |
634 | break; | |
635 | } | |
636 | /* | |
637 | * locate outgoing interface | |
638 | */ | |
639 | bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, | |
640 | sizeof(ipaddr.sin_addr)); | |
641 | if (opt == IPOPT_SSRR) { | |
642 | #define INA struct in_ifaddr * | |
643 | #define SA struct sockaddr * | |
644 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) | |
645 | ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); | |
646 | } else | |
647 | ia = ip_rtaddr(ipaddr.sin_addr); | |
648 | if (ia == 0) { | |
649 | type = ICMP_UNREACH; | |
650 | code = ICMP_UNREACH_SRCFAIL; | |
651 | goto bad; | |
652 | } | |
653 | ip->ip_dst = ipaddr.sin_addr; | |
654 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
655 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
656 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
657 | forward = 1; | |
658 | break; | |
659 | ||
660 | case IPOPT_RR: | |
661 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
662 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
663 | goto bad; | |
664 | } | |
665 | /* | |
666 | * If no space remains, ignore. | |
667 | */ | |
668 | off--; /* 0 origin */ | |
669 | if (off > optlen - sizeof(struct in_addr)) | |
670 | break; | |
671 | bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, | |
672 | sizeof(ipaddr.sin_addr)); | |
673 | /* | |
674 | * locate outgoing interface; if we're the destination, | |
675 | * use the incoming interface (should be same). | |
676 | */ | |
677 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && | |
678 | (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { | |
679 | type = ICMP_UNREACH; | |
680 | code = ICMP_UNREACH_HOST; | |
681 | goto bad; | |
682 | } | |
683 | bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), | |
684 | (caddr_t)(cp + off), sizeof(struct in_addr)); | |
685 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
686 | break; | |
687 | ||
688 | case IPOPT_TS: | |
689 | code = cp - (u_char *)ip; | |
690 | ipt = (struct ip_timestamp *)cp; | |
691 | if (ipt->ipt_len < 5) | |
692 | goto bad; | |
693 | if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { | |
694 | if (++ipt->ipt_oflw == 0) | |
695 | goto bad; | |
696 | break; | |
697 | } | |
698 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
699 | switch (ipt->ipt_flg) { | |
700 | ||
701 | case IPOPT_TS_TSONLY: | |
702 | break; | |
703 | ||
704 | case IPOPT_TS_TSANDADDR: | |
705 | if (ipt->ipt_ptr + sizeof(n_time) + | |
706 | sizeof(struct in_addr) > ipt->ipt_len) | |
707 | goto bad; | |
708 | ia = ifptoia(m->m_pkthdr.rcvif); | |
709 | bcopy((caddr_t)&IA_SIN(ia)->sin_addr, | |
710 | (caddr_t)sin, sizeof(struct in_addr)); | |
711 | ipt->ipt_ptr += sizeof(struct in_addr); | |
712 | break; | |
713 | ||
714 | case IPOPT_TS_PRESPEC: | |
715 | if (ipt->ipt_ptr + sizeof(n_time) + | |
716 | sizeof(struct in_addr) > ipt->ipt_len) | |
717 | goto bad; | |
718 | bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, | |
719 | sizeof(struct in_addr)); | |
720 | if (ifa_ifwithaddr((SA)&ipaddr) == 0) | |
721 | continue; | |
722 | ipt->ipt_ptr += sizeof(struct in_addr); | |
723 | break; | |
724 | ||
725 | default: | |
726 | goto bad; | |
727 | } | |
728 | ntime = iptime(); | |
729 | bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, | |
730 | sizeof(n_time)); | |
731 | ipt->ipt_ptr += sizeof(n_time); | |
732 | } | |
733 | } | |
734 | if (forward) { | |
735 | ip_forward(m, 1); | |
736 | return (1); | |
737 | } else | |
738 | return (0); | |
739 | bad: | |
2cb63509 GW |
740 | { |
741 | static struct in_addr fake; | |
742 | icmp_error(m, type, code, fake, 0); | |
743 | } | |
15637ed4 RG |
744 | return (1); |
745 | } | |
746 | ||
747 | /* | |
748 | * Given address of next destination (final or next hop), | |
749 | * return internet address info of interface to be used to get there. | |
750 | */ | |
751 | struct in_ifaddr * | |
752 | ip_rtaddr(dst) | |
753 | struct in_addr dst; | |
754 | { | |
755 | register struct sockaddr_in *sin; | |
756 | ||
757 | sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; | |
758 | ||
759 | if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { | |
760 | if (ipforward_rt.ro_rt) { | |
761 | RTFREE(ipforward_rt.ro_rt); | |
762 | ipforward_rt.ro_rt = 0; | |
763 | } | |
764 | sin->sin_family = AF_INET; | |
765 | sin->sin_len = sizeof(*sin); | |
766 | sin->sin_addr = dst; | |
767 | ||
768 | rtalloc(&ipforward_rt); | |
769 | } | |
770 | if (ipforward_rt.ro_rt == 0) | |
771 | return ((struct in_ifaddr *)0); | |
772 | return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); | |
773 | } | |
774 | ||
775 | /* | |
776 | * Save incoming source route for use in replies, | |
777 | * to be picked up later by ip_srcroute if the receiver is interested. | |
778 | */ | |
4c45483e | 779 | static void |
15637ed4 RG |
780 | save_rte(option, dst) |
781 | u_char *option; | |
782 | struct in_addr dst; | |
783 | { | |
784 | unsigned olen; | |
785 | ||
786 | olen = option[IPOPT_OLEN]; | |
787 | #ifdef DIAGNOSTIC | |
788 | if (ipprintfs) | |
789 | printf("save_rte: olen %d\n", olen); | |
790 | #endif | |
791 | if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) | |
792 | return; | |
793 | bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); | |
794 | ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); | |
795 | ip_srcrt.dst = dst; | |
796 | } | |
797 | ||
798 | /* | |
799 | * Retrieve incoming source route for use in replies, | |
800 | * in the same form used by setsockopt. | |
801 | * The first hop is placed before the options, will be removed later. | |
802 | */ | |
803 | struct mbuf * | |
804 | ip_srcroute() | |
805 | { | |
806 | register struct in_addr *p, *q; | |
807 | register struct mbuf *m; | |
808 | ||
809 | if (ip_nhops == 0) | |
810 | return ((struct mbuf *)0); | |
811 | m = m_get(M_DONTWAIT, MT_SOOPTS); | |
812 | if (m == 0) | |
813 | return ((struct mbuf *)0); | |
814 | ||
815 | #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) | |
816 | ||
817 | /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ | |
818 | m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + | |
819 | OPTSIZ; | |
820 | #ifdef DIAGNOSTIC | |
821 | if (ipprintfs) | |
822 | printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); | |
823 | #endif | |
824 | ||
825 | /* | |
826 | * First save first hop for return route | |
827 | */ | |
828 | p = &ip_srcrt.route[ip_nhops - 1]; | |
829 | *(mtod(m, struct in_addr *)) = *p--; | |
830 | #ifdef DIAGNOSTIC | |
831 | if (ipprintfs) | |
832 | printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); | |
833 | #endif | |
834 | ||
835 | /* | |
836 | * Copy option fields and padding (nop) to mbuf. | |
837 | */ | |
838 | ip_srcrt.nop = IPOPT_NOP; | |
839 | ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; | |
840 | bcopy((caddr_t)&ip_srcrt.nop, | |
841 | mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); | |
842 | q = (struct in_addr *)(mtod(m, caddr_t) + | |
843 | sizeof(struct in_addr) + OPTSIZ); | |
844 | #undef OPTSIZ | |
845 | /* | |
846 | * Record return path as an IP source route, | |
847 | * reversing the path (pointers are now aligned). | |
848 | */ | |
849 | while (p >= ip_srcrt.route) { | |
850 | #ifdef DIAGNOSTIC | |
851 | if (ipprintfs) | |
852 | printf(" %lx", ntohl(q->s_addr)); | |
853 | #endif | |
854 | *q++ = *p--; | |
855 | } | |
856 | /* | |
857 | * Last hop goes to final destination. | |
858 | */ | |
859 | *q = ip_srcrt.dst; | |
860 | #ifdef DIAGNOSTIC | |
861 | if (ipprintfs) | |
862 | printf(" %lx\n", ntohl(q->s_addr)); | |
863 | #endif | |
864 | return (m); | |
865 | } | |
866 | ||
867 | /* | |
868 | * Strip out IP options, at higher | |
869 | * level protocol in the kernel. | |
870 | * Second argument is buffer to which options | |
871 | * will be moved, and return value is their length. | |
872 | * XXX should be deleted; last arg currently ignored. | |
873 | */ | |
4c45483e | 874 | void |
15637ed4 RG |
875 | ip_stripoptions(m, mopt) |
876 | register struct mbuf *m; | |
877 | struct mbuf *mopt; | |
878 | { | |
879 | register int i; | |
880 | struct ip *ip = mtod(m, struct ip *); | |
881 | register caddr_t opts; | |
882 | int olen; | |
883 | ||
884 | olen = (ip->ip_hl<<2) - sizeof (struct ip); | |
885 | opts = (caddr_t)(ip + 1); | |
886 | i = m->m_len - (sizeof (struct ip) + olen); | |
887 | bcopy(opts + olen, opts, (unsigned)i); | |
888 | m->m_len -= olen; | |
889 | if (m->m_flags & M_PKTHDR) | |
890 | m->m_pkthdr.len -= olen; | |
891 | ip->ip_hl = sizeof(struct ip) >> 2; | |
892 | } | |
893 | ||
15637ed4 RG |
894 | /* |
895 | * Forward a packet. If some error occurs return the sender | |
896 | * an icmp packet. Note we can't always generate a meaningful | |
897 | * icmp message because icmp doesn't have a large enough repertoire | |
898 | * of codes and types. | |
899 | * | |
900 | * If not forwarding, just drop the packet. This could be confusing | |
901 | * if ipforwarding was zero but some routing protocol was advancing | |
902 | * us as a gateway to somewhere. However, we must let the routing | |
903 | * protocol deal with that. | |
904 | * | |
905 | * The srcrt parameter indicates whether the packet is being forwarded | |
906 | * via a source route. | |
907 | */ | |
4c45483e | 908 | static void |
15637ed4 RG |
909 | ip_forward(m, srcrt) |
910 | struct mbuf *m; | |
911 | int srcrt; | |
912 | { | |
913 | register struct ip *ip = mtod(m, struct ip *); | |
914 | register struct sockaddr_in *sin; | |
915 | register struct rtentry *rt; | |
4c45483e | 916 | int error, type = 0, code = 0; |
15637ed4 RG |
917 | struct mbuf *mcopy; |
918 | struct in_addr dest; | |
4c45483e | 919 | int mtu = 0; |
15637ed4 RG |
920 | |
921 | dest.s_addr = 0; | |
922 | #ifdef DIAGNOSTIC | |
923 | if (ipprintfs) | |
924 | printf("forward: src %x dst %x ttl %x\n", ip->ip_src, | |
925 | ip->ip_dst, ip->ip_ttl); | |
926 | #endif | |
927 | if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { | |
928 | ipstat.ips_cantforward++; | |
929 | m_freem(m); | |
930 | return; | |
931 | } | |
932 | HTONS(ip->ip_id); | |
933 | if (ip->ip_ttl <= IPTTLDEC) { | |
2cb63509 | 934 | icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); |
15637ed4 RG |
935 | return; |
936 | } | |
937 | ip->ip_ttl -= IPTTLDEC; | |
938 | ||
939 | sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; | |
940 | if ((rt = ipforward_rt.ro_rt) == 0 || | |
941 | ip->ip_dst.s_addr != sin->sin_addr.s_addr) { | |
942 | if (ipforward_rt.ro_rt) { | |
943 | RTFREE(ipforward_rt.ro_rt); | |
944 | ipforward_rt.ro_rt = 0; | |
945 | } | |
946 | sin->sin_family = AF_INET; | |
947 | sin->sin_len = sizeof(*sin); | |
948 | sin->sin_addr = ip->ip_dst; | |
949 | ||
950 | rtalloc(&ipforward_rt); | |
951 | if (ipforward_rt.ro_rt == 0) { | |
2cb63509 | 952 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); |
15637ed4 RG |
953 | return; |
954 | } | |
955 | rt = ipforward_rt.ro_rt; | |
2cb63509 GW |
956 | mtu = rt->rt_ifp->if_mtu; |
957 | /* salt away if's mtu */ | |
15637ed4 RG |
958 | } |
959 | ||
960 | /* | |
961 | * Save at most 64 bytes of the packet in case | |
962 | * we need to generate an ICMP message to the src. | |
963 | */ | |
964 | mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); | |
965 | ||
966 | #ifdef GATEWAY | |
967 | ip_ifmatrix[rt->rt_ifp->if_index + | |
968 | if_index * m->m_pkthdr.rcvif->if_index]++; | |
969 | #endif | |
970 | /* | |
971 | * If forwarding packet using same interface that it came in on, | |
972 | * perhaps should send a redirect to sender to shortcut a hop. | |
973 | * Only send redirect if source is sending directly to us, | |
974 | * and if packet was not source routed (or has any options). | |
975 | * Also, don't send redirect if forwarding using a default route | |
976 | * or a route modified by a redirect. | |
977 | */ | |
978 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
979 | if (rt->rt_ifp == m->m_pkthdr.rcvif && | |
980 | (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && | |
981 | satosin(rt_key(rt))->sin_addr.s_addr != 0 && | |
982 | ipsendredirects && !srcrt) { | |
983 | struct in_ifaddr *ia; | |
984 | u_long src = ntohl(ip->ip_src.s_addr); | |
985 | u_long dst = ntohl(ip->ip_dst.s_addr); | |
986 | ||
987 | if ((ia = ifptoia(m->m_pkthdr.rcvif)) && | |
988 | (src & ia->ia_subnetmask) == ia->ia_subnet) { | |
989 | if (rt->rt_flags & RTF_GATEWAY) | |
990 | dest = satosin(rt->rt_gateway)->sin_addr; | |
991 | else | |
992 | dest = ip->ip_dst; | |
993 | /* | |
994 | * If the destination is reached by a route to host, | |
995 | * is on a subnet of a local net, or is directly | |
996 | * on the attached net (!), use host redirect. | |
997 | * (We may be the correct first hop for other subnets.) | |
998 | */ | |
999 | #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) | |
1000 | type = ICMP_REDIRECT; | |
1001 | if ((rt->rt_flags & RTF_HOST) || | |
1002 | (rt->rt_flags & RTF_GATEWAY) == 0) | |
1003 | code = ICMP_REDIRECT_HOST; | |
1004 | else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && | |
1005 | (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) | |
1006 | code = ICMP_REDIRECT_HOST; | |
1007 | else | |
1008 | code = ICMP_REDIRECT_NET; | |
1009 | #ifdef DIAGNOSTIC | |
1010 | if (ipprintfs) | |
1011 | printf("redirect (%d) to %x\n", code, dest.s_addr); | |
1012 | #endif | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); | |
1017 | if (error) | |
1018 | ipstat.ips_cantforward++; | |
1019 | else { | |
1020 | ipstat.ips_forward++; | |
1021 | if (type) | |
1022 | ipstat.ips_redirectsent++; | |
1023 | else { | |
1024 | if (mcopy) | |
1025 | m_freem(mcopy); | |
1026 | return; | |
1027 | } | |
1028 | } | |
1029 | if (mcopy == NULL) | |
1030 | return; | |
1031 | switch (error) { | |
1032 | ||
1033 | case 0: /* forwarded, but need redirect */ | |
1034 | /* type, code set above */ | |
1035 | break; | |
1036 | ||
1037 | case ENETUNREACH: /* shouldn't happen, checked above */ | |
1038 | case EHOSTUNREACH: | |
1039 | case ENETDOWN: | |
1040 | case EHOSTDOWN: | |
1041 | default: | |
1042 | type = ICMP_UNREACH; | |
1043 | code = ICMP_UNREACH_HOST; | |
1044 | break; | |
1045 | ||
1046 | case EMSGSIZE: | |
1047 | type = ICMP_UNREACH; | |
1048 | code = ICMP_UNREACH_NEEDFRAG; | |
1049 | ipstat.ips_cantfrag++; | |
1050 | break; | |
1051 | ||
1052 | case ENOBUFS: | |
1053 | type = ICMP_SOURCEQUENCH; | |
1054 | code = 0; | |
1055 | break; | |
1056 | } | |
2cb63509 | 1057 | icmp_error(mcopy, type, code, dest, mtu); |
15637ed4 | 1058 | } |