2017年4月26日 星期三

利用 FreeBSD for Raspberry Pi 2 來製作無線網路基地台


俗諺說,每個 IT 人家裡都有一塊 Raspberry Pi,你家是不是也有一塊閒置的 Raspberry Pi 呢? 只要再加上一個無線網路卡就可以把它當作無線網路基地台使用 (俗諺說,每個 IT 人家裡也都會同時買一塊 Edimax 無線網卡。XD)

那為什麼要用 FreeBSD 不用 Raspbian 呢? 因為 FreeBSD 的無線網卡驅動程式就支援hostapd,如果你用 Raspbian,您還要去下載製造商的 vendor driver。再來就是 FreeBSD hostapd、 dhcp、防火牆設定相對簡單許多。


附上 Raspbian的設定給您參考,http://blog.itist.tw/2014/05/hotspotd.html


以下的設定,是假設您家中的網路是 DHCP 自動分配 ip,設定出來的無線基地台是路由模式, DHCP自動分配 ip 出去。


準備硬體:

1. Raspberry Pi 2,目前 Raspberry Pi 1、Raspberry Pi 3支援有問題,或許之後會有改善,請您自己查詢。

2. USB無線網路卡,可以查看 FreeBSD rtwn driver 有支援的就可以。
https://www.freebsd.org/cgi/man.cgi?query=rtwn&apropos=0&sektion=0&manpath=FreeBSD+12-current&arch=default&format=html


燒錄 image


接下來就到官網下載最新的 Raspberry Pi 2 image


https://download.freebsd.org/ftp/snapshots/arm/armv6/ISO-IMAGES/12.0/


把 image 寫入 micro SD card

插上電源開機

預設'帳號密碼是 root/root,freebsd/freebsd

FreeBSD 預設是無法用 root ssh 遠端登入。

設定 /boot/loader.conf


加入這兩行(這是 rtwn driver,如果網卡不是rtwn driver,需要不同設定):

if_rtwn_load="YES"
if_rtwn_usb_load="YES"

如果您希望開機別等 10 秒, 若要等 2 秒就好, 加入這行:

autoboot_delay=2

安裝dhcp server 套件:

pkg install isc-dhcp43-server


設定 /etc/pf.conf:


# $FreeBSD: head/share/examples/pf/pf.conf 293862 2016-01-14 01:32:17Z kevlo $
# $OpenBSD: pf.conf,v 1.34 2007/02/24 19:30:59 millert Exp $
#
# See pf.conf(5) and /usr/share/examples/pf for syntax and examples.
# Remember to set gateway_enable="YES" and/or ipv6_gateway_enable="YES"
# in /etc/rc.conf if packets are to be forwarded between interfaces.

ext_if="ue0"
int_if="wlan0"

#table <spamd-white> persist

set skip on lo

scrub in

nat-anchor "ftp-proxy/*"
rdr-anchor "ftp-proxy/*"
nat on $ext_if inet from !($ext_if) -> ($ext_if:0)
rdr pass on $int_if proto tcp to port ftp -> 127.0.0.1 port 8021

#anchor "ftp-proxy/*"
#block in
#pass out

pass quick on { $int_if } no state
antispoof quick for { lo $int_if }



設定 /etc/hostapd.conf:

interface=wlan0
debug=1
ssid=rpi2-ap
auth_algs=3
wpa=1
wpa_passphrase=rpi2apwifi
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP


上述設定的 ssid 和wifi密碼(wpa_passphrase)請改成你自己喜好的設定。


設定/usr/local/etc/dhcpd.conf:


# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
#option domain-name "example.org";
option domain-name-servers 8.8.8.8, 8.8.4.4;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

subnet 10.0.1.0 netmask 255.255.255.0 {
  range 10.0.1.10 10.0.1.250;
  option routers 10.0.1.1;
}



將以下幾行加入 /etc/rc.conf:


ftpproxy_enable="YES"
hostapd_enable="YES"
pf_enable="YES"
gateway_enable="YES"
dhcpd_enable="YES"
dhcpd_flags="-q"
dhcpd_conf="/usr/local/etc/dhcpd.conf"
dhcpd_ifaces="wlan0"
dhcpd_withumask="022"
wlans_rtwn0="wlan0"
create_args_wlan0="wlanmode ap channel 6 mode 11ng"
ifconfig_wlan0="inet 10.0.1.1 netmask 255.255.255.0"


重新開機就大功告成了!!


----------------------------------------------

後記:


我在設定過程一直無法成功,後來發現是透過 stfp 傳進 Raspberry Pi 的設定檔有問題,砍掉自己重新手動輸入後,就可以了。


如果您無法成功,可以用 top 看看您的 hostapd 有沒有執行成功,如果不成功,執行 hostapd -B /etc/hostapd.conf 看看設定檔有沒有問題。


如果可以成功連線進去Raspberry Pi , client 的網路卻連不出去。可以執行 service pf restart看看pf是不是有順利執行。


SD card也可能是個問題來源,可以試看看不同的 micro SD card。

2 則留言: