]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/socket_info.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / socket_info.c
CommitLineData
0c530ab8 1/*
f427ee49 2 * Copyright (c) 2005-2020 Apple Inc. All rights reserved.
0c530ab8 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
0c530ab8
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
0c530ab8
A
27 */
28
29#include <sys/types.h>
30#include <sys/kernel_types.h>
31#include <sys/errno.h>
32#include <sys/kernel.h>
33#include <sys/file_internal.h>
34#include <sys/stat.h>
35#include <sys/select.h>
36#include <sys/pipe.h>
37#include <sys/proc_info.h>
38#include <sys/domain.h>
39#include <sys/protosw.h>
40#include <sys/domain.h>
41#include <sys/socketvar.h>
42#include <sys/unpcb.h>
43#include <sys/sys_domain.h>
44#include <sys/kern_event.h>
f427ee49 45#include <sys/vsock_domain.h>
316670eb 46#include <mach/vm_param.h>
0c530ab8
A
47#include <net/ndrv_var.h>
48#include <netinet/in_pcb.h>
49#include <netinet/tcp_var.h>
50#include <string.h>
51
52static void fill_sockbuf_info(struct sockbuf *sb, struct sockbuf_info *sbi);
53static void fill_common_sockinfo(struct socket *so, struct socket_info *si);
54
55static void
56fill_sockbuf_info(struct sockbuf *sb, struct sockbuf_info *sbi)
57{
58 sbi->sbi_cc = sb->sb_cc;
59 sbi->sbi_hiwat = sb->sb_hiwat;
60 sbi->sbi_mbcnt = sb->sb_mbcnt;
61 sbi->sbi_mbmax = sb->sb_mbmax;
62 sbi->sbi_lowat = sb->sb_lowat;
f427ee49
A
63 sbi->sbi_flags = (short)sb->sb_flags;
64 sbi->sbi_timeo = (short)((sb->sb_timeo.tv_sec * hz) +
65 sb->sb_timeo.tv_usec / tick);
0a7de745 66 if (sbi->sbi_timeo == 0 && sb->sb_timeo.tv_usec != 0) {
0c530ab8 67 sbi->sbi_timeo = 1;
0a7de745 68 }
0c530ab8
A
69}
70
71static void
72fill_common_sockinfo(struct socket *so, struct socket_info *si)
73{
316670eb 74 si->soi_so = (u_int64_t)VM_KERNEL_ADDRPERM(so);
0c530ab8 75 si->soi_type = so->so_type;
316670eb 76 si->soi_options = (short)(so->so_options & 0xffff);
0c530ab8
A
77 si->soi_linger = so->so_linger;
78 si->soi_state = so->so_state;
316670eb 79 si->soi_pcb = (u_int64_t)VM_KERNEL_ADDRPERM(so->so_pcb);
0c530ab8 80 if (so->so_proto) {
39236c6e 81 si->soi_protocol = SOCK_PROTO(so);
0a7de745 82 if (so->so_proto->pr_domain) {
39236c6e 83 si->soi_family = SOCK_DOM(so);
0a7de745 84 } else {
0c530ab8 85 si->soi_family = 0;
0a7de745 86 }
39236c6e 87 } else {
0c530ab8 88 si->soi_protocol = si->soi_family = 0;
39236c6e 89 }
0c530ab8
A
90 si->soi_qlen = so->so_qlen;
91 si->soi_incqlen = so->so_incqlen;
92 si->soi_qlimit = so->so_qlimit;
93 si->soi_timeo = so->so_timeo;
94 si->soi_error = so->so_error;
95 si->soi_oobmark = so->so_oobmark;
96 fill_sockbuf_info(&so->so_snd, &si->soi_snd);
97 fill_sockbuf_info(&so->so_rcv, &si->soi_rcv);
0c530ab8
A
98}
99
100errno_t
101fill_socketinfo(struct socket *so, struct socket_info *si)
102{
103 errno_t error = 0;
39236c6e 104 int domain;
0c530ab8
A
105 short type;
106 short protocol;
39236c6e 107
0c530ab8
A
108 socket_lock(so, 0);
109
110 si->soi_kind = SOCKINFO_GENERIC;
111
112 fill_common_sockinfo(so, si);
113
39236c6e 114 if (so->so_pcb == NULL || so->so_proto == 0 ||
0a7de745 115 so->so_proto->pr_domain == NULL) {
0c530ab8 116 goto out;
0a7de745 117 }
0c530ab8 118
39236c6e
A
119 /*
120 * The kind of socket is determined by the triplet
121 * {domain, type, protocol}
122 */
123 domain = SOCK_DOM(so);
124 type = SOCK_TYPE(so);
125 protocol = SOCK_PROTO(so);
126 switch (domain) {
127 case PF_INET:
128 case PF_INET6: {
129 struct in_sockinfo *insi = &si->soi_proto.pri_in;
130 struct inpcb *inp = (struct inpcb *)so->so_pcb;
131
132 si->soi_kind = SOCKINFO_IN;
133
134 insi->insi_fport = inp->inp_fport;
135 insi->insi_lport = inp->inp_lport;
136 insi->insi_gencnt = inp->inp_gencnt;
137 insi->insi_flags = inp->inp_flags;
138 insi->insi_vflag = inp->inp_vflag;
139 insi->insi_ip_ttl = inp->inp_ip_ttl;
140 insi->insi_faddr.ina_6 = inp->inp_dependfaddr.inp6_foreign;
141 insi->insi_laddr.ina_6 = inp->inp_dependladdr.inp6_local;
142 insi->insi_v4.in4_tos = inp->inp_depend4.inp4_ip_tos;
143 insi->insi_v6.in6_hlim = 0;
144 insi->insi_v6.in6_cksum = inp->inp_depend6.inp6_cksum;
145 insi->insi_v6.in6_ifindex = 0;
146 insi->insi_v6.in6_hops = inp->inp_depend6.inp6_hops;
147
148 if (type == SOCK_STREAM && (protocol == 0 ||
149 protocol == IPPROTO_TCP) && inp->inp_ppcb != NULL) {
150 struct tcp_sockinfo *tcpsi = &si->soi_proto.pri_tcp;
151 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
152
153 si->soi_kind = SOCKINFO_TCP;
154
155 tcpsi->tcpsi_state = tp->t_state;
fe8ab488 156 tcpsi->tcpsi_timer[TSI_T_REXMT] =
39236c6e 157 tp->t_timer[TCPT_REXMT];
fe8ab488 158 tcpsi->tcpsi_timer[TSI_T_PERSIST] =
39236c6e 159 tp->t_timer[TCPT_PERSIST];
fe8ab488
A
160 tcpsi->tcpsi_timer[TSI_T_KEEP] =
161 tp->t_timer[TCPT_KEEP];
162 tcpsi->tcpsi_timer[TSI_T_2MSL] =
163 tp->t_timer[TCPT_2MSL];
39236c6e
A
164 tcpsi->tcpsi_mss = tp->t_maxseg;
165 tcpsi->tcpsi_flags = tp->t_flags;
166 tcpsi->tcpsi_tp =
167 (u_int64_t)VM_KERNEL_ADDRPERM(tp);
168 }
169 break;
170 }
171 case PF_UNIX: {
172 struct unpcb *unp = (struct unpcb *)so->so_pcb;
173 struct un_sockinfo *unsi = &si->soi_proto.pri_un;
174
175 si->soi_kind = SOCKINFO_UN;
176
177 unsi->unsi_conn_pcb =
178 (uint64_t)VM_KERNEL_ADDRPERM(unp->unp_conn);
0a7de745 179 if (unp->unp_conn) {
39236c6e
A
180 unsi->unsi_conn_so = (uint64_t)
181 VM_KERNEL_ADDRPERM(unp->unp_conn->unp_socket);
0a7de745 182 }
39236c6e
A
183
184 if (unp->unp_addr) {
0a7de745 185 size_t addrlen = unp->unp_addr->sun_len;
39236c6e 186
0a7de745 187 if (addrlen > SOCK_MAXADDRLEN) {
39236c6e 188 addrlen = SOCK_MAXADDRLEN;
0a7de745 189 }
39236c6e 190 bcopy(unp->unp_addr, &unsi->unsi_addr, addrlen);
0c530ab8 191 }
39236c6e 192 if (unp->unp_conn && unp->unp_conn->unp_addr) {
0a7de745 193 size_t addrlen = unp->unp_conn->unp_addr->sun_len;
39236c6e 194
0a7de745 195 if (addrlen > SOCK_MAXADDRLEN) {
39236c6e 196 addrlen = SOCK_MAXADDRLEN;
0a7de745 197 }
39236c6e
A
198 bcopy(unp->unp_conn->unp_addr, &unsi->unsi_caddr,
199 addrlen);
0c530ab8 200 }
39236c6e
A
201 break;
202 }
203 case PF_NDRV: {
204 struct ndrv_cb *ndrv_cb = (struct ndrv_cb *)so->so_pcb;
205 struct ndrv_info *ndrvsi = &si->soi_proto.pri_ndrv;
206
207 si->soi_kind = SOCKINFO_NDRV;
208
209 /* TDB lock ifnet ???? */
210 if (ndrv_cb->nd_if != 0) {
211 struct ifnet *ifp = ndrv_cb->nd_if;
212
213 ndrvsi->ndrvsi_if_family = ifp->if_family;
214 ndrvsi->ndrvsi_if_unit = ifp->if_unit;
215 strlcpy(ndrvsi->ndrvsi_if_name, ifp->if_name, IFNAMSIZ);
0c530ab8 216 }
39236c6e 217 break;
0c530ab8 218 }
f427ee49
A
219 case PF_VSOCK: {
220 const struct vsockpcb *pcb = (struct vsockpcb *)(so)->so_pcb;
221 struct vsock_sockinfo *vsocksi = &si->soi_proto.pri_vsock;
222
223 si->soi_kind = SOCKINFO_VSOCK;
224
225 vsocksi->local_cid = pcb->local_address.cid;
226 vsocksi->local_port = pcb->local_address.port;
227 vsocksi->remote_cid = pcb->remote_address.cid;
228 vsocksi->remote_port = pcb->remote_address.port;
229
230 break;
231 }
39236c6e
A
232 case PF_SYSTEM:
233 if (SOCK_PROTO(so) == SYSPROTO_EVENT) {
234 struct kern_event_pcb *ev_pcb =
235 (struct kern_event_pcb *)so->so_pcb;
236 struct kern_event_info *kesi =
237 &si->soi_proto.pri_kern_event;
238
239 si->soi_kind = SOCKINFO_KERN_EVENT;
240
241 kesi->kesi_vendor_code_filter =
242 ev_pcb->evp_vendor_code_filter;
243 kesi->kesi_class_filter = ev_pcb->evp_class_filter;
244 kesi->kesi_subclass_filter = ev_pcb->evp_subclass_filter;
39236c6e 245 } else if (SOCK_PROTO(so) == SYSPROTO_CONTROL) {
3e170ce0 246 kctl_fill_socketinfo(so, si);
39236c6e
A
247 }
248 break;
249
250 case PF_ROUTE:
251 case PF_PPP:
252 default:
253 break;
254 }
255out:
0c530ab8 256 socket_unlock(so, 0);
0c530ab8 257
0a7de745 258 return error;
39236c6e 259}