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