blob: 550855a91509f3c9dca140d47bb003fb95f52bd3 [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;
593 struct hostap_ieee80211_hdr *hdr;
594
595 if (!ap->local->hostapd || !ap->local->apdev) {
596 dev_kfree_skb(skb);
597 return;
598 }
599
600 hdr = (struct hostap_ieee80211_hdr *) skb->data;
601 fc = le16_to_cpu(hdr->frame_control);
602
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);
608 hdr->frame_control = cpu_to_le16(fc);
609
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;
625 struct hostap_ieee80211_hdr *hdr;
626 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
635 hdr = (struct hostap_ieee80211_hdr *) skb->data;
636 fc = le16_to_cpu(hdr->frame_control);
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;
694 struct hostap_ieee80211_hdr *hdr;
695 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
704 hdr = (struct hostap_ieee80211_hdr *) skb->data;
705 fc = le16_to_cpu(hdr->frame_control);
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;
759 struct hostap_ieee80211_hdr *hdr;
760 struct sta_info *sta;
761
762 if (skb->len < 24)
763 goto fail;
764 hdr = (struct hostap_ieee80211_hdr *) skb->data;
765 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;
920 struct hostap_ieee80211_hdr *hdr;
921 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);
946 hdr = (struct hostap_ieee80211_hdr *) skb_put(skb, hdrlen);
947 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
970 hdr->frame_control = cpu_to_le16(fc);
971
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;
1287 struct hostap_ieee80211_hdr *hdr =
1288 (struct hostap_ieee80211_hdr *) skb->data;
1289 size_t hdrlen;
1290 struct ap_data *ap = local->ap;
1291 char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
1292 int len, olen;
1293 u16 auth_alg, auth_transaction, status_code, *pos;
1294 u16 resp = WLAN_STATUS_SUCCESS, fc;
1295 struct sta_info *sta = NULL;
Jouni Malinen62fe7e32005-07-30 20:43:20 -07001296 struct ieee80211_crypt_data *crypt;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001297 char *txt = "";
1298
1299 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1300
1301 fc = le16_to_cpu(hdr->frame_control);
1302 hdrlen = hostap_80211_get_hdrlen(fc);
1303
1304 if (len < 6) {
1305 PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
1306 "(len=%d) from " MACSTR "\n", dev->name, len,
1307 MAC2STR(hdr->addr2));
1308 return;
1309 }
1310
1311 spin_lock_bh(&local->ap->sta_table_lock);
1312 sta = ap_get_sta(local->ap, hdr->addr2);
1313 if (sta)
1314 atomic_inc(&sta->users);
1315 spin_unlock_bh(&local->ap->sta_table_lock);
1316
1317 if (sta && sta->crypt)
1318 crypt = sta->crypt;
1319 else {
1320 int idx = 0;
1321 if (skb->len >= hdrlen + 3)
1322 idx = skb->data[hdrlen + 3] >> 6;
1323 crypt = local->crypt[idx];
1324 }
1325
1326 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1327 auth_alg = __le16_to_cpu(*pos);
1328 pos++;
1329 auth_transaction = __le16_to_cpu(*pos);
1330 pos++;
1331 status_code = __le16_to_cpu(*pos);
1332 pos++;
1333
1334 if (memcmp(dev->dev_addr, hdr->addr2, ETH_ALEN) == 0 ||
1335 ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
1336 txt = "authentication denied";
1337 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1338 goto fail;
1339 }
1340
1341 if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
1342 auth_alg == WLAN_AUTH_OPEN) ||
1343 ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
1344 crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
1345 } else {
1346 txt = "unsupported algorithm";
1347 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1348 goto fail;
1349 }
1350
1351 if (len >= 8) {
1352 u8 *u = (u8 *) pos;
1353 if (*u == WLAN_EID_CHALLENGE) {
1354 if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
1355 txt = "invalid challenge len";
1356 resp = WLAN_STATUS_CHALLENGE_FAIL;
1357 goto fail;
1358 }
1359 if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
1360 txt = "challenge underflow";
1361 resp = WLAN_STATUS_CHALLENGE_FAIL;
1362 goto fail;
1363 }
1364 challenge = (char *) (u + 2);
1365 }
1366 }
1367
1368 if (sta && sta->ap) {
1369 if (time_after(jiffies, sta->u.ap.last_beacon +
1370 (10 * sta->listen_interval * HZ) / 1024)) {
1371 PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
1372 " assuming AP " MACSTR " is now STA\n",
1373 dev->name, MAC2STR(sta->addr));
1374 sta->ap = 0;
1375 sta->flags = 0;
1376 sta->u.sta.challenge = NULL;
1377 } else {
1378 txt = "AP trying to authenticate?";
1379 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1380 goto fail;
1381 }
1382 }
1383
1384 if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
1385 (auth_alg == WLAN_AUTH_SHARED_KEY &&
1386 (auth_transaction == 1 ||
1387 (auth_transaction == 3 && sta != NULL &&
1388 sta->u.sta.challenge != NULL)))) {
1389 } else {
1390 txt = "unknown authentication transaction number";
1391 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1392 goto fail;
1393 }
1394
1395 if (sta == NULL) {
1396 txt = "new STA";
1397
1398 if (local->ap->num_sta >= MAX_STA_COUNT) {
1399 /* FIX: might try to remove some old STAs first? */
1400 txt = "no more room for new STAs";
1401 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1402 goto fail;
1403 }
1404
1405 sta = ap_add_sta(local->ap, hdr->addr2);
1406 if (sta == NULL) {
1407 txt = "ap_add_sta failed";
1408 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1409 goto fail;
1410 }
1411 }
1412
1413 switch (auth_alg) {
1414 case WLAN_AUTH_OPEN:
1415 txt = "authOK";
1416 /* IEEE 802.11 standard is not completely clear about
1417 * whether STA is considered authenticated after
1418 * authentication OK frame has been send or after it
1419 * has been ACKed. In order to reduce interoperability
1420 * issues, mark the STA authenticated before ACK. */
1421 sta->flags |= WLAN_STA_AUTH;
1422 break;
1423
1424 case WLAN_AUTH_SHARED_KEY:
1425 if (auth_transaction == 1) {
1426 if (sta->u.sta.challenge == NULL) {
1427 sta->u.sta.challenge =
1428 ap_auth_make_challenge(local->ap);
1429 if (sta->u.sta.challenge == NULL) {
1430 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1431 goto fail;
1432 }
1433 }
1434 } else {
1435 if (sta->u.sta.challenge == NULL ||
1436 challenge == NULL ||
1437 memcmp(sta->u.sta.challenge, challenge,
1438 WLAN_AUTH_CHALLENGE_LEN) != 0 ||
1439 !(fc & WLAN_FC_ISWEP)) {
1440 txt = "challenge response incorrect";
1441 resp = WLAN_STATUS_CHALLENGE_FAIL;
1442 goto fail;
1443 }
1444
1445 txt = "challenge OK - authOK";
1446 /* IEEE 802.11 standard is not completely clear about
1447 * whether STA is considered authenticated after
1448 * authentication OK frame has been send or after it
1449 * has been ACKed. In order to reduce interoperability
1450 * issues, mark the STA authenticated before ACK. */
1451 sta->flags |= WLAN_STA_AUTH;
1452 kfree(sta->u.sta.challenge);
1453 sta->u.sta.challenge = NULL;
1454 }
1455 break;
1456 }
1457
1458 fail:
1459 pos = (u16 *) body;
1460 *pos = cpu_to_le16(auth_alg);
1461 pos++;
1462 *pos = cpu_to_le16(auth_transaction + 1);
1463 pos++;
1464 *pos = cpu_to_le16(resp); /* status_code */
1465 pos++;
1466 olen = 6;
1467
1468 if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
1469 sta->u.sta.challenge != NULL &&
1470 auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
1471 u8 *tmp = (u8 *) pos;
1472 *tmp++ = WLAN_EID_CHALLENGE;
1473 *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
1474 pos++;
1475 memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
1476 olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
1477 }
1478
1479 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_AUTH,
1480 body, olen, hdr->addr2, ap->tx_callback_auth);
1481
1482 if (sta) {
1483 sta->last_rx = jiffies;
1484 atomic_dec(&sta->users);
1485 }
1486
1487 if (resp) {
1488 PDEBUG(DEBUG_AP, "%s: " MACSTR " auth (alg=%d trans#=%d "
1489 "stat=%d len=%d fc=%04x) ==> %d (%s)\n",
1490 dev->name, MAC2STR(hdr->addr2), auth_alg,
1491 auth_transaction, status_code, len, fc, resp, txt);
1492 }
1493}
1494
1495
1496/* Called only as a scheduled task for pending AP frames. */
1497static void handle_assoc(local_info_t *local, struct sk_buff *skb,
1498 struct hostap_80211_rx_status *rx_stats, int reassoc)
1499{
1500 struct net_device *dev = local->dev;
1501 struct hostap_ieee80211_hdr *hdr =
1502 (struct hostap_ieee80211_hdr *) skb->data;
1503 char body[12], *p, *lpos;
1504 int len, left;
1505 u16 *pos;
1506 u16 resp = WLAN_STATUS_SUCCESS;
1507 struct sta_info *sta = NULL;
1508 int send_deauth = 0;
1509 char *txt = "";
1510 u8 prev_ap[ETH_ALEN];
1511
1512 left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
1513
1514 if (len < (reassoc ? 10 : 4)) {
1515 PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
1516 "(len=%d, reassoc=%d) from " MACSTR "\n",
1517 dev->name, len, reassoc, MAC2STR(hdr->addr2));
1518 return;
1519 }
1520
1521 spin_lock_bh(&local->ap->sta_table_lock);
1522 sta = ap_get_sta(local->ap, hdr->addr2);
1523 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1524 spin_unlock_bh(&local->ap->sta_table_lock);
1525 txt = "trying to associate before authentication";
1526 send_deauth = 1;
1527 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1528 sta = NULL; /* do not decrement sta->users */
1529 goto fail;
1530 }
1531 atomic_inc(&sta->users);
1532 spin_unlock_bh(&local->ap->sta_table_lock);
1533
1534 pos = (u16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1535 sta->capability = __le16_to_cpu(*pos);
1536 pos++; left -= 2;
1537 sta->listen_interval = __le16_to_cpu(*pos);
1538 pos++; left -= 2;
1539
1540 if (reassoc) {
1541 memcpy(prev_ap, pos, ETH_ALEN);
1542 pos++; pos++; pos++; left -= 6;
1543 } else
1544 memset(prev_ap, 0, ETH_ALEN);
1545
1546 if (left >= 2) {
1547 unsigned int ileft;
1548 unsigned char *u = (unsigned char *) pos;
1549
1550 if (*u == WLAN_EID_SSID) {
1551 u++; left--;
1552 ileft = *u;
1553 u++; left--;
1554
1555 if (ileft > left || ileft > MAX_SSID_LEN) {
1556 txt = "SSID overflow";
1557 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1558 goto fail;
1559 }
1560
1561 if (ileft != strlen(local->essid) ||
1562 memcmp(local->essid, u, ileft) != 0) {
1563 txt = "not our SSID";
1564 resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1565 goto fail;
1566 }
1567
1568 u += ileft;
1569 left -= ileft;
1570 }
1571
1572 if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
1573 u++; left--;
1574 ileft = *u;
1575 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04001576
Jouni Malinenff1d2762005-05-12 22:54:16 -04001577 if (ileft > left || ileft == 0 ||
1578 ileft > WLAN_SUPP_RATES_MAX) {
1579 txt = "SUPP_RATES len error";
1580 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1581 goto fail;
1582 }
1583
1584 memset(sta->supported_rates, 0,
1585 sizeof(sta->supported_rates));
1586 memcpy(sta->supported_rates, u, ileft);
1587 prism2_check_tx_rates(sta);
1588
1589 u += ileft;
1590 left -= ileft;
1591 }
1592
1593 if (left > 0) {
1594 PDEBUG(DEBUG_AP, "%s: assoc from " MACSTR " with extra"
1595 " data (%d bytes) [",
1596 dev->name, MAC2STR(hdr->addr2), left);
1597 while (left > 0) {
1598 PDEBUG2(DEBUG_AP, "<%02x>", *u);
1599 u++; left--;
1600 }
1601 PDEBUG2(DEBUG_AP, "]\n");
1602 }
1603 } else {
1604 txt = "frame underflow";
1605 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1606 goto fail;
1607 }
1608
1609 /* get a unique AID */
1610 if (sta->aid > 0)
1611 txt = "OK, old AID";
1612 else {
1613 spin_lock_bh(&local->ap->sta_table_lock);
1614 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1615 if (local->ap->sta_aid[sta->aid - 1] == NULL)
1616 break;
1617 if (sta->aid > MAX_AID_TABLE_SIZE) {
1618 sta->aid = 0;
1619 spin_unlock_bh(&local->ap->sta_table_lock);
1620 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1621 txt = "no room for more AIDs";
1622 } else {
1623 local->ap->sta_aid[sta->aid - 1] = sta;
1624 spin_unlock_bh(&local->ap->sta_table_lock);
1625 txt = "OK, new AID";
1626 }
1627 }
1628
1629 fail:
1630 pos = (u16 *) body;
1631
1632 if (send_deauth) {
1633 *pos = __constant_cpu_to_le16(
1634 WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
1635 pos++;
1636 } else {
1637 /* FIX: CF-Pollable and CF-PollReq should be set to match the
1638 * values in beacons/probe responses */
1639 /* FIX: how about privacy and WEP? */
1640 /* capability */
1641 *pos = __constant_cpu_to_le16(WLAN_CAPABILITY_ESS);
1642 pos++;
1643
1644 /* status_code */
1645 *pos = __cpu_to_le16(resp);
1646 pos++;
1647
1648 *pos = __cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
1649 BIT(14) | BIT(15)); /* AID */
1650 pos++;
1651
1652 /* Supported rates (Information element) */
1653 p = (char *) pos;
1654 *p++ = WLAN_EID_SUPP_RATES;
1655 lpos = p;
1656 *p++ = 0; /* len */
1657 if (local->tx_rate_control & WLAN_RATE_1M) {
1658 *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
1659 (*lpos)++;
1660 }
1661 if (local->tx_rate_control & WLAN_RATE_2M) {
1662 *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
1663 (*lpos)++;
1664 }
1665 if (local->tx_rate_control & WLAN_RATE_5M5) {
1666 *p++ = local->basic_rates & WLAN_RATE_5M5 ?
1667 0x8b : 0x0b;
1668 (*lpos)++;
1669 }
1670 if (local->tx_rate_control & WLAN_RATE_11M) {
1671 *p++ = local->basic_rates & WLAN_RATE_11M ?
1672 0x96 : 0x16;
1673 (*lpos)++;
1674 }
1675 pos = (u16 *) p;
1676 }
1677
1678 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT,
1679 (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1680 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1681 WLAN_FC_STYPE_ASSOC_RESP)),
1682 body, (u8 *) pos - (u8 *) body,
1683 hdr->addr2,
1684 send_deauth ? 0 : local->ap->tx_callback_assoc);
1685
1686 if (sta) {
1687 if (resp == WLAN_STATUS_SUCCESS) {
1688 sta->last_rx = jiffies;
1689 /* STA will be marked associated from TX callback, if
1690 * AssocResp is ACKed */
1691 }
1692 atomic_dec(&sta->users);
1693 }
1694
1695#if 0
1696 PDEBUG(DEBUG_AP, "%s: " MACSTR " %sassoc (len=%d prev_ap=" MACSTR
1697 ") => %d(%d) (%s)\n",
1698 dev->name, MAC2STR(hdr->addr2), reassoc ? "re" : "", len,
1699 MAC2STR(prev_ap), resp, send_deauth, txt);
1700#endif
1701}
1702
1703
1704/* Called only as a scheduled task for pending AP frames. */
1705static void handle_deauth(local_info_t *local, struct sk_buff *skb,
1706 struct hostap_80211_rx_status *rx_stats)
1707{
1708 struct net_device *dev = local->dev;
1709 struct hostap_ieee80211_hdr *hdr =
1710 (struct hostap_ieee80211_hdr *) skb->data;
1711 char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1712 int len;
1713 u16 reason_code, *pos;
1714 struct sta_info *sta = NULL;
1715
1716 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1717
1718 if (len < 2) {
1719 printk("handle_deauth - too short payload (len=%d)\n", len);
1720 return;
1721 }
1722
1723 pos = (u16 *) body;
1724 reason_code = __le16_to_cpu(*pos);
1725
1726 PDEBUG(DEBUG_AP, "%s: deauthentication: " MACSTR " len=%d, "
1727 "reason_code=%d\n", dev->name, MAC2STR(hdr->addr2), len,
1728 reason_code);
1729
1730 spin_lock_bh(&local->ap->sta_table_lock);
1731 sta = ap_get_sta(local->ap, hdr->addr2);
1732 if (sta != NULL) {
1733 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1734 hostap_event_expired_sta(local->dev, sta);
1735 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1736 }
1737 spin_unlock_bh(&local->ap->sta_table_lock);
1738 if (sta == NULL) {
1739 printk("%s: deauthentication from " MACSTR ", "
1740 "reason_code=%d, but STA not authenticated\n", dev->name,
1741 MAC2STR(hdr->addr2), reason_code);
1742 }
1743}
1744
1745
1746/* Called only as a scheduled task for pending AP frames. */
1747static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
1748 struct hostap_80211_rx_status *rx_stats)
1749{
1750 struct net_device *dev = local->dev;
1751 struct hostap_ieee80211_hdr *hdr =
1752 (struct hostap_ieee80211_hdr *) skb->data;
1753 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1754 int len;
1755 u16 reason_code, *pos;
1756 struct sta_info *sta = NULL;
1757
1758 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1759
1760 if (len < 2) {
1761 printk("handle_disassoc - too short payload (len=%d)\n", len);
1762 return;
1763 }
1764
1765 pos = (u16 *) body;
1766 reason_code = __le16_to_cpu(*pos);
1767
1768 PDEBUG(DEBUG_AP, "%s: disassociation: " MACSTR " len=%d, "
1769 "reason_code=%d\n", dev->name, MAC2STR(hdr->addr2), len,
1770 reason_code);
1771
1772 spin_lock_bh(&local->ap->sta_table_lock);
1773 sta = ap_get_sta(local->ap, hdr->addr2);
1774 if (sta != NULL) {
1775 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1776 hostap_event_expired_sta(local->dev, sta);
1777 sta->flags &= ~WLAN_STA_ASSOC;
1778 }
1779 spin_unlock_bh(&local->ap->sta_table_lock);
1780 if (sta == NULL) {
1781 printk("%s: disassociation from " MACSTR ", "
1782 "reason_code=%d, but STA not authenticated\n",
1783 dev->name, MAC2STR(hdr->addr2), reason_code);
1784 }
1785}
1786
1787
1788/* Called only as a scheduled task for pending AP frames. */
1789static void ap_handle_data_nullfunc(local_info_t *local,
1790 struct hostap_ieee80211_hdr *hdr)
1791{
1792 struct net_device *dev = local->dev;
1793
1794 /* some STA f/w's seem to require control::ACK frame for
1795 * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
1796 * not send this..
1797 * send control::ACK for the data::nullfunc */
1798
1799 printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
1800 prism2_send_mgmt(dev, WLAN_FC_TYPE_CTRL, WLAN_FC_STYPE_ACK,
1801 NULL, 0, hdr->addr2, 0);
1802}
1803
1804
1805/* Called only as a scheduled task for pending AP frames. */
1806static void ap_handle_dropped_data(local_info_t *local,
1807 struct hostap_ieee80211_hdr *hdr)
1808{
1809 struct net_device *dev = local->dev;
1810 struct sta_info *sta;
1811 u16 reason;
1812
1813 spin_lock_bh(&local->ap->sta_table_lock);
1814 sta = ap_get_sta(local->ap, hdr->addr2);
1815 if (sta)
1816 atomic_inc(&sta->users);
1817 spin_unlock_bh(&local->ap->sta_table_lock);
1818
1819 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
1820 PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
1821 atomic_dec(&sta->users);
1822 return;
1823 }
1824
1825 reason = __constant_cpu_to_le16(
1826 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1827 prism2_send_mgmt(dev, WLAN_FC_TYPE_MGMT,
1828 ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
1829 WLAN_FC_STYPE_DEAUTH : WLAN_FC_STYPE_DISASSOC),
1830 (char *) &reason, sizeof(reason), hdr->addr2, 0);
1831
1832 if (sta)
1833 atomic_dec(&sta->users);
1834}
1835
1836#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1837
1838
1839/* Called only as a scheduled task for pending AP frames. */
1840static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
1841 struct sk_buff *skb)
1842{
Jouni Malinen5bee7202005-08-14 19:08:39 -07001843 struct hostap_skb_tx_data *meta;
1844
Jouni Malinenff1d2762005-05-12 22:54:16 -04001845 if (!(sta->flags & WLAN_STA_PS)) {
1846 /* Station has moved to non-PS mode, so send all buffered
1847 * frames using normal device queue. */
1848 dev_queue_xmit(skb);
1849 return;
1850 }
1851
1852 /* add a flag for hostap_handle_sta_tx() to know that this skb should
1853 * be passed through even though STA is using PS */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001854 meta = (struct hostap_skb_tx_data *) skb->cb;
1855 meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001856 if (!skb_queue_empty(&sta->tx_buf)) {
1857 /* indicate to STA that more frames follow */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001858 meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001859 }
1860 dev_queue_xmit(skb);
1861}
1862
1863
1864/* Called only as a scheduled task for pending AP frames. */
1865static void handle_pspoll(local_info_t *local,
1866 struct hostap_ieee80211_hdr *hdr,
1867 struct hostap_80211_rx_status *rx_stats)
1868{
1869 struct net_device *dev = local->dev;
1870 struct sta_info *sta;
1871 u16 aid;
1872 struct sk_buff *skb;
1873
1874 PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=" MACSTR ", TA=" MACSTR
1875 " PWRMGT=%d\n",
1876 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1877 !!(le16_to_cpu(hdr->frame_control) & WLAN_FC_PWRMGT));
1878
1879 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
1880 PDEBUG(DEBUG_AP, "handle_pspoll - addr1(BSSID)=" MACSTR
1881 " not own MAC\n", MAC2STR(hdr->addr1));
1882 return;
1883 }
1884
1885 aid = __le16_to_cpu(hdr->duration_id);
1886 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
1887 PDEBUG(DEBUG_PS, " PSPOLL and AID[15:14] not set\n");
1888 return;
1889 }
1890 aid &= ~BIT(15) & ~BIT(14);
1891 if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
1892 PDEBUG(DEBUG_PS, " invalid aid=%d\n", aid);
1893 return;
1894 }
1895 PDEBUG(DEBUG_PS2, " aid=%d\n", aid);
1896
1897 spin_lock_bh(&local->ap->sta_table_lock);
1898 sta = ap_get_sta(local->ap, hdr->addr2);
1899 if (sta)
1900 atomic_inc(&sta->users);
1901 spin_unlock_bh(&local->ap->sta_table_lock);
1902
1903 if (sta == NULL) {
1904 PDEBUG(DEBUG_PS, " STA not found\n");
1905 return;
1906 }
1907 if (sta->aid != aid) {
1908 PDEBUG(DEBUG_PS, " received aid=%i does not match with "
1909 "assoc.aid=%d\n", aid, sta->aid);
1910 return;
1911 }
1912
1913 /* FIX: todo:
1914 * - add timeout for buffering (clear aid in TIM vector if buffer timed
1915 * out (expiry time must be longer than ListenInterval for
1916 * the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
1917 * - what to do, if buffered, pspolled, and sent frame is not ACKed by
1918 * sta; store buffer for later use and leave TIM aid bit set? use
1919 * TX event to check whether frame was ACKed?
1920 */
1921
1922 while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
1923 /* send buffered frame .. */
1924 PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
1925 " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
1926
1927 pspoll_send_buffered(local, sta, skb);
1928
1929 if (sta->flags & WLAN_STA_PS) {
1930 /* send only one buffered packet per PS Poll */
1931 /* FIX: should ignore further PS Polls until the
1932 * buffered packet that was just sent is acknowledged
1933 * (Tx or TxExc event) */
1934 break;
1935 }
1936 }
1937
1938 if (skb_queue_empty(&sta->tx_buf)) {
1939 /* try to clear aid from TIM */
1940 if (!(sta->flags & WLAN_STA_TIM))
1941 PDEBUG(DEBUG_PS2, "Re-unsetting TIM for aid %d\n",
1942 aid);
1943 hostap_set_tim(local, aid, 0);
1944 sta->flags &= ~WLAN_STA_TIM;
1945 }
1946
1947 atomic_dec(&sta->users);
1948}
1949
1950
1951#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1952
1953static void handle_wds_oper_queue(void *data)
1954{
1955 local_info_t *local = data;
1956 struct wds_oper_data *entry, *prev;
1957
1958 spin_lock_bh(&local->lock);
1959 entry = local->ap->wds_oper_entries;
1960 local->ap->wds_oper_entries = NULL;
1961 spin_unlock_bh(&local->lock);
1962
1963 while (entry) {
1964 PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
1965 "to AP " MACSTR "\n",
1966 local->dev->name,
1967 entry->type == WDS_ADD ? "adding" : "removing",
1968 MAC2STR(entry->addr));
1969 if (entry->type == WDS_ADD)
1970 prism2_wds_add(local, entry->addr, 0);
1971 else if (entry->type == WDS_DEL)
1972 prism2_wds_del(local, entry->addr, 0, 1);
1973
1974 prev = entry;
1975 entry = entry->next;
1976 kfree(prev);
1977 }
1978}
1979
1980
1981/* Called only as a scheduled task for pending AP frames. */
1982static void handle_beacon(local_info_t *local, struct sk_buff *skb,
1983 struct hostap_80211_rx_status *rx_stats)
1984{
1985 struct hostap_ieee80211_hdr *hdr =
1986 (struct hostap_ieee80211_hdr *) skb->data;
1987 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1988 int len, left;
1989 u16 *pos, beacon_int, capability;
1990 char *ssid = NULL;
1991 unsigned char *supp_rates = NULL;
1992 int ssid_len = 0, supp_rates_len = 0;
1993 struct sta_info *sta = NULL;
1994 int new_sta = 0, channel = -1;
1995
1996 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1997
1998 if (len < 8 + 2 + 2) {
1999 printk(KERN_DEBUG "handle_beacon - too short payload "
2000 "(len=%d)\n", len);
2001 return;
2002 }
2003
2004 pos = (u16 *) body;
2005 left = len;
2006
2007 /* Timestamp (8 octets) */
2008 pos += 4; left -= 8;
2009 /* Beacon interval (2 octets) */
2010 beacon_int = __le16_to_cpu(*pos);
2011 pos++; left -= 2;
2012 /* Capability information (2 octets) */
2013 capability = __le16_to_cpu(*pos);
2014 pos++; left -= 2;
2015
2016 if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
2017 capability & WLAN_CAPABILITY_IBSS)
2018 return;
2019
2020 if (left >= 2) {
2021 unsigned int ileft;
2022 unsigned char *u = (unsigned char *) pos;
2023
2024 if (*u == WLAN_EID_SSID) {
2025 u++; left--;
2026 ileft = *u;
2027 u++; left--;
2028
2029 if (ileft > left || ileft > MAX_SSID_LEN) {
2030 PDEBUG(DEBUG_AP, "SSID: overflow\n");
2031 return;
2032 }
2033
2034 if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
2035 (ileft != strlen(local->essid) ||
2036 memcmp(local->essid, u, ileft) != 0)) {
2037 /* not our SSID */
2038 return;
2039 }
2040
2041 ssid = u;
2042 ssid_len = ileft;
2043
2044 u += ileft;
2045 left -= ileft;
2046 }
2047
2048 if (*u == WLAN_EID_SUPP_RATES) {
2049 u++; left--;
2050 ileft = *u;
2051 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002052
Jouni Malinenff1d2762005-05-12 22:54:16 -04002053 if (ileft > left || ileft == 0 || ileft > 8) {
2054 PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
2055 return;
2056 }
2057
2058 supp_rates = u;
2059 supp_rates_len = ileft;
2060
2061 u += ileft;
2062 left -= ileft;
2063 }
2064
2065 if (*u == WLAN_EID_DS_PARAMS) {
2066 u++; left--;
2067 ileft = *u;
2068 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002069
Jouni Malinenff1d2762005-05-12 22:54:16 -04002070 if (ileft > left || ileft != 1) {
2071 PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
2072 return;
2073 }
2074
2075 channel = *u;
2076
2077 u += ileft;
2078 left -= ileft;
2079 }
2080 }
2081
2082 spin_lock_bh(&local->ap->sta_table_lock);
2083 sta = ap_get_sta(local->ap, hdr->addr2);
2084 if (sta != NULL)
2085 atomic_inc(&sta->users);
2086 spin_unlock_bh(&local->ap->sta_table_lock);
2087
2088 if (sta == NULL) {
2089 /* add new AP */
2090 new_sta = 1;
2091 sta = ap_add_sta(local->ap, hdr->addr2);
2092 if (sta == NULL) {
2093 printk(KERN_INFO "prism2: kmalloc failed for AP "
2094 "data structure\n");
2095 return;
2096 }
2097 hostap_event_new_sta(local->dev, sta);
2098
2099 /* mark APs authentication and associated for pseudo ad-hoc
2100 * style communication */
2101 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
2102
2103 if (local->ap->autom_ap_wds) {
2104 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
2105 }
2106 }
2107
2108 sta->ap = 1;
2109 if (ssid) {
2110 sta->u.ap.ssid_len = ssid_len;
2111 memcpy(sta->u.ap.ssid, ssid, ssid_len);
2112 sta->u.ap.ssid[ssid_len] = '\0';
2113 } else {
2114 sta->u.ap.ssid_len = 0;
2115 sta->u.ap.ssid[0] = '\0';
2116 }
2117 sta->u.ap.channel = channel;
2118 sta->rx_packets++;
2119 sta->rx_bytes += len;
2120 sta->u.ap.last_beacon = sta->last_rx = jiffies;
2121 sta->capability = capability;
2122 sta->listen_interval = beacon_int;
2123
2124 atomic_dec(&sta->users);
2125
2126 if (new_sta) {
2127 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
2128 memcpy(sta->supported_rates, supp_rates, supp_rates_len);
2129 prism2_check_tx_rates(sta);
2130 }
2131}
2132
2133#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2134
2135
2136/* Called only as a tasklet. */
2137static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
2138 struct hostap_80211_rx_status *rx_stats)
2139{
2140#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2141 struct net_device *dev = local->dev;
2142#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2143 u16 fc, type, stype;
2144 struct hostap_ieee80211_hdr *hdr;
2145
2146 /* FIX: should give skb->len to handler functions and check that the
2147 * buffer is long enough */
2148 hdr = (struct hostap_ieee80211_hdr *) skb->data;
2149 fc = le16_to_cpu(hdr->frame_control);
Jouni Malinenebed67d2005-07-30 20:43:19 -07002150 type = HOSTAP_FC_GET_TYPE(fc);
2151 stype = HOSTAP_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002152
2153#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2154 if (!local->hostapd && type == WLAN_FC_TYPE_DATA) {
2155 PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
2156
2157 if (!(fc & WLAN_FC_TODS) || (fc & WLAN_FC_FROMDS)) {
2158 if (stype == WLAN_FC_STYPE_NULLFUNC) {
2159 /* no ToDS nullfunc seems to be used to check
2160 * AP association; so send reject message to
2161 * speed up re-association */
2162 ap_handle_dropped_data(local, hdr);
2163 goto done;
2164 }
2165 PDEBUG(DEBUG_AP, " not ToDS frame (fc=0x%04x)\n",
2166 fc);
2167 goto done;
2168 }
2169
2170 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
2171 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)="
2172 MACSTR " not own MAC\n",
2173 MAC2STR(hdr->addr1));
2174 goto done;
2175 }
2176
2177 if (local->ap->nullfunc_ack && stype == WLAN_FC_STYPE_NULLFUNC)
2178 ap_handle_data_nullfunc(local, hdr);
2179 else
2180 ap_handle_dropped_data(local, hdr);
2181 goto done;
2182 }
2183
2184 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_BEACON) {
2185 handle_beacon(local, skb, rx_stats);
2186 goto done;
2187 }
2188#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2189
2190 if (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL) {
2191 handle_pspoll(local, hdr, rx_stats);
2192 goto done;
2193 }
2194
2195 if (local->hostapd) {
2196 PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
2197 "subtype=0x%02x\n", type, stype);
2198 goto done;
2199 }
2200
2201#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2202 if (type != WLAN_FC_TYPE_MGMT) {
2203 PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
2204 goto done;
2205 }
2206
2207 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
2208 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=" MACSTR
2209 " not own MAC\n", MAC2STR(hdr->addr1));
2210 goto done;
2211 }
2212
2213 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN)) {
2214 PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=" MACSTR
2215 " not own MAC\n", MAC2STR(hdr->addr3));
2216 goto done;
2217 }
2218
2219 switch (stype) {
2220 case WLAN_FC_STYPE_ASSOC_REQ:
2221 handle_assoc(local, skb, rx_stats, 0);
2222 break;
2223 case WLAN_FC_STYPE_ASSOC_RESP:
2224 PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
2225 break;
2226 case WLAN_FC_STYPE_REASSOC_REQ:
2227 handle_assoc(local, skb, rx_stats, 1);
2228 break;
2229 case WLAN_FC_STYPE_REASSOC_RESP:
2230 PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
2231 break;
2232 case WLAN_FC_STYPE_ATIM:
2233 PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
2234 break;
2235 case WLAN_FC_STYPE_DISASSOC:
2236 handle_disassoc(local, skb, rx_stats);
2237 break;
2238 case WLAN_FC_STYPE_AUTH:
2239 handle_authen(local, skb, rx_stats);
2240 break;
2241 case WLAN_FC_STYPE_DEAUTH:
2242 handle_deauth(local, skb, rx_stats);
2243 break;
2244 default:
2245 PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n", stype);
2246 break;
2247 }
2248#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2249
2250 done:
2251 dev_kfree_skb(skb);
2252}
2253
2254
2255/* Called only as a tasklet (software IRQ) */
2256void hostap_rx(struct net_device *dev, struct sk_buff *skb,
2257 struct hostap_80211_rx_status *rx_stats)
2258{
2259 struct hostap_interface *iface;
2260 local_info_t *local;
2261 u16 fc;
2262 struct hostap_ieee80211_hdr *hdr;
2263
2264 iface = netdev_priv(dev);
2265 local = iface->local;
2266
2267 if (skb->len < 16)
2268 goto drop;
2269
2270 local->stats.rx_packets++;
2271
2272 hdr = (struct hostap_ieee80211_hdr *) skb->data;
2273 fc = le16_to_cpu(hdr->frame_control);
2274
2275 if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
Jouni Malinenebed67d2005-07-30 20:43:19 -07002276 HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2277 HOSTAP_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002278 goto drop;
2279
2280 skb->protocol = __constant_htons(ETH_P_HOSTAP);
2281 handle_ap_item(local, skb, rx_stats);
2282 return;
2283
2284 drop:
2285 dev_kfree_skb(skb);
2286}
2287
2288
2289/* Called only as a tasklet (software IRQ) */
2290static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
2291{
2292 struct sk_buff *skb;
2293 struct hostap_ieee80211_hdr *hdr;
2294 struct hostap_80211_rx_status rx_stats;
2295
2296 if (skb_queue_empty(&sta->tx_buf))
2297 return;
2298
2299 skb = dev_alloc_skb(16);
2300 if (skb == NULL) {
2301 printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
2302 "failed\n", local->dev->name);
2303 return;
2304 }
2305
2306 hdr = (struct hostap_ieee80211_hdr *) skb_put(skb, 16);
2307
2308 /* Generate a fake pspoll frame to start packet delivery */
2309 hdr->frame_control = __constant_cpu_to_le16(
2310 (WLAN_FC_TYPE_CTRL << 2) | (WLAN_FC_STYPE_PSPOLL << 4));
2311 memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
2312 memcpy(hdr->addr2, sta->addr, ETH_ALEN);
2313 hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
2314
2315 PDEBUG(DEBUG_PS2, "%s: Scheduling buffered packet delivery for "
2316 "STA " MACSTR "\n", local->dev->name, MAC2STR(sta->addr));
2317
2318 skb->dev = local->dev;
2319
2320 memset(&rx_stats, 0, sizeof(rx_stats));
2321 hostap_rx(local->dev, skb, &rx_stats);
2322}
2323
2324
2325static int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
2326 struct iw_quality qual[], int buf_size,
2327 int aplist)
2328{
2329 struct ap_data *ap = local->ap;
2330 struct list_head *ptr;
2331 int count = 0;
2332
2333 spin_lock_bh(&ap->sta_table_lock);
2334
2335 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2336 ptr = ptr->next) {
2337 struct sta_info *sta = (struct sta_info *) ptr;
2338
2339 if (aplist && !sta->ap)
2340 continue;
2341 addr[count].sa_family = ARPHRD_ETHER;
2342 memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
2343 if (sta->last_rx_silence == 0)
2344 qual[count].qual = sta->last_rx_signal < 27 ?
2345 0 : (sta->last_rx_signal - 27) * 92 / 127;
2346 else
2347 qual[count].qual = sta->last_rx_signal -
2348 sta->last_rx_silence - 35;
2349 qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2350 qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2351 qual[count].updated = sta->last_rx_updated;
2352
2353 sta->last_rx_updated = 0;
2354
2355 count++;
2356 if (count >= buf_size)
2357 break;
2358 }
2359 spin_unlock_bh(&ap->sta_table_lock);
2360
2361 return count;
2362}
2363
2364
2365/* Translate our list of Access Points & Stations to a card independant
2366 * format that the Wireless Tools will understand - Jean II */
2367static int prism2_ap_translate_scan(struct net_device *dev, char *buffer)
2368{
2369 struct hostap_interface *iface;
2370 local_info_t *local;
2371 struct ap_data *ap;
2372 struct list_head *ptr;
2373 struct iw_event iwe;
2374 char *current_ev = buffer;
2375 char *end_buf = buffer + IW_SCAN_MAX_DATA;
2376#if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
2377 char buf[64];
2378#endif
2379
2380 iface = netdev_priv(dev);
2381 local = iface->local;
2382 ap = local->ap;
2383
2384 spin_lock_bh(&ap->sta_table_lock);
2385
2386 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2387 ptr = ptr->next) {
2388 struct sta_info *sta = (struct sta_info *) ptr;
2389
2390 /* First entry *MUST* be the AP MAC address */
2391 memset(&iwe, 0, sizeof(iwe));
2392 iwe.cmd = SIOCGIWAP;
2393 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2394 memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
2395 iwe.len = IW_EV_ADDR_LEN;
2396 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2397 IW_EV_ADDR_LEN);
2398
2399 /* Use the mode to indicate if it's a station or
2400 * an Access Point */
2401 memset(&iwe, 0, sizeof(iwe));
2402 iwe.cmd = SIOCGIWMODE;
2403 if (sta->ap)
2404 iwe.u.mode = IW_MODE_MASTER;
2405 else
2406 iwe.u.mode = IW_MODE_INFRA;
2407 iwe.len = IW_EV_UINT_LEN;
2408 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2409 IW_EV_UINT_LEN);
2410
2411 /* Some quality */
2412 memset(&iwe, 0, sizeof(iwe));
2413 iwe.cmd = IWEVQUAL;
2414 if (sta->last_rx_silence == 0)
2415 iwe.u.qual.qual = sta->last_rx_signal < 27 ?
2416 0 : (sta->last_rx_signal - 27) * 92 / 127;
2417 else
2418 iwe.u.qual.qual = sta->last_rx_signal -
2419 sta->last_rx_silence - 35;
2420 iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2421 iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2422 iwe.u.qual.updated = sta->last_rx_updated;
2423 iwe.len = IW_EV_QUAL_LEN;
2424 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2425 IW_EV_QUAL_LEN);
2426
2427#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2428 if (sta->ap) {
2429 memset(&iwe, 0, sizeof(iwe));
2430 iwe.cmd = SIOCGIWESSID;
2431 iwe.u.data.length = sta->u.ap.ssid_len;
2432 iwe.u.data.flags = 1;
2433 current_ev = iwe_stream_add_point(current_ev, end_buf,
2434 &iwe,
2435 sta->u.ap.ssid);
2436
2437 memset(&iwe, 0, sizeof(iwe));
2438 iwe.cmd = SIOCGIWENCODE;
2439 if (sta->capability & WLAN_CAPABILITY_PRIVACY)
2440 iwe.u.data.flags =
2441 IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2442 else
2443 iwe.u.data.flags = IW_ENCODE_DISABLED;
2444 current_ev = iwe_stream_add_point(current_ev, end_buf,
2445 &iwe,
2446 sta->u.ap.ssid
2447 /* 0 byte memcpy */);
2448
2449 if (sta->u.ap.channel > 0 &&
2450 sta->u.ap.channel <= FREQ_COUNT) {
2451 memset(&iwe, 0, sizeof(iwe));
2452 iwe.cmd = SIOCGIWFREQ;
2453 iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
2454 * 100000;
2455 iwe.u.freq.e = 1;
2456 current_ev = iwe_stream_add_event(
2457 current_ev, end_buf, &iwe,
2458 IW_EV_FREQ_LEN);
2459 }
2460
2461 memset(&iwe, 0, sizeof(iwe));
2462 iwe.cmd = IWEVCUSTOM;
2463 sprintf(buf, "beacon_interval=%d",
2464 sta->listen_interval);
2465 iwe.u.data.length = strlen(buf);
2466 current_ev = iwe_stream_add_point(current_ev, end_buf,
2467 &iwe, buf);
2468 }
2469#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2470
2471 sta->last_rx_updated = 0;
2472
2473 /* To be continued, we should make good use of IWEVCUSTOM */
2474 }
2475
2476 spin_unlock_bh(&ap->sta_table_lock);
2477
2478 return current_ev - buffer;
2479}
2480
2481
2482static int prism2_hostapd_add_sta(struct ap_data *ap,
2483 struct prism2_hostapd_param *param)
2484{
2485 struct sta_info *sta;
2486
2487 spin_lock_bh(&ap->sta_table_lock);
2488 sta = ap_get_sta(ap, param->sta_addr);
2489 if (sta)
2490 atomic_inc(&sta->users);
2491 spin_unlock_bh(&ap->sta_table_lock);
2492
2493 if (sta == NULL) {
2494 sta = ap_add_sta(ap, param->sta_addr);
2495 if (sta == NULL)
2496 return -1;
2497 }
2498
2499 if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2500 hostap_event_new_sta(sta->local->dev, sta);
2501
2502 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
2503 sta->last_rx = jiffies;
2504 sta->aid = param->u.add_sta.aid;
2505 sta->capability = param->u.add_sta.capability;
2506 sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
2507 if (sta->tx_supp_rates & WLAN_RATE_1M)
2508 sta->supported_rates[0] = 2;
2509 if (sta->tx_supp_rates & WLAN_RATE_2M)
2510 sta->supported_rates[1] = 4;
2511 if (sta->tx_supp_rates & WLAN_RATE_5M5)
2512 sta->supported_rates[2] = 11;
2513 if (sta->tx_supp_rates & WLAN_RATE_11M)
2514 sta->supported_rates[3] = 22;
2515 prism2_check_tx_rates(sta);
2516 atomic_dec(&sta->users);
2517 return 0;
2518}
2519
2520
2521static int prism2_hostapd_remove_sta(struct ap_data *ap,
2522 struct prism2_hostapd_param *param)
2523{
2524 struct sta_info *sta;
2525
2526 spin_lock_bh(&ap->sta_table_lock);
2527 sta = ap_get_sta(ap, param->sta_addr);
2528 if (sta) {
2529 ap_sta_hash_del(ap, sta);
2530 list_del(&sta->list);
2531 }
2532 spin_unlock_bh(&ap->sta_table_lock);
2533
2534 if (!sta)
2535 return -ENOENT;
2536
2537 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2538 hostap_event_expired_sta(sta->local->dev, sta);
2539 ap_free_sta(ap, sta);
2540
2541 return 0;
2542}
2543
2544
2545static int prism2_hostapd_get_info_sta(struct ap_data *ap,
2546 struct prism2_hostapd_param *param)
2547{
2548 struct sta_info *sta;
2549
2550 spin_lock_bh(&ap->sta_table_lock);
2551 sta = ap_get_sta(ap, param->sta_addr);
2552 if (sta)
2553 atomic_inc(&sta->users);
2554 spin_unlock_bh(&ap->sta_table_lock);
2555
2556 if (!sta)
2557 return -ENOENT;
2558
2559 param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
2560
2561 atomic_dec(&sta->users);
2562
2563 return 1;
2564}
2565
2566
2567static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
2568 struct prism2_hostapd_param *param)
2569{
2570 struct sta_info *sta;
2571
2572 spin_lock_bh(&ap->sta_table_lock);
2573 sta = ap_get_sta(ap, param->sta_addr);
2574 if (sta) {
2575 sta->flags |= param->u.set_flags_sta.flags_or;
2576 sta->flags &= param->u.set_flags_sta.flags_and;
2577 }
2578 spin_unlock_bh(&ap->sta_table_lock);
2579
2580 if (!sta)
2581 return -ENOENT;
2582
2583 return 0;
2584}
2585
2586
2587static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
2588 struct prism2_hostapd_param *param)
2589{
2590 struct sta_info *sta;
2591 int rate;
2592
2593 spin_lock_bh(&ap->sta_table_lock);
2594 sta = ap_get_sta(ap, param->sta_addr);
2595 if (sta) {
2596 sta->rx_packets = sta->tx_packets = 0;
2597 sta->rx_bytes = sta->tx_bytes = 0;
2598 for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
2599 sta->tx_count[rate] = 0;
2600 sta->rx_count[rate] = 0;
2601 }
2602 }
2603 spin_unlock_bh(&ap->sta_table_lock);
2604
2605 if (!sta)
2606 return -ENOENT;
2607
2608 return 0;
2609}
2610
2611
2612static int prism2_hostapd(struct ap_data *ap,
2613 struct prism2_hostapd_param *param)
2614{
2615 switch (param->cmd) {
2616 case PRISM2_HOSTAPD_FLUSH:
2617 ap_control_kickall(ap);
2618 return 0;
2619 case PRISM2_HOSTAPD_ADD_STA:
2620 return prism2_hostapd_add_sta(ap, param);
2621 case PRISM2_HOSTAPD_REMOVE_STA:
2622 return prism2_hostapd_remove_sta(ap, param);
2623 case PRISM2_HOSTAPD_GET_INFO_STA:
2624 return prism2_hostapd_get_info_sta(ap, param);
2625 case PRISM2_HOSTAPD_SET_FLAGS_STA:
2626 return prism2_hostapd_set_flags_sta(ap, param);
2627 case PRISM2_HOSTAPD_STA_CLEAR_STATS:
2628 return prism2_hostapd_sta_clear_stats(ap, param);
2629 default:
2630 printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
2631 param->cmd);
2632 return -EOPNOTSUPP;
2633 }
2634}
2635
2636
2637/* Update station info for host-based TX rate control and return current
2638 * TX rate */
2639static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
2640{
2641 int ret = sta->tx_rate;
2642 struct hostap_interface *iface;
2643 local_info_t *local;
2644
2645 iface = netdev_priv(dev);
2646 local = iface->local;
2647
2648 sta->tx_count[sta->tx_rate_idx]++;
2649 sta->tx_since_last_failure++;
2650 sta->tx_consecutive_exc = 0;
2651 if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
2652 sta->tx_rate_idx < sta->tx_max_rate) {
2653 /* use next higher rate */
2654 int old_rate, new_rate;
2655 old_rate = new_rate = sta->tx_rate_idx;
2656 while (new_rate < sta->tx_max_rate) {
2657 new_rate++;
2658 if (ap_tx_rate_ok(new_rate, sta, local)) {
2659 sta->tx_rate_idx = new_rate;
2660 break;
2661 }
2662 }
2663 if (old_rate != sta->tx_rate_idx) {
2664 switch (sta->tx_rate_idx) {
2665 case 0: sta->tx_rate = 10; break;
2666 case 1: sta->tx_rate = 20; break;
2667 case 2: sta->tx_rate = 55; break;
2668 case 3: sta->tx_rate = 110; break;
2669 default: sta->tx_rate = 0; break;
2670 }
2671 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " TX rate raised to"
2672 " %d\n", dev->name, MAC2STR(sta->addr),
2673 sta->tx_rate);
2674 }
2675 sta->tx_since_last_failure = 0;
2676 }
2677
2678 return ret;
2679}
2680
2681
2682/* Called only from software IRQ. Called for each TX frame prior possible
2683 * encryption and transmit. */
2684ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
2685{
2686 struct sta_info *sta = NULL;
2687 struct sk_buff *skb = tx->skb;
2688 int set_tim, ret;
2689 struct hostap_ieee80211_hdr *hdr;
2690 struct hostap_skb_tx_data *meta;
2691
2692 meta = (struct hostap_skb_tx_data *) skb->cb;
2693 ret = AP_TX_CONTINUE;
2694 if (local->ap == NULL || skb->len < 10 ||
2695 meta->iface->type == HOSTAP_INTERFACE_STA)
2696 goto out;
2697
2698 hdr = (struct hostap_ieee80211_hdr *) skb->data;
2699
2700 if (hdr->addr1[0] & 0x01) {
2701 /* broadcast/multicast frame - no AP related processing */
2702 goto out;
2703 }
2704
2705 /* unicast packet - check whether destination STA is associated */
2706 spin_lock(&local->ap->sta_table_lock);
2707 sta = ap_get_sta(local->ap, hdr->addr1);
2708 if (sta)
2709 atomic_inc(&sta->users);
2710 spin_unlock(&local->ap->sta_table_lock);
2711
Jouni Malinen5bee7202005-08-14 19:08:39 -07002712 if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
2713 !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
Jouni Malinenff1d2762005-05-12 22:54:16 -04002714 meta->iface->type != HOSTAP_INTERFACE_MASTER &&
2715 meta->iface->type != HOSTAP_INTERFACE_AP) {
2716#if 0
2717 /* This can happen, e.g., when wlan0 is added to a bridge and
2718 * bridging code does not know which port is the correct target
2719 * for a unicast frame. In this case, the packet is send to all
2720 * ports of the bridge. Since this is a valid scenario, do not
2721 * print out any errors here. */
2722 if (net_ratelimit()) {
2723 printk(KERN_DEBUG "AP: drop packet to non-associated "
2724 "STA " MACSTR "\n", MAC2STR(hdr->addr1));
2725 }
2726#endif
2727 local->ap->tx_drop_nonassoc++;
2728 ret = AP_TX_DROP;
2729 goto out;
2730 }
2731
2732 if (sta == NULL)
2733 goto out;
2734
2735 if (!(sta->flags & WLAN_STA_AUTHORIZED))
2736 ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
2737
2738 /* Set tx_rate if using host-based TX rate control */
2739 if (!local->fw_tx_rate_control)
2740 local->ap->last_tx_rate = meta->rate =
2741 ap_update_sta_tx_rate(sta, local->dev);
2742
2743 if (local->iw_mode != IW_MODE_MASTER)
2744 goto out;
2745
2746 if (!(sta->flags & WLAN_STA_PS))
2747 goto out;
2748
Jouni Malinen5bee7202005-08-14 19:08:39 -07002749 if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
2750 /* indicate to STA that more frames follow */
2751 hdr->frame_control |= __constant_cpu_to_le16(WLAN_FC_MOREDATA);
2752 }
Jouni Malinenff1d2762005-05-12 22:54:16 -04002753
Jouni Malinen5bee7202005-08-14 19:08:39 -07002754 if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
2755 /* packet was already buffered and now send due to
2756 * PS poll, so do not rebuffer it */
2757 goto out;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002758 }
2759
2760 if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
2761 PDEBUG(DEBUG_PS, "%s: No more space in STA (" MACSTR ")'s PS "
2762 "mode buffer\n", local->dev->name, MAC2STR(sta->addr));
2763 /* Make sure that TIM is set for the station (it might not be
2764 * after AP wlan hw reset). */
2765 /* FIX: should fix hw reset to restore bits based on STA
2766 * buffer state.. */
2767 hostap_set_tim(local, sta->aid, 1);
2768 sta->flags |= WLAN_STA_TIM;
2769 ret = AP_TX_DROP;
2770 goto out;
2771 }
2772
2773 /* STA in PS mode, buffer frame for later delivery */
2774 set_tim = skb_queue_empty(&sta->tx_buf);
2775 skb_queue_tail(&sta->tx_buf, skb);
2776 /* FIX: could save RX time to skb and expire buffered frames after
2777 * some time if STA does not poll for them */
2778
2779 if (set_tim) {
2780 if (sta->flags & WLAN_STA_TIM)
2781 PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
2782 sta->aid);
2783 hostap_set_tim(local, sta->aid, 1);
2784 sta->flags |= WLAN_STA_TIM;
2785 }
2786
2787 ret = AP_TX_BUFFERED;
2788
2789 out:
2790 if (sta != NULL) {
2791 if (ret == AP_TX_CONTINUE ||
2792 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
2793 sta->tx_packets++;
2794 sta->tx_bytes += skb->len;
2795 sta->last_tx = jiffies;
2796 }
2797
2798 if ((ret == AP_TX_CONTINUE ||
2799 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
2800 sta->crypt && tx->host_encrypt) {
2801 tx->crypt = sta->crypt;
2802 tx->sta_ptr = sta; /* hostap_handle_sta_release() will
2803 * be called to release sta info
2804 * later */
2805 } else
2806 atomic_dec(&sta->users);
2807 }
2808
2809 return ret;
2810}
2811
2812
2813void hostap_handle_sta_release(void *ptr)
2814{
2815 struct sta_info *sta = ptr;
2816 atomic_dec(&sta->users);
2817}
2818
2819
2820/* Called only as a tasklet (software IRQ) */
2821void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
2822{
2823 struct sta_info *sta;
2824 struct hostap_ieee80211_hdr *hdr;
2825 struct hostap_skb_tx_data *meta;
2826
2827 hdr = (struct hostap_ieee80211_hdr *) skb->data;
2828 meta = (struct hostap_skb_tx_data *) skb->cb;
2829
2830 spin_lock(&local->ap->sta_table_lock);
2831 sta = ap_get_sta(local->ap, hdr->addr1);
2832 if (!sta) {
2833 spin_unlock(&local->ap->sta_table_lock);
2834 PDEBUG(DEBUG_AP, "%s: Could not find STA " MACSTR " for this "
2835 "TX error (@%lu)\n",
2836 local->dev->name, MAC2STR(hdr->addr1), jiffies);
2837 return;
2838 }
2839
2840 sta->tx_since_last_failure = 0;
2841 sta->tx_consecutive_exc++;
Jeff Garzik74fae822005-07-31 13:08:32 -04002842
Jouni Malinenff1d2762005-05-12 22:54:16 -04002843 if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
2844 sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
2845 /* use next lower rate */
2846 int old, rate;
2847 old = rate = sta->tx_rate_idx;
2848 while (rate > 0) {
2849 rate--;
2850 if (ap_tx_rate_ok(rate, sta, local)) {
2851 sta->tx_rate_idx = rate;
2852 break;
2853 }
2854 }
2855 if (old != sta->tx_rate_idx) {
2856 switch (sta->tx_rate_idx) {
2857 case 0: sta->tx_rate = 10; break;
2858 case 1: sta->tx_rate = 20; break;
2859 case 2: sta->tx_rate = 55; break;
2860 case 3: sta->tx_rate = 110; break;
2861 default: sta->tx_rate = 0; break;
2862 }
2863 PDEBUG(DEBUG_AP, "%s: STA " MACSTR " TX rate lowered "
2864 "to %d\n", local->dev->name, MAC2STR(sta->addr),
2865 sta->tx_rate);
2866 }
2867 sta->tx_consecutive_exc = 0;
2868 }
2869 spin_unlock(&local->ap->sta_table_lock);
2870}
2871
2872
2873static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
2874 int pwrmgt, int type, int stype)
2875{
2876 if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
2877 sta->flags |= WLAN_STA_PS;
2878 PDEBUG(DEBUG_PS2, "STA " MACSTR " changed to use PS "
2879 "mode (type=0x%02X, stype=0x%02X)\n",
2880 MAC2STR(sta->addr), type, stype);
2881 } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
2882 sta->flags &= ~WLAN_STA_PS;
2883 PDEBUG(DEBUG_PS2, "STA " MACSTR " changed to not use "
2884 "PS mode (type=0x%02X, stype=0x%02X)\n",
2885 MAC2STR(sta->addr), type, stype);
2886 if (type != WLAN_FC_TYPE_CTRL || stype != WLAN_FC_STYPE_PSPOLL)
2887 schedule_packet_send(local, sta);
2888 }
2889}
2890
2891
2892/* Called only as a tasklet (software IRQ). Called for each RX frame to update
2893 * STA power saving state. pwrmgt is a flag from 802.11 frame_control field. */
2894int hostap_update_sta_ps(local_info_t *local, struct hostap_ieee80211_hdr *hdr)
2895{
2896 struct sta_info *sta;
2897 u16 fc;
2898
2899 spin_lock(&local->ap->sta_table_lock);
2900 sta = ap_get_sta(local->ap, hdr->addr2);
2901 if (sta)
2902 atomic_inc(&sta->users);
2903 spin_unlock(&local->ap->sta_table_lock);
2904
2905 if (!sta)
2906 return -1;
2907
2908 fc = le16_to_cpu(hdr->frame_control);
2909 hostap_update_sta_ps2(local, sta, fc & WLAN_FC_PWRMGT,
Jouni Malinenebed67d2005-07-30 20:43:19 -07002910 HOSTAP_FC_GET_TYPE(fc), HOSTAP_FC_GET_STYPE(fc));
Jouni Malinenff1d2762005-05-12 22:54:16 -04002911
2912 atomic_dec(&sta->users);
2913 return 0;
2914}
2915
2916
2917/* Called only as a tasklet (software IRQ). Called for each RX frame after
2918 * getting RX header and payload from hardware. */
2919ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
2920 struct sk_buff *skb,
2921 struct hostap_80211_rx_status *rx_stats,
2922 int wds)
2923{
2924 int ret;
2925 struct sta_info *sta;
2926 u16 fc, type, stype;
2927 struct hostap_ieee80211_hdr *hdr;
2928
2929 if (local->ap == NULL)
2930 return AP_RX_CONTINUE;
2931
2932 hdr = (struct hostap_ieee80211_hdr *) skb->data;
2933
2934 fc = le16_to_cpu(hdr->frame_control);
Jouni Malinenebed67d2005-07-30 20:43:19 -07002935 type = HOSTAP_FC_GET_TYPE(fc);
2936 stype = HOSTAP_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002937
2938 spin_lock(&local->ap->sta_table_lock);
2939 sta = ap_get_sta(local->ap, hdr->addr2);
2940 if (sta)
2941 atomic_inc(&sta->users);
2942 spin_unlock(&local->ap->sta_table_lock);
2943
2944 if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
2945 ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
2946 else
2947 ret = AP_RX_CONTINUE;
2948
2949
2950 if (fc & WLAN_FC_TODS) {
2951 if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2952 if (local->hostapd) {
2953 prism2_rx_80211(local->apdev, skb, rx_stats,
2954 PRISM2_RX_NON_ASSOC);
2955#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2956 } else {
2957 printk(KERN_DEBUG "%s: dropped received packet"
2958 " from non-associated STA " MACSTR
2959 " (type=0x%02x, subtype=0x%02x)\n",
2960 dev->name, MAC2STR(hdr->addr2), type,
2961 stype);
2962 hostap_rx(dev, skb, rx_stats);
2963#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2964 }
2965 ret = AP_RX_EXIT;
2966 goto out;
2967 }
2968 } else if (fc & WLAN_FC_FROMDS) {
2969 if (!wds) {
2970 /* FromDS frame - not for us; probably
2971 * broadcast/multicast in another BSS - drop */
2972 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2973 printk(KERN_DEBUG "Odd.. FromDS packet "
2974 "received with own BSSID\n");
2975 hostap_dump_rx_80211(dev->name, skb, rx_stats);
2976 }
2977 ret = AP_RX_DROP;
2978 goto out;
2979 }
2980 } else if (stype == WLAN_FC_STYPE_NULLFUNC && sta == NULL &&
2981 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2982
2983 if (local->hostapd) {
2984 prism2_rx_80211(local->apdev, skb, rx_stats,
2985 PRISM2_RX_NON_ASSOC);
2986#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2987 } else {
2988 /* At least Lucent f/w seems to send data::nullfunc
2989 * frames with no ToDS flag when the current AP returns
2990 * after being unavailable for some time. Speed up
2991 * re-association by informing the station about it not
2992 * being associated. */
2993 printk(KERN_DEBUG "%s: rejected received nullfunc "
2994 "frame without ToDS from not associated STA "
2995 MACSTR "\n",
2996 dev->name, MAC2STR(hdr->addr2));
2997 hostap_rx(dev, skb, rx_stats);
2998#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2999 }
3000 ret = AP_RX_EXIT;
3001 goto out;
3002 } else if (stype == WLAN_FC_STYPE_NULLFUNC) {
3003 /* At least Lucent cards seem to send periodic nullfunc
3004 * frames with ToDS. Let these through to update SQ
3005 * stats and PS state. Nullfunc frames do not contain
3006 * any data and they will be dropped below. */
3007 } else {
3008 /* If BSSID (Addr3) is foreign, this frame is a normal
3009 * broadcast frame from an IBSS network. Drop it silently.
3010 * If BSSID is own, report the dropping of this frame. */
3011 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
3012 printk(KERN_DEBUG "%s: dropped received packet from "
3013 MACSTR " with no ToDS flag (type=0x%02x, "
3014 "subtype=0x%02x)\n", dev->name,
3015 MAC2STR(hdr->addr2), type, stype);
3016 hostap_dump_rx_80211(dev->name, skb, rx_stats);
3017 }
3018 ret = AP_RX_DROP;
3019 goto out;
3020 }
3021
3022 if (sta) {
3023 hostap_update_sta_ps2(local, sta, fc & WLAN_FC_PWRMGT,
3024 type, stype);
3025
3026 sta->rx_packets++;
3027 sta->rx_bytes += skb->len;
3028 sta->last_rx = jiffies;
3029 }
3030
3031 if (local->ap->nullfunc_ack && stype == WLAN_FC_STYPE_NULLFUNC &&
3032 fc & WLAN_FC_TODS) {
3033 if (local->hostapd) {
3034 prism2_rx_80211(local->apdev, skb, rx_stats,
3035 PRISM2_RX_NULLFUNC_ACK);
3036#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3037 } else {
3038 /* some STA f/w's seem to require control::ACK frame
3039 * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
3040 * from Compaq) does not send this.. Try to generate
3041 * ACK for these frames from the host driver to make
3042 * power saving work with, e.g., Lucent WaveLAN f/w */
3043 hostap_rx(dev, skb, rx_stats);
3044#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3045 }
3046 ret = AP_RX_EXIT;
3047 goto out;
3048 }
3049
3050 out:
3051 if (sta)
3052 atomic_dec(&sta->users);
3053
3054 return ret;
3055}
3056
3057
3058/* Called only as a tasklet (software IRQ) */
3059int hostap_handle_sta_crypto(local_info_t *local,
3060 struct hostap_ieee80211_hdr *hdr,
Jouni Malinen62fe7e32005-07-30 20:43:20 -07003061 struct ieee80211_crypt_data **crypt,
3062 void **sta_ptr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003063{
3064 struct sta_info *sta;
3065
3066 spin_lock(&local->ap->sta_table_lock);
3067 sta = ap_get_sta(local->ap, hdr->addr2);
3068 if (sta)
3069 atomic_inc(&sta->users);
3070 spin_unlock(&local->ap->sta_table_lock);
3071
3072 if (!sta)
3073 return -1;
3074
3075 if (sta->crypt) {
3076 *crypt = sta->crypt;
3077 *sta_ptr = sta;
3078 /* hostap_handle_sta_release() will be called to release STA
3079 * info */
3080 } else
3081 atomic_dec(&sta->users);
3082
3083 return 0;
3084}
3085
3086
3087/* Called only as a tasklet (software IRQ) */
3088int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
3089{
3090 struct sta_info *sta;
3091 int ret = 0;
3092
3093 spin_lock(&ap->sta_table_lock);
3094 sta = ap_get_sta(ap, sta_addr);
3095 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
3096 ret = 1;
3097 spin_unlock(&ap->sta_table_lock);
3098
3099 return ret;
3100}
3101
3102
3103/* Called only as a tasklet (software IRQ) */
3104int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
3105{
3106 struct sta_info *sta;
3107 int ret = 0;
3108
3109 spin_lock(&ap->sta_table_lock);
3110 sta = ap_get_sta(ap, sta_addr);
3111 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
3112 ((sta->flags & WLAN_STA_AUTHORIZED) ||
3113 ap->local->ieee_802_1x == 0))
3114 ret = 1;
3115 spin_unlock(&ap->sta_table_lock);
3116
3117 return ret;
3118}
3119
3120
3121/* Called only as a tasklet (software IRQ) */
3122int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
3123{
3124 struct sta_info *sta;
3125 int ret = 1;
3126
3127 if (!ap)
3128 return -1;
3129
3130 spin_lock(&ap->sta_table_lock);
3131 sta = ap_get_sta(ap, sta_addr);
3132 if (sta)
3133 ret = 0;
3134 spin_unlock(&ap->sta_table_lock);
3135
3136 if (ret == 1) {
3137 sta = ap_add_sta(ap, sta_addr);
3138 if (!sta)
3139 ret = -1;
3140 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
3141 sta->ap = 1;
3142 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
3143 /* No way of knowing which rates are supported since we did not
3144 * get supported rates element from beacon/assoc req. Assume
3145 * that remote end supports all 802.11b rates. */
3146 sta->supported_rates[0] = 0x82;
3147 sta->supported_rates[1] = 0x84;
3148 sta->supported_rates[2] = 0x0b;
3149 sta->supported_rates[3] = 0x16;
3150 sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
3151 WLAN_RATE_5M5 | WLAN_RATE_11M;
3152 sta->tx_rate = 110;
3153 sta->tx_max_rate = sta->tx_rate_idx = 3;
3154 }
3155
3156 return ret;
3157}
3158
3159
3160/* Called only as a tasklet (software IRQ) */
3161int hostap_update_rx_stats(struct ap_data *ap,
3162 struct hostap_ieee80211_hdr *hdr,
3163 struct hostap_80211_rx_status *rx_stats)
3164{
3165 struct sta_info *sta;
3166
3167 if (!ap)
3168 return -1;
3169
3170 spin_lock(&ap->sta_table_lock);
3171 sta = ap_get_sta(ap, hdr->addr2);
3172 if (sta) {
3173 sta->last_rx_silence = rx_stats->noise;
3174 sta->last_rx_signal = rx_stats->signal;
3175 sta->last_rx_rate = rx_stats->rate;
3176 sta->last_rx_updated = 7;
3177 if (rx_stats->rate == 10)
3178 sta->rx_count[0]++;
3179 else if (rx_stats->rate == 20)
3180 sta->rx_count[1]++;
3181 else if (rx_stats->rate == 55)
3182 sta->rx_count[2]++;
3183 else if (rx_stats->rate == 110)
3184 sta->rx_count[3]++;
3185 }
3186 spin_unlock(&ap->sta_table_lock);
3187
3188 return sta ? 0 : -1;
3189}
3190
3191
3192void hostap_update_rates(local_info_t *local)
3193{
3194 struct list_head *ptr;
3195 struct ap_data *ap = local->ap;
3196
3197 if (!ap)
3198 return;
3199
3200 spin_lock_bh(&ap->sta_table_lock);
3201 for (ptr = ap->sta_list.next; ptr != &ap->sta_list; ptr = ptr->next) {
3202 struct sta_info *sta = (struct sta_info *) ptr;
3203 prism2_check_tx_rates(sta);
3204 }
3205 spin_unlock_bh(&ap->sta_table_lock);
3206}
3207
3208
3209static void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
Jouni Malinen62fe7e32005-07-30 20:43:20 -07003210 struct ieee80211_crypt_data ***crypt)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003211{
3212 struct sta_info *sta;
3213
3214 spin_lock_bh(&ap->sta_table_lock);
3215 sta = ap_get_sta(ap, addr);
3216 if (sta)
3217 atomic_inc(&sta->users);
3218 spin_unlock_bh(&ap->sta_table_lock);
3219
3220 if (!sta && permanent)
3221 sta = ap_add_sta(ap, addr);
3222
3223 if (!sta)
3224 return NULL;
3225
3226 if (permanent)
3227 sta->flags |= WLAN_STA_PERM;
3228
3229 *crypt = &sta->crypt;
3230
3231 return sta;
3232}
3233
3234
3235void hostap_add_wds_links(local_info_t *local)
3236{
3237 struct ap_data *ap = local->ap;
3238 struct list_head *ptr;
3239
3240 spin_lock_bh(&ap->sta_table_lock);
3241 list_for_each(ptr, &ap->sta_list) {
3242 struct sta_info *sta = list_entry(ptr, struct sta_info, list);
3243 if (sta->ap)
3244 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
3245 }
3246 spin_unlock_bh(&ap->sta_table_lock);
3247
3248 schedule_work(&local->ap->wds_oper_queue);
3249}
3250
3251
3252void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
3253{
3254 struct wds_oper_data *entry;
3255
3256 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
3257 if (!entry)
3258 return;
3259 memcpy(entry->addr, addr, ETH_ALEN);
3260 entry->type = type;
3261 spin_lock_bh(&local->lock);
3262 entry->next = local->ap->wds_oper_entries;
3263 local->ap->wds_oper_entries = entry;
3264 spin_unlock_bh(&local->lock);
3265
3266 schedule_work(&local->ap->wds_oper_queue);
3267}
3268
3269
3270EXPORT_SYMBOL(hostap_init_data);
3271EXPORT_SYMBOL(hostap_init_ap_proc);
3272EXPORT_SYMBOL(hostap_free_data);
3273EXPORT_SYMBOL(hostap_check_sta_fw_version);
3274EXPORT_SYMBOL(hostap_handle_sta_tx);
3275EXPORT_SYMBOL(hostap_handle_sta_release);
3276EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
3277EXPORT_SYMBOL(hostap_update_sta_ps);
3278EXPORT_SYMBOL(hostap_handle_sta_rx);
3279EXPORT_SYMBOL(hostap_is_sta_assoc);
3280EXPORT_SYMBOL(hostap_is_sta_authorized);
3281EXPORT_SYMBOL(hostap_add_sta);
3282EXPORT_SYMBOL(hostap_update_rates);
3283EXPORT_SYMBOL(hostap_add_wds_links);
3284EXPORT_SYMBOL(hostap_wds_link_oper);
3285#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3286EXPORT_SYMBOL(hostap_deauth_all_stas);
3287#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */