aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/driver_atmel.c
blob: da233a017e670627eaece83cb7da1bb5dd726a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * WPA Supplicant - Driver interaction with Atmel Wireless LAN drivers
 * Copyright (c) 2000-2005, ATMEL Corporation
 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 */

/******************************************************************************
	Copyright 2000-2001 ATMEL Corporation.
	
    WPA Supplicant - driver interaction with Atmel Wireless lan drivers.
    
    This is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Atmel wireless lan drivers; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

******************************************************************************/

/*
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 */

#include "includes.h"
#include <sys/ioctl.h>

#include "wireless_copy.h"
#include "common.h"
#include "driver.h"
#include "driver_wext.h"
#include "wpa_supplicant.h"

struct wpa_driver_atmel_data {
	void *wext; /* private data for driver_wext */
	void *ctx;
	char ifname[IFNAMSIZ + 1];
	int sock;
};


#define ATMEL_WPA_IOCTL                (SIOCIWFIRSTPRIV + 2)
#define ATMEL_WPA_IOCTL_PARAM          (SIOCIWFIRSTPRIV + 3)
#define ATMEL_WPA_IOCTL_GET_PARAM      (SIOCIWFIRSTPRIV + 4)


/* ATMEL_WPA_IOCTL ioctl() cmd: */
enum {
    SET_WPA_ENCRYPTION  = 1,
    SET_CIPHER_SUITES   = 2,
    MLME_STA_DEAUTH     = 3,
    MLME_STA_DISASSOC   = 4
};

/* ATMEL_WPA_IOCTL_PARAM ioctl() cmd: */
enum {
            ATMEL_PARAM_WPA = 1,
            ATMEL_PARAM_PRIVACY_INVOKED = 2,
            ATMEL_PARAM_WPA_TYPE = 3
};

#define MAX_KEY_LENGTH      40

struct atmel_param{
    unsigned char sta_addr[6];
        int     cmd;
        u8      alg;
        u8      key_idx;
        u8      set_tx;
        u8      seq[8];
        u8      seq_len;
        u16     key_len;
        u8      key[MAX_KEY_LENGTH];
    struct{
        int     reason_code;
        u8      state;
    }mlme;
    u8          pairwise_suite;
    u8          group_suite;
    u8          key_mgmt_suite;
};

    
    
static int atmel_ioctl(struct wpa_driver_atmel_data *drv,
		       struct atmel_param *param,
		       int len, int show_err)
{
	struct iwreq iwr;

	memset(&iwr, 0, sizeof(iwr));
	strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
	iwr.u.data.pointer = (caddr_t) param;
	iwr.u.data.length = len;

	if (ioctl(drv->sock, ATMEL_WPA_IOCTL, &iwr) < 0) {
		int ret;
		ret = errno;
		if (show_err) 
			perror("ioctl[ATMEL_WPA_IOCTL]");
		return ret;
	}

	return 0;
}


static int atmel2param(struct wpa_driver_atmel_data *drv, int param, int value)
{
	struct iwreq iwr;
	int *i, ret = 0;

	memset(&iwr, 0, sizeof(iwr));
	strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
	i = (int *) iwr.u.name;
	*i++ = param;
	*i++ = value;

	if (ioctl(drv->sock, ATMEL_WPA_IOCTL_PARAM, &iwr) < 0) {
		perror("ioctl[ATMEL_WPA_IOCTL_PARAM]");
		ret = -1;
	}
	return ret;
}


#if 0
static int wpa_driver_atmel_set_wpa_ie(struct wpa_driver_atmel_data *drv,
				       const char *wpa_ie, size_t wpa_ie_len)
{
	struct atmel_param *param;
	int res;
	size_t blen = ATMEL_GENERIC_ELEMENT_HDR_LEN + wpa_ie_len;
	if (blen < sizeof(*param))
		blen = sizeof(*param);

	param = (struct atmel_param *) malloc(blen);
	if (param == NULL)
		return -1;

	memset(param, 0, blen);
	param->cmd = ATMEL_SET_GENERIC_ELEMENT;
	param->u.generic_elem.len = wpa_ie_len;
	memcpy(param->u.generic_elem.data, wpa_ie, wpa_ie_len);
	res = atmel_ioctl(drv, param, blen, 1);

	free(param);

	return res;
}
#endif


static int wpa_driver_atmel_set_wpa(void *priv, int enabled)
{
	struct wpa_driver_atmel_data *drv = priv;
        int ret = 0;
	
        printf("wpa_driver_atmel_set_wpa %s\n", drv->ifname);

	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);

#if 0
	if (!enabled && wpa_driver_atmel_set_wpa_ie(drv, NULL, 0) < 0)
		ret = -1;
#endif
	if (atmel2param(drv, ATMEL_PARAM_PRIVACY_INVOKED, enabled) < 0)
		ret = -1;
	if (atmel2param(drv, ATMEL_PARAM_WPA, enabled) < 0)
		ret = -1;

	return ret;
}


static int wpa_driver_atmel_set_key(void *priv, 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)
{
	struct wpa_driver_atmel_data *drv = priv;
	int ret = 0;
        struct atmel_param *param;
	u8 *buf;
        u8 alg_type;
        
	size_t blen;
	char *alg_name;

	switch (alg) {
	case WPA_ALG_NONE:
		alg_name = "none";
                alg_type = 0;
		break;
	case WPA_ALG_WEP:
		alg_name = "WEP";
		alg_type = 1;
                break;
	case WPA_ALG_TKIP:
		alg_name = "TKIP";
		alg_type = 2;
                break;
	case WPA_ALG_CCMP:
		alg_name = "CCMP";
		alg_type = 3;
                break;
	default:
		return -1;
	}

	wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
		   "key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
		   (unsigned long) seq_len, (unsigned long) key_len);

	if (seq_len > 8)
		return -2;

	blen = sizeof(*param) + key_len;
	buf = malloc(blen);
	if (buf == NULL)
		return -1;
	memset(buf, 0, blen);

	param = (struct atmel_param *) buf;
        
        param->cmd = SET_WPA_ENCRYPTION; 
        
        if (addr == NULL)
		memset(param->sta_addr, 0xff, ETH_ALEN);
	else
		memcpy(param->sta_addr, addr, ETH_ALEN);
        
        param->alg = alg_type;
        param->key_idx = key_idx;
        param->set_tx = set_tx;
        memcpy(param->seq, seq, seq_len);
        param->seq_len = seq_len;
        param->key_len = key_len;
	memcpy((u8 *)param->key, key, key_len);
	
        if (atmel_ioctl(drv, param, blen, 1)) {
		wpa_printf(MSG_WARNING, "Failed to set encryption.");
		/* TODO: show key error*/
		ret = -1;
	}
	free(buf);

	return ret;
}


static int wpa_driver_atmel_set_countermeasures(void *priv,
						 int enabled)
{
	/* FIX */
	printf("wpa_driver_atmel_set_countermeasures - not yet "
	       "implemented\n");
	return 0;
}


static int wpa_driver_atmel_set_drop_unencrypted(void *priv,
						  int enabled)
{
	/* FIX */
	printf("wpa_driver_atmel_set_drop_unencrypted - not yet "
	       "implemented\n");
	return 0;
}


static int wpa_driver_atmel_mlme(void *priv, const u8 *addr, int cmd,
				 int reason_code)
{
	struct wpa_driver_atmel_data *drv = priv;
	struct atmel_param param;
	int ret;
        int mgmt_error = 0xaa;
        
	memset(&param, 0, sizeof(param));
	memcpy(param.sta_addr, addr, ETH_ALEN);
	param.cmd = cmd;
	param.mlme.reason_code = reason_code;
        param.mlme.state = mgmt_error;
	ret = atmel_ioctl(drv, &param, sizeof(param), 1);
	return ret;
}


#if 0
static int wpa_driver_atmel_set_suites(struct wpa_driver_atmel_data *drv,
				       u8 pairwise_suite, u8 group_suite,
				       u8 key_mgmt_suite)
{
	struct atmel_param param;
	int ret;
        
	memset(&param, 0, sizeof(param));
        param.cmd = SET_CIPHER_SUITES;
        param.pairwise_suite = pairwise_suite;
        param.group_suite = group_suite;
        param.key_mgmt_suite = key_mgmt_suite;
	        
	ret = atmel_ioctl(drv, &param, sizeof(param), 1);
	return ret;
}
#endif


static int wpa_driver_atmel_deauthenticate(void *priv, const u8 *addr,
					   int reason_code)
{
	struct wpa_driver_atmel_data *drv = priv;
	printf("wpa_driver_atmel_deauthenticate\n");
        wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
	return wpa_driver_atmel_mlme(drv, addr, MLME_STA_DEAUTH,
				     reason_code);

}


static int wpa_driver_atmel_disassociate(void *priv, const u8 *addr,
					 int reason_code)
{
	struct wpa_driver_atmel_data *drv = priv;
	printf("wpa_driver_atmel_disassociate\n");
	wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
	return wpa_driver_atmel_mlme(drv, addr, MLME_STA_DISASSOC,
				     reason_code);

}


/* Atmel driver uses specific values for each cipher suite */
static int convertSuiteToDriver(wpa_cipher suite)
{
    u8 suite_type;
    
    switch(suite) {
        case CIPHER_NONE:
                suite_type =  0;
                break;
        case CIPHER_WEP40:
                suite_type =  1;
                break;
        case CIPHER_TKIP:
                suite_type = 2;
                break;
        case CIPHER_WEP104:
                suite_type = 5;
                break;
        case CIPHER_CCMP:
                suite_type = 3;
                break;
        default:
                suite_type = 2;
    }
    
    return suite_type;

}
    
static int
wpa_driver_atmel_associate(void *priv,
			   struct wpa_driver_associate_params *params)
{
	struct wpa_driver_atmel_data *drv = priv;
	int ret = 0;
        u8 pairwise_suite_driver;
        u8 group_suite_driver;
        u8 key_mgmt_suite_driver;


        pairwise_suite_driver = convertSuiteToDriver(params->pairwise_suite);
        group_suite_driver    = convertSuiteToDriver(params->group_suite);
        key_mgmt_suite_driver = convertSuiteToDriver(params->key_mgmt_suite);
        
#if 0
        if (wpa_driver_atmel_set_suites(drv, pairwise_suite_driver,
					group_suite_driver,
					key_mgmt_suite_driver) < 0){
		printf("wpa_driver_atmel_set_suites.\n");
                ret = -1;
        }
        if (wpa_driver_wext_set_freq(drv->wext, params->freq) < 0) {
	        printf("wpa_driver_atmel_set_freq.\n");
		ret = -1;
        }
#endif
	if (wpa_driver_wext_set_ssid(drv->wext, params->ssid, params->ssid_len)
	    < 0) {
	        printf("FAILED : wpa_driver_atmel_set_ssid.\n");
		ret = -1;
        }
	if (wpa_driver_wext_set_bssid(drv->wext, params->bssid) < 0) {
	        printf("FAILED : wpa_driver_atmel_set_bssid.\n");
		ret = -1;
        }

	return ret;
}


static int wpa_driver_atmel_get_bssid(void *priv, u8 *bssid)
{
	struct wpa_driver_atmel_data *drv = priv;
	return wpa_driver_wext_get_bssid(drv->wext, bssid);
}


static int wpa_driver_atmel_get_ssid(void *priv, u8 *ssid)
{
	struct wpa_driver_atmel_data *drv = priv;
	return wpa_driver_wext_get_ssid(drv->wext, ssid);
}


static int wpa_driver_atmel_scan(void *priv, const u8 *ssid, size_t ssid_len)
{
	struct wpa_driver_atmel_data *drv = priv;
	return wpa_driver_wext_scan(drv->wext, ssid, ssid_len);
}


static int wpa_driver_atmel_get_scan_results(void *priv,
					    struct wpa_scan_result *results,
					    size_t max_size)
{
	struct wpa_driver_atmel_data *drv = priv;
	return wpa_driver_wext_get_scan_results(drv->wext, results, max_size);
}


static void * wpa_driver_atmel_init(void *ctx, const char *ifname)
{
	struct wpa_driver_atmel_data *drv;

	drv = malloc(sizeof(*drv));
	if (drv == NULL)
		return NULL;
	memset(drv, 0, sizeof(*drv));
	drv->wext = wpa_driver_wext_init(ctx, ifname);
	if (drv->wext == NULL) {
		free(drv);
		return NULL;
	}

	drv->ctx = ctx;
	strncpy(drv->ifname, ifname, sizeof(drv->ifname));
	drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
	if (drv->sock < 0) {
		wpa_driver_wext_deinit(drv->wext);
		free(drv);
		return NULL;
	}

	return drv;
}


static void wpa_driver_atmel_deinit(void *priv)
{
	struct wpa_driver_atmel_data *drv = priv;
	wpa_driver_wext_deinit(drv->wext);
	close(drv->sock);
	free(drv);
}


const struct wpa_driver_ops wpa_driver_atmel_ops = {
	.name = "atmel",
	.desc = "ATMEL AT76C5XXx (USB, PCMCIA)",
	.get_bssid = wpa_driver_atmel_get_bssid,
	.get_ssid = wpa_driver_atmel_get_ssid,
	.set_wpa = wpa_driver_atmel_set_wpa,
	.set_key = wpa_driver_atmel_set_key,
	.init = wpa_driver_atmel_init,
	.deinit = wpa_driver_atmel_deinit,
	.set_countermeasures = wpa_driver_atmel_set_countermeasures,
	.set_drop_unencrypted = wpa_driver_atmel_set_drop_unencrypted,
	.scan = wpa_driver_atmel_scan,
	.get_scan_results = wpa_driver_atmel_get_scan_results,
	.deauthenticate = wpa_driver_atmel_deauthenticate,
	.disassociate = wpa_driver_atmel_disassociate,
	.associate = wpa_driver_atmel_associate,
};