ieee80211softmac_io.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /*****************************************************************************
  135. * Create Management packets
  136. *****************************************************************************/
  137. /* Creates an association request packet */
  138. static u32
  139. ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt,
  140. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  141. {
  142. u8 *data;
  143. (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
  144. 2 + /* Capability Info */
  145. 2 + /* Listen Interval */
  146. /* SSID IE */
  147. 1 + 1 + IW_ESSID_MAX_SIZE +
  148. /* Rates IE */
  149. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  150. /* Extended Rates IE */
  151. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
  152. /* WPA IE if present */
  153. mac->wpa.IElen
  154. /* Other IE's? Optional?
  155. * Yeah, probably need an extra IE parameter -- lots of vendors like to
  156. * fill in their own IEs */
  157. );
  158. if (unlikely((*pkt) == NULL))
  159. return 0;
  160. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
  161. /* Fill in capability Info */
  162. (*pkt)->capability = (mac->ieee->iw_mode == IW_MODE_MASTER) || (mac->ieee->iw_mode == IW_MODE_INFRA) ?
  163. cpu_to_le16(WLAN_CAPABILITY_ESS) :
  164. cpu_to_le16(WLAN_CAPABILITY_IBSS);
  165. /* Need to add this
  166. (*pkt)->capability |= mac->ieee->short_slot ?
  167. cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME) : 0;
  168. */
  169. (*pkt)->capability |= mac->ieee->sec.level ? cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
  170. /* Fill in Listen Interval (?) */
  171. (*pkt)->listen_interval = cpu_to_le16(10);
  172. data = (u8 *)(*pkt)->info_element;
  173. /* Add SSID */
  174. data = ieee80211softmac_add_essid(data, &net->essid);
  175. /* Add Rates */
  176. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  177. /* Add WPA IE */
  178. if (mac->wpa.IElen && mac->wpa.IE) {
  179. memcpy(data, mac->wpa.IE, mac->wpa.IElen);
  180. data += mac->wpa.IElen;
  181. }
  182. /* Return the number of used bytes */
  183. return (data - (u8*)(*pkt));
  184. }
  185. /* Create a reassociation request packet */
  186. static u32
  187. ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt,
  188. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  189. {
  190. u8 *data;
  191. (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
  192. 2 + /* Capability Info */
  193. 2 + /* Listen Interval */
  194. ETH_ALEN + /* AP MAC */
  195. /* SSID IE */
  196. 1 + 1 + IW_ESSID_MAX_SIZE +
  197. /* Rates IE */
  198. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  199. /* Extended Rates IE */
  200. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  201. /* Other IE's? */
  202. );
  203. if (unlikely((*pkt) == NULL))
  204. return 0;
  205. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
  206. /* Fill in capability Info */
  207. (*pkt)->capability = mac->ieee->iw_mode == IW_MODE_MASTER ?
  208. cpu_to_le16(WLAN_CAPABILITY_ESS) :
  209. cpu_to_le16(WLAN_CAPABILITY_IBSS);
  210. /*
  211. (*pkt)->capability |= mac->ieee->short_slot ?
  212. cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME) : 0;
  213. */
  214. (*pkt)->capability |= mac->ieee->sec.level ?
  215. cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
  216. /* Fill in Listen Interval (?) */
  217. (*pkt)->listen_interval = cpu_to_le16(10);
  218. /* Fill in the current AP MAC */
  219. memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
  220. data = (u8 *)(*pkt)->info_element;
  221. /* Add SSID */
  222. data = ieee80211softmac_add_essid(data, &net->essid);
  223. /* Add Rates */
  224. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  225. /* Return packet size */
  226. return (data - (u8 *)(*pkt));
  227. }
  228. /* Create an authentication packet */
  229. static u32
  230. ieee80211softmac_auth(struct ieee80211_auth **pkt,
  231. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  232. u16 transaction, u16 status)
  233. {
  234. u8 *data;
  235. /* Allocate Packet */
  236. (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
  237. 2 + /* Auth Algorithm */
  238. 2 + /* Auth Transaction Seq */
  239. 2 + /* Status Code */
  240. /* Challenge Text IE */
  241. mac->ieee->open_wep ? 0 :
  242. 1 + 1 + WLAN_AUTH_CHALLENGE_LEN
  243. );
  244. if (unlikely((*pkt) == NULL))
  245. return 0;
  246. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
  247. /* Algorithm */
  248. (*pkt)->algorithm = mac->ieee->open_wep ?
  249. cpu_to_le16(WLAN_AUTH_OPEN) :
  250. cpu_to_le16(WLAN_AUTH_SHARED_KEY);
  251. /* Transaction */
  252. (*pkt)->transaction = cpu_to_le16(transaction);
  253. /* Status */
  254. (*pkt)->status = cpu_to_le16(status);
  255. data = (u8 *)(*pkt)->info_element;
  256. /* Challenge Text */
  257. if(!mac->ieee->open_wep){
  258. *data = MFIE_TYPE_CHALLENGE;
  259. data++;
  260. /* Copy the challenge in */
  261. // *data = challenge length
  262. // data += sizeof(u16);
  263. // memcpy(data, challenge, challenge length);
  264. // data += challenge length;
  265. /* Add the full size to the packet length */
  266. }
  267. /* Return the packet size */
  268. return (data - (u8 *)(*pkt));
  269. }
  270. /* Create a disassocation or deauthentication packet */
  271. static u32
  272. ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
  273. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  274. u16 type, u16 reason)
  275. {
  276. /* Allocate Packet */
  277. (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
  278. if (unlikely((*pkt) == NULL))
  279. return 0;
  280. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
  281. /* Reason */
  282. (*pkt)->reason = cpu_to_le16(reason);
  283. /* Return the packet size */
  284. return (2 + IEEE80211_3ADDR_LEN);
  285. }
  286. /* Create a probe request packet */
  287. static u32
  288. ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
  289. struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
  290. {
  291. u8 *data;
  292. /* Allocate Packet */
  293. (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
  294. /* SSID of requested network */
  295. 1 + 1 + IW_ESSID_MAX_SIZE +
  296. /* Rates IE */
  297. 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
  298. /* Extended Rates IE */
  299. 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
  300. );
  301. if (unlikely((*pkt) == NULL))
  302. return 0;
  303. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
  304. data = (u8 *)(*pkt)->info_element;
  305. /* Add ESSID (can be NULL) */
  306. data = ieee80211softmac_add_essid(data, essid);
  307. /* Add Rates */
  308. data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
  309. /* Return packet size */
  310. return (data - (u8 *)(*pkt));
  311. }
  312. /* Create a probe response packet */
  313. /* FIXME: Not complete */
  314. static u32
  315. ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
  316. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
  317. {
  318. u8 *data;
  319. /* Allocate Packet */
  320. (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
  321. 8 + /* Timestamp */
  322. 2 + /* Beacon Interval */
  323. 2 + /* Capability Info */
  324. /* SSID IE */
  325. 1 + 1 + IW_ESSID_MAX_SIZE +
  326. 7 + /* FH Parameter Set */
  327. 2 + /* DS Parameter Set */
  328. 8 + /* CF Parameter Set */
  329. 4 /* IBSS Parameter Set */
  330. );
  331. if (unlikely((*pkt) == NULL))
  332. return 0;
  333. ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
  334. data = (u8 *)(*pkt)->info_element;
  335. /* Return the packet size */
  336. return (data - (u8 *)(*pkt));
  337. }
  338. /* Sends a manangement packet
  339. * FIXME: document the use of the arg parameter
  340. * for _AUTH: (transaction #) | (status << 16)
  341. */
  342. int
  343. ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
  344. void *ptrarg, u32 type, u32 arg)
  345. {
  346. void *pkt = NULL;
  347. u32 pkt_size = 0;
  348. switch(type) {
  349. case IEEE80211_STYPE_ASSOC_REQ:
  350. pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  351. break;
  352. case IEEE80211_STYPE_REASSOC_REQ:
  353. pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  354. break;
  355. case IEEE80211_STYPE_AUTH:
  356. pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16));
  357. break;
  358. case IEEE80211_STYPE_DISASSOC:
  359. case IEEE80211_STYPE_DEAUTH:
  360. pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
  361. break;
  362. case IEEE80211_STYPE_PROBE_REQ:
  363. pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
  364. break;
  365. case IEEE80211_STYPE_PROBE_RESP:
  366. pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
  367. break;
  368. default:
  369. printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
  370. return -EINVAL;
  371. };
  372. if(pkt_size == 0 || pkt == NULL) {
  373. printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
  374. return -ENOMEM;
  375. }
  376. /* Send the packet to the ieee80211 layer for tx */
  377. /* we defined softmac->mgmt_xmit for this. Should we keep it
  378. * as it is (that means we'd need to wrap this into a txb),
  379. * modify the prototype (so it matches this function),
  380. * or get rid of it alltogether?
  381. * Does this work for you now?
  382. */
  383. ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt, pkt_size);
  384. kfree(pkt);
  385. return 0;
  386. }
  387. /* Create an rts/cts frame */
  388. static u32
  389. ieee80211softmac_rts_cts(struct ieee80211_hdr_2addr **pkt,
  390. struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
  391. u32 type)
  392. {
  393. /* Allocate Packet */
  394. (*pkt) = kmalloc(IEEE80211_2ADDR_LEN, GFP_ATOMIC);
  395. memset(*pkt, 0, IEEE80211_2ADDR_LEN);
  396. if((*pkt) == NULL)
  397. return 0;
  398. ieee80211softmac_hdr_2addr(mac, (*pkt), type, net->bssid);
  399. return IEEE80211_2ADDR_LEN;
  400. }
  401. /* Sends a control packet */
  402. static int
  403. ieee80211softmac_send_ctl_frame(struct ieee80211softmac_device *mac,
  404. struct ieee80211softmac_network *net, u32 type, u32 arg)
  405. {
  406. void *pkt = NULL;
  407. u32 pkt_size = 0;
  408. switch(type) {
  409. case IEEE80211_STYPE_RTS:
  410. case IEEE80211_STYPE_CTS:
  411. pkt_size = ieee80211softmac_rts_cts((struct ieee80211_hdr_2addr **)(&pkt), mac, net, type);
  412. break;
  413. default:
  414. printkl(KERN_DEBUG PFX "Unsupported Control Frame type: %i\n", type);
  415. return -EINVAL;
  416. }
  417. if(pkt_size == 0)
  418. return -ENOMEM;
  419. /* Send the packet to the ieee80211 layer for tx */
  420. ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *) pkt, pkt_size);
  421. kfree(pkt);
  422. return 0;
  423. }