]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/udp_usrreq.c
1b2bd8b1fec4cf81ef82f6c647b3b0745d33fea1
[apple/xnu.git] / bsd / netinet / udp_usrreq.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * 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
61 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
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>
69 #include <sys/domain.h>
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>
75 #include <sys/mcache.h>
76 #include <net/ntstat.h>
77
78 #include <kern/zalloc.h>
79 #include <mach/boolean.h>
80
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
84 #include <net/dlil.h>
85
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #if INET6
90 #include <netinet/ip6.h>
91 #endif /* INET6 */
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #if INET6
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/udp6_var.h>
99 #endif /* INET6 */
100 #include <netinet/ip_icmp.h>
101 #include <netinet/icmp_var.h>
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>
108 #include <netinet6/esp.h>
109 extern int ipsec_bypass;
110 extern int esp_udp_encap_port;
111 #endif /* IPSEC */
112
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)
119
120 /*
121 * UDP protocol implementation.
122 * Per RFC 768, August, 1980.
123 */
124 #ifndef COMPAT_42
125 static int udpcksum = 1;
126 #else
127 static int udpcksum = 0; /* XXX */
128 #endif
129 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum,
130 CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, "");
131
132 int udp_log_in_vain = 0;
133 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED,
134 &udp_log_in_vain, 0, "Log all incoming UDP packets");
135
136 static int blackhole = 0;
137 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED,
138 &blackhole, 0, "Do not send port unreachables for refused connects");
139
140 struct inpcbhead udb; /* from udp_var.h */
141 #define udb6 udb /* for KAME src sync over BSD*'s */
142 struct inpcbinfo udbinfo;
143
144 #ifndef UDBHASHSIZE
145 #define UDBHASHSIZE 16
146 #endif
147
148 /* Garbage collection performed during most recent udp_gc() run */
149 static boolean_t udp_gc_done = FALSE;
150
151 #if IPFIREWALL
152 extern int fw_verbose;
153 extern void ipfwsyslog( int level, const char *format,...);
154 extern 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 } \
165 }
166 #else /* !IPFIREWALL */
167 #define log_in_vain_log( a ) { log a; }
168 #endif /* !IPFIREWALL */
169
170 static int udp_getstat SYSCTL_HANDLER_ARGS;
171 struct udpstat udpstat; /* from udp_var.h */
172 SYSCTL_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
176 SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount,
177 CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0,
178 "Number of active PCBs");
179
180 __private_extern__ int udp_use_randomport = 1;
181 SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports,
182 CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0,
183 "Randomize UDP port numbers");
184
185 #if INET6
186 struct udp_in6 {
187 struct sockaddr_in6 uin6_sin;
188 u_char uin6_init_done : 1;
189 };
190 struct udp_ip6 {
191 struct ip6_hdr uip6_ip6;
192 u_char uip6_init_done : 1;
193 };
194
195 static int udp_abort(struct socket *);
196 static int udp_attach(struct socket *, int, struct proc *);
197 static int udp_bind(struct socket *, struct sockaddr *, struct proc *);
198 static int udp_connect(struct socket *, struct sockaddr *, struct proc *);
199 static 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);
202 static int udp_detach(struct socket *);
203 static int udp_disconnect(struct socket *);
204 static int udp_disconnectx(struct socket *, associd_t, connid_t);
205 static int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *,
206 struct mbuf *, struct proc *);
207 static 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 */
210 static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
211 struct sockaddr_in *, struct ifnet *);
212 #endif /* !INET6 */
213 static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int);
214 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
215 struct mbuf *, struct proc *);
216 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
217 static void udp_gc(struct inpcbinfo *);
218
219 struct 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 };
236
237 void
238 udp_init(struct protosw *pp, struct domain *dp)
239 {
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;
250
251 LIST_INIT(&udb);
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;
261 /*
262 * allocate lock group attribute and group for udp pcb mutexes
263 */
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 }
273
274 udbinfo.ipi_gc = udp_gc;
275 in_pcbinfo_attach(&udbinfo);
276 }
277
278 void
279 udp_input(struct mbuf *m, int iphlen)
280 {
281 struct ip *ip;
282 struct udphdr *uh;
283 struct inpcb *inp;
284 struct mbuf *opts = NULL;
285 int len, isbroadcast;
286 struct ip save_ip;
287 struct sockaddr *append_sa;
288 struct inpcbinfo *pcbinfo = &udbinfo;
289 struct sockaddr_in udp_in;
290 struct ip_moptions *imo = NULL;
291 int foundmembership = 0, ret = 0;
292 #if INET6
293 struct udp_in6 udp_in6;
294 struct udp_ip6 udp_ip6;
295 #endif /* INET6 */
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 */
308
309 udpstat.udps_ipackets++;
310
311 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
312
313 /* Expect 32-bit aligned data pointer on strict-align platforms */
314 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
315
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);
324 iphlen = sizeof (struct ip);
325 }
326
327 /*
328 * Get IP and UDP header together in first mbuf.
329 */
330 ip = mtod(m, struct ip *);
331 if (m->m_len < iphlen + sizeof (struct udphdr)) {
332 m = m_pullup(m, iphlen + sizeof (struct udphdr));
333 if (m == NULL) {
334 udpstat.udps_hdrops++;
335 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
336 0,0,0,0,0);
337 return;
338 }
339 ip = mtod(m, struct ip *);
340 }
341 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
342
343 /* destination port of 0 is illegal, based on RFC768. */
344 if (uh->uh_dport == 0) {
345 IF_UDP_STATINC(ifp, port0);
346 goto bad;
347 }
348
349 KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
350 ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
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) {
358 if (len > ip->ip_len || len < sizeof (struct udphdr)) {
359 udpstat.udps_badlen++;
360 IF_UDP_STATINC(ifp, badlength);
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 */
375 if (udp_input_checksum(m, uh, iphlen, len))
376 goto bad;
377
378 isbroadcast = in_broadcast(ip->ip_dst, ifp);
379
380 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) {
381 int reuse_sock = 0, mcast_delivered = 0;
382
383 lck_rw_lock_shared(pcbinfo->ipi_lock);
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 */
409 #if INET6
410 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
411 #endif /* INET6 */
412 LIST_FOREACH(inp, &udb, inp_list) {
413 #if IPSEC
414 int skipit;
415 #endif /* IPSEC */
416
417 if (inp->inp_socket == NULL)
418 continue;
419 if (inp != sotoinpcb(inp->inp_socket)) {
420 panic("%s: bad so back ptr inp=%p\n",
421 __func__, inp);
422 /* NOTREACHED */
423 }
424 #if INET6
425 if ((inp->inp_vflag & INP_IPV4) == 0)
426 continue;
427 #endif /* INET6 */
428 if (inp_restricted(inp, ifp))
429 continue;
430
431 if (IFNET_IS_CELLULAR(ifp) &&
432 (inp->inp_flags & INP_NO_IFT_CELLULAR))
433 continue;
434
435 if ((inp->inp_moptions == NULL) &&
436 (ntohl(ip->ip_dst.s_addr) !=
437 INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0))
438 continue;
439
440 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
441 WNT_STOPUSING)
442 continue;
443
444 udp_lock(inp->inp_socket, 1, 0);
445
446 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
447 WNT_STOPUSING) {
448 udp_unlock(inp->inp_socket, 1, 0);
449 continue;
450 }
451
452 if (inp->inp_lport != uh->uh_dport) {
453 udp_unlock(inp->inp_socket, 1, 0);
454 continue;
455 }
456 if (inp->inp_laddr.s_addr != INADDR_ANY) {
457 if (inp->inp_laddr.s_addr !=
458 ip->ip_dst.s_addr) {
459 udp_unlock(inp->inp_socket, 1, 0);
460 continue;
461 }
462 }
463 if (inp->inp_faddr.s_addr != INADDR_ANY) {
464 if (inp->inp_faddr.s_addr !=
465 ip->ip_src.s_addr ||
466 inp->inp_fport != uh->uh_sport) {
467 udp_unlock(inp->inp_socket, 1, 0);
468 continue;
469 }
470 }
471
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) {
478 udp_unlock(inp->inp_socket, 1, 0);
479 continue;
480 }
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;
503 }
504
505 reuse_sock = (inp->inp_socket->so_options &
506 (SO_REUSEPORT|SO_REUSEADDR));
507
508 #if IPSEC
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)
518 #endif /*IPSEC*/
519 {
520 struct mbuf *n = NULL;
521
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;
536 }
537 udp_unlock(inp->inp_socket, 1, 0);
538
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
544 * port. It assumes that an application will never
545 * clear these options after setting them.
546 */
547 if (reuse_sock == 0 || m == NULL)
548 break;
549
550 /*
551 * Expect 32-bit aligned data pointer on strict-align
552 * platforms.
553 */
554 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
555 /*
556 * Recompute IP and UDP header pointers for new mbuf
557 */
558 ip = mtod(m, struct ip *);
559 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
560 }
561 lck_rw_done(pcbinfo->ipi_lock);
562
563 if (mcast_delivered == 0) {
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++;
570 IF_UDP_STATINC(ifp, port_unreach);
571 goto bad;
572 }
573
574 /* free the extra copy of mbuf or skipped by IPSec */
575 if (m != NULL)
576 m_freem(m);
577 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
578 return;
579 }
580
581 #if IPSEC
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 &&
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) {
597 udpstat.udps_hdrops++;
598 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
599 0,0,0,0,0);
600 return;
601 }
602 /*
603 * Expect 32-bit aligned data pointer on strict-align
604 * platforms.
605 */
606 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
607
608 ip = mtod(m, struct ip *);
609 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
610 }
611 /* Check for NAT keepalive packet */
612 if (payload_len == 1 && *(u_int8_t*)
613 ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) {
614 m_freem(m);
615 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
616 0,0,0,0,0);
617 return;
618 } else if (payload_len == 4 && *(u_int32_t*)(void *)
619 ((caddr_t)uh + sizeof (struct udphdr)) != 0) {
620 /* UDP encapsulated IPSec packet to pass through NAT */
621 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
622 0,0,0,0,0);
623 /* preserve the udp header */
624 esp4_input(m, iphlen + sizeof (struct udphdr));
625 return;
626 }
627 }
628 #endif /* IPSEC */
629
630 /*
631 * Locate pcb for datagram.
632 */
633 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
634 ip->ip_dst, uh->uh_dport, 1, ifp);
635 if (inp == NULL) {
636 IF_UDP_STATINC(ifp, port_unreach);
637
638 if (udp_log_in_vain) {
639 char buf[MAX_IPv4_STR_LEN];
640 char buf2[MAX_IPv4_STR_LEN];
641
642 /* check src and dst address */
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)),
649 ntohs(uh->uh_sport));
650 } else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
651 ip->ip_dst.s_addr != ip->ip_src.s_addr) {
652 log_in_vain_log((LOG_INFO,
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 }
660 }
661 udpstat.udps_noport++;
662 if (m->m_flags & (M_BCAST | M_MCAST)) {
663 udpstat.udps_noportbcast++;
664 goto bad;
665 }
666 #if ICMP_BANDLIM
667 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
668 goto bad;
669 #endif /* ICMP_BANDLIM */
670 if (blackhole)
671 if (ifp && ifp->if_type != IFT_LOOP)
672 goto bad;
673 *ip = save_ip;
674 ip->ip_len += iphlen;
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 }
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);
683 IF_UDP_STATINC(ifp, cleanup);
684 goto bad;
685 }
686 #if IPSEC
687 if (ipsec_bypass == 0 && inp != NULL) {
688 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
689 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
690 udp_unlock(inp->inp_socket, 1, 0);
691 IF_UDP_STATINC(ifp, badipsec);
692 goto bad;
693 }
694 }
695 #endif /* IPSEC */
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;
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) {
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;
713 ret = ip6_savecontrol(inp, m, &opts);
714 inp->inp_flags = savedflags;
715 } else
716 #endif /* INET6 */
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 }
724 }
725 m_adj(m, iphlen + sizeof (struct udphdr));
726
727 KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
728 save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
729
730 #if INET6
731 if (inp->inp_vflag & INP_IPV6) {
732 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
733 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
734 } else
735 #endif /* INET6 */
736 {
737 append_sa = (struct sockaddr *)&udp_in;
738 }
739 if (nstat_collect) {
740 INP_ADD_STAT(inp, cell, wifi, rxpackets, 1);
741 INP_ADD_STAT(inp, cell, wifi, rxbytes, m->m_pkthdr.len);
742 }
743 so_recv_data_stat(inp->inp_socket, m, 0);
744 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa,
745 m, opts, NULL) == 0) {
746 udpstat.udps_fullsock++;
747 } else {
748 sorwakeup(inp->inp_socket);
749 }
750 udp_unlock(inp->inp_socket, 1, 0);
751 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
752 return;
753 bad:
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
761 static void
762 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
763 {
764 bzero(ip6, sizeof (*ip6));
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;
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 }
778 }
779 #endif /* INET6 */
780
781 /*
782 * subroutine of udp_input(), mainly for source code readability.
783 */
784 static void
785 #if INET6
786 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
787 struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6,
788 struct udp_ip6 *pudp_ip6, struct ifnet *ifp)
789 #else /* !INET6 */
790 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
791 struct sockaddr_in *pudp_in, struct ifnet *ifp)
792 #endif /* !INET6 */
793 {
794 struct sockaddr *append_sa;
795 struct mbuf *opts = 0;
796 boolean_t cell = IFNET_IS_CELLULAR(ifp);
797 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
798 int ret = 0;
799
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 }
805 #endif /* CONFIG_MACF_NET */
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) {
809 #if INET6
810 if (last->inp_vflag & INP_IPV6) {
811 int savedflags;
812
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;
816 }
817 savedflags = last->inp_flags;
818 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
819 ret = ip6_savecontrol(last, n, &opts);
820 if (ret != 0) {
821 last->inp_flags = savedflags;
822 goto error;
823 }
824 last->inp_flags = savedflags;
825 } else
826 #endif /* INET6 */
827 {
828 ret = ip_savecontrol(last, &opts, ip, n);
829 if (ret != 0) {
830 goto error;
831 }
832 }
833 }
834 #if INET6
835 if (last->inp_vflag & INP_IPV6) {
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;
839 }
840 append_sa = (struct sockaddr *)&pudp_in6->uin6_sin;
841 } else
842 #endif /* INET6 */
843 append_sa = (struct sockaddr *)pudp_in;
844 if (nstat_collect) {
845 INP_ADD_STAT(last, cell, wifi, rxpackets, 1);
846 INP_ADD_STAT(last, cell, wifi, rxbytes, n->m_pkthdr.len);
847 }
848 so_recv_data_stat(last->inp_socket, n, 0);
849 m_adj(n, off);
850 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa,
851 n, opts, NULL) == 0) {
852 udpstat.udps_fullsock++;
853 } else {
854 sorwakeup(last->inp_socket);
855 }
856 return;
857 error:
858 m_freem(n);
859 m_freem(opts);
860 return;
861 }
862
863 /*
864 * Notify a udp user of an asynchronous error;
865 * just wake up so that he can collect error status.
866 */
867 void
868 udp_notify(struct inpcb *inp, int errno)
869 {
870 inp->inp_socket->so_error = errno;
871 sorwakeup(inp->inp_socket);
872 sowwakeup(inp->inp_socket);
873 }
874
875 void
876 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
877 {
878 struct ip *ip = vip;
879 void (*notify)(struct inpcb *, int) = udp_notify;
880 struct in_addr faddr;
881 struct inpcb *inp;
882
883 faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
884 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
885 return;
886
887 if (PRC_IS_REDIRECT(cmd)) {
888 ip = 0;
889 notify = in_rtchange;
890 } else if (cmd == PRC_HOSTDEAD) {
891 ip = 0;
892 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
893 return;
894 }
895 if (ip) {
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);
901 if (inp != NULL && inp->inp_socket != NULL) {
902 udp_lock(inp->inp_socket, 1, 0);
903 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
904 WNT_STOPUSING) {
905 udp_unlock(inp->inp_socket, 1, 0);
906 return;
907 }
908 (*notify)(inp, inetctlerrmap[cmd]);
909 udp_unlock(inp->inp_socket, 1, 0);
910 }
911 } else {
912 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
913 }
914 }
915
916 int
917 udp_ctloutput(struct socket *so, struct sockopt *sopt)
918 {
919 int error, optval;
920 struct inpcb *inp;
921
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))
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
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
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
981 static int
982 udp_pcblist SYSCTL_HANDLER_ARGS
983 {
984 #pragma unused(oidp, arg1, arg2)
985 int error, i, n;
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 */
994 lck_rw_lock_exclusive(udbinfo.ipi_lock);
995 if (req->oldptr == USER_ADDR_NULL) {
996 n = udbinfo.ipi_count;
997 req->oldidx = 2 * (sizeof (xig))
998 + (n + n/8) * sizeof (struct xinpcb);
999 lck_rw_done(udbinfo.ipi_lock);
1000 return (0);
1001 }
1002
1003 if (req->newptr != USER_ADDR_NULL) {
1004 lck_rw_done(udbinfo.ipi_lock);
1005 return (EPERM);
1006 }
1007
1008 /*
1009 * OK, now we're committed to doing something.
1010 */
1011 gencnt = udbinfo.ipi_gencnt;
1012 n = udbinfo.ipi_count;
1013
1014 bzero(&xig, sizeof (xig));
1015 xig.xig_len = sizeof (xig);
1016 xig.xig_count = n;
1017 xig.xig_gen = gencnt;
1018 xig.xig_sogen = so_gencnt;
1019 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1020 if (error) {
1021 lck_rw_done(udbinfo.ipi_lock);
1022 return (error);
1023 }
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);
1033 if (inp_list == 0) {
1034 lck_rw_done(udbinfo.ipi_lock);
1035 return (ENOMEM);
1036 }
1037
1038 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
1039 inp = LIST_NEXT(inp, inp_list)) {
1040 if (inp->inp_gencnt <= gencnt &&
1041 inp->inp_state != INPCB_STATE_DEAD)
1042 inp_list[i++] = inp;
1043 }
1044 n = i;
1045
1046 error = 0;
1047 for (i = 0; i < n; i++) {
1048 inp = inp_list[i];
1049 if (inp->inp_gencnt <= gencnt &&
1050 inp->inp_state != INPCB_STATE_DEAD) {
1051 struct xinpcb xi;
1052
1053 bzero(&xi, sizeof (xi));
1054 xi.xi_len = sizeof (xi);
1055 /* XXX should avoid extra copy */
1056 inpcb_to_compat(inp, &xi.xi_inp);
1057 if (inp->inp_socket)
1058 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1059 error = SYSCTL_OUT(req, &xi, sizeof (xi));
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 */
1070 bzero(&xig, sizeof (xig));
1071 xig.xig_len = sizeof (xig);
1072 xig.xig_gen = udbinfo.ipi_gencnt;
1073 xig.xig_sogen = so_gencnt;
1074 xig.xig_count = udbinfo.ipi_count;
1075 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1076 }
1077 FREE(inp_list, M_TEMP);
1078 lck_rw_done(udbinfo.ipi_lock);
1079 return (error);
1080 }
1081
1082 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
1083 CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist,
1084 "S,xinpcb", "List of active UDP sockets");
1085
1086
1087 static int
1088 udp_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 */
1100 lck_rw_lock_shared(udbinfo.ipi_lock);
1101 if (req->oldptr == USER_ADDR_NULL) {
1102 n = udbinfo.ipi_count;
1103 req->oldidx =
1104 2 * (sizeof (xig)) + (n + n/8) * sizeof (struct xinpcb64);
1105 lck_rw_done(udbinfo.ipi_lock);
1106 return (0);
1107 }
1108
1109 if (req->newptr != USER_ADDR_NULL) {
1110 lck_rw_done(udbinfo.ipi_lock);
1111 return (EPERM);
1112 }
1113
1114 /*
1115 * OK, now we're committed to doing something.
1116 */
1117 gencnt = udbinfo.ipi_gencnt;
1118 n = udbinfo.ipi_count;
1119
1120 bzero(&xig, sizeof (xig));
1121 xig.xig_len = sizeof (xig);
1122 xig.xig_count = n;
1123 xig.xig_gen = gencnt;
1124 xig.xig_sogen = so_gencnt;
1125 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1126 if (error) {
1127 lck_rw_done(udbinfo.ipi_lock);
1128 return (error);
1129 }
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);
1139 if (inp_list == 0) {
1140 lck_rw_done(udbinfo.ipi_lock);
1141 return (ENOMEM);
1142 }
1143
1144 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
1145 inp = LIST_NEXT(inp, inp_list)) {
1146 if (inp->inp_gencnt <= gencnt &&
1147 inp->inp_state != INPCB_STATE_DEAD)
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];
1155 if (inp->inp_gencnt <= gencnt &&
1156 inp->inp_state != INPCB_STATE_DEAD) {
1157 struct xinpcb64 xi;
1158
1159 bzero(&xi, sizeof (xi));
1160 xi.xi_len = sizeof (xi);
1161 inpcb_to_xinpcb64(inp, &xi);
1162 if (inp->inp_socket)
1163 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1164 error = SYSCTL_OUT(req, &xi, sizeof (xi));
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 */
1175 bzero(&xig, sizeof (xig));
1176 xig.xig_len = sizeof (xig);
1177 xig.xig_gen = udbinfo.ipi_gencnt;
1178 xig.xig_sogen = so_gencnt;
1179 xig.xig_count = udbinfo.ipi_count;
1180 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1181 }
1182 FREE(inp_list, M_TEMP);
1183 lck_rw_done(udbinfo.ipi_lock);
1184 return (error);
1185 }
1186
1187 SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist64,
1188 CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist64,
1189 "S,xinpcb64", "List of active UDP sockets");
1190
1191
1192 static int
1193 udp_pcblist_n SYSCTL_HANDLER_ARGS
1194 {
1195 #pragma unused(oidp, arg1, arg2)
1196 return (get_pcblist_n(IPPROTO_UDP, req, &udbinfo));
1197 }
1198
1199 SYSCTL_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");
1202
1203 __private_extern__ void
1204 udp_get_ports_used(uint32_t ifindex, int protocol, uint32_t wildcardok,
1205 bitstr_t *bitfield)
1206 {
1207 inpcb_get_ports_used(ifindex, protocol, wildcardok, bitfield, &udbinfo);
1208 }
1209
1210 __private_extern__ uint32_t
1211 udp_count_opportunistic(unsigned int ifindex, u_int32_t flags)
1212 {
1213 return (inpcb_count_opportunistic(ifindex, &udbinfo, flags));
1214 }
1215
1216 __private_extern__ uint32_t
1217 udp_find_anypcb_byaddr(struct ifaddr *ifa)
1218 {
1219 return (inpcb_find_anypcb_byaddr(ifa, &udbinfo));
1220 }
1221
1222 static int
1223 udp_check_pktinfo(struct mbuf *control, struct ifnet **outif,
1224 struct in_addr *laddr)
1225 {
1226 struct cmsghdr *cm = 0;
1227 struct in_pktinfo *pktinfo;
1228 struct ifnet *ifp;
1229
1230 if (outif != NULL)
1231 *outif = NULL;
1232
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
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)
1247 return (EINVAL);
1248
1249 if (cm->cmsg_level != IPPROTO_IP || cm->cmsg_type != IP_PKTINFO)
1250 continue;
1251
1252 if (cm->cmsg_len != CMSG_LEN(sizeof (struct in_pktinfo)))
1253 return (EINVAL);
1254
1255 pktinfo = (struct in_pktinfo *)(void *)CMSG_DATA(cm);
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
1265 /*
1266 * If ipi_ifindex is specified it takes precedence
1267 * over ipi_spec_dst.
1268 */
1269 if (pktinfo->ipi_ifindex) {
1270 ifp = ifindex2ifnet[pktinfo->ipi_ifindex];
1271 if (ifp == NULL) {
1272 ifnet_head_done();
1273 return (ENXIO);
1274 }
1275 if (outif != NULL) {
1276 ifnet_reference(ifp);
1277 *outif = ifp;
1278 }
1279 ifnet_head_done();
1280 laddr->s_addr = INADDR_ANY;
1281 break;
1282 }
1283
1284 ifnet_head_done();
1285
1286 /*
1287 * Use the provided ipi_spec_dst address for temp
1288 * source address.
1289 */
1290 *laddr = pktinfo->ipi_spec_dst;
1291 break;
1292 }
1293 return (0);
1294 }
1295
1296 static int
1297 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1298 struct mbuf *control, struct proc *p)
1299 {
1300 struct udpiphdr *ui;
1301 int len = m->m_pkthdr.len;
1302 struct sockaddr_in *sin;
1303 struct in_addr origladdr, laddr, faddr, pi_laddr;
1304 u_short lport, fport;
1305 int error = 0, udp_dodisconnect = 0, pktinfo = 0;
1306 struct socket *so = inp->inp_socket;
1307 int soopts = 0;
1308 struct mbuf *inpopts;
1309 struct ip_moptions *mopts;
1310 struct route ro;
1311 struct ip_out_args ipoa =
1312 { IFSCOPE_NONE, { 0 }, IPOAF_SELECT_SRCIF, 0 };
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;
1321 pi_laddr.s_addr = INADDR_ANY;
1322
1323 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
1324
1325 lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1326 if (control != NULL) {
1327 msc = mbuf_service_class_from_control(control);
1328 VERIFY(outif == NULL);
1329 error = udp_check_pktinfo(control, &outif, &pi_laddr);
1330 m_freem(control);
1331 control = NULL;
1332 if (error)
1333 goto release;
1334 pktinfo++;
1335 if (outif != NULL)
1336 ipoa.ipoa_boundif = outif->if_index;
1337 }
1338
1339 KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
1340 inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
1341 (htons((u_short)len + sizeof (struct udphdr))));
1342
1343 if (len + sizeof (struct udpiphdr) > IP_MAXPACKET) {
1344 error = EMSGSIZE;
1345 goto release;
1346 }
1347
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 }
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 */
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);
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;
1372 soopts |= IP_OUTARGS;
1373
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.
1378 */
1379 if (ROUTE_UNUSABLE(&inp->inp_route)) {
1380 struct in_ifaddr *ia = NULL;
1381
1382 ROUTE_RELEASE(&inp->inp_route);
1383
1384 /* src address is gone? */
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) ||
1388 (so->so_state & SS_ISCONNECTED)) {
1389 /*
1390 * Rdar://5448998
1391 * If the source address is gone, return an
1392 * error if:
1393 * - the source was specified
1394 * - the socket was already connected
1395 */
1396 soevent(so, (SO_FILT_HINT_LOCKED |
1397 SO_FILT_HINT_NOSRCADDR));
1398 error = EADDRNOTAVAIL;
1399 goto release;
1400 } else {
1401 /* new src will be set later */
1402 inp->inp_laddr.s_addr = INADDR_ANY;
1403 inp->inp_last_outifp = NULL;
1404 }
1405 }
1406 if (ia != NULL)
1407 IFA_REMREF(&ia->ia_ifa);
1408 }
1409
1410 origoutifp = inp->inp_last_outifp;
1411
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.
1416 */
1417 if (pi_laddr.s_addr != INADDR_ANY ||
1418 (ipoa.ipoa_boundif != IFSCOPE_NONE && pktinfo)) {
1419 /* temp src address for this datagram only */
1420 laddr = pi_laddr;
1421 origladdr.s_addr = INADDR_ANY;
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;
1426 } else {
1427 origladdr = laddr = inp->inp_laddr;
1428 }
1429
1430 origoutifp = inp->inp_last_outifp;
1431 faddr = inp->inp_faddr;
1432 lport = inp->inp_lport;
1433 fport = inp->inp_fport;
1434
1435 if (addr) {
1436 sin = (struct sockaddr_in *)(void *)addr;
1437 if (faddr.s_addr != INADDR_ANY) {
1438 error = EISCONN;
1439 goto release;
1440 }
1441 if (lport == 0) {
1442 /*
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.
1447 */
1448 /* if we have a source address specified, use that */
1449 if (pi_laddr.s_addr != INADDR_ANY)
1450 inp->inp_laddr = pi_laddr;
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)
1459 goto release;
1460
1461 laddr = inp->inp_laddr;
1462 lport = inp->inp_lport;
1463 faddr = inp->inp_faddr;
1464 fport = inp->inp_fport;
1465 udp_dodisconnect = 1;
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;
1470 }
1471 else {
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.
1481 */
1482 if (laddr.s_addr == INADDR_ANY) {
1483 if ((error = in_pcbladdr(inp, addr, &laddr,
1484 ipoa.ipoa_boundif, &outif)) != 0)
1485 goto release;
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;
1496 }
1497
1498 faddr = sin->sin_addr;
1499 fport = sin->sin_port;
1500 }
1501 } else {
1502 if (faddr.s_addr == INADDR_ANY) {
1503 error = ENOTCONN;
1504 goto release;
1505 }
1506 }
1507
1508 #if CONFIG_MACF_NET
1509 mac_mbuf_label_associate_inpcb(inp, m);
1510 #endif /* CONFIG_MACF_NET */
1511
1512 if (inp->inp_flowhash == 0)
1513 inp->inp_flowhash = inp_calc_flowhash(inp);
1514
1515 /*
1516 * Calculate data length and get a mbuf
1517 * for UDP and IP headers.
1518 */
1519 M_PREPEND(m, sizeof (struct udpiphdr), M_DONTWAIT);
1520 if (m == 0) {
1521 error = ENOBUFS;
1522 goto abort;
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 *);
1530 bzero(ui->ui_x1, sizeof (ui->ui_x1)); /* XXX still needed? */
1531 ui->ui_pr = IPPROTO_UDP;
1532 ui->ui_src = laddr;
1533 ui->ui_dst = faddr;
1534 ui->ui_sport = lport;
1535 ui->ui_dport = fport;
1536 ui->ui_ulen = htons((u_short)len + sizeof (struct udphdr));
1537
1538 /*
1539 * Set up checksum and output datagram.
1540 */
1541 if (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM)) {
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));
1544 m->m_pkthdr.csum_flags = CSUM_UDP;
1545 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1546 } else {
1547 ui->ui_sum = 0;
1548 }
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
1557 #if IPSEC
1558 if (ipsec_bypass == 0 && ipsec_setsocket(m, inp->inp_socket) != 0) {
1559 error = ENOBUFS;
1560 goto abort;
1561 }
1562 #endif /* IPSEC */
1563
1564 inpopts = inp->inp_options;
1565 soopts |= (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1566 mopts = inp->inp_moptions;
1567 if (mopts != NULL) {
1568 IMO_LOCK(mopts);
1569 IMO_ADDREF_LOCKED(mopts);
1570 if (IN_MULTICAST(ntohl(ui->ui_dst.s_addr)) &&
1571 mopts->imo_multicast_ifp != NULL) {
1572 /* no reference needed */
1573 inp->inp_last_outifp = mopts->imo_multicast_ifp;
1574 }
1575 IMO_UNLOCK(mopts);
1576 }
1577
1578 /* Copy the cached route and take an extra reference */
1579 inp_route_copyout(inp, &ro);
1580
1581 set_packet_service_class(m, so, msc, 0);
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);
1586 if (flowadv)
1587 m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
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++;
1596
1597 socket_unlock(so, 0);
1598 error = ip_output(m, inpopts, &ro, soopts, mopts, &ipoa);
1599 m = NULL;
1600 socket_lock(so, 0);
1601 if (mopts != NULL)
1602 IMO_REMREF(mopts);
1603
1604 if (error == 0 && nstat_collect) {
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);
1615 }
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
1630 /* Synchronize PCB cached route */
1631 inp_route_copyin(inp, &ro);
1632
1633 abort:
1634 if (udp_dodisconnect) {
1635 /* Always discard the cached route for unconnected socket */
1636 ROUTE_RELEASE(&inp->inp_route);
1637 in_pcbdisconnect(inp);
1638 inp->inp_laddr = origladdr; /* XXX rehash? */
1639 /* no reference needed */
1640 inp->inp_last_outifp = origoutifp;
1641 } else if (inp->inp_route.ro_rt != NULL) {
1642 struct rtentry *rt = inp->inp_route.ro_rt;
1643 struct ifnet *outifp;
1644
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 */
1650 if (rt == NULL)
1651 ROUTE_RELEASE(&inp->inp_route);
1652
1653 /*
1654 * If the destination route is unicast, update outifp with
1655 * that of the route interface used by IP.
1656 */
1657 if (rt != NULL && (outifp = rt->rt_ifp) != inp->inp_last_outifp)
1658 inp->inp_last_outifp = outifp; /* no reference needed */
1659 } else {
1660 ROUTE_RELEASE(&inp->inp_route);
1661 }
1662
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
1671 release:
1672 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
1673
1674 if (m != NULL)
1675 m_freem(m);
1676
1677 if (outif != NULL)
1678 ifnet_release(outif);
1679
1680 return (error);
1681 }
1682
1683 u_int32_t udp_sendspace = 9216; /* really max datagram size */
1684 /* 187 1K datagrams (approx 192 KB) */
1685 u_int32_t udp_recvspace = 187 * (1024 +
1686 #if INET6
1687 sizeof (struct sockaddr_in6)
1688 #else /* !INET6 */
1689 sizeof (struct sockaddr_in)
1690 #endif /* !INET6 */
1691 );
1692
1693 /* Check that the values of udp send and recv space do not exceed sb_max */
1694 static int
1695 sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2,
1696 struct sysctl_req *req)
1697 {
1698 #pragma unused(arg1, arg2)
1699 u_int32_t new_value = 0, *space_p = NULL;
1700 int changed = 0, error = 0;
1701 u_quad_t sb_effective_max = (sb_max/(MSIZE+MCLBYTES)) * MCLBYTES;
1702
1703 switch (oidp->oid_number) {
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;
1712 }
1713 error = sysctl_io_number(req, *space_p, sizeof (u_int32_t),
1714 &new_value, &changed);
1715 if (changed) {
1716 if (new_value > 0 && new_value <= sb_effective_max)
1717 *space_p = new_value;
1718 else
1719 error = ERANGE;
1720 }
1721 return (error);
1722 }
1723
1724 SYSCTL_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");
1727
1728 SYSCTL_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");
1731
1732 static int
1733 udp_abort(struct socket *so)
1734 {
1735 struct inpcb *inp;
1736
1737 inp = sotoinpcb(so);
1738 if (inp == NULL) {
1739 panic("%s: so=%p null inp\n", __func__, so);
1740 /* NOTREACHED */
1741 }
1742 soisdisconnected(so);
1743 in_pcbdetach(inp);
1744 return (0);
1745 }
1746
1747 static int
1748 udp_attach(struct socket *so, int proto, struct proc *p)
1749 {
1750 #pragma unused(proto)
1751 struct inpcb *inp;
1752 int error;
1753
1754 inp = sotoinpcb(so);
1755 if (inp != NULL) {
1756 panic ("%s so=%p inp=%p\n", __func__, so, inp);
1757 /* NOTREACHED */
1758 }
1759 error = in_pcballoc(so, &udbinfo, p);
1760 if (error != 0)
1761 return (error);
1762 error = soreserve(so, udp_sendspace, udp_recvspace);
1763 if (error != 0)
1764 return (error);
1765 inp = (struct inpcb *)so->so_pcb;
1766 inp->inp_vflag |= INP_IPV4;
1767 inp->inp_ip_ttl = ip_defttl;
1768 if (nstat_collect)
1769 nstat_udp_new_pcb(inp);
1770 return (0);
1771 }
1772
1773 static int
1774 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1775 {
1776 struct inpcb *inp;
1777 int error;
1778
1779 if (nam->sa_family != 0 && nam->sa_family != AF_INET &&
1780 nam->sa_family != AF_INET6)
1781 return (EAFNOSUPPORT);
1782
1783 inp = sotoinpcb(so);
1784 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1785 return (inp == NULL ? EINVAL : EPROTOTYPE);
1786 error = in_pcbbind(inp, nam, p);
1787 return (error);
1788 }
1789
1790 static int
1791 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
1792 {
1793 struct inpcb *inp;
1794 int error;
1795
1796 inp = sotoinpcb(so);
1797 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1798 return (inp == NULL ? EINVAL : EPROTOTYPE);
1799 if (inp->inp_faddr.s_addr != INADDR_ANY)
1800 return (EISCONN);
1801 error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL);
1802 if (error == 0) {
1803 soisconnected(so);
1804 if (inp->inp_flowhash == 0)
1805 inp->inp_flowhash = inp_calc_flowhash(inp);
1806 }
1807 return (error);
1808 }
1809
1810 int
1811 udp_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
1869 static int
1870 udp_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));
1877 }
1878
1879 static int
1880 udp_detach(struct socket *so)
1881 {
1882 struct inpcb *inp;
1883
1884 inp = sotoinpcb(so);
1885 if (inp == NULL) {
1886 panic("%s: so=%p null inp\n", __func__, so);
1887 /* NOTREACHED */
1888 }
1889 in_pcbdetach(inp);
1890 inp->inp_state = INPCB_STATE_DEAD;
1891 return (0);
1892 }
1893
1894 static int
1895 udp_disconnect(struct socket *so)
1896 {
1897 struct inpcb *inp;
1898
1899 inp = sotoinpcb(so);
1900 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
1901 return (inp == NULL ? EINVAL : EPROTOTYPE);
1902 if (inp->inp_faddr.s_addr == INADDR_ANY)
1903 return (ENOTCONN);
1904
1905 in_pcbdisconnect(inp);
1906
1907 /* reset flow controlled state, just in case */
1908 inp_reset_fc_state(inp);
1909
1910 inp->inp_laddr.s_addr = INADDR_ANY;
1911 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1912 inp->inp_last_outifp = NULL;
1913 return (0);
1914 }
1915
1916 static int
1917 udp_disconnectx(struct socket *so, associd_t aid, connid_t cid)
1918 {
1919 #pragma unused(cid)
1920 if (aid != ASSOCID_ANY && aid != ASSOCID_ALL)
1921 return (EINVAL);
1922
1923 return (udp_disconnect(so));
1924 }
1925
1926 static int
1927 udp_send(struct socket *so, int flags, struct mbuf *m,
1928 struct sockaddr *addr, struct mbuf *control, struct proc *p)
1929 {
1930 #pragma unused(flags)
1931 struct inpcb *inp;
1932
1933 inp = sotoinpcb(so);
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);
1940 }
1941
1942 return (udp_output(inp, m, addr, control, p));
1943 }
1944
1945 int
1946 udp_shutdown(struct socket *so)
1947 {
1948 struct inpcb *inp;
1949
1950 inp = sotoinpcb(so);
1951 if (inp == NULL)
1952 return (EINVAL);
1953 socantsendmore(so);
1954 return (0);
1955 }
1956
1957 int
1958 udp_lock(struct socket *so, int refcount, void *debug)
1959 {
1960 void *lr_saved;
1961
1962 if (debug == NULL)
1963 lr_saved = __builtin_return_address(0);
1964 else
1965 lr_saved = debug;
1966
1967 if (so->so_pcb != NULL) {
1968 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
1969 LCK_MTX_ASSERT_NOTOWNED);
1970 lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
1971 } else {
1972 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
1973 so, lr_saved, solockhistory_nr(so));
1974 /* NOTREACHED */
1975 }
1976 if (refcount)
1977 so->so_usecount++;
1978
1979 so->lock_lr[so->next_lock_lr] = lr_saved;
1980 so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
1981 return (0);
1982 }
1983
1984 int
1985 udp_unlock(struct socket *so, int refcount, void *debug)
1986 {
1987 void *lr_saved;
1988
1989 if (debug == NULL)
1990 lr_saved = __builtin_return_address(0);
1991 else
1992 lr_saved = debug;
1993
1994 if (refcount)
1995 so->so_usecount--;
1996
1997 if (so->so_pcb == NULL) {
1998 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
1999 so, lr_saved, solockhistory_nr(so));
2000 /* NOTREACHED */
2001 } else {
2002 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2003 LCK_MTX_ASSERT_OWNED);
2004 so->unlock_lr[so->next_unlock_lr] = lr_saved;
2005 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
2006 lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2007 }
2008 return (0);
2009 }
2010
2011 lck_mtx_t *
2012 udp_getlock(struct socket *so, int locktype)
2013 {
2014 #pragma unused(locktype)
2015 struct inpcb *inp = sotoinpcb(so);
2016
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 */
2021 }
2022 return (&inp->inpcb_mtx);
2023 }
2024
2025 /*
2026 * UDP garbage collector callback (inpcb_timer_func_t).
2027 *
2028 * Returns > 0 to keep timer active.
2029 */
2030 static void
2031 udp_gc(struct inpcbinfo *ipi)
2032 {
2033 struct inpcb *inp, *inpnxt;
2034 struct socket *so;
2035
2036 if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) {
2037 if (udp_gc_done == TRUE) {
2038 udp_gc_done = FALSE;
2039 /* couldn't get the lock, must lock next time */
2040 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2041 return;
2042 }
2043 lck_rw_lock_exclusive(ipi->ipi_lock);
2044 }
2045
2046 udp_gc_done = TRUE;
2047
2048 for (inp = udb.lh_first; inp != NULL; inp = inpnxt) {
2049 inpnxt = inp->inp_list.le_next;
2050
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)
2058 continue;
2059
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);
2066 continue;
2067 }
2068
2069 /*
2070 * Keep gc active unless usecount is 0.
2071 */
2072 so = inp->inp_socket;
2073 if (so->so_usecount == 0) {
2074 if (inp->inp_state != INPCB_STATE_DEAD) {
2075 #if INET6
2076 if (SOCK_CHECK_DOM(so, PF_INET6))
2077 in6_pcbdetach(inp);
2078 else
2079 #endif /* INET6 */
2080 in_pcbdetach(inp);
2081 }
2082 in_pcbdispose(inp);
2083 } else {
2084 lck_mtx_unlock(&inp->inpcb_mtx);
2085 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2086 }
2087 }
2088 lck_rw_done(ipi->ipi_lock);
2089
2090 return;
2091 }
2092
2093 static int
2094 udp_getstat SYSCTL_HANDLER_ARGS
2095 {
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)));
2101 }
2102
2103 void
2104 udp_in_cksum_stats(u_int32_t len)
2105 {
2106 udpstat.udps_rcv_swcsum++;
2107 udpstat.udps_rcv_swcsum_bytes += len;
2108 }
2109
2110 void
2111 udp_out_cksum_stats(u_int32_t len)
2112 {
2113 udpstat.udps_snd_swcsum++;
2114 udpstat.udps_snd_swcsum_bytes += len;
2115 }
2116
2117 #if INET6
2118 void
2119 udp_in6_cksum_stats(u_int32_t len)
2120 {
2121 udpstat.udps_rcv6_swcsum++;
2122 udpstat.udps_rcv6_swcsum_bytes += len;
2123 }
2124
2125 void
2126 udp_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 */
2136 static int
2137 udp_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);
2210 }