Print this page
4770 soconfig(1M) needs an option to print the in-kernel socket configuration table
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/sockfs/sockparams.c
+++ new/usr/src/uts/common/fs/sockfs/sockparams.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 25 */
25 26
26 27 #include <sys/types.h>
27 28 #include <sys/t_lock.h>
28 29 #include <sys/param.h>
29 30 #include <sys/systm.h>
30 31 #include <sys/sysmacros.h>
31 32 #include <sys/cmn_err.h>
32 33 #include <sys/list.h>
34 +#include <sys/sunddi.h>
33 35
34 36 #include <sys/stropts.h>
35 37 #include <sys/socket.h>
36 38 #include <sys/socketvar.h>
37 39
38 40 #include <fs/sockfs/sockcommon.h>
39 41 #include <fs/sockfs/sockfilter_impl.h>
40 42 #include <fs/sockfs/socktpi.h>
41 43
42 44 /*
43 45 * Socket Parameters
44 46 *
45 47 * Socket parameter (struct sockparams) entries represent the socket types
46 48 * available on the system.
47 49 *
48 50 * Flags (sp_flags):
49 51 *
50 52 * SOCKPARAMS_EPHEMERAL: A temporary sockparams entry that will be deleted
51 53 * as soon as its' ref count drops to zero. In addition, ephemeral entries will
52 54 * never be hooked onto the global sockparams list. Ephemeral entries are
53 55 * created when application requests to create a socket using an application
54 56 * supplied device path, or when a socket is falling back to TPI.
55 57 *
56 58 * Lock order:
57 59 * The lock order is sockconf_lock -> sp_lock.
58 60 */
59 61 extern int kobj_path_exists(char *, int);
60 62
61 63 static int sockparams_sdev_init(struct sockparams *, char *, int);
62 64 static void sockparams_sdev_fini(struct sockparams *);
63 65
64 66 /*
65 67 * Global sockparams list (populated via soconfig(1M)).
66 68 */
67 69 static list_t sphead;
68 70
69 71 /*
70 72 * List of ephemeral sockparams.
71 73 */
72 74 static list_t sp_ephem_list;
73 75
74 76 /* Global kstats for sockparams */
75 77 typedef struct sockparams_g_stats {
76 78 kstat_named_t spgs_ephem_nalloc;
77 79 kstat_named_t spgs_ephem_nreuse;
78 80 } sockparams_g_stats_t;
79 81
80 82 static sockparams_g_stats_t sp_g_stats;
81 83 static kstat_t *sp_g_kstat;
82 84
83 85
84 86 void
85 87 sockparams_init(void)
86 88 {
87 89 list_create(&sphead, sizeof (struct sockparams),
88 90 offsetof(struct sockparams, sp_node));
89 91 list_create(&sp_ephem_list, sizeof (struct sockparams),
90 92 offsetof(struct sockparams, sp_node));
91 93
92 94 kstat_named_init(&sp_g_stats.spgs_ephem_nalloc, "ephemeral_nalloc",
93 95 KSTAT_DATA_UINT64);
94 96 kstat_named_init(&sp_g_stats.spgs_ephem_nreuse, "ephemeral_nreuse",
95 97 KSTAT_DATA_UINT64);
96 98
97 99 sp_g_kstat = kstat_create("sockfs", 0, "sockparams", "misc",
98 100 KSTAT_TYPE_NAMED, sizeof (sp_g_stats) / sizeof (kstat_named_t),
99 101 KSTAT_FLAG_VIRTUAL);
100 102 if (sp_g_kstat == NULL)
101 103 return;
102 104
103 105 sp_g_kstat->ks_data = &sp_g_stats;
104 106
105 107 kstat_install(sp_g_kstat);
106 108 }
107 109
108 110 static int
109 111 sockparams_kstat_update(kstat_t *ksp, int rw)
110 112 {
111 113 struct sockparams *sp = ksp->ks_private;
112 114 sockparams_stats_t *sps = ksp->ks_data;
113 115
114 116 if (rw == KSTAT_WRITE)
115 117 return (EACCES);
116 118
117 119 sps->sps_nactive.value.ui64 = sp->sp_refcnt;
118 120
119 121 return (0);
120 122 }
121 123
122 124 /*
123 125 * Setup kstats for the given sockparams entry.
124 126 */
125 127 static void
126 128 sockparams_kstat_init(struct sockparams *sp)
127 129 {
128 130 char name[KSTAT_STRLEN];
129 131
130 132 (void) snprintf(name, KSTAT_STRLEN, "socket_%d_%d_%d", sp->sp_family,
131 133 sp->sp_type, sp->sp_protocol);
132 134
133 135 sp->sp_kstat = kstat_create("sockfs", 0, name, "misc", KSTAT_TYPE_NAMED,
134 136 sizeof (sockparams_stats_t) / sizeof (kstat_named_t),
135 137 KSTAT_FLAG_VIRTUAL);
136 138
137 139 if (sp->sp_kstat == NULL)
138 140 return;
139 141
140 142 sp->sp_kstat->ks_data = &sp->sp_stats;
141 143 sp->sp_kstat->ks_update = sockparams_kstat_update;
142 144 sp->sp_kstat->ks_private = sp;
143 145 kstat_install(sp->sp_kstat);
144 146 }
145 147
146 148 static void
147 149 sockparams_kstat_fini(struct sockparams *sp)
148 150 {
149 151 if (sp->sp_kstat != NULL) {
150 152 kstat_delete(sp->sp_kstat);
151 153 sp->sp_kstat = NULL;
152 154 }
153 155 }
154 156
155 157 /*
156 158 * sockparams_create(int family, int type, int protocol, char *modname,
157 159 * char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
158 160 *
159 161 * Create a new sockparams entry.
160 162 *
161 163 * Arguments:
162 164 * family, type, protocol: specifies the socket type
163 165 * modname: Name of the module associated with the socket type. The
164 166 * module can be NULL if a device path is given, in which
165 167 * case the TPI module is used.
166 168 * devpath: Path to the STREAMS device. Must be NULL for non-STREAMS
167 169 * based transports.
168 170 * devpathlen: Length of the devpath string. The argument can be 0,
169 171 * indicating that devpath was allocated statically, and should
170 172 * not be freed when the sockparams entry is destroyed.
171 173 *
172 174 * flags : SOCKPARAMS_EPHEMERAL is the only flag that is allowed.
173 175 * kmflags: KM_{NO,}SLEEP
174 176 * errorp : Value-return argument, set when an error occurs.
175 177 *
176 178 * Returns:
177 179 * On success a new sockparams entry is returned, and *errorp is set
178 180 * to 0. On failure NULL is returned and *errorp is set to indicate the
179 181 * type of error that occured.
180 182 *
181 183 * Notes:
182 184 * devpath and modname are freed upon failure.
183 185 */
184 186 struct sockparams *
185 187 sockparams_create(int family, int type, int protocol, char *modname,
186 188 char *devpath, int devpathlen, int flags, int kmflags, int *errorp)
187 189 {
188 190 struct sockparams *sp = NULL;
189 191 size_t size;
190 192
191 193 ASSERT((flags & ~SOCKPARAMS_EPHEMERAL) == 0);
192 194 if (flags & ~SOCKPARAMS_EPHEMERAL) {
193 195 *errorp = EINVAL;
194 196 goto error;
195 197 }
196 198
197 199 /* either a module or device must be given, but not both */
198 200 if (modname == NULL && devpath == NULL) {
199 201 *errorp = EINVAL;
200 202 goto error;
201 203 }
202 204
203 205 sp = kmem_zalloc(sizeof (*sp), kmflags);
204 206 if (sp == NULL) {
205 207 *errorp = ENOMEM;
206 208 goto error;
207 209 }
208 210 sp->sp_family = family;
209 211 sp->sp_type = type;
210 212 sp->sp_protocol = protocol;
211 213 sp->sp_refcnt = 0;
212 214 sp->sp_flags = flags;
213 215
214 216 list_create(&sp->sp_auto_filters, sizeof (sp_filter_t),
215 217 offsetof(sp_filter_t, spf_node));
216 218 list_create(&sp->sp_prog_filters, sizeof (sp_filter_t),
217 219 offsetof(sp_filter_t, spf_node));
218 220
219 221 kstat_named_init(&sp->sp_stats.sps_nfallback, "nfallback",
220 222 KSTAT_DATA_UINT64);
221 223 kstat_named_init(&sp->sp_stats.sps_nactive, "nactive",
222 224 KSTAT_DATA_UINT64);
223 225 kstat_named_init(&sp->sp_stats.sps_ncreate, "ncreate",
224 226 KSTAT_DATA_UINT64);
225 227
226 228 /*
227 229 * Track how many ephemeral entries we have created.
228 230 */
229 231 if (sp->sp_flags & SOCKPARAMS_EPHEMERAL)
230 232 sp_g_stats.spgs_ephem_nalloc.value.ui64++;
231 233
232 234 if (modname != NULL) {
233 235 sp->sp_smod_name = modname;
234 236 } else {
235 237 size = strlen(SOTPI_SMOD_NAME) + 1;
236 238 modname = kmem_zalloc(size, kmflags);
237 239 if (modname == NULL) {
238 240 *errorp = ENOMEM;
239 241 goto error;
240 242 }
241 243 sp->sp_smod_name = modname;
242 244 (void) sprintf(sp->sp_smod_name, "%s", SOTPI_SMOD_NAME);
243 245 }
244 246
245 247 if (devpath != NULL) {
246 248 /* Set up the device entry. */
247 249 *errorp = sockparams_sdev_init(sp, devpath, devpathlen);
248 250 if (*errorp != 0)
249 251 goto error;
250 252 }
251 253
252 254 mutex_init(&sp->sp_lock, NULL, MUTEX_DEFAULT, NULL);
253 255 *errorp = 0;
254 256 return (sp);
255 257 error:
256 258 ASSERT(*errorp != 0);
257 259 if (modname != NULL)
258 260 kmem_free(modname, strlen(modname) + 1);
259 261 if (devpathlen != 0)
260 262 kmem_free(devpath, devpathlen);
261 263 if (sp != NULL)
262 264 kmem_free(sp, sizeof (*sp));
263 265 return (NULL);
264 266 }
265 267
266 268 /*
267 269 * Initialize the STREAMS device aspect of the sockparams entry.
268 270 */
269 271 static int
270 272 sockparams_sdev_init(struct sockparams *sp, char *devpath, int devpathlen)
271 273 {
272 274 vnode_t *vp = NULL;
273 275 int error;
274 276
275 277 ASSERT(devpath != NULL);
276 278
277 279 if ((error = sogetvp(devpath, &vp, UIO_SYSSPACE)) != 0) {
278 280 dprint(0, ("sockparams_sdev_init: vp %s failed with %d\n",
279 281 devpath, error));
280 282 return (error);
281 283 }
282 284
283 285 ASSERT(vp != NULL);
284 286 sp->sp_sdev_info.sd_vnode = vp;
285 287 sp->sp_sdev_info.sd_devpath = devpath;
286 288 sp->sp_sdev_info.sd_devpathlen = devpathlen;
287 289
288 290 return (0);
289 291 }
290 292
291 293 /*
292 294 * sockparams_destroy(struct sockparams *sp)
293 295 *
294 296 * Releases all the resources associated with the sockparams entry,
295 297 * and frees the sockparams entry.
296 298 *
297 299 * Arguments:
298 300 * sp: the sockparams entry to destroy.
299 301 *
300 302 * Returns:
301 303 * Nothing.
302 304 *
303 305 * Locking:
304 306 * The sp_lock of the entry can not be held.
305 307 */
306 308 void
307 309 sockparams_destroy(struct sockparams *sp)
308 310 {
309 311 ASSERT(sp->sp_refcnt == 0);
310 312 ASSERT(!list_link_active(&sp->sp_node));
311 313
312 314 sockparams_sdev_fini(sp);
313 315
314 316 if (sp->sp_smod_info != NULL)
315 317 SMOD_DEC_REF(sp->sp_smod_info, sp->sp_smod_name);
316 318 kmem_free(sp->sp_smod_name, strlen(sp->sp_smod_name) + 1);
317 319 sp->sp_smod_name = NULL;
318 320 sp->sp_smod_info = NULL;
319 321 mutex_destroy(&sp->sp_lock);
320 322 sockparams_kstat_fini(sp);
321 323
322 324 sof_sockparams_fini(sp);
323 325 list_destroy(&sp->sp_auto_filters);
324 326 list_destroy(&sp->sp_prog_filters);
325 327
326 328 kmem_free(sp, sizeof (*sp));
327 329 }
328 330
329 331 /*
330 332 * Clean up the STREAMS device part of the sockparams entry.
331 333 */
332 334 static void
333 335 sockparams_sdev_fini(struct sockparams *sp)
334 336 {
335 337 sdev_info_t sd;
336 338
337 339 /*
338 340 * if the entry does not have a STREAMS device, then there
339 341 * is nothing to do.
340 342 */
341 343 if (!SOCKPARAMS_HAS_DEVICE(sp))
342 344 return;
343 345
344 346 sd = sp->sp_sdev_info;
345 347 if (sd.sd_vnode != NULL)
346 348 VN_RELE(sd.sd_vnode);
347 349 if (sd.sd_devpathlen != 0)
348 350 kmem_free(sd.sd_devpath, sd.sd_devpathlen);
349 351
350 352 sp->sp_sdev_info.sd_vnode = NULL;
351 353 sp->sp_sdev_info.sd_devpath = NULL;
352 354 }
353 355
354 356 /*
355 357 * Look for a matching sockparams entry on the given list.
356 358 * The caller must hold the associated list lock.
357 359 */
358 360 static struct sockparams *
359 361 sockparams_find(list_t *list, int family, int type, int protocol,
360 362 boolean_t by_devpath, const char *name)
361 363 {
362 364 struct sockparams *sp;
363 365
364 366 for (sp = list_head(list); sp != NULL; sp = list_next(list, sp)) {
365 367 if (sp->sp_family == family && sp->sp_type == type) {
366 368 if (sp->sp_protocol == protocol) {
367 369 if (name == NULL)
368 370 break;
369 371 else if (by_devpath &&
370 372 sp->sp_sdev_info.sd_devpath != NULL &&
371 373 strcmp(sp->sp_sdev_info.sd_devpath,
372 374 name) == 0)
373 375 break;
374 376 else if (strcmp(sp->sp_smod_name, name) == 0)
375 377 break;
376 378 }
377 379 }
378 380 }
379 381 return (sp);
380 382 }
381 383
382 384 /*
383 385 * sockparams_hold_ephemeral()
384 386 *
385 387 * Returns an ephemeral sockparams entry of the requested family, type and
386 388 * protocol. The entry is returned held, and the caller is responsible for
387 389 * dropping the reference using SOCKPARAMS_DEC_REF() once done.
388 390 *
389 391 * All ephemeral entries are on list (sp_ephem_list). If there is an
390 392 * entry on the list that match the search criteria, then a reference is
391 393 * placed on that entry. Otherwise, a new entry is created and inserted
392 394 * in the list. The entry is removed from the list when the last reference
393 395 * is dropped.
394 396 *
395 397 * The tpi flag is used to determine whether name refers to a device or
396 398 * module name.
397 399 */
398 400 static struct sockparams *
399 401 sockparams_hold_ephemeral(int family, int type, int protocol,
400 402 const char *name, boolean_t by_devpath, int kmflag, int *errorp)
401 403 {
402 404 struct sockparams *sp = NULL;
403 405 *errorp = 0;
404 406
405 407 /*
406 408 * First look for an existing entry
407 409 */
408 410 rw_enter(&sockconf_lock, RW_READER);
409 411 sp = sockparams_find(&sp_ephem_list, family, type, protocol,
410 412 by_devpath, name);
411 413 if (sp != NULL) {
412 414 SOCKPARAMS_INC_REF(sp);
413 415 rw_exit(&sockconf_lock);
414 416 sp_g_stats.spgs_ephem_nreuse.value.ui64++;
415 417
416 418 return (sp);
417 419 } else {
418 420 struct sockparams *newsp = NULL;
419 421 char *namebuf = NULL;
420 422 int namelen = 0;
421 423
422 424 rw_exit(&sockconf_lock);
423 425
424 426 namelen = strlen(name) + 1;
425 427 namebuf = kmem_alloc(namelen, kmflag);
426 428 if (namebuf == NULL) {
427 429 *errorp = ENOMEM;
428 430 return (NULL);
429 431 }
430 432
431 433 (void *)strncpy(namebuf, name, namelen);
432 434 if (by_devpath) {
433 435 newsp = sockparams_create(family, type,
434 436 protocol, NULL, namebuf, namelen,
435 437 SOCKPARAMS_EPHEMERAL, kmflag, errorp);
436 438 } else {
437 439 newsp = sockparams_create(family, type,
438 440 protocol, namebuf, NULL, 0,
439 441 SOCKPARAMS_EPHEMERAL, kmflag, errorp);
440 442 }
441 443
442 444 if (newsp == NULL) {
443 445 ASSERT(*errorp != 0);
444 446 return (NULL);
445 447 }
446 448
447 449 /*
448 450 * Time to load the socket module.
449 451 */
450 452 ASSERT(newsp->sp_smod_info == NULL);
451 453 newsp->sp_smod_info =
452 454 smod_lookup_byname(newsp->sp_smod_name);
453 455 if (newsp->sp_smod_info == NULL) {
454 456 /* Failed to load */
455 457 sockparams_destroy(newsp);
456 458 *errorp = ENXIO;
457 459 return (NULL);
458 460 }
459 461
460 462 /*
461 463 * The sockparams entry was created, now try to add it
462 464 * to the list. We need to hold the lock as a WRITER.
463 465 */
464 466 rw_enter(&sockconf_lock, RW_WRITER);
465 467 sp = sockparams_find(&sp_ephem_list, family, type, protocol,
466 468 by_devpath, name);
467 469 if (sp != NULL) {
468 470 /*
469 471 * Someone has requested a matching entry, so just
470 472 * place a hold on it and release the entry we alloc'ed.
471 473 */
472 474 SOCKPARAMS_INC_REF(sp);
473 475 rw_exit(&sockconf_lock);
474 476
475 477 sockparams_destroy(newsp);
476 478 } else {
477 479 *errorp = sof_sockparams_init(newsp);
478 480 if (*errorp != 0) {
479 481 rw_exit(&sockconf_lock);
480 482 sockparams_destroy(newsp);
481 483 return (NULL);
482 484 }
483 485 SOCKPARAMS_INC_REF(newsp);
484 486 list_insert_tail(&sp_ephem_list, newsp);
485 487 rw_exit(&sockconf_lock);
486 488
487 489 sp = newsp;
488 490 }
489 491 ASSERT(*errorp == 0);
490 492
491 493 return (sp);
492 494 }
493 495 }
494 496
495 497 struct sockparams *
496 498 sockparams_hold_ephemeral_bydev(int family, int type, int protocol,
497 499 const char *dev, int kmflag, int *errorp)
498 500 {
499 501 return (sockparams_hold_ephemeral(family, type, protocol, dev, B_TRUE,
500 502 kmflag, errorp));
501 503 }
502 504
503 505 struct sockparams *
504 506 sockparams_hold_ephemeral_bymod(int family, int type, int protocol,
505 507 const char *mod, int kmflag, int *errorp)
506 508 {
507 509 return (sockparams_hold_ephemeral(family, type, protocol, mod, B_FALSE,
508 510 kmflag, errorp));
509 511 }
510 512
511 513 /*
512 514 * Called when the last socket using the ephemeral entry is dropping
513 515 * its' reference. To maintain lock order we must drop the sockparams
514 516 * lock before calling this function. As a result, a new reference
515 517 * might be placed on the entry, in which case there is nothing to
516 518 * do. However, if ref count goes to zero, we delete the entry.
517 519 */
518 520 void
519 521 sockparams_ephemeral_drop_last_ref(struct sockparams *sp)
520 522 {
521 523 ASSERT(sp->sp_flags & SOCKPARAMS_EPHEMERAL);
522 524 ASSERT(MUTEX_NOT_HELD(&sp->sp_lock));
523 525
524 526 rw_enter(&sockconf_lock, RW_WRITER);
525 527 mutex_enter(&sp->sp_lock);
526 528
527 529 if (--sp->sp_refcnt == 0) {
528 530 list_remove(&sp_ephem_list, sp);
529 531 mutex_exit(&sp->sp_lock);
530 532 rw_exit(&sockconf_lock);
531 533
532 534 sockparams_destroy(sp);
533 535 } else {
534 536 mutex_exit(&sp->sp_lock);
535 537 rw_exit(&sockconf_lock);
536 538 }
537 539 }
538 540
539 541 /*
540 542 * sockparams_add(struct sockparams *sp)
541 543 *
542 544 * Tries to add the given sockparams entry to the global list.
543 545 *
544 546 * Arguments:
545 547 * sp: the sockparms entry to add
546 548 *
547 549 * Returns:
548 550 * On success 0, but if an entry already exists, then EEXIST
549 551 * is returned.
550 552 *
551 553 * Locking:
552 554 * The caller can not be holding sockconf_lock.
553 555 */
554 556 int
555 557 sockparams_add(struct sockparams *sp)
556 558 {
557 559 int error;
558 560
559 561 ASSERT(!(sp->sp_flags & SOCKPARAMS_EPHEMERAL));
560 562
561 563 rw_enter(&sockconf_lock, RW_WRITER);
562 564 if (sockparams_find(&sphead, sp->sp_family, sp->sp_type,
563 565 sp->sp_protocol, B_TRUE, NULL) != 0) {
564 566 rw_exit(&sockconf_lock);
565 567 return (EEXIST);
566 568 } else {
567 569 /*
568 570 * Unique sockparams entry, so init the kstats.
569 571 */
570 572 sockparams_kstat_init(sp);
571 573
572 574 /*
573 575 * Before making the socket type available we must make
574 576 * sure that interested socket filters are aware of it.
575 577 */
576 578 error = sof_sockparams_init(sp);
577 579 if (error != 0) {
578 580 rw_exit(&sockconf_lock);
579 581 return (error);
580 582 }
581 583 list_insert_tail(&sphead, sp);
582 584 rw_exit(&sockconf_lock);
583 585 return (0);
584 586 }
585 587 }
586 588
587 589 /*
588 590 * sockparams_delete(int family, int type, int protocol)
589 591 *
590 592 * Marks the sockparams entry for a specific family, type and protocol
591 593 * for deletion. The entry is removed from the list and destroyed
592 594 * if no one is holding a reference to it.
593 595 *
594 596 * Arguments:
595 597 * family, type, protocol: the socket type that should be removed.
596 598 *
597 599 * Returns:
598 600 * On success 0, otherwise ENXIO.
599 601 *
600 602 * Locking:
601 603 * Caller can not be holding sockconf_lock or the sp_lock of
602 604 * any sockparams entry.
603 605 */
604 606 int
605 607 sockparams_delete(int family, int type, int protocol)
606 608 {
607 609 struct sockparams *sp;
608 610
609 611 rw_enter(&sockconf_lock, RW_WRITER);
610 612 sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
611 613
612 614 if (sp != NULL) {
613 615 /*
614 616 * If no one is holding a reference to the entry, then
615 617 * we go ahead and remove it from the list and then
616 618 * destroy it.
617 619 */
618 620 mutex_enter(&sp->sp_lock);
619 621 if (sp->sp_refcnt != 0) {
620 622 mutex_exit(&sp->sp_lock);
621 623 rw_exit(&sockconf_lock);
622 624 return (EBUSY);
623 625 }
624 626 mutex_exit(&sp->sp_lock);
625 627 /* Delete the sockparams entry. */
626 628 list_remove(&sphead, sp);
627 629 rw_exit(&sockconf_lock);
628 630
629 631 sockparams_destroy(sp);
630 632 return (0);
631 633 } else {
632 634 rw_exit(&sockconf_lock);
633 635 return (ENXIO);
634 636 }
635 637 }
636 638
637 639
638 640 /*
639 641 * solookup(int family, int type, int protocol, struct sockparams **spp)
640 642 *
641 643 * Lookup an entry in the sockparams list based on the triple. The returned
642 644 * entry either exactly match the given tuple, or it is the 'default' entry
643 645 * for the given <family, type>. A default entry is on with a protocol
644 646 * value of zero.
645 647 *
646 648 * Arguments:
647 649 * family, type, protocol: tuple to search for
648 650 * spp: Value-return argument
649 651 *
650 652 * Returns:
651 653 * If an entry is found, 0 is returned and *spp is set to point to the
652 654 * entry. In case an entry is not found, *spp is set to NULL, and an
653 655 * error code is returned. The errors are (in decreasing precedence):
654 656 * EAFNOSUPPORT - address family not in list
655 657 * EPROTONOSUPPORT - address family supported but not protocol.
656 658 * EPROTOTYPE - address family and protocol supported but not socket type.
657 659 *
658 660 * TODO: should use ddi_modopen()/ddi_modclose()
659 661 */
660 662 int
661 663 solookup(int family, int type, int protocol, struct sockparams **spp)
662 664 {
663 665 struct sockparams *sp = NULL;
664 666 int error = 0;
665 667
666 668 *spp = NULL;
667 669 rw_enter(&sockconf_lock, RW_READER);
668 670
669 671 /*
670 672 * Search the sockparams list for an appropiate entry.
671 673 * Hopefully we find an entry that match the exact family,
672 674 * type and protocol specified by the user, in which case
673 675 * we return that entry. However, we also keep track of
674 676 * the default entry for a specific family and type, the
675 677 * entry of which would have a protocol value of 0.
676 678 */
677 679 sp = sockparams_find(&sphead, family, type, protocol, B_TRUE, NULL);
678 680
679 681 if (sp == NULL) {
680 682 int found = 0;
681 683
682 684 /* Determine correct error code */
683 685 for (sp = list_head(&sphead); sp != NULL;
684 686 sp = list_next(&sphead, sp)) {
685 687 if (sp->sp_family == family && found < 1)
686 688 found = 1;
687 689 if (sp->sp_family == family &&
688 690 sp->sp_protocol == protocol && found < 2)
689 691 found = 2;
690 692 }
691 693 rw_exit(&sockconf_lock);
692 694 switch (found) {
693 695 case 0:
694 696 error = EAFNOSUPPORT;
695 697 break;
696 698 case 1:
697 699 error = EPROTONOSUPPORT;
698 700 break;
699 701 case 2:
700 702 error = EPROTOTYPE;
701 703 break;
702 704 }
703 705 return (error);
704 706 }
705 707
706 708 /*
707 709 * An entry was found.
708 710 *
709 711 * We put a hold on the entry early on, so if the
710 712 * sockmod is not loaded, and we have to exit
711 713 * sockconf_lock to call modload(), we know that the
712 714 * sockparams entry wont go away. That way we don't
713 715 * have to look up the entry once we come back from
714 716 * modload().
715 717 */
716 718 SOCKPARAMS_INC_REF(sp);
717 719 rw_exit(&sockconf_lock);
718 720
719 721 if (sp->sp_smod_info == NULL) {
720 722 smod_info_t *smod = smod_lookup_byname(sp->sp_smod_name);
721 723
722 724 if (smod == NULL) {
723 725 /*
724 726 * We put a hold on the sockparams entry
725 727 * earlier, hoping everything would work out.
726 728 * That obviously did not happen, so release
727 729 * the hold here.
728 730 */
729 731 SOCKPARAMS_DEC_REF(sp);
730 732 /*
731 733 * We should probably mark the sockparams as
732 734 * "bad", and redo the lookup skipping the
733 735 * "bad" entries. I.e., sp->sp_mod_state |= BAD,
734 736 * return (solookup(...))
735 737 */
736 738 return (ENXIO);
737 739 }
738 740 /*
739 741 * Another thread might have already looked up the socket
740 742 * module for this entry. In that case we need to drop our
741 743 * reference to `smod' to ensure that the sockparams entry
742 744 * only holds one reference.
743 745 */
744 746 mutex_enter(&sp->sp_lock);
745 747 if (sp->sp_smod_info == NULL)
746 748 sp->sp_smod_info = smod;
747 749 else
748 750 SMOD_DEC_REF(smod, sp->sp_smod_name);
749 751 mutex_exit(&sp->sp_lock);
750 752 }
751 753
752 754 /*
753 755 * Alright, we have a valid sockparams entry.
754 756 */
755 757 *spp = sp;
756 758 return (0);
757 759 }
758 760
759 761 /*
760 762 * Called when filter entry `ent' is going away. All sockparams remove
761 763 * their references to `ent'.
762 764 */
763 765 static void
764 766 sockparams_filter_cleanup_impl(sof_entry_t *ent, list_t *list)
765 767 {
766 768 struct sockparams *sp;
767 769 sp_filter_t *fil;
768 770 list_t *flist;
769 771
770 772 ASSERT(RW_WRITE_HELD(&sockconf_lock));
771 773
772 774 for (sp = list_head(list); sp != NULL;
773 775 sp = list_next(list, sp)) {
774 776 flist = (ent->sofe_flags & SOFEF_AUTO) ?
775 777 &sp->sp_auto_filters : &sp->sp_prog_filters;
776 778 for (fil = list_head(flist); fil != NULL;
777 779 fil = list_next(flist, fil)) {
778 780 if (fil->spf_filter == ent) {
779 781 list_remove(flist, fil);
780 782 kmem_free(fil, sizeof (sp_filter_t));
781 783 break;
782 784 }
783 785 }
784 786 }
785 787 }
786 788 void
787 789 sockparams_filter_cleanup(sof_entry_t *ent)
788 790 {
789 791 sockparams_filter_cleanup_impl(ent, &sphead);
790 792 sockparams_filter_cleanup_impl(ent, &sp_ephem_list);
791 793 }
792 794
793 795 /*
794 796 * New filter is being added; walk the list of sockparams to see if
795 797 * the filter is interested in any of the sockparams.
796 798 */
797 799 static int
798 800 sockparams_new_filter_impl(sof_entry_t *ent, list_t *list)
799 801 {
800 802 struct sockparams *sp;
801 803 int err;
802 804
803 805 ASSERT(RW_WRITE_HELD(&sockconf_lock));
804 806
805 807 for (sp = list_head(list); sp != NULL;
806 808 sp = list_next(list, sp)) {
807 809 if ((err = sof_entry_proc_sockparams(ent, sp)) != 0) {
808 810 sockparams_filter_cleanup(ent);
809 811 return (err);
810 812 }
811 813 }
812 814 return (0);
813 815 }
814 816
815 817 int
↓ open down ↓ |
773 lines elided |
↑ open up ↑ |
816 818 sockparams_new_filter(sof_entry_t *ent)
817 819 {
818 820 int error;
819 821
820 822 if ((error = sockparams_new_filter_impl(ent, &sphead)) != 0)
821 823 return (error);
822 824
823 825 if ((error = sockparams_new_filter_impl(ent, &sp_ephem_list)) != 0)
824 826 sockparams_filter_cleanup_impl(ent, &sphead);
825 827 return (error);
828 +}
829 +
830 +/*
831 + * Setup and return socket configuration table.
832 + */
833 +int
834 +sockparams_copyout_socktable(uintptr_t socktable)
835 +{
836 + STRUCT_DECL(sockconfig_socktable, st);
837 + struct sockparams *sp;
838 + uint_t count;
839 + uint_t i = 0;
840 + int ret = 0;
841 + sockconfig_socktable_entry_t *se;
842 +
843 + STRUCT_INIT(st, get_udatamodel());
844 + if (ddi_copyin((void *)socktable, STRUCT_BUF(st),
845 + STRUCT_SIZE(st), 0) != 0)
846 + return (EFAULT);
847 +
848 + rw_enter(&sockconf_lock, RW_READER);
849 +
850 + count = STRUCT_FGET(st, num_of_entries);
851 + /*
852 + * If the output buffer is size zero, just copy out the count.
853 + */
854 + if (count == 0) {
855 + for (sp = list_head(&sphead); sp != NULL;
856 + sp = list_next(&sphead, sp)) {
857 + count++;
858 + }
859 + STRUCT_FSET(st, num_of_entries, count);
860 +
861 + rw_exit(&sockconf_lock);
862 + if (ddi_copyout(STRUCT_BUF(st), (void *)socktable,
863 + STRUCT_SIZE(st), 0) != 0)
864 + return (EFAULT);
865 +
866 + return (0);
867 + }
868 +
869 + se = kmem_alloc(count * sizeof (sockconfig_socktable_entry_t),
870 + KM_SLEEP);
871 + for (sp = list_head(&sphead); sp != NULL;
872 + sp = list_next(&sphead, sp)) {
873 + if (i >= count) {
874 + /*
875 + * Return if the number of entries has changed.
876 + */
877 + rw_exit(&sockconf_lock);
878 + kmem_free(se,
879 + count * sizeof (sockconfig_socktable_entry_t));
880 + return (EAGAIN);
881 + }
882 + se[i].se_family = sp->sp_family;
883 + se[i].se_type = sp->sp_type;
884 + se[i].se_protocol = sp->sp_protocol;
885 + (void) strncpy(se[i].se_modname, sp->sp_smod_name,
886 + MODMAXNAMELEN);
887 + if (sp->sp_sdev_info.sd_devpath != NULL)
888 + (void) strncpy(se[i].se_strdev,
889 + sp->sp_sdev_info.sd_devpath, MAXPATHLEN);
890 + se[i].se_refcnt = sp->sp_refcnt;
891 + se[i].se_flags = sp->sp_flags;
892 + i++;
893 + }
894 + rw_exit(&sockconf_lock);
895 + if (ddi_copyout(se, STRUCT_FGETP(st, st_entries),
896 + i * sizeof (sockconfig_socktable_entry_t), 0) != 0)
897 + ret = EFAULT;
898 +
899 + STRUCT_FSET(st, num_of_entries, i);
900 + kmem_free(se, count * sizeof (sockconfig_socktable_entry_t));
901 +
902 + if (ddi_copyout(STRUCT_BUF(st), (void *)socktable,
903 + STRUCT_SIZE(st), 0) != 0)
904 + ret = EFAULT;
905 +
906 + return (ret);
826 907 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX