Skip to content

Commit 0dfa5f7

Browse files
authored
Merge pull request #70 from jychen0611/ad-hoc
Support ad hoc network
2 parents db7245e + a5fc5f0 commit 0dfa5f7

File tree

6 files changed

+652
-30
lines changed

6 files changed

+652
-30
lines changed

README.md

+213-18
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ $ pip3 install numpy matplotlib
3636
```
3737

3838
## Testing environment (non-virtio)
39+
To test the network environment effectively, we utilize **Linux network namespaces**. These namespaces isolate network environments from the host system, providing distinct instances of network stacks with independent routes, firewall rules, and network devices.
40+
41+
Without network namespaces, virtual interfaces created within the same namespace use the loopback device for packet transmission between them, as the kernel recognizes them as residing on the same host.
42+
43+
In our testing setup, all interfaces created by `vwifi` are placed within an isolated network namespace. This approach ensures that each virtual interface operates independently, facilitating comprehensive testing of networking functionalities without interference from the host's network configuration.
44+
45+
Below, we will conduct two separate tests: Infrastructure BSS and Independent BSS.
46+
### Infrastructure BSS
3947
<p align="center"><img src="assets/vwifi.png" alt="logo image" width=60%></p>
4048

4149
The testing environment consists of **one AP and two STAs**.
@@ -48,15 +56,12 @@ The AP then performs the following actions based on the packet type:
4856
2. Broadcast: The AP forwards the packet to all other STAs in the network, except for the source STA, and then passes it to the protocol stack.
4957
3. Multicast: The AP treats multicast packets the same way as broadcast packets.
5058

51-
To test the network environment, we can utilize the **Linux network namespace**.
52-
Linux network namespace allows us to isolate a network environment from the host system, providing its own routes, firewall rules, and network devices.
53-
Essentially, it creates a separate instance of the network stack.
59+
### Independent BSS
60+
<p align="center"><img src="assets/ibss.png" alt="logo image" width=75%></p>
5461

55-
Without network namespace, when virtual interfaces are created that share the same network namespace and start transmitting/receiving packets between them,
56-
the kernel will use the loopback device for packet transmission/reception. This behavior occurs because the kernel identifies that the sender and receiver are on the same host.
57-
58-
In conclusion, all the interfaces created by `vwifi` in the testing environment will be added to an isolated network namespace.
62+
The testing environment consists of **two IBSS devices**.
5963

64+
The testing environment operates in IEEE 802.11 independent BSS. IBSS devices can communicate with any device in the same IBSS network **without the need to establish a connection beforehand**. However, devices in different IBSS networks cannot communicate with each other.
6065
## Build and Run (non-virtio)
6166

6267
To build the kernel module, execute the following command:
@@ -72,7 +77,7 @@ $ sudo modprobe cfg80211
7277
Insert the `vwifi` driver.
7378
This will create three interfaces (the "station" parameter can be modified according to preference):
7479
```shell
75-
$ sudo insmod vwifi.ko station=3
80+
$ sudo insmod vwifi.ko station=5
7681
```
7782

7883
Please note that interfaces can only be created in station mode during the initialization phase.
@@ -85,7 +90,7 @@ To check the network interfaces, run the following command:
8590
$ ip link
8691
```
8792

88-
There should be entries starting with `vw0`, `vw1`, and `vw2`, which correspond to the interfaces created by `vwifi`.
93+
There should be entries starting with `vw0`, `vw1`, `vw2`, `vw3`, and `vw4`, which correspond to the interfaces created by `vwifi`.
8994

9095
To view the available wireless interfaces, execute the following command:
9196
```shell
@@ -94,24 +99,41 @@ $ sudo iw dev
9499

95100
You should see something similar to the following output:
96101
```
97-
phy#2
102+
phy#5
103+
Interface vw4
104+
ifindex 7
105+
wdev 0x500000001
106+
addr 00:76:77:34:00:00
107+
type managed
108+
txpower 0.00 dBm
109+
phy#4
110+
Interface vw3
111+
ifindex 6
112+
wdev 0x400000001
113+
addr 00:76:77:33:00:00
114+
type managed
115+
txpower 0.00 dBm
116+
phy#3
98117
Interface vw2
99118
ifindex 5
100-
wdev 0x200000001
101-
addr 00:6f:77:6c:32:00
119+
wdev 0x300000001
120+
addr 00:76:77:32:00:00
102121
type managed
103-
phy#1
122+
txpower 0.00 dBm
123+
phy#2
104124
Interface vw1
105125
ifindex 4
106-
wdev 0x100000001
107-
addr 00:6f:77:6c:31:00
126+
wdev 0x200000001
127+
addr 00:76:77:31:00:00
108128
type managed
109-
phy#0
129+
txpower 0.00 dBm
130+
phy#1
110131
Interface vw0
111132
ifindex 3
112-
wdev 0x1
113-
addr 00:6f:77:6c:30:00
133+
wdev 0x100000001
134+
addr 00:76:77:30:00:00
114135
type managed
136+
txpower 0.00 dBm
115137
```
116138

117139
As observed, each interface has its own phy (`struct wiphy`), allowing them to be placed into separate network namespaces.
@@ -125,6 +147,10 @@ $ sudo iw list
125147

126148
Reference output:
127149
```
150+
Wiphy vw_phy4
151+
(... omit)
152+
Wiphy vw_phy3
153+
(... omit)
128154
Wiphy vw_phy2
129155
(... omit)
130156
Wiphy vw_phy1
@@ -366,6 +392,175 @@ PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
366392
4 packets transmitted, 4 received, 0% packet loss, time 3058ms
367393
rtt min/avg/max/mdev = 0.054/0.141/0.342/0.117 ms
368394
```
395+
### IBSS mode
396+
397+
#### Creating Network Namespaces
398+
Create three network namespaces using the following commands:
399+
```shell
400+
$ sudo ip netns add ns3
401+
$ sudo ip netns add ns4
402+
```
403+
Find the `wiphy` name for the two interfaces.
404+
The index number for the `wiphy` name postfix might be different each time.
405+
Please use the following command for the ease of memorizing different index number everytime.
406+
```shell
407+
$ vw3_phy=$(sudo iw dev vw3 info | grep wiphy | awk '{print $2}')
408+
$ vw3_phy=$(sudo iw list | grep "wiphy index: $vw3_phy" -B 1 | grep Wiphy | awk '{print $2}')
409+
$ vw4_phy=$(sudo iw dev vw4 info | grep wiphy | awk '{print $2}')
410+
$ vw4_phy=$(sudo iw list | grep "wiphy index: $vw4_phy" -B 1 | grep Wiphy | awk '{print $2}')
411+
```
412+
Check whether the name of each `wiphy` is the same as the name listing under the command `sudo iw list`
413+
```shell
414+
$ echo $vw3_phy
415+
vw_phy3
416+
$ echo $vw4_phy
417+
vw_phy4
418+
```
419+
Assign the two interfaces to separate network namespaces.
420+
Please note that the `wiphy` is placed within the network namespace, and the interface associated with that wiphy will be contained within it.
421+
```shell
422+
$ sudo iw phy vw_phy3 set netns name ns3
423+
$ sudo iw phy vw_phy4 set netns name ns4
424+
```
425+
#### Assigning IP Addresses to Each Interface
426+
427+
Now, assign an IP address to both interfaces using the following commands:
428+
```shell
429+
$ sudo ip netns exec ns3 ip addr add 10.0.0.4/24 dev vw3
430+
$ sudo ip netns exec ns4 ip addr add 10.0.0.5/24 dev vw4
431+
```
432+
There are two methods to configure an IBSS network: manual configuration or using WPA.
433+
#### Option1 : Manual configuration
434+
##### Switch to IBSS mode
435+
Switch device to IBSS mode using the following command :
436+
437+
***iw dev [interface] set type ibss***
438+
439+
The following commands switch `vw3` and `vw4` to IBSS mode.
440+
```shell
441+
$ sudo ip netns exec ns3 iw dev vw3 set type ibss
442+
$ sudo ip netns exec ns4 iw dev vw4 set type ibss
443+
```
444+
Check the information of `vw3`.
445+
```shell
446+
$ sudo ip netns exec ns3 iw dev vw3 info
447+
```
448+
You should see output similar to the following:
449+
```
450+
Interface vw3
451+
ifindex 6
452+
wdev 0x400000001
453+
addr 00:76:77:33:00:00
454+
type IBSS
455+
wiphy 4
456+
txpower 0.00 dBm
457+
```
458+
##### Join IBSS network
459+
```shell
460+
$ sudo ip netns exec ns3 ip link set vw3 up
461+
$ sudo ip netns exec ns4 ip link set vw4 up
462+
```
463+
Users can join a specific IBSS cell and configure additional settings using the command :
464+
465+
***iw dev [interface] ibss join [SSID] [freq in MHz] [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [fixed-freq] [fixed-bssid] [beacon-interval <TU>] [basic-rates <rate in Mbps,rate2,…>] [mcast-rate <rate in Mbps>] [key d:0:abcde]***
466+
467+
If the IBSS cell does not already exist, it will be created.
468+
469+
The following command makes `vw3` and `vw4` join the same IBSS cell with the SSID `ibss1` and specifies the frequency as 2412 MHz:
470+
```shell
471+
$ sudo ip netns exec ns3 iw dev vw3 ibss join ibss1 2412 NOHT fixed-freq 00:76:77:33:00:00 beacon-interval 200
472+
$ sudo ip netns exec ns4 iw dev vw4 ibss join ibss1 2412 NOHT fixed-freq 00:76:77:33:00:00 beacon-interval 200
473+
```
474+
Check the information of `vw3`.
475+
```shell
476+
$ sudo ip netns exec ns3 iw dev vw3 info
477+
```
478+
You should see output similar to the following:
479+
```
480+
Interface vw3
481+
ifindex 6
482+
wdev 0x400000001
483+
addr 00:76:77:33:00:00
484+
ssid ibss1
485+
type IBSS
486+
wiphy 4
487+
txpower 0.00 dBm
488+
```
489+
#### Option2 : Using WPA
490+
```shell
491+
$ sudo ip netns exec ns3 ip link set vw3 up
492+
$ sudo ip netns exec ns4 ip link set vw4 up
493+
```
494+
Prepare the following script `wpa_supplicant_ibss.conf` (you can modify the script based on your needs):
495+
```shell
496+
network={
497+
ssid="ibss1"
498+
mode=1
499+
frequency=2412
500+
key_mgmt=WPA-PSK
501+
proto=RSN
502+
pairwise=CCMP
503+
group=CCMP
504+
psk="12345678"
505+
}
506+
```
507+
Using the command **wpa_supplicant**, configure `vw3` and `vw4` to join `ibss1`.
508+
```shell
509+
$ sudo ip netns exec ns3 wpa_supplicant -i vw3 -B -c scripts/wpa_supplicant_ibss.conf
510+
$ sudo ip netns exec ns4 wpa_supplicant -i vw4 -B -c scripts/wpa_supplicant_ibss.conf
511+
```
512+
Check the information of `vw3`.
513+
```shell
514+
$ sudo ip netns exec ns3 iw dev vw3 info
515+
```
516+
You should see output similar to the following:
517+
```
518+
Interface vw3
519+
ifindex 6
520+
wdev 0x400000001
521+
addr 00:76:77:33:00:00
522+
ssid ibss1
523+
type IBSS
524+
wiphy 4
525+
txpower 0.00 dBm
526+
```
527+
#### Transmission/Receivement test
528+
To perform a ping test between two IBSS devices (`vw3` and `vw4`) in the same ibss cell (`ibss1`), use the following command:
529+
```shell
530+
$ sudo ip netns exec ns3 ping -c 1 10.0.0.5
531+
```
532+
You should see output similar to the following:
533+
```
534+
PING 10.0.0.5 (10.0.0.5) 56(84) bytes of data.
535+
64 bytes from 10.0.0.5: icmp_seq=1 ttl=64 time=0.093 ms
536+
537+
--- 10.0.0.5 ping statistics ---
538+
1 packets transmitted, 1 received, 0% packet loss, time 0ms
539+
rtt min/avg/max/mdev = 0.093/0.093/0.093/0.000 ms
540+
```
541+
#### Leave IBSS network
542+
To leave the current IBSS cell, use ***iw dev [interface] ibss leave***.
543+
544+
The following command makes `vw3` and `vw4` leave `ibss1`:
545+
```shell
546+
$ sudo ip netns exec ns3 iw dev vw3 ibss leave
547+
$ sudo ip netns exec ns4 iw dev vw4 ibss leave
548+
```
549+
Check the information of `vw3`.
550+
```shell
551+
$ sudo ip netns exec ns3 iw dev vw3 info
552+
```
553+
You should see output similar to the following:
554+
```
555+
Interface vw3
556+
ifindex 6
557+
wdev 0x400000001
558+
addr 00:76:77:33:00:00
559+
type IBSS
560+
wiphy 4
561+
txpower 0.00 dBm
562+
```
563+
369564
### vwifi-tool
370565
A userspace tool which supports more user-specific utilization for vwifi.
371566
Aiming to provide more flexibility and customization for users of vwifi.

assets/ibss.drawio

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<mxfile host="app.diagrams.net" modified="2024-07-07T06:17:01.630Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" etag="-HEaOgMibt1UBT4tSmFY" version="24.5.4" type="device">
2+
<diagram id="yF4yq0laEPX1mU6WioKN" name="Page-1">
3+
<mxGraphModel dx="1715" dy="574" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" background="#FFFFFF" math="0" shadow="0">
4+
<root>
5+
<mxCell id="0" />
6+
<mxCell id="1" parent="0" />
7+
<mxCell id="5jAbq0H1r1dMHXJugziX-10" value="" style="ellipse;shape=cloud;whiteSpace=wrap;html=1;fillStyle=solid;gradientDirection=radial;fillColor=none;" parent="1" vertex="1">
8+
<mxGeometry x="-70" y="221" width="870" height="330" as="geometry" />
9+
</mxCell>
10+
<mxCell id="1VKriumNMIqOGRltyLQd-10" value="" style="shape=mxgraph.arrows2.wedgeArrowDashed2;html=1;bendable=0;startWidth=150;stepSize=15;rounded=0;strokeColor=#006633;elbow=vertical;sketch=1;curveFitting=1;jiggle=2;shadow=0;flowAnimation=1;fillColor=#e51400;" parent="1" edge="1">
11+
<mxGeometry width="100" height="100" relative="1" as="geometry">
12+
<mxPoint x="270" y="340" as="sourcePoint" />
13+
<mxPoint x="318.1258741258739" y="311" as="targetPoint" />
14+
<Array as="points">
15+
<mxPoint x="430.05" y="230" />
16+
<mxPoint x="420.05" y="190" />
17+
<mxPoint x="279.99999999999994" y="253" />
18+
</Array>
19+
</mxGeometry>
20+
</mxCell>
21+
<mxCell id="1VKriumNMIqOGRltyLQd-3" value="" style="shape=mxgraph.arrows2.wedgeArrowDashed2;html=1;bendable=0;startWidth=150;stepSize=15;rounded=0;strokeColor=#006633;elbow=vertical;sketch=1;curveFitting=1;jiggle=2;shadow=0;flowAnimation=1;entryX=0.75;entryY=1;entryDx=0;entryDy=0;fillColor=#e51400;" parent="1" edge="1">
22+
<mxGeometry width="100" height="100" relative="1" as="geometry">
23+
<mxPoint x="470" y="280" as="sourcePoint" />
24+
<mxPoint x="432.5" y="306" as="targetPoint" />
25+
<Array as="points">
26+
<mxPoint x="638.61" y="177" />
27+
<mxPoint x="628.61" y="137" />
28+
<mxPoint x="488.55999999999995" y="200" />
29+
</Array>
30+
</mxGeometry>
31+
</mxCell>
32+
<mxCell id="RjUjesMydYoAQQ34KLtV-6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#000000;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
33+
<mxGeometry x="80" y="340" width="170" height="80" as="geometry" />
34+
</mxCell>
35+
<mxCell id="RjUjesMydYoAQQ34KLtV-7" value="&lt;font color=&quot;#ffffff&quot;&gt;netns: ns3&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;" parent="1" vertex="1">
36+
<mxGeometry x="110" y="343" width="110" height="30" as="geometry" />
37+
</mxCell>
38+
<mxCell id="RjUjesMydYoAQQ34KLtV-9" value="vw3&lt;div&gt;(IBSS mode)&lt;/div&gt;" style="rounded=1;whiteSpace=wrap;html=1;fontSize=18;fillColor=#006633;strokeColor=#006633;fontColor=#ffffff;" parent="1" vertex="1">
39+
<mxGeometry x="105" y="400" width="120" height="60" as="geometry" />
40+
</mxCell>
41+
<mxCell id="RjUjesMydYoAQQ34KLtV-10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#000000;fontColor=#ffffff;strokeColor=#006EAF;" parent="1" vertex="1">
42+
<mxGeometry x="505" y="340" width="170" height="80" as="geometry" />
43+
</mxCell>
44+
<mxCell id="RjUjesMydYoAQQ34KLtV-11" value="&lt;font color=&quot;#ffffff&quot;&gt;netns: ns4&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;" parent="1" vertex="1">
45+
<mxGeometry x="535" y="343" width="110" height="30" as="geometry" />
46+
</mxCell>
47+
<mxCell id="RjUjesMydYoAQQ34KLtV-13" value="vw4&lt;br&gt;(IBSS mode)" style="rounded=1;whiteSpace=wrap;html=1;fontSize=18;fillColor=#006633;strokeColor=#006633;fontColor=#ffffff;" parent="1" vertex="1">
48+
<mxGeometry x="530" y="400" width="120" height="60" as="geometry" />
49+
</mxCell>
50+
<mxCell id="RjUjesMydYoAQQ34KLtV-22" value="&lt;font color=&quot;#ffffff&quot;&gt;IP address: 10.0.0.4&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" parent="1" vertex="1">
51+
<mxGeometry x="92" y="371" width="150" height="30" as="geometry" />
52+
</mxCell>
53+
<mxCell id="RjUjesMydYoAQQ34KLtV-24" value="&lt;font color=&quot;#ffffff&quot;&gt;IP address: 10.0.0.5&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" parent="1" vertex="1">
54+
<mxGeometry x="515" y="368" width="150" height="30" as="geometry" />
55+
</mxCell>
56+
<mxCell id="5jAbq0H1r1dMHXJugziX-4" value="" style="endArrow=classic;html=1;rounded=0;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;dashed=1;" parent="1" edge="1">
57+
<mxGeometry relative="1" as="geometry">
58+
<mxPoint x="529" y="425" as="sourcePoint" />
59+
<mxPoint x="224" y="425" as="targetPoint" />
60+
</mxGeometry>
61+
</mxCell>
62+
<mxCell id="5jAbq0H1r1dMHXJugziX-5" value="ARP Reply" style="edgeLabel;resizable=0;html=1;;align=center;verticalAlign=middle;" parent="5jAbq0H1r1dMHXJugziX-4" connectable="0" vertex="1">
63+
<mxGeometry relative="1" as="geometry" />
64+
</mxCell>
65+
<mxCell id="5jAbq0H1r1dMHXJugziX-6" value="" style="endArrow=classic;html=1;rounded=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;dashed=1;" parent="1" edge="1">
66+
<mxGeometry relative="1" as="geometry">
67+
<mxPoint x="225" y="438.5" as="sourcePoint" />
68+
<mxPoint x="530" y="438.5" as="targetPoint" />
69+
</mxGeometry>
70+
</mxCell>
71+
<mxCell id="5jAbq0H1r1dMHXJugziX-7" value="Ping Request" style="edgeLabel;resizable=0;html=1;;align=center;verticalAlign=middle;" parent="5jAbq0H1r1dMHXJugziX-6" connectable="0" vertex="1">
72+
<mxGeometry relative="1" as="geometry" />
73+
</mxCell>
74+
<mxCell id="5jAbq0H1r1dMHXJugziX-8" value="" style="endArrow=classic;html=1;rounded=0;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;dashed=1;" parent="1" edge="1">
75+
<mxGeometry relative="1" as="geometry">
76+
<mxPoint x="528" y="453" as="sourcePoint" />
77+
<mxPoint x="223" y="453" as="targetPoint" />
78+
</mxGeometry>
79+
</mxCell>
80+
<mxCell id="5jAbq0H1r1dMHXJugziX-9" value="Ping Reply" style="edgeLabel;resizable=0;html=1;;align=center;verticalAlign=middle;" parent="5jAbq0H1r1dMHXJugziX-8" connectable="0" vertex="1">
81+
<mxGeometry relative="1" as="geometry" />
82+
</mxCell>
83+
<mxCell id="5jAbq0H1r1dMHXJugziX-11" value="&lt;font style=&quot;font-size: 26px;&quot;&gt;ad hoc network&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;" parent="1" vertex="1">
84+
<mxGeometry x="310" y="299" width="130" height="27" as="geometry" />
85+
</mxCell>
86+
<mxCell id="1VKriumNMIqOGRltyLQd-8" value="" style="endArrow=classic;html=1;rounded=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;dashed=1;" parent="1" edge="1">
87+
<mxGeometry relative="1" as="geometry">
88+
<mxPoint x="226" y="409.5" as="sourcePoint" />
89+
<mxPoint x="531" y="409.5" as="targetPoint" />
90+
</mxGeometry>
91+
</mxCell>
92+
<mxCell id="1VKriumNMIqOGRltyLQd-9" value="ARP Request" style="edgeLabel;resizable=0;html=1;;align=center;verticalAlign=middle;" parent="1VKriumNMIqOGRltyLQd-8" connectable="0" vertex="1">
93+
<mxGeometry relative="1" as="geometry" />
94+
</mxCell>
95+
</root>
96+
</mxGraphModel>
97+
</diagram>
98+
</mxfile>

assets/ibss.png

276 KB
Loading

0 commit comments

Comments
 (0)