uipc_domain.c revision 1.68 1 /* $NetBSD: uipc_domain.c,v 1.68 2007/08/07 04:06:20 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.68 2007/08/07 04:06:20 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 callout_t 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, 0);
92 callout_init(&pfslowtimo_ch, 0);
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 (dp->dom_sa_pool != NULL) {
123 pool_setlowat(dp->dom_sa_pool, 32);
124 if (pool_prime(dp->dom_sa_pool, 32) != 0)
125 printf("%s: pool_prime failed\n", __func__);
126 }
127
128 if (max_linkhdr < 16) /* XXX */
129 max_linkhdr = 16;
130 max_hdr = max_linkhdr + max_protohdr;
131 max_datalen = MHLEN - max_hdr;
132 }
133
134 struct domain *
135 pffinddomain(int family)
136 {
137 struct domain *dp;
138
139 if (family < __arraycount(domain_array) && domain_array[family] != NULL)
140 return domain_array[family];
141
142 DOMAIN_FOREACH(dp)
143 if (dp->dom_family == family)
144 return (dp);
145 return (NULL);
146 }
147
148 const struct protosw *
149 pffindtype(int family, int type)
150 {
151 struct domain *dp;
152 const struct protosw *pr;
153
154 dp = pffinddomain(family);
155 if (dp == NULL)
156 return (NULL);
157
158 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
159 if (pr->pr_type && pr->pr_type == type)
160 return (pr);
161
162 return (NULL);
163 }
164
165 const struct protosw *
166 pffindproto(int family, int protocol, int type)
167 {
168 struct domain *dp;
169 const struct protosw *pr;
170 const struct protosw *maybe = NULL;
171
172 if (family == 0)
173 return (NULL);
174
175 dp = pffinddomain(family);
176 if (dp == NULL)
177 return (NULL);
178
179 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
180 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
181 return (pr);
182
183 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
184 pr->pr_protocol == 0 && maybe == NULL)
185 maybe = pr;
186 }
187 return (maybe);
188 }
189
190 struct sockaddr *
191 sockaddr_alloc(sa_family_t af, int flags)
192 {
193 const struct domain *dom;
194 struct sockaddr *sa;
195
196 if ((dom = pffinddomain(af)) == NULL)
197 return NULL;
198
199 if ((sa = pool_get(dom->dom_sa_pool, flags)) == NULL)
200 return NULL;
201
202 sa->sa_family = af;
203 sa->sa_len = dom->dom_sa_len;
204 return sa;
205 }
206
207 static void
208 sockaddr_fixlen(struct sockaddr *dst, uint8_t deslen)
209 {
210 struct domain *dom;
211
212 if ((dom = pffinddomain(dst->sa_family)) == NULL)
213 panic("%s: unknown domain %d", __func__, dst->sa_family);
214 if (dom->dom_sa_len < deslen)
215 panic("%s: source too long, %d bytes", __func__, deslen);
216 dst->sa_len = dom->dom_sa_len;
217 }
218
219 struct sockaddr *
220 sockaddr_copy(struct sockaddr *dst, const struct sockaddr *src)
221 {
222 KASSERT(dst->sa_family == src->sa_family);
223
224 if (__predict_false(dst->sa_len < src->sa_len))
225 sockaddr_fixlen(dst, src->sa_len);
226
227 memcpy(dst, src, src->sa_len);
228
229 return dst;
230 }
231
232 int
233 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
234 {
235 int len, rc;
236 struct domain *dom;
237
238 if (sa1->sa_family != sa2->sa_family)
239 return sa1->sa_family - sa2->sa_family;
240
241 dom = pffinddomain(sa1->sa_family);
242
243 if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
244 return (*dom->dom_sockaddr_cmp)(sa1, sa2);
245
246 len = MIN(sa1->sa_len, sa2->sa_len);
247
248 if (dom == NULL || dom->dom_sa_cmplen == 0) {
249 if ((rc = memcmp(sa1, sa2, len)) != 0)
250 return rc;
251 return sa1->sa_len - sa2->sa_len;
252 }
253
254 if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
255 (const char *)sa2 + dom->dom_sa_cmpofs,
256 MIN(dom->dom_sa_cmplen,
257 len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
258 return rc;
259
260 return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
261 MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
262 }
263
264 struct sockaddr *
265 sockaddr_dup(const struct sockaddr *src, int flags)
266 {
267 struct sockaddr *dst;
268
269 if ((dst = sockaddr_alloc(src->sa_family, flags)) == NULL)
270 return NULL;
271
272 KASSERT(dst->sa_len >= src->sa_len);
273
274 return sockaddr_copy(dst, src);
275 }
276
277 void
278 sockaddr_free(struct sockaddr *sa)
279 {
280 const struct domain *dom;
281
282 if ((dom = pffinddomain(sa->sa_family)) == NULL)
283 panic("%s: no such domain %d\n", __func__, sa->sa_family);
284
285 pool_put(dom->dom_sa_pool, sa);
286 }
287
288 /*
289 * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
290 */
291 static void
292 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
293 {
294 struct unpcb *unp = sotounpcb(so);
295 struct sockaddr_un *un = unp->unp_addr;
296
297 memset(pcb, 0, sizeof(*pcb));
298
299 pcb->ki_family = so->so_proto->pr_domain->dom_family;
300 pcb->ki_type = so->so_proto->pr_type;
301 pcb->ki_protocol = so->so_proto->pr_protocol;
302 pcb->ki_pflags = unp->unp_flags;
303
304 pcb->ki_pcbaddr = PTRTOUINT64(unp);
305 /* pcb->ki_ppcbaddr = unp has no ppcb... */
306 pcb->ki_sockaddr = PTRTOUINT64(so);
307
308 pcb->ki_sostate = so->so_state;
309 /* pcb->ki_prstate = unp has no state... */
310
311 pcb->ki_rcvq = so->so_rcv.sb_cc;
312 pcb->ki_sndq = so->so_snd.sb_cc;
313
314 un = (struct sockaddr_un *)&pcb->ki_src;
315 /*
316 * local domain sockets may bind without having a local
317 * endpoint. bleah!
318 */
319 if (unp->unp_addr != NULL) {
320 un->sun_len = unp->unp_addr->sun_len;
321 un->sun_family = unp->unp_addr->sun_family;
322 strlcpy(un->sun_path, unp->unp_addr->sun_path,
323 sizeof(pcb->ki_s));
324 }
325 else {
326 un->sun_len = offsetof(struct sockaddr_un, sun_path);
327 un->sun_family = pcb->ki_family;
328 }
329 if (unp->unp_conn != NULL) {
330 un = (struct sockaddr_un *)&pcb->ki_dst;
331 if (unp->unp_conn->unp_addr != NULL) {
332 un->sun_len = unp->unp_conn->unp_addr->sun_len;
333 un->sun_family = unp->unp_conn->unp_addr->sun_family;
334 un->sun_family = unp->unp_conn->unp_addr->sun_family;
335 strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path,
336 sizeof(pcb->ki_d));
337 }
338 else {
339 un->sun_len = offsetof(struct sockaddr_un, sun_path);
340 un->sun_family = pcb->ki_family;
341 }
342 }
343
344 pcb->ki_inode = unp->unp_ino;
345 pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode);
346 pcb->ki_conn = PTRTOUINT64(unp->unp_conn);
347 pcb->ki_refs = PTRTOUINT64(unp->unp_refs);
348 pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref);
349 }
350
351 static int
352 sysctl_unpcblist(SYSCTLFN_ARGS)
353 {
354 struct file *fp;
355 struct socket *so;
356 struct kinfo_pcb pcb;
357 char *dp;
358 u_int op, arg;
359 size_t len, needed, elem_size, out_size;
360 int error, elem_count, pf, type, pf2;
361
362 if (namelen == 1 && name[0] == CTL_QUERY)
363 return (sysctl_query(SYSCTLFN_CALL(rnode)));
364
365 if (namelen != 4)
366 return (EINVAL);
367
368 if (oldp != NULL) {
369 len = *oldlenp;
370 elem_size = name[2];
371 elem_count = name[3];
372 if (elem_size != sizeof(pcb))
373 return EINVAL;
374 } else {
375 len = 0;
376 elem_size = sizeof(pcb);
377 elem_count = INT_MAX;
378 }
379 error = 0;
380 dp = oldp;
381 op = name[0];
382 arg = name[1];
383 out_size = elem_size;
384 needed = 0;
385
386 if (name - oname != 4)
387 return (EINVAL);
388
389 pf = oname[1];
390 type = oname[2];
391 pf2 = (oldp == NULL) ? 0 : pf;
392
393 /*
394 * there's no "list" of local domain sockets, so we have
395 * to walk the file list looking for them. :-/
396 */
397 LIST_FOREACH(fp, &filehead, f_list) {
398 if (kauth_authorize_generic(l->l_cred,
399 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
400 continue;
401 if (fp->f_type != DTYPE_SOCKET)
402 continue;
403 so = (struct socket *)fp->f_data;
404 if (so->so_type != type)
405 continue;
406 if (so->so_proto->pr_domain->dom_family != pf)
407 continue;
408 if (len >= elem_size && elem_count > 0) {
409 sysctl_dounpcb(&pcb, so);
410 error = copyout(&pcb, dp, out_size);
411 if (error)
412 break;
413 dp += elem_size;
414 len -= elem_size;
415 }
416 if (elem_count > 0) {
417 needed += elem_size;
418 if (elem_count != INT_MAX)
419 elem_count--;
420 }
421 }
422
423 *oldlenp = needed;
424 if (oldp == NULL)
425 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
426
427 return (error);
428 }
429
430 SYSCTL_SETUP(sysctl_net_setup, "sysctl net subtree setup")
431 {
432 sysctl_createv(clog, 0, NULL, NULL,
433 CTLFLAG_PERMANENT,
434 CTLTYPE_NODE, "net", NULL,
435 NULL, 0, NULL, 0,
436 CTL_NET, CTL_EOL);
437 sysctl_createv(clog, 0, NULL, NULL,
438 CTLFLAG_PERMANENT,
439 CTLTYPE_NODE, "local",
440 SYSCTL_DESCR("PF_LOCAL related settings"),
441 NULL, 0, NULL, 0,
442 CTL_NET, PF_LOCAL, CTL_EOL);
443 sysctl_createv(clog, 0, NULL, NULL,
444 CTLFLAG_PERMANENT,
445 CTLTYPE_NODE, "stream",
446 SYSCTL_DESCR("SOCK_STREAM settings"),
447 NULL, 0, NULL, 0,
448 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
449 sysctl_createv(clog, 0, NULL, NULL,
450 CTLFLAG_PERMANENT,
451 CTLTYPE_NODE, "dgram",
452 SYSCTL_DESCR("SOCK_DGRAM settings"),
453 NULL, 0, NULL, 0,
454 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
455
456 sysctl_createv(clog, 0, NULL, NULL,
457 CTLFLAG_PERMANENT,
458 CTLTYPE_STRUCT, "pcblist",
459 SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
460 sysctl_unpcblist, 0, NULL, 0,
461 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
462 sysctl_createv(clog, 0, NULL, NULL,
463 CTLFLAG_PERMANENT,
464 CTLTYPE_STRUCT, "pcblist",
465 SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
466 sysctl_unpcblist, 0, NULL, 0,
467 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
468 }
469
470 void
471 pfctlinput(int cmd, const struct sockaddr *sa)
472 {
473 struct domain *dp;
474 const struct protosw *pr;
475
476 DOMAIN_FOREACH(dp) {
477 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
478 if (pr->pr_ctlinput != NULL)
479 (*pr->pr_ctlinput)(cmd, sa, NULL);
480 }
481 }
482 }
483
484 void
485 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
486 {
487 struct domain *dp;
488 const struct protosw *pr;
489
490 if (sa == NULL)
491 return;
492
493 DOMAIN_FOREACH(dp) {
494 /*
495 * the check must be made by xx_ctlinput() anyways, to
496 * make sure we use data item pointed to by ctlparam in
497 * correct way. the following check is made just for safety.
498 */
499 if (dp->dom_family != sa->sa_family)
500 continue;
501
502 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
503 if (pr->pr_ctlinput != NULL)
504 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
505 }
506 }
507 }
508
509 void
510 pfslowtimo(void *arg)
511 {
512 struct domain *dp;
513 const struct protosw *pr;
514
515 pfslowtimo_now++;
516
517 DOMAIN_FOREACH(dp) {
518 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
519 if (pr->pr_slowtimo)
520 (*pr->pr_slowtimo)();
521 }
522 callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
523 }
524
525 void
526 pffasttimo(void *arg)
527 {
528 struct domain *dp;
529 const struct protosw *pr;
530
531 pffasttimo_now++;
532
533 DOMAIN_FOREACH(dp) {
534 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
535 if (pr->pr_fasttimo)
536 (*pr->pr_fasttimo)();
537 }
538 callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
539 }
540