FreeBSD/sys/netipsec/key.c
/*
 * allocating a SA for policy entry from SAD.
 * NOTE: searching SAD of aliving state.
 * OUT:	NULL:	not found.
 *	others:	found and return the pointer.
 */
allocating a SA for policy entry from SAD.
NOTE: searching SAD of aliving state.
OUT: NULL:   not found.
others: found and return the pointer.
allocating a SA for policy entry from SAD . NOTE : searching SAD of aliving state . OUT : NULL : not found . others : found and return the pointer .
static struct secasvar *
key_allocsa_policy(const struct secasindex *saidx)
{
#define	N(a)	_ARRAYLEN(a)
	struct secashead *sah;
	struct secasvar *sav;
	u_int stateidx, arraysize;
	const u_int *state_valid;
	state_valid = NULL;	
	arraysize = 0;		
	SAHTREE_LOCK();
	LIST_FOREACH(sah, &V_sahtree, chain) {
		if (sah->state == SADB_SASTATE_DEAD)
			continue;
		if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
			if (V_key_preferred_oldsa) {
				state_valid = saorder_state_valid_prefer_old;
				arraysize = N(saorder_state_valid_prefer_old);
			} else {
				state_valid = saorder_state_valid_prefer_new;
				arraysize = N(saorder_state_valid_prefer_new);
			}
			break;
		}
	}
	SAHTREE_UNLOCK();
	if (sah == NULL)
		return NULL;
	for (stateidx = 0; stateidx < arraysize; stateidx++) {
		sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
		if (sav != NULL)
			return sav;
	}
	return NULL;
#undef N
}
static struct secasvar * key_allocsa_policy ( const struct secasindex * saidx ) { #define N ( a ) 	_ARRAYLEN(a) struct secashead * sah ; struct secasvar * sav ; u_int stateidx , arraysize ; const u_int * state_valid ; state_valid = NULL ; arraysize = 0 ; SAHTREE_LOCK ( ) ; LIST_FOREACH ( sah , & V_sahtree , chain ) { if ( sah -> state == SADB_SASTATE_DEAD ) continue ; if ( key_cmpsaidx ( & sah -> saidx , saidx , CMP_MODE_REQID ) ) { if ( V_key_preferred_oldsa ) { state_valid = saorder_state_valid_prefer_old ; arraysize = N ( saorder_state_valid_prefer_old ) ; } else { state_valid = saorder_state_valid_prefer_new ; arraysize = N ( saorder_state_valid_prefer_new ) ; } break ; } } SAHTREE_UNLOCK ( ) ; if ( sah == NULL ) return NULL ; for ( stateidx = 0 ; stateidx < arraysize ; stateidx ++ ) { sav = key_do_allocsa_policy ( sah , state_valid [ stateidx ] ) ; if ( sav != NULL ) return sav ; } return NULL ; #undef  N }
allocating a SA for policy entry from SAD.
allocating a SA for policy entry from SAD .
/* silence gcc */ /* silence gcc */ /* search valid state */