1 /*
2 * Copyright (c) 2010-2021 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, 1990, 1993
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
61 #include <sys/types.h>
62 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/protosw.h>
66 #include <sys/domain.h>
67 #include <sys/filedesc.h>
68 #include <sys/file_internal.h>
69 #include <sys/kernel.h>
70 #include <sys/sysctl.h>
71 #include <sys/dtrace.h>
72 #include <sys/kauth.h>
73
74 #include <net/route.h>
75 #include <net/if_var.h>
76 #include <net/if_ports_used.h>
77 #include <net/ntstat.h>
78
79 #include <netinet/in.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip_var.h>
83
84 #include <netinet/udp.h>
85 #include <netinet/udp_var.h>
86
87 #include <netinet/tcp.h>
88 #include <netinet/tcp_fsm.h>
89 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet6/in6_var.h>
93
94 #include <os/log.h>
95
96 #ifndef ROUNDUP64
97 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
98 #endif
99
100 #ifndef ADVANCE64
101 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
102 #endif
103
104 static void inpcb_to_xinpcb_n(struct inpcb *, struct xinpcb_n *);
105 static void tcpcb_to_xtcpcb_n(struct tcpcb *, struct xtcpcb_n *);
106 void shutdown_sockets_on_interface(struct ifnet *ifp);
107
108
109 __private_extern__ void
sotoxsocket_n(struct socket * so,struct xsocket_n * xso)110 sotoxsocket_n(struct socket *so, struct xsocket_n *xso)
111 {
112 xso->xso_len = sizeof(struct xsocket_n);
113 xso->xso_kind = XSO_SOCKET;
114
115 if (so == NULL) {
116 return;
117 }
118
119 xso->xso_so = (uint64_t)VM_KERNEL_ADDRPERM(so);
120 xso->so_type = so->so_type;
121 xso->so_options = so->so_options;
122 xso->so_linger = so->so_linger;
123 xso->so_state = so->so_state;
124 xso->so_pcb = (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb);
125 if (so->so_proto) {
126 xso->xso_protocol = SOCK_PROTO(so);
127 xso->xso_family = SOCK_DOM(so);
128 } else {
129 xso->xso_protocol = xso->xso_family = 0;
130 }
131 xso->so_qlen = so->so_qlen;
132 xso->so_incqlen = so->so_incqlen;
133 xso->so_qlimit = so->so_qlimit;
134 xso->so_timeo = so->so_timeo;
135 xso->so_error = so->so_error;
136 xso->so_pgid = so->so_pgid;
137 xso->so_oobmark = so->so_oobmark;
138 xso->so_uid = kauth_cred_getuid(so->so_cred);
139 xso->so_last_pid = so->last_pid;
140 xso->so_e_pid = so->e_pid;
141 }
142
143 __private_extern__ void
sbtoxsockbuf_n(struct sockbuf * sb,struct xsockbuf_n * xsb)144 sbtoxsockbuf_n(struct sockbuf *sb, struct xsockbuf_n *xsb)
145 {
146 xsb->xsb_len = sizeof(struct xsockbuf_n);
147
148 if (sb == NULL) {
149 return;
150 }
151
152 xsb->xsb_kind = (sb->sb_flags & SB_RECV) ? XSO_RCVBUF : XSO_SNDBUF;
153 xsb->sb_cc = sb->sb_cc;
154 xsb->sb_hiwat = sb->sb_hiwat;
155 xsb->sb_mbcnt = sb->sb_mbcnt;
156 xsb->sb_mbmax = sb->sb_mbmax;
157 xsb->sb_lowat = sb->sb_lowat;
158 xsb->sb_flags = (short)sb->sb_flags;
159 xsb->sb_timeo = (short)((sb->sb_timeo.tv_sec * hz) +
160 sb->sb_timeo.tv_usec / tick);
161 if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0) {
162 xsb->sb_timeo = 1;
163 }
164 }
165
166 __private_extern__ void
sbtoxsockstat_n(struct socket * so,struct xsockstat_n * xst)167 sbtoxsockstat_n(struct socket *so, struct xsockstat_n *xst)
168 {
169 int i;
170
171 xst->xst_len = sizeof(struct xsockstat_n);
172 xst->xst_kind = XSO_STATS;
173
174 if (so == NULL) {
175 return;
176 }
177
178 for (i = 0; i < SO_TC_STATS_MAX; i++) {
179 xst->xst_tc_stats[i].rxpackets = so->so_tc_stats[i].rxpackets;
180 xst->xst_tc_stats[i].rxbytes = so->so_tc_stats[i].rxbytes;
181 xst->xst_tc_stats[i].txpackets = so->so_tc_stats[i].txpackets;
182 xst->xst_tc_stats[i].txbytes = so->so_tc_stats[i].txbytes;
183 }
184 }
185
186 static void
inpcb_to_xinpcb_n(struct inpcb * inp,struct xinpcb_n * xinp)187 inpcb_to_xinpcb_n(struct inpcb *inp, struct xinpcb_n *xinp)
188 {
189 xinp->xi_len = sizeof(struct xinpcb_n);
190 xinp->xi_kind = XSO_INPCB;
191 xinp->xi_inpp = (uint64_t)VM_KERNEL_ADDRPERM(inp);
192 xinp->inp_fport = inp->inp_fport;
193 xinp->inp_lport = inp->inp_lport;
194 xinp->inp_ppcb = (uint64_t)VM_KERNEL_ADDRPERM(inp->inp_ppcb);
195 xinp->inp_gencnt = inp->inp_gencnt;
196 xinp->inp_flags = inp->inp_flags;
197 xinp->inp_flow = inp->inp_flow;
198 xinp->inp_vflag = inp->inp_vflag;
199 xinp->inp_ip_ttl = inp->inp_ip_ttl;
200 xinp->inp_ip_p = inp->inp_ip_p;
201 xinp->inp_dependfaddr.inp6_foreign = inp->inp_dependfaddr.inp6_foreign;
202 xinp->inp_dependladdr.inp6_local = inp->inp_dependladdr.inp6_local;
203 xinp->inp_depend4.inp4_ip_tos = inp->inp_depend4.inp4_ip_tos;
204 xinp->inp_depend6.inp6_hlim = 0;
205 xinp->inp_depend6.inp6_cksum = inp->inp_depend6.inp6_cksum;
206 xinp->inp_depend6.inp6_ifindex = 0;
207 xinp->inp_depend6.inp6_hops = inp->inp_depend6.inp6_hops;
208 xinp->inp_flowhash = inp->inp_flowhash;
209 xinp->inp_flags2 = inp->inp_flags2;
210 }
211
212 __private_extern__ void
tcpcb_to_xtcpcb_n(struct tcpcb * tp,struct xtcpcb_n * xt)213 tcpcb_to_xtcpcb_n(struct tcpcb *tp, struct xtcpcb_n *xt)
214 {
215 xt->xt_len = sizeof(struct xtcpcb_n);
216 xt->xt_kind = XSO_TCPCB;
217
218 xt->t_segq = (uint32_t)VM_KERNEL_ADDRPERM(tp->t_segq.lh_first);
219 xt->t_dupacks = tp->t_dupacks;
220 xt->t_timer[TCPT_REXMT_EXT] = tp->t_timer[TCPT_REXMT];
221 xt->t_timer[TCPT_PERSIST_EXT] = tp->t_timer[TCPT_PERSIST];
222 xt->t_timer[TCPT_KEEP_EXT] = tp->t_timer[TCPT_KEEP];
223 xt->t_timer[TCPT_2MSL_EXT] = tp->t_timer[TCPT_2MSL];
224 xt->t_state = tp->t_state;
225 xt->t_flags = tp->t_flags;
226 xt->t_force = (tp->t_flagsext & TF_FORCE) ? 1 : 0;
227 xt->snd_una = tp->snd_una;
228 xt->snd_max = tp->snd_max;
229 xt->snd_nxt = tp->snd_nxt;
230 xt->snd_up = tp->snd_up;
231 xt->snd_wl1 = tp->snd_wl1;
232 xt->snd_wl2 = tp->snd_wl2;
233 xt->iss = tp->iss;
234 xt->irs = tp->irs;
235 xt->rcv_nxt = tp->rcv_nxt;
236 xt->rcv_adv = tp->rcv_adv;
237 xt->rcv_wnd = tp->rcv_wnd;
238 xt->rcv_up = tp->rcv_up;
239 xt->snd_wnd = tp->snd_wnd;
240 xt->snd_cwnd = tp->snd_cwnd;
241 xt->snd_ssthresh = tp->snd_ssthresh;
242 xt->t_maxopd = tp->t_maxopd;
243 xt->t_rcvtime = tp->t_rcvtime;
244 xt->t_starttime = tp->t_starttime;
245 xt->t_rtttime = tp->t_rtttime;
246 xt->t_rtseq = tp->t_rtseq;
247 xt->t_rxtcur = tp->t_rxtcur;
248 xt->t_maxseg = tp->t_maxseg;
249 xt->t_srtt = tp->t_srtt;
250 xt->t_rttvar = tp->t_rttvar;
251 xt->t_rxtshift = tp->t_rxtshift;
252 xt->t_rttmin = tp->t_rttmin;
253 xt->t_rttupdated = tp->t_rttupdated;
254 xt->max_sndwnd = tp->max_sndwnd;
255 xt->t_softerror = tp->t_softerror;
256 xt->t_oobflags = tp->t_oobflags;
257 xt->t_iobc = tp->t_iobc;
258 xt->snd_scale = tp->snd_scale;
259 xt->rcv_scale = tp->rcv_scale;
260 xt->request_r_scale = tp->request_r_scale;
261 xt->requested_s_scale = tp->requested_s_scale;
262 xt->ts_recent = tp->ts_recent;
263 xt->ts_recent_age = tp->ts_recent_age;
264 xt->last_ack_sent = tp->last_ack_sent;
265 xt->cc_send = 0;
266 xt->cc_recv = 0;
267 xt->snd_recover = tp->snd_recover;
268 xt->snd_cwnd_prev = tp->snd_cwnd_prev;
269 xt->snd_ssthresh_prev = tp->snd_ssthresh_prev;
270 }
271
272 __private_extern__ int
get_pcblist_n(short proto,struct sysctl_req * req,struct inpcbinfo * pcbinfo)273 get_pcblist_n(short proto, struct sysctl_req *req, struct inpcbinfo *pcbinfo)
274 {
275 int error = 0;
276 int i, n, sz;
277 struct inpcb *inp, **inp_list = NULL;
278 inp_gen_t gencnt;
279 struct xinpgen xig;
280 void *buf = NULL;
281 size_t item_size = ROUNDUP64(sizeof(struct xinpcb_n)) +
282 ROUNDUP64(sizeof(struct xsocket_n)) +
283 2 * ROUNDUP64(sizeof(struct xsockbuf_n)) +
284 ROUNDUP64(sizeof(struct xsockstat_n));
285 #if SKYWALK
286 int nuserland;
287 void *userlandsnapshot = NULL;
288 #endif /* SKYWALK */
289
290 if (proto == IPPROTO_TCP) {
291 item_size += ROUNDUP64(sizeof(struct xtcpcb_n));
292 }
293
294 if (req->oldptr == USER_ADDR_NULL) {
295 n = pcbinfo->ipi_count;
296 #if SKYWALK
297 n += ntstat_userland_count(proto);
298 #endif /* SKYWALK */
299 req->oldidx = 2 * (sizeof(xig)) + (n + n / 8 + 1) * item_size;
300 return 0;
301 }
302
303 if (req->newptr != USER_ADDR_NULL) {
304 return EPERM;
305 }
306
307 #if SKYWALK
308 /*
309 * Get a snapshot of the state of the user level flows so we know
310 * the exact number of results to give back to the user.
311 * This could take a while and use other locks, so do this prior
312 * to taking any locks of our own.
313 */
314 error = nstat_userland_get_snapshot(proto, &userlandsnapshot, &nuserland);
315
316 if (error) {
317 return error;
318 }
319 #endif /* SKYWALK */
320
321 /*
322 * The process of preparing the PCB list is too time-consuming and
323 * resource-intensive to repeat twice on every request.
324 */
325 lck_rw_lock_exclusive(&pcbinfo->ipi_lock);
326 /*
327 * OK, now we're committed to doing something.
328 */
329 gencnt = pcbinfo->ipi_gencnt;
330 n = sz = pcbinfo->ipi_count;
331
332 bzero(&xig, sizeof(xig));
333 xig.xig_len = sizeof(xig);
334 xig.xig_count = n;
335 #if SKYWALK
336 xig.xig_count += nuserland;
337 #endif /* SKYWALK */
338 xig.xig_gen = gencnt;
339 xig.xig_sogen = so_gencnt;
340 error = SYSCTL_OUT(req, &xig, sizeof(xig));
341 if (error) {
342 goto done;
343 }
344 /*
345 * We are done if there is no pcb
346 */
347 if (xig.xig_count == 0) {
348 goto done;
349 }
350
351 buf = kalloc_data(item_size, Z_WAITOK);
352 if (buf == NULL) {
353 error = ENOMEM;
354 goto done;
355 }
356
357 inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
358 if (inp_list == NULL) {
359 error = ENOMEM;
360 goto done;
361 }
362
363 /*
364 * Special case TCP to include the connections in time wait
365 */
366 if (proto == IPPROTO_TCP) {
367 n = get_tcp_inp_list(inp_list, n, gencnt);
368 } else {
369 for (inp = pcbinfo->ipi_listhead->lh_first, i = 0; inp && i < n;
370 inp = inp->inp_list.le_next) {
371 if (inp->inp_gencnt <= gencnt &&
372 inp->inp_state != INPCB_STATE_DEAD) {
373 inp_list[i++] = inp;
374 }
375 }
376 n = i;
377 }
378
379
380 error = 0;
381 for (i = 0; i < n; i++) {
382 inp = inp_list[i];
383 if (inp->inp_gencnt <= gencnt &&
384 inp->inp_state != INPCB_STATE_DEAD) {
385 struct xinpcb_n *xi = (struct xinpcb_n *)buf;
386 struct xsocket_n *xso = (struct xsocket_n *)
387 ADVANCE64(xi, sizeof(*xi));
388 struct xsockbuf_n *xsbrcv = (struct xsockbuf_n *)
389 ADVANCE64(xso, sizeof(*xso));
390 struct xsockbuf_n *xsbsnd = (struct xsockbuf_n *)
391 ADVANCE64(xsbrcv, sizeof(*xsbrcv));
392 struct xsockstat_n *xsostats = (struct xsockstat_n *)
393 ADVANCE64(xsbsnd, sizeof(*xsbsnd));
394
395 bzero(buf, item_size);
396
397 inpcb_to_xinpcb_n(inp, xi);
398 sotoxsocket_n(inp->inp_socket, xso);
399 sbtoxsockbuf_n(inp->inp_socket ?
400 &inp->inp_socket->so_rcv : NULL, xsbrcv);
401 sbtoxsockbuf_n(inp->inp_socket ?
402 &inp->inp_socket->so_snd : NULL, xsbsnd);
403 sbtoxsockstat_n(inp->inp_socket, xsostats);
404 if (proto == IPPROTO_TCP) {
405 struct xtcpcb_n *xt = (struct xtcpcb_n *)
406 ADVANCE64(xsostats, sizeof(*xsostats));
407
408 /*
409 * inp->inp_ppcb, can only be NULL on
410 * an initialization race window.
411 * No need to lock.
412 */
413 if (inp->inp_ppcb == NULL) {
414 continue;
415 }
416
417 tcpcb_to_xtcpcb_n((struct tcpcb *)
418 inp->inp_ppcb, xt);
419 }
420 error = SYSCTL_OUT(req, buf, item_size);
421 if (error) {
422 break;
423 }
424 }
425 }
426 #if SKYWALK
427 if (!error && nuserland > 0) {
428 error = nstat_userland_list_snapshot(proto, req, userlandsnapshot, nuserland);
429 }
430 #endif /* SKYWALK */
431
432 if (!error) {
433 /*
434 * Give the user an updated idea of our state.
435 * If the generation differs from what we told
436 * her before, she knows that something happened
437 * while we were processing this request, and it
438 * might be necessary to retry.
439 */
440 bzero(&xig, sizeof(xig));
441 xig.xig_len = sizeof(xig);
442 xig.xig_gen = pcbinfo->ipi_gencnt;
443 xig.xig_sogen = so_gencnt;
444 xig.xig_count = pcbinfo->ipi_count;
445 #if SKYWALK
446 xig.xig_count += nuserland;
447 #endif /* SKYWALK */
448 error = SYSCTL_OUT(req, &xig, sizeof(xig));
449 }
450 done:
451 lck_rw_done(&pcbinfo->ipi_lock);
452
453 #if SKYWALK
454 nstat_userland_release_snapshot(userlandsnapshot, nuserland);
455 #endif /* SKYWALK */
456
457 kfree_type(struct inpcb *, sz, inp_list);
458 if (buf != NULL) {
459 kfree_data(buf, item_size);
460 }
461 return error;
462 }
463
464 static void
inpcb_get_if_ports_used(ifnet_t ifp,int protocol,uint32_t flags,bitstr_t * bitfield,struct inpcbinfo * pcbinfo)465 inpcb_get_if_ports_used(ifnet_t ifp, int protocol, uint32_t flags,
466 bitstr_t *bitfield, struct inpcbinfo *pcbinfo)
467 {
468 struct inpcb *inp;
469 struct socket *so;
470 inp_gen_t gencnt;
471 bool iswildcard, wildcardok, nowakeok;
472 bool recvanyifonly, extbgidleok;
473 bool activeonly;
474 bool anytcpstateok;
475
476 if (ifp == NULL) {
477 return;
478 }
479
480 wildcardok = ((flags & IFNET_GET_LOCAL_PORTS_WILDCARDOK) != 0);
481 nowakeok = ((flags & IFNET_GET_LOCAL_PORTS_NOWAKEUPOK) != 0);
482 recvanyifonly = ((flags & IFNET_GET_LOCAL_PORTS_RECVANYIFONLY) != 0);
483 extbgidleok = ((flags & IFNET_GET_LOCAL_PORTS_EXTBGIDLEONLY) != 0);
484 activeonly = ((flags & IFNET_GET_LOCAL_PORTS_ACTIVEONLY) != 0);
485 anytcpstateok = ((flags & IFNET_GET_LOCAL_PORTS_ANYTCPSTATEOK) != 0);
486
487 lck_rw_lock_shared(&pcbinfo->ipi_lock);
488 gencnt = pcbinfo->ipi_gencnt;
489
490 for (inp = LIST_FIRST(pcbinfo->ipi_listhead); inp;
491 inp = LIST_NEXT(inp, inp_list)) {
492 if (inp->inp_gencnt > gencnt ||
493 inp->inp_state == INPCB_STATE_DEAD ||
494 inp->inp_wantcnt == WNT_STOPUSING) {
495 continue;
496 }
497
498 if ((so = inp->inp_socket) == NULL || inp->inp_lport == 0) {
499 continue;
500 }
501
502 /*
503 * ANYTCPSTATEOK means incoming packets cannot be filtered
504 * reception so cast a wide net of possibilities
505 */
506 if (!anytcpstateok &&
507 ((so->so_state & SS_DEFUNCT) ||
508 (so->so_state & SS_ISDISCONNECTED))) {
509 continue;
510 }
511
512 /*
513 * If protocol is specified, filter out inpcbs that
514 * are not relevant to the protocol family of interest.
515 */
516 if (protocol != PF_UNSPEC) {
517 if (protocol == PF_INET) {
518 /*
519 * If protocol of interest is IPv4, skip the inpcb
520 * if the family is not IPv4.
521 * OR
522 * If the family is IPv4, skip if the IPv4 flow is
523 * CLAT46 translated.
524 */
525 if ((inp->inp_vflag & INP_IPV4) == 0 ||
526 (inp->inp_flags2 & INP2_CLAT46_FLOW) != 0) {
527 continue;
528 }
529 } else if (protocol == PF_INET6) {
530 /*
531 * If protocol of interest is IPv6, skip the inpcb
532 * if the family is not IPv6.
533 * AND
534 * The flow is not a CLAT46'd flow.
535 */
536 if ((inp->inp_vflag & INP_IPV6) == 0 &&
537 (inp->inp_flags2 & INP2_CLAT46_FLOW) == 0) {
538 continue;
539 }
540 } else {
541 /* Protocol family not supported */
542 continue;
543 }
544 }
545
546 if (SOCK_PROTO(inp->inp_socket) != IPPROTO_UDP &&
547 SOCK_PROTO(inp->inp_socket) != IPPROTO_TCP) {
548 continue;
549 }
550
551 iswildcard = (((inp->inp_vflag & INP_IPV4) &&
552 inp->inp_laddr.s_addr == INADDR_ANY) ||
553 ((inp->inp_vflag & INP_IPV6) &&
554 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)));
555
556 if (!wildcardok && iswildcard) {
557 continue;
558 }
559
560 if ((so->so_options & SO_NOWAKEFROMSLEEP) &&
561 !nowakeok) {
562 continue;
563 }
564
565 if (!(inp->inp_flags & INP_RECV_ANYIF) &&
566 recvanyifonly) {
567 continue;
568 }
569
570 if (!(so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) &&
571 extbgidleok) {
572 continue;
573 }
574
575 if (!iswildcard &&
576 !(inp->inp_last_outifp == NULL || ifp == inp->inp_last_outifp)) {
577 continue;
578 }
579
580 if (SOCK_PROTO(inp->inp_socket) == IPPROTO_UDP &&
581 so->so_state & SS_CANTRCVMORE) {
582 continue;
583 }
584
585 if (SOCK_PROTO(inp->inp_socket) == IPPROTO_TCP) {
586 struct tcpcb *tp = sototcpcb(inp->inp_socket);
587
588 /*
589 * Workaround race where inp_ppcb is NULL during
590 * socket initialization
591 */
592 if (tp == NULL) {
593 continue;
594 }
595
596 switch (tp->t_state) {
597 case TCPS_CLOSED:
598 if (anytcpstateok && inp->inp_fport != 0) {
599 /*
600 * A foreign port means we had a 4 tuple at
601 * least a connection attempt so packets
602 * may be received for the 4 tuple after the
603 * connection is gone
604 */
605 break;
606 }
607 continue;
608 /* NOT REACHED */
609 case TCPS_LISTEN:
610 case TCPS_SYN_SENT:
611 case TCPS_SYN_RECEIVED:
612 case TCPS_ESTABLISHED:
613 case TCPS_FIN_WAIT_1:
614 /*
615 * Note: FIN_WAIT_1 is an active state
616 * because we need our FIN to be
617 * acknowledged
618 */
619 break;
620 case TCPS_CLOSE_WAIT:
621 case TCPS_CLOSING:
622 case TCPS_LAST_ACK:
623 case TCPS_FIN_WAIT_2:
624 /*
625 * In the closing states, the connection
626 * is active when there is outgoing
627 * data having to be acknowledged
628 */
629 if (!anytcpstateok &&
630 (activeonly && so->so_snd.sb_cc == 0)) {
631 continue;
632 }
633 break;
634 case TCPS_TIME_WAIT:
635 if (anytcpstateok) {
636 /*
637 * Packets may still be received for the 4 tuple
638 * after the connection is gone
639 */
640 break;
641 }
642 continue;
643 /* NOT REACHED */
644 }
645 }
646
647 bitstr_set(bitfield, ntohs(inp->inp_lport));
648
649 (void) if_ports_used_add_inpcb(ifp->if_index, inp);
650 }
651 lck_rw_done(&pcbinfo->ipi_lock);
652 }
653
654 __private_extern__ void
inpcb_get_ports_used(ifnet_t ifp,int protocol,uint32_t flags,bitstr_t * bitfield,struct inpcbinfo * pcbinfo)655 inpcb_get_ports_used(ifnet_t ifp, int protocol, uint32_t flags,
656 bitstr_t *bitfield, struct inpcbinfo *pcbinfo)
657 {
658 if (ifp != NULL) {
659 inpcb_get_if_ports_used(ifp, protocol, flags, bitfield, pcbinfo);
660 } else {
661 errno_t error;
662 ifnet_t *ifp_list;
663 uint32_t count, i;
664
665 error = ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp_list, &count);
666 if (error != 0) {
667 os_log_error(OS_LOG_DEFAULT,
668 "%s: ifnet_list_get_all() failed %d",
669 __func__, error);
670 return;
671 }
672 for (i = 0; i < count; i++) {
673 if (TAILQ_EMPTY(&ifp_list[i]->if_addrhead)) {
674 continue;
675 }
676 inpcb_get_if_ports_used(ifp_list[i], protocol, flags,
677 bitfield, pcbinfo);
678 }
679 ifnet_list_free(ifp_list);
680 }
681 }
682
683 __private_extern__ uint32_t
inpcb_count_opportunistic(unsigned int ifindex,struct inpcbinfo * pcbinfo,u_int32_t flags)684 inpcb_count_opportunistic(unsigned int ifindex, struct inpcbinfo *pcbinfo,
685 u_int32_t flags)
686 {
687 uint32_t opportunistic = 0;
688 struct inpcb *inp;
689 inp_gen_t gencnt;
690
691 lck_rw_lock_shared(&pcbinfo->ipi_lock);
692 gencnt = pcbinfo->ipi_gencnt;
693 for (inp = LIST_FIRST(pcbinfo->ipi_listhead);
694 inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
695 if (inp->inp_gencnt <= gencnt &&
696 inp->inp_state != INPCB_STATE_DEAD &&
697 inp->inp_socket != NULL &&
698 so_get_opportunistic(inp->inp_socket) &&
699 inp->inp_last_outifp != NULL &&
700 ifindex == inp->inp_last_outifp->if_index) {
701 opportunistic++;
702 struct socket *so = inp->inp_socket;
703 if ((flags & INPCB_OPPORTUNISTIC_SETCMD) &&
704 (so->so_state & SS_ISCONNECTED)) {
705 socket_lock(so, 1);
706 if (flags & INPCB_OPPORTUNISTIC_THROTTLEON) {
707 so->so_flags |= SOF_SUSPENDED;
708 soevent(so,
709 (SO_FILT_HINT_LOCKED |
710 SO_FILT_HINT_SUSPEND));
711 } else {
712 so->so_flags &= ~(SOF_SUSPENDED);
713 soevent(so,
714 (SO_FILT_HINT_LOCKED |
715 SO_FILT_HINT_RESUME));
716 }
717 SOTHROTTLELOG("throttle[%d]: so 0x%llx "
718 "[%d,%d] %s\n", so->last_pid,
719 (uint64_t)VM_KERNEL_ADDRPERM(so),
720 SOCK_DOM(so), SOCK_TYPE(so),
721 (so->so_flags & SOF_SUSPENDED) ?
722 "SUSPENDED" : "RESUMED");
723 socket_unlock(so, 1);
724 }
725 }
726 }
727
728 lck_rw_done(&pcbinfo->ipi_lock);
729
730 return opportunistic;
731 }
732
733 __private_extern__ uint32_t
inpcb_find_anypcb_byaddr(struct ifaddr * ifa,struct inpcbinfo * pcbinfo)734 inpcb_find_anypcb_byaddr(struct ifaddr *ifa, struct inpcbinfo *pcbinfo)
735 {
736 struct inpcb *inp;
737 inp_gen_t gencnt = pcbinfo->ipi_gencnt;
738 struct socket *so = NULL;
739 int af;
740
741 if ((ifa->ifa_addr->sa_family != AF_INET) &&
742 (ifa->ifa_addr->sa_family != AF_INET6)) {
743 return 0;
744 }
745
746 lck_rw_lock_shared(&pcbinfo->ipi_lock);
747 for (inp = LIST_FIRST(pcbinfo->ipi_listhead);
748 inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
749 if (inp->inp_gencnt <= gencnt &&
750 inp->inp_state != INPCB_STATE_DEAD &&
751 inp->inp_socket != NULL) {
752 so = inp->inp_socket;
753 af = SOCK_DOM(so);
754 if (af != ifa->ifa_addr->sa_family) {
755 continue;
756 }
757 if (inp->inp_last_outifp != ifa->ifa_ifp) {
758 continue;
759 }
760
761 if (af == AF_INET) {
762 if (inp->inp_laddr.s_addr ==
763 (satosin(ifa->ifa_addr))->sin_addr.s_addr) {
764 lck_rw_done(&pcbinfo->ipi_lock);
765 return 1;
766 }
767 }
768 if (af == AF_INET6) {
769 if (in6_are_addr_equal_scoped(IFA_IN6(ifa), &inp->in6p_laddr, ((struct sockaddr_in6 *)(void *)(ifa->ifa_addr))->sin6_scope_id, inp->inp_lifscope)) {
770 lck_rw_done(&pcbinfo->ipi_lock);
771 return 1;
772 }
773 }
774 }
775 }
776 lck_rw_done(&pcbinfo->ipi_lock);
777 return 0;
778 }
779
780 static int
shutdown_sockets_on_interface_proc_callout(proc_t p,void * arg)781 shutdown_sockets_on_interface_proc_callout(proc_t p, void *arg)
782 {
783 struct fileproc *fp;
784 struct ifnet *ifp = (struct ifnet *)arg;
785
786 if (ifp == NULL) {
787 return PROC_RETURNED;
788 }
789
790 fdt_foreach(fp, p) {
791 struct fileglob *fg = fp->fp_glob;
792 struct socket *so;
793 struct inpcb *inp;
794 struct ifnet *inp_ifp;
795 int error;
796
797 if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET) {
798 continue;
799 }
800
801 so = (struct socket *)fp_get_data(fp);
802 if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
803 continue;
804 }
805
806 inp = (struct inpcb *)so->so_pcb;
807
808 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) {
809 continue;
810 }
811
812 socket_lock(so, 1);
813
814 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
815 socket_unlock(so, 1);
816 continue;
817 }
818
819 if (inp->inp_boundifp != NULL) {
820 inp_ifp = inp->inp_boundifp;
821 } else if (inp->inp_last_outifp != NULL) {
822 inp_ifp = inp->inp_last_outifp;
823 } else {
824 socket_unlock(so, 1);
825 continue;
826 }
827
828 if (inp_ifp != ifp && inp_ifp->if_delegated.ifp != ifp) {
829 socket_unlock(so, 1);
830 continue;
831 }
832 error = sosetdefunct(p, so, 0, TRUE);
833 if (error != 0) {
834 log(LOG_ERR, "%s: sosetdefunct() error %d",
835 __func__, error);
836 } else {
837 error = sodefunct(p, so, 0);
838 if (error != 0) {
839 log(LOG_ERR, "%s: sodefunct() error %d",
840 __func__, error);
841 }
842 }
843
844 socket_unlock(so, 1);
845 }
846 proc_fdunlock(p);
847
848 return PROC_RETURNED;
849 }
850
851 void
shutdown_sockets_on_interface(struct ifnet * ifp)852 shutdown_sockets_on_interface(struct ifnet *ifp)
853 {
854 proc_iterate(PROC_ALLPROCLIST,
855 shutdown_sockets_on_interface_proc_callout,
856 ifp, NULL, NULL);
857 }
858
859 __private_extern__ int
inp_limit_companion_link(struct inpcbinfo * pcbinfo,u_int32_t limit)860 inp_limit_companion_link(struct inpcbinfo *pcbinfo, u_int32_t limit)
861 {
862 struct inpcb *inp;
863 struct socket *so = NULL;
864
865 lck_rw_lock_shared(&pcbinfo->ipi_lock);
866 inp_gen_t gencnt = pcbinfo->ipi_gencnt;
867 for (inp = LIST_FIRST(pcbinfo->ipi_listhead);
868 inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
869 if (inp->inp_gencnt <= gencnt &&
870 inp->inp_state != INPCB_STATE_DEAD &&
871 inp->inp_socket != NULL) {
872 so = inp->inp_socket;
873
874 if ((so->so_state & SS_DEFUNCT) || so->so_state & SS_ISDISCONNECTED ||
875 SOCK_PROTO(so) != IPPROTO_TCP || inp->inp_last_outifp == NULL ||
876 !IFNET_IS_COMPANION_LINK(inp->inp_last_outifp)) {
877 continue;
878 }
879 so->so_snd.sb_flags &= ~SB_LIMITED;
880 u_int32_t new_size = MAX(MIN(limit, so->so_snd.sb_lowat), so->so_snd.sb_cc);
881 sbreserve(&so->so_snd, new_size);
882 so->so_snd.sb_flags |= SB_LIMITED;
883 }
884 }
885 lck_rw_done(&pcbinfo->ipi_lock);
886 return 0;
887 }
888
889 __private_extern__ int
inp_recover_companion_link(struct inpcbinfo * pcbinfo)890 inp_recover_companion_link(struct inpcbinfo *pcbinfo)
891 {
892 struct inpcb *inp;
893 inp_gen_t gencnt = pcbinfo->ipi_gencnt;
894 struct socket *so = NULL;
895
896 lck_rw_lock_shared(&pcbinfo->ipi_lock);
897 for (inp = LIST_FIRST(pcbinfo->ipi_listhead);
898 inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
899 if (inp->inp_gencnt <= gencnt &&
900 inp->inp_state != INPCB_STATE_DEAD &&
901 inp->inp_socket != NULL) {
902 so = inp->inp_socket;
903
904 if (SOCK_PROTO(so) != IPPROTO_TCP || inp->inp_last_outifp == NULL ||
905 !(so->so_snd.sb_flags & SB_LIMITED)) {
906 continue;
907 }
908
909 so->so_snd.sb_flags &= ~SB_LIMITED;
910 }
911 }
912 lck_rw_done(&pcbinfo->ipi_lock);
913 return 0;
914 }
915