dbus: Fix init-failure-detection
[hostap-gosc2009.git] / src / drivers / driver_ipw.c
blobbeda6d0d6d6691e8b6e6e6c03893f91540ac8081
1 /*
2 * WPA Supplicant - driver interaction with Linux ipw2100/2200 drivers
3 * Copyright (c) 2005 Zhu Yi <yi.zhu@intel.com>
4 * Copyright (c) 2004 Lubomir Gelo <lgelo@cnc.sk>
5 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
12 * license.
14 * See README and COPYING for more details.
16 * Please note that ipw2100/2200 drivers change to use generic Linux wireless
17 * extensions if the kernel includes support for WE-18 or newer (Linux 2.6.13
18 * or newer). driver_wext.c should be used in those cases.
21 #include "includes.h"
22 #include <sys/ioctl.h>
24 #include "wireless_copy.h"
25 #include "common.h"
26 #include "driver.h"
27 #include "driver_wext.h"
29 struct wpa_driver_ipw_data {
30 void *wext; /* private data for driver_wext */
31 void *ctx;
32 char ifname[IFNAMSIZ + 1];
33 int sock;
36 /* following definitions must be kept in sync with ipw2100.c and ipw2200.c */
38 #define IPW_IOCTL_WPA_SUPPLICANT SIOCIWFIRSTPRIV+30
40 #define IPW_CMD_SET_WPA_PARAM 1
41 #define IPW_CMD_SET_WPA_IE 2
42 #define IPW_CMD_SET_ENCRYPTION 3
43 #define IPW_CMD_MLME 4
45 #define IPW_PARAM_WPA_ENABLED 1
46 #define IPW_PARAM_TKIP_COUNTERMEASURES 2
47 #define IPW_PARAM_DROP_UNENCRYPTED 3
48 #define IPW_PARAM_PRIVACY_INVOKED 4
49 #define IPW_PARAM_AUTH_ALGS 5
50 #define IPW_PARAM_IEEE_802_1X 6
52 #define IPW_MLME_STA_DEAUTH 1
53 #define IPW_MLME_STA_DISASSOC 2
55 #define IPW_CRYPT_ERR_UNKNOWN_ALG 2
56 #define IPW_CRYPT_ERR_UNKNOWN_ADDR 3
57 #define IPW_CRYPT_ERR_CRYPT_INIT_FAILED 4
58 #define IPW_CRYPT_ERR_KEY_SET_FAILED 5
59 #define IPW_CRYPT_ERR_TX_KEY_SET_FAILED 6
60 #define IPW_CRYPT_ERR_CARD_CONF_FAILED 7
62 #define IPW_CRYPT_ALG_NAME_LEN 16
64 struct ipw_param {
65 u32 cmd;
66 u8 sta_addr[ETH_ALEN];
67 union {
68 struct {
69 u8 name;
70 u32 value;
71 } wpa_param;
72 struct {
73 u32 len;
74 u8 reserved[32];
75 u8 data[0];
76 } wpa_ie;
77 struct{
78 u32 command;
79 u32 reason_code;
80 } mlme;
81 struct {
82 u8 alg[IPW_CRYPT_ALG_NAME_LEN];
83 u8 set_tx;
84 u32 err;
85 u8 idx;
86 u8 seq[8];
87 u16 key_len;
88 u8 key[0];
89 } crypt;
91 } u;
94 /* end of ipw2100.c and ipw2200.c code */
96 static int wpa_driver_ipw_set_auth_alg(void *priv, int auth_alg);
98 static int ipw_ioctl(struct wpa_driver_ipw_data *drv,
99 struct ipw_param *param, int len, int show_err)
101 struct iwreq iwr;
103 os_memset(&iwr, 0, sizeof(iwr));
104 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
105 iwr.u.data.pointer = (caddr_t) param;
106 iwr.u.data.length = len;
108 if (ioctl(drv->sock, IPW_IOCTL_WPA_SUPPLICANT, &iwr) < 0) {
109 int ret = errno;
110 if (show_err)
111 perror("ioctl[IPW_IOCTL_WPA_SUPPLICANT]");
112 return ret;
115 return 0;
119 static void ipw_show_set_key_error(struct ipw_param *param)
121 switch (param->u.crypt.err) {
122 case IPW_CRYPT_ERR_UNKNOWN_ALG:
123 wpa_printf(MSG_INFO, "Unknown algorithm '%s'.",
124 param->u.crypt.alg);
125 wpa_printf(MSG_INFO, "You may need to load kernel module to "
126 "register that algorithm.");
127 wpa_printf(MSG_INFO, "E.g., 'modprobe ieee80211_crypt_wep' for"
128 " WEP.");
129 break;
130 case IPW_CRYPT_ERR_UNKNOWN_ADDR:
131 wpa_printf(MSG_INFO, "Unknown address " MACSTR ".",
132 MAC2STR(param->sta_addr));
133 break;
134 case IPW_CRYPT_ERR_CRYPT_INIT_FAILED:
135 wpa_printf(MSG_INFO, "Crypt algorithm initialization failed.");
136 break;
137 case IPW_CRYPT_ERR_KEY_SET_FAILED:
138 wpa_printf(MSG_INFO, "Key setting failed.");
139 break;
140 case IPW_CRYPT_ERR_TX_KEY_SET_FAILED:
141 wpa_printf(MSG_INFO, "TX key index setting failed.");
142 break;
143 case IPW_CRYPT_ERR_CARD_CONF_FAILED:
144 wpa_printf(MSG_INFO, "Card configuration failed.");
145 break;
150 static int ipw_set_wpa_ie(struct wpa_driver_ipw_data *drv,
151 const u8 *wpa_ie, size_t wpa_ie_len)
153 struct ipw_param *param;
154 int ret;
155 size_t blen = sizeof(*param) + wpa_ie_len;
157 param = os_zalloc(blen);
158 if (param == NULL)
159 return -1;
161 param->cmd = IPW_CMD_SET_WPA_IE;
162 param->u.wpa_ie.len = wpa_ie_len;
163 os_memcpy(param->u.wpa_ie.data, wpa_ie, wpa_ie_len);
165 ret = ipw_ioctl(drv, param, blen, 1);
167 os_free(param);
168 return ret;
172 static int ipw_set_wpa_param(struct wpa_driver_ipw_data *drv, u8 name,
173 u32 value)
175 struct ipw_param param;
177 os_memset(&param, 0, sizeof(param));
178 param.cmd = IPW_CMD_SET_WPA_PARAM;
179 param.u.wpa_param.name = name;
180 param.u.wpa_param.value = value;
182 return ipw_ioctl(drv, &param, sizeof(param), 1);
186 static int ipw_mlme(struct wpa_driver_ipw_data *drv, const u8 *addr,
187 int cmd, int reason)
189 struct ipw_param param;
191 os_memset(&param, 0, sizeof(param));
192 os_memcpy(param.sta_addr, addr, ETH_ALEN);
193 param.cmd = IPW_CMD_MLME;
194 param.u.mlme.command = cmd;
195 param.u.mlme.reason_code = reason;
197 return ipw_ioctl(drv, &param, sizeof(param), 1);
201 static int wpa_driver_ipw_set_wpa(void *priv, int enabled)
203 struct wpa_driver_ipw_data *drv = priv;
204 int ret = 0;
206 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
208 if (!enabled && ipw_set_wpa_ie(drv, NULL, 0) < 0)
209 ret = -1;
211 if (ipw_set_wpa_param(drv, IPW_PARAM_WPA_ENABLED, enabled) < 0)
212 ret = -1;
214 return ret;
218 static int wpa_driver_ipw_set_key(const char *ifname, void *priv,
219 enum wpa_alg alg, const u8 *addr,
220 int key_idx, int set_tx,
221 const u8 *seq, size_t seq_len,
222 const u8 *key, size_t key_len)
224 struct wpa_driver_ipw_data *drv = priv;
225 struct ipw_param *param;
226 u8 *buf;
227 size_t blen;
228 int ret = 0;
229 char *alg_name;
231 switch (alg) {
232 case WPA_ALG_NONE:
233 alg_name = "none";
234 break;
235 case WPA_ALG_WEP:
236 alg_name = "WEP";
237 break;
238 case WPA_ALG_TKIP:
239 alg_name = "TKIP";
240 break;
241 case WPA_ALG_CCMP:
242 alg_name = "CCMP";
243 break;
244 default:
245 return -1;
248 wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
249 "key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
250 (unsigned long) seq_len, (unsigned long) key_len);
252 if (seq_len > 8)
253 return -2;
255 blen = sizeof(*param) + key_len;
256 buf = os_zalloc(blen);
257 if (buf == NULL)
258 return -1;
260 param = (struct ipw_param *) buf;
261 param->cmd = IPW_CMD_SET_ENCRYPTION;
262 os_memset(param->sta_addr, 0xff, ETH_ALEN);
263 os_strlcpy((char *) param->u.crypt.alg, alg_name,
264 IPW_CRYPT_ALG_NAME_LEN);
265 param->u.crypt.set_tx = set_tx ? 1 : 0;
266 param->u.crypt.idx = key_idx;
267 os_memcpy(param->u.crypt.seq, seq, seq_len);
268 param->u.crypt.key_len = key_len;
269 os_memcpy((u8 *) (param + 1), key, key_len);
271 if (ipw_ioctl(drv, param, blen, 1)) {
272 wpa_printf(MSG_WARNING, "Failed to set encryption.");
273 ipw_show_set_key_error(param);
274 ret = -1;
276 os_free(buf);
278 return ret;
282 static int wpa_driver_ipw_set_countermeasures(void *priv, int enabled)
284 struct wpa_driver_ipw_data *drv = priv;
285 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
286 return ipw_set_wpa_param(drv, IPW_PARAM_TKIP_COUNTERMEASURES,
287 enabled);
292 static int wpa_driver_ipw_set_drop_unencrypted(void *priv, int enabled)
294 struct wpa_driver_ipw_data *drv = priv;
295 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
296 return ipw_set_wpa_param(drv, IPW_PARAM_DROP_UNENCRYPTED,
297 enabled);
301 static int wpa_driver_ipw_deauthenticate(void *priv, const u8 *addr,
302 int reason_code)
304 struct wpa_driver_ipw_data *drv = priv;
305 return ipw_mlme(drv, addr, IPW_MLME_STA_DEAUTH, reason_code);
309 static int wpa_driver_ipw_disassociate(void *priv, const u8 *addr,
310 int reason_code)
312 struct wpa_driver_ipw_data *drv = priv;
313 return ipw_mlme(drv, addr, IPW_MLME_STA_DISASSOC, reason_code);
317 static int
318 wpa_driver_ipw_associate(void *priv, struct wpa_driver_associate_params *params)
320 struct wpa_driver_ipw_data *drv = priv;
321 int ret = 0;
322 int unencrypted_eapol;
324 if (wpa_driver_ipw_set_auth_alg(drv, params->auth_alg) < 0)
325 ret = -1;
326 if (wpa_driver_ipw_set_drop_unencrypted(drv, params->drop_unencrypted)
327 < 0)
328 ret = -1;
329 if (ipw_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
330 ret = -1;
331 if (wpa_driver_wext_set_ssid(drv->wext, params->ssid,
332 params->ssid_len) < 0)
333 ret = -1;
334 if (wpa_driver_wext_set_bssid(drv->wext, params->bssid) < 0)
335 ret = -1;
337 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
338 params->key_mgmt_suite == KEY_MGMT_PSK)
339 unencrypted_eapol = 0;
340 else
341 unencrypted_eapol = 1;
343 if (ipw_set_wpa_param(drv, IPW_PARAM_IEEE_802_1X,
344 unencrypted_eapol) < 0) {
345 wpa_printf(MSG_DEBUG, "ipw: Failed to configure "
346 "ieee_802_1x param");
349 return ret;
353 static int wpa_driver_ipw_set_auth_alg(void *priv, int auth_alg)
355 struct wpa_driver_ipw_data *drv = priv;
356 int algs = 0;
358 if (auth_alg & AUTH_ALG_OPEN_SYSTEM)
359 algs |= 1;
360 if (auth_alg & AUTH_ALG_SHARED_KEY)
361 algs |= 2;
362 if (auth_alg & AUTH_ALG_LEAP)
363 algs |= 4;
364 if (algs == 0)
365 algs = 1; /* at least one algorithm should be set */
367 wpa_printf(MSG_DEBUG, "%s: auth_alg=0x%x", __FUNCTION__, algs);
368 return ipw_set_wpa_param(drv, IPW_PARAM_AUTH_ALGS, algs);
372 static int wpa_driver_ipw_get_bssid(void *priv, u8 *bssid)
374 struct wpa_driver_ipw_data *drv = priv;
375 return wpa_driver_wext_get_bssid(drv->wext, bssid);
379 static int wpa_driver_ipw_get_ssid(void *priv, u8 *ssid)
381 struct wpa_driver_ipw_data *drv = priv;
382 return wpa_driver_wext_get_ssid(drv->wext, ssid);
386 static int wpa_driver_ipw_scan(void *priv,
387 struct wpa_driver_scan_params *params)
389 struct wpa_driver_ipw_data *drv = priv;
390 return wpa_driver_wext_scan(drv->wext, params);
394 static struct wpa_scan_results * wpa_driver_ipw_get_scan_results(void *priv)
396 struct wpa_driver_ipw_data *drv = priv;
397 return wpa_driver_wext_get_scan_results(drv->wext);
401 static int wpa_driver_ipw_set_operstate(void *priv, int state)
403 struct wpa_driver_ipw_data *drv = priv;
404 return wpa_driver_wext_set_operstate(drv->wext, state);
408 static void * wpa_driver_ipw_init(void *ctx, const char *ifname)
410 struct wpa_driver_ipw_data *drv;
411 int ver;
413 wpa_printf(MSG_DEBUG, "%s is called", __FUNCTION__);
414 drv = os_zalloc(sizeof(*drv));
415 if (drv == NULL)
416 return NULL;
417 drv->wext = wpa_driver_wext_init(ctx, ifname);
418 if (drv->wext == NULL) {
419 os_free(drv);
420 return NULL;
423 ver = wpa_driver_wext_get_version(drv->wext);
424 if (ver >= 18) {
425 wpa_printf(MSG_WARNING, "Linux wireless extensions version %d "
426 "detected.", ver);
427 wpa_printf(MSG_WARNING, "ipw2x00 driver uses driver_wext "
428 "(-Dwext) instead of driver_ipw.");
431 drv->ctx = ctx;
432 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
433 drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
434 if (drv->sock < 0) {
435 wpa_driver_wext_deinit(drv->wext);
436 os_free(drv);
437 return NULL;
440 wpa_driver_ipw_set_wpa(drv, 1);
442 return drv;
446 static void wpa_driver_ipw_deinit(void *priv)
448 struct wpa_driver_ipw_data *drv = priv;
449 wpa_driver_ipw_set_wpa(drv, 0);
450 wpa_driver_wext_deinit(drv->wext);
451 close(drv->sock);
452 os_free(drv);
456 const struct wpa_driver_ops wpa_driver_ipw_ops = {
457 .name = "ipw",
458 .desc = "Intel ipw2100/2200 driver (old; use wext with Linux 2.6.13 "
459 "or newer)",
460 .get_bssid = wpa_driver_ipw_get_bssid,
461 .get_ssid = wpa_driver_ipw_get_ssid,
462 .set_key = wpa_driver_ipw_set_key,
463 .set_countermeasures = wpa_driver_ipw_set_countermeasures,
464 .scan2 = wpa_driver_ipw_scan,
465 .get_scan_results2 = wpa_driver_ipw_get_scan_results,
466 .deauthenticate = wpa_driver_ipw_deauthenticate,
467 .disassociate = wpa_driver_ipw_disassociate,
468 .associate = wpa_driver_ipw_associate,
469 .init = wpa_driver_ipw_init,
470 .deinit = wpa_driver_ipw_deinit,
471 .set_operstate = wpa_driver_ipw_set_operstate,