Bug Summary

File:netinet/ip_input.c
Warning:line 1838, column 4
Value stored to 'mp' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ip_input.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/netinet/ip_input.c
1/* $OpenBSD: ip_input.c,v 1.387 2023/09/16 09:33:27 mpi Exp $ */
2/* $NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $ */
3
4/*
5 * Copyright (c) 1982, 1986, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
33 */
34
35#include "pf.h"
36#include "carp.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/mbuf.h>
41#include <sys/domain.h>
42#include <sys/mutex.h>
43#include <sys/protosw.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/sysctl.h>
47#include <sys/pool.h>
48#include <sys/task.h>
49
50#include <net/if.h>
51#include <net/if_var.h>
52#include <net/if_dl.h>
53#include <net/route.h>
54#include <net/netisr.h>
55
56#include <netinet/in.h>
57#include <netinet/in_systm.h>
58#include <netinet/if_ether.h>
59#include <netinet/ip.h>
60#include <netinet/in_pcb.h>
61#include <netinet/in_var.h>
62#include <netinet/ip_var.h>
63#include <netinet/ip_icmp.h>
64#include <net/if_types.h>
65
66#ifdef INET61
67#include <netinet6/ip6_var.h>
68#endif
69
70#if NPF1 > 0
71#include <net/pfvar.h>
72#endif
73
74#ifdef MROUTING1
75#include <netinet/ip_mroute.h>
76#endif
77
78#ifdef IPSEC1
79#include <netinet/ip_ipsp.h>
80#endif /* IPSEC */
81
82#if NCARP1 > 0
83#include <netinet/ip_carp.h>
84#endif
85
86/* values controllable via sysctl */
87int ipforwarding = 0;
88int ipmforwarding = 0;
89int ipmultipath = 0;
90int ipsendredirects = 1;
91int ip_dosourceroute = 0;
92int ip_defttl = IPDEFTTL64;
93int ip_mtudisc = 1;
94int ip_mtudisc_timeout = IPMTUDISCTIMEOUT(10 * 60);
95int ip_directedbcast = 0;
96
97/* Protects `ipq' and `ip_frags'. */
98struct mutex ipq_mutex = MUTEX_INITIALIZER(IPL_SOFTNET){ ((void *)0), ((((0x2)) > 0x0 && ((0x2)) < 0x9
) ? 0x9 : ((0x2))), 0x0 }
;
99
100/* IP reassembly queue */
101LIST_HEAD(, ipq)struct { struct ipq *lh_first; } ipq;
102
103/* Keep track of memory used for reassembly */
104int ip_maxqueue = 300;
105int ip_frags = 0;
106
107const struct sysctl_bounded_args ipctl_vars[] = {
108#ifdef MROUTING1
109 { IPCTL_MRTPROTO34, &ip_mrtproto, SYSCTL_INT_READONLY1,0 },
110#endif
111 { IPCTL_FORWARDING1, &ipforwarding, 0, 2 },
112 { IPCTL_SENDREDIRECTS2, &ipsendredirects, 0, 1 },
113 { IPCTL_DEFTTL3, &ip_defttl, 0, 255 },
114 { IPCTL_DIRECTEDBCAST6, &ip_directedbcast, 0, 1 },
115 { IPCTL_IPPORT_FIRSTAUTO7, &ipport_firstauto, 0, 65535 },
116 { IPCTL_IPPORT_LASTAUTO8, &ipport_lastauto, 0, 65535 },
117 { IPCTL_IPPORT_HIFIRSTAUTO9, &ipport_hifirstauto, 0, 65535 },
118 { IPCTL_IPPORT_HILASTAUTO10, &ipport_hilastauto, 0, 65535 },
119 { IPCTL_IPPORT_MAXQUEUE11, &ip_maxqueue, 0, 10000 },
120 { IPCTL_MFORWARDING31, &ipmforwarding, 0, 1 },
121 { IPCTL_MULTIPATH32, &ipmultipath, 0, 1 },
122 { IPCTL_ARPTIMEOUT39, &arpt_keep, 0, INT_MAX0x7fffffff },
123 { IPCTL_ARPDOWN40, &arpt_down, 0, INT_MAX0x7fffffff },
124};
125
126struct niqueue ipintrq = NIQUEUE_INITIALIZER(IPQ_MAXLEN, NETISR_IP){ { { ((void *)0), ((((0x4)) > 0x0 && ((0x4)) <
0x9) ? 0x9 : ((0x4))), 0x0 }, { ((void *)0), ((void *)0), 0 }
, ((2048)), 0 }, (2) }
;
127
128struct pool ipqent_pool;
129struct pool ipq_pool;
130
131struct cpumem *ipcounters;
132
133int ip_sysctl_ipstat(void *, size_t *, void *);
134
135static struct mbuf_queue ipsend_mq;
136static struct mbuf_queue ipsendraw_mq;
137
138extern struct niqueue arpinq;
139
140int ip_ours(struct mbuf **, int *, int, int);
141int ip_dooptions(struct mbuf *, struct ifnet *);
142int in_ouraddr(struct mbuf *, struct ifnet *, struct rtentry **);
143
144int ip_fragcheck(struct mbuf **, int *);
145struct mbuf * ip_reass(struct ipqent *, struct ipq *);
146void ip_freef(struct ipq *);
147void ip_flush(void);
148
149static void ip_send_dispatch(void *);
150static void ip_sendraw_dispatch(void *);
151static struct task ipsend_task = TASK_INITIALIZER(ip_send_dispatch, &ipsend_mq){{ ((void *)0), ((void *)0) }, (ip_send_dispatch), (&ipsend_mq
), 0 }
;
152static struct task ipsendraw_task =
153 TASK_INITIALIZER(ip_sendraw_dispatch, &ipsendraw_mq){{ ((void *)0), ((void *)0) }, (ip_sendraw_dispatch), (&ipsendraw_mq
), 0 }
;
154
155/*
156 * Used to save the IP options in case a protocol wants to respond
157 * to an incoming packet over the same route if the packet got here
158 * using IP source routing. This allows connection establishment and
159 * maintenance when the remote end is on a network that is not known
160 * to us.
161 */
162struct ip_srcrt {
163 int isr_nhops; /* number of hops */
164 struct in_addr isr_dst; /* final destination */
165 char isr_nop; /* one NOP to align */
166 char isr_hdr[IPOPT_OFFSET2 + 1]; /* OPTVAL, OLEN & OFFSET */
167 struct in_addr isr_routes[MAX_IPOPTLEN40/sizeof(struct in_addr)];
168};
169
170void save_rte(struct mbuf *, u_char *, struct in_addr);
171
172/*
173 * IP initialization: fill in IP protocol switch table.
174 * All protocols not implemented in kernel go to raw IP protocol handler.
175 */
176void
177ip_init(void)
178{
179 const struct protosw *pr;
180 int i;
181 const u_int16_t defbaddynamicports_tcp[] = DEFBADDYNAMICPORTS_TCP{ 587, 749, 750, 751, 853, 871, 2049, 6000, 6001, 6002, 6003,
6004, 6005, 6006, 6007, 6008, 6009, 6010, 0 }
;
182 const u_int16_t defbaddynamicports_udp[] = DEFBADDYNAMICPORTS_UDP{ 623, 664, 749, 750, 751, 2049, 3784, 3785, 7784, 0 };
183 const u_int16_t defrootonlyports_tcp[] = DEFROOTONLYPORTS_TCP{ 2049, 0 };
184 const u_int16_t defrootonlyports_udp[] = DEFROOTONLYPORTS_UDP{ 2049, 0 };
185
186 ipcounters = counters_alloc(ips_ncounters);
187
188 pool_init(&ipqent_pool, sizeof(struct ipqent), 0,
189 IPL_SOFTNET0x2, 0, "ipqe", NULL((void *)0));
190 pool_init(&ipq_pool, sizeof(struct ipq), 0,
191 IPL_SOFTNET0x2, 0, "ipq", NULL((void *)0));
192
193 pr = pffindproto(PF_INET2, IPPROTO_RAW255, SOCK_RAW3);
194 if (pr == NULL((void *)0))
195 panic("ip_init");
196 for (i = 0; i < IPPROTO_MAX256; i++)
197 ip_protox[i] = pr - inetsw;
198 for (pr = inetdomain.dom_protosw;
199 pr < inetdomain.dom_protoswNPROTOSW; pr++)
200 if (pr->pr_domain->dom_family == PF_INET2 &&
201 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW255 &&
202 pr->pr_protocol < IPPROTO_MAX256)
203 ip_protox[pr->pr_protocol] = pr - inetsw;
204 LIST_INIT(&ipq)do { ((&ipq)->lh_first) = ((void *)0); } while (0);
205
206 /* Fill in list of ports not to allocate dynamically. */
207 memset(&baddynamicports, 0, sizeof(baddynamicports))__builtin_memset((&baddynamicports), (0), (sizeof(baddynamicports
)))
;
208 for (i = 0; defbaddynamicports_tcp[i] != 0; i++)
209 DP_SET(baddynamicports.tcp, defbaddynamicports_tcp[i])((baddynamicports.tcp)[(defbaddynamicports_tcp[i]) / (sizeof(
u_int32_t) * 8)] |= (1U << ((defbaddynamicports_tcp[i])
% (sizeof(u_int32_t) * 8))))
;
210 for (i = 0; defbaddynamicports_udp[i] != 0; i++)
211 DP_SET(baddynamicports.udp, defbaddynamicports_udp[i])((baddynamicports.udp)[(defbaddynamicports_udp[i]) / (sizeof(
u_int32_t) * 8)] |= (1U << ((defbaddynamicports_udp[i])
% (sizeof(u_int32_t) * 8))))
;
212
213 /* Fill in list of ports only root can bind to. */
214 memset(&rootonlyports, 0, sizeof(rootonlyports))__builtin_memset((&rootonlyports), (0), (sizeof(rootonlyports
)))
;
215 for (i = 0; defrootonlyports_tcp[i] != 0; i++)
216 DP_SET(rootonlyports.tcp, defrootonlyports_tcp[i])((rootonlyports.tcp)[(defrootonlyports_tcp[i]) / (sizeof(u_int32_t
) * 8)] |= (1U << ((defrootonlyports_tcp[i]) % (sizeof(
u_int32_t) * 8))))
;
217 for (i = 0; defrootonlyports_udp[i] != 0; i++)
218 DP_SET(rootonlyports.udp, defrootonlyports_udp[i])((rootonlyports.udp)[(defrootonlyports_udp[i]) / (sizeof(u_int32_t
) * 8)] |= (1U << ((defrootonlyports_udp[i]) % (sizeof(
u_int32_t) * 8))))
;
219
220 mq_init(&ipsend_mq, 64, IPL_SOFTNET0x2);
221 mq_init(&ipsendraw_mq, 64, IPL_SOFTNET0x2);
222
223 arpinit();
224#ifdef IPSEC1
225 ipsec_init();
226#endif
227#ifdef MROUTING1
228 rt_timer_queue_init(&ip_mrouterq, MCAST_EXPIRE_FREQUENCY30,
229 &mfc_expire_route);
230#endif
231}
232
233/*
234 * Enqueue packet for local delivery. Queuing is used as a boundary
235 * between the network layer (input/forward path) running with
236 * NET_LOCK_SHARED() and the transport layer needing it exclusively.
237 */
238int
239ip_ours(struct mbuf **mp, int *offp, int nxt, int af)
240{
241 nxt = ip_fragcheck(mp, offp);
242 if (nxt == IPPROTO_DONE257)
243 return IPPROTO_DONE257;
244
245 /* We are already in a IPv4/IPv6 local deliver loop. */
246 if (af != AF_UNSPEC0)
247 return nxt;
248
249 niq_enqueue(&ipintrq, *mp);
250 *mp = NULL((void *)0);
251 return IPPROTO_DONE257;
252}
253
254/*
255 * Dequeue and process locally delivered packets.
256 * This is called with exclusive NET_LOCK().
257 */
258void
259ipintr(void)
260{
261 struct mbuf *m;
262
263 while ((m = niq_dequeue(&ipintrq)mq_dequeue(&(&ipintrq)->ni_q)) != NULL((void *)0)) {
264 struct ip *ip;
265 int off, nxt;
266
267#ifdef DIAGNOSTIC1
268 if ((m->m_flagsm_hdr.mh_flags & M_PKTHDR0x0002) == 0)
269 panic("ipintr no HDR");
270#endif
271 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
272 off = ip->ip_hl << 2;
273 nxt = ip->ip_p;
274
275 nxt = ip_deliver(&m, &off, nxt, AF_INET2);
276 KASSERT(nxt == IPPROTO_DONE)((nxt == 257) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/netinet/ip_input.c"
, 276, "nxt == IPPROTO_DONE"))
;
277 }
278}
279
280/*
281 * IPv4 input routine.
282 *
283 * Checksum and byte swap header. Process options. Forward or deliver.
284 */
285void
286ipv4_input(struct ifnet *ifp, struct mbuf *m)
287{
288 int off, nxt;
289
290 off = 0;
291 nxt = ip_input_if(&m, &off, IPPROTO_IPV44, AF_UNSPEC0, ifp);
292 KASSERT(nxt == IPPROTO_DONE)((nxt == 257) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/netinet/ip_input.c"
, 292, "nxt == IPPROTO_DONE"))
;
293}
294
295struct mbuf *
296ipv4_check(struct ifnet *ifp, struct mbuf *m)
297{
298 struct ip *ip;
299 int hlen, len;
300
301 if (m->m_lenm_hdr.mh_len < sizeof(*ip)) {
302 m = m_pullup(m, sizeof(*ip));
303 if (m == NULL((void *)0)) {
304 ipstat_inc(ips_toosmall);
305 return (NULL((void *)0));
306 }
307 }
308
309 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
310 if (ip->ip_v != IPVERSION4) {
311 ipstat_inc(ips_badvers);
312 goto bad;
313 }
314
315 hlen = ip->ip_hl << 2;
316 if (hlen < sizeof(*ip)) { /* minimum header length */
317 ipstat_inc(ips_badhlen);
318 goto bad;
319 }
320 if (hlen > m->m_lenm_hdr.mh_len) {
321 m = m_pullup(m, hlen);
322 if (m == NULL((void *)0)) {
323 ipstat_inc(ips_badhlen);
324 return (NULL((void *)0));
325 }
326 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
327 }
328
329 /* 127/8 must not appear on wire - RFC1122 */
330 if ((ntohl(ip->ip_dst.s_addr)(__uint32_t)(__builtin_constant_p(ip->ip_dst.s_addr) ? (__uint32_t
)(((__uint32_t)(ip->ip_dst.s_addr) & 0xff) << 24
| ((__uint32_t)(ip->ip_dst.s_addr) & 0xff00) <<
8 | ((__uint32_t)(ip->ip_dst.s_addr) & 0xff0000) >>
8 | ((__uint32_t)(ip->ip_dst.s_addr) & 0xff000000) >>
24) : __swap32md(ip->ip_dst.s_addr))
>> IN_CLASSA_NSHIFT24) == IN_LOOPBACKNET127 ||
331 (ntohl(ip->ip_src.s_addr)(__uint32_t)(__builtin_constant_p(ip->ip_src.s_addr) ? (__uint32_t
)(((__uint32_t)(ip->ip_src.s_addr) & 0xff) << 24
| ((__uint32_t)(ip->ip_src.s_addr) & 0xff00) <<
8 | ((__uint32_t)(ip->ip_src.s_addr) & 0xff0000) >>
8 | ((__uint32_t)(ip->ip_src.s_addr) & 0xff000000) >>
24) : __swap32md(ip->ip_src.s_addr))
>> IN_CLASSA_NSHIFT24) == IN_LOOPBACKNET127) {
332 if ((ifp->if_flags & IFF_LOOPBACK0x8) == 0) {
333 ipstat_inc(ips_badaddr);
334 goto bad;
335 }
336 }
337
338 if (!ISSET(m->m_pkthdr.csum_flags, M_IPV4_CSUM_IN_OK)((m->M_dat.MH.MH_pkthdr.csum_flags) & (0x0008))) {
339 if (ISSET(m->m_pkthdr.csum_flags, M_IPV4_CSUM_IN_BAD)((m->M_dat.MH.MH_pkthdr.csum_flags) & (0x0010))) {
340 ipstat_inc(ips_badsum);
341 goto bad;
342 }
343
344 ipstat_inc(ips_inswcsum);
345 if (in_cksum(m, hlen) != 0) {
346 ipstat_inc(ips_badsum);
347 goto bad;
348 }
349
350 SET(m->m_pkthdr.csum_flags, M_IPV4_CSUM_IN_OK)((m->M_dat.MH.MH_pkthdr.csum_flags) |= (0x0008));
351 }
352
353 /* Retrieve the packet length. */
354 len = ntohs(ip->ip_len)(__uint16_t)(__builtin_constant_p(ip->ip_len) ? (__uint16_t
)(((__uint16_t)(ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ip->ip_len) & 0xff00U) >> 8) : __swap16md(ip->
ip_len))
;
355
356 /*
357 * Convert fields to host representation.
358 */
359 if (len < hlen) {
360 ipstat_inc(ips_badlen);
361 goto bad;
362 }
363
364 /*
365 * Check that the amount of data in the buffers
366 * is at least as much as the IP header would have us expect.
367 * Trim mbufs if longer than we expect.
368 * Drop packet if shorter than we expect.
369 */
370 if (m->m_pkthdrM_dat.MH.MH_pkthdr.len < len) {
371 ipstat_inc(ips_tooshort);
372 goto bad;
373 }
374 if (m->m_pkthdrM_dat.MH.MH_pkthdr.len > len) {
375 if (m->m_lenm_hdr.mh_len == m->m_pkthdrM_dat.MH.MH_pkthdr.len) {
376 m->m_lenm_hdr.mh_len = len;
377 m->m_pkthdrM_dat.MH.MH_pkthdr.len = len;
378 } else
379 m_adj(m, len - m->m_pkthdrM_dat.MH.MH_pkthdr.len);
380 }
381
382 return (m);
383bad:
384 m_freem(m);
385 return (NULL((void *)0));
386}
387
388int
389ip_input_if(struct mbuf **mp, int *offp, int nxt, int af, struct ifnet *ifp)
390{
391 struct mbuf *m;
392 struct rtentry *rt = NULL((void *)0);
393 struct ip *ip;
394 int hlen;
395 in_addr_t pfrdr = 0;
396
397 KASSERT(*offp == 0)((*offp == 0) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/netinet/ip_input.c"
, 397, "*offp == 0"))
;
398
399 ipstat_inc(ips_total);
400 m = *mp = ipv4_check(ifp, *mp);
401 if (m == NULL((void *)0))
402 goto bad;
403
404 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
405
406#if NCARP1 > 0
407 if (carp_lsdrop(ifp, m, AF_INET2, &ip->ip_src.s_addr,
408 &ip->ip_dst.s_addr, (ip->ip_p == IPPROTO_ICMP1 ? 0 : 1)))
409 goto bad;
410#endif
411
412#if NPF1 > 0
413 /*
414 * Packet filter
415 */
416 pfrdr = ip->ip_dst.s_addr;
417 if (pf_test(AF_INET2, PF_IN, ifp, mp) != PF_PASS)
418 goto bad;
419 m = *mp;
420 if (m == NULL((void *)0))
421 goto bad;
422
423 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
424 pfrdr = (pfrdr != ip->ip_dst.s_addr);
425#endif
426
427 hlen = ip->ip_hl << 2;
428
429 /*
430 * Process options and, if not destined for us,
431 * ship it on. ip_dooptions returns 1 when an
432 * error was detected (causing an icmp message
433 * to be sent and the original packet to be freed).
434 */
435 if (hlen > sizeof (struct ip) && ip_dooptions(m, ifp)) {
436 m = *mp = NULL((void *)0);
437 goto bad;
438 }
439
440 if (ip->ip_dst.s_addr == INADDR_BROADCAST((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xffffffff
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0xffffffff)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0xffffffff)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0xffffffff)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0xffffffff)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0xffffffff
))))
||
441 ip->ip_dst.s_addr == INADDR_ANY((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x00000000
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x00000000)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0x00000000)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x00000000)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x00000000)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x00000000
))))
) {
442 nxt = ip_ours(mp, offp, nxt, af);
443 goto out;
444 }
445
446 switch(in_ouraddr(m, ifp, &rt)) {
447 case 2:
448 goto bad;
449 case 1:
450 nxt = ip_ours(mp, offp, nxt, af);
451 goto out;
452 }
453
454 if (IN_MULTICAST(ip->ip_dst.s_addr)(((u_int32_t)(ip->ip_dst.s_addr) & ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0xf0000000)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xf0000000))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xe0000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0xe0000000)))))
) {
455 /*
456 * Make sure M_MCAST is set. It should theoretically
457 * already be there, but let's play safe because upper
458 * layers check for this flag.
459 */
460 m->m_flagsm_hdr.mh_flags |= M_MCAST0x0200;
461
462#ifdef MROUTING1
463 if (ipmforwarding && ip_mrouter[ifp->if_rdomainif_data.ifi_rdomain]) {
464 int error;
465
466 if (m->m_flagsm_hdr.mh_flags & M_EXT0x0001) {
467 if ((m = *mp = m_pullup(m, hlen)) == NULL((void *)0)) {
468 ipstat_inc(ips_toosmall);
469 goto bad;
470 }
471 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
472 }
473 /*
474 * If we are acting as a multicast router, all
475 * incoming multicast packets are passed to the
476 * kernel-level multicast forwarding function.
477 * The packet is returned (relatively) intact; if
478 * ip_mforward() returns a non-zero value, the packet
479 * must be discarded, else it may be accepted below.
480 *
481 * (The IP ident field is put in the same byte order
482 * as expected when ip_mforward() is called from
483 * ip_output().)
484 */
485 KERNEL_LOCK()_kernel_lock();
486 error = ip_mforward(m, ifp);
487 KERNEL_UNLOCK()_kernel_unlock();
488 if (error) {
489 ipstat_inc(ips_cantforward);
490 goto bad;
491 }
492
493 /*
494 * The process-level routing daemon needs to receive
495 * all multicast IGMP packets, whether or not this
496 * host belongs to their destination groups.
497 */
498 if (ip->ip_p == IPPROTO_IGMP2) {
499 nxt = ip_ours(mp, offp, nxt, af);
500 goto out;
501 }
502 ipstat_inc(ips_forward);
503 }
504#endif
505 /*
506 * See if we belong to the destination multicast group on the
507 * arrival interface.
508 */
509 if (!in_hasmulti(&ip->ip_dst, ifp)) {
510 ipstat_inc(ips_notmember);
511 if (!IN_LOCAL_GROUP(ip->ip_dst.s_addr)(((u_int32_t)(ip->ip_dst.s_addr) & ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0xffffff00)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0xffffff00)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0xffffff00)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xffffff00)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xffffff00)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xffffff00))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xe0000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0xe0000000)))))
)
512 ipstat_inc(ips_cantforward);
513 goto bad;
514 }
515 nxt = ip_ours(mp, offp, nxt, af);
516 goto out;
517 }
518
519#if NCARP1 > 0
520 if (ip->ip_p == IPPROTO_ICMP1 &&
521 carp_lsdrop(ifp, m, AF_INET2, &ip->ip_src.s_addr,
522 &ip->ip_dst.s_addr, 1))
523 goto bad;
524#endif
525 /*
526 * Not for us; forward if possible and desirable.
527 */
528 if (ipforwarding == 0) {
529 ipstat_inc(ips_cantforward);
530 goto bad;
531 }
532#ifdef IPSEC1
533 if (ipsec_in_use) {
534 int rv;
535
536 rv = ipsec_forward_check(m, hlen, AF_INET2);
537 if (rv != 0) {
538 ipstat_inc(ips_cantforward);
539 goto bad;
540 }
541 /*
542 * Fall through, forward packet. Outbound IPsec policy
543 * checking will occur in ip_output().
544 */
545 }
546#endif /* IPSEC */
547
548 ip_forward(m, ifp, rt, pfrdr);
549 *mp = NULL((void *)0);
550 return IPPROTO_DONE257;
551 bad:
552 nxt = IPPROTO_DONE257;
553 m_freemp(mp);
554 out:
555 rtfree(rt);
556 return nxt;
557}
558
559int
560ip_fragcheck(struct mbuf **mp, int *offp)
561{
562 struct ip *ip;
563 struct ipq *fp;
564 struct ipqent *ipqe;
565 int hlen;
566 uint16_t mff;
567
568 ip = mtod(*mp, struct ip *)((struct ip *)((*mp)->m_hdr.mh_data));
569 hlen = ip->ip_hl << 2;
570
571 /*
572 * If offset or more fragments are set, must reassemble.
573 * Otherwise, nothing need be done.
574 * (We could look in the reassembly queue to see
575 * if the packet was previously fragmented,
576 * but it's not worth the time; just let them time out.)
577 */
578 if (ISSET(ip->ip_off, htons(IP_OFFMASK | IP_MF))((ip->ip_off) & ((__uint16_t)(__builtin_constant_p(0x1fff
| 0x2000) ? (__uint16_t)(((__uint16_t)(0x1fff | 0x2000) &
0xffU) << 8 | ((__uint16_t)(0x1fff | 0x2000) & 0xff00U
) >> 8) : __swap16md(0x1fff | 0x2000))))
) {
579 if ((*mp)->m_flagsm_hdr.mh_flags & M_EXT0x0001) { /* XXX */
580 if ((*mp = m_pullup(*mp, hlen)) == NULL((void *)0)) {
581 ipstat_inc(ips_toosmall);
582 return IPPROTO_DONE257;
583 }
584 ip = mtod(*mp, struct ip *)((struct ip *)((*mp)->m_hdr.mh_data));
585 }
586
587 /*
588 * Adjust ip_len to not reflect header,
589 * set ipqe_mff if more fragments are expected,
590 * convert offset of this to bytes.
591 */
592 ip->ip_len = htons(ntohs(ip->ip_len) - hlen)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ip->ip_len) ? (__uint16_t)(((__uint16_t)(ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ip->ip_len) & 0xff00U
) >> 8) : __swap16md(ip->ip_len)) - hlen) ? (__uint16_t
)(((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - hlen) & 0xffU) << 8 |
((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - hlen) & 0xff00U) >> 8
) : __swap16md((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - hlen))
;
593 mff = ISSET(ip->ip_off, htons(IP_MF))((ip->ip_off) & ((__uint16_t)(__builtin_constant_p(0x2000
) ? (__uint16_t)(((__uint16_t)(0x2000) & 0xffU) << 8
| ((__uint16_t)(0x2000) & 0xff00U) >> 8) : __swap16md
(0x2000))))
;
594 if (mff) {
595 /*
596 * Make sure that fragments have a data length
597 * that's a non-zero multiple of 8 bytes.
598 */
599 if (ntohs(ip->ip_len)(__uint16_t)(__builtin_constant_p(ip->ip_len) ? (__uint16_t
)(((__uint16_t)(ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ip->ip_len) & 0xff00U) >> 8) : __swap16md(ip->
ip_len))
== 0 ||
600 (ntohs(ip->ip_len)(__uint16_t)(__builtin_constant_p(ip->ip_len) ? (__uint16_t
)(((__uint16_t)(ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ip->ip_len) & 0xff00U) >> 8) : __swap16md(ip->
ip_len))
& 0x7) != 0) {
601 ipstat_inc(ips_badfrags);
602 m_freemp(mp);
603 return IPPROTO_DONE257;
604 }
605 }
606 ip->ip_off = htons(ntohs(ip->ip_off) << 3)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ip->ip_off) ? (__uint16_t)(((__uint16_t)(ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ip->ip_off) & 0xff00U
) >> 8) : __swap16md(ip->ip_off)) << 3) ? (__uint16_t
)(((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ip->ip_off) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_off) & 0xff00U) >> 8) :
__swap16md(ip->ip_off)) << 3) & 0xffU) <<
8 | ((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ip->ip_off) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_off) & 0xff00U) >> 8) :
__swap16md(ip->ip_off)) << 3) & 0xff00U) >>
8) : __swap16md((__uint16_t)(__builtin_constant_p(ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ip->ip_off) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_off) & 0xff00U) >> 8) :
__swap16md(ip->ip_off)) << 3))
;
607
608 mtx_enter(&ipq_mutex);
609
610 /*
611 * Look for queue of fragments
612 * of this datagram.
613 */
614 LIST_FOREACH(fp, &ipq, ipq_q)for((fp) = ((&ipq)->lh_first); (fp)!= ((void *)0); (fp
) = ((fp)->ipq_q.le_next))
{
615 if (ip->ip_id == fp->ipq_id &&
616 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
617 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
618 ip->ip_p == fp->ipq_p)
619 break;
620 }
621
622 /*
623 * If datagram marked as having more fragments
624 * or if this is not the first fragment,
625 * attempt reassembly; if it succeeds, proceed.
626 */
627 if (mff || ip->ip_off) {
628 ipstat_inc(ips_fragments);
629 if (ip_frags + 1 > ip_maxqueue) {
630 ip_flush();
631 ipstat_inc(ips_rcvmemdrop);
632 goto bad;
633 }
634
635 ipqe = pool_get(&ipqent_pool, PR_NOWAIT0x0002);
636 if (ipqe == NULL((void *)0)) {
637 ipstat_inc(ips_rcvmemdrop);
638 goto bad;
639 }
640 ip_frags++;
641 ipqe->ipqe_mff = mff;
642 ipqe->ipqe_m = *mp;
643 ipqe->ipqe_ip = ip;
644 *mp = ip_reass(ipqe, fp);
645 if (*mp == NULL((void *)0))
646 goto bad;
647 ipstat_inc(ips_reassembled);
648 ip = mtod(*mp, struct ip *)((struct ip *)((*mp)->m_hdr.mh_data));
649 hlen = ip->ip_hl << 2;
650 ip->ip_len = htons(ntohs(ip->ip_len) + hlen)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ip->ip_len) ? (__uint16_t)(((__uint16_t)(ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ip->ip_len) & 0xff00U
) >> 8) : __swap16md(ip->ip_len)) + hlen) ? (__uint16_t
)(((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) + hlen) & 0xffU) << 8 |
((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) + hlen) & 0xff00U) >> 8
) : __swap16md((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) + hlen))
;
651 } else {
652 if (fp != NULL((void *)0))
653 ip_freef(fp);
654 }
655
656 mtx_leave(&ipq_mutex);
657 }
658
659 *offp = hlen;
660 return ip->ip_p;
661
662 bad:
663 mtx_leave(&ipq_mutex);
664 m_freemp(mp);
665 return IPPROTO_DONE257;
666}
667
668#ifndef INET61
669#define IPSTAT_INC(name) ipstat_inc(ips_##name)
670#else
671#define IPSTAT_INC(name) (af == AF_INET2 ? \
672 ipstat_inc(ips_##name) : ip6stat_inc(ip6s_##name))
673#endif
674
675int
676ip_deliver(struct mbuf **mp, int *offp, int nxt, int af)
677{
678 const struct protosw *psw;
679 int naf = af;
680#ifdef INET61
681 int nest = 0;
682#endif /* INET6 */
683
684 NET_ASSERT_LOCKED_EXCLUSIVE()do { int _s = rw_status(&netlock); if ((splassert_ctl >
0) && (_s != 0x0001UL)) splassert_fail(0x0001UL, _s,
__func__); } while (0)
;
685
686 /*
687 * Tell launch routine the next header
688 */
689 IPSTAT_INC(delivered);
690
691 while (nxt != IPPROTO_DONE257) {
692#ifdef INET61
693 if (af == AF_INET624 &&
694 ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
695 ip6stat_inc(ip6s_toomanyhdr);
696 goto bad;
697 }
698#endif /* INET6 */
699
700 /*
701 * protection against faulty packet - there should be
702 * more sanity checks in header chain processing.
703 */
704 if ((*mp)->m_pkthdrM_dat.MH.MH_pkthdr.len < *offp) {
705 IPSTAT_INC(tooshort);
706 goto bad;
707 }
708
709#ifdef IPSEC1
710 if (ipsec_in_use) {
711 if (ipsec_local_check(*mp, *offp, nxt, af) != 0) {
712 IPSTAT_INC(cantforward);
713 goto bad;
714 }
715 }
716 /* Otherwise, just fall through and deliver the packet */
717#endif /* IPSEC */
718
719 switch (nxt) {
720 case IPPROTO_IPV44:
721 naf = AF_INET2;
722 ipstat_inc(ips_delivered);
723 break;
724#ifdef INET61
725 case IPPROTO_IPV641:
726 naf = AF_INET624;
727 ip6stat_inc(ip6s_delivered);
728 break;
729#endif /* INET6 */
730 }
731 switch (af) {
732 case AF_INET2:
733 psw = &inetsw[ip_protox[nxt]];
734 break;
735#ifdef INET61
736 case AF_INET624:
737 psw = &inet6sw[ip6_protox[nxt]];
738 break;
739#endif /* INET6 */
740 }
741 nxt = (*psw->pr_input)(mp, offp, nxt, af);
742 af = naf;
743 }
744 return nxt;
745 bad:
746 m_freemp(mp);
747 return IPPROTO_DONE257;
748}
749#undef IPSTAT_INC
750
751int
752in_ouraddr(struct mbuf *m, struct ifnet *ifp, struct rtentry **prt)
753{
754 struct rtentry *rt;
755 struct ip *ip;
756 struct sockaddr_in sin;
757 int match = 0;
758
759#if NPF1 > 0
760 switch (pf_ouraddr(m)) {
761 case 0:
762 return (0);
763 case 1:
764 return (1);
765 default:
766 /* pf does not know it */
767 break;
768 }
769#endif
770
771 ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
772
773 memset(&sin, 0, sizeof(sin))__builtin_memset((&sin), (0), (sizeof(sin)));
774 sin.sin_len = sizeof(sin);
775 sin.sin_family = AF_INET2;
776 sin.sin_addr = ip->ip_dst;
777 rt = rtalloc_mpath(sintosa(&sin), &ip->ip_src.s_addr,
778 m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid);
779 if (rtisvalid(rt)) {
780 if (ISSET(rt->rt_flags, RTF_LOCAL)((rt->rt_flags) & (0x200000)))
781 match = 1;
782
783 /*
784 * If directedbcast is enabled we only consider it local
785 * if it is received on the interface with that address.
786 */
787 if (ISSET(rt->rt_flags, RTF_BROADCAST)((rt->rt_flags) & (0x400000)) &&
788 (!ip_directedbcast || rt->rt_ifidx == ifp->if_index)) {
789 match = 1;
790
791 /* Make sure M_BCAST is set */
792 m->m_flagsm_hdr.mh_flags |= M_BCAST0x0100;
793 }
794 }
795 *prt = rt;
796
797 if (!match) {
798 struct ifaddr *ifa;
799
800 /*
801 * No local address or broadcast address found, so check for
802 * ancient classful broadcast addresses.
803 * It must have been broadcast on the link layer, and for an
804 * address on the interface it was received on.
805 */
806 if (!ISSET(m->m_flags, M_BCAST)((m->m_hdr.mh_flags) & (0x0100)) ||
807 !IN_CLASSFULBROADCAST(ip->ip_dst.s_addr, ip->ip_dst.s_addr)(((((u_int32_t)(ip->ip_dst.s_addr) & ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0xe0000000)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xe0000000))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xc0000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0xc0000000))))) &&
(ip->ip_dst.s_addr | ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x000000ff)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x000000ff)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x000000ff)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x000000ff)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x000000ff)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x000000ff))))) == ip->ip_dst.s_addr) || ((((u_int32_t)(
ip->ip_dst.s_addr) & ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0xc0000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0xc0000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0xc0000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0xc0000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0xc0000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0xc0000000))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x80000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x80000000))))) && (ip->ip_dst.s_addr | ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x0000ffff)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0x0000ffff))))) == ip
->ip_dst.s_addr) || ((((u_int32_t)(ip->ip_dst.s_addr) &
((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x80000000
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x80000000)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0x80000000)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x80000000)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x80000000)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x80000000
))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t
)(0x00000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x00000000
)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(0x00000000
)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x00000000
)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x00000000
)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x00000000
))))) && (ip->ip_dst.s_addr | ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0x00ffffff)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0x00ffffff)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0x00ffffff)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0x00ffffff)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0x00ffffff)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0x00ffffff))))) == ip->
ip_dst.s_addr))
)
808 return (0);
809
810 if (ifp->if_rdomainif_data.ifi_rdomain != rtable_l2(m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid))
811 return (0);
812 /*
813 * The check in the loop assumes you only rx a packet on an UP
814 * interface, and that M_BCAST will only be set on a BROADCAST
815 * interface.
816 */
817 NET_ASSERT_LOCKED()do { int _s = rw_status(&netlock); if ((splassert_ctl >
0) && (_s != 0x0001UL && _s != 0x0002UL)) splassert_fail
(0x0002UL, _s, __func__); } while (0)
;
818 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)for((ifa) = ((&ifp->if_addrlist)->tqh_first); (ifa)
!= ((void *)0); (ifa) = ((ifa)->ifa_list.tqe_next))
{
819 if (ifa->ifa_addr->sa_family != AF_INET2)
820 continue;
821
822 if (IN_CLASSFULBROADCAST(ip->ip_dst.s_addr,(((((u_int32_t)(ifatoia(ifa)->ia_addr.sin_addr.s_addr) &
((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xe0000000
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0xe0000000)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0xe0000000
))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t
)(0xc0000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0xc0000000
))))) && (ifatoia(ifa)->ia_addr.sin_addr.s_addr | (
(u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x000000ff
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x000000ff)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x000000ff
))))) == ip->ip_dst.s_addr) || ((((u_int32_t)(ifatoia(ifa)
->ia_addr.sin_addr.s_addr) & ((u_int32_t) (__uint32_t)
(__builtin_constant_p((u_int32_t)(0xc0000000)) ? (__uint32_t)
(((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff) << 24
| ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xc0000000))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x80000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0x80000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0x80000000))))) &&
(ifatoia(ifa)->ia_addr.sin_addr.s_addr | ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0x0000ffff)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0x0000ffff))))) == ip->
ip_dst.s_addr) || ((((u_int32_t)(ifatoia(ifa)->ia_addr.sin_addr
.s_addr) & ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x80000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x80000000))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x00000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x00000000))))) && (ifatoia(ifa)->ia_addr.sin_addr
.s_addr | ((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t
)(0x00ffffff)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x00ffffff
))))) == ip->ip_dst.s_addr))
823 ifatoia(ifa)->ia_addr.sin_addr.s_addr)(((((u_int32_t)(ifatoia(ifa)->ia_addr.sin_addr.s_addr) &
((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xe0000000
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0xe0000000)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0xe0000000
))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t
)(0xc0000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0xc0000000
)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0xc0000000
))))) && (ifatoia(ifa)->ia_addr.sin_addr.s_addr | (
(u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x000000ff
)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff) << 24 | ((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x000000ff)) &
0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x000000ff)
) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x000000ff
))))) == ip->ip_dst.s_addr) || ((((u_int32_t)(ifatoia(ifa)
->ia_addr.sin_addr.s_addr) & ((u_int32_t) (__uint32_t)
(__builtin_constant_p((u_int32_t)(0xc0000000)) ? (__uint32_t)
(((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff) << 24
| ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xc0000000)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xc0000000))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0x80000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0x80000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0x80000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0x80000000))))) &&
(ifatoia(ifa)->ia_addr.sin_addr.s_addr | ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0x0000ffff)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0x0000ffff)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0x0000ffff))))) == ip->
ip_dst.s_addr) || ((((u_int32_t)(ifatoia(ifa)->ia_addr.sin_addr
.s_addr) & ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x80000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x80000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x80000000))))) == ((u_int32_t) (__uint32_t)(__builtin_constant_p
((u_int32_t)(0x00000000)) ? (__uint32_t)(((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff) << 24 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t
)(0x00000000)) & 0xff000000) >> 24) : __swap32md((u_int32_t
)(0x00000000))))) && (ifatoia(ifa)->ia_addr.sin_addr
.s_addr | ((u_int32_t) (__uint32_t)(__builtin_constant_p((u_int32_t
)(0x00ffffff)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(0x00ffffff
)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(0x00ffffff
))))) == ip->ip_dst.s_addr))
) {
824 match = 1;
825 break;
826 }
827 }
828 } else if (ipforwarding == 0 && rt->rt_ifidx != ifp->if_index &&
829 !((ifp->if_flags & IFF_LOOPBACK0x8) || (ifp->if_typeif_data.ifi_type == IFT_ENC0xf4) ||
830 (m->m_pkthdrM_dat.MH.MH_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST0x04))) {
831 /* received on wrong interface. */
832#if NCARP1 > 0
833 struct ifnet *out_if;
834
835 /*
836 * Virtual IPs on carp interfaces need to be checked also
837 * against the parent interface and other carp interfaces
838 * sharing the same parent.
839 */
840 out_if = if_get(rt->rt_ifidx);
841 if (!(out_if && carp_strict_addr_chk(out_if, ifp))) {
842 ipstat_inc(ips_wrongif);
843 match = 2;
844 }
845 if_put(out_if);
846#else
847 ipstat_inc(ips_wrongif);
848 match = 2;
849#endif
850 }
851
852 return (match);
853}
854
855/*
856 * Take incoming datagram fragment and try to
857 * reassemble it into whole datagram. If a chain for
858 * reassembly of this datagram already exists, then it
859 * is given as fp; otherwise have to make a chain.
860 */
861struct mbuf *
862ip_reass(struct ipqent *ipqe, struct ipq *fp)
863{
864 struct mbuf *m = ipqe->ipqe_m;
865 struct ipqent *nq, *p, *q;
866 struct ip *ip;
867 struct mbuf *t;
868 int hlen = ipqe->ipqe_ip->ip_hl << 2;
869 int i, next;
870 u_int8_t ecn, ecn0;
871
872 MUTEX_ASSERT_LOCKED(&ipq_mutex)do { if (((&ipq_mutex)->mtx_owner != ({struct cpu_info
*__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof
(struct cpu_info, ci_self))); __ci;})) && !(panicstr ||
db_active)) panic("mutex %p not held in %s", (&ipq_mutex
), __func__); } while (0)
;
873
874 /*
875 * Presence of header sizes in mbufs
876 * would confuse code below.
877 */
878 m->m_datam_hdr.mh_data += hlen;
879 m->m_lenm_hdr.mh_len -= hlen;
880
881 /*
882 * If first fragment to arrive, create a reassembly queue.
883 */
884 if (fp == NULL((void *)0)) {
885 fp = pool_get(&ipq_pool, PR_NOWAIT0x0002);
886 if (fp == NULL((void *)0))
887 goto dropfrag;
888 LIST_INSERT_HEAD(&ipq, fp, ipq_q)do { if (((fp)->ipq_q.le_next = (&ipq)->lh_first) !=
((void *)0)) (&ipq)->lh_first->ipq_q.le_prev = &
(fp)->ipq_q.le_next; (&ipq)->lh_first = (fp); (fp)->
ipq_q.le_prev = &(&ipq)->lh_first; } while (0)
;
889 fp->ipq_ttl = IPFRAGTTL60;
890 fp->ipq_p = ipqe->ipqe_ip->ip_p;
891 fp->ipq_id = ipqe->ipqe_ip->ip_id;
892 LIST_INIT(&fp->ipq_fragq)do { ((&fp->ipq_fragq)->lh_first) = ((void *)0); } while
(0)
;
893 fp->ipq_src = ipqe->ipqe_ip->ip_src;
894 fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
895 p = NULL((void *)0);
896 goto insert;
897 }
898
899 /*
900 * Handle ECN by comparing this segment with the first one;
901 * if CE is set, do not lose CE.
902 * drop if CE and not-ECT are mixed for the same packet.
903 */
904 ecn = ipqe->ipqe_ip->ip_tos & IPTOS_ECN_MASK0x03;
905 ecn0 = LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first)->ipqe_ip->ip_tos & IPTOS_ECN_MASK0x03;
906 if (ecn == IPTOS_ECN_CE0x03) {
907 if (ecn0 == IPTOS_ECN_NOTECT0x00)
908 goto dropfrag;
909 if (ecn0 != IPTOS_ECN_CE0x03)
910 LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first)->ipqe_ip->ip_tos |=
911 IPTOS_ECN_CE0x03;
912 }
913 if (ecn == IPTOS_ECN_NOTECT0x00 && ecn0 != IPTOS_ECN_NOTECT0x00)
914 goto dropfrag;
915
916 /*
917 * Find a segment which begins after this one does.
918 */
919 for (p = NULL((void *)0), q = LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first); q != NULL((void *)0);
920 p = q, q = LIST_NEXT(q, ipqe_q)((q)->ipqe_q.le_next))
921 if (ntohs(q->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_off))
> ntohs(ipqe->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_off))
)
922 break;
923
924 /*
925 * If there is a preceding segment, it may provide some of
926 * our data already. If so, drop the data from the incoming
927 * segment. If it provides all of our data, drop us.
928 */
929 if (p != NULL((void *)0)) {
930 i = ntohs(p->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(p->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(p->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(p->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(p->ipqe_ip->ip_off))
+ ntohs(p->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(p->ipqe_ip->ip_len) ?
(__uint16_t)(((__uint16_t)(p->ipqe_ip->ip_len) & 0xffU
) << 8 | ((__uint16_t)(p->ipqe_ip->ip_len) & 0xff00U
) >> 8) : __swap16md(p->ipqe_ip->ip_len))
-
931 ntohs(ipqe->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_off))
;
932 if (i > 0) {
933 if (i >= ntohs(ipqe->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_len
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_len))
)
934 goto dropfrag;
935 m_adj(ipqe->ipqe_m, i);
936 ipqe->ipqe_ip->ip_off =
937 htons(ntohs(ipqe->ipqe_ip->ip_off) + i)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ipqe->ipqe_ip->ip_off) ? (__uint16_t)(((__uint16_t)(ipqe
->ipqe_ip->ip_off) & 0xffU) << 8 | ((__uint16_t
)(ipqe->ipqe_ip->ip_off) & 0xff00U) >> 8) : __swap16md
(ipqe->ipqe_ip->ip_off)) + i) ? (__uint16_t)(((__uint16_t
)((__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_off)) + i) & 0xffU) << 8 | ((__uint16_t)((__uint16_t
)(__builtin_constant_p(ipqe->ipqe_ip->ip_off) ? (__uint16_t
)(((__uint16_t)(ipqe->ipqe_ip->ip_off) & 0xffU) <<
8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(ipqe->ipqe_ip->ip_off)) + i)
& 0xff00U) >> 8) : __swap16md((__uint16_t)(__builtin_constant_p
(ipqe->ipqe_ip->ip_off) ? (__uint16_t)(((__uint16_t)(ipqe
->ipqe_ip->ip_off) & 0xffU) << 8 | ((__uint16_t
)(ipqe->ipqe_ip->ip_off) & 0xff00U) >> 8) : __swap16md
(ipqe->ipqe_ip->ip_off)) + i))
;
938 ipqe->ipqe_ip->ip_len =
939 htons(ntohs(ipqe->ipqe_ip->ip_len) - i)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ipqe->ipqe_ip->ip_len) ? (__uint16_t)(((__uint16_t)(ipqe
->ipqe_ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ipqe->ipqe_ip->ip_len) & 0xff00U) >> 8) : __swap16md
(ipqe->ipqe_ip->ip_len)) - i) ? (__uint16_t)(((__uint16_t
)((__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_len
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_len)) - i) & 0xffU) << 8 | ((__uint16_t)((__uint16_t
)(__builtin_constant_p(ipqe->ipqe_ip->ip_len) ? (__uint16_t
)(((__uint16_t)(ipqe->ipqe_ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ipqe->ipqe_ip->ip_len) & 0xff00U
) >> 8) : __swap16md(ipqe->ipqe_ip->ip_len)) - i)
& 0xff00U) >> 8) : __swap16md((__uint16_t)(__builtin_constant_p
(ipqe->ipqe_ip->ip_len) ? (__uint16_t)(((__uint16_t)(ipqe
->ipqe_ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ipqe->ipqe_ip->ip_len) & 0xff00U) >> 8) : __swap16md
(ipqe->ipqe_ip->ip_len)) - i))
;
940 }
941 }
942
943 /*
944 * While we overlap succeeding segments trim them or,
945 * if they are completely covered, dequeue them.
946 */
947 for (; q != NULL((void *)0) &&
948 ntohs(ipqe->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_off))
+ ntohs(ipqe->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_len
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_len))
>
949 ntohs(q->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_off))
; q = nq) {
950 i = (ntohs(ipqe->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_off
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_off) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_off))
+
951 ntohs(ipqe->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(ipqe->ipqe_ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ipqe->ipqe_ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ipqe->ipqe_ip->ip_len
) & 0xff00U) >> 8) : __swap16md(ipqe->ipqe_ip->
ip_len))
) - ntohs(q->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_off))
;
952 if (i < ntohs(q->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_len) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_len) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_len) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_len))
) {
953 q->ipqe_ip->ip_len =
954 htons(ntohs(q->ipqe_ip->ip_len) - i)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(q->ipqe_ip->ip_len) ? (__uint16_t)(((__uint16_t)(q->
ipqe_ip->ip_len) & 0xffU) << 8 | ((__uint16_t)(q
->ipqe_ip->ip_len) & 0xff00U) >> 8) : __swap16md
(q->ipqe_ip->ip_len)) - i) ? (__uint16_t)(((__uint16_t)
((__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_len) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_len) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_len) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_len)) - i) &
0xffU) << 8 | ((__uint16_t)((__uint16_t)(__builtin_constant_p
(q->ipqe_ip->ip_len) ? (__uint16_t)(((__uint16_t)(q->
ipqe_ip->ip_len) & 0xffU) << 8 | ((__uint16_t)(q
->ipqe_ip->ip_len) & 0xff00U) >> 8) : __swap16md
(q->ipqe_ip->ip_len)) - i) & 0xff00U) >> 8) :
__swap16md((__uint16_t)(__builtin_constant_p(q->ipqe_ip->
ip_len) ? (__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_len
) & 0xffU) << 8 | ((__uint16_t)(q->ipqe_ip->ip_len
) & 0xff00U) >> 8) : __swap16md(q->ipqe_ip->ip_len
)) - i))
;
955 q->ipqe_ip->ip_off =
956 htons(ntohs(q->ipqe_ip->ip_off) + i)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(q->ipqe_ip->ip_off) ? (__uint16_t)(((__uint16_t)(q->
ipqe_ip->ip_off) & 0xffU) << 8 | ((__uint16_t)(q
->ipqe_ip->ip_off) & 0xff00U) >> 8) : __swap16md
(q->ipqe_ip->ip_off)) + i) ? (__uint16_t)(((__uint16_t)
((__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_off)) + i) &
0xffU) << 8 | ((__uint16_t)((__uint16_t)(__builtin_constant_p
(q->ipqe_ip->ip_off) ? (__uint16_t)(((__uint16_t)(q->
ipqe_ip->ip_off) & 0xffU) << 8 | ((__uint16_t)(q
->ipqe_ip->ip_off) & 0xff00U) >> 8) : __swap16md
(q->ipqe_ip->ip_off)) + i) & 0xff00U) >> 8) :
__swap16md((__uint16_t)(__builtin_constant_p(q->ipqe_ip->
ip_off) ? (__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off
) & 0xffU) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off
) & 0xff00U) >> 8) : __swap16md(q->ipqe_ip->ip_off
)) + i))
;
957 m_adj(q->ipqe_m, i);
958 break;
959 }
960 nq = LIST_NEXT(q, ipqe_q)((q)->ipqe_q.le_next);
961 m_freem(q->ipqe_m);
962 LIST_REMOVE(q, ipqe_q)do { if ((q)->ipqe_q.le_next != ((void *)0)) (q)->ipqe_q
.le_next->ipqe_q.le_prev = (q)->ipqe_q.le_prev; *(q)->
ipqe_q.le_prev = (q)->ipqe_q.le_next; ((q)->ipqe_q.le_prev
) = ((void *)-1); ((q)->ipqe_q.le_next) = ((void *)-1); } while
(0)
;
963 pool_put(&ipqent_pool, q);
964 ip_frags--;
965 }
966
967insert:
968 /*
969 * Stick new segment in its place;
970 * check for complete reassembly.
971 */
972 if (p == NULL((void *)0)) {
973 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q)do { if (((ipqe)->ipqe_q.le_next = (&fp->ipq_fragq)
->lh_first) != ((void *)0)) (&fp->ipq_fragq)->lh_first
->ipqe_q.le_prev = &(ipqe)->ipqe_q.le_next; (&fp
->ipq_fragq)->lh_first = (ipqe); (ipqe)->ipqe_q.le_prev
= &(&fp->ipq_fragq)->lh_first; } while (0)
;
974 } else {
975 LIST_INSERT_AFTER(p, ipqe, ipqe_q)do { if (((ipqe)->ipqe_q.le_next = (p)->ipqe_q.le_next)
!= ((void *)0)) (p)->ipqe_q.le_next->ipqe_q.le_prev = &
(ipqe)->ipqe_q.le_next; (p)->ipqe_q.le_next = (ipqe); (
ipqe)->ipqe_q.le_prev = &(p)->ipqe_q.le_next; } while
(0)
;
976 }
977 next = 0;
978 for (p = NULL((void *)0), q = LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first); q != NULL((void *)0);
979 p = q, q = LIST_NEXT(q, ipqe_q)((q)->ipqe_q.le_next)) {
980 if (ntohs(q->ipqe_ip->ip_off)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_off) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_off) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_off) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_off))
!= next)
981 return (0);
982 next += ntohs(q->ipqe_ip->ip_len)(__uint16_t)(__builtin_constant_p(q->ipqe_ip->ip_len) ?
(__uint16_t)(((__uint16_t)(q->ipqe_ip->ip_len) & 0xffU
) << 8 | ((__uint16_t)(q->ipqe_ip->ip_len) & 0xff00U
) >> 8) : __swap16md(q->ipqe_ip->ip_len))
;
983 }
984 if (p->ipqe_mff)
985 return (0);
986
987 /*
988 * Reassembly is complete. Check for a bogus message size and
989 * concatenate fragments.
990 */
991 q = LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first);
992 ip = q->ipqe_ip;
993 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET65535) {
994 ipstat_inc(ips_toolong);
995 ip_freef(fp);
996 return (0);
997 }
998 m = q->ipqe_m;
999 t = m->m_nextm_hdr.mh_next;
1000 m->m_nextm_hdr.mh_next = 0;
1001 m_cat(m, t);
1002 nq = LIST_NEXT(q, ipqe_q)((q)->ipqe_q.le_next);
1003 pool_put(&ipqent_pool, q);
1004 ip_frags--;
1005 for (q = nq; q != NULL((void *)0); q = nq) {
1006 t = q->ipqe_m;
1007 nq = LIST_NEXT(q, ipqe_q)((q)->ipqe_q.le_next);
1008 pool_put(&ipqent_pool, q);
1009 ip_frags--;
1010 m_removehdr(t);
1011 m_cat(m, t);
1012 }
1013
1014 /*
1015 * Create header for new ip packet by
1016 * modifying header of first packet;
1017 * dequeue and discard fragment reassembly header.
1018 * Make header visible.
1019 */
1020 ip->ip_len = htons(next)(__uint16_t)(__builtin_constant_p(next) ? (__uint16_t)(((__uint16_t
)(next) & 0xffU) << 8 | ((__uint16_t)(next) & 0xff00U
) >> 8) : __swap16md(next))
;
1021 ip->ip_src = fp->ipq_src;
1022 ip->ip_dst = fp->ipq_dst;
1023 LIST_REMOVE(fp, ipq_q)do { if ((fp)->ipq_q.le_next != ((void *)0)) (fp)->ipq_q
.le_next->ipq_q.le_prev = (fp)->ipq_q.le_prev; *(fp)->
ipq_q.le_prev = (fp)->ipq_q.le_next; ((fp)->ipq_q.le_prev
) = ((void *)-1); ((fp)->ipq_q.le_next) = ((void *)-1); } while
(0)
;
1024 pool_put(&ipq_pool, fp);
1025 m->m_lenm_hdr.mh_len += (ip->ip_hl << 2);
1026 m->m_datam_hdr.mh_data -= (ip->ip_hl << 2);
1027 m_calchdrlen(m);
1028 return (m);
1029
1030dropfrag:
1031 ipstat_inc(ips_fragdropped);
1032 m_freem(m);
1033 pool_put(&ipqent_pool, ipqe);
1034 ip_frags--;
1035 return (NULL((void *)0));
1036}
1037
1038/*
1039 * Free a fragment reassembly header and all
1040 * associated datagrams.
1041 */
1042void
1043ip_freef(struct ipq *fp)
1044{
1045 struct ipqent *q;
1046
1047 MUTEX_ASSERT_LOCKED(&ipq_mutex)do { if (((&ipq_mutex)->mtx_owner != ({struct cpu_info
*__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof
(struct cpu_info, ci_self))); __ci;})) && !(panicstr ||
db_active)) panic("mutex %p not held in %s", (&ipq_mutex
), __func__); } while (0)
;
1048
1049 while ((q = LIST_FIRST(&fp->ipq_fragq)((&fp->ipq_fragq)->lh_first)) != NULL((void *)0)) {
1050 LIST_REMOVE(q, ipqe_q)do { if ((q)->ipqe_q.le_next != ((void *)0)) (q)->ipqe_q
.le_next->ipqe_q.le_prev = (q)->ipqe_q.le_prev; *(q)->
ipqe_q.le_prev = (q)->ipqe_q.le_next; ((q)->ipqe_q.le_prev
) = ((void *)-1); ((q)->ipqe_q.le_next) = ((void *)-1); } while
(0)
;
1051 m_freem(q->ipqe_m);
1052 pool_put(&ipqent_pool, q);
1053 ip_frags--;
1054 }
1055 LIST_REMOVE(fp, ipq_q)do { if ((fp)->ipq_q.le_next != ((void *)0)) (fp)->ipq_q
.le_next->ipq_q.le_prev = (fp)->ipq_q.le_prev; *(fp)->
ipq_q.le_prev = (fp)->ipq_q.le_next; ((fp)->ipq_q.le_prev
) = ((void *)-1); ((fp)->ipq_q.le_next) = ((void *)-1); } while
(0)
;
1056 pool_put(&ipq_pool, fp);
1057}
1058
1059/*
1060 * IP timer processing;
1061 * if a timer expires on a reassembly queue, discard it.
1062 */
1063void
1064ip_slowtimo(void)
1065{
1066 struct ipq *fp, *nfp;
1067
1068 mtx_enter(&ipq_mutex);
1069 LIST_FOREACH_SAFE(fp, &ipq, ipq_q, nfp)for ((fp) = ((&ipq)->lh_first); (fp) && ((nfp)
= ((fp)->ipq_q.le_next), 1); (fp) = (nfp))
{
1070 if (--fp->ipq_ttl == 0) {
1071 ipstat_inc(ips_fragtimeout);
1072 ip_freef(fp);
1073 }
1074 }
1075 mtx_leave(&ipq_mutex);
1076}
1077
1078/*
1079 * Flush a bunch of datagram fragments, till we are down to 75%.
1080 */
1081void
1082ip_flush(void)
1083{
1084 int max = 50;
1085
1086 MUTEX_ASSERT_LOCKED(&ipq_mutex)do { if (((&ipq_mutex)->mtx_owner != ({struct cpu_info
*__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof
(struct cpu_info, ci_self))); __ci;})) && !(panicstr ||
db_active)) panic("mutex %p not held in %s", (&ipq_mutex
), __func__); } while (0)
;
1087
1088 while (!LIST_EMPTY(&ipq)(((&ipq)->lh_first) == ((void *)0)) && ip_frags > ip_maxqueue * 3 / 4 && --max) {
1089 ipstat_inc(ips_fragdropped);
1090 ip_freef(LIST_FIRST(&ipq)((&ipq)->lh_first));
1091 }
1092}
1093
1094/*
1095 * Do option processing on a datagram,
1096 * possibly discarding it if bad options are encountered,
1097 * or forwarding it if source-routed.
1098 * Returns 1 if packet has been forwarded/freed,
1099 * 0 if the packet should be processed further.
1100 */
1101int
1102ip_dooptions(struct mbuf *m, struct ifnet *ifp)
1103{
1104 struct ip *ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
1105 unsigned int rtableid = m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid;
1106 struct rtentry *rt;
1107 struct sockaddr_in ipaddr;
1108 u_char *cp;
1109 struct ip_timestamp ipt;
1110 struct in_ifaddr *ia;
1111 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB12, forward = 0;
1112 struct in_addr sin, dst;
1113 u_int32_t ntime;
1114
1115 dst = ip->ip_dst;
1116 cp = (u_char *)(ip + 1);
1117 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1118
1119 KERNEL_LOCK()_kernel_lock();
1120 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1121 opt = cp[IPOPT_OPTVAL0];
1122 if (opt == IPOPT_EOL0)
1123 break;
1124 if (opt == IPOPT_NOP1)
1125 optlen = 1;
1126 else {
1127 if (cnt < IPOPT_OLEN1 + sizeof(*cp)) {
1128 code = &cp[IPOPT_OLEN1] - (u_char *)ip;
1129 goto bad;
1130 }
1131 optlen = cp[IPOPT_OLEN1];
1132 if (optlen < IPOPT_OLEN1 + sizeof(*cp) || optlen > cnt) {
1133 code = &cp[IPOPT_OLEN1] - (u_char *)ip;
1134 goto bad;
1135 }
1136 }
1137
1138 switch (opt) {
1139
1140 default:
1141 break;
1142
1143 /*
1144 * Source routing with record.
1145 * Find interface with current destination address.
1146 * If none on this machine then drop if strictly routed,
1147 * or do nothing if loosely routed.
1148 * Record interface address and bring up next address
1149 * component. If strictly routed make sure next
1150 * address is on directly accessible net.
1151 */
1152 case IPOPT_LSRR131:
1153 case IPOPT_SSRR137:
1154 if (!ip_dosourceroute) {
1155 type = ICMP_UNREACH3;
1156 code = ICMP_UNREACH_SRCFAIL5;
1157 goto bad;
1158 }
1159 if (optlen < IPOPT_OFFSET2 + sizeof(*cp)) {
1160 code = &cp[IPOPT_OLEN1] - (u_char *)ip;
1161 goto bad;
1162 }
1163 if ((off = cp[IPOPT_OFFSET2]) < IPOPT_MINOFF4) {
1164 code = &cp[IPOPT_OFFSET2] - (u_char *)ip;
1165 goto bad;
1166 }
1167 memset(&ipaddr, 0, sizeof(ipaddr))__builtin_memset((&ipaddr), (0), (sizeof(ipaddr)));
1168 ipaddr.sin_family = AF_INET2;
1169 ipaddr.sin_len = sizeof(ipaddr);
1170 ipaddr.sin_addr = ip->ip_dst;
1171 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr),
1172 m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid));
1173 if (ia == NULL((void *)0)) {
1174 if (opt == IPOPT_SSRR137) {
1175 type = ICMP_UNREACH3;
1176 code = ICMP_UNREACH_SRCFAIL5;
1177 goto bad;
1178 }
1179 /*
1180 * Loose routing, and not at next destination
1181 * yet; nothing to do except forward.
1182 */
1183 break;
1184 }
1185 off--; /* 0 origin */
1186 if ((off + sizeof(struct in_addr)) > optlen) {
1187 /*
1188 * End of source route. Should be for us.
1189 */
1190 save_rte(m, cp, ip->ip_src);
1191 break;
1192 }
1193
1194 /*
1195 * locate outgoing interface
1196 */
1197 memset(&ipaddr, 0, sizeof(ipaddr))__builtin_memset((&ipaddr), (0), (sizeof(ipaddr)));
1198 ipaddr.sin_family = AF_INET2;
1199 ipaddr.sin_len = sizeof(ipaddr);
1200 memcpy(&ipaddr.sin_addr, cp + off,__builtin_memcpy((&ipaddr.sin_addr), (cp + off), (sizeof(
ipaddr.sin_addr)))
1201 sizeof(ipaddr.sin_addr))__builtin_memcpy((&ipaddr.sin_addr), (cp + off), (sizeof(
ipaddr.sin_addr)))
;
1202 /* keep packet in the virtual instance */
1203 rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE1, rtableid);
1204 if (!rtisvalid(rt) || ((opt == IPOPT_SSRR137) &&
1205 ISSET(rt->rt_flags, RTF_GATEWAY)((rt->rt_flags) & (0x2)))) {
1206 type = ICMP_UNREACH3;
1207 code = ICMP_UNREACH_SRCFAIL5;
1208 rtfree(rt);
1209 goto bad;
1210 }
1211 ia = ifatoia(rt->rt_ifa);
1212 memcpy(cp + off, &ia->ia_addr.sin_addr,__builtin_memcpy((cp + off), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
1213 sizeof(struct in_addr))__builtin_memcpy((cp + off), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
;
1214 rtfree(rt);
1215 cp[IPOPT_OFFSET2] += sizeof(struct in_addr);
1216 ip->ip_dst = ipaddr.sin_addr;
1217 /*
1218 * Let ip_intr's mcast routing check handle mcast pkts
1219 */
1220 forward = !IN_MULTICAST(ip->ip_dst.s_addr)(((u_int32_t)(ip->ip_dst.s_addr) & ((u_int32_t) (__uint32_t
)(__builtin_constant_p((u_int32_t)(0xf0000000)) ? (__uint32_t
)(((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff) <<
24 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff00) <<
8 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff0000) >>
8 | ((__uint32_t)((u_int32_t)(0xf0000000)) & 0xff000000)
>> 24) : __swap32md((u_int32_t)(0xf0000000))))) == ((u_int32_t
) (__uint32_t)(__builtin_constant_p((u_int32_t)(0xe0000000)) ?
(__uint32_t)(((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff
) << 24 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff00
) << 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff0000
) >> 8 | ((__uint32_t)((u_int32_t)(0xe0000000)) & 0xff000000
) >> 24) : __swap32md((u_int32_t)(0xe0000000)))))
;
1221 break;
1222
1223 case IPOPT_RR7:
1224 if (optlen < IPOPT_OFFSET2 + sizeof(*cp)) {
1225 code = &cp[IPOPT_OLEN1] - (u_char *)ip;
1226 goto bad;
1227 }
1228 if ((off = cp[IPOPT_OFFSET2]) < IPOPT_MINOFF4) {
1229 code = &cp[IPOPT_OFFSET2] - (u_char *)ip;
1230 goto bad;
1231 }
1232
1233 /*
1234 * If no space remains, ignore.
1235 */
1236 off--; /* 0 origin */
1237 if ((off + sizeof(struct in_addr)) > optlen)
1238 break;
1239 memset(&ipaddr, 0, sizeof(ipaddr))__builtin_memset((&ipaddr), (0), (sizeof(ipaddr)));
1240 ipaddr.sin_family = AF_INET2;
1241 ipaddr.sin_len = sizeof(ipaddr);
1242 ipaddr.sin_addr = ip->ip_dst;
1243 /*
1244 * locate outgoing interface; if we're the destination,
1245 * use the incoming interface (should be same).
1246 * Again keep the packet inside the virtual instance.
1247 */
1248 rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE1, rtableid);
1249 if (!rtisvalid(rt)) {
1250 type = ICMP_UNREACH3;
1251 code = ICMP_UNREACH_HOST1;
1252 rtfree(rt);
1253 goto bad;
1254 }
1255 ia = ifatoia(rt->rt_ifa);
1256 memcpy(cp + off, &ia->ia_addr.sin_addr,__builtin_memcpy((cp + off), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
1257 sizeof(struct in_addr))__builtin_memcpy((cp + off), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
;
1258 rtfree(rt);
1259 cp[IPOPT_OFFSET2] += sizeof(struct in_addr);
1260 break;
1261
1262 case IPOPT_TS68:
1263 code = cp - (u_char *)ip;
1264 if (optlen < sizeof(struct ip_timestamp))
1265 goto bad;
1266 memcpy(&ipt, cp, sizeof(struct ip_timestamp))__builtin_memcpy((&ipt), (cp), (sizeof(struct ip_timestamp
)))
;
1267 if (ipt.ipt_ptr < 5 || ipt.ipt_len < 5)
1268 goto bad;
1269 if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) > ipt.ipt_len) {
1270 if (++ipt.ipt_oflw == 0)
1271 goto bad;
1272 break;
1273 }
1274 memcpy(&sin, cp + ipt.ipt_ptr - 1, sizeof sin)__builtin_memcpy((&sin), (cp + ipt.ipt_ptr - 1), (sizeof sin
))
;
1275 switch (ipt.ipt_flg) {
1276
1277 case IPOPT_TS_TSONLY0:
1278 break;
1279
1280 case IPOPT_TS_TSANDADDR1:
1281 if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1282 sizeof(struct in_addr) > ipt.ipt_len)
1283 goto bad;
1284 memset(&ipaddr, 0, sizeof(ipaddr))__builtin_memset((&ipaddr), (0), (sizeof(ipaddr)));
1285 ipaddr.sin_family = AF_INET2;
1286 ipaddr.sin_len = sizeof(ipaddr);
1287 ipaddr.sin_addr = dst;
1288 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1289 ifp));
1290 if (ia == NULL((void *)0))
1291 continue;
1292 memcpy(&sin, &ia->ia_addr.sin_addr,__builtin_memcpy((&sin), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
1293 sizeof(struct in_addr))__builtin_memcpy((&sin), (&ia->ia_addr.sin_addr), (
sizeof(struct in_addr)))
;
1294 ipt.ipt_ptr += sizeof(struct in_addr);
1295 break;
1296
1297 case IPOPT_TS_PRESPEC3:
1298 if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1299 sizeof(struct in_addr) > ipt.ipt_len)
1300 goto bad;
1301 memset(&ipaddr, 0, sizeof(ipaddr))__builtin_memset((&ipaddr), (0), (sizeof(ipaddr)));
1302 ipaddr.sin_family = AF_INET2;
1303 ipaddr.sin_len = sizeof(ipaddr);
1304 ipaddr.sin_addr = sin;
1305 if (ifa_ifwithaddr(sintosa(&ipaddr),
1306 m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid) == NULL((void *)0))
1307 continue;
1308 ipt.ipt_ptr += sizeof(struct in_addr);
1309 break;
1310
1311 default:
1312 /* XXX can't take &ipt->ipt_flg */
1313 code = (u_char *)&ipt.ipt_ptr -
1314 (u_char *)ip + 1;
1315 goto bad;
1316 }
1317 ntime = iptime();
1318 memcpy(cp + ipt.ipt_ptr - 1, &ntime, sizeof(u_int32_t))__builtin_memcpy((cp + ipt.ipt_ptr - 1), (&ntime), (sizeof
(u_int32_t)))
;
1319 ipt.ipt_ptr += sizeof(u_int32_t);
1320 }
1321 }
1322 KERNEL_UNLOCK()_kernel_unlock();
1323 if (forward && ipforwarding > 0) {
1324 ip_forward(m, ifp, NULL((void *)0), 1);
1325 return (1);
1326 }
1327 return (0);
1328bad:
1329 KERNEL_UNLOCK()_kernel_unlock();
1330 icmp_error(m, type, code, 0, 0);
1331 ipstat_inc(ips_badoptions);
1332 return (1);
1333}
1334
1335/*
1336 * Save incoming source route for use in replies,
1337 * to be picked up later by ip_srcroute if the receiver is interested.
1338 */
1339void
1340save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
1341{
1342 struct ip_srcrt *isr;
1343 struct m_tag *mtag;
1344 unsigned olen;
1345
1346 olen = option[IPOPT_OLEN1];
1347 if (olen > sizeof(isr->isr_hdr) + sizeof(isr->isr_routes))
1348 return;
1349
1350 mtag = m_tag_get(PACKET_TAG_SRCROUTE0x1000, sizeof(*isr), M_NOWAIT0x0002);
1351 if (mtag == NULL((void *)0)) {
1352 ipstat_inc(ips_idropped);
1353 return;
1354 }
1355 isr = (struct ip_srcrt *)(mtag + 1);
1356
1357 memcpy(isr->isr_hdr, option, olen)__builtin_memcpy((isr->isr_hdr), (option), (olen));
1358 isr->isr_nhops = (olen - IPOPT_OFFSET2 - 1) / sizeof(struct in_addr);
1359 isr->isr_dst = dst;
1360 m_tag_prepend(m, mtag);
1361}
1362
1363/*
1364 * Retrieve incoming source route for use in replies,
1365 * in the same form used by setsockopt.
1366 * The first hop is placed before the options, will be removed later.
1367 */
1368struct mbuf *
1369ip_srcroute(struct mbuf *m0)
1370{
1371 struct in_addr *p, *q;
1372 struct mbuf *m;
1373 struct ip_srcrt *isr;
1374 struct m_tag *mtag;
1375
1376 if (!ip_dosourceroute)
1377 return (NULL((void *)0));
1378
1379 mtag = m_tag_find(m0, PACKET_TAG_SRCROUTE0x1000, NULL((void *)0));
1380 if (mtag == NULL((void *)0))
1381 return (NULL((void *)0));
1382 isr = (struct ip_srcrt *)(mtag + 1);
1383
1384 if (isr->isr_nhops == 0)
1385 return (NULL((void *)0));
1386 m = m_get(M_DONTWAIT0x0002, MT_SOOPTS4);
1387 if (m == NULL((void *)0)) {
1388 ipstat_inc(ips_idropped);
1389 return (NULL((void *)0));
1390 }
1391
1392#define OPTSIZ (sizeof(isr->isr_nop) + sizeof(isr->isr_hdr))
1393
1394 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + header) */
1395 m->m_lenm_hdr.mh_len = (isr->isr_nhops + 1) * sizeof(struct in_addr) + OPTSIZ;
1396
1397 /*
1398 * First save first hop for return route
1399 */
1400 p = &(isr->isr_routes[isr->isr_nhops - 1]);
1401 *(mtod(m, struct in_addr *)((struct in_addr *)((m)->m_hdr.mh_data))) = *p--;
1402
1403 /*
1404 * Copy option fields and padding (nop) to mbuf.
1405 */
1406 isr->isr_nop = IPOPT_NOP1;
1407 isr->isr_hdr[IPOPT_OFFSET2] = IPOPT_MINOFF4;
1408 memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &isr->isr_nop,__builtin_memcpy((((caddr_t)((m)->m_hdr.mh_data)) + sizeof
(struct in_addr)), (&isr->isr_nop), (OPTSIZ))
1409 OPTSIZ)__builtin_memcpy((((caddr_t)((m)->m_hdr.mh_data)) + sizeof
(struct in_addr)), (&isr->isr_nop), (OPTSIZ))
;
1410 q = (struct in_addr *)(mtod(m, caddr_t)((caddr_t)((m)->m_hdr.mh_data)) +
1411 sizeof(struct in_addr) + OPTSIZ);
1412#undef OPTSIZ
1413 /*
1414 * Record return path as an IP source route,
1415 * reversing the path (pointers are now aligned).
1416 */
1417 while (p >= isr->isr_routes) {
1418 *q++ = *p--;
1419 }
1420 /*
1421 * Last hop goes to final destination.
1422 */
1423 *q = isr->isr_dst;
1424 m_tag_delete(m0, (struct m_tag *)isr);
1425 return (m);
1426}
1427
1428/*
1429 * Strip out IP options, at higher level protocol in the kernel.
1430 */
1431void
1432ip_stripoptions(struct mbuf *m)
1433{
1434 int i;
1435 struct ip *ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
1436 caddr_t opts;
1437 int olen;
1438
1439 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1440 opts = (caddr_t)(ip + 1);
1441 i = m->m_lenm_hdr.mh_len - (sizeof (struct ip) + olen);
1442 memmove(opts, opts + olen, i)__builtin_memmove((opts), (opts + olen), (i));
1443 m->m_lenm_hdr.mh_len -= olen;
1444 if (m->m_flagsm_hdr.mh_flags & M_PKTHDR0x0002)
1445 m->m_pkthdrM_dat.MH.MH_pkthdr.len -= olen;
1446 ip->ip_hl = sizeof(struct ip) >> 2;
1447 ip->ip_len = htons(ntohs(ip->ip_len) - olen)(__uint16_t)(__builtin_constant_p((__uint16_t)(__builtin_constant_p
(ip->ip_len) ? (__uint16_t)(((__uint16_t)(ip->ip_len) &
0xffU) << 8 | ((__uint16_t)(ip->ip_len) & 0xff00U
) >> 8) : __swap16md(ip->ip_len)) - olen) ? (__uint16_t
)(((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - olen) & 0xffU) << 8 |
((__uint16_t)((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - olen) & 0xff00U) >> 8
) : __swap16md((__uint16_t)(__builtin_constant_p(ip->ip_len
) ? (__uint16_t)(((__uint16_t)(ip->ip_len) & 0xffU) <<
8 | ((__uint16_t)(ip->ip_len) & 0xff00U) >> 8) :
__swap16md(ip->ip_len)) - olen))
;
1448}
1449
1450const u_char inetctlerrmap[PRC_NCMDS21] = {
1451 0, 0, 0, 0,
1452 0, EMSGSIZE40, EHOSTDOWN64, EHOSTUNREACH65,
1453 EHOSTUNREACH65, EHOSTUNREACH65, ECONNREFUSED61, ECONNREFUSED61,
1454 EMSGSIZE40, EHOSTUNREACH65, 0, 0,
1455 0, 0, 0, 0,
1456 ENOPROTOOPT42
1457};
1458
1459/*
1460 * Forward a packet. If some error occurs return the sender
1461 * an icmp packet. Note we can't always generate a meaningful
1462 * icmp message because icmp doesn't have a large enough repertoire
1463 * of codes and types.
1464 *
1465 * If not forwarding, just drop the packet. This could be confusing
1466 * if ipforwarding was zero but some routing protocol was advancing
1467 * us as a gateway to somewhere. However, we must let the routing
1468 * protocol deal with that.
1469 *
1470 * The srcrt parameter indicates whether the packet is being forwarded
1471 * via a source route.
1472 */
1473void
1474ip_forward(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt, int srcrt)
1475{
1476 struct mbuf mfake, *mcopy = NULL((void *)0);
1477 struct ip *ip = mtod(m, struct ip *)((struct ip *)((m)->m_hdr.mh_data));
1478 struct sockaddr_in *sin;
1479 struct route ro;
1480 int error = 0, type = 0, code = 0, destmtu = 0, fake = 0, len;
1481 u_int32_t dest;
1482
1483 dest = 0;
1484 if (m->m_flagsm_hdr.mh_flags & (M_BCAST0x0100|M_MCAST0x0200) || in_canforward(ip->ip_dst) == 0) {
1485 ipstat_inc(ips_cantforward);
1486 m_freem(m);
1487 goto freecopy;
1488 }
1489 if (ip->ip_ttl <= IPTTLDEC1) {
1490 icmp_error(m, ICMP_TIMXCEED11, ICMP_TIMXCEED_INTRANS0, dest, 0);
1491 goto freecopy;
1492 }
1493
1494 memset(&ro, 0, sizeof(ro))__builtin_memset((&ro), (0), (sizeof(ro)));
1495 sin = satosin(&ro.ro_dst);
1496 sin->sin_family = AF_INET2;
1497 sin->sin_len = sizeof(*sin);
1498 sin->sin_addr = ip->ip_dst;
1499
1500 if (!rtisvalid(rt)) {
1501 rtfree(rt);
1502 rt = rtalloc_mpath(sintosa(sin), &ip->ip_src.s_addr,
1503 m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid);
1504 if (rt == NULL((void *)0)) {
1505 ipstat_inc(ips_noroute);
1506 icmp_error(m, ICMP_UNREACH3, ICMP_UNREACH_HOST1, dest, 0);
1507 return;
1508 }
1509 }
1510
1511 /*
1512 * Save at most 68 bytes of the packet in case
1513 * we need to generate an ICMP message to the src.
1514 * The data is saved in the mbuf on the stack that
1515 * acts as a temporary storage not intended to be
1516 * passed down the IP stack or to the mfree.
1517 */
1518 memset(&mfake.m_hdr, 0, sizeof(mfake.m_hdr))__builtin_memset((&mfake.m_hdr), (0), (sizeof(mfake.m_hdr
)))
;
1519 mfake.m_typem_hdr.mh_type = m->m_typem_hdr.mh_type;
1520 if (m_dup_pkthdr(&mfake, m, M_DONTWAIT0x0002) == 0) {
1521 mfake.m_datam_hdr.mh_data = mfake.m_pktdatM_dat.MH.MH_dat.MH_databuf;
1522 len = min(ntohs(ip->ip_len)(__uint16_t)(__builtin_constant_p(ip->ip_len) ? (__uint16_t
)(((__uint16_t)(ip->ip_len) & 0xffU) << 8 | ((__uint16_t
)(ip->ip_len) & 0xff00U) >> 8) : __swap16md(ip->
ip_len))
, 68);
1523 m_copydata(m, 0, len, mfake.m_pktdatM_dat.MH.MH_dat.MH_databuf);
1524 mfake.m_pkthdrM_dat.MH.MH_pkthdr.len = mfake.m_lenm_hdr.mh_len = len;
1525#if NPF1 > 0
1526 pf_pkt_addr_changed(&mfake);
1527#endif /* NPF > 0 */
1528 fake = 1;
1529 }
1530
1531 ip->ip_ttl -= IPTTLDEC1;
1532
1533 /*
1534 * If forwarding packet using same interface that it came in on,
1535 * perhaps should send a redirect to sender to shortcut a hop.
1536 * Only send redirect if source is sending directly to us,
1537 * and if packet was not source routed (or has any options).
1538 * Also, don't send redirect if forwarding using a default route
1539 * or a route modified by a redirect.
1540 * Don't send redirect if we advertise destination's arp address
1541 * as ours (proxy arp).
1542 */
1543 if ((rt->rt_ifidx == ifp->if_index) &&
1544 (rt->rt_flags & (RTF_DYNAMIC0x10|RTF_MODIFIED0x20)) == 0 &&
1545 satosin(rt_key(rt)((rt)->rt_dest))->sin_addr.s_addr != 0 &&
1546 ipsendredirects && !srcrt &&
1547 !arpproxy(satosin(rt_key(rt)((rt)->rt_dest))->sin_addr, m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid)) {
1548 if ((ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_netmask) ==
1549 ifatoia(rt->rt_ifa)->ia_net) {
1550 if (rt->rt_flags & RTF_GATEWAY0x2)
1551 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1552 else
1553 dest = ip->ip_dst.s_addr;
1554 /* Router requirements says to only send host redirects */
1555 type = ICMP_REDIRECT5;
1556 code = ICMP_REDIRECT_HOST1;
1557 }
1558 }
1559
1560 ro.ro_rt = rt;
1561 ro.ro_tableid = m->m_pkthdrM_dat.MH.MH_pkthdr.ph_rtableid;
1562 error = ip_output(m, NULL((void *)0), &ro,
1563 (IP_FORWARDING0x1 | (ip_directedbcast ? IP_ALLOWBROADCAST0x0020 : 0)),
1564 NULL((void *)0), NULL((void *)0), 0);
1565 rt = ro.ro_rt;
1566 if (error)
1567 ipstat_inc(ips_cantforward);
1568 else {
1569 ipstat_inc(ips_forward);
1570 if (type)
1571 ipstat_inc(ips_redirectsent);
1572 else
1573 goto freecopy;
1574 }
1575 if (!fake)
1576 goto freecopy;
1577
1578 switch (error) {
1579 case 0: /* forwarded, but need redirect */
1580 /* type, code set above */
1581 break;
1582
1583 case EMSGSIZE40:
1584 type = ICMP_UNREACH3;
1585 code = ICMP_UNREACH_NEEDFRAG4;
1586 if (rt != NULL((void *)0)) {
1587 if (rt->rt_mturt_rmx.rmx_mtu) {
1588 destmtu = rt->rt_mturt_rmx.rmx_mtu;
1589 } else {
1590 struct ifnet *destifp;
1591
1592 destifp = if_get(rt->rt_ifidx);
1593 if (destifp != NULL((void *)0))
1594 destmtu = destifp->if_mtuif_data.ifi_mtu;
1595 if_put(destifp);
1596 }
1597 }
1598 ipstat_inc(ips_cantfrag);
1599 if (destmtu == 0)
1600 goto freecopy;
1601 break;
1602
1603 case EACCES13:
1604 /*
1605 * pf(4) blocked the packet. There is no need to send an ICMP
1606 * packet back since pf(4) takes care of it.
1607 */
1608 goto freecopy;
1609
1610 case ENOBUFS55:
1611 /*
1612 * a router should not generate ICMP_SOURCEQUENCH as
1613 * required in RFC1812 Requirements for IP Version 4 Routers.
1614 * source quench could be a big problem under DoS attacks,
1615 * or the underlying interface is rate-limited.
1616 */
1617 goto freecopy;
1618
1619 case ENETUNREACH51: /* shouldn't happen, checked above */
1620 case EHOSTUNREACH65:
1621 case ENETDOWN50:
1622 case EHOSTDOWN64:
1623 default:
1624 type = ICMP_UNREACH3;
1625 code = ICMP_UNREACH_HOST1;
1626 break;
1627 }
1628 mcopy = m_copym(&mfake, 0, len, M_DONTWAIT0x0002);
1629 if (mcopy)
1630 icmp_error(mcopy, type, code, dest, destmtu);
1631
1632freecopy:
1633 if (fake)
1634 m_tag_delete_chain(&mfake);
1635 rtfree(rt);
1636}
1637
1638int
1639ip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1640 size_t newlen)
1641{
1642 int error;
1643#ifdef MROUTING1
1644 extern struct mrtstat mrtstat;
1645#endif
1646
1647 /* Almost all sysctl names at this level are terminal. */
1648 if (namelen != 1 && name[0] != IPCTL_IFQUEUE30 &&
1649 name[0] != IPCTL_ARPQUEUE41)
1650 return (ENOTDIR20);
1651
1652 switch (name[0]) {
1653 case IPCTL_SOURCEROUTE5:
1654 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1655 error = sysctl_securelevel_int(oldp, oldlenp, newp, newlen,
1656 &ip_dosourceroute);
1657 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1658 return (error);
1659 case IPCTL_MTUDISC27:
1660 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1661 error = sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtudisc);
1662 if (ip_mtudisc == 0)
1663 rt_timer_queue_flush(&ip_mtudisc_timeout_q);
1664 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1665 return error;
1666 case IPCTL_MTUDISCTIMEOUT28:
1667 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1668 error = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
1669 &ip_mtudisc_timeout, 0, INT_MAX0x7fffffff);
1670 rt_timer_queue_change(&ip_mtudisc_timeout_q,
1671 ip_mtudisc_timeout);
1672 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1673 return (error);
1674#ifdef IPSEC1
1675 case IPCTL_ENCDEBUG12:
1676 case IPCTL_IPSEC_STATS13:
1677 case IPCTL_IPSEC_EXPIRE_ACQUIRE14:
1678 case IPCTL_IPSEC_EMBRYONIC_SA_TIMEOUT15:
1679 case IPCTL_IPSEC_REQUIRE_PFS16:
1680 case IPCTL_IPSEC_SOFT_ALLOCATIONS17:
1681 case IPCTL_IPSEC_ALLOCATIONS18:
1682 case IPCTL_IPSEC_SOFT_BYTES19:
1683 case IPCTL_IPSEC_BYTES20:
1684 case IPCTL_IPSEC_TIMEOUT21:
1685 case IPCTL_IPSEC_SOFT_TIMEOUT22:
1686 case IPCTL_IPSEC_SOFT_FIRSTUSE23:
1687 case IPCTL_IPSEC_FIRSTUSE24:
1688 case IPCTL_IPSEC_ENC_ALGORITHM25:
1689 case IPCTL_IPSEC_AUTH_ALGORITHM26:
1690 case IPCTL_IPSEC_IPCOMP_ALGORITHM29:
1691 return (ipsec_sysctl(name, namelen, oldp, oldlenp, newp,
1692 newlen));
1693#endif
1694 case IPCTL_IFQUEUE30:
1695 return (sysctl_niq(name + 1, namelen - 1,sysctl_mq((name + 1), (namelen - 1), (oldp), (oldlenp), (newp
), (newlen), &(&ipintrq)->ni_q)
1696 oldp, oldlenp, newp, newlen, &ipintrq)sysctl_mq((name + 1), (namelen - 1), (oldp), (oldlenp), (newp
), (newlen), &(&ipintrq)->ni_q)
);
1697 case IPCTL_ARPQUEUE41:
1698 return (sysctl_niq(name + 1, namelen - 1,sysctl_mq((name + 1), (namelen - 1), (oldp), (oldlenp), (newp
), (newlen), &(&arpinq)->ni_q)
1699 oldp, oldlenp, newp, newlen, &arpinq)sysctl_mq((name + 1), (namelen - 1), (oldp), (oldlenp), (newp
), (newlen), &(&arpinq)->ni_q)
);
1700 case IPCTL_ARPQUEUED36:
1701 return (sysctl_rdint(oldp, oldlenp, newp,
1702 atomic_load_int(&la_hold_total)));
1703 case IPCTL_STATS33:
1704 return (ip_sysctl_ipstat(oldp, oldlenp, newp));
1705#ifdef MROUTING1
1706 case IPCTL_MRTSTATS35:
1707 return (sysctl_rdstruct(oldp, oldlenp, newp,
1708 &mrtstat, sizeof(mrtstat)));
1709 case IPCTL_MRTMFC37:
1710 if (newp)
1711 return (EPERM1);
1712 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1713 error = mrt_sysctl_mfc(oldp, oldlenp);
1714 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1715 return (error);
1716 case IPCTL_MRTVIF38:
1717 if (newp)
1718 return (EPERM1);
1719 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1720 error = mrt_sysctl_vif(oldp, oldlenp);
1721 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1722 return (error);
1723#else
1724 case IPCTL_MRTPROTO34:
1725 case IPCTL_MRTSTATS35:
1726 case IPCTL_MRTMFC37:
1727 case IPCTL_MRTVIF38:
1728 return (EOPNOTSUPP45);
1729#endif
1730 default:
1731 NET_LOCK()do { rw_enter_write(&netlock); } while (0);
1732 error = sysctl_bounded_arr(ipctl_vars, nitems(ipctl_vars)(sizeof((ipctl_vars)) / sizeof((ipctl_vars)[0])),
1733 name, namelen, oldp, oldlenp, newp, newlen);
1734 NET_UNLOCK()do { rw_exit_write(&netlock); } while (0);
1735 return (error);
1736 }
1737 /* NOTREACHED */
1738}
1739
1740int
1741ip_sysctl_ipstat(void *oldp, size_t *oldlenp, void *newp)
1742{
1743 uint64_t counters[ips_ncounters];
1744 struct ipstat ipstat;
1745 u_long *words = (u_long *)&ipstat;
1746 int i;
1747
1748 CTASSERT(sizeof(ipstat) == (nitems(counters) * sizeof(u_long)))extern char _ctassert[(sizeof(ipstat) == ((sizeof((counters))
/ sizeof((counters)[0])) * sizeof(u_long))) ? 1 : -1 ] __attribute__
((__unused__))
;
1749 memset(&ipstat, 0, sizeof ipstat)__builtin_memset((&ipstat), (0), (sizeof ipstat));
1750 counters_read(ipcounters, counters, nitems(counters)(sizeof((counters)) / sizeof((counters)[0])), NULL((void *)0));
1751
1752 for (i = 0; i < nitems(counters)(sizeof((counters)) / sizeof((counters)[0])); i++)
1753 words[i] = (u_long)counters[i];
1754
1755 return (sysctl_rdstruct(oldp, oldlenp, newp, &ipstat, sizeof(ipstat)));
1756}
1757
1758void
1759ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1760 struct mbuf *m)
1761{
1762 if (inp->inp_socket->so_options & SO_TIMESTAMP0x0800) {
1763 struct timeval tv;
1764
1765 m_microtime(m, &tv);
1766 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1767 SCM_TIMESTAMP0x04, SOL_SOCKET0xffff);
1768 if (*mp)
1769 mp = &(*mp)->m_nextm_hdr.mh_next;
1770 }
1771
1772 if (inp->inp_flags & INP_RECVDSTADDR0x004) {
1773 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1774 sizeof(struct in_addr), IP_RECVDSTADDR7, IPPROTO_IP0);
1775 if (*mp)
1776 mp = &(*mp)->m_nextm_hdr.mh_next;
1777 }
1778#ifdef notyet
1779 /* this code is broken and will probably never be fixed. */
1780 /* options were tossed already */
1781 if (inp->inp_flags & INP_RECVOPTS0x001) {
1782 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1783 sizeof(struct in_addr), IP_RECVOPTS5, IPPROTO_IP0);
1784 if (*mp)
1785 mp = &(*mp)->m_nextm_hdr.mh_next;
1786 }
1787 /* ip_srcroute doesn't do what we want here, need to fix */
1788 if (inp->inp_flags & INP_RECVRETOPTS0x002) {
1789 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
1790 sizeof(struct in_addr), IP_RECVRETOPTS6, IPPROTO_IP0);
1791 if (*mp)
1792 mp = &(*mp)->m_nextm_hdr.mh_next;
1793 }
1794#endif
1795 if (inp->inp_flags & INP_RECVIF0x080) {
1796 struct sockaddr_dl sdl;
1797 struct ifnet *ifp;
1798
1799 ifp = if_get(m->m_pkthdrM_dat.MH.MH_pkthdr.ph_ifidx);
1800 if (ifp == NULL((void *)0) || ifp->if_sadl == NULL((void *)0)) {
1801 memset(&sdl, 0, sizeof(sdl))__builtin_memset((&sdl), (0), (sizeof(sdl)));
1802 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0])__builtin_offsetof(struct sockaddr_dl, sdl_data[0]);
1803 sdl.sdl_family = AF_LINK18;
1804 sdl.sdl_index = ifp != NULL((void *)0) ? ifp->if_index : 0;
1805 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1806 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1807 IP_RECVIF30, IPPROTO_IP0);
1808 } else {
1809 *mp = sbcreatecontrol((caddr_t) ifp->if_sadl,
1810 ifp->if_sadl->sdl_len, IP_RECVIF30, IPPROTO_IP0);
1811 }
1812 if (*mp)
1813 mp = &(*mp)->m_nextm_hdr.mh_next;
1814 if_put(ifp);
1815 }
1816 if (inp->inp_flags & INP_RECVTTL0x040) {
1817 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
1818 sizeof(u_int8_t), IP_RECVTTL31, IPPROTO_IP0);
1819 if (*mp)
1820 mp = &(*mp)->m_nextm_hdr.mh_next;
1821 }
1822 if (inp->inp_flags & INP_RECVRTABLE0x400) {
1823 u_int rtableid = inp->inp_rtableid;
1824
1825#if NPF1 > 0
1826 if (m && m->m_pkthdrM_dat.MH.MH_pkthdr.pf.flags & PF_TAG_DIVERTED0x08) {
1827 struct pf_divert *divert;
1828
1829 divert = pf_find_divert(m);
1830 KASSERT(divert != NULL)((divert != ((void *)0)) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/netinet/ip_input.c"
, 1830, "divert != NULL"))
;
1831 rtableid = divert->rdomain;
1832 }
1833#endif
1834
1835 *mp = sbcreatecontrol((caddr_t) &rtableid,
1836 sizeof(u_int), IP_RECVRTABLE35, IPPROTO_IP0);
1837 if (*mp)
1838 mp = &(*mp)->m_nextm_hdr.mh_next;
Value stored to 'mp' is never read
1839 }
1840}
1841
1842void
1843ip_send_do_dispatch(void *xmq, int flags)
1844{
1845 struct mbuf_queue *mq = xmq;
1846 struct mbuf *m;
1847 struct mbuf_list ml;
1848 struct m_tag *mtag;
1849
1850 mq_delist(mq, &ml);
1851 if (ml_empty(&ml)((&ml)->ml_len == 0))
1852 return;
1853
1854 NET_LOCK_SHARED()do { rw_enter_read(&netlock); } while (0);
1855 while ((m = ml_dequeue(&ml)) != NULL((void *)0)) {
1856 u_int32_t ipsecflowinfo = 0;
1857
1858 if ((mtag = m_tag_find(m, PACKET_TAG_IPSEC_FLOWINFO0x0004, NULL((void *)0)))
1859 != NULL((void *)0)) {
1860 ipsecflowinfo = *(u_int32_t *)(mtag + 1);
1861 m_tag_delete(m, mtag);
1862 }
1863 ip_output(m, NULL((void *)0), NULL((void *)0), flags, NULL((void *)0), NULL((void *)0), ipsecflowinfo);
1864 }
1865 NET_UNLOCK_SHARED()do { rw_exit_read(&netlock); } while (0);
1866}
1867
1868void
1869ip_sendraw_dispatch(void *xmq)
1870{
1871 ip_send_do_dispatch(xmq, IP_RAWOUTPUT0x2);
1872}
1873
1874void
1875ip_send_dispatch(void *xmq)
1876{
1877 ip_send_do_dispatch(xmq, 0);
1878}
1879
1880void
1881ip_send(struct mbuf *m)
1882{
1883 mq_enqueue(&ipsend_mq, m);
1884 task_add(net_tq(0), &ipsend_task);
1885}
1886
1887void
1888ip_send_raw(struct mbuf *m)
1889{
1890 mq_enqueue(&ipsendraw_mq, m);
1891 task_add(net_tq(0), &ipsendraw_task);
1892}