uipc_domain.c revision 1.64 1 /* $NetBSD: uipc_domain.c,v 1.64 2007/05/02 20:40:23 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.64 2007/05/02 20:40:23 dyoung Exp $");
36
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/protosw.h>
41 #include <sys/domain.h>
42 #include <sys/mbuf.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/queue.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 #include <sys/un.h>
51 #include <sys/unpcb.h>
52 #include <sys/file.h>
53 #include <sys/kauth.h>
54
55 void pffasttimo(void *);
56 void pfslowtimo(void *);
57
58 struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
59 static struct domain *domain_array[AF_MAX];
60
61 struct callout pffasttimo_ch, pfslowtimo_ch;
62
63 /*
64 * Current time values for fast and slow timeouts. We can use u_int
65 * relatively safely. The fast timer will roll over in 27 years and
66 * the slow timer in 68 years.
67 */
68 u_int pfslowtimo_now;
69 u_int pffasttimo_now;
70
71 void
72 domaininit(void)
73 {
74 __link_set_decl(domains, struct domain);
75 struct domain * const * dpp;
76 struct domain *rt_domain = NULL;
77
78 /*
79 * Add all of the domains. Make sure the PF_ROUTE
80 * domain is added last.
81 */
82 __link_set_foreach(dpp, domains) {
83 if ((*dpp)->dom_family == PF_ROUTE)
84 rt_domain = *dpp;
85 else
86 domain_attach(*dpp);
87 }
88 if (rt_domain)
89 domain_attach(rt_domain);
90
91 callout_init(&pffasttimo_ch);
92 callout_init(&pfslowtimo_ch);
93
94 callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
95 callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
96 }
97
98 void
99 domain_attach(struct domain *dp)
100 {
101 const struct protosw *pr;
102
103 STAILQ_INSERT_TAIL(&domains, dp, dom_link);
104 if (dp->dom_family < __arraycount(domain_array))
105 domain_array[dp->dom_family] = dp;
106
107 if (dp->dom_init)
108 (*dp->dom_init)();
109
110 #ifdef MBUFTRACE
111 if (dp->dom_mowner.mo_name[0] == '\0') {
112 strncpy(dp->dom_mowner.mo_name, dp->dom_name,
113 sizeof(dp->dom_mowner.mo_name));
114 MOWNER_ATTACH(&dp->dom_mowner);
115 }
116 #endif
117 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
118 if (pr->pr_init)
119 (*pr->pr_init)();
120 }
121
122 if (max_linkhdr < 16) /* XXX */
123 max_linkhdr = 16;
124 max_hdr = max_linkhdr + max_protohdr;
125 max_datalen = MHLEN - max_hdr;
126 }
127
128 struct domain *
129 pffinddomain(int family)
130 {
131 struct domain *dp;
132
133 if (family < __arraycount(domain_array) && domain_array[family] != NULL)
134 return domain_array[family];
135
136 DOMAIN_FOREACH(dp)
137 if (dp->dom_family == family)
138 return (dp);
139 return (NULL);
140 }
141
142 const struct protosw *
143 pffindtype(int family, int type)
144 {
145 struct domain *dp;
146 const struct protosw *pr;
147
148 dp = pffinddomain(family);
149 if (dp == NULL)
150 return (NULL);
151
152 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
153 if (pr->pr_type && pr->pr_type == type)
154 return (pr);
155
156 return (NULL);
157 }
158
159 const struct protosw *
160 pffindproto(int family, int protocol, int type)
161 {
162 struct domain *dp;
163 const struct protosw *pr;
164 const struct protosw *maybe = NULL;
165
166 if (family == 0)
167 return (NULL);
168
169 dp = pffinddomain(family);
170 if (dp == NULL)
171 return (NULL);
172
173 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
174 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
175 return (pr);
176
177 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
178 pr->pr_protocol == 0 && maybe == NULL)
179 maybe = pr;
180 }
181 return (maybe);
182 }
183
184 struct sockaddr *
185 sockaddr_alloc(sa_family_t af, int flags)
186 {
187 const struct domain *dom;
188 struct sockaddr *sa;
189
190 if ((dom = pffinddomain(af)) == NULL)
191 return NULL;
192
193 if ((sa = pool_get(dom->dom_sa_pool, flags)) == NULL)
194 return NULL;
195
196 sa->sa_family = af;
197 sa->sa_len = dom->dom_sa_len;
198 return sa;
199 }
200
201 struct sockaddr *
202 sockaddr_copy(struct sockaddr *dst, const struct sockaddr *src)
203 {
204 KASSERT(dst->sa_family == src->sa_family);
205 memcpy(dst, src, src->sa_len);
206 return dst;
207 }
208
209 int
210 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
211 {
212 int rc;
213 struct domain *dom;
214
215 if (sa1->sa_family != sa2->sa_family)
216 return sa1->sa_family - sa2->sa_family;
217
218 if ((dom = pffinddomain(sa1->sa_family)) != NULL &&
219 dom->dom_sockaddr_cmp != NULL)
220 return (*dom->dom_sockaddr_cmp)(sa1, sa2);
221
222 if ((rc = memcmp(sa1, sa2, MIN(sa1->sa_len, sa2->sa_len))) != 0)
223 return rc;
224
225 return sa1->sa_len - sa2->sa_len;
226 }
227
228 struct sockaddr *
229 sockaddr_dup(const struct sockaddr *src, int flags)
230 {
231 struct sockaddr *dst;
232
233 if ((dst = sockaddr_alloc(src->sa_family, flags)) == NULL)
234 return NULL;
235
236 KASSERT(dst->sa_len >= src->sa_len);
237
238 return sockaddr_copy(dst, src);
239 }
240
241 void
242 sockaddr_free(struct sockaddr *sa)
243 {
244 const struct domain *dom;
245
246 if ((dom = pffinddomain(sa->sa_family)) == NULL)
247 panic("%s: no such domain %d\n", __func__, sa->sa_family);
248
249 pool_put(dom->dom_sa_pool, sa);
250 }
251
252 /*
253 * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
254 */
255 static void
256 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
257 {
258 struct unpcb *unp = sotounpcb(so);
259 struct sockaddr_un *un = unp->unp_addr;
260
261 memset(pcb, 0, sizeof(*pcb));
262
263 pcb->ki_family = so->so_proto->pr_domain->dom_family;
264 pcb->ki_type = so->so_proto->pr_type;
265 pcb->ki_protocol = so->so_proto->pr_protocol;
266 pcb->ki_pflags = unp->unp_flags;
267
268 pcb->ki_pcbaddr = PTRTOUINT64(unp);
269 /* pcb->ki_ppcbaddr = unp has no ppcb... */
270 pcb->ki_sockaddr = PTRTOUINT64(so);
271
272 pcb->ki_sostate = so->so_state;
273 /* pcb->ki_prstate = unp has no state... */
274
275 pcb->ki_rcvq = so->so_rcv.sb_cc;
276 pcb->ki_sndq = so->so_snd.sb_cc;
277
278 un = (struct sockaddr_un *)&pcb->ki_src;
279 /*
280 * local domain sockets may bind without having a local
281 * endpoint. bleah!
282 */
283 if (unp->unp_addr != NULL) {
284 un->sun_len = unp->unp_addr->sun_len;
285 un->sun_family = unp->unp_addr->sun_family;
286 strlcpy(un->sun_path, unp->unp_addr->sun_path,
287 sizeof(pcb->ki_s));
288 }
289 else {
290 un->sun_len = offsetof(struct sockaddr_un, sun_path);
291 un->sun_family = pcb->ki_family;
292 }
293 if (unp->unp_conn != NULL) {
294 un = (struct sockaddr_un *)&pcb->ki_dst;
295 if (unp->unp_conn->unp_addr != NULL) {
296 un->sun_len = unp->unp_conn->unp_addr->sun_len;
297 un->sun_family = unp->unp_conn->unp_addr->sun_family;
298 un->sun_family = unp->unp_conn->unp_addr->sun_family;
299 strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
300 sizeof(pcb->ki_d));
301 }
302 else {
303 un->sun_len = offsetof(struct sockaddr_un, sun_path);
304 un->sun_family = pcb->ki_family;
305 }
306 }
307
308 pcb->ki_inode = unp->unp_ino;
309 pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
310 pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
311 pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
312 pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
313 }
314
315 static int
316 sysctl_unpcblist(SYSCTLFN_ARGS)
317 {
318 struct file *fp;
319 struct socket *so;
320 struct kinfo_pcb pcb;
321 char *dp;
322 u_int op, arg;
323 size_t len, needed, elem_size, out_size;
324 int error, elem_count, pf, type, pf2;
325
326 if (namelen == 1 && name[0] == CTL_QUERY)
327 return (sysctl_query(SYSCTLFN_CALL(rnode)));
328
329 if (namelen != 4)
330 return (EINVAL);
331
332 if (oldp != NULL) {
333 len = *oldlenp;
334 elem_size = name[2];
335 elem_count = name[3];
336 if (elem_size != sizeof(pcb))
337 return EINVAL;
338 } else {
339 len = 0;
340 elem_size = sizeof(pcb);
341 elem_count = INT_MAX;
342 }
343 error = 0;
344 dp = oldp;
345 op = name[0];
346 arg = name[1];
347 out_size = elem_size;
348 needed = 0;
349
350 if (name - oname != 4)
351 return (EINVAL);
352
353 pf = oname[1];
354 type = oname[2];
355 pf2 = (oldp == NULL) ? 0 : pf;
356
357 /*
358 * there's no "list" of local domain sockets, so we have
359 * to walk the file list looking for them. :-/
360 */
361 LIST_FOREACH(fp, &filehead, f_list) {
362 if (kauth_authorize_generic(l->l_cred,
363 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
364 continue;
365 if (fp->f_type != DTYPE_SOCKET)
366 continue;
367 so = (struct socket *)fp->f_data;
368 if (so->so_type != type)
369 continue;
370 if (so->so_proto->pr_domain->dom_family != pf)
371 continue;
372 if (len >= elem_size && elem_count > 0) {
373 sysctl_dounpcb(&pcb, so);
374 error = copyout(&pcb, dp, out_size);
375 if (error)
376 break;
377 dp += elem_size;
378 len -= elem_size;
379 }
380 if (elem_count > 0) {
381 needed += elem_size;
382 if (elem_count != INT_MAX)
383 elem_count--;
384 }
385 }
386
387 *oldlenp = needed;
388 if (oldp == NULL)
389 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
390
391 return (error);
392 }
393
394 SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
395 {
396 sysctl_createv(clog, 0, NULL, NULL,
397 CTLFLAG_PERMANENT,
398 CTLTYPE_NODE, "net", NULL,
399 NULL, 0, NULL, 0,
400 CTL_NET, CTL_EOL);
401 sysctl_createv(clog, 0, NULL, NULL,
402 CTLFLAG_PERMANENT,
403 CTLTYPE_NODE, "local",
404 SYSCTL_DESCR("PF_LOCAL related settings"),
405 NULL, 0, NULL, 0,
406 CTL_NET, PF_LOCAL, CTL_EOL);
407 sysctl_createv(clog, 0, NULL, NULL,
408 CTLFLAG_PERMANENT,
409 CTLTYPE_NODE, "stream",
410 SYSCTL_DESCR("SOCK_STREAM settings"),
411 NULL, 0, NULL, 0,
412 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
413 sysctl_createv(clog, 0, NULL, NULL,
414 CTLFLAG_PERMANENT,
415 CTLTYPE_NODE, "dgram",
416 SYSCTL_DESCR("SOCK_DGRAM settings"),
417 NULL, 0, NULL, 0,
418 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
419
420 sysctl_createv(clog, 0, NULL, NULL,
421 CTLFLAG_PERMANENT,
422 CTLTYPE_STRUCT, "pcblist",
423 SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
424 sysctl_unpcblist, 0, NULL, 0,
425 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
426 sysctl_createv(clog, 0, NULL, NULL,
427 CTLFLAG_PERMANENT,
428 CTLTYPE_STRUCT, "pcblist",
429 SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
430 sysctl_unpcblist, 0, NULL, 0,
431 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
432 }
433
434 void
435 pfctlinput(int cmd, const struct sockaddr *sa)
436 {
437 struct domain *dp;
438 const struct protosw *pr;
439
440 DOMAIN_FOREACH(dp) {
441 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
442 if (pr->pr_ctlinput != NULL)
443 (*pr->pr_ctlinput)(cmd, sa, NULL);
444 }
445 }
446 }
447
448 void
449 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
450 {
451 struct domain *dp;
452 const struct protosw *pr;
453
454 if (sa == NULL)
455 return;
456
457 DOMAIN_FOREACH(dp) {
458 /*
459 * the check must be made by xx_ctlinput() anyways, to
460 * make sure we use data item pointed to by ctlparam in
461 * correct way. the following check is made just for safety.
462 */
463 if (dp->dom_family != sa->sa_family)
464 continue;
465
466 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
467 if (pr->pr_ctlinput != NULL)
468 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
469 }
470 }
471 }
472
473 void
474 pfslowtimo(void *arg)
475 {
476 struct domain *dp;
477 const struct protosw *pr;
478
479 pfslowtimo_now++;
480
481 DOMAIN_FOREACH(dp) {
482 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
483 if (pr->pr_slowtimo)
484 (*pr->pr_slowtimo)();
485 }
486 callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
487 }
488
489 void
490 pffasttimo(void *arg)
491 {
492 struct domain *dp;
493 const struct protosw *pr;
494
495 pffasttimo_now++;
496
497 DOMAIN_FOREACH(dp) {
498 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
499 if (pr->pr_fasttimo)
500 (*pr->pr_fasttimo)();
501 }
502 callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
503 }
504