Merge remote-tracking branch 'asoc/fix/core' into asoc-linus
[cascardo/linux.git] / drivers / net / wireless / hostap / hostap_main.c
1 /*
2  * Host AP (software wireless LAN access point) driver for
3  * Intersil Prism2/2.5/3 - hostap.o module, common routines
4  *
5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6  * <j@w1.fi>
7  * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation. See README and COPYING for
12  * more details.
13  */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/proc_fs.h>
19 #include <linux/if_arp.h>
20 #include <linux/delay.h>
21 #include <linux/random.h>
22 #include <linux/workqueue.h>
23 #include <linux/kmod.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/wireless.h>
26 #include <linux/etherdevice.h>
27 #include <net/net_namespace.h>
28 #include <net/iw_handler.h>
29 #include <net/lib80211.h>
30 #include <asm/uaccess.h>
31
32 #include "hostap_wlan.h"
33 #include "hostap_80211.h"
34 #include "hostap_ap.h"
35 #include "hostap.h"
36
37 MODULE_AUTHOR("Jouni Malinen");
38 MODULE_DESCRIPTION("Host AP common routines");
39 MODULE_LICENSE("GPL");
40
41 #define TX_TIMEOUT (2 * HZ)
42
43 #define PRISM2_MAX_FRAME_SIZE 2304
44 #define PRISM2_MIN_MTU 256
45 /* FIX: */
46 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
47
48
49 struct net_device * hostap_add_interface(struct local_info *local,
50                                          int type, int rtnl_locked,
51                                          const char *prefix,
52                                          const char *name)
53 {
54         struct net_device *dev, *mdev;
55         struct hostap_interface *iface;
56         int ret;
57
58         dev = alloc_etherdev(sizeof(struct hostap_interface));
59         if (dev == NULL)
60                 return NULL;
61
62         iface = netdev_priv(dev);
63         iface->dev = dev;
64         iface->local = local;
65         iface->type = type;
66         list_add(&iface->list, &local->hostap_interfaces);
67
68         mdev = local->dev;
69         eth_hw_addr_inherit(dev, mdev);
70         dev->base_addr = mdev->base_addr;
71         dev->irq = mdev->irq;
72         dev->mem_start = mdev->mem_start;
73         dev->mem_end = mdev->mem_end;
74
75         hostap_setup_dev(dev, local, type);
76         dev->destructor = free_netdev;
77
78         sprintf(dev->name, "%s%s", prefix, name);
79         if (!rtnl_locked)
80                 rtnl_lock();
81
82         SET_NETDEV_DEV(dev, mdev->dev.parent);
83         ret = register_netdevice(dev);
84
85         if (!rtnl_locked)
86                 rtnl_unlock();
87
88         if (ret < 0) {
89                 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
90                        dev->name);
91                 free_netdev(dev);
92                 return NULL;
93         }
94
95         printk(KERN_DEBUG "%s: registered netdevice %s\n",
96                mdev->name, dev->name);
97
98         return dev;
99 }
100
101
102 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
103                              int remove_from_list)
104 {
105         struct hostap_interface *iface;
106
107         if (!dev)
108                 return;
109
110         iface = netdev_priv(dev);
111
112         if (remove_from_list) {
113                 list_del(&iface->list);
114         }
115
116         if (dev == iface->local->ddev)
117                 iface->local->ddev = NULL;
118         else if (dev == iface->local->apdev)
119                 iface->local->apdev = NULL;
120         else if (dev == iface->local->stadev)
121                 iface->local->stadev = NULL;
122
123         if (rtnl_locked)
124                 unregister_netdevice(dev);
125         else
126                 unregister_netdev(dev);
127
128         /* dev->destructor = free_netdev() will free the device data, including
129          * private data, when removing the device */
130 }
131
132
133 static inline int prism2_wds_special_addr(u8 *addr)
134 {
135         if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
136                 return 0;
137
138         return 1;
139 }
140
141
142 int prism2_wds_add(local_info_t *local, u8 *remote_addr,
143                    int rtnl_locked)
144 {
145         struct net_device *dev;
146         struct list_head *ptr;
147         struct hostap_interface *iface, *empty, *match;
148
149         empty = match = NULL;
150         read_lock_bh(&local->iface_lock);
151         list_for_each(ptr, &local->hostap_interfaces) {
152                 iface = list_entry(ptr, struct hostap_interface, list);
153                 if (iface->type != HOSTAP_INTERFACE_WDS)
154                         continue;
155
156                 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
157                         empty = iface;
158                 else if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
159                         match = iface;
160                         break;
161                 }
162         }
163         if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
164                 /* take pre-allocated entry into use */
165                 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
166                 read_unlock_bh(&local->iface_lock);
167                 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
168                        local->dev->name, empty->dev->name);
169                 return 0;
170         }
171         read_unlock_bh(&local->iface_lock);
172
173         if (!prism2_wds_special_addr(remote_addr)) {
174                 if (match)
175                         return -EEXIST;
176                 hostap_add_sta(local->ap, remote_addr);
177         }
178
179         if (local->wds_connections >= local->wds_max_connections)
180                 return -ENOBUFS;
181
182         /* verify that there is room for wds# postfix in the interface name */
183         if (strlen(local->dev->name) >= IFNAMSIZ - 5) {
184                 printk(KERN_DEBUG "'%s' too long base device name\n",
185                        local->dev->name);
186                 return -EINVAL;
187         }
188
189         dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
190                                    local->ddev->name, "wds%d");
191         if (dev == NULL)
192                 return -ENOMEM;
193
194         iface = netdev_priv(dev);
195         memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
196
197         local->wds_connections++;
198
199         return 0;
200 }
201
202
203 int prism2_wds_del(local_info_t *local, u8 *remote_addr,
204                    int rtnl_locked, int do_not_remove)
205 {
206         unsigned long flags;
207         struct list_head *ptr;
208         struct hostap_interface *iface, *selected = NULL;
209
210         write_lock_irqsave(&local->iface_lock, flags);
211         list_for_each(ptr, &local->hostap_interfaces) {
212                 iface = list_entry(ptr, struct hostap_interface, list);
213                 if (iface->type != HOSTAP_INTERFACE_WDS)
214                         continue;
215
216                 if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
217                         selected = iface;
218                         break;
219                 }
220         }
221         if (selected && !do_not_remove)
222                 list_del(&selected->list);
223         write_unlock_irqrestore(&local->iface_lock, flags);
224
225         if (selected) {
226                 if (do_not_remove)
227                         memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
228                 else {
229                         hostap_remove_interface(selected->dev, rtnl_locked, 0);
230                         local->wds_connections--;
231                 }
232         }
233
234         return selected ? 0 : -ENODEV;
235 }
236
237
238 u16 hostap_tx_callback_register(local_info_t *local,
239                                 void (*func)(struct sk_buff *, int ok, void *),
240                                 void *data)
241 {
242         unsigned long flags;
243         struct hostap_tx_callback_info *entry;
244
245         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
246         if (entry == NULL)
247                 return 0;
248
249         entry->func = func;
250         entry->data = data;
251
252         spin_lock_irqsave(&local->lock, flags);
253         entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
254         entry->next = local->tx_callback;
255         local->tx_callback = entry;
256         spin_unlock_irqrestore(&local->lock, flags);
257
258         return entry->idx;
259 }
260
261
262 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
263 {
264         unsigned long flags;
265         struct hostap_tx_callback_info *cb, *prev = NULL;
266
267         spin_lock_irqsave(&local->lock, flags);
268         cb = local->tx_callback;
269         while (cb != NULL && cb->idx != idx) {
270                 prev = cb;
271                 cb = cb->next;
272         }
273         if (cb) {
274                 if (prev == NULL)
275                         local->tx_callback = cb->next;
276                 else
277                         prev->next = cb->next;
278                 kfree(cb);
279         }
280         spin_unlock_irqrestore(&local->lock, flags);
281
282         return cb ? 0 : -1;
283 }
284
285
286 /* val is in host byte order */
287 int hostap_set_word(struct net_device *dev, int rid, u16 val)
288 {
289         struct hostap_interface *iface;
290         __le16 tmp = cpu_to_le16(val);
291         iface = netdev_priv(dev);
292         return iface->local->func->set_rid(dev, rid, &tmp, 2);
293 }
294
295
296 int hostap_set_string(struct net_device *dev, int rid, const char *val)
297 {
298         struct hostap_interface *iface;
299         char buf[MAX_SSID_LEN + 2];
300         int len;
301
302         iface = netdev_priv(dev);
303         len = strlen(val);
304         if (len > MAX_SSID_LEN)
305                 return -1;
306         memset(buf, 0, sizeof(buf));
307         buf[0] = len; /* little endian 16 bit word */
308         memcpy(buf + 2, val, len);
309
310         return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
311 }
312
313
314 u16 hostap_get_porttype(local_info_t *local)
315 {
316         if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
317                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
318         if (local->iw_mode == IW_MODE_ADHOC)
319                 return HFA384X_PORTTYPE_IBSS;
320         if (local->iw_mode == IW_MODE_INFRA)
321                 return HFA384X_PORTTYPE_BSS;
322         if (local->iw_mode == IW_MODE_REPEAT)
323                 return HFA384X_PORTTYPE_WDS;
324         if (local->iw_mode == IW_MODE_MONITOR)
325                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
326         return HFA384X_PORTTYPE_HOSTAP;
327 }
328
329
330 int hostap_set_encryption(local_info_t *local)
331 {
332         u16 val, old_val;
333         int i, keylen, len, idx;
334         char keybuf[WEP_KEY_LEN + 1];
335         enum { NONE, WEP, OTHER } encrypt_type;
336
337         idx = local->crypt_info.tx_keyidx;
338         if (local->crypt_info.crypt[idx] == NULL ||
339             local->crypt_info.crypt[idx]->ops == NULL)
340                 encrypt_type = NONE;
341         else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
342                 encrypt_type = WEP;
343         else
344                 encrypt_type = OTHER;
345
346         if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
347                                  1) < 0) {
348                 printk(KERN_DEBUG "Could not read current WEP flags.\n");
349                 goto fail;
350         }
351         le16_to_cpus(&val);
352         old_val = val;
353
354         if (encrypt_type != NONE || local->privacy_invoked)
355                 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
356         else
357                 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
358
359         if (local->open_wep || encrypt_type == NONE ||
360             ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
361                 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
362         else
363                 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
364
365         if ((encrypt_type != NONE || local->privacy_invoked) &&
366             (encrypt_type == OTHER || local->host_encrypt))
367                 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
368         else
369                 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
370         if ((encrypt_type != NONE || local->privacy_invoked) &&
371             (encrypt_type == OTHER || local->host_decrypt))
372                 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
373         else
374                 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
375
376         if (val != old_val &&
377             hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
378                 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
379                        val);
380                 goto fail;
381         }
382
383         if (encrypt_type != WEP)
384                 return 0;
385
386         /* 104-bit support seems to require that all the keys are set to the
387          * same keylen */
388         keylen = 6; /* first 5 octets */
389         len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
390                                                            local->crypt_info.crypt[idx]->priv);
391         if (idx >= 0 && idx < WEP_KEYS && len > 5)
392                 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
393
394         for (i = 0; i < WEP_KEYS; i++) {
395                 memset(keybuf, 0, sizeof(keybuf));
396                 if (local->crypt_info.crypt[i]) {
397                         (void) local->crypt_info.crypt[i]->ops->get_key(
398                                 keybuf, sizeof(keybuf),
399                                 NULL, local->crypt_info.crypt[i]->priv);
400                 }
401                 if (local->func->set_rid(local->dev,
402                                          HFA384X_RID_CNFDEFAULTKEY0 + i,
403                                          keybuf, keylen)) {
404                         printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
405                                i, keylen);
406                         goto fail;
407                 }
408         }
409         if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
410                 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
411                 goto fail;
412         }
413
414         return 0;
415
416  fail:
417         printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
418         return -1;
419 }
420
421
422 int hostap_set_antsel(local_info_t *local)
423 {
424         u16 val;
425         int ret = 0;
426
427         if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
428             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
429                              HFA386X_CR_TX_CONFIGURE,
430                              NULL, &val) == 0) {
431                 val &= ~(BIT(2) | BIT(1));
432                 switch (local->antsel_tx) {
433                 case HOSTAP_ANTSEL_DIVERSITY:
434                         val |= BIT(1);
435                         break;
436                 case HOSTAP_ANTSEL_LOW:
437                         break;
438                 case HOSTAP_ANTSEL_HIGH:
439                         val |= BIT(2);
440                         break;
441                 }
442
443                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
444                                      HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
445                         printk(KERN_INFO "%s: setting TX AntSel failed\n",
446                                local->dev->name);
447                         ret = -1;
448                 }
449         }
450
451         if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
452             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
453                              HFA386X_CR_RX_CONFIGURE,
454                              NULL, &val) == 0) {
455                 val &= ~(BIT(1) | BIT(0));
456                 switch (local->antsel_rx) {
457                 case HOSTAP_ANTSEL_DIVERSITY:
458                         break;
459                 case HOSTAP_ANTSEL_LOW:
460                         val |= BIT(0);
461                         break;
462                 case HOSTAP_ANTSEL_HIGH:
463                         val |= BIT(0) | BIT(1);
464                         break;
465                 }
466
467                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
468                                      HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
469                         printk(KERN_INFO "%s: setting RX AntSel failed\n",
470                                local->dev->name);
471                         ret = -1;
472                 }
473         }
474
475         return ret;
476 }
477
478
479 int hostap_set_roaming(local_info_t *local)
480 {
481         u16 val;
482
483         switch (local->host_roaming) {
484         case 1:
485                 val = HFA384X_ROAMING_HOST;
486                 break;
487         case 2:
488                 val = HFA384X_ROAMING_DISABLED;
489                 break;
490         case 0:
491         default:
492                 val = HFA384X_ROAMING_FIRMWARE;
493                 break;
494         }
495
496         return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
497 }
498
499
500 int hostap_set_auth_algs(local_info_t *local)
501 {
502         int val = local->auth_algs;
503         /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
504          * set to include both Open and Shared Key flags. It tries to use
505          * Shared Key authentication in that case even if WEP keys are not
506          * configured.. STA f/w v0.7.6 is able to handle such configuration,
507          * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
508         if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
509             val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
510                 val = PRISM2_AUTH_OPEN;
511
512         if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
513                 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
514                        "failed\n", local->dev->name, local->auth_algs);
515                 return -EINVAL;
516         }
517
518         return 0;
519 }
520
521
522 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
523 {
524         u16 status, fc;
525
526         status = __le16_to_cpu(rx->status);
527
528         printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
529                "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
530                "jiffies=%ld\n",
531                name, status, (status >> 8) & 0x07, status >> 13, status & 1,
532                rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
533
534         fc = __le16_to_cpu(rx->frame_control);
535         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
536                "data_len=%d%s%s\n",
537                fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
538                (fc & IEEE80211_FCTL_STYPE) >> 4,
539                __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
540                __le16_to_cpu(rx->data_len),
541                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
542                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
543
544         printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
545                rx->addr1, rx->addr2, rx->addr3, rx->addr4);
546
547         printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
548                rx->dst_addr, rx->src_addr,
549                __be16_to_cpu(rx->len));
550 }
551
552
553 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
554 {
555         u16 fc;
556
557         printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
558                "tx_control=0x%04x; jiffies=%ld\n",
559                name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
560                __le16_to_cpu(tx->tx_control), jiffies);
561
562         fc = __le16_to_cpu(tx->frame_control);
563         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
564                "data_len=%d%s%s\n",
565                fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
566                (fc & IEEE80211_FCTL_STYPE) >> 4,
567                __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
568                __le16_to_cpu(tx->data_len),
569                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
570                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
571
572         printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
573                tx->addr1, tx->addr2, tx->addr3, tx->addr4);
574
575         printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
576                tx->dst_addr, tx->src_addr,
577                __be16_to_cpu(tx->len));
578 }
579
580
581 static int hostap_80211_header_parse(const struct sk_buff *skb,
582                                      unsigned char *haddr)
583 {
584         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
585         return ETH_ALEN;
586 }
587
588
589 int hostap_80211_get_hdrlen(__le16 fc)
590 {
591         if (ieee80211_is_data(fc) && ieee80211_has_a4 (fc))
592                 return 30; /* Addr4 */
593         else if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc))
594                 return 10;
595         else if (ieee80211_is_ctl(fc))
596                 return 16;
597
598         return 24;
599 }
600
601
602 static int prism2_close(struct net_device *dev)
603 {
604         struct hostap_interface *iface;
605         local_info_t *local;
606
607         PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
608
609         iface = netdev_priv(dev);
610         local = iface->local;
611
612         if (dev == local->ddev) {
613                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
614         }
615 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
616         if (!local->hostapd && dev == local->dev &&
617             (!local->func->card_present || local->func->card_present(local)) &&
618             local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
619                 hostap_deauth_all_stas(dev, local->ap, 1);
620 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
621
622         if (dev == local->dev) {
623                 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
624         }
625
626         if (netif_running(dev)) {
627                 netif_stop_queue(dev);
628                 netif_device_detach(dev);
629         }
630
631         cancel_work_sync(&local->reset_queue);
632         cancel_work_sync(&local->set_multicast_list_queue);
633         cancel_work_sync(&local->set_tim_queue);
634 #ifndef PRISM2_NO_STATION_MODES
635         cancel_work_sync(&local->info_queue);
636 #endif
637         cancel_work_sync(&local->comms_qual_update);
638
639         module_put(local->hw_module);
640
641         local->num_dev_open--;
642
643         if (dev != local->dev && local->dev->flags & IFF_UP &&
644             local->master_dev_auto_open && local->num_dev_open == 1) {
645                 /* Close master radio interface automatically if it was also
646                  * opened automatically and we are now closing the last
647                  * remaining non-master device. */
648                 dev_close(local->dev);
649         }
650
651         return 0;
652 }
653
654
655 static int prism2_open(struct net_device *dev)
656 {
657         struct hostap_interface *iface;
658         local_info_t *local;
659
660         PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
661
662         iface = netdev_priv(dev);
663         local = iface->local;
664
665         if (local->no_pri) {
666                 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
667                        "f/w\n", dev->name);
668                 return -ENODEV;
669         }
670
671         if ((local->func->card_present && !local->func->card_present(local)) ||
672             local->hw_downloading)
673                 return -ENODEV;
674
675         if (!try_module_get(local->hw_module))
676                 return -ENODEV;
677         local->num_dev_open++;
678
679         if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
680                 printk(KERN_WARNING "%s: could not enable MAC port\n",
681                        dev->name);
682                 prism2_close(dev);
683                 return -ENODEV;
684         }
685         if (!local->dev_enabled)
686                 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
687         local->dev_enabled = 1;
688
689         if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
690                 /* Master radio interface is needed for all operation, so open
691                  * it automatically when any virtual net_device is opened. */
692                 local->master_dev_auto_open = 1;
693                 dev_open(local->dev);
694         }
695
696         netif_device_attach(dev);
697         netif_start_queue(dev);
698
699         return 0;
700 }
701
702
703 static int prism2_set_mac_address(struct net_device *dev, void *p)
704 {
705         struct hostap_interface *iface;
706         local_info_t *local;
707         struct list_head *ptr;
708         struct sockaddr *addr = p;
709
710         iface = netdev_priv(dev);
711         local = iface->local;
712
713         if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
714                                  ETH_ALEN) < 0 || local->func->reset_port(dev))
715                 return -EINVAL;
716
717         read_lock_bh(&local->iface_lock);
718         list_for_each(ptr, &local->hostap_interfaces) {
719                 iface = list_entry(ptr, struct hostap_interface, list);
720                 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
721         }
722         memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
723         read_unlock_bh(&local->iface_lock);
724
725         return 0;
726 }
727
728
729 /* TODO: to be further implemented as soon as Prism2 fully supports
730  *       GroupAddresses and correct documentation is available */
731 void hostap_set_multicast_list_queue(struct work_struct *work)
732 {
733         local_info_t *local =
734                 container_of(work, local_info_t, set_multicast_list_queue);
735         struct net_device *dev = local->dev;
736
737         if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
738                             local->is_promisc)) {
739                 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
740                        dev->name, local->is_promisc ? "en" : "dis");
741         }
742 }
743
744
745 static void hostap_set_multicast_list(struct net_device *dev)
746 {
747 #if 0
748         /* FIX: promiscuous mode seems to be causing a lot of problems with
749          * some station firmware versions (FCSErr frames, invalid MACPort, etc.
750          * corrupted incoming frames). This code is now commented out while the
751          * problems are investigated. */
752         struct hostap_interface *iface;
753         local_info_t *local;
754
755         iface = netdev_priv(dev);
756         local = iface->local;
757         if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
758                 local->is_promisc = 1;
759         } else {
760                 local->is_promisc = 0;
761         }
762
763         schedule_work(&local->set_multicast_list_queue);
764 #endif
765 }
766
767
768 static int prism2_change_mtu(struct net_device *dev, int new_mtu)
769 {
770         if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
771                 return -EINVAL;
772
773         dev->mtu = new_mtu;
774         return 0;
775 }
776
777
778 static void prism2_tx_timeout(struct net_device *dev)
779 {
780         struct hostap_interface *iface;
781         local_info_t *local;
782         struct hfa384x_regs regs;
783
784         iface = netdev_priv(dev);
785         local = iface->local;
786
787         printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
788         netif_stop_queue(local->dev);
789
790         local->func->read_regs(dev, &regs);
791         printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
792                "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
793                dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
794                regs.swsupport0);
795
796         local->func->schedule_reset(local);
797 }
798
799 const struct header_ops hostap_80211_ops = {
800         .create         = eth_header,
801         .rebuild        = eth_rebuild_header,
802         .cache          = eth_header_cache,
803         .cache_update   = eth_header_cache_update,
804         .parse          = hostap_80211_header_parse,
805 };
806 EXPORT_SYMBOL(hostap_80211_ops);
807
808
809 static const struct net_device_ops hostap_netdev_ops = {
810         .ndo_start_xmit         = hostap_data_start_xmit,
811
812         .ndo_open               = prism2_open,
813         .ndo_stop               = prism2_close,
814         .ndo_do_ioctl           = hostap_ioctl,
815         .ndo_set_mac_address    = prism2_set_mac_address,
816         .ndo_set_rx_mode        = hostap_set_multicast_list,
817         .ndo_change_mtu         = prism2_change_mtu,
818         .ndo_tx_timeout         = prism2_tx_timeout,
819         .ndo_validate_addr      = eth_validate_addr,
820 };
821
822 static const struct net_device_ops hostap_mgmt_netdev_ops = {
823         .ndo_start_xmit         = hostap_mgmt_start_xmit,
824
825         .ndo_open               = prism2_open,
826         .ndo_stop               = prism2_close,
827         .ndo_do_ioctl           = hostap_ioctl,
828         .ndo_set_mac_address    = prism2_set_mac_address,
829         .ndo_set_rx_mode        = hostap_set_multicast_list,
830         .ndo_change_mtu         = prism2_change_mtu,
831         .ndo_tx_timeout         = prism2_tx_timeout,
832         .ndo_validate_addr      = eth_validate_addr,
833 };
834
835 static const struct net_device_ops hostap_master_ops = {
836         .ndo_start_xmit         = hostap_master_start_xmit,
837
838         .ndo_open               = prism2_open,
839         .ndo_stop               = prism2_close,
840         .ndo_do_ioctl           = hostap_ioctl,
841         .ndo_set_mac_address    = prism2_set_mac_address,
842         .ndo_set_rx_mode        = hostap_set_multicast_list,
843         .ndo_change_mtu         = prism2_change_mtu,
844         .ndo_tx_timeout         = prism2_tx_timeout,
845         .ndo_validate_addr      = eth_validate_addr,
846 };
847
848 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
849                       int type)
850 {
851         struct hostap_interface *iface;
852
853         iface = netdev_priv(dev);
854         ether_setup(dev);
855         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
856
857         /* kernel callbacks */
858         if (iface) {
859                 /* Currently, we point to the proper spy_data only on
860                  * the main_dev. This could be fixed. Jean II */
861                 iface->wireless_data.spy_data = &iface->spy_data;
862                 dev->wireless_data = &iface->wireless_data;
863         }
864         dev->wireless_handlers = &hostap_iw_handler_def;
865         dev->watchdog_timeo = TX_TIMEOUT;
866
867         switch(type) {
868         case HOSTAP_INTERFACE_AP:
869                 dev->tx_queue_len = 0;  /* use main radio device queue */
870                 dev->netdev_ops = &hostap_mgmt_netdev_ops;
871                 dev->type = ARPHRD_IEEE80211;
872                 dev->header_ops = &hostap_80211_ops;
873                 break;
874         case HOSTAP_INTERFACE_MASTER:
875                 dev->netdev_ops = &hostap_master_ops;
876                 break;
877         default:
878                 dev->tx_queue_len = 0;  /* use main radio device queue */
879                 dev->netdev_ops = &hostap_netdev_ops;
880         }
881
882         dev->mtu = local->mtu;
883
884
885         dev->ethtool_ops = &prism2_ethtool_ops;
886
887 }
888
889 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
890 {
891         struct net_device *dev = local->dev;
892
893         if (local->apdev)
894                 return -EEXIST;
895
896         printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
897
898         local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
899                                             rtnl_locked, local->ddev->name,
900                                             "ap");
901         if (local->apdev == NULL)
902                 return -ENOMEM;
903
904         return 0;
905 }
906
907
908 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
909 {
910         struct net_device *dev = local->dev;
911
912         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
913
914         hostap_remove_interface(local->apdev, rtnl_locked, 1);
915         local->apdev = NULL;
916
917         return 0;
918 }
919
920
921 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
922 {
923         struct net_device *dev = local->dev;
924
925         if (local->stadev)
926                 return -EEXIST;
927
928         printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
929
930         local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
931                                              rtnl_locked, local->ddev->name,
932                                              "sta");
933         if (local->stadev == NULL)
934                 return -ENOMEM;
935
936         return 0;
937 }
938
939
940 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
941 {
942         struct net_device *dev = local->dev;
943
944         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
945
946         hostap_remove_interface(local->stadev, rtnl_locked, 1);
947         local->stadev = NULL;
948
949         return 0;
950 }
951
952
953 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
954 {
955         int ret;
956
957         if (val < 0 || val > 1)
958                 return -EINVAL;
959
960         if (local->hostapd == val)
961                 return 0;
962
963         if (val) {
964                 ret = hostap_enable_hostapd(local, rtnl_locked);
965                 if (ret == 0)
966                         local->hostapd = 1;
967         } else {
968                 local->hostapd = 0;
969                 ret = hostap_disable_hostapd(local, rtnl_locked);
970                 if (ret != 0)
971                         local->hostapd = 1;
972         }
973
974         return ret;
975 }
976
977
978 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
979 {
980         int ret;
981
982         if (val < 0 || val > 1)
983                 return -EINVAL;
984
985         if (local->hostapd_sta == val)
986                 return 0;
987
988         if (val) {
989                 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
990                 if (ret == 0)
991                         local->hostapd_sta = 1;
992         } else {
993                 local->hostapd_sta = 0;
994                 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
995                 if (ret != 0)
996                         local->hostapd_sta = 1;
997         }
998
999
1000         return ret;
1001 }
1002
1003
1004 int prism2_update_comms_qual(struct net_device *dev)
1005 {
1006         struct hostap_interface *iface;
1007         local_info_t *local;
1008         int ret = 0;
1009         struct hfa384x_comms_quality sq;
1010
1011         iface = netdev_priv(dev);
1012         local = iface->local;
1013         if (!local->sta_fw_ver)
1014                 ret = -1;
1015         else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1016                 if (local->func->get_rid(local->dev,
1017                                          HFA384X_RID_DBMCOMMSQUALITY,
1018                                          &sq, sizeof(sq), 1) >= 0) {
1019                         local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1020                         local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1021                         local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1022                         local->last_comms_qual_update = jiffies;
1023                 } else
1024                         ret = -1;
1025         } else {
1026                 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1027                                          &sq, sizeof(sq), 1) >= 0) {
1028                         local->comms_qual = le16_to_cpu(sq.comm_qual);
1029                         local->avg_signal = HFA384X_LEVEL_TO_dBm(
1030                                 le16_to_cpu(sq.signal_level));
1031                         local->avg_noise = HFA384X_LEVEL_TO_dBm(
1032                                 le16_to_cpu(sq.noise_level));
1033                         local->last_comms_qual_update = jiffies;
1034                 } else
1035                         ret = -1;
1036         }
1037
1038         return ret;
1039 }
1040
1041
1042 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1043                          u8 *body, size_t bodylen)
1044 {
1045         struct sk_buff *skb;
1046         struct hostap_ieee80211_mgmt *mgmt;
1047         struct hostap_skb_tx_data *meta;
1048         struct net_device *dev = local->dev;
1049
1050         skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1051         if (skb == NULL)
1052                 return -ENOMEM;
1053
1054         mgmt = (struct hostap_ieee80211_mgmt *)
1055                 skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1056         memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1057         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1058         memcpy(mgmt->da, dst, ETH_ALEN);
1059         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1060         memcpy(mgmt->bssid, dst, ETH_ALEN);
1061         if (body)
1062                 memcpy(skb_put(skb, bodylen), body, bodylen);
1063
1064         meta = (struct hostap_skb_tx_data *) skb->cb;
1065         memset(meta, 0, sizeof(*meta));
1066         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1067         meta->iface = netdev_priv(dev);
1068
1069         skb->dev = dev;
1070         skb_reset_mac_header(skb);
1071         skb_reset_network_header(skb);
1072         dev_queue_xmit(skb);
1073
1074         return 0;
1075 }
1076
1077
1078 int prism2_sta_deauth(local_info_t *local, u16 reason)
1079 {
1080         union iwreq_data wrqu;
1081         int ret;
1082         __le16 val = cpu_to_le16(reason);
1083
1084         if (local->iw_mode != IW_MODE_INFRA ||
1085             is_zero_ether_addr(local->bssid) ||
1086             ether_addr_equal(local->bssid, "\x44\x44\x44\x44\x44\x44"))
1087                 return 0;
1088
1089         ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1090                                    (u8 *) &val, 2);
1091         memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1092         wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1093         return ret;
1094 }
1095
1096
1097 struct proc_dir_entry *hostap_proc;
1098
1099 static int __init hostap_init(void)
1100 {
1101         if (init_net.proc_net != NULL) {
1102                 hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1103                 if (!hostap_proc)
1104                         printk(KERN_WARNING "Failed to mkdir "
1105                                "/proc/net/hostap\n");
1106         } else
1107                 hostap_proc = NULL;
1108
1109         return 0;
1110 }
1111
1112
1113 static void __exit hostap_exit(void)
1114 {
1115         if (hostap_proc != NULL) {
1116                 hostap_proc = NULL;
1117                 remove_proc_entry("hostap", init_net.proc_net);
1118         }
1119 }
1120
1121
1122 EXPORT_SYMBOL(hostap_set_word);
1123 EXPORT_SYMBOL(hostap_set_string);
1124 EXPORT_SYMBOL(hostap_get_porttype);
1125 EXPORT_SYMBOL(hostap_set_encryption);
1126 EXPORT_SYMBOL(hostap_set_antsel);
1127 EXPORT_SYMBOL(hostap_set_roaming);
1128 EXPORT_SYMBOL(hostap_set_auth_algs);
1129 EXPORT_SYMBOL(hostap_dump_rx_header);
1130 EXPORT_SYMBOL(hostap_dump_tx_header);
1131 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1132 EXPORT_SYMBOL(hostap_setup_dev);
1133 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1134 EXPORT_SYMBOL(hostap_set_hostapd);
1135 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1136 EXPORT_SYMBOL(hostap_add_interface);
1137 EXPORT_SYMBOL(hostap_remove_interface);
1138 EXPORT_SYMBOL(prism2_update_comms_qual);
1139
1140 module_init(hostap_init);
1141 module_exit(hostap_exit);