1 /* $NetBSD: uipc_domain.c,v 1.76 2008/04/24 11:38:36 ad 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.76 2008/04/24 11:38:36 ad 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/filedesc.h>
54 #include <sys/kauth.h>
55
56 MALLOC_DECLARE(M_SOCKADDR);
57
58 MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
59
60 void pffasttimo(void *);
61 void pfslowtimo(void *);
62
63 struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
64 static struct domain *domain_array[AF_MAX];
65
66 callout_t pffasttimo_ch, pfslowtimo_ch;
67
68 /*
69 * Current time values for fast and slow timeouts. We can use u_int
70 * relatively safely. The fast timer will roll over in 27 years and
71 * the slow timer in 68 years.
72 */
73 u_int pfslowtimo_now;
74 u_int pffasttimo_now;
75
76 void
77 domaininit(void)
78 {
79 __link_set_decl(domains, struct domain);
80 struct domain * const * dpp;
81 struct domain *rt_domain = NULL;
82
83 /*
84 * Add all of the domains. Make sure the PF_ROUTE
85 * domain is added last.
86 */
87 __link_set_foreach(dpp, domains) {
88 if ((*dpp)->dom_family == PF_ROUTE)
89 rt_domain = *dpp;
90 else
91 domain_attach(*dpp);
92 }
93 if (rt_domain)
94 domain_attach(rt_domain);
95
96 callout_init(&pffasttimo_ch, CALLOUT_MPSAFE);
97 callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE);
98
99 callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
100 callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
101 }
102
103 void
104 domain_attach(struct domain *dp)
105 {
106 const struct protosw *pr;
107
108 STAILQ_INSERT_TAIL(&domains, dp, dom_link);
109 if (dp->dom_family < __arraycount(domain_array))
110 domain_array[dp->dom_family] = dp;
111
112 if (dp->dom_init)
113 (*dp->dom_init)();
114
115 #ifdef MBUFTRACE
116 if (dp->dom_mowner.mo_name[0] == '\0') {
117 strncpy(dp->dom_mowner.mo_name, dp->dom_name,
118 sizeof(dp->dom_mowner.mo_name));
119 MOWNER_ATTACH(&dp->dom_mowner);
120 }
121 #endif
122 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
123 if (pr->pr_init)
124 (*pr->pr_init)();
125 }
126
127 if (max_linkhdr < 16) /* XXX */
128 max_linkhdr = 16;
129 max_hdr = max_linkhdr + max_protohdr;
130 max_datalen = MHLEN - max_hdr;
131 }
132
133 struct domain *
134 pffinddomain(int family)
135 {
136 struct domain *dp;
137
138 if (family < __arraycount(domain_array) && domain_array[family] != NULL)
139 return domain_array[family];
140
141 DOMAIN_FOREACH(dp)
142 if (dp->dom_family == family)
143 return (dp);
144 return (NULL);
145 }
146
147 const struct protosw *
148 pffindtype(int family, int type)
149 {
150 struct domain *dp;
151 const struct protosw *pr;
152
153 dp = pffinddomain(family);
154 if (dp == NULL)
155 return (NULL);
156
157 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
158 if (pr->pr_type && pr->pr_type == type)
159 return (pr);
160
161 return (NULL);
162 }
163
164 const struct protosw *
165 pffindproto(int family, int protocol, int type)
166 {
167 struct domain *dp;
168 const struct protosw *pr;
169 const struct protosw *maybe = NULL;
170
171 if (family == 0)
172 return (NULL);
173
174 dp = pffinddomain(family);
175 if (dp == NULL)
176 return (NULL);
177
178 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
179 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
180 return (pr);
181
182 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
183 pr->pr_protocol == 0 && maybe == NULL)
184 maybe = pr;
185 }
186 return (maybe);
187 }
188
189 void *
190 sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
191 {
192 const struct domain *dom;
193
194 if ((dom = pffinddomain(sa->sa_family)) == NULL ||
195 dom->dom_sockaddr_addr == NULL)
196 return NULL;
197
198 return (*dom->dom_sockaddr_addr)(sa, slenp);
199 }
200
201 const void *
202 sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
203 {
204 const struct domain *dom;
205
206 if ((dom = pffinddomain(sa->sa_family)) == NULL ||
207 dom->dom_sockaddr_const_addr == NULL)
208 return NULL;
209
210 return (*dom->dom_sockaddr_const_addr)(sa, slenp);
211 }
212
213 const struct sockaddr *
214 sockaddr_any(const struct sockaddr *sa)
215 {
216 const struct domain *dom;
217
218 if ((dom = pffinddomain(sa->sa_family)) == NULL)
219 return NULL;
220
221 return dom->dom_sa_any;
222 }
223
224 const void *
225 sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
226 {
227 const struct sockaddr *any;
228
229 if ((any = sockaddr_any(sa)) == NULL)
230 return NULL;
231
232 return sockaddr_const_addr(any, slenp);
233 }
234
235 struct sockaddr *
236 sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
237 {
238 struct sockaddr *sa;
239 socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
240
241 if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
242 return NULL;
243
244 sa->sa_family = af;
245 sa->sa_len = reallen;
246 return sa;
247 }
248
249 struct sockaddr *
250 sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
251 const struct sockaddr *src)
252 {
253 if (__predict_false(socklen < src->sa_len)) {
254 panic("%s: source too long, %d < %d bytes", __func__, socklen,
255 src->sa_len);
256 }
257 return memcpy(dst, src, src->sa_len);
258 }
259
260 int
261 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
262 {
263 int len, rc;
264 struct domain *dom;
265
266 if (sa1->sa_family != sa2->sa_family)
267 return sa1->sa_family - sa2->sa_family;
268
269 dom = pffinddomain(sa1->sa_family);
270
271 if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
272 return (*dom->dom_sockaddr_cmp)(sa1, sa2);
273
274 len = MIN(sa1->sa_len, sa2->sa_len);
275
276 if (dom == NULL || dom->dom_sa_cmplen == 0) {
277 if ((rc = memcmp(sa1, sa2, len)) != 0)
278 return rc;
279 return sa1->sa_len - sa2->sa_len;
280 }
281
282 if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
283 (const char *)sa2 + dom->dom_sa_cmpofs,
284 MIN(dom->dom_sa_cmplen,
285 len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
286 return rc;
287
288 return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
289 MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
290 }
291
292 struct sockaddr *
293 sockaddr_dup(const struct sockaddr *src, int flags)
294 {
295 struct sockaddr *dst;
296
297 if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
298 return NULL;
299
300 return sockaddr_copy(dst, dst->sa_len, src);
301 }
302
303 void
304 sockaddr_free(struct sockaddr *sa)
305 {
306 free(sa, M_SOCKADDR);
307 }
308
309 /*
310 * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
311 */
312 static void
313 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
314 {
315 struct unpcb *unp = sotounpcb(so);
316 struct sockaddr_un *un = unp->unp_addr;
317
318 memset(pcb, 0, sizeof(*pcb));
319
320 pcb->ki_family = so->so_proto->pr_domain->dom_family;
321 pcb->ki_type = so->so_proto->pr_type;
322 pcb->ki_protocol = so->so_proto->pr_protocol;
323 pcb->ki_pflags = unp->unp_flags;
324
325 pcb->ki_pcbaddr = PTRTOUINT64(unp);
326 /* pcb->ki_ppcbaddr = unp has no ppcb... */
327 pcb->ki_sockaddr = PTRTOUINT64(so);
328
329 pcb->ki_sostate = so->so_state;
330 /* pcb->ki_prstate = unp has no state... */
331
332 pcb->ki_rcvq = so->so_rcv.sb_cc;
333 pcb->ki_sndq = so->so_snd.sb_cc;
334
335 un = (struct sockaddr_un *)&pcb->ki_src;
336 /*
337 * local domain sockets may bind without having a local
338 * endpoint. bleah!
339 */
340 if (unp->unp_addr != NULL) {
341 un->sun_len = unp->unp_addr->sun_len;
342 un->sun_family = unp->unp_addr->sun_family;
343 strlcpy(un->sun_path, unp->unp_addr->sun_path,
344 sizeof(pcb->ki_s));
345 }
346 else {
347 un->sun_len = offsetof(struct sockaddr_un, sun_path);
348 un->sun_family = pcb->ki_family;
349 }
350 if (unp->unp_conn != NULL) {
351 un = (struct sockaddr_un *)&pcb->ki_dst;
352 if (unp->unp_conn->unp_addr != NULL) {
353 un->sun_len = unp->unp_conn->unp_addr->sun_len;
354 un->sun_family = unp->unp_conn->unp_addr->sun_family;
355 un->sun_family = unp->unp_conn->unp_addr->sun_family;
356 strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
357 sizeof(pcb->ki_d));
358 }
359 else {
360 un->sun_len = offsetof(struct sockaddr_un, sun_path);
361 un->sun_family = pcb->ki_family;
362 }
363 }
364
365 pcb->ki_inode = unp->unp_ino;
366 pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
367 pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
368 pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
369 pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
370 }
371
372 static int
373 sysctl_unpcblist(SYSCTLFN_ARGS)
374 {
375 struct file *fp, *dfp, *np;
376 struct socket *so;
377 struct kinfo_pcb pcb;
378 char *dp;
379 u_int op, arg;
380 size_t len, needed, elem_size, out_size;
381 int error, elem_count, pf, type, pf2;
382
383 if (namelen == 1 && name[0] == CTL_QUERY)
384 return (sysctl_query(SYSCTLFN_CALL(rnode)));
385
386 if (namelen != 4)
387 return (EINVAL);
388
389 if (oldp != NULL) {
390 len = *oldlenp;
391 elem_size = name[2];
392 elem_count = name[3];
393 if (elem_size != sizeof(pcb))
394 return EINVAL;
395 } else {
396 len = 0;
397 elem_size = sizeof(pcb);
398 elem_count = INT_MAX;
399 }
400 error = 0;
401 dp = oldp;
402 op = name[0];
403 arg = name[1];
404 out_size = elem_size;
405 needed = 0;
406
407 if (name - oname != 4)
408 return (EINVAL);
409
410 pf = oname[1];
411 type = oname[2];
412 pf2 = (oldp == NULL) ? 0 : pf;
413
414 /*
415 * allocate dummy file descriptor to make position in list.
416 */
417 sysctl_unlock();
418 if ((dfp = fgetdummy()) == NULL) {
419 sysctl_relock();
420 return ENOMEM;
421 }
422
423 /*
424 * there's no "list" of local domain sockets, so we have
425 * to walk the file list looking for them. :-/
426 */
427 mutex_enter(&filelist_lock);
428 LIST_FOREACH(fp, &filehead, f_list) {
429 np = LIST_NEXT(fp, f_list);
430 if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
431 fp->f_data == NULL)
432 continue;
433 if (kauth_authorize_generic(l->l_cred,
434 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
435 continue;
436 so = (struct socket *)fp->f_data;
437 if (so->so_type != type)
438 continue;
439 if (so->so_proto->pr_domain->dom_family != pf)
440 continue;
441 if (len >= elem_size && elem_count > 0) {
442 mutex_enter(&fp->f_lock);
443 fp->f_count++;
444 mutex_exit(&fp->f_lock);
445 LIST_INSERT_AFTER(fp, dfp, f_list);
446 mutex_exit(&filelist_lock);
447 sysctl_dounpcb(&pcb, so);
448 error = copyout(&pcb, dp, out_size);
449 closef(fp);
450 mutex_enter(&filelist_lock);
451 np = LIST_NEXT(dfp, f_list);
452 LIST_REMOVE(dfp, f_list);
453 if (error)
454 break;
455 dp += elem_size;
456 len -= elem_size;
457 }
458 if (elem_count > 0) {
459 needed += elem_size;
460 if (elem_count != INT_MAX)
461 elem_count--;
462 }
463 }
464 mutex_exit(&filelist_lock);
465 fputdummy(dfp);
466 *oldlenp = needed;
467 if (oldp == NULL)
468 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
469 sysctl_relock();
470
471 return (error);
472 }
473
474 SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
475 {
476 sysctl_createv(clog, 0, NULL, NULL,
477 CTLFLAG_PERMANENT,
478 CTLTYPE_NODE, "net", NULL,
479 NULL, 0, NULL, 0,
480 CTL_NET, CTL_EOL);
481 sysctl_createv(clog, 0, NULL, NULL,
482 CTLFLAG_PERMANENT,
483 CTLTYPE_NODE, "local",
484 SYSCTL_DESCR("PF_LOCAL related settings"),
485 NULL, 0, NULL, 0,
486 CTL_NET, PF_LOCAL, CTL_EOL);
487 sysctl_createv(clog, 0, NULL, NULL,
488 CTLFLAG_PERMANENT,
489 CTLTYPE_NODE, "stream",
490 SYSCTL_DESCR("SOCK_STREAM settings"),
491 NULL, 0, NULL, 0,
492 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
493 sysctl_createv(clog, 0, NULL, NULL,
494 CTLFLAG_PERMANENT,
495 CTLTYPE_NODE, "dgram",
496 SYSCTL_DESCR("SOCK_DGRAM settings"),
497 NULL, 0, NULL, 0,
498 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
499
500 sysctl_createv(clog, 0, NULL, NULL,
501 CTLFLAG_PERMANENT,
502 CTLTYPE_STRUCT, "pcblist",
503 SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
504 sysctl_unpcblist, 0, NULL, 0,
505 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
506 sysctl_createv(clog, 0, NULL, NULL,
507 CTLFLAG_PERMANENT,
508 CTLTYPE_STRUCT, "pcblist",
509 SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
510 sysctl_unpcblist, 0, NULL, 0,
511 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
512 }
513
514 void
515 pfctlinput(int cmd, const struct sockaddr *sa)
516 {
517 struct domain *dp;
518 const struct protosw *pr;
519
520 DOMAIN_FOREACH(dp) {
521 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
522 if (pr->pr_ctlinput != NULL)
523 (*pr->pr_ctlinput)(cmd, sa, NULL);
524 }
525 }
526 }
527
528 void
529 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
530 {
531 struct domain *dp;
532 const struct protosw *pr;
533
534 if (sa == NULL)
535 return;
536
537 DOMAIN_FOREACH(dp) {
538 /*
539 * the check must be made by xx_ctlinput() anyways, to
540 * make sure we use data item pointed to by ctlparam in
541 * correct way. the following check is made just for safety.
542 */
543 if (dp->dom_family != sa->sa_family)
544 continue;
545
546 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
547 if (pr->pr_ctlinput != NULL)
548 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
549 }
550 }
551 }
552
553 void
554 pfslowtimo(void *arg)
555 {
556 struct domain *dp;
557 const struct protosw *pr;
558
559 pfslowtimo_now++;
560
561 DOMAIN_FOREACH(dp) {
562 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
563 if (pr->pr_slowtimo)
564 (*pr->pr_slowtimo)();
565 }
566 callout_schedule(&pfslowtimo_ch, hz / 2);
567 }
568
569 void
570 pffasttimo(void *arg)
571 {
572 struct domain *dp;
573 const struct protosw *pr;
574
575 pffasttimo_now++;
576
577 DOMAIN_FOREACH(dp) {
578 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
579 if (pr->pr_fasttimo)
580 (*pr->pr_fasttimo)();
581 }
582 callout_schedule(&pffasttimo_ch, hz / 5);
583 }
Cache object: bd8902ed4f473e33fc3b39fce1235d5f
|