xref: /illumos-gate/usr/src/uts/common/inet/inet_common.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*aa92d85bSgt145670  * Common Development and Distribution License (the "License").
6*aa92d85bSgt145670  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*aa92d85bSgt145670  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Minor number allocation for various protocol modules.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <inet/common.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate typedef struct inet_arena {
407c478bd9Sstevel@tonic-gate 	vmem_t *ineta_arena;	/* Minor number arena */
417c478bd9Sstevel@tonic-gate 	minor_t ineta_maxminor;	/* max minor number in the arena */
427c478bd9Sstevel@tonic-gate } inet_arena_t;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate void *
inet_minor_create(char * name,dev_t min_dev,dev_t max_dev,int kmflags)45*aa92d85bSgt145670 inet_minor_create(char *name, dev_t min_dev, dev_t max_dev, int kmflags)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	inet_arena_t *arena = kmem_alloc(sizeof (inet_arena_t), kmflags);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (arena != NULL) {
50*aa92d85bSgt145670 		arena->ineta_maxminor = max_dev;
517c478bd9Sstevel@tonic-gate 		arena->ineta_arena = vmem_create(name,
527c478bd9Sstevel@tonic-gate 		    (void *)min_dev, arena->ineta_maxminor - min_dev + 1,
537c478bd9Sstevel@tonic-gate 		    1, NULL, NULL, NULL, 1, kmflags | VMC_IDENTIFIER);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 		if (arena->ineta_arena == NULL) {
567c478bd9Sstevel@tonic-gate 			kmem_free(arena, sizeof (inet_arena_t));
577c478bd9Sstevel@tonic-gate 			arena = NULL;
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	return (arena);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate void
inet_minor_destroy(void * a)657c478bd9Sstevel@tonic-gate inet_minor_destroy(void *a)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	inet_arena_t *arena = (inet_arena_t *)a;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (arena != NULL) {
707c478bd9Sstevel@tonic-gate 		vmem_destroy(arena->ineta_arena);
717c478bd9Sstevel@tonic-gate 		kmem_free(arena, sizeof (inet_arena_t));
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate dev_t
inet_minor_alloc(void * arena)76*aa92d85bSgt145670 inet_minor_alloc(void *arena)
777c478bd9Sstevel@tonic-gate {
78*aa92d85bSgt145670 	return ((dev_t)vmem_alloc(((inet_arena_t *)arena)->ineta_arena,
79*aa92d85bSgt145670 	    1, VM_NOSLEEP));
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
inet_minor_free(void * arena,dev_t dev)83*aa92d85bSgt145670 inet_minor_free(void *arena, dev_t dev)
847c478bd9Sstevel@tonic-gate {
85*aa92d85bSgt145670 	ASSERT((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN));
86*aa92d85bSgt145670 	vmem_free(((inet_arena_t *)arena)->ineta_arena, (void *)dev, 1);
877c478bd9Sstevel@tonic-gate }
88ff550d0eSmasputra 
89ff550d0eSmasputra /*
90ff550d0eSmasputra  * This function is used to free a message that has gone through
91ff550d0eSmasputra  * mi_copyin processing which modifies the M_IOCTL mblk's b_next
92ff550d0eSmasputra  * and b_prev pointers. We use this function to set b_next/b_prev
93ff550d0eSmasputra  * to NULL and free them.
94ff550d0eSmasputra  */
95ff550d0eSmasputra void
inet_freemsg(mblk_t * mp)96ff550d0eSmasputra inet_freemsg(mblk_t *mp)
97ff550d0eSmasputra {
98ff550d0eSmasputra 	mblk_t	*bp = mp;
99ff550d0eSmasputra 
100ff550d0eSmasputra 	for (; bp != NULL; bp = bp->b_cont) {
101ff550d0eSmasputra 		bp->b_prev = NULL;
102ff550d0eSmasputra 		bp->b_next = NULL;
103ff550d0eSmasputra 	}
104ff550d0eSmasputra 	freemsg(mp);
105ff550d0eSmasputra }
106