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 #include <net/sockaddr_utils.h>
53
54 static void fill_sockbuf_info(struct sockbuf *sb, struct sockbuf_info *sbi);
55 static void fill_common_sockinfo(struct socket *so, struct socket_info *si);
56
57 static void
fill_sockbuf_info(struct sockbuf * sb,struct sockbuf_info * sbi)58 fill_sockbuf_info(struct sockbuf *sb, struct sockbuf_info *sbi)
59 {
60 sbi->sbi_cc = sb->sb_cc;
61 sbi->sbi_hiwat = sb->sb_hiwat;
62 sbi->sbi_mbcnt = sb->sb_mbcnt;
63 sbi->sbi_mbmax = sb->sb_mbmax;
64 sbi->sbi_lowat = sb->sb_lowat;
65 sbi->sbi_flags = (short)sb->sb_flags;
66 sbi->sbi_timeo = (short)((sb->sb_timeo.tv_sec * hz) +
67 sb->sb_timeo.tv_usec / tick);
68 if (sbi->sbi_timeo == 0 && sb->sb_timeo.tv_usec != 0) {
69 sbi->sbi_timeo = 1;
70 }
71 }
72
73 static void
fill_common_sockinfo(struct socket * so,struct socket_info * si)74 fill_common_sockinfo(struct socket *so, struct socket_info *si)
75 {
76 si->soi_so = (u_int64_t)VM_KERNEL_ADDRHASH(so);
77 si->soi_type = so->so_type;
78 si->soi_options = (short)(so->so_options & 0xffff);
79 si->soi_linger = so->so_linger;
80 si->soi_state = so->so_state;
81 si->soi_pcb = (u_int64_t)VM_KERNEL_ADDRHASH(so->so_pcb);
82 if (so->so_proto) {
83 si->soi_protocol = SOCK_PROTO(so);
84 if (so->so_proto->pr_domain) {
85 si->soi_family = SOCK_DOM(so);
86 } else {
87 si->soi_family = 0;
88 }
89 } else {
90 si->soi_protocol = si->soi_family = 0;
91 }
92 si->soi_qlen = so->so_qlen;
93 si->soi_incqlen = so->so_incqlen;
94 si->soi_qlimit = so->so_qlimit;
95 si->soi_timeo = so->so_timeo;
96 si->soi_error = so->so_error;
97 si->soi_oobmark = so->so_oobmark;
98 fill_sockbuf_info(&so->so_snd, &si->soi_snd);
99 fill_sockbuf_info(&so->so_rcv, &si->soi_rcv);
100 }
101
102 errno_t
fill_socketinfo(struct socket * so,struct socket_info * si)103 fill_socketinfo(struct socket *so, struct socket_info *si)
104 {
105 errno_t error = 0;
106 int domain;
107 short type;
108 short protocol;
109
110 socket_lock(so, 0);
111
112 si->soi_kind = SOCKINFO_GENERIC;
113
114 fill_common_sockinfo(so, si);
115
116 if (so->so_pcb == NULL || so->so_proto == 0 ||
117 so->so_proto->pr_domain == NULL) {
118 goto out;
119 }
120
121 /*
122 * The kind of socket is determined by the triplet
123 * {domain, type, protocol}
124 */
125 domain = SOCK_DOM(so);
126 type = SOCK_TYPE(so);
127 protocol = SOCK_PROTO(so);
128 switch (domain) {
129 case PF_INET:
130 case PF_INET6: {
131 struct in_sockinfo *insi = &si->soi_proto.pri_in;
132 struct inpcb *inp = (struct inpcb *)so->so_pcb;
133
134 si->soi_kind = SOCKINFO_IN;
135
136 insi->insi_fport = inp->inp_fport;
137 insi->insi_lport = inp->inp_lport;
138 insi->insi_gencnt = inp->inp_gencnt;
139 insi->insi_flags = inp->inp_flags;
140 insi->insi_vflag = inp->inp_vflag;
141 insi->insi_ip_ttl = inp->inp_ip_ttl;
142 insi->insi_faddr.ina_6 = inp->inp_dependfaddr.inp6_foreign;
143 insi->insi_laddr.ina_6 = inp->inp_dependladdr.inp6_local;
144 insi->insi_v4.in4_tos = inp->inp_depend4.inp4_ip_tos;
145 insi->insi_v6.in6_hlim = 0;
146 insi->insi_v6.in6_cksum = inp->inp_depend6.inp6_cksum;
147 insi->insi_v6.in6_ifindex = 0;
148 insi->insi_v6.in6_hops = inp->inp_depend6.inp6_hops;
149
150 if (type == SOCK_STREAM && (protocol == 0 ||
151 protocol == IPPROTO_TCP) && inp->inp_ppcb != NULL) {
152 struct tcp_sockinfo *tcpsi = &si->soi_proto.pri_tcp;
153 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
154
155 si->soi_kind = SOCKINFO_TCP;
156
157 tcpsi->tcpsi_state = tp->t_state;
158 tcpsi->tcpsi_timer[TSI_T_REXMT] =
159 tp->t_timer[TCPT_REXMT];
160 tcpsi->tcpsi_timer[TSI_T_PERSIST] =
161 tp->t_timer[TCPT_PERSIST];
162 tcpsi->tcpsi_timer[TSI_T_KEEP] =
163 tp->t_timer[TCPT_KEEP];
164 tcpsi->tcpsi_timer[TSI_T_2MSL] =
165 tp->t_timer[TCPT_2MSL];
166 tcpsi->tcpsi_mss = tp->t_maxseg;
167 tcpsi->tcpsi_flags = tp->t_flags;
168 tcpsi->tcpsi_tp =
169 (u_int64_t)VM_KERNEL_ADDRHASH(tp);
170 }
171 break;
172 }
173 case PF_UNIX: {
174 struct unpcb *unp = (struct unpcb *)so->so_pcb;
175 struct un_sockinfo *unsi = &si->soi_proto.pri_un;
176
177 si->soi_kind = SOCKINFO_UN;
178
179 unsi->unsi_conn_pcb =
180 (uint64_t)VM_KERNEL_ADDRHASH(unp->unp_conn);
181 if (unp->unp_conn) {
182 unsi->unsi_conn_so = (uint64_t)
183 VM_KERNEL_ADDRHASH(unp->unp_conn->unp_socket);
184 }
185
186 if (unp->unp_addr) {
187 size_t addrlen = unp->unp_addr->sun_len;
188
189 if (addrlen > SOCK_MAXADDRLEN) {
190 addrlen = SOCK_MAXADDRLEN;
191 }
192 SOCKADDR_COPY(unp->unp_addr, &unsi->unsi_addr.ua_sun,
193 addrlen);
194 }
195 if (unp->unp_conn && unp->unp_conn->unp_addr) {
196 size_t addrlen = unp->unp_conn->unp_addr->sun_len;
197
198 if (addrlen > SOCK_MAXADDRLEN) {
199 addrlen = SOCK_MAXADDRLEN;
200 }
201 SOCKADDR_COPY(unp->unp_conn->unp_addr, &unsi->unsi_caddr.ua_sun,
202 addrlen);
203 }
204 break;
205 }
206 case PF_NDRV: {
207 struct ndrv_cb *ndrv_cb = (struct ndrv_cb *)so->so_pcb;
208 struct ndrv_info *ndrvsi = &si->soi_proto.pri_ndrv;
209
210 si->soi_kind = SOCKINFO_NDRV;
211
212 /* TDB lock ifnet ???? */
213 if (ndrv_cb->nd_if != 0) {
214 struct ifnet *ifp = ndrv_cb->nd_if;
215
216 ndrvsi->ndrvsi_if_family = ifp->if_family;
217 ndrvsi->ndrvsi_if_unit = ifp->if_unit;
218 strlcpy(ndrvsi->ndrvsi_if_name, ifp->if_name, IFNAMSIZ);
219 }
220 break;
221 }
222 case PF_VSOCK: {
223 const struct vsockpcb *pcb = (struct vsockpcb *)(so)->so_pcb;
224 struct vsock_sockinfo *vsocksi = &si->soi_proto.pri_vsock;
225
226 si->soi_kind = SOCKINFO_VSOCK;
227
228 vsocksi->local_cid = pcb->local_address.cid;
229 vsocksi->local_port = pcb->local_address.port;
230 vsocksi->remote_cid = pcb->remote_address.cid;
231 vsocksi->remote_port = pcb->remote_address.port;
232
233 break;
234 }
235 case PF_SYSTEM:
236 if (SOCK_PROTO(so) == SYSPROTO_EVENT) {
237 struct kern_event_pcb *ev_pcb =
238 (struct kern_event_pcb *)so->so_pcb;
239 struct kern_event_info *kesi =
240 &si->soi_proto.pri_kern_event;
241
242 si->soi_kind = SOCKINFO_KERN_EVENT;
243
244 kesi->kesi_vendor_code_filter =
245 ev_pcb->evp_vendor_code_filter;
246 kesi->kesi_class_filter = ev_pcb->evp_class_filter;
247 kesi->kesi_subclass_filter = ev_pcb->evp_subclass_filter;
248 } else if (SOCK_PROTO(so) == SYSPROTO_CONTROL) {
249 kctl_fill_socketinfo(so, si);
250 }
251 break;
252
253 case PF_ROUTE:
254 case PF_PPP:
255 default:
256 break;
257 }
258 out:
259 socket_unlock(so, 0);
260
261 return error;
262 }
263