📄 wrapper.c
字号:
/* * Copyright (C) 2003 Pontus Fuchs * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/vmalloc.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/miscdevice.h>#include <linux/pci.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/wireless.h>#include <linux/if_arp.h>#include <net/iw_handler.h>#include <asm/uaccess.h>#include "wrapper.h"#include "loader.h"#include "ndis.h"#define DRV_NAME "ndiswrapper"#define DRV_VERSION "0.2"/* Define this if you are developing and ndis_init_one crashes. When using the old PCI-API a reboot is not needed when this function crashes. A simple rmmod -f will do the trick and you can try again.*//*#define DEBUG_CRASH_ON_INIT*//* List of loaded drivers */static LIST_HEAD(driverlist);/* Protects driver list */static spinlock_t driverlist_lock = SPIN_LOCK_UNLOCKED;extern int image_offset;/* * Perform a sync query and deal with the possibility of an async query. * */static int doquery(struct ndis_handle *handle, unsigned int oid, char *buf, int bufsize, unsigned int *written , unsigned int *needed){ int res; spin_lock(&handle->query_lock); handle->query_wait_done = 0; DBGTRACE("Calling query at %08x rva(%08x)\n", (int)handle->driver->miniport_char.query, (int)handle->driver->miniport_char.query - image_offset); res = handle->driver->miniport_char.query(handle->adapter_ctx, oid, buf, bufsize, written, needed); if(!res) goto out; if(res != NDIS_STATUS_PENDING) goto out; /* This is a very bad way to wait but we cannot sleap here as when ifconfig gathers statistics we * get a query from a context where is_atomic() = 1 */ while(handle->query_wait_done == 0); res = handle->query_wait_res;out: spin_unlock(&handle->query_lock); return res; }/* * * Called via function pointer if query returns NDIS_STATUS_PENDING */STDCALL void NdisMQueryInformationComplete(struct ndis_handle *handle, unsigned int status){ DBGTRACE("%s: %08x\n", __FUNCTION__, status); handle->query_wait_res = status; handle->query_wait_done = 1;}/* * * Called via function pointer if setinformation returns NDIS_STATUS_PENDING */STDCALL void NdisMSetInformationComplete(struct ndis_handle *handle, unsigned int status){ DBGTRACE("%s: %08x\n", __FUNCTION__, status);}/* * Make a query that has an int as the result. * */static int query_int(struct ndis_handle *handle, int oid, int *data){ unsigned int res, written, needed; res = doquery(handle, oid, (char*)data, 4, &written, &needed); if(!res) return 0; *data = 0; return res;}/* * Set an int * */static int set_int(struct ndis_handle *handle, int oid, int data){ unsigned int written, needed; return handle->driver->miniport_char.setinfo(handle->adapter_ctx, oid, (char*)&data, sizeof(int), &written, &needed);}static int ndis_set_essid(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int res, written, needed; struct essid_req req; if(wrqu->essid.length > 33) { printk(KERN_ERR "%s: ESSID too long\n", __FUNCTION__); return -1; } memset(&req.essid, 0, sizeof(req.essid)); memcpy(&req.essid, extra, wrqu->essid.length-1); req.len = wrqu->essid.length-1; res = handle->driver->miniport_char.setinfo(handle->adapter_ctx, NDIS_OID_ESSID, (char*)&req, sizeof(req), &written, &needed); if(res) { DBGTRACE("set_essid returned failure: %08x\n", res); return -1; } return 0;}static int ndis_get_essid(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int res, written, needed; struct essid_req req; res = doquery(handle, NDIS_OID_ESSID, (char*)&req, sizeof(req), &written, &needed); if(res) return -1; memcpy(extra, &req.essid, req.len); extra[req.len] = 0; wrqu->essid.flags = 1; wrqu->essid.length = req.len + 1; return 0;}static int ndis_set_mode(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ int ndis_mode; int res; switch(wrqu->mode) { case IW_MODE_ADHOC: ndis_mode = 0; break; case IW_MODE_INFRA: ndis_mode = 1; break; default: printk(KERN_ERR "%s Unknown mode %d\n", __FUNCTION__, wrqu->mode); return -1; } res = set_int(dev->priv, NDIS_OID_MODE, ndis_mode); if(res) { DBGTRACE("set_mode returned failure: %08x\n", res); return -1; } return 0;}static int ndis_get_mode(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int ndis_mode, mode; int res = query_int(handle, NDIS_OID_MODE, &ndis_mode); if(res) return -1; switch(ndis_mode) { case 0: mode = IW_MODE_ADHOC; break; case 1: mode = IW_MODE_INFRA; break; default: printk(KERN_ERR "%s Unknown mode\n", __FUNCTION__); return -1; break; } wrqu->mode = mode; return 0;}const char *net_type_to_name(int net_type){ static const char *net_names[] = {"IEEE 802.11FH", "IEEE 802.11b", "IEEE 802.11a", "IEEE 802.11g"}; static const char *unknown = "IEEE 802.11"; if (net_type >= 0 && net_type < (sizeof(net_names)/sizeof(net_names[0]))) return net_names[net_type]; else return unknown;}static int ndis_get_name(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int network_type, res; res = query_int(handle, NDIS_OID_NETWORK_TYPE_IN_USE, &network_type); if (res) network_type = -1; strncpy(wrqu->name, net_type_to_name(network_type), sizeof(wrqu->name) - 1); wrqu->name[sizeof(wrqu->name)-1] = 0; return 0;}static int ndis_get_freq(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int res, written, needed; struct ndis_configuration req; res = doquery(handle, NDIS_OID_CONFIGURATION, (char*)&req, sizeof(req), &written, &needed); if(res) return -1; memset(&(wrqu->freq), 0, sizeof(struct iw_freq)); /* see comment in wireless.h above the "struct iw_freq" definition for an explanation of this if NOTE: 1000000 is due to the kHz */ if (req.ds_config > 1000000) { wrqu->freq.m = req.ds_config / 10; wrqu->freq.e = 1; } else wrqu->freq.m = req.ds_config; /* convert from kHz to Hz */ wrqu->freq.e += 3; return 0;}static int ndis_get_tx_power(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int ndis_power; int res = query_int(handle, NDIS_OID_MODE, &ndis_power); if(res) return -1; /* TODO: convert mW to dBm but, without floating point we could at best do a table, so for now just return the mW * 1000 as an obviously bogus value. */ wrqu->power.value = ndis_power * 1000; return 0;}static int ndis_get_bitrate(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int ndis_rate; /* not sure if this is the corrent OID or if it gives only the max rate */ int res = query_int(handle, NDIS_OID_GEN_SPEED, &ndis_rate); if(res) return -1; /* *of course* windows specifies the rate in multiples of 100 */ wrqu->bitrate.value = ndis_rate * 100; return 0;}static int ndis_get_rts_threshold(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int ndis_rts_threshold; int res = query_int(handle, NDIS_OID_RTS_THRESH, &ndis_rts_threshold); if(res) return -1; wrqu->rts.value = ndis_rts_threshold; return 0;}static int ndis_get_frag_threshold(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int ndis_frag_threshold; int res = query_int(handle, NDIS_OID_FRAG_THRESH, &ndis_frag_threshold); if(res) return -1; wrqu->frag.value = ndis_frag_threshold; return 0;}static int ndis_get_ap_address(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int res, written, needed; __u8 mac_address[6]; res = doquery(handle, NDIS_OID_BSSID, (char*)&mac_address, sizeof(mac_address), &written, &needed); if(res) return -1; memcpy(wrqu->ap_addr.sa_data, mac_address, 6); wrqu->ap_addr.sa_family = ARPHRD_ETHER; return 0;}static int ndis_set_wep(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; unsigned int res, written, needed, auth_mode; struct wep_req req; if (wrqu->data.flags & IW_ENCODE_NOKEY) { res = set_int(handle, NDIS_OID_WEP_STATUS, NDIS_ENCODE_NOKEY); if (res) return -1; else return 0; } if (wrqu->data.flags & IW_ENCODE_DISABLED) { res = set_int(handle, NDIS_OID_WEP_STATUS, NDIS_ENCODE_DISABLED); } else { req.len = sizeof(struct wep_req); req.keyindex = wrqu->data.flags & IW_ENCODE_INDEX; req.keyindex |= (1 << 31); req.keylength = wrqu->data.length; handle->driver->key_len = req.keylength; memcpy(handle->driver->key_val, wrqu->data.pointer, req.keylength); memcpy(req.keymaterial, handle->driver->key_val, req.keylength); res = handle->driver->miniport_char.setinfo(handle->adapter_ctx, NDIS_OID_ADD_WEP, (char*)&req, sizeof(req), &written, &needed); if (res) return -1; res = set_int(handle, NDIS_OID_WEP_STATUS, NDIS_ENCODE_ENABLED); if (res) return -1; if (wrqu->data.flags & IW_ENCODE_OPEN) auth_mode = NDIS_ENCODE_OPEN; else auth_mode = NDIS_ENCODE_RESTRICTED; /* Ndis has another flag Ndis802_11AuthModeAutoSwitch * However, there is no equivalent IW_ENCODE flag for it */ res = set_int(handle, NDIS_OID_AUTH_MODE, auth_mode); if (res) return -1; } return 0;}static int ndis_get_wep(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra){ struct ndis_handle *handle = dev->priv; int status, res; res = query_int(handle, NDIS_OID_WEP_STATUS, &status); if (res) return -1; if (status & NDIS_ENCODE_ENABLED) wrqu->data.flags |= IW_ENCODE_ENABLED; else if (status & NDIS_ENCODE_DISABLED) wrqu->data.flags |= IW_ENCODE_DISABLED; else if (status & NDIS_ENCODE_NOKEY) wrqu->data.flags |= IW_ENCODE_NOKEY; res = query_int(handle, NDIS_OID_AUTH_MODE, &status); if (res) return -1; if (status & NDIS_ENCODE_OPEN) wrqu->data.flags |= IW_ENCODE_OPEN; if (status & NDIS_ENCODE_RESTRICTED) wrqu->data.flags |= IW_ENCODE_RESTRICTED; if (status & NDIS_ENCODE_OPEN_RESTRICTED) wrqu->data.flags |= (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED); wrqu->data.length = handle->driver->key_len; memcpy(extra, handle->driver->key_val, handle->driver->key_len); return 0;} char *ndis_translate_scan(struct net_device *dev, char *event, char *end_buf, struct ssid_item *item){ struct iw_event iwe; char *current_val; int i; /* add mac address */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -