f53ad7d898cefa38193deff73a4e991636e90092
[oweals/openwrt.git] / openwrt / package / hostapd / patches / 002-madwifi.patch
1 diff -ruN hostapd-0.4.5-old/driver_madwifi.c hostapd-0.4.5-new/driver_madwifi.c
2 --- hostapd-0.4.5-old/driver_madwifi.c  2005-10-23 14:52:39.000000000 +0200
3 +++ hostapd-0.4.5-new/driver_madwifi.c  2005-10-23 15:09:45.000000000 +0200
4 @@ -20,19 +20,20 @@
5  #include <errno.h>
6  #include <sys/types.h>
7  #include <sys/socket.h>
8 +#include <linux/if.h>
9 +#include <stdint.h>
10  
11  #include <include/compat.h>
12  #include <net80211/ieee80211.h>
13  #ifdef WME_NUM_AC
14  /* Assume this is built against BSD branch of madwifi driver. */
15 -#define MADWIFI_BSD
16  #include <net80211/_ieee80211.h>
17  #endif /* WME_NUM_AC */
18  #include <net80211/ieee80211_crypto.h>
19  #include <net80211/ieee80211_ioctl.h>
20  
21  #include <net/if_arp.h>
22 -#include "wireless_copy.h"
23 +#include <wireless.h>
24  
25  #include <netinet/in.h>
26  #include <netpacket/packet.h>
27 diff -ruN hostapd-0.4.5-old/driver_madwifi.~c hostapd-0.4.5-new/driver_madwifi.~c
28 --- hostapd-0.4.5-old/driver_madwifi.~c 1970-01-01 01:00:00.000000000 +0100
29 +++ hostapd-0.4.5-new/driver_madwifi.~c 2005-10-23 15:06:20.000000000 +0200
30 @@ -0,0 +1,1248 @@
31 +/*
32 + * Host AP - driver interaction with MADWIFI 802.11 driver
33 + * Copyright (c) 2004, Sam Leffler <sam@errno.com>
34 + * Copyright (c) 2004, Video54 Technologies
35 + *
36 + * This program is free software; you can redistribute it and/or modify
37 + * it under the terms of the GNU General Public License version 2 as
38 + * published by the Free Software Foundation.
39 + *
40 + * Alternatively, this software may be distributed under the terms of BSD
41 + * license.
42 + *
43 + * See README and COPYING for more details.
44 + */
45 +#include <stdlib.h>
46 +#include <stdio.h>
47 +#include <unistd.h>
48 +#include <string.h>
49 +#include <sys/ioctl.h>
50 +#include <errno.h>
51 +#include <sys/types.h>
52 +#include <sys/socket.h>
53 +
54 +#include <include/compat.h>
55 +#include <net80211/ieee80211.h>
56 +#ifdef WME_NUM_AC
57 +/* Assume this is built against BSD branch of madwifi driver. */
58 +#include <net80211/_ieee80211.h>
59 +#endif /* WME_NUM_AC */
60 +#include <net80211/ieee80211_crypto.h>
61 +#include <net80211/ieee80211_ioctl.h>
62 +
63 +#include <net/if_arp.h>
64 +#include <wireless.h>
65 +
66 +#include <netinet/in.h>
67 +#include <netpacket/packet.h>
68 +
69 +#include "hostapd.h"
70 +#include "driver.h"
71 +#include "ieee802_1x.h"
72 +#include "eloop.h"
73 +#include "priv_netlink.h"
74 +#include "sta_info.h"
75 +#include "l2_packet.h"
76 +#include "hostap_common.h"
77 +
78 +#include "eapol_sm.h"
79 +#include "wpa.h"
80 +#include "radius.h"
81 +#include "ieee802_11.h"
82 +#include "accounting.h"
83 +#include "common.h"
84 +
85 +
86 +struct madwifi_driver_data {
87 +       struct driver_ops ops;                  /* base class */
88 +       struct hostapd_data *hapd;              /* back pointer */
89 +
90 +       char    iface[IFNAMSIZ + 1];
91 +       int     ifindex;
92 +       struct l2_packet_data *sock_xmit;       /* raw packet xmit socket */
93 +       struct l2_packet_data *sock_recv;       /* raw packet recv socket */
94 +       int     ioctl_sock;                     /* socket for ioctl() use */
95 +       int     wext_sock;                      /* socket for wireless events */
96 +       int     we_version;
97 +       u8      acct_mac[ETH_ALEN];
98 +       struct hostap_sta_driver_data acct_data;
99 +};
100 +
101 +static const struct driver_ops madwifi_driver_ops;
102 +
103 +static int madwifi_sta_deauth(void *priv, u8 *addr, int reason_code);
104 +
105 +static int
106 +set80211priv(struct madwifi_driver_data *drv, int op, void *data, int len)
107 +{
108 +#define        N(a)    (sizeof(a)/sizeof(a[0]))
109 +       struct iwreq iwr;
110 +
111 +       memset(&iwr, 0, sizeof(iwr));
112 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
113 +       if (len < IFNAMSIZ) {
114 +               /*
115 +                * Argument data fits inline; put it there.
116 +                */
117 +               memcpy(iwr.u.name, data, len);
118 +       } else {
119 +               /*
120 +                * Argument data too big for inline transfer; setup a
121 +                * parameter block instead; the kernel will transfer
122 +                * the data for the driver.
123 +                */
124 +               iwr.u.data.pointer = data;
125 +               iwr.u.data.length = len;
126 +       }
127 +
128 +       if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
129 +               static const char *opnames[] = {
130 +                       "ioctl[IEEE80211_IOCTL_SETPARAM]",
131 +                       "ioctl[IEEE80211_IOCTL_GETPARAM]",
132 +                       "ioctl[IEEE80211_IOCTL_SETKEY]",
133 +                       "ioctl[SIOCIWFIRSTPRIV+3]",
134 +                       "ioctl[IEEE80211_IOCTL_DELKEY]",
135 +                       "ioctl[SIOCIWFIRSTPRIV+5]",
136 +                       "ioctl[IEEE80211_IOCTL_SETMLME]",
137 +                       "ioctl[SIOCIWFIRSTPRIV+7]",
138 +                       "ioctl[IEEE80211_IOCTL_SETOPTIE]",
139 +                       "ioctl[IEEE80211_IOCTL_GETOPTIE]",
140 +                       "ioctl[IEEE80211_IOCTL_ADDMAC]",
141 +                       "ioctl[SIOCIWFIRSTPRIV+11]",
142 +                       "ioctl[IEEE80211_IOCTL_DELMAC]",
143 +                       "ioctl[SIOCIWFIRSTPRIV+13]",
144 +                       "ioctl[IEEE80211_IOCTL_CHANLIST]",
145 +                       "ioctl[SIOCIWFIRSTPRIV+15]",
146 +                       "ioctl[IEEE80211_IOCTL_GETRSN]",
147 +                       "ioctl[SIOCIWFIRSTPRIV+17]",
148 +                       "ioctl[IEEE80211_IOCTL_GETKEY]",
149 +               };
150 +               op -= SIOCIWFIRSTPRIV;
151 +               if (0 <= op && op < N(opnames))
152 +                       perror(opnames[op]);
153 +               else
154 +                       perror("ioctl[unknown???]");
155 +               return -1;
156 +       }
157 +       return 0;
158 +#undef N
159 +}
160 +
161 +static int
162 +set80211param(struct madwifi_driver_data *drv, int op, int arg)
163 +{
164 +       struct iwreq iwr;
165 +
166 +       memset(&iwr, 0, sizeof(iwr));
167 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
168 +       iwr.u.mode = op;
169 +       memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
170 +
171 +       if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
172 +               perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
173 +               return -1;
174 +       }
175 +       return 0;
176 +}
177 +
178 +static const char *
179 +ether_sprintf(const u8 *addr)
180 +{
181 +       static char buf[sizeof(MACSTR)];
182 +
183 +       if (addr != NULL)
184 +               snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
185 +       else
186 +               snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
187 +       return buf;
188 +}
189 +
190 +/*
191 + * Configure WPA parameters.
192 + */
193 +static int
194 +madwifi_configure_wpa(struct madwifi_driver_data *drv)
195 +{
196 +       hostapd *hapd = drv->hapd;
197 +       struct hostapd_config *conf = hapd->conf;
198 +       int v;
199 +
200 +       switch (conf->wpa_group) {
201 +       case WPA_CIPHER_CCMP:
202 +               v = IEEE80211_CIPHER_AES_CCM;
203 +               break;
204 +       case WPA_CIPHER_TKIP:
205 +               v = IEEE80211_CIPHER_TKIP;
206 +               break;
207 +       case WPA_CIPHER_WEP104:
208 +               v = IEEE80211_CIPHER_WEP;
209 +               break;
210 +       case WPA_CIPHER_WEP40:
211 +               v = IEEE80211_CIPHER_WEP;
212 +               break;
213 +       case WPA_CIPHER_NONE:
214 +               v = IEEE80211_CIPHER_NONE;
215 +               break;
216 +       default:
217 +               printf("Unknown group key cipher %u\n",
218 +                       conf->wpa_group);
219 +               return -1;
220 +       }
221 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
222 +               "%s: group key cipher=%d\n", __func__, v);
223 +       if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
224 +               printf("Unable to set group key cipher to %u\n", v);
225 +               return -1;
226 +       }
227 +       if (v == IEEE80211_CIPHER_WEP) {
228 +               /* key length is done only for specific ciphers */
229 +               v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
230 +               if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
231 +                       printf("Unable to set group key length to %u\n", v);
232 +                       return -1;
233 +               }
234 +       }
235 +
236 +       v = 0;
237 +       if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
238 +               v |= 1<<IEEE80211_CIPHER_AES_CCM;
239 +       if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
240 +               v |= 1<<IEEE80211_CIPHER_TKIP;
241 +       if (conf->wpa_pairwise & WPA_CIPHER_NONE)
242 +               v |= 1<<IEEE80211_CIPHER_NONE;
243 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
244 +               "%s: pairwise key ciphers=0x%x\n", __func__, v);
245 +       if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
246 +               printf("Unable to set pairwise key ciphers to 0x%x\n", v);
247 +               return -1;
248 +       }
249 +
250 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
251 +               "%s: key management algorithms=0x%x\n",
252 +               __func__, conf->wpa_key_mgmt);
253 +       if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS, conf->wpa_key_mgmt)) {
254 +               printf("Unable to set key management algorithms to 0x%x\n",
255 +                       conf->wpa_key_mgmt);
256 +               return -1;
257 +       }
258 +
259 +       v = 0;
260 +       if (conf->rsn_preauth)
261 +               v |= BIT(0);
262 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
263 +               "%s: rsn capabilities=0x%x\n", __func__, conf->rsn_preauth);
264 +       if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
265 +               printf("Unable to set RSN capabilities to 0x%x\n", v);
266 +               return -1;
267 +       }
268 +
269 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
270 +               "%s: enable WPA= 0x%x\n", __func__, conf->wpa);
271 +       if (set80211param(drv, IEEE80211_PARAM_WPA, conf->wpa)) {
272 +               printf("Unable to set WPA to %u\n", conf->wpa);
273 +               return -1;
274 +       }
275 +       return 0;
276 +}
277 +
278 +
279 +static int
280 +madwifi_set_iface_flags(void *priv, int dev_up)
281 +{
282 +       struct madwifi_driver_data *drv = priv;
283 +       hostapd *hapd = drv->hapd;
284 +       struct ifreq ifr;
285 +
286 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
287 +               "%s: dev_up=%d\n", __func__, dev_up);
288 +
289 +       if (drv->ioctl_sock < 0)
290 +               return -1;
291 +
292 +       memset(&ifr, 0, sizeof(ifr));
293 +       snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
294 +
295 +       if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
296 +               perror("ioctl[SIOCGIFFLAGS]");
297 +               return -1;
298 +       }
299 +
300 +       if (dev_up)
301 +               ifr.ifr_flags |= IFF_UP;
302 +       else
303 +               ifr.ifr_flags &= ~IFF_UP;
304 +
305 +       if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
306 +               perror("ioctl[SIOCSIFFLAGS]");
307 +               return -1;
308 +       }
309 +
310 +       if (dev_up) {
311 +               memset(&ifr, 0, sizeof(ifr));
312 +               snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
313 +               ifr.ifr_mtu = HOSTAPD_MTU;
314 +               if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
315 +                       perror("ioctl[SIOCSIFMTU]");
316 +                       printf("Setting MTU failed - trying to survive with "
317 +                              "current value\n");
318 +               }
319 +       }
320 +
321 +       return 0;
322 +}
323 +
324 +static int
325 +madwifi_set_ieee8021x(void *priv, int enabled)
326 +{
327 +       struct madwifi_driver_data *drv = priv;
328 +       hostapd *hapd = drv->hapd;
329 +       struct hostapd_config *conf = hapd->conf;
330 +
331 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
332 +               "%s: enabled=%d\n", __func__, enabled);
333 +
334 +       if (!enabled) {
335 +               /* XXX restore state */
336 +               return set80211param(priv, IEEE80211_PARAM_AUTHMODE,
337 +                       IEEE80211_AUTH_AUTO);
338 +       }
339 +       if (!conf->wpa && !conf->ieee802_1x) {
340 +               hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
341 +                       HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
342 +               return -1;
343 +       }
344 +       if (conf->wpa && madwifi_configure_wpa(drv) != 0) {
345 +               hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
346 +                       HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
347 +               return -1;
348 +       }
349 +       if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
350 +               (conf->wpa ?  IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
351 +               hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
352 +                       HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
353 +               return -1;
354 +       }
355 +       return madwifi_set_iface_flags(priv, 1);
356 +}
357 +
358 +static int
359 +madwifi_set_privacy(void *priv, int enabled)
360 +{
361 +       struct madwifi_driver_data *drv = priv;
362 +       hostapd *hapd = drv->hapd;
363 +
364 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
365 +               "%s: enabled=%d\n", __func__, enabled);
366 +
367 +       return set80211param(priv, IEEE80211_PARAM_PRIVACY, enabled);
368 +}
369 +
370 +static int
371 +madwifi_set_sta_authorized(void *priv, u8 *addr, int authorized)
372 +{
373 +       struct madwifi_driver_data *drv = priv;
374 +       hostapd *hapd = drv->hapd;
375 +       struct ieee80211req_mlme mlme;
376 +
377 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
378 +               "%s: addr=%s authorized=%d\n",
379 +               __func__, ether_sprintf(addr), authorized);
380 +
381 +       if (authorized)
382 +               mlme.im_op = IEEE80211_MLME_AUTHORIZE;
383 +       else
384 +               mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
385 +       mlme.im_reason = 0;
386 +       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
387 +       return set80211priv(priv, IEEE80211_IOCTL_SETMLME, &mlme,
388 +                           sizeof(mlme));
389 +}
390 +
391 +static int
392 +madwifi_del_key(void *priv, unsigned char *addr, int key_idx)
393 +{
394 +       struct madwifi_driver_data *drv = priv;
395 +       hostapd *hapd = drv->hapd;
396 +       struct ieee80211req_del_key wk;
397 +
398 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
399 +               "%s: addr=%s key_idx=%d\n",
400 +               __func__, ether_sprintf(addr), key_idx);
401 +
402 +       memset(&wk, 0, sizeof(wk));
403 +       if (addr != NULL) {
404 +               memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
405 +               wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
406 +       } else {
407 +               wk.idk_keyix = key_idx;
408 +       }
409 +
410 +       return set80211priv(priv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
411 +}
412 +
413 +static int
414 +madwifi_set_key(void *priv, const char *alg,
415 +            unsigned char *addr, int key_idx,
416 +            u8 *key, size_t key_len)
417 +{
418 +       struct madwifi_driver_data *drv = priv;
419 +       hostapd *hapd = drv->hapd;
420 +       struct ieee80211req_key wk;
421 +       u_int8_t cipher;
422 +
423 +       if (strcmp(alg, "none") == 0)
424 +               return madwifi_del_key(priv, addr, key_idx);
425 +
426 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
427 +               "%s: alg=%s addr=%s key_idx=%d\n",
428 +               __func__, alg, ether_sprintf(addr), key_idx);
429 +
430 +       if (strcmp(alg, "WEP") == 0)
431 +               cipher = IEEE80211_CIPHER_WEP;
432 +       else if (strcmp(alg, "TKIP") == 0)
433 +               cipher = IEEE80211_CIPHER_TKIP;
434 +       else if (strcmp(alg, "CCMP") == 0)
435 +               cipher = IEEE80211_CIPHER_AES_CCM;
436 +       else {
437 +               printf("%s: unknown/unsupported algorithm %s\n",
438 +                       __func__, alg);
439 +               return -1;
440 +       }
441 +
442 +       if (key_len > sizeof(wk.ik_keydata)) {
443 +               printf("%s: key length %lu too big\n", __func__,
444 +                      (unsigned long) key_len);
445 +               return -3;
446 +       }
447 +
448 +       memset(&wk, 0, sizeof(wk));
449 +       wk.ik_type = cipher;
450 +       wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
451 +       if (addr == NULL) {
452 +               memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
453 +               wk.ik_keyix = key_idx;
454 +               wk.ik_flags |= IEEE80211_KEY_DEFAULT;
455 +       } else {
456 +               memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
457 +               wk.ik_keyix = IEEE80211_KEYIX_NONE;
458 +       }
459 +       wk.ik_keylen = key_len;
460 +       memcpy(wk.ik_keydata, key, key_len);
461 +
462 +       return set80211priv(priv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
463 +}
464 +
465 +
466 +static int
467 +madwifi_get_seqnum(void *priv, u8 *addr, int idx, u8 *seq)
468 +{
469 +       struct madwifi_driver_data *drv = priv;
470 +       hostapd *hapd = drv->hapd;
471 +       struct ieee80211req_key wk;
472 +
473 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
474 +               "%s: addr=%s idx=%d\n", __func__, ether_sprintf(addr), idx);
475 +
476 +       memset(&wk, 0, sizeof(wk));
477 +       if (addr == NULL)
478 +               memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
479 +       else
480 +               memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
481 +       wk.ik_keyix = idx;
482 +
483 +       if (set80211priv(priv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
484 +               printf("Failed to get encryption.\n");
485 +               return -1;
486 +       }
487 +
488 +#ifdef WORDS_BIGENDIAN
489 +       {
490 +               /*
491 +                * wk.ik_keytsc is in host byte order (big endian), need to
492 +                * swap it to match with the byte order used in WPA.
493 +                */
494 +               int i;
495 +               u8 tmp[WPA_KEY_RSC_LEN];
496 +               memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
497 +               for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
498 +                       seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
499 +               }
500 +       }
501 +#else /* WORDS_BIGENDIAN */
502 +       memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
503 +#endif /* WORDS_BIGENDIAN */
504 +       return 0;
505 +}
506 +
507 +
508 +static int 
509 +madwifi_flush(void *priv)
510 +{
511 +#ifdef MADWIFI_BSD
512 +       u8 allsta[IEEE80211_ADDR_LEN];
513 +       memset(allsta, 0xff, IEEE80211_ADDR_LEN);
514 +       return madwifi_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
515 +#else /* MADWIFI_BSD */
516 +       return 0;               /* XXX */
517 +#endif /* MADWIFI_BSD */
518 +}
519 +
520 +
521 +static int
522 +madwifi_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
523 +                                       u8 *addr)
524 +{
525 +       struct madwifi_driver_data *drv = priv;
526 +
527 +#ifdef MADWIFI_BSD
528 +       struct ieee80211req_sta_stats stats;
529 +
530 +       memset(data, 0, sizeof(*data));
531 +
532 +       /*
533 +        * Fetch statistics for station from the system.
534 +        */
535 +       memset(&stats, 0, sizeof(stats));
536 +       memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
537 +       if (set80211priv(drv, IEEE80211_IOCTL_GETSTASTATS, &stats,
538 +                        sizeof(stats))) {
539 +               if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
540 +                       memcpy(data, &drv->acct_data, sizeof(*data));
541 +                       return 0;
542 +               }
543 +
544 +               printf("Failed to get station stats information element.\n");
545 +               return -1;
546 +       }
547 +
548 +       data->rx_packets = stats.is_stats.ns_rx_data;
549 +       data->rx_bytes = stats.is_stats.ns_rx_bytes;
550 +       data->tx_packets = stats.is_stats.ns_tx_data;
551 +       data->tx_bytes = stats.is_stats.ns_tx_bytes;
552 +       return 0;
553 +
554 +#else /* MADWIFI_BSD */
555 +
556 +       char buf[1024], line[128], *pos;
557 +       FILE *f;
558 +       unsigned long val;
559 +
560 +       memset(data, 0, sizeof(*data));
561 +       snprintf(buf, sizeof(buf), "/proc/net/madwifi/%s/" MACSTR,
562 +                drv->iface, MAC2STR(addr));
563 +
564 +       f = fopen(buf, "r");
565 +       if (!f) {
566 +               if (memcmp(addr, drv->acct_mac, ETH_ALEN) != 0)
567 +                       return -1;
568 +               memcpy(data, &drv->acct_data, sizeof(*data));
569 +               return 0;
570 +       }
571 +       /* Need to read proc file with in one piece, so use large enough
572 +        * buffer. */
573 +       setbuffer(f, buf, sizeof(buf));
574 +
575 +       while (fgets(line, sizeof(line), f)) {
576 +               pos = strchr(line, '=');
577 +               if (!pos)
578 +                       continue;
579 +               *pos++ = '\0';
580 +               val = strtoul(pos, NULL, 10);
581 +               if (strcmp(line, "rx_packets") == 0)
582 +                       data->rx_packets = val;
583 +               else if (strcmp(line, "tx_packets") == 0)
584 +                       data->tx_packets = val;
585 +               else if (strcmp(line, "rx_bytes") == 0)
586 +                       data->rx_bytes = val;
587 +               else if (strcmp(line, "tx_bytes") == 0)
588 +                       data->tx_bytes = val;
589 +       }
590 +
591 +       fclose(f);
592 +
593 +       return 0;
594 +#endif /* MADWIFI_BSD */
595 +}
596 +
597 +
598 +static int
599 +madwifi_sta_clear_stats(void *priv, u8 *addr)
600 +{
601 +#ifdef MADWIFI_BSD
602 +       struct madwifi_driver_data *drv = priv;
603 +       struct hostapd_data *hapd = drv->hapd;
604 +       struct ieee80211req_mlme mlme;
605 +       
606 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: addr=%s\n",
607 +                     __func__, ether_sprintf(addr));
608 +
609 +       mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
610 +       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
611 +       return set80211priv(priv, IEEE80211_IOCTL_SETMLME, &mlme,
612 +                           sizeof(mlme));
613 +#else /* MADWIFI_BSD */
614 +       return 0; /* FIX */
615 +#endif /* MADWIFI_BSD */
616 +}
617 +
618 +
619 +static int
620 +madwifi_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
621 +{
622 +       /*
623 +        * Do nothing; we setup parameters at startup that define the
624 +        * contents of the beacon information element.
625 +        */
626 +       return 0;
627 +}
628 +
629 +static int
630 +madwifi_sta_deauth(void *priv, u8 *addr, int reason_code)
631 +{
632 +       struct madwifi_driver_data *drv = priv;
633 +       hostapd *hapd = drv->hapd;
634 +       struct ieee80211req_mlme mlme;
635 +
636 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
637 +               "%s: addr=%s reason_code=%d\n",
638 +               __func__, ether_sprintf(addr), reason_code);
639 +
640 +       mlme.im_op = IEEE80211_MLME_DEAUTH;
641 +       mlme.im_reason = reason_code;
642 +       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
643 +       return set80211priv(priv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
644 +}
645 +
646 +static int
647 +madwifi_sta_disassoc(void *priv, u8 *addr, int reason_code)
648 +{
649 +       struct madwifi_driver_data *drv = priv;
650 +       hostapd *hapd = drv->hapd;
651 +       struct ieee80211req_mlme mlme;
652 +
653 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
654 +               "%s: addr=%s reason_code=%d\n",
655 +               __func__, ether_sprintf(addr), reason_code);
656 +
657 +       mlme.im_reason = reason_code;
658 +       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
659 +       return set80211priv(priv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
660 +}
661 +
662 +static int
663 +madwifi_del_sta(struct madwifi_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
664 +{
665 +       struct hostapd_data *hapd = drv->hapd;
666 +       struct sta_info *sta;
667 +
668 +       hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
669 +               HOSTAPD_LEVEL_INFO, "deassociated");
670 +
671 +       sta = ap_get_sta(hapd, addr);
672 +       if (sta != NULL) {
673 +               sta->flags &= ~WLAN_STA_ASSOC;
674 +               wpa_sm_event(hapd, sta, WPA_DISASSOC);
675 +               sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
676 +               ieee802_1x_set_port_enabled(hapd, sta, 0);
677 +               ap_free_sta(hapd, sta);
678 +       }
679 +       return 0;
680 +}
681 +
682 +static int
683 +madwifi_process_wpa_ie(struct madwifi_driver_data *drv, struct sta_info *sta)
684 +{
685 +       struct hostapd_data *hapd = drv->hapd;
686 +       struct ieee80211req_wpaie ie;
687 +       int ielen, res;
688 +
689 +       /*
690 +        * Fetch negotiated WPA/RSN parameters from the system.
691 +        */
692 +       memset(&ie, 0, sizeof(ie));
693 +       memcpy(ie.wpa_macaddr, sta->addr, IEEE80211_ADDR_LEN);
694 +       if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
695 +               printf("Failed to get WPA/RSN information element.\n");
696 +               return -1;              /* XXX not right */
697 +       }
698 +       ielen = ie.wpa_ie[1];
699 +       if (ielen == 0) {
700 +               printf("No WPA/RSN information element for station!?\n");
701 +               return -1;              /* XXX not right */
702 +       }
703 +       ielen += 2;
704 +       res = wpa_validate_wpa_ie(hapd, sta, ie.wpa_ie, ielen,
705 +                       ie.wpa_ie[0] == WLAN_EID_RSN ?
706 +                           HOSTAPD_WPA_VERSION_WPA2 : HOSTAPD_WPA_VERSION_WPA);
707 +       if (res != WPA_IE_OK) {
708 +               printf("WPA/RSN information element rejected? (res %u)\n", res);
709 +               return -1;
710 +       }
711 +       free(sta->wpa_ie);
712 +       sta->wpa_ie = malloc(ielen);
713 +       if (sta->wpa_ie == NULL) {
714 +               printf("No memory to save WPA/RSN information element!\n");
715 +               return -1;
716 +       }
717 +       memcpy(sta->wpa_ie, ie.wpa_ie, ielen);
718 +       sta->wpa_ie_len = ielen;
719 +       return 0;
720 +}
721 +
722 +static int
723 +madwifi_new_sta(struct madwifi_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
724 +{
725 +       struct hostapd_data *hapd = drv->hapd;
726 +       struct sta_info *sta;
727 +       int new_assoc;
728 +
729 +       hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
730 +               HOSTAPD_LEVEL_INFO, "associated");
731 +
732 +       sta = ap_get_sta(hapd, addr);
733 +       if (sta) {
734 +               accounting_sta_stop(hapd, sta);
735 +       } else {
736 +               sta = ap_sta_add(hapd, addr);
737 +               if (sta == NULL)
738 +                       return -1;
739 +       }
740 +
741 +       if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
742 +               /* Cached accounting data is not valid anymore. */
743 +               memset(drv->acct_mac, 0, ETH_ALEN);
744 +               memset(&drv->acct_data, 0, sizeof(drv->acct_data));
745 +       }
746 +       accounting_sta_get_id(hapd, sta);
747 +
748 +       if (hapd->conf->wpa) {
749 +               if (madwifi_process_wpa_ie(drv, sta))
750 +                       return -1;
751 +       } else {
752 +               free(sta->wpa_ie);
753 +               sta->wpa_ie = NULL;
754 +               sta->wpa_ie_len = 0;
755 +       }
756 +
757 +       /*
758 +        * Now that the internal station state is setup
759 +        * kick the authenticator into action.
760 +        */
761 +       new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
762 +       sta->flags |= WLAN_STA_ASSOC;
763 +       wpa_sm_event(hapd, sta, WPA_ASSOC);
764 +       hostapd_new_assoc_sta(hapd, sta, !new_assoc);
765 +       ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
766 +       return 0;
767 +}
768 +
769 +static void
770 +madwifi_wireless_event_wireless_custom(struct madwifi_driver_data *drv,
771 +                                      char *custom)
772 +{
773 +       struct hostapd_data *hapd = drv->hapd;
774 +
775 +       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Custom wireless event: '%s'\n",
776 +                     custom);
777 +
778 +       if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
779 +               char *pos;
780 +               u8 addr[ETH_ALEN];
781 +               pos = strstr(custom, "addr=");
782 +               if (pos == NULL) {
783 +                       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
784 +                                     "MLME-MICHAELMICFAILURE.indication "
785 +                                     "without sender address ignored\n");
786 +                       return;
787 +               }
788 +               pos += 5;
789 +               if (hwaddr_aton(pos, addr) == 0) {
790 +                       ieee80211_michael_mic_failure(drv->hapd, addr, 1);
791 +               } else {
792 +                       HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
793 +                                     "MLME-MICHAELMICFAILURE.indication "
794 +                                     "with invalid MAC address");
795 +               }
796 +       } else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
797 +               char *key, *value;
798 +               u32 val;
799 +               key = custom;
800 +               while ((key = strchr(key, '\n')) != NULL) {
801 +                       key++;
802 +                       value = strchr(key, '=');
803 +                       if (value == NULL)
804 +                               continue;
805 +                       *value++ = '\0';
806 +                       val = strtoul(value, NULL, 10);
807 +                       if (strcmp(key, "mac") == 0)
808 +                               hwaddr_aton(value, drv->acct_mac);
809 +                       else if (strcmp(key, "rx_packets") == 0)
810 +                               drv->acct_data.rx_packets = val;
811 +                       else if (strcmp(key, "tx_packets") == 0)
812 +                               drv->acct_data.tx_packets = val;
813 +                       else if (strcmp(key, "rx_bytes") == 0)
814 +                               drv->acct_data.rx_bytes = val;
815 +                       else if (strcmp(key, "tx_bytes") == 0)
816 +                               drv->acct_data.tx_bytes = val;
817 +                       key = value;
818 +               }
819 +       }
820 +}
821 +
822 +static void
823 +madwifi_wireless_event_wireless(struct madwifi_driver_data *drv,
824 +                                           char *data, int len)
825 +{
826 +       struct hostapd_data *hapd = drv->hapd;
827 +       struct iw_event iwe_buf, *iwe = &iwe_buf;
828 +       char *pos, *end, *custom, *buf;
829 +
830 +       pos = data;
831 +       end = data + len;
832 +
833 +       while (pos + IW_EV_LCP_LEN <= end) {
834 +               /* Event data may be unaligned, so make a local, aligned copy
835 +                * before processing. */
836 +               memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
837 +               HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "Wireless event: "
838 +                             "cmd=0x%x len=%d\n", iwe->cmd, iwe->len);
839 +               if (iwe->len <= IW_EV_LCP_LEN)
840 +                       return;
841 +
842 +               custom = pos + IW_EV_POINT_LEN;
843 +               if (drv->we_version > 18 &&
844 +                   (iwe->cmd == IWEVMICHAELMICFAILURE ||
845 +                    iwe->cmd == IWEVCUSTOM)) {
846 +                       /* WE-19 removed the pointer from struct iw_point */
847 +                       char *dpos = (char *) &iwe_buf.u.data.length;
848 +                       int dlen = dpos - (char *) &iwe_buf;
849 +                       memcpy(dpos, pos + IW_EV_LCP_LEN,
850 +                              sizeof(struct iw_event) - dlen);
851 +               } else {
852 +                       memcpy(&iwe_buf, pos, sizeof(struct iw_event));
853 +                       custom += IW_EV_POINT_OFF;
854 +               }
855 +
856 +               switch (iwe->cmd) {
857 +               case IWEVEXPIRED:
858 +                       madwifi_del_sta(drv, iwe->u.addr.sa_data);
859 +                       break;
860 +               case IWEVREGISTERED:
861 +                       madwifi_new_sta(drv, iwe->u.addr.sa_data);
862 +                       break;
863 +               case IWEVCUSTOM:
864 +                       if (custom + iwe->u.data.length > end)
865 +                               return;
866 +                       buf = malloc(iwe->u.data.length + 1);
867 +                       if (buf == NULL)
868 +                               return;         /* XXX */
869 +                       memcpy(buf, custom, iwe->u.data.length);
870 +                       buf[iwe->u.data.length] = '\0';
871 +                       madwifi_wireless_event_wireless_custom(drv, buf);
872 +                       free(buf);
873 +                       break;
874 +               }
875 +
876 +               pos += iwe->len;
877 +       }
878 +}
879 +
880 +
881 +static void
882 +madwifi_wireless_event_rtm_newlink(struct madwifi_driver_data *drv,
883 +                                              struct nlmsghdr *h, int len)
884 +{
885 +       struct ifinfomsg *ifi;
886 +       int attrlen, nlmsg_len, rta_len;
887 +       struct rtattr * attr;
888 +
889 +       if (len < sizeof(*ifi))
890 +               return;
891 +
892 +       ifi = NLMSG_DATA(h);
893 +
894 +       if (ifi->ifi_index != drv->ifindex)
895 +               return;
896 +
897 +       nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
898 +
899 +       attrlen = h->nlmsg_len - nlmsg_len;
900 +       if (attrlen < 0)
901 +               return;
902 +
903 +       attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
904 +
905 +       rta_len = RTA_ALIGN(sizeof(struct rtattr));
906 +       while (RTA_OK(attr, attrlen)) {
907 +               if (attr->rta_type == IFLA_WIRELESS) {
908 +                       madwifi_wireless_event_wireless(
909 +                               drv, ((char *) attr) + rta_len,
910 +                               attr->rta_len - rta_len);
911 +               }
912 +               attr = RTA_NEXT(attr, attrlen);
913 +       }
914 +}
915 +
916 +
917 +static void
918 +madwifi_wireless_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
919 +{
920 +       char buf[256];
921 +       int left;
922 +       struct sockaddr_nl from;
923 +       socklen_t fromlen;
924 +       struct nlmsghdr *h;
925 +       struct madwifi_driver_data *drv = eloop_ctx;
926 +
927 +       fromlen = sizeof(from);
928 +       left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
929 +                       (struct sockaddr *) &from, &fromlen);
930 +       if (left < 0) {
931 +               if (errno != EINTR && errno != EAGAIN)
932 +                       perror("recvfrom(netlink)");
933 +               return;
934 +       }
935 +
936 +       h = (struct nlmsghdr *) buf;
937 +       while (left >= sizeof(*h)) {
938 +               int len, plen;
939 +
940 +               len = h->nlmsg_len;
941 +               plen = len - sizeof(*h);
942 +               if (len > left || plen < 0) {
943 +                       printf("Malformed netlink message: "
944 +                              "len=%d left=%d plen=%d\n",
945 +                              len, left, plen);
946 +                       break;
947 +               }
948 +
949 +               switch (h->nlmsg_type) {
950 +               case RTM_NEWLINK:
951 +                       madwifi_wireless_event_rtm_newlink(drv, h, plen);
952 +                       break;
953 +               }
954 +
955 +               len = NLMSG_ALIGN(len);
956 +               left -= len;
957 +               h = (struct nlmsghdr *) ((char *) h + len);
958 +       }
959 +
960 +       if (left > 0) {
961 +               printf("%d extra bytes in the end of netlink message\n", left);
962 +       }
963 +}
964 +
965 +
966 +static int
967 +madwifi_get_we_version(struct madwifi_driver_data *drv)
968 +{
969 +       struct iw_range *range;
970 +       struct iwreq iwr;
971 +       int minlen;
972 +       size_t buflen;
973 +
974 +       drv->we_version = 0;
975 +
976 +       /*
977 +        * Use larger buffer than struct iw_range in order to allow the
978 +        * structure to grow in the future.
979 +        */
980 +       buflen = sizeof(struct iw_range) + 500;
981 +       range = malloc(buflen);
982 +       if (range == NULL)
983 +               return -1;
984 +       memset(range, 0, buflen);
985 +
986 +       memset(&iwr, 0, sizeof(iwr));
987 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
988 +       iwr.u.data.pointer = (caddr_t) range;
989 +       iwr.u.data.length = buflen;
990 +
991 +       minlen = ((char *) &range->enc_capa) - (char *) range +
992 +               sizeof(range->enc_capa);
993 +
994 +       if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
995 +               perror("ioctl[SIOCGIWRANGE]");
996 +               free(range);
997 +               return -1;
998 +       } else if (iwr.u.data.length >= minlen &&
999 +                  range->we_version_compiled >= 18) {
1000 +               wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1001 +                          "WE(source)=%d enc_capa=0x%x",
1002 +                          range->we_version_compiled,
1003 +                          range->we_version_source,
1004 +                          range->enc_capa);
1005 +               drv->we_version = range->we_version_compiled;
1006 +       }
1007 +
1008 +       free(range);
1009 +       return 0;
1010 +}
1011 +
1012 +
1013 +static int
1014 +madwifi_wireless_event_init(void *priv)
1015 +{
1016 +       struct madwifi_driver_data *drv = priv;
1017 +       int s;
1018 +       struct sockaddr_nl local;
1019 +
1020 +       madwifi_get_we_version(drv);
1021 +
1022 +       drv->wext_sock = -1;
1023 +
1024 +       s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1025 +       if (s < 0) {
1026 +               perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1027 +               return -1;
1028 +       }
1029 +
1030 +       memset(&local, 0, sizeof(local));
1031 +       local.nl_family = AF_NETLINK;
1032 +       local.nl_groups = RTMGRP_LINK;
1033 +       if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1034 +               perror("bind(netlink)");
1035 +               close(s);
1036 +               return -1;
1037 +       }
1038 +
1039 +       eloop_register_read_sock(s, madwifi_wireless_event_receive, drv, NULL);
1040 +       drv->wext_sock = s;
1041 +
1042 +       return 0;
1043 +}
1044 +
1045 +
1046 +static void
1047 +madwifi_wireless_event_deinit(void *priv)
1048 +{
1049 +       struct madwifi_driver_data *drv = priv;
1050 +
1051 +       if (drv != NULL) {
1052 +               if (drv->wext_sock < 0)
1053 +                       return;
1054 +               eloop_unregister_read_sock(drv->wext_sock);
1055 +               close(drv->wext_sock);
1056 +       }
1057 +}
1058 +
1059 +
1060 +static int
1061 +madwifi_send_eapol(void *priv, u8 *addr, u8 *data, size_t data_len, int encrypt)
1062 +{
1063 +       struct madwifi_driver_data *drv = priv;
1064 +       hostapd *hapd = drv->hapd;
1065 +       unsigned char buf[3000];
1066 +       unsigned char *bp = buf;
1067 +       struct l2_ethhdr *eth;
1068 +       size_t len;
1069 +       int status;
1070 +
1071 +       /*
1072 +        * Prepend the Etherent header.  If the caller left us
1073 +        * space at the front we could just insert it but since
1074 +        * we don't know we copy to a local buffer.  Given the frequency
1075 +        * and size of frames this probably doesn't matter.
1076 +        */
1077 +       len = data_len + sizeof(struct l2_ethhdr);
1078 +       if (len > sizeof(buf)) {
1079 +               bp = malloc(len);
1080 +               if (bp == NULL) {
1081 +                       printf("EAPOL frame discarded, cannot malloc temp "
1082 +                              "buffer of size %lu!\n", (unsigned long) len);
1083 +                       return -1;
1084 +               }
1085 +       }
1086 +       eth = (struct l2_ethhdr *) bp;
1087 +       memcpy(eth->h_dest, addr, ETH_ALEN);
1088 +       memcpy(eth->h_source, drv->hapd->own_addr, ETH_ALEN);
1089 +       eth->h_proto = htons(ETH_P_EAPOL);
1090 +       memcpy(eth+1, data, data_len);
1091 +
1092 +       if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS))
1093 +               hostapd_hexdump("TX EAPOL", bp, len);
1094 +
1095 +       status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1096 +
1097 +       if (bp != buf)
1098 +               free(bp);
1099 +       return status;
1100 +}
1101 +
1102 +static void
1103 +handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1104 +{
1105 +       struct madwifi_driver_data *drv = ctx;
1106 +       hostapd *hapd = drv->hapd;
1107 +       struct sta_info *sta;
1108 +
1109 +       sta = ap_get_sta(hapd, src_addr);
1110 +       if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1111 +               printf("Data frame from not associated STA %s\n",
1112 +                      ether_sprintf(src_addr));
1113 +               /* XXX cannot happen */
1114 +               return;
1115 +       }
1116 +       ieee802_1x_receive(hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1117 +                          len - sizeof(struct l2_ethhdr));
1118 +}
1119 +
1120 +static int
1121 +madwifi_init(struct hostapd_data *hapd)
1122 +{
1123 +       struct madwifi_driver_data *drv;
1124 +       struct ifreq ifr;
1125 +       struct iwreq iwr;
1126 +
1127 +       drv = malloc(sizeof(struct madwifi_driver_data));
1128 +       if (drv == NULL) {
1129 +               printf("Could not allocate memory for madwifi driver data\n");
1130 +               goto bad;
1131 +       }
1132 +
1133 +       memset(drv, 0, sizeof(*drv));
1134 +       drv->ops = madwifi_driver_ops;
1135 +       drv->hapd = hapd;
1136 +       drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1137 +       if (drv->ioctl_sock < 0) {
1138 +               perror("socket[PF_INET,SOCK_DGRAM]");
1139 +               goto bad;
1140 +       }
1141 +       memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1142 +
1143 +       memset(&ifr, 0, sizeof(ifr));
1144 +       snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", drv->iface);
1145 +       if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1146 +               perror("ioctl(SIOCGIFINDEX)");
1147 +               goto bad;
1148 +       }
1149 +       drv->ifindex = ifr.ifr_ifindex;
1150 +
1151 +       drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1152 +                                       handle_read, drv, 1);
1153 +       if (drv->sock_xmit == NULL)
1154 +               goto bad;
1155 +       if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
1156 +               goto bad;
1157 +       if (hapd->conf->bridge[0] != '\0') {
1158 +               HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
1159 +                       "Configure bridge %s for EAPOL traffic.\n",
1160 +                       hapd->conf->bridge);
1161 +               drv->sock_recv = l2_packet_init(hapd->conf->bridge, NULL,
1162 +                                               ETH_P_EAPOL, handle_read, drv,
1163 +                                               0);
1164 +               if (drv->sock_recv == NULL)
1165 +                       goto bad;
1166 +       } else
1167 +               drv->sock_recv = drv->sock_xmit;
1168 +
1169 +       memset(&iwr, 0, sizeof(iwr));
1170 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1171 +
1172 +       iwr.u.mode = IW_MODE_MASTER;
1173 +
1174 +       if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1175 +               perror("ioctl[SIOCSIWMODE]");
1176 +               printf("Could not set interface to master mode!\n");
1177 +               goto bad;
1178 +       }
1179 +
1180 +       madwifi_set_iface_flags(drv, 0);        /* mark down during setup */
1181 +
1182 +       hapd->driver = &drv->ops;
1183 +       return 0;
1184 +bad:
1185 +       if (drv->sock_xmit != NULL)
1186 +               l2_packet_deinit(drv->sock_xmit);
1187 +       if (drv->ioctl_sock >= 0)
1188 +               close(drv->ioctl_sock);
1189 +       if (drv != NULL)
1190 +               free(drv);
1191 +       return -1;
1192 +}
1193 +
1194 +
1195 +static void
1196 +madwifi_deinit(void *priv)
1197 +{
1198 +       struct madwifi_driver_data *drv = priv;
1199 +
1200 +       drv->hapd->driver = NULL;
1201 +
1202 +       (void) madwifi_set_iface_flags(drv, 0);
1203 +       if (drv->ioctl_sock >= 0)
1204 +               close(drv->ioctl_sock);
1205 +       if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1206 +               l2_packet_deinit(drv->sock_recv);
1207 +       if (drv->sock_xmit != NULL)
1208 +               l2_packet_deinit(drv->sock_xmit);
1209 +       free(drv);
1210 +}
1211 +
1212 +static int
1213 +madwifi_set_ssid(void *priv, u8 *buf, int len)
1214 +{
1215 +       struct madwifi_driver_data *drv = priv;
1216 +       struct iwreq iwr;
1217 +
1218 +       memset(&iwr, 0, sizeof(iwr));
1219 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1220 +       iwr.u.essid.flags = 1; /* SSID active */
1221 +       iwr.u.essid.pointer = (caddr_t) buf;
1222 +       iwr.u.essid.length = len + 1;
1223 +
1224 +       if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1225 +               perror("ioctl[SIOCSIWESSID]");
1226 +               printf("len=%d\n", len);
1227 +               return -1;
1228 +       }
1229 +       return 0;
1230 +}
1231 +
1232 +static int
1233 +madwifi_get_ssid(void *priv, u8 *buf, int len)
1234 +{
1235 +       struct madwifi_driver_data *drv = priv;
1236 +       struct iwreq iwr;
1237 +       int ret = 0;
1238 +
1239 +       memset(&iwr, 0, sizeof(iwr));
1240 +       strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1241 +       iwr.u.essid.pointer = (caddr_t) buf;
1242 +       iwr.u.essid.length = len;
1243 +
1244 +       if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1245 +               perror("ioctl[SIOCGIWESSID]");
1246 +               ret = -1;
1247 +       } else
1248 +               ret = iwr.u.essid.length;
1249 +
1250 +       return ret;
1251 +}
1252 +
1253 +static const struct driver_ops madwifi_driver_ops = {
1254 +       .name                   = "madwifi",
1255 +       .init                   = madwifi_init,
1256 +       .deinit                 = madwifi_deinit,
1257 +       .set_ieee8021x          = madwifi_set_ieee8021x,
1258 +       .set_privacy            = madwifi_set_privacy,
1259 +       .set_encryption         = madwifi_set_key,
1260 +       .get_seqnum             = madwifi_get_seqnum,
1261 +       .flush                  = madwifi_flush,
1262 +       .set_generic_elem       = madwifi_set_opt_ie,
1263 +       .wireless_event_init    = madwifi_wireless_event_init,
1264 +       .wireless_event_deinit  = madwifi_wireless_event_deinit,
1265 +       .set_sta_authorized     = madwifi_set_sta_authorized,
1266 +       .read_sta_data          = madwifi_read_sta_driver_data,
1267 +       .send_eapol             = madwifi_send_eapol,
1268 +       .sta_disassoc           = madwifi_sta_disassoc,
1269 +       .sta_deauth             = madwifi_sta_deauth,
1270 +       .set_ssid               = madwifi_set_ssid,
1271 +       .get_ssid               = madwifi_get_ssid,
1272 +       .sta_clear_stats        = madwifi_sta_clear_stats,
1273 +};
1274 +
1275 +void madwifi_driver_register(void)
1276 +{
1277 +       driver_register(madwifi_driver_ops.name, &madwifi_driver_ops);
1278 +}