driver_nl80211: use Linux socket filter to improve performance
[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                 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1656                            ok ? "ACK" : "fail");
1657                 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1658                 break;
1659         default:
1660                 printf("unknown TX callback frame type %d\n", type);
1661                 break;
1662         }
1663 }
1664
1665
1666 static void handle_frame(struct i802_driver_data *drv,
1667                          struct hostapd_iface *iface, u8 *buf, size_t len,
1668                          struct hostapd_frame_info *hfi,
1669                          enum ieee80211_msg_type msg_type)
1670 {
1671         struct ieee80211_hdr *hdr;
1672         u16 fc, type, stype;
1673         size_t data_len = len;
1674         struct hostapd_data *hapd = NULL;
1675         int broadcast_bssid = 0;
1676         size_t i;
1677         u8 *bssid;
1678
1679         /*
1680          * PS-Poll frames are 16 bytes. All other frames are
1681          * 24 bytes or longer.
1682          */
1683         if (len < 16)
1684                 return;
1685
1686         hdr = (struct ieee80211_hdr *) buf;
1687         fc = le_to_host16(hdr->frame_control);
1688
1689         type = WLAN_FC_GET_TYPE(fc);
1690         stype = WLAN_FC_GET_STYPE(fc);
1691
1692         switch (type) {
1693         case WLAN_FC_TYPE_DATA:
1694                 if (len < 24)
1695                         return;
1696                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1697                 case WLAN_FC_TODS:
1698                         bssid = hdr->addr1;
1699                         break;
1700                 default:
1701                         /* discard */
1702                         return;
1703                 }
1704                 break;
1705         case WLAN_FC_TYPE_CTRL:
1706                 /* discard non-ps-poll frames */
1707                 if (stype != WLAN_FC_STYPE_PSPOLL)
1708                         return;
1709                 bssid = hdr->addr1;
1710                 break;
1711         case WLAN_FC_TYPE_MGMT:
1712                 bssid = hdr->addr3;
1713                 break;
1714         default:
1715                 /* discard */
1716                 return;
1717         }
1718
1719         /* find interface frame belongs to */
1720         for (i = 0; i < iface->num_bss; i++) {
1721                 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1722                         hapd = iface->bss[i];
1723                         break;
1724                 }
1725         }
1726
1727         if (hapd == NULL) {
1728                 hapd = iface->bss[0];
1729
1730                 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1731                     bssid[2] != 0xff || bssid[3] != 0xff ||
1732                     bssid[4] != 0xff || bssid[5] != 0xff) {
1733                         /*
1734                          * Unknown BSSID - drop frame if this is not from
1735                          * passive scanning or a beacon (at least ProbeReq
1736                          * frames to other APs may be allowed through RX
1737                          * filtering in the wlan hw/driver)
1738                          */
1739                         if ((type != WLAN_FC_TYPE_MGMT ||
1740                              stype != WLAN_FC_STYPE_BEACON))
1741                                 return;
1742                 } else
1743                         broadcast_bssid = 1;
1744         }
1745
1746         switch (msg_type) {
1747         case ieee80211_msg_normal:
1748                 /* continue processing */
1749                 break;
1750         case ieee80211_msg_tx_callback_ack:
1751                 handle_tx_callback(hapd, buf, data_len, 1);
1752                 return;
1753         case ieee80211_msg_tx_callback_fail:
1754                 handle_tx_callback(hapd, buf, data_len, 0);
1755                 return;
1756         }
1757
1758         switch (type) {
1759         case WLAN_FC_TYPE_MGMT:
1760                 if (stype != WLAN_FC_STYPE_BEACON &&
1761                     stype != WLAN_FC_STYPE_PROBE_REQ)
1762                         wpa_printf(MSG_MSGDUMP, "MGMT");
1763                 if (broadcast_bssid) {
1764                         for (i = 0; i < iface->num_bss; i++)
1765                                 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1766                                                 stype, hfi);
1767                 } else
1768                         hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1769                 break;
1770         case WLAN_FC_TYPE_CTRL:
1771                 /* can only get here with PS-Poll frames */
1772                 wpa_printf(MSG_DEBUG, "CTRL");
1773                 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1774                 break;
1775         case WLAN_FC_TYPE_DATA:
1776                 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1777                 break;
1778         }
1779 }
1780
1781
1782 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1783 {
1784         struct i802_driver_data *drv = eloop_ctx;
1785         struct hostapd_data *hapd = drv->hapd;
1786         struct sockaddr_ll lladdr;
1787         unsigned char buf[3000];
1788         int len;
1789         socklen_t fromlen = sizeof(lladdr);
1790
1791         len = recvfrom(sock, buf, sizeof(buf), 0,
1792                        (struct sockaddr *)&lladdr, &fromlen);
1793         if (len < 0) {
1794                 perror("recv");
1795                 return;
1796         }
1797
1798         if (have_ifidx(drv, lladdr.sll_ifindex))
1799                 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1800 }
1801
1802
1803 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1804 {
1805         struct i802_driver_data *drv = eloop_ctx;
1806         int len;
1807         unsigned char buf[3000];
1808         struct hostapd_data *hapd = drv->hapd;
1809         struct ieee80211_radiotap_iterator iter;
1810         int ret;
1811         struct hostapd_frame_info hfi;
1812         int injected = 0, failed = 0, msg_type, rxflags = 0;
1813
1814         len = recv(sock, buf, sizeof(buf), 0);
1815         if (len < 0) {
1816                 perror("recv");
1817                 return;
1818         }
1819
1820         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1821                 printf("received invalid radiotap frame\n");
1822                 return;
1823         }
1824
1825         memset(&hfi, 0, sizeof(hfi));
1826
1827         while (1) {
1828                 ret = ieee80211_radiotap_iterator_next(&iter);
1829                 if (ret == -ENOENT)
1830                         break;
1831                 if (ret) {
1832                         printf("received invalid radiotap frame (%d)\n", ret);
1833                         return;
1834                 }
1835                 switch (iter.this_arg_index) {
1836                 case IEEE80211_RADIOTAP_FLAGS:
1837                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1838                                 len -= 4;
1839                         break;
1840                 case IEEE80211_RADIOTAP_RX_FLAGS:
1841                         rxflags = 1;
1842                         break;
1843                 case IEEE80211_RADIOTAP_TX_FLAGS:
1844                         injected = 1;
1845                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1846                                         IEEE80211_RADIOTAP_F_TX_FAIL;
1847                         break;
1848                 case IEEE80211_RADIOTAP_DATA_RETRIES:
1849                         break;
1850                 case IEEE80211_RADIOTAP_CHANNEL:
1851                         /* TODO convert from freq/flags to channel number
1852                         hfi.channel = XXX;
1853                         hfi.phytype = XXX;
1854                          */
1855                         break;
1856                 case IEEE80211_RADIOTAP_RATE:
1857                         hfi.datarate = *iter.this_arg * 5;
1858                         break;
1859                 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1860                         hfi.ssi_signal = *iter.this_arg;
1861                         break;
1862                 }
1863         }
1864
1865         if (rxflags && injected)
1866                 return;
1867
1868         if (!injected)
1869                 msg_type = ieee80211_msg_normal;
1870         else if (failed)
1871                 msg_type = ieee80211_msg_tx_callback_fail;
1872         else
1873                 msg_type = ieee80211_msg_tx_callback_ack;
1874
1875         handle_frame(drv, hapd->iface, buf + iter.max_length,
1876                      len - iter.max_length, &hfi, msg_type);
1877 }
1878
1879
1880 /*
1881  * we post-process the filter code later and rewrite
1882  * this to the offset to the last instruction
1883  */
1884 #define PASS    0xFF
1885 #define FAIL    0xFE
1886
1887 static struct sock_filter msock_filter_insns[] = {
1888         /*
1889          * do a little-endian load of the radiotap length field
1890          */
1891         /* load lower byte into A */
1892         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
1893         /* put it into X (== index register) */
1894         BPF_STMT(BPF_MISC| BPF_TAX, 0),
1895         /* load upper byte into A */
1896         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
1897         /* left-shift it by 8 */
1898         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1899         /* or with X */
1900         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1901         /* put result into X */
1902         BPF_STMT(BPF_MISC| BPF_TAX, 0),
1903
1904         /*
1905          * Allow management frames through, this also gives us those
1906          * management frames that we sent ourselves with status
1907          */
1908         /* load the lower byte of the IEEE 802.11 frame control field */
1909         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
1910         /* mask off frame type and version */
1911         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1912         /* accept frame if it's both 0, fall through otherwise */
1913         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1914
1915         /*
1916          * TODO: add a bit to radiotap RX flags that indicates
1917          * that the sending station is not associated, then
1918          * add a filter here that filters on our DA and that flag
1919          * to allow us to deauth frames to that bad station.
1920          *
1921          * Not a regression -- we didn't do it before either.
1922          */
1923
1924 #if 0
1925         /*
1926          * drop non-data frames, WDS frames
1927          */
1928         /* load the lower byte of the frame control field */
1929         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1930         /* mask off QoS bit */
1931         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
1932         /* drop non-data frames */
1933         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
1934         /* load the upper byte of the frame control field */
1935         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1936         /* mask off toDS/fromDS */
1937         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
1938         /* drop WDS frames */
1939         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, FAIL, 0),
1940 #endif
1941
1942         /*
1943          * add header length to index
1944          */
1945         /* load the lower byte of the frame control field */
1946         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
1947         /* mask off QoS bit */
1948         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
1949         /* right shift it by 6 to give 0 or 2 */
1950         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
1951         /* add data frame header length */
1952         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
1953         /* add index, was start of 802.11 header */
1954         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
1955         /* move to index, now start of LL header */
1956         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1957
1958         /*
1959          * Accept empty data frames, we use those for
1960          * polling activity.
1961          */
1962         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
1963         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
1964
1965         /*
1966          * Accept EAPOL frames
1967          */
1968         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
1969         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
1970         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
1971         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
1972
1973         /* keep these last two statements or change the code below */
1974         /* return 0 == "DROP" */
1975         BPF_STMT(BPF_RET | BPF_K, 0),
1976         /* return ~0 == "keep all" */
1977         BPF_STMT(BPF_RET | BPF_K, ~0),
1978 };
1979
1980 static struct sock_fprog msock_filter = {
1981         .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
1982         .filter = msock_filter_insns,
1983 };
1984
1985
1986 static int add_monitor_filter(int s)
1987 {
1988         int idx;
1989
1990         /* rewrite all PASS/FAIL jump offsets */
1991         for (idx = 0; idx < msock_filter.len; idx++) {
1992                 struct sock_filter *insn = &msock_filter_insns[idx];
1993
1994                 if (BPF_CLASS(insn->code) == BPF_JMP) {
1995                         if (insn->code == (BPF_JMP|BPF_JA)) {
1996                                 if (insn->k == PASS)
1997                                         insn->k = msock_filter.len - idx - 2;
1998                                 else if (insn->k == FAIL)
1999                                         insn->k = msock_filter.len - idx - 3;
2000                         }
2001
2002                         if (insn->jt == PASS)
2003                                 insn->jt = msock_filter.len - idx - 2;
2004                         else if (insn->jt == FAIL)
2005                                 insn->jt = msock_filter.len - idx - 3;
2006
2007                         if (insn->jf == PASS)
2008                                 insn->jf = msock_filter.len - idx - 2;
2009                         else if (insn->jf == FAIL)
2010                                 insn->jf = msock_filter.len - idx - 3;
2011                 }
2012         }
2013
2014         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2015                        &msock_filter, sizeof(msock_filter))) {
2016                 perror("SO_ATTACH_FILTER");
2017                 return -1;
2018         }
2019
2020         return 0;
2021 }
2022
2023
2024 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2025 {
2026         char buf[IFNAMSIZ];
2027         struct sockaddr_ll ll;
2028         int optval;
2029         socklen_t optlen;
2030
2031         snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2032         buf[IFNAMSIZ - 1] = '\0';
2033
2034         drv->monitor_ifidx =
2035                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2036
2037         if (drv->monitor_ifidx < 0)
2038                 return -1;
2039
2040         if (hostapd_set_iface_flags(drv, buf, 1))
2041                 goto error;
2042
2043         memset(&ll, 0, sizeof(ll));
2044         ll.sll_family = AF_PACKET;
2045         ll.sll_ifindex = drv->monitor_ifidx;
2046         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2047         if (drv->monitor_sock < 0) {
2048                 perror("socket[PF_PACKET,SOCK_RAW]");
2049                 goto error;
2050         }
2051
2052         if (add_monitor_filter(drv->monitor_sock)) {
2053                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2054                            "interface; do filtering in user space");
2055                 /* This works, but will cost in performance. */
2056         }
2057
2058         if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2059                  sizeof(ll)) < 0) {
2060                 perror("monitor socket bind");
2061                 goto error;
2062         }
2063
2064         optlen = sizeof(optval);
2065         optval = 20;
2066         if (setsockopt
2067             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2068                 perror("Failed to set socket priority");
2069                 goto error;
2070         }
2071
2072         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2073                                      drv, NULL)) {
2074                 printf("Could not register monitor read socket\n");
2075                 goto error;
2076         }
2077
2078         return 0;
2079  error:
2080         nl80211_remove_iface(drv, drv->monitor_ifidx);
2081         return -1;
2082 }
2083
2084
2085 static int nl80211_set_master_mode(struct i802_driver_data *drv,
2086                                    const char *ifname)
2087 {
2088         struct nl_msg *msg;
2089         int ret = -ENOBUFS;
2090
2091         msg = nlmsg_alloc();
2092         if (!msg)
2093                 return -ENOMEM;
2094
2095         genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2096                     0, NL80211_CMD_SET_INTERFACE, 0);
2097         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2098                     if_nametoindex(ifname));
2099         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
2100
2101         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2102         if (!ret)
2103                 return 0;
2104  nla_put_failure:
2105         wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2106                    "mode.", ifname);
2107         return ret;
2108 }
2109
2110
2111 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2112 {
2113         struct ifreq ifr;
2114         struct sockaddr_ll addr;
2115
2116         drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2117         if (drv->ioctl_sock < 0) {
2118                 perror("socket[PF_INET,SOCK_DGRAM]");
2119                 return -1;
2120         }
2121
2122         /* start listening for EAPOL on the default AP interface */
2123         add_ifidx(drv, if_nametoindex(drv->iface));
2124
2125         if (hostapd_set_iface_flags(drv, drv->iface, 0))
2126                 return -1;
2127
2128         if (bssid) {
2129                 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2130                 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2131                 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2132
2133                 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2134                         perror("ioctl(SIOCSIFHWADDR)");
2135                         return -1;
2136                 }
2137         }
2138
2139         /*
2140          * initialise generic netlink and nl80211
2141          */
2142         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2143         if (!drv->nl_cb) {
2144                 printf("Failed to allocate netlink callbacks.\n");
2145                 return -1;
2146         }
2147
2148         drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2149         if (!drv->nl_handle) {
2150                 printf("Failed to allocate netlink handle.\n");
2151                 return -1;
2152         }
2153
2154         if (genl_connect(drv->nl_handle)) {
2155                 printf("Failed to connect to generic netlink.\n");
2156                 return -1;
2157         }
2158
2159 #ifdef CONFIG_LIBNL20
2160         if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2161                 printf("Failed to allocate generic netlink cache.\n");
2162                 return -1;
2163         }
2164 #else /* CONFIG_LIBNL20 */
2165         drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2166         if (!drv->nl_cache) {
2167                 printf("Failed to allocate generic netlink cache.\n");
2168                 return -1;
2169         }
2170 #endif /* CONFIG_LIBNL20 */
2171
2172         drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2173         if (!drv->nl80211) {
2174                 printf("nl80211 not found.\n");
2175                 return -1;
2176         }
2177
2178         /* Initialise a monitor interface */
2179         if (nl80211_create_monitor_interface(drv))
2180                 return -1;
2181
2182         if (nl80211_set_master_mode(drv, drv->iface))
2183                 goto fail1;
2184
2185         if (hostapd_set_iface_flags(drv, drv->iface, 1))
2186                 goto fail1;
2187
2188         memset(&addr, 0, sizeof(addr));
2189         addr.sll_family = AF_PACKET;
2190         addr.sll_ifindex = ifr.ifr_ifindex;
2191         wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2192                    addr.sll_ifindex);
2193
2194         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2195         if (drv->eapol_sock < 0) {
2196                 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2197                 goto fail1;
2198         }
2199
2200         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2201         {
2202                 printf("Could not register read socket for eapol\n");
2203                 return -1;
2204         }
2205
2206         memset(&ifr, 0, sizeof(ifr));
2207         os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2208         if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2209                 perror("ioctl(SIOCGIFHWADDR)");
2210                 goto fail1;
2211         }
2212
2213         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2214                 printf("Invalid HW-addr family 0x%04x\n",
2215                        ifr.ifr_hwaddr.sa_family);
2216                 goto fail1;
2217         }
2218         memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2219
2220         return 0;
2221
2222 fail1:
2223         nl80211_remove_iface(drv, drv->monitor_ifidx);
2224         return -1;
2225 }
2226
2227
2228 static int i802_get_inact_sec(void *priv, const u8 *addr)
2229 {
2230         struct hostap_sta_driver_data data;
2231         int ret;
2232
2233         data.inactive_msec = (unsigned long) -1;
2234         ret = i802_read_sta_data(priv, &data, addr);
2235         if (ret || data.inactive_msec == (unsigned long) -1)
2236                 return -1;
2237         return data.inactive_msec / 1000;
2238 }
2239
2240
2241 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2242 {
2243 #if 0
2244         /* TODO */
2245 #endif
2246         return 0;
2247 }
2248
2249
2250 static void
2251 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2252                                        char *custom)
2253 {
2254         wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2255
2256         if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2257                 char *pos;
2258                 u8 addr[ETH_ALEN];
2259                 pos = strstr(custom, "addr=");
2260                 if (pos == NULL) {
2261                         wpa_printf(MSG_DEBUG,
2262                                    "MLME-MICHAELMICFAILURE.indication "
2263                                    "without sender address ignored");
2264                         return;
2265                 }
2266                 pos += 5;
2267                 if (hwaddr_aton(pos, addr) == 0) {
2268                         hostapd_michael_mic_failure(drv->hapd, addr);
2269                 } else {
2270                         wpa_printf(MSG_DEBUG,
2271                                    "MLME-MICHAELMICFAILURE.indication "
2272                                    "with invalid MAC address");
2273                 }
2274         }
2275 }
2276
2277
2278 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2279                                             char *data, int len)
2280 {
2281         struct iw_event iwe_buf, *iwe = &iwe_buf;
2282         char *pos, *end, *custom, *buf;
2283
2284         pos = data;
2285         end = data + len;
2286
2287         while (pos + IW_EV_LCP_LEN <= end) {
2288                 /* Event data may be unaligned, so make a local, aligned copy
2289                  * before processing. */
2290                 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2291                 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2292                            iwe->cmd, iwe->len);
2293                 if (iwe->len <= IW_EV_LCP_LEN)
2294                         return;
2295
2296                 custom = pos + IW_EV_POINT_LEN;
2297                 if (drv->we_version > 18 &&
2298                     (iwe->cmd == IWEVMICHAELMICFAILURE ||
2299                      iwe->cmd == IWEVCUSTOM)) {
2300                         /* WE-19 removed the pointer from struct iw_point */
2301                         char *dpos = (char *) &iwe_buf.u.data.length;
2302                         int dlen = dpos - (char *) &iwe_buf;
2303                         memcpy(dpos, pos + IW_EV_LCP_LEN,
2304                                sizeof(struct iw_event) - dlen);
2305                 } else {
2306                         memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2307                         custom += IW_EV_POINT_OFF;
2308                 }
2309
2310                 switch (iwe->cmd) {
2311                 case IWEVCUSTOM:
2312                         if (custom + iwe->u.data.length > end)
2313                                 return;
2314                         buf = malloc(iwe->u.data.length + 1);
2315                         if (buf == NULL)
2316                                 return;
2317                         memcpy(buf, custom, iwe->u.data.length);
2318                         buf[iwe->u.data.length] = '\0';
2319                         hostapd_wireless_event_wireless_custom(drv, buf);
2320                         free(buf);
2321                         break;
2322                 }
2323
2324                 pos += iwe->len;
2325         }
2326 }
2327
2328
2329 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2330                                                struct nlmsghdr *h, int len)
2331 {
2332         struct ifinfomsg *ifi;
2333         int attrlen, nlmsg_len, rta_len;
2334         struct rtattr *attr;
2335
2336         if (len < (int) sizeof(*ifi))
2337                 return;
2338
2339         ifi = NLMSG_DATA(h);
2340
2341         /* TODO: use ifi->ifi_index to filter out wireless events from other
2342          * interfaces */
2343
2344         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2345
2346         attrlen = h->nlmsg_len - nlmsg_len;
2347         if (attrlen < 0)
2348                 return;
2349
2350         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2351
2352         rta_len = RTA_ALIGN(sizeof(struct rtattr));
2353         while (RTA_OK(attr, attrlen)) {
2354                 if (attr->rta_type == IFLA_WIRELESS) {
2355                         hostapd_wireless_event_wireless(
2356                                 drv, ((char *) attr) + rta_len,
2357                                 attr->rta_len - rta_len);
2358                 }
2359                 attr = RTA_NEXT(attr, attrlen);
2360         }
2361 }
2362
2363
2364 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2365                                            void *sock_ctx)
2366 {
2367         char buf[256];
2368         int left;
2369         struct sockaddr_nl from;
2370         socklen_t fromlen;
2371         struct nlmsghdr *h;
2372         struct i802_driver_data *drv = eloop_ctx;
2373
2374         fromlen = sizeof(from);
2375         left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2376                         (struct sockaddr *) &from, &fromlen);
2377         if (left < 0) {
2378                 if (errno != EINTR && errno != EAGAIN)
2379                         perror("recvfrom(netlink)");
2380                 return;
2381         }
2382
2383         h = (struct nlmsghdr *) buf;
2384         while (left >= (int) sizeof(*h)) {
2385                 int len, plen;
2386
2387                 len = h->nlmsg_len;
2388                 plen = len - sizeof(*h);
2389                 if (len > left || plen < 0) {
2390                         printf("Malformed netlink message: "
2391                                "len=%d left=%d plen=%d\n",
2392                                len, left, plen);
2393                         break;
2394                 }
2395
2396                 switch (h->nlmsg_type) {
2397                 case RTM_NEWLINK:
2398                         hostapd_wireless_event_rtm_newlink(drv, h, plen);
2399                         break;
2400                 }
2401
2402                 len = NLMSG_ALIGN(len);
2403                 left -= len;
2404                 h = (struct nlmsghdr *) ((char *) h + len);
2405         }
2406
2407         if (left > 0) {
2408                 printf("%d extra bytes in the end of netlink message\n", left);
2409         }
2410 }
2411
2412
2413 static int hostap_get_we_version(struct i802_driver_data *drv)
2414 {
2415         struct iw_range *range;
2416         struct iwreq iwr;
2417         int minlen;
2418         size_t buflen;
2419
2420         drv->we_version = 0;
2421
2422         /*
2423          * Use larger buffer than struct iw_range in order to allow the
2424          * structure to grow in the future.
2425          */
2426         buflen = sizeof(struct iw_range) + 500;
2427         range = os_zalloc(buflen);
2428         if (range == NULL)
2429                 return -1;
2430
2431         memset(&iwr, 0, sizeof(iwr));
2432         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2433         iwr.u.data.pointer = (caddr_t) range;
2434         iwr.u.data.length = buflen;
2435
2436         minlen = ((char *) &range->enc_capa) - (char *) range +
2437                 sizeof(range->enc_capa);
2438
2439         if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2440                 perror("ioctl[SIOCGIWRANGE]");
2441                 free(range);
2442                 return -1;
2443         } else if (iwr.u.data.length >= minlen &&
2444                    range->we_version_compiled >= 18) {
2445                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2446                            "WE(source)=%d enc_capa=0x%x",
2447                            range->we_version_compiled,
2448                            range->we_version_source,
2449                            range->enc_capa);
2450                 drv->we_version = range->we_version_compiled;
2451         }
2452
2453         free(range);
2454         return 0;
2455 }
2456
2457
2458 static int i802_wireless_event_init(void *priv)
2459 {
2460         struct i802_driver_data *drv = priv;
2461         int s;
2462         struct sockaddr_nl local;
2463
2464         hostap_get_we_version(drv);
2465
2466         drv->wext_sock = -1;
2467
2468         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2469         if (s < 0) {
2470                 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2471                 return -1;
2472         }
2473
2474         memset(&local, 0, sizeof(local));
2475         local.nl_family = AF_NETLINK;
2476         local.nl_groups = RTMGRP_LINK;
2477         if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2478                 perror("bind(netlink)");
2479                 close(s);
2480                 return -1;
2481         }
2482
2483         eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2484                                  NULL);
2485         drv->wext_sock = s;
2486
2487         return 0;
2488 }
2489
2490
2491 static void i802_wireless_event_deinit(void *priv)
2492 {
2493         struct i802_driver_data *drv = priv;
2494         if (drv->wext_sock < 0)
2495                 return;
2496         eloop_unregister_read_sock(drv->wext_sock);
2497         close(drv->wext_sock);
2498 }
2499
2500
2501 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2502 {
2503         struct i802_driver_data *drv = priv;
2504         struct ieee80211_mgmt mgmt;
2505
2506         memset(&mgmt, 0, sizeof(mgmt));
2507         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2508                                           WLAN_FC_STYPE_DEAUTH);
2509         memcpy(mgmt.da, addr, ETH_ALEN);
2510         memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2511         memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2512         mgmt.u.deauth.reason_code = host_to_le16(reason);
2513         return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2514                                       sizeof(mgmt.u.deauth), 0);
2515 }
2516
2517
2518 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2519 {
2520         struct i802_driver_data *drv = priv;
2521         struct ieee80211_mgmt mgmt;
2522
2523         memset(&mgmt, 0, sizeof(mgmt));
2524         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2525                                           WLAN_FC_STYPE_DISASSOC);
2526         memcpy(mgmt.da, addr, ETH_ALEN);
2527         memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2528         memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2529         mgmt.u.disassoc.reason_code = host_to_le16(reason);
2530         return  i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2531                                        sizeof(mgmt.u.disassoc), 0);
2532 }
2533
2534
2535 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2536 {
2537         struct i802_driver_data *drv;
2538
2539         drv = os_zalloc(sizeof(struct i802_driver_data));
2540         if (drv == NULL) {
2541                 printf("Could not allocate memory for i802 driver data\n");
2542                 return NULL;
2543         }
2544
2545         drv->hapd = hapd;
2546         memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2547
2548         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2549         drv->if_indices = drv->default_if_indices;
2550         drv->bridge = if_nametoindex(hapd->conf->bridge);
2551
2552         if (i802_init_sockets(drv, bssid))
2553                 goto failed;
2554
2555         return drv;
2556
2557 failed:
2558         free(drv);
2559         return NULL;
2560 }
2561
2562
2563 static void *i802_init(struct hostapd_data *hapd)
2564 {
2565         return i802_init_bssid(hapd, NULL);
2566 }
2567
2568
2569 static void i802_deinit(void *priv)
2570 {
2571         struct i802_driver_data *drv = priv;
2572
2573         if (drv->last_freq_ht) {
2574                 /* Clear HT flags from the driver */
2575                 struct hostapd_freq_params freq;
2576                 os_memset(&freq, 0, sizeof(freq));
2577                 freq.freq = drv->last_freq;
2578                 i802_set_freq2(priv, &freq);
2579         }
2580
2581         i802_del_beacon(drv);
2582
2583         /* remove monitor interface */
2584         nl80211_remove_iface(drv, drv->monitor_ifidx);
2585
2586         (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2587
2588         if (drv->monitor_sock >= 0) {
2589                 eloop_unregister_read_sock(drv->monitor_sock);
2590                 close(drv->monitor_sock);
2591         }
2592         if (drv->ioctl_sock >= 0)
2593                 close(drv->ioctl_sock);
2594         if (drv->eapol_sock >= 0) {
2595                 eloop_unregister_read_sock(drv->eapol_sock);
2596                 close(drv->eapol_sock);
2597         }
2598
2599         genl_family_put(drv->nl80211);
2600         nl_cache_free(drv->nl_cache);
2601         nl_handle_destroy(drv->nl_handle);
2602         nl_cb_put(drv->nl_cb);
2603
2604         if (drv->if_indices != drv->default_if_indices)
2605                 free(drv->if_indices);
2606
2607         free(drv);
2608 }
2609
2610
2611 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2612         .name = "nl80211",
2613         .init = i802_init,
2614         .init_bssid = i802_init_bssid,
2615         .deinit = i802_deinit,
2616         .wireless_event_init = i802_wireless_event_init,
2617         .wireless_event_deinit = i802_wireless_event_deinit,
2618         .set_ieee8021x = i802_set_ieee8021x,
2619         .set_privacy = i802_set_privacy,
2620         .set_encryption = i802_set_encryption,
2621         .get_seqnum = i802_get_seqnum,
2622         .flush = i802_flush,
2623         .read_sta_data = i802_read_sta_data,
2624         .send_eapol = i802_send_eapol,
2625         .sta_set_flags = i802_sta_set_flags,
2626         .sta_deauth = i802_sta_deauth,
2627         .sta_disassoc = i802_sta_disassoc,
2628         .sta_remove = i802_sta_remove,
2629         .send_mgmt_frame = i802_send_mgmt_frame,
2630         .sta_add2 = i802_sta_add2,
2631         .get_inact_sec = i802_get_inact_sec,
2632         .sta_clear_stats = i802_sta_clear_stats,
2633         .set_freq2 = i802_set_freq2,
2634         .set_rts = i802_set_rts,
2635         .get_rts = i802_get_rts,
2636         .set_frag = i802_set_frag,
2637         .get_frag = i802_get_frag,
2638         .set_retry = i802_set_retry,
2639         .get_retry = i802_get_retry,
2640         .set_rate_sets = i802_set_rate_sets,
2641         .set_beacon = i802_set_beacon,
2642         .set_internal_bridge = i802_set_internal_bridge,
2643         .set_beacon_int = i802_set_beacon_int,
2644         .set_dtim_period = i802_set_dtim_period,
2645         .set_cts_protect = i802_set_cts_protect,
2646         .set_preamble = i802_set_preamble,
2647         .set_short_slot_time = i802_set_short_slot_time,
2648         .set_tx_queue_params = i802_set_tx_queue_params,
2649         .bss_add = i802_bss_add,
2650         .bss_remove = i802_bss_remove,
2651         .if_add = i802_if_add,
2652         .if_update = i802_if_update,
2653         .if_remove = i802_if_remove,
2654         .get_hw_feature_data = i802_get_hw_feature_data,
2655         .set_sta_vlan = i802_set_sta_vlan,
2656         .set_country = i802_set_country,
2657 };