1 | /* |
2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
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 | |
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 $ */ |
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 | */ |
95 | #include <sys/kernel.h> |
96 | #include <sys/malloc.h> |
97 | #include <sys/mbuf.h> |
98 | #include <sys/param.h> |
99 | #include <sys/protosw.h> |
100 | #include <sys/socket.h> |
101 | #include <sys/socketvar.h> |
102 | #include <sys/sysctl.h> |
103 | #include <sys/errno.h> |
104 | #include <sys/stat.h> |
105 | #include <sys/systm.h> |
106 | #include <sys/syslog.h> |
107 | #include <sys/proc.h> |
108 | #include <sys/kauth.h> |
109 | |
110 | #include <net/if.h> |
111 | #include <net/route.h> |
112 | #include <net/if_types.h> |
113 | #include <net/ntstat.h> |
114 | #include <net/dlil.h> |
115 | #include <net/net_api_stats.h> |
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> |
134 | #include <netinet6/ipsec6.h> |
135 | #include <netinet6/esp6.h> |
136 | extern int ipsec_bypass; |
137 | extern int esp_udp_encap_port; |
138 | #endif /* IPSEC */ |
139 | |
140 | #if NECP |
141 | #include <net/necp.h> |
142 | #endif /* NECP */ |
143 | |
144 | #if FLOW_DIVERT |
145 | #include <netinet/flow_divert.h> |
146 | #endif /* FLOW_DIVERT */ |
147 | |
148 | #if CONTENT_FILTER |
149 | #include <net/content_filter.h> |
150 | #endif /* CONTENT_FILTER */ |
151 | |
152 | /* |
153 | * UDP protocol inplementation. |
154 | * Per RFC 768, August, 1980. |
155 | */ |
156 | |
157 | static int udp6_abort(struct socket *); |
158 | static int udp6_attach(struct socket *, int, struct proc *); |
159 | static int udp6_bind(struct socket *, struct sockaddr *, struct proc *); |
160 | static int udp6_connectx(struct socket *, struct sockaddr *, |
161 | struct sockaddr *, struct proc *, uint32_t, sae_associd_t, |
162 | sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *); |
163 | static int udp6_detach(struct socket *); |
164 | static int udp6_disconnect(struct socket *); |
165 | static int udp6_disconnectx(struct socket *, sae_associd_t, sae_connid_t); |
166 | static int udp6_send(struct socket *, int, struct mbuf *, struct sockaddr *, |
167 | struct mbuf *, struct proc *); |
168 | static void udp6_append(struct inpcb *, struct ip6_hdr *, |
169 | struct sockaddr_in6 *, struct mbuf *, int, struct ifnet *); |
170 | static int udp6_input_checksum(struct mbuf *, struct udphdr *, int, int); |
171 | |
172 | struct pr_usrreqs udp6_usrreqs = { |
173 | .pru_abort = udp6_abort, |
174 | .pru_attach = udp6_attach, |
175 | .pru_bind = udp6_bind, |
176 | .pru_connect = udp6_connect, |
177 | .pru_connectx = udp6_connectx, |
178 | .pru_control = in6_control, |
179 | .pru_detach = udp6_detach, |
180 | .pru_disconnect = udp6_disconnect, |
181 | .pru_disconnectx = udp6_disconnectx, |
182 | .pru_peeraddr = in6_mapped_peeraddr, |
183 | .pru_send = udp6_send, |
184 | .pru_shutdown = udp_shutdown, |
185 | .pru_sockaddr = in6_mapped_sockaddr, |
186 | .pru_sosend = sosend, |
187 | .pru_soreceive = soreceive, |
188 | .pru_soreceive_list = soreceive_list, |
189 | }; |
190 | |
191 | /* |
192 | * subroutine of udp6_input(), mainly for source code readability. |
193 | */ |
194 | static void |
195 | udp6_append(struct inpcb *last, struct ip6_hdr *ip6, |
196 | struct sockaddr_in6 *udp_in6, struct mbuf *n, int off, struct ifnet *ifp) |
197 | { |
198 | #pragma unused(ip6) |
199 | struct mbuf *opts = NULL; |
200 | int ret = 0; |
201 | boolean_t cell = IFNET_IS_CELLULAR(ifp); |
202 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); |
203 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
204 | |
205 | #if CONFIG_MACF_NET |
206 | if (mac_inpcb_check_deliver(last, n, AF_INET6, SOCK_DGRAM) != 0) { |
207 | m_freem(n); |
208 | return; |
209 | } |
210 | #endif /* CONFIG_MACF_NET */ |
211 | if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || |
212 | (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || |
213 | (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 || |
214 | (last->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) { |
215 | ret = ip6_savecontrol(last, n, &opts); |
216 | if (ret != 0) { |
217 | m_freem(n); |
218 | m_freem(opts); |
219 | return; |
220 | } |
221 | } |
222 | m_adj(n, off); |
223 | if (nstat_collect) { |
224 | INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1); |
225 | INP_ADD_STAT(last, cell, wifi, wired, rxbytes, n->m_pkthdr.len); |
226 | inp_set_activity_bitmap(last); |
227 | } |
228 | so_recv_data_stat(last->in6p_socket, n, 0); |
229 | if (sbappendaddr(&last->in6p_socket->so_rcv, |
230 | (struct sockaddr *)udp_in6, n, opts, NULL) == 0) |
231 | udpstat.udps_fullsock++; |
232 | else |
233 | sorwakeup(last->in6p_socket); |
234 | } |
235 | |
236 | int |
237 | udp6_input(struct mbuf **mp, int *offp, int proto) |
238 | { |
239 | #pragma unused(proto) |
240 | struct mbuf *m = *mp; |
241 | struct ifnet *ifp; |
242 | struct ip6_hdr *ip6; |
243 | struct udphdr *uh; |
244 | struct inpcb *in6p; |
245 | struct mbuf *opts = NULL; |
246 | int off = *offp; |
247 | int plen, ulen, ret = 0; |
248 | boolean_t cell, wifi, wired; |
249 | struct sockaddr_in6 udp_in6; |
250 | struct inpcbinfo *pcbinfo = &udbinfo; |
251 | struct sockaddr_in6 fromsa; |
252 | |
253 | IP6_EXTHDR_CHECK(m, off, sizeof (struct udphdr), return IPPROTO_DONE); |
254 | |
255 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
256 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); |
257 | |
258 | ifp = m->m_pkthdr.rcvif; |
259 | ip6 = mtod(m, struct ip6_hdr *); |
260 | cell = IFNET_IS_CELLULAR(ifp); |
261 | wifi = (!cell && IFNET_IS_WIFI(ifp)); |
262 | wired = (!wifi && IFNET_IS_WIRED(ifp)); |
263 | |
264 | udpstat.udps_ipackets++; |
265 | |
266 | plen = ntohs(ip6->ip6_plen) - off + sizeof (*ip6); |
267 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
268 | ulen = ntohs((u_short)uh->uh_ulen); |
269 | |
270 | if (plen != ulen) { |
271 | udpstat.udps_badlen++; |
272 | IF_UDP_STATINC(ifp, badlength); |
273 | goto bad; |
274 | } |
275 | |
276 | /* destination port of 0 is illegal, based on RFC768. */ |
277 | if (uh->uh_dport == 0) { |
278 | IF_UDP_STATINC(ifp, port0); |
279 | goto bad; |
280 | } |
281 | |
282 | /* |
283 | * Checksum extended UDP header and data. |
284 | */ |
285 | if (udp6_input_checksum(m, uh, off, ulen)) |
286 | goto bad; |
287 | |
288 | /* |
289 | * Construct sockaddr format source address. |
290 | */ |
291 | init_sin6(&fromsa, m); |
292 | fromsa.sin6_port = uh->uh_sport; |
293 | |
294 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
295 | int reuse_sock = 0, mcast_delivered = 0; |
296 | struct ip6_moptions *imo; |
297 | |
298 | /* |
299 | * Deliver a multicast datagram to all sockets |
300 | * for which the local and remote addresses and ports match |
301 | * those of the incoming datagram. This allows more than |
302 | * one process to receive multicasts on the same port. |
303 | * (This really ought to be done for unicast datagrams as |
304 | * well, but that would cause problems with existing |
305 | * applications that open both address-specific sockets and |
306 | * a wildcard socket listening to the same port -- they would |
307 | * end up receiving duplicates of every unicast datagram. |
308 | * Those applications open the multiple sockets to overcome an |
309 | * inadequacy of the UDP socket interface, but for backwards |
310 | * compatibility we avoid the problem here rather than |
311 | * fixing the interface. Maybe 4.5BSD will remedy this?) |
312 | */ |
313 | |
314 | /* |
315 | * In a case that laddr should be set to the link-local |
316 | * address (this happens in RIPng), the multicast address |
317 | * specified in the received packet does not match with |
318 | * laddr. To cure this situation, the matching is relaxed |
319 | * if the receiving interface is the same as one specified |
320 | * in the socket and if the destination multicast address |
321 | * matches one of the multicast groups specified in the socket. |
322 | */ |
323 | |
324 | /* |
325 | * Construct sockaddr format source address. |
326 | */ |
327 | init_sin6(&udp_in6, m); /* general init */ |
328 | udp_in6.sin6_port = uh->uh_sport; |
329 | /* |
330 | * KAME note: usually we drop udphdr from mbuf here. |
331 | * We need udphdr for IPsec processing so we do that later. |
332 | */ |
333 | |
334 | /* |
335 | * Locate pcb(s) for datagram. |
336 | * (Algorithm copied from raw_intr().) |
337 | */ |
338 | lck_rw_lock_shared(pcbinfo->ipi_lock); |
339 | |
340 | LIST_FOREACH(in6p, &udb, inp_list) { |
341 | #if IPSEC |
342 | int skipit; |
343 | #endif /* IPSEC */ |
344 | |
345 | if ((in6p->inp_vflag & INP_IPV6) == 0) |
346 | continue; |
347 | |
348 | if (inp_restricted_recv(in6p, ifp)) |
349 | continue; |
350 | |
351 | if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) == |
352 | WNT_STOPUSING) |
353 | continue; |
354 | |
355 | udp_lock(in6p->in6p_socket, 1, 0); |
356 | |
357 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == |
358 | WNT_STOPUSING) { |
359 | udp_unlock(in6p->in6p_socket, 1, 0); |
360 | continue; |
361 | } |
362 | if (in6p->in6p_lport != uh->uh_dport) { |
363 | udp_unlock(in6p->in6p_socket, 1, 0); |
364 | continue; |
365 | } |
366 | |
367 | /* |
368 | * Handle socket delivery policy for any-source |
369 | * and source-specific multicast. [RFC3678] |
370 | */ |
371 | imo = in6p->in6p_moptions; |
372 | if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
373 | struct sockaddr_in6 mcaddr; |
374 | int blocked; |
375 | |
376 | IM6O_LOCK(imo); |
377 | bzero(&mcaddr, sizeof (struct sockaddr_in6)); |
378 | mcaddr.sin6_len = sizeof (struct sockaddr_in6); |
379 | mcaddr.sin6_family = AF_INET6; |
380 | mcaddr.sin6_addr = ip6->ip6_dst; |
381 | |
382 | blocked = im6o_mc_filter(imo, ifp, |
383 | &mcaddr, &fromsa); |
384 | IM6O_UNLOCK(imo); |
385 | if (blocked != MCAST_PASS) { |
386 | udp_unlock(in6p->in6p_socket, 1, 0); |
387 | if (blocked == MCAST_NOTSMEMBER || |
388 | blocked == MCAST_MUTED) |
389 | udpstat.udps_filtermcast++; |
390 | continue; |
391 | } |
392 | } |
393 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && |
394 | (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, |
395 | &ip6->ip6_src) || |
396 | in6p->in6p_fport != uh->uh_sport)) { |
397 | udp_unlock(in6p->in6p_socket, 1, 0); |
398 | continue; |
399 | } |
400 | |
401 | reuse_sock = in6p->inp_socket->so_options & |
402 | (SO_REUSEPORT | SO_REUSEADDR); |
403 | |
404 | #if NECP |
405 | skipit = 0; |
406 | if (!necp_socket_is_allowed_to_send_recv_v6(in6p, |
407 | uh->uh_dport, uh->uh_sport, &ip6->ip6_dst, |
408 | &ip6->ip6_src, ifp, NULL, NULL, NULL)) { |
409 | /* do not inject data to pcb */ |
410 | skipit = 1; |
411 | } |
412 | if (skipit == 0) |
413 | #endif /* NECP */ |
414 | { |
415 | struct mbuf *n = NULL; |
416 | /* |
417 | * KAME NOTE: do not |
418 | * m_copy(m, offset, ...) below. |
419 | * sbappendaddr() expects M_PKTHDR, |
420 | * and m_copy() will copy M_PKTHDR |
421 | * only if offset is 0. |
422 | */ |
423 | if (reuse_sock) |
424 | n = m_copy(m, 0, M_COPYALL); |
425 | udp6_append(in6p, ip6, &udp_in6, m, |
426 | off + sizeof (struct udphdr), ifp); |
427 | mcast_delivered++; |
428 | m = n; |
429 | } |
430 | udp_unlock(in6p->in6p_socket, 1, 0); |
431 | |
432 | /* |
433 | * Don't look for additional matches if this one does |
434 | * not have either the SO_REUSEPORT or SO_REUSEADDR |
435 | * socket options set. This heuristic avoids searching |
436 | * through all pcbs in the common case of a non-shared |
437 | * port. It assumes that an application will never |
438 | * clear these options after setting them. |
439 | */ |
440 | if (reuse_sock == 0 || m == NULL) |
441 | break; |
442 | |
443 | /* |
444 | * Expect 32-bit aligned data pointer on strict-align |
445 | * platforms. |
446 | */ |
447 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); |
448 | |
449 | /* |
450 | * Recompute IP and UDP header pointers for new mbuf |
451 | */ |
452 | ip6 = mtod(m, struct ip6_hdr *); |
453 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
454 | } |
455 | lck_rw_done(pcbinfo->ipi_lock); |
456 | |
457 | if (mcast_delivered == 0) { |
458 | /* |
459 | * No matching pcb found; discard datagram. |
460 | * (No need to send an ICMP Port Unreachable |
461 | * for a broadcast or multicast datgram.) |
462 | */ |
463 | udpstat.udps_noport++; |
464 | udpstat.udps_noportmcast++; |
465 | IF_UDP_STATINC(ifp, port_unreach); |
466 | goto bad; |
467 | } |
468 | |
469 | /* free the extra copy of mbuf or skipped by NECP */ |
470 | if (m != NULL) |
471 | m_freem(m); |
472 | return (IPPROTO_DONE); |
473 | } |
474 | |
475 | #if IPSEC |
476 | /* |
477 | * UDP to port 4500 with a payload where the first four bytes are |
478 | * not zero is a UDP encapsulated IPSec packet. Packets where |
479 | * the payload is one byte and that byte is 0xFF are NAT keepalive |
480 | * packets. Decapsulate the ESP packet and carry on with IPSec input |
481 | * or discard the NAT keep-alive. |
482 | */ |
483 | if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 && |
484 | uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) { |
485 | int payload_len = ulen - sizeof (struct udphdr) > 4 ? 4 : |
486 | ulen - sizeof (struct udphdr); |
487 | |
488 | if (m->m_len < off + sizeof (struct udphdr) + payload_len) { |
489 | if ((m = m_pullup(m, off + sizeof (struct udphdr) + |
490 | payload_len)) == NULL) { |
491 | udpstat.udps_hdrops++; |
492 | goto bad; |
493 | } |
494 | /* |
495 | * Expect 32-bit aligned data pointer on strict-align |
496 | * platforms. |
497 | */ |
498 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); |
499 | |
500 | ip6 = mtod(m, struct ip6_hdr *); |
501 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
502 | } |
503 | /* Check for NAT keepalive packet */ |
504 | if (payload_len == 1 && *(u_int8_t*) |
505 | ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) { |
506 | goto bad; |
507 | } else if (payload_len == 4 && *(u_int32_t*)(void *) |
508 | ((caddr_t)uh + sizeof (struct udphdr)) != 0) { |
509 | /* UDP encapsulated IPSec packet to pass through NAT */ |
510 | /* preserve the udp header */ |
511 | *offp = off + sizeof (struct udphdr); |
512 | return (esp6_input(mp, offp, IPPROTO_UDP)); |
513 | } |
514 | } |
515 | #endif /* IPSEC */ |
516 | |
517 | /* |
518 | * Locate pcb for datagram. |
519 | */ |
520 | in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, |
521 | &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); |
522 | if (in6p == NULL) { |
523 | IF_UDP_STATINC(ifp, port_unreach); |
524 | |
525 | if (udp_log_in_vain) { |
526 | char buf[INET6_ADDRSTRLEN]; |
527 | |
528 | strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof (buf)); |
529 | if (udp_log_in_vain < 3) { |
530 | log(LOG_INFO, "Connection attempt to UDP " |
531 | "%s:%d from %s:%d\n" , buf, |
532 | ntohs(uh->uh_dport), |
533 | ip6_sprintf(&ip6->ip6_src), |
534 | ntohs(uh->uh_sport)); |
535 | } else if (!(m->m_flags & (M_BCAST | M_MCAST)) && |
536 | !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { |
537 | log(LOG_INFO, "Connection attempt " |
538 | "to UDP %s:%d from %s:%d\n" , buf, |
539 | ntohs(uh->uh_dport), |
540 | ip6_sprintf(&ip6->ip6_src), |
541 | ntohs(uh->uh_sport)); |
542 | } |
543 | } |
544 | udpstat.udps_noport++; |
545 | if (m->m_flags & M_MCAST) { |
546 | printf("UDP6: M_MCAST is set in a unicast packet.\n" ); |
547 | udpstat.udps_noportmcast++; |
548 | IF_UDP_STATINC(ifp, badmcast); |
549 | goto bad; |
550 | } |
551 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); |
552 | return (IPPROTO_DONE); |
553 | } |
554 | #if NECP |
555 | if (!necp_socket_is_allowed_to_send_recv_v6(in6p, uh->uh_dport, |
556 | uh->uh_sport, &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL, NULL)) { |
557 | in_pcb_checkstate(in6p, WNT_RELEASE, 0); |
558 | IF_UDP_STATINC(ifp, badipsec); |
559 | goto bad; |
560 | } |
561 | #endif /* NECP */ |
562 | |
563 | /* |
564 | * Construct sockaddr format source address. |
565 | * Stuff source address and datagram in user buffer. |
566 | */ |
567 | udp_lock(in6p->in6p_socket, 1, 0); |
568 | |
569 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) { |
570 | udp_unlock(in6p->in6p_socket, 1, 0); |
571 | IF_UDP_STATINC(ifp, cleanup); |
572 | goto bad; |
573 | } |
574 | |
575 | init_sin6(&udp_in6, m); /* general init */ |
576 | udp_in6.sin6_port = uh->uh_sport; |
577 | if ((in6p->in6p_flags & INP_CONTROLOPTS) != 0 || |
578 | (in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0 || |
579 | (in6p->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 || |
580 | (in6p->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) { |
581 | ret = ip6_savecontrol(in6p, m, &opts); |
582 | if (ret != 0) { |
583 | udp_unlock(in6p->in6p_socket, 1, 0); |
584 | goto bad; |
585 | } |
586 | } |
587 | m_adj(m, off + sizeof (struct udphdr)); |
588 | if (nstat_collect) { |
589 | INP_ADD_STAT(in6p, cell, wifi, wired, rxpackets, 1); |
590 | INP_ADD_STAT(in6p, cell, wifi, wired, rxbytes, m->m_pkthdr.len); |
591 | inp_set_activity_bitmap(in6p); |
592 | } |
593 | so_recv_data_stat(in6p->in6p_socket, m, 0); |
594 | if (sbappendaddr(&in6p->in6p_socket->so_rcv, |
595 | (struct sockaddr *)&udp_in6, m, opts, NULL) == 0) { |
596 | m = NULL; |
597 | opts = NULL; |
598 | udpstat.udps_fullsock++; |
599 | udp_unlock(in6p->in6p_socket, 1, 0); |
600 | goto bad; |
601 | } |
602 | sorwakeup(in6p->in6p_socket); |
603 | udp_unlock(in6p->in6p_socket, 1, 0); |
604 | return (IPPROTO_DONE); |
605 | bad: |
606 | if (m != NULL) |
607 | m_freem(m); |
608 | if (opts != NULL) |
609 | m_freem(opts); |
610 | return (IPPROTO_DONE); |
611 | } |
612 | |
613 | void |
614 | udp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp) |
615 | { |
616 | struct udphdr uh; |
617 | struct ip6_hdr *ip6; |
618 | struct mbuf *m; |
619 | int off = 0; |
620 | struct ip6ctlparam *ip6cp = NULL; |
621 | struct icmp6_hdr *icmp6 = NULL; |
622 | const struct sockaddr_in6 *sa6_src = NULL; |
623 | void (*notify)(struct inpcb *, int) = udp_notify; |
624 | struct udp_portonly { |
625 | u_int16_t uh_sport; |
626 | u_int16_t uh_dport; |
627 | } *uhp; |
628 | |
629 | if (sa->sa_family != AF_INET6 || |
630 | sa->sa_len != sizeof (struct sockaddr_in6)) |
631 | return; |
632 | |
633 | if ((unsigned)cmd >= PRC_NCMDS) |
634 | return; |
635 | if (PRC_IS_REDIRECT(cmd)) { |
636 | notify = in6_rtchange; |
637 | d = NULL; |
638 | } else if (cmd == PRC_HOSTDEAD) |
639 | d = NULL; |
640 | else if (inet6ctlerrmap[cmd] == 0) |
641 | return; |
642 | |
643 | /* if the parameter is from icmp6, decode it. */ |
644 | if (d != NULL) { |
645 | ip6cp = (struct ip6ctlparam *)d; |
646 | icmp6 = ip6cp->ip6c_icmp6; |
647 | m = ip6cp->ip6c_m; |
648 | ip6 = ip6cp->ip6c_ip6; |
649 | off = ip6cp->ip6c_off; |
650 | sa6_src = ip6cp->ip6c_src; |
651 | } else { |
652 | m = NULL; |
653 | ip6 = NULL; |
654 | sa6_src = &sa6_any; |
655 | } |
656 | |
657 | if (ip6 != NULL) { |
658 | /* |
659 | * XXX: We assume that when IPV6 is non NULL, |
660 | * M and OFF are valid. |
661 | */ |
662 | /* check if we can safely examine src and dst ports */ |
663 | if (m->m_pkthdr.len < off + sizeof (*uhp)) |
664 | return; |
665 | |
666 | bzero(&uh, sizeof (uh)); |
667 | m_copydata(m, off, sizeof (*uhp), (caddr_t)&uh); |
668 | |
669 | (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport, |
670 | (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport, |
671 | cmd, NULL, notify); |
672 | } else { |
673 | (void) in6_pcbnotify(&udbinfo, sa, 0, |
674 | (struct sockaddr *)&sa6_src, 0, cmd, NULL, notify); |
675 | } |
676 | } |
677 | |
678 | static int |
679 | udp6_abort(struct socket *so) |
680 | { |
681 | struct inpcb *inp; |
682 | |
683 | inp = sotoinpcb(so); |
684 | if (inp == NULL) { |
685 | panic("%s: so=%p null inp\n" , __func__, so); |
686 | /* NOTREACHED */ |
687 | } |
688 | soisdisconnected(so); |
689 | in6_pcbdetach(inp); |
690 | return (0); |
691 | } |
692 | |
693 | static int |
694 | udp6_attach(struct socket *so, int proto, struct proc *p) |
695 | { |
696 | #pragma unused(proto) |
697 | struct inpcb *inp; |
698 | int error; |
699 | |
700 | inp = sotoinpcb(so); |
701 | if (inp != NULL) |
702 | return (EINVAL); |
703 | |
704 | error = in_pcballoc(so, &udbinfo, p); |
705 | if (error) |
706 | return (error); |
707 | |
708 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { |
709 | error = soreserve(so, udp_sendspace, udp_recvspace); |
710 | if (error) |
711 | return (error); |
712 | } |
713 | inp = (struct inpcb *)so->so_pcb; |
714 | inp->inp_vflag |= INP_IPV6; |
715 | if (ip6_mapped_addr_on) |
716 | inp->inp_vflag |= INP_IPV4; |
717 | inp->in6p_hops = -1; /* use kernel default */ |
718 | inp->in6p_cksum = -1; /* just to be sure */ |
719 | /* |
720 | * XXX: ugly!! |
721 | * IPv4 TTL initialization is necessary for an IPv6 socket as well, |
722 | * because the socket may be bound to an IPv6 wildcard address, |
723 | * which may match an IPv4-mapped IPv6 address. |
724 | */ |
725 | inp->inp_ip_ttl = ip_defttl; |
726 | if (nstat_collect) |
727 | nstat_udp_new_pcb(inp); |
728 | return (0); |
729 | } |
730 | |
731 | static int |
732 | udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p) |
733 | { |
734 | struct inpcb *inp; |
735 | int error; |
736 | |
737 | inp = sotoinpcb(so); |
738 | if (inp == NULL) |
739 | return (EINVAL); |
740 | |
741 | inp->inp_vflag &= ~INP_IPV4; |
742 | inp->inp_vflag |= INP_IPV6; |
743 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
744 | struct sockaddr_in6 *sin6_p; |
745 | |
746 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
747 | |
748 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { |
749 | inp->inp_vflag |= INP_IPV4; |
750 | } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
751 | struct sockaddr_in sin; |
752 | |
753 | in6_sin6_2_sin(&sin, sin6_p); |
754 | inp->inp_vflag |= INP_IPV4; |
755 | inp->inp_vflag &= ~INP_IPV6; |
756 | error = in_pcbbind(inp, (struct sockaddr *)&sin, p); |
757 | return (error); |
758 | } |
759 | } |
760 | |
761 | error = in6_pcbbind(inp, nam, p); |
762 | return (error); |
763 | } |
764 | |
765 | int |
766 | udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p) |
767 | { |
768 | struct inpcb *inp; |
769 | int error; |
770 | #if defined(NECP) && defined(FLOW_DIVERT) |
771 | int should_use_flow_divert = 0; |
772 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
773 | |
774 | inp = sotoinpcb(so); |
775 | if (inp == NULL) |
776 | return (EINVAL); |
777 | |
778 | #if defined(NECP) && defined(FLOW_DIVERT) |
779 | should_use_flow_divert = necp_socket_should_use_flow_divert(inp); |
780 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
781 | |
782 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
783 | struct sockaddr_in6 *sin6_p; |
784 | |
785 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
786 | if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
787 | struct sockaddr_in sin; |
788 | |
789 | if (inp->inp_faddr.s_addr != INADDR_ANY) |
790 | return (EISCONN); |
791 | |
792 | if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) { |
793 | so->so_flags1 |= SOF1_CONNECT_COUNTED; |
794 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected); |
795 | } |
796 | |
797 | in6_sin6_2_sin(&sin, sin6_p); |
798 | #if defined(NECP) && defined(FLOW_DIVERT) |
799 | if (should_use_flow_divert) { |
800 | goto do_flow_divert; |
801 | } |
802 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
803 | error = in_pcbconnect(inp, (struct sockaddr *)&sin, |
804 | p, IFSCOPE_NONE, NULL); |
805 | if (error == 0) { |
806 | #if NECP |
807 | /* Update NECP client with connected five-tuple */ |
808 | if (!uuid_is_null(inp->necp_client_uuid)) { |
809 | socket_unlock(so, 0); |
810 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); |
811 | socket_lock(so, 0); |
812 | } |
813 | #endif /* NECP */ |
814 | inp->inp_vflag |= INP_IPV4; |
815 | inp->inp_vflag &= ~INP_IPV6; |
816 | soisconnected(so); |
817 | } |
818 | return (error); |
819 | } |
820 | } |
821 | |
822 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) |
823 | return (EISCONN); |
824 | |
825 | if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) { |
826 | so->so_flags1 |= SOF1_CONNECT_COUNTED; |
827 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected); |
828 | } |
829 | |
830 | #if defined(NECP) && defined(FLOW_DIVERT) |
831 | do_flow_divert: |
832 | if (should_use_flow_divert) { |
833 | uint32_t fd_ctl_unit = necp_socket_get_flow_divert_control_unit(inp); |
834 | if (fd_ctl_unit > 0) { |
835 | error = flow_divert_pcb_init(so, fd_ctl_unit); |
836 | if (error == 0) { |
837 | error = flow_divert_connect_out(so, nam, p); |
838 | } |
839 | } else { |
840 | error = ENETDOWN; |
841 | } |
842 | return (error); |
843 | } |
844 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
845 | |
846 | error = in6_pcbconnect(inp, nam, p); |
847 | if (error == 0) { |
848 | /* should be non mapped addr */ |
849 | if (ip6_mapped_addr_on || |
850 | (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
851 | inp->inp_vflag &= ~INP_IPV4; |
852 | inp->inp_vflag |= INP_IPV6; |
853 | } |
854 | #if NECP |
855 | /* Update NECP client with connected five-tuple */ |
856 | if (!uuid_is_null(inp->necp_client_uuid)) { |
857 | socket_unlock(so, 0); |
858 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); |
859 | socket_lock(so, 0); |
860 | } |
861 | #endif /* NECP */ |
862 | soisconnected(so); |
863 | if (inp->inp_flowhash == 0) |
864 | inp->inp_flowhash = inp_calc_flowhash(inp); |
865 | /* update flowinfo - RFC 6437 */ |
866 | if (inp->inp_flow == 0 && |
867 | inp->in6p_flags & IN6P_AUTOFLOWLABEL) { |
868 | inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; |
869 | inp->inp_flow |= |
870 | (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK); |
871 | } |
872 | } |
873 | return (error); |
874 | } |
875 | |
876 | static int |
877 | udp6_connectx(struct socket *so, struct sockaddr *src, |
878 | struct sockaddr *dst, struct proc *p, uint32_t ifscope, |
879 | sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, |
880 | uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written) |
881 | { |
882 | return (udp_connectx_common(so, AF_INET6, src, dst, |
883 | p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)); |
884 | } |
885 | |
886 | static int |
887 | udp6_detach(struct socket *so) |
888 | { |
889 | struct inpcb *inp; |
890 | |
891 | inp = sotoinpcb(so); |
892 | if (inp == NULL) |
893 | return (EINVAL); |
894 | in6_pcbdetach(inp); |
895 | return (0); |
896 | } |
897 | |
898 | static int |
899 | udp6_disconnect(struct socket *so) |
900 | { |
901 | struct inpcb *inp; |
902 | |
903 | inp = sotoinpcb(so); |
904 | if (inp == NULL |
905 | #if NECP |
906 | || (necp_socket_should_use_flow_divert(inp)) |
907 | #endif /* NECP */ |
908 | ) |
909 | return (inp == NULL ? EINVAL : EPROTOTYPE); |
910 | |
911 | if (inp->inp_vflag & INP_IPV4) { |
912 | struct pr_usrreqs *pru; |
913 | |
914 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; |
915 | return ((*pru->pru_disconnect)(so)); |
916 | } |
917 | |
918 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) |
919 | return (ENOTCONN); |
920 | |
921 | in6_pcbdisconnect(inp); |
922 | |
923 | /* reset flow-controlled state, just in case */ |
924 | inp_reset_fc_state(inp); |
925 | |
926 | inp->in6p_laddr = in6addr_any; |
927 | inp->in6p_last_outifp = NULL; |
928 | |
929 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
930 | return (0); |
931 | } |
932 | |
933 | static int |
934 | udp6_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid) |
935 | { |
936 | #pragma unused(cid) |
937 | if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) |
938 | return (EINVAL); |
939 | |
940 | return (udp6_disconnect(so)); |
941 | } |
942 | |
943 | static int |
944 | udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, |
945 | struct mbuf *control, struct proc *p) |
946 | { |
947 | struct inpcb *inp; |
948 | int error = 0; |
949 | #if defined(NECP) && defined(FLOW_DIVERT) |
950 | int should_use_flow_divert = 0; |
951 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
952 | #if CONTENT_FILTER |
953 | struct m_tag *cfil_tag = NULL; |
954 | struct sockaddr *cfil_faddr = NULL; |
955 | #endif |
956 | |
957 | inp = sotoinpcb(so); |
958 | if (inp == NULL) { |
959 | error = EINVAL; |
960 | goto bad; |
961 | } |
962 | |
963 | #if CONTENT_FILTER |
964 | //If socket is subject to UDP Content Filter and unconnected, get addr from tag. |
965 | if (so->so_cfil_db && !addr && IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { |
966 | cfil_tag = cfil_udp_get_socket_state(m, NULL, NULL, &cfil_faddr); |
967 | if (cfil_tag) { |
968 | addr = (struct sockaddr *)cfil_faddr; |
969 | } |
970 | } |
971 | #endif |
972 | |
973 | #if defined(NECP) && defined(FLOW_DIVERT) |
974 | should_use_flow_divert = necp_socket_should_use_flow_divert(inp); |
975 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
976 | |
977 | if (addr != NULL) { |
978 | if (addr->sa_len != sizeof (struct sockaddr_in6)) { |
979 | error = EINVAL; |
980 | goto bad; |
981 | } |
982 | if (addr->sa_family != AF_INET6) { |
983 | error = EAFNOSUPPORT; |
984 | goto bad; |
985 | } |
986 | } |
987 | |
988 | if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
989 | int hasv4addr; |
990 | struct sockaddr_in6 *sin6 = NULL; |
991 | |
992 | if (addr == NULL) { |
993 | hasv4addr = (inp->inp_vflag & INP_IPV4); |
994 | } else { |
995 | sin6 = (struct sockaddr_in6 *)(void *)addr; |
996 | hasv4addr = |
997 | IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 1 : 0; |
998 | } |
999 | if (hasv4addr) { |
1000 | struct pr_usrreqs *pru; |
1001 | |
1002 | if (sin6 != NULL) |
1003 | in6_sin6_2_sin_in_sock(addr); |
1004 | #if defined(NECP) && defined(FLOW_DIVERT) |
1005 | if (should_use_flow_divert) { |
1006 | goto do_flow_divert; |
1007 | } |
1008 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
1009 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; |
1010 | error = ((*pru->pru_send)(so, flags, m, addr, |
1011 | control, p)); |
1012 | #if CONTENT_FILTER |
1013 | if (cfil_tag) |
1014 | m_tag_free(cfil_tag); |
1015 | #endif |
1016 | /* addr will just be freed in sendit(). */ |
1017 | return (error); |
1018 | } |
1019 | } |
1020 | |
1021 | #if defined(NECP) && defined(FLOW_DIVERT) |
1022 | do_flow_divert: |
1023 | if (should_use_flow_divert) { |
1024 | /* Implicit connect */ |
1025 | error = flow_divert_implicit_data_out(so, flags, m, addr, control, p); |
1026 | #if CONTENT_FILTER |
1027 | if (cfil_tag) |
1028 | m_tag_free(cfil_tag); |
1029 | #endif |
1030 | return error; |
1031 | } |
1032 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ |
1033 | |
1034 | error = udp6_output(inp, m, addr, control, p); |
1035 | #if CONTENT_FILTER |
1036 | if (cfil_tag) |
1037 | m_tag_free(cfil_tag); |
1038 | #endif |
1039 | return error; |
1040 | |
1041 | bad: |
1042 | VERIFY(error != 0); |
1043 | |
1044 | if (m != NULL) |
1045 | m_freem(m); |
1046 | if (control != NULL) |
1047 | m_freem(control); |
1048 | #if CONTENT_FILTER |
1049 | if (cfil_tag) |
1050 | m_tag_free(cfil_tag); |
1051 | #endif |
1052 | return (error); |
1053 | } |
1054 | |
1055 | /* |
1056 | * Checksum extended UDP header and data. |
1057 | */ |
1058 | static int |
1059 | udp6_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen) |
1060 | { |
1061 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
1062 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); |
1063 | |
1064 | if (!(m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && |
1065 | uh->uh_sum == 0) { |
1066 | /* UDP/IPv6 checksum is mandatory (RFC2460) */ |
1067 | |
1068 | /* |
1069 | * If checksum was already validated, ignore this check. |
1070 | * This is necessary for transport-mode ESP, which may be |
1071 | * getting UDP payloads without checksums when the network |
1072 | * has a NAT64. |
1073 | */ |
1074 | udpstat.udps_nosum++; |
1075 | goto badsum; |
1076 | } |
1077 | |
1078 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || |
1079 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && |
1080 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { |
1081 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { |
1082 | uh->uh_sum = m->m_pkthdr.csum_rx_val; |
1083 | } else { |
1084 | uint32_t sum = m->m_pkthdr.csum_rx_val; |
1085 | uint32_t start = m->m_pkthdr.csum_rx_start; |
1086 | int32_t trailer = (m_pktlen(m) - (off + ulen)); |
1087 | |
1088 | /* |
1089 | * Perform 1's complement adjustment of octets |
1090 | * that got included/excluded in the hardware- |
1091 | * calculated checksum value. Also take care |
1092 | * of any trailing bytes and subtract out |
1093 | * their partial sum. |
1094 | */ |
1095 | ASSERT(trailer >= 0); |
1096 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && |
1097 | (start != off || trailer != 0)) { |
1098 | uint32_t swbytes = (uint32_t)trailer; |
1099 | uint16_t s = 0, d = 0; |
1100 | |
1101 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { |
1102 | s = ip6->ip6_src.s6_addr16[1]; |
1103 | ip6->ip6_src.s6_addr16[1] = 0 ; |
1104 | } |
1105 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { |
1106 | d = ip6->ip6_dst.s6_addr16[1]; |
1107 | ip6->ip6_dst.s6_addr16[1] = 0; |
1108 | } |
1109 | |
1110 | /* callee folds in sum */ |
1111 | sum = m_adj_sum16(m, start, off, ulen, sum); |
1112 | if (off > start) |
1113 | swbytes += (off - start); |
1114 | else |
1115 | swbytes += (start - off); |
1116 | |
1117 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) |
1118 | ip6->ip6_src.s6_addr16[1] = s; |
1119 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) |
1120 | ip6->ip6_dst.s6_addr16[1] = d; |
1121 | |
1122 | if (swbytes != 0) |
1123 | udp_in_cksum_stats(swbytes); |
1124 | if (trailer != 0) |
1125 | m_adj(m, -trailer); |
1126 | } |
1127 | |
1128 | uh->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst, |
1129 | sum + htonl(ulen + IPPROTO_UDP)); |
1130 | } |
1131 | uh->uh_sum ^= 0xffff; |
1132 | } else { |
1133 | udp_in6_cksum_stats(ulen); |
1134 | uh->uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen); |
1135 | } |
1136 | |
1137 | if (uh->uh_sum != 0) { |
1138 | badsum: |
1139 | udpstat.udps_badsum++; |
1140 | IF_UDP_STATINC(ifp, badchksum); |
1141 | return (-1); |
1142 | } |
1143 | |
1144 | return (0); |
1145 | } |
1146 | |