hostap_80211_rx.c 30 KB

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