forked from sysprog21/vwifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vwifi.c
3022 lines (2481 loc) · 95.3 KB
/
vwifi.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
#include <linux/etherdevice.h>
#include <linux/hashtable.h>
#include <linux/hrtimer.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/random.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/version.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ids.h>
#include <linux/workqueue.h>
#include <net/cfg80211.h>
#include <uapi/linux/virtio_net.h>
#include <linux/netlink.h>
#include <net/sock.h>
MODULE_LICENSE("Dual MIT/GPL");
MODULE_AUTHOR("National Cheng Kung University, Taiwan");
MODULE_DESCRIPTION("virtual cfg80211 driver");
#define NAME_PREFIX "vw"
#define NDEV_NAME NAME_PREFIX "%d"
#define VWIFI_WIPHY_NAME_LEN 12
#define VWIFI_WIPHY_PREFIX "vw_phy"
#define DOT11_MGMT_HDR_LEN 24 /* d11 management header len */
#define DOT11_BCN_PRB_FIXED_LEN 12 /* beacon/probe fixed length */
#define MAX_PROBED_SSIDS 69
#define IE_MAX_LEN 512
#define SCAN_TIMEOUT_MS 100 /*< millisecond */
/* Note: vwifi_cipher_suites is an array of int defining which cipher suites
* are supported. A pointer to this array and the number of entries is passed
* on to upper layers.
*/
static const u32 vwifi_cipher_suites[] = {
WLAN_CIPHER_SUITE_WEP40,
WLAN_CIPHER_SUITE_WEP104,
WLAN_CIPHER_SUITE_TKIP,
WLAN_CIPHER_SUITE_CCMP,
};
struct vwifi_packet {
int datalen;
u8 data[ETH_DATA_LEN];
struct list_head list;
};
enum vwifi_state { VWIFI_READY, VWIFI_SHUTDOWN };
/* Context for the whole program, so there's only single vwifi_context
* regardless of the number of virtual interfaces. Fields in the structure
* are interface-independent.
*/
struct vwifi_context {
/* We may not need this lock because vif_list would not change during
* the whole lifetime.
*/
struct mutex lock;
enum vwifi_state state; /**< indicate the program state */
struct list_head vif_list; /**< maintaining all interfaces */
struct list_head ap_list; /**< maintaining multiple AP */
char *blocklist; /**< maintaining the blocklist */
};
static DEFINE_SPINLOCK(vif_list_lock);
/* SME stands for "station management entity" */
enum sme_state { SME_DISCONNECTED, SME_CONNECTING, SME_CONNECTED };
/* Each virtual interface contains a wiphy, vwifi_wiphy_counter is responsible
* for recording the number of wiphy in vwifi.
*/
static atomic_t vwifi_wiphy_counter = ATOMIC_INIT(0);
/* Virtual interface pointed to by netdev_priv(). Fields in the structure are
* interface-dependent. Every interface has its own vwifi_vif, regardless of the
* interface mode (STA, AP, Ad-hoc...).
*/
struct vwifi_vif {
struct wireless_dev wdev;
struct net_device *ndev;
struct net_device_stats stats;
size_t ssid_len;
/* Currently connected BSS id */
u8 bssid[ETH_ALEN];
u8 ssid[IEEE80211_MAX_SSID_LEN];
struct list_head rx_queue; /**< Head of received packet queue */
/* Store all vwifi_vif which is in the same BSS (AP will be the head). */
struct list_head bss_list;
/* List entry for maintaining all vwifi_vif, which can be accessed via
* vwifi->vif_list.
*/
struct list_head list;
struct mutex lock;
/* Split logic for STA and AP mode */
union {
/* Structure for STA mode */
struct {
/* For the case the STA is going to roam to another BSS */
u8 req_ssid[IEEE80211_MAX_SSID_LEN];
struct cfg80211_scan_request *scan_request;
enum sme_state sme_state; /* connection information */
/* last connection time to a AP (in jiffies) */
unsigned long conn_time;
unsigned long active_time; /**< last tx/rx time (in jiffies) */
u16 disconnect_reason_code;
struct timer_list scan_timeout;
struct work_struct ws_connect, ws_disconnect;
struct work_struct ws_scan, ws_scan_timeout;
/* For quickly finding the AP */
struct vwifi_vif *ap;
};
/* Structure for AP mode */
struct {
bool ap_enabled;
bool privacy;
/* List node for storing AP (vwifi->ap_list is the head),
* this field is for interface in AP mode.
*/
struct list_head ap_list;
/* beacon interval in us */
u64 beacon_int;
struct hrtimer beacon_timer;
struct ieee80211_channel *channel;
enum nl80211_chan_width bw;
};
};
struct timer_list scan_complete;
u8 req_bssid[ETH_ALEN];
u32 beacon_ie_len;
u8 beacon_ie[IE_MAX_LEN];
/* Store all STAs in the same BSS, right now only used when virtio enabled
*/
DECLARE_HASHTABLE(bss_sta_table, 4);
/* Don't share the vif->lock because updating bss_sta_table may take a long
* time */
struct mutex bss_sta_table_lock;
u32 bss_sta_table_entry_num;
/* Packet virtio header size */
u8 vnet_hdr_len;
};
static int station = 2;
module_param(station, int, 0444);
MODULE_PARM_DESC(station, "Number of virtual interfaces running in STA mode.");
/* Global context */
static struct vwifi_context *vwifi = NULL;
/* Blocklist content */
#define MAX_BLACKLIST_SIZE 1024
static struct sock *nl_sk = NULL;
static int blocklist_check(char *dest, char *source)
{
if (!vwifi->blocklist || !*(vwifi->blocklist))
return 0;
char *user_input =
kmalloc(sizeof(char) * (strlen(vwifi->blocklist) + 1), GFP_KERNEL);
strncpy(user_input, vwifi->blocklist, strlen(vwifi->blocklist));
char *token = strsep(&user_input, "\n");
while (token) {
char *blacklist_dest = strsep(&token, " ");
strsep(&token, " ");
char *blacklist_source = token;
if (!strcmp(dest, blacklist_dest) &&
!strcmp(source, blacklist_source)) {
kfree(user_input);
return 1;
}
token = strsep(&user_input, "\n");
}
kfree(user_input);
return 0;
}
static void blocklist_load(char *blist)
{
if (!vwifi->blocklist) {
pr_info("vwifi->blocklist have to be kmalloc first\n");
return;
}
memset(vwifi->blocklist, '\0',
MAX_BLACKLIST_SIZE); /* clear the blocklist */
strncpy(vwifi->blocklist, blist, strlen(blist));
}
static void blocklist_nl_recv(struct sk_buff *skb)
{
struct nlmsghdr *nlh; /* netlink message header */
int pid;
struct sk_buff *skb_out;
char *msg = "vwifi has received your blocklist";
int msg_size = strlen(msg);
nlh = (struct nlmsghdr *) skb->data;
blocklist_load((char *) nlmsg_data(nlh));
/* pid of sending process */
pid = nlh->nlmsg_pid;
skb_out = nlmsg_new(msg_size, 0);
if (!skb_out) {
pr_info("netlink: Failed to allocate new skb\n");
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0);
NETLINK_CB(skb_out).dst_group = 0; /* unicast group */
strncpy(nlmsg_data(nlh), msg, msg_size);
if (nlmsg_unicast(nl_sk, skb_out, pid) < 0)
pr_info("netlink: Error while sending back to user\n");
}
static struct netlink_kernel_cfg nl_config = {
.input = blocklist_nl_recv,
};
/**
* enum virtio_vqs - queues for virtio frame transmission and receivement
*
* For virtio-net device, We expect 1 RX virtqueue followed by 1 TX virtqueue,
* followed by possible N-1 RX/TX queue pairs used in multiqueue mode, followed
* by possible control vq. For now, we don't support multiqueue mode virtio-net
* device and control vq as well, so there are only 1 RX vq and 1 TX vq.
*
* @VWIFI_VQ_TX: send frames to external entity
* @VWIFI_VQ_RX: receive frames
* @VWIFI_NUM_VQS: enum limit
*/
enum {
VWIFI_VQ_RX,
VWIFI_VQ_TX,
VWIFI_NUM_VQS,
};
static struct virtqueue *vwifi_vqs[VWIFI_NUM_VQS];
static bool vwifi_virtio_enabled;
static DEFINE_SPINLOCK(vwifi_virtio_lock);
static void vwifi_virtio_rx_work(struct work_struct *work);
static DECLARE_WORK(vwifi_virtio_rx, vwifi_virtio_rx_work);
/**
* enum VWIFI_VIRTIO_PACKET_TYPE - non-standard management frame type for VWIFI
*
* Most of these types are inspired by IEEE 802.11 management frame, with
* little modifications since we are sending Ethernet frames. And note that
* we send these management frame with the Ethertype/length field being
* length (i.e. 802.3 frames), so we can distinguish it from a data frame
* (Ethernet II).
*
* See struct vwifi_virtio_header for the VWIFI management frame structures.
* For future modifications, please ensure that the VWIFI management frame
* contains the needed informations consumed by cfg80211/nl80211.
*
* For the reason why we have the VWIFI_STA_ENTRY_REQUEST and
* VWIFI_STA_ENTRY_RESPONSE: There are three to four addresses in a normal
* 802.11 data frame, an STA can recognize whether the frame is for our BSS or
* not by looking at a specific address (ToDS/FromDS in the frame control
* determines the address layout). However, we get Ethernet frames from network
* stack (cfg80211 doesn't implement the 802.11 data TX path), and we don't
* convert it to IEEE 802.11 frame since we pretend ourself (vwifi) to be a
* virtio-net driver and would like to pass Ethernet frame to virtio-net device.
* So the VWIFI_STA_ENTRY_REQUEST and VWIFI_STA_ENTRY_RESPONSE comes to the
* rescue. After an STA has connected to an an AP, the STA will request the AP
* about the informations (currently only MAC address) of other STAs in the same
* BSS, and then store them in an STA entry table. Whenever an STA receives a
* data frame, it checks whether the source address in the Ethernet header is in
* its STA entry table, and only if the condition is true, the STA pass the
* frame into network stack.
*
* @VWIFI_SCAN_REQUEST: active scan, request AP to reveal its informations.
* @VWIFI_SCAN_RESPONSE: AP informs its informations to STA.
* @VWIFI_CONNECT_REQUEST: request a connection to an AP.
* @VWIFI_CONNECT_RESPONSE: inform the STA about the success of the connection,
* and AP will call cfg80211_add_sta() to inform
* hostapd. If the AP runs with WPA/WPA2 (STA knows it since the beacon_ies
* contains WPA/RSN IE), hostapd will then fire the 4-way handshake to change
* keys with the STA. Only until the 4-way handshake is done (we learn it from
* cfg80211->change_station()), the STA can start to exchange packets with other
* STAs in the same BSS.
* @VWIFI_DISCONNECT: inform the disconnection. This type can be sent by STA or
* AP.
* @VWIFI_STA_ENTRY_REQUEST: STA requests the connected AP for the STA entries
* in the same BSS.
* @VWIFI_STA_ENTRY_RESPONSE: There are two case:
* 1. AP reply the STA's request about the STA
* entries and the STA entries include all the STAs in the BSS.
* 2. An unsolicited VWIFI_STA_ENTRY_RESPONSE will be
* broadcasted by AP when an STA is connected or disconnected, so other STAs in
* the same BSS can update their STA entry table.
*/
enum VWIFI_VIRTIO_PACKET_TYPE {
VWIFI_SCAN_REQUEST,
VWIFI_SCAN_RESPONSE,
VWIFI_CONNECT_REQUEST,
VWIFI_CONNECT_RESPONSE,
VWIFI_DISCONNECT,
VWIFI_STA_ENTRY_REQUEST,
VWIFI_STA_ENTRY_RESPONSE,
};
struct vwifi_virtio_header {
__le16 type;
#define VWIFI_VIRTIO_HEADER_TYPE_BYTE 2
union {
struct vwifi_virtio_scan_req {
__le32 ssid_len;
u8 ssid[IEEE80211_MAX_SSID_LEN];
} __packed scan_req;
struct vwifi_virtio_scan_resp {
u8 bssid[ETH_ALEN];
__le64 timestamp;
__le16 beacon_int;
__le16 capab_info;
__le32 ssid_len;
u8 ssid[IEEE80211_MAX_SSID_LEN];
__le32 channel; /* center frquency */
__le32 beacon_ies_len;
u8 beacon_ies[];
} __packed scan_resp;
struct vwifi_virtio_conn_req {
u8 bssid[ETH_ALEN];
__le32 ssid_len;
u8 ssid[IEEE80211_MAX_SSID_LEN];
} __packed connect_req;
struct vwifi_virtio_conn_resp {
__le16 status_code;
__le16 capab_info;
} __packed connect_resp;
struct vwifi_virtio_disconn {
u8 bssid[ETH_ALEN];
__le16 reason_code;
} __packed disconn;
struct vwifi_virtio_sta_entry_resp {
u8 bssid[ETH_ALEN];
__le16 cmd;
__le32 count;
u8 macs[ETH_ALEN];
} __packed sta_entry_resp;
} u;
} __packed;
enum VWIFI_STA_ENTRY_CMD {
VWIFI_STA_ENTRY_ADD,
VWIFI_STA_ENTRY_ADD_ALL,
VWIFI_STA_ENTRY_DEL,
};
struct bss_sta_entry {
struct hlist_node node;
u8 mac[ETH_ALEN];
};
/* helper function to retrieve vif from net_device */
static inline struct vwifi_vif *ndev_get_vwifi_vif(struct net_device *ndev)
{
return (struct vwifi_vif *) netdev_priv(ndev);
}
/* helper function to retrieve vif from wireless_dev */
static inline struct vwifi_vif *wdev_get_vwifi_vif(struct wireless_dev *wdev)
{
return container_of(wdev, struct vwifi_vif, wdev);
}
static inline u32 vwifi_mac_to_32(const u8 *mac)
{
u32 h = 3323198485U;
for (int i = 0; i < ETH_ALEN; i++) {
h ^= *(mac + i);
h *= 0x5bd1e995;
h ^= h >> 15;
}
return h;
}
#define SIN_S3_MIN (-(1 << 12))
#define SIN_S3_MAX (1 << 12)
/* A sine approximation via a third-order approx.
* Refer to https://www.coranac.com/2009/07/sines for details about the
* algorithm. Some parameters have been adjusted to increase the frequency
* of the sine function.
* Note: __sin_s3() is intended for internal use by rand_int_smooth() and
* should not be called elsewhere.
*
* @x: seed to generate third-order sine value
* @return: signed 32-bit integer ranging from SIN_S3_MIN to SIN_S3_MAX
*/
static inline s32 __sin_s3(s32 x)
{
/* S(x) = (x * (3 * 2^p - (x * x)/2^r)) / 2^s
* @n: the angle scale
* @A: the amplitude
* @p: keep the multiplication from overflowing
*/
const int32_t n = 6, A = 12, p = 10, r = 2 * n - p, s = n + p + 1 - A;
x = x << (30 - n);
if ((x ^ (x << 1)) < 0)
x = (1 << 31) - x;
x = x >> (30 - n);
return (x * ((3 << p) - ((x * x) >> r))) >> s;
}
/* Generate a signed 32-bit integer by feeding the seed into __sin_s3().
* The distribution of (seed, rand_int_smooth()) is closer to a sine function
* when plotted.
*/
static inline s32 rand_int_smooth(s32 low, s32 up, s32 seed)
{
s32 result = __sin_s3(seed) - SIN_S3_MIN;
result = (result * (up - low)) / (SIN_S3_MAX - SIN_S3_MIN);
result += low;
return result;
}
/* Helper function that prepares a structure with self-defined BSS information
* and "informs" the kernel about the "new" BSS. Most of the code is copied from
* the upcoming inform_dummy_bss function.
*/
static void inform_bss(struct vwifi_vif *vif)
{
struct vwifi_vif *ap;
list_for_each_entry (ap, &vwifi->ap_list, ap_list) {
struct cfg80211_bss *bss = NULL;
struct cfg80211_inform_bss data = {
/* the only channel */
.chan = &ap->wdev.wiphy->bands[NL80211_BAND_2GHZ]->channels[0],
.scan_width = NL80211_BSS_CHAN_WIDTH_20,
.signal = DBM_TO_MBM(rand_int_smooth(-100, -30, jiffies)),
};
int capability = WLAN_CAPABILITY_ESS;
if (ap->privacy)
capability |= WLAN_CAPABILITY_PRIVACY;
pr_info("vwifi: %s performs scan, found %s (SSID: %s, BSSID: %pM)\n",
vif->ndev->name, ap->ndev->name, ap->ssid, ap->bssid);
pr_info("cap = %d, beacon_ie_len = %d\n", capability,
ap->beacon_ie_len);
/* Using the CLOCK_BOOTTIME clock, which remains unaffected by changes
* in the system time-of-day clock and includes any time that the
* system is suspended.
* This clock is suitable for synchronizing the machines in the BSS
* using tsf.
*/
u64 tsf = div_u64(ktime_get_boottime_ns(), 1000);
/* It is possible to use cfg80211_inform_bss() instead. */
bss = cfg80211_inform_bss_data(
vif->wdev.wiphy, &data, CFG80211_BSS_FTYPE_UNKNOWN, ap->bssid, tsf,
capability, 100, ap->beacon_ie, ap->beacon_ie_len, GFP_KERNEL);
/* cfg80211_inform_bss_data() returns cfg80211_bss structure reference
* counter of which should be decremented if it is unused.
*/
cfg80211_put_bss(vif->wdev.wiphy, bss);
}
}
static void vwifi_beacon_inform_bss(struct vwifi_vif *ap,
struct vwifi_vif *sta,
struct cfg80211_inform_bss *bss_meta,
int capability,
u64 tsf)
{
struct cfg80211_bss *bss = NULL;
bss_meta->signal = DBM_TO_MBM(rand_int_smooth(-100, -30, jiffies));
/* It is possible to use cfg80211_inform_bss() instead. */
bss = cfg80211_inform_bss_data(sta->wdev.wiphy, bss_meta,
CFG80211_BSS_FTYPE_BEACON, ap->bssid, tsf,
capability, ap->beacon_int, ap->beacon_ie,
ap->beacon_ie_len, GFP_KERNEL);
/* cfg80211_inform_bss_data() returns cfg80211_bss structure reference
* counter of which should be decremented if it is unused.
*/
if (bss)
cfg80211_put_bss(sta->wdev.wiphy, bss);
}
/* The callback function of the beacon timer prepares a structure with
* custom BSS information and "notifies" the core about the "new"
* BSS information.
*/
static enum hrtimer_restart vwifi_beacon(struct hrtimer *timer)
{
struct vwifi_vif *vif = container_of(timer, struct vwifi_vif, beacon_timer);
if (vif->wdev.iftype != NL80211_IFTYPE_AP &&
vif->wdev.iftype != NL80211_IFTYPE_MESH_POINT &&
vif->wdev.iftype != NL80211_IFTYPE_ADHOC &&
vif->wdev.iftype != NL80211_IFTYPE_OCB)
return HRTIMER_NORESTART;
u64 timestamp = div_u64(ktime_get_boottime_ns(), 1000);
struct cfg80211_inform_bss bss_meta = {
.boottime_ns = ktime_get_boottime_ns(),
.chan = vif->channel,
};
switch (vif->bw) {
case NL80211_CHAN_WIDTH_5:
bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
break;
case NL80211_CHAN_WIDTH_10:
bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
break;
default:
bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
break;
}
int capability = WLAN_CAPABILITY_ESS;
if (vif->privacy)
capability |= WLAN_CAPABILITY_PRIVACY;
spin_lock(&vif_list_lock);
struct vwifi_vif *sta;
list_for_each_entry (sta, &vwifi->vif_list, list) {
if (sta->wdev.iftype != NL80211_IFTYPE_STATION)
continue;
vwifi_beacon_inform_bss(vif, sta, &bss_meta, capability, timestamp);
}
spin_unlock(&vif_list_lock);
/* beacon at next TBTT */
u64 tsf, until_tbtt;
tsf = ktime_to_us(ktime_get_real());
u32 bcn_int = vif->beacon_int;
until_tbtt = bcn_int - do_div(tsf, bcn_int);
hrtimer_forward_now(&vif->beacon_timer,
ns_to_ktime(until_tbtt * NSEC_PER_USEC));
return HRTIMER_RESTART;
}
static void vwifi_virtio_fill_vq(struct virtqueue *vq, u8 vnet_hdr_len);
static int vwifi_ndo_open(struct net_device *dev)
{
struct vwifi_vif *vif = ndev_get_vwifi_vif(dev);
netif_start_queue(dev);
vwifi_virtio_fill_vq(vwifi_vqs[VWIFI_VQ_RX], vif->vnet_hdr_len);
return 0;
}
static int vwifi_ndo_stop(struct net_device *dev)
{
struct vwifi_vif *vif = ndev_get_vwifi_vif(dev);
struct vwifi_packet *pkt, *is = NULL;
list_for_each_entry_safe (pkt, is, &vif->rx_queue, list) {
list_del(&pkt->list);
kfree(pkt);
}
netif_stop_queue(dev);
return 0;
}
static struct net_device_stats *vwifi_ndo_get_stats(struct net_device *dev)
{
struct vwifi_vif *vif = ndev_get_vwifi_vif(dev);
return &vif->stats;
}
static netdev_tx_t vwifi_ndo_start_xmit(struct sk_buff *skb,
struct net_device *dev);
/* Receive a packet: retrieve, encapsulate it in an skb, and perform the
* following operations based on the interface mode:
* - STA mode: Pass the skb to the upper level (protocol stack).
* - AP mode: Perform the following operations based on the packet type:
* 1. Unicast: If the skb is intended for another STA, pass it to that
* STA and do not pass it to the protocol stack. If the skb is intended
* for the AP itself, pass it to the protocol stack.
* 2. Broadcast: Pass the skb to all other STAs except the source STA, and
* then pass it to the protocol stack.
* 3. Multicast: Perform the same operations as for broadcast.
*/
static void vwifi_rx(struct net_device *dev)
{
struct vwifi_vif *vif = ndev_get_vwifi_vif(dev);
/* socket buffer will be sended to protocol stack */
struct sk_buff *skb;
/* socket buffer will be transmitted to another STA */
struct sk_buff *skb1 = NULL;
struct vwifi_packet *pkt;
if (list_empty(&vif->rx_queue)) {
pr_info("vwifi rx: No packet in rx_queue\n");
return;
}
if (mutex_lock_interruptible(&vif->lock))
goto pkt_free;
pkt = list_first_entry(&vif->rx_queue, struct vwifi_packet, list);
vif->stats.rx_packets++;
vif->stats.rx_bytes += pkt->datalen;
vif->active_time = jiffies;
mutex_unlock(&vif->lock);
/* Put raw packet into socket buffer */
skb = dev_alloc_skb(pkt->datalen + 2);
if (!skb) {
pr_info("vwifi rx: low on mem - packet dropped\n");
vif->stats.rx_dropped++;
goto pkt_free;
}
skb_reserve(skb, 2); /* align IP address on 16B boundary */
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
list_del(&pkt->list);
kfree(pkt);
if (vif->wdev.iftype == NL80211_IFTYPE_AP) {
struct ethhdr *eth_hdr = (struct ethhdr *) skb->data;
/* When receiving a multicast/broadcast packet, it is sent to every
* STA except the source STA, and then passed to the protocol stack.
*/
if (is_multicast_ether_addr(eth_hdr->h_dest)) {
pr_info("vwifi: is_multicast_ether_addr\n");
skb1 = skb_copy(skb, GFP_KERNEL);
}
/* Receiving a unicast packet */
else {
/* The packet is not intended for the AP itself. Instead, it is
* sent to the destination STA and not passed to the protocol stack.
*/
if (!ether_addr_equal(eth_hdr->h_dest, vif->ndev->dev_addr)) {
skb1 = skb;
skb = NULL;
}
}
if (skb1) {
pr_info("vwifi: AP %s relay:\n", vif->ndev->name);
vwifi_ndo_start_xmit(skb1, vif->ndev);
}
/* Nothing to pass to protocol stack */
if (!skb)
return;
}
/* Pass the skb to protocol stack */
skb->dev = dev;
skb->protocol = eth_type_trans(skb, dev);
skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
netif_rx_ni(skb);
#else
netif_rx(skb);
#endif
return;
pkt_free:
list_del(&pkt->list);
kfree(pkt);
}
static int __vwifi_ndo_start_xmit(struct vwifi_vif *vif,
struct vwifi_vif *dest_vif,
struct sk_buff *skb)
{
struct vwifi_packet *pkt = NULL;
struct ethhdr *eth_hdr = (struct ethhdr *) skb->data;
int datalen;
if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
pr_info("vwifi: STA %s (%pM) send packet to AP %s (%pM)\n",
vif->ndev->name, eth_hdr->h_source, dest_vif->ndev->name,
eth_hdr->h_dest);
} else if (vif->wdev.iftype == NL80211_IFTYPE_AP) {
pr_info("vwifi: AP %s (%pM) send packet to STA %s (%pM)\n",
vif->ndev->name, eth_hdr->h_source, dest_vif->ndev->name,
eth_hdr->h_dest);
}
pkt = kmalloc(sizeof(struct vwifi_packet), GFP_KERNEL);
if (!pkt) {
pr_info("Ran out of memory allocating packet pool\n");
return NETDEV_TX_OK;
}
datalen = skb->len;
memcpy(pkt->data, skb->data, datalen);
pkt->datalen = datalen;
/* enqueue packet to destination vif's rx_queue */
if (mutex_lock_interruptible(&dest_vif->lock))
goto error_before_rx_queue;
list_add_tail(&pkt->list, &dest_vif->rx_queue);
mutex_unlock(&dest_vif->lock);
if (mutex_lock_interruptible(&vif->lock))
goto erorr_after_rx_queue;
/* Update interface statistics */
vif->stats.tx_packets++;
vif->stats.tx_bytes += datalen;
vif->active_time = jiffies;
mutex_unlock(&vif->lock);
if (dest_vif->wdev.iftype == NL80211_IFTYPE_STATION) {
pr_info("vwifi: STA %s (%pM) receive packet from AP %s (%pM)\n",
dest_vif->ndev->name, eth_hdr->h_dest, vif->ndev->name,
eth_hdr->h_source);
} else if (dest_vif->wdev.iftype == NL80211_IFTYPE_AP) {
pr_info("vwifi: AP %s (%pM) receive packet from STA %s (%pM)\n",
dest_vif->ndev->name, eth_hdr->h_dest, vif->ndev->name,
eth_hdr->h_source);
}
/* Directly send to rx_queue, simulate the rx interrupt */
vwifi_rx(dest_vif->ndev);
return datalen;
erorr_after_rx_queue:
list_del(&pkt->list);
error_before_rx_queue:
kfree(pkt);
return 0;
}
static netdev_tx_t vwifi_virtio_tx(struct vwifi_vif *vif, struct sk_buff *skb);
/* Network packet transmit.
* Callback called by the kernel when packets need to be sent.
*/
static netdev_tx_t vwifi_ndo_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
struct vwifi_vif *vif = ndev_get_vwifi_vif(dev);
struct vwifi_vif *dest_vif = NULL;
struct ethhdr *eth_hdr = (struct ethhdr *) skb->data;
unsigned long flags;
int err;
int count = 0;
spin_lock_irqsave(&vwifi_virtio_lock, flags);
if (vwifi_virtio_enabled) {
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
err = vwifi_virtio_tx(vif, skb);
return err;
}
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
/* TX by interface of STA mode */
if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
if (vif->ap && vif->ap->ap_enabled) {
dest_vif = vif->ap;
if (__vwifi_ndo_start_xmit(vif, dest_vif, skb))
count++;
}
}
/* TX by interface of AP mode */
else if (vif->wdev.iftype == NL80211_IFTYPE_AP) {
/* Find the source interface */
struct vwifi_vif *src_vif;
list_for_each_entry (src_vif, &vif->bss_list, bss_list) {
if (ether_addr_equal(eth_hdr->h_source, src_vif->ndev->dev_addr))
break;
}
/* Check if the packet is broadcasting */
if (is_broadcast_ether_addr(eth_hdr->h_dest)) {
list_for_each_entry (dest_vif, &vif->bss_list, bss_list) {
/* Don't send broadcast packet back to the source interface.
*/
if (ether_addr_equal(eth_hdr->h_source,
dest_vif->ndev->dev_addr))
continue;
/* Don't send packet from dest_vif's blocklist */
if (blocklist_check(dest_vif->ndev->name, src_vif->ndev->name))
continue;
if (__vwifi_ndo_start_xmit(vif, dest_vif, skb))
count++;
}
}
/* The packet is unicasting */
else {
list_for_each_entry (dest_vif, &vif->bss_list, bss_list) {
if (ether_addr_equal(eth_hdr->h_dest,
dest_vif->ndev->dev_addr)) {
if (!blocklist_check(dest_vif->ndev->name,
src_vif->ndev->name) &&
__vwifi_ndo_start_xmit(vif, dest_vif, skb))
count++;
break;
}
}
}
}
if (!count)
vif->stats.tx_dropped++;
/* Don't forget to cleanup skb, as its ownership moved to xmit callback. */
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
/* Structure of functions for network devices.
* It should have at least ndo_start_xmit functions called for packet to be
* sent.
*/
static struct net_device_ops vwifi_ndev_ops = {
.ndo_open = vwifi_ndo_open,
.ndo_stop = vwifi_ndo_stop,
.ndo_start_xmit = vwifi_ndo_start_xmit,
.ndo_get_stats = vwifi_ndo_get_stats,
};
/* Inform the "dummy" BSS to kernel and call cfg80211_scan_done() to finish
* scan.
*/
static void vwifi_scan_timeout_work(struct work_struct *w)
{
struct vwifi_vif *vif = container_of(w, struct vwifi_vif, ws_scan_timeout);
struct cfg80211_scan_info info = {
/* if scan was aborted by user (calling cfg80211_ops->abort_scan) or by
* any driver/hardware issue - field should be set to "true"
*/
.aborted = false,
};
/* inform with dummy BSS */
inform_bss(vif);
if (mutex_lock_interruptible(&vif->lock))
return;
/* finish scan */
cfg80211_scan_done(vif->scan_request, &info);
vif->scan_request = NULL;
mutex_unlock(&vif->lock);
}
/* Callback called when the scan timer timeouts. This function just schedules
* the timeout work and offloads the job of informing "dummy" BSS to kernel
* onto it.
*/
static void vwifi_scan_timeout(struct timer_list *t)
{
struct vwifi_vif *vif = container_of(t, struct vwifi_vif, scan_timeout);
if (vif->scan_request)
schedule_work(&vif->ws_scan_timeout);
}
static void vwifi_virtio_scan_request(struct vwifi_vif *vif);
/* Scan routine. It simulates a fake BSS scan (in fact, it does nothing) and
* sets a scan timer to start from then. Once the timer timeouts, the timeout
* routine vwifi_scan_timeout() will be invoked. This routine schedules a
* timeout work that informs the kernel about the "dummy" BSS and completes the
* scan.
*/
static void vwifi_scan_routine(struct work_struct *w)
{
struct vwifi_vif *vif = container_of(w, struct vwifi_vif, ws_scan);
unsigned long flags;
spin_lock_irqsave(&vwifi_virtio_lock, flags);
if (vwifi_virtio_enabled) {
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
vwifi_virtio_scan_request(vif);
return;
}
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
/* In a real-world driver, BSS scanning would occur here. However, in the
* case of viwifi, scanning is not performed because dummy BSS entries are
* already stored in the SSID hash table. Instead, a scan timeout is set
* after a specific number of jiffies. The timeout worker informs the
* kernel about the "dummy" BSS and calls cfg80211_scan_done() to complete
* the scan.
*/
mod_timer(&vif->scan_timeout, jiffies + msecs_to_jiffies(SCAN_TIMEOUT_MS));
}
static void vwifi_virtio_connect_request(struct vwifi_vif *vif);
static void vwifi_connect_routine(struct work_struct *w)
{
struct vwifi_vif *vif = container_of(w, struct vwifi_vif, ws_connect);
struct vwifi_vif *ap = NULL;
struct station_info *sinfo;
unsigned long flags;
spin_lock_irqsave(&vwifi_virtio_lock, flags);
if (vwifi_virtio_enabled) {
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
vwifi_virtio_connect_request(vif);
return;
}
spin_unlock_irqrestore(&vwifi_virtio_lock, flags);
if (mutex_lock_interruptible(&vif->lock))
return;
/* Finding the AP by request SSID */
list_for_each_entry (ap, &vwifi->ap_list, ap_list) {
if (!memcmp(ap->ssid, vif->req_ssid, ap->ssid_len)) {
pr_info("vwifi: %s is connected to AP %s (SSID: %s, BSSID: %pM)\n",
vif->ndev->name, ap->ndev->name, ap->ssid, ap->bssid);
if (mutex_lock_interruptible(&ap->lock))
return;
/* AP connection part */
sinfo = kmalloc(sizeof(struct station_info), GFP_KERNEL);
if (!sinfo)
return;
/* It is safe that we fake the association request IEs
* by beacon IEs, since they both possibly have the WPA/RSN IE
* which is what the upper user-space program (e.g. hostapd)
* cares about.
*/
sinfo->assoc_req_ies = ap->beacon_ie;
sinfo->assoc_req_ies_len = ap->beacon_ie_len;
list_add_tail(&vif->bss_list, &ap->bss_list);
/* nl80211 will inform the user-space program (e.g. hostapd)
* about the newly-associated station via generic netlink
* command NL80211_CMD_NEW_STATION for latter processing
* (e.g. 4-way handshake).
*/
cfg80211_new_sta(ap->ndev, vif->ndev->dev_addr, sinfo, GFP_KERNEL);
mutex_unlock(&ap->lock);
/* STA connection part */
cfg80211_connect_result(vif->ndev, ap->bssid, NULL, 0, NULL, 0,
WLAN_STATUS_SUCCESS, GFP_KERNEL);
memcpy(vif->ssid, ap->ssid, ap->ssid_len);
memcpy(vif->bssid, ap->bssid, ETH_ALEN);
vif->sme_state = SME_CONNECTED;
vif->conn_time = jiffies;
vif->ap = ap;