CentOS5でIPv6ルータ

近頃のLinuxは標準でIPv6に対応している。OSインストール時に特に指定や設定をしないでもifconfigを見ると自動的にリンクローカルなIPv6アドレスが設定されている。ここでは一歩すすんで、CentOS5をIPv6ルータとして設定してみよう。

まず特に設定をする前のインターフェース状態を見てみよう。inet6 addrが設定されており、ScopeがLinkであることがわかる。

[root@ml115 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1B:78:9E:D6:6B  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::21b:78ff:fe9e:d66b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:238099 errors:0 dropped:0 overruns:0 frame:0
          TX packets:264177 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:26886732 (25.6 MiB)  TX bytes:381478301 (363.8 MiB)

これだけで同一セグメント上のIPv6ホスト同士は通信ができる。またネットワーク上にIPv6ルータがいればグローバルアドレスとデフォルトゲートウェイも自動的に設定され、他のネットワークやインターネットとの通信もできる。

さて、IPv6ルータとして最低限必要な動作は、IPv6アドレスの設定と、IPv6パケットの転送、ルータアドバタイズメントというルータ情報等のネットワークへの送出である。ルータアドバタイズメントはIPv6から追加された機能であり、ホストアドレスの自動設定やルータの冗長化を実現する。

IPv6アドレスの設定は、IPv4と同様に/etc/sysconfig/network-script/ifcfg-eth0等に記述する。IPv6アドレスはローカルIPv6アドレス(FD00::/8)から適当にとろう。うちの場合このネットワークはIPv4で192.168.0.0/24なので、見やすいように以下のようにした。

[root@ml115 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:1b:78:9e:d6:6b
IPADDR=192.168.0.2
NETMASK=255.255.255.0
IPV6INIT=yes
IPV6ADDR=fd00:192:168::2/64
ONBOOT=yes
TYPE=Ethernet

また、/etc/sysconfig/networkにNETWORKING_IPV6=yesの設定を加える。

[root@ml115 ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=ml115.grandarbre.net

そうしたらネットワークを再起動して設定を反映させる。ただし以下のコマンドだとカーネルパラメータ等の設定が飛んでしまうので、できればOSを再起動したほうが確実。

[root@ml115 ~]#service network restart

インターフェースにちゃんとIPv6アドレスが付与されたか見てみよう。

[root@ml115 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1B:78:9E:D6:6B  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fd00:192:168::2/64 Scope:Global
          inet6 addr: fe80::21b:78ff:fe9e:d66b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1199 errors:0 dropped:0 overruns:0 frame:0
          TX packets:668 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:253144 (247.2 KiB)  TX bytes:95419 (93.1 KiB)

また、ルータとして動作するのだから、自分のアドレスをルータアドバタイズメントから自動設定してはいけない。これは以下のようにカーネルパラメータで設定する。

[root@ml115 ~]# echo 0 > /proc/sys/net/ipv6/conf/all/accept_ra

IPv6パケットの転送もカーネルパラメータで設定する。

[root@ml115 ~]# echo 1 > /proc/sys/net/ipv6/conf/all/forwarding

恒久的に変更するには/etc/rc.d/rc.localに上記の2つのコマンドを追記する。

次にルータアドバタイズメントを行うデーモンをインストールする。

[root@ml115 ~]# yum install radvd

設定ファイルは/etc/radvd.conf。ほとんど設定サンプルどおりでいいが、自分のネットワークで使うプレフィックス(ネットワークアドレス)だけは決めなければいけない。

interface eth0
{
       AdvSendAdvert on;
       MinRtrAdvInterval 30;
       MaxRtrAdvInterval 100;
       prefix fd00:192:168:0::/64
       {
               AdvOnLink on;
               AdvAutonomous on;
               AdvRouterAddr off;
       };

};

それではデーモンを起動してみよう。

[root@ml115 etc]# service radvd start
Starting radvd:                                            [  OK  ]
[root@ml115 ~]# chkconfig radvd on

ルータアドバタイズが送信されている様子は、 radvdumpコマンドで表示することができる。今回の設定では30秒から100秒おきに以下のようなアドバタイズが送信される。

[root@ml115 ~]# radvdump eth0
#
# radvd configuration generated by radvdump 0.9.1
# based on Router Advertisement from fe80::21b:78ff:fe9e:d66b
# received by interface eth0
#

interface eth0
{
        AdvSendAdvert on;
        # Note: {Min,Max}RtrAdvInterval cannot be obtained with radvdump
        AdvManagedFlag off;
        AdvOtherConfigFlag off;
        AdvReachableTime 0;
        AdvRetransTimer 0;
        AdvCurHopLimit 64;
        AdvHomeAgentFlag off;
        AdvDefaultPreference medium;
        AdvSourceLLAddress on;

        prefix fd00:192:168::/64
        {
                AdvValidLifetime 2592000;
                AdvPreferredLifetime 604800;
                AdvOnLink on;
                AdvAutonomous on;
                AdvRouterAddr off;
        }; # End of prefix definition

}; # End of interface definition

ここまで設定すれば、このLinuxサーバはIPv6ルータとして動作している。配下のWindows XPでは以下のようにIPv6アドレスとデフォルトゲートウェイが取得されている。

C:¥>ipconfig

Windows IP Configuration

Ethernet adapter wlan0:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.0.7
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        IP Address. . . . . . . . . . . . : fd00:192:168:0:2c9c:62b1:1de3:b294
        IP Address. . . . . . . . . . . . : fd00:192:168:0:219:7eff:fe4b:f6b
        IP Address. . . . . . . . . . . . : fe80::219:7eff:fe4b:f6b%8
        Default Gateway . . . . . . . . . : 192.168.0.2
                                            fe80::21b:78ff:fe9e:d66b%8

Tunnel adapter Teredo Tunneling Pseudo-Interface:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : fe80::ffff:ffff:fffd%4
        Default Gateway . . . . . . . . . :

Tunnel adapter Automatic Tunneling Pseudo-Interface:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : fe80::5efe:192.168.0.7%2
        Default Gateway . . . . . . . . . :

また配下のLinuxサーバでは以下のようにIPv6アドレスとデフォルトゲートウェイが取得されている。

[root@r60e ~]# ifconfig ath0
ath0      Link encap:Ethernet  HWaddr 00:19:7D:39:5E:87  
          inet addr:192.168.0.3  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fd00:192:168:0:219:7dff:fe39:5e87/64 Scope:Global
          inet6 addr: fe80::219:7dff:fe39:5e87/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:45319 errors:0 dropped:0 overruns:0 frame:0
          TX packets:43011 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:34823760 (33.2 MiB)  TX bytes:5576371 (5.3 MiB)
[root@r60e ~]# route --inet6
Kernel IPv6 routing table
Destination                                 Next Hop                                Flags Metric Ref    Use Iface
fd00:192:168::/64                           *                                       UA    256    67       0 ath0    
fe80::/64                                   *                                       U     256    0        0 ath0     
*/0                                         fe80::21b:78ff:fe9e:d66b                UGDA  1024   0        0 ath0    
localhost6.localdomain6/128                 *                                       U     0      1        1 lo      
fd00:192:168:0:219:7dff:fe39:5e87/128       *                                       U     0      0        1 lo      
fe80::219:7dff:fe39:5e87/128                *                                       U     0      2        1 lo      
fe80::f498:ddff:fec3:1f54/128               *                                       U     0      0        1 lo      
ff00::/8                                    *                                       U     256    0        0 ath0    

ちなみにfe80::21b:78ff:fe9e:d66bというのはルータのリンクローカルアドレスである。IPv6ルーティングテーブルでは通常グローバルアドレス(fd00:192:168::2)ではなくリンクローカルアドレスが使われる。