xref: /illumos-gate/usr/src/uts/common/inet/inet_common.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
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 /*
27  * Minor number allocation for various protocol modules.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/kmem.h>
32 #include <sys/mutex.h>
33 #include <sys/ddi.h>
34 #include <sys/types.h>
35 #include <sys/mkdev.h>
36 #include <sys/param.h>
37 #include <inet/common.h>
38 
39 typedef struct inet_arena {
40 	vmem_t *ineta_arena;	/* Minor number arena */
41 	minor_t ineta_maxminor;	/* max minor number in the arena */
42 } inet_arena_t;
43 
44 void *
inet_minor_create(char * name,dev_t min_dev,dev_t max_dev,int kmflags)45 inet_minor_create(char *name, dev_t min_dev, dev_t max_dev, int kmflags)
46 {
47 	inet_arena_t *arena = kmem_alloc(sizeof (inet_arena_t), kmflags);
48 
49 	if (arena != NULL) {
50 		arena->ineta_maxminor = max_dev;
51 		arena->ineta_arena = vmem_create(name,
52 		    (void *)min_dev, arena->ineta_maxminor - min_dev + 1,
53 		    1, NULL, NULL, NULL, 1, kmflags | VMC_IDENTIFIER);
54 
55 		if (arena->ineta_arena == NULL) {
56 			kmem_free(arena, sizeof (inet_arena_t));
57 			arena = NULL;
58 		}
59 	}
60 
61 	return (arena);
62 }
63 
64 void
inet_minor_destroy(void * a)65 inet_minor_destroy(void *a)
66 {
67 	inet_arena_t *arena = (inet_arena_t *)a;
68 
69 	if (arena != NULL) {
70 		vmem_destroy(arena->ineta_arena);
71 		kmem_free(arena, sizeof (inet_arena_t));
72 	}
73 }
74 
75 dev_t
inet_minor_alloc(void * arena)76 inet_minor_alloc(void *arena)
77 {
78 	return ((dev_t)vmem_alloc(((inet_arena_t *)arena)->ineta_arena,
79 	    1, VM_NOSLEEP));
80 }
81 
82 void
inet_minor_free(void * arena,dev_t dev)83 inet_minor_free(void *arena, dev_t dev)
84 {
85 	ASSERT((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN));
86 	vmem_free(((inet_arena_t *)arena)->ineta_arena, (void *)dev, 1);
87 }
88 
89 /*
90  * This function is used to free a message that has gone through
91  * mi_copyin processing which modifies the M_IOCTL mblk's b_next
92  * and b_prev pointers. We use this function to set b_next/b_prev
93  * to NULL and free them.
94  */
95 void
inet_freemsg(mblk_t * mp)96 inet_freemsg(mblk_t *mp)
97 {
98 	mblk_t	*bp = mp;
99 
100 	for (; bp != NULL; bp = bp->b_cont) {
101 		bp->b_prev = NULL;
102 		bp->b_next = NULL;
103 	}
104 	freemsg(mp);
105 }
106