arm使用hostapd创建热点

    xiaoxiao2023-10-17  226

    硬件:IMX6的arm板、树莓派 Raspberry Pi 无线USB网卡 EDUP EP-N8508GS 黄金版 免驱,并且已经带了驱动

    关于wifi连接可以看 https://blog.csdn.net/chenjk10/article/details/90521093

     

    普通热点

    开启普通热点,只有一个无线网卡,无法上网的热点

    # 如果启动了wpa_supplicant,先要停止 killall wpa_supplicant > /dev/null 2>&1 # 在停止了wpa_supplicant后,关联的网卡会被变成禁用状态 # 注意:如果使用代码停止wpa_supplicant,需要休眠等待一下,要等待wpa_supplicant真的退出并且网卡状态变化

    修改或者创建hostapd.conf文件,这个文件一般在/etc/hostapd.conf

    ##### hostapd configuration file ############################################## interface=wlan0 ctrl_interface=/var/run/hostapd ssid=MyAPName # Channel number (IEEE 802.11) # (default: 0, i.e., not set) # Please note that some drivers do not use this value from hostapd and the # channel will need to be configured separately with iwconfig. # # If CONFIG_ACS build option is enabled, the channel can be selected # automatically at run time by setting channel=acs_survey or channel=0, both of # which will enable the ACS survey based algorithm. channel=6 # Enable WPA. Setting this variable configures the AP to require WPA (either # WPA-PSK or WPA-RADIUS/EAP based on other configuration). For WPA-PSK, either # wpa_psk or wpa_passphrase must be set and wpa_key_mgmt must include WPA-PSK. # Instead of wpa_psk / wpa_passphrase, wpa_psk_radius might suffice. # For WPA-RADIUS/EAP, ieee8021x must be set (but without dynamic WEP keys), # RADIUS authentication server must be configured, and WPA-EAP must be included # in wpa_key_mgmt. # This field is a bit field that can be used to enable WPA (IEEE 802.11i/D3.0) # and/or WPA2 (full IEEE 802.11i/RSN): # bit0 = WPA # bit1 = IEEE 802.11i/RSN (WPA2) (dot11RSNAEnabled) wpa=2 # WPA pre-shared keys for WPA-PSK. This can be either entered as a 256-bit # secret in hex format (64 hex digits), wpa_psk, or as an ASCII passphrase # (8..63 characters) that will be converted to PSK. This conversion uses SSID # so the PSK changes when ASCII passphrase is used and the SSID is changed. # wpa_psk (dot11RSNAConfigPSKValue) # wpa_passphrase (dot11RSNAConfigPSKPassPhrase) #wpa_psk=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef wpa_passphrase=12345678 # Set of accepted key management algorithms (WPA-PSK, WPA-EAP, or both). The # entries are separated with a space. WPA-PSK-SHA256 and WPA-EAP-SHA256 can be # added to enable SHA256-based stronger algorithms. # (dot11RSNAConfigAuthenticationSuitesTable) #wpa_key_mgmt=WPA-PSK WPA-EAP wpa_key_mgmt=WPA-PSK # Set of accepted cipher suites (encryption algorithms) for pairwise keys # (unicast packets). This is a space separated list of algorithms: # CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0] # TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] # Group cipher suite (encryption algorithm for broadcast and multicast frames) # is automatically selected based on this configuration. If only CCMP is # allowed as the pairwise cipher, group cipher will also be CCMP. Otherwise, # TKIP will be used as the group cipher. # (dot11RSNAConfigPairwiseCiphersTable) # Pairwise cipher for WPA (v1) (default: TKIP) wpa_pairwise=TKIP CCMP #wpa_pairwise=CCMP # Pairwise cipher for RSN/WPA2 (default: use wpa_pairwise value) #rsn_pairwise=CCMP rsn_pairwise=CCMP TKIP #rsn_pairwise=CCMP # Time interval for rekeying GTK (broadcast/multicast encryption keys) in # seconds. (dot11RSNAConfigGroupRekeyTime) #wpa_group_rekey=600 #wpa_group_rekey=86400 # auto add current network card into bridge #bridge=br0 # IEEE 802.11 specifies two authentication algorithms. hostapd can be # configured to allow both of these or only one. Open system authentication # should be used with IEEE 802.1X. # Bit fields of allowed authentication algorithms: # bit 0 = Open System Authentication # bit 1 = Shared Key Authentication (requires WEP) #auth_algs=3 ##### Wi-Fi Protected Setup (WPS) ############################################# eap_server=1 # WPS state # 0 = WPS disabled (default) # 1 = WPS enabled, not configured # 2 = WPS enabled, configured wps_state=2 uuid=12345678-9abc-def0-1234-56789abcdef0 # Device Name # User-friendly description of device; up to 32 octets encoded in UTF-8 device_name=RTL8192CU # Manufacturer # The manufacturer of the device (up to 64 ASCII characters) manufacturer=Realtek # Model Name # Model of the device (up to 32 ASCII characters) model_name=RTW_SOFTAP # Model Number # Additional device description (up to 32 ASCII characters) model_number=WLAN_CU # Serial Number # Serial number of the device (up to 32 characters) serial_number=12345 # Primary Device Type # Used format: <categ>-<OUI>-<subcateg> # categ = Category as an integer value # OUI = OUI and type octet as a 4-octet hex-encoded value; 0050F204 for # default WPS OUI # subcateg = OUI-specific Sub Category as an integer value # Examples: # 1-0050F204-1 (Computer / PC) # 1-0050F204-2 (Computer / Server) # 5-0050F204-1 (Storage / NAS) # 6-0050F204-1 (Network Infrastructure / AP) device_type=6-0050F204-1 # OS Version # 4-octet operating system version number (hex string) os_version=01020300 # Config Methods # List of the supported configuration methods config_methods=label display push_button keypad ##### default configuration ####################################### driver=rtl871xdrv beacon_int=100 # Operation mode (a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g, # ad = IEEE 802.11ad (60 GHz); a/g options are used with IEEE 802.11n, too, to # specify band) # Default: IEEE 802.11b hw_mode=g ieee80211n=1 wme_enabled=1 ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+] max_num_sta=8

    部分配置说明

    # 设置使用的网卡 interface=wlan0 # 配置热点名称 ssid=MyAPName # 热点支持的加密类型, # bit0 = WPA # bit1 = IEEE 802.11i/RSN (WPA2) (dot11RSNAEnabled) wpa=2 # 热点加密类型 wpa_key_mgmt=WPA-PSK # 热点密码 wpa_passphrase=12345678 # 接受的密匙类型 wpa_pairwise=TKIP CCMP rsn_pairwise=CCMP TKIP

    官方配置文件:http://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf 

    热点要使用DHCP功能,所以还要配置DHCP配置文件,使用是udhcpd,配置文件在/etc/udhcpd.conf

    如果使用静态IP地址也可以不使用DHCP

    # Sample udhcpd configuration file (/etc/udhcpd.conf) # The start and end of the IP lease block start 192.168.0.150 #default: 192.168.0.20 end 192.168.0.240 #default: 192.168.0.254 # The interface that udhcpd will use interface wlan0 #default: eth0 # The maximim number of leases (includes addressesd reserved # by OFFER's, DECLINE's, and ARP conficts max_leases 21 #default: 254 # If remaining is true (default), udhcpd will store the time # remaining for each lease in the udhcpd leases file. This is # for embedded systems that cannot keep time between reboots. # If you set remaining to no, the absolute time that the lease # expires at will be stored in the dhcpd.leases file. #remaining yes #default: yes # The time period at which udhcpd will write out a dhcpd.leases # file. If this is 0, udhcpd will never automatically write a # lease file. (specified in seconds) #auto_time 7200 #default: 7200 (2 hours) # The amount of time that an IP will be reserved (leased) for if a # DHCP decline message is received (seconds). #decline_time 3600 #default: 3600 (1 hour) # The amount of time that an IP will be reserved (leased) for if an # ARP conflct occurs. (seconds #conflict_time 3600 #default: 3600 (1 hour) # How long an offered address is reserved (leased) in seconds #offer_time 60 #default: 60 (1 minute) # If a lease to be given is below this value, the full lease time is # instead used (seconds). #min_lease 60 #defult: 60 # The location of the leases file #lease_file /var/lib/misc/udhcpd.leases #defualt: /var/lib/misc/udhcpd.leases # The location of the pid file #pidfile /var/run/udhcpd.pid #default: /var/run/udhcpd.pid # Everytime udhcpd writes a leases file, the below script will be called. # Useful for writing the lease file to flash every few hours. #notify_file #default: (no script) #notify_file dumpleases # <--- usefull for debugging # The following are bootp specific options, setable by udhcpd. #siaddr 192.168.0.22 #default: 0.0.0.0 #sname zorak #default: (none) #boot_file /var/nfs_root #default: (none) # The remainer of options are DHCP options and can be specifed with the # keyword 'opt' or 'option'. If an option can take multiple items, such # as the dns option, they can be listed on the same line, or multiple # lines. The only option with a default is 'lease'. #Examles opt dns 116.116.116.116 option subnet 255.255.255.0 opt router 192.168.0.1 #opt wins 192.168.10.10 option dns 129.219.13.81 # appened to above DNS servers for a total of 3 option domain local option lease 864000 # 10 days of seconds # Currently supported options, for more info, see options.c #opt subnet #opt timezone #opt router #opt timesvr #opt namesvr #opt dns #opt logsvr #opt cookiesvr #opt lprsvr #opt bootsize #opt domain #opt swapsvr #opt rootpath #opt ipttl #opt mtu #opt broadcast #opt wins #opt lease #opt ntpsrv #opt tftp #opt bootfile # Static leases map #static_lease 00:60:08:11:CE:4E 192.168.0.54 #static_lease 00:60:08:11:CE:3E 192.168.0.44

    部分参数说明:

    # 使用的无线网卡 interface wlan0 # IP池范围 start 192.168.0.100 end 192.168.0.240 # 路由地址 opt router 192.168.0.1

    官方配置文件说明:https://udhcp.busybox.net/udhcpd.conf 

    开始配置热点:

    # 启用网卡 ifconfig wlan0 up # 给无线网卡设置IP地址 ifconfig wlan0 192.168.0.1 # 启动DHCP,必须先设置无线网卡地址,不然启动udhcp会失败 udhcpd /etc/udhcpd.conf # 启动热点 hostapd /etc/hostapd.conf -B

     

    桥接方式建立热点

    配置文件和上面的一样,这里桥接的是以太网卡eth0。

    # 设置两个网卡IP为0.0.0.0 ifconfig eth0 0.0.0.0 ifconfig wlan0 0.0.0.0 # 如果网桥已经存在则删除 brctl delbr br0 >/dev/null 2>&1 # 新建网桥br0 brctl addbr br0 # Stop STP(spanning tree protocol, this protocol seems to prevent the network from generating loops, because the self-defined network, there should be no loop, this protocol can be stopped) brctl stp br0 off # 添加无线网卡和以太网卡到网桥br0中 brctl addif br0 wlan0 brctl addif br0 eth0 # Set the bridge forwarding delay time brctl setfd br0 0 # 设置网桥静态IP地址 ifconfig br0 192.168.1.205 netmask 255.255.255.0 # 查看当前网桥配置 brctl show # 启动hostapd hostapd /etc/hostapd.conf -B # 给无线网卡设置IP地址,这个和udhcpd.conf中配置的路由地址一样 ifconfig wlan0 192.168.0.1 # 启动udhcpd, 必须先设置wlan0的IP地址,不然这步会失败 udhcpd /etc/udhcpd.conf

    感觉上面有些步骤似乎是多余的,桥接eth0后,连通了eth0的路由,实际上似乎也是使用eth0连接的外部路由的DHCP,arm板上似乎也没必要开DHCP服务。

    启动热点后,连接并使用热点可能会有错误br0: received packet on eth0 with own address as source address,看了一下网桥br0和eth0的MAC地址是一样的,估计br0是复制了eth0的MAC地址,这里需要修改eth0的MAC地址,修改br0的MAC地址似乎还是继续报错,修改命令为:

    ifconfig eth0 hw ether dc:07:c1:00:ed:BB

    尝试了一下,去掉udhcpd部分,修改成如下:

    # 设置两个网卡IP为0.0.0.0 ifconfig eth0 0.0.0.0 ifconfig wlan0 0.0.0.0 # 如果网桥已经存在则删除 brctl delbr br0 >/dev/null 2>&1 # 新建网桥br0 brctl addbr br0 # Stop STP(spanning tree protocol, this protocol seems to prevent the network from generating loops, because the self-defined network, there should be no loop, this protocol can be stopped) brctl stp br0 off # 添加无线网卡和以太网卡到网桥br0中 brctl addif br0 wlan0 brctl addif br0 eth0 # Set the bridge forwarding delay time brctl setfd br0 0 # 启用网桥 ifconfig br0 up # 查看当前网桥配置 brctl show # 启动hostapd hostapd /etc/hostapd.conf -B # 由于没有设置静态IP,这里使用udhcpc更新IP地址和DNS # -n Exit with failure if lease is not immediately obtained udhcpc -i br0 -n

    完成后使用ifconfig显示的网卡配置为,可以正常连接热点上网

    br0 Link encap:Ethernet HWaddr dc:07:c1:00:ed:85 inet addr:192.168.1.62 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1274 errors:0 dropped:0 overruns:0 frame:0 TX packets:13 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:65848 (64.3 KiB) TX bytes:1590 (1.5 KiB) eth0 Link encap:Ethernet HWaddr dc:07:c1:00:ed:85 inet6 addr: fe80::de07:c1ff:fe00:ed85/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4801 errors:0 dropped:0 overruns:0 frame:0 TX packets:271 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:395515 (386.2 KiB) TX bytes:44022 (42.9 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:9 errors:0 dropped:0 overruns:0 frame:0 TX packets:9 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:612 (612.0 B) TX bytes:612 (612.0 B) wlan0 Link encap:Ethernet HWaddr e8:4e:06:6f:87:dc inet6 addr: fe80::ea4e:6ff:fe6f:87dc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:240 errors:0 dropped:7 overruns:0 frame:0 TX packets:1113 errors:0 dropped:7 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:183837 (179.5 KiB) TX bytes:161991 (158.1 KiB)

     下面是一个arm上的脚本,用于启动wifi热点,原来有个脚本不过有些问题,我修改了一下

    #!/bin/sh # Check that the /etc/udhcpd.conf file exists and can be executed if [ ! -x /usr/sbin/hostapd ] then echo "/usr/sbin/hostapd no exist or can't execute" exit 0 fi # check network card wlanX ifconfig -a|grep wlan > /dev/null if [[ $? -ne 0 ]] then echo no device wlanX exit 0 fi # Gets the full name of the wlan and assigns it to wlanx wlanx=`ifconfig -a|grep wlan|awk '{print $1}'` if [[ $? -ne 0 ]] then echo failed to get device wlanX name exit 0 fi # Check that the /etc/hostapd.conf file exists and can be write if [ ! -w /etc/hostapd.conf ] then echo "/etc/hostapd.conf no exist or can't write" exit 0 fi # Check that the /etc/udhcpd.conf file exists and can be write if [ ! -w /etc/udhcpd.conf ] then echo "/etc/udhcpd.conf no exist or can't write" exit 0 fi # network card configure file: /etc/network/interfaces #busybox sed -i "s/eth0/br0/g" /etc/network/interfaces #busybox sed -i "s/auto wlan0/#auto wlan0/g" /etc/network/interfaces #busybox sed -i "s/iface wlan0 inet dhcp/#iface wlan0 inet dhcp/g" /etc/network/interfaces # check command if [ "$1" != "start" -a "$1" != "stop" -a "$1" != "restart" ] then echo paramter error.only accept start/stop/restart. fi # Close all related programs if [ "$1" = "stop" -o "$1" = "restart" -o "$1" = "start" ] then echo "Stopping the hostapd and udhcpd and wpa_supplicant : " killall hostapd >/dev/null 2>&1 killall udhcpd >/dev/null 2>&1 killall wpa_supplicant > /dev/null 2>&1 fi if [ "$1" = "start" -o "$1" = "restart" ] then # /etc/hostapd.conf echo "Starting the hostapd and udhcpd : " wlan_old=`cat /etc/hostapd.conf|grep '^interface'` wlan_new="interface="$wlanx busybox sed -i "s/$wlan_old/$wlan_new/g" /etc/hostapd.conf # /etc/udhcpd.conf #wlan_old=`cat /etc/udhcpd.conf|grep '^interface'` #wlan_new="interface "$wlanx" #default: eth0" #busybox sed -i "s/$wlan_old/$wlan_new/g" /etc/udhcpd.conf #start bridge eth0 ifconfig eth0 0.0.0.0 ifconfig $wlanx 0.0.0.0 ifconfig $wlanx up # Delete possible Bridges brctl delbr br0 >/dev/null 2>&1 # add new bridge br0 brctl addbr br0 # Stop STP(spanning tree protocol, this protocol seems to prevent the network from generating loops, because the self-defined network, there should be no loop, this protocol can be stopped) brctl stp br0 off # Add the port eth0 and the wireless port to the bridge brctl addif br0 $wlanx brctl addif br0 eth0 # Set the bridge forwarding delay time brctl setfd br0 0 # set static ip addr #ifconfig br0 192.168.1.205 netmask 255.255.255.0 # enabel br0 ifconfig br0 up # Displays the current bridge configuration brctl show # Check the status of network card,Network cable plug status #ifplugd -I -i eth0 -d 0 -t 0 -r /usr/sbin/auto_bridge.sh # start hostapd hostapd /etc/hostapd.conf -B # set wlan new ip #ifconfig $wlanx 192.168.0.1 # start udhcpd, must set $wlan static ip addr before start udhcpd #udhcpd /etc/udhcpd.conf # get ip from dhcp server udhcpc -i br0 -n fi echo 'wifi ap shell exit'

     

    NAT方式建立热点:

    这种方式试的时候失败了,似乎是arm不支持

    # start NAT sysctl net.ipv4.ip_forward=1 #echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

    上面这个命令一直失败,似乎是iptables不支持

    下面是一个shell脚本,以NAT方式启动热点,因为不支持,没测试过

    #!/bin/sh # Check that the /etc/udhcpd.conf file exists and can be executed if [ ! -x /usr/sbin/hostapd ] then echo "/usr/sbin/hostapd no exist or can't execute" exit 0 fi # check network card wlanX ifconfig -a|grep wlan > /dev/null if [[ $? -ne 0 ]] then echo no device wlanX exit 0 fi # Gets the full name of the wlan and assigns it to wlanx wlanx=`ifconfig -a|grep wlan|awk '{print $1}'` if [[ $? -ne 0 ]] then echo failed to get device wlanX name exit 0 fi # Check that the /etc/hostapd.conf file exists and can be write if [ ! -w /etc/hostapd.conf ] then echo "/etc/hostapd.conf no exist or can't write" exit 0 fi # Check that the /etc/udhcpd.conf file exists and can be write if [ ! -w /etc/udhcpd.conf ] then echo "/etc/udhcpd.conf no exist or can't write" exit 0 fi # network card configure file: /etc/network/interfaces #busybox sed -i "s/eth0/br0/g" /etc/network/interfaces #busybox sed -i "s/auto wlan0/#auto wlan0/g" /etc/network/interfaces #busybox sed -i "s/iface wlan0 inet dhcp/#iface wlan0 inet dhcp/g" /etc/network/interfaces # check command if [ "$1" != "start" -a "$1" != "stop" -a "$1" != "restart" ] then echo paramter error.only accept start/stop/restart. fi # Close all related programs if [ "$1" = "stop" -o "$1" = "restart" -o "$1" = "start" ] then echo "Stopping the hostapd and udhcpd and wpa_supplicant : " killall hostapd >/dev/null 2>&1 killall udhcpd >/dev/null 2>&1 killall wpa_supplicant > /dev/null 2>&1 fi if [ "$1" = "start" -o "$1" = "restart" ] then # /etc/hostapd.conf echo "Starting the hostapd and udhcpd : " wlan_old=`cat /etc/hostapd.conf|grep '^interface'` wlan_new="interface="$wlanx busybox sed -i "s/$wlan_old/$wlan_new/g" /etc/hostapd.conf # /etc/udhcpd.conf wlan_old=`cat /etc/udhcpd.conf|grep '^interface'` wlan_new="interface "$wlanx" #default: eth0" busybox sed -i "s/$wlan_old/$wlan_new/g" /etc/udhcpd.conf #start bridge eth0 ifconfig eth0 0.0.0.0 ifconfig $wlanx 0.0.0.0 # Delete possible Bridges brctl delbr br0 >/dev/null 2>&1 # Check the status of network card,Network cable plug status #ifplugd -I -i eth0 -d 0 -t 0 -r /usr/sbin/auto_bridge.sh ifplugd -I -i eth0 -d 0 -t 0 -r `udhcpc -i eth0 -n` # start hostapd hostapd /etc/hostapd.conf -B # set wlan new ip ifconfig $wlanx 192.168.0.1 # start udhcpd, must set $wlan static ip addr before start udhcpd udhcpd /etc/udhcpd.conf # start NAT sysctl net.ipv4.ip_forward=1 #echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE fi echo 'wifi ap shell exit'

     

     

    参考:

    http://w1.fi/

    http://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf

    http://hantianliliang.blog.sohu.com/304891332.html

    http://hantianliliang.blog.sohu.com/304879882.html

    最新回复(0)