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