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