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