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