📄 hostap_main.c
字号:
/* * Host AP (software wireless LAN access point) driver for * Intersil Prism2/2.5/3 - hostap.o module, common routines * * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen * <j@w1.fi> * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <linux/module.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/proc_fs.h>#include <linux/if_arp.h>#include <linux/delay.h>#include <linux/random.h>#include <linux/workqueue.h>#include <linux/kmod.h>#include <linux/rtnetlink.h>#include <linux/wireless.h>#include <linux/etherdevice.h>#include <net/net_namespace.h>#include <net/iw_handler.h>#include <net/ieee80211.h>#include <net/ieee80211_crypt.h>#include <asm/uaccess.h>#include "hostap_wlan.h"#include "hostap_80211.h"#include "hostap_ap.h"#include "hostap.h"MODULE_AUTHOR("Jouni Malinen");MODULE_DESCRIPTION("Host AP common routines");MODULE_LICENSE("GPL");#define TX_TIMEOUT (2 * HZ)#define PRISM2_MAX_FRAME_SIZE 2304#define PRISM2_MIN_MTU 256/* FIX: */#define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))struct net_device * hostap_add_interface(struct local_info *local, int type, int rtnl_locked, const char *prefix, const char *name){ struct net_device *dev, *mdev; struct hostap_interface *iface; int ret; dev = alloc_etherdev(sizeof(struct hostap_interface)); if (dev == NULL) return NULL; iface = netdev_priv(dev); iface->dev = dev; iface->local = local; iface->type = type; list_add(&iface->list, &local->hostap_interfaces); mdev = local->dev; memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN); dev->base_addr = mdev->base_addr; dev->irq = mdev->irq; dev->mem_start = mdev->mem_start; dev->mem_end = mdev->mem_end; hostap_setup_dev(dev, local, type); dev->destructor = free_netdev; sprintf(dev->name, "%s%s", prefix, name); if (!rtnl_locked) rtnl_lock(); ret = 0; if (strchr(dev->name, '%')) ret = dev_alloc_name(dev, dev->name); SET_NETDEV_DEV(dev, mdev->dev.parent); if (ret >= 0) ret = register_netdevice(dev); if (!rtnl_locked) rtnl_unlock(); if (ret < 0) { printk(KERN_WARNING "%s: failed to add new netdevice!\n", dev->name); free_netdev(dev); return NULL; } printk(KERN_DEBUG "%s: registered netdevice %s\n", mdev->name, dev->name); return dev;}void hostap_remove_interface(struct net_device *dev, int rtnl_locked, int remove_from_list){ struct hostap_interface *iface; if (!dev) return; iface = netdev_priv(dev); if (remove_from_list) { list_del(&iface->list); } if (dev == iface->local->ddev) iface->local->ddev = NULL; else if (dev == iface->local->apdev) iface->local->apdev = NULL; else if (dev == iface->local->stadev) iface->local->stadev = NULL; if (rtnl_locked) unregister_netdevice(dev); else unregister_netdev(dev); /* dev->destructor = free_netdev() will free the device data, including * private data, when removing the device */}static inline int prism2_wds_special_addr(u8 *addr){ if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5]) return 0; return 1;}int prism2_wds_add(local_info_t *local, u8 *remote_addr, int rtnl_locked){ struct net_device *dev; struct list_head *ptr; struct hostap_interface *iface, *empty, *match; empty = match = NULL; read_lock_bh(&local->iface_lock); list_for_each(ptr, &local->hostap_interfaces) { iface = list_entry(ptr, struct hostap_interface, list); if (iface->type != HOSTAP_INTERFACE_WDS) continue; if (prism2_wds_special_addr(iface->u.wds.remote_addr)) empty = iface; else if (memcmp(iface->u.wds.remote_addr, remote_addr, ETH_ALEN) == 0) { match = iface; break; } } if (!match && empty && !prism2_wds_special_addr(remote_addr)) { /* take pre-allocated entry into use */ memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN); read_unlock_bh(&local->iface_lock); printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n", local->dev->name, empty->dev->name); return 0; } read_unlock_bh(&local->iface_lock); if (!prism2_wds_special_addr(remote_addr)) { if (match) return -EEXIST; hostap_add_sta(local->ap, remote_addr); } if (local->wds_connections >= local->wds_max_connections) return -ENOBUFS; /* verify that there is room for wds# postfix in the interface name */ if (strlen(local->dev->name) > IFNAMSIZ - 5) { printk(KERN_DEBUG "'%s' too long base device name\n", local->dev->name); return -EINVAL; } dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked, local->ddev->name, "wds%d"); if (dev == NULL) return -ENOMEM; iface = netdev_priv(dev); memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN); local->wds_connections++; return 0;}int prism2_wds_del(local_info_t *local, u8 *remote_addr, int rtnl_locked, int do_not_remove){ unsigned long flags; struct list_head *ptr; struct hostap_interface *iface, *selected = NULL; write_lock_irqsave(&local->iface_lock, flags); list_for_each(ptr, &local->hostap_interfaces) { iface = list_entry(ptr, struct hostap_interface, list); if (iface->type != HOSTAP_INTERFACE_WDS) continue; if (memcmp(iface->u.wds.remote_addr, remote_addr, ETH_ALEN) == 0) { selected = iface; break; } } if (selected && !do_not_remove) list_del(&selected->list); write_unlock_irqrestore(&local->iface_lock, flags); if (selected) { if (do_not_remove) memset(selected->u.wds.remote_addr, 0, ETH_ALEN); else { hostap_remove_interface(selected->dev, rtnl_locked, 0); local->wds_connections--; } } return selected ? 0 : -ENODEV;}u16 hostap_tx_callback_register(local_info_t *local, void (*func)(struct sk_buff *, int ok, void *), void *data){ unsigned long flags; struct hostap_tx_callback_info *entry; entry = kmalloc(sizeof(*entry), GFP_ATOMIC); if (entry == NULL) return 0; entry->func = func; entry->data = data; spin_lock_irqsave(&local->lock, flags); entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1; entry->next = local->tx_callback; local->tx_callback = entry; spin_unlock_irqrestore(&local->lock, flags); return entry->idx;}int hostap_tx_callback_unregister(local_info_t *local, u16 idx){ unsigned long flags; struct hostap_tx_callback_info *cb, *prev = NULL; spin_lock_irqsave(&local->lock, flags); cb = local->tx_callback; while (cb != NULL && cb->idx != idx) { prev = cb; cb = cb->next; } if (cb) { if (prev == NULL) local->tx_callback = cb->next; else prev->next = cb->next; kfree(cb); } spin_unlock_irqrestore(&local->lock, flags); return cb ? 0 : -1;}/* val is in host byte order */int hostap_set_word(struct net_device *dev, int rid, u16 val){ struct hostap_interface *iface; u16 tmp = cpu_to_le16(val); iface = netdev_priv(dev); return iface->local->func->set_rid(dev, rid, &tmp, 2);}int hostap_set_string(struct net_device *dev, int rid, const char *val){ struct hostap_interface *iface; char buf[MAX_SSID_LEN + 2]; int len; iface = netdev_priv(dev); len = strlen(val); if (len > MAX_SSID_LEN) return -1; memset(buf, 0, sizeof(buf)); buf[0] = len; /* little endian 16 bit word */ memcpy(buf + 2, val, len); return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);}u16 hostap_get_porttype(local_info_t *local){ if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc) return HFA384X_PORTTYPE_PSEUDO_IBSS; if (local->iw_mode == IW_MODE_ADHOC) return HFA384X_PORTTYPE_IBSS; if (local->iw_mode == IW_MODE_INFRA) return HFA384X_PORTTYPE_BSS; if (local->iw_mode == IW_MODE_REPEAT) return HFA384X_PORTTYPE_WDS; if (local->iw_mode == IW_MODE_MONITOR) return HFA384X_PORTTYPE_PSEUDO_IBSS; return HFA384X_PORTTYPE_HOSTAP;}int hostap_set_encryption(local_info_t *local){ u16 val, old_val; int i, keylen, len, idx; char keybuf[WEP_KEY_LEN + 1]; enum { NONE, WEP, OTHER } encrypt_type; idx = local->tx_keyidx; if (local->crypt[idx] == NULL || local->crypt[idx]->ops == NULL) encrypt_type = NONE; else if (strcmp(local->crypt[idx]->ops->name, "WEP") == 0) encrypt_type = WEP; else encrypt_type = OTHER; if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2, 1) < 0) { printk(KERN_DEBUG "Could not read current WEP flags.\n"); goto fail; } le16_to_cpus(&val); old_val = val; if (encrypt_type != NONE || local->privacy_invoked) val |= HFA384X_WEPFLAGS_PRIVACYINVOKED; else val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED; if (local->open_wep || encrypt_type == NONE || ((local->ieee_802_1x || local->wpa) && local->host_decrypt)) val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED; else val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED; if ((encrypt_type != NONE || local->privacy_invoked) && (encrypt_type == OTHER || local->host_encrypt)) val |= HFA384X_WEPFLAGS_HOSTENCRYPT; else val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT; if ((encrypt_type != NONE || local->privacy_invoked) && (encrypt_type == OTHER || local->host_decrypt)) val |= HFA384X_WEPFLAGS_HOSTDECRYPT; else val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT; if (val != old_val && hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) { printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n", val); goto fail; } if (encrypt_type != WEP) return 0; /* 104-bit support seems to require that all the keys are set to the * same keylen */ keylen = 6; /* first 5 octets */ len = local->crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL, local->crypt[idx]->priv); if (idx >= 0 && idx < WEP_KEYS && len > 5) keylen = WEP_KEY_LEN + 1; /* first 13 octets */ for (i = 0; i < WEP_KEYS; i++) { memset(keybuf, 0, sizeof(keybuf)); if (local->crypt[i]) { (void) local->crypt[i]->ops->get_key( keybuf, sizeof(keybuf), NULL, local->crypt[i]->priv); } if (local->func->set_rid(local->dev, HFA384X_RID_CNFDEFAULTKEY0 + i, keybuf, keylen)) { printk(KERN_DEBUG "Could not set key %d (len=%d)\n", i, keylen); goto fail; } } if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) { printk(KERN_DEBUG "Could not set default keyid %d\n", idx); goto fail; } return 0; fail: printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name); return -1;}int hostap_set_antsel(local_info_t *local){ u16 val; int ret = 0; if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH && local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF, HFA386X_CR_TX_CONFIGURE, NULL, &val) == 0) { val &= ~(BIT(2) | BIT(1)); switch (local->antsel_tx) { case HOSTAP_ANTSEL_DIVERSITY: val |= BIT(1); break; case HOSTAP_ANTSEL_LOW: break; case HOSTAP_ANTSEL_HIGH: val |= BIT(2); break; } if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF, HFA386X_CR_TX_CONFIGURE, &val, NULL)) { printk(KERN_INFO "%s: setting TX AntSel failed\n", local->dev->name); ret = -1; } } if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH && local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF, HFA386X_CR_RX_CONFIGURE, NULL, &val) == 0) { val &= ~(BIT(1) | BIT(0)); switch (local->antsel_rx) { case HOSTAP_ANTSEL_DIVERSITY: break; case HOSTAP_ANTSEL_LOW: val |= BIT(0); break; case HOSTAP_ANTSEL_HIGH: val |= BIT(0) | BIT(1); break; } if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF, HFA386X_CR_RX_CONFIGURE, &val, NULL)) { printk(KERN_INFO "%s: setting RX AntSel failed\n", local->dev->name); ret = -1; } } return ret;}int hostap_set_roaming(local_info_t *local){ u16 val; switch (local->host_roaming) { case 1: val = HFA384X_ROAMING_HOST; break; case 2: val = HFA384X_ROAMING_DISABLED; break; case 0: default: val = HFA384X_ROAMING_FIRMWARE; break; } return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);}int hostap_set_auth_algs(local_info_t *local){ int val = local->auth_algs; /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication * set to include both Open and Shared Key flags. It tries to use * Shared Key authentication in that case even if WEP keys are not * configured.. STA f/w v0.7.6 is able to handle such configuration, * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */ if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) && val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY) val = PRISM2_AUTH_OPEN; if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) { printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x " "failed\n", local->dev->name, local->auth_algs); return -EINVAL; } return 0;}void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx){ u16 status, fc; DECLARE_MAC_BUF(mac); DECLARE_MAC_BUF(mac2); DECLARE_MAC_BUF(mac3); DECLARE_MAC_BUF(mac4); status = __le16_to_cpu(rx->status); printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, " "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; " "jiffies=%ld\n", name, status, (status >> 8) & 0x07, status >> 13, status & 1, rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies); fc = __le16_to_cpu(rx->frame_control); printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x " "data_len=%d%s%s\n", fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4, __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl), __le16_to_cpu(rx->data_len), fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "", fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : ""); printk(KERN_DEBUG " A1=%s A2=%s A3=%s A4=%s\n", print_mac(mac, rx->addr1), print_mac(mac2, rx->addr2), print_mac(mac3, rx->addr3), print_mac(mac4, rx->addr4)); printk(KERN_DEBUG " dst=%s src=%s len=%d\n", print_mac(mac, rx->dst_addr), print_mac(mac2, rx->src_addr), __be16_to_cpu(rx->len));}void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx){ u16 fc; DECLARE_MAC_BUF(mac); DECLARE_MAC_BUF(mac2); DECLARE_MAC_BUF(mac3); DECLARE_MAC_BUF(mac4); printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d " "tx_control=0x%04x; jiffies=%ld\n", name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate, __le16_to_cpu(tx->tx_control), jiffies); fc = __le16_to_cpu(tx->frame_control); printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x " "data_len=%d%s%s\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -