web-dev-qa-db-ja.com

ブリッジドインターフェイス間のポート転送

そのため、メインのイーサネットデバイスにバインドされた一連のブリッジインターフェイスがあります(em1、HPのせいです)。これらは、サーバーで実行しているさまざまなLXCコンテナーを提供し、ネットワーク上の他の物理デバイスから簡単にアクセスできるようにします。

name    id                  STP   interfaces    IP
br0     8000.989096db8b8a   no    em1           10.10.0.2
                                  veth236T4V    10.10.0.15
                                  veth269GNR    10.10.0.16
                                  vethBYBC0Y    10.10.0.17

これらはすべて、メインネットワークDHCP(静的リースを割り当てる)からIPを取得します。

メインホスト(em110.10.0.2、ポート9000、9001)で実行されているサービスを最初のLXCコンテナーに移動したい。私はこれを行ったので、10.10.0.15:9000-9001を介してアクセスできるようになりましたが、ネットワーク上の他のすべてのものは10.10.0.2:9000-9001でそれを見ることができます。

iptablesを介した従来のポート転送は機能しないようです。私はもう試した:

-A PREROUTING -i em1 -p tcp --dport 9000 -j DNAT --to 10.10.0.15:9000
-A PREROUTING -i em1 -p tcp --dport 9001 -j DNAT --to 10.10.0.15:9001

そして、br0の代わりにem1を試しましたが、どちらも動作しません。

午前3時の調査で、私はebtablesが必要であることを示唆するものをたくさん見つけましたが、それを聞いたことがありませんでした。問題の半分は、ほとんどの人がLXCでlxcbrNデバイスを使用しているようですが、外部IPが必要でした。何が必要かわかりません。 ebtables documentation Wordの「ポート」を他の何かとして定義しているように見えるため、これは役に立ちません。

深みがありません。私はもう床を感じることができず、水を踏み始めています。誰も私に回線を投げて、Iブリッジされたインターフェース間でいくつかのポートをリダイレクトする必要があることを明確に言うことができますか?

5
Oli

Iptablesを使用できます。以下は、提案されたソリューションのスクリプトバージョンです。あなたが既に持っているかもしれないiptablesルールがわからないので、マージ作業が必要になるかもしれません。

#!/bin/sh
FWVER=0.02
#
# test-oli rule set 2016.01.14 Ver:0.02
#     Having tested this on my test server using port 80,
#     convert for what Oli actually wants (which I can not test).
#
# test-oli rule set 2016.01.14 Ver:0.01
#     Port forward when this computer has one nic and
#     is not a router / gateway.
#     In this case the destination is a guest VM on this
#     Host but, with bridged networking and all IP addresses
#     from the main LAN, that should not be relevant.
#
#     This script may conflict with other iptables rules on the
#     Host, I don't know. On my test server, clobbering the existing
#     iptables rules is O.K. because I do not use the virbr0 stuff,
#     nor the default virtual network,  anyhow.
#
#     References:
#     http://askubuntu.com/questions/720207/port-forwarding-between-bridged-interfaces
#     http://ubuntuforums.org/showthread.php?t=1855192
#     http://www.linuxquestions.org/questions/linux-networking-3/iptables-forwarding-with-one-nic-80009/
#
#     run as Sudo
#
echo "test-oli rule set version $FWVER..\n"

# The location of the iptables program
#
IPTABLES=/sbin/iptables

# Setting the EXTERNAL and INTERNAL interfaces and addresses for the network
# Use br0 instead of eth0. While using eth0 seems to work fine, the packet counters
# don't work, so debugging information is better and more complete using br0.
#
#
INTIF="br0"
INTIP="10.10.0.2"
FORIP="10.10.0.15"
UNIVERSE="0.0.0.0/0"

echo " Internal Interface: $INTIF  Internal IP: $INTIP  Forward IP $FORIP"

# CRITICAL:  Enable IP forwarding since it is disabled by default
#
echo Enabling forwarding...
echo "1" > /proc/sys/net/ipv4/ip_forward

# Clearing any previous configuration
#
echo " Clearing any existing rules and setting default policy to ACCEPT.."
$IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -F FORWARD
$IPTABLES -t nat -F
# Delete user defined chains
$IPTABLES -X
# Reset all IPTABLES counters
$IPTABLES -Z
# While my references do not have it, I think this is needed.
$IPTABLES -t nat -Z

# First we change the destination of any incoming port 80 traffic
#
$IPTABLES -t nat -A PREROUTING -p tcp -i $INTIF --dport 9000 -j DNAT --to-destination $FORIP:9000
$IPTABLES -t nat -A PREROUTING -p tcp -i $INTIF --dport 9001 -j DNAT --to-destination $FORIP:9001

# And then we do the actual forward
# FORWARD rules would only be needed if the default policy is not ACCEPT
# (Shown here for completeness)
#
$IPTABLES -A FORWARD -p tcp -i $INTIF -d $FORIP --dport 9000 -j ACCEPT
$IPTABLES -A FORWARD -p tcp -i $INTIF -d $FORIP --dport 9001 -j ACCEPT

# Now, we need to change the source address, otherwise the reply packets
# would be sent directly to the client, causing confusion.
$IPTABLES -t nat -A POSTROUTING -o $INTIF -j SNAT --to-source $INTIP

echo "test-oli rule set version $FWVER done."
5
Doug Smythies