blob: 5cd8d23c30ee9084b263468a798eff6e2b3801db [file] [log] [blame]
Jouni Malinenff1d2762005-05-12 22:54:16 -04001/*
2 * Intersil Prism2 driver with Host AP (software access point) support
3 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
4 * <jkmaline@cc.hut.fi>
Jouni Malinen62fe7e32005-07-30 20:43:20 -07005 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
Jouni Malinenff1d2762005-05-12 22:54:16 -04006 *
7 * This file is to be included into hostap.c when S/W AP functionality is
8 * compiled.
9 *
10 * AP: FIX:
11 * - if unicast Class 2 (assoc,reassoc,disassoc) frame received from
12 * unauthenticated STA, send deauth. frame (8802.11: 5.5)
13 * - if unicast Class 3 (data with to/from DS,deauth,pspoll) frame received
14 * from authenticated, but unassoc STA, send disassoc frame (8802.11: 5.5)
15 * - if unicast Class 3 received from unauthenticated STA, send deauth. frame
16 * (8802.11: 5.5)
17 */
18
19static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
20 DEF_INTS };
21module_param_array(other_ap_policy, int, NULL, 0444);
22MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
23
24static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
25 DEF_INTS };
26module_param_array(ap_max_inactivity, int, NULL, 0444);
27MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
28 "inactivity");
29
30static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
31module_param_array(ap_bridge_packets, int, NULL, 0444);
32MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
33 "stations");
34
35static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
36module_param_array(autom_ap_wds, int, NULL, 0444);
37MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
38 "automatically");
39
40
41static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
42static void hostap_event_expired_sta(struct net_device *dev,
43 struct sta_info *sta);
44static void handle_add_proc_queue(void *data);
45
46#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
47static void handle_wds_oper_queue(void *data);
48static void prism2_send_mgmt(struct net_device *dev,
49 int type, int subtype, char *body,
50 int body_len, u8 *addr, u16 tx_cb_idx);
51#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
52
53
54#ifndef PRISM2_NO_PROCFS_DEBUG
55static int ap_debug_proc_read(char *page, char **start, off_t off,
56 int count, int *eof, void *data)
57{
58 char *p = page;
59 struct ap_data *ap = (struct ap_data *) data;
60
61 if (off != 0) {
62 *eof = 1;
63 return 0;
64 }
65
66 p += sprintf(p, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
67 p += sprintf(p, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
68 p += sprintf(p, "max_inactivity=%u\n", ap->max_inactivity / HZ);
69 p += sprintf(p, "bridge_packets=%u\n", ap->bridge_packets);
70 p += sprintf(p, "nullfunc_ack=%u\n", ap->nullfunc_ack);
71 p += sprintf(p, "autom_ap_wds=%u\n", ap->autom_ap_wds);
72 p += sprintf(p, "auth_algs=%u\n", ap->local->auth_algs);
73 p += sprintf(p, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
74
75 return (p - page);
76}
77#endif /* PRISM2_NO_PROCFS_DEBUG */
78
79
80static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
81{
82 sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
83 ap->sta_hash[STA_HASH(sta->addr)] = sta;
84}
85
86static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
87{
88 struct sta_info *s;
89
90 s = ap->sta_hash[STA_HASH(sta->addr)];
91 if (s == NULL) return;
92 if (memcmp(s->addr, sta->addr, ETH_ALEN) == 0) {
93 ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
94 return;
95 }
96
97 while (s->hnext != NULL && memcmp(s->hnext->addr, sta->addr, ETH_ALEN)
98 != 0)
99 s = s->hnext;
100 if (s->hnext != NULL)
101 s->hnext = s->hnext->hnext;
102 else
103 printk("AP: could not remove STA " MACSTR " from hash table\n",
104 MAC2STR(sta->addr));
105}
106
107static void ap_free_sta(struct ap_data *ap, struct sta_info *sta)
108{
109 if (sta->ap && sta->local)
110 hostap_event_expired_sta(sta->local->dev, sta);
111
112 if (ap->proc != NULL) {
113 char name[20];
114 sprintf(name, MACSTR, MAC2STR(sta->addr));
115 remove_proc_entry(name, ap->proc);
116 }
117
118 if (sta->crypt) {
119 sta->crypt->ops->deinit(sta->crypt->priv);
120 kfree(sta->crypt);
121 sta->crypt = NULL;
122 }
123
124 skb_queue_purge(&sta->tx_buf);
125
126 ap->num_sta--;
127#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
128 if (sta->aid > 0)
129 ap->sta_aid[sta->aid - 1] = NULL;
130
131 if (!sta->ap && sta->u.sta.challenge)
132 kfree(sta->u.sta.challenge);
133 del_timer(&sta->timer);
134#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
135
136 kfree(sta);
137}
138
139
140static void hostap_set_tim(local_info_t *local, int aid, int set)
141{
142 if (local->func->set_tim)
143 local->func->set_tim(local->dev, aid, set);
144}
145
146
147static void hostap_event_new_sta(struct net_device *dev, struct sta_info *sta)
148{
149 union iwreq_data wrqu;
150 memset(&wrqu, 0, sizeof(wrqu));
151 memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
152 wrqu.addr.sa_family = ARPHRD_ETHER;
153 wireless_send_event(dev, IWEVREGISTERED, &wrqu, NULL);
154}
155
156
157static void hostap_event_expired_sta(struct net_device *dev,
158 struct sta_info *sta)
159{
160 union iwreq_data wrqu;
161 memset(&wrqu, 0, sizeof(wrqu));
162 memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
163 wrqu.addr.sa_family = ARPHRD_ETHER;
164 wireless_send_event(dev, IWEVEXPIRED, &wrqu, NULL);
165}
166
167
168#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
169
170static void ap_handle_timer(unsigned long data)
171{
172 struct sta_info *sta = (struct sta_info *) data;
173 local_info_t *local;
174 struct ap_data *ap;
175 unsigned long next_time = 0;
176 int was_assoc;
177
178 if (sta == NULL || sta->local == NULL || sta->local->ap == NULL) {
179 PDEBUG(DEBUG_AP, "ap_handle_timer() called with NULL data\n");
180 return;
181 }
182
183 local = sta->local;
184 ap = local->ap;
185 was_assoc = sta->flags & WLAN_STA_ASSOC;
186
187 if (atomic_read(&sta->users) != 0)
188 next_time = jiffies + HZ;
189 else if ((sta->flags & WLAN_STA_PERM) && !(sta->flags & WLAN_STA_AUTH))
190 next_time = jiffies + ap->max_inactivity;
191
192 if (time_before(jiffies, sta->last_rx + ap->max_inactivity)) {
193 /* station activity detected; reset timeout state */
194 sta->timeout_next = STA_NULLFUNC;
195 next_time = sta->last_rx + ap->max_inactivity;
196 } else if (sta->timeout_next == STA_DISASSOC &&
197 !(sta->flags & WLAN_STA_PENDING_POLL)) {
198 /* STA ACKed data nullfunc frame poll */
199 sta->timeout_next = STA_NULLFUNC;
200 next_time = jiffies + ap->max_inactivity;
201 }
202
203 if (next_time) {
204 sta->timer.expires = next_time;
205 add_timer(&sta->timer);
206 return;
207 }
208
209 if (sta->ap)
210 sta->timeout_next = STA_DEAUTH;
211
212 if (sta->timeout_next == STA_DEAUTH && !(sta->flags & WLAN_STA_PERM)) {
213 spin_lock(&ap->sta_table_lock);
214 ap_sta_hash_del(ap, sta);
215 list_del(&sta->list);
216 spin_unlock(&ap->sta_table_lock);
217 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
218 } else if (sta->timeout_next == STA_DISASSOC)
219 sta->flags &= ~WLAN_STA_ASSOC;
220
221 if (was_assoc && !(sta->flags & WLAN_STA_ASSOC) && !sta->ap)
222 hostap_event_expired_sta(local->dev, sta);
223
224 if (sta->timeout_next == STA_DEAUTH && sta->aid > 0 &&
225 !skb_queue_empty(&sta->tx_buf)) {
226 hostap_set_tim(local, sta->aid, 0);
227 sta->flags &= ~WLAN_STA_TIM;
228 }
229
230 if (sta->ap) {
231 if (ap->autom_ap_wds) {
232 PDEBUG(DEBUG_AP, "%s: removing automatic WDS "
233 "connection to AP " MACSTR "\n",
234 local->dev->name, MAC2STR(sta->addr));
235 hostap_wds_link_oper(local, sta->addr, WDS_DEL);
236 }
237 } else if (sta->timeout_next == STA_NULLFUNC) {
238 /* send data frame to poll STA and check whether this frame
239 * is ACKed */
240 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
241 * it is apparently not retried so TX Exc events are not
242 * received for it */
243 sta->flags |= WLAN_STA_PENDING_POLL;
244 prism2_send_mgmt(local->dev, WLAN_FC_TYPE_DATA,
245 WLAN_FC_STYPE_DATA, NULL, 0,
246 sta->addr, ap->tx_callback_poll);
247 } else {
248 int deauth = sta->timeout_next == STA_DEAUTH;
249 u16 resp;
250 PDEBUG(DEBUG_AP, "%s: sending %s info to STA " MACSTR
251 "(last=%lu, jiffies=%lu)\n",
252 local->dev->name,
253 deauth ? "deauthentication" : "disassociation",
254 MAC2STR(sta->addr), sta->last_rx, jiffies);
255
256 resp = cpu_to_le16(deauth ? WLAN_REASON_PREV_AUTH_NOT_VALID :
257 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
258 prism2_send_mgmt(local->dev, WLAN_FC_TYPE_MGMT,
259 (deauth ? WLAN_FC_STYPE_DEAUTH :
260 WLAN_FC_STYPE_DISASSOC),
261 (char *) &resp, 2, sta->addr, 0);
262 }
263
264 if (sta->timeout_next == STA_DEAUTH) {
265 if (sta->flags & WLAN_STA_PERM) {
266 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " would have been "
267 "removed, but it has 'perm' flag\n",
268 local->dev->name, MAC2STR(sta->addr));
269 } else
270 ap_free_sta(ap, sta);
271 return;
272 }
273
274 if (sta->timeout_next == STA_NULLFUNC) {
275 sta->timeout_next = STA_DISASSOC;
276 sta->timer.expires = jiffies + AP_DISASSOC_DELAY;
277 } else {
278 sta->timeout_next = STA_DEAUTH;
279 sta->timer.expires = jiffies + AP_DEAUTH_DELAY;
280 }
281
282 add_timer(&sta->timer);
283}
284
285
286void hostap_deauth_all_stas(struct net_device *dev, struct ap_data *ap,
287 int resend)
288{
289 u8 addr[ETH_ALEN];
290 u16 resp;
291 int i;
292
293 PDEBUG(DEBUG_AP, "%s: Deauthenticate all stations\n", dev->name);
294 memset(addr, 0xff, ETH_ALEN);
295
296 resp = __constant_cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
297
298 /* deauth message sent; try to resend it few times; the message is
299 * broadcast, so it may be delayed until next DTIM; there is not much
300 * else we can do at this point since the driver is going to be shut
301 * down */
302 for (i = 0; i < 5; i++) {
303 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH,
304 (char *) &resp, 2, addr, 0);
305
306 if (!resend || ap->num_sta <= 0)
307 return;
308
309 mdelay(50);
310 }
311}
312
313
314static int ap_control_proc_read(char *page, char **start, off_t off,
315 int count, int *eof, void *data)
316{
317 char *p = page;
318 struct ap_data *ap = (struct ap_data *) data;
319 char *policy_txt;
320 struct list_head *ptr;
321 struct mac_entry *entry;
322
323 if (off != 0) {
324 *eof = 1;
325 return 0;
326 }
327
328 switch (ap->mac_restrictions.policy) {
329 case MAC_POLICY_OPEN:
330 policy_txt = "open";
331 break;
332 case MAC_POLICY_ALLOW:
333 policy_txt = "allow";
334 break;
335 case MAC_POLICY_DENY:
336 policy_txt = "deny";
337 break;
338 default:
339 policy_txt = "unknown";
340 break;
341 };
342 p += sprintf(p, "MAC policy: %s\n", policy_txt);
343 p += sprintf(p, "MAC entries: %u\n", ap->mac_restrictions.entries);
344 p += sprintf(p, "MAC list:\n");
345 spin_lock_bh(&ap->mac_restrictions.lock);
346 for (ptr = ap->mac_restrictions.mac_list.next;
347 ptr != &ap->mac_restrictions.mac_list; ptr = ptr->next) {
348 if (p - page > PAGE_SIZE - 80) {
349 p += sprintf(p, "All entries did not fit one page.\n");
350 break;
351 }
352
353 entry = list_entry(ptr, struct mac_entry, list);
354 p += sprintf(p, MACSTR "\n", MAC2STR(entry->addr));
355 }
356 spin_unlock_bh(&ap->mac_restrictions.lock);
357
358 return (p - page);
359}
360
361
362static int ap_control_add_mac(struct mac_restrictions *mac_restrictions,
363 u8 *mac)
364{
365 struct mac_entry *entry;
366
367 entry = kmalloc(sizeof(struct mac_entry), GFP_KERNEL);
368 if (entry == NULL)
369 return -1;
370
371 memcpy(entry->addr, mac, ETH_ALEN);
372
373 spin_lock_bh(&mac_restrictions->lock);
374 list_add_tail(&entry->list, &mac_restrictions->mac_list);
375 mac_restrictions->entries++;
376 spin_unlock_bh(&mac_restrictions->lock);
377
378 return 0;
379}
380
381
382static int ap_control_del_mac(struct mac_restrictions *mac_restrictions,
383 u8 *mac)
384{
385 struct list_head *ptr;
386 struct mac_entry *entry;
387
388 spin_lock_bh(&mac_restrictions->lock);
389 for (ptr = mac_restrictions->mac_list.next;
390 ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
391 entry = list_entry(ptr, struct mac_entry, list);
392
393 if (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
394 list_del(ptr);
395 kfree(entry);
396 mac_restrictions->entries--;
397 spin_unlock_bh(&mac_restrictions->lock);
398 return 0;
399 }
400 }
401 spin_unlock_bh(&mac_restrictions->lock);
402 return -1;
403}
404
405
406static int ap_control_mac_deny(struct mac_restrictions *mac_restrictions,
407 u8 *mac)
408{
409 struct list_head *ptr;
410 struct mac_entry *entry;
411 int found = 0;
412
413 if (mac_restrictions->policy == MAC_POLICY_OPEN)
414 return 0;
415
416 spin_lock_bh(&mac_restrictions->lock);
417 for (ptr = mac_restrictions->mac_list.next;
418 ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
419 entry = list_entry(ptr, struct mac_entry, list);
420
421 if (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
422 found = 1;
423 break;
424 }
425 }
426 spin_unlock_bh(&mac_restrictions->lock);
427
428 if (mac_restrictions->policy == MAC_POLICY_ALLOW)
429 return !found;
430 else
431 return found;
432}
433
434
435static void ap_control_flush_macs(struct mac_restrictions *mac_restrictions)
436{
437 struct list_head *ptr, *n;
438 struct mac_entry *entry;
439
440 if (mac_restrictions->entries == 0)
441 return;
442
443 spin_lock_bh(&mac_restrictions->lock);
444 for (ptr = mac_restrictions->mac_list.next, n = ptr->next;
445 ptr != &mac_restrictions->mac_list;
446 ptr = n, n = ptr->next) {
447 entry = list_entry(ptr, struct mac_entry, list);
448 list_del(ptr);
449 kfree(entry);
450 }
451 mac_restrictions->entries = 0;
452 spin_unlock_bh(&mac_restrictions->lock);
453}
454
455
456static int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev,
457 u8 *mac)
458{
459 struct sta_info *sta;
460 u16 resp;
461
462 spin_lock_bh(&ap->sta_table_lock);
463 sta = ap_get_sta(ap, mac);
464 if (sta) {
465 ap_sta_hash_del(ap, sta);
466 list_del(&sta->list);
467 }
468 spin_unlock_bh(&ap->sta_table_lock);
469
470 if (!sta)
471 return -EINVAL;
472
473 resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
474 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH,
475 (char *) &resp, 2, sta->addr, 0);
476
477 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
478 hostap_event_expired_sta(dev, sta);
479
480 ap_free_sta(ap, sta);
481
482 return 0;
483}
484
485#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
486
487
488static void ap_control_kickall(struct ap_data *ap)
489{
490 struct list_head *ptr, *n;
491 struct sta_info *sta;
Jeff Garzik74fae822005-07-31 13:08:32 -0400492
Jouni Malinenff1d2762005-05-12 22:54:16 -0400493 spin_lock_bh(&ap->sta_table_lock);
494 for (ptr = ap->sta_list.next, n = ptr->next; ptr != &ap->sta_list;
495 ptr = n, n = ptr->next) {
496 sta = list_entry(ptr, struct sta_info, list);
497 ap_sta_hash_del(ap, sta);
498 list_del(&sta->list);
499 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
500 hostap_event_expired_sta(sta->local->dev, sta);
501 ap_free_sta(ap, sta);
502 }
503 spin_unlock_bh(&ap->sta_table_lock);
504}
505
506
507#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
508
509#define PROC_LIMIT (PAGE_SIZE - 80)
510
511static int prism2_ap_proc_read(char *page, char **start, off_t off,
512 int count, int *eof, void *data)
513{
514 char *p = page;
515 struct ap_data *ap = (struct ap_data *) data;
516 struct list_head *ptr;
517 int i;
518
519 if (off > PROC_LIMIT) {
520 *eof = 1;
521 return 0;
522 }
523
524 p += sprintf(p, "# BSSID CHAN SIGNAL NOISE RATE SSID FLAGS\n");
525 spin_lock_bh(&ap->sta_table_lock);
526 for (ptr = ap->sta_list.next; ptr != &ap->sta_list; ptr = ptr->next) {
527 struct sta_info *sta = (struct sta_info *) ptr;
528
529 if (!sta->ap)
530 continue;
531
532 p += sprintf(p, MACSTR " %d %d %d %d '", MAC2STR(sta->addr),
533 sta->u.ap.channel, sta->last_rx_signal,
534 sta->last_rx_silence, sta->last_rx_rate);
535 for (i = 0; i < sta->u.ap.ssid_len; i++)
536 p += sprintf(p, ((sta->u.ap.ssid[i] >= 32 &&
537 sta->u.ap.ssid[i] < 127) ?
538 "%c" : "<%02x>"),
539 sta->u.ap.ssid[i]);
540 p += sprintf(p, "'");
541 if (sta->capability & WLAN_CAPABILITY_ESS)
542 p += sprintf(p, " [ESS]");
543 if (sta->capability & WLAN_CAPABILITY_IBSS)
544 p += sprintf(p, " [IBSS]");
545 if (sta->capability & WLAN_CAPABILITY_PRIVACY)
546 p += sprintf(p, " [WEP]");
547 p += sprintf(p, "\n");
548
549 if ((p - page) > PROC_LIMIT) {
550 printk(KERN_DEBUG "hostap: ap proc did not fit\n");
551 break;
552 }
553 }
554 spin_unlock_bh(&ap->sta_table_lock);
555
556 if ((p - page) <= off) {
557 *eof = 1;
558 return 0;
559 }
560
561 *start = page + off;
562
563 return (p - page - off);
564}
565#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
566
567
568void hostap_check_sta_fw_version(struct ap_data *ap, int sta_fw_ver)
569{
570 if (!ap)
571 return;
572
573 if (sta_fw_ver == PRISM2_FW_VER(0,8,0)) {
574 PDEBUG(DEBUG_AP, "Using data::nullfunc ACK workaround - "
575 "firmware upgrade recommended\n");
576 ap->nullfunc_ack = 1;
577 } else
578 ap->nullfunc_ack = 0;
579
580 if (sta_fw_ver == PRISM2_FW_VER(1,4,2)) {
581 printk(KERN_WARNING "%s: Warning: secondary station firmware "
582 "version 1.4.2 does not seem to work in Host AP mode\n",
583 ap->local->dev->name);
584 }
585}
586
587
588/* Called only as a tasklet (software IRQ) */
589static void hostap_ap_tx_cb(struct sk_buff *skb, int ok, void *data)
590{
591 struct ap_data *ap = data;
592 u16 fc;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700593 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400594
595 if (!ap->local->hostapd || !ap->local->apdev) {
596 dev_kfree_skb(skb);
597 return;
598 }
599
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700600 hdr = (struct ieee80211_hdr *) skb->data;
601 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400602
603 /* Pass the TX callback frame to the hostapd; use 802.11 header version
604 * 1 to indicate failure (no ACK) and 2 success (frame ACKed) */
605
606 fc &= ~WLAN_FC_PVER;
607 fc |= ok ? BIT(1) : BIT(0);
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700608 hdr->frame_ctl = cpu_to_le16(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400609
610 skb->dev = ap->local->apdev;
611 skb_pull(skb, hostap_80211_get_hdrlen(fc));
612 skb->pkt_type = PACKET_OTHERHOST;
613 skb->protocol = __constant_htons(ETH_P_802_2);
614 memset(skb->cb, 0, sizeof(skb->cb));
615 netif_rx(skb);
616}
617
618
619#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
620/* Called only as a tasklet (software IRQ) */
621static void hostap_ap_tx_cb_auth(struct sk_buff *skb, int ok, void *data)
622{
623 struct ap_data *ap = data;
624 struct net_device *dev = ap->local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700625 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400626 u16 fc, *pos, auth_alg, auth_transaction, status;
627 struct sta_info *sta = NULL;
628 char *txt = NULL;
629
630 if (ap->local->hostapd) {
631 dev_kfree_skb(skb);
632 return;
633 }
634
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700635 hdr = (struct ieee80211_hdr *) skb->data;
636 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenebed67d2005-07-30 20:43:19 -0700637 if (HOSTAP_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
638 HOSTAP_FC_GET_STYPE(fc) != WLAN_FC_STYPE_AUTH ||
Jouni Malinenff1d2762005-05-12 22:54:16 -0400639 skb->len < IEEE80211_MGMT_HDR_LEN + 6) {
640 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_auth received invalid "
641 "frame\n", dev->name);
642 dev_kfree_skb(skb);
643 return;
644 }
645
646 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
647 auth_alg = le16_to_cpu(*pos++);
648 auth_transaction = le16_to_cpu(*pos++);
649 status = le16_to_cpu(*pos++);
650
651 if (!ok) {
652 txt = "frame was not ACKed";
653 goto done;
654 }
655
656 spin_lock(&ap->sta_table_lock);
657 sta = ap_get_sta(ap, hdr->addr1);
658 if (sta)
659 atomic_inc(&sta->users);
660 spin_unlock(&ap->sta_table_lock);
661
662 if (!sta) {
663 txt = "STA not found";
664 goto done;
665 }
666
667 if (status == WLAN_STATUS_SUCCESS &&
668 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
669 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
670 txt = "STA authenticated";
671 sta->flags |= WLAN_STA_AUTH;
672 sta->last_auth = jiffies;
673 } else if (status != WLAN_STATUS_SUCCESS)
674 txt = "authentication failed";
675
676 done:
677 if (sta)
678 atomic_dec(&sta->users);
679 if (txt) {
680 PDEBUG(DEBUG_AP, "%s: " MACSTR " auth_cb - alg=%d trans#=%d "
681 "status=%d - %s\n",
682 dev->name, MAC2STR(hdr->addr1), auth_alg,
683 auth_transaction, status, txt);
684 }
685 dev_kfree_skb(skb);
686}
687
688
689/* Called only as a tasklet (software IRQ) */
690static void hostap_ap_tx_cb_assoc(struct sk_buff *skb, int ok, void *data)
691{
692 struct ap_data *ap = data;
693 struct net_device *dev = ap->local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700694 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400695 u16 fc, *pos, status;
696 struct sta_info *sta = NULL;
697 char *txt = NULL;
698
699 if (ap->local->hostapd) {
700 dev_kfree_skb(skb);
701 return;
702 }
703
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700704 hdr = (struct ieee80211_hdr *) skb->data;
705 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenebed67d2005-07-30 20:43:19 -0700706 if (HOSTAP_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
707 (HOSTAP_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ASSOC_RESP &&
708 HOSTAP_FC_GET_STYPE(fc) != WLAN_FC_STYPE_REASSOC_RESP) ||
Jouni Malinenff1d2762005-05-12 22:54:16 -0400709 skb->len < IEEE80211_MGMT_HDR_LEN + 4) {
710 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_assoc received invalid "
711 "frame\n", dev->name);
712 dev_kfree_skb(skb);
713 return;
714 }
715
716 if (!ok) {
717 txt = "frame was not ACKed";
718 goto done;
719 }
720
721 spin_lock(&ap->sta_table_lock);
722 sta = ap_get_sta(ap, hdr->addr1);
723 if (sta)
724 atomic_inc(&sta->users);
725 spin_unlock(&ap->sta_table_lock);
726
727 if (!sta) {
728 txt = "STA not found";
729 goto done;
730 }
731
732 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
733 pos++;
734 status = le16_to_cpu(*pos++);
735 if (status == WLAN_STATUS_SUCCESS) {
736 if (!(sta->flags & WLAN_STA_ASSOC))
737 hostap_event_new_sta(dev, sta);
738 txt = "STA associated";
739 sta->flags |= WLAN_STA_ASSOC;
740 sta->last_assoc = jiffies;
741 } else
742 txt = "association failed";
743
744 done:
745 if (sta)
746 atomic_dec(&sta->users);
747 if (txt) {
748 PDEBUG(DEBUG_AP, "%s: " MACSTR " assoc_cb - %s\n",
749 dev->name, MAC2STR(hdr->addr1), txt);
750 }
751 dev_kfree_skb(skb);
752}
753
754/* Called only as a tasklet (software IRQ); TX callback for poll frames used
755 * in verifying whether the STA is still present. */
756static void hostap_ap_tx_cb_poll(struct sk_buff *skb, int ok, void *data)
757{
758 struct ap_data *ap = data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700759 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400760 struct sta_info *sta;
761
762 if (skb->len < 24)
763 goto fail;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700764 hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400765 if (ok) {
766 spin_lock(&ap->sta_table_lock);
767 sta = ap_get_sta(ap, hdr->addr1);
768 if (sta)
769 sta->flags &= ~WLAN_STA_PENDING_POLL;
770 spin_unlock(&ap->sta_table_lock);
771 } else {
772 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " did not ACK activity "
773 "poll frame\n", ap->local->dev->name,
774 MAC2STR(hdr->addr1));
775 }
776
777 fail:
778 dev_kfree_skb(skb);
779}
780#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
781
782
783void hostap_init_data(local_info_t *local)
784{
785 struct ap_data *ap = local->ap;
786
787 if (ap == NULL) {
788 printk(KERN_WARNING "hostap_init_data: ap == NULL\n");
789 return;
790 }
791 memset(ap, 0, sizeof(struct ap_data));
792 ap->local = local;
793
794 ap->ap_policy = GET_INT_PARM(other_ap_policy, local->card_idx);
795 ap->bridge_packets = GET_INT_PARM(ap_bridge_packets, local->card_idx);
796 ap->max_inactivity =
797 GET_INT_PARM(ap_max_inactivity, local->card_idx) * HZ;
798 ap->autom_ap_wds = GET_INT_PARM(autom_ap_wds, local->card_idx);
799
800 spin_lock_init(&ap->sta_table_lock);
801 INIT_LIST_HEAD(&ap->sta_list);
802
803 /* Initialize task queue structure for AP management */
804 INIT_WORK(&local->ap->add_sta_proc_queue, handle_add_proc_queue, ap);
805
806 ap->tx_callback_idx =
807 hostap_tx_callback_register(local, hostap_ap_tx_cb, ap);
808 if (ap->tx_callback_idx == 0)
809 printk(KERN_WARNING "%s: failed to register TX callback for "
810 "AP\n", local->dev->name);
811#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
812 INIT_WORK(&local->ap->wds_oper_queue, handle_wds_oper_queue, local);
813
814 ap->tx_callback_auth =
815 hostap_tx_callback_register(local, hostap_ap_tx_cb_auth, ap);
816 ap->tx_callback_assoc =
817 hostap_tx_callback_register(local, hostap_ap_tx_cb_assoc, ap);
818 ap->tx_callback_poll =
819 hostap_tx_callback_register(local, hostap_ap_tx_cb_poll, ap);
820 if (ap->tx_callback_auth == 0 || ap->tx_callback_assoc == 0 ||
821 ap->tx_callback_poll == 0)
822 printk(KERN_WARNING "%s: failed to register TX callback for "
823 "AP\n", local->dev->name);
824
825 spin_lock_init(&ap->mac_restrictions.lock);
826 INIT_LIST_HEAD(&ap->mac_restrictions.mac_list);
827#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
828
829 ap->initialized = 1;
830}
831
832
833void hostap_init_ap_proc(local_info_t *local)
834{
835 struct ap_data *ap = local->ap;
836
837 ap->proc = local->proc;
838 if (ap->proc == NULL)
839 return;
840
841#ifndef PRISM2_NO_PROCFS_DEBUG
842 create_proc_read_entry("ap_debug", 0, ap->proc,
843 ap_debug_proc_read, ap);
844#endif /* PRISM2_NO_PROCFS_DEBUG */
845
846#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
847 create_proc_read_entry("ap_control", 0, ap->proc,
848 ap_control_proc_read, ap);
849 create_proc_read_entry("ap", 0, ap->proc,
850 prism2_ap_proc_read, ap);
851#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
852
853}
854
855
856void hostap_free_data(struct ap_data *ap)
857{
858 struct list_head *n, *ptr;
859
860 if (ap == NULL || !ap->initialized) {
861 printk(KERN_DEBUG "hostap_free_data: ap has not yet been "
862 "initialized - skip resource freeing\n");
863 return;
864 }
865
866#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
867 if (ap->crypt)
868 ap->crypt->deinit(ap->crypt_priv);
869 ap->crypt = ap->crypt_priv = NULL;
870#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
871
872 list_for_each_safe(ptr, n, &ap->sta_list) {
873 struct sta_info *sta = list_entry(ptr, struct sta_info, list);
874 ap_sta_hash_del(ap, sta);
875 list_del(&sta->list);
876 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
877 hostap_event_expired_sta(sta->local->dev, sta);
878 ap_free_sta(ap, sta);
879 }
880
881#ifndef PRISM2_NO_PROCFS_DEBUG
882 if (ap->proc != NULL) {
883 remove_proc_entry("ap_debug", ap->proc);
884 }
885#endif /* PRISM2_NO_PROCFS_DEBUG */
886
887#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
888 if (ap->proc != NULL) {
889 remove_proc_entry("ap", ap->proc);
890 remove_proc_entry("ap_control", ap->proc);
891 }
892 ap_control_flush_macs(&ap->mac_restrictions);
893#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
894
895 ap->initialized = 0;
896}
897
898
899/* caller should have mutex for AP STA list handling */
900static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta)
901{
902 struct sta_info *s;
903
904 s = ap->sta_hash[STA_HASH(sta)];
905 while (s != NULL && memcmp(s->addr, sta, ETH_ALEN) != 0)
906 s = s->hnext;
907 return s;
908}
909
910
911#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
912
913/* Called from timer handler and from scheduled AP queue handlers */
914static void prism2_send_mgmt(struct net_device *dev,
915 int type, int subtype, char *body,
916 int body_len, u8 *addr, u16 tx_cb_idx)
917{
918 struct hostap_interface *iface;
919 local_info_t *local;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700920 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400921 u16 fc;
922 struct sk_buff *skb;
923 struct hostap_skb_tx_data *meta;
924 int hdrlen;
925
926 iface = netdev_priv(dev);
927 local = iface->local;
928 dev = local->dev; /* always use master radio device */
929 iface = netdev_priv(dev);
930
931 if (!(dev->flags & IFF_UP)) {
932 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt - device is not UP - "
933 "cannot send frame\n", dev->name);
934 return;
935 }
936
937 skb = dev_alloc_skb(sizeof(*hdr) + body_len);
938 if (skb == NULL) {
939 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt failed to allocate "
940 "skb\n", dev->name);
941 return;
942 }
943
944 fc = (type << 2) | (subtype << 4);
945 hdrlen = hostap_80211_get_hdrlen(fc);
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700946 hdr = (struct ieee80211_hdr *) skb_put(skb, hdrlen);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400947 if (body)
948 memcpy(skb_put(skb, body_len), body, body_len);
949
950 memset(hdr, 0, hdrlen);
951
952 /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
953 * tx_control instead of using local->tx_control */
954
955
956 memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
957 if (type == WLAN_FC_TYPE_DATA) {
958 fc |= WLAN_FC_FROMDS;
959 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
960 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
961 } else if (type == WLAN_FC_TYPE_CTRL) {
962 /* control:ACK does not have addr2 or addr3 */
963 memset(hdr->addr2, 0, ETH_ALEN);
964 memset(hdr->addr3, 0, ETH_ALEN);
965 } else {
966 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
967 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
968 }
969
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700970 hdr->frame_ctl = cpu_to_le16(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400971
972 meta = (struct hostap_skb_tx_data *) skb->cb;
973 memset(meta, 0, sizeof(*meta));
974 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
975 meta->iface = iface;
976 meta->tx_cb_idx = tx_cb_idx;
977
978 skb->dev = dev;
979 skb->mac.raw = skb->nh.raw = skb->data;
980 dev_queue_xmit(skb);
981}
982#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
983
984
985static int prism2_sta_proc_read(char *page, char **start, off_t off,
986 int count, int *eof, void *data)
987{
988 char *p = page;
989 struct sta_info *sta = (struct sta_info *) data;
990 int i;
991
992 /* FIX: possible race condition.. the STA data could have just expired,
993 * but proc entry was still here so that the read could have started;
994 * some locking should be done here.. */
995
996 if (off != 0) {
997 *eof = 1;
998 return 0;
999 }
1000
1001 p += sprintf(p, "%s=" MACSTR "\nusers=%d\naid=%d\n"
1002 "flags=0x%04x%s%s%s%s%s%s%s\n"
1003 "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
1004 sta->ap ? "AP" : "STA",
1005 MAC2STR(sta->addr), atomic_read(&sta->users), sta->aid,
1006 sta->flags,
1007 sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
1008 sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
1009 sta->flags & WLAN_STA_PS ? " PS" : "",
1010 sta->flags & WLAN_STA_TIM ? " TIM" : "",
1011 sta->flags & WLAN_STA_PERM ? " PERM" : "",
1012 sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
1013 sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
1014 sta->capability, sta->listen_interval);
1015 /* supported_rates: 500 kbit/s units with msb ignored */
1016 for (i = 0; i < sizeof(sta->supported_rates); i++)
1017 if (sta->supported_rates[i] != 0)
1018 p += sprintf(p, "%d%sMbps ",
1019 (sta->supported_rates[i] & 0x7f) / 2,
1020 sta->supported_rates[i] & 1 ? ".5" : "");
1021 p += sprintf(p, "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
1022 "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
1023 "tx_packets=%lu\n"
1024 "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
1025 "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
1026 "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
1027 "tx[11M]=%d\n"
1028 "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
1029 jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
1030 sta->last_tx,
1031 sta->rx_packets, sta->tx_packets, sta->rx_bytes,
1032 sta->tx_bytes, skb_queue_len(&sta->tx_buf),
1033 sta->last_rx_silence,
1034 sta->last_rx_signal, sta->last_rx_rate / 10,
1035 sta->last_rx_rate % 10 ? ".5" : "",
1036 sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
1037 sta->tx_count[2], sta->tx_count[3], sta->rx_count[0],
1038 sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
1039 if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
1040 p = sta->crypt->ops->print_stats(p, sta->crypt->priv);
1041#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1042 if (sta->ap) {
1043 if (sta->u.ap.channel >= 0)
1044 p += sprintf(p, "channel=%d\n", sta->u.ap.channel);
1045 p += sprintf(p, "ssid=");
1046 for (i = 0; i < sta->u.ap.ssid_len; i++)
1047 p += sprintf(p, ((sta->u.ap.ssid[i] >= 32 &&
1048 sta->u.ap.ssid[i] < 127) ?
1049 "%c" : "<%02x>"),
1050 sta->u.ap.ssid[i]);
1051 p += sprintf(p, "\n");
1052 }
1053#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1054
1055 return (p - page);
1056}
1057
1058
1059static void handle_add_proc_queue(void *data)
1060{
1061 struct ap_data *ap = (struct ap_data *) data;
1062 struct sta_info *sta;
1063 char name[20];
1064 struct add_sta_proc_data *entry, *prev;
1065
1066 entry = ap->add_sta_proc_entries;
1067 ap->add_sta_proc_entries = NULL;
1068
1069 while (entry) {
1070 spin_lock_bh(&ap->sta_table_lock);
1071 sta = ap_get_sta(ap, entry->addr);
1072 if (sta)
1073 atomic_inc(&sta->users);
1074 spin_unlock_bh(&ap->sta_table_lock);
1075
1076 if (sta) {
1077 sprintf(name, MACSTR, MAC2STR(sta->addr));
1078 sta->proc = create_proc_read_entry(
1079 name, 0, ap->proc,
1080 prism2_sta_proc_read, sta);
1081
1082 atomic_dec(&sta->users);
1083 }
1084
1085 prev = entry;
1086 entry = entry->next;
1087 kfree(prev);
1088 }
1089}
1090
1091
1092static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
1093{
1094 struct sta_info *sta;
1095
1096 sta = (struct sta_info *)
1097 kmalloc(sizeof(struct sta_info), GFP_ATOMIC);
1098 if (sta == NULL) {
1099 PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
1100 return NULL;
1101 }
1102
1103 /* initialize STA info data */
1104 memset(sta, 0, sizeof(struct sta_info));
1105 sta->local = ap->local;
1106 skb_queue_head_init(&sta->tx_buf);
1107 memcpy(sta->addr, addr, ETH_ALEN);
1108
1109 atomic_inc(&sta->users);
1110 spin_lock_bh(&ap->sta_table_lock);
1111 list_add(&sta->list, &ap->sta_list);
1112 ap->num_sta++;
1113 ap_sta_hash_add(ap, sta);
1114 spin_unlock_bh(&ap->sta_table_lock);
1115
1116 if (ap->proc) {
1117 struct add_sta_proc_data *entry;
1118 /* schedule a non-interrupt context process to add a procfs
1119 * entry for the STA since procfs code use GFP_KERNEL */
1120 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1121 if (entry) {
1122 memcpy(entry->addr, sta->addr, ETH_ALEN);
1123 entry->next = ap->add_sta_proc_entries;
1124 ap->add_sta_proc_entries = entry;
1125 schedule_work(&ap->add_sta_proc_queue);
1126 } else
1127 printk(KERN_DEBUG "Failed to add STA proc data\n");
1128 }
1129
1130#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1131 init_timer(&sta->timer);
1132 sta->timer.expires = jiffies + ap->max_inactivity;
1133 sta->timer.data = (unsigned long) sta;
1134 sta->timer.function = ap_handle_timer;
1135 if (!ap->local->hostapd)
1136 add_timer(&sta->timer);
1137#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1138
1139 return sta;
1140}
1141
1142
1143static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
1144 local_info_t *local)
1145{
1146 if (rateidx > sta->tx_max_rate ||
1147 !(sta->tx_supp_rates & (1 << rateidx)))
1148 return 0;
1149
1150 if (local->tx_rate_control != 0 &&
1151 !(local->tx_rate_control & (1 << rateidx)))
1152 return 0;
1153
1154 return 1;
1155}
1156
1157
1158static void prism2_check_tx_rates(struct sta_info *sta)
1159{
1160 int i;
1161
1162 sta->tx_supp_rates = 0;
1163 for (i = 0; i < sizeof(sta->supported_rates); i++) {
1164 if ((sta->supported_rates[i] & 0x7f) == 2)
1165 sta->tx_supp_rates |= WLAN_RATE_1M;
1166 if ((sta->supported_rates[i] & 0x7f) == 4)
1167 sta->tx_supp_rates |= WLAN_RATE_2M;
1168 if ((sta->supported_rates[i] & 0x7f) == 11)
1169 sta->tx_supp_rates |= WLAN_RATE_5M5;
1170 if ((sta->supported_rates[i] & 0x7f) == 22)
1171 sta->tx_supp_rates |= WLAN_RATE_11M;
1172 }
1173 sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
1174 if (sta->tx_supp_rates & WLAN_RATE_1M) {
1175 sta->tx_max_rate = 0;
1176 if (ap_tx_rate_ok(0, sta, sta->local)) {
1177 sta->tx_rate = 10;
1178 sta->tx_rate_idx = 0;
1179 }
1180 }
1181 if (sta->tx_supp_rates & WLAN_RATE_2M) {
1182 sta->tx_max_rate = 1;
1183 if (ap_tx_rate_ok(1, sta, sta->local)) {
1184 sta->tx_rate = 20;
1185 sta->tx_rate_idx = 1;
1186 }
1187 }
1188 if (sta->tx_supp_rates & WLAN_RATE_5M5) {
1189 sta->tx_max_rate = 2;
1190 if (ap_tx_rate_ok(2, sta, sta->local)) {
1191 sta->tx_rate = 55;
1192 sta->tx_rate_idx = 2;
1193 }
1194 }
1195 if (sta->tx_supp_rates & WLAN_RATE_11M) {
1196 sta->tx_max_rate = 3;
1197 if (ap_tx_rate_ok(3, sta, sta->local)) {
1198 sta->tx_rate = 110;
1199 sta->tx_rate_idx = 3;
1200 }
1201 }
1202}
1203
1204
1205#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1206
1207static void ap_crypt_init(struct ap_data *ap)
1208{
Jouni Malinen62fe7e32005-07-30 20:43:20 -07001209 ap->crypt = ieee80211_get_crypto_ops("WEP");
Jouni Malinenff1d2762005-05-12 22:54:16 -04001210
1211 if (ap->crypt) {
1212 if (ap->crypt->init) {
1213 ap->crypt_priv = ap->crypt->init(0);
1214 if (ap->crypt_priv == NULL)
1215 ap->crypt = NULL;
1216 else {
1217 u8 key[WEP_KEY_LEN];
1218 get_random_bytes(key, WEP_KEY_LEN);
1219 ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
1220 ap->crypt_priv);
1221 }
1222 }
1223 }
1224
1225 if (ap->crypt == NULL) {
1226 printk(KERN_WARNING "AP could not initialize WEP: load module "
Jouni Malinen62fe7e32005-07-30 20:43:20 -07001227 "ieee80211_crypt_wep.ko\n");
Jouni Malinenff1d2762005-05-12 22:54:16 -04001228 }
1229}
1230
1231
1232/* Generate challenge data for shared key authentication. IEEE 802.11 specifies
1233 * that WEP algorithm is used for generating challange. This should be unique,
1234 * but otherwise there is not really need for randomness etc. Initialize WEP
1235 * with pseudo random key and then use increasing IV to get unique challenge
1236 * streams.
1237 *
1238 * Called only as a scheduled task for pending AP frames.
1239 */
1240static char * ap_auth_make_challenge(struct ap_data *ap)
1241{
1242 char *tmpbuf;
1243 struct sk_buff *skb;
1244
1245 if (ap->crypt == NULL) {
1246 ap_crypt_init(ap);
1247 if (ap->crypt == NULL)
1248 return NULL;
1249 }
1250
1251 tmpbuf = (char *) kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
1252 if (tmpbuf == NULL) {
1253 PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
1254 return NULL;
1255 }
1256
1257 skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
1258 ap->crypt->extra_prefix_len +
1259 ap->crypt->extra_postfix_len);
1260 if (skb == NULL) {
1261 kfree(tmpbuf);
1262 return NULL;
1263 }
1264
1265 skb_reserve(skb, ap->crypt->extra_prefix_len);
1266 memset(skb_put(skb, WLAN_AUTH_CHALLENGE_LEN), 0,
1267 WLAN_AUTH_CHALLENGE_LEN);
1268 if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
1269 dev_kfree_skb(skb);
1270 kfree(tmpbuf);
1271 return NULL;
1272 }
1273
1274 memcpy(tmpbuf, skb->data + ap->crypt->extra_prefix_len,
1275 WLAN_AUTH_CHALLENGE_LEN);
1276 dev_kfree_skb(skb);
1277
1278 return tmpbuf;
1279}
1280
1281
1282/* Called only as a scheduled task for pending AP frames. */
1283static void handle_authen(local_info_t *local, struct sk_buff *skb,
1284 struct hostap_80211_rx_status *rx_stats)
1285{
1286 struct net_device *dev = local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001287 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001288 size_t hdrlen;
1289 struct ap_data *ap = local->ap;
1290 char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
1291 int len, olen;
1292 u16 auth_alg, auth_transaction, status_code, *pos;
1293 u16 resp = WLAN_STATUS_SUCCESS, fc;
1294 struct sta_info *sta = NULL;
Jouni Malinen62fe7e32005-07-30 20:43:20 -07001295 struct ieee80211_crypt_data *crypt;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001296 char *txt = "";
1297
1298 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1299
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001300 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001301 hdrlen = hostap_80211_get_hdrlen(fc);
1302
1303 if (len < 6) {
1304 PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
1305 "(len=%d) from " MACSTR "\n", dev->name, len,
1306 MAC2STR(hdr->addr2));
1307 return;
1308 }
1309
1310 spin_lock_bh(&local->ap->sta_table_lock);
1311 sta = ap_get_sta(local->ap, hdr->addr2);
1312 if (sta)
1313 atomic_inc(&sta->users);
1314 spin_unlock_bh(&local->ap->sta_table_lock);
1315
1316 if (sta && sta->crypt)
1317 crypt = sta->crypt;
1318 else {
1319 int idx = 0;
1320 if (skb->len >= hdrlen + 3)
1321 idx = skb->data[hdrlen + 3] >> 6;
1322 crypt = local->crypt[idx];
1323 }
1324
1325 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1326 auth_alg = __le16_to_cpu(*pos);
1327 pos++;
1328 auth_transaction = __le16_to_cpu(*pos);
1329 pos++;
1330 status_code = __le16_to_cpu(*pos);
1331 pos++;
1332
1333 if (memcmp(dev->dev_addr, hdr->addr2, ETH_ALEN) == 0 ||
1334 ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
1335 txt = "authentication denied";
1336 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1337 goto fail;
1338 }
1339
1340 if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
1341 auth_alg == WLAN_AUTH_OPEN) ||
1342 ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
1343 crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
1344 } else {
1345 txt = "unsupported algorithm";
1346 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1347 goto fail;
1348 }
1349
1350 if (len >= 8) {
1351 u8 *u = (u8 *) pos;
1352 if (*u == WLAN_EID_CHALLENGE) {
1353 if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
1354 txt = "invalid challenge len";
1355 resp = WLAN_STATUS_CHALLENGE_FAIL;
1356 goto fail;
1357 }
1358 if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
1359 txt = "challenge underflow";
1360 resp = WLAN_STATUS_CHALLENGE_FAIL;
1361 goto fail;
1362 }
1363 challenge = (char *) (u + 2);
1364 }
1365 }
1366
1367 if (sta && sta->ap) {
1368 if (time_after(jiffies, sta->u.ap.last_beacon +
1369 (10 * sta->listen_interval * HZ) / 1024)) {
1370 PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
1371 " assuming AP " MACSTR " is now STA\n",
1372 dev->name, MAC2STR(sta->addr));
1373 sta->ap = 0;
1374 sta->flags = 0;
1375 sta->u.sta.challenge = NULL;
1376 } else {
1377 txt = "AP trying to authenticate?";
1378 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1379 goto fail;
1380 }
1381 }
1382
1383 if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
1384 (auth_alg == WLAN_AUTH_SHARED_KEY &&
1385 (auth_transaction == 1 ||
1386 (auth_transaction == 3 && sta != NULL &&
1387 sta->u.sta.challenge != NULL)))) {
1388 } else {
1389 txt = "unknown authentication transaction number";
1390 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1391 goto fail;
1392 }
1393
1394 if (sta == NULL) {
1395 txt = "new STA";
1396
1397 if (local->ap->num_sta >= MAX_STA_COUNT) {
1398 /* FIX: might try to remove some old STAs first? */
1399 txt = "no more room for new STAs";
1400 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1401 goto fail;
1402 }
1403
1404 sta = ap_add_sta(local->ap, hdr->addr2);
1405 if (sta == NULL) {
1406 txt = "ap_add_sta failed";
1407 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1408 goto fail;
1409 }
1410 }
1411
1412 switch (auth_alg) {
1413 case WLAN_AUTH_OPEN:
1414 txt = "authOK";
1415 /* IEEE 802.11 standard is not completely clear about
1416 * whether STA is considered authenticated after
1417 * authentication OK frame has been send or after it
1418 * has been ACKed. In order to reduce interoperability
1419 * issues, mark the STA authenticated before ACK. */
1420 sta->flags |= WLAN_STA_AUTH;
1421 break;
1422
1423 case WLAN_AUTH_SHARED_KEY:
1424 if (auth_transaction == 1) {
1425 if (sta->u.sta.challenge == NULL) {
1426 sta->u.sta.challenge =
1427 ap_auth_make_challenge(local->ap);
1428 if (sta->u.sta.challenge == NULL) {
1429 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1430 goto fail;
1431 }
1432 }
1433 } else {
1434 if (sta->u.sta.challenge == NULL ||
1435 challenge == NULL ||
1436 memcmp(sta->u.sta.challenge, challenge,
1437 WLAN_AUTH_CHALLENGE_LEN) != 0 ||
1438 !(fc & WLAN_FC_ISWEP)) {
1439 txt = "challenge response incorrect";
1440 resp = WLAN_STATUS_CHALLENGE_FAIL;
1441 goto fail;
1442 }
1443
1444 txt = "challenge OK - authOK";
1445 /* IEEE 802.11 standard is not completely clear about
1446 * whether STA is considered authenticated after
1447 * authentication OK frame has been send or after it
1448 * has been ACKed. In order to reduce interoperability
1449 * issues, mark the STA authenticated before ACK. */
1450 sta->flags |= WLAN_STA_AUTH;
1451 kfree(sta->u.sta.challenge);
1452 sta->u.sta.challenge = NULL;
1453 }
1454 break;
1455 }
1456
1457 fail:
1458 pos = (u16 *) body;
1459 *pos = cpu_to_le16(auth_alg);
1460 pos++;
1461 *pos = cpu_to_le16(auth_transaction + 1);
1462 pos++;
1463 *pos = cpu_to_le16(resp); /* status_code */
1464 pos++;
1465 olen = 6;
1466
1467 if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
1468 sta->u.sta.challenge != NULL &&
1469 auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
1470 u8 *tmp = (u8 *) pos;
1471 *tmp++ = WLAN_EID_CHALLENGE;
1472 *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
1473 pos++;
1474 memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
1475 olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
1476 }
1477
1478 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_AUTH,
1479 body, olen, hdr->addr2, ap->tx_callback_auth);
1480
1481 if (sta) {
1482 sta->last_rx = jiffies;
1483 atomic_dec(&sta->users);
1484 }
1485
1486 if (resp) {
1487 PDEBUG(DEBUG_AP, "%s: " MACSTR " auth (alg=%d trans#=%d "
1488 "stat=%d len=%d fc=%04x) ==> %d (%s)\n",
1489 dev->name, MAC2STR(hdr->addr2), auth_alg,
1490 auth_transaction, status_code, len, fc, resp, txt);
1491 }
1492}
1493
1494
1495/* Called only as a scheduled task for pending AP frames. */
1496static void handle_assoc(local_info_t *local, struct sk_buff *skb,
1497 struct hostap_80211_rx_status *rx_stats, int reassoc)
1498{
1499 struct net_device *dev = local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001500 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001501 char body[12], *p, *lpos;
1502 int len, left;
1503 u16 *pos;
1504 u16 resp = WLAN_STATUS_SUCCESS;
1505 struct sta_info *sta = NULL;
1506 int send_deauth = 0;
1507 char *txt = "";
1508 u8 prev_ap[ETH_ALEN];
1509
1510 left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
1511
1512 if (len < (reassoc ? 10 : 4)) {
1513 PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
1514 "(len=%d, reassoc=%d) from " MACSTR "\n",
1515 dev->name, len, reassoc, MAC2STR(hdr->addr2));
1516 return;
1517 }
1518
1519 spin_lock_bh(&local->ap->sta_table_lock);
1520 sta = ap_get_sta(local->ap, hdr->addr2);
1521 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1522 spin_unlock_bh(&local->ap->sta_table_lock);
1523 txt = "trying to associate before authentication";
1524 send_deauth = 1;
1525 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1526 sta = NULL; /* do not decrement sta->users */
1527 goto fail;
1528 }
1529 atomic_inc(&sta->users);
1530 spin_unlock_bh(&local->ap->sta_table_lock);
1531
1532 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1533 sta->capability = __le16_to_cpu(*pos);
1534 pos++; left -= 2;
1535 sta->listen_interval = __le16_to_cpu(*pos);
1536 pos++; left -= 2;
1537
1538 if (reassoc) {
1539 memcpy(prev_ap, pos, ETH_ALEN);
1540 pos++; pos++; pos++; left -= 6;
1541 } else
1542 memset(prev_ap, 0, ETH_ALEN);
1543
1544 if (left >= 2) {
1545 unsigned int ileft;
1546 unsigned char *u = (unsigned char *) pos;
1547
1548 if (*u == WLAN_EID_SSID) {
1549 u++; left--;
1550 ileft = *u;
1551 u++; left--;
1552
1553 if (ileft > left || ileft > MAX_SSID_LEN) {
1554 txt = "SSID overflow";
1555 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1556 goto fail;
1557 }
1558
1559 if (ileft != strlen(local->essid) ||
1560 memcmp(local->essid, u, ileft) != 0) {
1561 txt = "not our SSID";
1562 resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1563 goto fail;
1564 }
1565
1566 u += ileft;
1567 left -= ileft;
1568 }
1569
1570 if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
1571 u++; left--;
1572 ileft = *u;
1573 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04001574
Jouni Malinenff1d2762005-05-12 22:54:16 -04001575 if (ileft > left || ileft == 0 ||
1576 ileft > WLAN_SUPP_RATES_MAX) {
1577 txt = "SUPP_RATES len error";
1578 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1579 goto fail;
1580 }
1581
1582 memset(sta->supported_rates, 0,
1583 sizeof(sta->supported_rates));
1584 memcpy(sta->supported_rates, u, ileft);
1585 prism2_check_tx_rates(sta);
1586
1587 u += ileft;
1588 left -= ileft;
1589 }
1590
1591 if (left > 0) {
1592 PDEBUG(DEBUG_AP, "%s: assoc from " MACSTR " with extra"
1593 " data (%d bytes) [",
1594 dev->name, MAC2STR(hdr->addr2), left);
1595 while (left > 0) {
1596 PDEBUG2(DEBUG_AP, "<%02x>", *u);
1597 u++; left--;
1598 }
1599 PDEBUG2(DEBUG_AP, "]\n");
1600 }
1601 } else {
1602 txt = "frame underflow";
1603 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1604 goto fail;
1605 }
1606
1607 /* get a unique AID */
1608 if (sta->aid > 0)
1609 txt = "OK, old AID";
1610 else {
1611 spin_lock_bh(&local->ap->sta_table_lock);
1612 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1613 if (local->ap->sta_aid[sta->aid - 1] == NULL)
1614 break;
1615 if (sta->aid > MAX_AID_TABLE_SIZE) {
1616 sta->aid = 0;
1617 spin_unlock_bh(&local->ap->sta_table_lock);
1618 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1619 txt = "no room for more AIDs";
1620 } else {
1621 local->ap->sta_aid[sta->aid - 1] = sta;
1622 spin_unlock_bh(&local->ap->sta_table_lock);
1623 txt = "OK, new AID";
1624 }
1625 }
1626
1627 fail:
1628 pos = (u16 *) body;
1629
1630 if (send_deauth) {
1631 *pos = __constant_cpu_to_le16(
1632 WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
1633 pos++;
1634 } else {
1635 /* FIX: CF-Pollable and CF-PollReq should be set to match the
1636 * values in beacons/probe responses */
1637 /* FIX: how about privacy and WEP? */
1638 /* capability */
1639 *pos = __constant_cpu_to_le16(WLAN_CAPABILITY_ESS);
1640 pos++;
1641
1642 /* status_code */
1643 *pos = __cpu_to_le16(resp);
1644 pos++;
1645
1646 *pos = __cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
1647 BIT(14) | BIT(15)); /* AID */
1648 pos++;
1649
1650 /* Supported rates (Information element) */
1651 p = (char *) pos;
1652 *p++ = WLAN_EID_SUPP_RATES;
1653 lpos = p;
1654 *p++ = 0; /* len */
1655 if (local->tx_rate_control & WLAN_RATE_1M) {
1656 *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
1657 (*lpos)++;
1658 }
1659 if (local->tx_rate_control & WLAN_RATE_2M) {
1660 *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
1661 (*lpos)++;
1662 }
1663 if (local->tx_rate_control & WLAN_RATE_5M5) {
1664 *p++ = local->basic_rates & WLAN_RATE_5M5 ?
1665 0x8b : 0x0b;
1666 (*lpos)++;
1667 }
1668 if (local->tx_rate_control & WLAN_RATE_11M) {
1669 *p++ = local->basic_rates & WLAN_RATE_11M ?
1670 0x96 : 0x16;
1671 (*lpos)++;
1672 }
1673 pos = (u16 *) p;
1674 }
1675
1676 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT,
1677 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1678 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1679 WLAN_FC_STYPE_ASSOC_RESP)),
1680 body, (u8 *) pos - (u8 *) body,
1681 hdr->addr2,
1682 send_deauth ? 0 : local->ap->tx_callback_assoc);
1683
1684 if (sta) {
1685 if (resp == WLAN_STATUS_SUCCESS) {
1686 sta->last_rx = jiffies;
1687 /* STA will be marked associated from TX callback, if
1688 * AssocResp is ACKed */
1689 }
1690 atomic_dec(&sta->users);
1691 }
1692
1693#if 0
1694 PDEBUG(DEBUG_AP, "%s: " MACSTR " %sassoc (len=%d prev_ap=" MACSTR
1695 ") => %d(%d) (%s)\n",
1696 dev->name, MAC2STR(hdr->addr2), reassoc ? "re" : "", len,
1697 MAC2STR(prev_ap), resp, send_deauth, txt);
1698#endif
1699}
1700
1701
1702/* Called only as a scheduled task for pending AP frames. */
1703static void handle_deauth(local_info_t *local, struct sk_buff *skb,
1704 struct hostap_80211_rx_status *rx_stats)
1705{
1706 struct net_device *dev = local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001707 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001708 char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1709 int len;
1710 u16 reason_code, *pos;
1711 struct sta_info *sta = NULL;
1712
1713 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1714
1715 if (len < 2) {
1716 printk("handle_deauth - too short payload (len=%d)\n", len);
1717 return;
1718 }
1719
1720 pos = (u16 *) body;
1721 reason_code = __le16_to_cpu(*pos);
1722
1723 PDEBUG(DEBUG_AP, "%s: deauthentication: " MACSTR " len=%d, "
1724 "reason_code=%d\n", dev->name, MAC2STR(hdr->addr2), len,
1725 reason_code);
1726
1727 spin_lock_bh(&local->ap->sta_table_lock);
1728 sta = ap_get_sta(local->ap, hdr->addr2);
1729 if (sta != NULL) {
1730 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1731 hostap_event_expired_sta(local->dev, sta);
1732 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1733 }
1734 spin_unlock_bh(&local->ap->sta_table_lock);
1735 if (sta == NULL) {
1736 printk("%s: deauthentication from " MACSTR ", "
1737 "reason_code=%d, but STA not authenticated\n", dev->name,
1738 MAC2STR(hdr->addr2), reason_code);
1739 }
1740}
1741
1742
1743/* Called only as a scheduled task for pending AP frames. */
1744static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
1745 struct hostap_80211_rx_status *rx_stats)
1746{
1747 struct net_device *dev = local->dev;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001748 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001749 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1750 int len;
1751 u16 reason_code, *pos;
1752 struct sta_info *sta = NULL;
1753
1754 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1755
1756 if (len < 2) {
1757 printk("handle_disassoc - too short payload (len=%d)\n", len);
1758 return;
1759 }
1760
1761 pos = (u16 *) body;
1762 reason_code = __le16_to_cpu(*pos);
1763
1764 PDEBUG(DEBUG_AP, "%s: disassociation: " MACSTR " len=%d, "
1765 "reason_code=%d\n", dev->name, MAC2STR(hdr->addr2), len,
1766 reason_code);
1767
1768 spin_lock_bh(&local->ap->sta_table_lock);
1769 sta = ap_get_sta(local->ap, hdr->addr2);
1770 if (sta != NULL) {
1771 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1772 hostap_event_expired_sta(local->dev, sta);
1773 sta->flags &= ~WLAN_STA_ASSOC;
1774 }
1775 spin_unlock_bh(&local->ap->sta_table_lock);
1776 if (sta == NULL) {
1777 printk("%s: disassociation from " MACSTR ", "
1778 "reason_code=%d, but STA not authenticated\n",
1779 dev->name, MAC2STR(hdr->addr2), reason_code);
1780 }
1781}
1782
1783
1784/* Called only as a scheduled task for pending AP frames. */
1785static void ap_handle_data_nullfunc(local_info_t *local,
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001786 struct ieee80211_hdr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001787{
1788 struct net_device *dev = local->dev;
1789
1790 /* some STA f/w's seem to require control::ACK frame for
1791 * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
1792 * not send this..
1793 * send control::ACK for the data::nullfunc */
1794
1795 printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
1796 prism2_send_mgmt(dev, WLAN_FC_TYPE_CTRL, WLAN_FC_STYPE_ACK,
1797 NULL, 0, hdr->addr2, 0);
1798}
1799
1800
1801/* Called only as a scheduled task for pending AP frames. */
1802static void ap_handle_dropped_data(local_info_t *local,
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001803 struct ieee80211_hdr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001804{
1805 struct net_device *dev = local->dev;
1806 struct sta_info *sta;
1807 u16 reason;
1808
1809 spin_lock_bh(&local->ap->sta_table_lock);
1810 sta = ap_get_sta(local->ap, hdr->addr2);
1811 if (sta)
1812 atomic_inc(&sta->users);
1813 spin_unlock_bh(&local->ap->sta_table_lock);
1814
1815 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
1816 PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
1817 atomic_dec(&sta->users);
1818 return;
1819 }
1820
1821 reason = __constant_cpu_to_le16(
1822 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1823 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT,
1824 ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
1825 WLAN_FC_STYPE_DEAUTH : WLAN_FC_STYPE_DISASSOC),
1826 (char *) &reason, sizeof(reason), hdr->addr2, 0);
1827
1828 if (sta)
1829 atomic_dec(&sta->users);
1830}
1831
1832#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1833
1834
1835/* Called only as a scheduled task for pending AP frames. */
1836static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
1837 struct sk_buff *skb)
1838{
Jouni Malinen5bee7202005-08-14 19:08:39 -07001839 struct hostap_skb_tx_data *meta;
1840
Jouni Malinenff1d2762005-05-12 22:54:16 -04001841 if (!(sta->flags & WLAN_STA_PS)) {
1842 /* Station has moved to non-PS mode, so send all buffered
1843 * frames using normal device queue. */
1844 dev_queue_xmit(skb);
1845 return;
1846 }
1847
1848 /* add a flag for hostap_handle_sta_tx() to know that this skb should
1849 * be passed through even though STA is using PS */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001850 meta = (struct hostap_skb_tx_data *) skb->cb;
1851 meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001852 if (!skb_queue_empty(&sta->tx_buf)) {
1853 /* indicate to STA that more frames follow */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001854 meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001855 }
1856 dev_queue_xmit(skb);
1857}
1858
1859
1860/* Called only as a scheduled task for pending AP frames. */
1861static void handle_pspoll(local_info_t *local,
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001862 struct ieee80211_hdr *hdr,
Jouni Malinenff1d2762005-05-12 22:54:16 -04001863 struct hostap_80211_rx_status *rx_stats)
1864{
1865 struct net_device *dev = local->dev;
1866 struct sta_info *sta;
1867 u16 aid;
1868 struct sk_buff *skb;
1869
1870 PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=" MACSTR ", TA=" MACSTR
1871 " PWRMGT=%d\n",
1872 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001873 !!(le16_to_cpu(hdr->frame_ctl) & WLAN_FC_PWRMGT));
Jouni Malinenff1d2762005-05-12 22:54:16 -04001874
1875 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
1876 PDEBUG(DEBUG_AP, "handle_pspoll - addr1(BSSID)=" MACSTR
1877 " not own MAC\n", MAC2STR(hdr->addr1));
1878 return;
1879 }
1880
1881 aid = __le16_to_cpu(hdr->duration_id);
1882 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
1883 PDEBUG(DEBUG_PS, " PSPOLL and AID[15:14] not set\n");
1884 return;
1885 }
1886 aid &= ~BIT(15) & ~BIT(14);
1887 if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
1888 PDEBUG(DEBUG_PS, " invalid aid=%d\n", aid);
1889 return;
1890 }
1891 PDEBUG(DEBUG_PS2, " aid=%d\n", aid);
1892
1893 spin_lock_bh(&local->ap->sta_table_lock);
1894 sta = ap_get_sta(local->ap, hdr->addr2);
1895 if (sta)
1896 atomic_inc(&sta->users);
1897 spin_unlock_bh(&local->ap->sta_table_lock);
1898
1899 if (sta == NULL) {
1900 PDEBUG(DEBUG_PS, " STA not found\n");
1901 return;
1902 }
1903 if (sta->aid != aid) {
1904 PDEBUG(DEBUG_PS, " received aid=%i does not match with "
1905 "assoc.aid=%d\n", aid, sta->aid);
1906 return;
1907 }
1908
1909 /* FIX: todo:
1910 * - add timeout for buffering (clear aid in TIM vector if buffer timed
1911 * out (expiry time must be longer than ListenInterval for
1912 * the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
1913 * - what to do, if buffered, pspolled, and sent frame is not ACKed by
1914 * sta; store buffer for later use and leave TIM aid bit set? use
1915 * TX event to check whether frame was ACKed?
1916 */
1917
1918 while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
1919 /* send buffered frame .. */
1920 PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
1921 " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
1922
1923 pspoll_send_buffered(local, sta, skb);
1924
1925 if (sta->flags & WLAN_STA_PS) {
1926 /* send only one buffered packet per PS Poll */
1927 /* FIX: should ignore further PS Polls until the
1928 * buffered packet that was just sent is acknowledged
1929 * (Tx or TxExc event) */
1930 break;
1931 }
1932 }
1933
1934 if (skb_queue_empty(&sta->tx_buf)) {
1935 /* try to clear aid from TIM */
1936 if (!(sta->flags & WLAN_STA_TIM))
1937 PDEBUG(DEBUG_PS2, "Re-unsetting TIM for aid %d\n",
1938 aid);
1939 hostap_set_tim(local, aid, 0);
1940 sta->flags &= ~WLAN_STA_TIM;
1941 }
1942
1943 atomic_dec(&sta->users);
1944}
1945
1946
1947#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1948
1949static void handle_wds_oper_queue(void *data)
1950{
1951 local_info_t *local = data;
1952 struct wds_oper_data *entry, *prev;
1953
1954 spin_lock_bh(&local->lock);
1955 entry = local->ap->wds_oper_entries;
1956 local->ap->wds_oper_entries = NULL;
1957 spin_unlock_bh(&local->lock);
1958
1959 while (entry) {
1960 PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
1961 "to AP " MACSTR "\n",
1962 local->dev->name,
1963 entry->type == WDS_ADD ? "adding" : "removing",
1964 MAC2STR(entry->addr));
1965 if (entry->type == WDS_ADD)
1966 prism2_wds_add(local, entry->addr, 0);
1967 else if (entry->type == WDS_DEL)
1968 prism2_wds_del(local, entry->addr, 0, 1);
1969
1970 prev = entry;
1971 entry = entry->next;
1972 kfree(prev);
1973 }
1974}
1975
1976
1977/* Called only as a scheduled task for pending AP frames. */
1978static void handle_beacon(local_info_t *local, struct sk_buff *skb,
1979 struct hostap_80211_rx_status *rx_stats)
1980{
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001981 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001982 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1983 int len, left;
1984 u16 *pos, beacon_int, capability;
1985 char *ssid = NULL;
1986 unsigned char *supp_rates = NULL;
1987 int ssid_len = 0, supp_rates_len = 0;
1988 struct sta_info *sta = NULL;
1989 int new_sta = 0, channel = -1;
1990
1991 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1992
1993 if (len < 8 + 2 + 2) {
1994 printk(KERN_DEBUG "handle_beacon - too short payload "
1995 "(len=%d)\n", len);
1996 return;
1997 }
1998
1999 pos = (u16 *) body;
2000 left = len;
2001
2002 /* Timestamp (8 octets) */
2003 pos += 4; left -= 8;
2004 /* Beacon interval (2 octets) */
2005 beacon_int = __le16_to_cpu(*pos);
2006 pos++; left -= 2;
2007 /* Capability information (2 octets) */
2008 capability = __le16_to_cpu(*pos);
2009 pos++; left -= 2;
2010
2011 if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
2012 capability & WLAN_CAPABILITY_IBSS)
2013 return;
2014
2015 if (left >= 2) {
2016 unsigned int ileft;
2017 unsigned char *u = (unsigned char *) pos;
2018
2019 if (*u == WLAN_EID_SSID) {
2020 u++; left--;
2021 ileft = *u;
2022 u++; left--;
2023
2024 if (ileft > left || ileft > MAX_SSID_LEN) {
2025 PDEBUG(DEBUG_AP, "SSID: overflow\n");
2026 return;
2027 }
2028
2029 if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
2030 (ileft != strlen(local->essid) ||
2031 memcmp(local->essid, u, ileft) != 0)) {
2032 /* not our SSID */
2033 return;
2034 }
2035
2036 ssid = u;
2037 ssid_len = ileft;
2038
2039 u += ileft;
2040 left -= ileft;
2041 }
2042
2043 if (*u == WLAN_EID_SUPP_RATES) {
2044 u++; left--;
2045 ileft = *u;
2046 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002047
Jouni Malinenff1d2762005-05-12 22:54:16 -04002048 if (ileft > left || ileft == 0 || ileft > 8) {
2049 PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
2050 return;
2051 }
2052
2053 supp_rates = u;
2054 supp_rates_len = ileft;
2055
2056 u += ileft;
2057 left -= ileft;
2058 }
2059
2060 if (*u == WLAN_EID_DS_PARAMS) {
2061 u++; left--;
2062 ileft = *u;
2063 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002064
Jouni Malinenff1d2762005-05-12 22:54:16 -04002065 if (ileft > left || ileft != 1) {
2066 PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
2067 return;
2068 }
2069
2070 channel = *u;
2071
2072 u += ileft;
2073 left -= ileft;
2074 }
2075 }
2076
2077 spin_lock_bh(&local->ap->sta_table_lock);
2078 sta = ap_get_sta(local->ap, hdr->addr2);
2079 if (sta != NULL)
2080 atomic_inc(&sta->users);
2081 spin_unlock_bh(&local->ap->sta_table_lock);
2082
2083 if (sta == NULL) {
2084 /* add new AP */
2085 new_sta = 1;
2086 sta = ap_add_sta(local->ap, hdr->addr2);
2087 if (sta == NULL) {
2088 printk(KERN_INFO "prism2: kmalloc failed for AP "
2089 "data structure\n");
2090 return;
2091 }
2092 hostap_event_new_sta(local->dev, sta);
2093
2094 /* mark APs authentication and associated for pseudo ad-hoc
2095 * style communication */
2096 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
2097
2098 if (local->ap->autom_ap_wds) {
2099 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
2100 }
2101 }
2102
2103 sta->ap = 1;
2104 if (ssid) {
2105 sta->u.ap.ssid_len = ssid_len;
2106 memcpy(sta->u.ap.ssid, ssid, ssid_len);
2107 sta->u.ap.ssid[ssid_len] = '\0';
2108 } else {
2109 sta->u.ap.ssid_len = 0;
2110 sta->u.ap.ssid[0] = '\0';
2111 }
2112 sta->u.ap.channel = channel;
2113 sta->rx_packets++;
2114 sta->rx_bytes += len;
2115 sta->u.ap.last_beacon = sta->last_rx = jiffies;
2116 sta->capability = capability;
2117 sta->listen_interval = beacon_int;
2118
2119 atomic_dec(&sta->users);
2120
2121 if (new_sta) {
2122 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
2123 memcpy(sta->supported_rates, supp_rates, supp_rates_len);
2124 prism2_check_tx_rates(sta);
2125 }
2126}
2127
2128#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2129
2130
2131/* Called only as a tasklet. */
2132static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
2133 struct hostap_80211_rx_status *rx_stats)
2134{
2135#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2136 struct net_device *dev = local->dev;
2137#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2138 u16 fc, type, stype;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002139 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002140
2141 /* FIX: should give skb->len to handler functions and check that the
2142 * buffer is long enough */
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002143 hdr = (struct ieee80211_hdr *) skb->data;
2144 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenebed67d2005-07-30 20:43:19 -07002145 type = HOSTAP_FC_GET_TYPE(fc);
2146 stype = HOSTAP_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002147
2148#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2149 if (!local->hostapd && type == WLAN_FC_TYPE_DATA) {
2150 PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
2151
2152 if (!(fc & WLAN_FC_TODS) || (fc & WLAN_FC_FROMDS)) {
2153 if (stype == WLAN_FC_STYPE_NULLFUNC) {
2154 /* no ToDS nullfunc seems to be used to check
2155 * AP association; so send reject message to
2156 * speed up re-association */
2157 ap_handle_dropped_data(local, hdr);
2158 goto done;
2159 }
2160 PDEBUG(DEBUG_AP, " not ToDS frame (fc=0x%04x)\n",
2161 fc);
2162 goto done;
2163 }
2164
2165 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
2166 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)="
2167 MACSTR " not own MAC\n",
2168 MAC2STR(hdr->addr1));
2169 goto done;
2170 }
2171
2172 if (local->ap->nullfunc_ack && stype == WLAN_FC_STYPE_NULLFUNC)
2173 ap_handle_data_nullfunc(local, hdr);
2174 else
2175 ap_handle_dropped_data(local, hdr);
2176 goto done;
2177 }
2178
2179 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_BEACON) {
2180 handle_beacon(local, skb, rx_stats);
2181 goto done;
2182 }
2183#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2184
2185 if (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL) {
2186 handle_pspoll(local, hdr, rx_stats);
2187 goto done;
2188 }
2189
2190 if (local->hostapd) {
2191 PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
2192 "subtype=0x%02x\n", type, stype);
2193 goto done;
2194 }
2195
2196#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2197 if (type != WLAN_FC_TYPE_MGMT) {
2198 PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
2199 goto done;
2200 }
2201
2202 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
2203 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=" MACSTR
2204 " not own MAC\n", MAC2STR(hdr->addr1));
2205 goto done;
2206 }
2207
2208 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN)) {
2209 PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=" MACSTR
2210 " not own MAC\n", MAC2STR(hdr->addr3));
2211 goto done;
2212 }
2213
2214 switch (stype) {
2215 case WLAN_FC_STYPE_ASSOC_REQ:
2216 handle_assoc(local, skb, rx_stats, 0);
2217 break;
2218 case WLAN_FC_STYPE_ASSOC_RESP:
2219 PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
2220 break;
2221 case WLAN_FC_STYPE_REASSOC_REQ:
2222 handle_assoc(local, skb, rx_stats, 1);
2223 break;
2224 case WLAN_FC_STYPE_REASSOC_RESP:
2225 PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
2226 break;
2227 case WLAN_FC_STYPE_ATIM:
2228 PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
2229 break;
2230 case WLAN_FC_STYPE_DISASSOC:
2231 handle_disassoc(local, skb, rx_stats);
2232 break;
2233 case WLAN_FC_STYPE_AUTH:
2234 handle_authen(local, skb, rx_stats);
2235 break;
2236 case WLAN_FC_STYPE_DEAUTH:
2237 handle_deauth(local, skb, rx_stats);
2238 break;
2239 default:
2240 PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n", stype);
2241 break;
2242 }
2243#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2244
2245 done:
2246 dev_kfree_skb(skb);
2247}
2248
2249
2250/* Called only as a tasklet (software IRQ) */
2251void hostap_rx(struct net_device *dev, struct sk_buff *skb,
2252 struct hostap_80211_rx_status *rx_stats)
2253{
2254 struct hostap_interface *iface;
2255 local_info_t *local;
2256 u16 fc;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002257 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002258
2259 iface = netdev_priv(dev);
2260 local = iface->local;
2261
2262 if (skb->len < 16)
2263 goto drop;
2264
2265 local->stats.rx_packets++;
2266
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002267 hdr = (struct ieee80211_hdr *) skb->data;
2268 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002269
2270 if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
Jouni Malinenebed67d2005-07-30 20:43:19 -07002271 HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2272 HOSTAP_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002273 goto drop;
2274
2275 skb->protocol = __constant_htons(ETH_P_HOSTAP);
2276 handle_ap_item(local, skb, rx_stats);
2277 return;
2278
2279 drop:
2280 dev_kfree_skb(skb);
2281}
2282
2283
2284/* Called only as a tasklet (software IRQ) */
2285static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
2286{
2287 struct sk_buff *skb;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002288 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002289 struct hostap_80211_rx_status rx_stats;
2290
2291 if (skb_queue_empty(&sta->tx_buf))
2292 return;
2293
2294 skb = dev_alloc_skb(16);
2295 if (skb == NULL) {
2296 printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
2297 "failed\n", local->dev->name);
2298 return;
2299 }
2300
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002301 hdr = (struct ieee80211_hdr *) skb_put(skb, 16);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002302
2303 /* Generate a fake pspoll frame to start packet delivery */
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002304 hdr->frame_ctl = __constant_cpu_to_le16(
Jouni Malinenff1d2762005-05-12 22:54:16 -04002305 (WLAN_FC_TYPE_CTRL << 2) | (WLAN_FC_STYPE_PSPOLL << 4));
2306 memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
2307 memcpy(hdr->addr2, sta->addr, ETH_ALEN);
2308 hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
2309
2310 PDEBUG(DEBUG_PS2, "%s: Scheduling buffered packet delivery for "
2311 "STA " MACSTR "\n", local->dev->name, MAC2STR(sta->addr));
2312
2313 skb->dev = local->dev;
2314
2315 memset(&rx_stats, 0, sizeof(rx_stats));
2316 hostap_rx(local->dev, skb, &rx_stats);
2317}
2318
2319
2320static int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
2321 struct iw_quality qual[], int buf_size,
2322 int aplist)
2323{
2324 struct ap_data *ap = local->ap;
2325 struct list_head *ptr;
2326 int count = 0;
2327
2328 spin_lock_bh(&ap->sta_table_lock);
2329
2330 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2331 ptr = ptr->next) {
2332 struct sta_info *sta = (struct sta_info *) ptr;
2333
2334 if (aplist && !sta->ap)
2335 continue;
2336 addr[count].sa_family = ARPHRD_ETHER;
2337 memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
2338 if (sta->last_rx_silence == 0)
2339 qual[count].qual = sta->last_rx_signal < 27 ?
2340 0 : (sta->last_rx_signal - 27) * 92 / 127;
2341 else
2342 qual[count].qual = sta->last_rx_signal -
2343 sta->last_rx_silence - 35;
2344 qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2345 qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2346 qual[count].updated = sta->last_rx_updated;
2347
2348 sta->last_rx_updated = 0;
2349
2350 count++;
2351 if (count >= buf_size)
2352 break;
2353 }
2354 spin_unlock_bh(&ap->sta_table_lock);
2355
2356 return count;
2357}
2358
2359
2360/* Translate our list of Access Points & Stations to a card independant
2361 * format that the Wireless Tools will understand - Jean II */
2362static int prism2_ap_translate_scan(struct net_device *dev, char *buffer)
2363{
2364 struct hostap_interface *iface;
2365 local_info_t *local;
2366 struct ap_data *ap;
2367 struct list_head *ptr;
2368 struct iw_event iwe;
2369 char *current_ev = buffer;
2370 char *end_buf = buffer + IW_SCAN_MAX_DATA;
2371#if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
2372 char buf[64];
2373#endif
2374
2375 iface = netdev_priv(dev);
2376 local = iface->local;
2377 ap = local->ap;
2378
2379 spin_lock_bh(&ap->sta_table_lock);
2380
2381 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2382 ptr = ptr->next) {
2383 struct sta_info *sta = (struct sta_info *) ptr;
2384
2385 /* First entry *MUST* be the AP MAC address */
2386 memset(&iwe, 0, sizeof(iwe));
2387 iwe.cmd = SIOCGIWAP;
2388 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2389 memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
2390 iwe.len = IW_EV_ADDR_LEN;
2391 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2392 IW_EV_ADDR_LEN);
2393
2394 /* Use the mode to indicate if it's a station or
2395 * an Access Point */
2396 memset(&iwe, 0, sizeof(iwe));
2397 iwe.cmd = SIOCGIWMODE;
2398 if (sta->ap)
2399 iwe.u.mode = IW_MODE_MASTER;
2400 else
2401 iwe.u.mode = IW_MODE_INFRA;
2402 iwe.len = IW_EV_UINT_LEN;
2403 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2404 IW_EV_UINT_LEN);
2405
2406 /* Some quality */
2407 memset(&iwe, 0, sizeof(iwe));
2408 iwe.cmd = IWEVQUAL;
2409 if (sta->last_rx_silence == 0)
2410 iwe.u.qual.qual = sta->last_rx_signal < 27 ?
2411 0 : (sta->last_rx_signal - 27) * 92 / 127;
2412 else
2413 iwe.u.qual.qual = sta->last_rx_signal -
2414 sta->last_rx_silence - 35;
2415 iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2416 iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2417 iwe.u.qual.updated = sta->last_rx_updated;
2418 iwe.len = IW_EV_QUAL_LEN;
2419 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2420 IW_EV_QUAL_LEN);
2421
2422#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2423 if (sta->ap) {
2424 memset(&iwe, 0, sizeof(iwe));
2425 iwe.cmd = SIOCGIWESSID;
2426 iwe.u.data.length = sta->u.ap.ssid_len;
2427 iwe.u.data.flags = 1;
2428 current_ev = iwe_stream_add_point(current_ev, end_buf,
2429 &iwe,
2430 sta->u.ap.ssid);
2431
2432 memset(&iwe, 0, sizeof(iwe));
2433 iwe.cmd = SIOCGIWENCODE;
2434 if (sta->capability & WLAN_CAPABILITY_PRIVACY)
2435 iwe.u.data.flags =
2436 IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2437 else
2438 iwe.u.data.flags = IW_ENCODE_DISABLED;
2439 current_ev = iwe_stream_add_point(current_ev, end_buf,
2440 &iwe,
2441 sta->u.ap.ssid
2442 /* 0 byte memcpy */);
2443
2444 if (sta->u.ap.channel > 0 &&
2445 sta->u.ap.channel <= FREQ_COUNT) {
2446 memset(&iwe, 0, sizeof(iwe));
2447 iwe.cmd = SIOCGIWFREQ;
2448 iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
2449 * 100000;
2450 iwe.u.freq.e = 1;
2451 current_ev = iwe_stream_add_event(
2452 current_ev, end_buf, &iwe,
2453 IW_EV_FREQ_LEN);
2454 }
2455
2456 memset(&iwe, 0, sizeof(iwe));
2457 iwe.cmd = IWEVCUSTOM;
2458 sprintf(buf, "beacon_interval=%d",
2459 sta->listen_interval);
2460 iwe.u.data.length = strlen(buf);
2461 current_ev = iwe_stream_add_point(current_ev, end_buf,
2462 &iwe, buf);
2463 }
2464#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2465
2466 sta->last_rx_updated = 0;
2467
2468 /* To be continued, we should make good use of IWEVCUSTOM */
2469 }
2470
2471 spin_unlock_bh(&ap->sta_table_lock);
2472
2473 return current_ev - buffer;
2474}
2475
2476
2477static int prism2_hostapd_add_sta(struct ap_data *ap,
2478 struct prism2_hostapd_param *param)
2479{
2480 struct sta_info *sta;
2481
2482 spin_lock_bh(&ap->sta_table_lock);
2483 sta = ap_get_sta(ap, param->sta_addr);
2484 if (sta)
2485 atomic_inc(&sta->users);
2486 spin_unlock_bh(&ap->sta_table_lock);
2487
2488 if (sta == NULL) {
2489 sta = ap_add_sta(ap, param->sta_addr);
2490 if (sta == NULL)
2491 return -1;
2492 }
2493
2494 if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2495 hostap_event_new_sta(sta->local->dev, sta);
2496
2497 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
2498 sta->last_rx = jiffies;
2499 sta->aid = param->u.add_sta.aid;
2500 sta->capability = param->u.add_sta.capability;
2501 sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
2502 if (sta->tx_supp_rates & WLAN_RATE_1M)
2503 sta->supported_rates[0] = 2;
2504 if (sta->tx_supp_rates & WLAN_RATE_2M)
2505 sta->supported_rates[1] = 4;
2506 if (sta->tx_supp_rates & WLAN_RATE_5M5)
2507 sta->supported_rates[2] = 11;
2508 if (sta->tx_supp_rates & WLAN_RATE_11M)
2509 sta->supported_rates[3] = 22;
2510 prism2_check_tx_rates(sta);
2511 atomic_dec(&sta->users);
2512 return 0;
2513}
2514
2515
2516static int prism2_hostapd_remove_sta(struct ap_data *ap,
2517 struct prism2_hostapd_param *param)
2518{
2519 struct sta_info *sta;
2520
2521 spin_lock_bh(&ap->sta_table_lock);
2522 sta = ap_get_sta(ap, param->sta_addr);
2523 if (sta) {
2524 ap_sta_hash_del(ap, sta);
2525 list_del(&sta->list);
2526 }
2527 spin_unlock_bh(&ap->sta_table_lock);
2528
2529 if (!sta)
2530 return -ENOENT;
2531
2532 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2533 hostap_event_expired_sta(sta->local->dev, sta);
2534 ap_free_sta(ap, sta);
2535
2536 return 0;
2537}
2538
2539
2540static int prism2_hostapd_get_info_sta(struct ap_data *ap,
2541 struct prism2_hostapd_param *param)
2542{
2543 struct sta_info *sta;
2544
2545 spin_lock_bh(&ap->sta_table_lock);
2546 sta = ap_get_sta(ap, param->sta_addr);
2547 if (sta)
2548 atomic_inc(&sta->users);
2549 spin_unlock_bh(&ap->sta_table_lock);
2550
2551 if (!sta)
2552 return -ENOENT;
2553
2554 param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
2555
2556 atomic_dec(&sta->users);
2557
2558 return 1;
2559}
2560
2561
2562static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
2563 struct prism2_hostapd_param *param)
2564{
2565 struct sta_info *sta;
2566
2567 spin_lock_bh(&ap->sta_table_lock);
2568 sta = ap_get_sta(ap, param->sta_addr);
2569 if (sta) {
2570 sta->flags |= param->u.set_flags_sta.flags_or;
2571 sta->flags &= param->u.set_flags_sta.flags_and;
2572 }
2573 spin_unlock_bh(&ap->sta_table_lock);
2574
2575 if (!sta)
2576 return -ENOENT;
2577
2578 return 0;
2579}
2580
2581
2582static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
2583 struct prism2_hostapd_param *param)
2584{
2585 struct sta_info *sta;
2586 int rate;
2587
2588 spin_lock_bh(&ap->sta_table_lock);
2589 sta = ap_get_sta(ap, param->sta_addr);
2590 if (sta) {
2591 sta->rx_packets = sta->tx_packets = 0;
2592 sta->rx_bytes = sta->tx_bytes = 0;
2593 for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
2594 sta->tx_count[rate] = 0;
2595 sta->rx_count[rate] = 0;
2596 }
2597 }
2598 spin_unlock_bh(&ap->sta_table_lock);
2599
2600 if (!sta)
2601 return -ENOENT;
2602
2603 return 0;
2604}
2605
2606
2607static int prism2_hostapd(struct ap_data *ap,
2608 struct prism2_hostapd_param *param)
2609{
2610 switch (param->cmd) {
2611 case PRISM2_HOSTAPD_FLUSH:
2612 ap_control_kickall(ap);
2613 return 0;
2614 case PRISM2_HOSTAPD_ADD_STA:
2615 return prism2_hostapd_add_sta(ap, param);
2616 case PRISM2_HOSTAPD_REMOVE_STA:
2617 return prism2_hostapd_remove_sta(ap, param);
2618 case PRISM2_HOSTAPD_GET_INFO_STA:
2619 return prism2_hostapd_get_info_sta(ap, param);
2620 case PRISM2_HOSTAPD_SET_FLAGS_STA:
2621 return prism2_hostapd_set_flags_sta(ap, param);
2622 case PRISM2_HOSTAPD_STA_CLEAR_STATS:
2623 return prism2_hostapd_sta_clear_stats(ap, param);
2624 default:
2625 printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
2626 param->cmd);
2627 return -EOPNOTSUPP;
2628 }
2629}
2630
2631
2632/* Update station info for host-based TX rate control and return current
2633 * TX rate */
2634static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
2635{
2636 int ret = sta->tx_rate;
2637 struct hostap_interface *iface;
2638 local_info_t *local;
2639
2640 iface = netdev_priv(dev);
2641 local = iface->local;
2642
2643 sta->tx_count[sta->tx_rate_idx]++;
2644 sta->tx_since_last_failure++;
2645 sta->tx_consecutive_exc = 0;
2646 if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
2647 sta->tx_rate_idx < sta->tx_max_rate) {
2648 /* use next higher rate */
2649 int old_rate, new_rate;
2650 old_rate = new_rate = sta->tx_rate_idx;
2651 while (new_rate < sta->tx_max_rate) {
2652 new_rate++;
2653 if (ap_tx_rate_ok(new_rate, sta, local)) {
2654 sta->tx_rate_idx = new_rate;
2655 break;
2656 }
2657 }
2658 if (old_rate != sta->tx_rate_idx) {
2659 switch (sta->tx_rate_idx) {
2660 case 0: sta->tx_rate = 10; break;
2661 case 1: sta->tx_rate = 20; break;
2662 case 2: sta->tx_rate = 55; break;
2663 case 3: sta->tx_rate = 110; break;
2664 default: sta->tx_rate = 0; break;
2665 }
2666 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " TX rate raised to"
2667 " %d\n", dev->name, MAC2STR(sta->addr),
2668 sta->tx_rate);
2669 }
2670 sta->tx_since_last_failure = 0;
2671 }
2672
2673 return ret;
2674}
2675
2676
2677/* Called only from software IRQ. Called for each TX frame prior possible
2678 * encryption and transmit. */
2679ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
2680{
2681 struct sta_info *sta = NULL;
2682 struct sk_buff *skb = tx->skb;
2683 int set_tim, ret;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002684 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002685 struct hostap_skb_tx_data *meta;
2686
2687 meta = (struct hostap_skb_tx_data *) skb->cb;
2688 ret = AP_TX_CONTINUE;
2689 if (local->ap == NULL || skb->len < 10 ||
2690 meta->iface->type == HOSTAP_INTERFACE_STA)
2691 goto out;
2692
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002693 hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002694
2695 if (hdr->addr1[0] & 0x01) {
2696 /* broadcast/multicast frame - no AP related processing */
2697 goto out;
2698 }
2699
2700 /* unicast packet - check whether destination STA is associated */
2701 spin_lock(&local->ap->sta_table_lock);
2702 sta = ap_get_sta(local->ap, hdr->addr1);
2703 if (sta)
2704 atomic_inc(&sta->users);
2705 spin_unlock(&local->ap->sta_table_lock);
2706
Jouni Malinen5bee7202005-08-14 19:08:39 -07002707 if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
2708 !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
Jouni Malinenff1d2762005-05-12 22:54:16 -04002709 meta->iface->type != HOSTAP_INTERFACE_MASTER &&
2710 meta->iface->type != HOSTAP_INTERFACE_AP) {
2711#if 0
2712 /* This can happen, e.g., when wlan0 is added to a bridge and
2713 * bridging code does not know which port is the correct target
2714 * for a unicast frame. In this case, the packet is send to all
2715 * ports of the bridge. Since this is a valid scenario, do not
2716 * print out any errors here. */
2717 if (net_ratelimit()) {
2718 printk(KERN_DEBUG "AP: drop packet to non-associated "
2719 "STA " MACSTR "\n", MAC2STR(hdr->addr1));
2720 }
2721#endif
2722 local->ap->tx_drop_nonassoc++;
2723 ret = AP_TX_DROP;
2724 goto out;
2725 }
2726
2727 if (sta == NULL)
2728 goto out;
2729
2730 if (!(sta->flags & WLAN_STA_AUTHORIZED))
2731 ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
2732
2733 /* Set tx_rate if using host-based TX rate control */
2734 if (!local->fw_tx_rate_control)
2735 local->ap->last_tx_rate = meta->rate =
2736 ap_update_sta_tx_rate(sta, local->dev);
2737
2738 if (local->iw_mode != IW_MODE_MASTER)
2739 goto out;
2740
2741 if (!(sta->flags & WLAN_STA_PS))
2742 goto out;
2743
Jouni Malinen5bee7202005-08-14 19:08:39 -07002744 if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
2745 /* indicate to STA that more frames follow */
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002746 hdr->frame_ctl |= __constant_cpu_to_le16(WLAN_FC_MOREDATA);
Jouni Malinen5bee7202005-08-14 19:08:39 -07002747 }
Jouni Malinenff1d2762005-05-12 22:54:16 -04002748
Jouni Malinen5bee7202005-08-14 19:08:39 -07002749 if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
2750 /* packet was already buffered and now send due to
2751 * PS poll, so do not rebuffer it */
2752 goto out;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002753 }
2754
2755 if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
2756 PDEBUG(DEBUG_PS, "%s: No more space in STA (" MACSTR ")'s PS "
2757 "mode buffer\n", local->dev->name, MAC2STR(sta->addr));
2758 /* Make sure that TIM is set for the station (it might not be
2759 * after AP wlan hw reset). */
2760 /* FIX: should fix hw reset to restore bits based on STA
2761 * buffer state.. */
2762 hostap_set_tim(local, sta->aid, 1);
2763 sta->flags |= WLAN_STA_TIM;
2764 ret = AP_TX_DROP;
2765 goto out;
2766 }
2767
2768 /* STA in PS mode, buffer frame for later delivery */
2769 set_tim = skb_queue_empty(&sta->tx_buf);
2770 skb_queue_tail(&sta->tx_buf, skb);
2771 /* FIX: could save RX time to skb and expire buffered frames after
2772 * some time if STA does not poll for them */
2773
2774 if (set_tim) {
2775 if (sta->flags & WLAN_STA_TIM)
2776 PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
2777 sta->aid);
2778 hostap_set_tim(local, sta->aid, 1);
2779 sta->flags |= WLAN_STA_TIM;
2780 }
2781
2782 ret = AP_TX_BUFFERED;
2783
2784 out:
2785 if (sta != NULL) {
2786 if (ret == AP_TX_CONTINUE ||
2787 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
2788 sta->tx_packets++;
2789 sta->tx_bytes += skb->len;
2790 sta->last_tx = jiffies;
2791 }
2792
2793 if ((ret == AP_TX_CONTINUE ||
2794 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
2795 sta->crypt && tx->host_encrypt) {
2796 tx->crypt = sta->crypt;
2797 tx->sta_ptr = sta; /* hostap_handle_sta_release() will
2798 * be called to release sta info
2799 * later */
2800 } else
2801 atomic_dec(&sta->users);
2802 }
2803
2804 return ret;
2805}
2806
2807
2808void hostap_handle_sta_release(void *ptr)
2809{
2810 struct sta_info *sta = ptr;
2811 atomic_dec(&sta->users);
2812}
2813
2814
2815/* Called only as a tasklet (software IRQ) */
2816void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
2817{
2818 struct sta_info *sta;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002819 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002820 struct hostap_skb_tx_data *meta;
2821
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002822 hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002823 meta = (struct hostap_skb_tx_data *) skb->cb;
2824
2825 spin_lock(&local->ap->sta_table_lock);
2826 sta = ap_get_sta(local->ap, hdr->addr1);
2827 if (!sta) {
2828 spin_unlock(&local->ap->sta_table_lock);
2829 PDEBUG(DEBUG_AP, "%s: Could not find STA " MACSTR " for this "
2830 "TX error (@%lu)\n",
2831 local->dev->name, MAC2STR(hdr->addr1), jiffies);
2832 return;
2833 }
2834
2835 sta->tx_since_last_failure = 0;
2836 sta->tx_consecutive_exc++;
Jeff Garzik74fae822005-07-31 13:08:32 -04002837
Jouni Malinenff1d2762005-05-12 22:54:16 -04002838 if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
2839 sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
2840 /* use next lower rate */
2841 int old, rate;
2842 old = rate = sta->tx_rate_idx;
2843 while (rate > 0) {
2844 rate--;
2845 if (ap_tx_rate_ok(rate, sta, local)) {
2846 sta->tx_rate_idx = rate;
2847 break;
2848 }
2849 }
2850 if (old != sta->tx_rate_idx) {
2851 switch (sta->tx_rate_idx) {
2852 case 0: sta->tx_rate = 10; break;
2853 case 1: sta->tx_rate = 20; break;
2854 case 2: sta->tx_rate = 55; break;
2855 case 3: sta->tx_rate = 110; break;
2856 default: sta->tx_rate = 0; break;
2857 }
2858 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " TX rate lowered "
2859 "to %d\n", local->dev->name, MAC2STR(sta->addr),
2860 sta->tx_rate);
2861 }
2862 sta->tx_consecutive_exc = 0;
2863 }
2864 spin_unlock(&local->ap->sta_table_lock);
2865}
2866
2867
2868static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
2869 int pwrmgt, int type, int stype)
2870{
2871 if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
2872 sta->flags |= WLAN_STA_PS;
2873 PDEBUG(DEBUG_PS2, "STA " MACSTR " changed to use PS "
2874 "mode (type=0x%02X, stype=0x%02X)\n",
2875 MAC2STR(sta->addr), type, stype);
2876 } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
2877 sta->flags &= ~WLAN_STA_PS;
2878 PDEBUG(DEBUG_PS2, "STA " MACSTR " changed to not use "
2879 "PS mode (type=0x%02X, stype=0x%02X)\n",
2880 MAC2STR(sta->addr), type, stype);
2881 if (type != WLAN_FC_TYPE_CTRL || stype != WLAN_FC_STYPE_PSPOLL)
2882 schedule_packet_send(local, sta);
2883 }
2884}
2885
2886
2887/* Called only as a tasklet (software IRQ). Called for each RX frame to update
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002888 * STA power saving state. pwrmgt is a flag from 802.11 frame_ctl field. */
2889int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002890{
2891 struct sta_info *sta;
2892 u16 fc;
2893
2894 spin_lock(&local->ap->sta_table_lock);
2895 sta = ap_get_sta(local->ap, hdr->addr2);
2896 if (sta)
2897 atomic_inc(&sta->users);
2898 spin_unlock(&local->ap->sta_table_lock);
2899
2900 if (!sta)
2901 return -1;
2902
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002903 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002904 hostap_update_sta_ps2(local, sta, fc & WLAN_FC_PWRMGT,
Jouni Malinenebed67d2005-07-30 20:43:19 -07002905 HOSTAP_FC_GET_TYPE(fc), HOSTAP_FC_GET_STYPE(fc));
Jouni Malinenff1d2762005-05-12 22:54:16 -04002906
2907 atomic_dec(&sta->users);
2908 return 0;
2909}
2910
2911
2912/* Called only as a tasklet (software IRQ). Called for each RX frame after
2913 * getting RX header and payload from hardware. */
2914ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
2915 struct sk_buff *skb,
2916 struct hostap_80211_rx_status *rx_stats,
2917 int wds)
2918{
2919 int ret;
2920 struct sta_info *sta;
2921 u16 fc, type, stype;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002922 struct ieee80211_hdr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002923
2924 if (local->ap == NULL)
2925 return AP_RX_CONTINUE;
2926
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002927 hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002928
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002929 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenebed67d2005-07-30 20:43:19 -07002930 type = HOSTAP_FC_GET_TYPE(fc);
2931 stype = HOSTAP_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002932
2933 spin_lock(&local->ap->sta_table_lock);
2934 sta = ap_get_sta(local->ap, hdr->addr2);
2935 if (sta)
2936 atomic_inc(&sta->users);
2937 spin_unlock(&local->ap->sta_table_lock);
2938
2939 if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
2940 ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
2941 else
2942 ret = AP_RX_CONTINUE;
2943
2944
2945 if (fc & WLAN_FC_TODS) {
2946 if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2947 if (local->hostapd) {
2948 prism2_rx_80211(local->apdev, skb, rx_stats,
2949 PRISM2_RX_NON_ASSOC);
2950#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2951 } else {
2952 printk(KERN_DEBUG "%s: dropped received packet"
2953 " from non-associated STA " MACSTR
2954 " (type=0x%02x, subtype=0x%02x)\n",
2955 dev->name, MAC2STR(hdr->addr2), type,
2956 stype);
2957 hostap_rx(dev, skb, rx_stats);
2958#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2959 }
2960 ret = AP_RX_EXIT;
2961 goto out;
2962 }
2963 } else if (fc & WLAN_FC_FROMDS) {
2964 if (!wds) {
2965 /* FromDS frame - not for us; probably
2966 * broadcast/multicast in another BSS - drop */
2967 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2968 printk(KERN_DEBUG "Odd.. FromDS packet "
2969 "received with own BSSID\n");
2970 hostap_dump_rx_80211(dev->name, skb, rx_stats);
2971 }
2972 ret = AP_RX_DROP;
2973 goto out;
2974 }
2975 } else if (stype == WLAN_FC_STYPE_NULLFUNC && sta == NULL &&
2976 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2977
2978 if (local->hostapd) {
2979 prism2_rx_80211(local->apdev, skb, rx_stats,
2980 PRISM2_RX_NON_ASSOC);
2981#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2982 } else {
2983 /* At least Lucent f/w seems to send data::nullfunc
2984 * frames with no ToDS flag when the current AP returns
2985 * after being unavailable for some time. Speed up
2986 * re-association by informing the station about it not
2987 * being associated. */
2988 printk(KERN_DEBUG "%s: rejected received nullfunc "
2989 "frame without ToDS from not associated STA "
2990 MACSTR "\n",
2991 dev->name, MAC2STR(hdr->addr2));
2992 hostap_rx(dev, skb, rx_stats);
2993#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2994 }
2995 ret = AP_RX_EXIT;
2996 goto out;
2997 } else if (stype == WLAN_FC_STYPE_NULLFUNC) {
2998 /* At least Lucent cards seem to send periodic nullfunc
2999 * frames with ToDS. Let these through to update SQ
3000 * stats and PS state. Nullfunc frames do not contain
3001 * any data and they will be dropped below. */
3002 } else {
3003 /* If BSSID (Addr3) is foreign, this frame is a normal
3004 * broadcast frame from an IBSS network. Drop it silently.
3005 * If BSSID is own, report the dropping of this frame. */
3006 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
3007 printk(KERN_DEBUG "%s: dropped received packet from "
3008 MACSTR " with no ToDS flag (type=0x%02x, "
3009 "subtype=0x%02x)\n", dev->name,
3010 MAC2STR(hdr->addr2), type, stype);
3011 hostap_dump_rx_80211(dev->name, skb, rx_stats);
3012 }
3013 ret = AP_RX_DROP;
3014 goto out;
3015 }
3016
3017 if (sta) {
3018 hostap_update_sta_ps2(local, sta, fc & WLAN_FC_PWRMGT,
3019 type, stype);
3020
3021 sta->rx_packets++;
3022 sta->rx_bytes += skb->len;
3023 sta->last_rx = jiffies;
3024 }
3025
3026 if (local->ap->nullfunc_ack && stype == WLAN_FC_STYPE_NULLFUNC &&
3027 fc & WLAN_FC_TODS) {
3028 if (local->hostapd) {
3029 prism2_rx_80211(local->apdev, skb, rx_stats,
3030 PRISM2_RX_NULLFUNC_ACK);
3031#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3032 } else {
3033 /* some STA f/w's seem to require control::ACK frame
3034 * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
3035 * from Compaq) does not send this.. Try to generate
3036 * ACK for these frames from the host driver to make
3037 * power saving work with, e.g., Lucent WaveLAN f/w */
3038 hostap_rx(dev, skb, rx_stats);
3039#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3040 }
3041 ret = AP_RX_EXIT;
3042 goto out;
3043 }
3044
3045 out:
3046 if (sta)
3047 atomic_dec(&sta->users);
3048
3049 return ret;
3050}
3051
3052
3053/* Called only as a tasklet (software IRQ) */
3054int hostap_handle_sta_crypto(local_info_t *local,
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07003055 struct ieee80211_hdr *hdr,
Jouni Malinen62fe7e32005-07-30 20:43:20 -07003056 struct ieee80211_crypt_data **crypt,
3057 void **sta_ptr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003058{
3059 struct sta_info *sta;
3060
3061 spin_lock(&local->ap->sta_table_lock);
3062 sta = ap_get_sta(local->ap, hdr->addr2);
3063 if (sta)
3064 atomic_inc(&sta->users);
3065 spin_unlock(&local->ap->sta_table_lock);
3066
3067 if (!sta)
3068 return -1;
3069
3070 if (sta->crypt) {
3071 *crypt = sta->crypt;
3072 *sta_ptr = sta;
3073 /* hostap_handle_sta_release() will be called to release STA
3074 * info */
3075 } else
3076 atomic_dec(&sta->users);
3077
3078 return 0;
3079}
3080
3081
3082/* Called only as a tasklet (software IRQ) */
3083int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
3084{
3085 struct sta_info *sta;
3086 int ret = 0;
3087
3088 spin_lock(&ap->sta_table_lock);
3089 sta = ap_get_sta(ap, sta_addr);
3090 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
3091 ret = 1;
3092 spin_unlock(&ap->sta_table_lock);
3093
3094 return ret;
3095}
3096
3097
3098/* Called only as a tasklet (software IRQ) */
3099int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
3100{
3101 struct sta_info *sta;
3102 int ret = 0;
3103
3104 spin_lock(&ap->sta_table_lock);
3105 sta = ap_get_sta(ap, sta_addr);
3106 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
3107 ((sta->flags & WLAN_STA_AUTHORIZED) ||
3108 ap->local->ieee_802_1x == 0))
3109 ret = 1;
3110 spin_unlock(&ap->sta_table_lock);
3111
3112 return ret;
3113}
3114
3115
3116/* Called only as a tasklet (software IRQ) */
3117int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
3118{
3119 struct sta_info *sta;
3120 int ret = 1;
3121
3122 if (!ap)
3123 return -1;
3124
3125 spin_lock(&ap->sta_table_lock);
3126 sta = ap_get_sta(ap, sta_addr);
3127 if (sta)
3128 ret = 0;
3129 spin_unlock(&ap->sta_table_lock);
3130
3131 if (ret == 1) {
3132 sta = ap_add_sta(ap, sta_addr);
3133 if (!sta)
3134 ret = -1;
3135 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
3136 sta->ap = 1;
3137 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
3138 /* No way of knowing which rates are supported since we did not
3139 * get supported rates element from beacon/assoc req. Assume
3140 * that remote end supports all 802.11b rates. */
3141 sta->supported_rates[0] = 0x82;
3142 sta->supported_rates[1] = 0x84;
3143 sta->supported_rates[2] = 0x0b;
3144 sta->supported_rates[3] = 0x16;
3145 sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
3146 WLAN_RATE_5M5 | WLAN_RATE_11M;
3147 sta->tx_rate = 110;
3148 sta->tx_max_rate = sta->tx_rate_idx = 3;
3149 }
3150
3151 return ret;
3152}
3153
3154
3155/* Called only as a tasklet (software IRQ) */
3156int hostap_update_rx_stats(struct ap_data *ap,
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07003157 struct ieee80211_hdr *hdr,
Jouni Malinenff1d2762005-05-12 22:54:16 -04003158 struct hostap_80211_rx_status *rx_stats)
3159{
3160 struct sta_info *sta;
3161
3162 if (!ap)
3163 return -1;
3164
3165 spin_lock(&ap->sta_table_lock);
3166 sta = ap_get_sta(ap, hdr->addr2);
3167 if (sta) {
3168 sta->last_rx_silence = rx_stats->noise;
3169 sta->last_rx_signal = rx_stats->signal;
3170 sta->last_rx_rate = rx_stats->rate;
3171 sta->last_rx_updated = 7;
3172 if (rx_stats->rate == 10)
3173 sta->rx_count[0]++;
3174 else if (rx_stats->rate == 20)
3175 sta->rx_count[1]++;
3176 else if (rx_stats->rate == 55)
3177 sta->rx_count[2]++;
3178 else if (rx_stats->rate == 110)
3179 sta->rx_count[3]++;
3180 }
3181 spin_unlock(&ap->sta_table_lock);
3182
3183 return sta ? 0 : -1;
3184}
3185
3186
3187void hostap_update_rates(local_info_t *local)
3188{
3189 struct list_head *ptr;
3190 struct ap_data *ap = local->ap;
3191
3192 if (!ap)
3193 return;
3194
3195 spin_lock_bh(&ap->sta_table_lock);
3196 for (ptr = ap->sta_list.next; ptr != &ap->sta_list; ptr = ptr->next) {
3197 struct sta_info *sta = (struct sta_info *) ptr;
3198 prism2_check_tx_rates(sta);
3199 }
3200 spin_unlock_bh(&ap->sta_table_lock);
3201}
3202
3203
3204static void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
Jouni Malinen62fe7e32005-07-30 20:43:20 -07003205 struct ieee80211_crypt_data ***crypt)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003206{
3207 struct sta_info *sta;
3208
3209 spin_lock_bh(&ap->sta_table_lock);
3210 sta = ap_get_sta(ap, addr);
3211 if (sta)
3212 atomic_inc(&sta->users);
3213 spin_unlock_bh(&ap->sta_table_lock);
3214
3215 if (!sta && permanent)
3216 sta = ap_add_sta(ap, addr);
3217
3218 if (!sta)
3219 return NULL;
3220
3221 if (permanent)
3222 sta->flags |= WLAN_STA_PERM;
3223
3224 *crypt = &sta->crypt;
3225
3226 return sta;
3227}
3228
3229
3230void hostap_add_wds_links(local_info_t *local)
3231{
3232 struct ap_data *ap = local->ap;
3233 struct list_head *ptr;
3234
3235 spin_lock_bh(&ap->sta_table_lock);
3236 list_for_each(ptr, &ap->sta_list) {
3237 struct sta_info *sta = list_entry(ptr, struct sta_info, list);
3238 if (sta->ap)
3239 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
3240 }
3241 spin_unlock_bh(&ap->sta_table_lock);
3242
3243 schedule_work(&local->ap->wds_oper_queue);
3244}
3245
3246
3247void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
3248{
3249 struct wds_oper_data *entry;
3250
3251 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
3252 if (!entry)
3253 return;
3254 memcpy(entry->addr, addr, ETH_ALEN);
3255 entry->type = type;
3256 spin_lock_bh(&local->lock);
3257 entry->next = local->ap->wds_oper_entries;
3258 local->ap->wds_oper_entries = entry;
3259 spin_unlock_bh(&local->lock);
3260
3261 schedule_work(&local->ap->wds_oper_queue);
3262}
3263
3264
3265EXPORT_SYMBOL(hostap_init_data);
3266EXPORT_SYMBOL(hostap_init_ap_proc);
3267EXPORT_SYMBOL(hostap_free_data);
3268EXPORT_SYMBOL(hostap_check_sta_fw_version);
3269EXPORT_SYMBOL(hostap_handle_sta_tx);
3270EXPORT_SYMBOL(hostap_handle_sta_release);
3271EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
3272EXPORT_SYMBOL(hostap_update_sta_ps);
3273EXPORT_SYMBOL(hostap_handle_sta_rx);
3274EXPORT_SYMBOL(hostap_is_sta_assoc);
3275EXPORT_SYMBOL(hostap_is_sta_authorized);
3276EXPORT_SYMBOL(hostap_add_sta);
3277EXPORT_SYMBOL(hostap_update_rates);
3278EXPORT_SYMBOL(hostap_add_wds_links);
3279EXPORT_SYMBOL(hostap_wds_link_oper);
3280#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3281EXPORT_SYMBOL(hostap_deauth_all_stas);
3282#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */