⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ieee80211_crypto.c

📁 Linux下wifi实现
💻 C
📖 第 1 页 / 共 2 页
字号:
	/*	 * Hardware TKIP with software MIC is an important	 * combination; we handle it by flagging each key,	 * the cipher modules honor it.	 */	if (cipher == IEEE80211_CIPHER_TKIP) {		if ((vap->iv_caps & IEEE80211_C_TKIPMIC) == 0) {			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,				"%s: no h/w support for TKIP MIC, falling back to s/w\n",				__func__);        			flags |= IEEE80211_KEY_SWMIC;        	} else if (((vap->iv_caps & IEEE80211_C_WME_TKIPMIC) == 0) &&		    (vap->iv_flags & IEEE80211_F_WME)) {			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,				"%s: no h/w support for TKIP MIC when WMM is turned on,"				" falling back to s/w\n",				__func__);			flags |= IEEE80211_KEY_SWMIC;            		}	}	/*	 * Bind cipher to key instance.  Note we do this	 * after checking the device capabilities so the	 * cipher module can optimize space usage based on	 * whether or not it needs to do the cipher work.	 */	if (key->wk_cipher != cip || key->wk_flags != flags) {again:		/*		 * Fill in the flags so cipher modules can see s/w		 * crypto requirements and potentially allocate		 * different state and/or attach different method		 * pointers.		 *		 * XXX this is not right when s/w crypto fallback		 *     fails and we try to restore previous state.		 */		key->wk_flags = flags;		keyctx = cip->ic_attach(vap, key);		if (keyctx == NULL) {			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,				"%s: unable to attach cipher %s\n",				__func__, cip->ic_name);			key->wk_flags = oflags;	/* restore old flags */			vap->iv_stats.is_crypto_attachfail++;			return 0;		}		cipher_detach(key);		key->wk_cipher = cip;		/* XXX refcnt? */		key->wk_private = keyctx;	}	/*	 * Commit to requested usage so driver can see the flags.	 */	key->wk_flags = flags;	/*	 * Ask the driver for a key index if we don't have one.	 * Note that entries in the global key table always have	 * an index; this means it's safe to call this routine	 * for these entries just to setup the reference to the	 * cipher template.  Note also that when using software	 * crypto we also call the driver to give us a key index.	 */	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {		key->wk_keyix = dev_key_alloc(vap, key);		if (key->wk_keyix == IEEE80211_KEYIX_NONE) {			/*			 * Driver has no room; fallback to doing crypto			 * in the host.  We change the flags and start the			 * procedure over.  If we get back here then there's			 * no hope and we bail.  Note that this can leave			 * the key in a inconsistent state if the caller			 * continues to use it.			 */			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {				vap->iv_stats.is_crypto_swfallback++;				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,					"%s: no h/w resources for cipher %s, "					"falling back to s/w\n",					__func__, cip->ic_name);				oflags = key->wk_flags;				flags |= IEEE80211_KEY_SWCRYPT;				if (cipher == IEEE80211_CIPHER_TKIP)					flags |= IEEE80211_KEY_SWMIC;				goto again;			}			vap->iv_stats.is_crypto_keyfail++;			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,				"%s: unable to setup cipher %s\n",				__func__, cip->ic_name);			return 0;		}	}	return 1;#undef N}EXPORT_SYMBOL(ieee80211_crypto_newkey);/* * Remove the key (no locking, for internal use). */static int_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key,	struct ieee80211_node *ni){	u_int16_t keyix;	KASSERT(key->wk_cipher != NULL, ("No cipher!"));	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,		"%s: %s keyix %u flags 0x%x tsc %llu len %u\n",		__func__, key->wk_cipher->ic_name,		key->wk_keyix, key->wk_flags,		key->wk_keytsc, key->wk_keylen);	keyix = key->wk_keyix;	if (keyix != IEEE80211_KEYIX_NONE) {		/*		 * Remove hardware entry.		 */		/* XXX key cache */		if (!dev_key_delete(vap, key, ni)) {			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,				"%s: driver did not delete key index %u\n",				__func__, keyix);			vap->iv_stats.is_crypto_delkey++;			/* XXX recovery? */		}	}	cipher_detach(key);	memset(key, 0, sizeof(*key));	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);	return 1;}/* * Remove the specified key. */intieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key, 	struct ieee80211_node *ni){	int status;#ifdef ATH_SUPERG_COMP	/* if valid node entry is present cleanup the compression state */	if (ni)		dev_comp_set(vap, ni, 0);#endif	ieee80211_key_update_begin(vap);	status = _ieee80211_crypto_delkey(vap, key, ni);	ieee80211_key_update_end(vap);	return status;}EXPORT_SYMBOL(ieee80211_crypto_delkey);/* * Clear the global key table. */voidieee80211_crypto_delglobalkeys(struct ieee80211vap *vap){	int i;	ieee80211_key_update_begin(vap);	for (i = 0; i < IEEE80211_WEP_NKID; i++)		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i], NULL);	ieee80211_key_update_end(vap);}EXPORT_SYMBOL(ieee80211_crypto_delglobalkeys);/* * Set the contents of the specified key. * * Locking must be handled by the caller using: *	ieee80211_key_update_begin(vap); *	ieee80211_key_update_end(vap); */intieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key,	const u_int8_t macaddr[IEEE80211_ADDR_LEN],	struct ieee80211_node *ni){	const struct ieee80211_cipher *cip = key->wk_cipher;	int ret;	KASSERT(cip != NULL, ("No cipher!"));	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,		"%s: %s keyix %u flags 0x%x mac %s  tsc %llu len %u\n",		__func__, cip->ic_name, key->wk_keyix,		key->wk_flags, ether_sprintf(macaddr),		key->wk_keytsc, key->wk_keylen);	/*	 * Give cipher a chance to validate key contents.	 * XXX should happen before modifying state.	 */	if (!cip->ic_setkey(key)) {		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,			"%s: cipher %s rejected key index %u len %u flags 0x%x\n",			__func__, cip->ic_name, key->wk_keyix,			key->wk_keylen, key->wk_flags);		vap->iv_stats.is_crypto_setkey_cipher++;		return 0;	}	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {		/* XXX nothing allocated, should not happen */		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,			"%s: no key index; should not happen!\n", __func__);		vap->iv_stats.is_crypto_setkey_nokey++;		return 0;	}	ret = dev_key_set(vap, key, macaddr);#ifdef ATH_SUPERG_COMP	if (ret && ni) {               /* Enable decompression only receive key entries */                if (key->wk_flags & IEEE80211_KEY_RECV)			dev_comp_set(vap, ni, 1);	}#endif	return ret;}EXPORT_SYMBOL(ieee80211_crypto_setkey);/* * Add privacy headers appropriate for the specified key. */struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *ni, struct sk_buff *skb){	struct ieee80211vap *vap = ni->ni_vap;	struct ieee80211_key *k;	struct ieee80211_frame *wh;	const struct ieee80211_cipher *cip;	u_int8_t keyid;	/*	 * Multicast traffic always uses the multicast key.	 * Otherwise if a unicast key is set we use that and	 * it is always key index 0.  When no unicast key is	 * set we fall back to the default transmit key.	 */	wh = (struct ieee80211_frame *)skb->data;	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,				wh->i_addr1,				"no default transmit key (%s) deftxkey %u",				__func__, vap->iv_def_txkey);			vap->iv_stats.is_tx_nodefkey++;			return NULL;		}		keyid = vap->iv_def_txkey;		k = &vap->iv_nw_keys[vap->iv_def_txkey];	} else {		keyid = 0;		k = &ni->ni_ucastkey;	}	cip = k->wk_cipher;	if (skb_headroom(skb) < cip->ic_header) {		/*		 * Should not happen; ieee80211_skbhdr_adjust should		 * have allocated enough space for all headers.		 */		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr1,			"%s: malformed packet for cipher %s; headroom %u",			__func__, cip->ic_name, skb_headroom(skb));		vap->iv_stats.is_tx_noheadroom++;		return NULL;	}	return (cip->ic_encap(k, skb, keyid << 6) ? k : NULL);}EXPORT_SYMBOL(ieee80211_crypto_encap);/* * Validate and strip privacy headers (and trailer) for a * received frame that has the WEP/Privacy bit set. */struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *ni, struct sk_buff *skb, int hdrlen){#define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)#define	IEEE80211_WEP_MINLEN \	(sizeof(struct ieee80211_frame) + \	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)	struct ieee80211vap *vap = ni->ni_vap;	struct ieee80211_key *k;	struct ieee80211_frame *wh;	const struct ieee80211_cipher *cip;	const u_int8_t *ivp;	u_int8_t keyid;	/* NB: this minimum size data frame could be bigger */	if (skb->len < IEEE80211_WEP_MINLEN) {		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,			"%s: WEP data frame too short, len %u",			__func__, skb->len);		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */		return NULL;	}	/*	 * Locate the key. If unicast and there is no unicast	 * key then we fall back to the key id in the header.	 * This assumes unicast keys are only configured when	 * the key id in the header is meaningless (typically 0).	 */	wh = (struct ieee80211_frame *) skb->data;	ivp = skb->data + hdrlen;	keyid = ivp[IEEE80211_WEP_IVLEN];	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)		k = &vap->iv_nw_keys[keyid >> 6];	else		k = &ni->ni_ucastkey;	cip = k->wk_cipher;	return (cip->ic_decap(k, skb, hdrlen) ? k : NULL);#undef IEEE80211_WEP_MINLEN#undef IEEE80211_WEP_HDRLEN}EXPORT_SYMBOL(ieee80211_crypto_decap);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -