]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2013 Apple Inc. All rights reserved. |
6d2010ae A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
9bccf70c A |
29 | /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $ */ |
30 | /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */ | |
1c79356b A |
31 | |
32 | /* | |
33 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | /* | |
62 | * Copyright (c) 1982, 1986, 1989, 1993 | |
63 | * The Regents of the University of California. All rights reserved. | |
64 | * | |
65 | * Redistribution and use in source and binary forms, with or without | |
66 | * modification, are permitted provided that the following conditions | |
67 | * are met: | |
68 | * 1. Redistributions of source code must retain the above copyright | |
69 | * notice, this list of conditions and the following disclaimer. | |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in the | |
72 | * documentation and/or other materials provided with the distribution. | |
73 | * 3. All advertising materials mentioning features or use of this software | |
74 | * must display the following acknowledgement: | |
75 | * This product includes software developed by the University of | |
76 | * California, Berkeley and its contributors. | |
77 | * 4. Neither the name of the University nor the names of its contributors | |
78 | * may be used to endorse or promote products derived from this software | |
79 | * without specific prior written permission. | |
80 | * | |
81 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
82 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
83 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
84 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
85 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
86 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
87 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
88 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
89 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
90 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
91 | * SUCH DAMAGE. | |
92 | * | |
93 | * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 | |
94 | */ | |
1c79356b A |
95 | |
96 | #include <sys/param.h> | |
97 | #include <sys/kernel.h> | |
98 | #include <sys/malloc.h> | |
99 | #include <sys/mbuf.h> | |
100 | #include <sys/protosw.h> | |
101 | #include <sys/socket.h> | |
102 | #include <sys/socketvar.h> | |
103 | #include <sys/sysctl.h> | |
104 | #include <sys/errno.h> | |
105 | #include <sys/stat.h> | |
106 | #include <sys/systm.h> | |
107 | #include <sys/syslog.h> | |
108 | #include <sys/proc.h> | |
91447636 | 109 | #include <sys/kauth.h> |
1c79356b A |
110 | |
111 | #include <net/if.h> | |
112 | #include <net/route.h> | |
113 | #include <net/if_types.h> | |
6d2010ae | 114 | #include <net/ntstat.h> |
39236c6e | 115 | #include <net/dlil.h> |
1c79356b A |
116 | |
117 | #include <netinet/in.h> | |
118 | #include <netinet/in_systm.h> | |
119 | #include <netinet/ip.h> | |
120 | #include <netinet/in_pcb.h> | |
121 | #include <netinet/in_var.h> | |
122 | #include <netinet/ip_var.h> | |
123 | #include <netinet/udp.h> | |
124 | #include <netinet/udp_var.h> | |
125 | #include <netinet/ip6.h> | |
126 | #include <netinet6/ip6_var.h> | |
127 | #include <netinet6/in6_pcb.h> | |
128 | #include <netinet/icmp6.h> | |
129 | #include <netinet6/udp6_var.h> | |
130 | #include <netinet6/ip6protosw.h> | |
131 | ||
132 | #if IPSEC | |
133 | #include <netinet6/ipsec.h> | |
9bccf70c A |
134 | #include <netinet6/ipsec6.h> |
135 | extern int ipsec_bypass; | |
39236c6e | 136 | #endif /* IPSEC */ |
1c79356b A |
137 | |
138 | /* | |
139 | * UDP protocol inplementation. | |
140 | * Per RFC 768, August, 1980. | |
141 | */ | |
142 | ||
39236c6e A |
143 | static int udp6_abort(struct socket *); |
144 | static int udp6_attach(struct socket *, int, struct proc *); | |
145 | static int udp6_bind(struct socket *, struct sockaddr *, struct proc *); | |
146 | static int udp6_connectx(struct socket *, struct sockaddr_list **, | |
147 | struct sockaddr_list **, struct proc *, uint32_t, associd_t, connid_t *, | |
148 | uint32_t, void *, uint32_t); | |
149 | static int udp6_detach(struct socket *); | |
150 | static int udp6_disconnect(struct socket *); | |
151 | static int udp6_disconnectx(struct socket *, associd_t, connid_t); | |
152 | static int udp6_send(struct socket *, int, struct mbuf *, struct sockaddr *, | |
153 | struct mbuf *, struct proc *); | |
2d21ac55 | 154 | static void udp6_append(struct inpcb *, struct ip6_hdr *, |
39236c6e A |
155 | struct sockaddr_in6 *, struct mbuf *, int, struct ifnet *); |
156 | static int udp6_input_checksum(struct mbuf *, struct udphdr *, int, int); | |
91447636 | 157 | |
2d21ac55 | 158 | #if IPFIREWALL |
39236c6e A |
159 | extern int fw_verbose; |
160 | extern void ipfwsyslog( int level, const char *format,...); | |
161 | extern void ipfw_stealth_stats_incr_udpv6(void); | |
162 | ||
163 | /* Apple logging, log to ipfw.log */ | |
164 | #define log_in_vain_log(a) { \ | |
165 | if ((udp_log_in_vain == 3) && (fw_verbose == 2)) { \ | |
166 | ipfwsyslog a; \ | |
167 | } else if ((udp_log_in_vain == 4) && (fw_verbose == 2)) { \ | |
168 | ipfw_stealth_stats_incr_udpv6(); \ | |
169 | } else { \ | |
170 | log a; \ | |
171 | } \ | |
91447636 | 172 | } |
39236c6e | 173 | #else /* !IPFIREWALL */ |
2d21ac55 | 174 | #define log_in_vain_log( a ) { log a; } |
39236c6e A |
175 | #endif /* !IPFIREWALL */ |
176 | ||
177 | struct pr_usrreqs udp6_usrreqs = { | |
178 | .pru_abort = udp6_abort, | |
179 | .pru_attach = udp6_attach, | |
180 | .pru_bind = udp6_bind, | |
181 | .pru_connect = udp6_connect, | |
182 | .pru_connectx = udp6_connectx, | |
183 | .pru_control = in6_control, | |
184 | .pru_detach = udp6_detach, | |
185 | .pru_disconnect = udp6_disconnect, | |
186 | .pru_disconnectx = udp6_disconnectx, | |
187 | .pru_peeraddr = in6_mapped_peeraddr, | |
188 | .pru_send = udp6_send, | |
189 | .pru_shutdown = udp_shutdown, | |
190 | .pru_sockaddr = in6_mapped_sockaddr, | |
191 | .pru_sosend = sosend, | |
192 | .pru_soreceive = soreceive, | |
193 | }; | |
1c79356b | 194 | |
2d21ac55 A |
195 | /* |
196 | * subroutine of udp6_input(), mainly for source code readability. | |
197 | */ | |
198 | static void | |
39236c6e A |
199 | udp6_append(struct inpcb *last, struct ip6_hdr *ip6, |
200 | struct sockaddr_in6 *udp_in6, struct mbuf *n, int off, struct ifnet *ifp) | |
2d21ac55 | 201 | { |
39236c6e | 202 | #pragma unused(ip6) |
2d21ac55 | 203 | struct mbuf *opts = NULL; |
6d2010ae | 204 | int ret = 0; |
39236c6e A |
205 | boolean_t cell = IFNET_IS_CELLULAR(ifp); |
206 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
207 | ||
2d21ac55 A |
208 | #if CONFIG_MACF_NET |
209 | if (mac_inpcb_check_deliver(last, n, AF_INET6, SOCK_DGRAM) != 0) { | |
210 | m_freem(n); | |
211 | return; | |
212 | } | |
39236c6e A |
213 | #endif /* CONFIG_MACF_NET */ |
214 | if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || | |
6d2010ae A |
215 | (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || |
216 | (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
217 | ret = ip6_savecontrol(last, n, &opts); | |
218 | if (ret != 0) { | |
219 | m_freem(n); | |
220 | m_freem(opts); | |
221 | return; | |
222 | } | |
223 | } | |
2d21ac55 | 224 | m_adj(n, off); |
6d2010ae | 225 | if (nstat_collect) { |
39236c6e A |
226 | INP_ADD_STAT(last, cell, wifi, rxpackets, 1); |
227 | INP_ADD_STAT(last, cell, wifi, rxbytes, n->m_pkthdr.len); | |
6d2010ae A |
228 | } |
229 | so_recv_data_stat(last->in6p_socket, n, 0); | |
2d21ac55 A |
230 | if (sbappendaddr(&last->in6p_socket->so_rcv, |
231 | (struct sockaddr *)udp_in6, n, opts, NULL) == 0) | |
232 | udpstat.udps_fullsock++; | |
233 | else | |
234 | sorwakeup(last->in6p_socket); | |
235 | } | |
236 | ||
1c79356b | 237 | int |
39236c6e | 238 | udp6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 239 | { |
6d2010ae | 240 | #pragma unused(proto) |
1c79356b | 241 | struct mbuf *m = *mp; |
6d2010ae | 242 | struct ifnet *ifp; |
39236c6e A |
243 | struct ip6_hdr *ip6; |
244 | struct udphdr *uh; | |
245 | struct inpcb *in6p; | |
9bccf70c | 246 | struct mbuf *opts = NULL; |
1c79356b | 247 | int off = *offp; |
6d2010ae | 248 | int plen, ulen, ret = 0; |
39236c6e | 249 | boolean_t cell, wifi; |
1c79356b | 250 | struct sockaddr_in6 udp_in6; |
91447636 | 251 | struct inpcbinfo *pcbinfo = &udbinfo; |
6d2010ae | 252 | struct sockaddr_in6 fromsa; |
1c79356b | 253 | |
39236c6e | 254 | IP6_EXTHDR_CHECK(m, off, sizeof (struct udphdr), return IPPROTO_DONE); |
9bccf70c | 255 | |
316670eb A |
256 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
257 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
258 | ||
6d2010ae | 259 | ifp = m->m_pkthdr.rcvif; |
9bccf70c | 260 | ip6 = mtod(m, struct ip6_hdr *); |
39236c6e A |
261 | cell = IFNET_IS_CELLULAR(ifp); |
262 | wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
9bccf70c | 263 | |
9bccf70c | 264 | udpstat.udps_ipackets++; |
1c79356b | 265 | |
39236c6e | 266 | plen = ntohs(ip6->ip6_plen) - off + sizeof (*ip6); |
316670eb | 267 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
1c79356b A |
268 | ulen = ntohs((u_short)uh->uh_ulen); |
269 | ||
270 | if (plen != ulen) { | |
271 | udpstat.udps_badlen++; | |
39236c6e | 272 | IF_UDP_STATINC(ifp, badlength); |
1c79356b A |
273 | goto bad; |
274 | } | |
275 | ||
6d2010ae | 276 | /* destination port of 0 is illegal, based on RFC768. */ |
316670eb | 277 | if (uh->uh_dport == 0) { |
39236c6e | 278 | IF_UDP_STATINC(ifp, port0); |
6d2010ae | 279 | goto bad; |
316670eb | 280 | } |
6d2010ae | 281 | |
1c79356b A |
282 | /* |
283 | * Checksum extended UDP header and data. | |
284 | */ | |
39236c6e A |
285 | if (udp6_input_checksum(m, uh, off, ulen)) |
286 | goto bad; | |
6d2010ae A |
287 | |
288 | /* | |
289 | * Construct sockaddr format source address. | |
290 | */ | |
291 | init_sin6(&fromsa, m); | |
292 | fromsa.sin6_port = uh->uh_sport; | |
293 | ||
1c79356b | 294 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
2d21ac55 | 295 | int reuse_sock = 0, mcast_delivered = 0; |
6d2010ae | 296 | struct ip6_moptions *imo; |
2d21ac55 | 297 | struct mbuf *n = NULL; |
1c79356b A |
298 | |
299 | /* | |
300 | * Deliver a multicast datagram to all sockets | |
301 | * for which the local and remote addresses and ports match | |
302 | * those of the incoming datagram. This allows more than | |
303 | * one process to receive multicasts on the same port. | |
304 | * (This really ought to be done for unicast datagrams as | |
305 | * well, but that would cause problems with existing | |
306 | * applications that open both address-specific sockets and | |
307 | * a wildcard socket listening to the same port -- they would | |
308 | * end up receiving duplicates of every unicast datagram. | |
309 | * Those applications open the multiple sockets to overcome an | |
310 | * inadequacy of the UDP socket interface, but for backwards | |
311 | * compatibility we avoid the problem here rather than | |
312 | * fixing the interface. Maybe 4.5BSD will remedy this?) | |
313 | */ | |
314 | ||
315 | /* | |
316 | * In a case that laddr should be set to the link-local | |
317 | * address (this happens in RIPng), the multicast address | |
318 | * specified in the received packet does not match with | |
319 | * laddr. To cure this situation, the matching is relaxed | |
320 | * if the receiving interface is the same as one specified | |
321 | * in the socket and if the destination multicast address | |
322 | * matches one of the multicast groups specified in the socket. | |
323 | */ | |
324 | ||
325 | /* | |
326 | * Construct sockaddr format source address. | |
327 | */ | |
328 | init_sin6(&udp_in6, m); /* general init */ | |
329 | udp_in6.sin6_port = uh->uh_sport; | |
330 | /* | |
331 | * KAME note: usually we drop udphdr from mbuf here. | |
332 | * We need udphdr for IPsec processing so we do that later. | |
333 | */ | |
334 | ||
335 | /* | |
336 | * Locate pcb(s) for datagram. | |
337 | * (Algorithm copied from raw_intr().) | |
338 | */ | |
39236c6e | 339 | lck_rw_lock_shared(pcbinfo->ipi_lock); |
91447636 | 340 | |
1c79356b | 341 | LIST_FOREACH(in6p, &udb, inp_list) { |
39236c6e A |
342 | #if IPSEC |
343 | int skipit; | |
344 | #endif /* IPSEC */ | |
91447636 | 345 | |
1c79356b A |
346 | if ((in6p->inp_vflag & INP_IPV6) == 0) |
347 | continue; | |
91447636 | 348 | |
39236c6e A |
349 | if (inp_restricted(in6p, ifp)) |
350 | continue; | |
351 | ||
352 | if (IFNET_IS_CELLULAR(ifp) && | |
353 | (in6p->in6p_flags & INP_NO_IFT_CELLULAR)) | |
316670eb A |
354 | continue; |
355 | ||
39236c6e A |
356 | if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) == |
357 | WNT_STOPUSING) | |
91447636 A |
358 | continue; |
359 | ||
316670eb | 360 | udp_lock(in6p->in6p_socket, 1, 0); |
91447636 | 361 | |
39236c6e A |
362 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == |
363 | WNT_STOPUSING) { | |
91447636 A |
364 | udp_unlock(in6p->in6p_socket, 1, 0); |
365 | continue; | |
366 | } | |
367 | if (in6p->in6p_lport != uh->uh_dport) { | |
368 | udp_unlock(in6p->in6p_socket, 1, 0); | |
1c79356b | 369 | continue; |
91447636 | 370 | } |
6d2010ae A |
371 | |
372 | /* | |
373 | * Handle socket delivery policy for any-source | |
374 | * and source-specific multicast. [RFC3678] | |
375 | */ | |
376 | imo = in6p->in6p_moptions; | |
377 | if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
39236c6e A |
378 | struct sockaddr_in6 mcaddr; |
379 | int blocked; | |
6d2010ae | 380 | |
316670eb | 381 | IM6O_LOCK(imo); |
39236c6e A |
382 | bzero(&mcaddr, sizeof (struct sockaddr_in6)); |
383 | mcaddr.sin6_len = sizeof (struct sockaddr_in6); | |
6d2010ae A |
384 | mcaddr.sin6_family = AF_INET6; |
385 | mcaddr.sin6_addr = ip6->ip6_dst; | |
386 | ||
387 | blocked = im6o_mc_filter(imo, ifp, | |
39236c6e A |
388 | (struct sockaddr *)&mcaddr, |
389 | (struct sockaddr *)&fromsa); | |
316670eb | 390 | IM6O_UNLOCK(imo); |
6d2010ae | 391 | if (blocked != MCAST_PASS) { |
91447636 | 392 | udp_unlock(in6p->in6p_socket, 1, 0); |
39236c6e A |
393 | if (blocked == MCAST_NOTSMEMBER || |
394 | blocked == MCAST_MUTED) | |
395 | udpstat.udps_filtermcast++; | |
1c79356b | 396 | continue; |
91447636 | 397 | } |
1c79356b | 398 | } |
39236c6e A |
399 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && |
400 | (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, | |
401 | &ip6->ip6_src) || | |
402 | in6p->in6p_fport != uh->uh_sport)) { | |
403 | udp_unlock(in6p->in6p_socket, 1, 0); | |
404 | continue; | |
1c79356b A |
405 | } |
406 | ||
2d21ac55 A |
407 | reuse_sock = in6p->inp_socket->so_options & |
408 | (SO_REUSEPORT | SO_REUSEADDR); | |
1c79356b A |
409 | |
410 | #if IPSEC | |
39236c6e A |
411 | skipit = 0; |
412 | /* Check AH/ESP integrity. */ | |
413 | if (ipsec_bypass == 0 && | |
414 | ipsec6_in_reject_so(m, in6p->inp_socket)) { | |
415 | IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio); | |
416 | /* do not inject data to pcb */ | |
417 | skipit = 1; | |
418 | } | |
419 | if (skipit == 0) | |
420 | #endif /* IPSEC */ | |
421 | { | |
422 | /* | |
423 | * KAME NOTE: do not | |
424 | * m_copy(m, offset, ...) below. | |
425 | * sbappendaddr() expects M_PKTHDR, | |
426 | * and m_copy() will copy M_PKTHDR | |
427 | * only if offset is 0. | |
428 | */ | |
429 | if (reuse_sock) | |
430 | n = m_copy(m, 0, M_COPYALL); | |
431 | udp6_append(in6p, ip6, &udp_in6, m, | |
432 | off + sizeof (struct udphdr), ifp); | |
433 | mcast_delivered++; | |
1c79356b | 434 | } |
39236c6e A |
435 | udp_unlock(in6p->in6p_socket, 1, 0); |
436 | ||
1c79356b A |
437 | /* |
438 | * Don't look for additional matches if this one does | |
439 | * not have either the SO_REUSEPORT or SO_REUSEADDR | |
440 | * socket options set. This heuristic avoids searching | |
441 | * through all pcbs in the common case of a non-shared | |
442 | * port. It assumes that an application will never | |
443 | * clear these options after setting them. | |
444 | */ | |
2d21ac55 | 445 | if (reuse_sock == 0 || ((m = n) == NULL)) |
1c79356b | 446 | break; |
316670eb A |
447 | |
448 | /* | |
449 | * Expect 32-bit aligned data pointer on strict-align | |
450 | * platforms. | |
451 | */ | |
452 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
453 | ||
b0d623f7 A |
454 | /* |
455 | * Recompute IP and UDP header pointers for new mbuf | |
456 | */ | |
457 | ip6 = mtod(m, struct ip6_hdr *); | |
316670eb | 458 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
1c79356b | 459 | } |
39236c6e | 460 | lck_rw_done(pcbinfo->ipi_lock); |
1c79356b | 461 | |
2d21ac55 | 462 | if (mcast_delivered == 0) { |
1c79356b A |
463 | /* |
464 | * No matching pcb found; discard datagram. | |
465 | * (No need to send an ICMP Port Unreachable | |
466 | * for a broadcast or multicast datgram.) | |
467 | */ | |
468 | udpstat.udps_noport++; | |
9bccf70c | 469 | udpstat.udps_noportmcast++; |
39236c6e | 470 | IF_UDP_STATINC(ifp, port_unreach); |
1c79356b A |
471 | goto bad; |
472 | } | |
2d21ac55 A |
473 | |
474 | if (reuse_sock != 0) /* free the extra copy of mbuf */ | |
475 | m_freem(m); | |
39236c6e | 476 | return (IPPROTO_DONE); |
1c79356b A |
477 | } |
478 | /* | |
479 | * Locate pcb for datagram. | |
480 | */ | |
481 | in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, | |
39236c6e | 482 | &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); |
316670eb | 483 | if (in6p == NULL) { |
39236c6e | 484 | IF_UDP_STATINC(ifp, port_unreach); |
316670eb | 485 | |
39236c6e | 486 | if (udp_log_in_vain) { |
1c79356b A |
487 | char buf[INET6_ADDRSTRLEN]; |
488 | ||
39236c6e A |
489 | strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof (buf)); |
490 | if (udp_log_in_vain < 3) { | |
491 | log(LOG_INFO, "Connection attempt to UDP " | |
492 | "%s:%d from %s:%d\n", buf, | |
493 | ntohs(uh->uh_dport), | |
494 | ip6_sprintf(&ip6->ip6_src), | |
495 | ntohs(uh->uh_sport)); | |
496 | } else if (!(m->m_flags & (M_BCAST | M_MCAST)) && | |
497 | !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { | |
498 | log_in_vain_log((LOG_INFO, "Connection attempt " | |
499 | "to UDP %s:%d from %s:%d\n", buf, | |
500 | ntohs(uh->uh_dport), | |
501 | ip6_sprintf(&ip6->ip6_src), | |
502 | ntohs(uh->uh_sport))); | |
503 | } | |
1c79356b A |
504 | } |
505 | udpstat.udps_noport++; | |
506 | if (m->m_flags & M_MCAST) { | |
507 | printf("UDP6: M_MCAST is set in a unicast packet.\n"); | |
9bccf70c | 508 | udpstat.udps_noportmcast++; |
39236c6e | 509 | IF_UDP_STATINC(ifp, badmcast); |
1c79356b A |
510 | goto bad; |
511 | } | |
512 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); | |
39236c6e | 513 | return (IPPROTO_DONE); |
1c79356b A |
514 | } |
515 | #if IPSEC | |
516 | /* | |
517 | * Check AH/ESP integrity. | |
518 | */ | |
91447636 | 519 | if (ipsec_bypass == 0) { |
91447636 | 520 | if (ipsec6_in_reject_so(m, in6p->in6p_socket)) { |
2d21ac55 | 521 | IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio); |
91447636 | 522 | in_pcb_checkstate(in6p, WNT_RELEASE, 0); |
39236c6e | 523 | IF_UDP_STATINC(ifp, badipsec); |
91447636 A |
524 | goto bad; |
525 | } | |
1c79356b | 526 | } |
39236c6e | 527 | #endif /* IPSEC */ |
1c79356b A |
528 | |
529 | /* | |
530 | * Construct sockaddr format source address. | |
531 | * Stuff source address and datagram in user buffer. | |
532 | */ | |
91447636 A |
533 | udp_lock(in6p->in6p_socket, 1, 0); |
534 | ||
535 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
536 | udp_unlock(in6p->in6p_socket, 1, 0); | |
39236c6e | 537 | IF_UDP_STATINC(ifp, cleanup); |
91447636 A |
538 | goto bad; |
539 | } | |
316670eb | 540 | |
1c79356b A |
541 | init_sin6(&udp_in6, m); /* general init */ |
542 | udp_in6.sin6_port = uh->uh_sport; | |
39236c6e A |
543 | if ((in6p->in6p_flags & INP_CONTROLOPTS) != 0 || |
544 | (in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0 || | |
545 | (in6p->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
6d2010ae A |
546 | ret = ip6_savecontrol(in6p, m, &opts); |
547 | if (ret != 0) { | |
548 | udp_unlock(in6p->in6p_socket, 1, 0); | |
549 | goto bad; | |
550 | } | |
551 | } | |
39236c6e | 552 | m_adj(m, off + sizeof (struct udphdr)); |
6d2010ae | 553 | if (nstat_collect) { |
39236c6e A |
554 | INP_ADD_STAT(in6p, cell, wifi, rxpackets, 1); |
555 | INP_ADD_STAT(in6p, cell, wifi, rxbytes, m->m_pkthdr.len); | |
6d2010ae A |
556 | } |
557 | so_recv_data_stat(in6p->in6p_socket, m, 0); | |
1c79356b | 558 | if (sbappendaddr(&in6p->in6p_socket->so_rcv, |
39236c6e | 559 | (struct sockaddr *)&udp_in6, m, opts, NULL) == 0) { |
91447636 A |
560 | m = NULL; |
561 | opts = NULL; | |
1c79356b | 562 | udpstat.udps_fullsock++; |
91447636 | 563 | udp_unlock(in6p->in6p_socket, 1, 0); |
1c79356b A |
564 | goto bad; |
565 | } | |
566 | sorwakeup(in6p->in6p_socket); | |
91447636 | 567 | udp_unlock(in6p->in6p_socket, 1, 0); |
39236c6e | 568 | return (IPPROTO_DONE); |
1c79356b | 569 | bad: |
39236c6e | 570 | if (m != NULL) |
1c79356b | 571 | m_freem(m); |
39236c6e | 572 | if (opts != NULL) |
9bccf70c | 573 | m_freem(opts); |
39236c6e | 574 | return (IPPROTO_DONE); |
1c79356b A |
575 | } |
576 | ||
577 | void | |
39236c6e | 578 | udp6_ctlinput(int cmd, struct sockaddr *sa, void *d) |
1c79356b | 579 | { |
1c79356b | 580 | struct udphdr uh; |
1c79356b A |
581 | struct ip6_hdr *ip6; |
582 | struct mbuf *m; | |
583 | int off = 0; | |
9bccf70c A |
584 | struct ip6ctlparam *ip6cp = NULL; |
585 | const struct sockaddr_in6 *sa6_src = NULL; | |
91447636 | 586 | void (*notify)(struct inpcb *, int) = udp_notify; |
9bccf70c A |
587 | struct udp_portonly { |
588 | u_int16_t uh_sport; | |
589 | u_int16_t uh_dport; | |
590 | } *uhp; | |
1c79356b A |
591 | |
592 | if (sa->sa_family != AF_INET6 || | |
39236c6e | 593 | sa->sa_len != sizeof (struct sockaddr_in6)) |
1c79356b A |
594 | return; |
595 | ||
596 | if ((unsigned)cmd >= PRC_NCMDS) | |
597 | return; | |
598 | if (PRC_IS_REDIRECT(cmd)) | |
599 | notify = in6_rtchange, d = NULL; | |
600 | else if (cmd == PRC_HOSTDEAD) | |
601 | d = NULL; | |
602 | else if (inet6ctlerrmap[cmd] == 0) | |
603 | return; | |
604 | ||
605 | /* if the parameter is from icmp6, decode it. */ | |
606 | if (d != NULL) { | |
9bccf70c | 607 | ip6cp = (struct ip6ctlparam *)d; |
1c79356b A |
608 | m = ip6cp->ip6c_m; |
609 | ip6 = ip6cp->ip6c_ip6; | |
610 | off = ip6cp->ip6c_off; | |
9bccf70c | 611 | sa6_src = ip6cp->ip6c_src; |
1c79356b A |
612 | } else { |
613 | m = NULL; | |
614 | ip6 = NULL; | |
9bccf70c | 615 | sa6_src = &sa6_any; |
1c79356b A |
616 | } |
617 | ||
39236c6e | 618 | if (ip6 != NULL) { |
1c79356b A |
619 | /* |
620 | * XXX: We assume that when IPV6 is non NULL, | |
621 | * M and OFF are valid. | |
622 | */ | |
1c79356b | 623 | |
9bccf70c | 624 | /* check if we can safely examine src and dst ports */ |
39236c6e | 625 | if (m->m_pkthdr.len < off + sizeof (*uhp)) |
9bccf70c | 626 | return; |
1c79356b | 627 | |
39236c6e A |
628 | bzero(&uh, sizeof (uh)); |
629 | m_copydata(m, off, sizeof (*uhp), (caddr_t)&uh); | |
9bccf70c | 630 | |
91447636 | 631 | (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport, |
39236c6e A |
632 | (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport, |
633 | cmd, NULL, notify); | |
634 | } else { | |
635 | (void) in6_pcbnotify(&udbinfo, sa, 0, | |
636 | (struct sockaddr *)&sa6_src, 0, cmd, NULL, notify); | |
1c79356b | 637 | } |
1c79356b A |
638 | } |
639 | ||
1c79356b A |
640 | static int |
641 | udp6_abort(struct socket *so) | |
642 | { | |
643 | struct inpcb *inp; | |
1c79356b A |
644 | |
645 | inp = sotoinpcb(so); | |
39236c6e A |
646 | if (inp == NULL) { |
647 | panic("%s: so=%p null inp\n", __func__, so); | |
648 | /* NOTREACHED */ | |
649 | } | |
1c79356b | 650 | soisdisconnected(so); |
1c79356b | 651 | in6_pcbdetach(inp); |
39236c6e | 652 | return (0); |
1c79356b A |
653 | } |
654 | ||
655 | static int | |
39236c6e | 656 | udp6_attach(struct socket *so, int proto, struct proc *p) |
1c79356b | 657 | { |
39236c6e | 658 | #pragma unused(proto) |
1c79356b | 659 | struct inpcb *inp; |
91447636 | 660 | int error; |
1c79356b A |
661 | |
662 | inp = sotoinpcb(so); | |
39236c6e A |
663 | if (inp != NULL) |
664 | return (EINVAL); | |
1c79356b | 665 | |
91447636 A |
666 | error = in_pcballoc(so, &udbinfo, p); |
667 | if (error) | |
39236c6e | 668 | return (error); |
91447636 | 669 | |
1c79356b A |
670 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { |
671 | error = soreserve(so, udp_sendspace, udp_recvspace); | |
672 | if (error) | |
39236c6e | 673 | return (error); |
1c79356b | 674 | } |
1c79356b A |
675 | inp = (struct inpcb *)so->so_pcb; |
676 | inp->inp_vflag |= INP_IPV6; | |
55e303ae A |
677 | if (ip6_mapped_addr_on) |
678 | inp->inp_vflag |= INP_IPV4; | |
1c79356b A |
679 | inp->in6p_hops = -1; /* use kernel default */ |
680 | inp->in6p_cksum = -1; /* just to be sure */ | |
9bccf70c A |
681 | /* |
682 | * XXX: ugly!! | |
683 | * IPv4 TTL initialization is necessary for an IPv6 socket as well, | |
684 | * because the socket may be bound to an IPv6 wildcard address, | |
685 | * which may match an IPv4-mapped IPv6 address. | |
686 | */ | |
687 | inp->inp_ip_ttl = ip_defttl; | |
316670eb A |
688 | if (nstat_collect) |
689 | nstat_udp_new_pcb(inp); | |
39236c6e | 690 | return (0); |
1c79356b A |
691 | } |
692 | ||
693 | static int | |
694 | udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
695 | { | |
696 | struct inpcb *inp; | |
91447636 | 697 | int error; |
1c79356b A |
698 | |
699 | inp = sotoinpcb(so); | |
39236c6e A |
700 | if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) |
701 | return (inp == NULL ? EINVAL : EPROTOTYPE); | |
1c79356b A |
702 | |
703 | inp->inp_vflag &= ~INP_IPV4; | |
704 | inp->inp_vflag |= INP_IPV6; | |
9bccf70c | 705 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b A |
706 | struct sockaddr_in6 *sin6_p; |
707 | ||
316670eb | 708 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
1c79356b | 709 | |
39236c6e | 710 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { |
1c79356b | 711 | inp->inp_vflag |= INP_IPV4; |
39236c6e | 712 | } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
1c79356b A |
713 | struct sockaddr_in sin; |
714 | ||
715 | in6_sin6_2_sin(&sin, sin6_p); | |
716 | inp->inp_vflag |= INP_IPV4; | |
717 | inp->inp_vflag &= ~INP_IPV6; | |
1c79356b | 718 | error = in_pcbbind(inp, (struct sockaddr *)&sin, p); |
39236c6e | 719 | return (error); |
1c79356b A |
720 | } |
721 | } | |
722 | ||
1c79356b | 723 | error = in6_pcbbind(inp, nam, p); |
39236c6e | 724 | return (error); |
1c79356b A |
725 | } |
726 | ||
39236c6e | 727 | int |
1c79356b A |
728 | udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p) |
729 | { | |
730 | struct inpcb *inp; | |
91447636 | 731 | int error; |
1c79356b A |
732 | |
733 | inp = sotoinpcb(so); | |
39236c6e A |
734 | if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) |
735 | return (inp == NULL ? EINVAL : EPROTOTYPE); | |
1c79356b | 736 | |
9bccf70c | 737 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b A |
738 | struct sockaddr_in6 *sin6_p; |
739 | ||
316670eb | 740 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
1c79356b A |
741 | if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
742 | struct sockaddr_in sin; | |
743 | ||
744 | if (inp->inp_faddr.s_addr != INADDR_ANY) | |
39236c6e | 745 | return (EISCONN); |
1c79356b | 746 | in6_sin6_2_sin(&sin, sin6_p); |
39236c6e A |
747 | error = in_pcbconnect(inp, (struct sockaddr *)&sin, |
748 | p, IFSCOPE_NONE, NULL); | |
1c79356b A |
749 | if (error == 0) { |
750 | inp->inp_vflag |= INP_IPV4; | |
751 | inp->inp_vflag &= ~INP_IPV6; | |
752 | soisconnected(so); | |
753 | } | |
39236c6e | 754 | return (error); |
1c79356b A |
755 | } |
756 | } | |
757 | ||
758 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) | |
39236c6e | 759 | return (EISCONN); |
1c79356b | 760 | error = in6_pcbconnect(inp, nam, p); |
1c79356b | 761 | if (error == 0) { |
39236c6e A |
762 | /* should be non mapped addr */ |
763 | if (ip6_mapped_addr_on || | |
764 | (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { | |
1c79356b A |
765 | inp->inp_vflag &= ~INP_IPV4; |
766 | inp->inp_vflag |= INP_IPV6; | |
767 | } | |
768 | soisconnected(so); | |
316670eb A |
769 | if (inp->inp_flowhash == 0) |
770 | inp->inp_flowhash = inp_calc_flowhash(inp); | |
39236c6e A |
771 | /* update flowinfo - RFC 6437 */ |
772 | if (inp->inp_flow == 0 && | |
773 | inp->in6p_flags & IN6P_AUTOFLOWLABEL) { | |
774 | inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; | |
775 | inp->inp_flow |= | |
776 | (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK); | |
777 | } | |
1c79356b | 778 | } |
39236c6e A |
779 | return (error); |
780 | } | |
781 | ||
782 | static int | |
783 | udp6_connectx(struct socket *so, struct sockaddr_list **src_sl, | |
784 | struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope, | |
785 | associd_t aid, connid_t *pcid, uint32_t flags, void *arg, | |
786 | uint32_t arglen) | |
787 | { | |
788 | return (udp_connectx_common(so, AF_INET6, src_sl, dst_sl, | |
789 | p, ifscope, aid, pcid, flags, arg, arglen)); | |
1c79356b A |
790 | } |
791 | ||
792 | static int | |
793 | udp6_detach(struct socket *so) | |
794 | { | |
795 | struct inpcb *inp; | |
1c79356b A |
796 | |
797 | inp = sotoinpcb(so); | |
39236c6e A |
798 | if (inp == NULL) |
799 | return (EINVAL); | |
1c79356b | 800 | in6_pcbdetach(inp); |
39236c6e | 801 | return (0); |
1c79356b A |
802 | } |
803 | ||
804 | static int | |
805 | udp6_disconnect(struct socket *so) | |
806 | { | |
807 | struct inpcb *inp; | |
1c79356b A |
808 | |
809 | inp = sotoinpcb(so); | |
39236c6e A |
810 | if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) |
811 | return (inp == NULL ? EINVAL : EPROTOTYPE); | |
1c79356b A |
812 | |
813 | if (inp->inp_vflag & INP_IPV4) { | |
814 | struct pr_usrreqs *pru; | |
815 | ||
816 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; | |
817 | return ((*pru->pru_disconnect)(so)); | |
818 | } | |
819 | ||
820 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) | |
39236c6e | 821 | return (ENOTCONN); |
1c79356b | 822 | |
1c79356b | 823 | in6_pcbdisconnect(inp); |
316670eb A |
824 | |
825 | /* reset flow-controlled state, just in case */ | |
826 | inp_reset_fc_state(inp); | |
827 | ||
1c79356b | 828 | inp->in6p_laddr = in6addr_any; |
316670eb | 829 | inp->in6p_last_outifp = NULL; |
1c79356b | 830 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
39236c6e A |
831 | return (0); |
832 | } | |
833 | ||
834 | static int | |
835 | udp6_disconnectx(struct socket *so, associd_t aid, connid_t cid) | |
836 | { | |
837 | #pragma unused(cid) | |
838 | if (aid != ASSOCID_ANY && aid != ASSOCID_ALL) | |
839 | return (EINVAL); | |
840 | ||
841 | return (udp6_disconnect(so)); | |
1c79356b A |
842 | } |
843 | ||
844 | static int | |
845 | udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, | |
39236c6e | 846 | struct mbuf *control, struct proc *p) |
1c79356b A |
847 | { |
848 | struct inpcb *inp; | |
9bccf70c | 849 | int error = 0; |
1c79356b A |
850 | |
851 | inp = sotoinpcb(so); | |
39236c6e A |
852 | if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) { |
853 | error = (inp == NULL ? EINVAL : EPROTOTYPE); | |
9bccf70c A |
854 | goto bad; |
855 | } | |
856 | ||
39236c6e A |
857 | if (addr != NULL) { |
858 | if (addr->sa_len != sizeof (struct sockaddr_in6)) { | |
9bccf70c A |
859 | error = EINVAL; |
860 | goto bad; | |
861 | } | |
862 | if (addr->sa_family != AF_INET6) { | |
863 | error = EAFNOSUPPORT; | |
864 | goto bad; | |
865 | } | |
1c79356b A |
866 | } |
867 | ||
55e303ae | 868 | if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b | 869 | int hasv4addr; |
39236c6e | 870 | struct sockaddr_in6 *sin6 = NULL; |
1c79356b | 871 | |
39236c6e | 872 | if (addr == NULL) { |
1c79356b | 873 | hasv4addr = (inp->inp_vflag & INP_IPV4); |
39236c6e | 874 | } else { |
316670eb | 875 | sin6 = (struct sockaddr_in6 *)(void *)addr; |
39236c6e A |
876 | hasv4addr = |
877 | IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 1 : 0; | |
1c79356b A |
878 | } |
879 | if (hasv4addr) { | |
880 | struct pr_usrreqs *pru; | |
1c79356b | 881 | |
39236c6e | 882 | if (sin6 != NULL) |
1c79356b A |
883 | in6_sin6_2_sin_in_sock(addr); |
884 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; | |
39236c6e A |
885 | error = ((*pru->pru_send)(so, flags, m, addr, |
886 | control, p)); | |
1c79356b | 887 | /* addr will just be freed in sendit(). */ |
39236c6e | 888 | return (error); |
1c79356b A |
889 | } |
890 | } | |
39236c6e A |
891 | return (udp6_output(inp, m, addr, control, p)); |
892 | ||
893 | bad: | |
894 | VERIFY(error != 0); | |
1c79356b | 895 | |
39236c6e A |
896 | if (m != NULL) |
897 | m_freem(m); | |
898 | if (control != NULL) | |
899 | m_freem(control); | |
9bccf70c | 900 | |
39236c6e | 901 | return (error); |
1c79356b A |
902 | } |
903 | ||
39236c6e A |
904 | /* |
905 | * Checksum extended UDP header and data. | |
906 | */ | |
907 | static int | |
908 | udp6_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen) | |
909 | { | |
910 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
911 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
912 | ||
913 | if (uh->uh_sum == 0) { | |
914 | /* UDP/IPv6 checksum is mandatory (RFC2460) */ | |
915 | udpstat.udps_nosum++; | |
916 | goto badsum; | |
917 | } | |
918 | ||
919 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || | |
920 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && | |
921 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { | |
922 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { | |
923 | uh->uh_sum = m->m_pkthdr.csum_rx_val; | |
924 | } else { | |
925 | uint16_t sum = m->m_pkthdr.csum_rx_val; | |
926 | uint16_t start = m->m_pkthdr.csum_rx_start; | |
927 | ||
928 | /* | |
929 | * Perform 1's complement adjustment of octets | |
930 | * that got included/excluded in the hardware- | |
931 | * calculated checksum value. | |
932 | */ | |
933 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && | |
934 | start != off) { | |
935 | uint16_t s, d; | |
936 | ||
937 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { | |
938 | s = ip6->ip6_src.s6_addr16[1]; | |
939 | ip6->ip6_src.s6_addr16[1] = 0 ; | |
940 | } | |
941 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
942 | d = ip6->ip6_dst.s6_addr16[1]; | |
943 | ip6->ip6_dst.s6_addr16[1] = 0; | |
944 | } | |
945 | ||
946 | /* callee folds in sum */ | |
947 | sum = m_adj_sum16(m, start, off, sum); | |
948 | ||
949 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) | |
950 | ip6->ip6_src.s6_addr16[1] = s; | |
951 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) | |
952 | ip6->ip6_dst.s6_addr16[1] = d; | |
953 | } | |
954 | ||
955 | uh->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst, | |
956 | sum + htonl(ulen + IPPROTO_UDP)); | |
957 | } | |
958 | uh->uh_sum ^= 0xffff; | |
959 | } else { | |
960 | udp_in6_cksum_stats(ulen); | |
961 | uh->uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen); | |
962 | } | |
963 | ||
964 | if (uh->uh_sum != 0) { | |
965 | badsum: | |
966 | udpstat.udps_badsum++; | |
967 | IF_UDP_STATINC(ifp, badchksum); | |
968 | return (-1); | |
969 | } | |
970 | ||
971 | return (0); | |
972 | } |