OpenVPN サーバーの設定と起動

2013年7月10日

ここで取り扱うOpenVPNの構成はL2とL3の両方を自動起動するという物です。
構成は下の図の通りで、遠方の家族の端末や、出先からのアクセスを10.12.59.0/24のセグメントに収容し、タブレットや友人の端末を10.8.0.0/24のセグメントに収容します。

openvpn-network

ネットワーク設定

SELinuxを有効にした状態でこれを実現するために、ネットワークのブリッジをsystemdに委ねます。

これに伴い、/etc/sysconfig/nwtwork-scripts内のファイルの追加編集をします。

まず、tapデバイスに関する起動と停止のスクリプトの追加です。
openvpnコマンドを使用することから、サフィックスをovpnとします。

#!/bin/bash

. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

CONFIG=${1}

need_config ${CONFIG}

source_config

/usr/sbin/openvpn --mktun --dev ${DEVICE}

if [ -n "${BRIDGE}" -a -x /usr/sbin/brctl ]; then
     if ! LC_ALL=C /usr/sbin/brctl show | LC_ALL=C grep -q "^${BRIDGE} "; then
         /usr/sbin/brctl addbr ${BRIDGE} 2>/dev/null
     fi
     /sbin/ip addr flush dev ${DEVICE} 2>/dev/null
     /sbin/ip link set dev ${DEVICE} up
     if [ -n "$ETHTOOL_OPTS" ] ; then
       /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
     fi
     [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY}
     /usr/sbin/brctl addif ${BRIDGE} ${DEVICE}
     # Upon adding a device to a bridge,
     # it's necessary to make radvd reload its config
     [ -r /var/run/radvd/radvd.pid ] && kill -HUP `cat /var/run/radvd/radvd.pid`
     exit 0
fi
#!/bin/bash

. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

CONFIG=${1}

source_config

/usr/sbin/openvpn --rmtun --dev ${DEVICE}
retcode=$?

exit $retcode

 ブリッジネットワークを構成して予めtap0を作成するように、ifcfg-eth0、ifcfg-br0、ifcfg-tap0の起動スクリプトを用意します。

# promiscuous 
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
HWADDR=00:24:8c:77:b6:8a
DEVICE=eth0
TYPE=Ethernet

NAME="System eth0"
ONBOOT=yes
USERCTL=no
NM_CONTROLLED=no

BRIDGE=br0
DEVICE=br0
TYPE=Bridge

NAME="System br0"
ONBOOT=yes
USERCTL=no
NM_CONTROLLED=no

BOOTPROTO=none

IPADDR=10.12.59.2
NETWORK=10.12.59.0
NETMASK=255.255.255.0
BROADCAST=10.12.59.255
GATEWAY=10.12.59.1
DEFROUTE=yes

DNS1=10.12.59.2 # 内向きDNSを設置しているためこれを設定しています。
DEVICE=tap0
TYPE=ovpn

NAME="System tap0"
ONBOOT=yes
USERCTL=yes
NM_CONTROLLED=no

BRIDGE=br0

 上記の設定でのifconfigの結果は以下の通り、これで起動時にブリッジネットワークが確立されます。

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.12.59.2  netmask 255.255.255.0  broadcast 10.12.59.255
        inet6 fe80::224:8cff:fe77:b68a  prefixlen 64  scopeid 0x20<link>
        inet6 2408:104:42b3:0:224:8cff:fe77:b68a  prefixlen 64  scopeid 0x0<global>
        ether **:**:**:**:**:**  txqueuelen 0  (Ethernet)
        RX packets 54387385  bytes 190421163047 (177.3 GiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 105295414  bytes 100431169570 (93.5 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4675<UP,BROADCAST,RUNNING,ALLMULTI,MULTICAST>  mtu 1500
        inet6 fe80::224:8cff:fe77:b68a  prefixlen 64  scopeid 0x20<link>
        ether **:**:**:**:**:**  txqueuelen 1000  (Ethernet)
        RX packets 151281285  bytes 197601202188 (184.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 105365632  bytes 100455092269 (93.5 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 16325  bytes 2089947 (1.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16325  bytes 2089947 (1.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f8cb:51ff:fec5:9c04  prefixlen 64  scopeid 0x20<link>
        ether **:**:**:**:**:**  txqueuelen 100  (Ethernet)
        RX packets 75590  bytes 24640380 (23.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 102893  bytes 13914085 (13.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
        inet 10.8.0.1  netmask 255.255.255.255  destination 10.8.0.2
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 100  (UNSPEC)
        RX packets 1120664  bytes 56766931 (54.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2097425  bytes 2793700365 (2.6 GiB)
        TX errors 0  dropped 74 overruns 0  carrier 0  collisions 0

 OpenVPN設定

/etc/openvpn内にserver.confserver-bridge.confを作成します。SELinuxの権限の問題もあり、up/down時に実行するスクリプトは設定しません。openvpnのセキュリティコンテキストにiptables等の権限を与えるよりもsysytemdに委ねたほうが良いと考えます。

port 1194
proto udp
dev tun

user	openvpn
group	openvpn

management localhost 7505

server 10.8.0.0 255.255.255.0

client-to-client
replay-window 128

client-config-dir ccd
ifconfig-pool-persist ipp.txt

ca		keys/ca.crt
cert		keys/server.crt
key		keys/server.key  # This file should be kept secret
dh		keys/dh1024.pem

crl-verify	crl.pem
tls-auth	ta.key 0

keepalive 10 120
comp-lzo

persist-key
persist-tun

verb 3
status /var/log/openvpn-status.log
log-append  /var/log/openvpn.log
port 1197
proto udp
dev tap0 # 作成済みのデバイスを指定

user	openvpn
group	openvpn

management localhost 7506

server-bridge 10.12.59.2 255.255.255.0 10.12.59.100 10.12.59.128
route 10.12.59.0 255.255.255.0

push "route 10.12.59.0 255.255.255.0"

client-to-client
replay-window 128

ifconfig-pool-persist ipp-bridge.txt

ca		keys/ca.crt
cert		keys/server.crt
key		keys/server.key  # This file should be kept secret
dh		keys/dh1024.pem

crl-verify	crl.pem
tls-auth	ta.key 0

keepalive 10 120
comp-lzo

persist-key
persist-tun

verb 3
status /var/log/openvpn-bridge-status.log
log-append  /var/log/openvpn-bridge.log

記述されている証明書や鍵ファイルは前回作成した通り、client-config-dirで設定されているディレクトリが存在しないとエラーになるので予め作成しておくこと。

あとはsystemdからの自動起動を有効にします。各confのファイル名.serviceでサービスを設置できるので、さらにOpenVPNのネットワークを追加することも出来ます。

systemctl enable openvpn@server.service
systemctl enable openvpn@server-bridge.service

iptables設定

Fedora 19へのアップグレードでsystem-config-firewallがFirewallDによって無効化されました。
時期RedHat Linuxのベース候補であるFedora 18の時にFirewallDが導入されたものの、Fedora 18へのアップグレードでは強制はされませんでしたが、今回置き換えが行われたという事は移行を促していると思う事にします。関連の読み物としては、こことかここを参照。

設定はfirewall-configというGUIが用意されていて、簡単な設定であればコマンドラインを使う必要は無し。openvpnのサービスが最初から定義されてあったのでbridge用のポートを付け足すだけで設定終了。

iptablesの設定はOpenvpn – FedoraProject:Setting up an OpenVPN serverに書かれている通りの方法がありますが、運用中のサーバーではsystem-config-firewallを使用しているため、フィルター用のファイルを作成して設定します。

-I INPUT -i tap0 -j ACCEPT
-I INPUT -i br0 -j ACCEPT
-I FORWARD -i br0 -j ACCEPT

-I OUTPUT -o tun+ -j ACCEPT
-I FORWARD -o tun+ -j ACCEPT
-I INPUT -i tun+ -j ACCEPT
-I FORWARD -i tun+ -d 10.12.59.0/24 -j ACCEPT

 system-config-firewall

カーネルチューニング

/etc/sysctl.confnet.ipv4.ip_forward = 1を追加、パケットフォワードを有効にする。sysctl -pのコマンドで有効化。

起動と保守

systemctl [start/stop] openvpn@[server/server-bridge].serviceで各サービスの起動と停止。
managementのオプションが有効なのでtelnetで状態の確認とクライアントのチェックが出来ます。

>telnet localhost 7505

help
Management Interface for OpenVPN 2.3.2 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [eurephia] [MH] [IPv6] built on Jun  3 2013
Commands:
auth-retry t           : Auth failure retry mode (none,interact,nointeract).
bytecount n            : Show bytes in/out, update every n secs (0=off).
echo [on|off] [N|all]  : Like log, but only show messages in echo buffer.
exit|quit              : Close management session.
forget-passwords       : Forget passwords entered so far.
help                   : Print this message.
hold [on|off|release]  : Set/show hold flag to on/off state, or
                         release current hold and start tunnel.
kill cn                : Kill the client instance(s) having common name cn.
kill IP:port           : Kill the client instance connecting from IP:port.
load-stats             : Show global server load stats.
log [on|off] [N|all]   : Turn on/off realtime log display
                         + show last N lines or 'all' for entire history.
mute [n]               : Set log mute level to n, or show level if n is absent.
needok type action     : Enter confirmation for NEED-OK request of 'type',
                         where action = 'ok' or 'cancel'.
needstr type action    : Enter confirmation for NEED-STR request of 'type',
                         where action is reply string.
net                    : (Windows only) Show network info and routing table.
password type p        : Enter password p for a queried OpenVPN password.
remote type [host port] : Override remote directive, type=ACCEPT|MOD|SKIP.
proxy type [host port flags] : Enter dynamic proxy server info.
pid                    : Show process ID of the current OpenVPN process.
pkcs11-id-count        : Get number of available PKCS#11 identities.
pkcs11-id-get index    : Get PKCS#11 identity at index.
client-auth CID KID    : Authenticate client-id/key-id CID/KID (MULTILINE)
client-auth-nt CID KID : Authenticate client-id/key-id CID/KID
client-deny CID KID R [CR] : Deny auth client-id/key-id CID/KID with log reason
                             text R and optional client reason text CR
client-kill CID [M]    : Kill client instance CID with message M (def=RESTART)
env-filter [level]     : Set env-var filter level
client-pf CID          : Define packet filter for client CID (MULTILINE)
rsa-sig                : Enter an RSA signature in response to >RSA_SIGN challenge
                         Enter signature base64 on subsequent lines followed by END
signal s               : Send signal s to daemon,
                         s = SIGHUP|SIGTERM|SIGUSR1|SIGUSR2.
state [on|off] [N|all] : Like log, but show state history.
status [n]             : Show current daemon status info using format #n.
test n                 : Produce n lines of output for testing/debugging.
username type u        : Enter username u for a queried OpenVPN username.
verb [n]               : Set log verbosity level to n, or show if n is absent.
version                : Show current version number.
END
status
OpenVPN CLIENT LIST
Updated,Sat Jun 29 19:29:42 2013
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
rednexsus7,10.12.59.32:59333,3741,4807,Sat Jun 29 19:29:09 2013
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.8.0.14,rednexsus7,10.12.59.32:59333,Sat Jun 29 19:29:10 2013
GLOBAL STATS
Max bcast/mcast queue length,2
END

 

 

OpenVPNbridge,Fedora,openvpn,vpn

Posted by redchat