]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1986, 1988, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 | |
55 | * $ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $ | |
56 | */ | |
57 | ||
58 | #define _IP_VHL | |
59 | ||
60 | #include <stddef.h> | |
61 | ||
62 | #include <sys/param.h> | |
63 | #include <sys/systm.h> | |
64 | #include <sys/mbuf.h> | |
65 | #include <sys/malloc.h> | |
66 | #include <sys/domain.h> | |
67 | #include <sys/protosw.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/time.h> | |
70 | #include <sys/kernel.h> | |
71 | #include <sys/syslog.h> | |
72 | #include <sys/sysctl.h> | |
73 | ||
74 | #include <kern/queue.h> | |
75 | ||
76 | #include <net/if.h> | |
77 | #include <net/if_var.h> | |
78 | #include <net/if_dl.h> | |
79 | #include <net/route.h> | |
80 | #include <net/netisr.h> | |
81 | ||
82 | #include <netinet/in.h> | |
83 | #include <netinet/in_systm.h> | |
84 | #include <netinet/in_var.h> | |
85 | #include <netinet/ip.h> | |
86 | #ifdef INET6 | |
87 | #include <netinet/ip6.h> | |
88 | #include <netinet6/ip6_var.h> | |
89 | #endif | |
90 | #include <netinet/in_pcb.h> | |
91 | #include <netinet/ip_var.h> | |
92 | #include <netinet/ip_icmp.h> | |
93 | #include <sys/socketvar.h> | |
94 | ||
95 | #include <sys/kdebug.h> | |
96 | ||
97 | ||
98 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0) | |
99 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2) | |
100 | #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8)) | |
101 | ||
102 | ||
103 | #if IPFIREWALL | |
104 | #include <netinet/ip_fw.h> | |
105 | #endif | |
106 | ||
107 | #if IPSEC | |
108 | #include <netinet6/ipsec.h> | |
109 | #include <netkey/key.h> | |
110 | #include <netkey/key_debug.h> | |
111 | #endif | |
112 | ||
113 | #include "faith.h" | |
114 | #if defined(NFAITH) && NFAITH > 0 | |
115 | #include <net/if_types.h> | |
116 | #endif | |
117 | ||
118 | #if DUMMYNET | |
119 | #include <netinet/ip_dummynet.h> | |
120 | #endif | |
121 | ||
122 | int rsvp_on = 0; | |
123 | static int ip_rsvp_on; | |
124 | struct socket *ip_rsvpd; | |
125 | ||
126 | int ipforwarding = 0; | |
127 | SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, | |
128 | &ipforwarding, 0, ""); | |
129 | ||
130 | static int ipsendredirects = 1; /* XXX */ | |
131 | SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, | |
132 | &ipsendredirects, 0, ""); | |
133 | ||
134 | int ip_defttl = IPDEFTTL; | |
135 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, | |
136 | &ip_defttl, 0, ""); | |
137 | ||
138 | static int ip_dosourceroute = 0; | |
139 | SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, | |
140 | &ip_dosourceroute, 0, ""); | |
141 | ||
142 | static int ip_acceptsourceroute = 0; | |
143 | SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, | |
144 | CTLFLAG_RW, &ip_acceptsourceroute, 0, ""); | |
145 | ||
146 | static int ip_keepfaith = 0; | |
147 | SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, | |
148 | &ip_keepfaith, 0, ""); | |
149 | ||
150 | #if DIAGNOSTIC | |
151 | static int ipprintfs = 0; | |
152 | #endif | |
153 | ||
154 | extern struct domain inetdomain; | |
155 | extern struct protosw inetsw[]; | |
156 | struct protosw *ip_protox[IPPROTO_MAX]; | |
157 | static int ipqmaxlen = IFQ_MAXLEN; | |
158 | struct in_ifaddrhead in_ifaddrhead; /* first inet address */ | |
159 | struct ifqueue ipintrq; | |
160 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD, | |
161 | &ipintrq.ifq_maxlen, 0, ""); | |
162 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, | |
163 | &ipintrq.ifq_drops, 0, ""); | |
164 | ||
165 | struct ipstat ipstat; | |
166 | SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD, | |
167 | &ipstat, ipstat, ""); | |
168 | ||
169 | /* Packet reassembly stuff */ | |
170 | #define IPREASS_NHASH_LOG2 6 | |
171 | #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) | |
172 | #define IPREASS_HMASK (IPREASS_NHASH - 1) | |
173 | #define IPREASS_HASH(x,y) \ | |
174 | ((((x) & 0xF | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) | |
175 | ||
176 | static struct ipq ipq[IPREASS_NHASH]; | |
177 | static int nipq = 0; /* total # of reass queues */ | |
178 | static int maxnipq; | |
179 | ||
180 | #if IPCTL_DEFMTU | |
181 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, | |
182 | &ip_mtu, 0, ""); | |
183 | #endif | |
184 | ||
185 | #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 | |
186 | #undef COMPAT_IPFW | |
187 | #define COMPAT_IPFW 1 | |
188 | #else | |
189 | #undef COMPAT_IPFW | |
190 | #endif | |
191 | ||
192 | #if COMPAT_IPFW | |
193 | ||
194 | #include <netinet/ip_fw.h> | |
195 | ||
196 | /* Firewall hooks */ | |
197 | ip_fw_chk_t *ip_fw_chk_ptr; | |
198 | ip_fw_ctl_t *ip_fw_ctl_ptr; | |
199 | ||
200 | #if DUMMYNET | |
201 | ip_dn_ctl_t *ip_dn_ctl_ptr; | |
202 | #endif | |
203 | ||
204 | /* IP Network Address Translation (NAT) hooks */ | |
205 | ip_nat_t *ip_nat_ptr; | |
206 | ip_nat_ctl_t *ip_nat_ctl_ptr; | |
207 | #endif | |
208 | ||
1c79356b A |
209 | /* |
210 | * We need to save the IP options in case a protocol wants to respond | |
211 | * to an incoming packet over the same route if the packet got here | |
212 | * using IP source routing. This allows connection establishment and | |
213 | * maintenance when the remote end is on a network that is not known | |
214 | * to us. | |
215 | */ | |
216 | static int ip_nhops = 0; | |
217 | static struct ip_srcrt { | |
218 | struct in_addr dst; /* final destination */ | |
219 | char nop; /* one NOP to align */ | |
220 | char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ | |
221 | struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; | |
222 | } ip_srcrt; | |
223 | ||
224 | #if IPDIVERT | |
225 | /* | |
226 | * Shared variable between ip_input() and ip_reass() to communicate | |
227 | * about which packets, once assembled from fragments, get diverted, | |
228 | * and to which port. | |
229 | */ | |
230 | static u_short frag_divert_port; | |
231 | #endif | |
232 | ||
233 | struct sockaddr_in *ip_fw_fwd_addr; | |
234 | ||
235 | static void save_rte __P((u_char *, struct in_addr)); | |
236 | static int ip_dooptions __P((struct mbuf *)); | |
237 | #ifndef NATPT | |
238 | static | |
239 | #endif | |
240 | void ip_forward __P((struct mbuf *, int)); | |
241 | static void ip_freef __P((struct ipq *)); | |
242 | static struct ip * | |
243 | ip_reass __P((struct mbuf *, struct ipq *, struct ipq *)); | |
244 | static struct in_ifaddr * | |
245 | ip_rtaddr __P((struct in_addr)); | |
246 | void ipintr __P((void)); | |
247 | ||
248 | #if PM | |
249 | extern int doNatFil; | |
250 | extern int doRoute; | |
251 | ||
252 | extern int pm_in __P((struct ifnet *, struct ip *, struct mbuf *)); | |
253 | extern struct route *pm_route __P((struct mbuf *)); | |
254 | #endif | |
255 | ||
256 | #if defined(NATPT) | |
257 | extern int ip6_protocol_tr; | |
258 | ||
259 | int natpt_in4 __P((struct mbuf *, struct mbuf **)); | |
260 | ||
261 | #endif /* NATPT */ | |
262 | ||
263 | /* | |
264 | * IP initialization: fill in IP protocol switch table. | |
265 | * All protocols not implemented in kernel go to raw IP protocol handler. | |
266 | */ | |
267 | void | |
268 | ip_init() | |
269 | { | |
270 | register struct protosw *pr; | |
271 | register int i; | |
272 | static ip_initialized = 0; | |
273 | ||
274 | if (!ip_initialized) | |
275 | { | |
276 | TAILQ_INIT(&in_ifaddrhead); | |
277 | pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); | |
278 | if (pr == 0) | |
279 | panic("ip_init"); | |
280 | for (i = 0; i < IPPROTO_MAX; i++) | |
281 | ip_protox[i] = pr; | |
282 | for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next) | |
283 | { if(!((unsigned int)pr->pr_domain)) continue; /* If uninitialized, skip */ | |
284 | if (pr->pr_domain->dom_family == PF_INET && | |
285 | pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) | |
286 | ip_protox[pr->pr_protocol] = pr; | |
287 | } | |
288 | for (i = 0; i < IPREASS_NHASH; i++) | |
289 | ipq[i].next = ipq[i].prev = &ipq[i]; | |
290 | ||
291 | maxnipq = nmbclusters/4; | |
292 | ||
293 | ip_id = time_second & 0xffff; | |
294 | ipintrq.ifq_maxlen = ipqmaxlen; | |
295 | #if DUMMYNET | |
296 | ip_dn_init(); | |
297 | #endif | |
298 | #if IPNAT | |
299 | ip_nat_init(); | |
1c79356b A |
300 | #endif |
301 | ip_initialized = 1; | |
302 | } | |
303 | } | |
304 | ||
305 | /* Initialize the PF_INET domain, and add in the pre-defined protos */ | |
306 | void | |
307 | in_dinit() | |
308 | { register int i; | |
309 | register struct protosw *pr; | |
310 | register struct domain *dp; | |
311 | static inetdomain_initted = 0; | |
312 | extern int in_proto_count; | |
313 | ||
314 | if (!inetdomain_initted) | |
315 | { kprintf("Initing %d protosw entries\n", in_proto_count); | |
316 | dp = &inetdomain; | |
317 | ||
318 | for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++) | |
319 | net_add_proto(pr, dp); | |
320 | inetdomain_initted = 1; | |
321 | } | |
322 | } | |
323 | ||
324 | static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; | |
325 | static struct route ipforward_rt; | |
326 | ||
327 | /* | |
328 | * Ip input routine. Checksum and byte swap header. If fragmented | |
329 | * try to reassemble. Process options. Pass to next level. | |
330 | */ | |
331 | void | |
332 | ip_input(struct mbuf *m) | |
333 | { | |
334 | struct ip *ip; | |
335 | struct ipq *fp; | |
336 | struct in_ifaddr *ia; | |
337 | int i, hlen, mff; | |
338 | u_short sum; | |
339 | #if !IPDIVERT /* dummy variable for the firewall code to play with */ | |
340 | u_short ip_divert_cookie = 0 ; | |
341 | #endif | |
342 | #if COMPAT_IPFW | |
343 | struct ip_fw_chain *rule = NULL ; | |
344 | #endif | |
345 | ||
346 | #if IPFIREWALL && DUMMYNET | |
347 | /* | |
348 | * dummynet packet are prepended a vestigial mbuf with | |
349 | * m_type = MT_DUMMYNET and m_data pointing to the matching | |
350 | * rule. | |
351 | */ | |
352 | if (m->m_type == MT_DUMMYNET) { | |
353 | struct mbuf *m0 = m ; | |
354 | rule = (struct ip_fw_chain *)(m->m_data) ; | |
355 | m = m->m_next ; | |
356 | FREE(m0, M_IPFW); | |
357 | ip = mtod(m, struct ip *); | |
358 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
359 | goto iphack ; | |
360 | } else | |
361 | rule = NULL ; | |
362 | #endif | |
363 | ||
364 | #if DIAGNOSTIC | |
365 | if (m == NULL || (m->m_flags & M_PKTHDR) == 0) | |
366 | panic("ip_input no HDR"); | |
367 | #endif | |
368 | /* | |
369 | * If no IP addresses have been set yet but the interfaces | |
370 | * are receiving, can't do anything with incoming packets yet. | |
371 | * XXX This is broken! We should be able to receive broadcasts | |
372 | * and multicasts even without any local addresses configured. | |
373 | */ | |
374 | if (TAILQ_EMPTY(&in_ifaddrhead)) | |
375 | goto bad; | |
376 | ipstat.ips_total++; | |
377 | ||
378 | if (m->m_pkthdr.len < sizeof(struct ip)) | |
379 | goto tooshort; | |
380 | ||
381 | if (m->m_len < sizeof (struct ip) && | |
382 | (m = m_pullup(m, sizeof (struct ip))) == 0) { | |
383 | ipstat.ips_toosmall++; | |
384 | return; | |
385 | } | |
386 | ip = mtod(m, struct ip *); | |
387 | ||
388 | KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr, | |
389 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
390 | ||
391 | if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { | |
392 | ipstat.ips_badvers++; | |
393 | goto bad; | |
394 | } | |
395 | ||
396 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
397 | if (hlen < sizeof(struct ip)) { /* minimum header length */ | |
398 | ipstat.ips_badhlen++; | |
399 | goto bad; | |
400 | } | |
401 | if (hlen > m->m_len) { | |
402 | if ((m = m_pullup(m, hlen)) == 0) { | |
403 | ipstat.ips_badhlen++; | |
404 | return; | |
405 | } | |
406 | ip = mtod(m, struct ip *); | |
407 | } | |
408 | ||
0b4e3aa0 A |
409 | if (m->m_pkthdr.rcvif->if_hwassist == 0) |
410 | m->m_pkthdr.csum_flags = 0; | |
411 | ||
412 | if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { | |
413 | sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); | |
414 | } else | |
415 | sum = in_cksum(m, hlen); | |
1c79356b A |
416 | |
417 | if (sum) { | |
418 | ipstat.ips_badsum++; | |
419 | goto bad; | |
420 | } | |
421 | ||
422 | /* | |
423 | * Convert fields to host representation. | |
424 | */ | |
425 | NTOHS(ip->ip_len); | |
426 | if (ip->ip_len < hlen) { | |
427 | ipstat.ips_badlen++; | |
428 | goto bad; | |
429 | } | |
430 | NTOHS(ip->ip_id); | |
431 | NTOHS(ip->ip_off); | |
432 | ||
433 | /* | |
434 | * Check that the amount of data in the buffers | |
435 | * is as at least much as the IP header would have us expect. | |
436 | * Trim mbufs if longer than we expect. | |
437 | * Drop packet if shorter than we expect. | |
438 | */ | |
439 | if (m->m_pkthdr.len < ip->ip_len) { | |
440 | tooshort: | |
441 | ipstat.ips_tooshort++; | |
442 | goto bad; | |
443 | } | |
444 | if (m->m_pkthdr.len > ip->ip_len) { | |
765c9de3 A |
445 | /* Invalidate hwcksuming */ |
446 | m->m_pkthdr.csum_flags = 0; | |
447 | m->m_pkthdr.csum_data = 0; | |
448 | ||
1c79356b A |
449 | if (m->m_len == m->m_pkthdr.len) { |
450 | m->m_len = ip->ip_len; | |
451 | m->m_pkthdr.len = ip->ip_len; | |
452 | } else | |
453 | m_adj(m, ip->ip_len - m->m_pkthdr.len); | |
454 | } | |
455 | /* | |
456 | * IpHack's section. | |
457 | * Right now when no processing on packet has done | |
458 | * and it is still fresh out of network we do our black | |
459 | * deals with it. | |
460 | * - Firewall: deny/allow/divert | |
461 | * - Xlate: translate packet's addr/port (NAT). | |
462 | * - Pipe: pass pkt through dummynet. | |
463 | * - Wrap: fake packet's addr/port <unimpl.> | |
464 | * - Encapsulate: put it in another IP and send out. <unimp.> | |
465 | */ | |
466 | ||
467 | #if defined(IPFIREWALL) && defined(DUMMYNET) | |
468 | iphack: | |
469 | #endif | |
1c79356b A |
470 | #if COMPAT_IPFW |
471 | if (ip_fw_chk_ptr) { | |
472 | #if IPFIREWALL_FORWARD | |
473 | /* | |
474 | * If we've been forwarded from the output side, then | |
475 | * skip the firewall a second time | |
476 | */ | |
477 | if (ip_fw_fwd_addr) | |
478 | goto ours; | |
479 | #endif /* IPFIREWALL_FORWARD */ | |
480 | i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie, | |
481 | &m, &rule, &ip_fw_fwd_addr); | |
482 | /* | |
483 | * see the comment in ip_output for the return values | |
484 | * produced by the firewall. | |
485 | */ | |
486 | if (!m) /* packet discarded by firewall */ | |
487 | return ; | |
488 | if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ | |
489 | goto pass ; | |
490 | #if DUMMYNET | |
491 | if (i & 0x10000) { | |
492 | /* send packet to the appropriate pipe */ | |
493 | dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule); | |
494 | return ; | |
495 | } | |
496 | #endif | |
497 | #if IPDIVERT | |
498 | if (i > 0 && i < 0x10000) { | |
499 | /* Divert packet */ | |
500 | frag_divert_port = i & 0xffff ; | |
501 | goto ours; | |
502 | } | |
503 | #endif | |
504 | #if IPFIREWALL_FORWARD | |
505 | if (i == 0 && ip_fw_fwd_addr != NULL) | |
506 | goto pass ; | |
507 | #endif | |
508 | /* | |
509 | * if we get here, the packet must be dropped | |
510 | */ | |
511 | m_freem(m); | |
512 | return; | |
513 | } | |
514 | pass: | |
515 | ||
516 | if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN)) { | |
517 | #if IPFIREWALL_FORWARD | |
518 | ip_fw_fwd_addr = NULL; | |
519 | #endif | |
520 | return; | |
521 | } | |
522 | #endif /* !COMPAT_IPFW */ | |
523 | ||
524 | #if defined(PM) | |
525 | /* | |
526 | * Process ip-filter/NAT. | |
527 | * Return TRUE if this packed is discarded. | |
528 | * Return FALSE if this packed is accepted. | |
529 | */ | |
530 | ||
531 | if (doNatFil && pm_in(m->m_pkthdr.rcvif, ip, m)) | |
532 | return; | |
533 | #endif | |
534 | ||
535 | #if defined(NATPT) | |
536 | /* | |
537 | * | |
538 | */ | |
539 | if (ip6_protocol_tr) | |
540 | { | |
541 | struct mbuf *m1 = NULL; | |
542 | ||
543 | switch (natpt_in4(m, &m1)) | |
544 | { | |
545 | case IPPROTO_IP: goto dooptions; | |
546 | case IPPROTO_IPV4: ip_forward(m1, 0); break; | |
547 | case IPPROTO_IPV6: ip6_forward(m1, 1); break; | |
548 | case IPPROTO_MAX: /* discard this packet */ | |
549 | default: | |
550 | } | |
551 | ||
552 | if (m != m1) | |
553 | m_freem(m); | |
554 | ||
555 | return; | |
556 | } | |
557 | dooptions: | |
558 | #endif | |
559 | ||
560 | /* | |
561 | * Process options and, if not destined for us, | |
562 | * ship it on. ip_dooptions returns 1 when an | |
563 | * error was detected (causing an icmp message | |
564 | * to be sent and the original packet to be freed). | |
565 | */ | |
566 | ip_nhops = 0; /* for source routed packets */ | |
567 | if (hlen > sizeof (struct ip) && ip_dooptions(m)) { | |
568 | #if IPFIREWALL_FORWARD | |
569 | ip_fw_fwd_addr = NULL; | |
570 | #endif | |
571 | return; | |
572 | } | |
573 | ||
574 | /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no | |
575 | * matter if it is destined to another node, or whether it is | |
576 | * a multicast one, RSVP wants it! and prevents it from being forwarded | |
577 | * anywhere else. Also checks if the rsvp daemon is running before | |
578 | * grabbing the packet. | |
579 | */ | |
580 | if (rsvp_on && ip->ip_p==IPPROTO_RSVP) | |
581 | goto ours; | |
582 | ||
583 | /* | |
584 | * Check our list of addresses, to see if the packet is for us. | |
585 | */ | |
586 | for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; | |
587 | ia = TAILQ_NEXT(ia, ia_link)) { | |
588 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
589 | ||
590 | if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) | |
591 | goto ours; | |
592 | ||
593 | if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) | |
594 | goto ours; | |
595 | ||
596 | #if IPFIREWALL_FORWARD | |
597 | /* | |
598 | * If the addr to forward to is one of ours, we pretend to | |
599 | * be the destination for this packet. | |
600 | */ | |
601 | if (ip_fw_fwd_addr == NULL) { | |
602 | if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) | |
603 | goto ours; | |
604 | } else if (IA_SIN(ia)->sin_addr.s_addr == | |
605 | ip_fw_fwd_addr->sin_addr.s_addr) | |
606 | goto ours; | |
607 | #else | |
608 | if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) | |
609 | goto ours; | |
610 | #endif | |
611 | if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { | |
612 | if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == | |
613 | ip->ip_dst.s_addr) | |
614 | goto ours; | |
615 | if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) | |
616 | goto ours; | |
617 | } | |
618 | } | |
619 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { | |
620 | struct in_multi *inm; | |
621 | if (ip_mrouter) { | |
622 | /* | |
623 | * If we are acting as a multicast router, all | |
624 | * incoming multicast packets are passed to the | |
625 | * kernel-level multicast forwarding function. | |
626 | * The packet is returned (relatively) intact; if | |
627 | * ip_mforward() returns a non-zero value, the packet | |
628 | * must be discarded, else it may be accepted below. | |
629 | * | |
630 | * (The IP ident field is put in the same byte order | |
631 | * as expected when ip_mforward() is called from | |
632 | * ip_output().) | |
633 | */ | |
634 | ip->ip_id = htons(ip->ip_id); | |
635 | if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { | |
636 | ipstat.ips_cantforward++; | |
637 | m_freem(m); | |
638 | return; | |
639 | } | |
640 | ip->ip_id = ntohs(ip->ip_id); | |
641 | ||
642 | /* | |
643 | * The process-level routing demon needs to receive | |
644 | * all multicast IGMP packets, whether or not this | |
645 | * host belongs to their destination groups. | |
646 | */ | |
647 | if (ip->ip_p == IPPROTO_IGMP) | |
648 | goto ours; | |
649 | ipstat.ips_forward++; | |
650 | } | |
651 | /* | |
652 | * See if we belong to the destination multicast group on the | |
653 | * arrival interface. | |
654 | */ | |
655 | IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); | |
656 | if (inm == NULL) { | |
657 | ipstat.ips_notmember++; | |
658 | m_freem(m); | |
659 | return; | |
660 | } | |
661 | goto ours; | |
662 | } | |
663 | if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) | |
664 | goto ours; | |
665 | if (ip->ip_dst.s_addr == INADDR_ANY) | |
666 | goto ours; | |
667 | ||
0b4e3aa0 A |
668 | if (m->m_pkthdr.rcvif |
669 | && (m->m_pkthdr.rcvif->if_eflags & IFEF_AUTOCONFIGURING) | |
670 | && ip->ip_p == IPPROTO_UDP) { | |
671 | goto ours; | |
672 | } | |
673 | ||
1c79356b A |
674 | #if defined(NFAITH) && NFAITH > 0 |
675 | /* | |
676 | * FAITH(Firewall Aided Internet Translator) | |
677 | */ | |
678 | if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { | |
679 | if (ip_keepfaith) { | |
680 | if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) | |
681 | goto ours; | |
682 | } | |
683 | m_freem(m); | |
684 | return; | |
685 | } | |
686 | #endif | |
687 | /* | |
688 | * Not for us; forward if possible and desirable. | |
689 | */ | |
690 | if (ipforwarding == 0) { | |
691 | ipstat.ips_cantforward++; | |
692 | m_freem(m); | |
693 | } else | |
694 | ip_forward(m, 0); | |
695 | #if IPFIREWALL_FORWARD | |
696 | ip_fw_fwd_addr = NULL; | |
697 | #endif | |
698 | return; | |
699 | ||
700 | ours: | |
701 | ||
702 | /* | |
703 | * If offset or IP_MF are set, must reassemble. | |
704 | * Otherwise, nothing need be done. | |
705 | * (We could look in the reassembly queue to see | |
706 | * if the packet was previously fragmented, | |
707 | * but it's not worth the time; just let them time out.) | |
708 | */ | |
709 | if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { | |
710 | if (m->m_flags & M_EXT) { /* XXX */ | |
711 | if ((m = m_pullup(m, hlen)) == 0) { | |
712 | ipstat.ips_toosmall++; | |
713 | #if IPDIVERT | |
714 | frag_divert_port = 0; | |
715 | ip_divert_cookie = 0; | |
716 | #endif | |
717 | #if IPFIREWALL_FORWARD | |
718 | ip_fw_fwd_addr = NULL; | |
719 | #endif | |
720 | return; | |
721 | } | |
722 | ip = mtod(m, struct ip *); | |
723 | } | |
724 | sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); | |
725 | /* | |
726 | * Look for queue of fragments | |
727 | * of this datagram. | |
728 | */ | |
729 | for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) | |
730 | if (ip->ip_id == fp->ipq_id && | |
731 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
732 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
733 | ip->ip_p == fp->ipq_p) | |
734 | goto found; | |
735 | ||
736 | fp = 0; | |
737 | ||
738 | /* check if there's a place for the new queue */ | |
739 | if (nipq > maxnipq) { | |
740 | /* | |
741 | * drop something from the tail of the current queue | |
742 | * before proceeding further | |
743 | */ | |
744 | if (ipq[sum].prev == &ipq[sum]) { /* gak */ | |
745 | for (i = 0; i < IPREASS_NHASH; i++) { | |
746 | if (ipq[i].prev != &ipq[i]) { | |
747 | ip_freef(ipq[i].prev); | |
748 | break; | |
749 | } | |
750 | } | |
751 | } else | |
752 | ip_freef(ipq[sum].prev); | |
753 | } | |
754 | found: | |
755 | /* | |
756 | * Adjust ip_len to not reflect header, | |
757 | * set ip_mff if more fragments are expected, | |
758 | * convert offset of this to bytes. | |
759 | */ | |
760 | ip->ip_len -= hlen; | |
761 | mff = (ip->ip_off & IP_MF) != 0; | |
762 | if (mff) { | |
763 | /* | |
764 | * Make sure that fragments have a data length | |
765 | * that's a non-zero multiple of 8 bytes. | |
766 | */ | |
767 | if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { | |
768 | ipstat.ips_toosmall++; /* XXX */ | |
769 | goto bad; | |
770 | } | |
771 | m->m_flags |= M_FRAG; | |
772 | } | |
773 | ip->ip_off <<= 3; | |
774 | ||
775 | /* | |
776 | * If datagram marked as having more fragments | |
777 | * or if this is not the first fragment, | |
778 | * attempt reassembly; if it succeeds, proceed. | |
779 | */ | |
780 | if (mff || ip->ip_off) { | |
781 | ipstat.ips_fragments++; | |
782 | m->m_pkthdr.header = ip; | |
783 | ip = ip_reass(m, fp, &ipq[sum]); | |
784 | if (ip == 0) { | |
785 | #if IPFIREWALL_FORWARD | |
786 | ip_fw_fwd_addr = NULL; | |
787 | #endif | |
788 | return; | |
789 | } | |
790 | /* Get the length of the reassembled packets header */ | |
791 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
792 | ipstat.ips_reassembled++; | |
793 | m = dtom(ip); | |
794 | #if IPDIVERT | |
795 | if (frag_divert_port) { | |
796 | struct mbuf m; | |
797 | m.m_next = 0; | |
798 | m.m_len = hlen; | |
799 | m.m_data = (char *) ip; | |
800 | ip->ip_len += hlen; | |
801 | HTONS(ip->ip_len); | |
802 | HTONS(ip->ip_off); | |
803 | HTONS(ip->ip_id); | |
804 | ip->ip_sum = 0; | |
805 | ip->ip_sum = in_cksum(&m, hlen); | |
806 | NTOHS(ip->ip_id); | |
807 | NTOHS(ip->ip_off); | |
808 | NTOHS(ip->ip_len); | |
809 | ip->ip_len -= hlen; | |
810 | } | |
811 | #endif | |
812 | } else | |
813 | if (fp) | |
814 | ip_freef(fp); | |
815 | } else | |
816 | ip->ip_len -= hlen; | |
817 | ||
818 | #if IPDIVERT | |
819 | /* | |
820 | * Divert reassembled packets to the divert protocol if required | |
821 | * If divert port is null then cookie should be too, | |
822 | * so we shouldn't need to clear them here. Assume ip_divert does so. | |
823 | */ | |
824 | if (frag_divert_port) { | |
825 | ipstat.ips_delivered++; | |
826 | ip_divert_port = frag_divert_port; | |
827 | frag_divert_port = 0; | |
828 | (*ip_protox[IPPROTO_DIVERT]->pr_input)(m, hlen); | |
829 | return; | |
830 | } | |
831 | ||
832 | /* Don't let packets divert themselves */ | |
833 | if (ip->ip_p == IPPROTO_DIVERT) { | |
834 | ipstat.ips_noproto++; | |
835 | goto bad; | |
836 | } | |
837 | ||
838 | #endif | |
839 | ||
840 | /* | |
841 | * Switch out to protocol's input routine. | |
842 | */ | |
843 | ipstat.ips_delivered++; | |
844 | ||
845 | KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr, | |
846 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
847 | ||
848 | (*ip_protox[ip->ip_p]->pr_input)(m, hlen); | |
849 | #if IPFIREWALL_FORWARD | |
850 | ip_fw_fwd_addr = NULL; /* tcp needed it */ | |
851 | #endif | |
852 | return; | |
853 | bad: | |
854 | #if IPFIREWALL_FORWARD | |
855 | ip_fw_fwd_addr = NULL; | |
856 | #endif | |
857 | KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0); | |
858 | m_freem(m); | |
859 | } | |
860 | ||
861 | /* | |
862 | * IP software interrupt routine - to go away sometime soon | |
863 | */ | |
864 | void | |
865 | ipintr(void) | |
866 | { | |
867 | int s; | |
868 | struct mbuf *m; | |
869 | ||
870 | KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_START, 0,0,0,0,0); | |
871 | ||
872 | while(1) { | |
873 | s = splimp(); | |
874 | IF_DEQUEUE(&ipintrq, m); | |
875 | splx(s); | |
876 | if (m == 0) { | |
877 | KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
878 | return; | |
879 | } | |
880 | ||
881 | ip_input(m); | |
882 | } | |
883 | } | |
884 | ||
885 | NETISR_SET(NETISR_IP, ipintr); | |
886 | ||
887 | /* | |
888 | * Take incoming datagram fragment and try to | |
889 | * reassemble it into whole datagram. If a chain for | |
890 | * reassembly of this datagram already exists, then it | |
891 | * is given as fp; otherwise have to make a chain. | |
892 | */ | |
893 | static struct ip * | |
894 | ip_reass(m, fp, where) | |
895 | register struct mbuf *m; | |
896 | register struct ipq *fp; | |
897 | struct ipq *where; | |
898 | { | |
899 | struct ip *ip = mtod(m, struct ip *); | |
900 | register struct mbuf *p = 0, *q, *nq; | |
901 | struct mbuf *t; | |
902 | int hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
903 | int i, next; | |
904 | ||
905 | /* | |
906 | * Presence of header sizes in mbufs | |
907 | * would confuse code below. | |
908 | */ | |
909 | m->m_data += hlen; | |
910 | m->m_len -= hlen; | |
911 | ||
0b4e3aa0 A |
912 | if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
913 | m->m_pkthdr.csum_flags = 0; | |
1c79356b A |
914 | /* |
915 | * If first fragment to arrive, create a reassembly queue. | |
916 | */ | |
917 | if (fp == 0) { | |
918 | if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) | |
919 | goto dropfrag; | |
920 | fp = mtod(t, struct ipq *); | |
921 | insque((void *) fp, (void *) where); | |
922 | nipq++; | |
923 | fp->ipq_ttl = IPFRAGTTL; | |
924 | fp->ipq_p = ip->ip_p; | |
925 | fp->ipq_id = ip->ip_id; | |
926 | fp->ipq_src = ip->ip_src; | |
927 | fp->ipq_dst = ip->ip_dst; | |
928 | fp->ipq_frags = m; | |
929 | m->m_nextpkt = NULL; | |
930 | #if IPDIVERT | |
931 | fp->ipq_divert = 0; | |
932 | fp->ipq_div_cookie = 0; | |
933 | #endif | |
934 | goto inserted; | |
935 | } | |
936 | ||
937 | #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) | |
938 | ||
939 | /* | |
940 | * Find a segment which begins after this one does. | |
941 | */ | |
942 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) | |
943 | if (GETIP(q)->ip_off > ip->ip_off) | |
944 | break; | |
945 | ||
946 | /* | |
947 | * If there is a preceding segment, it may provide some of | |
948 | * our data already. If so, drop the data from the incoming | |
949 | * segment. If it provides all of our data, drop us, otherwise | |
950 | * stick new segment in the proper place. | |
951 | */ | |
952 | if (p) { | |
953 | i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; | |
954 | if (i > 0) { | |
955 | if (i >= ip->ip_len) | |
956 | goto dropfrag; | |
957 | m_adj(dtom(ip), i); | |
0b4e3aa0 | 958 | m->m_pkthdr.csum_flags = 0; |
1c79356b A |
959 | ip->ip_off += i; |
960 | ip->ip_len -= i; | |
961 | } | |
962 | m->m_nextpkt = p->m_nextpkt; | |
963 | p->m_nextpkt = m; | |
964 | } else { | |
965 | m->m_nextpkt = fp->ipq_frags; | |
966 | fp->ipq_frags = m; | |
967 | } | |
968 | ||
969 | /* | |
970 | * While we overlap succeeding segments trim them or, | |
971 | * if they are completely covered, dequeue them. | |
972 | */ | |
973 | for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; | |
974 | q = nq) { | |
975 | i = (ip->ip_off + ip->ip_len) - | |
976 | GETIP(q)->ip_off; | |
977 | if (i < GETIP(q)->ip_len) { | |
978 | GETIP(q)->ip_len -= i; | |
979 | GETIP(q)->ip_off += i; | |
980 | m_adj(q, i); | |
0b4e3aa0 | 981 | q->m_pkthdr.csum_flags = 0; |
1c79356b A |
982 | break; |
983 | } | |
984 | nq = q->m_nextpkt; | |
985 | m->m_nextpkt = nq; | |
986 | m_freem(q); | |
987 | } | |
988 | ||
989 | inserted: | |
990 | ||
991 | #if IPDIVERT | |
992 | /* | |
993 | * Any fragment diverting causes the whole packet to divert | |
994 | */ | |
995 | if (frag_divert_port) { | |
996 | fp->ipq_divert = frag_divert_port; | |
997 | fp->ipq_div_cookie = ip_divert_cookie; | |
998 | } | |
999 | frag_divert_port = 0; | |
1000 | ip_divert_cookie = 0; | |
1001 | #endif | |
1002 | ||
1003 | /* | |
1004 | * Check for complete reassembly. | |
1005 | */ | |
1006 | next = 0; | |
1007 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { | |
1008 | if (GETIP(q)->ip_off != next) | |
1009 | return (0); | |
1010 | next += GETIP(q)->ip_len; | |
1011 | } | |
1012 | /* Make sure the last packet didn't have the IP_MF flag */ | |
1013 | if (p->m_flags & M_FRAG) | |
1014 | return (0); | |
1015 | ||
1016 | /* | |
1017 | * Reassembly is complete. Make sure the packet is a sane size. | |
1018 | */ | |
1019 | q = fp->ipq_frags; | |
1020 | ip = GETIP(q); | |
1021 | if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { | |
1022 | ipstat.ips_toolong++; | |
1023 | ip_freef(fp); | |
1024 | return (0); | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Concatenate fragments. | |
1029 | */ | |
1030 | m = q; | |
1031 | t = m->m_next; | |
1032 | m->m_next = 0; | |
1033 | m_cat(m, t); | |
1034 | nq = q->m_nextpkt; | |
1035 | q->m_nextpkt = 0; | |
1036 | for (q = nq; q != NULL; q = nq) { | |
1037 | nq = q->m_nextpkt; | |
1038 | q->m_nextpkt = NULL; | |
0b4e3aa0 A |
1039 | if (q->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
1040 | m->m_pkthdr.csum_flags = 0; | |
1041 | else { | |
1042 | m->m_pkthdr.csum_data += q->m_pkthdr.csum_data ; | |
1043 | m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; | |
1044 | } | |
1c79356b A |
1045 | m_cat(m, q); |
1046 | } | |
1047 | ||
1048 | #if IPDIVERT | |
1049 | /* | |
1050 | * extract divert port for packet, if any | |
1051 | */ | |
1052 | frag_divert_port = fp->ipq_divert; | |
1053 | ip_divert_cookie = fp->ipq_div_cookie; | |
1054 | #endif | |
1055 | ||
1056 | /* | |
1057 | * Create header for new ip packet by | |
1058 | * modifying header of first packet; | |
1059 | * dequeue and discard fragment reassembly header. | |
1060 | * Make header visible. | |
1061 | */ | |
1062 | ip->ip_len = next; | |
1063 | ip->ip_src = fp->ipq_src; | |
1064 | ip->ip_dst = fp->ipq_dst; | |
1065 | remque((void *) fp); | |
1066 | nipq--; | |
1067 | (void) m_free(dtom(fp)); | |
1068 | m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); | |
1069 | m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); | |
1070 | /* some debugging cruft by sklower, below, will go away soon */ | |
1071 | if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ | |
1072 | register int plen = 0; | |
1073 | for (t = m; m; m = m->m_next) | |
1074 | plen += m->m_len; | |
1075 | t->m_pkthdr.len = plen; | |
1076 | } | |
1077 | return (ip); | |
1078 | ||
1079 | dropfrag: | |
1080 | #if IPDIVERT | |
1081 | frag_divert_port = 0; | |
1082 | ip_divert_cookie = 0; | |
1083 | #endif | |
1084 | ipstat.ips_fragdropped++; | |
1085 | m_freem(m); | |
1086 | return (0); | |
1087 | ||
1088 | #undef GETIP | |
1089 | } | |
1090 | ||
1091 | /* | |
1092 | * Free a fragment reassembly header and all | |
1093 | * associated datagrams. | |
1094 | */ | |
1095 | static void | |
1096 | ip_freef(fp) | |
1097 | struct ipq *fp; | |
1098 | { | |
1099 | register struct mbuf *q; | |
1100 | ||
1101 | while (fp->ipq_frags) { | |
1102 | q = fp->ipq_frags; | |
1103 | fp->ipq_frags = q->m_nextpkt; | |
1104 | m_freem(q); | |
1105 | } | |
1106 | remque((void *) fp); | |
1107 | (void) m_free(dtom(fp)); | |
1108 | nipq--; | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * IP timer processing; | |
1113 | * if a timer expires on a reassembly | |
1114 | * queue, discard it. | |
1115 | */ | |
1116 | void | |
1117 | ip_slowtimo() | |
1118 | { | |
1119 | register struct ipq *fp; | |
1120 | int s = splnet(); | |
1121 | int i; | |
1122 | ||
1123 | for (i = 0; i < IPREASS_NHASH; i++) { | |
1124 | fp = ipq[i].next; | |
1125 | if (fp == 0) | |
1126 | continue; | |
1127 | while (fp != &ipq[i]) { | |
1128 | --fp->ipq_ttl; | |
1129 | fp = fp->next; | |
1130 | if (fp->prev->ipq_ttl == 0) { | |
1131 | ipstat.ips_fragtimeout++; | |
1132 | ip_freef(fp->prev); | |
1133 | } | |
1134 | } | |
1135 | } | |
1136 | ipflow_slowtimo(); | |
1137 | splx(s); | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * Drain off all datagram fragments. | |
1142 | */ | |
1143 | void | |
1144 | ip_drain() | |
1145 | { | |
1146 | int i; | |
1147 | ||
1148 | for (i = 0; i < IPREASS_NHASH; i++) { | |
1149 | while (ipq[i].next != &ipq[i]) { | |
1150 | ipstat.ips_fragdropped++; | |
1151 | ip_freef(ipq[i].next); | |
1152 | } | |
1153 | } | |
1154 | in_rtqdrain(); | |
1155 | } | |
1156 | ||
1157 | /* | |
1158 | * Do option processing on a datagram, | |
1159 | * possibly discarding it if bad options are encountered, | |
1160 | * or forwarding it if source-routed. | |
1161 | * Returns 1 if packet has been forwarded/freed, | |
1162 | * 0 if the packet should be processed further. | |
1163 | */ | |
1164 | static int | |
1165 | ip_dooptions(m) | |
1166 | struct mbuf *m; | |
1167 | { | |
1168 | register struct ip *ip = mtod(m, struct ip *); | |
1169 | register u_char *cp; | |
1170 | register struct ip_timestamp *ipt; | |
1171 | register struct in_ifaddr *ia; | |
1172 | int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; | |
1173 | struct in_addr *sin, dst; | |
1174 | n_time ntime; | |
1175 | ||
1176 | dst = ip->ip_dst; | |
1177 | cp = (u_char *)(ip + 1); | |
1178 | cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
1179 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1180 | opt = cp[IPOPT_OPTVAL]; | |
1181 | if (opt == IPOPT_EOL) | |
1182 | break; | |
1183 | if (opt == IPOPT_NOP) | |
1184 | optlen = 1; | |
1185 | else { | |
1186 | if (cnt < IPOPT_OLEN + sizeof(*cp)) { | |
1187 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1188 | goto bad; | |
1189 | } | |
1190 | optlen = cp[IPOPT_OLEN]; | |
1191 | if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { | |
1192 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1193 | goto bad; | |
1194 | } | |
1195 | } | |
1196 | switch (opt) { | |
1197 | ||
1198 | default: | |
1199 | break; | |
1200 | ||
1201 | /* | |
1202 | * Source routing with record. | |
1203 | * Find interface with current destination address. | |
1204 | * If none on this machine then drop if strictly routed, | |
1205 | * or do nothing if loosely routed. | |
1206 | * Record interface address and bring up next address | |
1207 | * component. If strictly routed make sure next | |
1208 | * address is on directly accessible net. | |
1209 | */ | |
1210 | case IPOPT_LSRR: | |
1211 | case IPOPT_SSRR: | |
1212 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
1213 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1214 | goto bad; | |
1215 | } | |
1216 | ipaddr.sin_addr = ip->ip_dst; | |
1217 | ia = (struct in_ifaddr *) | |
1218 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
1219 | if (ia == 0) { | |
1220 | if (opt == IPOPT_SSRR) { | |
1221 | type = ICMP_UNREACH; | |
1222 | code = ICMP_UNREACH_SRCFAIL; | |
1223 | goto bad; | |
1224 | } | |
1225 | if (!ip_dosourceroute) | |
1226 | goto nosourcerouting; | |
1227 | /* | |
1228 | * Loose routing, and not at next destination | |
1229 | * yet; nothing to do except forward. | |
1230 | */ | |
1231 | break; | |
1232 | } | |
1233 | off--; /* 0 origin */ | |
1234 | if (off > optlen - sizeof(struct in_addr)) { | |
1235 | /* | |
1236 | * End of source route. Should be for us. | |
1237 | */ | |
1238 | if (!ip_acceptsourceroute) | |
1239 | goto nosourcerouting; | |
1240 | save_rte(cp, ip->ip_src); | |
1241 | break; | |
1242 | } | |
1243 | ||
1244 | if (!ip_dosourceroute) { | |
1245 | if (ipforwarding) { | |
1246 | char buf[16]; /* aaa.bbb.ccc.ddd\0 */ | |
1247 | /* | |
1248 | * Acting as a router, so generate ICMP | |
1249 | */ | |
1250 | nosourcerouting: | |
1251 | strcpy(buf, inet_ntoa(ip->ip_dst)); | |
1252 | log(LOG_WARNING, | |
1253 | "attempted source route from %s to %s\n", | |
1254 | inet_ntoa(ip->ip_src), buf); | |
1255 | type = ICMP_UNREACH; | |
1256 | code = ICMP_UNREACH_SRCFAIL; | |
1257 | goto bad; | |
1258 | } else { | |
1259 | /* | |
1260 | * Not acting as a router, so silently drop. | |
1261 | */ | |
1262 | ipstat.ips_cantforward++; | |
1263 | m_freem(m); | |
1264 | return (1); | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * locate outgoing interface | |
1270 | */ | |
1271 | (void)memcpy(&ipaddr.sin_addr, cp + off, | |
1272 | sizeof(ipaddr.sin_addr)); | |
1273 | ||
1274 | if (opt == IPOPT_SSRR) { | |
1275 | #define INA struct in_ifaddr * | |
1276 | #define SA struct sockaddr * | |
1277 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) | |
1278 | ia = (INA)ifa_ifwithnet((SA)&ipaddr); | |
1279 | } else | |
1280 | ia = ip_rtaddr(ipaddr.sin_addr); | |
1281 | if (ia == 0) { | |
1282 | type = ICMP_UNREACH; | |
1283 | code = ICMP_UNREACH_SRCFAIL; | |
1284 | goto bad; | |
1285 | } | |
1286 | ip->ip_dst = ipaddr.sin_addr; | |
1287 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), | |
1288 | sizeof(struct in_addr)); | |
1289 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
1290 | /* | |
1291 | * Let ip_intr's mcast routing check handle mcast pkts | |
1292 | */ | |
1293 | forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); | |
1294 | break; | |
1295 | ||
1296 | case IPOPT_RR: | |
1297 | if (optlen < IPOPT_OFFSET + sizeof(*cp)) { | |
1298 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1299 | goto bad; | |
1300 | } | |
1301 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
1302 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1303 | goto bad; | |
1304 | } | |
1305 | /* | |
1306 | * If no space remains, ignore. | |
1307 | */ | |
1308 | off--; /* 0 origin */ | |
1309 | if (off > optlen - sizeof(struct in_addr)) | |
1310 | break; | |
1311 | (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, | |
1312 | sizeof(ipaddr.sin_addr)); | |
1313 | /* | |
1314 | * locate outgoing interface; if we're the destination, | |
1315 | * use the incoming interface (should be same). | |
1316 | */ | |
1317 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && | |
1318 | (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { | |
1319 | type = ICMP_UNREACH; | |
1320 | code = ICMP_UNREACH_HOST; | |
1321 | goto bad; | |
1322 | } | |
1323 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), | |
1324 | sizeof(struct in_addr)); | |
1325 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); | |
1326 | break; | |
1327 | ||
1328 | case IPOPT_TS: | |
1329 | code = cp - (u_char *)ip; | |
1330 | ipt = (struct ip_timestamp *)cp; | |
1331 | if (ipt->ipt_len < 5) | |
1332 | goto bad; | |
1333 | if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) { | |
1334 | if (++ipt->ipt_oflw == 0) | |
1335 | goto bad; | |
1336 | break; | |
1337 | } | |
1338 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
1339 | switch (ipt->ipt_flg) { | |
1340 | ||
1341 | case IPOPT_TS_TSONLY: | |
1342 | break; | |
1343 | ||
1344 | case IPOPT_TS_TSANDADDR: | |
1345 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
1346 | sizeof(struct in_addr) > ipt->ipt_len) | |
1347 | goto bad; | |
1348 | ipaddr.sin_addr = dst; | |
1349 | ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, | |
1350 | m->m_pkthdr.rcvif); | |
1351 | if (ia == 0) | |
1352 | continue; | |
1353 | (void)memcpy(sin, &IA_SIN(ia)->sin_addr, | |
1354 | sizeof(struct in_addr)); | |
1355 | ipt->ipt_ptr += sizeof(struct in_addr); | |
1356 | break; | |
1357 | ||
1358 | case IPOPT_TS_PRESPEC: | |
1359 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
1360 | sizeof(struct in_addr) > ipt->ipt_len) | |
1361 | goto bad; | |
1362 | (void)memcpy(&ipaddr.sin_addr, sin, | |
1363 | sizeof(struct in_addr)); | |
1364 | if (ifa_ifwithaddr((SA)&ipaddr) == 0) | |
1365 | continue; | |
1366 | ipt->ipt_ptr += sizeof(struct in_addr); | |
1367 | break; | |
1368 | ||
1369 | default: | |
1370 | goto bad; | |
1371 | } | |
1372 | ntime = iptime(); | |
1373 | (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, | |
1374 | sizeof(n_time)); | |
1375 | ipt->ipt_ptr += sizeof(n_time); | |
1376 | } | |
1377 | } | |
1378 | if (forward && ipforwarding) { | |
1379 | ip_forward(m, 1); | |
1380 | return (1); | |
1381 | } | |
1382 | return (0); | |
1383 | bad: | |
1384 | ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ | |
1385 | icmp_error(m, type, code, 0, 0); | |
1386 | ipstat.ips_badoptions++; | |
1387 | return (1); | |
1388 | } | |
1389 | ||
1390 | /* | |
1391 | * Given address of next destination (final or next hop), | |
1392 | * return internet address info of interface to be used to get there. | |
1393 | */ | |
1394 | static struct in_ifaddr * | |
1395 | ip_rtaddr(dst) | |
1396 | struct in_addr dst; | |
1397 | { | |
1398 | register struct sockaddr_in *sin; | |
1399 | ||
1400 | sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; | |
1401 | ||
1402 | if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { | |
1403 | if (ipforward_rt.ro_rt) { | |
1404 | RTFREE(ipforward_rt.ro_rt); | |
1405 | ipforward_rt.ro_rt = 0; | |
1406 | } | |
1407 | sin->sin_family = AF_INET; | |
1408 | sin->sin_len = sizeof(*sin); | |
1409 | sin->sin_addr = dst; | |
1410 | ||
1411 | rtalloc_ign(&ipforward_rt, RTF_PRCLONING); | |
1412 | } | |
1413 | if (ipforward_rt.ro_rt == 0) | |
1414 | return ((struct in_ifaddr *)0); | |
1415 | return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); | |
1416 | } | |
1417 | ||
1418 | /* | |
1419 | * Save incoming source route for use in replies, | |
1420 | * to be picked up later by ip_srcroute if the receiver is interested. | |
1421 | */ | |
1422 | void | |
1423 | save_rte(option, dst) | |
1424 | u_char *option; | |
1425 | struct in_addr dst; | |
1426 | { | |
1427 | unsigned olen; | |
1428 | ||
1429 | olen = option[IPOPT_OLEN]; | |
1430 | #if DIAGNOSTIC | |
1431 | if (ipprintfs) | |
1432 | printf("save_rte: olen %d\n", olen); | |
1433 | #endif | |
1434 | if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) | |
1435 | return; | |
1436 | bcopy(option, ip_srcrt.srcopt, olen); | |
1437 | ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); | |
1438 | ip_srcrt.dst = dst; | |
1439 | } | |
1440 | ||
1441 | /* | |
1442 | * Retrieve incoming source route for use in replies, | |
1443 | * in the same form used by setsockopt. | |
1444 | * The first hop is placed before the options, will be removed later. | |
1445 | */ | |
1446 | struct mbuf * | |
1447 | ip_srcroute() | |
1448 | { | |
1449 | register struct in_addr *p, *q; | |
1450 | register struct mbuf *m; | |
1451 | ||
1452 | if (ip_nhops == 0) | |
1453 | return ((struct mbuf *)0); | |
1454 | m = m_get(M_DONTWAIT, MT_HEADER); | |
1455 | if (m == 0) | |
1456 | return ((struct mbuf *)0); | |
1457 | ||
1458 | #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) | |
1459 | ||
1460 | /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ | |
1461 | m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + | |
1462 | OPTSIZ; | |
1463 | #if DIAGNOSTIC | |
1464 | if (ipprintfs) | |
1465 | printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); | |
1466 | #endif | |
1467 | ||
1468 | /* | |
1469 | * First save first hop for return route | |
1470 | */ | |
1471 | p = &ip_srcrt.route[ip_nhops - 1]; | |
1472 | *(mtod(m, struct in_addr *)) = *p--; | |
1473 | #if DIAGNOSTIC | |
1474 | if (ipprintfs) | |
1475 | printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); | |
1476 | #endif | |
1477 | ||
1478 | /* | |
1479 | * Copy option fields and padding (nop) to mbuf. | |
1480 | */ | |
1481 | ip_srcrt.nop = IPOPT_NOP; | |
1482 | ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; | |
1483 | (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), | |
1484 | &ip_srcrt.nop, OPTSIZ); | |
1485 | q = (struct in_addr *)(mtod(m, caddr_t) + | |
1486 | sizeof(struct in_addr) + OPTSIZ); | |
1487 | #undef OPTSIZ | |
1488 | /* | |
1489 | * Record return path as an IP source route, | |
1490 | * reversing the path (pointers are now aligned). | |
1491 | */ | |
1492 | while (p >= ip_srcrt.route) { | |
1493 | #if DIAGNOSTIC | |
1494 | if (ipprintfs) | |
1495 | printf(" %lx", (u_long)ntohl(q->s_addr)); | |
1496 | #endif | |
1497 | *q++ = *p--; | |
1498 | } | |
1499 | /* | |
1500 | * Last hop goes to final destination. | |
1501 | */ | |
1502 | *q = ip_srcrt.dst; | |
1503 | #if DIAGNOSTIC | |
1504 | if (ipprintfs) | |
1505 | printf(" %lx\n", (u_long)ntohl(q->s_addr)); | |
1506 | #endif | |
1507 | return (m); | |
1508 | } | |
1509 | ||
1510 | /* | |
1511 | * Strip out IP options, at higher | |
1512 | * level protocol in the kernel. | |
1513 | * Second argument is buffer to which options | |
1514 | * will be moved, and return value is their length. | |
1515 | * XXX should be deleted; last arg currently ignored. | |
1516 | */ | |
1517 | void | |
1518 | ip_stripoptions(m, mopt) | |
1519 | register struct mbuf *m; | |
1520 | struct mbuf *mopt; | |
1521 | { | |
1522 | register int i; | |
1523 | struct ip *ip = mtod(m, struct ip *); | |
1524 | register caddr_t opts; | |
1525 | int olen; | |
1526 | ||
1527 | olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
1528 | opts = (caddr_t)(ip + 1); | |
1529 | i = m->m_len - (sizeof (struct ip) + olen); | |
1530 | bcopy(opts + olen, opts, (unsigned)i); | |
1531 | m->m_len -= olen; | |
1532 | if (m->m_flags & M_PKTHDR) | |
1533 | m->m_pkthdr.len -= olen; | |
1534 | ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); | |
1535 | } | |
1536 | ||
1537 | u_char inetctlerrmap[PRC_NCMDS] = { | |
1538 | 0, 0, 0, 0, | |
1539 | 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, | |
1540 | EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, | |
1541 | EMSGSIZE, EHOSTUNREACH, 0, 0, | |
1542 | 0, 0, 0, 0, | |
1543 | ENOPROTOOPT | |
1544 | }; | |
1545 | ||
1546 | /* | |
1547 | * Forward a packet. If some error occurs return the sender | |
1548 | * an icmp packet. Note we can't always generate a meaningful | |
1549 | * icmp message because icmp doesn't have a large enough repertoire | |
1550 | * of codes and types. | |
1551 | * | |
1552 | * If not forwarding, just drop the packet. This could be confusing | |
1553 | * if ipforwarding was zero but some routing protocol was advancing | |
1554 | * us as a gateway to somewhere. However, we must let the routing | |
1555 | * protocol deal with that. | |
1556 | * | |
1557 | * The srcrt parameter indicates whether the packet is being forwarded | |
1558 | * via a source route. | |
1559 | */ | |
1560 | #ifndef NATPT | |
1561 | static | |
1562 | #endif | |
1563 | void | |
1564 | ip_forward(m, srcrt) | |
1565 | struct mbuf *m; | |
1566 | int srcrt; | |
1567 | { | |
1568 | register struct ip *ip = mtod(m, struct ip *); | |
1569 | register struct sockaddr_in *sin; | |
1570 | register struct rtentry *rt; | |
1571 | int error, type = 0, code = 0; | |
1572 | struct mbuf *mcopy; | |
1573 | n_long dest; | |
1574 | struct ifnet *destifp; | |
1575 | #if IPSEC | |
1576 | struct ifnet dummyifp; | |
1577 | #endif | |
1578 | ||
1579 | dest = 0; | |
1580 | #if DIAGNOSTIC | |
1581 | if (ipprintfs) | |
1582 | printf("forward: src %lx dst %lx ttl %x\n", | |
1583 | (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, | |
1584 | ip->ip_ttl); | |
1585 | #endif | |
1586 | ||
1587 | ||
1588 | if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { | |
1589 | ipstat.ips_cantforward++; | |
1590 | m_freem(m); | |
1591 | return; | |
1592 | } | |
1593 | HTONS(ip->ip_id); | |
1594 | if (ip->ip_ttl <= IPTTLDEC) { | |
1595 | icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); | |
1596 | return; | |
1597 | } | |
1598 | ip->ip_ttl -= IPTTLDEC; | |
1599 | ||
1600 | #if defined(PM) | |
1601 | if (doRoute) | |
1602 | { | |
1603 | struct route *ipfw_rt; | |
1604 | ||
1605 | if ((ipfw_rt = pm_route(m)) != NULL) | |
1606 | { | |
1607 | mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); | |
1608 | #if IPSEC | |
1609 | ipsec_setsocket(m, NULL); | |
1610 | #endif /*IPSEC*/ | |
1611 | error = ip_output(m, (struct mbuf *)0, ipfw_rt, | |
1612 | IP_FORWARDING | IP_PROTOCOLROUTE , 0); | |
1613 | goto clearAway; | |
1614 | } | |
1615 | ||
1616 | } | |
1617 | #endif | |
1618 | ||
1619 | sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; | |
1620 | if ((rt = ipforward_rt.ro_rt) == 0 || | |
1621 | ip->ip_dst.s_addr != sin->sin_addr.s_addr) { | |
1622 | if (ipforward_rt.ro_rt) { | |
1623 | RTFREE(ipforward_rt.ro_rt); | |
1624 | ipforward_rt.ro_rt = 0; | |
1625 | } | |
1626 | sin->sin_family = AF_INET; | |
1627 | sin->sin_len = sizeof(*sin); | |
1628 | sin->sin_addr = ip->ip_dst; | |
1629 | ||
1630 | rtalloc_ign(&ipforward_rt, RTF_PRCLONING); | |
1631 | if (ipforward_rt.ro_rt == 0) { | |
1632 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); | |
1633 | return; | |
1634 | } | |
1635 | rt = ipforward_rt.ro_rt; | |
1636 | } | |
1637 | ||
1638 | /* | |
1639 | * Save at most 64 bytes of the packet in case | |
1640 | * we need to generate an ICMP message to the src. | |
1641 | */ | |
1642 | mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); | |
1643 | ||
1644 | /* | |
1645 | * If forwarding packet using same interface that it came in on, | |
1646 | * perhaps should send a redirect to sender to shortcut a hop. | |
1647 | * Only send redirect if source is sending directly to us, | |
1648 | * and if packet was not source routed (or has any options). | |
1649 | * Also, don't send redirect if forwarding using a default route | |
1650 | * or a route modified by a redirect. | |
1651 | */ | |
1652 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
1653 | if (rt->rt_ifp == m->m_pkthdr.rcvif && | |
1654 | (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && | |
1655 | satosin(rt_key(rt))->sin_addr.s_addr != 0 && | |
1656 | ipsendredirects && !srcrt) { | |
1657 | #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) | |
1658 | u_long src = ntohl(ip->ip_src.s_addr); | |
1659 | ||
1660 | if (RTA(rt) && | |
1661 | (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { | |
1662 | if (rt->rt_flags & RTF_GATEWAY) | |
1663 | dest = satosin(rt->rt_gateway)->sin_addr.s_addr; | |
1664 | else | |
1665 | dest = ip->ip_dst.s_addr; | |
1666 | /* Router requirements says to only send host redirects */ | |
1667 | type = ICMP_REDIRECT; | |
1668 | code = ICMP_REDIRECT_HOST; | |
1669 | #if DIAGNOSTIC | |
1670 | if (ipprintfs) | |
1671 | printf("redirect (%d) to %lx\n", code, (u_long)dest); | |
1672 | #endif | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | #if IPSEC | |
1677 | ipsec_setsocket(m, NULL); | |
1678 | #endif /*IPSEC*/ | |
1679 | error = ip_output(m, (struct mbuf *)0, &ipforward_rt, | |
1680 | IP_FORWARDING, 0); | |
1681 | #if defined(PM) | |
1682 | clearAway:; | |
1683 | #endif | |
1684 | if (error) | |
1685 | ipstat.ips_cantforward++; | |
1686 | else { | |
1687 | ipstat.ips_forward++; | |
1688 | if (type) | |
1689 | ipstat.ips_redirectsent++; | |
1690 | else { | |
1691 | if (mcopy) { | |
1692 | ipflow_create(&ipforward_rt, mcopy); | |
1693 | m_freem(mcopy); | |
1694 | } | |
1695 | return; | |
1696 | } | |
1697 | } | |
1698 | if (mcopy == NULL) | |
1699 | return; | |
1700 | destifp = NULL; | |
1701 | ||
1702 | switch (error) { | |
1703 | ||
1704 | case 0: /* forwarded, but need redirect */ | |
1705 | /* type, code set above */ | |
1706 | break; | |
1707 | ||
1708 | case ENETUNREACH: /* shouldn't happen, checked above */ | |
1709 | case EHOSTUNREACH: | |
1710 | case ENETDOWN: | |
1711 | case EHOSTDOWN: | |
1712 | default: | |
1713 | type = ICMP_UNREACH; | |
1714 | code = ICMP_UNREACH_HOST; | |
1715 | break; | |
1716 | ||
1717 | case EMSGSIZE: | |
1718 | type = ICMP_UNREACH; | |
1719 | code = ICMP_UNREACH_NEEDFRAG; | |
1720 | #ifndef IPSEC | |
1721 | if (ipforward_rt.ro_rt) | |
1722 | destifp = ipforward_rt.ro_rt->rt_ifp; | |
1723 | #else | |
1724 | /* | |
1725 | * If the packet is routed over IPsec tunnel, tell the | |
1726 | * originator the tunnel MTU. | |
1727 | * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz | |
1728 | * XXX quickhack!!! | |
1729 | */ | |
1730 | if (ipforward_rt.ro_rt) { | |
1731 | struct secpolicy *sp = NULL; | |
1732 | int ipsecerror; | |
1733 | int ipsechdr; | |
1734 | struct route *ro; | |
1735 | ||
1736 | sp = ipsec4_getpolicybyaddr(mcopy, | |
1737 | IPSEC_DIR_OUTBOUND, | |
1738 | IP_FORWARDING, | |
1739 | &ipsecerror); | |
1740 | ||
1741 | if (sp == NULL) | |
1742 | destifp = ipforward_rt.ro_rt->rt_ifp; | |
1743 | else { | |
1744 | /* count IPsec header size */ | |
1745 | ipsechdr = ipsec4_hdrsiz(mcopy, | |
1746 | IPSEC_DIR_OUTBOUND, | |
1747 | NULL); | |
1748 | ||
1749 | /* | |
1750 | * find the correct route for outer IPv4 | |
1751 | * header, compute tunnel MTU. | |
1752 | * | |
1753 | * XXX BUG ALERT | |
1754 | * The "dummyifp" code relies upon the fact | |
1755 | * that icmp_error() touches only ifp->if_mtu. | |
1756 | */ | |
1757 | /*XXX*/ | |
1758 | destifp = NULL; | |
1759 | if (sp->req != NULL | |
1760 | && sp->req->sav != NULL | |
1761 | && sp->req->sav->sah != NULL) { | |
1762 | ro = &sp->req->sav->sah->sa_route; | |
1763 | if (ro->ro_rt && ro->ro_rt->rt_ifp) { | |
1764 | dummyifp.if_mtu = | |
1765 | ro->ro_rt->rt_ifp->if_mtu; | |
1766 | dummyifp.if_mtu -= ipsechdr; | |
1767 | destifp = &dummyifp; | |
1768 | } | |
1769 | } | |
1770 | ||
1771 | key_freesp(sp); | |
1772 | } | |
1773 | } | |
1774 | #endif /*IPSEC*/ | |
1775 | ipstat.ips_cantfrag++; | |
1776 | break; | |
1777 | ||
1778 | case ENOBUFS: | |
1779 | type = ICMP_SOURCEQUENCH; | |
1780 | code = 0; | |
1781 | break; | |
1782 | } | |
1783 | icmp_error(mcopy, type, code, dest, destifp); | |
1784 | } | |
1785 | ||
1786 | void | |
1787 | ip_savecontrol(inp, mp, ip, m) | |
1788 | register struct inpcb *inp; | |
1789 | register struct mbuf **mp; | |
1790 | register struct ip *ip; | |
1791 | register struct mbuf *m; | |
1792 | { | |
1793 | if (inp->inp_socket->so_options & SO_TIMESTAMP) { | |
1794 | struct timeval tv; | |
1795 | ||
1796 | microtime(&tv); | |
1797 | *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), | |
1798 | SCM_TIMESTAMP, SOL_SOCKET); | |
1799 | if (*mp) | |
1800 | mp = &(*mp)->m_next; | |
1801 | } | |
1802 | if (inp->inp_flags & INP_RECVDSTADDR) { | |
1803 | *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, | |
1804 | sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); | |
1805 | if (*mp) | |
1806 | mp = &(*mp)->m_next; | |
1807 | } | |
1808 | #ifdef notyet | |
1809 | /* XXX | |
1810 | * Moving these out of udp_input() made them even more broken | |
1811 | * than they already were. | |
1812 | */ | |
1813 | /* options were tossed already */ | |
1814 | if (inp->inp_flags & INP_RECVOPTS) { | |
1815 | *mp = sbcreatecontrol((caddr_t) opts_deleted_above, | |
1816 | sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); | |
1817 | if (*mp) | |
1818 | mp = &(*mp)->m_next; | |
1819 | } | |
1820 | /* ip_srcroute doesn't do what we want here, need to fix */ | |
1821 | if (inp->inp_flags & INP_RECVRETOPTS) { | |
1822 | *mp = sbcreatecontrol((caddr_t) ip_srcroute(), | |
1823 | sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); | |
1824 | if (*mp) | |
1825 | mp = &(*mp)->m_next; | |
1826 | } | |
1827 | #endif | |
1828 | if (inp->inp_flags & INP_RECVIF) { | |
1829 | struct ifnet *ifp; | |
1830 | struct sdlbuf { | |
1831 | struct sockaddr_dl sdl; | |
1832 | u_char pad[32]; | |
1833 | } sdlbuf; | |
1834 | struct sockaddr_dl *sdp; | |
1835 | struct sockaddr_dl *sdl2 = &sdlbuf.sdl; | |
1836 | ||
1837 | if (((ifp = m->m_pkthdr.rcvif)) | |
1838 | && ( ifp->if_index && (ifp->if_index <= if_index))) { | |
1839 | sdp = (struct sockaddr_dl *)(ifnet_addrs | |
1840 | [ifp->if_index - 1]->ifa_addr); | |
1841 | /* | |
1842 | * Change our mind and don't try copy. | |
1843 | */ | |
1844 | if ((sdp->sdl_family != AF_LINK) | |
1845 | || (sdp->sdl_len > sizeof(sdlbuf))) { | |
1846 | goto makedummy; | |
1847 | } | |
1848 | bcopy(sdp, sdl2, sdp->sdl_len); | |
1849 | } else { | |
1850 | makedummy: | |
1851 | sdl2->sdl_len | |
1852 | = offsetof(struct sockaddr_dl, sdl_data[0]); | |
1853 | sdl2->sdl_family = AF_LINK; | |
1854 | sdl2->sdl_index = 0; | |
1855 | sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; | |
1856 | } | |
1857 | *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, | |
1858 | IP_RECVIF, IPPROTO_IP); | |
1859 | if (*mp) | |
1860 | mp = &(*mp)->m_next; | |
1861 | } | |
1862 | } | |
1863 | ||
1864 | int | |
1865 | ip_rsvp_init(struct socket *so) | |
1866 | { | |
1867 | if (so->so_type != SOCK_RAW || | |
1868 | so->so_proto->pr_protocol != IPPROTO_RSVP) | |
1869 | return EOPNOTSUPP; | |
1870 | ||
1871 | if (ip_rsvpd != NULL) | |
1872 | return EADDRINUSE; | |
1873 | ||
1874 | ip_rsvpd = so; | |
1875 | /* | |
1876 | * This may seem silly, but we need to be sure we don't over-increment | |
1877 | * the RSVP counter, in case something slips up. | |
1878 | */ | |
1879 | if (!ip_rsvp_on) { | |
1880 | ip_rsvp_on = 1; | |
1881 | rsvp_on++; | |
1882 | } | |
1883 | ||
1884 | return 0; | |
1885 | } | |
1886 | ||
1887 | int | |
1888 | ip_rsvp_done(void) | |
1889 | { | |
1890 | ip_rsvpd = NULL; | |
1891 | /* | |
1892 | * This may seem silly, but we need to be sure we don't over-decrement | |
1893 | * the RSVP counter, in case something slips up. | |
1894 | */ | |
1895 | if (ip_rsvp_on) { | |
1896 | ip_rsvp_on = 0; | |
1897 | rsvp_on--; | |
1898 | } | |
1899 | return 0; | |
1900 | } |