| 1 | [[PageOutline]] |
| 2 | |
| 3 | = iptablesによるIP MASQUERADE = |
| 4 | * [http://www.atmarkit.co.jp/flinux/rensai/iptables02/iptables02c.html @IT:natテーブルを利用したLinuxルータの作成(3/6)] |
| 5 | |
| 6 | * " wiki:TipAndDoc/network/vmtest#networkmap のネットワークを使用 |
| 7 | |
| 8 | * 到達テスト用IP => 202.12.27.33 (M.ROOT-SERVERS.NET.) |
| 9 | |
| 10 | = without MASQUERADE = |
| 11 | |
| 12 | == case 1 == |
| 13 | * router -> centos-outer -(NAPT)-> WAN |
| 14 | |
| 15 | * ubuntu-router:~$ ping 202.12.27.33 -c 1 |
| 16 | {{{ |
| 17 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 18 | |
| 19 | --- 202.12.27.33 ping statistics --- |
| 20 | 1 packets transmitted, 0 received, 100% packet loss, time 0ms |
| 21 | }}} |
| 22 | 1. ubuntu-router:eth0 |
| 23 | {{{ |
| 24 | 21:36:03.484785 IP 10.0.0.254 > 202.12.27.33: ICMP echo request, id 60949, seq 1, length 64 |
| 25 | }}} |
| 26 | 1. centos-outer:eth1 |
| 27 | {{{ |
| 28 | 21:36:03.419741 IP 10.0.0.254 > 202.12.27.33: ICMP echo request, id 60949, seq 1, length 64 |
| 29 | }}} |
| 30 | 1. centos-outer:eth0 |
| 31 | {{{ |
| 32 | 21:36:03.420277 IP 10.0.0.254 > 202.12.27.33: ICMP echo request, id 60949, seq 1, length 64 |
| 33 | }}} |
| 34 | |
| 35 | == case 2 == |
| 36 | * ubuntu-outer -> router(redirect) -> centos-outer -(NAPT)-> WAN |
| 37 | |
| 38 | * ubuntu-outer:~$ ping 202.12.27.33 -c 1 |
| 39 | {{{ |
| 40 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 41 | From 10.0.0.254: icmp_seq=1 Redirect Host(New nexthop: 10.0.0.20) |
| 42 | |
| 43 | --- 202.12.27.33 ping statistics --- |
| 44 | 1 packets transmitted, 0 received, 100% packet loss, time 0ms |
| 45 | }}} |
| 46 | 1. ubuntu-outer:eth1 |
| 47 | {{{ |
| 48 | 21:28:12.959737 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 53525, seq 1, length 64 |
| 49 | 21:28:12.965215 IP 10.0.0.254 > 10.0.0.10: ICMP redirect 202.12.27.33 to host 10.0.0.20, length 92 |
| 50 | }}} |
| 51 | 1. ubuntu-router:eth0 |
| 52 | {{{ |
| 53 | 21:28:12.962459 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 53525, seq 1, length 64 |
| 54 | 21:28:12.965204 IP 10.0.0.254 > 10.0.0.10: ICMP redirect 202.12.27.33 to host 10.0.0.20, length 92 |
| 55 | 21:28:12.974739 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 53525, seq 1, length 64 |
| 56 | }}} |
| 57 | 1. centos-outer:eth1 |
| 58 | {{{ |
| 59 | 21:28:12.915534 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 53525, seq 1, length 64 |
| 60 | }}} |
| 61 | 1. centos-outer:eth0 |
| 62 | {{{ |
| 63 | 21:28:12.915855 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 53525, seq 1, length 64 |
| 64 | }}} |
| 65 | |
| 66 | == case 3 == |
| 67 | * ubuntu-inner-AB -> router -> centos-outer -(NAPT)-> WAN |
| 68 | |
| 69 | * ubuntu-inner-AB:~$ ping 202.12.27.33 -c 1 |
| 70 | {{{ |
| 71 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 72 | |
| 73 | --- 202.12.27.33 ping statistics --- |
| 74 | 1 packets transmitted, 0 received, 100% packet loss, time 0ms |
| 75 | }}} |
| 76 | 1. ubuntu-inner-AB:eth1 |
| 77 | {{{ |
| 78 | 20:50:23.110897 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 4372, seq 1, length 64 |
| 79 | }}} |
| 80 | 1. ubuntu-router:eth2 |
| 81 | {{{ |
| 82 | 20:50:23.125749 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 4372, seq 1, length 64 |
| 83 | }}} |
| 84 | 1. ubuntu-router:eth0 |
| 85 | {{{ |
| 86 | 20:50:23.134574 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 4372, seq 1, length 64 |
| 87 | }}} |
| 88 | 1. centos-outer:eth1 |
| 89 | {{{ |
| 90 | 20:50:23.090600 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 4372, seq 1, length 64 |
| 91 | }}} |
| 92 | 1. centos-outer:eth0 |
| 93 | {{{ |
| 94 | 20:50:23.092625 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 4372, seq 1, length 64 |
| 95 | }}} |
| 96 | |
| 97 | = IP MASQUERADE = |
| 98 | * 上記のcase1, 2は、centos-outerにIP MASQUERADEの設定をすれば解決する |
| 99 | * centos-outer ~]$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE |
| 100 | * centos-outer ~]$ sudo iptables -t nat -L -v |
| 101 | {{{ |
| 102 | Chain PREROUTING (policy ACCEPT 21 packets, 3810 bytes) |
| 103 | pkts bytes target prot opt in out source destination |
| 104 | |
| 105 | Chain POSTROUTING (policy ACCEPT 4 packets, 336 bytes) |
| 106 | pkts bytes target prot opt in out source destination |
| 107 | 0 0 MASQUERADE all -- any eth0 anywhere anywhere |
| 108 | |
| 109 | Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) |
| 110 | pkts bytes target prot opt in out source destination |
| 111 | }}} |
| 112 | |
| 113 | == case 1 == |
| 114 | * ubuntu-router:~$ ping 202.12.27.33 -c 1 |
| 115 | {{{ |
| 116 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 117 | 64 bytes from 202.12.27.33: icmp_seq=1 ttl=239 time=16.3 ms |
| 118 | |
| 119 | --- 202.12.27.33 ping statistics --- |
| 120 | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
| 121 | rtt min/avg/max/mdev = 16.394/16.394/16.394/0.000 ms |
| 122 | }}} |
| 123 | 1. ubuntu-router:eth0 |
| 124 | {{{ |
| 125 | 21:52:23.674784 IP 10.0.0.254 > 202.12.27.33: ICMP echo request, id 29462, seq 1, length 64 |
| 126 | 21:52:23.686245 IP 202.12.27.33 > 10.0.0.254: ICMP echo reply, id 29462, seq 1, length 64 |
| 127 | }}} |
| 128 | 1. centos-outer:eth1 |
| 129 | {{{ |
| 130 | 21:52:23.585426 IP 10.0.0.254 > 202.12.27.33: ICMP echo request, id 29462, seq 1, length 64 |
| 131 | 21:52:23.600322 IP 202.12.27.33 > 10.0.0.254: ICMP echo reply, id 29462, seq 1, length 64 |
| 132 | }}} |
| 133 | 1. centos-outer:eth0 |
| 134 | {{{ |
| 135 | 21:52:23.586646 IP 192.168.40.20 > 202.12.27.33: ICMP echo request, id 29462, seq 1, length 64 |
| 136 | 21:52:23.600260 IP 202.12.27.33 > 192.168.40.20: ICMP echo reply, id 29462, seq 1, length 64 |
| 137 | }}} |
| 138 | |
| 139 | == case 2 == |
| 140 | * ubuntu-outer:~$ ping 202.12.27.33 -c 1 |
| 141 | {{{ |
| 142 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 143 | From 10.0.0.254: icmp_seq=1 Redirect Host(New nexthop: 10.0.0.20) |
| 144 | 64 bytes from 202.12.27.33: icmp_seq=1 ttl=239 time=18.2 ms |
| 145 | |
| 146 | --- 202.12.27.33 ping statistics --- |
| 147 | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
| 148 | rtt min/avg/max/mdev = 18.232/18.232/18.232/0.000 ms |
| 149 | }}} |
| 150 | 1. ubuntu-outer:eth1 |
| 151 | {{{ |
| 152 | 21:54:52.110334 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 42518, seq 1, length 64 |
| 153 | 21:54:52.112754 IP 10.0.0.254 > 10.0.0.10: ICMP redirect 202.12.27.33 to host 10.0.0.20, length 92 |
| 154 | 21:54:52.128234 IP 202.12.27.33 > 10.0.0.10: ICMP echo reply, id 42518, seq 1, length 64 |
| 155 | }}} |
| 156 | 1. ubuntu-router:eth0 |
| 157 | {{{ |
| 158 | 21:54:52.110893 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 42518, seq 1, length 64 |
| 159 | 21:54:52.112704 IP 10.0.0.254 > 10.0.0.10: ICMP redirect 202.12.27.33 to host 10.0.0.20, length 92 |
| 160 | 21:54:52.113196 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 42518, seq 1, length 64 |
| 161 | }}} |
| 162 | 1. centos-outer:eth1 |
| 163 | {{{ |
| 164 | 21:54:52.033678 IP 10.0.0.10 > 202.12.27.33: ICMP echo request, id 42518, seq 1, length 64 |
| 165 | 21:54:52.048781 IP 202.12.27.33 > 10.0.0.10: ICMP echo reply, id 42518, seq 1, length 64 |
| 166 | }}} |
| 167 | 1. centos-outer:eth0 |
| 168 | {{{ |
| 169 | 21:54:52.035689 IP 192.168.40.20 > 202.12.27.33: ICMP echo request, id 42518, seq 1, length 64 |
| 170 | 21:54:52.048112 IP 202.12.27.33 > 192.168.40.20: ICMP echo reply, id 42518, seq 1, length 64 |
| 171 | }}} |
| 172 | |
| 173 | == case 3 == |
| 174 | * ubuntu-inner-AB -> router -> centos-outer -(NAPT)-> WAN |
| 175 | * この場合、WAN -(NAPT)-> centos-outer で帰ってきたパケットを10.2.0.30(ubuntu-inner-AB)に送り返そうとするが、centos-outerのデフォルトゲートウェイがあるネットワークはeth0の向きなので、ubuntu-inner-ABがあるeth1ではなくeth0にパケットが送出されてしまう。 |
| 176 | * MASQUERADEに併せて、centos-outerにstatic routeの設定が必要 |
| 177 | |
| 178 | * ubuntu-inner-AB:~$ ping 202.12.27.33 -c 1 |
| 179 | {{{ |
| 180 | PING 202.12.27.33 (202.12.27.33) 56(84) bytes of data. |
| 181 | |
| 182 | --- 202.12.27.33 ping statistics --- |
| 183 | 1 packets transmitted, 0 received, 100% packet loss, time 0ms |
| 184 | }}} |
| 185 | 1. ubuntu-inner-AB:eth1 |
| 186 | {{{ |
| 187 | 20:56:09.881451 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 15892, seq 1, length 64 |
| 188 | }}} |
| 189 | 1. ubuntu-router:eth2 |
| 190 | {{{ |
| 191 | 20:56:09.895738 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 15892, seq 1, length 64 |
| 192 | }}} |
| 193 | 1. ubuntu-router:eth0 |
| 194 | {{{ |
| 195 | 20:56:09.896113 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 15892, seq 1, length 64 |
| 196 | }}} |
| 197 | 1. centos-outer:eth1 |
| 198 | {{{ |
| 199 | 20:56:09.861559 IP 10.2.0.30 > 202.12.27.33: ICMP echo request, id 15892, seq 1, length 64 |
| 200 | }}} |
| 201 | 1. centos-outer:eth0 |
| 202 | {{{ |
| 203 | 20:56:09.863646 IP 192.168.40.20 > 202.12.27.33: ICMP echo request, id 15892, seq 1, length 64 |
| 204 | 20:56:09.876772 IP 202.12.27.33 > 192.168.40.20: ICMP echo reply, id 15892, seq 1, length 64 |
| 205 | 20:56:09.878089 IP 202.12.27.33 > 10.2.0.30: ICMP echo reply, id 15892, seq 1, length 64 |
| 206 | }}} |