ieee80211softmac_io.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 Listen Interval (?) */
  203. (*pkt)->listen_interval = cpu_to_le16(10);
  204. data = (u8 *)(*pkt)->info_element;
  205. /* Add SSID */
  206. data = ieee80211softmac_add_essid(data, &net->essid);
  207. /* Add Rates */
  208. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  209. /* Add WPA IE */
  210. if (mac->wpa.IElen && mac->wpa.IE) {
  211. memcpy(data, mac->wpa.IE, mac->wpa.IElen);
  212. data += mac->wpa.IElen;
  213. }
  214. /* Return the number of used bytes */
  215. return (data - (u8*)(*pkt));
  216. }
  217. /* Create a reassociation request packet */
  218. static u32
  219. ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt,
  220. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  221. {
  222. u8 *data;
  223. (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
  224. 2 + /* Capability Info */
  225. 2 + /* Listen Interval */
  226. ETH_ALEN + /* AP MAC */
  227. /* SSID IE */
  228. 1 + 1 + IW_ESSID_MAX_SIZE +
  229. /* Rates IE */
  230. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  231. /* Extended Rates IE */
  232. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  233. /* Other IE's? */
  234. );
  235. if (unlikely((*pkt) == NULL))
  236. return 0;
  237. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
  238. /* Fill in the capabilities */
  239. (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
  240. /* Fill in Listen Interval (?) */
  241. (*pkt)->listen_interval = cpu_to_le16(10);
  242. /* Fill in the current AP MAC */
  243. memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
  244. data = (u8 *)(*pkt)->info_element;
  245. /* Add SSID */
  246. data = ieee80211softmac_add_essid(data, &net->essid);
  247. /* Add Rates */
  248. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  249. /* Return packet size */
  250. return (data - (u8 *)(*pkt));
  251. }
  252. /* Create an authentication packet */
  253. static u32
  254. ieee80211softmac_auth(struct ieee80211_auth **pkt,
  255. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  256. u16 transaction, u16 status, int *encrypt_mpdu)
  257. {
  258. u8 *data;
  259. int auth_mode = mac->ieee->sec.auth_mode;
  260. int is_shared_response = (auth_mode == WLAN_AUTH_SHARED_KEY
  261. && transaction == IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE);
  262. /* Allocate Packet */
  263. (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
  264. 2 + /* Auth Algorithm */
  265. 2 + /* Auth Transaction Seq */
  266. 2 + /* Status Code */
  267. /* Challenge Text IE */
  268. is_shared_response ? 0 : 1 + 1 + net->challenge_len
  269. );
  270. if (unlikely((*pkt) == NULL))
  271. return 0;
  272. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
  273. /* Algorithm */
  274. (*pkt)->algorithm = cpu_to_le16(auth_mode);
  275. /* Transaction */
  276. (*pkt)->transaction = cpu_to_le16(transaction);
  277. /* Status */
  278. (*pkt)->status = cpu_to_le16(status);
  279. data = (u8 *)(*pkt)->info_element;
  280. /* Challenge Text */
  281. if (is_shared_response) {
  282. *data = MFIE_TYPE_CHALLENGE;
  283. data++;
  284. /* Copy the challenge in */
  285. *data = net->challenge_len;
  286. data++;
  287. memcpy(data, net->challenge, net->challenge_len);
  288. data += net->challenge_len;
  289. /* Make sure this frame gets encrypted with the shared key */
  290. *encrypt_mpdu = 1;
  291. } else
  292. *encrypt_mpdu = 0;
  293. /* Return the packet size */
  294. return (data - (u8 *)(*pkt));
  295. }
  296. /* Create a disassocation or deauthentication packet */
  297. static u32
  298. ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
  299. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  300. u16 type, u16 reason)
  301. {
  302. /* Allocate Packet */
  303. (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
  304. if (unlikely((*pkt) == NULL))
  305. return 0;
  306. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
  307. /* Reason */
  308. (*pkt)->reason = cpu_to_le16(reason);
  309. /* Return the packet size */
  310. return (2 + IEEE80211_3ADDR_LEN);
  311. }
  312. /* Create a probe request packet */
  313. static u32
  314. ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
  315. struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
  316. {
  317. u8 *data;
  318. /* Allocate Packet */
  319. (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
  320. /* SSID of requested network */
  321. 1 + 1 + IW_ESSID_MAX_SIZE +
  322. /* Rates IE */
  323. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  324. /* Extended Rates IE */
  325. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  326. );
  327. if (unlikely((*pkt) == NULL))
  328. return 0;
  329. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
  330. data = (u8 *)(*pkt)->info_element;
  331. /* Add ESSID (can be NULL) */
  332. data = ieee80211softmac_add_essid(data, essid);
  333. /* Add Rates */
  334. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  335. /* Return packet size */
  336. return (data - (u8 *)(*pkt));
  337. }
  338. /* Create a probe response packet */
  339. /* FIXME: Not complete */
  340. static u32
  341. ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
  342. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  343. {
  344. u8 *data;
  345. /* Allocate Packet */
  346. (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
  347. 8 + /* Timestamp */
  348. 2 + /* Beacon Interval */
  349. 2 + /* Capability Info */
  350. /* SSID IE */
  351. 1 + 1 + IW_ESSID_MAX_SIZE +
  352. 7 + /* FH Parameter Set */
  353. 2 + /* DS Parameter Set */
  354. 8 + /* CF Parameter Set */
  355. 4 /* IBSS Parameter Set */
  356. );
  357. if (unlikely((*pkt) == NULL))
  358. return 0;
  359. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
  360. data = (u8 *)(*pkt)->info_element;
  361. /* Return the packet size */
  362. return (data - (u8 *)(*pkt));
  363. }
  364. /* Sends a manangement packet
  365. * FIXME: document the use of the arg parameter
  366. * for _AUTH: (transaction #) | (status << 16)
  367. */
  368. int
  369. ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
  370. void *ptrarg, u32 type, u32 arg)
  371. {
  372. void *pkt = NULL;
  373. u32 pkt_size = 0;
  374. int encrypt_mpdu = 0;
  375. switch(type) {
  376. case IEEE80211_STYPE_ASSOC_REQ:
  377. pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  378. break;
  379. case IEEE80211_STYPE_REASSOC_REQ:
  380. pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  381. break;
  382. case IEEE80211_STYPE_AUTH:
  383. pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16), &encrypt_mpdu);
  384. break;
  385. case IEEE80211_STYPE_DISASSOC:
  386. case IEEE80211_STYPE_DEAUTH:
  387. pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
  388. break;
  389. case IEEE80211_STYPE_PROBE_REQ:
  390. pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
  391. break;
  392. case IEEE80211_STYPE_PROBE_RESP:
  393. pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  394. break;
  395. default:
  396. printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
  397. return -EINVAL;
  398. };
  399. if(pkt_size == 0 || pkt == NULL) {
  400. printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
  401. return -ENOMEM;
  402. }
  403. /* Send the packet to the ieee80211 layer for tx */
  404. /* we defined softmac->mgmt_xmit for this. Should we keep it
  405. * as it is (that means we'd need to wrap this into a txb),
  406. * modify the prototype (so it matches this function),
  407. * or get rid of it alltogether?
  408. * Does this work for you now?
  409. */
  410. ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt,
  411. IEEE80211_3ADDR_LEN, pkt_size, encrypt_mpdu);
  412. kfree(pkt);
  413. return 0;
  414. }