LoginSignup
27
22

More than 5 years have passed since last update.

Raspberry Pi 3 Wireless Access Point 覚書

Last updated at Posted at 2016-10-03

前提とする環境

  • ハードウェア: Raspberry Pi 3 Model B
  • OS: Raspbian
  • 有線 LAN から無線 LAN へパケット転送を行う。

パッケージのインストール

apt-get install hostapd isc-dhcp-server haveged iw

iw については Raspbian に標準でインストールされている様子。

ネットワークの構築

/etc/network/interfaces を編集する。

/etc/network/interfaces
allow-hotplug wlan0
iface wlan0 inet manual
    # wpa-conf / etc/wpa_supplicant/wpa_supplicant.conf

wpa-conf / etc/wpa_supplicant/wpa_supplicant.conf の行をコメントアウトするだけ。
ubuntu でアクセスポイントを作る場合はネットワーク定義を
/etc/network/interfaces に記述するが、 Raspbian の場合は不要。

/etc/dhcpcd.conf を編集する。
後述の /etc/dhcp/dhcpd.conf と間違えそうになるが、
全く別物なので要注意。

/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.2.1/24

パケットの転送設定

/etc/sysctl.conf を編集する。

/etc/sysctl.conf
# なりすまし攻撃を防止する。
net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.all.rp_filter=1

# TCP/IP SYN クッキーを無効にする。
net.ipv4.tcp_syncookies=0

# IPv4 パケット転送を有効にする。
net.ipv4.ip_forward=1

# ICMP リダイレクトを受信しない。(MITM 攻撃の防止)
net.ipv4.conf.all.accept_redirects=0
net.ipv6.conf.all.accept_redirects=0

# IP ソースルートパケットを受信しない。
net.ipv4.conf.all.accept_source_route=0
net.ipv6.conf.all.accept_source_route=0

# 有り得ないパケットを記録しない。
net.ipv4.conf.all.log_martians=0

下記のコマンドを実行する。

sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

iptables を設定する。

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sh -c "iptables-save > /etc/iptables.ipv4.nat"

転送設定が維持されるようにスクリプトを作り、
/etc/network/if-pre-up.d/iptables と命名する。

/etc/network/if-pre-up.d/iptables
#!/bin/sh
iptables-restore < /etc/iptables.ipv4.nat
exit 0

isc-dhcp-server の設定

/etc/dhcp/dhcpd.conf を編集する。

/etc/dhcp/dhcpd.conf
authoritative;

subnet 192.168.2.0 netmask 255.255.255.0{
  range 192.168.2.10 192.168.2.50;
  option routers 192.168.2.1
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "任意のドメイン名";
  option domain-name-servers 8.8.8.8,8.8.4.4;
}

/etc/default/isc-dhcp-server を編集する。

/etc/default/isc-dhcp-server
INTERFACES="wlan0"

hostapd の設定

/etc/hostapd/hostapd.conf を編集する。

/etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=任意の ssid
country_code=JP
hw_mode=g
ieee80211d=1
channel=6
auth_algs=1
ignore_broadcast_ssid=0
disassoc_low_ack=1
ieee80211n=1
ht_capab=[HT40] [SHORT-GI-20] [DSSS_CCK-40]
require_ht=0
wpa=2
wpa_passphrase=任意のパスワード
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_group_rekey=86400
max_num_sta=3

/etc/default/hostapd を編集する。

/etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"
DAEMON_OPTS="-dd"

動作確認

Raspberry Pi 3 を再起動した後、下記のコマンドでサービスの状態を確認する。

service hostapd status
service isc-dhcp-server status

hostapd も isc-dhcp-server もステータスが active(running) になっており、
他の端末から WiFi へ接続できれば成功。

参考文献

27
22
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
22