| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = iproute2 = |
| | 4 | * see wiki:TipAndDoc/network/iproute |
| | 5 | |
| | 6 | * [../#networkmap] のネットワークを使用 |
| | 7 | |
| | 8 | = centos-inner = |
| | 9 | * パケットの往路と復路が異なってしまう問題への対応 |
| | 10 | * see [../route#imperfect] |
| | 11 | |
| | 12 | == 設定前 == |
| | 13 | * centos-inner ~]$ route |
| | 14 | {{{ |
| | 15 | Kernel IP routing table |
| | 16 | Destination Gateway Genmask Flags Metric Ref Use Iface |
| | 17 | 192.168.50.0 * 255.255.255.0 U 0 0 0 eth0 |
| | 18 | 10.2.0.0 * 255.255.0.0 U 0 0 0 eth2 |
| | 19 | 10.0.0.0 10.1.0.254 255.255.0.0 UG 0 0 0 eth1 |
| | 20 | 10.1.0.0 * 255.255.0.0 U 0 0 0 eth1 |
| | 21 | 169.254.0.0 * 255.255.0.0 U 0 0 0 eth2 |
| | 22 | default 192.168.50.1 0.0.0.0 UG 0 0 0 eth0 |
| | 23 | }}} |
| | 24 | * 10.0.0.0/16のルートについては設定済み |
| | 25 | |
| | 26 | * centos-inner ~]$ ip route |
| | 27 | {{{ |
| | 28 | 192.168.50.0/24 dev eth0 proto kernel scope link src 192.168.50.110 |
| | 29 | 10.2.0.0/16 dev eth2 proto kernel scope link src 10.2.0.110 |
| | 30 | 10.0.0.0/16 via 10.1.0.254 dev eth1 |
| | 31 | 10.1.0.0/16 dev eth1 proto kernel scope link src 10.1.0.110 |
| | 32 | 169.254.0.0/16 dev eth2 scope link |
| | 33 | default via 192.168.50.1 dev eth0 |
| | 34 | }}} |
| | 35 | * centos-inner ~]$ ip route show dev eth0 |
| | 36 | {{{ |
| | 37 | 192.168.50.0/24 proto kernel scope link src 192.168.50.110 |
| | 38 | default via 192.168.50.1 |
| | 39 | }}} |
| | 40 | * centos-inner ~]$ ip route show dev eth1 |
| | 41 | {{{ |
| | 42 | 10.0.0.0/16 via 10.1.0.254 |
| | 43 | 10.1.0.0/16 proto kernel scope link src 10.1.0.110 |
| | 44 | }}} |
| | 45 | * centos-inner ~]$ ip route show dev eth2 |
| | 46 | {{{ |
| | 47 | 10.2.0.0/16 proto kernel scope link src 10.2.0.110 |
| | 48 | 169.254.0.0/16 scope link |
| | 49 | }}} |
| | 50 | |
| | 51 | * centos-inner ~]$ ip rule |
| | 52 | {{{ |
| | 53 | 0: from all lookup 255 |
| | 54 | 32766: from all lookup main |
| | 55 | 32767: from all lookup default |
| | 56 | }}} |
| | 57 | |
| | 58 | * centos-outer <=(router)=> centos-inner |
| | 59 | * centos-outer ~]$ ping 10.2.0.110 -c 1 |
| | 60 | 1. centos-outer:eth1 |
| | 61 | 1. ubuntu-router:eth0 |
| | 62 | 1. ubuntu-router:eth2 |
| | 63 | {{{ |
| | 64 | 17:18:03.739868 IP 10.0.0.20 > 10.2.0.110: ICMP echo request, id 31500, seq 1, length 64 |
| | 65 | }}} |
| | 66 | 1. centos-inner:eth2 |
| | 67 | {{{ |
| | 68 | 17:18:03.794897 IP 10.0.0.20 > 10.2.0.110: ICMP echo request, id 31500, seq 1, length 64 |
| | 69 | }}} |
| | 70 | 1. centos-inner:eth1 |
| | 71 | {{{ |
| | 72 | 17:18:03.795150 IP 10.2.0.110 > 10.0.0.20: ICMP echo reply, id 31500, seq 1, length 64 |
| | 73 | }}} |
| | 74 | 1. ubuntu-router:eth1 |
| | 75 | {{{ |
| | 76 | 17:18:03.741063 IP 10.2.0.110 > 10.0.0.20: ICMP echo reply, id 31500, seq 1, length 64 |
| | 77 | }}} |
| | 78 | 1. ubuntu-router:eth0 |
| | 79 | 1. centos-outer:eth1 |
| | 80 | |
| | 81 | == 設定 == |
| | 82 | * routing table 設定 |
| | 83 | 1. centos-inner ~]$ sudo ip route add default via 10.1.0.254 table 1 |
| | 84 | 1. centos-inner ~]$ sudo ip route add default via 10.2.0.254 table 2 |
| | 85 | * routing policy 設定 |
| | 86 | 1. centos-inner ~]$ sudo ip rule add from 10.1.0.110 table 1 prio 1 |
| | 87 | 1. centos-inner ~]$ sudo ip rule add from 10.2.0.110 table 2 prio 2 |
| | 88 | |
| | 89 | * routing policy に関しては、centos-innerはeth1とeth2でネットワークが分かれているので以下のような設定でも良い |
| | 90 | 1. centos-inner ~]$ sudo ip rule add from 10.1.0.0/16 table 1 prio 1 |
| | 91 | 1. centos-inner ~]$ sudo ip rule add from 10.2.0.0/16 table 2 prio 2 |
| | 92 | * centos-inner ~]$ ip rule |
| | 93 | {{{ |
| | 94 | 0: from all lookup 255 |
| | 95 | 1: from 10.1.0.0/16 lookup 1 |
| | 96 | 2: from 10.2.0.0/16 lookup 2 |
| | 97 | 32766: from all lookup main |
| | 98 | 32767: from all lookup default |
| | 99 | }}} |
| | 100 | |
| | 101 | === 反映 === |
| | 102 | * ip route list table main |
| | 103 | {{{ |
| | 104 | 192.168.50.0/24 dev eth0 proto kernel scope link src 192.168.50.110 |
| | 105 | 10.2.0.0/16 dev eth2 proto kernel scope link src 10.2.0.110 |
| | 106 | 10.0.0.0/16 via 10.1.0.254 dev eth1 |
| | 107 | 10.1.0.0/16 dev eth1 proto kernel scope link src 10.1.0.110 |
| | 108 | 169.254.0.0/16 dev eth2 scope link |
| | 109 | default via 192.168.50.1 dev eth0 |
| | 110 | }}} |
| | 111 | * main(default)のtableには変化無し |
| | 112 | * ip route list table 1 |
| | 113 | {{{ |
| | 114 | default via 10.1.0.254 dev eth1 |
| | 115 | }}} |
| | 116 | * ip route list table 2 |
| | 117 | {{{ |
| | 118 | default via 10.2.0.254 dev eth2 |
| | 119 | }}} |
| | 120 | * ip rule |
| | 121 | {{{ |
| | 122 | 0: from all lookup 255 |
| | 123 | 1: from 10.1.0.110 lookup 1 |
| | 124 | 2: from 10.2.0.110 lookup 2 |
| | 125 | 32766: from all lookup main |
| | 126 | 32767: from all lookup default |
| | 127 | }}} |
| | 128 | |
| | 129 | == 結果 == |
| | 130 | * eth1(10.1.0.110)に入ってきたパケットはeth1から、eth2(10.2.0.110)に入ってきたパケットはeth2から送出されるようになる。 |
| | 131 | |
| | 132 | * centos-outer ~]$ ping 10.2.0.110 -c 1 |
| | 133 | 1. centos-outer:eth1 |
| | 134 | 1. ubuntu-router:eth0 |
| | 135 | 1. ubuntu-router:eth2 |
| | 136 | {{{ |
| | 137 | 17:09:25.667375 IP 10.0.0.20 > 10.2.0.110: ICMP echo request, id 22028, seq 1, length 64 |
| | 138 | 17:09:25.668310 IP 10.2.0.110 > 10.0.0.20: ICMP echo reply, id 22028, seq 1, length 64 |
| | 139 | }}} |
| | 140 | 1. centos-inner:eth2 |
| | 141 | {{{ |
| | 142 | 17:09:25.682422 IP 10.0.0.20 > 10.2.0.110: ICMP echo request, id 22028, seq 1, length 64 |
| | 143 | 17:09:25.682547 IP 10.2.0.110 > 10.0.0.20: ICMP echo reply, id 22028, seq 1, length 64 |
| | 144 | }}} |
| | 145 | |
| | 146 | * centos-outer ~]$ ping 10.1.0.110 -c 1 |
| | 147 | 1. centos-outer:eth1 |
| | 148 | 1. ubuntu-router:eth0 |
| | 149 | 1. ubuntu-router:eth1 |
| | 150 | {{{ |
| | 151 | 17:12:29.065247 IP 10.0.0.20 > 10.1.0.110: ICMP echo request, id 25356, seq 1, length 64 |
| | 152 | 17:12:29.075733 IP 10.1.0.110 > 10.0.0.20: ICMP echo reply, id 25356, seq 1, length 64 |
| | 153 | }}} |
| | 154 | 1. centos-inner:eth1 |
| | 155 | {{{ |
| | 156 | 17:12:29.111101 IP 10.0.0.20 > 10.1.0.110: ICMP echo request, id 25356, seq 1, length 64 |
| | 157 | 17:12:29.116137 IP 10.1.0.110 > 10.0.0.20: ICMP echo reply, id 25356, seq 1, length 64 |
| | 158 | }}} |
| | 159 | |
| | 160 | = 補足 = |
| | 161 | * ルールを削除する場合は、table あるいは priority を指定して削除する |
| | 162 | {{{ |
| | 163 | 0: from all lookup 255 |
| | 164 | 1: from 10.1.0.110 lookup 1 |
| | 165 | 2: from 10.2.0.110 lookup 2 |
| | 166 | 32766: from all lookup main |
| | 167 | 32767: from all lookup default |
| | 168 | }}} |
| | 169 | * sudo ip rule del table 1 |
| | 170 | * sudo ip rule del prio 1 |
| | 171 | |
| | 172 | * priority は現在のところ省略可能 |
| | 173 | * sudo ip rule add from 10.1.0.110 table 1 |
| | 174 | * ip rule |
| | 175 | {{{ |
| | 176 | 0: from all lookup 255 |
| | 177 | 32765: from 10.1.0.110 lookup 1 |
| | 178 | 32766: from all lookup main |
| | 179 | 32767: from all lookup default |
| | 180 | }}} |
| | 181 | * attachment:ip-cref.pdf:wiki:TipAndDoc/network/iproute p.38 |
| | 182 | |
| | 183 | Really, for historical reasons '''ip rule add''' does not require a priority value and |
| | 184 | allows them to be non-unique. If the user does not supplied a priority, it is selected |
| | 185 | by the kernel. If the user creates a rule with a priority value that already exists, the |
| | 186 | kernel does not reject the request. It adds the new rule before all old rules of the same |
| | 187 | priority. |
| | 188 | It is mistake in design, no more. And it will be fixed one day, so do not rely on this |
| | 189 | feature. Use explicit priorities. |
| | 190 | |
| | 191 | * prio(priorityの省略形)の代わりに、preferも使える模様 |
| | 192 | * sudo ip rule add from 10.1.0.110 table 1 prefer 1 |
| | 193 | * ip rule |
| | 194 | {{{ |
| | 195 | 0: from all lookup 255 |
| | 196 | 1: from 10.1.0.110 lookup 1 |
| | 197 | 32766: from all lookup main |
| | 198 | 32767: from all lookup default |
| | 199 | }}} |
| | 200 | [mitty@centos-inner ~]$ |
| | 201 | [mitty@centos-inner ~]$ ip rule |
| | 202 | 0: from all lookup 255 |
| | 203 | 32766: from all lookup main |
| | 204 | 32767: from all lookup default |
| | 205 | |
| | 206 | * routing table はキャッシュされるので、すぐに反映されない場合がある |
| | 207 | * cacheの確認 => ip route show cache |
| | 208 | * cacheの破棄 => sudo ip route flush cache |
| | 209 | |
| | 210 | * attachment:ip-cref.pdf:wiki:TipAndDoc/network/iproute p.38 |
| | 211 | |
| | 212 | Warning: Changes to the RPDB made with these commands do not become active |
| | 213 | immediately. It is assumed that after a script finishes a batch of updates, it flushes |
| | 214 | the routing cache with '''ip route flush cache'''. |