forked from pritambaral/hostapd-rtl871xdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_rtw.c
1993 lines (1579 loc) · 49.5 KB
/
driver_rtw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* hostapd / Driver interface for rtl871x driver
* Copyright (c) 2010,
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
//#define CONFIG_MGNT_L2SOCK 1
#define CONFIG_MLME_OFFLOAD 1
#include "includes.h"
#include <net/if.h>
#include <sys/ioctl.h>
#include "common.h"
/*#include "wireless_copy.h"*/
#include "linux_wext.h"
#include "driver.h"
#include "eloop.h"
#include "priv_netlink.h"
#include "l2_packet/l2_packet.h"
#include "common/ieee802_11_defs.h"
#include "netlink.h"
#include "linux_ioctl.h"
//#include "../src/ap/hostapd.h"
//#include "../src/ap/ap_config.h"
#include "ap/hostapd.h"
#include "ap/ap_config.h"
#ifdef USE_KERNEL_HEADERS
/* compat-wireless does not include linux/compiler.h to define __user, so
* define it here */
#ifndef __user
#define __user
#endif /* __user */
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#include <linux/if_arp.h>
#include <linux/wireless.h>
#else /* USE_KERNEL_HEADERS */
#include <net/if_arp.h>
#include <netpacket/packet.h>
//#include "wireless_copy.h"
#endif /* USE_KERNEL_HEADERS */
//#include <net/if.h>
#ifndef ETH_P_80211_RAW
#define ETH_P_80211_RAW 0x0019
#endif
#if 0
#include "hostapd.h"
#include "driver.h"
#include "ieee802_1x.h"
#include "eloop.h"
#include "priv_netlink.h"
#include "sta_info.h"
#include "l2_packet/l2_packet.h"
#include "wpa.h"
#include "accounting.h"
#include "ieee802_11.h"
#include "hw_features.h"
#include "radius/radius.h"
#endif
#include "driver_rtl.h"
//static int rtl871x_sta_remove_ops(void *priv, const u8 *addr);
struct rtl871x_driver_data {
struct hostapd_data *hapd;
char iface[IFNAMSIZ + 1];
int ifindex;
struct l2_packet_data *l2_sock;/* socket for sending eapol frames*/
struct l2_packet_data *l2_sock_recv;/* raw packet recv socket from bridge interface*/
#ifdef CONFIG_MGNT_L2SOCK
struct l2_packet_data *mgnt_l2_sock; /* socket for tx/rx management frames*/
#else
int mgnt_sock;/* socket for tx/rx management frames*/
#endif
int ioctl_sock; /* socket for ioctl() use */
int wext_sock; /* socket for wireless events */
struct netlink_data *netlink;
int we_version;
u8 hw_mac[ETH_ALEN];
u8 acct_mac[ETH_ALEN];
struct hostap_sta_driver_data acct_data;
};
/*
static const char *ether_sprintf(const u8 *addr)
{
static char buf[sizeof(MACSTR)];
if (addr != NULL)
snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
else
snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
return buf;
}
*/
#ifndef CONFIG_MLME_OFFLOAD
static int rtl871x_set_iface_flags(void *priv, int dev_up)
{
struct rtl871x_driver_data *drv = priv;
struct ifreq ifr;
wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
if (drv->mgnt_sock < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
//os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
//os_strlcpy(ifr.ifr_name, "mgnt.wlan", IFNAMSIZ);
snprintf(ifr.ifr_name, IFNAMSIZ, "mgnt.%s", "wlan0");
if (ioctl(drv->mgnt_sock, SIOCGIFFLAGS, &ifr) != 0) {
perror("ioctl[SIOCGIFFLAGS]");
return -1;
}
if (dev_up)
ifr.ifr_flags |= IFF_UP;
else
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(drv->mgnt_sock, SIOCSIFFLAGS, &ifr) != 0) {
perror("ioctl[SIOCSIFFLAGS]");
return -1;
}
#if 0
if (dev_up) {
memset(&ifr, 0, sizeof(ifr));
os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
ifr.ifr_mtu = HOSTAPD_MTU;
if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
perror("ioctl[SIOCSIFMTU]");
printf("Setting MTU failed - trying to survive with "
"current value\n");
}
}
#endif
return 0;
}
#endif
static int rtl871x_hostapd_ioctl(struct rtl871x_driver_data *drv, ieee_param *param, int len)
{
struct iwreq iwr;
memset(&iwr, 0, sizeof(iwr));
os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
iwr.u.data.pointer = (caddr_t) param;
iwr.u.data.length = len;
if (ioctl(drv->ioctl_sock, RTL_IOCTL_HOSTAPD, &iwr) < 0) {
perror("ioctl[RTL_IOCTL_HOSTAPD]");
return -1;
}
return 0;
}
static int rtl871x_set_mode(struct rtl871x_driver_data *drv, u32 mode)
{
struct iwreq iwr;
if (drv->ioctl_sock < 0)
return -1;
memset(&iwr, 0, sizeof(iwr));
os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
//iwr.u.mode = IW_MODE_MASTER;
iwr.u.mode = mode;
if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
perror("ioctl[SIOCSIWMODE]");
printf("Could not set interface to mode(%d)!\n", mode);
return -1;
}
return 0;
}
/*
static int rtl871x_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
const u8 *ie, size_t ielen)
{
struct sta_info *sta;
int new_assoc, res;
//hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
// HOSTAPD_LEVEL_INFO, "associated");
sta = ap_get_sta(hapd, addr);
if (sta) {
accounting_sta_stop(hapd, sta);
} else {
sta = ap_sta_add(hapd, addr);
if (sta == NULL)
{
rtl871x_sta_remove_ops(hapd->drv_priv, addr);
return -1;
}
}
sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
if (hapd->conf->wpa) {
if (ie == NULL || ielen == 0) {
if (hapd->conf->wps_state) {
wpa_printf(MSG_DEBUG, "STA did not include "
"WPA/RSN IE in (Re)Association "
"Request - possible WPS use");
sta->flags |= WLAN_STA_MAYBE_WPS;
goto skip_wpa_check;
}
wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
return -1;
}
if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
sta->flags |= WLAN_STA_WPS;
goto skip_wpa_check;
}
if (sta->wpa_sm == NULL)
sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
sta->addr);
if (sta->wpa_sm == NULL) {
wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
"machine");
return -1;
}
res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
ie, ielen, NULL, 0);
if (res != WPA_IE_OK) {
wpa_printf(MSG_DEBUG, "WPA/RSN information element "
"rejected? (res %u)", res);
wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
return -1;
}
} else if (hapd->conf->wps_state) {
if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
sta->flags |= WLAN_STA_WPS;
} else
sta->flags |= WLAN_STA_MAYBE_WPS;
}
skip_wpa_check:
new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
hostapd_new_assoc_sta(hapd, sta, !new_assoc);
ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
return 0;
}
*/
static int rtl871x_get_sta_wpaie(struct rtl871x_driver_data *drv, u8 *iebuf, u8 *addr)
{
struct ieee_param param;
printf("+%s, " MACSTR " is sta's address\n", __func__, MAC2STR(addr));
memset(¶m, 0, sizeof(param));
param.cmd = RTL871X_HOSTAPD_GET_WPAIE_STA;
memcpy(param.sta_addr, addr, ETH_ALEN);
if (rtl871x_hostapd_ioctl(drv, ¶m, sizeof(param))) {
printf("Could not get sta wpaie from kernel driver.\n");
return -1;
}
if(param.u.wpa_ie.len > 32)
return -1;
memcpy(iebuf, param.u.wpa_ie.reserved, param.u.wpa_ie.len);
return 0;
}
static int rtl871x_del_sta(struct rtl871x_driver_data *drv, u8 *addr)
{
struct hostapd_data *hapd = drv->hapd;
#if 1
//union wpa_event_data event;
//os_memset(&event, 0, sizeof(event));
//event.disassoc_info.addr = addr;
//wpa_supplicant_event(hapd, EVENT_DISASSOC, &event);
drv_event_disassoc(hapd, addr);
#else
struct sta_info *sta;
//hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
// HOSTAPD_LEVEL_INFO, "disassociated");
sta = ap_get_sta(hapd, addr);
if (sta != NULL)
{
sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
ap_free_sta(hapd, sta);
}
else
{
wpa_printf(MSG_DEBUG, "Disassociation notification for "
"unknown STA " MACSTR, MAC2STR(addr));
}
#endif
return 0;
}
static int rtl871x_new_sta(struct rtl871x_driver_data *drv, u8 *addr)
{
struct hostapd_data *hapd = drv->hapd;
//struct ieee80211req_wpaie ie;
int ielen = 0, res=0;
//u8 *iebuf = NULL;
u8 iebuf[32], *piebuf=NULL;
/*
* Fetch negotiated WPA/RSN parameters from the driver.
*/
//memset(&ie, 0, sizeof(ie));
//memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
memset(iebuf, 0 , sizeof(iebuf));
if (rtl871x_get_sta_wpaie(drv, iebuf, addr)) {
//if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
__func__, strerror(errno));
goto no_ie;
}
//wpa_hexdump(MSG_MSGDUMP, "req WPA IE",
// ie.wpa_ie, IEEE80211_MAX_OPT_IE);
//wpa_hexdump(MSG_MSGDUMP, "req RSN IE",
// ie.rsn_ie, IEEE80211_MAX_OPT_IE);
//iebuf = ie.wpa_ie;
/*
if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
iebuf[1] = 0;
if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
iebuf = ie.rsn_ie;
if (iebuf[0] != WLAN_EID_RSN)
iebuf[1] = 0;
}
*/
if ((iebuf[0] == WLAN_EID_VENDOR_SPECIFIC) || (iebuf[0] == WLAN_EID_RSN) )
{
piebuf = iebuf;
ielen = iebuf[1];
if (ielen == 0)
piebuf = NULL;
else
ielen += 2;
}
no_ie:
//res = rtl871x_notif_assoc(hapd, addr, piebuf, ielen);
//drv_event_assoc(hapd, addr, piebuf, ielen);
drv_event_assoc(hapd, addr, piebuf, ielen, 0);
if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
/* Cached accounting data is not valid anymore. */
memset(drv->acct_mac, 0, ETH_ALEN);
memset(&drv->acct_data, 0, sizeof(drv->acct_data));
}
return res;
}
static void rtl871x_wireless_event_wireless(struct rtl871x_driver_data *drv,
char *data, int len)
{
struct iw_event iwe_buf, *iwe = &iwe_buf;
char *pos, *end, *custom, *buf;
pos = data;
end = data + len;
while (pos + IW_EV_LCP_LEN <= end) {
/* Event data may be unaligned, so make a local, aligned copy
* before processing. */
memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
iwe->cmd, iwe->len);
if (iwe->len <= IW_EV_LCP_LEN)
return;
custom = pos + IW_EV_POINT_LEN;
if (drv->we_version > 18 &&
(iwe->cmd == IWEVMICHAELMICFAILURE ||
iwe->cmd == IWEVCUSTOM)) {
/* WE-19 removed the pointer from struct iw_point */
char *dpos = (char *) &iwe_buf.u.data.length;
int dlen = dpos - (char *) &iwe_buf;
memcpy(dpos, pos + IW_EV_LCP_LEN,
sizeof(struct iw_event) - dlen);
} else {
memcpy(&iwe_buf, pos, sizeof(struct iw_event));
custom += IW_EV_POINT_OFF;
}
//printf("got wireless event, iwe->cmd=%d\n", iwe->cmd);
switch (iwe->cmd) {
case IWEVEXPIRED:
rtl871x_del_sta(drv, (u8 *)iwe->u.addr.sa_data);
break;
case IWEVREGISTERED:
if(rtl871x_new_sta(drv, (u8 *)iwe->u.addr.sa_data))
{
printf("Failed to add new sta: "MACSTR" \n", MAC2STR((u8 *)iwe->u.addr.sa_data));
}
break;
case IWEVCUSTOM:
if (custom + iwe->u.data.length > end)
return;
buf = malloc(iwe->u.data.length + 1);
if (buf == NULL)
return; /* XXX */
memcpy(buf, custom, iwe->u.data.length);
buf[iwe->u.data.length] = '\0';
//madwifi_wireless_event_wireless_custom(drv, buf);
free(buf);
break;
}
pos += iwe->len;
}
}
#if 1
static void rtl871x_wireless_event_rtm_newlink(void *ctx,
struct ifinfomsg *ifi, u8 *buf, size_t len)
{
struct rtl871x_driver_data *drv = ctx;
int attrlen, rta_len;
struct rtattr *attr;
if (ifi->ifi_index != drv->ifindex)
return;
attrlen = len;
attr = (struct rtattr *) buf;
rta_len = RTA_ALIGN(sizeof(struct rtattr));
while (RTA_OK(attr, attrlen)) {
if (attr->rta_type == IFLA_WIRELESS) {
rtl871x_wireless_event_wireless(
drv, ((char *) attr) + rta_len,
attr->rta_len - rta_len);
}
attr = RTA_NEXT(attr, attrlen);
}
}
#else
static void rtl871x_wireless_event_rtm_newlink(struct rtl871x_driver_data *drv,
struct nlmsghdr *h, int len)
{
struct ifinfomsg *ifi;
int attrlen, nlmsg_len, rta_len;
struct rtattr * attr;
if (len < (int) sizeof(*ifi))
return;
ifi = NLMSG_DATA(h);
if (ifi->ifi_index != drv->ifindex)
return;
nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
attrlen = h->nlmsg_len - nlmsg_len;
if (attrlen < 0)
return;
attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
rta_len = RTA_ALIGN(sizeof(struct rtattr));
while (RTA_OK(attr, attrlen)) {
if (attr->rta_type == IFLA_WIRELESS) {
rtl871x_wireless_event_wireless(
drv, ((char *) attr) + rta_len,
attr->rta_len - rta_len);
}
attr = RTA_NEXT(attr, attrlen);
}
}
#endif
/*
static void rtl871x_wireless_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
{
char buf[256];//!!!
int left;
struct sockaddr_nl from;
socklen_t fromlen;
struct nlmsghdr *h;
struct rtl871x_driver_data *drv = eloop_ctx;
//printf("+rtl871x_wireless_event_receive\n");
fromlen = sizeof(from);
left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
(struct sockaddr *) &from, &fromlen);
if (left < 0) {
if (errno != EINTR && errno != EAGAIN)
perror("recvfrom(netlink)");
return;
}
h = (struct nlmsghdr *)buf;
while (left >= (int) sizeof(*h)) {
int len, plen;
len = h->nlmsg_len;
plen = len - sizeof(*h);//payload len
if (len > left || plen < 0) {
printf("Malformed netlink message: "
"len=%d left=%d plen=%d\n",
len, left, plen);
break;
}
switch (h->nlmsg_type) {
case RTM_NEWLINK:
rtl871x_wireless_event_rtm_newlink(drv, h, plen);
break;
}
len = NLMSG_ALIGN(len);
left -= len;
h = (struct nlmsghdr *) ((char *) h + len);
}
if (left > 0) {
printf("%d extra bytes in the end of netlink message\n", left);
}
}
*/
static int rtl871x_wireless_event_init(struct rtl871x_driver_data *drv)
{
struct netlink_config *cfg;
//madwifi_get_we_version(drv);
cfg = os_zalloc(sizeof(*cfg));
if (cfg == NULL)
return -1;
cfg->ctx = drv;
cfg->newlink_cb = rtl871x_wireless_event_rtm_newlink;
drv->netlink = netlink_init(cfg);
if (drv->netlink == NULL) {
os_free(cfg);
return -1;
}
return 0;
}
/*
static int rtl871x_wireless_event_init_ops(void *priv)
{
int s;
struct sockaddr_nl local;
struct rtl871x_driver_data *drv = priv;
//madwifi_get_we_version(drv);
drv->wext_sock = -1;
s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (s < 0) {
perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
return -1;
}
memset(&local, 0, sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_groups = RTMGRP_LINK;
if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
perror("bind(netlink)");
close(s);
return -1;
}
eloop_register_read_sock(s, rtl871x_wireless_event_receive, drv, NULL);
drv->wext_sock = s;
return 0;
}
static void rtl871x_wireless_event_deinit_ops(void *priv)
{
struct rtl871x_driver_data *drv = priv;
if (drv != NULL) {
if (drv->wext_sock < 0)
return;
eloop_unregister_read_sock(drv->wext_sock);
close(drv->wext_sock);
}
}
*/
#if 1
static void rtl871x_handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
{
struct rtl871x_driver_data *drv = ctx;
drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
len - sizeof(struct l2_ethhdr));
}
#else
static void rtl871x_handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
{
struct rtl871x_driver_data *drv = ctx;
struct hostapd_data *hapd = drv->hapd;
struct sta_info *sta;
sta = ap_get_sta(hapd, src_addr);
if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
printf("Data frame from not associated STA %s\n",
ether_sprintf(src_addr));
/* XXX cannot happen */
return;
}
ieee802_1x_receive(hapd, src_addr, buf + sizeof(struct l2_ethhdr),
len - sizeof(struct l2_ethhdr));
}
#endif
static int rtl871x_send_eapol_ops(void *priv, const u8 *addr, const u8 *data, size_t data_len,
int encrypt, const u8 *own_addr, u32 flags)
{
struct rtl871x_driver_data *drv = priv;
unsigned char buf[3000];
unsigned char *bp = buf;
struct l2_ethhdr *eth;
size_t len;
int status;
printf("+rtl871x_send_eapol\n");
/*
* Prepend the Ethernet header. If the caller left us
* space at the front we could just insert it but since
* we don't know we copy to a local buffer. Given the frequency
* and size of frames this probably doesn't matter.
*/
len = data_len + sizeof(struct l2_ethhdr);
if (len > sizeof(buf)) {
bp = malloc(len);
if (bp == NULL) {
printf("EAPOL frame discarded, cannot malloc temp "
"buffer of size %lu!\n", (unsigned long) len);
return -1;
}
}
eth = (struct l2_ethhdr *) bp;
memcpy(eth->h_dest, addr, ETH_ALEN);
memcpy(eth->h_source, own_addr, ETH_ALEN);
eth->h_proto = htons(ETH_P_EAPOL);
memcpy(eth+1, data, data_len);
wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
status = l2_packet_send(drv->l2_sock, addr, ETH_P_EAPOL, bp, len);
if (bp != buf)
free(bp);
return status;
}
#ifndef CONFIG_MLME_OFFLOAD
static void rtl871x_receive_mgnt(struct rtl871x_driver_data *drv , const u8 *buf, size_t len)
{
const struct ieee80211_mgmt *mgmt;
//const u8 *end, *ie;
u16 fc, type, stype;
//size_t ie_len;
struct hostapd_data *hapd = drv->hapd;
//printf("+rtl871x_receive_mgnt, " MACSTR " is our address\n", MAC2STR(hapd->own_addr));
#if 0
{
int i;
for(i=0; i<len; i+=8)
{
printf("%x:%x:%x:%x:%x:%x:%x:%x\n", buf[i], buf[i+1], buf[i+2], buf[i+3], buf[i+4], buf[i+5], buf[i+6], buf[i+7]);
}
}
#endif
if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
return;
mgmt = (const struct ieee80211_mgmt *)buf;
fc = le_to_host16(mgmt->frame_control);
type = WLAN_FC_GET_TYPE(fc);
stype = WLAN_FC_GET_STYPE(fc);
#if 1
if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP)
{
//printf("MGNT Frame - PROBE_RESP Frame\n");
}
#endif
//end = buf + len;
//ie = mgmt->u.probe_req.variable;
//ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
//hostapd_wps_probe_req_rx(drv->hapd, mgmt->sa, ie, ie_len);
switch (type) {
case WLAN_FC_TYPE_MGMT:
if (stype != WLAN_FC_STYPE_BEACON)
wpa_printf(MSG_MSGDUMP, "MGMT");
if (stype == WLAN_FC_STYPE_PROBE_REQ)
{
}
else
{
//printf("rtl871x_receive_mgnt, type=0x%x, stype=0x%x\n", type, stype);
}
//ieee802_11_mgmt(hapd, (u8 *)buf, len, stype, NULL);
break;
case WLAN_FC_TYPE_CTRL:
printf("rtl871x_receive_mgnt, CTRL\n");
break;
case WLAN_FC_TYPE_DATA:
printf("rtl871x_receive_mgnt, DATA\n");
//handle_data(hapd, buf, data_len, stype);
break;
default:
printf("unknown frame type %d\n", type);
break;
}
}
#ifdef CONFIG_MGNT_L2SOCK
static void rtl871x_recvive_mgmt_frame(void *ctx, const u8 *src_addr, const u8 *buf,
size_t len)
{
struct rtl871x_driver_data *drv = ctx;
rtl871x_receive_mgnt(drv, buf, len);
}
#else
static void rtl871x_recvive_mgmt_frame(int sock, void *eloop_ctx, void *sock_ctx)
{
#if 0
int len;
unsigned char buf[1024];
struct hostapd_data *hapd = (struct hostapd_data *)eloop_ctx;
struct rtl871x_driver_data *drv = (struct rtl871x_driver_data *)hapd->drv_priv;
len = recv(sock, buf, sizeof(buf), 0);
if (len < 0) {
perror("recv");
return;
}
rtl871x_receive_mgnt(drv, buf, len);
#endif
}
static int rtl871x_mgnt_sock_init(struct rtl871x_driver_data *drv, const char *name)
{
int sock;
struct ifreq ifr;
struct sockaddr_ll addr;
sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock < 0) {
perror("socket[PF_PACKET,SOCK_RAW]");
return -1;
}
if (eloop_register_read_sock(sock, rtl871x_recvive_mgmt_frame, drv->hapd, NULL))
{
printf("Could not register read socket\n");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
//snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
os_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFINDEX, &ifr) != 0) {
perror("ioctl(SIOCGIFINDEX)");
return -1;
}
//if (rtl871x_set_iface_flags(drv, 1)) {
// return -1;
//}
memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = ifr.ifr_ifindex;
wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
addr.sll_ifindex);
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("bind");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
os_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) {
perror("ioctl(SIOCGIFHWADDR)");
return -1;
}
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
printf("Invalid HW-addr family 0x%04x\n",
ifr.ifr_hwaddr.sa_family);
return -1;
}
//memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
return sock;
}
#endif
#endif
static void rtl871x_handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
int ok)
{
#if 0
struct ieee80211_hdr *hdr;
u16 fc, type, stype;
struct sta_info *sta;
//printf("%s\n", __func__);
hdr = (struct ieee80211_hdr *) buf;
fc = le_to_host16(hdr->frame_control);
type = WLAN_FC_GET_TYPE(fc);
stype = WLAN_FC_GET_STYPE(fc);
switch (type) {
case WLAN_FC_TYPE_MGMT:
//printf("MGMT (TX callback) %s\n",
// ok ? "ACK" : "fail");
ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
break;
case WLAN_FC_TYPE_CTRL:
printf("CTRL (TX callback) %s\n",
ok ? "ACK" : "fail");
break;
case WLAN_FC_TYPE_DATA:
printf("DATA (TX callback) %s\n",
ok ? "ACK" : "fail");
sta = ap_get_sta(hapd, hdr->addr1);
if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
wpa_printf(MSG_DEBUG, "STA " MACSTR
" %s pending activity poll",
MAC2STR(sta->addr),
ok ? "ACKed" : "did not ACK");
if (ok)
sta->flags &= ~WLAN_STA_PENDING_POLL;
}
if (sta)
ieee802_1x_tx_status(hapd, sta, buf, len, ok);
break;
default:
printf("unknown TX callback frame type %d\n", type);
break;
}
#endif
}
static int rtl871x_send_mgnt(struct rtl871x_driver_data *drv, const void *msg, size_t len)
{
int res=0;
return res;
}
static int rtl871x_send_mgmt_frame_ops(void *priv, const void *msg, size_t len,
int flags)
{
struct rtl871x_driver_data *drv = priv;
//struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msg;
int res=0;
//printf("%s\n", __func__);
//hdr->frame_control |= host_to_le16(BIT(1));/* Request TX callback */
#ifdef CONFIG_MGNT_L2SOCK
//res = send(drv->mgnt_l2_sock, msg, len, flags);
//res = l2_packet_send(drv->mgnt_l2_sock, addr, ETH_P_EAPOL, msg, len);
if(drv->mgnt_l2_sock == NULL)
return res;
res = l2_packet_send(drv->mgnt_l2_sock, NULL, ETH_P_80211_RAW, msg, len);
#else
if(drv->mgnt_sock < 0)
return res;
res = send(drv->mgnt_sock, msg, len, flags);
#endif
//hdr->frame_control &= ~host_to_le16(BIT(1));
rtl871x_send_mgnt(drv, msg, len);
rtl871x_handle_tx_callback(drv->hapd, (u8*)msg, len, 1);
return res;
}