hostap_80211_rx.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #include <linux/etherdevice.h>
  2. #include "hostap_80211.h"
  3. #include "hostap.h"
  4. void hostap_dump_rx_80211(const char *name, struct sk_buff *skb,
  5. struct hostap_80211_rx_status *rx_stats)
  6. {
  7. struct ieee80211_hdr *hdr;
  8. u16 fc;
  9. hdr = (struct ieee80211_hdr *) skb->data;
  10. printk(KERN_DEBUG "%s: RX signal=%d noise=%d rate=%d len=%d "
  11. "jiffies=%ld\n",
  12. name, rx_stats->signal, rx_stats->noise, rx_stats->rate,
  13. skb->len, jiffies);
  14. if (skb->len < 2)
  15. return;
  16. fc = le16_to_cpu(hdr->frame_ctl);
  17. printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
  18. fc, HOSTAP_FC_GET_TYPE(fc), HOSTAP_FC_GET_STYPE(fc),
  19. fc & WLAN_FC_TODS ? " [ToDS]" : "",
  20. fc & WLAN_FC_FROMDS ? " [FromDS]" : "");
  21. if (skb->len < IEEE80211_DATA_HDR3_LEN) {
  22. printk("\n");
  23. return;
  24. }
  25. printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
  26. le16_to_cpu(hdr->seq_ctl));
  27. printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
  28. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
  29. if (skb->len >= 30)
  30. printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
  31. printk("\n");
  32. }
  33. /* Send RX frame to netif with 802.11 (and possible prism) header.
  34. * Called from hardware or software IRQ context. */
  35. int prism2_rx_80211(struct net_device *dev, struct sk_buff *skb,
  36. struct hostap_80211_rx_status *rx_stats, int type)
  37. {
  38. struct hostap_interface *iface;
  39. local_info_t *local;
  40. int hdrlen, phdrlen, head_need, tail_need;
  41. u16 fc;
  42. int prism_header, ret;
  43. struct ieee80211_hdr *hdr;
  44. iface = netdev_priv(dev);
  45. local = iface->local;
  46. dev->last_rx = jiffies;
  47. if (dev->type == ARPHRD_IEEE80211_PRISM) {
  48. if (local->monitor_type == PRISM2_MONITOR_PRISM) {
  49. prism_header = 1;
  50. phdrlen = sizeof(struct linux_wlan_ng_prism_hdr);
  51. } else { /* local->monitor_type == PRISM2_MONITOR_CAPHDR */
  52. prism_header = 2;
  53. phdrlen = sizeof(struct linux_wlan_ng_cap_hdr);
  54. }
  55. } else {
  56. prism_header = 0;
  57. phdrlen = 0;
  58. }
  59. hdr = (struct ieee80211_hdr *) skb->data;
  60. fc = le16_to_cpu(hdr->frame_ctl);
  61. if (type == PRISM2_RX_MGMT && (fc & WLAN_FC_PVER)) {
  62. printk(KERN_DEBUG "%s: dropped management frame with header "
  63. "version %d\n", dev->name, fc & WLAN_FC_PVER);
  64. dev_kfree_skb_any(skb);
  65. return 0;
  66. }
  67. hdrlen = hostap_80211_get_hdrlen(fc);
  68. /* check if there is enough room for extra data; if not, expand skb
  69. * buffer to be large enough for the changes */
  70. head_need = phdrlen;
  71. tail_need = 0;
  72. #ifdef PRISM2_ADD_BOGUS_CRC
  73. tail_need += 4;
  74. #endif /* PRISM2_ADD_BOGUS_CRC */
  75. head_need -= skb_headroom(skb);
  76. tail_need -= skb_tailroom(skb);
  77. if (head_need > 0 || tail_need > 0) {
  78. if (pskb_expand_head(skb, head_need > 0 ? head_need : 0,
  79. tail_need > 0 ? tail_need : 0,
  80. GFP_ATOMIC)) {
  81. printk(KERN_DEBUG "%s: prism2_rx_80211 failed to "
  82. "reallocate skb buffer\n", dev->name);
  83. dev_kfree_skb_any(skb);
  84. return 0;
  85. }
  86. }
  87. /* We now have an skb with enough head and tail room, so just insert
  88. * the extra data */
  89. #ifdef PRISM2_ADD_BOGUS_CRC
  90. memset(skb_put(skb, 4), 0xff, 4); /* Prism2 strips CRC */
  91. #endif /* PRISM2_ADD_BOGUS_CRC */
  92. if (prism_header == 1) {
  93. struct linux_wlan_ng_prism_hdr *hdr;
  94. hdr = (struct linux_wlan_ng_prism_hdr *)
  95. skb_push(skb, phdrlen);
  96. memset(hdr, 0, phdrlen);
  97. hdr->msgcode = LWNG_CAP_DID_BASE;
  98. hdr->msglen = sizeof(*hdr);
  99. memcpy(hdr->devname, dev->name, sizeof(hdr->devname));
  100. #define LWNG_SETVAL(f,i,s,l,d) \
  101. hdr->f.did = LWNG_CAP_DID_BASE | (i << 12); \
  102. hdr->f.status = s; hdr->f.len = l; hdr->f.data = d
  103. LWNG_SETVAL(hosttime, 1, 0, 4, jiffies);
  104. LWNG_SETVAL(mactime, 2, 0, 4, rx_stats->mac_time);
  105. LWNG_SETVAL(channel, 3, 1 /* no value */, 4, 0);
  106. LWNG_SETVAL(rssi, 4, 1 /* no value */, 4, 0);
  107. LWNG_SETVAL(sq, 5, 1 /* no value */, 4, 0);
  108. LWNG_SETVAL(signal, 6, 0, 4, rx_stats->signal);
  109. LWNG_SETVAL(noise, 7, 0, 4, rx_stats->noise);
  110. LWNG_SETVAL(rate, 8, 0, 4, rx_stats->rate / 5);
  111. LWNG_SETVAL(istx, 9, 0, 4, 0);
  112. LWNG_SETVAL(frmlen, 10, 0, 4, skb->len - phdrlen);
  113. #undef LWNG_SETVAL
  114. } else if (prism_header == 2) {
  115. struct linux_wlan_ng_cap_hdr *hdr;
  116. hdr = (struct linux_wlan_ng_cap_hdr *)
  117. skb_push(skb, phdrlen);
  118. memset(hdr, 0, phdrlen);
  119. hdr->version = htonl(LWNG_CAPHDR_VERSION);
  120. hdr->length = htonl(phdrlen);
  121. hdr->mactime = __cpu_to_be64(rx_stats->mac_time);
  122. hdr->hosttime = __cpu_to_be64(jiffies);
  123. hdr->phytype = htonl(4); /* dss_dot11_b */
  124. hdr->channel = htonl(local->channel);
  125. hdr->datarate = htonl(rx_stats->rate);
  126. hdr->antenna = htonl(0); /* unknown */
  127. hdr->priority = htonl(0); /* unknown */
  128. hdr->ssi_type = htonl(3); /* raw */
  129. hdr->ssi_signal = htonl(rx_stats->signal);
  130. hdr->ssi_noise = htonl(rx_stats->noise);
  131. hdr->preamble = htonl(0); /* unknown */
  132. hdr->encoding = htonl(1); /* cck */
  133. }
  134. ret = skb->len - phdrlen;
  135. skb->dev = dev;
  136. skb->mac.raw = skb->data;
  137. skb_pull(skb, hdrlen);
  138. if (prism_header)
  139. skb_pull(skb, phdrlen);
  140. skb->pkt_type = PACKET_OTHERHOST;
  141. skb->protocol = __constant_htons(ETH_P_802_2);
  142. memset(skb->cb, 0, sizeof(skb->cb));
  143. netif_rx(skb);
  144. return ret;
  145. }
  146. /* Called only as a tasklet (software IRQ) */
  147. static void monitor_rx(struct net_device *dev, struct sk_buff *skb,
  148. struct hostap_80211_rx_status *rx_stats)
  149. {
  150. struct net_device_stats *stats;
  151. int len;
  152. len = prism2_rx_80211(dev, skb, rx_stats, PRISM2_RX_MONITOR);
  153. stats = hostap_get_stats(dev);
  154. stats->rx_packets++;
  155. stats->rx_bytes += len;
  156. }
  157. /* Called only as a tasklet (software IRQ) */
  158. static struct prism2_frag_entry *
  159. prism2_frag_cache_find(local_info_t *local, unsigned int seq,
  160. unsigned int frag, u8 *src, u8 *dst)
  161. {
  162. struct prism2_frag_entry *entry;
  163. int i;
  164. for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
  165. entry = &local->frag_cache[i];
  166. if (entry->skb != NULL &&
  167. time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  168. printk(KERN_DEBUG "%s: expiring fragment cache entry "
  169. "seq=%u last_frag=%u\n",
  170. local->dev->name, entry->seq, entry->last_frag);
  171. dev_kfree_skb(entry->skb);
  172. entry->skb = NULL;
  173. }
  174. if (entry->skb != NULL && entry->seq == seq &&
  175. (entry->last_frag + 1 == frag || frag == -1) &&
  176. memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
  177. memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
  178. return entry;
  179. }
  180. return NULL;
  181. }
  182. /* Called only as a tasklet (software IRQ) */
  183. static struct sk_buff *
  184. prism2_frag_cache_get(local_info_t *local, struct ieee80211_hdr *hdr)
  185. {
  186. struct sk_buff *skb = NULL;
  187. u16 sc;
  188. unsigned int frag, seq;
  189. struct prism2_frag_entry *entry;
  190. sc = le16_to_cpu(hdr->seq_ctl);
  191. frag = WLAN_GET_SEQ_FRAG(sc);
  192. seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
  193. if (frag == 0) {
  194. /* Reserve enough space to fit maximum frame length */
  195. skb = dev_alloc_skb(local->dev->mtu +
  196. sizeof(struct ieee80211_hdr) +
  197. 8 /* LLC */ +
  198. 2 /* alignment */ +
  199. 8 /* WEP */ + ETH_ALEN /* WDS */);
  200. if (skb == NULL)
  201. return NULL;
  202. entry = &local->frag_cache[local->frag_next_idx];
  203. local->frag_next_idx++;
  204. if (local->frag_next_idx >= PRISM2_FRAG_CACHE_LEN)
  205. local->frag_next_idx = 0;
  206. if (entry->skb != NULL)
  207. dev_kfree_skb(entry->skb);
  208. entry->first_frag_time = jiffies;
  209. entry->seq = seq;
  210. entry->last_frag = frag;
  211. entry->skb = skb;
  212. memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
  213. memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
  214. } else {
  215. /* received a fragment of a frame for which the head fragment
  216. * should have already been received */
  217. entry = prism2_frag_cache_find(local, seq, frag, hdr->addr2,
  218. hdr->addr1);
  219. if (entry != NULL) {
  220. entry->last_frag = frag;
  221. skb = entry->skb;
  222. }
  223. }
  224. return skb;
  225. }
  226. /* Called only as a tasklet (software IRQ) */
  227. static int prism2_frag_cache_invalidate(local_info_t *local,
  228. struct ieee80211_hdr *hdr)
  229. {
  230. u16 sc;
  231. unsigned int seq;
  232. struct prism2_frag_entry *entry;
  233. sc = le16_to_cpu(hdr->seq_ctl);
  234. seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
  235. entry = prism2_frag_cache_find(local, seq, -1, hdr->addr2, hdr->addr1);
  236. if (entry == NULL) {
  237. printk(KERN_DEBUG "%s: could not invalidate fragment cache "
  238. "entry (seq=%u)\n",
  239. local->dev->name, seq);
  240. return -1;
  241. }
  242. entry->skb = NULL;
  243. return 0;
  244. }
  245. static struct hostap_bss_info *__hostap_get_bss(local_info_t *local, u8 *bssid,
  246. u8 *ssid, size_t ssid_len)
  247. {
  248. struct list_head *ptr;
  249. struct hostap_bss_info *bss;
  250. list_for_each(ptr, &local->bss_list) {
  251. bss = list_entry(ptr, struct hostap_bss_info, list);
  252. if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  253. (ssid == NULL ||
  254. (ssid_len == bss->ssid_len &&
  255. memcmp(ssid, bss->ssid, ssid_len) == 0))) {
  256. list_move(&bss->list, &local->bss_list);
  257. return bss;
  258. }
  259. }
  260. return NULL;
  261. }
  262. static struct hostap_bss_info *__hostap_add_bss(local_info_t *local, u8 *bssid,
  263. u8 *ssid, size_t ssid_len)
  264. {
  265. struct hostap_bss_info *bss;
  266. if (local->num_bss_info >= HOSTAP_MAX_BSS_COUNT) {
  267. bss = list_entry(local->bss_list.prev,
  268. struct hostap_bss_info, list);
  269. list_del(&bss->list);
  270. local->num_bss_info--;
  271. } else {
  272. bss = (struct hostap_bss_info *)
  273. kmalloc(sizeof(*bss), GFP_ATOMIC);
  274. if (bss == NULL)
  275. return NULL;
  276. }
  277. memset(bss, 0, sizeof(*bss));
  278. memcpy(bss->bssid, bssid, ETH_ALEN);
  279. memcpy(bss->ssid, ssid, ssid_len);
  280. bss->ssid_len = ssid_len;
  281. local->num_bss_info++;
  282. list_add(&bss->list, &local->bss_list);
  283. return bss;
  284. }
  285. static void __hostap_expire_bss(local_info_t *local)
  286. {
  287. struct hostap_bss_info *bss;
  288. while (local->num_bss_info > 0) {
  289. bss = list_entry(local->bss_list.prev,
  290. struct hostap_bss_info, list);
  291. if (!time_after(jiffies, bss->last_update + 60 * HZ))
  292. break;
  293. list_del(&bss->list);
  294. local->num_bss_info--;
  295. kfree(bss);
  296. }
  297. }
  298. /* Both IEEE 802.11 Beacon and Probe Response frames have similar structure, so
  299. * the same routine can be used to parse both of them. */
  300. static void hostap_rx_sta_beacon(local_info_t *local, struct sk_buff *skb,
  301. int stype)
  302. {
  303. struct hostap_ieee80211_mgmt *mgmt;
  304. int left, chan = 0;
  305. u8 *pos;
  306. u8 *ssid = NULL, *wpa = NULL, *rsn = NULL;
  307. size_t ssid_len = 0, wpa_len = 0, rsn_len = 0;
  308. struct hostap_bss_info *bss;
  309. if (skb->len < IEEE80211_MGMT_HDR_LEN + sizeof(mgmt->u.beacon))
  310. return;
  311. mgmt = (struct hostap_ieee80211_mgmt *) skb->data;
  312. pos = mgmt->u.beacon.variable;
  313. left = skb->len - (pos - skb->data);
  314. while (left >= 2) {
  315. if (2 + pos[1] > left)
  316. return; /* parse failed */
  317. switch (*pos) {
  318. case WLAN_EID_SSID:
  319. ssid = pos + 2;
  320. ssid_len = pos[1];
  321. break;
  322. case WLAN_EID_GENERIC:
  323. if (pos[1] >= 4 &&
  324. pos[2] == 0x00 && pos[3] == 0x50 &&
  325. pos[4] == 0xf2 && pos[5] == 1) {
  326. wpa = pos;
  327. wpa_len = pos[1] + 2;
  328. }
  329. break;
  330. case WLAN_EID_RSN:
  331. rsn = pos;
  332. rsn_len = pos[1] + 2;
  333. break;
  334. case WLAN_EID_DS_PARAMS:
  335. if (pos[1] >= 1)
  336. chan = pos[2];
  337. break;
  338. }
  339. left -= 2 + pos[1];
  340. pos += 2 + pos[1];
  341. }
  342. if (wpa_len > MAX_WPA_IE_LEN)
  343. wpa_len = MAX_WPA_IE_LEN;
  344. if (rsn_len > MAX_WPA_IE_LEN)
  345. rsn_len = MAX_WPA_IE_LEN;
  346. if (ssid_len > sizeof(bss->ssid))
  347. ssid_len = sizeof(bss->ssid);
  348. spin_lock(&local->lock);
  349. bss = __hostap_get_bss(local, mgmt->bssid, ssid, ssid_len);
  350. if (bss == NULL)
  351. bss = __hostap_add_bss(local, mgmt->bssid, ssid, ssid_len);
  352. if (bss) {
  353. bss->last_update = jiffies;
  354. bss->count++;
  355. bss->capab_info = le16_to_cpu(mgmt->u.beacon.capab_info);
  356. if (wpa) {
  357. memcpy(bss->wpa_ie, wpa, wpa_len);
  358. bss->wpa_ie_len = wpa_len;
  359. } else
  360. bss->wpa_ie_len = 0;
  361. if (rsn) {
  362. memcpy(bss->rsn_ie, rsn, rsn_len);
  363. bss->rsn_ie_len = rsn_len;
  364. } else
  365. bss->rsn_ie_len = 0;
  366. bss->chan = chan;
  367. }
  368. __hostap_expire_bss(local);
  369. spin_unlock(&local->lock);
  370. }
  371. static inline int
  372. hostap_rx_frame_mgmt(local_info_t *local, struct sk_buff *skb,
  373. struct hostap_80211_rx_status *rx_stats, u16 type,
  374. u16 stype)
  375. {
  376. if (local->iw_mode == IW_MODE_MASTER) {
  377. hostap_update_sta_ps(local, (struct ieee80211_hdr *)
  378. skb->data);
  379. }
  380. if (local->hostapd && type == WLAN_FC_TYPE_MGMT) {
  381. if (stype == WLAN_FC_STYPE_BEACON &&
  382. local->iw_mode == IW_MODE_MASTER) {
  383. struct sk_buff *skb2;
  384. /* Process beacon frames also in kernel driver to
  385. * update STA(AP) table statistics */
  386. skb2 = skb_clone(skb, GFP_ATOMIC);
  387. if (skb2)
  388. hostap_rx(skb2->dev, skb2, rx_stats);
  389. }
  390. /* send management frames to the user space daemon for
  391. * processing */
  392. local->apdevstats.rx_packets++;
  393. local->apdevstats.rx_bytes += skb->len;
  394. if (local->apdev == NULL)
  395. return -1;
  396. prism2_rx_80211(local->apdev, skb, rx_stats, PRISM2_RX_MGMT);
  397. return 0;
  398. }
  399. if (local->iw_mode == IW_MODE_MASTER) {
  400. if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
  401. printk(KERN_DEBUG "%s: unknown management frame "
  402. "(type=0x%02x, stype=0x%02x) dropped\n",
  403. skb->dev->name, type, stype);
  404. return -1;
  405. }
  406. hostap_rx(skb->dev, skb, rx_stats);
  407. return 0;
  408. } else if (type == WLAN_FC_TYPE_MGMT &&
  409. (stype == WLAN_FC_STYPE_BEACON ||
  410. stype == WLAN_FC_STYPE_PROBE_RESP)) {
  411. hostap_rx_sta_beacon(local, skb, stype);
  412. return -1;
  413. } else if (type == WLAN_FC_TYPE_MGMT &&
  414. (stype == WLAN_FC_STYPE_ASSOC_RESP ||
  415. stype == WLAN_FC_STYPE_REASSOC_RESP)) {
  416. /* Ignore (Re)AssocResp silently since these are not currently
  417. * needed but are still received when WPA/RSN mode is enabled.
  418. */
  419. return -1;
  420. } else {
  421. printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: dropped unhandled"
  422. " management frame in non-Host AP mode (type=%d:%d)\n",
  423. skb->dev->name, type, stype);
  424. return -1;
  425. }
  426. }
  427. /* Called only as a tasklet (software IRQ) */
  428. static inline struct net_device *prism2_rx_get_wds(local_info_t *local,
  429. u8 *addr)
  430. {
  431. struct hostap_interface *iface = NULL;
  432. struct list_head *ptr;
  433. read_lock_bh(&local->iface_lock);
  434. list_for_each(ptr, &local->hostap_interfaces) {
  435. iface = list_entry(ptr, struct hostap_interface, list);
  436. if (iface->type == HOSTAP_INTERFACE_WDS &&
  437. memcmp(iface->u.wds.remote_addr, addr, ETH_ALEN) == 0)
  438. break;
  439. iface = NULL;
  440. }
  441. read_unlock_bh(&local->iface_lock);
  442. return iface ? iface->dev : NULL;
  443. }
  444. static inline int
  445. hostap_rx_frame_wds(local_info_t *local, struct ieee80211_hdr *hdr,
  446. u16 fc, struct net_device **wds)
  447. {
  448. /* FIX: is this really supposed to accept WDS frames only in Master
  449. * mode? What about Repeater or Managed with WDS frames? */
  450. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) !=
  451. (WLAN_FC_TODS | WLAN_FC_FROMDS) &&
  452. (local->iw_mode != IW_MODE_MASTER || !(fc & WLAN_FC_TODS)))
  453. return 0; /* not a WDS frame */
  454. /* Possible WDS frame: either IEEE 802.11 compliant (if FromDS)
  455. * or own non-standard frame with 4th address after payload */
  456. if (memcmp(hdr->addr1, local->dev->dev_addr, ETH_ALEN) != 0 &&
  457. (hdr->addr1[0] != 0xff || hdr->addr1[1] != 0xff ||
  458. hdr->addr1[2] != 0xff || hdr->addr1[3] != 0xff ||
  459. hdr->addr1[4] != 0xff || hdr->addr1[5] != 0xff)) {
  460. /* RA (or BSSID) is not ours - drop */
  461. PDEBUG(DEBUG_EXTRA, "%s: received WDS frame with "
  462. "not own or broadcast %s=" MACSTR "\n",
  463. local->dev->name, fc & WLAN_FC_FROMDS ? "RA" : "BSSID",
  464. MAC2STR(hdr->addr1));
  465. return -1;
  466. }
  467. /* check if the frame came from a registered WDS connection */
  468. *wds = prism2_rx_get_wds(local, hdr->addr2);
  469. if (*wds == NULL && fc & WLAN_FC_FROMDS &&
  470. (local->iw_mode != IW_MODE_INFRA ||
  471. !(local->wds_type & HOSTAP_WDS_AP_CLIENT) ||
  472. memcmp(hdr->addr2, local->bssid, ETH_ALEN) != 0)) {
  473. /* require that WDS link has been registered with TA or the
  474. * frame is from current AP when using 'AP client mode' */
  475. PDEBUG(DEBUG_EXTRA, "%s: received WDS[4 addr] frame "
  476. "from unknown TA=" MACSTR "\n",
  477. local->dev->name, MAC2STR(hdr->addr2));
  478. if (local->ap && local->ap->autom_ap_wds)
  479. hostap_wds_link_oper(local, hdr->addr2, WDS_ADD);
  480. return -1;
  481. }
  482. if (*wds && !(fc & WLAN_FC_FROMDS) && local->ap &&
  483. hostap_is_sta_assoc(local->ap, hdr->addr2)) {
  484. /* STA is actually associated with us even though it has a
  485. * registered WDS link. Assume it is in 'AP client' mode.
  486. * Since this is a 3-addr frame, assume it is not (bogus) WDS
  487. * frame and process it like any normal ToDS frame from
  488. * associated STA. */
  489. *wds = NULL;
  490. }
  491. return 0;
  492. }
  493. static int hostap_is_eapol_frame(local_info_t *local, struct sk_buff *skb)
  494. {
  495. struct net_device *dev = local->dev;
  496. u16 fc, ethertype;
  497. struct ieee80211_hdr *hdr;
  498. u8 *pos;
  499. if (skb->len < 24)
  500. return 0;
  501. hdr = (struct ieee80211_hdr *) skb->data;
  502. fc = le16_to_cpu(hdr->frame_ctl);
  503. /* check that the frame is unicast frame to us */
  504. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == WLAN_FC_TODS &&
  505. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
  506. memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
  507. /* ToDS frame with own addr BSSID and DA */
  508. } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == WLAN_FC_FROMDS &&
  509. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
  510. /* FromDS frame with own addr as DA */
  511. } else
  512. return 0;
  513. if (skb->len < 24 + 8)
  514. return 0;
  515. /* check for port access entity Ethernet type */
  516. pos = skb->data + 24;
  517. ethertype = (pos[6] << 8) | pos[7];
  518. if (ethertype == ETH_P_PAE)
  519. return 1;
  520. return 0;
  521. }
  522. /* Called only as a tasklet (software IRQ) */
  523. static inline int
  524. hostap_rx_frame_decrypt(local_info_t *local, struct sk_buff *skb,
  525. struct ieee80211_crypt_data *crypt)
  526. {
  527. struct ieee80211_hdr *hdr;
  528. int res, hdrlen;
  529. if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
  530. return 0;
  531. hdr = (struct ieee80211_hdr *) skb->data;
  532. hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  533. if (local->tkip_countermeasures &&
  534. strcmp(crypt->ops->name, "TKIP") == 0) {
  535. if (net_ratelimit()) {
  536. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  537. "received packet from " MACSTR "\n",
  538. local->dev->name, MAC2STR(hdr->addr2));
  539. }
  540. return -1;
  541. }
  542. atomic_inc(&crypt->refcnt);
  543. res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
  544. atomic_dec(&crypt->refcnt);
  545. if (res < 0) {
  546. printk(KERN_DEBUG "%s: decryption failed (SA=" MACSTR
  547. ") res=%d\n",
  548. local->dev->name, MAC2STR(hdr->addr2), res);
  549. local->comm_tallies.rx_discards_wep_undecryptable++;
  550. return -1;
  551. }
  552. return res;
  553. }
  554. /* Called only as a tasklet (software IRQ) */
  555. static inline int
  556. hostap_rx_frame_decrypt_msdu(local_info_t *local, struct sk_buff *skb,
  557. int keyidx, struct ieee80211_crypt_data *crypt)
  558. {
  559. struct ieee80211_hdr *hdr;
  560. int res, hdrlen;
  561. if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
  562. return 0;
  563. hdr = (struct ieee80211_hdr *) skb->data;
  564. hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  565. atomic_inc(&crypt->refcnt);
  566. res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
  567. atomic_dec(&crypt->refcnt);
  568. if (res < 0) {
  569. printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
  570. " (SA=" MACSTR " keyidx=%d)\n",
  571. local->dev->name, MAC2STR(hdr->addr2), keyidx);
  572. return -1;
  573. }
  574. return 0;
  575. }
  576. /* All received frames are sent to this function. @skb contains the frame in
  577. * IEEE 802.11 format, i.e., in the format it was sent over air.
  578. * This function is called only as a tasklet (software IRQ). */
  579. void hostap_80211_rx(struct net_device *dev, struct sk_buff *skb,
  580. struct hostap_80211_rx_status *rx_stats)
  581. {
  582. struct hostap_interface *iface;
  583. local_info_t *local;
  584. struct ieee80211_hdr *hdr;
  585. size_t hdrlen;
  586. u16 fc, type, stype, sc;
  587. struct net_device *wds = NULL;
  588. struct net_device_stats *stats;
  589. unsigned int frag;
  590. u8 *payload;
  591. struct sk_buff *skb2 = NULL;
  592. u16 ethertype;
  593. int frame_authorized = 0;
  594. int from_assoc_ap = 0;
  595. u8 dst[ETH_ALEN];
  596. u8 src[ETH_ALEN];
  597. struct ieee80211_crypt_data *crypt = NULL;
  598. void *sta = NULL;
  599. int keyidx = 0;
  600. iface = netdev_priv(dev);
  601. local = iface->local;
  602. iface->stats.rx_packets++;
  603. iface->stats.rx_bytes += skb->len;
  604. /* dev is the master radio device; change this to be the default
  605. * virtual interface (this may be changed to WDS device below) */
  606. dev = local->ddev;
  607. iface = netdev_priv(dev);
  608. hdr = (struct ieee80211_hdr *) skb->data;
  609. stats = hostap_get_stats(dev);
  610. if (skb->len < 10)
  611. goto rx_dropped;
  612. fc = le16_to_cpu(hdr->frame_ctl);
  613. type = HOSTAP_FC_GET_TYPE(fc);
  614. stype = HOSTAP_FC_GET_STYPE(fc);
  615. sc = le16_to_cpu(hdr->seq_ctl);
  616. frag = WLAN_GET_SEQ_FRAG(sc);
  617. hdrlen = hostap_80211_get_hdrlen(fc);
  618. /* Put this code here so that we avoid duplicating it in all
  619. * Rx paths. - Jean II */
  620. #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
  621. /* If spy monitoring on */
  622. if (iface->spy_data.spy_number > 0) {
  623. struct iw_quality wstats;
  624. wstats.level = rx_stats->signal;
  625. wstats.noise = rx_stats->noise;
  626. wstats.updated = 6; /* No qual value */
  627. /* Update spy records */
  628. wireless_spy_update(dev, hdr->addr2, &wstats);
  629. }
  630. #endif /* IW_WIRELESS_SPY */
  631. hostap_update_rx_stats(local->ap, hdr, rx_stats);
  632. if (local->iw_mode == IW_MODE_MONITOR) {
  633. monitor_rx(dev, skb, rx_stats);
  634. return;
  635. }
  636. if (local->host_decrypt) {
  637. int idx = 0;
  638. if (skb->len >= hdrlen + 3)
  639. idx = skb->data[hdrlen + 3] >> 6;
  640. crypt = local->crypt[idx];
  641. sta = NULL;
  642. /* Use station specific key to override default keys if the
  643. * receiver address is a unicast address ("individual RA"). If
  644. * bcrx_sta_key parameter is set, station specific key is used
  645. * even with broad/multicast targets (this is against IEEE
  646. * 802.11, but makes it easier to use different keys with
  647. * stations that do not support WEP key mapping). */
  648. if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
  649. (void) hostap_handle_sta_crypto(local, hdr, &crypt,
  650. &sta);
  651. /* allow NULL decrypt to indicate an station specific override
  652. * for default encryption */
  653. if (crypt && (crypt->ops == NULL ||
  654. crypt->ops->decrypt_mpdu == NULL))
  655. crypt = NULL;
  656. if (!crypt && (fc & WLAN_FC_ISWEP)) {
  657. #if 0
  658. /* This seems to be triggered by some (multicast?)
  659. * frames from other than current BSS, so just drop the
  660. * frames silently instead of filling system log with
  661. * these reports. */
  662. printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
  663. " (SA=" MACSTR ")\n",
  664. local->dev->name, MAC2STR(hdr->addr2));
  665. #endif
  666. local->comm_tallies.rx_discards_wep_undecryptable++;
  667. goto rx_dropped;
  668. }
  669. }
  670. if (type != WLAN_FC_TYPE_DATA) {
  671. if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
  672. fc & WLAN_FC_ISWEP && local->host_decrypt &&
  673. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  674. {
  675. printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
  676. "from " MACSTR "\n", dev->name,
  677. MAC2STR(hdr->addr2));
  678. /* TODO: could inform hostapd about this so that it
  679. * could send auth failure report */
  680. goto rx_dropped;
  681. }
  682. if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
  683. goto rx_dropped;
  684. else
  685. goto rx_exit;
  686. }
  687. /* Data frame - extract src/dst addresses */
  688. if (skb->len < IEEE80211_DATA_HDR3_LEN)
  689. goto rx_dropped;
  690. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  691. case WLAN_FC_FROMDS:
  692. memcpy(dst, hdr->addr1, ETH_ALEN);
  693. memcpy(src, hdr->addr3, ETH_ALEN);
  694. break;
  695. case WLAN_FC_TODS:
  696. memcpy(dst, hdr->addr3, ETH_ALEN);
  697. memcpy(src, hdr->addr2, ETH_ALEN);
  698. break;
  699. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  700. if (skb->len < IEEE80211_DATA_HDR4_LEN)
  701. goto rx_dropped;
  702. memcpy(dst, hdr->addr3, ETH_ALEN);
  703. memcpy(src, hdr->addr4, ETH_ALEN);
  704. break;
  705. case 0:
  706. memcpy(dst, hdr->addr1, ETH_ALEN);
  707. memcpy(src, hdr->addr2, ETH_ALEN);
  708. break;
  709. }
  710. if (hostap_rx_frame_wds(local, hdr, fc, &wds))
  711. goto rx_dropped;
  712. if (wds) {
  713. skb->dev = dev = wds;
  714. stats = hostap_get_stats(dev);
  715. }
  716. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  717. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == WLAN_FC_FROMDS &&
  718. local->stadev &&
  719. memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
  720. /* Frame from BSSID of the AP for which we are a client */
  721. skb->dev = dev = local->stadev;
  722. stats = hostap_get_stats(dev);
  723. from_assoc_ap = 1;
  724. }
  725. dev->last_rx = jiffies;
  726. if ((local->iw_mode == IW_MODE_MASTER ||
  727. local->iw_mode == IW_MODE_REPEAT) &&
  728. !from_assoc_ap) {
  729. switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
  730. wds != NULL)) {
  731. case AP_RX_CONTINUE_NOT_AUTHORIZED:
  732. frame_authorized = 0;
  733. break;
  734. case AP_RX_CONTINUE:
  735. frame_authorized = 1;
  736. break;
  737. case AP_RX_DROP:
  738. goto rx_dropped;
  739. case AP_RX_EXIT:
  740. goto rx_exit;
  741. }
  742. }
  743. /* Nullfunc frames may have PS-bit set, so they must be passed to
  744. * hostap_handle_sta_rx() before being dropped here. */
  745. if (stype != WLAN_FC_STYPE_DATA &&
  746. stype != WLAN_FC_STYPE_DATA_CFACK &&
  747. stype != WLAN_FC_STYPE_DATA_CFPOLL &&
  748. stype != WLAN_FC_STYPE_DATA_CFACKPOLL) {
  749. if (stype != WLAN_FC_STYPE_NULLFUNC)
  750. printk(KERN_DEBUG "%s: RX: dropped data frame "
  751. "with no data (type=0x%02x, subtype=0x%02x)\n",
  752. dev->name, type, stype);
  753. goto rx_dropped;
  754. }
  755. /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
  756. if (local->host_decrypt && (fc & WLAN_FC_ISWEP) &&
  757. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  758. goto rx_dropped;
  759. hdr = (struct ieee80211_hdr *) skb->data;
  760. /* skb: hdr + (possibly fragmented) plaintext payload */
  761. if (local->host_decrypt && (fc & WLAN_FC_ISWEP) &&
  762. (frag != 0 || (fc & WLAN_FC_MOREFRAG))) {
  763. int flen;
  764. struct sk_buff *frag_skb =
  765. prism2_frag_cache_get(local, hdr);
  766. if (!frag_skb) {
  767. printk(KERN_DEBUG "%s: Rx cannot get skb from "
  768. "fragment cache (morefrag=%d seq=%u frag=%u)\n",
  769. dev->name, (fc & WLAN_FC_MOREFRAG) != 0,
  770. WLAN_GET_SEQ_SEQ(sc) >> 4, frag);
  771. goto rx_dropped;
  772. }
  773. flen = skb->len;
  774. if (frag != 0)
  775. flen -= hdrlen;
  776. if (frag_skb->tail + flen > frag_skb->end) {
  777. printk(KERN_WARNING "%s: host decrypted and "
  778. "reassembled frame did not fit skb\n",
  779. dev->name);
  780. prism2_frag_cache_invalidate(local, hdr);
  781. goto rx_dropped;
  782. }
  783. if (frag == 0) {
  784. /* copy first fragment (including full headers) into
  785. * beginning of the fragment cache skb */
  786. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  787. } else {
  788. /* append frame payload to the end of the fragment
  789. * cache skb */
  790. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  791. flen);
  792. }
  793. dev_kfree_skb(skb);
  794. skb = NULL;
  795. if (fc & WLAN_FC_MOREFRAG) {
  796. /* more fragments expected - leave the skb in fragment
  797. * cache for now; it will be delivered to upper layers
  798. * after all fragments have been received */
  799. goto rx_exit;
  800. }
  801. /* this was the last fragment and the frame will be
  802. * delivered, so remove skb from fragment cache */
  803. skb = frag_skb;
  804. hdr = (struct ieee80211_hdr *) skb->data;
  805. prism2_frag_cache_invalidate(local, hdr);
  806. }
  807. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  808. * encrypted/authenticated */
  809. if (local->host_decrypt && (fc & WLAN_FC_ISWEP) &&
  810. hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
  811. goto rx_dropped;
  812. hdr = (struct ieee80211_hdr *) skb->data;
  813. if (crypt && !(fc & WLAN_FC_ISWEP) && !local->open_wep) {
  814. if (local->ieee_802_1x &&
  815. hostap_is_eapol_frame(local, skb)) {
  816. /* pass unencrypted EAPOL frames even if encryption is
  817. * configured */
  818. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
  819. "unencrypted EAPOL frame\n", local->dev->name);
  820. } else {
  821. printk(KERN_DEBUG "%s: encryption configured, but RX "
  822. "frame not encrypted (SA=" MACSTR ")\n",
  823. local->dev->name, MAC2STR(hdr->addr2));
  824. goto rx_dropped;
  825. }
  826. }
  827. if (local->drop_unencrypted && !(fc & WLAN_FC_ISWEP) &&
  828. !hostap_is_eapol_frame(local, skb)) {
  829. if (net_ratelimit()) {
  830. printk(KERN_DEBUG "%s: dropped unencrypted RX data "
  831. "frame from " MACSTR " (drop_unencrypted=1)\n",
  832. dev->name, MAC2STR(hdr->addr2));
  833. }
  834. goto rx_dropped;
  835. }
  836. /* skb: hdr + (possible reassembled) full plaintext payload */
  837. payload = skb->data + hdrlen;
  838. ethertype = (payload[6] << 8) | payload[7];
  839. /* If IEEE 802.1X is used, check whether the port is authorized to send
  840. * the received frame. */
  841. if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
  842. if (ethertype == ETH_P_PAE) {
  843. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
  844. dev->name);
  845. if (local->hostapd && local->apdev) {
  846. /* Send IEEE 802.1X frames to the user
  847. * space daemon for processing */
  848. prism2_rx_80211(local->apdev, skb, rx_stats,
  849. PRISM2_RX_MGMT);
  850. local->apdevstats.rx_packets++;
  851. local->apdevstats.rx_bytes += skb->len;
  852. goto rx_exit;
  853. }
  854. } else if (!frame_authorized) {
  855. printk(KERN_DEBUG "%s: dropped frame from "
  856. "unauthorized port (IEEE 802.1X): "
  857. "ethertype=0x%04x\n",
  858. dev->name, ethertype);
  859. goto rx_dropped;
  860. }
  861. }
  862. /* convert hdr + possible LLC headers into Ethernet header */
  863. if (skb->len - hdrlen >= 8 &&
  864. ((memcmp(payload, rfc1042_header, 6) == 0 &&
  865. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  866. memcmp(payload, bridge_tunnel_header, 6) == 0)) {
  867. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  868. * replace EtherType */
  869. skb_pull(skb, hdrlen + 6);
  870. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  871. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  872. } else {
  873. u16 len;
  874. /* Leave Ethernet header part of hdr and full payload */
  875. skb_pull(skb, hdrlen);
  876. len = htons(skb->len);
  877. memcpy(skb_push(skb, 2), &len, 2);
  878. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  879. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  880. }
  881. if (wds && ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == WLAN_FC_TODS) &&
  882. skb->len >= ETH_HLEN + ETH_ALEN) {
  883. /* Non-standard frame: get addr4 from its bogus location after
  884. * the payload */
  885. memcpy(skb->data + ETH_ALEN,
  886. skb->data + skb->len - ETH_ALEN, ETH_ALEN);
  887. skb_trim(skb, skb->len - ETH_ALEN);
  888. }
  889. stats->rx_packets++;
  890. stats->rx_bytes += skb->len;
  891. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  892. local->ap->bridge_packets) {
  893. if (dst[0] & 0x01) {
  894. /* copy multicast frame both to the higher layers and
  895. * to the wireless media */
  896. local->ap->bridged_multicast++;
  897. skb2 = skb_clone(skb, GFP_ATOMIC);
  898. if (skb2 == NULL)
  899. printk(KERN_DEBUG "%s: skb_clone failed for "
  900. "multicast frame\n", dev->name);
  901. } else if (hostap_is_sta_authorized(local->ap, dst)) {
  902. /* send frame directly to the associated STA using
  903. * wireless media and not passing to higher layers */
  904. local->ap->bridged_unicast++;
  905. skb2 = skb;
  906. skb = NULL;
  907. }
  908. }
  909. if (skb2 != NULL) {
  910. /* send to wireless media */
  911. skb2->protocol = __constant_htons(ETH_P_802_3);
  912. skb2->mac.raw = skb2->nh.raw = skb2->data;
  913. /* skb2->nh.raw = skb2->data + ETH_HLEN; */
  914. skb2->dev = dev;
  915. dev_queue_xmit(skb2);
  916. }
  917. if (skb) {
  918. skb->protocol = eth_type_trans(skb, dev);
  919. memset(skb->cb, 0, sizeof(skb->cb));
  920. skb->dev = dev;
  921. netif_rx(skb);
  922. }
  923. rx_exit:
  924. if (sta)
  925. hostap_handle_sta_release(sta);
  926. return;
  927. rx_dropped:
  928. dev_kfree_skb(skb);
  929. stats->rx_dropped++;
  930. goto rx_exit;
  931. }
  932. EXPORT_SYMBOL(hostap_80211_rx);