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