2 * Some parts based on code from net80211
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
35 #include "ieee80211softmac_priv.h"
37 /* Helper functions for inserting data into the frames */
40 * Adds an ESSID element to the frame
44 ieee80211softmac_add_essid(u8 *dst, struct ieee80211softmac_essid *essid)
47 *dst++ = MFIE_TYPE_SSID;
49 memcpy(dst, essid->data, essid->len);
50 return dst+essid->len;
52 *dst++ = MFIE_TYPE_SSID;
58 /* Adds Supported Rates and if required Extended Rates Information Element
59 * to the frame, ASSUMES WE HAVE A SORTED LIST OF RATES */
61 ieee80211softmac_frame_add_rates(u8 *dst, const struct ieee80211softmac_ratesinfo *r)
63 int cck_len, ofdm_len;
64 *dst++ = MFIE_TYPE_RATES;
66 for(cck_len=0; ieee80211_is_cck_rate(r->rates[cck_len]) && (cck_len < r->count);cck_len++);
68 if(cck_len > IEEE80211SOFTMAC_MAX_RATES_LEN)
69 cck_len = IEEE80211SOFTMAC_MAX_RATES_LEN;
71 memcpy(dst, r->rates, cck_len);
74 if(cck_len < r->count){
75 for (ofdm_len=0; ieee80211_is_ofdm_rate(r->rates[ofdm_len + cck_len]) && (ofdm_len + cck_len < r->count); ofdm_len++);
77 if (ofdm_len > IEEE80211SOFTMAC_MAX_EX_RATES_LEN)
78 ofdm_len = IEEE80211SOFTMAC_MAX_EX_RATES_LEN;
79 *dst++ = MFIE_TYPE_RATES_EX;
81 memcpy(dst, r->rates + cck_len, ofdm_len);
88 /* Allocate a management frame */
90 ieee80211softmac_alloc_mgt(u32 size)
94 /* Add the header and FCS to the size */
95 size = size + IEEE80211_3ADDR_LEN;
96 if(size > IEEE80211_DATA_LEN)
98 /* Allocate the frame */
99 data = kzalloc(size, GFP_ATOMIC);
104 * Add a 2 Address Header
107 ieee80211softmac_hdr_2addr(struct ieee80211softmac_device *mac,
108 struct ieee80211_hdr_2addr *header, u32 type, u8 *dest)
110 /* Fill in the frame control flags */
111 header->frame_ctl = cpu_to_le16(type);
112 /* Control packets always have WEP turned off */
113 if(type > IEEE80211_STYPE_CFENDACK && type < IEEE80211_STYPE_PSPOLL)
114 header->frame_ctl |= mac->ieee->sec.level ? cpu_to_le16(IEEE80211_FCTL_PROTECTED) : 0;
116 /* Fill in the duration */
117 header->duration_id = 0;
118 /* FIXME: How do I find this?
119 * calculate. But most drivers just fill in 0 (except if it's a station id of course) */
121 /* Fill in the Destination Address */
123 memset(header->addr1, 0xFF, ETH_ALEN);
125 memcpy(header->addr1, dest, ETH_ALEN);
126 /* Fill in the Source Address */
127 memcpy(header->addr2, mac->ieee->dev->dev_addr, ETH_ALEN);
132 /* Add a 3 Address Header */
134 ieee80211softmac_hdr_3addr(struct ieee80211softmac_device *mac,
135 struct ieee80211_hdr_3addr *header, u32 type, u8 *dest, u8 *bssid)
137 /* This is common with 2addr, so use that instead */
138 ieee80211softmac_hdr_2addr(mac, (struct ieee80211_hdr_2addr *)header, type, dest);
140 /* Fill in the BSS ID */
142 memset(header->addr3, 0xFF, ETH_ALEN);
144 memcpy(header->addr3, bssid, ETH_ALEN);
146 /* Fill in the sequence # */
147 /* FIXME: I need to add this to the softmac struct
148 * shouldn't the sequence number be in ieee80211? */
152 ieee80211softmac_capabilities(struct ieee80211softmac_device *mac,
153 struct ieee80211softmac_network *net)
155 __le16 capability = 0;
157 /* ESS and IBSS bits are set according to the current mode */
158 switch (mac->ieee->iw_mode) {
160 capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
163 capability = cpu_to_le16(WLAN_CAPABILITY_IBSS);
166 capability = cpu_to_le16(net->capabilities &
167 (WLAN_CAPABILITY_ESS|WLAN_CAPABILITY_IBSS));
170 /* bleh. we don't ever go to these modes */
171 printk(KERN_ERR PFX "invalid iw_mode!\n");
175 /* CF Pollable / CF Poll Request */
176 /* Needs to be implemented, for now, the 0's == not supported */
179 capability |= mac->ieee->sec.level ?
180 cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
183 /* Always supported: we probably won't ever be powering devices which
184 * dont support this... */
185 capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
188 /* Not widely used */
190 /* Channel Agility */
191 /* Not widely used */
194 /* Will be implemented later */
197 /* Not widely used */
202 /*****************************************************************************
203 * Create Management packets
204 *****************************************************************************/
206 /* Creates an association request packet */
208 ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt,
209 struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
212 (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
213 2 + /* Capability Info */
214 2 + /* Listen Interval */
216 1 + 1 + IW_ESSID_MAX_SIZE +
218 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
219 /* Extended Rates IE */
220 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
221 /* WPA IE if present */
223 /* Other IE's? Optional?
224 * Yeah, probably need an extra IE parameter -- lots of vendors like to
225 * fill in their own IEs */
227 if (unlikely((*pkt) == NULL))
229 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
231 /* Fill in the capabilities */
232 (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
234 /* Fill in Listen Interval (?) */
235 (*pkt)->listen_interval = cpu_to_le16(10);
237 data = (u8 *)(*pkt)->info_element;
239 data = ieee80211softmac_add_essid(data, &net->essid);
241 data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
243 if (mac->wpa.IElen && mac->wpa.IE) {
244 memcpy(data, mac->wpa.IE, mac->wpa.IElen);
245 data += mac->wpa.IElen;
247 /* Return the number of used bytes */
248 return (data - (u8*)(*pkt));
251 /* Create a reassociation request packet */
253 ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt,
254 struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
257 (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
258 2 + /* Capability Info */
259 2 + /* Listen Interval */
260 ETH_ALEN + /* AP MAC */
262 1 + 1 + IW_ESSID_MAX_SIZE +
264 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
265 /* Extended Rates IE */
266 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
269 if (unlikely((*pkt) == NULL))
271 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
273 /* Fill in the capabilities */
274 (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
276 /* Fill in Listen Interval (?) */
277 (*pkt)->listen_interval = cpu_to_le16(10);
278 /* Fill in the current AP MAC */
279 memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
281 data = (u8 *)(*pkt)->info_element;
283 data = ieee80211softmac_add_essid(data, &net->essid);
285 data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
286 /* Return packet size */
287 return (data - (u8 *)(*pkt));
290 /* Create an authentication packet */
292 ieee80211softmac_auth(struct ieee80211_auth **pkt,
293 struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
294 u16 transaction, u16 status, int *encrypt_mpdu)
297 int auth_mode = mac->ieee->sec.auth_mode;
298 int is_shared_response = (auth_mode == WLAN_AUTH_SHARED_KEY
299 && transaction == IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE);
301 /* Allocate Packet */
302 (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
303 2 + /* Auth Algorithm */
304 2 + /* Auth Transaction Seq */
305 2 + /* Status Code */
306 /* Challenge Text IE */
307 (is_shared_response ? 1 + 1 + net->challenge_len : 0)
309 if (unlikely((*pkt) == NULL))
311 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
314 (*pkt)->algorithm = cpu_to_le16(auth_mode);
316 (*pkt)->transaction = cpu_to_le16(transaction);
318 (*pkt)->status = cpu_to_le16(status);
320 data = (u8 *)(*pkt)->info_element;
322 if (is_shared_response) {
323 *data = MFIE_TYPE_CHALLENGE;
326 /* Copy the challenge in */
327 *data = net->challenge_len;
329 memcpy(data, net->challenge, net->challenge_len);
330 data += net->challenge_len;
332 /* Make sure this frame gets encrypted with the shared key */
337 /* Return the packet size */
338 return (data - (u8 *)(*pkt));
341 /* Create a disassocation or deauthentication packet */
343 ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
344 struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
345 u16 type, u16 reason)
347 /* Allocate Packet */
348 (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
349 if (unlikely((*pkt) == NULL))
351 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
353 (*pkt)->reason = cpu_to_le16(reason);
354 /* Return the packet size */
355 return (2 + IEEE80211_3ADDR_LEN);
358 /* Create a probe request packet */
360 ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
361 struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
364 /* Allocate Packet */
365 (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
366 /* SSID of requested network */
367 1 + 1 + IW_ESSID_MAX_SIZE +
369 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
370 /* Extended Rates IE */
371 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN
373 if (unlikely((*pkt) == NULL))
375 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
377 data = (u8 *)(*pkt)->info_element;
378 /* Add ESSID (can be NULL) */
379 data = ieee80211softmac_add_essid(data, essid);
381 data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
382 /* Return packet size */
383 return (data - (u8 *)(*pkt));
386 /* Create a probe response packet */
387 /* FIXME: Not complete */
389 ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
390 struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
393 /* Allocate Packet */
394 (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
396 2 + /* Beacon Interval */
397 2 + /* Capability Info */
399 1 + 1 + IW_ESSID_MAX_SIZE +
400 7 + /* FH Parameter Set */
401 2 + /* DS Parameter Set */
402 8 + /* CF Parameter Set */
403 4 /* IBSS Parameter Set */
405 if (unlikely((*pkt) == NULL))
407 ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
408 data = (u8 *)(*pkt)->info_element;
410 /* Return the packet size */
411 return (data - (u8 *)(*pkt));
415 /* Sends a manangement packet
416 * FIXME: document the use of the arg parameter
417 * for _AUTH: (transaction #) | (status << 16)
420 ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
421 void *ptrarg, u32 type, u32 arg)
425 int encrypt_mpdu = 0;
428 case IEEE80211_STYPE_ASSOC_REQ:
429 pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
431 case IEEE80211_STYPE_REASSOC_REQ:
432 pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
434 case IEEE80211_STYPE_AUTH:
435 pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16), &encrypt_mpdu);
437 case IEEE80211_STYPE_DISASSOC:
438 case IEEE80211_STYPE_DEAUTH:
439 pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
441 case IEEE80211_STYPE_PROBE_REQ:
442 pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
444 case IEEE80211_STYPE_PROBE_RESP:
445 pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
448 printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
452 if(pkt_size == 0 || pkt == NULL) {
453 printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
457 /* Send the packet to the ieee80211 layer for tx */
458 /* we defined softmac->mgmt_xmit for this. Should we keep it
459 * as it is (that means we'd need to wrap this into a txb),
460 * modify the prototype (so it matches this function),
461 * or get rid of it alltogether?
462 * Does this work for you now?
464 ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt,
465 IEEE80211_3ADDR_LEN, pkt_size, encrypt_mpdu);
471 /* Beacon handling */
472 int ieee80211softmac_handle_beacon(struct net_device *dev,
473 struct ieee80211_beacon *beacon,
474 struct ieee80211_network *network)
476 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
478 /* This might race, but we don't really care and it's not worth
479 * adding heavyweight locking in this fastpath.
481 if (mac->associnfo.associated) {
482 if (memcmp(network->bssid, mac->associnfo.bssid, ETH_ALEN) == 0)
483 ieee80211softmac_process_erp(mac, network->erp_value);