]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/socket_info.c
b4171b5076e8f5ee27c77fb7adbba0a8304e2b79
[apple/xnu.git] / bsd / kern / socket_info.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <sys/types.h>
32 #include <sys/kernel_types.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/file_internal.h>
36 #include <sys/stat.h>
37 #include <sys/select.h>
38 #include <sys/pipe.h>
39 #include <sys/proc_info.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/domain.h>
43 #include <sys/socketvar.h>
44 #include <sys/unpcb.h>
45 #include <sys/sys_domain.h>
46 #include <sys/kern_event.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 = sb->sb_flags;
64 sbi->sbi_timeo = (u_long)(sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick;
65 if (sbi->sbi_timeo == 0 && sb->sb_timeo.tv_usec != 0)
66 sbi->sbi_timeo = 1;
67 }
68
69 static void
70 fill_common_sockinfo(struct socket *so, struct socket_info *si)
71 {
72 si->soi_so = (u_int64_t)((uintptr_t)so);
73 si->soi_type = so->so_type;
74 si->soi_options = so->so_options;
75 si->soi_linger = so->so_linger;
76 si->soi_state = so->so_state;
77 si->soi_pcb = (u_int64_t)((uintptr_t)so->so_pcb);
78 if (so->so_proto) {
79 si->soi_protocol = so->so_proto->pr_protocol;
80 if (so->so_proto->pr_domain)
81 si->soi_family = so->so_proto->pr_domain->dom_family;
82 else
83 si->soi_family = 0;
84 } else
85 si->soi_protocol = si->soi_family = 0;
86 si->soi_qlen = so->so_qlen;
87 si->soi_incqlen = so->so_incqlen;
88 si->soi_qlimit = so->so_qlimit;
89 si->soi_timeo = so->so_timeo;
90 si->soi_error = so->so_error;
91 si->soi_oobmark = so->so_oobmark;
92 fill_sockbuf_info(&so->so_snd, &si->soi_snd);
93 fill_sockbuf_info(&so->so_rcv, &si->soi_rcv);
94
95 }
96
97 errno_t
98 fill_socketinfo(struct socket *so, struct socket_info *si)
99 {
100 errno_t error = 0;
101 int family;
102 short type;
103 short protocol;
104
105 socket_lock(so, 0);
106
107 si->soi_kind = SOCKINFO_GENERIC;
108
109 fill_common_sockinfo(so, si);
110
111 if (so->so_pcb == 0 || so->so_proto == 0 || so->so_proto->pr_domain == 0)
112 goto out;
113
114 /* The kind of socket is determined by the triplet {family, type, protocol} */
115 family = so->so_proto->pr_domain->dom_family;
116 type = so->so_proto->pr_type;
117 protocol = so->so_proto->pr_protocol;
118 switch (family) {
119 case AF_INET:
120 case AF_INET6: {
121 struct in_sockinfo *insi = &si->soi_proto.pri_in;
122 struct inpcb *inp = (struct inpcb *)so->so_pcb;
123
124 si->soi_kind = SOCKINFO_IN;
125
126 insi->insi_fport = inp->inp_fport;
127 insi->insi_lport = inp->inp_lport;
128 insi->insi_gencnt = inp->inp_gencnt;
129 insi->insi_flags = inp->inp_flags;
130 insi->insi_vflag = inp->inp_vflag;
131 insi->insi_ip_ttl = inp->inp_ip_ttl;
132 insi->insi_faddr.ina_6 = inp->inp_dependfaddr.inp6_foreign;
133 insi->insi_laddr.ina_6 = inp->inp_dependladdr.inp6_local;
134 insi->insi_v4.in4_tos = inp->inp_depend4.inp4_ip_tos;
135 insi->insi_v6.in6_hlim = inp->inp_depend6.inp6_hlim;
136 insi->insi_v6.in6_cksum = inp->inp_depend6.inp6_cksum;
137 insi->insi_v6.in6_ifindex = inp->inp6_ifindex;
138 insi->insi_v6.in6_hops = inp->inp_depend6.inp6_hops;
139
140 if (type == SOCK_STREAM && (protocol == 0 || protocol == IPPROTO_TCP) && inp->inp_ppcb != 0) {
141 struct tcp_sockinfo *tcpsi = &si->soi_proto.pri_tcp;
142 struct tcpcb *tp= (struct tcpcb *)inp->inp_ppcb;
143
144 si->soi_kind = SOCKINFO_TCP;
145
146 tcpsi->tcpsi_state = tp->t_state;
147 tcpsi->tcpsi_timer[TCPT_REXMT] = tp->t_timer[TCPT_REXMT];
148 tcpsi->tcpsi_timer[TCPT_PERSIST] = tp->t_timer[TCPT_PERSIST];
149 tcpsi->tcpsi_timer[TCPT_KEEP] = tp->t_timer[TCPT_KEEP];
150 tcpsi->tcpsi_timer[TCPT_2MSL] = tp->t_timer[TCPT_2MSL];
151 tcpsi->tcpsi_mss = tp->t_maxseg;
152 tcpsi->tcpsi_flags = tp->t_flags;
153 tcpsi->tcpsi_tp = (u_int64_t)((uintptr_t)tp);
154 }
155 break;
156 }
157 case AF_UNIX: {
158 struct unpcb *unp = (struct unpcb *)so->so_pcb;
159 struct un_sockinfo *unsi = &si->soi_proto.pri_un;
160
161 si->soi_kind = SOCKINFO_UN;
162
163 unsi->unsi_conn_pcb = (uint64_t)((uintptr_t)unp->unp_conn);
164 if (unp->unp_conn)
165 unsi->unsi_conn_so = (uint64_t)((uintptr_t)unp->unp_conn->unp_socket);
166
167
168 if (unp->unp_addr) {
169 size_t addrlen = unp->unp_addr->sun_len;
170
171 if (addrlen > SOCK_MAXADDRLEN)
172 addrlen = SOCK_MAXADDRLEN;
173 bcopy(unp->unp_addr, &unsi->unsi_addr, addrlen);
174 }
175 if (unp->unp_conn && unp->unp_conn->unp_addr) {
176 size_t addrlen = unp->unp_conn->unp_addr->sun_len;
177
178 if (addrlen > SOCK_MAXADDRLEN)
179 addrlen = SOCK_MAXADDRLEN;
180 bcopy(unp->unp_conn->unp_addr, &unsi->unsi_caddr, addrlen);
181 }
182 break;
183 }
184 case AF_NDRV: {
185 struct ndrv_cb *ndrv_cb = (struct ndrv_cb *)so->so_pcb;
186 struct ndrv_info *ndrvsi = &si->soi_proto.pri_ndrv;
187
188 si->soi_kind = SOCKINFO_NDRV;
189
190 /* TDB lock ifnet ???? */
191 if (ndrv_cb->nd_if != 0) {
192 struct ifnet *ifp = ndrv_cb->nd_if;
193
194 ndrvsi->ndrvsi_if_family = ifp->if_family;
195 ndrvsi->ndrvsi_if_unit = ifp->if_unit;
196 strncpy(ndrvsi->ndrvsi_if_name, ifp->if_name, IFNAMSIZ);
197 }
198
199 break;
200 }
201 case AF_SYSTEM:
202 if (so->so_proto->pr_protocol == SYSPROTO_EVENT) {
203 struct kern_event_pcb *ev_pcb = (struct kern_event_pcb *)so->so_pcb;
204 struct kern_event_info *kesi = &si->soi_proto.pri_kern_event;
205
206 si->soi_kind = SOCKINFO_KERN_EVENT;
207
208 kesi->kesi_vendor_code_filter = ev_pcb->vendor_code_filter;
209 kesi->kesi_class_filter = ev_pcb->class_filter;
210 kesi->kesi_subclass_filter = ev_pcb->subclass_filter;
211
212 } else if (so->so_proto->pr_protocol == SYSPROTO_CONTROL) {
213 struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb;
214 struct kern_ctl_info *kcsi = &si->soi_proto.pri_kern_ctl;
215 struct kctl *kctl = kcb->kctl;
216
217
218 si->soi_kind = SOCKINFO_KERN_CTL;
219
220 if (kctl == 0)
221 break;
222 kcsi->kcsi_id = kctl->id;
223 kcsi->kcsi_reg_unit = kctl->id;
224 kcsi->kcsi_flags = kctl->flags;
225 kcsi->kcsi_recvbufsize = kctl->recvbufsize;
226 kcsi->kcsi_sendbufsize = kctl->sendbufsize;
227 kcsi->kcsi_unit = kcb->unit;
228 strncpy(kcsi->kcsi_name, kctl->name, MAX_KCTL_NAME);
229 }
230 break;
231
232 case AF_APPLETALK:
233 break;
234
235 case AF_ROUTE:
236 break;
237
238 case AF_PPP:
239 break;
240
241 default:
242 break;
243 }
244 out:
245 socket_unlock(so, 0);
246
247 return error;
248 }
249