hostap_80211_rx.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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_4addr *hdr;
  8. u16 fc;
  9. hdr = (struct ieee80211_hdr_4addr *) 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_4addr *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_4addr *) 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_4addr *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_4addr) +
  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_4addr *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_4addr *)
  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_4addr *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_4addr *hdr;
  500. u8 *pos;
  501. if (skb->len < 24)
  502. return 0;
  503. hdr = (struct ieee80211_hdr_4addr *) 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_4addr *hdr;
  532. int res, hdrlen;
  533. if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
  534. return 0;
  535. hdr = (struct ieee80211_hdr_4addr *) 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_4addr *hdr;
  564. int res, hdrlen;
  565. if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
  566. return 0;
  567. hdr = (struct ieee80211_hdr_4addr *) 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_4addr *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_4addr *) 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 = IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_UPDATED
  631. | IW_QUAL_QUAL_INVALID | IW_QUAL_DBM;
  632. /* Update spy records */
  633. wireless_spy_update(dev, hdr->addr2, &wstats);
  634. }
  635. #endif /* IW_WIRELESS_SPY */
  636. hostap_update_rx_stats(local->ap, hdr, rx_stats);
  637. if (local->iw_mode == IW_MODE_MONITOR) {
  638. monitor_rx(dev, skb, rx_stats);
  639. return;
  640. }
  641. if (local->host_decrypt) {
  642. int idx = 0;
  643. if (skb->len >= hdrlen + 3)
  644. idx = skb->data[hdrlen + 3] >> 6;
  645. crypt = local->crypt[idx];
  646. sta = NULL;
  647. /* Use station specific key to override default keys if the
  648. * receiver address is a unicast address ("individual RA"). If
  649. * bcrx_sta_key parameter is set, station specific key is used
  650. * even with broad/multicast targets (this is against IEEE
  651. * 802.11, but makes it easier to use different keys with
  652. * stations that do not support WEP key mapping). */
  653. if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
  654. (void) hostap_handle_sta_crypto(local, hdr, &crypt,
  655. &sta);
  656. /* allow NULL decrypt to indicate an station specific override
  657. * for default encryption */
  658. if (crypt && (crypt->ops == NULL ||
  659. crypt->ops->decrypt_mpdu == NULL))
  660. crypt = NULL;
  661. if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
  662. #if 0
  663. /* This seems to be triggered by some (multicast?)
  664. * frames from other than current BSS, so just drop the
  665. * frames silently instead of filling system log with
  666. * these reports. */
  667. printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
  668. " (SA=" MACSTR ")\n",
  669. local->dev->name, MAC2STR(hdr->addr2));
  670. #endif
  671. local->comm_tallies.rx_discards_wep_undecryptable++;
  672. goto rx_dropped;
  673. }
  674. }
  675. if (type != IEEE80211_FTYPE_DATA) {
  676. if (type == IEEE80211_FTYPE_MGMT &&
  677. stype == IEEE80211_STYPE_AUTH &&
  678. fc & IEEE80211_FCTL_PROTECTED && local->host_decrypt &&
  679. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  680. {
  681. printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
  682. "from " MACSTR "\n", dev->name,
  683. MAC2STR(hdr->addr2));
  684. /* TODO: could inform hostapd about this so that it
  685. * could send auth failure report */
  686. goto rx_dropped;
  687. }
  688. if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
  689. goto rx_dropped;
  690. else
  691. goto rx_exit;
  692. }
  693. /* Data frame - extract src/dst addresses */
  694. if (skb->len < IEEE80211_DATA_HDR3_LEN)
  695. goto rx_dropped;
  696. switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
  697. case IEEE80211_FCTL_FROMDS:
  698. memcpy(dst, hdr->addr1, ETH_ALEN);
  699. memcpy(src, hdr->addr3, ETH_ALEN);
  700. break;
  701. case IEEE80211_FCTL_TODS:
  702. memcpy(dst, hdr->addr3, ETH_ALEN);
  703. memcpy(src, hdr->addr2, ETH_ALEN);
  704. break;
  705. case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
  706. if (skb->len < IEEE80211_DATA_HDR4_LEN)
  707. goto rx_dropped;
  708. memcpy(dst, hdr->addr3, ETH_ALEN);
  709. memcpy(src, hdr->addr4, ETH_ALEN);
  710. break;
  711. case 0:
  712. memcpy(dst, hdr->addr1, ETH_ALEN);
  713. memcpy(src, hdr->addr2, ETH_ALEN);
  714. break;
  715. }
  716. if (hostap_rx_frame_wds(local, hdr, fc, &wds))
  717. goto rx_dropped;
  718. if (wds) {
  719. skb->dev = dev = wds;
  720. stats = hostap_get_stats(dev);
  721. }
  722. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  723. (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  724. IEEE80211_FCTL_FROMDS &&
  725. local->stadev &&
  726. memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
  727. /* Frame from BSSID of the AP for which we are a client */
  728. skb->dev = dev = local->stadev;
  729. stats = hostap_get_stats(dev);
  730. from_assoc_ap = 1;
  731. }
  732. dev->last_rx = jiffies;
  733. if ((local->iw_mode == IW_MODE_MASTER ||
  734. local->iw_mode == IW_MODE_REPEAT) &&
  735. !from_assoc_ap) {
  736. switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
  737. wds != NULL)) {
  738. case AP_RX_CONTINUE_NOT_AUTHORIZED:
  739. frame_authorized = 0;
  740. break;
  741. case AP_RX_CONTINUE:
  742. frame_authorized = 1;
  743. break;
  744. case AP_RX_DROP:
  745. goto rx_dropped;
  746. case AP_RX_EXIT:
  747. goto rx_exit;
  748. }
  749. }
  750. /* Nullfunc frames may have PS-bit set, so they must be passed to
  751. * hostap_handle_sta_rx() before being dropped here. */
  752. if (stype != IEEE80211_STYPE_DATA &&
  753. stype != IEEE80211_STYPE_DATA_CFACK &&
  754. stype != IEEE80211_STYPE_DATA_CFPOLL &&
  755. stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
  756. if (stype != IEEE80211_STYPE_NULLFUNC)
  757. printk(KERN_DEBUG "%s: RX: dropped data frame "
  758. "with no data (type=0x%02x, subtype=0x%02x)\n",
  759. dev->name, type >> 2, stype >> 4);
  760. goto rx_dropped;
  761. }
  762. /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
  763. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  764. (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
  765. goto rx_dropped;
  766. hdr = (struct ieee80211_hdr_4addr *) skb->data;
  767. /* skb: hdr + (possibly fragmented) plaintext payload */
  768. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  769. (frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
  770. int flen;
  771. struct sk_buff *frag_skb =
  772. prism2_frag_cache_get(local, hdr);
  773. if (!frag_skb) {
  774. printk(KERN_DEBUG "%s: Rx cannot get skb from "
  775. "fragment cache (morefrag=%d seq=%u frag=%u)\n",
  776. dev->name, (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
  777. WLAN_GET_SEQ_SEQ(sc) >> 4, frag);
  778. goto rx_dropped;
  779. }
  780. flen = skb->len;
  781. if (frag != 0)
  782. flen -= hdrlen;
  783. if (frag_skb->tail + flen > frag_skb->end) {
  784. printk(KERN_WARNING "%s: host decrypted and "
  785. "reassembled frame did not fit skb\n",
  786. dev->name);
  787. prism2_frag_cache_invalidate(local, hdr);
  788. goto rx_dropped;
  789. }
  790. if (frag == 0) {
  791. /* copy first fragment (including full headers) into
  792. * beginning of the fragment cache skb */
  793. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  794. } else {
  795. /* append frame payload to the end of the fragment
  796. * cache skb */
  797. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  798. flen);
  799. }
  800. dev_kfree_skb(skb);
  801. skb = NULL;
  802. if (fc & IEEE80211_FCTL_MOREFRAGS) {
  803. /* more fragments expected - leave the skb in fragment
  804. * cache for now; it will be delivered to upper layers
  805. * after all fragments have been received */
  806. goto rx_exit;
  807. }
  808. /* this was the last fragment and the frame will be
  809. * delivered, so remove skb from fragment cache */
  810. skb = frag_skb;
  811. hdr = (struct ieee80211_hdr_4addr *) skb->data;
  812. prism2_frag_cache_invalidate(local, hdr);
  813. }
  814. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  815. * encrypted/authenticated */
  816. if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  817. hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
  818. goto rx_dropped;
  819. hdr = (struct ieee80211_hdr_4addr *) skb->data;
  820. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !local->open_wep) {
  821. if (local->ieee_802_1x &&
  822. hostap_is_eapol_frame(local, skb)) {
  823. /* pass unencrypted EAPOL frames even if encryption is
  824. * configured */
  825. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
  826. "unencrypted EAPOL frame\n", local->dev->name);
  827. } else {
  828. printk(KERN_DEBUG "%s: encryption configured, but RX "
  829. "frame not encrypted (SA=" MACSTR ")\n",
  830. local->dev->name, MAC2STR(hdr->addr2));
  831. goto rx_dropped;
  832. }
  833. }
  834. if (local->drop_unencrypted && !(fc & IEEE80211_FCTL_PROTECTED) &&
  835. !hostap_is_eapol_frame(local, skb)) {
  836. if (net_ratelimit()) {
  837. printk(KERN_DEBUG "%s: dropped unencrypted RX data "
  838. "frame from " MACSTR " (drop_unencrypted=1)\n",
  839. dev->name, MAC2STR(hdr->addr2));
  840. }
  841. goto rx_dropped;
  842. }
  843. /* skb: hdr + (possible reassembled) full plaintext payload */
  844. payload = skb->data + hdrlen;
  845. ethertype = (payload[6] << 8) | payload[7];
  846. /* If IEEE 802.1X is used, check whether the port is authorized to send
  847. * the received frame. */
  848. if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
  849. if (ethertype == ETH_P_PAE) {
  850. PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
  851. dev->name);
  852. if (local->hostapd && local->apdev) {
  853. /* Send IEEE 802.1X frames to the user
  854. * space daemon for processing */
  855. prism2_rx_80211(local->apdev, skb, rx_stats,
  856. PRISM2_RX_MGMT);
  857. local->apdevstats.rx_packets++;
  858. local->apdevstats.rx_bytes += skb->len;
  859. goto rx_exit;
  860. }
  861. } else if (!frame_authorized) {
  862. printk(KERN_DEBUG "%s: dropped frame from "
  863. "unauthorized port (IEEE 802.1X): "
  864. "ethertype=0x%04x\n",
  865. dev->name, ethertype);
  866. goto rx_dropped;
  867. }
  868. }
  869. /* convert hdr + possible LLC headers into Ethernet header */
  870. if (skb->len - hdrlen >= 8 &&
  871. ((memcmp(payload, rfc1042_header, 6) == 0 &&
  872. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  873. memcmp(payload, bridge_tunnel_header, 6) == 0)) {
  874. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  875. * replace EtherType */
  876. skb_pull(skb, hdrlen + 6);
  877. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  878. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  879. } else {
  880. u16 len;
  881. /* Leave Ethernet header part of hdr and full payload */
  882. skb_pull(skb, hdrlen);
  883. len = htons(skb->len);
  884. memcpy(skb_push(skb, 2), &len, 2);
  885. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  886. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  887. }
  888. if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  889. IEEE80211_FCTL_TODS) &&
  890. skb->len >= ETH_HLEN + ETH_ALEN) {
  891. /* Non-standard frame: get addr4 from its bogus location after
  892. * the payload */
  893. memcpy(skb->data + ETH_ALEN,
  894. skb->data + skb->len - ETH_ALEN, ETH_ALEN);
  895. skb_trim(skb, skb->len - ETH_ALEN);
  896. }
  897. stats->rx_packets++;
  898. stats->rx_bytes += skb->len;
  899. if (local->iw_mode == IW_MODE_MASTER && !wds &&
  900. local->ap->bridge_packets) {
  901. if (dst[0] & 0x01) {
  902. /* copy multicast frame both to the higher layers and
  903. * to the wireless media */
  904. local->ap->bridged_multicast++;
  905. skb2 = skb_clone(skb, GFP_ATOMIC);
  906. if (skb2 == NULL)
  907. printk(KERN_DEBUG "%s: skb_clone failed for "
  908. "multicast frame\n", dev->name);
  909. } else if (hostap_is_sta_authorized(local->ap, dst)) {
  910. /* send frame directly to the associated STA using
  911. * wireless media and not passing to higher layers */
  912. local->ap->bridged_unicast++;
  913. skb2 = skb;
  914. skb = NULL;
  915. }
  916. }
  917. if (skb2 != NULL) {
  918. /* send to wireless media */
  919. skb2->protocol = __constant_htons(ETH_P_802_3);
  920. skb2->mac.raw = skb2->nh.raw = skb2->data;
  921. /* skb2->nh.raw = skb2->data + ETH_HLEN; */
  922. skb2->dev = dev;
  923. dev_queue_xmit(skb2);
  924. }
  925. if (skb) {
  926. skb->protocol = eth_type_trans(skb, dev);
  927. memset(skb->cb, 0, sizeof(skb->cb));
  928. skb->dev = dev;
  929. netif_rx(skb);
  930. }
  931. rx_exit:
  932. if (sta)
  933. hostap_handle_sta_release(sta);
  934. return;
  935. rx_dropped:
  936. dev_kfree_skb(skb);
  937. stats->rx_dropped++;
  938. goto rx_exit;
  939. }
  940. EXPORT_SYMBOL(hostap_80211_rx);