1 | /* |
2 | * Copyright (c) 2005-2020 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 | #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> |
45 | #include <sys/vsock_domain.h> |
46 | #include <mach/vm_param.h> |
47 | #include <net/ndrv_var.h> |
48 | #include <netinet/in_pcb.h> |
49 | #include <netinet/tcp_var.h> |
50 | #include <string.h> |
51 | |
52 | static void fill_sockbuf_info(struct sockbuf *sb, struct sockbuf_info *sbi); |
53 | static void fill_common_sockinfo(struct socket *so, struct socket_info *si); |
54 | |
55 | static void |
56 | fill_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; |
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); |
66 | if (sbi->sbi_timeo == 0 && sb->sb_timeo.tv_usec != 0) { |
67 | sbi->sbi_timeo = 1; |
68 | } |
69 | } |
70 | |
71 | static void |
72 | fill_common_sockinfo(struct socket *so, struct socket_info *si) |
73 | { |
74 | si->soi_so = (u_int64_t)VM_KERNEL_ADDRHASH(so); |
75 | si->soi_type = so->so_type; |
76 | si->soi_options = (short)(so->so_options & 0xffff); |
77 | si->soi_linger = so->so_linger; |
78 | si->soi_state = so->so_state; |
79 | si->soi_pcb = (u_int64_t)VM_KERNEL_ADDRHASH(so->so_pcb); |
80 | if (so->so_proto) { |
81 | si->soi_protocol = SOCK_PROTO(so); |
82 | if (so->so_proto->pr_domain) { |
83 | si->soi_family = SOCK_DOM(so); |
84 | } else { |
85 | si->soi_family = 0; |
86 | } |
87 | } else { |
88 | si->soi_protocol = si->soi_family = 0; |
89 | } |
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(sb: &so->so_snd, sbi: &si->soi_snd); |
97 | fill_sockbuf_info(sb: &so->so_rcv, sbi: &si->soi_rcv); |
98 | } |
99 | |
100 | errno_t |
101 | fill_socketinfo(struct socket *so, struct socket_info *si) |
102 | { |
103 | errno_t error = 0; |
104 | int domain; |
105 | short type; |
106 | short protocol; |
107 | |
108 | socket_lock(so, refcount: 0); |
109 | |
110 | si->soi_kind = SOCKINFO_GENERIC; |
111 | |
112 | fill_common_sockinfo(so, si); |
113 | |
114 | if (so->so_pcb == NULL || so->so_proto == 0 || |
115 | so->so_proto->pr_domain == NULL) { |
116 | goto out; |
117 | } |
118 | |
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; |
156 | tcpsi->tcpsi_timer[TSI_T_REXMT] = |
157 | tp->t_timer[TCPT_REXMT]; |
158 | tcpsi->tcpsi_timer[TSI_T_PERSIST] = |
159 | tp->t_timer[TCPT_PERSIST]; |
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]; |
164 | tcpsi->tcpsi_mss = tp->t_maxseg; |
165 | tcpsi->tcpsi_flags = tp->t_flags; |
166 | tcpsi->tcpsi_tp = |
167 | (u_int64_t)VM_KERNEL_ADDRHASH(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_ADDRHASH(unp->unp_conn); |
179 | if (unp->unp_conn) { |
180 | unsi->unsi_conn_so = (uint64_t) |
181 | VM_KERNEL_ADDRHASH(unp->unp_conn->unp_socket); |
182 | } |
183 | |
184 | if (unp->unp_addr) { |
185 | size_t addrlen = unp->unp_addr->sun_len; |
186 | |
187 | if (addrlen > SOCK_MAXADDRLEN) { |
188 | addrlen = SOCK_MAXADDRLEN; |
189 | } |
190 | bcopy(src: unp->unp_addr, dst: &unsi->unsi_addr, n: addrlen); |
191 | } |
192 | if (unp->unp_conn && unp->unp_conn->unp_addr) { |
193 | size_t addrlen = unp->unp_conn->unp_addr->sun_len; |
194 | |
195 | if (addrlen > SOCK_MAXADDRLEN) { |
196 | addrlen = SOCK_MAXADDRLEN; |
197 | } |
198 | bcopy(src: unp->unp_conn->unp_addr, dst: &unsi->unsi_caddr, |
199 | n: addrlen); |
200 | } |
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(dst: ndrvsi->ndrvsi_if_name, src: ifp->if_name, IFNAMSIZ); |
216 | } |
217 | break; |
218 | } |
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 | } |
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; |
245 | } else if (SOCK_PROTO(so) == SYSPROTO_CONTROL) { |
246 | kctl_fill_socketinfo(so, si); |
247 | } |
248 | break; |
249 | |
250 | case PF_ROUTE: |
251 | case PF_PPP: |
252 | default: |
253 | break; |
254 | } |
255 | out: |
256 | socket_unlock(so, refcount: 0); |
257 | |
258 | return error; |
259 | } |
260 | |