ieee80211softmac_io.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Some parts based on code from net80211
  3. * Copyright (c) 2001 Atsushi Onoe
  4. * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include "ieee80211softmac_priv.h"
  35. /* Helper functions for inserting data into the frames */
  36. /*
  37. * Adds an ESSID element to the frame
  38. *
  39. */
  40. static u8 *
  41. ieee80211softmac_add_essid(u8 *dst, struct ieee80211softmac_essid *essid)
  42. {
  43. if (essid) {
  44. *dst++ = MFIE_TYPE_SSID;
  45. *dst++ = essid->len;
  46. memcpy(dst, essid->data, essid->len);
  47. return dst+essid->len;
  48. } else {
  49. *dst++ = MFIE_TYPE_SSID;
  50. *dst++ = 0;
  51. return dst;
  52. }
  53. }
  54. /* Adds Supported Rates and if required Extended Rates Information Element
  55. * to the frame, ASSUMES WE HAVE A SORTED LIST OF RATES */
  56. static u8 *
  57. ieee80211softmac_frame_add_rates(u8 *dst, const struct ieee80211softmac_ratesinfo *r)
  58. {
  59. int cck_len, ofdm_len;
  60. *dst++ = MFIE_TYPE_RATES;
  61. for(cck_len=0; ieee80211_is_cck_rate(r->rates[cck_len]) && (cck_len < r->count);cck_len++);
  62. if(cck_len > IEEE80211SOFTMAC_MAX_RATES_LEN)
  63. cck_len = IEEE80211SOFTMAC_MAX_RATES_LEN;
  64. *dst++ = cck_len;
  65. memcpy(dst, r->rates, cck_len);
  66. dst += cck_len;
  67. if(cck_len < r->count){
  68. for (ofdm_len=0; ieee80211_is_ofdm_rate(r->rates[ofdm_len + cck_len]) && (ofdm_len + cck_len < r->count); ofdm_len++);
  69. if (ofdm_len > 0) {
  70. if (ofdm_len > IEEE80211SOFTMAC_MAX_EX_RATES_LEN)
  71. ofdm_len = IEEE80211SOFTMAC_MAX_EX_RATES_LEN;
  72. *dst++ = MFIE_TYPE_RATES_EX;
  73. *dst++ = ofdm_len;
  74. memcpy(dst, r->rates + cck_len, ofdm_len);
  75. dst += ofdm_len;
  76. }
  77. }
  78. return dst;
  79. }
  80. /* Allocate a management frame */
  81. static u8 *
  82. ieee80211softmac_alloc_mgt(u32 size)
  83. {
  84. u8 * data;
  85. /* Add the header and FCS to the size */
  86. size = size + IEEE80211_3ADDR_LEN;
  87. if(size > IEEE80211_DATA_LEN)
  88. return NULL;
  89. /* Allocate the frame */
  90. data = kzalloc(size, GFP_ATOMIC);
  91. return data;
  92. }
  93. /*
  94. * Add a 2 Address Header
  95. */
  96. static void
  97. ieee80211softmac_hdr_2addr(struct ieee80211softmac_device *mac,
  98. struct ieee80211_hdr_2addr *header, u32 type, u8 *dest)
  99. {
  100. /* Fill in the frame control flags */
  101. header->frame_ctl = cpu_to_le16(type);
  102. /* Control packets always have WEP turned off */
  103. if(type > IEEE80211_STYPE_CFENDACK && type < IEEE80211_STYPE_PSPOLL)
  104. header->frame_ctl |= mac->ieee->sec.level ? cpu_to_le16(IEEE80211_FCTL_PROTECTED) : 0;
  105. /* Fill in the duration */
  106. header->duration_id = 0;
  107. /* FIXME: How do I find this?
  108. * calculate. But most drivers just fill in 0 (except if it's a station id of course) */
  109. /* Fill in the Destination Address */
  110. if(dest == NULL)
  111. memset(header->addr1, 0xFF, ETH_ALEN);
  112. else
  113. memcpy(header->addr1, dest, ETH_ALEN);
  114. /* Fill in the Source Address */
  115. memcpy(header->addr2, mac->ieee->dev->dev_addr, ETH_ALEN);
  116. }
  117. /* Add a 3 Address Header */
  118. static void
  119. ieee80211softmac_hdr_3addr(struct ieee80211softmac_device *mac,
  120. struct ieee80211_hdr_3addr *header, u32 type, u8 *dest, u8 *bssid)
  121. {
  122. /* This is common with 2addr, so use that instead */
  123. ieee80211softmac_hdr_2addr(mac, (struct ieee80211_hdr_2addr *)header, type, dest);
  124. /* Fill in the BSS ID */
  125. if(bssid == NULL)
  126. memset(header->addr3, 0xFF, ETH_ALEN);
  127. else
  128. memcpy(header->addr3, bssid, ETH_ALEN);
  129. /* Fill in the sequence # */
  130. /* FIXME: I need to add this to the softmac struct
  131. * shouldn't the sequence number be in ieee80211? */
  132. }
  133. static u16
  134. ieee80211softmac_capabilities(struct ieee80211softmac_device *mac,
  135. struct ieee80211softmac_network *net)
  136. {
  137. u16 capability = 0;
  138. /* ESS and IBSS bits are set according to the current mode */
  139. switch (mac->ieee->iw_mode) {
  140. case IW_MODE_INFRA:
  141. capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
  142. break;
  143. case IW_MODE_ADHOC:
  144. capability = cpu_to_le16(WLAN_CAPABILITY_IBSS);
  145. break;
  146. case IW_MODE_AUTO:
  147. capability = net->capabilities &
  148. (WLAN_CAPABILITY_ESS|WLAN_CAPABILITY_IBSS);
  149. break;
  150. default:
  151. /* bleh. we don't ever go to these modes */
  152. printk(KERN_ERR PFX "invalid iw_mode!\n");
  153. break;
  154. }
  155. /* CF Pollable / CF Poll Request */
  156. /* Needs to be implemented, for now, the 0's == not supported */
  157. /* Privacy Bit */
  158. capability |= mac->ieee->sec.level ?
  159. cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
  160. /* Short Preamble */
  161. /* Always supported: we probably won't ever be powering devices which
  162. * dont support this... */
  163. capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  164. /* PBCC */
  165. /* Not widely used */
  166. /* Channel Agility */
  167. /* Not widely used */
  168. /* Short Slot */
  169. /* Will be implemented later */
  170. /* DSSS-OFDM */
  171. /* Not widely used */
  172. return capability;
  173. }
  174. /*****************************************************************************
  175. * Create Management packets
  176. *****************************************************************************/
  177. /* Creates an association request packet */
  178. static u32
  179. ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt,
  180. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  181. {
  182. u8 *data;
  183. (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
  184. 2 + /* Capability Info */
  185. 2 + /* Listen Interval */
  186. /* SSID IE */
  187. 1 + 1 + IW_ESSID_MAX_SIZE +
  188. /* Rates IE */
  189. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  190. /* Extended Rates IE */
  191. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
  192. /* WPA IE if present */
  193. mac->wpa.IElen
  194. /* Other IE's? Optional?
  195. * Yeah, probably need an extra IE parameter -- lots of vendors like to
  196. * fill in their own IEs */
  197. );
  198. if (unlikely((*pkt) == NULL))
  199. return 0;
  200. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
  201. /* Fill in the capabilities */
  202. (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
  203. /* Fill in Listen Interval (?) */
  204. (*pkt)->listen_interval = cpu_to_le16(10);
  205. data = (u8 *)(*pkt)->info_element;
  206. /* Add SSID */
  207. data = ieee80211softmac_add_essid(data, &net->essid);
  208. /* Add Rates */
  209. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  210. /* Add WPA IE */
  211. if (mac->wpa.IElen && mac->wpa.IE) {
  212. memcpy(data, mac->wpa.IE, mac->wpa.IElen);
  213. data += mac->wpa.IElen;
  214. }
  215. /* Return the number of used bytes */
  216. return (data - (u8*)(*pkt));
  217. }
  218. /* Create a reassociation request packet */
  219. static u32
  220. ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt,
  221. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  222. {
  223. u8 *data;
  224. (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
  225. 2 + /* Capability Info */
  226. 2 + /* Listen Interval */
  227. ETH_ALEN + /* AP MAC */
  228. /* SSID IE */
  229. 1 + 1 + IW_ESSID_MAX_SIZE +
  230. /* Rates IE */
  231. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  232. /* Extended Rates IE */
  233. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  234. /* Other IE's? */
  235. );
  236. if (unlikely((*pkt) == NULL))
  237. return 0;
  238. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
  239. /* Fill in the capabilities */
  240. (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
  241. /* Fill in Listen Interval (?) */
  242. (*pkt)->listen_interval = cpu_to_le16(10);
  243. /* Fill in the current AP MAC */
  244. memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
  245. data = (u8 *)(*pkt)->info_element;
  246. /* Add SSID */
  247. data = ieee80211softmac_add_essid(data, &net->essid);
  248. /* Add Rates */
  249. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  250. /* Return packet size */
  251. return (data - (u8 *)(*pkt));
  252. }
  253. /* Create an authentication packet */
  254. static u32
  255. ieee80211softmac_auth(struct ieee80211_auth **pkt,
  256. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  257. u16 transaction, u16 status, int *encrypt_mpdu)
  258. {
  259. u8 *data;
  260. int auth_mode = mac->ieee->sec.auth_mode;
  261. int is_shared_response = (auth_mode == WLAN_AUTH_SHARED_KEY
  262. && transaction == IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE);
  263. /* Allocate Packet */
  264. (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
  265. 2 + /* Auth Algorithm */
  266. 2 + /* Auth Transaction Seq */
  267. 2 + /* Status Code */
  268. /* Challenge Text IE */
  269. (is_shared_response ? 1 + 1 + net->challenge_len : 0)
  270. );
  271. if (unlikely((*pkt) == NULL))
  272. return 0;
  273. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
  274. /* Algorithm */
  275. (*pkt)->algorithm = cpu_to_le16(auth_mode);
  276. /* Transaction */
  277. (*pkt)->transaction = cpu_to_le16(transaction);
  278. /* Status */
  279. (*pkt)->status = cpu_to_le16(status);
  280. data = (u8 *)(*pkt)->info_element;
  281. /* Challenge Text */
  282. if (is_shared_response) {
  283. *data = MFIE_TYPE_CHALLENGE;
  284. data++;
  285. /* Copy the challenge in */
  286. *data = net->challenge_len;
  287. data++;
  288. memcpy(data, net->challenge, net->challenge_len);
  289. data += net->challenge_len;
  290. /* Make sure this frame gets encrypted with the shared key */
  291. *encrypt_mpdu = 1;
  292. } else
  293. *encrypt_mpdu = 0;
  294. /* Return the packet size */
  295. return (data - (u8 *)(*pkt));
  296. }
  297. /* Create a disassocation or deauthentication packet */
  298. static u32
  299. ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
  300. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  301. u16 type, u16 reason)
  302. {
  303. /* Allocate Packet */
  304. (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
  305. if (unlikely((*pkt) == NULL))
  306. return 0;
  307. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
  308. /* Reason */
  309. (*pkt)->reason = cpu_to_le16(reason);
  310. /* Return the packet size */
  311. return (2 + IEEE80211_3ADDR_LEN);
  312. }
  313. /* Create a probe request packet */
  314. static u32
  315. ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
  316. struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
  317. {
  318. u8 *data;
  319. /* Allocate Packet */
  320. (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
  321. /* SSID of requested network */
  322. 1 + 1 + IW_ESSID_MAX_SIZE +
  323. /* Rates IE */
  324. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  325. /* Extended Rates IE */
  326. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  327. );
  328. if (unlikely((*pkt) == NULL))
  329. return 0;
  330. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
  331. data = (u8 *)(*pkt)->info_element;
  332. /* Add ESSID (can be NULL) */
  333. data = ieee80211softmac_add_essid(data, essid);
  334. /* Add Rates */
  335. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  336. /* Return packet size */
  337. return (data - (u8 *)(*pkt));
  338. }
  339. /* Create a probe response packet */
  340. /* FIXME: Not complete */
  341. static u32
  342. ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
  343. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  344. {
  345. u8 *data;
  346. /* Allocate Packet */
  347. (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
  348. 8 + /* Timestamp */
  349. 2 + /* Beacon Interval */
  350. 2 + /* Capability Info */
  351. /* SSID IE */
  352. 1 + 1 + IW_ESSID_MAX_SIZE +
  353. 7 + /* FH Parameter Set */
  354. 2 + /* DS Parameter Set */
  355. 8 + /* CF Parameter Set */
  356. 4 /* IBSS Parameter Set */
  357. );
  358. if (unlikely((*pkt) == NULL))
  359. return 0;
  360. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
  361. data = (u8 *)(*pkt)->info_element;
  362. /* Return the packet size */
  363. return (data - (u8 *)(*pkt));
  364. }
  365. /* Sends a manangement packet
  366. * FIXME: document the use of the arg parameter
  367. * for _AUTH: (transaction #) | (status << 16)
  368. */
  369. int
  370. ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
  371. void *ptrarg, u32 type, u32 arg)
  372. {
  373. void *pkt = NULL;
  374. u32 pkt_size = 0;
  375. int encrypt_mpdu = 0;
  376. switch(type) {
  377. case IEEE80211_STYPE_ASSOC_REQ:
  378. pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  379. break;
  380. case IEEE80211_STYPE_REASSOC_REQ:
  381. pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  382. break;
  383. case IEEE80211_STYPE_AUTH:
  384. pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16), &encrypt_mpdu);
  385. break;
  386. case IEEE80211_STYPE_DISASSOC:
  387. case IEEE80211_STYPE_DEAUTH:
  388. pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
  389. break;
  390. case IEEE80211_STYPE_PROBE_REQ:
  391. pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
  392. break;
  393. case IEEE80211_STYPE_PROBE_RESP:
  394. pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  395. break;
  396. default:
  397. printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
  398. return -EINVAL;
  399. };
  400. if(pkt_size == 0 || pkt == NULL) {
  401. printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
  402. return -ENOMEM;
  403. }
  404. /* Send the packet to the ieee80211 layer for tx */
  405. /* we defined softmac->mgmt_xmit for this. Should we keep it
  406. * as it is (that means we'd need to wrap this into a txb),
  407. * modify the prototype (so it matches this function),
  408. * or get rid of it alltogether?
  409. * Does this work for you now?
  410. */
  411. ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt,
  412. IEEE80211_3ADDR_LEN, pkt_size, encrypt_mpdu);
  413. kfree(pkt);
  414. return 0;
  415. }
  416. /* Beacon handling */
  417. int ieee80211softmac_handle_beacon(struct net_device *dev,
  418. struct ieee80211_beacon *beacon,
  419. struct ieee80211_network *network)
  420. {
  421. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  422. /* This might race, but we don't really care and it's not worth
  423. * adding heavyweight locking in this fastpath.
  424. */
  425. if (mac->associnfo.associated) {
  426. if (memcmp(network->bssid, mac->associnfo.bssid, ETH_ALEN) == 0)
  427. ieee80211softmac_process_erp(mac, network->erp_value);
  428. }
  429. return 0;
  430. }