RTL8188ETVによるOrange Piのルータ化

ブログ記事<RTL8188ETVによるOrange Piのルータ化>の補足です。アクセスポイントとして利用できるということは、ボードへのアクセスとインストールしたシステム設定を容易にしユーザビリティを向上させます。

RTL8188ETVのドライバーはRTL8188EUと同一です。

http://linux-sunxi.org/Wifi#Debian.2Fubuntu_with_NetworkManager

lsmodコマンドでドライバーが読み込まれていないようであれば、以下の作業が必要となります。(modprobeコマンドでも追加できます。)

$ sudo pico /etc/modules-load.d/modules.conf

8188eu --->wifiドングルのタイプにより指定します。
sunxi-cir --->ir(赤外線リモート)を有効にするために必要です。

カーネルモジュールは以下のファイルを参照して下さい。

/lib/modules/.xx.xx-sunxi/modules.order

今回参考としたサイトは以下の通りです。

https://wiki.archlinuxjp.org/index.php/Dnsmasq

$sudo iw listによる機能の確認で1つのインターフェイスでAP+STAモードの組合せで使用できるドングルもあります。この場合はinterface combinationsの項目に詳細が表示されます。

■ 対象ハードとOS

Orange Pi PC: ARMBIAN 5.25 stable Debian GNU/Linux 8 (jessie) 3.4.113-sun8i

■ 新規インストールソフト

hostapdはデフォルトでインストールされているため下記2点を新規インストールして下さい。

dnsmasq:DNS・DHCPサーバとして機能

iptables-persistent:iptablesによる設定事項を保存し起動時に読み込ませるのに必要です。

$ sudo apt-get install dnsmasq iptables-persistent

iptables-persistentでは2つの設定画面が表示されるので両方共yesを選択して下さい。

■ 設定フロー

設定フローは以下の通りです。

  1. iwlistによるWiFiモジュールの機能確認
  2. NetworkManagerの無効化
  3. hostapd(アクセスポイントデーモン)の設定
  4. ネットワークインターフェイスの設定
  5. dnsmasqの設定(投稿された記事も参照して下さい)
  6. NATとiptablesの設定

1. iw listによるWiFiモジュールの機能確認

iw listコマンドによりWiFiモジュールの機能確認をします。

$ sudo iw list

出力内容からSupported interface modeの項を確認します。

	Supported interface modes:
		 * IBSS
		 * managed
		 * AP
		 * monitor
		 * P2P-client
		 * P2P-GO
	software interface modes (can always be added):
		 * monitor
	interface combinations are not supported --->単一デバイスでのインターフェイスの組合せ(APモードとクライアントモードの同時使用)は出来ません。

APがリストアップされていればアクセスポイントとして利用できます。

RTL8188ETVのドライバにより、ワイヤレスインターフェイスを複数設定(CONCURRENT_MODEを有効化してコンパイルされたドライバにより、wlan0、wlan1のインターフェイスが利用できます)できるようになっているため、各々のインターフェイスにAPモードとクライアントモードを別々に割り当てることが出来るようになっています。

iw list コマンドにより、複数設定されている場合、phy0, phy1のようにデバイス毎に詳細が表示されます。

インターフェイスが複数設定されていない場合、ソースコードよりコンパイルする必要があります。Armbianの最新カーネル(2019/1/10時点)では、CONCURRENT_MODEが有効になっていないようです。

ソースコードからのコンパイルについては、ブログ記事と本フォーラム記事に詳細を掲載します。

2. NetworkManagerの無効化

ArmbianではデフォルトでインストールされているNetworkManagerですが、デスクトップ環境でネットワーク設定をしないのであれば無効化した方がネットワーク設定の自由度が上がります。システムサービスとして起動しているのでサービス停止・無効とし、サービスデーモンを再読込します。

$ sudo systemctl stop NetworkManager
$ sudo systemctl disable NetworkManager
$ sudo systemctl daemon-reload

NetworkManagerサービスを無効化することで従来のNetworkサービス(networking.service)が自動的に有効となります(メモリー消費量も50MB以上は削減されると思います)。

同様に以下のサービスも無効化します。

network-manager.service                      disabled 
NetworkManager-dispatcher.service            disabled 
NetworkManager-wait-online.service           disabled 

ネットワークの設定は/etc/network/interfacesファイルにより行います。

3. hostapd(アクセスポイントデーモン)の設定

hostapd起動時の設定ファイルを指定します。
https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf

$ sudo pico /etc/default/hostapd

以下DAEMON_CONF="/etc/hostapd/hostapd.conf"を指定します。

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF="/etc/hostapd/hostapd.conf"

hostapd設定ファイルを作成します。

$ sudo pico /etc/hostapd/hostapd.conf

ssidによるアクセス先とwpa_passphraseによるパスワードなどを指定します。interfaceには$sudo iwconfigコマンドにより表示される2つのWiFiネットワークインターフェイスwlan0またはwlan1を指定して下さい。(注意:チャンネルは自動に設定する場合channel=0としますが、ドライバが未対応だとエラーになります。)

ssid=access_point_name
interface=wlan0
hw_mode=g
# 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=5
driver=nl80211
logger_syslog=0
logger_syslog_level=0
wmm_enabled=1
ieee80211n=1
wpa=3
preamble=1
# wpa_psk=xxxxx
wpa_passphrase=pass_phrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
noscan=1
# ht_capab=[HT40-][SHORT-GI-40][SHORT-GI-40][DSSS_CCK-40]
country_code=JP
# ieee80211d=1

4. ネットワークインターフェイスの設定

/etc/network/interfacesファイルによるネットワークインターフェイスの設定を行います。

$ sudo pico /network/interfaces

設定内容は以下の通りですが内容は任意です。

# Wired adapter #1
allow-hotplug eth0
#  no-auto-down eth0
iface eth0 inet dhcp
#  address 192.168.0.100
#  netmask 255.255.255.0
#  gateway 192.168.0.1
dns-nameservers 8.8.8.8 8.8.4.4
#	hwaddress ether # if you want to set MAC manually
#	pre-up /sbin/ifconfig eth0 mtu 3838 # setting MTU for DHCP, static just: mtu 3838


# Wireless adapter #1
# Armbian ships with network-manager installed by default. To save you time
# and hassles consider using 'sudo nmtui' instead of configuring Wi-Fi settings
# manually. The below lines are only meant as an example how configuration could
# be done in an anachronistic way:
# 
#---- Realtek 8188eu wlan interface AP (access defined in /etc/hostapd/hostapd.conf)
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.14.164
netmask 255.255.255.0
network 192.168.14.0
#  gateway 192.168.0.1
#  dns-nameservers 8.8.8.8 8.8.4.4
#  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# Disable power saving on compatible chipsets (prevents SSH/connection dropouts over WiFi)
#  wireless-mode Managed
#  wireless-power off

#----Realtek 8188eu wlan interface client
allow-hotplug wlan1
iface wlan1 inet dhcp
#  address 192.168.10.164
#  netmask 255.255.255.0
#  network 192.168.10.0
#  gateway 192.168.10.1
dns-nameservers 8.8.8.8 8.8.4.4
   wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# Disable power saving on compatible chipsets (prevents SSH/connection dropouts over WiFi)
#  wireless-mode Managed
#  wireless-power off

# Local loopback
auto lo
iface lo inet loopback

wlan0をアクセスポイント、wlan1をインターネットに接続されている市販ルータのクライアントとして設定しています。

wlan1から市販ルータへの接続はwpa_supplicantにより処理されるのでwpa_supplicant.confを編集します。

以下コマンドによりアクセスポイントへのパスワードを暗号化します。

$ wpa_passphrase MYSSID passphrase

network={
    ssid="MYSSID"
    #psk="passphrase"
    psk=59e0d07fa4c7741797a4e394f38a5c321e3bed51d54ad5fcbd3f84bc7415d73d
}

出力された内容を以下のファイルに書き込みます。

$ sudo pico /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1

network={
    ssid="MYSSID"
    #psk="passphrase"
    psk=59e0d07fa4c7741797a4e394f38a5c321e3bed51d54ad5fcbd3f84bc7415d73d
    key_mgmt=WPA-PSK
    priority=99 
}

ネットワークの起動・停止コマンド(wlan0)

sudo ifconfig wlan0 up

sudo ifconfig wlan0 down

5. dnsmasqの設定

アクセスポイントとして設定したモジュールに接続するクライアント端末にIPアドレスを割り当てるために、ハード本体にDHCPサーバ機能を持たせる必要があります。isc-dhcp-serverも候補に入れましたがdnsmasqの方が軽量で設定も簡単であることから今回はdnsmasqをインストールしました。ページ頭の事前準備でインストール済です。dnsmasq.confにより設定をします。

$ sudo pico /etc/dnsmasq.conf

デフォルトでdnsmasqのDHCP機能は無効にされているため、使用したいときは/etc/dnsmasq.confで有効にする必要があります。以下は中心となる設定です:

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to
# localhost and udp port 67 to world:
interface=wlan0

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with
# dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world
# requests to them, but the paranoid might like to close them and let the 
# kernel handle them:
bind-interfaces

# Dynamic range of IPs to make available to LAN pc
dhcp-range=192.168.14.164,192.168.14.200,12h

# If you’d like to have dnsmasq assign static IPs, bind the LAN computer's
# NIC MAC address:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.14.164

dhcp-hostのwlan0のハードウェアアドレスは$sudo ifconfigにより確認して下さい。dhcp-rangeの設定など/etc/network/interfacesとの整合性を考慮する必要があります。

DNS キャッシュの設定

単一のコンピュータ上で dnsmasq を DNS キャッシュデーモンとしてセットアップするには /etc/dnsmasq.conf を編集して listen-address ディレクティブをアンコメントし、ローカルホストの IP アドレスを追記してください:

listen-address=127.0.0.1

DNSアドレスファイル

dnsmasq を設定した後は、DHCP クライアントが /etc/resolv.conf にある既知の DNS アドレスの前にローカルホストのアドレスを挿入させるようにする必要があります。これによって外部の DNS でクエリを解決しようとする前に全てのクエリが dnsmasq に送られるようになります。

■resolv.confにより設定

まず一つ目の選択肢は resolv.conf の設定です。これを使うには、/etc/resolv.conf の一番上のネームサーバをローカルホストに変えて下さい:

$sudo pico /etc/resolv.conf

nameserver 127.0.0.1
# External nameservers
nameserver 8.8.8.8
nameserver 8.8.4.4

これで DNS クエリは最初に dnsmasq によって解決されるようになります。dnsmasq がクエリを解決できなかった場合にのみ外部のサーバーが使用されます。

■DHCPクライアントソフトにより設定

dhcpcdの場合

dhcpcd には /etc/resolv.conf にネームサーバを前と後ろに追加する機能があります。それぞれ /etc/resolv.conf.head と /etc/resolv.conf.tail ファイルを作成します:

echo “nameserver 127.0.0.1” > /etc/resolv.conf.head

dhclientの場合

dhclient の場合、/etc/dhcp/dhclient.conf の以下の部分をアンコメントしてください:

prepend domain-name-servers 127.0.0.1;

6. NATとiptablesの設定

NATによりアクセスポイントに接続した複数のクライアントを1つのIPアドレスへと導きます。

$ sudo pico /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

net.ipv4.ip_forword=1の箇所をアンコメントします。

設定をすぐに反映させるため、

$ sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”

を実行します。

iptableによりwlan0とwlan1の橋渡しの設定をします。

$ sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
$ sudo iptables -A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ sudo iptables -A FORWARD -i wlan0 -o wlan1 -j ACCEPT

以下のコマンドで上記設定の確認ができます。

$ sudo iptables -t nat -S
$ sudo iptables -S

システム起動時に上記設定を自動的に読み込ませるためiptables-persistentによる以下コマンドを実行します。

$ sudo sh -c “iptables-save > /etc/iptables/rules.v4”

再起動してアクセスポイントの表示と接続が確立されるか確認して下さい。

3つ以上のネームサーバ

上記設定でも市販ルータ経由であればインターネットに接続できますが、念の為Googleによるネームサーバを指定します。

Linux による DNS クエリの処理には resolv.conf で使えるネームサーバは3つまでという制約があります。対応策として、resolv.conf にはローカルホストのネームサーバだけを記述して、外部のネームサーバ用に resolv-file を分けて作成する方法があります。まず、dnsmasq のための resolv ファイルを新しく作成してください:

$sudo nano /etc/resolv.dnsmasq.conf

# Google's nameservers, for example
nameserver 8.8.8.8
nameserver 8.8.4.4

それから /etc/dnsmasq.conf を新しい resolv ファイルを使用するように編集してください:

$sudo nano /etc/dnsmasq.conf

...
resolv-file=/etc/resolv.dnsmasq.conf
...

$sudo service dnsmasq status

上記コマンドによりdnsmasqのステータスを確認した結果、下記のエラーメッセージが表示される場合の対処方法

"ignoring nameserver 127.0.0.1 - local interface"

dnsmasq.confにresolv.confを読まないように設定します。

$sudo nano /etc/dnsmasq.conf

以下no-resolvの箇所をアンコメントします。

# If you don't want dnsmasq to read /etc/resolv.conf or any other
# file, getting its servers from this file instead (see below), then
# uncomment this.
no-resolv

前の返信記事でresolv-fileに127.0.0.1を含まないnameserverを指定することでも回避できます。

カスタムドメインの追加

(ローカル) ネットワークの hosts にカスタムドメインを追加することができます。

$ sudo nano /etc/dnsmasq.conf

以下local,domain,expand-hostsの箇所をアンコメント・記述します。

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.

local=/locallan/

# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"

domain=locallan

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.

expand-hosts

同一LANからはhostname.locallanでアクセスできるようになります。同一LAN内の任意の端末から以下コマンドでIPアドレスとカスタムドメインの関係を確認して下さい。

$ nslookup hostname

iwconfigによるLANインターフェイスの確認で、wlan0またはwlan1という表記ではなくwlx*******という表記になっている場合の対処方法

https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/

$ sudo ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules

$ sudo reboot

カスタムドメイン追加について補足

OpenWRTにもdnsmasqによるカスタムドメイン設定メニューが用意されています。

hostname.lanと指定することで、同一ローカルネットワーク内の他端末からアクセスすることができるようになります。

Ubuntuにはデフォルトでavahi-daemonが実装されているため、ホストネームに.localを付けることでLAN内の任意の端末にアクセスできます。AvahiとはmDNS/DNS-SDプロトコルによりLAN内の端末の発見・発行を容易にするシステムであり、OSXのBonjour(設定不必要のためZeroconfとも呼ばれています)と同一のシステムです。

Avahiサイト
http://avahi.org/

Avahi補足 /etc/nsswitch.confの設定
https://wiki.archlinux.jp/index.php/Avahi

Avahiの設定ファイル /etc/avahi/avahi-daemon.conf でホストネームやドメインネームを変更できます。

[server]
host-name=gateway
domain-name=localnet

この場合、gateway.localnet と指定することで接続しますが、ドメインネームをデフォルトのlocalから変更 (デフォルトはlocal) する場合には以下の手続きが必要になります。

/etc/nsswitch.conf 内で

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4

mdns4_minimal [NOTFOUND=return]dns または mdns に置換えます。

なお、/etc/avahi/avahi-daemon.confhost-nameのみを置換えた場合には、

/etc/nsswith.confのhostsの項目

mdns4_minimal [NOTFOUND=return] を追加して下さい。

名前解決を上記Avahiによる設定に頼っていたため気が付きませんでしたが、dnsmasqの設定による名前解決ができていなかったため以下の対応をしました。

(1)network/interfacesと/etc/hostsにdnsmasqのIPアドレスを指定

まず、/etc/network/interfacesのwlan0の項目にdnsmasqによるネームサーバを指定します。

dns-nameservers 192.168.14.164

次に、/etc/hosts/に上記IPアドレス192.168.14.164とhostname.locallanを追加します。

192.168.14.164 hostname.locallan

(2)dnsmasq.confによるネームサーバのIPアドレス指定

簡単な方法は、dnsmasq.conf内で

address=/new_hostname.locallan/192.168.14.164

とします。これで同じLANネットワーク内の他の端末からnew_hostname.locallanを指定することでsshなどでアクセスできるようになります。

new_hostname.locallanは任意で好みのアドレスを指定して下さい。この設定は*.new_hostname.locallanというワイルドカードと同意です。

Dnsmasq設定ファイルのまとめ

USB-WiFiドングルのアクセスポイント機能を有効にして、インターフェイスにwlan0を指定、ここに固定アドレスを割り当てDNSサーバ、DHCPサーバの起点としました。外部DNSを参照する前にローカルホストを参照するようにresolv.confの設定をしましたが、dnsmasq.conf内で外部DNSサーバを指定することで(resolv.confによる名前解決を無効化:no-resolvをアンコメント)resolv.confの設定は不要となります。 nslookupコマンドで名前解決するかどうか確認して下さい。

①Some Clients <-----> ②| AP(wlan0)----Client(wlan1) | <-----> ③|Router| <---> ④WAN(Internet)

AvahiはDebian派生のArmbianでは無効化されていますが、Dnsmasqを使用するのであれば有効にする必要はありません。(②のAPに接続された①からアクセスする場合のみ)

Avahiは③経由で②にSSH接続する場合などに有効です。

(1)//—>optional の箇所を設定した場合

xxxxxの箇所はワイルドカードでどんな文字列、空白でも可。

ex)

$ ssh [email protected]

$ ssh [email protected]

$ ssh youser_name@myhome

$ ssh [email protected]

(2)//—>optional の箇所を設定しない場合

この場合、アクセスポイントによる同一LAN内の他の端末からは、ユーザ名@xxxxx.myhome.gw でアクセスします(xxxxxの箇所はワイルドカードでどんな文字列、空白でも可)。

ex)

$ ssh [email protected]

$ ssh [email protected]

/etc/dnsmasq.conf:DNSに関する設定箇所(DHCPに関する箇所は上記記事参照)

domain-needed
bogus-priv
no-resolv
filterwin2k //--->optional
expand-hosts //--->optional
domain=localnet //--->optional
local=/localnet/ //--->optional
interface=wlan0
listen-address=127.0.0.1
listen-address=192.168.14.164
bind-interfaces
server=8.8.8.8
server=8.8.4.4
address=/myhome.gw/192.168.14.164

/etc/network/interfaces

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.14.164
netmask 255.255.255.0
network 192.168.14.0
dns-nameservers 192.168.14.164 //--->optional

/etc/hosts

127.0.0.1   localhost
127.0.1.1   orangepipc
192.168.14.164 myhome //--->optional
::1         ip6-localhost ip6-loopback
fe00::0     ip6-localnet
ff00::0     ip6-mcastprefix
ff02::1     ip6-allnodes
ff02::2     ip6-allrouters

/etc/resolv.conf

dnsmasq.conf内でno-resolvを有効にしたため設定不要

resolvconfのステータス確認

$ sudo service resolvconf status

Warning: /etc/resolv.conf is not a symbolic link to /etc/resolvconf/run/resolv.conf

/etc/resolf.confを削除してシンボリックリンク作成

$ sudo rm /etc/resolv.conf
$ sudo ln -s /etc/resolvconf/run/resolv.conf /etc/resolv.conf

ネームサーバの追加は以下のファイルで指定

$ sudo pico /etc/resolvconf/resolv.conf.d/tail

nameserver xx.xx.xx.xx
nameserver 1.1.1.1
nameserver 8.8.8.8

設定のリロード

$ sudo resolvconf -u