sys/net/if_clone.c
int
if_clone_create(char *name, int len, caddr_t params)
{
	struct if_clone *ifc;
	char *dp;
	int wildcard, bytoff, bitoff;
	int unit;
	int err;

	ifc = if_clone_lookup(name, &unit);
	if (ifc == NULL)
		return (EINVAL);

	if (ifunit(name) != NULL)
		return (EEXIST);

	bytoff = bitoff = 0;
	wildcard = (unit < 0);
	/*
	 * Find a free unit if none was given.
	 */
	if (wildcard) {
		while (bytoff < ifc->ifc_bmlen &&
		    ifc->ifc_units[bytoff] == 0xff)
			bytoff++;
		if (bytoff >= ifc->ifc_bmlen)
			return (ENOSPC);
		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
			bitoff++;
		unit = (bytoff << 3) + bitoff;
	}

	if (unit > ifc->ifc_maxunit)
		return (ENXIO);

	err = (*ifc->ifc_create)(ifc, unit, params);
	if (err != 0)
		return (err);

	if (!wildcard) {
		bytoff = unit >> 3;
		bitoff = unit - (bytoff << 3);
	}

	/*
	 * Allocate the unit in the bitmap.
	 */
	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
	    ("%s: bit is already set", __func__));
	ifc->ifc_units[bytoff] |= (1 << bitoff);

	/* In the wildcard case, we need to update the name. */
	if (wildcard) {
		for (dp = name; *dp != '\0'; dp++);
		if (ksnprintf(dp, len - (dp-name), "%d", unit) >
		    len - (dp-name) - 1) {
			/*
			 * This can only be a programmer error and
			 * there's no straightforward way to recover if
			 * it happens.
			 */
			panic("if_clone_create(): interface name too long");
		}

	}

	EVENTHANDLER_INVOKE(if_clone_event, ifc);

	return (0);
}
/*
 * Create a clone network interface.
 */
Create a clone network interface.
Create a clone network interface .
int
if_clone_create(char *name, int len, caddr_t params)
{
	struct if_clone *ifc;
	char *dp;
	int wildcard, bytoff, bitoff;
	int unit;
	int err;
	ifc = if_clone_lookup(name, &unit);
	if (ifc == NULL)
		return (EINVAL);
	if (ifunit(name) != NULL)
		return (EEXIST);
	bytoff = bitoff = 0;
	wildcard = (unit < 0);
	if (wildcard) {
		while (bytoff < ifc->ifc_bmlen &&
		    ifc->ifc_units[bytoff] == 0xff)
			bytoff++;
		if (bytoff >= ifc->ifc_bmlen)
			return (ENOSPC);
		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
			bitoff++;
		unit = (bytoff << 3) + bitoff;
	}
	if (unit > ifc->ifc_maxunit)
		return (ENXIO);
	err = (*ifc->ifc_create)(ifc, unit, params);
	if (err != 0)
		return (err);
	if (!wildcard) {
		bytoff = unit >> 3;
		bitoff = unit - (bytoff << 3);
	}
	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
	    ("%s: bit is already set", __func__));
	ifc->ifc_units[bytoff] |= (1 << bitoff);
	if (wildcard) {
		for (dp = name; *dp != '\0'; dp++);
		if (ksnprintf(dp, len - (dp-name), "%d", unit) >
		    len - (dp-name) - 1) {
			panic("if_clone_create(): interface name too long");
		}
	}
	EVENTHANDLER_INVOKE(if_clone_event, ifc);
	return (0);
}
int if_clone_create ( char * name , int len , caddr_t params ) { struct if_clone * ifc ; char * dp ; int wildcard , bytoff , bitoff ; int unit ; int err ; ifc = if_clone_lookup ( name , & unit ) ; if ( ifc == NULL ) return ( EINVAL ) ; if ( ifunit ( name ) != NULL ) return ( EEXIST ) ; bytoff = bitoff = 0 ; wildcard = ( unit < 0 ) ; if ( wildcard ) { while ( bytoff < ifc -> ifc_bmlen && ifc -> ifc_units [ bytoff ] == 0xff ) bytoff ++ ; if ( bytoff >= ifc -> ifc_bmlen ) return ( ENOSPC ) ; while ( ( ifc -> ifc_units [ bytoff ] & ( 1 << bitoff ) ) != 0 ) bitoff ++ ; unit = ( bytoff << 3 ) + bitoff ; } if ( unit > ifc -> ifc_maxunit ) return ( ENXIO ) ; err = ( * ifc -> ifc_create ) ( ifc , unit , params ) ; if ( err != 0 ) return ( err ) ; if ( ! wildcard ) { bytoff = unit >> 3 ; bitoff = unit - ( bytoff << 3 ) ; } KASSERT ( ( ifc -> ifc_units [ bytoff ] & ( 1 << bitoff ) ) == 0 , ( " " , __func__ ) ) ; ifc -> ifc_units [ bytoff ] |= ( 1 << bitoff ) ; if ( wildcard ) { for ( dp = name ; * dp != ' \0 ' ; dp ++ ) ; if ( ksnprintf ( dp , len - ( dp - name ) , " " , unit ) > len - ( dp - name ) - 1 ) { panic ( " " ) ; } } EVENTHANDLER_INVOKE ( if_clone_event , ifc ) ; return ( 0 ) ; }
Create a clone network interface.
Create a clone network interface .
/*
	 * Find a free unit if none was given.
	 */ /*
	 * Allocate the unit in the bitmap.
	 */ /* In the wildcard case, we need to update the name. */ /*
			 * This can only be a programmer error and
			 * there's no straightforward way to recover if
			 * it happens.
			 */