~ubuntu-branches/ubuntu/natty/hostapd/natty-updates

« back to all changes in this revision

Viewing changes to driver_bsd.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2009-04-03 07:07:06 UTC
  • mfrom: (1.1.11 upstream) (4.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090403070706-g8oc44qjhbixs4uf
Tags: 1:0.6.9-2
Enable CONFIG_IEEE80211W, IEEE 802.11w (management frame
protection). (Closes: #522328)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * hostapd / Driver interaction with BSD net80211 layer
3
 
 * Copyright (c) 2004, Sam Leffler <sam@errno.com>
4
 
 * Copyright (c) 2004, 2Wire, Inc
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License version 2 as
8
 
 * published by the Free Software Foundation.
9
 
 *
10
 
 * Alternatively, this software may be distributed under the terms of BSD
11
 
 * license.
12
 
 *
13
 
 * See README and COPYING for more details.
14
 
 */
15
 
 
16
 
#include "includes.h"
17
 
#include <sys/ioctl.h>
18
 
 
19
 
#include <net/if.h>
20
 
 
21
 
#include <net80211/ieee80211.h>
22
 
#include <net80211/ieee80211_crypto.h>
23
 
#include <net80211/ieee80211_ioctl.h>
24
 
 
25
 
#include "hostapd.h"
26
 
#include "driver.h"
27
 
#include "ieee802_1x.h"
28
 
#include "eloop.h"
29
 
#include "sta_info.h"
30
 
#include "l2_packet.h"
31
 
 
32
 
#include "eapol_sm.h"
33
 
#include "wpa.h"
34
 
#include "radius.h"
35
 
#include "ieee802_11.h"
36
 
#include "common.h"
37
 
 
38
 
struct bsd_driver_data {
39
 
        struct driver_ops ops;                  /* base class */
40
 
        struct hostapd_data *hapd;              /* back pointer */
41
 
 
42
 
        char    iface[IFNAMSIZ + 1];
43
 
        struct l2_packet_data *sock_xmit;       /* raw packet xmit socket */
44
 
        int     ioctl_sock;                     /* socket for ioctl() use */
45
 
        int     wext_sock;                      /* socket for wireless events */
46
 
};
47
 
 
48
 
static const struct driver_ops bsd_driver_ops;
49
 
 
50
 
static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
51
 
 
52
 
static int
53
 
set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
54
 
{
55
 
        struct ieee80211req ireq;
56
 
 
57
 
        memset(&ireq, 0, sizeof(ireq));
58
 
        strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
59
 
        ireq.i_type = op;
60
 
        ireq.i_len = arg_len;
61
 
        ireq.i_data = (void *) arg;
62
 
 
63
 
        if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
64
 
                perror("ioctl[SIOCS80211]");
65
 
                return -1;
66
 
        }
67
 
        return 0;
68
 
}
69
 
 
70
 
static int
71
 
get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
72
 
{
73
 
        struct ieee80211req ireq;
74
 
 
75
 
        memset(&ireq, 0, sizeof(ireq));
76
 
        strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
77
 
        ireq.i_type = op;
78
 
        ireq.i_len = arg_len;
79
 
        ireq.i_data = arg;
80
 
 
81
 
        if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
82
 
                perror("ioctl[SIOCG80211]");
83
 
                return -1;
84
 
        }
85
 
        return ireq.i_len;
86
 
}
87
 
 
88
 
static int
89
 
set80211param(struct bsd_driver_data *drv, int op, int arg)
90
 
{
91
 
        struct ieee80211req ireq;
92
 
 
93
 
        memset(&ireq, 0, sizeof(ireq));
94
 
        strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
95
 
        ireq.i_type = op;
96
 
        ireq.i_val = arg;
97
 
 
98
 
        if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
99
 
                perror("ioctl[SIOCS80211]");
100
 
                return -1;
101
 
        }
102
 
        return 0;
103
 
}
104
 
 
105
 
static const char *
106
 
ether_sprintf(const u8 *addr)
107
 
{
108
 
        static char buf[sizeof(MACSTR)];
109
 
 
110
 
        if (addr != NULL)
111
 
                snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
112
 
        else
113
 
                snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
114
 
        return buf;
115
 
}
116
 
 
117
 
/*
118
 
 * Configure WPA parameters.
119
 
 */
120
 
static int
121
 
bsd_configure_wpa(struct bsd_driver_data *drv)
122
 
{
123
 
        static const char *ciphernames[] =
124
 
                { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
125
 
        struct hostapd_data *hapd = drv->hapd;
126
 
        struct hostapd_bss_config *conf = hapd->conf;
127
 
        int v;
128
 
 
129
 
        switch (conf->wpa_group) {
130
 
        case WPA_CIPHER_CCMP:
131
 
                v = IEEE80211_CIPHER_AES_CCM;
132
 
                break;
133
 
        case WPA_CIPHER_TKIP:
134
 
                v = IEEE80211_CIPHER_TKIP;
135
 
                break;
136
 
        case WPA_CIPHER_WEP104:
137
 
                v = IEEE80211_CIPHER_WEP;
138
 
                break;
139
 
        case WPA_CIPHER_WEP40:
140
 
                v = IEEE80211_CIPHER_WEP;
141
 
                break;
142
 
        case WPA_CIPHER_NONE:
143
 
                v = IEEE80211_CIPHER_NONE;
144
 
                break;
145
 
        default:
146
 
                printf("Unknown group key cipher %u\n",
147
 
                        conf->wpa_group);
148
 
                return -1;
149
 
        }
150
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
151
 
                "%s: group key cipher=%s (%u)\n", __func__, ciphernames[v], v);
152
 
        if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
153
 
                printf("Unable to set group key cipher to %u (%s)\n",
154
 
                        v, ciphernames[v]);
155
 
                return -1;
156
 
        }
157
 
        if (v == IEEE80211_CIPHER_WEP) {
158
 
                /* key length is done only for specific ciphers */
159
 
                v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
160
 
                if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
161
 
                        printf("Unable to set group key length to %u\n", v);
162
 
                        return -1;
163
 
                }
164
 
        }
165
 
 
166
 
        v = 0;
167
 
        if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
168
 
                v |= 1<<IEEE80211_CIPHER_AES_CCM;
169
 
        if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
170
 
                v |= 1<<IEEE80211_CIPHER_TKIP;
171
 
        if (conf->wpa_pairwise & WPA_CIPHER_NONE)
172
 
                v |= 1<<IEEE80211_CIPHER_NONE;
173
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
174
 
                "%s: pairwise key ciphers=0x%x\n", __func__, v);
175
 
        if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
176
 
                printf("Unable to set pairwise key ciphers to 0x%x\n", v);
177
 
                return -1;
178
 
        }
179
 
 
180
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
181
 
                "%s: key management algorithms=0x%x\n",
182
 
                __func__, conf->wpa_key_mgmt);
183
 
        if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
184
 
                printf("Unable to set key management algorithms to 0x%x\n",
185
 
                        conf->wpa_key_mgmt);
186
 
                return -1;
187
 
        }
188
 
 
189
 
        v = 0;
190
 
        if (conf->rsn_preauth)
191
 
                v |= BIT(0);
192
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
193
 
                "%s: rsn capabilities=0x%x\n", __func__, conf->rsn_preauth);
194
 
        if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
195
 
                printf("Unable to set RSN capabilities to 0x%x\n", v);
196
 
                return -1;
197
 
        }
198
 
 
199
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
200
 
                "%s: enable WPA= 0x%x\n", __func__, conf->wpa);
201
 
        if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
202
 
                printf("Unable to set WPA to %u\n", conf->wpa);
203
 
                return -1;
204
 
        }
205
 
        return 0;
206
 
}
207
 
 
208
 
 
209
 
static int
210
 
bsd_set_iface_flags(void *priv, int dev_up)
211
 
{
212
 
        struct bsd_driver_data *drv = priv;
213
 
        struct hostapd_data *hapd = drv->hapd;
214
 
        struct ifreq ifr;
215
 
 
216
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
217
 
                "%s: dev_up=%d\n", __func__, dev_up);
218
 
 
219
 
        if (drv->ioctl_sock < 0)
220
 
                return -1;
221
 
 
222
 
        memset(&ifr, 0, sizeof(ifr));
223
 
        snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
224
 
 
225
 
        if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
226
 
                perror("ioctl[SIOCGIFFLAGS]");
227
 
                return -1;
228
 
        }
229
 
 
230
 
        if (dev_up)
231
 
                ifr.ifr_flags |= IFF_UP;
232
 
        else
233
 
                ifr.ifr_flags &= ~IFF_UP;
234
 
 
235
 
        if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
236
 
                perror("ioctl[SIOCSIFFLAGS]");
237
 
                return -1;
238
 
        }
239
 
 
240
 
        if (dev_up) {
241
 
                memset(&ifr, 0, sizeof(ifr));
242
 
                snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
243
 
                ifr.ifr_mtu = HOSTAPD_MTU;
244
 
                if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
245
 
                        perror("ioctl[SIOCSIFMTU]");
246
 
                        printf("Setting MTU failed - trying to survive with "
247
 
                               "current value\n");
248
 
                }
249
 
        }
250
 
 
251
 
        return 0;
252
 
}
253
 
 
254
 
static int
255
 
bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
256
 
{
257
 
        struct bsd_driver_data *drv = priv;
258
 
        struct hostapd_data *hapd = drv->hapd;
259
 
        struct hostapd_bss_config *conf = hapd->conf;
260
 
 
261
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
262
 
                "%s: enabled=%d\n", __func__, enabled);
263
 
 
264
 
        if (!enabled) {
265
 
                /* XXX restore state */
266
 
                return set80211param(priv, IEEE80211_IOC_AUTHMODE,
267
 
                        IEEE80211_AUTH_AUTO);
268
 
        }
269
 
        if (!conf->wpa && !conf->ieee802_1x) {
270
 
                hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
271
 
                        HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
272
 
                return -1;
273
 
        }
274
 
        if (conf->wpa && bsd_configure_wpa(drv) != 0) {
275
 
                hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
276
 
                        HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
277
 
                return -1;
278
 
        }
279
 
        if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
280
 
                (conf->wpa ?  IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
281
 
                hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
282
 
                        HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
283
 
                return -1;
284
 
        }
285
 
        return bsd_set_iface_flags(priv, 1);
286
 
}
287
 
 
288
 
static int
289
 
bsd_set_privacy(const char *ifname, void *priv, int enabled)
290
 
{
291
 
        struct bsd_driver_data *drv = priv;
292
 
        struct hostapd_data *hapd = drv->hapd;
293
 
 
294
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
295
 
                "%s: enabled=%d\n", __func__, enabled);
296
 
 
297
 
        return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled);
298
 
}
299
 
 
300
 
static int
301
 
bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
302
 
{
303
 
        struct bsd_driver_data *drv = priv;
304
 
        struct hostapd_data *hapd = drv->hapd;
305
 
        struct ieee80211req_mlme mlme;
306
 
 
307
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
308
 
                "%s: addr=%s authorized=%d\n",
309
 
                __func__, ether_sprintf(addr), authorized);
310
 
 
311
 
        if (authorized)
312
 
                mlme.im_op = IEEE80211_MLME_AUTHORIZE;
313
 
        else
314
 
                mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
315
 
        mlme.im_reason = 0;
316
 
        memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
317
 
        return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
318
 
}
319
 
 
320
 
static int
321
 
bsd_sta_set_flags(void *priv, const u8 *addr, int flags_or, int flags_and)
322
 
{
323
 
        /* For now, only support setting Authorized flag */
324
 
        if (flags_or & WLAN_STA_AUTHORIZED)
325
 
                return bsd_set_sta_authorized(priv, addr, 1);
326
 
        if (!(flags_and & WLAN_STA_AUTHORIZED))
327
 
                return bsd_set_sta_authorized(priv, addr, 0);
328
 
        return 0;
329
 
}
330
 
 
331
 
static int
332
 
bsd_del_key(void *priv, const u8 *addr, int key_idx)
333
 
{
334
 
        struct bsd_driver_data *drv = priv;
335
 
        struct hostapd_data *hapd = drv->hapd;
336
 
        struct ieee80211req_del_key wk;
337
 
 
338
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
339
 
                "%s: addr=%s key_idx=%d\n",
340
 
                __func__, ether_sprintf(addr), key_idx);
341
 
 
342
 
        memset(&wk, 0, sizeof(wk));
343
 
        if (addr != NULL) {
344
 
                memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
345
 
                wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
346
 
        } else {
347
 
                wk.idk_keyix = key_idx;
348
 
        }
349
 
 
350
 
        return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
351
 
}
352
 
 
353
 
static int
354
 
bsd_set_key(const char *ifname, void *priv, const char *alg,
355
 
            const u8 *addr, int key_idx,
356
 
            const u8 *key, size_t key_len, int txkey)
357
 
{
358
 
        struct bsd_driver_data *drv = priv;
359
 
        struct hostapd_data *hapd = drv->hapd;
360
 
        struct ieee80211req_key wk;
361
 
        u_int8_t cipher;
362
 
 
363
 
        if (strcmp(alg, "none") == 0)
364
 
                return bsd_del_key(priv, addr, key_idx);
365
 
 
366
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
367
 
                "%s: alg=%s addr=%s key_idx=%d\n",
368
 
                __func__, alg, ether_sprintf(addr), key_idx);
369
 
 
370
 
        if (strcmp(alg, "WEP") == 0)
371
 
                cipher = IEEE80211_CIPHER_WEP;
372
 
        else if (strcmp(alg, "TKIP") == 0)
373
 
                cipher = IEEE80211_CIPHER_TKIP;
374
 
        else if (strcmp(alg, "CCMP") == 0)
375
 
                cipher = IEEE80211_CIPHER_AES_CCM;
376
 
        else {
377
 
                printf("%s: unknown/unsupported algorithm %s\n",
378
 
                        __func__, alg);
379
 
                return -1;
380
 
        }
381
 
 
382
 
        if (key_len > sizeof(wk.ik_keydata)) {
383
 
                printf("%s: key length %d too big\n", __func__, key_len);
384
 
                return -3;
385
 
        }
386
 
 
387
 
        memset(&wk, 0, sizeof(wk));
388
 
        wk.ik_type = cipher;
389
 
        wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
390
 
        if (addr == NULL) {
391
 
                memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
392
 
                wk.ik_keyix = key_idx;
393
 
                wk.ik_flags |= IEEE80211_KEY_DEFAULT;
394
 
        } else {
395
 
                memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
396
 
                wk.ik_keyix = IEEE80211_KEYIX_NONE;
397
 
        }
398
 
        wk.ik_keylen = key_len;
399
 
        memcpy(wk.ik_keydata, key, key_len);
400
 
 
401
 
        return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
402
 
}
403
 
 
404
 
 
405
 
static int
406
 
bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
407
 
               u8 *seq)
408
 
{
409
 
        struct bsd_driver_data *drv = priv;
410
 
        struct hostapd_data *hapd = drv->hapd;
411
 
        struct ieee80211req_key wk;
412
 
 
413
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
414
 
                "%s: addr=%s idx=%d\n", __func__, ether_sprintf(addr), idx);
415
 
 
416
 
        memset(&wk, 0, sizeof(wk));
417
 
        if (addr == NULL)
418
 
                memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
419
 
        else
420
 
                memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
421
 
        wk.ik_keyix = idx;
422
 
 
423
 
        if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
424
 
                printf("Failed to get encryption.\n");
425
 
                return -1;
426
 
        }
427
 
 
428
 
#ifdef WORDS_BIGENDIAN
429
 
        {
430
 
                /*
431
 
                 * wk.ik_keytsc is in host byte order (big endian), need to
432
 
                 * swap it to match with the byte order used in WPA.
433
 
                 */
434
 
                int i;
435
 
                u8 tmp[WPA_KEY_RSC_LEN];
436
 
                memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
437
 
                for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
438
 
                        seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
439
 
                }
440
 
        }
441
 
#else /* WORDS_BIGENDIAN */
442
 
        memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
443
 
#endif /* WORDS_BIGENDIAN */
444
 
        return 0;
445
 
}
446
 
 
447
 
 
448
 
static int 
449
 
bsd_flush(void *priv)
450
 
{
451
 
        u8 allsta[IEEE80211_ADDR_LEN];
452
 
 
453
 
        memset(allsta, 0xff, IEEE80211_ADDR_LEN);
454
 
        return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
455
 
}
456
 
 
457
 
 
458
 
static int
459
 
bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
460
 
                         const u8 *addr)
461
 
{
462
 
        struct bsd_driver_data *drv = priv;
463
 
        struct ieee80211req_sta_stats stats;
464
 
 
465
 
        memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
466
 
        if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
467
 
                /* XXX? do packets counts include non-data frames? */
468
 
                data->rx_packets = stats.is_stats.ns_rx_data;
469
 
                data->rx_bytes = stats.is_stats.ns_rx_bytes;
470
 
                data->tx_packets = stats.is_stats.ns_tx_data;
471
 
                data->tx_bytes = stats.is_stats.ns_tx_bytes;
472
 
        }
473
 
        return 0;
474
 
}
475
 
 
476
 
static int
477
 
bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
478
 
{
479
 
        /*
480
 
         * Do nothing; we setup parameters at startup that define the
481
 
         * contents of the beacon information element.
482
 
         */
483
 
        return 0;
484
 
}
485
 
 
486
 
static int
487
 
bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
488
 
{
489
 
        struct bsd_driver_data *drv = priv;
490
 
        struct hostapd_data *hapd = drv->hapd;
491
 
        struct ieee80211req_mlme mlme;
492
 
 
493
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
494
 
                "%s: addr=%s reason_code=%d\n",
495
 
                __func__, ether_sprintf(addr), reason_code);
496
 
 
497
 
        mlme.im_op = IEEE80211_MLME_DEAUTH;
498
 
        mlme.im_reason = reason_code;
499
 
        memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
500
 
        return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
501
 
}
502
 
 
503
 
static int
504
 
bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
505
 
{
506
 
        struct bsd_driver_data *drv = priv;
507
 
        struct hostapd_data *hapd = drv->hapd;
508
 
        struct ieee80211req_mlme mlme;
509
 
 
510
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
511
 
                "%s: addr=%s reason_code=%d\n",
512
 
                __func__, ether_sprintf(addr), reason_code);
513
 
 
514
 
        mlme.im_op = IEEE80211_MLME_DISASSOC;
515
 
        mlme.im_reason = reason_code;
516
 
        memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
517
 
        return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
518
 
}
519
 
 
520
 
static int
521
 
bsd_del_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
522
 
{
523
 
        struct hostapd_data *hapd = drv->hapd;
524
 
        struct hostapd_bss_config *conf = hapd->conf;
525
 
        struct sta_info *sta;
526
 
 
527
 
        hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
528
 
                HOSTAPD_LEVEL_INFO, "deassociated");
529
 
 
530
 
        sta = ap_get_sta(hapd, addr);
531
 
        if (sta != NULL) {
532
 
                sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
533
 
                if (conf->wpa)
534
 
                        wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
535
 
                sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
536
 
                ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
537
 
                ap_free_sta(hapd, sta);
538
 
        }
539
 
        return 0;
540
 
}
541
 
 
542
 
static int
543
 
bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
544
 
{
545
 
        struct hostapd_data *hapd = drv->hapd;
546
 
        struct hostapd_bss_config *conf = hapd->conf;
547
 
        struct sta_info *sta;
548
 
        struct ieee80211req_wpaie ie;
549
 
        int new_assoc, ielen, res;
550
 
 
551
 
        hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
552
 
                HOSTAPD_LEVEL_INFO, "associated");
553
 
 
554
 
        sta = ap_sta_add(hapd, addr);
555
 
        if (sta == NULL)
556
 
                return -1;
557
 
        /*
558
 
         * Fetch and validate any negotiated WPA/RSN parameters.
559
 
         */
560
 
        if (conf->wpa) {
561
 
                memset(&ie, 0, sizeof(ie));
562
 
                memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
563
 
                if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
564
 
                        printf("Failed to get WPA/RSN information element.\n");
565
 
                        return -1;              /* XXX not right */
566
 
                }
567
 
                ielen = ie.wpa_ie[1];
568
 
                if (ielen == 0) {
569
 
                        printf("No WPA/RSN information element for station!\n");
570
 
                        return -1;              /* XXX not right */
571
 
                }
572
 
                ielen += 2;
573
 
                if (sta->wpa_sm == NULL)
574
 
                        sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
575
 
                                                        sta->addr);
576
 
                if (sta->wpa_sm == NULL) {
577
 
                        printf("Failed to initialize WPA state machine\n");
578
 
                        return -1;
579
 
                }
580
 
                res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
581
 
                                          ie.wpa_ie, ielen);
582
 
                if (res != WPA_IE_OK) {
583
 
                        printf("WPA/RSN information element rejected? "
584
 
                                "(res %u)\n", res);
585
 
                        return -1;
586
 
                }
587
 
        }
588
 
 
589
 
        /*
590
 
         * Now that the internal station state is setup
591
 
         * kick the authenticator into action.
592
 
         */
593
 
        new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
594
 
        sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
595
 
        wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
596
 
        hostapd_new_assoc_sta(hapd, sta, !new_assoc);
597
 
        ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
598
 
        return 0;
599
 
}
600
 
 
601
 
#include <net/route.h>
602
 
#include <net80211/ieee80211_freebsd.h>
603
 
 
604
 
static void
605
 
bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
606
 
{
607
 
        struct bsd_driver_data *drv = ctx;
608
 
        struct hostapd_data *hapd = drv->hapd;
609
 
        char buf[2048];
610
 
        struct if_announcemsghdr *ifan;
611
 
        struct rt_msghdr *rtm;
612
 
        struct ieee80211_michael_event *mic;
613
 
        struct ieee80211_join_event *join;
614
 
        struct ieee80211_leave_event *leave;
615
 
        int n;
616
 
 
617
 
        n = read(sock, buf, sizeof(buf));
618
 
        if (n < 0) {
619
 
                if (errno != EINTR && errno != EAGAIN)
620
 
                        perror("read(PF_ROUTE)");
621
 
                return;
622
 
        }
623
 
 
624
 
        rtm = (struct rt_msghdr *) buf;
625
 
        if (rtm->rtm_version != RTM_VERSION) {
626
 
                wpa_printf(MSG_DEBUG, "Routing message version %d not "
627
 
                        "understood\n", rtm->rtm_version);
628
 
                return;
629
 
        }
630
 
        ifan = (struct if_announcemsghdr *) rtm;
631
 
        switch (rtm->rtm_type) {
632
 
        case RTM_IEEE80211:
633
 
                switch (ifan->ifan_what) {
634
 
                case RTM_IEEE80211_ASSOC:
635
 
                case RTM_IEEE80211_REASSOC:
636
 
                case RTM_IEEE80211_DISASSOC:
637
 
                case RTM_IEEE80211_SCAN:
638
 
                        break;
639
 
                case RTM_IEEE80211_LEAVE:
640
 
                        leave = (struct ieee80211_leave_event *) &ifan[1];
641
 
                        bsd_del_sta(drv, leave->iev_addr);
642
 
                        break;
643
 
                case RTM_IEEE80211_JOIN:
644
 
#ifdef RTM_IEEE80211_REJOIN
645
 
                case RTM_IEEE80211_REJOIN:
646
 
#endif
647
 
                        join = (struct ieee80211_join_event *) &ifan[1];
648
 
                        bsd_new_sta(drv, join->iev_addr);
649
 
                        break;
650
 
                case RTM_IEEE80211_REPLAY:
651
 
                        /* ignore */
652
 
                        break;
653
 
                case RTM_IEEE80211_MICHAEL:
654
 
                        mic = (struct ieee80211_michael_event *) &ifan[1];
655
 
                        wpa_printf(MSG_DEBUG,
656
 
                                "Michael MIC failure wireless event: "
657
 
                                "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
658
 
                                MAC2STR(mic->iev_src));
659
 
                        ieee80211_michael_mic_failure(hapd, mic->iev_src, 1);
660
 
                        break;
661
 
                }
662
 
                break;
663
 
        }
664
 
}
665
 
 
666
 
static int
667
 
bsd_wireless_event_init(void *priv)
668
 
{
669
 
        struct bsd_driver_data *drv = priv;
670
 
        int s;
671
 
 
672
 
        drv->wext_sock = -1;
673
 
 
674
 
        s = socket(PF_ROUTE, SOCK_RAW, 0);
675
 
        if (s < 0) {
676
 
                perror("socket(PF_ROUTE,SOCK_RAW)");
677
 
                return -1;
678
 
        }
679
 
        eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL);
680
 
        drv->wext_sock = s;
681
 
 
682
 
        return 0;
683
 
}
684
 
 
685
 
static void
686
 
bsd_wireless_event_deinit(void *priv)
687
 
{
688
 
        struct bsd_driver_data *drv = priv;
689
 
 
690
 
        if (drv != NULL) {
691
 
                if (drv->wext_sock < 0)
692
 
                        return;
693
 
                eloop_unregister_read_sock(drv->wext_sock);
694
 
                close(drv->wext_sock);
695
 
        }
696
 
}
697
 
 
698
 
 
699
 
static int
700
 
bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
701
 
               int encrypt, const u8 *own_addr)
702
 
{
703
 
        struct bsd_driver_data *drv = priv;
704
 
        unsigned char buf[3000];
705
 
        unsigned char *bp = buf;
706
 
        struct l2_ethhdr *eth;
707
 
        size_t len;
708
 
        int status;
709
 
 
710
 
        /*
711
 
         * Prepend the Etherent header.  If the caller left us
712
 
         * space at the front we could just insert it but since
713
 
         * we don't know we copy to a local buffer.  Given the frequency
714
 
         * and size of frames this probably doesn't matter.
715
 
         */
716
 
        len = data_len + sizeof(struct l2_ethhdr);
717
 
        if (len > sizeof(buf)) {
718
 
                bp = malloc(len);
719
 
                if (bp == NULL) {
720
 
                        printf("EAPOL frame discarded, cannot malloc temp "
721
 
                                "buffer of size %u!\n", len);
722
 
                        return -1;
723
 
                }
724
 
        }
725
 
        eth = (struct l2_ethhdr *) bp;
726
 
        memcpy(eth->h_dest, addr, ETH_ALEN);
727
 
        memcpy(eth->h_source, own_addr, ETH_ALEN);
728
 
        eth->h_proto = htons(ETH_P_EAPOL);
729
 
        memcpy(eth+1, data, data_len);
730
 
 
731
 
        wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
732
 
 
733
 
        status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
734
 
 
735
 
        if (bp != buf)
736
 
                free(bp);
737
 
        return status;
738
 
}
739
 
 
740
 
static void
741
 
handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
742
 
{
743
 
        struct bsd_driver_data *drv = ctx;
744
 
        struct hostapd_data *hapd = drv->hapd;
745
 
        struct sta_info *sta;
746
 
 
747
 
        sta = ap_get_sta(hapd, src_addr);
748
 
        if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
749
 
                printf("Data frame from not associated STA %s\n",
750
 
                       ether_sprintf(src_addr));
751
 
                /* XXX cannot happen */
752
 
                return;
753
 
        }
754
 
        ieee802_1x_receive(hapd, src_addr, buf + sizeof(struct l2_ethhdr),
755
 
                           len - sizeof(struct l2_ethhdr));
756
 
}
757
 
 
758
 
static int
759
 
bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
760
 
{
761
 
        struct bsd_driver_data *drv = priv;
762
 
        struct hostapd_data *hapd = drv->hapd;
763
 
        int ssid_len = get80211var(priv, IEEE80211_IOC_SSID, buf, len);
764
 
 
765
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: ssid=\"%.*s\"\n",
766
 
                __func__, ssid_len, buf);
767
 
 
768
 
        return ssid_len;
769
 
}
770
 
 
771
 
static int
772
 
bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
773
 
{
774
 
        struct bsd_driver_data *drv = priv;
775
 
        struct hostapd_data *hapd = drv->hapd;
776
 
 
777
 
        HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: ssid=\"%.*s\"\n",
778
 
                __func__, len, buf);
779
 
 
780
 
        return set80211var(priv, IEEE80211_IOC_SSID, buf, len);
781
 
}
782
 
 
783
 
static int
784
 
bsd_init(struct hostapd_data *hapd)
785
 
{
786
 
        struct bsd_driver_data *drv;
787
 
 
788
 
        drv = wpa_zalloc(sizeof(struct bsd_driver_data));
789
 
        if (drv == NULL) {
790
 
                printf("Could not allocate memory for bsd driver data\n");
791
 
                goto bad;
792
 
        }
793
 
 
794
 
        drv->ops = bsd_driver_ops;
795
 
        drv->hapd = hapd;
796
 
        drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
797
 
        if (drv->ioctl_sock < 0) {
798
 
                perror("socket[PF_INET,SOCK_DGRAM]");
799
 
                goto bad;
800
 
        }
801
 
        memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
802
 
 
803
 
        drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
804
 
                                        handle_read, drv, 1);
805
 
        if (drv->sock_xmit == NULL)
806
 
                goto bad;
807
 
        if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
808
 
                goto bad;
809
 
 
810
 
        bsd_set_iface_flags(drv, 0);    /* mark down during setup */
811
 
 
812
 
        hapd->driver = &drv->ops;
813
 
        return 0;
814
 
bad:
815
 
        if (drv->sock_xmit != NULL)
816
 
                l2_packet_deinit(drv->sock_xmit);
817
 
        if (drv->ioctl_sock >= 0)
818
 
                close(drv->ioctl_sock);
819
 
        if (drv != NULL)
820
 
                free(drv);
821
 
        return -1;
822
 
}
823
 
 
824
 
 
825
 
static void
826
 
bsd_deinit(void *priv)
827
 
{
828
 
        struct bsd_driver_data *drv = priv;
829
 
 
830
 
        drv->hapd->driver = NULL;
831
 
 
832
 
        (void) bsd_set_iface_flags(drv, 0);
833
 
        if (drv->ioctl_sock >= 0)
834
 
                close(drv->ioctl_sock);
835
 
        if (drv->sock_xmit != NULL)
836
 
                l2_packet_deinit(drv->sock_xmit);
837
 
        free(drv);
838
 
}
839
 
 
840
 
static const struct driver_ops bsd_driver_ops = {
841
 
        .name                   = "bsd",
842
 
        .init                   = bsd_init,
843
 
        .deinit                 = bsd_deinit,
844
 
        .set_ieee8021x          = bsd_set_ieee8021x,
845
 
        .set_privacy            = bsd_set_privacy,
846
 
        .set_encryption         = bsd_set_key,
847
 
        .get_seqnum             = bsd_get_seqnum,
848
 
        .flush                  = bsd_flush,
849
 
        .set_generic_elem       = bsd_set_opt_ie,
850
 
        .wireless_event_init    = bsd_wireless_event_init,
851
 
        .wireless_event_deinit  = bsd_wireless_event_deinit,
852
 
        .sta_set_flags          = bsd_sta_set_flags,
853
 
        .read_sta_data          = bsd_read_sta_driver_data,
854
 
        .send_eapol             = bsd_send_eapol,
855
 
        .sta_disassoc           = bsd_sta_disassoc,
856
 
        .sta_deauth             = bsd_sta_deauth,
857
 
        .set_ssid               = bsd_set_ssid,
858
 
        .get_ssid               = bsd_get_ssid,
859
 
};
860
 
 
861
 
void bsd_driver_register(void)
862
 
{
863
 
        driver_register(bsd_driver_ops.name, &bsd_driver_ops);
864
 
}