uipc_domain.c revision 1.110 1 1.110 riastrad /* $NetBSD: uipc_domain.c,v 1.110 2024/12/06 18:36:31 riastradh Exp $ */
2 1.12 cgd
3 1.1 cgd /*
4 1.11 mycroft * Copyright (c) 1982, 1986, 1993
5 1.11 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.43 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd *
31 1.18 fvdl * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
32 1.1 cgd */
33 1.36 lukem
34 1.36 lukem #include <sys/cdefs.h>
35 1.110 riastrad __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.110 2024/12/06 18:36:31 riastradh Exp $");
36 1.1 cgd
37 1.5 mycroft #include <sys/param.h>
38 1.110 riastrad #include <sys/types.h>
39 1.110 riastrad
40 1.110 riastrad #include <sys/callout.h>
41 1.5 mycroft #include <sys/domain.h>
42 1.110 riastrad #include <sys/file.h>
43 1.110 riastrad #include <sys/filedesc.h>
44 1.110 riastrad #include <sys/kauth.h>
45 1.110 riastrad #include <sys/kernel.h>
46 1.5 mycroft #include <sys/mbuf.h>
47 1.110 riastrad #include <sys/proc.h>
48 1.110 riastrad #include <sys/protosw.h>
49 1.49 matt #include <sys/queue.h>
50 1.110 riastrad #include <sys/socket.h>
51 1.110 riastrad #include <sys/socketvar.h>
52 1.10 cgd #include <sys/sysctl.h>
53 1.110 riastrad #include <sys/systm.h>
54 1.110 riastrad #include <sys/time.h>
55 1.50 atatat #include <sys/un.h>
56 1.50 atatat #include <sys/unpcb.h>
57 1.96 christos
58 1.110 riastrad #include <net/if_dl.h>
59 1.96 christos #include <netatalk/at.h>
60 1.88 joerg #include <netinet/in.h>
61 1.13 christos
62 1.69 dyoung MALLOC_DECLARE(M_SOCKADDR);
63 1.69 dyoung
64 1.69 dyoung MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints");
65 1.69 dyoung
66 1.45 junyoung void pffasttimo(void *);
67 1.45 junyoung void pfslowtimo(void *);
68 1.37 matt
69 1.49 matt struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains);
70 1.64 dyoung static struct domain *domain_array[AF_MAX];
71 1.11 mycroft
72 1.66 ad callout_t pffasttimo_ch, pfslowtimo_ch;
73 1.30 thorpej
74 1.19 thorpej /*
75 1.19 thorpej * Current time values for fast and slow timeouts. We can use u_int
76 1.19 thorpej * relatively safely. The fast timer will roll over in 27 years and
77 1.19 thorpej * the slow timer in 68 years.
78 1.19 thorpej */
79 1.19 thorpej u_int pfslowtimo_now;
80 1.19 thorpej u_int pffasttimo_now;
81 1.19 thorpej
82 1.77 pooka static struct sysctllog *domain_sysctllog;
83 1.77 pooka static void sysctl_net_setup(void);
84 1.77 pooka
85 1.93 pooka /* ensure successful linkage even without any domains in link sets */
86 1.93 pooka static struct domain domain_dummy;
87 1.93 pooka __link_set_add_rodata(domains,domain_dummy);
88 1.93 pooka
89 1.101 ozaki static void
90 1.101 ozaki domain_init_timers(void)
91 1.101 ozaki {
92 1.101 ozaki
93 1.101 ozaki callout_init(&pffasttimo_ch, CALLOUT_MPSAFE);
94 1.101 ozaki callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE);
95 1.101 ozaki
96 1.101 ozaki callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
97 1.101 ozaki callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
98 1.101 ozaki }
99 1.101 ozaki
100 1.49 matt void
101 1.94 pooka domaininit(bool attach)
102 1.49 matt {
103 1.49 matt __link_set_decl(domains, struct domain);
104 1.49 matt struct domain * const * dpp;
105 1.49 matt struct domain *rt_domain = NULL;
106 1.49 matt
107 1.77 pooka sysctl_net_setup();
108 1.77 pooka
109 1.49 matt /*
110 1.49 matt * Add all of the domains. Make sure the PF_ROUTE
111 1.49 matt * domain is added last.
112 1.49 matt */
113 1.94 pooka if (attach) {
114 1.94 pooka __link_set_foreach(dpp, domains) {
115 1.94 pooka if (*dpp == &domain_dummy)
116 1.94 pooka continue;
117 1.94 pooka if ((*dpp)->dom_family == PF_ROUTE)
118 1.94 pooka rt_domain = *dpp;
119 1.94 pooka else
120 1.94 pooka domain_attach(*dpp);
121 1.94 pooka }
122 1.94 pooka if (rt_domain)
123 1.94 pooka domain_attach(rt_domain);
124 1.101 ozaki
125 1.101 ozaki domain_init_timers();
126 1.49 matt }
127 1.101 ozaki }
128 1.49 matt
129 1.101 ozaki /*
130 1.101 ozaki * Must be called only if domaininit has been called with false and
131 1.101 ozaki * after all domains have been attached.
132 1.101 ozaki */
133 1.101 ozaki void
134 1.101 ozaki domaininit_post(void)
135 1.101 ozaki {
136 1.49 matt
137 1.101 ozaki domain_init_timers();
138 1.1 cgd }
139 1.1 cgd
140 1.4 andrew void
141 1.49 matt domain_attach(struct domain *dp)
142 1.1 cgd {
143 1.47 matt const struct protosw *pr;
144 1.1 cgd
145 1.49 matt STAILQ_INSERT_TAIL(&domains, dp, dom_link);
146 1.64 dyoung if (dp->dom_family < __arraycount(domain_array))
147 1.64 dyoung domain_array[dp->dom_family] = dp;
148 1.49 matt
149 1.49 matt if (dp->dom_init)
150 1.49 matt (*dp->dom_init)();
151 1.1 cgd
152 1.38 matt #ifdef MBUFTRACE
153 1.49 matt if (dp->dom_mowner.mo_name[0] == '\0') {
154 1.49 matt strncpy(dp->dom_mowner.mo_name, dp->dom_name,
155 1.49 matt sizeof(dp->dom_mowner.mo_name));
156 1.49 matt MOWNER_ATTACH(&dp->dom_mowner);
157 1.49 matt }
158 1.38 matt #endif
159 1.49 matt for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
160 1.49 matt if (pr->pr_init)
161 1.49 matt (*pr->pr_init)();
162 1.1 cgd }
163 1.1 cgd
164 1.16 explorer if (max_linkhdr < 16) /* XXX */
165 1.16 explorer max_linkhdr = 16;
166 1.1 cgd max_hdr = max_linkhdr + max_protohdr;
167 1.1 cgd max_datalen = MHLEN - max_hdr;
168 1.1 cgd }
169 1.1 cgd
170 1.29 thorpej struct domain *
171 1.49 matt pffinddomain(int family)
172 1.29 thorpej {
173 1.29 thorpej struct domain *dp;
174 1.29 thorpej
175 1.64 dyoung if (family < __arraycount(domain_array) && domain_array[family] != NULL)
176 1.64 dyoung return domain_array[family];
177 1.64 dyoung
178 1.49 matt DOMAIN_FOREACH(dp)
179 1.29 thorpej if (dp->dom_family == family)
180 1.81 dyoung return dp;
181 1.81 dyoung return NULL;
182 1.29 thorpej }
183 1.29 thorpej
184 1.47 matt const struct protosw *
185 1.49 matt pffindtype(int family, int type)
186 1.1 cgd {
187 1.29 thorpej struct domain *dp;
188 1.47 matt const struct protosw *pr;
189 1.29 thorpej
190 1.29 thorpej dp = pffinddomain(family);
191 1.29 thorpej if (dp == NULL)
192 1.81 dyoung return NULL;
193 1.1 cgd
194 1.1 cgd for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
195 1.1 cgd if (pr->pr_type && pr->pr_type == type)
196 1.81 dyoung return pr;
197 1.29 thorpej
198 1.81 dyoung return NULL;
199 1.1 cgd }
200 1.1 cgd
201 1.47 matt const struct protosw *
202 1.49 matt pffindproto(int family, int protocol, int type)
203 1.1 cgd {
204 1.29 thorpej struct domain *dp;
205 1.47 matt const struct protosw *pr;
206 1.47 matt const struct protosw *maybe = NULL;
207 1.1 cgd
208 1.1 cgd if (family == 0)
209 1.81 dyoung return NULL;
210 1.29 thorpej
211 1.29 thorpej dp = pffinddomain(family);
212 1.29 thorpej if (dp == NULL)
213 1.81 dyoung return NULL;
214 1.29 thorpej
215 1.1 cgd for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
216 1.1 cgd if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
217 1.81 dyoung return pr;
218 1.1 cgd
219 1.1 cgd if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
220 1.29 thorpej pr->pr_protocol == 0 && maybe == NULL)
221 1.1 cgd maybe = pr;
222 1.1 cgd }
223 1.81 dyoung return maybe;
224 1.10 cgd }
225 1.10 cgd
226 1.71 dyoung void *
227 1.71 dyoung sockaddr_addr(struct sockaddr *sa, socklen_t *slenp)
228 1.71 dyoung {
229 1.71 dyoung const struct domain *dom;
230 1.71 dyoung
231 1.71 dyoung if ((dom = pffinddomain(sa->sa_family)) == NULL ||
232 1.71 dyoung dom->dom_sockaddr_addr == NULL)
233 1.71 dyoung return NULL;
234 1.71 dyoung
235 1.71 dyoung return (*dom->dom_sockaddr_addr)(sa, slenp);
236 1.71 dyoung }
237 1.71 dyoung
238 1.71 dyoung const void *
239 1.71 dyoung sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp)
240 1.71 dyoung {
241 1.71 dyoung const struct domain *dom;
242 1.83 dyoung
243 1.71 dyoung if ((dom = pffinddomain(sa->sa_family)) == NULL ||
244 1.71 dyoung dom->dom_sockaddr_const_addr == NULL)
245 1.71 dyoung return NULL;
246 1.71 dyoung
247 1.71 dyoung return (*dom->dom_sockaddr_const_addr)(sa, slenp);
248 1.71 dyoung }
249 1.71 dyoung
250 1.71 dyoung const struct sockaddr *
251 1.97 christos sockaddr_any_by_family(sa_family_t family)
252 1.71 dyoung {
253 1.71 dyoung const struct domain *dom;
254 1.83 dyoung
255 1.80 dyoung if ((dom = pffinddomain(family)) == NULL)
256 1.71 dyoung return NULL;
257 1.71 dyoung
258 1.71 dyoung return dom->dom_sa_any;
259 1.71 dyoung }
260 1.71 dyoung
261 1.80 dyoung const struct sockaddr *
262 1.80 dyoung sockaddr_any(const struct sockaddr *sa)
263 1.80 dyoung {
264 1.80 dyoung return sockaddr_any_by_family(sa->sa_family);
265 1.80 dyoung }
266 1.80 dyoung
267 1.71 dyoung const void *
268 1.71 dyoung sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp)
269 1.71 dyoung {
270 1.71 dyoung const struct sockaddr *any;
271 1.71 dyoung
272 1.71 dyoung if ((any = sockaddr_any(sa)) == NULL)
273 1.71 dyoung return NULL;
274 1.71 dyoung
275 1.71 dyoung return sockaddr_const_addr(any, slenp);
276 1.71 dyoung }
277 1.71 dyoung
278 1.97 christos socklen_t
279 1.97 christos sockaddr_getsize_by_family(sa_family_t af)
280 1.96 christos {
281 1.97 christos switch (af) {
282 1.96 christos case AF_INET:
283 1.97 christos return sizeof(struct sockaddr_in);
284 1.96 christos case AF_INET6:
285 1.97 christos return sizeof(struct sockaddr_in6);
286 1.96 christos case AF_UNIX:
287 1.97 christos return sizeof(struct sockaddr_un);
288 1.96 christos case AF_LINK:
289 1.97 christos return sizeof(struct sockaddr_dl);
290 1.96 christos case AF_APPLETALK:
291 1.97 christos return sizeof(struct sockaddr_at);
292 1.96 christos default:
293 1.97 christos #ifdef DIAGNOSTIC
294 1.102 mrg printf("%s: (%s:%u:%u) Unhandled address family=%hhu\n",
295 1.102 mrg __func__, curlwp->l_proc->p_comm,
296 1.102 mrg curlwp->l_proc->p_pid, curlwp->l_lid, af);
297 1.97 christos #endif
298 1.97 christos return 0;
299 1.97 christos }
300 1.97 christos }
301 1.97 christos
302 1.97 christos #ifdef DIAGNOSTIC
303 1.97 christos static void
304 1.97 christos sockaddr_checklen(const struct sockaddr *sa)
305 1.97 christos {
306 1.99 christos // Can't tell how much was allocated, if it was allocated.
307 1.99 christos if (sa->sa_family == AF_LINK)
308 1.99 christos return;
309 1.99 christos
310 1.97 christos socklen_t len = sockaddr_getsize_by_family(sa->sa_family);
311 1.97 christos if (len == 0 || len == sa->sa_len)
312 1.96 christos return;
313 1.97 christos
314 1.97 christos char buf[512];
315 1.97 christos sockaddr_format(sa, buf, sizeof(buf));
316 1.97 christos printf("%s: %p bad len af=%hhu socklen=%hhu len=%u [%s]\n",
317 1.97 christos __func__, sa, sa->sa_family, sa->sa_len, (unsigned)len, buf);
318 1.96 christos }
319 1.96 christos #else
320 1.96 christos #define sockaddr_checklen(sa) ((void)0)
321 1.96 christos #endif
322 1.96 christos
323 1.64 dyoung struct sockaddr *
324 1.69 dyoung sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags)
325 1.64 dyoung {
326 1.64 dyoung struct sockaddr *sa;
327 1.69 dyoung socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0]));
328 1.64 dyoung
329 1.109 riastrad #ifdef DIAGNOSTIC
330 1.109 riastrad /*
331 1.109 riastrad * sockaddr_checklen passes sa to sockaddr_format which
332 1.109 riastrad * requires it to be fully initialized.
333 1.109 riastrad *
334 1.109 riastrad * XXX This should be factored better.
335 1.109 riastrad */
336 1.109 riastrad flags |= M_ZERO;
337 1.109 riastrad #endif
338 1.69 dyoung if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL)
339 1.64 dyoung return NULL;
340 1.64 dyoung
341 1.64 dyoung sa->sa_family = af;
342 1.69 dyoung sa->sa_len = reallen;
343 1.96 christos sockaddr_checklen(sa);
344 1.64 dyoung return sa;
345 1.64 dyoung }
346 1.64 dyoung
347 1.64 dyoung struct sockaddr *
348 1.69 dyoung sockaddr_copy(struct sockaddr *dst, socklen_t socklen,
349 1.69 dyoung const struct sockaddr *src)
350 1.64 dyoung {
351 1.70 dyoung if (__predict_false(socklen < src->sa_len)) {
352 1.70 dyoung panic("%s: source too long, %d < %d bytes", __func__, socklen,
353 1.70 dyoung src->sa_len);
354 1.70 dyoung }
355 1.96 christos sockaddr_checklen(src);
356 1.70 dyoung return memcpy(dst, src, src->sa_len);
357 1.64 dyoung }
358 1.64 dyoung
359 1.84 dyoung struct sockaddr *
360 1.84 dyoung sockaddr_externalize(struct sockaddr *dst, socklen_t socklen,
361 1.84 dyoung const struct sockaddr *src)
362 1.84 dyoung {
363 1.84 dyoung struct domain *dom;
364 1.84 dyoung
365 1.84 dyoung dom = pffinddomain(src->sa_family);
366 1.84 dyoung
367 1.84 dyoung if (dom != NULL && dom->dom_sockaddr_externalize != NULL)
368 1.84 dyoung return (*dom->dom_sockaddr_externalize)(dst, socklen, src);
369 1.84 dyoung
370 1.84 dyoung return sockaddr_copy(dst, socklen, src);
371 1.84 dyoung }
372 1.84 dyoung
373 1.64 dyoung int
374 1.64 dyoung sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
375 1.64 dyoung {
376 1.65 dyoung int len, rc;
377 1.64 dyoung struct domain *dom;
378 1.64 dyoung
379 1.64 dyoung if (sa1->sa_family != sa2->sa_family)
380 1.64 dyoung return sa1->sa_family - sa2->sa_family;
381 1.64 dyoung
382 1.65 dyoung dom = pffinddomain(sa1->sa_family);
383 1.65 dyoung
384 1.65 dyoung if (dom != NULL && dom->dom_sockaddr_cmp != NULL)
385 1.64 dyoung return (*dom->dom_sockaddr_cmp)(sa1, sa2);
386 1.64 dyoung
387 1.65 dyoung len = MIN(sa1->sa_len, sa2->sa_len);
388 1.65 dyoung
389 1.65 dyoung if (dom == NULL || dom->dom_sa_cmplen == 0) {
390 1.65 dyoung if ((rc = memcmp(sa1, sa2, len)) != 0)
391 1.65 dyoung return rc;
392 1.65 dyoung return sa1->sa_len - sa2->sa_len;
393 1.65 dyoung }
394 1.65 dyoung
395 1.65 dyoung if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs,
396 1.65 dyoung (const char *)sa2 + dom->dom_sa_cmpofs,
397 1.65 dyoung MIN(dom->dom_sa_cmplen,
398 1.65 dyoung len - MIN(len, dom->dom_sa_cmpofs)))) != 0)
399 1.64 dyoung return rc;
400 1.64 dyoung
401 1.65 dyoung return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) -
402 1.65 dyoung MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len);
403 1.64 dyoung }
404 1.64 dyoung
405 1.64 dyoung struct sockaddr *
406 1.64 dyoung sockaddr_dup(const struct sockaddr *src, int flags)
407 1.64 dyoung {
408 1.64 dyoung struct sockaddr *dst;
409 1.64 dyoung
410 1.69 dyoung if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL)
411 1.64 dyoung return NULL;
412 1.64 dyoung
413 1.69 dyoung return sockaddr_copy(dst, dst->sa_len, src);
414 1.64 dyoung }
415 1.64 dyoung
416 1.64 dyoung void
417 1.64 dyoung sockaddr_free(struct sockaddr *sa)
418 1.64 dyoung {
419 1.69 dyoung free(sa, M_SOCKADDR);
420 1.64 dyoung }
421 1.64 dyoung
422 1.96 christos static int
423 1.96 christos sun_print(char *buf, size_t len, const void *v)
424 1.96 christos {
425 1.96 christos const struct sockaddr_un *sun = v;
426 1.107 mlelstv size_t plen;
427 1.107 mlelstv
428 1.107 mlelstv KASSERT(sun->sun_len >= offsetof(struct sockaddr_un, sun_path[0]));
429 1.107 mlelstv plen = sun->sun_len - offsetof(struct sockaddr_un, sun_path[0]);
430 1.107 mlelstv
431 1.107 mlelstv len = MIN(len, plen);
432 1.107 mlelstv
433 1.96 christos return snprintf(buf, len, "%s", sun->sun_path);
434 1.96 christos }
435 1.96 christos
436 1.96 christos int
437 1.88 joerg sockaddr_format(const struct sockaddr *sa, char *buf, size_t len)
438 1.88 joerg {
439 1.96 christos size_t plen = 0;
440 1.88 joerg
441 1.96 christos if (sa == NULL)
442 1.96 christos return strlcpy(buf, "(null)", len);
443 1.88 joerg
444 1.88 joerg switch (sa->sa_family) {
445 1.88 joerg case AF_LOCAL:
446 1.96 christos plen = strlcpy(buf, "unix: ", len);
447 1.96 christos break;
448 1.88 joerg case AF_INET:
449 1.96 christos plen = strlcpy(buf, "inet: ", len);
450 1.88 joerg break;
451 1.88 joerg case AF_INET6:
452 1.96 christos plen = strlcpy(buf, "inet6: ", len);
453 1.96 christos break;
454 1.96 christos case AF_LINK:
455 1.96 christos plen = strlcpy(buf, "link: ", len);
456 1.96 christos break;
457 1.96 christos case AF_APPLETALK:
458 1.96 christos plen = strlcpy(buf, "atalk: ", len);
459 1.88 joerg break;
460 1.96 christos default:
461 1.96 christos return snprintf(buf, len, "(unknown socket family %d)",
462 1.96 christos (int)sa->sa_family);
463 1.88 joerg }
464 1.96 christos
465 1.96 christos buf += plen;
466 1.96 christos if (plen > len)
467 1.96 christos len = 0;
468 1.96 christos else
469 1.96 christos len -= plen;
470 1.96 christos
471 1.96 christos switch (sa->sa_family) {
472 1.96 christos case AF_LOCAL:
473 1.96 christos return sun_print(buf, len, sa);
474 1.96 christos case AF_INET:
475 1.96 christos return sin_print(buf, len, sa);
476 1.96 christos case AF_INET6:
477 1.96 christos return sin6_print(buf, len, sa);
478 1.96 christos case AF_LINK:
479 1.96 christos return sdl_print(buf, len, sa);
480 1.96 christos case AF_APPLETALK:
481 1.96 christos return sat_print(buf, len, sa);
482 1.96 christos default:
483 1.96 christos panic("bad family %hhu", sa->sa_family);
484 1.88 joerg }
485 1.88 joerg }
486 1.88 joerg
487 1.50 atatat /*
488 1.50 atatat * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
489 1.50 atatat */
490 1.50 atatat static void
491 1.50 atatat sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so)
492 1.50 atatat {
493 1.105 maxv const bool allowaddr = get_expose_address(curproc);
494 1.50 atatat struct unpcb *unp = sotounpcb(so);
495 1.50 atatat struct sockaddr_un *un = unp->unp_addr;
496 1.50 atatat
497 1.50 atatat memset(pcb, 0, sizeof(*pcb));
498 1.50 atatat
499 1.50 atatat pcb->ki_family = so->so_proto->pr_domain->dom_family;
500 1.50 atatat pcb->ki_type = so->so_proto->pr_type;
501 1.50 atatat pcb->ki_protocol = so->so_proto->pr_protocol;
502 1.50 atatat pcb->ki_pflags = unp->unp_flags;
503 1.50 atatat
504 1.105 maxv COND_SET_VALUE(pcb->ki_pcbaddr, PTRTOUINT64(unp), allowaddr);
505 1.50 atatat /* pcb->ki_ppcbaddr = unp has no ppcb... */
506 1.105 maxv COND_SET_VALUE(pcb->ki_sockaddr, PTRTOUINT64(so), allowaddr);
507 1.50 atatat
508 1.50 atatat pcb->ki_sostate = so->so_state;
509 1.50 atatat /* pcb->ki_prstate = unp has no state... */
510 1.50 atatat
511 1.50 atatat pcb->ki_rcvq = so->so_rcv.sb_cc;
512 1.50 atatat pcb->ki_sndq = so->so_snd.sb_cc;
513 1.50 atatat
514 1.92 christos un = (struct sockaddr_un *)pcb->ki_spad;
515 1.50 atatat /*
516 1.50 atatat * local domain sockets may bind without having a local
517 1.50 atatat * endpoint. bleah!
518 1.50 atatat */
519 1.50 atatat if (unp->unp_addr != NULL) {
520 1.91 seanb /*
521 1.91 seanb * We've added one to sun_len when allocating to
522 1.91 seanb * hold terminating NUL which we want here. See
523 1.91 seanb * makeun().
524 1.91 seanb */
525 1.91 seanb memcpy(un, unp->unp_addr,
526 1.104 riastrad uimin(sizeof(pcb->ki_spad), unp->unp_addr->sun_len + 1));
527 1.50 atatat }
528 1.50 atatat else {
529 1.50 atatat un->sun_len = offsetof(struct sockaddr_un, sun_path);
530 1.50 atatat un->sun_family = pcb->ki_family;
531 1.50 atatat }
532 1.50 atatat if (unp->unp_conn != NULL) {
533 1.92 christos un = (struct sockaddr_un *)pcb->ki_dpad;
534 1.50 atatat if (unp->unp_conn->unp_addr != NULL) {
535 1.91 seanb memcpy(un, unp->unp_conn->unp_addr,
536 1.104 riastrad uimin(sizeof(pcb->ki_dpad), unp->unp_conn->unp_addr->sun_len + 1));
537 1.50 atatat }
538 1.50 atatat else {
539 1.50 atatat un->sun_len = offsetof(struct sockaddr_un, sun_path);
540 1.50 atatat un->sun_family = pcb->ki_family;
541 1.50 atatat }
542 1.50 atatat }
543 1.50 atatat
544 1.50 atatat pcb->ki_inode = unp->unp_ino;
545 1.105 maxv COND_SET_VALUE(pcb->ki_vnode, PTRTOUINT64(unp->unp_vnode), allowaddr);
546 1.105 maxv COND_SET_VALUE(pcb->ki_conn, PTRTOUINT64(unp->unp_conn), allowaddr);
547 1.105 maxv COND_SET_VALUE(pcb->ki_refs, PTRTOUINT64(unp->unp_refs), allowaddr);
548 1.105 maxv COND_SET_VALUE(pcb->ki_nextref, PTRTOUINT64(unp->unp_nextref),
549 1.105 maxv allowaddr);
550 1.50 atatat }
551 1.50 atatat
552 1.50 atatat static int
553 1.50 atatat sysctl_unpcblist(SYSCTLFN_ARGS)
554 1.50 atatat {
555 1.106 maxv struct file *fp, *np, *dfp;
556 1.50 atatat struct socket *so;
557 1.50 atatat struct kinfo_pcb pcb;
558 1.50 atatat char *dp;
559 1.50 atatat size_t len, needed, elem_size, out_size;
560 1.89 martin int error, elem_count, pf, type;
561 1.50 atatat
562 1.50 atatat if (namelen == 1 && name[0] == CTL_QUERY)
563 1.81 dyoung return sysctl_query(SYSCTLFN_CALL(rnode));
564 1.50 atatat
565 1.50 atatat if (namelen != 4)
566 1.81 dyoung return EINVAL;
567 1.50 atatat
568 1.56 christos if (oldp != NULL) {
569 1.56 christos len = *oldlenp;
570 1.56 christos elem_size = name[2];
571 1.56 christos elem_count = name[3];
572 1.56 christos if (elem_size != sizeof(pcb))
573 1.56 christos return EINVAL;
574 1.56 christos } else {
575 1.56 christos len = 0;
576 1.56 christos elem_size = sizeof(pcb);
577 1.56 christos elem_count = INT_MAX;
578 1.56 christos }
579 1.50 atatat error = 0;
580 1.50 atatat dp = oldp;
581 1.56 christos out_size = elem_size;
582 1.50 atatat needed = 0;
583 1.50 atatat
584 1.50 atatat if (name - oname != 4)
585 1.81 dyoung return EINVAL;
586 1.50 atatat
587 1.50 atatat pf = oname[1];
588 1.50 atatat type = oname[2];
589 1.50 atatat
590 1.50 atatat /*
591 1.74 ad * allocate dummy file descriptor to make position in list.
592 1.74 ad */
593 1.74 ad sysctl_unlock();
594 1.74 ad if ((dfp = fgetdummy()) == NULL) {
595 1.74 ad sysctl_relock();
596 1.74 ad return ENOMEM;
597 1.74 ad }
598 1.74 ad
599 1.74 ad /*
600 1.50 atatat * there's no "list" of local domain sockets, so we have
601 1.50 atatat * to walk the file list looking for them. :-/
602 1.50 atatat */
603 1.72 ad mutex_enter(&filelist_lock);
604 1.106 maxv LIST_FOREACH_SAFE(fp, &filehead, f_list, np) {
605 1.74 ad if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET ||
606 1.95 matt fp->f_socket == NULL)
607 1.74 ad continue;
608 1.95 matt so = fp->f_socket;
609 1.50 atatat if (so->so_type != type)
610 1.50 atatat continue;
611 1.50 atatat if (so->so_proto->pr_domain->dom_family != pf)
612 1.50 atatat continue;
613 1.85 elad if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
614 1.85 elad KAUTH_REQ_NETWORK_SOCKET_CANSEE, so, NULL, NULL) != 0)
615 1.85 elad continue;
616 1.50 atatat if (len >= elem_size && elem_count > 0) {
617 1.75 ad mutex_enter(&fp->f_lock);
618 1.100 joerg /*
619 1.100 joerg * Do not add references, if the count reached 0.
620 1.100 joerg * Since the check above has been performed without
621 1.100 joerg * locking, it must be rechecked here as a concurrent
622 1.100 joerg * closef could have reduced it.
623 1.100 joerg */
624 1.100 joerg if (fp->f_count == 0) {
625 1.100 joerg mutex_exit(&fp->f_lock);
626 1.100 joerg continue;
627 1.100 joerg }
628 1.75 ad fp->f_count++;
629 1.75 ad mutex_exit(&fp->f_lock);
630 1.74 ad LIST_INSERT_AFTER(fp, dfp, f_list);
631 1.72 ad mutex_exit(&filelist_lock);
632 1.50 atatat sysctl_dounpcb(&pcb, so);
633 1.50 atatat error = copyout(&pcb, dp, out_size);
634 1.75 ad closef(fp);
635 1.72 ad mutex_enter(&filelist_lock);
636 1.106 maxv np = LIST_NEXT(dfp, f_list);
637 1.74 ad LIST_REMOVE(dfp, f_list);
638 1.50 atatat if (error)
639 1.50 atatat break;
640 1.50 atatat dp += elem_size;
641 1.50 atatat len -= elem_size;
642 1.50 atatat }
643 1.78 mrg needed += elem_size;
644 1.78 mrg if (elem_count > 0 && elem_count != INT_MAX)
645 1.78 mrg elem_count--;
646 1.50 atatat }
647 1.72 ad mutex_exit(&filelist_lock);
648 1.74 ad fputdummy(dfp);
649 1.74 ad *oldlenp = needed;
650 1.50 atatat if (oldp == NULL)
651 1.50 atatat *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
652 1.74 ad sysctl_relock();
653 1.50 atatat
654 1.81 dyoung return error;
655 1.50 atatat }
656 1.50 atatat
657 1.77 pooka static void
658 1.79 cegger sysctl_net_setup(void)
659 1.10 cgd {
660 1.77 pooka
661 1.77 pooka KASSERT(domain_sysctllog == NULL);
662 1.77 pooka sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
663 1.46 atatat CTLFLAG_PERMANENT,
664 1.48 atatat CTLTYPE_NODE, "local",
665 1.48 atatat SYSCTL_DESCR("PF_LOCAL related settings"),
666 1.44 atatat NULL, 0, NULL, 0,
667 1.44 atatat CTL_NET, PF_LOCAL, CTL_EOL);
668 1.77 pooka sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
669 1.50 atatat CTLFLAG_PERMANENT,
670 1.50 atatat CTLTYPE_NODE, "stream",
671 1.50 atatat SYSCTL_DESCR("SOCK_STREAM settings"),
672 1.50 atatat NULL, 0, NULL, 0,
673 1.50 atatat CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL);
674 1.77 pooka sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
675 1.50 atatat CTLFLAG_PERMANENT,
676 1.86 manu CTLTYPE_NODE, "seqpacket",
677 1.86 manu SYSCTL_DESCR("SOCK_SEQPACKET settings"),
678 1.86 manu NULL, 0, NULL, 0,
679 1.86 manu CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_EOL);
680 1.86 manu sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
681 1.86 manu CTLFLAG_PERMANENT,
682 1.50 atatat CTLTYPE_NODE, "dgram",
683 1.50 atatat SYSCTL_DESCR("SOCK_DGRAM settings"),
684 1.50 atatat NULL, 0, NULL, 0,
685 1.50 atatat CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL);
686 1.10 cgd
687 1.77 pooka sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
688 1.50 atatat CTLFLAG_PERMANENT,
689 1.50 atatat CTLTYPE_STRUCT, "pcblist",
690 1.50 atatat SYSCTL_DESCR("SOCK_STREAM protocol control block list"),
691 1.50 atatat sysctl_unpcblist, 0, NULL, 0,
692 1.50 atatat CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
693 1.77 pooka sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
694 1.50 atatat CTLFLAG_PERMANENT,
695 1.50 atatat CTLTYPE_STRUCT, "pcblist",
696 1.86 manu SYSCTL_DESCR("SOCK_SEQPACKET protocol control "
697 1.86 manu "block list"),
698 1.86 manu sysctl_unpcblist, 0, NULL, 0,
699 1.86 manu CTL_NET, PF_LOCAL, SOCK_SEQPACKET, CTL_CREATE, CTL_EOL);
700 1.86 manu sysctl_createv(&domain_sysctllog, 0, NULL, NULL,
701 1.86 manu CTLFLAG_PERMANENT,
702 1.86 manu CTLTYPE_STRUCT, "pcblist",
703 1.50 atatat SYSCTL_DESCR("SOCK_DGRAM protocol control block list"),
704 1.50 atatat sysctl_unpcblist, 0, NULL, 0,
705 1.50 atatat CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
706 1.1 cgd }
707 1.1 cgd
708 1.4 andrew void
709 1.63 dyoung pfctlinput(int cmd, const struct sockaddr *sa)
710 1.1 cgd {
711 1.31 augustss struct domain *dp;
712 1.47 matt const struct protosw *pr;
713 1.1 cgd
714 1.63 dyoung DOMAIN_FOREACH(dp) {
715 1.63 dyoung for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
716 1.63 dyoung if (pr->pr_ctlinput != NULL)
717 1.13 christos (*pr->pr_ctlinput)(cmd, sa, NULL);
718 1.63 dyoung }
719 1.63 dyoung }
720 1.34 itojun }
721 1.34 itojun
722 1.34 itojun void
723 1.63 dyoung pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam)
724 1.34 itojun {
725 1.34 itojun struct domain *dp;
726 1.47 matt const struct protosw *pr;
727 1.34 itojun
728 1.63 dyoung if (sa == NULL)
729 1.34 itojun return;
730 1.49 matt
731 1.49 matt DOMAIN_FOREACH(dp) {
732 1.34 itojun /*
733 1.34 itojun * the check must be made by xx_ctlinput() anyways, to
734 1.34 itojun * make sure we use data item pointed to by ctlparam in
735 1.34 itojun * correct way. the following check is made just for safety.
736 1.34 itojun */
737 1.34 itojun if (dp->dom_family != sa->sa_family)
738 1.34 itojun continue;
739 1.34 itojun
740 1.63 dyoung for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
741 1.63 dyoung if (pr->pr_ctlinput != NULL)
742 1.34 itojun (*pr->pr_ctlinput)(cmd, sa, ctlparam);
743 1.63 dyoung }
744 1.34 itojun }
745 1.1 cgd }
746 1.1 cgd
747 1.4 andrew void
748 1.62 yamt pfslowtimo(void *arg)
749 1.1 cgd {
750 1.31 augustss struct domain *dp;
751 1.47 matt const struct protosw *pr;
752 1.1 cgd
753 1.19 thorpej pfslowtimo_now++;
754 1.19 thorpej
755 1.49 matt DOMAIN_FOREACH(dp) {
756 1.1 cgd for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
757 1.1 cgd if (pr->pr_slowtimo)
758 1.1 cgd (*pr->pr_slowtimo)();
759 1.49 matt }
760 1.87 seanb callout_schedule(&pfslowtimo_ch, hz / PR_SLOWHZ);
761 1.1 cgd }
762 1.1 cgd
763 1.4 andrew void
764 1.62 yamt pffasttimo(void *arg)
765 1.1 cgd {
766 1.31 augustss struct domain *dp;
767 1.47 matt const struct protosw *pr;
768 1.19 thorpej
769 1.19 thorpej pffasttimo_now++;
770 1.1 cgd
771 1.49 matt DOMAIN_FOREACH(dp) {
772 1.1 cgd for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
773 1.1 cgd if (pr->pr_fasttimo)
774 1.1 cgd (*pr->pr_fasttimo)();
775 1.49 matt }
776 1.87 seanb callout_schedule(&pffasttimo_ch, hz / PR_FASTHZ);
777 1.1 cgd }
778