⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wpa_supplicant.c

📁 一个Linux下无线网卡的设置工具
💻 C
📖 第 1 页 / 共 5 页
字号:
		} else {			/* Assume that dynamic WEP-104 keys will be used and			 * set cipher suites in order for drivers to expect			 * encryption. */			cipher_pairwise = cipher_group = CIPHER_WEP104;		}	}	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {		/* Set the key before (and later after) association */		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);	}	wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);	memset(&params, 0, sizeof(params));	if (bss) {		params.bssid = bss->bssid;		params.ssid = bss->ssid;		params.ssid_len = bss->ssid_len;		params.freq = bss->freq;	} else {		params.ssid = ssid->ssid;		params.ssid_len = ssid->ssid_len;	}	params.wpa_ie = wpa_ie;	params.wpa_ie_len = wpa_ie_len;	params.pairwise_suite = cipher_pairwise;	params.group_suite = cipher_group;	params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);	params.auth_alg = algs;	params.mode = ssid->mode;	if (wpa_drv_associate(wpa_s, &params) < 0) {		wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "			"failed");		/* try to continue anyway; new association will be tried again		 * after timeout */		assoc_failed = 1;	}	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {		/* Set the key after the association just in case association		 * cleared the previously configured key. */		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);		/* No need to timeout authentication since there is no key		 * management. */		wpa_supplicant_cancel_auth_timeout(wpa_s);		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);	} else {		/* Timeout for IEEE 802.11 authentication and association */		int timeout;		if (assoc_failed)			timeout = 5;		else if (wpa_s->conf->ap_scan == 1)			timeout = 10;		else			timeout = 60;		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);	}	if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&	    capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {		/* Set static WEP keys again */		int i;		for (i = 0; i < NUM_WEP_KEYS; i++) {			if (ssid->wep_key_len[i]) {				wpa_set_wep_key(wpa_s,						i == ssid->wep_tx_keyidx,						i, ssid->wep_key[i],						ssid->wep_key_len[i]);			}		}	}	wpa_s->current_ssid = ssid;	wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);	wpa_supplicant_initiate_eapol(wpa_s);}/** * wpa_supplicant_disassociate - Disassociate the current connection * @wpa_s: Pointer to wpa_supplicant data * @reason_code: IEEE 802.11 reason code for the disassociate frame * * This function is used to request %wpa_supplicant to disassociate with the * current AP. */void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,				 int reason_code){	u8 *addr = NULL;	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);	if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {		wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);		addr = wpa_s->bssid;	}	wpa_clear_keys(wpa_s, addr);	wpa_s->current_ssid = NULL;	wpa_sm_set_config(wpa_s->wpa, NULL);	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);}/** * wpa_supplicant_deauthenticate - Deauthenticate the current connection * @wpa_s: Pointer to wpa_supplicant data * @reason_code: IEEE 802.11 reason code for the deauthenticate frame * * This function is used to request %wpa_supplicant to disassociate with the * current AP. */void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,				   int reason_code){	u8 *addr = NULL;	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);	if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {		wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);		addr = wpa_s->bssid;	}	wpa_clear_keys(wpa_s, addr);	wpa_s->current_ssid = NULL;	wpa_sm_set_config(wpa_s->wpa, NULL);	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);}/** * wpa_supplicant_get_scan_results - Get scan results * @wpa_s: Pointer to wpa_supplicant data * Returns: 0 on success, -1 on failure * * This function is request the current scan results from the driver and stores * a local copy of the results in wpa_s->scan_results. */int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s){#define SCAN_AP_LIMIT 128	struct wpa_scan_result *results, *tmp;	int num;	results = malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));	if (results == NULL) {		wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "			   "results");		return -1;	}	num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);	wpa_printf(MSG_DEBUG, "Scan results: %d", num);	if (num < 0) {		wpa_printf(MSG_DEBUG, "Failed to get scan results");		free(results);		return -1;	}	if (num > SCAN_AP_LIMIT) {		wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",			   num, SCAN_AP_LIMIT);		num = SCAN_AP_LIMIT;	}	/* Free unneeded memory for unused scan result entries */	tmp = realloc(results, num * sizeof(struct wpa_scan_result));	if (tmp || num == 0) {		results = tmp;	}	free(wpa_s->scan_results);	wpa_s->scan_results = results;	wpa_s->num_scan_results = num;	return 0;}#ifndef CONFIG_NO_WPAstatic int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s){	int i, ret = 0;	struct wpa_scan_result *results, *curr = NULL;	results = wpa_s->scan_results;	if (results == NULL) {		return -1;	}	for (i = 0; i < wpa_s->num_scan_results; i++) {		if (memcmp(results[i].bssid, wpa_s->bssid, ETH_ALEN) == 0) {			curr = &results[i];			break;		}	}	if (curr) {		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, curr->wpa_ie,					 curr->wpa_ie_len) ||		    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, curr->rsn_ie,					 curr->rsn_ie_len))			ret = -1;	} else {		ret = -1;	}	return ret;}static int wpa_supplicant_get_beacon_ie(void *ctx){	struct wpa_supplicant *wpa_s = ctx;	if (wpa_get_beacon_ie(wpa_s) == 0) {		return 0;	}	/* No WPA/RSN IE found in the cached scan results. Try to get updated	 * scan results from the driver. */	if (wpa_supplicant_get_scan_results(wpa_s) < 0) {		return -1;	}	return wpa_get_beacon_ie(wpa_s);}#endif /* CONFIG_NO_WPA *//** * wpa_supplicant_get_ssid - Get a pointer to the current network structure * @wpa_s: Pointer to wpa_supplicant data * Returns: A pointer to the current network structure or %NULL on failure */struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s){	struct wpa_ssid *entry;	u8 ssid[MAX_SSID_LEN];	int ssid_len;	u8 bssid[ETH_ALEN];	ssid_len = wpa_drv_get_ssid(wpa_s, ssid);	if (ssid_len < 0) {		wpa_printf(MSG_WARNING, "Could not read SSID from driver.");		return NULL;	}	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {		wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");		return NULL;	}	entry = wpa_s->conf->ssid;	while (entry) {		if (!entry->disabled &&		    ssid_len == entry->ssid_len &&		    memcmp(ssid, entry->ssid, ssid_len) == 0 &&		    (!entry->bssid_set ||		     memcmp(bssid, entry->bssid, ETH_ALEN) == 0))			return entry;		entry = entry->next;	}	return NULL;}#ifndef CONFIG_NO_WPAstatic u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,			     const void *data, u16 data_len,			     size_t *msg_len, void **data_pos){	return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);}static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,			   const u8 *buf, size_t len){	return wpa_ether_send(wpa_s, dest, proto, buf, len);}static void _wpa_supplicant_req_scan(void *wpa_s, int sec, int usec){	wpa_supplicant_req_scan(wpa_s, sec, usec);}static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s){	wpa_supplicant_cancel_auth_timeout(wpa_s);}static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state){	wpa_supplicant_set_state(wpa_s, state);}static wpa_states _wpa_supplicant_get_state(void *wpa_s){	return wpa_supplicant_get_state(wpa_s);}static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code){	wpa_supplicant_disassociate(wpa_s, reason_code);}static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code){	wpa_supplicant_deauthenticate(wpa_s, reason_code);}static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s){	return wpa_supplicant_get_ssid(wpa_s);}static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid){	return wpa_drv_get_bssid(wpa_s, bssid);}static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,				  const u8 *addr, int key_idx, int set_tx,				  const u8 *seq, size_t seq_len,				  const u8 *key, size_t key_len){	return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,			       key, key_len);}static int wpa_supplicant_add_pmkid(void *wpa_s,				    const u8 *bssid, const u8 *pmkid){	return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);}static int wpa_supplicant_remove_pmkid(void *wpa_s,				       const u8 *bssid, const u8 *pmkid){	return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);}#endif /* CONFIG_NO_WPA */static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,				     const char *name){	int i;	if (wpa_s == NULL)		return -1;	if (wpa_supplicant_drivers[0] == NULL) {		wpa_printf(MSG_ERROR, "No driver interfaces build into "			   "wpa_supplicant.");		return -1;	}	if (name == NULL) {		/* default to first driver in the list */		wpa_s->driver = wpa_supplicant_drivers[0];		return 0;	}	for (i = 0; wpa_supplicant_drivers[i]; i++) {		if (strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {			wpa_s->driver = wpa_supplicant_drivers[i];			return 0;		}	}	printf("Unsupported driver '%s'.\n", name);	return -1;}void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,			     const u8 *buf, size_t len){	struct wpa_supplicant *wpa_s = ctx;	wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));	wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {		wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "			   "no key management is configured");		return;	}	if (wpa_s->eapol_received == 0) {		/* Timeout for completing IEEE 802.1X and WPA authentication */		wpa_supplicant_req_auth_timeout(			wpa_s,			(wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||			 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?			70 : 10, 0);	}	wpa_s->eapol_received++;	if (wpa_s->countermeasures) {		wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "			   "packet");		return;	}	/* Source address of the incoming EAPOL frame could be compared to the	 * current BSSID. However, it is possible that a centralized	 * Authenticator could be using another MAC address than the BSSID of	 * an AP, so just allow any address to be used for now. The replies are	 * still sent to the current BSSID (if available), though. */	memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);	if (wpa_s->key_mgmt != WPA_KEY_MGMT_PSK &&	    eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)		return;	wpa_drv_poll(wpa_s);	wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);}/** * wpa_supplicant_driver_init - Initialize driver interface parameters * @wpa_s: Pointer to wpa_supplicant data * @wait_for_interface: 0 = do not wait for the interface (reports a failure if * the interface is not present), 1 = wait until the interface is available * Returns: 0 on success, -1 on failure * * This function is called to initialize driver interface parameters. * wpa_drv_init() must have been called before this function to initialize the * driver interface. */int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s,			       int wait_for_interface){	static int interface_count = 0;	for (;;) {		if (wpa_s->driver->send_eapol) {			const u8 *addr = wpa_drv_get_mac_addr(wpa_s);			if (addr)				memcpy(wpa_s->own_addr, addr, ETH_ALEN);

⌨️ 快捷键说明

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