wrapper.c

来自「改文件可以安装无线网卡在linux下的驱动,大家可以在网站上查找一下用法」· C语言 代码 · 共 1,551 行 · 第 1/3 页

C
1,551 行
字号
	.get_link		= ndis_get_link,};#endif/* worker procedure to take care of setting/checking various states */static void wrapper_worker_proc(void *param){	struct ndis_handle *handle = (struct ndis_handle *)param;	DBGTRACE2("%lu", handle->wrapper_work);	if (test_bit(SHUTDOWN, &handle->wrapper_work))		TRACEEXIT3(return);	if (test_and_clear_bit(SET_INFRA_MODE, &handle->wrapper_work))		set_infra_mode(handle, handle->infrastructure_mode);	if (test_and_clear_bit(LINK_STATUS_CHANGED, &handle->wrapper_work))		link_status_handler(handle);	if (test_and_clear_bit(SET_ESSID, &handle->wrapper_work))		set_essid(handle, handle->essid.essid, handle->essid.length);	if (test_and_clear_bit(SET_PACKET_FILTER, &handle->wrapper_work))		set_packet_filter(handle);	if (test_and_clear_bit(COLLECT_STATS, &handle->wrapper_work))		update_wireless_stats(handle);	if (test_and_clear_bit(SUSPEND_RESUME, &handle->wrapper_work)) {		NDIS_STATUS res;		struct net_device *net_dev = handle->net_dev;		if (test_bit(HW_HALTED, &handle->hw_status)) {			res = miniport_init(handle);			if (res)				ERROR("initialization failed: %08X", res);			clear_bit(HW_HALTED, &handle->hw_status);		} else {			res = miniport_set_int(handle, OID_PNP_SET_POWER,					       NdisDeviceStateD0);			clear_bit(HW_SUSPENDED, &handle->hw_status);			DBGTRACE2("%s: setting power to state %d returns %d",				  net_dev->name, NdisDeviceStateD0, res);			if (res)				WARNING("No pnp capabilities for pm (%08X)",					res);			/* ignore this error and continue */			res = 0;			/*			  if (miniport->pnp_event_notify) {			  INFO("%s", "calling pnp_event_notify");			  LIN2WIN4(miniport->pnp_event_notify, handle,			  NDIS_PNP_PROFILE_CHANGED,			  &profile_inf, sizeof(profile_inf));			  }			*/		}		if (!res) {			hangcheck_add(handle);			stats_timer_add(handle);			set_scan(handle);			set_bit(SET_ESSID, &handle->wrapper_work);			schedule_work(&handle->wrapper_worker);			if (netif_running(net_dev)) {				netif_device_attach(net_dev);				netif_start_queue(net_dev);			}			DBGTRACE2("%s: device resumed", net_dev->name);		}	}	TRACEEXIT3(return);}/* check capabilites - mainly for WPA */static void check_capa(struct ndis_handle *handle){	int i, mode;	NDIS_STATUS res;	struct ndis_assoc_info ndis_assoc_info;	struct ndis_add_key ndis_key;	TRACEENTER1("%s", "");	/* check if WEP is supported */	if (set_encr_mode(handle, Ndis802_11Encryption1Enabled) == 0 &&	    miniport_query_int(handle,			       OID_802_11_ENCRYPTION_STATUS, &i) == 0 &&	    (i == Ndis802_11Encryption1Enabled ||	     i == Ndis802_11Encryption1KeyAbsent))		set_bit(Ndis802_11Encryption1Enabled, &handle->capa);	/* check if WPA is supported */	DBGTRACE2("%s", "");	if (set_auth_mode(handle, Ndis802_11AuthModeWPA) ||	    miniport_query_int(handle, OID_802_11_AUTHENTICATION_MODE, &i) ||	    i != Ndis802_11AuthModeWPA)		TRACEEXIT1(return);	/* check for highest encryption */	if (set_encr_mode(handle, Ndis802_11Encryption3Enabled) == 0 &&	    miniport_query_int(handle,			       OID_802_11_ENCRYPTION_STATUS, &i) == 0 &&	    (i == Ndis802_11Encryption3Enabled ||	     i == Ndis802_11Encryption3KeyAbsent))		mode = Ndis802_11Encryption3Enabled;	else if (set_encr_mode(handle, Ndis802_11Encryption2Enabled) == 0 &&		 miniport_query_int(handle,				    OID_802_11_ENCRYPTION_STATUS, &i) == 0 &&		 (i == Ndis802_11Encryption2Enabled ||		  i == Ndis802_11Encryption2KeyAbsent))		mode = Ndis802_11Encryption2Enabled;	else if (set_encr_mode(handle, Ndis802_11Encryption1Enabled) == 0 &&		 miniport_query_int(handle,				    OID_802_11_ENCRYPTION_STATUS, &i) == 0 &&		 (i == Ndis802_11Encryption1Enabled ||		  i == Ndis802_11Encryption1KeyAbsent))		mode = Ndis802_11Encryption1Enabled;	else		mode = Ndis802_11EncryptionDisabled;	DBGTRACE1("highest encryption mode supported = %d", mode);	if (mode == Ndis802_11EncryptionDisabled)		TRACEEXIT1(return);	set_bit(Ndis802_11Encryption1Enabled, &handle->capa);	if (mode == Ndis802_11Encryption1Enabled)		TRACEEXIT1(return);	ndis_key.length = 32;	ndis_key.index = 0xC0000001;	ndis_key.struct_size = sizeof(ndis_key);	res = miniport_set_info(handle, OID_802_11_ADD_KEY, &ndis_key,				ndis_key.struct_size);	DBGTRACE2("add key returns %08X, size = %lu",		  res, (unsigned long)sizeof(ndis_key));	if (res != NDIS_STATUS_INVALID_DATA)		TRACEEXIT1(return);	res = miniport_query_info(handle, OID_802_11_ASSOCIATION_INFORMATION,				  &ndis_assoc_info, sizeof(ndis_assoc_info));	DBGTRACE2("assoc info returns %d", res);	if (res == NDIS_STATUS_NOT_SUPPORTED)		TRACEEXIT1(return);	set_bit(Ndis802_11Encryption2Enabled, &handle->capa);	if (mode == Ndis802_11Encryption3Enabled)		set_bit(Ndis802_11Encryption3Enabled, &handle->capa);	TRACEEXIT1(return);}int ndis_reinit(struct ndis_handle *handle){	/* instead of implementing full shutdown/restart, we (ab)use	 * suspend/resume functionality */	int i = test_bit(ATTR_HALT_ON_SUSPEND, &handle->attributes);	set_bit(ATTR_HALT_ON_SUSPEND, &handle->attributes);	if (handle->device->bustype == NDIS_PCI_BUS) {		ndiswrapper_suspend_pci(handle->dev.pci, 3);		ndiswrapper_resume_pci(handle->dev.pci);	}	if (!i)		clear_bit(ATTR_HALT_ON_SUSPEND, &handle->attributes);	return 0;}static int ndis_set_mac_addr(struct net_device *dev, void *p){	struct ndis_handle *handle = dev->priv;	struct sockaddr *addr = p;	struct ndis_config_param param;	struct unicode_string key;	struct ansi_string ansi;	unsigned int i;	NDIS_STATUS res;	unsigned char mac_string[3 * ETH_ALEN];	mac_address mac;	/* string <-> ansi <-> unicode conversion is driving me nuts */	for (i = 0; i < sizeof(mac); i++)		mac[i] = addr->sa_data[i];	memset(mac_string, 0, sizeof(mac_string));	res = snprintf(mac_string, sizeof(mac_string), MACSTR,		       MAC2STR(mac));	DBGTRACE2("res = %d, mac_tring = %s", res, mac_string);	if (res != (sizeof(mac_string) - 1))		TRACEEXIT1(return -EINVAL);	ansi.buf = "mac_address";	ansi.buflen = ansi.len = strlen(ansi.buf);	if (RtlAnsiStringToUnicodeString(&key, &ansi, 1))		TRACEEXIT1(return -EINVAL);	ansi.buf = mac_string;	ansi.buflen = ansi.len = sizeof(mac_string);	if (RtlAnsiStringToUnicodeString(&param.data.ustring, &ansi, 1) !=	    NDIS_STATUS_SUCCESS) {		RtlFreeUnicodeString(&key);		TRACEEXIT1(return -EINVAL);	}	param.type = NDIS_CONFIG_PARAM_STRING;	NdisWriteConfiguration(&res, handle, &key, &param);	if (res != NDIS_STATUS_SUCCESS) {		RtlFreeUnicodeString(&key);		RtlFreeUnicodeString(&param.data.ustring);		TRACEEXIT1(return -EINVAL);	}	ndis_reinit(handle);	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);	RtlFreeUnicodeString(&key);	RtlFreeUnicodeString(&param.data.ustring);	TRACEEXIT1(return 0);}int setup_dev(struct net_device *dev){	struct ndis_handle *handle = dev->priv;	unsigned int i;	NDIS_STATUS res;	mac_address mac;	union iwreq_data wrqu;	if (strlen(if_name) > (IFNAMSIZ-1)) {		ERROR("interface name '%s' is too long", if_name);		return -1;	}	strncpy(dev->name, if_name, IFNAMSIZ-1);	dev->name[IFNAMSIZ-1] = '\0';	DBGTRACE1("%s: querying for mac", DRIVER_NAME);	res = miniport_query_info(handle, OID_802_3_CURRENT_ADDRESS,				  mac, sizeof(mac));	if (res) {		ERROR("%s", "unable to get mac address from driver");		return -EINVAL;	}	DBGTRACE1("mac:" MACSTR, MAC2STR(mac));	memcpy(&dev->dev_addr, mac, ETH_ALEN);	handle->max_send_packets = 1;	if (handle->driver->miniport_char.send_packets) {		res = miniport_query_int(handle, OID_GEN_MAXIMUM_SEND_PACKETS,					 &handle->max_send_packets);		DBGTRACE2("maximum send packets supported by driver: %d",			  handle->max_send_packets);		if (res == NDIS_STATUS_NOT_SUPPORTED)			handle->max_send_packets = 1;		else if (handle->max_send_packets > XMIT_RING_SIZE)			handle->max_send_packets = XMIT_RING_SIZE;		handle->xmit_array = kmalloc(sizeof(struct ndis_packet *) *					     handle->max_send_packets,					     GFP_KERNEL);		if (!handle->xmit_array) {			ERROR("couldn't allocate memory for tx_packets");			return -ENOMEM;		}	}	DBGTRACE2("maximum send packets used by ndiswrapper: %d",		  handle->max_send_packets);	memset(&wrqu, 0, sizeof(wrqu));	miniport_set_int(handle, OID_802_11_NETWORK_TYPE_IN_USE,			 Ndis802_11Automode);	set_infra_mode(handle, Ndis802_11Infrastructure);	set_essid(handle, "", 0);	res = miniport_query_int(handle, OID_802_3_MAXIMUM_LIST_SIZE, &i);	if (res == NDIS_STATUS_SUCCESS) {		DBGTRACE1("Multicast list size is %d", i);		handle->multicast_list_size = i;	}	if (handle->multicast_list_size)		handle->multicast_list =			kmalloc(handle->multicast_list_size * ETH_ALEN,				GFP_KERNEL);	if (set_privacy_filter(handle, Ndis802_11PrivFilterAcceptAll))		WARNING("%s", "Unable to set privacy filter");	ndis_set_rx_mode(dev);	dev->open = ndis_open;	dev->hard_start_xmit = start_xmit;	dev->stop = ndis_close;	dev->get_stats = ndis_get_stats;	dev->do_ioctl = ndis_ioctl;	dev->get_wireless_stats = get_wireless_stats;	dev->wireless_handlers	= (struct iw_handler_def *)&ndis_handler_def;	dev->set_multicast_list = ndis_set_rx_mode;	dev->set_mac_address = ndis_set_mac_addr;#ifdef HAVE_ETHTOOL	dev->ethtool_ops = &ndis_ethtool_ops;#endif	if (handle->ndis_irq)		dev->irq = handle->ndis_irq->irq.irq;	dev->mem_start = handle->mem_start;	dev->mem_end = handle->mem_end;	res = register_netdev(dev);	if (res) {		ERROR("cannot register net device %s", dev->name);		return res;	}	netif_stop_queue(dev);	printk(KERN_INFO "%s: %s ethernet device " MACSTR " using driver %s,"	       " configuration file %s\n",	       dev->name, DRIVER_NAME, MAC2STR(dev->dev_addr),	       handle->driver->name, handle->device->conf_file_name);	check_capa(handle);	DBGTRACE1("capbilities = %ld", handle->capa);	printk(KERN_INFO "%s: encryption modes supported: %s%s%s\n",	       dev->name,	       test_bit(Ndis802_11Encryption1Enabled, &handle->capa) ?	       "WEP" : "none",	       test_bit(Ndis802_11Encryption2Enabled, &handle->capa) ?	       ", WPA with TKIP" : "",	       test_bit(Ndis802_11Encryption3Enabled, &handle->capa) ?	       ", WPA with AES/CCMP" : "");	/* check_capa changes auth_mode and encr_mode, so set them again */	set_infra_mode(handle, Ndis802_11Infrastructure);	set_auth_mode(handle, Ndis802_11AuthModeOpen);	set_encr_mode(handle, Ndis802_11EncryptionDisabled);	set_essid(handle, "", 0);	/* some cards (e.g., RaLink) need a scan before they can associate */	set_scan(handle);	hangcheck_add(handle);	stats_timer_add(handle);	ndiswrapper_procfs_add_iface(handle);	return 0;}struct net_device *ndis_init_netdev(struct ndis_handle **phandle,				    struct ndis_device *device,				    struct ndis_driver *driver){	int i, *ip;	struct net_device *dev;	struct ndis_handle *handle;	dev = alloc_etherdev(sizeof(*handle));	if (!dev) {		ERROR("%s", "Unable to alloc etherdev");		return NULL;	}	SET_MODULE_OWNER(dev);	handle = dev->priv;	DBGTRACE1("handle= %p", handle);	/* Poison the fileds as they may contain function pointers	 * which my be called by the driver */	for (i = 0, ip = (int *)&handle->signature;	     (void *)&ip[i] < (void *)&handle->dev.pci; i++)		ip[i] = 0x1000+i;	handle->driver = driver;	handle->device = device;	handle->net_dev = dev;	handle->ndis_irq = NULL;	kspin_lock_init(&handle->xmit_lock);	init_MUTEX(&handle->ndis_comm_mutex);	init_waitqueue_head(&handle->ndis_comm_wq);	handle->ndis_comm_done = 0;	/* don't send packets until the card is associated */	handle->send_ok = 0;	INIT_WORK(&handle->xmit_work, xmit_worker, handle);	handle->xmit_ring_start = 0;	handle->xmit_ring_pending = 0;	kspin_lock_init(&handle->send_packet_done_lock);	handle->encr_mode = Ndis802_11EncryptionDisabled;	handle->auth_mode = Ndis802_11AuthModeOpen;	handle->capa = 0;	handle->attributes = 0;	handle->reset_status = 0;	INIT_LIST_HEAD(&handle->timers);	kspin_lock_init(&handle->timers_lock);	handle->rx_packet = WRAP_FUNC_PTR(NdisMIndicateReceivePacket);	handle->send_complete = WRAP_FUNC_PTR(NdisMSendComplete);	handle->send_resource_avail =		WRAP_FUNC_PTR(NdisMSendResourcesAvailable);	handle->status = WRAP_FUNC_PTR(NdisMIndicateStatus);	handle->status_complete = WRAP_FUNC_PTR(NdisMIndicateStatusComplete);	handle->query_complete = WRAP_FUNC_PTR(NdisMQueryInformationComplete);	handle->set_complete = WRAP_FUNC_PTR(NdisMSetInformationComplete);	handle->reset_complete = WRAP_FUNC_PTR(NdisMResetComplete);	handle->eth_rx_indicate = WRAP_FUNC_PTR(EthRxIndicateHandler);	handle->eth_rx_complete = WRAP_FUNC_PTR(EthRxComplete);	handle->td_complete = WRAP_FUNC_PTR(NdisMTransferDataComplete);	handle->driver->miniport_char.adapter_shutdown = NULL;	handle->map_count = 0;	handle->map_dma_addr = NULL;	handle->nick[0] = 0;	handle->hangcheck_interval = hangcheck_interval;	handle->hangcheck_active = 0;	handle->scan_timestamp = 0;	memset(&handle->essid, 0, sizeof(handle->essid));	memset(&handle->encr_info, 0, sizeof(handle->encr_info));	handle->infrastructure_mode = Ndis802_11Infrastructure;	INIT_WORK(&handle->wrapper_worker, wrapper_worker_proc, handle);	handle->phys_device_obj = NULL;	*phandle = handle;	return dev;}static void module_cleanup(void){	loader_exit();	ndiswrapper_procfs_remove();	ndis_exit();	ntoskernel_exit();	misc_funcs_exit();}static int __init wrapper_init(void){	char *argv[] = {"loadndisdriver", #if defined DEBUG && DEBUG >= 1			"1"#else			"0"#endif			, NDISWRAPPER_VERSION, "-a", 0};	char *env[] = {NULL};	int err;	spin_lock_init(&spinlock_kspin_lock);#if defined(DEBUG) && DEBUG > 0	debug = DEBUG;#else	debug = 0;#endif	printk(KERN_INFO "%s version %s%s loaded (preempt=%s,smp=%s)\n",	       DRIVER_NAME, NDISWRAPPER_VERSION, EXTRA_VERSION,#if defined CONFIG_PREEMPT	       "yes",#else	       "no",#endif#ifdef CONFIG_SMP	       "yes"#else	       "no"#endif		);	if (misc_funcs_init() || ntoskernel_init() || ndis_init() ||	    loader_init()#ifdef CONFIG_USB	     || usb_init()#endif		) {		module_cleanup();		ERROR("couldn't initialize %s", DRIVER_NAME);		TRACEEXIT1(return -EPERM);	}	ndiswrapper_procfs_init();	DBGTRACE1("%s", "calling loadndisdriver");	err = call_usermodehelper("/sbin/loadndisdriver", argv, env#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)				  , 1#endif		);	if (err) {		ERROR("loadndiswrapper failed (%d); check system log "		      "for messages from 'loadndisdriver'", err);		module_cleanup();		TRACEEXIT1(return -EPERM);	}	TRACEEXIT1(return 0);}static void __exit wrapper_exit(void){	TRACEENTER1("");	module_cleanup();}module_init(wrapper_init);module_exit(wrapper_exit);MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?