首先 加载mac80211.ko
, 参考: https://blog.csdn.net/fantasy_wxe/article/details/128057949
然后执行如下操作,创建 AP&STA 并进行扫描、关联、认证、ping包操作
wget https://w1.fi/releases/hostapd-2.10.tar.gz
tar zxvf hostapd-2.10.tar.gz
cd hostapd-2.10/hostapd/
cp defconfig .config
make
wget https://w1.fi/releases/wpa_supplicant-2.10.tar.gz
tar zxvf wpa_supplicant-2.10.tar.gz
cd wpa_supplicant-2.10/wpa_supplicant
cp defconfig .config
make
wget https://downloads.isc.org/isc/dhcp/4.4.3/dhcp-4.4.3.tar.gz
cd dhcp-4.4.3
./configure
make
创建hostapd.conf配置文件,文件内容如下
# hostapd.conf
interface=wlan0
driver=nl80211
country_code=CN
ssid=wifi_simu_test
channel=6
hw_mode=b
wpa=3
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
wpa_passphrase=helloworld
auth_algs=3
beacon_int=100
创建dhcpd.conf配置文件,文件内容如下
#dhcpd.conf
subnet 192.168.100.0 netmask 255.255.255.0 {
option routers 192.168.100.1 ;
option domain-name-servers 192.168.100.1 ;
range 192.168.100.80 192.168.100.150;
range 192.168.100.180 192.168.100.220;
default-lease-time 600 ;
max-lease-time 7200 ;
# we want this server to be the only DHCP server in this network
authoritative ;
}
在AP侧Terminal中执行如下命令
w512@w512-pc:~/wlan_test$ su root
root@w512-pc:/home/w512/wlan_test# ip netns add wifi_ap
root@w512-pc:/home/w512/wlan_test# ip netns list
wifi_ap
root@w512-pc:/home/w512/wlan_test# ip netns exec wifi_ap bash
root@w512-pc:/home/w512/wlan_test# echo $BASHPID
2280
注意: 2280在STA侧会用到
新建一个Terminal, 作为STA侧
su root
iw phy phy0 set netns 2280
在AP侧Terminal中输入如下命令
ip addr add 192.168.100.1/24 dev wlan0
hostapd-2.10/hostapd/hostapd hostapd.conf &
mkdir -p /var/db
touch /var/db/dhcpd.leases
dhcp-4.4.3/server/dhcpd -4 -f -d -cf dhcpd.conf -lf /var/db/dhcpd.leases wlan0&
通过ip a
查看wlan0信息
root@w512-pc:/home/w512/wlan_test# ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 02:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 scope global wlan0
valid_lft forever preferred_lft forever
inet6 fe80::ff:fe00:0/64 scope link
valid_lft forever preferred_lft forever
创建wpa_supplicant.conf文件,文件内容如下:
#wpa_supplicant.conf
network={
ssid="wifi_simu_test"
key_mgmt=WPA-PSK
psk="helloworld"
}
在STA侧Terminal中输入如下命令
wpa_supplicant-2.10/wpa_supplicant/wpa_supplicant -B -i wlan1 -c wpa_supplicant.conf
dhcp-4.4.3/client/dhclient -i wlan1
通过ip a
查看wlan1信息
root@w512-pc:/home/w512/wlan_test# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:9d:d4:e9 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
valid_lft 84831sec preferred_lft 84831sec
inet6 fe80::76da:36d3:6f9f:1ed7/64 scope link noprefixroute
valid_lft forever preferred_lft forever
4: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 02:00:00:00:01:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.80/24 brd 192.168.100.255 scope global dynamic wlan1
valid_lft 484sec preferred_lft 484sec
5: hwsim0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ieee802.11/radiotap 12:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
可见, STA获取到了IP地址为192.168.100.80
从AP侧ping
STA侧
root@w512-pc:/home/w512/wlan_test# ping 192.168.100.80
PING 192.168.100.80 (192.168.100.80) 56(84) bytes of data.
64 bytes from 192.168.100.80: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from 192.168.100.80: icmp_seq=2 ttl=64 time=0.076 ms
64 bytes from 192.168.100.80: icmp_seq=3 ttl=64 time=0.084 ms
64 bytes from 192.168.100.80: icmp_seq=4 ttl=64 time=0.086 ms
64 bytes from 192.168.100.80: icmp_seq=5 ttl=64 time=0.067 ms