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