[linux]특정 packet만 지정하여 다른 경로(route)로 보내기
네트워크 관련 작업을 하다보면, 같은 destination IP를 갖더라도 필터(destination port로 구분한다던가..)를 통해 각각 다른 경로로 메세지를 보내야 하는 상황이 발생한다.
linux에서는 iptables와 ip 명령어를 잘 조합하여 구현할 수 있다.
본 게시물에서는 특정 destination port는 다른 경로로 보내도록 설정하는 것이 목표다.
iptables에서 DSCP marking을 할 수 있는데, iptables의 route decision 과정 이전에 marking을 해야 원하는 route를 선택하도록 설정할 수 있다.
- 1. dscp 설정
아래 명령어는 source, destination addr 상관 없이 destination port가 40000~40999 범위에 속한다면 특정 dscp value로 marking한다. (dscp marking value를 10으로 한 것은 아무 의미 없음)
$ iptables -I PREROUTING -t mangle -p TCP --dport 40000:40999 -j DSCP --set-dscp 10
$ iptables -t mangle -nvL
Chain PREROUTING (policy ACCEPT 98 packets, 7008 bytes)
pkts bytes target prot opt in out source destination
0 0 DSCP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpts:40000:40999 DSCP set 0x0a
- 2. ip rule 설정
위에서 설정한 dscp marking은 아래와 같이 활용된다.
(dscp 10으로 marking된 packet은 route table 111을 보는것을 의미)
ip rule add fwmark 10 lookup 111
- 3. ip route 설정
그리고 아래와 같이 111 route table의 경로를 원하는 대로 설정한다.
(route table number 111은 아무 의미 없음)
$ ip r add default via 10.10.10.2 table 111
$ ip r show table 111
default via 10.10.10.2 dev eth0
위와 같은 과정을 통해 해당 서버로 들어오는 packet의 destination port가 40000~40999일 때, 10.10.10.2로 reroute하도록 동작할것이다.