RIP 與子網路切割

January 5, 2019

拓樸

學習目標

拓樸子網路分配取切割

定義了 192.168.1.0/24 是給一間學校的網段。其中 R1、R2、R3 是學校三棟大樓的核心。 R1 假設為一年級電腦教室,在介面 e5/0 下給了 40 個主機給一年級使用;R2 假設為二、三年級電腦教室,在介面 e5/0 給了 80 個主機給二年級電腦教室使用;R3 假設為四年級電腦教室,在介面 e5/0 給了 20 個主機給二年級電腦教室使用。但是要如何有效切割子網路才能達到節省 IP 的目標 ? 「必須利用無類別區隔路由(CIDR)技巧來達到節省 IP」。

IP 為 32 bit 組成,IP 位址以 Classful 分類為 A、B、C、D、E 五類。是以 32 bit 以每 8 bit 一組來區隔做不同的類別,如下表。

網路類別IP 位置範圍
Class A0.0.0.0 ~ 127.0.0.0
Class B128.0.0.0 ~ 191.255.0.0
Class C192.0.0.0 ~ 223.255.255.0
Class D224.0.0.0 ~ 239.255.255.255
Class E240.0.0.0 ~ 255.255.255.255

在每個類跟類之間都有一定的 IP 可用數量,可根據需求去分配位置,而這樣的方式會造成 IP 可用數量的浪費。

Classless Inter-Domain Routing(CIDR)

它跟上述 Classful 差異是在CIDR 可以變動子網路遮罩(netmask)。 以拓樸 Address Space 為例 192.168.1.0/24,此表示代表網路位址是 192.168.1.0,而後面的 24 表示網路遮罩的二進位表示法中,最後 8 個數字為 0,若以二進位表示網路遮罩的話如下: 11111111.11111111.11111111.0000000 24 個 1,將它轉換為 10 進位就變成 255.255.255.0

Network Address 和 Host Address 與 Broadcast address

假設有一個 IP 為 192.168.1.126/24 他位於 192.168.1.0/24 子網路內。則

子網路可使用 IP 數量

網路遮罩就是可以來限制一個子網路可擁有的 IP 數量。

實作切割子網路

我們先分配二年級 80 台電腦。

2x > 80,得知 x = 7 才會大於 80 32 - 7 = 25,得知 25 為子網遮罩,所以由 192.168.1.0/24 切給二年級的子網路為

11000000.10101000.00000001.00000000 (原本 Address Space)

11111111.11111111.11111111.10000000 (得知的網路遮罩 25)

11000000.10101000.00000001.00000000 (遮罩為 25 前 25 位元不能動)

再利用 192.168.1.0/25 來分配給 80 個主機。 因為 27 = 128,所以一個子網路只能有 128 個位址,因此分配給二年級 192.168.1.0 ~ 192.168.1.127 的位址,記住 Network Address(192.168.1.0) 和 Broadcast address(即一個子網路的最後一個位址 192.168.1.127) 不能當 Host address

再來分配一年級 40 個主機。 2x > 40,得知 x = 6 才會大於 40。 32 - 6 = 26,26 則為遮罩。 11000000.10101000.00000001.00000000 (原本 Address Space)

11111111.11111111.11111111.11000000 (得知的網路遮罩 26)

11000000.10101000.00000001.00000000 (遮罩為 25 前 25 位元不能動) 再利用 192.168.1.0/26 來分配給 40 個主機。 因為 26 = 64,所以一個子網路只能有 64 個位址,因此分配給一年級 192.168.1.0 ~ 192.168.1.63 的位址,但是因為二年級分配的位址是 192.168.1.0 ~ 192.168.1.127,因此子網路 192.168.1.0 ~ 192.168.1.63192.168.1.64 ~ 192.168.1.127 不能分配給一年級,要從 192.168.1.128 ~ 192.168.1.191

剩下部分交給讀者自行運算了解。路由器特性也很重要喔!!

建立子網路

設定 R1 和 PC IP 位址與 gateway

R1、R2、R3 以 R1 範例做為示範,R2、R3 設定也是相似的

R1

以 R1 e5/0 介面為例

R1#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#interface ethernet 5/0
R1(config-if)#ip address 192.168.1.129 255.255.255.192
R1(config-if)#no shutdown

檢察界面次是否有設定即啟動

R1#show ip int brief
Interface                  IP-Address      OK? Method Status                Prot                                          ocol
FastEthernet0/0            unassigned      YES NVRAM  administratively down down                                          
FastEthernet1/0            unassigned      YES NVRAM  administratively down down                                          
FastEthernet1/1            unassigned      YES NVRAM  administratively down down                                          
FastEthernet2/0            unassigned      YES NVRAM  administratively down down                                          
FastEthernet2/1            unassigned      YES NVRAM  administratively down down                                          
GigabitEthernet3/0         unassigned      YES NVRAM  administratively down down                                          
GigabitEthernet4/0         unassigned      YES NVRAM  administratively down down                                          
Ethernet5/0                192.168.1.129   YES NVRAM  up                    up                                            
Ethernet5/1                unassigned      YES NVRAM  administratively down down                                          
Ethernet5/2                unassigned      YES NVRAM  administratively down down                                          
Ethernet5/3                unassigned      YES NVRAM  administratively down down                                          
Serial6/0                  192.168.1.225   YES NVRAM  up                    up                                            
Serial6/1                  192.168.1.229   YES NVRAM  up                    up                                            
Serial6/2                  unassigned      YES NVRAM  administratively down down                                          
Serial6/3                  unassigned      YES NVRAM  administratively down down                                          
Serial6/4                  unassigned      YES NVRAM  administratively down down                                          
Serial6/5                  unassigned      YES NVRAM  administratively down down                                          
Serial6/6                  unassigned      YES NVRAM  administratively down down                                          
Serial6/7                  unassigned      YES NVRAM  administratively down down                                          

PC-1 與 ubuntu-1 和 Python,Go,Perl,PHP-1 設定

PC-1 就是 PC,假設讀者都將所有介面的 IP 都設置完畢。

PC-1> ip 192.168.1.126/25 192.168.1.1
PC-1> show ip # 查看 IP 資訊

NAME        : PC-1[1]
IP/MASK     : 192.168.1.126/25
GATEWAY     : 192.168.1.1
DNS         :
MAC         : 00:50:79:66:68:00
LPORT       : 10001
RHOST:PORT  : 127.0.0.1:10002
MTU:        : 1500

ubuntu-1 和 Python,Go,Perl,PHP-1 滑鼠點選電腦右鍵 Edit Config 即可編輯設定網路 以 ubuntu-1 為範例

#
# This is a sample network config uncomment lines to configure the network
#


# Static config for eth0
auto eth0
iface eth0 inet static
	address 192.168.1.190
	netmask 255.255.255.192
	gateway 192.168.1.129
#	up echo nameserver 192.168.0.1 > /etc/resolv.conf

# DHCP config for eth0
# auto eth0
# iface eth0 inet dhcp

ping 測試

當上述設定好之後,我們利用 ping 指令來測試能否到達自家的路由器。 以 PC-1 為測試 ping R2 的 e5/0 介面,路由器與路由器之間也可以用 ping 來測試,但是是要直連的網路,直連的網路不須透過路由協定即可學習並寫入至路由表

直連網路

查看 R1 路由表,原則上沒做到路由協定設定應該只會只有直連的網路。 C 表示直連網路,從字面上來看不必透過任何媒介即可到達目的地。PC-1 直接的連接 R2 e5/0 介面,但兩個介面應該要是 up 的。 路由器能夠路由所有發往直接連接到其 up 的介面的子網中所有主機的封包。

R1#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       + - replicated route, % - next hop override

Gateway of last resort is not set

      192.168.1.0/24 is variably subnetted, 8 subnets, 5 masks
R        192.168.1.0/25 [120/1] via 192.168.1.226, 00:00:14, Serial6/0
C        192.168.1.128/26 is directly connected, Ethernet5/0
L        192.168.1.129/32 is directly connected, Ethernet5/0
R        192.168.1.192/27 [120/1] via 192.168.1.230, 00:00:19, Serial6/1
C        192.168.1.224/30 is directly connected, Serial6/0
L        192.168.1.225/32 is directly connected, Serial6/0
C        192.168.1.228/30 is directly connected, Serial6/1
L        192.168.1.229/32 is directly connected, Serial6/1

PC-1

因為是直連網路所以要能通,如果出現錯誤,請檢察兩邊介面的 IP 或閘道和介面有無啟動。

PC-1> ping 192.168.1.1
84 bytes from 192.168.1.1 icmp_seq=1 ttl=255 time=3.930 ms
84 bytes from 192.168.1.1 icmp_seq=2 ttl=255 time=8.928 ms
84 bytes from 192.168.1.1 icmp_seq=3 ttl=255 time=17.355 ms
R3

! 表示有通,. 表示沒通。

R3#ping 192.168.1.229
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.229, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 16/31/52 ms

ping 可以用 ICMP 方式來診斷網路通訊的問題。

RIP

上面步驟如果都成功理論上,PC-1 想取得 ubuntu 資訊是不可能的。因為我們沒設定路由協定,讓路由學習路線資訊。因此,我們在這邊使用 RIPv2 路由協定來實現。

RIP 是採用 Distance Vector 演算法,利用 Hops 決定路徑,會在鄰近的路由設備間將路徑資料分享。

RIP 的協定是 UDP 用 520 Port

RIP 特性

  1. 使用 Distance Vector 演算法
  2. 根據網路路徑所必須經過的設備數目決定最佳路徑
  3. 一條網路路徑最多經過 15 個路由設備
  4. 預設每 30 秒互相傳遞路由資訊做更新
  5. 支援 Load blance 功能,預設四條路徑,至多六條

RIPv1 與 RIPv2 差異

差異RIPv1RIPv1
路由類型ClassfulClassless
可變化長度子網路遮罩
手動路由匯總
認證
傳送路由資訊BroadcastMulticast

路由設定

使用 R1 做設定,R2、R3 也是類似的設定

選擇使用的路由協定

R1(config)#router rip
R1(config-router)#version 2

設定網路

R1(config-router)#network 192.168.1.129
R1(config-router)#network 192.168.1.225
R1(config-router)#network 192.168.1.229
R1(config-router)#no auto-summary

做完之後 R2、R3 也可以進行設定了。

檢視 RIP 路由協定設定值

R1#show ip protocols
*** IP Routing is NSF aware ***

Routing Protocol is "rip"
  Outgoing update filter list for all interfaces is not set
  Incoming update filter list for all interfaces is not set
  Sending updates every 30 seconds, next due in 26 seconds
  Invalid after 180 seconds, hold down 180, flushed after 240
  Redistributing: rip
  Default version control: send version 2, receive version 2
    Interface             Send  Recv  Triggered RIP  Key-chain
    Ethernet5/0           2     2
    Serial6/0             2     2
    Serial6/1             2     2
  Automatic network summarization is not in effect
  Maximum path: 4
  Routing for Networks:
    192.168.1.0
  Routing Information Sources:
    Gateway         Distance      Last Update
    192.168.1.226        120      00:00:15
    192.168.1.230        120      00:00:25
  Distance: (default is 120)


Sending updates every 30 seconds 每 30 秒更新。 Invalid after 180 seconds, hold down 180, flushed after 240,假設 R1 180 秒內沒收到其他路由器傳的路由更新資訊,R1 會將此路由路徑標記為 Possibly Down

hold down 180,當 R1 發現有 Possibly Down 的路由資訊時,會立即啟動 Hold-Down Timer,如果經 180 秒後,都沒收到那筆路由資訊更新時,R1 會將此路由資訊視同無法到達。

flushed after 240,若經過 240 秒後沒再收到任意的路由資訊更新,則 R1 會把此筆路由資料從 Routing Table 中移除。

Routing for Networks,表示 R1 正在處裡的網段。

Routing Information Sources,代表這些 IP 會傳送路由資訊給 R1 這台路由器。

查看路由表

此時應該會多出現 R Codes。

R1#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       + - replicated route, % - next hop override

Gateway of last resort is not set

      192.168.1.0/24 is variably subnetted, 8 subnets, 5 masks
R        192.168.1.0/25 [120/1] via 192.168.1.226, 00:00:29, Serial6/0
C        192.168.1.128/26 is directly connected, Ethernet5/0
L        192.168.1.129/32 is directly connected, Ethernet5/0
R        192.168.1.192/27 [120/1] via 192.168.1.230, 00:00:27, Serial6/1
C        192.168.1.224/30 is directly connected, Serial6/0
L        192.168.1.225/32 is directly connected, Serial6/0
C        192.168.1.228/30 is directly connected, Serial6/1
L        192.168.1.229/32 is directly connected, Serial6/1

R 192.168.1.0/25 [120/1] via 192.168.1.226, 00:00:29, Serial6/0 作為解讀範例。 從 Serial6/0 介面出去,經由 192.168.1.226(下一台路由器介面 IP 位址) 可以抵達 192.168.1.0/25 子網路。[120/1],120 表 RIP 路由協定 AD 值,1 表 Hops,需要經過一個路由器才能到 192.168.1.0/25 子網路。

traceroute 查看

ubuntu-1 traceroute Python,Go,Perl,PHP-1 觀察他經過的節點,他經過 2 hops。

root@Ubuntu-1:~# traceroute 192.168.1.222
traceroute to 192.168.1.222 (192.168.1.222), 30 hops max, 60 byte packets
 1  192.168.1.129 (192.168.1.129)  7.106 ms  8.197 ms  8.512 ms
 2  192.168.1.230 (192.168.1.230)  27.850 ms  28.273 ms  28.259 ms
 3  192.168.1.222 (192.168.1.222)  37.986 ms  38.207 ms  38.246 ms

使用 wireshark 觀察

ubuntu-1 ping Python,Go,Perl,PHP-1

wireshark 監控 e7(ethernwtswitch-3) 與 eth0(Python,Go,Perl,PHP-1 ) 之間介面

root@Ubuntu-1:~# ping 192.168.1.222
PING 192.168.1.222 (192.168.1.222) 56(84) bytes of data.
64 bytes from 192.168.1.222: icmp_seq=1 ttl=62 time=42.3 ms
64 bytes from 192.168.1.222: icmp_seq=2 ttl=62 time=38.3 ms
64 bytes from 192.168.1.222: icmp_seq=3 ttl=62 time=24.4 ms
64 bytes from 192.168.1.222: icmp_seq=4 ttl=62 time=28.7 ms
64 bytes from 192.168.1.222: icmp_seq=5 ttl=62 time=24.4 ms
64 bytes from 192.168.1.222: icmp_seq=6 ttl=62 time=29.3 ms
64 bytes from 192.168.1.222: icmp_seq=7 ttl=62 time=22.4 ms
^C
--- 192.168.1.222 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6005ms
rtt min/avg/max/mdev = 22.416/30.037/42.391/7.015 ms
root@Ubuntu-1:~#

wireshark

抓到封包的資訊如下

這是使用 ping 的封包

查看第 40 個封包,觀察來源和目的的 MAC 位址。

從 RIPv2 協定查看內容,可以發現以 520 UDP 傳送資訊。

參考

wiki

wiki2

netadmin