自宅のLinuxルータをIPv6対応しました(予告編)


おはようございます。ライトノベル好きのツチノコです。異世界居酒屋「のぶ」 4杯目はもうみなさん読まれていることと思います。

IPv6が最近盛り上がっているようです(今世紀最高の出来)。ツチノコの巣でもIPv6勉強会が開かれましたスライドによると現状のユーザー側のIPv6接続環境はほぼデュアルスタックとのことです。

ツチノコのひとりとして世間一般に遅れてはいけないと思いましたので、自宅のLinuxルータをIPv6対応にしました。正確には新規にIPv4・IPv6両対応のLinuxルータを新規に作成して切り替えました。
せっかくなので手順をシェアします。といいつつ、今回の記事はIPv4でPPPoE接続するところまでです。
次回はIPv6対応部分のデザインとインストールをします。

なお、ご利用は自己責任でお願いします。また、(特にIPv4部分の)解説はある程度適当です。ツチノコブログの読者なら許してくれるはずです。
以下、設定ファイルを直接貼り付けているので大分長いです。

前提条件とハードウェア

前提条件は以下です。利用環境であるwataken家がこの条件というだけです。
・IPv4はPPPoE方式のプロバイダを利用していること
・IPv6はネイティブ方式のプロバイダを利用しており、IPv6オプションを契約していること
・ひかり電話を利用していないこと

ハードウェアはNICが2portあるPC・サーバが必要です。WAN側・LAN側で1portずつ使います。

作業の流れ

作業の流れは以下のようになっています。今日は(2)までです。
(1) OSのインストールとIPv4用のパッケージのインストールを行う(既存のインターネットに接続可能な環境を利用)
(2) IPv4用の設定を行う(この段階でこのルータでIPv4でインターネット接続可能になる)
(3) IPv6用のパッケージのインストールと設定を行う

OSのインストール

今回はDebian(stretch)を使います。Debian(jessie)でもよいかと思います。
後で必要なパッケージはapt-getでインストールするので、OSインストール時にはパッケージは何も選択しなくてもよいです。

他のディストリビューションを利用する場合は、Kernel version が 3.9.0 以上であることを確認してください。

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

# apt-get update
# apt-get upgrade
# apt-get dist-upgrade
# apt-get install pppoe iptables-persistent isc-dhcp-server unbound ntp ssh

一般に家庭用のルータでは、PPPoE client、FirewallとIP masquerade、DHCP server、DNS resolverが必要になります。それぞれインストールします。
あと、ntpとsshはとりあえずインストールしておきます。お好みでtcpdumpなんかもいれていいと思います。

IPv4用の設定

ここからIPv4用の設定をしていきます。
作業の最後にrebootして設定を反映するので、設定ファイルを編集後にそれぞれserviceのrestartなどは不要です。

kernel parameterの設定(/etc/sysctl.conf)

# /etc/sysctl.conf
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

ルータなのでforwardingを有効にします。

インターフェースの設定(/etc/network/interfaces)

#/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# wan interface
auto eth0
allow-hotplug eth0
iface eth0 inet static
    address 0.0.0.0
    up ifup ppp0
    down ifdown ppp0

iface ppp0 inet ppp
    provider dsl-provider-test

# lan interface
auto eth1
allow-hotplug eth1
iface eth1 inet static
    address 192.168.1.254
    netmask 255.255.255.0

eth0をWAN側、eth1をLAN側として利用します。
eth1(LAN側のインターフェース)は通常通りIP addressとnetmaskを設定します。
eth0(WAN側のインターフェース)はIP address無し(address 0.0.0.0)でupする、up時にppp0をupする、ppp0は/etc/ppp/peers/dsl-provider-testに基づいてpppoe接続する設定です。

pppoeの設定(/etc/ppp/peers/dsl-provider-test, /etc/ppp/chap-secrets)

# /etc/ppp/peers/dsl-provider-test
# Configuration file for PPP, using PPP over Ethernet
# to connect to a DSL provider.
#
# See the manual page pppd(8) for information on all the options.

##
# Section 1
#
# Stuff to configure...

# MUST CHANGE: Uncomment the following line, replacing the user@provider.net
# by the DSL user name given to your by your DSL provider.
# (There should be a matching entry in /etc/ppp/pap-secrets with the password.)
user testuser

# Use the pppoe program to send the ppp packets over the Ethernet link
# This line should work fine if this computer is the only one accessing
# the Internet through this DSL connection. This is the right line to use
# for most people.
pty "/usr/sbin/pppoe -I eth0 -T 80 -m 1452"

# The following two options should work fine for most DSL users.

# Assumes that your IP address is allocated dynamically
# by your DSL provider...
noipdefault
# Try to get the name server addresses from the ISP.
usepeerdns
# Use this connection as the default route.
# Comment out if you already have the correct default route installed.
defaultroute

##
# Section 3
#
# You shouldn't need to change these options...

hide-password
lcp-echo-interval 20
lcp-echo-failure 3
# Override any connect script that may have been set in /etc/ppp/options.
connect /bin/true
noauth
persist
mtu 1492

# RFC 2516, paragraph 7 mandates that the following options MUST NOT be
# requested and MUST be rejected if requested by the peer:
# Address-and-Control-Field-Compression (ACFC)
noaccomp
# Asynchronous-Control-Character-Map (ACCM)
default-asyncmap
#/etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
testuser  *  testsecret

/etc/ppp/peers/dsl-provider-test(/etc/network/interfacesでファイル名は指定)はpppoeの設定を行うファイルで、/etc/ppp/chap-secretsはユーザ名・パスワードの対応を格納するためのファイルです。
それぞれファイルの testuser と testsecret はプロバイダのユーザ名・パスワードを入れます。
面倒ならpppoeconfを利用してもいいと思います。

DHCP serverの設定(/etc/dhcp/dhcpd.conf)

# /etc/dhcp/dhcpd.conf
# ddns-update-style should be none for DHCPv2
ddns-update-style none;

# subnet definition
shared-network LAN {
  subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.128 192.168.1.195;
    authoritative;
    default-lease-time 28800;
    max-lease-time 57600;
    option broadcast-address 192.168.1.255;
    option domain-name-servers 192.168.1.254;
    option routers 192.168.1.254;
  }
}

今回は192.168.1.128-195の範囲をDHCPとして利用することにします。また、DNSとgatewayとしてLAN側IP(192.168.1.254)を配布します。

DNS resolverの設定(/etc/unbound/unbound.conf)

# /etc/unbound/unbound.conf
# See the unbound.conf(5) man page.
include: "/etc/unbound/unbound.conf.d/*.conf"

server:
  interface: 192.168.1.254
  access-control: 192.168.1.0/24 allow
  unwanted-reply-threshold: 10000000

LAN側IP(192.168.1.254)からの問い合わせに応答します。

FirewallとIP masqueradeの設定(iptables)

#!/bin/sh
# iptables.sh

WAN_IF=ppp0
LAN_IF=eth1
LAN_SUBNET=192.168.1.0/24

iptables -t nat -F
iptables -t filter -F
iptables -F

iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT ACCEPT
iptables -t filter -P FORWARD DROP

iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A INPUT -i $LAN_IF -j ACCEPT
iptables -t filter -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -t filter -A FORWARD -s $LAN_SUBNET -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
iptables -t filter -A FORWARD -s $LAN_SUBNET -o $WAN_IF -j ACCEPT
iptables -t filter -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

iptables -t nat -A POSTROUTING -s $LAN_SUBNET -o $WAN_IF -j MASQUERADE

iptablesはコマンド経由で設定するのが一般的なので、シェルスクリプトを書くことにします。適当な場所に iptables.sh を作成します。

設定の反映

# sh iptables.sh; service netfilter-persistent save; reboot

問題なく設定されていたら、これでIPv4対応のpppoeルータとして動作するはずです。

$ ping www.google.com -c 4
PING www.google.com (173.194.126.244) 56(84) bytes of data.
64 bytes from nrt04s08-in-f20.1e100.net (173.194.126.244): icmp_seq=1 ttl=52 time=4.83 ms
64 bytes from nrt04s08-in-f20.1e100.net (173.194.126.244): icmp_seq=2 ttl=52 time=5.09 ms
64 bytes from nrt04s08-in-f20.1e100.net (173.194.126.244): icmp_seq=3 ttl=52 time=8.30 ms
64 bytes from nrt04s08-in-f20.1e100.net (173.194.126.244): icmp_seq=4 ttl=52 time=5.65 ms

--- www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 4.832/5.972/8.305/1.381 ms

次回に続きます。

p.s. 4巻が出た路地裏バトルプリンセスが面白いです。

«

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

Optionally add an image (JPEG only)