WPS: Add support for external Registrars using UPnP transport
[wpasupplicant] / hostapd / driver_nl80211.c
1 /*
2  * hostapd / Kernel driver communication via nl80211
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5  * Copyright (c) 2005-2006, Devicescape Software, Inc.
6  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Alternatively, this software may be distributed under the terms of BSD
13  * license.
14  *
15  * See README and COPYING for more details.
16  */
17
18 #include "includes.h"
19
20 #include <sys/ioctl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
26 #include "nl80211_copy.h"
27 #include <net/if.h>
28 #include <netpacket/packet.h>
29 #include "wireless_copy.h"
30 #include <linux/filter.h>
31 #include <net/if_arp.h>
32
33 #include "hostapd.h"
34 #include "driver.h"
35 #include "eloop.h"
36 #include "hw_features.h"
37 #include "mlme.h"
38 #include "radiotap.h"
39 #include "radiotap_iter.h"
40 #include "ieee802_11_defs.h"
41
42 #ifdef CONFIG_LIBNL20
43 /* libnl 2.0 compatibility code */
44 #define nl_handle_alloc_cb nl_socket_alloc_cb
45 #define nl_handle_destroy nl_socket_free
46 #endif /* CONFIG_LIBNL20 */
47
48 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
49
50 enum ieee80211_msg_type {
51         ieee80211_msg_normal = 0,
52         ieee80211_msg_tx_callback_ack = 1,
53         ieee80211_msg_tx_callback_fail = 2,
54 };
55
56 struct i802_driver_data {
57         struct hostapd_data *hapd;
58
59         char iface[IFNAMSIZ + 1];
60         int bridge;
61         int ioctl_sock; /* socket for ioctl() use */
62         int wext_sock; /* socket for wireless events */
63         int eapol_sock; /* socket for EAPOL frames */
64         int monitor_sock; /* socket for monitor */
65         int monitor_ifidx;
66
67         int default_if_indices[16];
68         int *if_indices;
69         int num_if_indices;
70
71         int we_version;
72         struct nl_handle *nl_handle;
73         struct nl_cache *nl_cache;
74         struct nl_cb *nl_cb;
75         struct genl_family *nl80211;
76         int dtim_period, beacon_int;
77         unsigned int beacon_set:1;
78         unsigned int ieee802_1x_active:1;
79
80         int last_freq;
81         int last_freq_ht;
82 };
83
84
85 static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
86 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
87
88
89 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
90 {
91         int i;
92         int *old;
93
94         for (i = 0; i < drv->num_if_indices; i++) {
95                 if (drv->if_indices[i] == 0) {
96                         drv->if_indices[i] = ifidx;
97                         return;
98                 }
99         }
100
101         if (drv->if_indices != drv->default_if_indices)
102                 old = drv->if_indices;
103         else
104                 old = NULL;
105
106         drv->if_indices = realloc(old,
107                                   sizeof(int) * (drv->num_if_indices + 1));
108         if (!drv->if_indices) {
109                 if (!old)
110                         drv->if_indices = drv->default_if_indices;
111                 else
112                         drv->if_indices = old;
113                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
114                            "interfaces");
115                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
116                 return;
117         }
118         drv->if_indices[drv->num_if_indices] = ifidx;
119         drv->num_if_indices++;
120 }
121
122
123 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
124 {
125         int i;
126
127         for (i = 0; i < drv->num_if_indices; i++) {
128                 if (drv->if_indices[i] == ifidx) {
129                         drv->if_indices[i] = 0;
130                         break;
131                 }
132         }
133 }
134
135
136 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
137 {
138         int i;
139
140         if (ifidx == drv->bridge)
141                 return 1;
142
143         for (i = 0; i < drv->num_if_indices; i++)
144                 if (drv->if_indices[i] == ifidx)
145                         return 1;
146
147         return 0;
148 }
149
150
151 /* nl80211 code */
152 static int ack_handler(struct nl_msg *msg, void *arg)
153 {
154         int *err = arg;
155         *err = 0;
156         return NL_STOP;
157 }
158
159 static int finish_handler(struct nl_msg *msg, void *arg)
160 {
161         int *ret = arg;
162         *ret = 0;
163         return NL_SKIP;
164 }
165
166 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
167                          void *arg)
168 {
169         int *ret = arg;
170         *ret = err->error;
171         return NL_SKIP;
172 }
173
174 static int send_and_recv_msgs(struct i802_driver_data *drv,
175                               struct nl_msg *msg,
176                               int (*valid_handler)(struct nl_msg *, void *),
177                               void *valid_data)
178 {
179         struct nl_cb *cb;
180         int err = -ENOMEM;
181
182         cb = nl_cb_clone(drv->nl_cb);
183         if (!cb)
184                 goto out;
185
186         err = nl_send_auto_complete(drv->nl_handle, msg);
187         if (err < 0)
188                 goto out;
189
190         err = 1;
191
192         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
193         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
194         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
195
196         if (valid_handler)
197                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
198                           valid_handler, valid_data);
199
200         while (err > 0)
201                 nl_recvmsgs(drv->nl_handle, cb);
202  out:
203         nl_cb_put(cb);
204         nlmsg_free(msg);
205         return err;
206 }
207
208 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
209                                    const char *ifname, int dev_up)
210 {
211         struct ifreq ifr;
212
213         if (drv->ioctl_sock < 0)
214                 return -1;
215
216         memset(&ifr, 0, sizeof(ifr));
217         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
218
219         if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
220                 perror("ioctl[SIOCGIFFLAGS]");
221                 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
222                            drv->iface);
223                 return -1;
224         }
225
226         if (dev_up)
227                 ifr.ifr_flags |= IFF_UP;
228         else
229                 ifr.ifr_flags &= ~IFF_UP;
230
231         if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
232                 perror("ioctl[SIOCSIFFLAGS]");
233                 return -1;
234         }
235
236         return 0;
237 }
238
239
240 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
241                        const char *alg, const u8 *addr, int idx, const u8 *key,
242                        size_t key_len, int txkey)
243 {
244         struct nl_msg *msg;
245         int ret;
246
247         msg = nlmsg_alloc();
248         if (!msg)
249                 return -ENOMEM;
250
251         if (strcmp(alg, "none") == 0) {
252                 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
253                             0, NL80211_CMD_DEL_KEY, 0);
254         } else {
255                 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
256                             0, NL80211_CMD_NEW_KEY, 0);
257                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
258                 if (strcmp(alg, "WEP") == 0) {
259                         if (key_len == 5)
260                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
261                                             0x000FAC01);
262                         else
263                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
264                                             0x000FAC05);
265                 } else if (strcmp(alg, "TKIP") == 0)
266                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
267                 else if (strcmp(alg, "CCMP") == 0)
268                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
269                 else if (strcmp(alg, "IGTK") == 0)
270                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
271                 else {
272                         wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
273                                    "algorithm '%s'", __func__, alg);
274                         nlmsg_free(msg);
275                         return -1;
276                 }
277         }
278
279         if (addr)
280                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
281         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
282         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
283
284         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
285         if (ret == -ENOENT)
286                 ret = 0;
287
288         /*
289          * If we failed or don't need to set the default TX key (below),
290          * we're done here.
291          */
292         if (ret || !txkey || addr)
293                 return ret;
294
295         msg = nlmsg_alloc();
296         if (!msg)
297                 return -ENOMEM;
298
299         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
300                     0, NL80211_CMD_SET_KEY, 0);
301         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
302         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
303         if (strcmp(alg, "IGTK") == 0)
304                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
305         else
306                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
307
308         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
309         if (ret == -ENOENT)
310                 ret = 0;
311         return ret;
312  nla_put_failure:
313         return -ENOBUFS;
314 }
315
316
317 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
318                                const u8 *addr, int idx, const u8 *key,
319                                size_t key_len, int txkey)
320 {
321         struct i802_driver_data *drv = priv;
322         int ret;
323
324         ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, idx, key,
325                           key_len, txkey);
326         if (ret < 0)
327                 return ret;
328
329         return ret;
330 }
331
332
333 static inline int min_int(int a, int b)
334 {
335         if (a < b)
336                 return a;
337         return b;
338 }
339
340
341 static int get_key_handler(struct nl_msg *msg, void *arg)
342 {
343         struct nlattr *tb[NL80211_ATTR_MAX + 1];
344         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
345
346         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
347                   genlmsg_attrlen(gnlh, 0), NULL);
348
349         /*
350          * TODO: validate the key index and mac address!
351          * Otherwise, there's a race condition as soon as
352          * the kernel starts sending key notifications.
353          */
354
355         if (tb[NL80211_ATTR_KEY_SEQ])
356                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
357                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
358         return NL_SKIP;
359 }
360
361
362 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
363                            int idx, u8 *seq)
364 {
365         struct i802_driver_data *drv = priv;
366         struct nl_msg *msg;
367
368         msg = nlmsg_alloc();
369         if (!msg)
370                 return -ENOMEM;
371
372         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
373                     0, NL80211_CMD_GET_KEY, 0);
374
375         if (addr)
376                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
377         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
378         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
379
380         memset(seq, 0, 6);
381
382         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
383  nla_put_failure:
384         return -ENOBUFS;
385 }
386
387
388 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
389                               int mode)
390 {
391         struct i802_driver_data *drv = priv;
392         struct nl_msg *msg;
393         u8 rates[NL80211_MAX_SUPP_RATES];
394         u8 rates_len = 0;
395         int i;
396
397         msg = nlmsg_alloc();
398         if (!msg)
399                 return -ENOMEM;
400
401         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
402                     NL80211_CMD_SET_BSS, 0);
403
404         for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
405                 rates[rates_len++] = basic_rates[i] / 5;
406
407         NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
408
409         /* TODO: multi-BSS support */
410         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
411
412         return send_and_recv_msgs(drv, msg, NULL, NULL);
413  nla_put_failure:
414         return -ENOBUFS;
415 }
416
417
418 static int i802_send_frame(void *priv, const void *data, size_t len,
419                            int encrypt, int flags)
420 {
421         __u8 rtap_hdr[] = {
422                 0x00, 0x00, /* radiotap version */
423                 0x0e, 0x00, /* radiotap length */
424                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
425                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
426                 0x00,       /* padding */
427                 0x00, 0x00, /* RX and TX flags to indicate that */
428                 0x00, 0x00, /* this is the injected frame directly */
429         };
430         struct i802_driver_data *drv = priv;
431         struct iovec iov[2] = {
432                 {
433                         .iov_base = &rtap_hdr,
434                         .iov_len = sizeof(rtap_hdr),
435                 },
436                 {
437                         .iov_base = (void*)data,
438                         .iov_len = len,
439                 }
440         };
441         struct msghdr msg = {
442                 .msg_name = NULL,
443                 .msg_namelen = 0,
444                 .msg_iov = iov,
445                 .msg_iovlen = 2,
446                 .msg_control = NULL,
447                 .msg_controllen = 0,
448                 .msg_flags = 0,
449         };
450
451         if (encrypt)
452                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
453
454         return sendmsg(drv->monitor_sock, &msg, flags);
455 }
456
457 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
458                                 int flags)
459 {
460         struct ieee80211_mgmt *mgmt;
461         int do_not_encrypt = 0;
462         u16 fc;
463
464         mgmt = (struct ieee80211_mgmt *) data;
465         fc = le_to_host16(mgmt->frame_control);
466
467         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
468             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
469                 /*
470                  * Only one of the authentication frame types is encrypted.
471                  * In order for static WEP encryption to work properly (i.e.,
472                  * to not encrypt the frame), we need to tell mac80211 about
473                  * the frames that must not be encrypted.
474                  */
475                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
476                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
477                 if (auth_alg == WLAN_AUTH_OPEN ||
478                     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_trans != 3))
479                         do_not_encrypt = 1;
480         }
481
482         return i802_send_frame(priv, data, len, !do_not_encrypt, flags);
483 }
484
485 /* Set kernel driver on given frequency (MHz) */
486 static int i802_set_freq2(void *priv, struct hostapd_freq_params *freq)
487 {
488         struct i802_driver_data *drv = priv;
489         struct nl_msg *msg;
490
491         msg = nlmsg_alloc();
492         if (!msg)
493                 return -1;
494
495         drv->last_freq = freq->freq;
496         drv->last_freq_ht = freq->ht_enabled;
497
498         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
499                     NL80211_CMD_SET_WIPHY, 0);
500
501         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
502         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
503         if (freq->ht_enabled) {
504                 switch (freq->sec_channel_offset) {
505                 case -1:
506                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
507                                     NL80211_CHAN_HT40MINUS);
508                         break;
509                 case 1:
510                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
511                                     NL80211_CHAN_HT40PLUS);
512                         break;
513                 default:
514                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
515                                     NL80211_CHAN_HT20);
516                         break;
517                 }
518         }
519
520         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
521                 return 0;
522  nla_put_failure:
523         return -1;
524 }
525
526
527 static int i802_set_rts(void *priv, int rts)
528 {
529         struct i802_driver_data *drv = priv;
530         struct iwreq iwr;
531
532         memset(&iwr, 0, sizeof(iwr));
533         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
534         iwr.u.rts.value = rts;
535         iwr.u.rts.fixed = 1;
536
537         if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
538                 perror("ioctl[SIOCSIWRTS]");
539                 return -1;
540         }
541
542         return 0;
543 }
544
545
546 static int i802_get_rts(void *priv, int *rts)
547 {
548         struct i802_driver_data *drv = priv;
549         struct iwreq iwr;
550
551         memset(&iwr, 0, sizeof(iwr));
552         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
553
554         if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
555                 perror("ioctl[SIOCGIWRTS]");
556                 return -1;
557         }
558
559         *rts = iwr.u.rts.value;
560
561         return 0;
562 }
563
564
565 static int i802_set_frag(void *priv, int frag)
566 {
567         struct i802_driver_data *drv = priv;
568         struct iwreq iwr;
569
570         memset(&iwr, 0, sizeof(iwr));
571         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
572         iwr.u.frag.value = frag;
573         iwr.u.frag.fixed = 1;
574
575         if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
576                 perror("ioctl[SIOCSIWFRAG]");
577                 return -1;
578         }
579
580         return 0;
581 }
582
583
584 static int i802_get_frag(void *priv, int *frag)
585 {
586         struct i802_driver_data *drv = priv;
587         struct iwreq iwr;
588
589         memset(&iwr, 0, sizeof(iwr));
590         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
591
592         if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
593                 perror("ioctl[SIOCGIWFRAG]");
594                 return -1;
595         }
596
597         *frag = iwr.u.frag.value;
598
599         return 0;
600 }
601
602
603 static int i802_set_retry(void *priv, int short_retry, int long_retry)
604 {
605         struct i802_driver_data *drv = priv;
606         struct iwreq iwr;
607
608         memset(&iwr, 0, sizeof(iwr));
609         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
610
611         iwr.u.retry.value = short_retry;
612         iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
613         if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
614                 perror("ioctl[SIOCSIWRETRY(short)]");
615                 return -1;
616         }
617
618         iwr.u.retry.value = long_retry;
619         iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
620         if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
621                 perror("ioctl[SIOCSIWRETRY(long)]");
622                 return -1;
623         }
624
625         return 0;
626 }
627
628
629 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
630 {
631         struct i802_driver_data *drv = priv;
632         struct iwreq iwr;
633
634         memset(&iwr, 0, sizeof(iwr));
635         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
636
637         iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
638         if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
639                 perror("ioctl[SIOCGIWFRAG(short)]");
640                 return -1;
641         }
642         *short_retry = iwr.u.retry.value;
643
644         iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
645         if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
646                 perror("ioctl[SIOCGIWFRAG(long)]");
647                 return -1;
648         }
649         *long_retry = iwr.u.retry.value;
650
651         return 0;
652 }
653
654
655 static int i802_flush(void *priv)
656 {
657         struct i802_driver_data *drv = priv;
658         struct nl_msg *msg;
659
660         msg = nlmsg_alloc();
661         if (!msg)
662                 return -1;
663
664         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
665                     0, NL80211_CMD_DEL_STATION, 0);
666
667         /*
668          * XXX: FIX! this needs to flush all VLANs too
669          */
670         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
671                     if_nametoindex(drv->iface));
672
673         return send_and_recv_msgs(drv, msg, NULL, NULL);
674  nla_put_failure:
675         return -ENOBUFS;
676 }
677
678
679 static int get_sta_handler(struct nl_msg *msg, void *arg)
680 {
681         struct nlattr *tb[NL80211_ATTR_MAX + 1];
682         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
683         struct hostap_sta_driver_data *data = arg;
684         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
685         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
686                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
687                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
688                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
689         };
690
691         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
692                   genlmsg_attrlen(gnlh, 0), NULL);
693
694         /*
695          * TODO: validate the interface and mac address!
696          * Otherwise, there's a race condition as soon as
697          * the kernel starts sending station notifications.
698          */
699
700         if (!tb[NL80211_ATTR_STA_INFO]) {
701                 wpa_printf(MSG_DEBUG, "sta stats missing!");
702                 return NL_SKIP;
703         }
704         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
705                              tb[NL80211_ATTR_STA_INFO],
706                              stats_policy)) {
707                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
708                 return NL_SKIP;
709         }
710
711         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
712                 data->inactive_msec =
713                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
714         if (stats[NL80211_STA_INFO_RX_BYTES])
715                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
716         if (stats[NL80211_STA_INFO_TX_BYTES])
717                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
718
719         return NL_SKIP;
720 }
721
722 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
723                               const u8 *addr)
724 {
725         struct i802_driver_data *drv = priv;
726         struct nl_msg *msg;
727
728         msg = nlmsg_alloc();
729         if (!msg)
730                 return -ENOMEM;
731
732         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
733                     0, NL80211_CMD_GET_STATION, 0);
734
735         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
736         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
737
738         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
739  nla_put_failure:
740         return -ENOBUFS;
741 }
742
743
744 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
745                            size_t data_len, int encrypt, const u8 *own_addr)
746 {
747         struct i802_driver_data *drv = priv;
748         struct ieee80211_hdr *hdr;
749         size_t len;
750         u8 *pos;
751         int res;
752 #if 0 /* FIX */
753         int qos = sta->flags & WLAN_STA_WME;
754 #else
755         int qos = 0;
756 #endif
757
758         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
759                 data_len;
760         hdr = os_zalloc(len);
761         if (hdr == NULL) {
762                 printf("malloc() failed for i802_send_data(len=%lu)\n",
763                        (unsigned long) len);
764                 return -1;
765         }
766
767         hdr->frame_control =
768                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
769         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
770         if (encrypt)
771                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
772 #if 0 /* To be enabled if qos determination is added above */
773         if (qos) {
774                 hdr->frame_control |=
775                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
776         }
777 #endif
778
779         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
780         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
781         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
782         pos = (u8 *) (hdr + 1);
783
784 #if 0 /* To be enabled if qos determination is added above */
785         if (qos) {
786                 /* add an empty QoS header if needed */
787                 pos[0] = 0;
788                 pos[1] = 0;
789                 pos += 2;
790         }
791 #endif
792
793         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
794         pos += sizeof(rfc1042_header);
795         WPA_PUT_BE16(pos, ETH_P_PAE);
796         pos += 2;
797         memcpy(pos, data, data_len);
798
799         res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
800         free(hdr);
801
802         if (res < 0) {
803                 perror("i802_send_eapol: send");
804                 printf("i802_send_eapol - packet len: %lu - failed\n",
805                        (unsigned long) len);
806         }
807
808         return res;
809 }
810
811
812 static int i802_sta_add2(const char *ifname, void *priv,
813                          struct hostapd_sta_add_params *params)
814 {
815         struct i802_driver_data *drv = priv;
816         struct nl_msg *msg;
817         int ret = -ENOBUFS;
818
819         msg = nlmsg_alloc();
820         if (!msg)
821                 return -ENOMEM;
822
823         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
824                     0, NL80211_CMD_NEW_STATION, 0);
825
826         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
827                     if_nametoindex(drv->iface));
828         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
829         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
830         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
831                 params->supp_rates);
832         NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
833                     params->listen_interval);
834
835 #ifdef CONFIG_IEEE80211N
836         if (params->ht_capabilities) {
837                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
838                         params->ht_capabilities->length,
839                         &params->ht_capabilities->data);
840         }
841 #endif /* CONFIG_IEEE80211N */
842
843         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
844         if (ret == -EEXIST)
845                 ret = 0;
846  nla_put_failure:
847         return ret;
848 }
849
850
851 static int i802_sta_remove(void *priv, const u8 *addr)
852 {
853         struct i802_driver_data *drv = priv;
854         struct nl_msg *msg;
855         int ret;
856
857         msg = nlmsg_alloc();
858         if (!msg)
859                 return -ENOMEM;
860
861         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
862                     0, NL80211_CMD_DEL_STATION, 0);
863
864         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
865                     if_nametoindex(drv->iface));
866         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
867
868         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
869         if (ret == -ENOENT)
870                 return 0;
871         return ret;
872  nla_put_failure:
873         return -ENOBUFS;
874 }
875
876
877 static int i802_sta_set_flags(void *priv, const u8 *addr,
878                               int total_flags, int flags_or, int flags_and)
879 {
880         struct i802_driver_data *drv = priv;
881         struct nl_msg *msg, *flags = NULL;
882
883         msg = nlmsg_alloc();
884         if (!msg)
885                 return -ENOMEM;
886
887         flags = nlmsg_alloc();
888         if (!flags) {
889                 nlmsg_free(msg);
890                 return -ENOMEM;
891         }
892
893         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
894                     0, NL80211_CMD_SET_STATION, 0);
895
896         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
897                     if_nametoindex(drv->iface));
898         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
899
900         if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
901                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
902
903         if (total_flags & WLAN_STA_WME)
904                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
905
906         if (total_flags & WLAN_STA_SHORT_PREAMBLE)
907                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
908
909         if (total_flags & WLAN_STA_MFP)
910                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
911
912         if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
913                 goto nla_put_failure;
914
915         nlmsg_free(flags);
916
917         return send_and_recv_msgs(drv, msg, NULL, NULL);
918  nla_put_failure:
919         nlmsg_free(flags);
920         return -ENOBUFS;
921 }
922
923
924 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
925                                     int cw_min, int cw_max, int burst_time)
926 {
927         struct i802_driver_data *drv = priv;
928         struct nl_msg *msg;
929         struct nlattr *txq, *params;
930
931         msg = nlmsg_alloc();
932         if (!msg)
933                 return -1;
934
935         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
936                     0, NL80211_CMD_SET_WIPHY, 0);
937
938         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
939
940         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
941         if (!txq)
942                 goto nla_put_failure;
943
944         /* We are only sending parameters for a single TXQ at a time */
945         params = nla_nest_start(msg, 1);
946         if (!params)
947                 goto nla_put_failure;
948
949         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
950         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
951          * 32 usec, so need to convert the value here. */
952         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
953         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
954         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
955         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
956
957         nla_nest_end(msg, params);
958
959         nla_nest_end(msg, txq);
960
961         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
962                 return 0;
963  nla_put_failure:
964         return -1;
965 }
966
967
968 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
969 {
970         struct nl_msg *msg;
971
972         /* stop listening for EAPOL on this interface */
973         del_ifidx(drv, ifidx);
974
975         msg = nlmsg_alloc();
976         if (!msg)
977                 goto nla_put_failure;
978
979         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
980                     0, NL80211_CMD_DEL_INTERFACE, 0);
981         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
982
983         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
984                 return;
985  nla_put_failure:
986         printf("Failed to remove interface.\n");
987 }
988
989
990 static int nl80211_create_iface(struct i802_driver_data *drv,
991                                 const char *ifname,
992                                 enum nl80211_iftype iftype,
993                                 const u8 *addr)
994 {
995         struct nl_msg *msg, *flags = NULL;
996         int ifidx;
997         struct ifreq ifreq;
998         struct iwreq iwr;
999         int ret = -ENOBUFS;
1000
1001         msg = nlmsg_alloc();
1002         if (!msg)
1003                 return -1;
1004
1005         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1006                     0, NL80211_CMD_NEW_INTERFACE, 0);
1007         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1008         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1009         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1010
1011         if (iftype == NL80211_IFTYPE_MONITOR) {
1012                 int err;
1013
1014                 flags = nlmsg_alloc();
1015                 if (!flags)
1016                         goto nla_put_failure;
1017
1018                 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1019
1020                 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1021
1022                 nlmsg_free(flags);
1023
1024                 if (err)
1025                         goto nla_put_failure;
1026         }
1027
1028         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1029         if (ret) {
1030  nla_put_failure:
1031                 printf("Failed to create interface %s.\n", ifname);
1032                 return ret;
1033         }
1034
1035         ifidx = if_nametoindex(ifname);
1036
1037         if (ifidx <= 0)
1038                 return -1;
1039
1040         /* start listening for EAPOL on this interface */
1041         add_ifidx(drv, ifidx);
1042
1043         if (addr) {
1044                 switch (iftype) {
1045                 case NL80211_IFTYPE_AP:
1046                         os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1047                         memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1048                         ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1049
1050                         if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1051                                 nl80211_remove_iface(drv, ifidx);
1052                                 return -1;
1053                         }
1054                         break;
1055                 case NL80211_IFTYPE_WDS:
1056                         memset(&iwr, 0, sizeof(iwr));
1057                         os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1058                         iwr.u.addr.sa_family = ARPHRD_ETHER;
1059                         memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1060                         if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1061                                 return -1;
1062                         break;
1063                 default:
1064                         /* nothing */
1065                         break;
1066                 }
1067         }
1068
1069         return ifidx;
1070 }
1071
1072
1073 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1074 {
1075         int ifidx;
1076
1077         /*
1078          * The kernel supports that when the low-level driver does,
1079          * but we currently don't because we need per-BSS data that
1080          * currently we can't handle easily.
1081          */
1082         return -1;
1083
1084         ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1085         if (ifidx < 0)
1086                 return -1;
1087         if (hostapd_set_iface_flags(priv, ifname, 1)) {
1088                 nl80211_remove_iface(priv, ifidx);
1089                 return -1;
1090         }
1091         return 0;
1092 }
1093
1094
1095 static int i802_bss_remove(void *priv, const char *ifname)
1096 {
1097         nl80211_remove_iface(priv, if_nametoindex(ifname));
1098         return 0;
1099 }
1100
1101
1102 static int i802_set_beacon(const char *iface, void *priv,
1103                            u8 *head, size_t head_len,
1104                            u8 *tail, size_t tail_len)
1105 {
1106         struct i802_driver_data *drv = priv;
1107         struct nl_msg *msg;
1108         u8 cmd = NL80211_CMD_NEW_BEACON;
1109         int ret;
1110
1111         msg = nlmsg_alloc();
1112         if (!msg)
1113                 return -ENOMEM;
1114
1115         if (drv->beacon_set)
1116                 cmd = NL80211_CMD_SET_BEACON;
1117
1118         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1119                     0, cmd, 0);
1120         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1121         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1122         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1123         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1124
1125         if (!drv->dtim_period)
1126                 drv->dtim_period = 2;
1127         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1128
1129         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1130         if (!ret)
1131                 drv->beacon_set = 1;
1132         return ret;
1133  nla_put_failure:
1134         return -ENOBUFS;
1135 }
1136
1137
1138 static int i802_del_beacon(struct i802_driver_data *drv)
1139 {
1140         struct nl_msg *msg;
1141
1142         msg = nlmsg_alloc();
1143         if (!msg)
1144                 return -ENOMEM;
1145
1146         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1147                     0, NL80211_CMD_DEL_BEACON, 0);
1148         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1149
1150         return send_and_recv_msgs(drv, msg, NULL, NULL);
1151  nla_put_failure:
1152         return -ENOBUFS;
1153 }
1154
1155
1156 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1157 {
1158         struct i802_driver_data *drv = priv;
1159
1160         /*
1161          * FIXME: This needs to be per interface (BSS)
1162          */
1163         drv->ieee802_1x_active = enabled;
1164         return 0;
1165 }
1166
1167
1168 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1169 {
1170         struct i802_driver_data *drv = priv;
1171         struct iwreq iwr;
1172
1173         memset(&iwr, 0, sizeof(iwr));
1174
1175         os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1176         iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1177         iwr.u.param.value = enabled;
1178
1179         ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1180
1181         /* ignore errors, the kernel/driver might not care */
1182         return 0;
1183 }
1184
1185
1186 static int i802_set_internal_bridge(void *priv, int value)
1187 {
1188         return -1;
1189 }
1190
1191
1192 static int i802_set_beacon_int(void *priv, int value)
1193 {
1194         struct i802_driver_data *drv = priv;
1195         struct nl_msg *msg;
1196
1197         drv->beacon_int = value;
1198
1199         if (!drv->beacon_set)
1200                 return 0;
1201
1202         msg = nlmsg_alloc();
1203         if (!msg)
1204                 return -ENOMEM;
1205
1206         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1207                     0, NL80211_CMD_SET_BEACON, 0);
1208         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1209
1210         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1211
1212         return send_and_recv_msgs(drv, msg, NULL, NULL);
1213  nla_put_failure:
1214         return -ENOBUFS;
1215 }
1216
1217
1218 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1219 {
1220         struct i802_driver_data *drv = priv;
1221         struct nl_msg *msg;
1222
1223         msg = nlmsg_alloc();
1224         if (!msg)
1225                 return -ENOMEM;
1226
1227         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1228                     0, NL80211_CMD_SET_BEACON, 0);
1229         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1230
1231         drv->dtim_period = value;
1232         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1233
1234         return send_and_recv_msgs(drv, msg, NULL, NULL);
1235  nla_put_failure:
1236         return -ENOBUFS;
1237 }
1238
1239
1240 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1241 {
1242         struct i802_driver_data *drv = priv;
1243         struct nl_msg *msg;
1244
1245         msg = nlmsg_alloc();
1246         if (!msg)
1247                 return -ENOMEM;
1248
1249         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1250                     NL80211_CMD_SET_BSS, 0);
1251
1252         if (cts >= 0)
1253                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1254         if (preamble >= 0)
1255                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1256         if (slot >= 0)
1257                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1258
1259         /* TODO: multi-BSS support */
1260         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1261
1262         return send_and_recv_msgs(drv, msg, NULL, NULL);
1263  nla_put_failure:
1264         return -ENOBUFS;
1265 }
1266
1267
1268 static int i802_set_cts_protect(void *priv, int value)
1269 {
1270         return i802_set_bss(priv, value, -1, -1);
1271 }
1272
1273
1274 static int i802_set_preamble(void *priv, int value)
1275 {
1276         return i802_set_bss(priv, -1, value, -1);
1277 }
1278
1279
1280 static int i802_set_short_slot_time(void *priv, int value)
1281 {
1282         return i802_set_bss(priv, -1, -1, value);
1283 }
1284
1285
1286 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1287 {
1288         switch (type) {
1289         case HOSTAPD_IF_VLAN:
1290                 return NL80211_IFTYPE_AP_VLAN;
1291         case HOSTAPD_IF_WDS:
1292                 return NL80211_IFTYPE_WDS;
1293         }
1294         return -1;
1295 }
1296
1297
1298 static int i802_if_add(const char *iface, void *priv,
1299                        enum hostapd_driver_if_type type, char *ifname,
1300                        const u8 *addr)
1301 {
1302         if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1303                 return -1;
1304         return 0;
1305 }
1306
1307
1308 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1309                           char *ifname, const u8 *addr)
1310 {
1311         /* unused at the moment */
1312         return -1;
1313 }
1314
1315
1316 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1317                           const char *ifname, const u8 *addr)
1318 {
1319         nl80211_remove_iface(priv, if_nametoindex(ifname));
1320         return 0;
1321 }
1322
1323
1324 struct phy_info_arg {
1325         u16 *num_modes;
1326         struct hostapd_hw_modes *modes;
1327 };
1328
1329 static int phy_info_handler(struct nl_msg *msg, void *arg)
1330 {
1331         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1332         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1333         struct phy_info_arg *phy_info = arg;
1334
1335         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1336
1337         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1338         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1339                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1340                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1341                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1342                 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1343                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1344                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1345         };
1346
1347         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1348         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1349                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1350                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1351         };
1352
1353         struct nlattr *nl_band;
1354         struct nlattr *nl_freq;
1355         struct nlattr *nl_rate;
1356         int rem_band, rem_freq, rem_rate;
1357         struct hostapd_hw_modes *mode;
1358         int idx, mode_is_set;
1359
1360         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1361                   genlmsg_attrlen(gnlh, 0), NULL);
1362
1363         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1364                 return NL_SKIP;
1365
1366         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1367                 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1368                 if (!mode)
1369                         return NL_SKIP;
1370                 phy_info->modes = mode;
1371
1372                 mode_is_set = 0;
1373
1374                 mode = &phy_info->modes[*(phy_info->num_modes)];
1375                 memset(mode, 0, sizeof(*mode));
1376                 *(phy_info->num_modes) += 1;
1377
1378                 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1379                           nla_len(nl_band), NULL);
1380
1381                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1382                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1383                                   nla_len(nl_freq), freq_policy);
1384                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1385                                 continue;
1386                         mode->num_channels++;
1387                 }
1388
1389                 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1390                 if (!mode->channels)
1391                         return NL_SKIP;
1392
1393                 idx = 0;
1394
1395                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1396                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1397                                   nla_len(nl_freq), freq_policy);
1398                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1399                                 continue;
1400
1401                         mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1402                         mode->channels[idx].flag = 0;
1403
1404                         if (!mode_is_set) {
1405                                 /* crude heuristic */
1406                                 if (mode->channels[idx].freq < 4000)
1407                                         mode->mode = HOSTAPD_MODE_IEEE80211B;
1408                                 else
1409                                         mode->mode = HOSTAPD_MODE_IEEE80211A;
1410                                 mode_is_set = 1;
1411                         }
1412
1413                         /* crude heuristic */
1414                         if (mode->channels[idx].freq < 4000)
1415                                 if (mode->channels[idx].freq == 2848)
1416                                         mode->channels[idx].chan = 14;
1417                                 else
1418                                         mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1419                         else
1420                                 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1421
1422                         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1423                                 mode->channels[idx].flag |=
1424                                         HOSTAPD_CHAN_DISABLED;
1425                         if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1426                                 mode->channels[idx].flag |=
1427                                         HOSTAPD_CHAN_PASSIVE_SCAN;
1428                         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1429                                 mode->channels[idx].flag |=
1430                                         HOSTAPD_CHAN_NO_IBSS;
1431                         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1432                                 mode->channels[idx].flag |=
1433                                         HOSTAPD_CHAN_RADAR;
1434
1435                         if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
1436                             !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1437                                 mode->channels[idx].max_tx_power =
1438                                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
1439
1440                         idx++;
1441                 }
1442
1443                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1444                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1445                                   nla_len(nl_rate), rate_policy);
1446                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1447                                 continue;
1448                         mode->num_rates++;
1449                 }
1450
1451                 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1452                 if (!mode->rates)
1453                         return NL_SKIP;
1454
1455                 idx = 0;
1456
1457                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1458                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1459                                   nla_len(nl_rate), rate_policy);
1460                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1461                                 continue;
1462                         mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1463
1464                         /* crude heuristic */
1465                         if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1466                             mode->rates[idx].rate > 200)
1467                                 mode->mode = HOSTAPD_MODE_IEEE80211G;
1468
1469                         if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1470                                 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1471
1472                         idx++;
1473                 }
1474         }
1475
1476         return NL_SKIP;
1477 }
1478
1479 static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
1480                                              u16 *num_modes)
1481 {
1482         u16 m;
1483         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1484         int i, mode11g_idx = -1;
1485
1486         /* If only 802.11g mode is included, use it to construct matching
1487          * 802.11b mode data. */
1488
1489         for (m = 0; m < *num_modes; m++) {
1490                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1491                         return modes; /* 802.11b already included */
1492                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1493                         mode11g_idx = m;
1494         }
1495
1496         if (mode11g_idx < 0)
1497                 return modes; /* 2.4 GHz band not supported at all */
1498
1499         nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
1500         if (nmodes == NULL)
1501                 return modes; /* Could not add 802.11b mode */
1502
1503         mode = &nmodes[*num_modes];
1504         os_memset(mode, 0, sizeof(*mode));
1505         (*num_modes)++;
1506         modes = nmodes;
1507
1508         mode->mode = HOSTAPD_MODE_IEEE80211B;
1509
1510         mode11g = &modes[mode11g_idx];
1511         mode->num_channels = mode11g->num_channels;
1512         mode->channels = os_malloc(mode11g->num_channels *
1513                                    sizeof(struct hostapd_channel_data));
1514         if (mode->channels == NULL) {
1515                 (*num_modes)--;
1516                 return modes; /* Could not add 802.11b mode */
1517         }
1518         os_memcpy(mode->channels, mode11g->channels,
1519                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
1520
1521         mode->num_rates = 0;
1522         mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
1523         if (mode->rates == NULL) {
1524                 os_free(mode->channels);
1525                 (*num_modes)--;
1526                 return modes; /* Could not add 802.11b mode */
1527         }
1528
1529         for (i = 0; i < mode11g->num_rates; i++) {
1530                 if (mode11g->rates[i].rate > 110 ||
1531                     mode11g->rates[i].flags &
1532                     (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
1533                         continue;
1534                 mode->rates[mode->num_rates] = mode11g->rates[i];
1535                 mode->num_rates++;
1536                 if (mode->num_rates == 4)
1537                         break;
1538         }
1539
1540         if (mode->num_rates == 0) {
1541                 os_free(mode->channels);
1542                 os_free(mode->rates);
1543                 (*num_modes)--;
1544                 return modes; /* No 802.11b rates */
1545         }
1546
1547         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1548                    "information");
1549
1550         return modes;
1551 }
1552
1553 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1554                                                          u16 *num_modes,
1555                                                          u16 *flags)
1556 {
1557         struct i802_driver_data *drv = priv;
1558         struct nl_msg *msg;
1559         struct phy_info_arg result = {
1560                 .num_modes = num_modes,
1561                 .modes = NULL,
1562         };
1563
1564         *num_modes = 0;
1565         *flags = 0;
1566
1567         msg = nlmsg_alloc();
1568         if (!msg)
1569                 return NULL;
1570
1571         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1572                     0, NL80211_CMD_GET_WIPHY, 0);
1573
1574         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1575
1576         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1577                 return i802_add_11b(result.modes, num_modes);
1578  nla_put_failure:
1579         return NULL;
1580 }
1581
1582
1583 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1584                              const char *ifname, int vlan_id)
1585 {
1586         struct i802_driver_data *drv = priv;
1587         struct nl_msg *msg;
1588
1589         msg = nlmsg_alloc();
1590         if (!msg)
1591                 return -ENOMEM;
1592
1593         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1594                     0, NL80211_CMD_SET_STATION, 0);
1595
1596         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1597                     if_nametoindex(drv->iface));
1598         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1599         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1600                     if_nametoindex(ifname));
1601
1602         return send_and_recv_msgs(drv, msg, NULL, NULL);
1603  nla_put_failure:
1604         return -ENOBUFS;
1605 }
1606
1607
1608 static int i802_set_country(void *priv, const char *country)
1609 {
1610         struct i802_driver_data *drv = priv;
1611         struct nl_msg *msg;
1612         char alpha2[3];
1613
1614         msg = nlmsg_alloc();
1615         if (!msg)
1616                 return -ENOMEM;
1617
1618         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1619                     0, NL80211_CMD_REQ_SET_REG, 0);
1620
1621         alpha2[0] = country[0];
1622         alpha2[1] = country[1];
1623         alpha2[2] = '\0';
1624         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1625
1626         return send_and_recv_msgs(drv, msg, NULL, NULL);
1627  nla_put_failure:
1628         return -ENOBUFS;
1629 }
1630
1631
1632 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1633                                int ok)
1634 {
1635         struct ieee80211_hdr *hdr;
1636         u16 fc, type, stype;
1637
1638         hdr = (struct ieee80211_hdr *) buf;
1639         fc = le_to_host16(hdr->frame_control);
1640
1641         type = WLAN_FC_GET_TYPE(fc);
1642         stype = WLAN_FC_GET_STYPE(fc);
1643
1644         switch (type) {
1645         case WLAN_FC_TYPE_MGMT:
1646                 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1647                            ok ? "ACK" : "fail");
1648                 hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
1649                 break;
1650         case WLAN_FC_TYPE_CTRL:
1651                 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1652                            ok ? "ACK" : "fail");
1653                 break;
1654         case WLAN_FC_TYPE_DATA:
1655                 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1656                 break;
1657         default:
1658                 printf("unknown TX callback frame type %d\n", type);
1659                 break;
1660         }
1661 }
1662
1663
1664 static void handle_frame(struct i802_driver_data *drv,
1665                          struct hostapd_iface *iface, u8 *buf, size_t len,
1666                          struct hostapd_frame_info *hfi,
1667                          enum ieee80211_msg_type msg_type)
1668 {
1669         struct ieee80211_hdr *hdr;
1670         u16 fc, type, stype;
1671         size_t data_len = len;
1672         struct hostapd_data *hapd = NULL;
1673         int broadcast_bssid = 0;
1674         size_t i;
1675         u8 *bssid;
1676
1677         /*
1678          * PS-Poll frames are 16 bytes. All other frames are
1679          * 24 bytes or longer.
1680          */
1681         if (len < 16)
1682                 return;
1683
1684         hdr = (struct ieee80211_hdr *) buf;
1685         fc = le_to_host16(hdr->frame_control);
1686
1687         type = WLAN_FC_GET_TYPE(fc);
1688         stype = WLAN_FC_GET_STYPE(fc);
1689
1690         switch (type) {
1691         case WLAN_FC_TYPE_DATA:
1692                 if (len < 24)
1693                         return;
1694                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1695                 case WLAN_FC_TODS:
1696                         bssid = hdr->addr1;
1697                         break;
1698                 case WLAN_FC_FROMDS:
1699                         bssid = hdr->addr2;
1700                         break;
1701                 default:
1702                         /* discard */
1703                         return;
1704                 }
1705                 break;
1706         case WLAN_FC_TYPE_CTRL:
1707                 /* discard non-ps-poll frames */
1708                 if (stype != WLAN_FC_STYPE_PSPOLL)
1709                         return;
1710                 bssid = hdr->addr1;
1711                 break;
1712         case WLAN_FC_TYPE_MGMT:
1713                 bssid = hdr->addr3;
1714                 break;
1715         default:
1716                 /* discard */
1717                 return;
1718         }
1719
1720         /* find interface frame belongs to */
1721         for (i = 0; i < iface->num_bss; i++) {
1722                 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1723                         hapd = iface->bss[i];
1724                         break;
1725                 }
1726         }
1727
1728         if (hapd == NULL) {
1729                 hapd = iface->bss[0];
1730
1731                 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1732                     bssid[2] != 0xff || bssid[3] != 0xff ||
1733                     bssid[4] != 0xff || bssid[5] != 0xff) {
1734                         /*
1735                          * Unknown BSSID - drop frame if this is not from
1736                          * passive scanning or a beacon (at least ProbeReq
1737                          * frames to other APs may be allowed through RX
1738                          * filtering in the wlan hw/driver)
1739                          */
1740                         if ((type != WLAN_FC_TYPE_MGMT ||
1741                              stype != WLAN_FC_STYPE_BEACON))
1742                                 return;
1743                 } else
1744                         broadcast_bssid = 1;
1745         }
1746
1747         switch (msg_type) {
1748         case ieee80211_msg_normal:
1749                 /* continue processing */
1750                 break;
1751         case ieee80211_msg_tx_callback_ack:
1752                 handle_tx_callback(hapd, buf, data_len, 1);
1753                 return;
1754         case ieee80211_msg_tx_callback_fail:
1755                 handle_tx_callback(hapd, buf, data_len, 0);
1756                 return;
1757         }
1758
1759         switch (type) {
1760         case WLAN_FC_TYPE_MGMT:
1761                 if (stype != WLAN_FC_STYPE_BEACON &&
1762                     stype != WLAN_FC_STYPE_PROBE_REQ)
1763                         wpa_printf(MSG_MSGDUMP, "MGMT");
1764                 if (broadcast_bssid) {
1765                         for (i = 0; i < iface->num_bss; i++)
1766                                 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1767                                                 stype, hfi);
1768                 } else
1769                         hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1770                 break;
1771         case WLAN_FC_TYPE_CTRL:
1772                 /* can only get here with PS-Poll frames */
1773                 wpa_printf(MSG_DEBUG, "CTRL");
1774                 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1775                 break;
1776         case WLAN_FC_TYPE_DATA:
1777                 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1778                 break;
1779         }
1780 }
1781
1782
1783 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1784 {
1785         struct i802_driver_data *drv = eloop_ctx;
1786         struct hostapd_data *hapd = drv->hapd;
1787         struct sockaddr_ll lladdr;
1788         unsigned char buf[3000];
1789         int len;
1790         socklen_t fromlen = sizeof(lladdr);
1791
1792         len = recvfrom(sock, buf, sizeof(buf), 0,
1793                        (struct sockaddr *)&lladdr, &fromlen);
1794         if (len < 0) {
1795                 perror("recv");
1796                 return;
1797         }
1798
1799         if (have_ifidx(drv, lladdr.sll_ifindex))
1800                 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1801 }
1802
1803
1804 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1805 {
1806         struct i802_driver_data *drv = eloop_ctx;
1807         int len;
1808         unsigned char buf[3000];
1809         struct hostapd_data *hapd = drv->hapd;
1810         struct ieee80211_radiotap_iterator iter;
1811         int ret;
1812         struct hostapd_frame_info hfi;
1813         int injected = 0, failed = 0, msg_type, rxflags = 0;
1814
1815         len = recv(sock, buf, sizeof(buf), 0);
1816         if (len < 0) {
1817                 perror("recv");
1818                 return;
1819         }
1820
1821         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1822                 printf("received invalid radiotap frame\n");
1823                 return;
1824         }
1825
1826         memset(&hfi, 0, sizeof(hfi));
1827
1828         while (1) {
1829                 ret = ieee80211_radiotap_iterator_next(&iter);
1830                 if (ret == -ENOENT)
1831                         break;
1832                 if (ret) {
1833                         printf("received invalid radiotap frame (%d)\n", ret);
1834                         return;
1835                 }
1836                 switch (iter.this_arg_index) {
1837                 case IEEE80211_RADIOTAP_FLAGS:
1838                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1839                                 len -= 4;
1840                         break;
1841                 case IEEE80211_RADIOTAP_RX_FLAGS:
1842                         rxflags = 1;
1843                         break;
1844                 case IEEE80211_RADIOTAP_TX_FLAGS:
1845                         injected = 1;
1846                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1847                                         IEEE80211_RADIOTAP_F_TX_FAIL;
1848                         break;
1849                 case IEEE80211_RADIOTAP_DATA_RETRIES:
1850                         break;
1851                 case IEEE80211_RADIOTAP_CHANNEL:
1852                         /* TODO convert from freq/flags to channel number
1853                         hfi.channel = XXX;
1854                         hfi.phytype = XXX;
1855                          */
1856                         break;
1857                 case IEEE80211_RADIOTAP_RATE:
1858                         hfi.datarate = *iter.this_arg * 5;
1859                         break;
1860                 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1861                         hfi.ssi_signal = *iter.this_arg;
1862                         break;
1863                 }
1864         }
1865
1866         if (rxflags && injected)
1867                 return;
1868
1869         if (!injected)
1870                 msg_type = ieee80211_msg_normal;
1871         else if (failed)
1872                 msg_type = ieee80211_msg_tx_callback_fail;
1873         else
1874                 msg_type = ieee80211_msg_tx_callback_ack;
1875
1876         handle_frame(drv, hapd->iface, buf + iter.max_length,
1877                      len - iter.max_length, &hfi, msg_type);
1878 }
1879
1880
1881 /*
1882  * we post-process the filter code later and rewrite
1883  * this to the offset to the last instruction
1884  */
1885 #define PASS    0xFF
1886 #define FAIL    0xFE
1887
1888 static struct sock_filter msock_filter_insns[] = {
1889         /*
1890          * do a little-endian load of the radiotap length field
1891          */
1892         /* load lower byte into A */
1893         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
1894         /* put it into X (== index register) */
1895         BPF_STMT(BPF_MISC| BPF_TAX, 0),
1896         /* load upper byte into A */
1897         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
1898         /* left-shift it by 8 */
1899         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1900         /* or with X */
1901         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1902         /* put result into X */
1903         BPF_STMT(BPF_MISC| BPF_TAX, 0),
1904
1905         /*
1906          * Allow management frames through, this also gives us those
1907          * management frames that we sent ourselves with status
1908          */
1909         /* load the lower byte of the IEEE 802.11 frame control field */
1910         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
1911         /* mask off frame type and version */
1912         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1913         /* accept frame if it's both 0, fall through otherwise */
1914         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1915
1916         /*
1917          * TODO: add a bit to radiotap RX flags that indicates
1918          * that the sending station is not associated, then
1919          * add a filter here that filters on our DA and that flag
1920          * to allow us to deauth frames to that bad station.
1921          *
1922          * Not a regression -- we didn't do it before either.
1923          */
1924
1925 #if 0
1926         /*
1927          * drop non-data frames, WDS frames
1928          */
1929         /* load the lower byte of the frame control field */
1930         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1931         /* mask off QoS bit */
1932         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
1933         /* drop non-data frames */
1934         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
1935         /* load the upper byte of the frame control field */
1936         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1937         /* mask off toDS/fromDS */
1938         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
1939         /* drop WDS frames */
1940         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, FAIL, 0),
1941 #endif
1942
1943         /*
1944          * add header length to index
1945          */
1946         /* load the lower byte of the frame control field */
1947         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1948         /* mask off QoS bit */
1949         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
1950         /* right shift it by 6 to give 0 or 2 */
1951         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
1952         /* add data frame header length */
1953         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
1954         /* add index, was start of 802.11 header */
1955         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
1956         /* move to index, now start of LL header */
1957         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1958
1959         /*
1960          * Accept empty data frames, we use those for
1961          * polling activity.
1962          */
1963         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
1964         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
1965
1966         /*
1967          * Accept EAPOL frames
1968          */
1969         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
1970         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
1971         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
1972         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
1973
1974         /* keep these last two statements or change the code below */
1975         /* return 0 == "DROP" */
1976         BPF_STMT(BPF_RET | BPF_K, 0),
1977         /* return ~0 == "keep all" */
1978         BPF_STMT(BPF_RET | BPF_K, ~0),
1979 };
1980
1981 static struct sock_fprog msock_filter = {
1982         .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
1983         .filter = msock_filter_insns,
1984 };
1985
1986
1987 static int add_monitor_filter(int s)
1988 {
1989         int idx;
1990
1991         /* rewrite all PASS/FAIL jump offsets */
1992         for (idx = 0; idx < msock_filter.len; idx++) {
1993                 struct sock_filter *insn = &msock_filter_insns[idx];
1994
1995                 if (BPF_CLASS(insn->code) == BPF_JMP) {
1996                         if (insn->code == (BPF_JMP|BPF_JA)) {
1997                                 if (insn->k == PASS)
1998                                         insn->k = msock_filter.len - idx - 2;
1999                                 else if (insn->k == FAIL)
2000                                         insn->k = msock_filter.len - idx - 3;
2001                         }
2002
2003                         if (insn->jt == PASS)
2004                                 insn->jt = msock_filter.len - idx - 2;
2005                         else if (insn->jt == FAIL)
2006                                 insn->jt = msock_filter.len - idx - 3;
2007
2008                         if (insn->jf == PASS)
2009                                 insn->jf = msock_filter.len - idx - 2;
2010                         else if (insn->jf == FAIL)
2011                                 insn->jf = msock_filter.len - idx - 3;
2012                 }
2013         }
2014
2015         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2016                        &msock_filter, sizeof(msock_filter))) {
2017                 perror("SO_ATTACH_FILTER");
2018                 return -1;
2019         }
2020
2021         return 0;
2022 }
2023
2024
2025 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2026 {
2027         char buf[IFNAMSIZ];
2028         struct sockaddr_ll ll;
2029         int optval;
2030         socklen_t optlen;
2031
2032         snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2033         buf[IFNAMSIZ - 1] = '\0';
2034
2035         drv->monitor_ifidx =
2036                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2037
2038         if (drv->monitor_ifidx < 0)
2039                 return -1;
2040
2041         if (hostapd_set_iface_flags(drv, buf, 1))
2042                 goto error;
2043
2044         memset(&ll, 0, sizeof(ll));
2045         ll.sll_family = AF_PACKET;
2046         ll.sll_ifindex = drv->monitor_ifidx;
2047         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2048         if (drv->monitor_sock < 0) {
2049                 perror("socket[PF_PACKET,SOCK_RAW]");
2050                 goto error;
2051         }
2052
2053         if (add_monitor_filter(drv->monitor_sock)) {
2054                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2055                            "interface; do filtering in user space");
2056                 /* This works, but will cost in performance. */
2057         }
2058
2059         if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2060                  sizeof(ll)) < 0) {
2061                 perror("monitor socket bind");
2062                 goto error;
2063         }
2064
2065         optlen = sizeof(optval);
2066         optval = 20;
2067         if (setsockopt
2068             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2069                 perror("Failed to set socket priority");
2070                 goto error;
2071         }
2072
2073         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2074                                      drv, NULL)) {
2075                 printf("Could not register monitor read socket\n");
2076                 goto error;
2077         }
2078
2079         return 0;
2080  error:
2081         nl80211_remove_iface(drv, drv->monitor_ifidx);
2082         return -1;
2083 }
2084
2085
2086 static int nl80211_set_master_mode(struct i802_driver_data *drv,
2087                                    const char *ifname)
2088 {
2089         struct nl_msg *msg;
2090         int ret = -ENOBUFS;
2091
2092         msg = nlmsg_alloc();
2093         if (!msg)
2094                 return -ENOMEM;
2095
2096         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2097                     0, NL80211_CMD_SET_INTERFACE, 0);
2098         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2099                     if_nametoindex(ifname));
2100         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
2101
2102         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2103         if (!ret)
2104                 return 0;
2105  nla_put_failure:
2106         wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2107                    "mode.", ifname);
2108         return ret;
2109 }
2110
2111
2112 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2113 {
2114         struct ifreq ifr;
2115         struct sockaddr_ll addr;
2116
2117         drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2118         if (drv->ioctl_sock < 0) {
2119                 perror("socket[PF_INET,SOCK_DGRAM]");
2120                 return -1;
2121         }
2122
2123         /* start listening for EAPOL on the default AP interface */
2124         add_ifidx(drv, if_nametoindex(drv->iface));
2125
2126         if (hostapd_set_iface_flags(drv, drv->iface, 0))
2127                 return -1;
2128
2129         if (bssid) {
2130                 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2131                 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2132                 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2133
2134                 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2135                         perror("ioctl(SIOCSIFHWADDR)");
2136                         return -1;
2137                 }
2138         }
2139
2140         /*
2141          * initialise generic netlink and nl80211
2142          */
2143         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2144         if (!drv->nl_cb) {
2145                 printf("Failed to allocate netlink callbacks.\n");
2146                 return -1;
2147         }
2148
2149         drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2150         if (!drv->nl_handle) {
2151                 printf("Failed to allocate netlink handle.\n");
2152                 return -1;
2153         }
2154
2155         if (genl_connect(drv->nl_handle)) {
2156                 printf("Failed to connect to generic netlink.\n");
2157                 return -1;
2158         }
2159
2160 #ifdef CONFIG_LIBNL20
2161         if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2162                 printf("Failed to allocate generic netlink cache.\n");
2163                 return -1;
2164         }
2165 #else /* CONFIG_LIBNL20 */
2166         drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2167         if (!drv->nl_cache) {
2168                 printf("Failed to allocate generic netlink cache.\n");
2169                 return -1;
2170         }
2171 #endif /* CONFIG_LIBNL20 */
2172
2173         drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2174         if (!drv->nl80211) {
2175                 printf("nl80211 not found.\n");
2176                 return -1;
2177         }
2178
2179         /* Initialise a monitor interface */
2180         if (nl80211_create_monitor_interface(drv))
2181                 return -1;
2182
2183         if (nl80211_set_master_mode(drv, drv->iface))
2184                 goto fail1;
2185
2186         if (hostapd_set_iface_flags(drv, drv->iface, 1))
2187                 goto fail1;
2188
2189         memset(&addr, 0, sizeof(addr));
2190         addr.sll_family = AF_PACKET;
2191         addr.sll_ifindex = ifr.ifr_ifindex;
2192         wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2193                    addr.sll_ifindex);
2194
2195         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2196         if (drv->eapol_sock < 0) {
2197                 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2198                 goto fail1;
2199         }
2200
2201         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2202         {
2203                 printf("Could not register read socket for eapol\n");
2204                 return -1;
2205         }
2206
2207         memset(&ifr, 0, sizeof(ifr));
2208         os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2209         if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2210                 perror("ioctl(SIOCGIFHWADDR)");
2211                 goto fail1;
2212         }
2213
2214         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2215                 printf("Invalid HW-addr family 0x%04x\n",
2216                        ifr.ifr_hwaddr.sa_family);
2217                 goto fail1;
2218         }
2219         memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2220
2221         return 0;
2222
2223 fail1:
2224         nl80211_remove_iface(drv, drv->monitor_ifidx);
2225         return -1;
2226 }
2227
2228
2229 static int i802_get_inact_sec(void *priv, const u8 *addr)
2230 {
2231         struct hostap_sta_driver_data data;
2232         int ret;
2233
2234         data.inactive_msec = (unsigned long) -1;
2235         ret = i802_read_sta_data(priv, &data, addr);
2236         if (ret || data.inactive_msec == (unsigned long) -1)
2237                 return -1;
2238         return data.inactive_msec / 1000;
2239 }
2240
2241
2242 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2243 {
2244 #if 0
2245         /* TODO */
2246 #endif
2247         return 0;
2248 }
2249
2250
2251 static void
2252 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2253                                        char *custom)
2254 {
2255         wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2256
2257         if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2258                 char *pos;
2259                 u8 addr[ETH_ALEN];
2260                 pos = strstr(custom, "addr=");
2261                 if (pos == NULL) {
2262                         wpa_printf(MSG_DEBUG,
2263                                    "MLME-MICHAELMICFAILURE.indication "
2264                                    "without sender address ignored");
2265                         return;
2266                 }
2267                 pos += 5;
2268                 if (hwaddr_aton(pos, addr) == 0) {
2269                         hostapd_michael_mic_failure(drv->hapd, addr);
2270                 } else {
2271                         wpa_printf(MSG_DEBUG,
2272                                    "MLME-MICHAELMICFAILURE.indication "
2273                                    "with invalid MAC address");
2274                 }
2275         }
2276 }
2277
2278
2279 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2280                                             char *data, int len)
2281 {
2282         struct iw_event iwe_buf, *iwe = &iwe_buf;
2283         char *pos, *end, *custom, *buf;
2284
2285         pos = data;
2286         end = data + len;
2287
2288         while (pos + IW_EV_LCP_LEN <= end) {
2289                 /* Event data may be unaligned, so make a local, aligned copy
2290                  * before processing. */
2291                 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2292                 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2293                            iwe->cmd, iwe->len);
2294                 if (iwe->len <= IW_EV_LCP_LEN)
2295                         return;
2296
2297                 custom = pos + IW_EV_POINT_LEN;
2298                 if (drv->we_version > 18 &&
2299                     (iwe->cmd == IWEVMICHAELMICFAILURE ||
2300                      iwe->cmd == IWEVCUSTOM)) {
2301                         /* WE-19 removed the pointer from struct iw_point */
2302                         char *dpos = (char *) &iwe_buf.u.data.length;
2303                         int dlen = dpos - (char *) &iwe_buf;
2304                         memcpy(dpos, pos + IW_EV_LCP_LEN,
2305                                sizeof(struct iw_event) - dlen);
2306                 } else {
2307                         memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2308                         custom += IW_EV_POINT_OFF;
2309                 }
2310
2311                 switch (iwe->cmd) {
2312                 case IWEVCUSTOM:
2313                         if (custom + iwe->u.data.length > end)
2314                                 return;
2315                         buf = malloc(iwe->u.data.length + 1);
2316                         if (buf == NULL)
2317                                 return;
2318                         memcpy(buf, custom, iwe->u.data.length);
2319                         buf[iwe->u.data.length] = '\0';
2320                         hostapd_wireless_event_wireless_custom(drv, buf);
2321                         free(buf);
2322                         break;
2323                 }
2324
2325                 pos += iwe->len;
2326         }
2327 }
2328
2329
2330 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2331                                                struct nlmsghdr *h, int len)
2332 {
2333         struct ifinfomsg *ifi;
2334         int attrlen, nlmsg_len, rta_len;
2335         struct rtattr *attr;
2336
2337         if (len < (int) sizeof(*ifi))
2338                 return;
2339
2340         ifi = NLMSG_DATA(h);
2341
2342         /* TODO: use ifi->ifi_index to filter out wireless events from other
2343          * interfaces */
2344
2345         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2346
2347         attrlen = h->nlmsg_len - nlmsg_len;
2348         if (attrlen < 0)
2349                 return;
2350
2351         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2352
2353         rta_len = RTA_ALIGN(sizeof(struct rtattr));
2354         while (RTA_OK(attr, attrlen)) {
2355                 if (attr->rta_type == IFLA_WIRELESS) {
2356                         hostapd_wireless_event_wireless(
2357                                 drv, ((char *) attr) + rta_len,
2358                                 attr->rta_len - rta_len);
2359                 }
2360                 attr = RTA_NEXT(attr, attrlen);
2361         }
2362 }
2363
2364
2365 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2366                                            void *sock_ctx)
2367 {
2368         char buf[256];
2369         int left;
2370         struct sockaddr_nl from;
2371         socklen_t fromlen;
2372         struct nlmsghdr *h;
2373         struct i802_driver_data *drv = eloop_ctx;
2374
2375         fromlen = sizeof(from);
2376         left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2377                         (struct sockaddr *) &from, &fromlen);
2378         if (left < 0) {
2379                 if (errno != EINTR && errno != EAGAIN)
2380                         perror("recvfrom(netlink)");
2381                 return;
2382         }
2383
2384         h = (struct nlmsghdr *) buf;
2385         while (left >= (int) sizeof(*h)) {
2386                 int len, plen;
2387
2388                 len = h->nlmsg_len;
2389                 plen = len - sizeof(*h);
2390                 if (len > left || plen < 0) {
2391                         printf("Malformed netlink message: "
2392                                "len=%d left=%d plen=%d\n",
2393                                len, left, plen);
2394                         break;
2395                 }
2396
2397                 switch (h->nlmsg_type) {
2398                 case RTM_NEWLINK:
2399                         hostapd_wireless_event_rtm_newlink(drv, h, plen);
2400                         break;
2401                 }
2402
2403                 len = NLMSG_ALIGN(len);
2404                 left -= len;
2405                 h = (struct nlmsghdr *) ((char *) h + len);
2406         }
2407
2408         if (left > 0) {
2409                 printf("%d extra bytes in the end of netlink message\n", left);
2410         }
2411 }
2412
2413
2414 static int hostap_get_we_version(struct i802_driver_data *drv)
2415 {
2416         struct iw_range *range;
2417         struct iwreq iwr;
2418         int minlen;
2419         size_t buflen;
2420
2421         drv->we_version = 0;
2422
2423         /*
2424          * Use larger buffer than struct iw_range in order to allow the
2425          * structure to grow in the future.
2426          */
2427         buflen = sizeof(struct iw_range) + 500;
2428         range = os_zalloc(buflen);
2429         if (range == NULL)
2430                 return -1;
2431
2432         memset(&iwr, 0, sizeof(iwr));
2433         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2434         iwr.u.data.pointer = (caddr_t) range;
2435         iwr.u.data.length = buflen;
2436
2437         minlen = ((char *) &range->enc_capa) - (char *) range +
2438                 sizeof(range->enc_capa);
2439
2440         if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2441                 perror("ioctl[SIOCGIWRANGE]");
2442                 free(range);
2443                 return -1;
2444         } else if (iwr.u.data.length >= minlen &&
2445                    range->we_version_compiled >= 18) {
2446                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2447                            "WE(source)=%d enc_capa=0x%x",
2448                            range->we_version_compiled,
2449                            range->we_version_source,
2450                            range->enc_capa);
2451                 drv->we_version = range->we_version_compiled;
2452         }
2453
2454         free(range);
2455         return 0;
2456 }
2457
2458
2459 static int i802_wireless_event_init(void *priv)
2460 {
2461         struct i802_driver_data *drv = priv;
2462         int s;
2463         struct sockaddr_nl local;
2464
2465         hostap_get_we_version(drv);
2466
2467         drv->wext_sock = -1;
2468
2469         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2470         if (s < 0) {
2471                 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2472                 return -1;
2473         }
2474
2475         memset(&local, 0, sizeof(local));
2476         local.nl_family = AF_NETLINK;
2477         local.nl_groups = RTMGRP_LINK;
2478         if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2479                 perror("bind(netlink)");
2480                 close(s);
2481                 return -1;
2482         }
2483
2484         eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2485                                  NULL);
2486         drv->wext_sock = s;
2487
2488         return 0;
2489 }
2490
2491
2492 static void i802_wireless_event_deinit(void *priv)
2493 {
2494         struct i802_driver_data *drv = priv;
2495         if (drv->wext_sock < 0)
2496                 return;
2497         eloop_unregister_read_sock(drv->wext_sock);
2498         close(drv->wext_sock);
2499 }
2500
2501
2502 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2503 {
2504         struct i802_driver_data *drv = priv;
2505         struct ieee80211_mgmt mgmt;
2506
2507         memset(&mgmt, 0, sizeof(mgmt));
2508         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2509                                           WLAN_FC_STYPE_DEAUTH);
2510         memcpy(mgmt.da, addr, ETH_ALEN);
2511         memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2512         memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2513         mgmt.u.deauth.reason_code = host_to_le16(reason);
2514         return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2515                                       sizeof(mgmt.u.deauth), 0);
2516 }
2517
2518
2519 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2520 {
2521         struct i802_driver_data *drv = priv;
2522         struct ieee80211_mgmt mgmt;
2523
2524         memset(&mgmt, 0, sizeof(mgmt));
2525         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2526                                           WLAN_FC_STYPE_DISASSOC);
2527         memcpy(mgmt.da, addr, ETH_ALEN);
2528         memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2529         memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2530         mgmt.u.disassoc.reason_code = host_to_le16(reason);
2531         return  i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2532                                        sizeof(mgmt.u.disassoc), 0);
2533 }
2534
2535
2536 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2537 {
2538         struct i802_driver_data *drv;
2539
2540         drv = os_zalloc(sizeof(struct i802_driver_data));
2541         if (drv == NULL) {
2542                 printf("Could not allocate memory for i802 driver data\n");
2543                 return NULL;
2544         }
2545
2546         drv->hapd = hapd;
2547         memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2548
2549         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2550         drv->if_indices = drv->default_if_indices;
2551         drv->bridge = if_nametoindex(hapd->conf->bridge);
2552
2553         if (i802_init_sockets(drv, bssid))
2554                 goto failed;
2555
2556         return drv;
2557
2558 failed:
2559         free(drv);
2560         return NULL;
2561 }
2562
2563
2564 static void *i802_init(struct hostapd_data *hapd)
2565 {
2566         return i802_init_bssid(hapd, NULL);
2567 }
2568
2569
2570 static void i802_deinit(void *priv)
2571 {
2572         struct i802_driver_data *drv = priv;
2573
2574         if (drv->last_freq_ht) {
2575                 /* Clear HT flags from the driver */
2576                 struct hostapd_freq_params freq;
2577                 os_memset(&freq, 0, sizeof(freq));
2578                 freq.freq = drv->last_freq;
2579                 i802_set_freq2(priv, &freq);
2580         }
2581
2582         i802_del_beacon(drv);
2583
2584         /* remove monitor interface */
2585         nl80211_remove_iface(drv, drv->monitor_ifidx);
2586
2587         (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2588
2589         if (drv->monitor_sock >= 0) {
2590                 eloop_unregister_read_sock(drv->monitor_sock);
2591                 close(drv->monitor_sock);
2592         }
2593         if (drv->ioctl_sock >= 0)
2594                 close(drv->ioctl_sock);
2595         if (drv->eapol_sock >= 0) {
2596                 eloop_unregister_read_sock(drv->eapol_sock);
2597                 close(drv->eapol_sock);
2598         }
2599
2600         genl_family_put(drv->nl80211);
2601         nl_cache_free(drv->nl_cache);
2602         nl_handle_destroy(drv->nl_handle);
2603         nl_cb_put(drv->nl_cb);
2604
2605         if (drv->if_indices != drv->default_if_indices)
2606                 free(drv->if_indices);
2607
2608         free(drv);
2609 }
2610
2611
2612 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2613         .name = "nl80211",
2614         .init = i802_init,
2615         .init_bssid = i802_init_bssid,
2616         .deinit = i802_deinit,
2617         .wireless_event_init = i802_wireless_event_init,
2618         .wireless_event_deinit = i802_wireless_event_deinit,
2619         .set_ieee8021x = i802_set_ieee8021x,
2620         .set_privacy = i802_set_privacy,
2621         .set_encryption = i802_set_encryption,
2622         .get_seqnum = i802_get_seqnum,
2623         .flush = i802_flush,
2624         .read_sta_data = i802_read_sta_data,
2625         .send_eapol = i802_send_eapol,
2626         .sta_set_flags = i802_sta_set_flags,
2627         .sta_deauth = i802_sta_deauth,
2628         .sta_disassoc = i802_sta_disassoc,
2629         .sta_remove = i802_sta_remove,
2630         .send_mgmt_frame = i802_send_mgmt_frame,
2631         .sta_add2 = i802_sta_add2,
2632         .get_inact_sec = i802_get_inact_sec,
2633         .sta_clear_stats = i802_sta_clear_stats,
2634         .set_freq2 = i802_set_freq2,
2635         .set_rts = i802_set_rts,
2636         .get_rts = i802_get_rts,
2637         .set_frag = i802_set_frag,
2638         .get_frag = i802_get_frag,
2639         .set_retry = i802_set_retry,
2640         .get_retry = i802_get_retry,
2641         .set_rate_sets = i802_set_rate_sets,
2642         .set_beacon = i802_set_beacon,
2643         .set_internal_bridge = i802_set_internal_bridge,
2644         .set_beacon_int = i802_set_beacon_int,
2645         .set_dtim_period = i802_set_dtim_period,
2646         .set_cts_protect = i802_set_cts_protect,
2647         .set_preamble = i802_set_preamble,
2648         .set_short_slot_time = i802_set_short_slot_time,
2649         .set_tx_queue_params = i802_set_tx_queue_params,
2650         .bss_add = i802_bss_add,
2651         .bss_remove = i802_bss_remove,
2652         .if_add = i802_if_add,
2653         .if_update = i802_if_update,
2654         .if_remove = i802_if_remove,
2655         .get_hw_feature_data = i802_get_hw_feature_data,
2656         .set_sta_vlan = i802_set_sta_vlan,
2657         .set_country = i802_set_country,
2658 };