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