driver_ipw.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant - driver interaction with Linux ipw2100/2200 drivers
00003  * Copyright (c) 2005 Zhu Yi <yi.zhu@intel.com>
00004  * Copyright (c) 2004 Lubomir Gelo <lgelo@cnc.sk>
00005  * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License version 2 as
00009  * published by the Free Software Foundation.
00010  *
00011  * Alternatively, this software may be distributed under the terms of BSD
00012  * license.
00013  *
00014  * See README and COPYING for more details.
00015  *
00016  * Please note that ipw2100/2200 drivers change to use generic Linux wireless
00017  * extensions if the kernel includes support for WE-18 or newer (Linux 2.6.13
00018  * or newer). driver_wext.c should be used in those cases.
00019  */
00020 
00021 #include "includes.h"
00022 #include <sys/ioctl.h>
00023 
00024 #include "wireless_copy.h"
00025 #include "common.h"
00026 #include "driver.h"
00027 #include "driver_wext.h"
00028 
00029 struct wpa_driver_ipw_data {
00030         void *wext; /* private data for driver_wext */
00031         void *ctx;
00032         char ifname[IFNAMSIZ + 1];
00033         int sock;
00034 };
00035 
00036 /* following definitions must be kept in sync with ipw2100.c and ipw2200.c */
00037 
00038 #define IPW_IOCTL_WPA_SUPPLICANT                SIOCIWFIRSTPRIV+30
00039 
00040 #define IPW_CMD_SET_WPA_PARAM                   1
00041 #define IPW_CMD_SET_WPA_IE                      2
00042 #define IPW_CMD_SET_ENCRYPTION                  3
00043 #define IPW_CMD_MLME                            4
00044 
00045 #define IPW_PARAM_WPA_ENABLED                   1
00046 #define IPW_PARAM_TKIP_COUNTERMEASURES          2
00047 #define IPW_PARAM_DROP_UNENCRYPTED              3
00048 #define IPW_PARAM_PRIVACY_INVOKED               4
00049 #define IPW_PARAM_AUTH_ALGS                     5
00050 #define IPW_PARAM_IEEE_802_1X                   6
00051 
00052 #define IPW_MLME_STA_DEAUTH                     1
00053 #define IPW_MLME_STA_DISASSOC                   2
00054 
00055 #define IPW_CRYPT_ERR_UNKNOWN_ALG               2
00056 #define IPW_CRYPT_ERR_UNKNOWN_ADDR              3
00057 #define IPW_CRYPT_ERR_CRYPT_INIT_FAILED         4
00058 #define IPW_CRYPT_ERR_KEY_SET_FAILED            5
00059 #define IPW_CRYPT_ERR_TX_KEY_SET_FAILED         6
00060 #define IPW_CRYPT_ERR_CARD_CONF_FAILED          7
00061 
00062 #define IPW_CRYPT_ALG_NAME_LEN                  16
00063 
00064 struct ipw_param {
00065         u32 cmd;
00066         u8 sta_addr[ETH_ALEN];
00067         union {
00068                 struct {
00069                         u8 name;
00070                         u32 value;
00071                 } wpa_param;
00072                 struct {
00073                         u32 len;
00074                         u8 reserved[32];
00075                         u8 data[0];
00076                 } wpa_ie;
00077                 struct{
00078                         u32 command;
00079                         u32 reason_code;
00080                 } mlme;
00081                 struct {
00082                         u8 alg[IPW_CRYPT_ALG_NAME_LEN];
00083                         u8 set_tx;
00084                         u32 err;
00085                         u8 idx;
00086                         u8 seq[8];
00087                         u16 key_len;
00088                         u8 key[0];
00089                 } crypt;
00090 
00091         } u;
00092 };
00093 
00094 /* end of ipw2100.c and ipw2200.c code */
00095 
00096 static int wpa_driver_ipw_set_auth_alg(void *priv, int auth_alg);
00097 
00098 static int ipw_ioctl(struct wpa_driver_ipw_data *drv,
00099                      struct ipw_param *param, int len, int show_err)
00100 {
00101         struct iwreq iwr;
00102 
00103         os_memset(&iwr, 0, sizeof(iwr));
00104         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
00105         iwr.u.data.pointer = (caddr_t) param;
00106         iwr.u.data.length = len;
00107 
00108         if (ioctl(drv->sock, IPW_IOCTL_WPA_SUPPLICANT, &iwr) < 0) {
00109                 int ret = errno;
00110                 if (show_err) 
00111                         perror("ioctl[IPW_IOCTL_WPA_SUPPLICANT]");
00112                 return ret;
00113         }
00114 
00115         return 0;
00116 }
00117 
00118 
00119 static void ipw_show_set_key_error(struct ipw_param *param)
00120 {
00121         switch (param->u.crypt.err) {
00122         case IPW_CRYPT_ERR_UNKNOWN_ALG:
00123                 wpa_printf(MSG_INFO, "Unknown algorithm '%s'.",
00124                            param->u.crypt.alg);
00125                 wpa_printf(MSG_INFO, "You may need to load kernel module to "
00126                            "register that algorithm.");
00127                 wpa_printf(MSG_INFO, "E.g., 'modprobe ieee80211_crypt_wep' for"
00128                            " WEP.");
00129                 break;
00130         case IPW_CRYPT_ERR_UNKNOWN_ADDR:
00131                 wpa_printf(MSG_INFO, "Unknown address " MACSTR ".",
00132                            MAC2STR(param->sta_addr));
00133                 break;
00134         case IPW_CRYPT_ERR_CRYPT_INIT_FAILED:
00135                 wpa_printf(MSG_INFO, "Crypt algorithm initialization failed.");
00136                 break;
00137         case IPW_CRYPT_ERR_KEY_SET_FAILED:
00138                 wpa_printf(MSG_INFO, "Key setting failed.");
00139                 break;
00140         case IPW_CRYPT_ERR_TX_KEY_SET_FAILED:
00141                 wpa_printf(MSG_INFO, "TX key index setting failed.");
00142                 break;
00143         case IPW_CRYPT_ERR_CARD_CONF_FAILED:
00144                 wpa_printf(MSG_INFO, "Card configuration failed.");
00145                 break;
00146         }
00147 }
00148 
00149 
00150 static int ipw_set_wpa_ie(struct wpa_driver_ipw_data *drv,
00151                           const u8 *wpa_ie, size_t wpa_ie_len)
00152 {
00153         struct ipw_param *param;
00154         int ret;
00155         size_t blen = sizeof(*param) + wpa_ie_len;
00156 
00157         param = os_zalloc(blen);
00158         if (param == NULL)
00159                 return -1;
00160 
00161         param->cmd = IPW_CMD_SET_WPA_IE;
00162         param->u.wpa_ie.len = wpa_ie_len;
00163         os_memcpy(param->u.wpa_ie.data, wpa_ie, wpa_ie_len);
00164         
00165         ret = ipw_ioctl(drv, param, blen, 1);
00166 
00167         os_free(param);
00168         return ret;
00169 }
00170 
00171 
00172 static int ipw_set_wpa_param(struct wpa_driver_ipw_data *drv, u8 name,
00173                              u32 value)
00174 {
00175         struct ipw_param param;
00176 
00177         os_memset(&param, 0, sizeof(param));
00178         param.cmd = IPW_CMD_SET_WPA_PARAM;
00179         param.u.wpa_param.name = name;
00180         param.u.wpa_param.value = value;
00181 
00182         return ipw_ioctl(drv, &param, sizeof(param), 1);
00183 }
00184 
00185 
00186 static int ipw_mlme(struct wpa_driver_ipw_data *drv, const u8 *addr,
00187                     int cmd, int reason)
00188 {
00189         struct ipw_param param;
00190 
00191         os_memset(&param, 0, sizeof(param));
00192         os_memcpy(param.sta_addr, addr, ETH_ALEN);      
00193         param.cmd = IPW_CMD_MLME;
00194         param.u.mlme.command = cmd;
00195         param.u.mlme.reason_code = reason;
00196 
00197         return ipw_ioctl(drv, &param, sizeof(param), 1);
00198 }
00199 
00200 
00201 static int wpa_driver_ipw_set_wpa(void *priv, int enabled)
00202 {
00203         struct wpa_driver_ipw_data *drv = priv;
00204         int ret = 0;
00205 
00206         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
00207 
00208         if (!enabled && ipw_set_wpa_ie(drv, NULL, 0) < 0)
00209                 ret = -1;
00210 
00211         if (ipw_set_wpa_param(drv, IPW_PARAM_WPA_ENABLED, enabled) < 0)
00212                 ret = -1;
00213 
00214         return ret;
00215 }
00216 
00217 
00218 static int wpa_driver_ipw_set_key(const char *ifname, void *priv,
00219                                   enum wpa_alg alg, const u8 *addr,
00220                                   int key_idx, int set_tx,
00221                                   const u8 *seq, size_t seq_len,
00222                                   const u8 *key, size_t key_len)
00223 {
00224         struct wpa_driver_ipw_data *drv = priv;
00225         struct ipw_param *param;
00226         u8 *buf;
00227         size_t blen;
00228         int ret = 0;
00229         char *alg_name;
00230 
00231         switch (alg) {
00232         case WPA_ALG_NONE:
00233                 alg_name = "none";
00234                 break;
00235         case WPA_ALG_WEP:
00236                 alg_name = "WEP";
00237                 break;
00238         case WPA_ALG_TKIP:
00239                 alg_name = "TKIP";
00240                 break;
00241         case WPA_ALG_CCMP:
00242                 alg_name = "CCMP";
00243                 break;
00244         default:
00245                 return -1;
00246         }
00247 
00248         wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
00249                    "key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
00250                    (unsigned long) seq_len, (unsigned long) key_len);
00251 
00252         if (seq_len > 8)
00253                 return -2;
00254 
00255         blen = sizeof(*param) + key_len;
00256         buf = os_zalloc(blen);
00257         if (buf == NULL)
00258                 return -1;
00259 
00260         param = (struct ipw_param *) buf;
00261         param->cmd = IPW_CMD_SET_ENCRYPTION;
00262         os_memset(param->sta_addr, 0xff, ETH_ALEN);
00263         os_strlcpy((char *) param->u.crypt.alg, alg_name,
00264                    IPW_CRYPT_ALG_NAME_LEN);
00265         param->u.crypt.set_tx = set_tx ? 1 : 0;
00266         param->u.crypt.idx = key_idx;
00267         os_memcpy(param->u.crypt.seq, seq, seq_len);
00268         param->u.crypt.key_len = key_len;
00269         os_memcpy((u8 *) (param + 1), key, key_len);
00270 
00271         if (ipw_ioctl(drv, param, blen, 1)) {
00272                 wpa_printf(MSG_WARNING, "Failed to set encryption.");
00273                 ipw_show_set_key_error(param);
00274                 ret = -1;
00275         }
00276         os_free(buf);
00277 
00278         return ret;
00279 }
00280 
00281 
00282 static int wpa_driver_ipw_set_countermeasures(void *priv, int enabled)
00283 {
00284         struct wpa_driver_ipw_data *drv = priv;
00285         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
00286         return ipw_set_wpa_param(drv, IPW_PARAM_TKIP_COUNTERMEASURES,
00287                                      enabled);
00288 
00289 }
00290 
00291 
00292 static int wpa_driver_ipw_set_drop_unencrypted(void *priv, int enabled)
00293 {
00294         struct wpa_driver_ipw_data *drv = priv;
00295         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
00296         return ipw_set_wpa_param(drv, IPW_PARAM_DROP_UNENCRYPTED,
00297                                      enabled);
00298 }
00299 
00300 
00301 static int wpa_driver_ipw_deauthenticate(void *priv, const u8 *addr,
00302                                          int reason_code)
00303 {
00304         struct wpa_driver_ipw_data *drv = priv;
00305         return ipw_mlme(drv, addr, IPW_MLME_STA_DEAUTH, reason_code);
00306 }
00307 
00308 
00309 static int wpa_driver_ipw_disassociate(void *priv, const u8 *addr,
00310                                        int reason_code)
00311 {
00312         struct wpa_driver_ipw_data *drv = priv;
00313         return ipw_mlme(drv, addr, IPW_MLME_STA_DISASSOC, reason_code);
00314 }
00315 
00316 
00317 static int
00318 wpa_driver_ipw_associate(void *priv, struct wpa_driver_associate_params *params)
00319 {
00320         struct wpa_driver_ipw_data *drv = priv;
00321         int ret = 0;
00322         int unencrypted_eapol;
00323 
00324         if (wpa_driver_ipw_set_auth_alg(drv, params->auth_alg) < 0)
00325                 ret = -1;
00326         if (wpa_driver_ipw_set_drop_unencrypted(drv, params->drop_unencrypted)
00327             < 0)
00328                 ret = -1;
00329         if (ipw_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
00330                 ret = -1;
00331         if (wpa_driver_wext_set_ssid(drv->wext, params->ssid,
00332                                      params->ssid_len) < 0)
00333                 ret = -1;
00334         if (wpa_driver_wext_set_bssid(drv->wext, params->bssid) < 0)
00335                 ret = -1;
00336 
00337         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
00338             params->key_mgmt_suite == KEY_MGMT_PSK)
00339                 unencrypted_eapol = 0;
00340         else
00341                 unencrypted_eapol = 1;
00342         
00343         if (ipw_set_wpa_param(drv, IPW_PARAM_IEEE_802_1X,
00344                               unencrypted_eapol) < 0) {
00345                 wpa_printf(MSG_DEBUG, "ipw: Failed to configure "
00346                            "ieee_802_1x param");
00347         }
00348 
00349         return ret;
00350 }
00351 
00352 
00353 static int wpa_driver_ipw_set_auth_alg(void *priv, int auth_alg)
00354 {
00355         struct wpa_driver_ipw_data *drv = priv;
00356         int algs = 0;
00357 
00358         if (auth_alg & WPA_AUTH_ALG_OPEN)
00359                 algs |= 1;
00360         if (auth_alg & WPA_AUTH_ALG_SHARED)
00361                 algs |= 2;
00362         if (auth_alg & WPA_AUTH_ALG_LEAP)
00363                 algs |= 4;
00364         if (algs == 0)
00365                 algs = 1; /* at least one algorithm should be set */
00366 
00367         wpa_printf(MSG_DEBUG, "%s: auth_alg=0x%x", __FUNCTION__, algs);
00368         return ipw_set_wpa_param(drv, IPW_PARAM_AUTH_ALGS, algs);
00369 }
00370 
00371 
00372 static int wpa_driver_ipw_get_bssid(void *priv, u8 *bssid)
00373 {
00374         struct wpa_driver_ipw_data *drv = priv;
00375         return wpa_driver_wext_get_bssid(drv->wext, bssid);
00376 }
00377 
00378 
00379 static int wpa_driver_ipw_get_ssid(void *priv, u8 *ssid)
00380 {
00381         struct wpa_driver_ipw_data *drv = priv;
00382         return wpa_driver_wext_get_ssid(drv->wext, ssid);
00383 }
00384 
00385 
00386 static int wpa_driver_ipw_scan(void *priv,
00387                                struct wpa_driver_scan_params *params)
00388 {
00389         struct wpa_driver_ipw_data *drv = priv;
00390         return wpa_driver_wext_scan(drv->wext, params);
00391 }
00392 
00393 
00394 static struct wpa_scan_results * wpa_driver_ipw_get_scan_results(void *priv)
00395 {
00396         struct wpa_driver_ipw_data *drv = priv;
00397         return wpa_driver_wext_get_scan_results(drv->wext);
00398 }
00399 
00400 
00401 static int wpa_driver_ipw_set_operstate(void *priv, int state)
00402 {
00403         struct wpa_driver_ipw_data *drv = priv;
00404         return wpa_driver_wext_set_operstate(drv->wext, state);
00405 }
00406 
00407 
00408 static void * wpa_driver_ipw_init(void *ctx, const char *ifname)
00409 {
00410         struct wpa_driver_ipw_data *drv;
00411         int ver;
00412 
00413         wpa_printf(MSG_DEBUG, "%s is called", __FUNCTION__);
00414         drv = os_zalloc(sizeof(*drv));
00415         if (drv == NULL)
00416                 return NULL;
00417         drv->wext = wpa_driver_wext_init(ctx, ifname);
00418         if (drv->wext == NULL) {
00419                 os_free(drv);
00420                 return NULL;
00421         }
00422 
00423         ver = wpa_driver_wext_get_version(drv->wext);
00424         if (ver >= 18) {
00425                 wpa_printf(MSG_WARNING, "Linux wireless extensions version %d "
00426                            "detected.", ver);
00427                 wpa_printf(MSG_WARNING, "ipw2x00 driver uses driver_wext "
00428                            "(-Dwext) instead of driver_ipw.");
00429         }
00430 
00431         drv->ctx = ctx;
00432         os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
00433         drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
00434         if (drv->sock < 0) {
00435                 wpa_driver_wext_deinit(drv->wext);
00436                 os_free(drv);
00437                 return NULL;
00438         }
00439 
00440         wpa_driver_ipw_set_wpa(drv, 1);
00441 
00442         return drv;
00443 }
00444 
00445 
00446 static void wpa_driver_ipw_deinit(void *priv)
00447 {
00448         struct wpa_driver_ipw_data *drv = priv;
00449         wpa_driver_ipw_set_wpa(drv, 0);
00450         wpa_driver_wext_deinit(drv->wext);
00451         close(drv->sock);
00452         os_free(drv);
00453 }
00454 
00455 
00456 const struct wpa_driver_ops wpa_driver_ipw_ops = {
00457         .name = "ipw",
00458         .desc = "Intel ipw2100/2200 driver (old; use wext with Linux 2.6.13 "
00459         "or newer)",
00460         .get_bssid = wpa_driver_ipw_get_bssid,
00461         .get_ssid = wpa_driver_ipw_get_ssid,
00462         .set_key = wpa_driver_ipw_set_key,
00463         .set_countermeasures = wpa_driver_ipw_set_countermeasures,
00464         .scan2 = wpa_driver_ipw_scan,
00465         .get_scan_results2 = wpa_driver_ipw_get_scan_results,
00466         .deauthenticate = wpa_driver_ipw_deauthenticate,
00467         .disassociate = wpa_driver_ipw_disassociate,
00468         .associate = wpa_driver_ipw_associate,
00469         .init = wpa_driver_ipw_init,
00470         .deinit = wpa_driver_ipw_deinit,
00471         .set_operstate = wpa_driver_ipw_set_operstate,
00472 };


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:36