본문 바로가기

Linux(Centos or RHEL)/network

rp_filter란...?

1. Reverse Path Filtering?

1) rp_filter 설명

 

https://access.redhat.com/solutions/53031

 

위의 링크를 보면 다음과 같은 기능을 한다고 나와있습니다.

-----

rp_filter - INTEGER

0 - No source validation.

1 - Strict mode as defined in RFC3704 Strict Reverse Path Each incoming packet is tested against the FIB and if the interface is not the best reverse path the packet check will fail. By default failed packets are discarded.

2 - Loose mode as defined in RFC3704 Loose Reverse Path Each incoming packet's source address is also tested against the FIB and if the source address is not reachable via any interface the packet check will fail.

 

Current recommended practice in RFC3704 is to enable strict mode to prevent IP spoofing from DDos attacks. If using asymmetric routing or other complicated routing, then loose mode is recommended.

 

The max value from conf/{all,interface}/rp_filter is used when doing source validation on the {interface}.

Dfault value is 0. Note that some distributions enable it in startup scripts.

-----

 

 

즉, 1로 설정이 되어있으면, 특정 인터페이스로 들어온 패킷 정보와 OS의 FIB(Forward Information Base, Route cache라고도 부르며, "ip route"명령어 등으로 확인이 가능하다)에 정의된 정보와 일치하지 않을경우, 들어온 패킷을 버리는 기능을 합니다.

이 기능은 RHEL 7 부터 default값이 1로 변경되었습니다.

 

인터페이스에서 패킷을 버리지 않게 하기 위해서는 인터페이스의 rp_filter의 값을 0으로 바꿔 주거나, route cache를 추가해 주는 것으로 패킷을 버리지 않게끔 할 수 있습니다.

 

 

2) rp_filter 확인 법

rp_filter는 sysctl로 검색 할 시에는 "net.ipv4.conf.<interface DEVICE NAME>.rp_filter", /proc밑의 경로로는 "/proc/sys/net/ipv4/conf/<interface DEVICE name>/rp_filter"입니다.

 

vlan의 경우의 sysctl로 검색 결과는 "net.ipv4.conf.<interface DEVICE NAME>/<VLAN ID>.rp_filter"와 같이 인터페이스의 디바이스 이름과, VLAN ID 사이에 "/"가 들어갑니다.

 

(1) sysctl -a | grep "\.rp.filter"

(2) cat /proc/sys/net/ipv4/conf/<interface DEVICE name>/rp_filter

 

 

3) rp_filter의 all과 default 및 우선순위

rp_filter값은 인터페이스 외에 all, default값도 설정이 가능합니다. all의 경우는, 모든 rp_filter의 값을 해당 값으로써 동작하게끔 한다는 말입니다만, 특정 인터페이스에 설정된 rp_filter에 설정된 값이 all에 설정된 값 보다 클 경우에는 해당 인터페이스에 설정된 값을 따릅니다.

 

all설정 예)

(1) bond0의 rp_filter = 1, all의 rp_filter = 0 일 경우의 bond0의 rp_filter의 동작은 1(strict mode)로 동작합니다.

(2) bond0의 rp_filter = 1, all의 rp_filter = 2 일 경우의 bond0의 rp_filter의 동작은 2(loose mode)로 동작합니다.

 

default의 경우는, 새로 추가된 네트워크 인터페이스의 rp_filter값을 설정하는 역할을 합니다. 즉, default가 0일때, 새로운 네트워크 인터페이스를 추가하면, 해당 인터페이스의 rp_filter의 값은 0으로 설정됩니다.

 

 

 

2. rp_filter 동작 테스트

1) 테스트 환경

rp_filter의 동작 테스트는 의외로 간단합니다.

 

- 테스트 환경은 아래와 같습니다.

(1) VM software : VMware Workstation 15 Player(Non-commercial use only)

(2) OS : CentOS 7

 

- VM의 환경은 아래와 같습니다.

VM1 (rptest3)

NIC1(ens33, VMnet0)

NIC2(ens34, VMnet0)

 

VM2 (rptest4)

NIC1(ens33, VMnet0)

NIC2(ens34, VMnet19)

 

- VM의 네트워크 구성도는 다음과 같습니다.

VM1                                                 VM2

ens33 |                                             

        | --- bond0   ----------------------   ens33

ens34 |    (10.10.0.10)                          (10.10.0.11)

 

                                                       ens34

                                                       (10.10.1.11)

 

여기서 bond를 이용한 이유는, bond의 slave장치의 rp_filter는 동작하지 않는 것을 보여주기 위해서 입니다.

rp_filter기능은 IP/TCP 스택에서 동작하는 기능으로, IP자체가 없는 bond의 slave의 rp_filter 값은 신경쓰지 않아도 됩니다.

https://serverfault.com/questions/528018/should-rp-filter-sysctl-parameter-be-set-on-bonding-master-or-member-slaves

 

 

2) 동작 테스트

(1) 위와 같이 VM과 OS의 네트워크 설정을 마칩니다.

(2) 먼저 VM1에서 VM2로 ping이 잘 가는지 테스트 합니다.

 

(3) VM1의 bond0에서 VM2의 ens34(10.10.1.11)로 ping 테스트를 해 봅니다.

잘 가지 않는 것을 확인할 수 있습니다.

 

 

(4) VM1의 bond0의 rp_filter값을 0으로 설정하고 다시 ping 테스트를 해 봅니다.

ping이 잘 가는 것을 확인할 수 있습니다.

 

 

(5) 이번에는 VM1의 bond0의 rp_filter의 값은 1로 설정하고, VM1에서 VM2의 ens34로 가는 경로를 추가한 다음, ping 테스트를 해 봅니다.

ping이 잘 가는 것을 확인할 수 있습니다.

 

 

 

위의 테스트에서는 rp_filter의 기능으로 인해 생기는 로그는 따로 확인해 보지 않았습니다. 따로 로그를 확인해 보고 싶다면 아래와 같이 설정하면 됩니다. 

 

(1) net.ipv4.conf.all.log_martians의 값을 1로 설정합니다.

~]# sysctl -w net.ipv4.conf.all.log_martians=1

 

(2) /var/log/messages에서 "martians source"에 관한 로그를 검색합니다.

이로써 rp_filter 기능으로 인해 생기는 로그를 확인할 수 있습니다.