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