📄 wpa.c
字号:
/* * Copyright 2002-2004, Instant802 Networks, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/netdevice.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/skbuff.h>#include <linux/compiler.h>#include <net/mac80211.h>#include "ieee80211_i.h"#include "michael.h"#include "tkip.h"#include "aes_ccm.h"#include "wpa.h"static int ieee80211_get_hdr_info(const struct sk_buff *skb, u8 **sa, u8 **da, u8 *qos_tid, u8 **data, size_t *data_len){ struct ieee80211_hdr *hdr; size_t hdrlen; u16 fc; int a4_included; u8 *pos; hdr = (struct ieee80211_hdr *) skb->data; fc = le16_to_cpu(hdr->frame_control); hdrlen = 24; if ((fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) == (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) { hdrlen += ETH_ALEN; *sa = hdr->addr4; *da = hdr->addr3; } else if (fc & IEEE80211_FCTL_FROMDS) { *sa = hdr->addr3; *da = hdr->addr1; } else if (fc & IEEE80211_FCTL_TODS) { *sa = hdr->addr2; *da = hdr->addr3; } else { *sa = hdr->addr2; *da = hdr->addr1; } if (fc & 0x80) hdrlen += 2; *data = skb->data + hdrlen; *data_len = skb->len - hdrlen; a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS); if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA && fc & IEEE80211_STYPE_QOS_DATA) { pos = (u8 *) &hdr->addr4; if (a4_included) pos += 6; *qos_tid = pos[0] & 0x0f; *qos_tid |= 0x80; /* qos_included flag */ } else *qos_tid = 0; return skb->len < hdrlen ? -1 : 0;}ieee80211_txrx_resultieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data *tx){ u8 *data, *sa, *da, *key, *mic, qos_tid; size_t data_len; u16 fc; struct sk_buff *skb = tx->skb; int authenticator; int wpa_test = 0; fc = tx->fc; if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 || !WLAN_FC_DATA_PRESENT(fc)) return TXRX_CONTINUE; if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)) return TXRX_DROP; if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) && !(tx->flags & IEEE80211_TXRXD_FRAGMENTED) && !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) && !wpa_test) { /* hwaccel - with no need for preallocated room for Michael MIC */ return TXRX_CONTINUE; } if (skb_tailroom(skb) < MICHAEL_MIC_LEN) { I802_DEBUG_INC(tx->local->tx_expand_skb_head); if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, MICHAEL_MIC_LEN + TKIP_ICV_LEN, GFP_ATOMIC))) { printk(KERN_DEBUG "%s: failed to allocate more memory " "for Michael MIC\n", tx->dev->name); return TXRX_DROP; } }#if 0 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */#else authenticator = 1;#endif key = &tx->key->conf.key[authenticator ? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY : ALG_TKIP_TEMP_AUTH_RX_MIC_KEY]; mic = skb_put(skb, MICHAEL_MIC_LEN); michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic); return TXRX_CONTINUE;}ieee80211_txrx_resultieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data *rx){ u8 *data, *sa, *da, *key = NULL, qos_tid; size_t data_len; u16 fc; u8 mic[MICHAEL_MIC_LEN]; struct sk_buff *skb = rx->skb; int authenticator = 1, wpa_test = 0; DECLARE_MAC_BUF(mac); fc = rx->fc; /* * No way to verify the MIC if the hardware stripped it */ if (rx->u.rx.status->flag & RX_FLAG_MMIC_STRIPPED) return TXRX_CONTINUE; if (!rx->key || rx->key->conf.alg != ALG_TKIP || !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc)) return TXRX_CONTINUE; if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len) || data_len < MICHAEL_MIC_LEN) return TXRX_DROP; data_len -= MICHAEL_MIC_LEN;#if 0 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */#else authenticator = 1;#endif key = &rx->key->conf.key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY : ALG_TKIP_TEMP_AUTH_TX_MIC_KEY]; michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic); if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) { if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) return TXRX_DROP; printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from " "%s\n", rx->dev->name, print_mac(mac, sa)); mac80211_ev_michael_mic_failure(rx->dev, rx->key->conf.keyidx, (void *) skb->data); return TXRX_DROP; } /* remove Michael MIC from payload */ skb_trim(skb, skb->len - MICHAEL_MIC_LEN); /* update IV in key information to be able to detect replays */ rx->key->u.tkip.iv32_rx[rx->u.rx.queue] = rx->u.rx.tkip_iv32; rx->key->u.tkip.iv16_rx[rx->u.rx.queue] = rx->u.rx.tkip_iv16; return TXRX_CONTINUE;}static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb, int test){ struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; struct ieee80211_key *key = tx->key; int hdrlen, len, tailneed; u16 fc; u8 *pos; fc = le16_to_cpu(hdr->frame_control); hdrlen = ieee80211_get_hdrlen(fc); len = skb->len - hdrlen; if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) tailneed = 0; else tailneed = TKIP_ICV_LEN; if ((skb_headroom(skb) < TKIP_IV_LEN || skb_tailroom(skb) < tailneed)) { I802_DEBUG_INC(tx->local->tx_expand_skb_head); if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed, GFP_ATOMIC))) return -1; } pos = skb_push(skb, TKIP_IV_LEN); memmove(pos, pos + TKIP_IV_LEN, hdrlen); pos += hdrlen; /* Increase IV for the frame */ key->u.tkip.iv16++; if (key->u.tkip.iv16 == 0) key->u.tkip.iv32++; if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { hdr = (struct ieee80211_hdr *)skb->data; /* hwaccel - with preallocated room for IV */ ieee80211_tkip_add_iv(pos, key, (u8) (key->u.tkip.iv16 >> 8), (u8) (((key->u.tkip.iv16 >> 8) | 0x20) & 0x7f), (u8) key->u.tkip.iv16); tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx; return 0; } /* Add room for ICV */ skb_put(skb, TKIP_ICV_LEN); hdr = (struct ieee80211_hdr *) skb->data; ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm, key, pos, len, hdr->addr2); return 0;}ieee80211_txrx_resultieee80211_crypto_tkip_encrypt(struct ieee80211_txrx_data *tx){ struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data; u16 fc; struct sk_buff *skb = tx->skb; int wpa_test = 0, test = 0; fc = le16_to_cpu(hdr->frame_control); if (!WLAN_FC_DATA_PRESENT(fc)) return TXRX_CONTINUE; tx->u.tx.control->icv_len = TKIP_ICV_LEN; tx->u.tx.control->iv_len = TKIP_IV_LEN; ieee80211_tx_set_iswep(tx); if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) && !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) && !wpa_test) { /* hwaccel - with no need for preallocated room for IV/ICV */ tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx; return TXRX_CONTINUE; } if (tkip_encrypt_skb(tx, skb, test) < 0) return TXRX_DROP; if (tx->u.tx.extra_frag) { int i; for (i = 0; i < tx->u.tx.num_extra_frag; i++) { if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test) < 0) return TXRX_DROP; } } return TXRX_CONTINUE;}ieee80211_txrx_resultieee80211_crypto_tkip_decrypt(struct ieee80211_txrx_data *rx){ struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data; u16 fc; int hdrlen, res, hwaccel = 0, wpa_test = 0; struct ieee80211_key *key = rx->key; struct sk_buff *skb = rx->skb; DECLARE_MAC_BUF(mac); fc = le16_to_cpu(hdr->frame_control); hdrlen = ieee80211_get_hdrlen(fc); if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) return TXRX_CONTINUE; if (!rx->sta || skb->len - hdrlen < 12) return TXRX_DROP; if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED) { if (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) { /* * Hardware took care of all processing, including * replay protection, and stripped the ICV/IV so * we cannot do any checks here.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -