여러 인터넷 제공업체를 위한 라우터로서의 Linux

여러 인터넷 제공업체를 위한 라우터로서의 Linux

라우터로서의 Linux: 저는 각각 자체 모뎀을 갖춘 3개의 인터넷 제공업체를 보유하고 있습니다.

공급자 1즉, 게이트웨이 주소 192.168.1.1이
Linux 라우터에 연결됩니다.eth1/192.168.1.2

공급자 2, 게이트웨이 주소 192.168.2.1은
Linux 라우터에 연결됩니다.이더리움 2/192.168.2.2

공급자 3, 게이트웨이 주소 192.168.3.1은
Linux 라우터에 연결됩니다.이더리움 3/192.168.3.2

                                                                           ________
                                                   +------------+         /
                                                   |            |        |
                            +----------------------+ Provider 1 +--------|
        __                  |192.168.1.2           |192.168.1.1 |       /
    ___/  \_         +------+-------+              +------------+      |
  _/        \__      |    eth1      |              +------------+      /
 /             \ eth0|              |192.168.2.2   |            |      |
|Client network -----+  ROUTER  eth2|--------------+ Provider 2 +------|     Internet
 \10.0.0.0/24 __/    |              |              |192.168.2.1 |      |
   \__     __/       |    eth3      |              +------------+      \
      \___/          +------+-------+              +------------+       |
                            |192.168.3.2           |            |       \
                            +----------------------+ Provider 3 +-------|
                                                   |192.168.3.1 |       |
                                                   +------------+       \________

소스 IP를 기준으로 네트워크 10.0.0.0/24의 클라이언트를 다른 게이트웨이로 라우팅하고 싶습니다.
클라이언트 네트워크 인터페이스는이더넷 0/10.0.0.1은 모든 클라이언트의 기본 게이트웨이입니다.

예:
10.0.0.11은 Provider1@eth1로 라우팅되어야 합니다.
10.0.0.12는 Provider2@eth2로 라우팅되어야 합니다
... 등등...

ip routeSNAT에 및를 사용해야 할 것 같은데 iptables정확히 어떻게 사용하는지 파악하지 못했습니다.
이것은 지금까지 내 스크립트입니다.
ipv4 전달이 활성화되었습니다.

#!/bin/bash
# flush tables
ip route flush table connection1
ip route flush table connection2
ip route flush table connection3

# add the default gateways for each table
ip route add table connection1 default via 192.168.1.1
ip route add table connection2 default via 192.168.2.1
ip route add table connection3 default via 192.168.3.1

# add some IP addresses for marking
iptables -t mangle -A PREROUTING -s 10.0.0.11 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -s 10.0.0.12 -j MARK --set-mark 2
iptables -t mangle -A PREROUTING -s 10.0.0.13 -j MARK --set-mark 3

# add the source nat rules for each outgoing interface
iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to-source 192.168.1.2
iptables -t nat -A POSTROUTING -o eth2 -j SNAT --to-source 192.168.2.2
iptables -t nat -A POSTROUTING -o eth3 -j SNAT --to-source 192.168.3.2

# link routing tables to connections (?)
ip rule add fwmark 1 table connection1
ip rule add fwmark 2 table connection2
ip rule add fwmark 3 table connection3

#default route for anything not configured above should be eth2

답변1

다음은 라우터 중 하나에 대한 유사한 설정입니다(관련 없는 일부 항목은 제거됨). 이렇게 처리되니 참고하세요들어오는연결에도 마찬가지입니다.

하드 코딩된 태그 번호 대신 변수를 사용하십시오. 유지관리가 더 쉬워졌습니다! 이는 별도의 스크립트에 저장되며 . 테이블 이름은 에서 구성됩니다 /etc/iproute2/rt_tables. 인터페이스 이름은 에서 설정됩니다 /etc/udev/rules.d/70-persistent-net.rules.

##### fwmark ######
iptables -t mangle -F
iptables -t mangle -X

iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
iptables -t mangle -A PREROUTING -m mark ! --mark 0 -j RETURN # if already set, we're done
iptables -t mangle -A PREROUTING -i wan      -j MARK --set-mark $MARK_CAVTEL
iptables -t mangle -A PREROUTING -i comcast  -j MARK --set-mark $MARK_COMCAST
iptables -t mangle -A PREROUTING -i vz-dsl   -j MARK --set-mark $MARK_VZDSL

iptables -t mangle -A POSTROUTING -o wan     -j MARK --set-mark $MARK_CAVTEL
iptables -t mangle -A POSTROUTING -o comcast -j MARK --set-mark $MARK_COMCAST
iptables -t mangle -A POSTROUTING -o vz-dsl  -j MARK --set-mark $MARK_VZDSL
iptables -t mangle -A POSTROUTING -j CONNMARK --save-mark

##### NAT ######
iptables -t nat -F
iptables -t nat -X
for local in «list of internal IP/netmask combos»; do
    iptables -t nat -A POSTROUTING -s $local -o wan     -j SNAT --to-source «IP»
    iptables -t nat -A POSTROUTING -s $local -o comcast -j SNAT --to-source «IP»
    iptables -t nat -A POSTROUTING -s $local -o vz-dsl  -j SNAT --to-source «IP»
done

# this is an example of what the incoming traffic rules look like
for extip in «list of external IPs»; do
    iptables -t nat -A PREROUTING   -p tcp -d $extip --dport «port» -j DNAT --to-destination «internal-IP»:443
done

규칙은 다음과 같습니다.

ip rule flush
ip rule add from all               pref 1000  lookup main 
ip rule add from A.B.C.D/29        pref 1500  lookup comcast # these IPs are the external ranges (we have multiple IPs on each connection)
ip rule add from E.F.G.H/29        pref 1501  lookup cavtel
ip rule add from I.J.K.L/31        pref 1502  lookup vzdsl
ip rule add from M.N.O.P/31        pref 1502  lookup vzdsl # yes, you can have multiple ranges
ip rule add fwmark $MARK_COMCAST   pref 2000  lookup comcast
ip rule add fwmark $MARK_CAVTEL    pref 2001  lookup cavtel
ip rule add fwmark $MARK_VZDSL     pref 2002  lookup vzdsl
ip rule add                        pref 2500  lookup comcast # the pref order here determines the default—we default to Comcast.
ip rule add                        pref 2501  lookup cavtel
ip rule add                        pref 2502  lookup vzdsl
ip rule add                        pref 32767 lookup default

라우팅 테이블이 설정되어 있으므로 /etc/network/interfaces인터페이스를 종료하면 다른 인터페이스를 사용하도록 전환됩니다.

iface comcast inet static
        address A.B.C.Q
        netmask 255.255.255.248
        up ip route add table comcast default via A.B.C.R dev comcast
        down ip route flush table comcast

노트:FORWARD필터링도 수행하는 경우(아마도 그럴 것임) 트래픽 에 ACCEPT적절한 규칙을 추가 해야 합니다 . 특히 들어오는 트래픽의 경우.

관련 정보