ieee80211softmac_io.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 = kmalloc(size, GFP_ATOMIC);
  91. memset(data, 0, size);
  92. return data;
  93. }
  94. /*
  95. * Add a 2 Address Header
  96. */
  97. static void
  98. ieee80211softmac_hdr_2addr(struct ieee80211softmac_device *mac,
  99. struct ieee80211_hdr_2addr *header, u32 type, u8 *dest)
  100. {
  101. /* Fill in the frame control flags */
  102. header->frame_ctl = cpu_to_le16(type);
  103. /* Control packets always have WEP turned off */
  104. if(type > IEEE80211_STYPE_CFENDACK && type < IEEE80211_STYPE_PSPOLL)
  105. header->frame_ctl |= mac->ieee->sec.level ? cpu_to_le16(IEEE80211_FCTL_PROTECTED) : 0;
  106. /* Fill in the duration */
  107. header->duration_id = 0;
  108. /* FIXME: How do I find this?
  109. * calculate. But most drivers just fill in 0 (except if it's a station id of course) */
  110. /* Fill in the Destination Address */
  111. if(dest == NULL)
  112. memset(header->addr1, 0xFF, ETH_ALEN);
  113. else
  114. memcpy(header->addr1, dest, ETH_ALEN);
  115. /* Fill in the Source Address */
  116. memcpy(header->addr2, mac->ieee->dev->dev_addr, ETH_ALEN);
  117. }
  118. /* Add a 3 Address Header */
  119. static void
  120. ieee80211softmac_hdr_3addr(struct ieee80211softmac_device *mac,
  121. struct ieee80211_hdr_3addr *header, u32 type, u8 *dest, u8 *bssid)
  122. {
  123. /* This is common with 2addr, so use that instead */
  124. ieee80211softmac_hdr_2addr(mac, (struct ieee80211_hdr_2addr *)header, type, dest);
  125. /* Fill in the BSS ID */
  126. if(bssid == NULL)
  127. memset(header->addr3, 0xFF, ETH_ALEN);
  128. else
  129. memcpy(header->addr3, bssid, ETH_ALEN);
  130. /* Fill in the sequence # */
  131. /* FIXME: I need to add this to the softmac struct
  132. * shouldn't the sequence number be in ieee80211? */
  133. }
  134. static u16
  135. ieee80211softmac_capabilities(struct ieee80211softmac_device *mac,
  136. struct ieee80211softmac_network *net)
  137. {
  138. u16 capability = 0;
  139. /* ESS and IBSS bits are set according to the current mode */
  140. switch (mac->ieee->iw_mode) {
  141. case IW_MODE_INFRA:
  142. capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
  143. break;
  144. case IW_MODE_ADHOC:
  145. capability = cpu_to_le16(WLAN_CAPABILITY_IBSS);
  146. break;
  147. case IW_MODE_AUTO:
  148. capability = net->capabilities &
  149. (WLAN_CAPABILITY_ESS|WLAN_CAPABILITY_IBSS);
  150. break;
  151. default:
  152. /* bleh. we don't ever go to these modes */
  153. printk(KERN_ERR PFX "invalid iw_mode!\n");
  154. break;
  155. }
  156. /* CF Pollable / CF Poll Request */
  157. /* Needs to be implemented, for now, the 0's == not supported */
  158. /* Privacy Bit */
  159. capability |= mac->ieee->sec.level ?
  160. cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
  161. /* Short Preamble */
  162. /* Always supported: we probably won't ever be powering devices which
  163. * dont support this... */
  164. capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  165. /* PBCC */
  166. /* Not widely used */
  167. /* Channel Agility */
  168. /* Not widely used */
  169. /* Short Slot */
  170. /* Will be implemented later */
  171. /* DSSS-OFDM */
  172. /* Not widely used */
  173. return capability;
  174. }
  175. /*****************************************************************************
  176. * Create Management packets
  177. *****************************************************************************/
  178. /* Creates an association request packet */
  179. static u32
  180. ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt,
  181. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  182. {
  183. u8 *data;
  184. (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
  185. 2 + /* Capability Info */
  186. 2 + /* Listen Interval */
  187. /* SSID IE */
  188. 1 + 1 + IW_ESSID_MAX_SIZE +
  189. /* Rates IE */
  190. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  191. /* Extended Rates IE */
  192. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
  193. /* WPA IE if present */
  194. mac->wpa.IElen
  195. /* Other IE's? Optional?
  196. * Yeah, probably need an extra IE parameter -- lots of vendors like to
  197. * fill in their own IEs */
  198. );
  199. if (unlikely((*pkt) == NULL))
  200. return 0;
  201. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
  202. /* Fill in the capabilities */
  203. (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
  204. /* Fill in Listen Interval (?) */
  205. (*pkt)->listen_interval = cpu_to_le16(10);
  206. data = (u8 *)(*pkt)->info_element;
  207. /* Add SSID */
  208. data = ieee80211softmac_add_essid(data, &net->essid);
  209. /* Add Rates */
  210. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  211. /* Add WPA IE */
  212. if (mac->wpa.IElen && mac->wpa.IE) {
  213. memcpy(data, mac->wpa.IE, mac->wpa.IElen);
  214. data += mac->wpa.IElen;
  215. }
  216. /* Return the number of used bytes */
  217. return (data - (u8*)(*pkt));
  218. }
  219. /* Create a reassociation request packet */
  220. static u32
  221. ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt,
  222. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  223. {
  224. u8 *data;
  225. (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
  226. 2 + /* Capability Info */
  227. 2 + /* Listen Interval */
  228. ETH_ALEN + /* AP MAC */
  229. /* SSID IE */
  230. 1 + 1 + IW_ESSID_MAX_SIZE +
  231. /* Rates IE */
  232. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  233. /* Extended Rates IE */
  234. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  235. /* Other IE's? */
  236. );
  237. if (unlikely((*pkt) == NULL))
  238. return 0;
  239. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
  240. /* Fill in the capabilities */
  241. (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
  242. /* Fill in Listen Interval (?) */
  243. (*pkt)->listen_interval = cpu_to_le16(10);
  244. /* Fill in the current AP MAC */
  245. memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
  246. data = (u8 *)(*pkt)->info_element;
  247. /* Add SSID */
  248. data = ieee80211softmac_add_essid(data, &net->essid);
  249. /* Add Rates */
  250. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  251. /* Return packet size */
  252. return (data - (u8 *)(*pkt));
  253. }
  254. /* Create an authentication packet */
  255. static u32
  256. ieee80211softmac_auth(struct ieee80211_auth **pkt,
  257. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  258. u16 transaction, u16 status, int *encrypt_mpdu)
  259. {
  260. u8 *data;
  261. int auth_mode = mac->ieee->sec.auth_mode;
  262. int is_shared_response = (auth_mode == WLAN_AUTH_SHARED_KEY
  263. && transaction == IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE);
  264. /* Allocate Packet */
  265. (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
  266. 2 + /* Auth Algorithm */
  267. 2 + /* Auth Transaction Seq */
  268. 2 + /* Status Code */
  269. /* Challenge Text IE */
  270. is_shared_response ? 0 : 1 + 1 + net->challenge_len
  271. );
  272. if (unlikely((*pkt) == NULL))
  273. return 0;
  274. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
  275. /* Algorithm */
  276. (*pkt)->algorithm = cpu_to_le16(auth_mode);
  277. /* Transaction */
  278. (*pkt)->transaction = cpu_to_le16(transaction);
  279. /* Status */
  280. (*pkt)->status = cpu_to_le16(status);
  281. data = (u8 *)(*pkt)->info_element;
  282. /* Challenge Text */
  283. if (is_shared_response) {
  284. *data = MFIE_TYPE_CHALLENGE;
  285. data++;
  286. /* Copy the challenge in */
  287. *data = net->challenge_len;
  288. data++;
  289. memcpy(data, net->challenge, net->challenge_len);
  290. data += net->challenge_len;
  291. /* Make sure this frame gets encrypted with the shared key */
  292. *encrypt_mpdu = 1;
  293. } else
  294. *encrypt_mpdu = 0;
  295. /* Return the packet size */
  296. return (data - (u8 *)(*pkt));
  297. }
  298. /* Create a disassocation or deauthentication packet */
  299. static u32
  300. ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
  301. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  302. u16 type, u16 reason)
  303. {
  304. /* Allocate Packet */
  305. (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
  306. if (unlikely((*pkt) == NULL))
  307. return 0;
  308. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
  309. /* Reason */
  310. (*pkt)->reason = cpu_to_le16(reason);
  311. /* Return the packet size */
  312. return (2 + IEEE80211_3ADDR_LEN);
  313. }
  314. /* Create a probe request packet */
  315. static u32
  316. ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
  317. struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
  318. {
  319. u8 *data;
  320. /* Allocate Packet */
  321. (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
  322. /* SSID of requested network */
  323. 1 + 1 + IW_ESSID_MAX_SIZE +
  324. /* Rates IE */
  325. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  326. /* Extended Rates IE */
  327. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  328. );
  329. if (unlikely((*pkt) == NULL))
  330. return 0;
  331. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
  332. data = (u8 *)(*pkt)->info_element;
  333. /* Add ESSID (can be NULL) */
  334. data = ieee80211softmac_add_essid(data, essid);
  335. /* Add Rates */
  336. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  337. /* Return packet size */
  338. return (data - (u8 *)(*pkt));
  339. }
  340. /* Create a probe response packet */
  341. /* FIXME: Not complete */
  342. static u32
  343. ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
  344. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  345. {
  346. u8 *data;
  347. /* Allocate Packet */
  348. (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
  349. 8 + /* Timestamp */
  350. 2 + /* Beacon Interval */
  351. 2 + /* Capability Info */
  352. /* SSID IE */
  353. 1 + 1 + IW_ESSID_MAX_SIZE +
  354. 7 + /* FH Parameter Set */
  355. 2 + /* DS Parameter Set */
  356. 8 + /* CF Parameter Set */
  357. 4 /* IBSS Parameter Set */
  358. );
  359. if (unlikely((*pkt) == NULL))
  360. return 0;
  361. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
  362. data = (u8 *)(*pkt)->info_element;
  363. /* Return the packet size */
  364. return (data - (u8 *)(*pkt));
  365. }
  366. /* Sends a manangement packet
  367. * FIXME: document the use of the arg parameter
  368. * for _AUTH: (transaction #) | (status << 16)
  369. */
  370. int
  371. ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
  372. void *ptrarg, u32 type, u32 arg)
  373. {
  374. void *pkt = NULL;
  375. u32 pkt_size = 0;
  376. int encrypt_mpdu = 0;
  377. switch(type) {
  378. case IEEE80211_STYPE_ASSOC_REQ:
  379. pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  380. break;
  381. case IEEE80211_STYPE_REASSOC_REQ:
  382. pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  383. break;
  384. case IEEE80211_STYPE_AUTH:
  385. pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16), &encrypt_mpdu);
  386. break;
  387. case IEEE80211_STYPE_DISASSOC:
  388. case IEEE80211_STYPE_DEAUTH:
  389. pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
  390. break;
  391. case IEEE80211_STYPE_PROBE_REQ:
  392. pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
  393. break;
  394. case IEEE80211_STYPE_PROBE_RESP:
  395. pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  396. break;
  397. default:
  398. printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
  399. return -EINVAL;
  400. };
  401. if(pkt_size == 0 || pkt == NULL) {
  402. printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
  403. return -ENOMEM;
  404. }
  405. /* Send the packet to the ieee80211 layer for tx */
  406. /* we defined softmac->mgmt_xmit for this. Should we keep it
  407. * as it is (that means we'd need to wrap this into a txb),
  408. * modify the prototype (so it matches this function),
  409. * or get rid of it alltogether?
  410. * Does this work for you now?
  411. */
  412. ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt,
  413. IEEE80211_3ADDR_LEN, pkt_size, encrypt_mpdu);
  414. kfree(pkt);
  415. return 0;
  416. }