00001
00017 #include "includes.h"
00018 #include <sys/ioctl.h>
00019
00020 #include "wireless_copy.h"
00021 #include "common.h"
00022 #include "driver.h"
00023 #include "driver_wext.h"
00024 #include "driver_hostap.h"
00025 #include "l2_packet.h"
00026 #include "wpa_supplicant.h"
00027
00028 struct wpa_driver_prism54_data {
00029 void *wext;
00030 void *ctx;
00031 char ifname[IFNAMSIZ + 1];
00032 int sock;
00033 };
00034
00035 #define PRISM54_SET_WPA SIOCIWFIRSTPRIV+12
00036 #define PRISM54_HOSTAPD SIOCIWFIRSTPRIV+25
00037 #define PRISM54_DROP_UNENCRYPTED SIOCIWFIRSTPRIV+26
00038
00039 static void show_set_key_error(struct prism2_hostapd_param *);
00040
00041 static int hostapd_ioctl_prism54(struct wpa_driver_prism54_data *drv,
00042 struct prism2_hostapd_param *param,
00043 int len, int show_err)
00044 {
00045 struct iwreq iwr;
00046
00047 memset(&iwr, 0, sizeof(iwr));
00048 strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
00049 iwr.u.data.pointer = (caddr_t) param;
00050 iwr.u.data.length = len;
00051
00052 if (ioctl(drv->sock, PRISM54_HOSTAPD, &iwr) < 0) {
00053 int ret = errno;
00054 if (show_err)
00055 perror("ioctl[PRISM54_HOSTAPD]");
00056 return ret;
00057 }
00058
00059 return 0;
00060 }
00061
00062
00063 static int wpa_driver_prism54_set_wpa_ie(struct wpa_driver_prism54_data *drv,
00064 const u8 *wpa_ie,
00065 size_t wpa_ie_len)
00066 {
00067 struct prism2_hostapd_param *param;
00068 int res;
00069 size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + wpa_ie_len;
00070 if (blen < sizeof(*param))
00071 blen = sizeof(*param);
00072
00073 param = (struct prism2_hostapd_param *) malloc(blen);
00074 if (param == NULL)
00075 return -1;
00076
00077 memset(param, 0, blen);
00078 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
00079 param->u.generic_elem.len = wpa_ie_len;
00080 memcpy(param->u.generic_elem.data, wpa_ie, wpa_ie_len);
00081 res = hostapd_ioctl_prism54(drv, param, blen, 1);
00082
00083 free(param);
00084
00085 return res;
00086 }
00087
00088
00089
00090 static int wpa_driver_prism54_set_wpa(void *priv, int enabled)
00091 {
00092 struct wpa_driver_prism54_data *drv = priv;
00093 struct prism2_hostapd_param *param;
00094 int res;
00095 size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN;
00096 if (blen < sizeof(*param))
00097 blen = sizeof(*param);
00098
00099 param = (struct prism2_hostapd_param *) malloc(blen);
00100 if (param == NULL)
00101 return -1;
00102
00103 memset(param, 0, blen);
00104 param->cmd = PRISM54_SET_WPA;
00105 param->u.generic_elem.len = 0;
00106 res = hostapd_ioctl_prism54(drv, param, blen, 1);
00107
00108 free(param);
00109
00110 return res;
00111 }
00112
00113
00114 static int wpa_driver_prism54_set_key(void *priv, wpa_alg alg,
00115 const u8 *addr, int key_idx, int set_tx,
00116 const u8 *seq, size_t seq_len,
00117 const u8 *key, size_t key_len)
00118 {
00119 struct wpa_driver_prism54_data *drv = priv;
00120 struct prism2_hostapd_param *param;
00121 u8 *buf;
00122 size_t blen;
00123 int ret = 0;
00124 char *alg_name;
00125
00126 switch (alg) {
00127 case WPA_ALG_NONE:
00128 alg_name = "none";
00129 return -1;
00130 break;
00131 case WPA_ALG_WEP:
00132 alg_name = "WEP";
00133 return -1;
00134 break;
00135 case WPA_ALG_TKIP:
00136 alg_name = "TKIP";
00137 break;
00138 case WPA_ALG_CCMP:
00139 alg_name = "CCMP";
00140 return -1;
00141 break;
00142 default:
00143 return -1;
00144 }
00145
00146 wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
00147 "key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
00148 (unsigned long) seq_len, (unsigned long) key_len);
00149
00150 if (seq_len > 8)
00151 return -2;
00152
00153 blen = sizeof(*param) + key_len;
00154 buf = malloc(blen);
00155 if (buf == NULL)
00156 return -1;
00157 memset(buf, 0, blen);
00158
00159 param = (struct prism2_hostapd_param *) buf;
00160 param->cmd = PRISM2_SET_ENCRYPTION;
00161
00162
00163
00164
00165
00166
00167
00168 #if 0
00169 if (addr == NULL)
00170 memset(param->sta_addr, 0xff, ETH_ALEN);
00171 else
00172 memcpy(param->sta_addr, addr, ETH_ALEN);
00173 #else
00174 memset(param->sta_addr, 0xff, ETH_ALEN);
00175 #endif
00176 strncpy((char *) param->u.crypt.alg, alg_name,
00177 HOSTAP_CRYPT_ALG_NAME_LEN);
00178 param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
00179 param->u.crypt.idx = key_idx;
00180 memcpy(param->u.crypt.seq, seq, seq_len);
00181 param->u.crypt.key_len = key_len;
00182 memcpy((u8 *) (param + 1), key, key_len);
00183
00184 if (hostapd_ioctl_prism54(drv, param, blen, 1)) {
00185 wpa_printf(MSG_WARNING, "Failed to set encryption.");
00186 show_set_key_error(param);
00187 ret = -1;
00188 }
00189 free(buf);
00190
00191 return ret;
00192 }
00193
00194
00195 static int wpa_driver_prism54_set_countermeasures(void *priv,
00196 int enabled)
00197 {
00198
00199 printf("wpa_driver_prism54_set_countermeasures - not yet "
00200 "implemented\n");
00201 return 0;
00202 }
00203
00204
00205 static int wpa_driver_prism54_set_drop_unencrypted(void *priv,
00206 int enabled)
00207 {
00208 struct wpa_driver_prism54_data *drv = priv;
00209 struct prism2_hostapd_param *param;
00210 int res;
00211 size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN;
00212 if (blen < sizeof(*param))
00213 blen = sizeof(*param);
00214
00215 param = (struct prism2_hostapd_param *) malloc(blen);
00216 if (param == NULL)
00217 return -1;
00218
00219 memset(param, 0, blen);
00220 param->cmd = PRISM54_DROP_UNENCRYPTED;
00221 param->u.generic_elem.len = 0;
00222 res = hostapd_ioctl_prism54(drv, param, blen, 1);
00223
00224 free(param);
00225
00226 return res;
00227 }
00228
00229
00230 static int wpa_driver_prism54_deauthenticate(void *priv, const u8 *addr,
00231 int reason_code)
00232 {
00233
00234 printf("wpa_driver_prism54_deauthenticate - not yet implemented\n");
00235 return 0;
00236 }
00237
00238
00239 static int wpa_driver_prism54_disassociate(void *priv, const u8 *addr,
00240 int reason_code)
00241 {
00242
00243 printf("wpa_driver_prism54_disassociate - not yet implemented\n");
00244 return 0;
00245 }
00246
00247
00248 static int
00249 wpa_driver_prism54_associate(void *priv,
00250 struct wpa_driver_associate_params *params)
00251 {
00252 struct wpa_driver_prism54_data *drv = priv;
00253 int ret = 0;
00254
00255 if (wpa_driver_prism54_set_wpa_ie(drv, params->wpa_ie,
00256 params->wpa_ie_len) < 0)
00257 ret = -1;
00258 if (wpa_driver_wext_set_freq(drv->wext, params->freq) < 0)
00259 ret = -1;
00260 if (wpa_driver_wext_set_ssid(drv->wext, params->ssid,
00261 params->ssid_len) < 0)
00262 ret = -1;
00263 if (wpa_driver_wext_set_bssid(drv->wext, params->bssid) < 0)
00264 ret = -1;
00265
00266 return ret;
00267 }
00268
00269 static void show_set_key_error(struct prism2_hostapd_param *param)
00270 {
00271 switch (param->u.crypt.err) {
00272 case HOSTAP_CRYPT_ERR_UNKNOWN_ALG:
00273 wpa_printf(MSG_INFO, "Unknown algorithm '%s'.",
00274 param->u.crypt.alg);
00275 wpa_printf(MSG_INFO, "You may need to load kernel module to "
00276 "register that algorithm.");
00277 wpa_printf(MSG_INFO, "E.g., 'modprobe hostap_crypt_wep' for "
00278 "WEP.");
00279 break;
00280 case HOSTAP_CRYPT_ERR_UNKNOWN_ADDR:
00281 wpa_printf(MSG_INFO, "Unknown address " MACSTR ".",
00282 MAC2STR(param->sta_addr));
00283 break;
00284 case HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED:
00285 wpa_printf(MSG_INFO, "Crypt algorithm initialization failed.");
00286 break;
00287 case HOSTAP_CRYPT_ERR_KEY_SET_FAILED:
00288 wpa_printf(MSG_INFO, "Key setting failed.");
00289 break;
00290 case HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED:
00291 wpa_printf(MSG_INFO, "TX key index setting failed.");
00292 break;
00293 case HOSTAP_CRYPT_ERR_CARD_CONF_FAILED:
00294 wpa_printf(MSG_INFO, "Card configuration failed.");
00295 break;
00296 }
00297 }
00298
00299
00300 static int wpa_driver_prism54_get_bssid(void *priv, u8 *bssid)
00301 {
00302 struct wpa_driver_prism54_data *drv = priv;
00303 return wpa_driver_wext_get_bssid(drv->wext, bssid);
00304 }
00305
00306
00307 static int wpa_driver_prism54_get_ssid(void *priv, u8 *ssid)
00308 {
00309 struct wpa_driver_prism54_data *drv = priv;
00310 return wpa_driver_wext_get_ssid(drv->wext, ssid);
00311 }
00312
00313
00314 static int wpa_driver_prism54_scan(void *priv, const u8 *ssid, size_t ssid_len)
00315 {
00316 struct wpa_driver_prism54_data *drv = priv;
00317 return wpa_driver_wext_scan(drv->wext, ssid, ssid_len);
00318 }
00319
00320
00321 static int wpa_driver_prism54_get_scan_results(void *priv,
00322 struct wpa_scan_result *results,
00323 size_t max_size)
00324 {
00325 struct wpa_driver_prism54_data *drv = priv;
00326 return wpa_driver_wext_get_scan_results(drv->wext, results, max_size);
00327 }
00328
00329
00330 static void * wpa_driver_prism54_init(void *ctx, const char *ifname)
00331 {
00332 struct wpa_driver_prism54_data *drv;
00333
00334 drv = malloc(sizeof(*drv));
00335 if (drv == NULL)
00336 return NULL;
00337 memset(drv, 0, sizeof(*drv));
00338 drv->wext = wpa_driver_wext_init(ctx, ifname);
00339 if (drv->wext == NULL) {
00340 free(drv);
00341 return NULL;
00342 }
00343
00344 drv->ctx = ctx;
00345 strncpy(drv->ifname, ifname, sizeof(drv->ifname));
00346 drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
00347 if (drv->sock < 0) {
00348 wpa_driver_wext_deinit(drv->wext);
00349 free(drv);
00350 return NULL;
00351 }
00352
00353 return drv;
00354 }
00355
00356
00357 static void wpa_driver_prism54_deinit(void *priv)
00358 {
00359 struct wpa_driver_prism54_data *drv = priv;
00360 wpa_driver_wext_deinit(drv->wext);
00361 close(drv->sock);
00362 free(drv);
00363 }
00364
00365
00366 const struct wpa_driver_ops wpa_driver_prism54_ops = {
00367 .name = "prism54",
00368 .desc = "Prism54.org driver (Intersil Prism GT/Duette/Indigo)",
00369 .get_bssid = wpa_driver_prism54_get_bssid,
00370 .get_ssid = wpa_driver_prism54_get_ssid,
00371 .set_wpa = wpa_driver_prism54_set_wpa,
00372 .set_key = wpa_driver_prism54_set_key,
00373 .set_countermeasures = wpa_driver_prism54_set_countermeasures,
00374 .set_drop_unencrypted = wpa_driver_prism54_set_drop_unencrypted,
00375 .scan = wpa_driver_prism54_scan,
00376 .get_scan_results = wpa_driver_prism54_get_scan_results,
00377 .deauthenticate = wpa_driver_prism54_deauthenticate,
00378 .disassociate = wpa_driver_prism54_disassociate,
00379 .associate = wpa_driver_prism54_associate,
00380 .init = wpa_driver_prism54_init,
00381 .deinit = wpa_driver_prism54_deinit,
00382 };
00383