]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/udp_usrreq.c
xnu-2422.115.4.tar.gz
[apple/xnu.git] / bsd / netinet / udp_usrreq.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
9bccf70c 61 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
1c79356b
A
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67#include <sys/malloc.h>
68#include <sys/mbuf.h>
1c79356b 69#include <sys/domain.h>
1c79356b
A
70#include <sys/protosw.h>
71#include <sys/socket.h>
72#include <sys/socketvar.h>
73#include <sys/sysctl.h>
74#include <sys/syslog.h>
6d2010ae
A
75#include <sys/mcache.h>
76#include <net/ntstat.h>
1c79356b 77
2d21ac55 78#include <kern/zalloc.h>
39236c6e 79#include <mach/boolean.h>
2d21ac55 80
1c79356b 81#include <net/if.h>
91447636 82#include <net/if_types.h>
1c79356b 83#include <net/route.h>
39236c6e 84#include <net/dlil.h>
1c79356b
A
85
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
9bccf70c
A
89#if INET6
90#include <netinet/ip6.h>
39236c6e 91#endif /* INET6 */
1c79356b
A
92#include <netinet/in_pcb.h>
93#include <netinet/in_var.h>
94#include <netinet/ip_var.h>
1c79356b 95#if INET6
b0d623f7 96#include <netinet6/in6_pcb.h>
1c79356b 97#include <netinet6/ip6_var.h>
39236c6e
A
98#include <netinet6/udp6_var.h>
99#endif /* INET6 */
9bccf70c
A
100#include <netinet/ip_icmp.h>
101#include <netinet/icmp_var.h>
1c79356b
A
102#include <netinet/udp.h>
103#include <netinet/udp_var.h>
104#include <sys/kdebug.h>
105
106#if IPSEC
107#include <netinet6/ipsec.h>
2d21ac55 108#include <netinet6/esp.h>
9bccf70c 109extern int ipsec_bypass;
39236c6e
A
110extern int esp_udp_encap_port;
111#endif /* IPSEC */
1c79356b 112
39236c6e
A
113#define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
114#define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
115#define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
116#define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
117#define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
118#define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
1c79356b 119
1c79356b
A
120/*
121 * UDP protocol implementation.
122 * Per RFC 768, August, 1980.
123 */
124#ifndef COMPAT_42
39236c6e 125static int udpcksum = 1;
1c79356b 126#else
39236c6e 127static int udpcksum = 0; /* XXX */
1c79356b 128#endif
39236c6e
A
129SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum,
130 CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, "");
131
132int udp_log_in_vain = 0;
6d2010ae 133SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED,
39236c6e 134 &udp_log_in_vain, 0, "Log all incoming UDP packets");
9bccf70c 135
39236c6e 136static int blackhole = 0;
6d2010ae 137SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED,
39236c6e 138 &blackhole, 0, "Do not send port unreachables for refused connects");
1c79356b 139
39236c6e 140struct inpcbhead udb; /* from udp_var.h */
1c79356b 141#define udb6 udb /* for KAME src sync over BSD*'s */
39236c6e 142struct inpcbinfo udbinfo;
1c79356b
A
143
144#ifndef UDBHASHSIZE
145#define UDBHASHSIZE 16
146#endif
147
39236c6e
A
148/* Garbage collection performed during most recent udp_gc() run */
149static boolean_t udp_gc_done = FALSE;
91447636 150
2d21ac55 151#if IPFIREWALL
39236c6e
A
152extern int fw_verbose;
153extern void ipfwsyslog( int level, const char *format,...);
154extern void ipfw_stealth_stats_incr_udp(void);
155
156/* Apple logging, log to ipfw.log */
157#define log_in_vain_log(a) { \
158 if ((udp_log_in_vain == 3) && (fw_verbose == 2)) { \
159 ipfwsyslog a; \
160 } else if ((udp_log_in_vain == 4) && (fw_verbose == 2)) { \
161 ipfw_stealth_stats_incr_udp(); \
162 } else { \
163 log a; \
164 } \
91447636 165}
39236c6e 166#else /* !IPFIREWALL */
2d21ac55 167#define log_in_vain_log( a ) { log a; }
39236c6e 168#endif /* !IPFIREWALL */
91447636 169
39236c6e 170static int udp_getstat SYSCTL_HANDLER_ARGS;
1c79356b 171struct udpstat udpstat; /* from udp_var.h */
39236c6e
A
172SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED,
173 0, 0, udp_getstat, "S,udpstat",
174 "UDP statistics (struct udpstat, netinet/udp_var.h)");
175
176SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount,
177 CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0,
178 "Number of active PCBs");
1c79356b 179
b0d623f7 180__private_extern__ int udp_use_randomport = 1;
39236c6e
A
181SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports,
182 CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0,
183 "Randomize UDP port numbers");
b0d623f7 184
1c79356b
A
185#if INET6
186struct udp_in6 {
187 struct sockaddr_in6 uin6_sin;
188 u_char uin6_init_done : 1;
1c79356b
A
189};
190struct udp_ip6 {
191 struct ip6_hdr uip6_ip6;
192 u_char uip6_init_done : 1;
2d21ac55 193};
39236c6e
A
194
195static int udp_abort(struct socket *);
196static int udp_attach(struct socket *, int, struct proc *);
197static int udp_bind(struct socket *, struct sockaddr *, struct proc *);
198static int udp_connect(struct socket *, struct sockaddr *, struct proc *);
199static int udp_connectx(struct socket *, struct sockaddr_list **,
200 struct sockaddr_list **, struct proc *, uint32_t, associd_t, connid_t *,
201 uint32_t, void *, uint32_t);
202static int udp_detach(struct socket *);
203static int udp_disconnect(struct socket *);
204static int udp_disconnectx(struct socket *, associd_t, connid_t);
205static int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *,
206 struct mbuf *, struct proc *);
207static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
208 struct sockaddr_in *, struct udp_in6 *, struct udp_ip6 *, struct ifnet *);
209#else /* !INET6 */
210static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
211 struct sockaddr_in *, struct ifnet *);
212#endif /* !INET6 */
213static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int);
214static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
215 struct mbuf *, struct proc *);
91447636 216static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
39236c6e 217static void udp_gc(struct inpcbinfo *);
1c79356b 218
39236c6e
A
219struct pr_usrreqs udp_usrreqs = {
220 .pru_abort = udp_abort,
221 .pru_attach = udp_attach,
222 .pru_bind = udp_bind,
223 .pru_connect = udp_connect,
224 .pru_connectx = udp_connectx,
225 .pru_control = in_control,
226 .pru_detach = udp_detach,
227 .pru_disconnect = udp_disconnect,
228 .pru_disconnectx = udp_disconnectx,
229 .pru_peeraddr = in_getpeeraddr,
230 .pru_send = udp_send,
231 .pru_shutdown = udp_shutdown,
232 .pru_sockaddr = in_getsockaddr,
233 .pru_sosend = sosend,
234 .pru_soreceive = soreceive,
235};
1c79356b
A
236
237void
39236c6e 238udp_init(struct protosw *pp, struct domain *dp)
1c79356b 239{
39236c6e
A
240#pragma unused(dp)
241 static int udp_initialized = 0;
242 vm_size_t str_size;
243 struct inpcbinfo *pcbinfo;
244
245 VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED);
246
247 if (udp_initialized)
248 return;
249 udp_initialized = 1;
1c79356b
A
250
251 LIST_INIT(&udb);
39236c6e
A
252 udbinfo.ipi_listhead = &udb;
253 udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
254 &udbinfo.ipi_hashmask);
255 udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
256 &udbinfo.ipi_porthashmask);
257 str_size = (vm_size_t) sizeof (struct inpcb);
258 udbinfo.ipi_zone = zinit(str_size, 80000*str_size, 8192, "udpcb");
259
260 pcbinfo = &udbinfo;
91447636
A
261 /*
262 * allocate lock group attribute and group for udp pcb mutexes
263 */
39236c6e
A
264 pcbinfo->ipi_lock_grp_attr = lck_grp_attr_alloc_init();
265 pcbinfo->ipi_lock_grp = lck_grp_alloc_init("udppcb",
266 pcbinfo->ipi_lock_grp_attr);
267 pcbinfo->ipi_lock_attr = lck_attr_alloc_init();
268 if ((pcbinfo->ipi_lock = lck_rw_alloc_init(pcbinfo->ipi_lock_grp,
269 pcbinfo->ipi_lock_attr)) == NULL) {
270 panic("%s: unable to allocate PCB lock\n", __func__);
271 /* NOTREACHED */
272 }
91447636 273
39236c6e
A
274 udbinfo.ipi_gc = udp_gc;
275 in_pcbinfo_attach(&udbinfo);
1c79356b
A
276}
277
278void
39236c6e 279udp_input(struct mbuf *m, int iphlen)
1c79356b 280{
39236c6e
A
281 struct ip *ip;
282 struct udphdr *uh;
283 struct inpcb *inp;
284 struct mbuf *opts = NULL;
6d2010ae 285 int len, isbroadcast;
1c79356b
A
286 struct ip save_ip;
287 struct sockaddr *append_sa;
91447636 288 struct inpcbinfo *pcbinfo = &udbinfo;
39236c6e 289 struct sockaddr_in udp_in;
6d2010ae
A
290 struct ip_moptions *imo = NULL;
291 int foundmembership = 0, ret = 0;
2d21ac55 292#if INET6
39236c6e 293 struct udp_in6 udp_in6;
2d21ac55
A
294 struct udp_ip6 udp_ip6;
295#endif /* INET6 */
39236c6e
A
296 struct ifnet *ifp = m->m_pkthdr.rcvif;
297 boolean_t cell = IFNET_IS_CELLULAR(ifp);
298 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
299
300 bzero(&udp_in, sizeof (udp_in));
301 udp_in.sin_len = sizeof (struct sockaddr_in);
302 udp_in.sin_family = AF_INET;
303#if INET6
304 bzero(&udp_in6, sizeof (udp_in6));
305 udp_in6.uin6_sin.sin6_len = sizeof (struct sockaddr_in6);
306 udp_in6.uin6_sin.sin6_family = AF_INET6;
307#endif /* INET6 */
1c79356b
A
308
309 udpstat.udps_ipackets++;
1c79356b
A
310
311 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
312
316670eb
A
313 /* Expect 32-bit aligned data pointer on strict-align platforms */
314 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
315
1c79356b
A
316 /*
317 * Strip IP options, if any; should skip this,
318 * make available to user, and use on returned packets,
319 * but we don't yet have a way to check the checksum
320 * with options still present.
321 */
322 if (iphlen > sizeof (struct ip)) {
323 ip_stripoptions(m, (struct mbuf *)0);
39236c6e 324 iphlen = sizeof (struct ip);
1c79356b
A
325 }
326
327 /*
328 * Get IP and UDP header together in first mbuf.
329 */
330 ip = mtod(m, struct ip *);
39236c6e
A
331 if (m->m_len < iphlen + sizeof (struct udphdr)) {
332 m = m_pullup(m, iphlen + sizeof (struct udphdr));
333 if (m == NULL) {
1c79356b 334 udpstat.udps_hdrops++;
39236c6e
A
335 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
336 0,0,0,0,0);
1c79356b
A
337 return;
338 }
339 ip = mtod(m, struct ip *);
340 }
316670eb 341 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
1c79356b 342
9bccf70c 343 /* destination port of 0 is illegal, based on RFC768. */
316670eb 344 if (uh->uh_dport == 0) {
39236c6e 345 IF_UDP_STATINC(ifp, port0);
9bccf70c 346 goto bad;
316670eb 347 }
9bccf70c 348
1c79356b 349 KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
39236c6e 350 ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
1c79356b
A
351
352 /*
353 * Make mbuf data length reflect UDP length.
354 * If not enough data to reflect UDP length, drop.
355 */
356 len = ntohs((u_short)uh->uh_ulen);
357 if (ip->ip_len != len) {
39236c6e 358 if (len > ip->ip_len || len < sizeof (struct udphdr)) {
1c79356b 359 udpstat.udps_badlen++;
39236c6e 360 IF_UDP_STATINC(ifp, badlength);
1c79356b
A
361 goto bad;
362 }
363 m_adj(m, len - ip->ip_len);
364 /* ip->ip_len = len; */
365 }
366 /*
367 * Save a copy of the IP header in case we want restore it
368 * for sending an ICMP error message in response.
369 */
370 save_ip = *ip;
371
372 /*
373 * Checksum extended UDP header and data.
374 */
39236c6e
A
375 if (udp_input_checksum(m, uh, iphlen, len))
376 goto bad;
1c79356b 377
316670eb 378 isbroadcast = in_broadcast(ip->ip_dst, ifp);
6d2010ae
A
379
380 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) {
2d21ac55 381 int reuse_sock = 0, mcast_delivered = 0;
2d21ac55 382
39236c6e 383 lck_rw_lock_shared(pcbinfo->ipi_lock);
1c79356b
A
384 /*
385 * Deliver a multicast or broadcast datagram to *all* sockets
386 * for which the local and remote addresses and ports match
387 * those of the incoming datagram. This allows more than
388 * one process to receive multi/broadcasts on the same port.
389 * (This really ought to be done for unicast datagrams as
390 * well, but that would cause problems with existing
391 * applications that open both address-specific sockets and
392 * a wildcard socket listening to the same port -- they would
393 * end up receiving duplicates of every unicast datagram.
394 * Those applications open the multiple sockets to overcome an
395 * inadequacy of the UDP socket interface, but for backwards
396 * compatibility we avoid the problem here rather than
397 * fixing the interface. Maybe 4.5BSD will remedy this?)
398 */
399
400 /*
401 * Construct sockaddr format source address.
402 */
403 udp_in.sin_port = uh->uh_sport;
404 udp_in.sin_addr = ip->ip_src;
405 /*
406 * Locate pcb(s) for datagram.
407 * (Algorithm copied from raw_intr().)
408 */
1c79356b
A
409#if INET6
410 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
39236c6e 411#endif /* INET6 */
1c79356b 412 LIST_FOREACH(inp, &udb, inp_list) {
39236c6e
A
413#if IPSEC
414 int skipit;
415#endif /* IPSEC */
416
417 if (inp->inp_socket == NULL)
1c79356b 418 continue;
39236c6e
A
419 if (inp != sotoinpcb(inp->inp_socket)) {
420 panic("%s: bad so back ptr inp=%p\n",
421 __func__, inp);
422 /* NOTREACHED */
423 }
91447636
A
424#if INET6
425 if ((inp->inp_vflag & INP_IPV4) == 0)
426 continue;
39236c6e
A
427#endif /* INET6 */
428 if (inp_restricted(inp, ifp))
316670eb
A
429 continue;
430
39236c6e
A
431 if (IFNET_IS_CELLULAR(ifp) &&
432 (inp->inp_flags & INP_NO_IFT_CELLULAR))
6d2010ae
A
433 continue;
434
39236c6e
A
435 if ((inp->inp_moptions == NULL) &&
436 (ntohl(ip->ip_dst.s_addr) !=
437 INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0))
438 continue;
2d21ac55 439
39236c6e
A
440 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
441 WNT_STOPUSING)
91447636 442 continue;
91447636 443
39236c6e
A
444 udp_lock(inp->inp_socket, 1, 0);
445
446 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
447 WNT_STOPUSING) {
91447636 448 udp_unlock(inp->inp_socket, 1, 0);
1c79356b 449 continue;
91447636
A
450 }
451
452 if (inp->inp_lport != uh->uh_dport) {
453 udp_unlock(inp->inp_socket, 1, 0);
454 continue;
455 }
1c79356b
A
456 if (inp->inp_laddr.s_addr != INADDR_ANY) {
457 if (inp->inp_laddr.s_addr !=
91447636
A
458 ip->ip_dst.s_addr) {
459 udp_unlock(inp->inp_socket, 1, 0);
1c79356b 460 continue;
91447636 461 }
1c79356b
A
462 }
463 if (inp->inp_faddr.s_addr != INADDR_ANY) {
464 if (inp->inp_faddr.s_addr !=
465 ip->ip_src.s_addr ||
91447636
A
466 inp->inp_fport != uh->uh_sport) {
467 udp_unlock(inp->inp_socket, 1, 0);
1c79356b 468 continue;
91447636 469 }
1c79356b
A
470 }
471
39236c6e
A
472 if (isbroadcast == 0 && (ntohl(ip->ip_dst.s_addr) !=
473 INADDR_ALLHOSTS_GROUP)) {
474 struct sockaddr_in group;
475 int blocked;
476
477 if ((imo = inp->inp_moptions) == NULL) {
6d2010ae
A
478 udp_unlock(inp->inp_socket, 1, 0);
479 continue;
6d2010ae 480 }
39236c6e
A
481 IMO_LOCK(imo);
482
483 bzero(&group, sizeof (struct sockaddr_in));
484 group.sin_len = sizeof (struct sockaddr_in);
485 group.sin_family = AF_INET;
486 group.sin_addr = ip->ip_dst;
487
488 blocked = imo_multi_filter(imo, ifp,
489 (struct sockaddr *)&group,
490 (struct sockaddr *)&udp_in);
491 if (blocked == MCAST_PASS)
492 foundmembership = 1;
493
494 IMO_UNLOCK(imo);
495 if (!foundmembership) {
496 udp_unlock(inp->inp_socket, 1, 0);
497 if (blocked == MCAST_NOTSMEMBER ||
498 blocked == MCAST_MUTED)
499 udpstat.udps_filtermcast++;
500 continue;
501 }
502 foundmembership = 0;
6d2010ae 503 }
39236c6e
A
504
505 reuse_sock = (inp->inp_socket->so_options &
506 (SO_REUSEPORT|SO_REUSEADDR));
507
1c79356b 508#if IPSEC
39236c6e
A
509 skipit = 0;
510 /* check AH/ESP integrity. */
511 if (ipsec_bypass == 0 &&
512 ipsec4_in_reject_so(m, inp->inp_socket)) {
513 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
514 /* do not inject data to pcb */
515 skipit = 1;
516 }
517 if (skipit == 0)
1c79356b 518#endif /*IPSEC*/
39236c6e
A
519 {
520 struct mbuf *n = NULL;
b0d623f7 521
39236c6e
A
522 if (reuse_sock)
523 n = m_copy(m, 0, M_COPYALL);
524#if INET6
525 udp_append(inp, ip, m,
526 iphlen + sizeof (struct udphdr),
527 &udp_in, &udp_in6, &udp_ip6, ifp);
528#else /* !INET6 */
529 udp_append(inp, ip, m,
530 iphlen + sizeof (struct udphdr),
531 &udp_in, ifp);
532#endif /* !INET6 */
533 mcast_delivered++;
534
535 m = n;
1c79356b 536 }
39236c6e
A
537 udp_unlock(inp->inp_socket, 1, 0);
538
1c79356b
A
539 /*
540 * Don't look for additional matches if this one does
541 * not have either the SO_REUSEPORT or SO_REUSEADDR
542 * socket options set. This heuristic avoids searching
543 * through all pcbs in the common case of a non-shared
2d21ac55 544 * port. It assumes that an application will never
1c79356b
A
545 * clear these options after setting them.
546 */
b0d623f7 547 if (reuse_sock == 0 || m == NULL)
1c79356b 548 break;
316670eb
A
549
550 /*
551 * Expect 32-bit aligned data pointer on strict-align
552 * platforms.
553 */
554 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
b0d623f7
A
555 /*
556 * Recompute IP and UDP header pointers for new mbuf
557 */
558 ip = mtod(m, struct ip *);
316670eb 559 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
1c79356b 560 }
39236c6e 561 lck_rw_done(pcbinfo->ipi_lock);
1c79356b 562
2d21ac55 563 if (mcast_delivered == 0) {
1c79356b
A
564 /*
565 * No matching pcb found; discard datagram.
566 * (No need to send an ICMP Port Unreachable
567 * for a broadcast or multicast datgram.)
568 */
569 udpstat.udps_noportbcast++;
39236c6e 570 IF_UDP_STATINC(ifp, port_unreach);
1c79356b
A
571 goto bad;
572 }
2d21ac55 573
39236c6e
A
574 /* free the extra copy of mbuf or skipped by IPSec */
575 if (m != NULL)
2d21ac55 576 m_freem(m);
b0d623f7 577 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
1c79356b
A
578 return;
579 }
55e303ae 580
91447636 581#if IPSEC
55e303ae
A
582 /*
583 * UDP to port 4500 with a payload where the first four bytes are
584 * not zero is a UDP encapsulated IPSec packet. Packets where
585 * the payload is one byte and that byte is 0xFF are NAT keepalive
586 * packets. Decapsulate the ESP packet and carry on with IPSec input
587 * or discard the NAT keep-alive.
588 */
589 if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
39236c6e
A
590 uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) {
591 int payload_len = len - sizeof (struct udphdr) > 4 ? 4 :
592 len - sizeof (struct udphdr);
593
594 if (m->m_len < iphlen + sizeof (struct udphdr) + payload_len) {
595 if ((m = m_pullup(m, iphlen + sizeof (struct udphdr) +
596 payload_len)) == NULL) {
55e303ae 597 udpstat.udps_hdrops++;
39236c6e
A
598 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
599 0,0,0,0,0);
55e303ae
A
600 return;
601 }
316670eb
A
602 /*
603 * Expect 32-bit aligned data pointer on strict-align
604 * platforms.
605 */
606 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
607
55e303ae 608 ip = mtod(m, struct ip *);
316670eb 609 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
55e303ae
A
610 }
611 /* Check for NAT keepalive packet */
39236c6e
A
612 if (payload_len == 1 && *(u_int8_t*)
613 ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) {
55e303ae 614 m_freem(m);
39236c6e
A
615 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
616 0,0,0,0,0);
55e303ae 617 return;
39236c6e
A
618 } else if (payload_len == 4 && *(u_int32_t*)(void *)
619 ((caddr_t)uh + sizeof (struct udphdr)) != 0) {
55e303ae 620 /* UDP encapsulated IPSec packet to pass through NAT */
39236c6e
A
621 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
622 0,0,0,0,0);
6d2010ae 623 /* preserve the udp header */
39236c6e 624 esp4_input(m, iphlen + sizeof (struct udphdr));
55e303ae
A
625 return;
626 }
627 }
39236c6e 628#endif /* IPSEC */
55e303ae 629
1c79356b
A
630 /*
631 * Locate pcb for datagram.
632 */
633 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
316670eb 634 ip->ip_dst, uh->uh_dport, 1, ifp);
1c79356b 635 if (inp == NULL) {
39236c6e
A
636 IF_UDP_STATINC(ifp, port_unreach);
637
638 if (udp_log_in_vain) {
91447636
A
639 char buf[MAX_IPv4_STR_LEN];
640 char buf2[MAX_IPv4_STR_LEN];
641
642 /* check src and dst address */
39236c6e
A
643 if (udp_log_in_vain < 3) {
644 log(LOG_INFO, "Connection attempt to "
645 "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
646 &ip->ip_dst, buf, sizeof (buf)),
647 ntohs(uh->uh_dport), inet_ntop(AF_INET,
648 &ip->ip_src, buf2, sizeof (buf2)),
91447636 649 ntohs(uh->uh_sport));
39236c6e
A
650 } else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
651 ip->ip_dst.s_addr != ip->ip_src.s_addr) {
91447636 652 log_in_vain_log((LOG_INFO,
39236c6e
A
653 "Stealth Mode connection attempt to "
654 "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
655 &ip->ip_dst, buf, sizeof (buf)),
656 ntohs(uh->uh_dport), inet_ntop(AF_INET,
657 &ip->ip_src, buf2, sizeof (buf2)),
658 ntohs(uh->uh_sport)))
659 }
1c79356b
A
660 }
661 udpstat.udps_noport++;
662 if (m->m_flags & (M_BCAST | M_MCAST)) {
663 udpstat.udps_noportbcast++;
664 goto bad;
665 }
1c79356b 666#if ICMP_BANDLIM
9bccf70c 667 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
1c79356b 668 goto bad;
39236c6e 669#endif /* ICMP_BANDLIM */
9bccf70c 670 if (blackhole)
316670eb 671 if (ifp && ifp->if_type != IFT_LOOP)
91447636 672 goto bad;
9bccf70c
A
673 *ip = save_ip;
674 ip->ip_len += iphlen;
1c79356b
A
675 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
676 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
677 return;
678 }
91447636
A
679 udp_lock(inp->inp_socket, 1, 0);
680
681 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
682 udp_unlock(inp->inp_socket, 1, 0);
39236c6e 683 IF_UDP_STATINC(ifp, cleanup);
1c79356b
A
684 goto bad;
685 }
91447636
A
686#if IPSEC
687 if (ipsec_bypass == 0 && inp != NULL) {
91447636 688 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
2d21ac55 689 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
91447636 690 udp_unlock(inp->inp_socket, 1, 0);
39236c6e 691 IF_UDP_STATINC(ifp, badipsec);
91447636 692 goto bad;
316670eb 693 }
91447636 694 }
39236c6e 695#endif /* IPSEC */
1c79356b
A
696
697 /*
698 * Construct sockaddr format source address.
699 * Stuff source address and datagram in user buffer.
700 */
701 udp_in.sin_port = uh->uh_sport;
702 udp_in.sin_addr = ip->ip_src;
39236c6e
A
703 if ((inp->inp_flags & INP_CONTROLOPTS) != 0 ||
704 (inp->inp_socket->so_options & SO_TIMESTAMP) != 0 ||
705 (inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
1c79356b
A
706#if INET6
707 if (inp->inp_vflag & INP_IPV6) {
708 int savedflags;
709
710 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
711 savedflags = inp->inp_flags;
712 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
6d2010ae 713 ret = ip6_savecontrol(inp, m, &opts);
1c79356b
A
714 inp->inp_flags = savedflags;
715 } else
39236c6e 716#endif /* INET6 */
6d2010ae
A
717 {
718 ret = ip_savecontrol(inp, &opts, ip, m);
719 }
720 if (ret != 0) {
721 udp_unlock(inp->inp_socket, 1, 0);
722 goto bad;
723 }
1c79356b 724 }
39236c6e 725 m_adj(m, iphlen + sizeof (struct udphdr));
1c79356b
A
726
727 KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
39236c6e 728 save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
1c79356b
A
729
730#if INET6
731 if (inp->inp_vflag & INP_IPV6) {
732 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
2d21ac55 733 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
1c79356b 734 } else
39236c6e
A
735#endif /* INET6 */
736 {
737 append_sa = (struct sockaddr *)&udp_in;
738 }
6d2010ae 739 if (nstat_collect) {
39236c6e
A
740 INP_ADD_STAT(inp, cell, wifi, rxpackets, 1);
741 INP_ADD_STAT(inp, cell, wifi, rxbytes, m->m_pkthdr.len);
6d2010ae
A
742 }
743 so_recv_data_stat(inp->inp_socket, m, 0);
39236c6e
A
744 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa,
745 m, opts, NULL) == 0) {
1c79356b 746 udpstat.udps_fullsock++;
6d2010ae 747 } else {
91447636
A
748 sorwakeup(inp->inp_socket);
749 }
750 udp_unlock(inp->inp_socket, 1, 0);
1c79356b
A
751 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
752 return;
753bad:
754 m_freem(m);
755 if (opts)
756 m_freem(opts);
757 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
758}
759
760#if INET6
761static void
39236c6e 762ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
1c79356b 763{
39236c6e 764 bzero(ip6, sizeof (*ip6));
1c79356b
A
765
766 ip6->ip6_vfc = IPV6_VERSION;
767 ip6->ip6_plen = ip->ip_len;
768 ip6->ip6_nxt = ip->ip_p;
769 ip6->ip6_hlim = ip->ip_ttl;
316670eb
A
770 if (ip->ip_src.s_addr) {
771 ip6->ip6_src.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
772 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
773 }
774 if (ip->ip_dst.s_addr) {
775 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
776 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
777 }
1c79356b 778}
39236c6e 779#endif /* INET6 */
1c79356b
A
780
781/*
782 * subroutine of udp_input(), mainly for source code readability.
1c79356b
A
783 */
784static void
2d21ac55
A
785#if INET6
786udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
787 struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6,
39236c6e
A
788 struct udp_ip6 *pudp_ip6, struct ifnet *ifp)
789#else /* !INET6 */
2d21ac55 790udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
39236c6e
A
791 struct sockaddr_in *pudp_in, struct ifnet *ifp)
792#endif /* !INET6 */
1c79356b
A
793{
794 struct sockaddr *append_sa;
795 struct mbuf *opts = 0;
39236c6e
A
796 boolean_t cell = IFNET_IS_CELLULAR(ifp);
797 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
6d2010ae 798 int ret = 0;
1c79356b 799
2d21ac55
A
800#if CONFIG_MACF_NET
801 if (mac_inpcb_check_deliver(last, n, AF_INET, SOCK_DGRAM) != 0) {
802 m_freem(n);
803 return;
804 }
39236c6e 805#endif /* CONFIG_MACF_NET */
6d2010ae
A
806 if ((last->inp_flags & INP_CONTROLOPTS) != 0 ||
807 (last->inp_socket->so_options & SO_TIMESTAMP) != 0 ||
808 (last->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
1c79356b
A
809#if INET6
810 if (last->inp_vflag & INP_IPV6) {
811 int savedflags;
812
2d21ac55
A
813 if (pudp_ip6->uip6_init_done == 0) {
814 ip_2_ip6_hdr(&pudp_ip6->uip6_ip6, ip);
815 pudp_ip6->uip6_init_done = 1;
1c79356b
A
816 }
817 savedflags = last->inp_flags;
818 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
6d2010ae
A
819 ret = ip6_savecontrol(last, n, &opts);
820 if (ret != 0) {
821 last->inp_flags = savedflags;
822 goto error;
823 }
1c79356b
A
824 last->inp_flags = savedflags;
825 } else
39236c6e 826#endif /* INET6 */
6d2010ae
A
827 {
828 ret = ip_savecontrol(last, &opts, ip, n);
829 if (ret != 0) {
830 goto error;
831 }
832 }
1c79356b
A
833 }
834#if INET6
835 if (last->inp_vflag & INP_IPV6) {
2d21ac55
A
836 if (pudp_in6->uin6_init_done == 0) {
837 in6_sin_2_v4mapsin6(pudp_in, &pudp_in6->uin6_sin);
838 pudp_in6->uin6_init_done = 1;
1c79356b 839 }
2d21ac55 840 append_sa = (struct sockaddr *)&pudp_in6->uin6_sin;
1c79356b 841 } else
39236c6e 842#endif /* INET6 */
2d21ac55 843 append_sa = (struct sockaddr *)pudp_in;
6d2010ae 844 if (nstat_collect) {
39236c6e
A
845 INP_ADD_STAT(last, cell, wifi, rxpackets, 1);
846 INP_ADD_STAT(last, cell, wifi, rxbytes, n->m_pkthdr.len);
6d2010ae
A
847 }
848 so_recv_data_stat(last->inp_socket, n, 0);
1c79356b 849 m_adj(n, off);
39236c6e
A
850 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa,
851 n, opts, NULL) == 0) {
1c79356b 852 udpstat.udps_fullsock++;
6d2010ae 853 } else {
1c79356b 854 sorwakeup(last->inp_socket);
6d2010ae
A
855 }
856 return;
857error:
858 m_freem(n);
859 m_freem(opts);
860 return;
1c79356b
A
861}
862
1c79356b
A
863/*
864 * Notify a udp user of an asynchronous error;
865 * just wake up so that he can collect error status.
866 */
867void
39236c6e 868udp_notify(struct inpcb *inp, int errno)
1c79356b
A
869{
870 inp->inp_socket->so_error = errno;
871 sorwakeup(inp->inp_socket);
872 sowwakeup(inp->inp_socket);
873}
874
875void
39236c6e 876udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
1c79356b 877{
9bccf70c 878 struct ip *ip = vip;
91447636 879 void (*notify)(struct inpcb *, int) = udp_notify;
9bccf70c
A
880 struct in_addr faddr;
881 struct inpcb *inp;
9bccf70c 882
316670eb 883 faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
9bccf70c 884 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
316670eb 885 return;
1c79356b 886
9bccf70c
A
887 if (PRC_IS_REDIRECT(cmd)) {
888 ip = 0;
889 notify = in_rtchange;
39236c6e 890 } else if (cmd == PRC_HOSTDEAD) {
9bccf70c 891 ip = 0;
39236c6e 892 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
1c79356b 893 return;
39236c6e 894 }
1c79356b 895 if (ip) {
316670eb
A
896 struct udphdr uh;
897
898 bcopy(((caddr_t)ip + (ip->ip_hl << 2)), &uh, sizeof (uh));
899 inp = in_pcblookup_hash(&udbinfo, faddr, uh.uh_dport,
900 ip->ip_src, uh.uh_sport, 0, NULL);
91447636
A
901 if (inp != NULL && inp->inp_socket != NULL) {
902 udp_lock(inp->inp_socket, 1, 0);
316670eb
A
903 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
904 WNT_STOPUSING) {
91447636
A
905 udp_unlock(inp->inp_socket, 1, 0);
906 return;
907 }
9bccf70c 908 (*notify)(inp, inetctlerrmap[cmd]);
91447636
A
909 udp_unlock(inp->inp_socket, 1, 0);
910 }
39236c6e 911 } else {
91447636 912 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
39236c6e 913 }
1c79356b
A
914}
915
2d21ac55
A
916int
917udp_ctloutput(struct socket *so, struct sockopt *sopt)
918{
919 int error, optval;
920 struct inpcb *inp;
921
316670eb
A
922 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
923 if (sopt->sopt_level != IPPROTO_UDP &&
924 !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH))
2d21ac55
A
925 return (ip_ctloutput(so, sopt));
926
927 error = 0;
928 inp = sotoinpcb(so);
929
930 switch (sopt->sopt_dir) {
931 case SOPT_SET:
932 switch (sopt->sopt_name) {
933 case UDP_NOCKSUM:
934 /* This option is settable only for UDP over IPv4 */
935 if (!(inp->inp_vflag & INP_IPV4)) {
936 error = EINVAL;
937 break;
938 }
939
940 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
941 sizeof (optval))) != 0)
942 break;
943
944 if (optval != 0)
945 inp->inp_flags |= INP_UDP_NOCKSUM;
946 else
947 inp->inp_flags &= ~INP_UDP_NOCKSUM;
948 break;
949
316670eb
A
950 case SO_FLUSH:
951 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
952 sizeof (optval))) != 0)
953 break;
954
955 error = inp_flush(inp, optval);
956 break;
957
2d21ac55
A
958 default:
959 error = ENOPROTOOPT;
960 break;
961 }
962 break;
963
964 case SOPT_GET:
965 switch (sopt->sopt_name) {
966 case UDP_NOCKSUM:
967 optval = inp->inp_flags & INP_UDP_NOCKSUM;
968 break;
969
970 default:
971 error = ENOPROTOOPT;
972 break;
973 }
974 if (error == 0)
975 error = sooptcopyout(sopt, &optval, sizeof (optval));
976 break;
977 }
978 return (error);
979}
980
1c79356b
A
981static int
982udp_pcblist SYSCTL_HANDLER_ARGS
983{
2d21ac55 984#pragma unused(oidp, arg1, arg2)
91447636 985 int error, i, n;
1c79356b
A
986 struct inpcb *inp, **inp_list;
987 inp_gen_t gencnt;
988 struct xinpgen xig;
989
990 /*
991 * The process of preparing the TCB list is too time-consuming and
992 * resource-intensive to repeat twice on every request.
993 */
39236c6e 994 lck_rw_lock_exclusive(udbinfo.ipi_lock);
91447636 995 if (req->oldptr == USER_ADDR_NULL) {
1c79356b 996 n = udbinfo.ipi_count;
39236c6e
A
997 req->oldidx = 2 * (sizeof (xig))
998 + (n + n/8) * sizeof (struct xinpcb);
999 lck_rw_done(udbinfo.ipi_lock);
1000 return (0);
1c79356b
A
1001 }
1002
91447636 1003 if (req->newptr != USER_ADDR_NULL) {
39236c6e
A
1004 lck_rw_done(udbinfo.ipi_lock);
1005 return (EPERM);
91447636 1006 }
1c79356b
A
1007
1008 /*
1009 * OK, now we're committed to doing something.
1010 */
1c79356b
A
1011 gencnt = udbinfo.ipi_gencnt;
1012 n = udbinfo.ipi_count;
1c79356b 1013
39236c6e
A
1014 bzero(&xig, sizeof (xig));
1015 xig.xig_len = sizeof (xig);
1c79356b
A
1016 xig.xig_count = n;
1017 xig.xig_gen = gencnt;
1018 xig.xig_sogen = so_gencnt;
39236c6e 1019 error = SYSCTL_OUT(req, &xig, sizeof (xig));
91447636 1020 if (error) {
39236c6e
A
1021 lck_rw_done(udbinfo.ipi_lock);
1022 return (error);
91447636 1023 }
39236c6e
A
1024 /*
1025 * We are done if there is no pcb
1026 */
1027 if (n == 0) {
1028 lck_rw_done(udbinfo.ipi_lock);
1029 return (0);
1030 }
1031
1032 inp_list = _MALLOC(n * sizeof (*inp_list), M_TEMP, M_WAITOK);
1c79356b 1033 if (inp_list == 0) {
39236c6e
A
1034 lck_rw_done(udbinfo.ipi_lock);
1035 return (ENOMEM);
1c79356b 1036 }
39236c6e
A
1037
1038 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
9bccf70c 1039 inp = LIST_NEXT(inp, inp_list)) {
39236c6e
A
1040 if (inp->inp_gencnt <= gencnt &&
1041 inp->inp_state != INPCB_STATE_DEAD)
1c79356b
A
1042 inp_list[i++] = inp;
1043 }
1c79356b
A
1044 n = i;
1045
1046 error = 0;
1047 for (i = 0; i < n; i++) {
1048 inp = inp_list[i];
39236c6e
A
1049 if (inp->inp_gencnt <= gencnt &&
1050 inp->inp_state != INPCB_STATE_DEAD) {
1c79356b 1051 struct xinpcb xi;
3a60a9f5 1052
39236c6e
A
1053 bzero(&xi, sizeof (xi));
1054 xi.xi_len = sizeof (xi);
1c79356b 1055 /* XXX should avoid extra copy */
91447636 1056 inpcb_to_compat(inp, &xi.xi_inp);
1c79356b
A
1057 if (inp->inp_socket)
1058 sotoxsocket(inp->inp_socket, &xi.xi_socket);
39236c6e 1059 error = SYSCTL_OUT(req, &xi, sizeof (xi));
1c79356b
A
1060 }
1061 }
1062 if (!error) {
1063 /*
1064 * Give the user an updated idea of our state.
1065 * If the generation differs from what we told
1066 * her before, she knows that something happened
1067 * while we were processing this request, and it
1068 * might be necessary to retry.
1069 */
39236c6e
A
1070 bzero(&xig, sizeof (xig));
1071 xig.xig_len = sizeof (xig);
1c79356b
A
1072 xig.xig_gen = udbinfo.ipi_gencnt;
1073 xig.xig_sogen = so_gencnt;
1074 xig.xig_count = udbinfo.ipi_count;
39236c6e 1075 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1c79356b
A
1076 }
1077 FREE(inp_list, M_TEMP);
39236c6e
A
1078 lck_rw_done(udbinfo.ipi_lock);
1079 return (error);
1c79356b
A
1080}
1081
39236c6e
A
1082SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
1083 CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist,
1084 "S,xinpcb", "List of active UDP sockets");
1c79356b
A
1085
1086
b0d623f7
A
1087static int
1088udp_pcblist64 SYSCTL_HANDLER_ARGS
1089{
1090#pragma unused(oidp, arg1, arg2)
1091 int error, i, n;
1092 struct inpcb *inp, **inp_list;
1093 inp_gen_t gencnt;
1094 struct xinpgen xig;
1095
1096 /*
1097 * The process of preparing the TCB list is too time-consuming and
1098 * resource-intensive to repeat twice on every request.
1099 */
39236c6e 1100 lck_rw_lock_shared(udbinfo.ipi_lock);
b0d623f7
A
1101 if (req->oldptr == USER_ADDR_NULL) {
1102 n = udbinfo.ipi_count;
39236c6e
A
1103 req->oldidx =
1104 2 * (sizeof (xig)) + (n + n/8) * sizeof (struct xinpcb64);
1105 lck_rw_done(udbinfo.ipi_lock);
1106 return (0);
b0d623f7
A
1107 }
1108
1109 if (req->newptr != USER_ADDR_NULL) {
39236c6e
A
1110 lck_rw_done(udbinfo.ipi_lock);
1111 return (EPERM);
b0d623f7
A
1112 }
1113
1114 /*
1115 * OK, now we're committed to doing something.
1116 */
1117 gencnt = udbinfo.ipi_gencnt;
1118 n = udbinfo.ipi_count;
1119
39236c6e
A
1120 bzero(&xig, sizeof (xig));
1121 xig.xig_len = sizeof (xig);
b0d623f7
A
1122 xig.xig_count = n;
1123 xig.xig_gen = gencnt;
1124 xig.xig_sogen = so_gencnt;
39236c6e 1125 error = SYSCTL_OUT(req, &xig, sizeof (xig));
b0d623f7 1126 if (error) {
39236c6e
A
1127 lck_rw_done(udbinfo.ipi_lock);
1128 return (error);
b0d623f7 1129 }
39236c6e
A
1130 /*
1131 * We are done if there is no pcb
1132 */
1133 if (n == 0) {
1134 lck_rw_done(udbinfo.ipi_lock);
1135 return (0);
1136 }
1137
1138 inp_list = _MALLOC(n * sizeof (*inp_list), M_TEMP, M_WAITOK);
b0d623f7 1139 if (inp_list == 0) {
39236c6e
A
1140 lck_rw_done(udbinfo.ipi_lock);
1141 return (ENOMEM);
b0d623f7
A
1142 }
1143
39236c6e 1144 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
b0d623f7 1145 inp = LIST_NEXT(inp, inp_list)) {
39236c6e
A
1146 if (inp->inp_gencnt <= gencnt &&
1147 inp->inp_state != INPCB_STATE_DEAD)
b0d623f7
A
1148 inp_list[i++] = inp;
1149 }
1150 n = i;
1151
1152 error = 0;
1153 for (i = 0; i < n; i++) {
1154 inp = inp_list[i];
39236c6e
A
1155 if (inp->inp_gencnt <= gencnt &&
1156 inp->inp_state != INPCB_STATE_DEAD) {
b0d623f7
A
1157 struct xinpcb64 xi;
1158
39236c6e
A
1159 bzero(&xi, sizeof (xi));
1160 xi.xi_len = sizeof (xi);
b0d623f7
A
1161 inpcb_to_xinpcb64(inp, &xi);
1162 if (inp->inp_socket)
1163 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
39236c6e 1164 error = SYSCTL_OUT(req, &xi, sizeof (xi));
b0d623f7
A
1165 }
1166 }
1167 if (!error) {
1168 /*
1169 * Give the user an updated idea of our state.
1170 * If the generation differs from what we told
1171 * her before, she knows that something happened
1172 * while we were processing this request, and it
1173 * might be necessary to retry.
1174 */
39236c6e
A
1175 bzero(&xig, sizeof (xig));
1176 xig.xig_len = sizeof (xig);
b0d623f7
A
1177 xig.xig_gen = udbinfo.ipi_gencnt;
1178 xig.xig_sogen = so_gencnt;
1179 xig.xig_count = udbinfo.ipi_count;
39236c6e 1180 error = SYSCTL_OUT(req, &xig, sizeof (xig));
b0d623f7
A
1181 }
1182 FREE(inp_list, M_TEMP);
39236c6e
A
1183 lck_rw_done(udbinfo.ipi_lock);
1184 return (error);
b0d623f7
A
1185}
1186
39236c6e
A
1187SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist64,
1188 CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist64,
1189 "S,xinpcb64", "List of active UDP sockets");
b0d623f7 1190
1c79356b 1191
6d2010ae
A
1192static int
1193udp_pcblist_n SYSCTL_HANDLER_ARGS
1194{
1195#pragma unused(oidp, arg1, arg2)
39236c6e 1196 return (get_pcblist_n(IPPROTO_UDP, req, &udbinfo));
6d2010ae
A
1197}
1198
39236c6e
A
1199SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist_n,
1200 CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist_n,
1201 "S,xinpcb_n", "List of active UDP sockets");
6d2010ae 1202
316670eb 1203__private_extern__ void
39236c6e
A
1204udp_get_ports_used(uint32_t ifindex, int protocol, uint32_t wildcardok,
1205 bitstr_t *bitfield)
316670eb 1206{
39236c6e 1207 inpcb_get_ports_used(ifindex, protocol, wildcardok, bitfield, &udbinfo);
316670eb
A
1208}
1209
1210__private_extern__ uint32_t
1211udp_count_opportunistic(unsigned int ifindex, u_int32_t flags)
1212{
39236c6e 1213 return (inpcb_count_opportunistic(ifindex, &udbinfo, flags));
316670eb
A
1214}
1215
39236c6e
A
1216__private_extern__ uint32_t
1217udp_find_anypcb_byaddr(struct ifaddr *ifa)
91447636 1218{
39236c6e 1219 return (inpcb_find_anypcb_byaddr(ifa, &udbinfo));
91447636
A
1220}
1221
6d2010ae 1222static int
39236c6e
A
1223udp_check_pktinfo(struct mbuf *control, struct ifnet **outif,
1224 struct in_addr *laddr)
6d2010ae
A
1225{
1226 struct cmsghdr *cm = 0;
316670eb 1227 struct in_pktinfo *pktinfo;
6d2010ae
A
1228 struct ifnet *ifp;
1229
39236c6e
A
1230 if (outif != NULL)
1231 *outif = NULL;
1232
6d2010ae
A
1233 /*
1234 * XXX: Currently, we assume all the optional information is stored
1235 * in a single mbuf.
1236 */
1237 if (control->m_next)
1238 return (EINVAL);
1239
1240 if (control->m_len < CMSG_LEN(0))
1241 return (EINVAL);
1242
39236c6e
A
1243 for (cm = M_FIRST_CMSGHDR(control); cm;
1244 cm = M_NXT_CMSGHDR(control, cm)) {
1245 if (cm->cmsg_len < sizeof (struct cmsghdr) ||
1246 cm->cmsg_len > control->m_len)
6d2010ae 1247 return (EINVAL);
316670eb 1248
6d2010ae
A
1249 if (cm->cmsg_level != IPPROTO_IP || cm->cmsg_type != IP_PKTINFO)
1250 continue;
1251
39236c6e 1252 if (cm->cmsg_len != CMSG_LEN(sizeof (struct in_pktinfo)))
6d2010ae
A
1253 return (EINVAL);
1254
316670eb 1255 pktinfo = (struct in_pktinfo *)(void *)CMSG_DATA(cm);
6d2010ae
A
1256
1257 /* Check for a valid ifindex in pktinfo */
1258 ifnet_head_lock_shared();
1259
1260 if (pktinfo->ipi_ifindex > if_index) {
1261 ifnet_head_done();
1262 return (ENXIO);
1263 }
1264
39236c6e
A
1265 /*
1266 * If ipi_ifindex is specified it takes precedence
1267 * over ipi_spec_dst.
1268 */
6d2010ae
A
1269 if (pktinfo->ipi_ifindex) {
1270 ifp = ifindex2ifnet[pktinfo->ipi_ifindex];
1271 if (ifp == NULL) {
1272 ifnet_head_done();
1273 return (ENXIO);
1274 }
39236c6e
A
1275 if (outif != NULL) {
1276 ifnet_reference(ifp);
316670eb 1277 *outif = ifp;
39236c6e
A
1278 }
1279 ifnet_head_done();
6d2010ae
A
1280 laddr->s_addr = INADDR_ANY;
1281 break;
1282 }
1283
1284 ifnet_head_done();
1285
39236c6e
A
1286 /*
1287 * Use the provided ipi_spec_dst address for temp
1288 * source address.
1289 */
6d2010ae
A
1290 *laddr = pktinfo->ipi_spec_dst;
1291 break;
1292 }
1293 return (0);
1294}
1295
1c79356b 1296static int
39236c6e
A
1297udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1298 struct mbuf *control, struct proc *p)
1c79356b 1299{
39236c6e
A
1300 struct udpiphdr *ui;
1301 int len = m->m_pkthdr.len;
2d21ac55 1302 struct sockaddr_in *sin;
6d2010ae 1303 struct in_addr origladdr, laddr, faddr, pi_laddr;
91447636 1304 u_short lport, fport;
6d2010ae 1305 int error = 0, udp_dodisconnect = 0, pktinfo = 0;
2d21ac55 1306 struct socket *so = inp->inp_socket;
c910b4d9 1307 int soopts = 0;
2d21ac55
A
1308 struct mbuf *inpopts;
1309 struct ip_moptions *mopts;
1310 struct route ro;
39236c6e
A
1311 struct ip_out_args ipoa =
1312 { IFSCOPE_NONE, { 0 }, IPOAF_SELECT_SRCIF, 0 };
316670eb
A
1313 struct ifnet *outif = NULL;
1314 struct flowadv *adv = &ipoa.ipoa_flowadv;
1315 mbuf_svc_class_t msc = MBUF_SC_UNSPEC;
1316 struct ifnet *origoutifp;
1317 int flowadv = 0;
1318
1319 /* Enable flow advisory only when connected */
1320 flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0;
6d2010ae 1321 pi_laddr.s_addr = INADDR_ANY;
1c79356b
A
1322
1323 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
1324
316670eb 1325 lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
d41d1dae 1326 if (control != NULL) {
316670eb 1327 msc = mbuf_service_class_from_control(control);
39236c6e 1328 VERIFY(outif == NULL);
316670eb 1329 error = udp_check_pktinfo(control, &outif, &pi_laddr);
d41d1dae 1330 m_freem(control);
39236c6e 1331 control = NULL;
6d2010ae
A
1332 if (error)
1333 goto release;
1334 pktinfo++;
316670eb
A
1335 if (outif != NULL)
1336 ipoa.ipoa_boundif = outif->if_index;
d41d1dae 1337 }
6d2010ae 1338
1c79356b 1339 KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
39236c6e
A
1340 inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
1341 (htons((u_short)len + sizeof (struct udphdr))));
1c79356b 1342
39236c6e 1343 if (len + sizeof (struct udpiphdr) > IP_MAXPACKET) {
1c79356b
A
1344 error = EMSGSIZE;
1345 goto release;
1346 }
1347
316670eb
A
1348 if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(inp)) {
1349 /*
1350 * The socket is flow-controlled, drop the packets
1351 * until the inp is not flow controlled
1352 */
1353 error = ENOBUFS;
1354 goto release;
1355 }
6d2010ae
A
1356 /*
1357 * If socket was bound to an ifindex, tell ip_output about it.
1358 * If the ancillary IP_PKTINFO option contains an interface index,
1359 * it takes precedence over the one specified by IP_BOUND_IF.
1360 */
39236c6e
A
1361 if (ipoa.ipoa_boundif == IFSCOPE_NONE &&
1362 (inp->inp_flags & INP_BOUND_IF)) {
1363 VERIFY(inp->inp_boundifp != NULL);
1364 ifnet_reference(inp->inp_boundifp); /* for this routine */
1365 if (outif != NULL)
1366 ifnet_release(outif);
316670eb
A
1367 outif = inp->inp_boundifp;
1368 ipoa.ipoa_boundif = outif->if_index;
1369 }
1370 if (inp->inp_flags & INP_NO_IFT_CELLULAR)
1371 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
c910b4d9
A
1372 soopts |= IP_OUTARGS;
1373
39236c6e
A
1374 /*
1375 * If there was a routing change, discard cached route and check
1376 * that we have a valid source address. Reacquire a new source
1377 * address if INADDR_ANY was specified.
55e303ae 1378 */
39236c6e
A
1379 if (ROUTE_UNUSABLE(&inp->inp_route)) {
1380 struct in_ifaddr *ia = NULL;
1381
1382 ROUTE_RELEASE(&inp->inp_route);
b0d623f7
A
1383
1384 /* src address is gone? */
39236c6e
A
1385 if (inp->inp_laddr.s_addr != INADDR_ANY &&
1386 (ia = ifa_foraddr(inp->inp_laddr.s_addr)) == NULL) {
1387 if (!(inp->inp_flags & INP_INADDR_ANY) ||
316670eb 1388 (so->so_state & SS_ISCONNECTED)) {
39236c6e
A
1389 /*
1390 * Rdar://5448998
1391 * If the source address is gone, return an
1392 * error if:
6d2010ae
A
1393 * - the source was specified
1394 * - the socket was already connected
1395 */
39236c6e 1396 soevent(so, (SO_FILT_HINT_LOCKED |
316670eb 1397 SO_FILT_HINT_NOSRCADDR));
55e303ae
A
1398 error = EADDRNOTAVAIL;
1399 goto release;
6d2010ae
A
1400 } else {
1401 /* new src will be set later */
1402 inp->inp_laddr.s_addr = INADDR_ANY;
316670eb 1403 inp->inp_last_outifp = NULL;
55e303ae
A
1404 }
1405 }
b0d623f7 1406 if (ia != NULL)
6d2010ae 1407 IFA_REMREF(&ia->ia_ifa);
55e303ae
A
1408 }
1409
316670eb 1410 origoutifp = inp->inp_last_outifp;
6d2010ae 1411
39236c6e
A
1412 /*
1413 * IP_PKTINFO option check. If a temporary scope or src address
1414 * is provided, use it for this packet only and make sure we forget
1415 * it after sending this datagram.
6d2010ae 1416 */
6d2010ae
A
1417 if (pi_laddr.s_addr != INADDR_ANY ||
1418 (ipoa.ipoa_boundif != IFSCOPE_NONE && pktinfo)) {
39236c6e
A
1419 /* temp src address for this datagram only */
1420 laddr = pi_laddr;
6d2010ae 1421 origladdr.s_addr = INADDR_ANY;
39236c6e
A
1422 /* we don't want to keep the laddr or route */
1423 udp_dodisconnect = 1;
1424 /* remember we don't care about src addr.*/
1425 inp->inp_flags |= INP_INADDR_ANY;
6d2010ae
A
1426 } else {
1427 origladdr = laddr = inp->inp_laddr;
1428 }
1429
316670eb 1430 origoutifp = inp->inp_last_outifp;
91447636
A
1431 faddr = inp->inp_faddr;
1432 lport = inp->inp_lport;
1433 fport = inp->inp_fport;
1434
1c79356b 1435 if (addr) {
316670eb 1436 sin = (struct sockaddr_in *)(void *)addr;
91447636 1437 if (faddr.s_addr != INADDR_ANY) {
1c79356b
A
1438 error = EISCONN;
1439 goto release;
1440 }
91447636
A
1441 if (lport == 0) {
1442 /*
39236c6e
A
1443 * In case we don't have a local port set, go through
1444 * the full connect. We don't have a local port yet
1445 * (i.e., we can't be looked up), so it's not an issue
1446 * if the input runs at the same time we do this.
6d2010ae 1447 */
39236c6e
A
1448 /* if we have a source address specified, use that */
1449 if (pi_laddr.s_addr != INADDR_ANY)
6d2010ae 1450 inp->inp_laddr = pi_laddr;
39236c6e
A
1451 /*
1452 * If a scope is specified, use it. Scope from
1453 * IP_PKTINFO takes precendence over the the scope
1454 * set via INP_BOUND_IF.
1455 */
1456 error = in_pcbconnect(inp, addr, p, ipoa.ipoa_boundif,
1457 &outif);
1458 if (error)
91447636 1459 goto release;
39236c6e 1460
91447636
A
1461 laddr = inp->inp_laddr;
1462 lport = inp->inp_lport;
1463 faddr = inp->inp_faddr;
1464 fport = inp->inp_fport;
1465 udp_dodisconnect = 1;
39236c6e
A
1466
1467 /* synch up in case in_pcbladdr() overrides */
1468 if (outif != NULL && ipoa.ipoa_boundif != IFSCOPE_NONE)
1469 ipoa.ipoa_boundif = outif->if_index;
91447636 1470 }
6d2010ae 1471 else {
39236c6e
A
1472 /*
1473 * Fast path case
1474 *
1475 * We have a full address and a local port; use those
1476 * info to build the packet without changing the pcb
1477 * and interfering with the input path. See 3851370.
1478 *
1479 * Scope from IP_PKTINFO takes precendence over the
1480 * the scope set via INP_BOUND_IF.
91447636
A
1481 */
1482 if (laddr.s_addr == INADDR_ANY) {
39236c6e
A
1483 if ((error = in_pcbladdr(inp, addr, &laddr,
1484 ipoa.ipoa_boundif, &outif)) != 0)
316670eb 1485 goto release;
39236c6e
A
1486 /*
1487 * from pcbconnect: remember we don't
1488 * care about src addr.
1489 */
1490 inp->inp_flags |= INP_INADDR_ANY;
1491
1492 /* synch up in case in_pcbladdr() overrides */
1493 if (outif != NULL &&
1494 ipoa.ipoa_boundif != IFSCOPE_NONE)
1495 ipoa.ipoa_boundif = outif->if_index;
91447636 1496 }
6d2010ae 1497
91447636
A
1498 faddr = sin->sin_addr;
1499 fport = sin->sin_port;
1c79356b
A
1500 }
1501 } else {
91447636 1502 if (faddr.s_addr == INADDR_ANY) {
1c79356b
A
1503 error = ENOTCONN;
1504 goto release;
1505 }
1506 }
55e303ae 1507
2d21ac55
A
1508#if CONFIG_MACF_NET
1509 mac_mbuf_label_associate_inpcb(inp, m);
39236c6e
A
1510#endif /* CONFIG_MACF_NET */
1511
316670eb
A
1512 if (inp->inp_flowhash == 0)
1513 inp->inp_flowhash = inp_calc_flowhash(inp);
55e303ae 1514
1c79356b
A
1515 /*
1516 * Calculate data length and get a mbuf
1517 * for UDP and IP headers.
1518 */
39236c6e 1519 M_PREPEND(m, sizeof (struct udpiphdr), M_DONTWAIT);
1c79356b
A
1520 if (m == 0) {
1521 error = ENOBUFS;
55e303ae 1522 goto abort;
1c79356b
A
1523 }
1524
1525 /*
1526 * Fill in mbuf with extended UDP header
1527 * and addresses and length put into network format.
1528 */
1529 ui = mtod(m, struct udpiphdr *);
39236c6e 1530 bzero(ui->ui_x1, sizeof (ui->ui_x1)); /* XXX still needed? */
1c79356b 1531 ui->ui_pr = IPPROTO_UDP;
91447636
A
1532 ui->ui_src = laddr;
1533 ui->ui_dst = faddr;
1534 ui->ui_sport = lport;
1535 ui->ui_dport = fport;
39236c6e 1536 ui->ui_ulen = htons((u_short)len + sizeof (struct udphdr));
1c79356b
A
1537
1538 /*
9bccf70c 1539 * Set up checksum and output datagram.
1c79356b 1540 */
2d21ac55 1541 if (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM)) {
39236c6e
A
1542 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
1543 htons((u_short)len + sizeof (struct udphdr) + IPPROTO_UDP));
9bccf70c
A
1544 m->m_pkthdr.csum_flags = CSUM_UDP;
1545 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1546 } else {
0b4e3aa0 1547 ui->ui_sum = 0;
9bccf70c 1548 }
1c79356b
A
1549 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1550 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1551 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
1552 udpstat.udps_opackets++;
1553
1554 KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport,
1555 ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen);
1556
1c79356b 1557#if IPSEC
9bccf70c
A
1558 if (ipsec_bypass == 0 && ipsec_setsocket(m, inp->inp_socket) != 0) {
1559 error = ENOBUFS;
55e303ae 1560 goto abort;
9bccf70c 1561 }
39236c6e 1562#endif /* IPSEC */
2d21ac55
A
1563
1564 inpopts = inp->inp_options;
c910b4d9 1565 soopts |= (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
2d21ac55 1566 mopts = inp->inp_moptions;
316670eb
A
1567 if (mopts != NULL) {
1568 IMO_LOCK(mopts);
1569 IMO_ADDREF_LOCKED(mopts);
1570 if (IN_MULTICAST(ntohl(ui->ui_dst.s_addr)) &&
39236c6e
A
1571 mopts->imo_multicast_ifp != NULL) {
1572 /* no reference needed */
316670eb
A
1573 inp->inp_last_outifp = mopts->imo_multicast_ifp;
1574 }
1575 IMO_UNLOCK(mopts);
1576 }
2d21ac55 1577
b0d623f7
A
1578 /* Copy the cached route and take an extra reference */
1579 inp_route_copyout(inp, &ro);
2d21ac55 1580
316670eb 1581 set_packet_service_class(m, so, msc, 0);
39236c6e
A
1582 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
1583 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
1584 m->m_pkthdr.pkt_proto = IPPROTO_UDP;
1585 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC);
316670eb 1586 if (flowadv)
39236c6e 1587 m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
316670eb
A
1588
1589 if (ipoa.ipoa_boundif != IFSCOPE_NONE)
1590 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
1591
1592 if (laddr.s_addr != INADDR_ANY)
1593 ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
1594
1595 inp->inp_sndinprog_cnt++;
d1ecb069 1596
2d21ac55 1597 socket_unlock(so, 0);
39236c6e 1598 error = ip_output(m, inpopts, &ro, soopts, mopts, &ipoa);
6d2010ae 1599 m = NULL;
2d21ac55 1600 socket_lock(so, 0);
6d2010ae
A
1601 if (mopts != NULL)
1602 IMO_REMREF(mopts);
1c79356b 1603
6d2010ae 1604 if (error == 0 && nstat_collect) {
39236c6e
A
1605 boolean_t cell, wifi;
1606
1607 if (ro.ro_rt != NULL) {
1608 cell = IFNET_IS_CELLULAR(ro.ro_rt->rt_ifp);
1609 wifi = (!cell && IFNET_IS_WIFI(ro.ro_rt->rt_ifp));
1610 } else {
1611 cell = wifi = FALSE;
1612 }
1613 INP_ADD_STAT(inp, cell, wifi, txpackets, 1);
1614 INP_ADD_STAT(inp, cell, wifi, txbytes, len);
6d2010ae 1615 }
316670eb
A
1616
1617 if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
1618 adv->code == FADV_SUSPENDED)) {
1619 /* return a hint to the application that
1620 * the packet has been dropped
1621 */
1622 error = ENOBUFS;
1623 inp_set_fc_state(inp, adv->code);
1624 }
1625
1626 VERIFY(inp->inp_sndinprog_cnt > 0);
1627 if ( --inp->inp_sndinprog_cnt == 0)
1628 inp->inp_flags &= ~(INP_FC_FEEDBACK);
1629
b0d623f7
A
1630 /* Synchronize PCB cached route */
1631 inp_route_copyin(inp, &ro);
1632
6d2010ae 1633abort:
91447636 1634 if (udp_dodisconnect) {
d1ecb069 1635 /* Always discard the cached route for unconnected socket */
39236c6e 1636 ROUTE_RELEASE(&inp->inp_route);
1c79356b 1637 in_pcbdisconnect(inp);
91447636 1638 inp->inp_laddr = origladdr; /* XXX rehash? */
39236c6e 1639 /* no reference needed */
316670eb 1640 inp->inp_last_outifp = origoutifp;
6d2010ae
A
1641 } else if (inp->inp_route.ro_rt != NULL) {
1642 struct rtentry *rt = inp->inp_route.ro_rt;
316670eb 1643 struct ifnet *outifp;
d1ecb069 1644
6d2010ae
A
1645 if (rt->rt_flags & (RTF_MULTICAST|RTF_BROADCAST))
1646 rt = NULL; /* unusable */
1647 /*
1648 * Always discard if it is a multicast or broadcast route.
1649 */
39236c6e
A
1650 if (rt == NULL)
1651 ROUTE_RELEASE(&inp->inp_route);
1652
6d2010ae 1653 /*
316670eb
A
1654 * If the destination route is unicast, update outifp with
1655 * that of the route interface used by IP.
6d2010ae 1656 */
316670eb 1657 if (rt != NULL && (outifp = rt->rt_ifp) != inp->inp_last_outifp)
39236c6e
A
1658 inp->inp_last_outifp = outifp; /* no reference needed */
1659 } else {
1660 ROUTE_RELEASE(&inp->inp_route);
d1ecb069 1661 }
55e303ae 1662
39236c6e
A
1663 /*
1664 * If output interface was cellular, and this socket is denied
1665 * access to it, generate an event.
1666 */
1667 if (error != 0 && (ipoa.ipoa_retflags & IPOARF_IFDENIED) &&
1668 (inp->inp_flags & INP_NO_IFT_CELLULAR))
1669 soevent(so, (SO_FILT_HINT_LOCKED|SO_FILT_HINT_IFDENIED));
1670
1c79356b 1671release:
39236c6e
A
1672 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
1673
6d2010ae
A
1674 if (m != NULL)
1675 m_freem(m);
39236c6e
A
1676
1677 if (outif != NULL)
1678 ifnet_release(outif);
1679
1c79356b
A
1680 return (error);
1681}
1682
b0d623f7 1683u_int32_t udp_sendspace = 9216; /* really max datagram size */
316670eb
A
1684/* 187 1K datagrams (approx 192 KB) */
1685u_int32_t udp_recvspace = 187 * (1024 +
1c79356b 1686#if INET6
39236c6e
A
1687 sizeof (struct sockaddr_in6)
1688#else /* !INET6 */
1689 sizeof (struct sockaddr_in)
1690#endif /* !INET6 */
1691 );
b0d623f7
A
1692
1693/* Check that the values of udp send and recv space do not exceed sb_max */
1694static int
39236c6e
A
1695sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2,
1696 struct sysctl_req *req)
1697{
1698#pragma unused(arg1, arg2)
b0d623f7
A
1699 u_int32_t new_value = 0, *space_p = NULL;
1700 int changed = 0, error = 0;
39236c6e 1701 u_quad_t sb_effective_max = (sb_max/(MSIZE+MCLBYTES)) * MCLBYTES;
b0d623f7
A
1702
1703 switch (oidp->oid_number) {
39236c6e
A
1704 case UDPCTL_RECVSPACE:
1705 space_p = &udp_recvspace;
1706 break;
1707 case UDPCTL_MAXDGRAM:
1708 space_p = &udp_sendspace;
1709 break;
1710 default:
1711 return EINVAL;
b0d623f7 1712 }
39236c6e
A
1713 error = sysctl_io_number(req, *space_p, sizeof (u_int32_t),
1714 &new_value, &changed);
b0d623f7 1715 if (changed) {
39236c6e 1716 if (new_value > 0 && new_value <= sb_effective_max)
b0d623f7 1717 *space_p = new_value;
39236c6e 1718 else
b0d623f7 1719 error = ERANGE;
b0d623f7 1720 }
39236c6e 1721 return (error);
b0d623f7
A
1722}
1723
39236c6e
A
1724SYSCTL_PROC(_net_inet_udp, UDPCTL_RECVSPACE, recvspace,
1725 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_recvspace, 0,
1726 &sysctl_udp_sospace, "IU", "Maximum incoming UDP datagram size");
b0d623f7 1727
39236c6e
A
1728SYSCTL_PROC(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram,
1729 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_sendspace, 0,
1730 &sysctl_udp_sospace, "IU", "Maximum outgoing UDP datagram size");
1c79356b
A
1731
1732static int
1733udp_abort(struct socket *so)
1734{
1735 struct inpcb *inp;
1c79356b
A
1736
1737 inp = sotoinpcb(so);
39236c6e
A
1738 if (inp == NULL) {
1739 panic("%s: so=%p null inp\n", __func__, so);
1740 /* NOTREACHED */
1741 }
1c79356b 1742 soisdisconnected(so);
1c79356b 1743 in_pcbdetach(inp);
39236c6e 1744 return (0);
1c79356b
A
1745}
1746
1747static int
39236c6e 1748udp_attach(struct socket *so, int proto, struct proc *p)
1c79356b 1749{
39236c6e 1750#pragma unused(proto)
1c79356b 1751 struct inpcb *inp;
91447636 1752 int error;
1c79356b 1753
9bccf70c 1754 inp = sotoinpcb(so);
39236c6e
A
1755 if (inp != NULL) {
1756 panic ("%s so=%p inp=%p\n", __func__, so, inp);
1757 /* NOTREACHED */
1758 }
1c79356b 1759 error = in_pcballoc(so, &udbinfo, p);
39236c6e
A
1760 if (error != 0)
1761 return (error);
91447636 1762 error = soreserve(so, udp_sendspace, udp_recvspace);
39236c6e
A
1763 if (error != 0)
1764 return (error);
1c79356b
A
1765 inp = (struct inpcb *)so->so_pcb;
1766 inp->inp_vflag |= INP_IPV4;
1767 inp->inp_ip_ttl = ip_defttl;
316670eb
A
1768 if (nstat_collect)
1769 nstat_udp_new_pcb(inp);
39236c6e 1770 return (0);
1c79356b
A
1771}
1772
1773static int
1774udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1775{
1776 struct inpcb *inp;
91447636 1777 int error;
1c79356b 1778
39236c6e
A
1779 if (nam->sa_family != 0 && nam->sa_family != AF_INET &&
1780 nam->sa_family != AF_INET6)
1781 return (EAFNOSUPPORT);
1782
1c79356b 1783 inp = sotoinpcb(so);
39236c6e
A
1784 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1785 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b 1786 error = in_pcbbind(inp, nam, p);
39236c6e 1787 return (error);
1c79356b
A
1788}
1789
1790static int
1791udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
1792{
1793 struct inpcb *inp;
91447636 1794 int error;
1c79356b
A
1795
1796 inp = sotoinpcb(so);
39236c6e
A
1797 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1798 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b 1799 if (inp->inp_faddr.s_addr != INADDR_ANY)
39236c6e
A
1800 return (EISCONN);
1801 error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL);
316670eb 1802 if (error == 0) {
1c79356b 1803 soisconnected(so);
316670eb
A
1804 if (inp->inp_flowhash == 0)
1805 inp->inp_flowhash = inp_calc_flowhash(inp);
1806 }
39236c6e
A
1807 return (error);
1808}
1809
1810int
1811udp_connectx_common(struct socket *so, int af,
1812 struct sockaddr_list **src_sl, struct sockaddr_list **dst_sl,
1813 struct proc *p, uint32_t ifscope, associd_t aid, connid_t *pcid,
1814 uint32_t flags, void *arg, uint32_t arglen)
1815{
1816#pragma unused(aid, flags, arg, arglen)
1817 struct sockaddr_entry *src_se = NULL, *dst_se = NULL;
1818 struct inpcb *inp = sotoinpcb(so);
1819 int error;
1820
1821 if (inp == NULL)
1822 return (EINVAL);
1823
1824 VERIFY(dst_sl != NULL);
1825
1826 /* select source (if specified) and destination addresses */
1827 error = in_selectaddrs(af, src_sl, &src_se, dst_sl, &dst_se);
1828 if (error != 0)
1829 return (error);
1830
1831 VERIFY(*dst_sl != NULL && dst_se != NULL);
1832 VERIFY(src_se == NULL || *src_sl != NULL);
1833 VERIFY(dst_se->se_addr->sa_family == af);
1834 VERIFY(src_se == NULL || src_se->se_addr->sa_family == af);
1835
1836 /* bind socket to the specified interface, if requested */
1837 if (ifscope != IFSCOPE_NONE &&
1838 (error = inp_bindif(inp, ifscope, NULL)) != 0)
1839 return (error);
1840
1841 /* if source address and/or port is specified, bind to it */
1842 if (src_se != NULL) {
1843 struct sockaddr *sa = src_se->se_addr;
1844 error = sobindlock(so, sa, 0); /* already locked */
1845 if (error != 0)
1846 return (error);
1847 }
1848
1849 switch (af) {
1850 case AF_INET:
1851 error = udp_connect(so, dst_se->se_addr, p);
1852 break;
1853#if INET6
1854 case AF_INET6:
1855 error = udp6_connect(so, dst_se->se_addr, p);
1856 break;
1857#endif /* INET6 */
1858 default:
1859 VERIFY(0);
1860 /* NOTREACHED */
1861 }
1862
1863 if (error == 0 && pcid != NULL)
1864 *pcid = 1; /* there is only 1 connection for a UDP */
1865
1866 return (error);
1867}
1868
1869static int
1870udp_connectx(struct socket *so, struct sockaddr_list **src_sl,
1871 struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope,
1872 associd_t aid, connid_t *pcid, uint32_t flags, void *arg,
1873 uint32_t arglen)
1874{
1875 return (udp_connectx_common(so, AF_INET, src_sl, dst_sl,
1876 p, ifscope, aid, pcid, flags, arg, arglen));
1c79356b
A
1877}
1878
1879static int
1880udp_detach(struct socket *so)
1881{
1882 struct inpcb *inp;
1c79356b
A
1883
1884 inp = sotoinpcb(so);
39236c6e
A
1885 if (inp == NULL) {
1886 panic("%s: so=%p null inp\n", __func__, so);
1887 /* NOTREACHED */
1888 }
1c79356b 1889 in_pcbdetach(inp);
91447636 1890 inp->inp_state = INPCB_STATE_DEAD;
39236c6e 1891 return (0);
1c79356b
A
1892}
1893
1894static int
1895udp_disconnect(struct socket *so)
1896{
1897 struct inpcb *inp;
1c79356b
A
1898
1899 inp = sotoinpcb(so);
39236c6e
A
1900 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1901 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b 1902 if (inp->inp_faddr.s_addr == INADDR_ANY)
39236c6e 1903 return (ENOTCONN);
1c79356b 1904
1c79356b 1905 in_pcbdisconnect(inp);
39236c6e 1906
316670eb
A
1907 /* reset flow controlled state, just in case */
1908 inp_reset_fc_state(inp);
1909
1c79356b 1910 inp->inp_laddr.s_addr = INADDR_ANY;
1c79356b 1911 so->so_state &= ~SS_ISCONNECTED; /* XXX */
316670eb 1912 inp->inp_last_outifp = NULL;
39236c6e 1913 return (0);
1c79356b
A
1914}
1915
1916static int
39236c6e 1917udp_disconnectx(struct socket *so, associd_t aid, connid_t cid)
1c79356b 1918{
39236c6e
A
1919#pragma unused(cid)
1920 if (aid != ASSOCID_ANY && aid != ASSOCID_ALL)
1921 return (EINVAL);
1922
1923 return (udp_disconnect(so));
1924}
1925
1926static int
1927udp_send(struct socket *so, int flags, struct mbuf *m,
1928 struct sockaddr *addr, struct mbuf *control, struct proc *p)
1929{
1930#pragma unused(flags)
1c79356b
A
1931 struct inpcb *inp;
1932
1933 inp = sotoinpcb(so);
39236c6e
A
1934 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) {
1935 if (m != NULL)
1936 m_freem(m);
1937 if (control != NULL)
1938 m_freem(control);
1939 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b 1940 }
39236c6e
A
1941
1942 return (udp_output(inp, m, addr, control, p));
1c79356b
A
1943}
1944
1945int
1946udp_shutdown(struct socket *so)
1947{
1948 struct inpcb *inp;
1949
1950 inp = sotoinpcb(so);
39236c6e
A
1951 if (inp == NULL)
1952 return (EINVAL);
1c79356b 1953 socantsendmore(so);
39236c6e 1954 return (0);
1c79356b
A
1955}
1956
91447636 1957int
b0d623f7 1958udp_lock(struct socket *so, int refcount, void *debug)
91447636 1959{
b0d623f7
A
1960 void *lr_saved;
1961
1962 if (debug == NULL)
1963 lr_saved = __builtin_return_address(0);
1964 else
1965 lr_saved = debug;
91447636 1966
39236c6e 1967 if (so->so_pcb != NULL) {
6d2010ae 1968 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
b0d623f7 1969 LCK_MTX_ASSERT_NOTOWNED);
6d2010ae 1970 lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
b0d623f7 1971 } else {
39236c6e 1972 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
b0d623f7
A
1973 so, lr_saved, solockhistory_nr(so));
1974 /* NOTREACHED */
91447636 1975 }
b0d623f7 1976 if (refcount)
91447636
A
1977 so->so_usecount++;
1978
b0d623f7 1979 so->lock_lr[so->next_lock_lr] = lr_saved;
0c530ab8 1980 so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
91447636
A
1981 return (0);
1982}
1983
1984int
b0d623f7 1985udp_unlock(struct socket *so, int refcount, void *debug)
91447636 1986{
b0d623f7 1987 void *lr_saved;
0c530ab8 1988
b0d623f7
A
1989 if (debug == NULL)
1990 lr_saved = __builtin_return_address(0);
1991 else
1992 lr_saved = debug;
0c530ab8 1993
b0d623f7 1994 if (refcount)
91447636 1995 so->so_usecount--;
b0d623f7
A
1996
1997 if (so->so_pcb == NULL) {
39236c6e 1998 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
b0d623f7
A
1999 so, lr_saved, solockhistory_nr(so));
2000 /* NOTREACHED */
2001 } else {
6d2010ae 2002 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
b0d623f7
A
2003 LCK_MTX_ASSERT_OWNED);
2004 so->unlock_lr[so->next_unlock_lr] = lr_saved;
0c530ab8 2005 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
6d2010ae 2006 lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
91447636 2007 }
91447636
A
2008 return (0);
2009}
2010
2011lck_mtx_t *
39236c6e 2012udp_getlock(struct socket *so, int locktype)
91447636 2013{
39236c6e 2014#pragma unused(locktype)
91447636
A
2015 struct inpcb *inp = sotoinpcb(so);
2016
39236c6e
A
2017 if (so->so_pcb == NULL) {
2018 panic("%s: so=%p NULL so_pcb lrh= %s\n", __func__,
2019 so, solockhistory_nr(so));
2020 /* NOTREACHED */
91447636 2021 }
39236c6e 2022 return (&inp->inpcb_mtx);
91447636
A
2023}
2024
39236c6e
A
2025/*
2026 * UDP garbage collector callback (inpcb_timer_func_t).
2027 *
2028 * Returns > 0 to keep timer active.
2029 */
2030static void
2031udp_gc(struct inpcbinfo *ipi)
91447636
A
2032{
2033 struct inpcb *inp, *inpnxt;
2034 struct socket *so;
91447636 2035
39236c6e 2036 if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) {
2d21ac55
A
2037 if (udp_gc_done == TRUE) {
2038 udp_gc_done = FALSE;
39236c6e
A
2039 /* couldn't get the lock, must lock next time */
2040 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2041 return;
2d21ac55 2042 }
39236c6e 2043 lck_rw_lock_exclusive(ipi->ipi_lock);
2d21ac55
A
2044 }
2045
2046 udp_gc_done = TRUE;
91447636
A
2047
2048 for (inp = udb.lh_first; inp != NULL; inp = inpnxt) {
2049 inpnxt = inp->inp_list.le_next;
91447636 2050
39236c6e
A
2051 /*
2052 * Skip unless it's STOPUSING; garbage collector will
2053 * be triggered by in_pcb_checkstate() upon setting
2054 * wantcnt to that value. If the PCB is already dead,
2055 * keep gc active to anticipate wantcnt changing.
2056 */
2057 if (inp->inp_wantcnt != WNT_STOPUSING)
91447636
A
2058 continue;
2059
39236c6e
A
2060 /*
2061 * Skip if busy, no hurry for cleanup. Keep gc active
2062 * and try the lock again during next round.
2063 */
2064 if (!lck_mtx_try_lock(&inp->inpcb_mtx)) {
2065 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
91447636 2066 continue;
39236c6e 2067 }
91447636 2068
39236c6e
A
2069 /*
2070 * Keep gc active unless usecount is 0.
2071 */
2072 so = inp->inp_socket;
b0d623f7
A
2073 if (so->so_usecount == 0) {
2074 if (inp->inp_state != INPCB_STATE_DEAD) {
2075#if INET6
39236c6e 2076 if (SOCK_CHECK_DOM(so, PF_INET6))
b0d623f7
A
2077 in6_pcbdetach(inp);
2078 else
2079#endif /* INET6 */
39236c6e 2080 in_pcbdetach(inp);
b0d623f7 2081 }
91447636 2082 in_pcbdispose(inp);
b0d623f7 2083 } else {
6d2010ae 2084 lck_mtx_unlock(&inp->inpcb_mtx);
39236c6e 2085 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
b0d623f7 2086 }
91447636 2087 }
39236c6e
A
2088 lck_rw_done(ipi->ipi_lock);
2089
2090 return;
91447636
A
2091}
2092
39236c6e
A
2093static int
2094udp_getstat SYSCTL_HANDLER_ARGS
91447636 2095{
39236c6e
A
2096#pragma unused(oidp, arg1, arg2)
2097 if (req->oldptr == USER_ADDR_NULL)
2098 req->oldlen = (size_t)sizeof (struct udpstat);
2099
2100 return (SYSCTL_OUT(req, &udpstat, MIN(sizeof (udpstat), req->oldlen)));
91447636
A
2101}
2102
2d21ac55
A
2103void
2104udp_in_cksum_stats(u_int32_t len)
2105{
39236c6e
A
2106 udpstat.udps_rcv_swcsum++;
2107 udpstat.udps_rcv_swcsum_bytes += len;
2d21ac55
A
2108}
2109
2110void
2111udp_out_cksum_stats(u_int32_t len)
2112{
39236c6e
A
2113 udpstat.udps_snd_swcsum++;
2114 udpstat.udps_snd_swcsum_bytes += len;
2115}
2116
2117#if INET6
2118void
2119udp_in6_cksum_stats(u_int32_t len)
2120{
2121 udpstat.udps_rcv6_swcsum++;
2122 udpstat.udps_rcv6_swcsum_bytes += len;
2123}
2124
2125void
2126udp_out6_cksum_stats(u_int32_t len)
2127{
2128 udpstat.udps_snd6_swcsum++;
2129 udpstat.udps_snd6_swcsum_bytes += len;
2130}
2131#endif /* INET6 */
2132
2133/*
2134 * Checksum extended UDP header and data.
2135 */
2136static int
2137udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
2138{
2139 struct ifnet *ifp = m->m_pkthdr.rcvif;
2140 struct ip *ip = mtod(m, struct ip *);
2141 struct ipovly *ipov = (struct ipovly *)ip;
2142
2143 if (uh->uh_sum == 0) {
2144 udpstat.udps_nosum++;
2145 return (0);
2146 }
2147
2148 if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
2149 (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
2150 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
2151 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
2152 uh->uh_sum = m->m_pkthdr.csum_rx_val;
2153 } else {
2154 uint16_t sum = m->m_pkthdr.csum_rx_val;
2155 uint16_t start = m->m_pkthdr.csum_rx_start;
2156
2157 /*
2158 * Perform 1's complement adjustment of octets
2159 * that got included/excluded in the hardware-
2160 * calculated checksum value. Ignore cases
2161 * where the value includes or excludes the
2162 * IP header span, as the sum for those octets
2163 * would already be 0xffff and thus no-op.
2164 */
2165 if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
2166 start != 0 && (off - start) != off) {
2167#if BYTE_ORDER != BIG_ENDIAN
2168 if (start < off) {
2169 HTONS(ip->ip_len);
2170 HTONS(ip->ip_off);
2171 }
2172#endif /* BYTE_ORDER != BIG_ENDIAN */
2173 /* callee folds in sum */
2174 sum = m_adj_sum16(m, start, off, sum);
2175#if BYTE_ORDER != BIG_ENDIAN
2176 if (start < off) {
2177 NTOHS(ip->ip_off);
2178 NTOHS(ip->ip_len);
2179 }
2180#endif /* BYTE_ORDER != BIG_ENDIAN */
2181 }
2182
2183 /* callee folds in sum */
2184 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
2185 ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP));
2186 }
2187 uh->uh_sum ^= 0xffff;
2188 } else {
2189 uint16_t ip_sum;
2190 char b[9];
2191
2192 bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1));
2193 bzero(ipov->ih_x1, sizeof (ipov->ih_x1));
2194 ip_sum = ipov->ih_len;
2195 ipov->ih_len = uh->uh_ulen;
2196 uh->uh_sum = in_cksum(m, ulen + sizeof (struct ip));
2197 bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1));
2198 ipov->ih_len = ip_sum;
2199
2200 udp_in_cksum_stats(ulen);
2201 }
2202
2203 if (uh->uh_sum != 0) {
2204 udpstat.udps_badsum++;
2205 IF_UDP_STATINC(ifp, badchksum);
2206 return (-1);
2207 }
2208
2209 return (0);
2d21ac55 2210}