hostap_80211_rx.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
  19. fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
  20. fc & IEEE80211_FCTL_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 & IEEE80211_FCTL_VERS)) {
  62. printk(KERN_DEBUG "%s: dropped management frame with header "
  63. "version %d\n", dev->name, fc & IEEE80211_FCTL_VERS);
  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 == IEEE80211_FTYPE_MGMT) {
  381. if (stype == IEEE80211_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 != IEEE80211_FTYPE_MGMT &&
  401. type != IEEE80211_FTYPE_CTL) {
  402. printk(KERN_DEBUG "%s: unknown management frame "
  403. "(type=0x%02x, stype=0x%02x) dropped\n",
  404. skb->dev->name, type >> 2, stype >> 4);
  405. return -1;
  406. }
  407. hostap_rx(skb->dev, skb, rx_stats);
  408. return 0;
  409. } else if (type == IEEE80211_FTYPE_MGMT &&
  410. (stype == IEEE80211_STYPE_BEACON ||
  411. stype == IEEE80211_STYPE_PROBE_RESP)) {
  412. hostap_rx_sta_beacon(local, skb, stype);
  413. return -1;
  414. } else if (type == IEEE80211_FTYPE_MGMT &&
  415. (stype == IEEE80211_STYPE_ASSOC_RESP ||
  416. stype == IEEE80211_STYPE_REASSOC_RESP)) {
  417. /* Ignore (Re)AssocResp silently since these are not currently
  418. * needed but are still received when WPA/RSN mode is enabled.
  419. */
  420. return -1;
  421. } else {
  422. printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: dropped unhandled"
  423. " management frame in non-Host AP mode (type=%d:%d)\n",
  424. skb->dev->name, type >> 2, stype >> 4);
  425. return -1;
  426. }
  427. }
  428. /* Called only as a tasklet (software IRQ) */
  429. static inline struct net_device *prism2_rx_get_wds(local_info_t *local,
  430. u8 *addr)
  431. {
  432. struct hostap_interface *iface = NULL;
  433. struct list_head *ptr;
  434. read_lock_bh(&local->iface_lock);
  435. list_for_each(ptr, &local->hostap_interfaces) {
  436. iface = list_entry(ptr, struct hostap_interface, list);
  437. if (iface->type == HOSTAP_INTERFACE_WDS &&
  438. memcmp(iface->u.wds.remote_addr, addr, ETH_ALEN) == 0)
  439. break;
  440. iface = NULL;
  441. }
  442. read_unlock_bh(&local->iface_lock);
  443. return iface ? iface->dev : NULL;
  444. }
  445. static inline int
  446. hostap_rx_frame_wds(local_info_t *local, struct ieee80211_hdr *hdr,
  447. u16 fc, struct net_device **wds)
  448. {
  449. /* FIX: is this really supposed to accept WDS frames only in Master
  450. * mode? What about Repeater or Managed with WDS frames? */
  451. if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) !=
  452. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS) &&
  453. (local->iw_mode != IW_MODE_MASTER || !(fc & IEEE80211_FCTL_TODS)))
  454. return 0; /* not a WDS frame */
  455. /* Possible WDS frame: either IEEE 802.11 compliant (if FromDS)
  456. * or own non-standard frame with 4th address after payload */
  457. if (memcmp(hdr->addr1, local->dev->dev_addr, ETH_ALEN) != 0 &&
  458. (hdr->addr1[0] != 0xff || hdr->addr1[1] != 0xff ||
  459. hdr->addr1[2] != 0xff || hdr->addr1[3] != 0xff ||
  460. hdr->addr1[4] != 0xff || hdr->addr1[5] != 0xff)) {
  461. /* RA (or BSSID) is not ours - drop */
  462. PDEBUG(DEBUG_EXTRA, "%s: received WDS frame with "
  463. "not own or broadcast %s=" MACSTR "\n",
  464. local->dev->name,
  465. fc & IEEE80211_FCTL_FROMDS ? "RA" : "BSSID",
  466. MAC2STR(hdr->addr1));
  467. return -1;
  468. }
  469. /* check if the frame came from a registered WDS connection */
  470. *wds = prism2_rx_get_wds(local, hdr->addr2);
  471. if (*wds == NULL && fc & IEEE80211_FCTL_FROMDS &&
  472. (local->iw_mode != IW_MODE_INFRA ||
  473. !(local->wds_type & HOSTAP_WDS_AP_CLIENT) ||
  474. memcmp(hdr->addr2, local->bssid, ETH_ALEN) != 0)) {
  475. /* require that WDS link has been registered with TA or the
  476. * frame is from current AP when using 'AP client mode' */
  477. PDEBUG(DEBUG_EXTRA, "%s: received WDS[4 addr] frame "
  478. "from unknown TA=" MACSTR "\n",
  479. local->dev->name, MAC2STR(hdr->addr2));
  480. if (local->ap && local->ap->autom_ap_wds)
  481. hostap_wds_link_oper(local, hdr->addr2, WDS_ADD);
  482. return -1;
  483. }
  484. if (*wds && !(fc & IEEE80211_FCTL_FROMDS) && local->ap &&
  485. hostap_is_sta_assoc(local->ap, hdr->addr2)) {
  486. /* STA is actually associated with us even though it has a
  487. * registered WDS link. Assume it is in 'AP client' mode.
  488. * Since this is a 3-addr frame, assume it is not (bogus) WDS
  489. * frame and process it like any normal ToDS frame from
  490. * associated STA. */
  491. *wds = NULL;
  492. }
  493. return 0;
  494. }
  495. static int hostap_is_eapol_frame(local_info_t *local, struct sk_buff *skb)
  496. {
  497. struct net_device *dev = local->dev;
  498. u16 fc, ethertype;
  499. struct ieee80211_hdr *hdr;
  500. u8 *pos;
  501. if (skb->len < 24)
  502. return 0;
  503. hdr = (struct ieee80211_hdr *) skb->data;
  504. fc = le16_to_cpu(hdr->frame_ctl);
  505. /* check that the frame is unicast frame to us */
  506. if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  507. IEEE80211_FCTL_TODS &&
  508. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
  509. memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
  510. /* ToDS frame with own addr BSSID and DA */
  511. } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  512. IEEE80211_FCTL_FROMDS &&
  513. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
  514. /* FromDS frame with own addr as DA */
  515. } else
  516. return 0;
  517. if (skb->len < 24 + 8)
  518. return 0;
  519. /* check for port access entity Ethernet type */
  520. pos = skb->data + 24;
  521. ethertype = (pos[6] << 8) | pos[7];
  522. if (ethertype == ETH_P_PAE)
  523. return 1;
  524. return 0;
  525. }
  526. /* Called only as a tasklet (software IRQ) */
  527. static inline int
  528. hostap_rx_frame_decrypt(local_info_t *local, struct sk_buff *skb,
  529. struct ieee80211_crypt_data *crypt)
  530. {
  531. struct ieee80211_hdr *hdr;
  532. int res, hdrlen;
  533. if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
  534. return 0;
  535. hdr = (struct ieee80211_hdr *) skb->data;
  536. hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  537. if (local->tkip_countermeasures &&
  538. strcmp(crypt->ops->name, "TKIP") == 0) {
  539. if (net_ratelimit()) {
  540. printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
  541. "received packet from " MACSTR "\n",
  542. local->dev->name, MAC2STR(hdr->addr2));
  543. }
  544. return -1;
  545. }
  546. atomic_inc(&crypt->refcnt);
  547. res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
  548. atomic_dec(&crypt->refcnt);
  549. if (res < 0) {
  550. printk(KERN_DEBUG "%s: decryption failed (SA=" MACSTR
  551. ") res=%d\n",
  552. local->dev->name, MAC2STR(hdr->addr2), res);
  553. local->comm_tallies.rx_discards_wep_undecryptable++;
  554. return -1;
  555. }
  556. return res;
  557. }
  558. /* Called only as a tasklet (software IRQ) */
  559. static inline int
  560. hostap_rx_frame_decrypt_msdu(local_info_t *local, struct sk_buff *skb,
  561. int keyidx, struct ieee80211_crypt_data *crypt)
  562. {
  563. struct ieee80211_hdr *hdr;
  564. int res, hdrlen;
  565. if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
  566. return 0;
  567. hdr = (struct ieee80211_hdr *) skb->data;
  568. hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  569. atomic_inc(&crypt->refcnt);
  570. res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
  571. atomic_dec(&crypt->refcnt);
  572. if (res < 0) {
  573. printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
  574. " (SA=" MACSTR " keyidx=%d)\n",
  575. local->dev->name, MAC2STR(hdr->addr2), keyidx);
  576. return -1;
  577. }
  578. return 0;
  579. }
  580. /* All received frames are sent to this function. @skb contains the frame in
  581. * IEEE 802.11 format, i.e., in the format it was sent over air.
  582. * This function is called only as a tasklet (software IRQ). */
  583. void hostap_80211_rx(struct net_device *dev, struct sk_buff *skb,
  584. struct hostap_80211_rx_status *rx_stats)
  585. {
  586. struct hostap_interface *iface;
  587. local_info_t *local;
  588. struct ieee80211_hdr *hdr;
  589. size_t hdrlen;
  590. u16 fc, type, stype, sc;
  591. struct net_device *wds = NULL;
  592. struct net_device_stats *stats;
  593. unsigned int frag;
  594. u8 *payload;
  595. struct sk_buff *skb2 = NULL;
  596. u16 ethertype;
  597. int frame_authorized = 0;
  598. int from_assoc_ap = 0;
  599. u8 dst[ETH_ALEN];
  600. u8 src[ETH_ALEN];
  601. struct ieee80211_crypt_data *crypt = NULL;
  602. void *sta = NULL;
  603. int keyidx = 0;
  604. iface = netdev_priv(dev);
  605. local = iface->local;
  606. iface->stats.rx_packets++;
  607. iface->stats.rx_bytes += skb->len;
  608. /* dev is the master radio device; change this to be the default
  609. * virtual interface (this may be changed to WDS device below) */
  610. dev = local->ddev;
  611. iface = netdev_priv(dev);
  612. hdr = (struct ieee80211_hdr *) skb->data;
  613. stats = hostap_get_stats(dev);
  614. if (skb->len < 10)
  615. goto rx_dropped;
  616. fc = le16_to_cpu(hdr->frame_ctl);
  617. type = WLAN_FC_GET_TYPE(fc);
  618. stype = WLAN_FC_GET_STYPE(fc);
  619. sc = le16_to_cpu(hdr->seq_ctl);
  620. frag = WLAN_GET_SEQ_FRAG(sc);
  621. hdrlen = hostap_80211_get_hdrlen(fc);
  622. /* Put this code here so that we avoid duplicating it in all
  623. * Rx paths. - Jean II */
  624. #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
  625. /* If spy monitoring on */
  626. if (iface->spy_data.spy_number > 0) {
  627. struct iw_quality wstats;
  628. wstats.level = rx_stats->signal;
  629. wstats.noise = rx_stats->noise;
  630. wstats.updated = 6; /* No qual value */
  631. /* Update spy records */
  632. wireless_spy_update(dev, hdr->addr2, &wstats);
  633. }
  634. #endif /* IW_WIRELESS_SPY */
  635. hostap_update_rx_stats(local->ap, hdr, rx_stats);
  636. if (local->iw_mode == IW_MODE_MONITOR) {
  637. monitor_rx(dev, skb, rx_stats);
  638. return;
  639. }
  640. if (local->host_decrypt) {
  641. int idx = 0;
  642. if (skb->len >= hdrlen + 3)
  643. idx = skb->data[hdrlen + 3] >> 6;
  644. crypt = local->crypt[idx];
  645. sta = NULL;
  646. /* Use station specific key to override default keys if the
  647. * receiver address is a unicast address ("individual RA"). If
  648. * bcrx_sta_key parameter is set, station specific key is used
  649. * even with broad/multicast targets (this is against IEEE
  650. * 802.11, but makes it easier to use different keys with
  651. * stations that do not support WEP key mapping). */
  652. if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
  653. (void) hostap_handle_sta_crypto(local, hdr, &crypt,
  654. &sta);
  655. /* allow NULL decrypt to indicate an station specific override
  656. * for default encryption */
  657. if (crypt && (crypt->ops == NULL ||
  658. crypt->ops->decrypt_mpdu == NULL))
  659. crypt = NULL;
  660. if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
  661. #if 0
  662. /* This seems to be triggered by some (multicast?)
  663. * frames from other than current BSS, so just drop the
  664. * frames silently instead of filling system log with
  665. * these reports. */
  666. printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
  667. " (SA=" MACSTR ")\n",
  668. local->dev->name, MAC2STR(hdr->addr2));
  669. #endif
  670. local->comm_tallies.rx_discards_wep_undecryptable++;
  671. goto rx_dropped;
  672. }
  673. }
  674. if (type != IEEE80211_FTYPE_DATA) {
  675. if (type == IEEE80211_FTYPE_MGMT &&
  676. stype == IEEE80211_STYPE_AUTH &&
  677. fc & IEEE80211_FCTL_PROTECTED && local->host_decrypt &&
  678. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  679. {
  680. printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
  681. "from " MACSTR "\n", dev->name,
  682. MAC2STR(hdr->addr2));
  683. /* TODO: could inform hostapd about this so that it
  684. * could send auth failure report */
  685. goto rx_dropped;
  686. }
  687. if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
  688. goto rx_dropped;
  689. else
  690. goto rx_exit;
  691. }
  692. /* Data frame - extract src/dst addresses */
  693. if (skb->len < IEEE80211_DATA_HDR3_LEN)
  694. goto rx_dropped;
  695. switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
  696. case IEEE80211_FCTL_FROMDS:
  697. memcpy(dst, hdr->addr1, ETH_ALEN);
  698. memcpy(src, hdr->addr3, ETH_ALEN);
  699. break;
  700. case IEEE80211_FCTL_TODS:
  701. memcpy(dst, hdr->addr3, ETH_ALEN);
  702. memcpy(src, hdr->addr2, ETH_ALEN);
  703. break;
  704. case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
  705. if (skb->len < IEEE80211_DATA_HDR4_LEN)
  706. goto rx_dropped;
  707. memcpy(dst, hdr->addr3, ETH_ALEN);
  708. memcpy(src, hdr->addr4, ETH_ALEN);
  709. break;
  710. case 0:
  711. memcpy(dst, hdr->addr1, ETH_ALEN);
  712. memcpy(src, hdr->addr2, ETH_ALEN);
  713. break;
  714. }
  715. if (hostap_rx_frame_wds(local, hdr, fc, &wds))
  716. goto rx_dropped;
  717. if (wds) {
  718. skb->dev = dev = wds;
  719. stats = hostap_get_stats(dev);
  720. }
  721. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  722. (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  723. IEEE80211_FCTL_FROMDS &&
  724. local->stadev &&
  725. memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
  726. /* Frame from BSSID of the AP for which we are a client */
  727. skb->dev = dev = local->stadev;
  728. stats = hostap_get_stats(dev);
  729. from_assoc_ap = 1;
  730. }
  731. dev->last_rx = jiffies;
  732. if ((local->iw_mode == IW_MODE_MASTER ||
  733. local->iw_mode == IW_MODE_REPEAT) &&
  734. !from_assoc_ap) {
  735. switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
  736. wds != NULL)) {
  737. case AP_RX_CONTINUE_NOT_AUTHORIZED:
  738. frame_authorized = 0;
  739. break;
  740. case AP_RX_CONTINUE:
  741. frame_authorized = 1;
  742. break;
  743. case AP_RX_DROP:
  744. goto rx_dropped;
  745. case AP_RX_EXIT:
  746. goto rx_exit;
  747. }
  748. }
  749. /* Nullfunc frames may have PS-bit set, so they must be passed to
  750. * hostap_handle_sta_rx() before being dropped here. */
  751. if (stype != IEEE80211_STYPE_DATA &&
  752. stype != IEEE80211_STYPE_DATA_CFACK &&
  753. stype != IEEE80211_STYPE_DATA_CFPOLL &&
  754. stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
  755. if (stype != IEEE80211_STYPE_NULLFUNC)
  756. printk(KERN_DEBUG "%s: RX: dropped data frame "
  757. "with no data (type=0x%02x, subtype=0x%02x)\n",
  758. dev->name, type >> 2, stype >> 4);
  759. goto rx_dropped;
  760. }
  761. /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
  762. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  763. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  764. goto rx_dropped;
  765. hdr = (struct ieee80211_hdr *) skb->data;
  766. /* skb: hdr + (possibly fragmented) plaintext payload */
  767. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  768. (frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
  769. int flen;
  770. struct sk_buff *frag_skb =
  771. prism2_frag_cache_get(local, hdr);
  772. if (!frag_skb) {
  773. printk(KERN_DEBUG "%s: Rx cannot get skb from "
  774. "fragment cache (morefrag=%d seq=%u frag=%u)\n",
  775. dev->name, (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
  776. WLAN_GET_SEQ_SEQ(sc) >> 4, frag);
  777. goto rx_dropped;
  778. }
  779. flen = skb->len;
  780. if (frag != 0)
  781. flen -= hdrlen;
  782. if (frag_skb->tail + flen > frag_skb->end) {
  783. printk(KERN_WARNING "%s: host decrypted and "
  784. "reassembled frame did not fit skb\n",
  785. dev->name);
  786. prism2_frag_cache_invalidate(local, hdr);
  787. goto rx_dropped;
  788. }
  789. if (frag == 0) {
  790. /* copy first fragment (including full headers) into
  791. * beginning of the fragment cache skb */
  792. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  793. } else {
  794. /* append frame payload to the end of the fragment
  795. * cache skb */
  796. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  797. flen);
  798. }
  799. dev_kfree_skb(skb);
  800. skb = NULL;
  801. if (fc & IEEE80211_FCTL_MOREFRAGS) {
  802. /* more fragments expected - leave the skb in fragment
  803. * cache for now; it will be delivered to upper layers
  804. * after all fragments have been received */
  805. goto rx_exit;
  806. }
  807. /* this was the last fragment and the frame will be
  808. * delivered, so remove skb from fragment cache */
  809. skb = frag_skb;
  810. hdr = (struct ieee80211_hdr *) skb->data;
  811. prism2_frag_cache_invalidate(local, hdr);
  812. }
  813. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  814. * encrypted/authenticated */
  815. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  816. hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
  817. goto rx_dropped;
  818. hdr = (struct ieee80211_hdr *) skb->data;
  819. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !local->open_wep) {
  820. if (local->ieee_802_1x &&
  821. hostap_is_eapol_frame(local, skb)) {
  822. /* pass unencrypted EAPOL frames even if encryption is
  823. * configured */
  824. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
  825. "unencrypted EAPOL frame\n", local->dev->name);
  826. } else {
  827. printk(KERN_DEBUG "%s: encryption configured, but RX "
  828. "frame not encrypted (SA=" MACSTR ")\n",
  829. local->dev->name, MAC2STR(hdr->addr2));
  830. goto rx_dropped;
  831. }
  832. }
  833. if (local->drop_unencrypted && !(fc & IEEE80211_FCTL_PROTECTED) &&
  834. !hostap_is_eapol_frame(local, skb)) {
  835. if (net_ratelimit()) {
  836. printk(KERN_DEBUG "%s: dropped unencrypted RX data "
  837. "frame from " MACSTR " (drop_unencrypted=1)\n",
  838. dev->name, MAC2STR(hdr->addr2));
  839. }
  840. goto rx_dropped;
  841. }
  842. /* skb: hdr + (possible reassembled) full plaintext payload */
  843. payload = skb->data + hdrlen;
  844. ethertype = (payload[6] << 8) | payload[7];
  845. /* If IEEE 802.1X is used, check whether the port is authorized to send
  846. * the received frame. */
  847. if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
  848. if (ethertype == ETH_P_PAE) {
  849. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
  850. dev->name);
  851. if (local->hostapd && local->apdev) {
  852. /* Send IEEE 802.1X frames to the user
  853. * space daemon for processing */
  854. prism2_rx_80211(local->apdev, skb, rx_stats,
  855. PRISM2_RX_MGMT);
  856. local->apdevstats.rx_packets++;
  857. local->apdevstats.rx_bytes += skb->len;
  858. goto rx_exit;
  859. }
  860. } else if (!frame_authorized) {
  861. printk(KERN_DEBUG "%s: dropped frame from "
  862. "unauthorized port (IEEE 802.1X): "
  863. "ethertype=0x%04x\n",
  864. dev->name, ethertype);
  865. goto rx_dropped;
  866. }
  867. }
  868. /* convert hdr + possible LLC headers into Ethernet header */
  869. if (skb->len - hdrlen >= 8 &&
  870. ((memcmp(payload, rfc1042_header, 6) == 0 &&
  871. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  872. memcmp(payload, bridge_tunnel_header, 6) == 0)) {
  873. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  874. * replace EtherType */
  875. skb_pull(skb, hdrlen + 6);
  876. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  877. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  878. } else {
  879. u16 len;
  880. /* Leave Ethernet header part of hdr and full payload */
  881. skb_pull(skb, hdrlen);
  882. len = htons(skb->len);
  883. memcpy(skb_push(skb, 2), &len, 2);
  884. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  885. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  886. }
  887. if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  888. IEEE80211_FCTL_TODS) &&
  889. skb->len >= ETH_HLEN + ETH_ALEN) {
  890. /* Non-standard frame: get addr4 from its bogus location after
  891. * the payload */
  892. memcpy(skb->data + ETH_ALEN,
  893. skb->data + skb->len - ETH_ALEN, ETH_ALEN);
  894. skb_trim(skb, skb->len - ETH_ALEN);
  895. }
  896. stats->rx_packets++;
  897. stats->rx_bytes += skb->len;
  898. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  899. local->ap->bridge_packets) {
  900. if (dst[0] & 0x01) {
  901. /* copy multicast frame both to the higher layers and
  902. * to the wireless media */
  903. local->ap->bridged_multicast++;
  904. skb2 = skb_clone(skb, GFP_ATOMIC);
  905. if (skb2 == NULL)
  906. printk(KERN_DEBUG "%s: skb_clone failed for "
  907. "multicast frame\n", dev->name);
  908. } else if (hostap_is_sta_authorized(local->ap, dst)) {
  909. /* send frame directly to the associated STA using
  910. * wireless media and not passing to higher layers */
  911. local->ap->bridged_unicast++;
  912. skb2 = skb;
  913. skb = NULL;
  914. }
  915. }
  916. if (skb2 != NULL) {
  917. /* send to wireless media */
  918. skb2->protocol = __constant_htons(ETH_P_802_3);
  919. skb2->mac.raw = skb2->nh.raw = skb2->data;
  920. /* skb2->nh.raw = skb2->data + ETH_HLEN; */
  921. skb2->dev = dev;
  922. dev_queue_xmit(skb2);
  923. }
  924. if (skb) {
  925. skb->protocol = eth_type_trans(skb, dev);
  926. memset(skb->cb, 0, sizeof(skb->cb));
  927. skb->dev = dev;
  928. netif_rx(skb);
  929. }
  930. rx_exit:
  931. if (sta)
  932. hostap_handle_sta_release(sta);
  933. return;
  934. rx_dropped:
  935. dev_kfree_skb(skb);
  936. stats->rx_dropped++;
  937. goto rx_exit;
  938. }
  939. EXPORT_SYMBOL(hostap_80211_rx);