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