xref: /illumos-gate/usr/src/uts/common/inet/inet_common.c (revision d3d50737e566cade9a08d73d2af95105ac7cd960)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Minor number allocation for various protocol modules.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/mutex.h>
35 #include <sys/ddi.h>
36 #include <sys/types.h>
37 #include <sys/mkdev.h>
38 #include <sys/param.h>
39 #include <inet/common.h>
40 
41 typedef struct inet_arena {
42 	vmem_t *ineta_arena;	/* Minor number arena */
43 	minor_t ineta_maxminor;	/* max minor number in the arena */
44 } inet_arena_t;
45 
46 void *
47 inet_minor_create(char *name, dev_t min_dev, dev_t max_dev, int kmflags)
48 {
49 	inet_arena_t *arena = kmem_alloc(sizeof (inet_arena_t), kmflags);
50 
51 	if (arena != NULL) {
52 		arena->ineta_maxminor = max_dev;
53 		arena->ineta_arena = vmem_create(name,
54 		    (void *)min_dev, arena->ineta_maxminor - min_dev + 1,
55 		    1, NULL, NULL, NULL, 1, kmflags | VMC_IDENTIFIER);
56 
57 		if (arena->ineta_arena == NULL) {
58 			kmem_free(arena, sizeof (inet_arena_t));
59 			arena = NULL;
60 		}
61 	}
62 
63 	return (arena);
64 }
65 
66 void
67 inet_minor_destroy(void *a)
68 {
69 	inet_arena_t *arena = (inet_arena_t *)a;
70 
71 	if (arena != NULL) {
72 		vmem_destroy(arena->ineta_arena);
73 		kmem_free(arena, sizeof (inet_arena_t));
74 	}
75 }
76 
77 dev_t
78 inet_minor_alloc(void *arena)
79 {
80 	return ((dev_t)vmem_alloc(((inet_arena_t *)arena)->ineta_arena,
81 	    1, VM_NOSLEEP));
82 }
83 
84 void
85 inet_minor_free(void *arena, dev_t dev)
86 {
87 	ASSERT((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN));
88 	vmem_free(((inet_arena_t *)arena)->ineta_arena, (void *)dev, 1);
89 }
90 
91 /*
92  * This function is used to free a message that has gone through
93  * mi_copyin processing which modifies the M_IOCTL mblk's b_next
94  * and b_prev pointers. We use this function to set b_next/b_prev
95  * to NULL and free them.
96  */
97 void
98 inet_freemsg(mblk_t *mp)
99 {
100 	mblk_t	*bp = mp;
101 
102 	for (; bp != NULL; bp = bp->b_cont) {
103 		bp->b_prev = NULL;
104 		bp->b_next = NULL;
105 	}
106 	freemsg(mp);
107 }
108