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