【10日目】TCP/IPネットワーク入門(第4章パケットの積み替え編)

皆さん,こんにちは.bb_lotusです. さて,今回はTCP/IPネットワークに関しての10日目のアウトプットになります.

すぐアウトプットだけ見たい人はこちらから

(ネットワーク初学者のため,記載内容等間違いがありましたらどしどしコメント,指摘等していただければと思います.)

使用する教材はこちらです.

 

Linuxで動かしながら学ぶTCP/IPネットワーク入門」

 

Linuxで動かしながら学ぶTCP/IPネットワーク入門 | もみじあめ |本 | 通販 | Amazon

www.amazon.co.jp

 

--------------↓ここからアウトプット--------------

今回は第4章の続きをやっていきます!

まずはパケットの積み替えです!!

前回はセグメントが1つのみでしたが,今回は2つに増やしてフレームが積み替えられる様子を見ていきます!

今回のネットワーク構成はこちら!!

f:id:bb_lotus:20220411182908j:plain

f:id:bb_lotus:20220411182904j:plain

 

では,ネットワークを作っていきます!

~[復習]ネットワークの組み方~

その1. Network Namespaceの作成

その2. ネットワークインターフェースを作成し,つなげる.

その3. 2をNetwork Namespaceで使えるようにする.

その4. お互いが通信できるようにインターフェースにIPアドレスを付与する.

その5. ネットワークインターフェースを有効にする.

その6. pingで疎通確認.

※ルーティング,ルータのカーネルパラメータの設定も忘れずに!!

 

その1. Network Namespaceの作成

$ sudo ip netns add ns1
$ sudo ip netns add router
$ sudo ip netns add ns2

 

その2. ネットワークインターフェースを作成し,つなげる.

$ sudo ip netns add ns1
$ sudo ip netns add router
$ sudo ip netns add ns2

 

その3. 2をNetwork Namespaceで使えるようにする.

$ sudo ip link set ns1-veth0 netns ns1
$ sudo ip link set gw-veth0 netns router
$ sudo ip link set gw-veth1 netns router
$ sudo ip link set ns2-veth0 netns ns2

 

その4. お互いが通信できるようにインターフェースにIPアドレスを付与する.

$ sudo ip netns exec ns1 ip address add 192.0.2.1/24 dev ns1-veth0
$ sudo ip netns exec router ip address add 192.0.2.254/24 dev gw-veth0
$ sudo ip netns exec router ip address add 198.51.100.254/24 dev gw-veth1
$ sudo ip netns exec ns2 ip address add 198.51.100.1/24 dev ns2-veth0

 

その5. ネットワークインターフェースを有効にする.

$ sudo ip netns exec ns1 ip link set ns1-veth0 up
$ sudo ip netns exec router ip link set gw-veth0 up
$ sudo ip netns exec router ip link set gw-veth1 up
$ sudo ip netns exec ns2 ip link set ns2-veth0 up

 

・ns1,ns2にrouterのIPアドレスをデフォルトルートに設定.

$ sudo ip netns exec ns1 ip route add default via 192.0.2.254
$ sudo ip netns exec ns2 ip route add default via 198.51.100.254

 

・ルータにカーネルパラメータを設定.

$ sudo ip netns exec router sysctl net.ipv4.ip_forward=1

 

イーサネットの通信の観察をするためMACアドレスを変更

$ sudo ip netns exec ns1 ip link set dev ns1-veth0 address 00:00:5E:00:53:11
$ sudo ip netns exec router ip link set dev gw-veth0 address 00:00:5E:00:53:12
$ sudo ip netns exec router ip link set dev gw-veth1 address 00:00:5E:00:53:21
$ sudo ip netns exec ns2 ip link set dev ns2-veth0 address 00:00:5E:00:53:22

 

準備が完了したのでパケットキャプチャに入っていく!!

今回は新たに2つのターミナルを用意してtcpdumpコマンドを実行する.

キャプチャするのはrouterが持つ2つのインターフェイス

目的は,それぞれのインターフェースは所属するブロードキャストドメインが異なっているので,その環境でどのようにフレームが変化するのかを確認すること!!

 

というわけで,まずはrouterのgw-veth0インターフェイスをパケットキャプチャ.

$ sudo ip netns exec router tcpdump -tnel -i gw-veth0 icmp or arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on gw-veth0, link-type EN10MB (Ethernet), capture size 262144 bytes

 

続いてrouterのgw-veth1のインターフェイスをパケットキャプチャ.

$ sudo ip netns exec router tcpdump -tnel -i gw-veth1 icmp or arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on gw-veth1, link-type EN10MB (Ethernet), capture size 262144 bytes

 

それではその6. pingで疎通確認!!

$ sudo ip netns exec ns1 ping -c 3 198.51.100.1 -I 192.0.2.1
PING 198.51.100.1 (198.51.100.1) from 192.0.2.1 : 56(84) bytes of data.
64 bytes from 198.51.100.1: icmp_seq=1 ttl=63 time=0.070 ms
64 bytes from 198.51.100.1: icmp_seq=2 ttl=63 time=0.086 ms
64 bytes from 198.51.100.1: icmp_seq=3 ttl=63 time=0.077 ms

--- 198.51.100.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2051ms
rtt min/avg/max/mdev = 0.070/0.077/0.086/0.006 ms

 

それそれのパケットキャプチャを見てみると..

・routerのgw-veth0インターフェイス

$ sudo ip netns exec router tcpdump -tnel -i gw-veth0 icmp or arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on gw-veth0, link-type EN10MB (Ethernet), capture size 262144 bytes
00:00:5e:00:53:11 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 192.0.2.254 tell 192.0.2.1, length 28
00:00:5e:00:53:12 > 00:00:5e:00:53:11, ethertype ARP (0x0806), length 42: Reply 192.0.2.254 is-at 00:00:5e:00:53:12, length 28
00:00:5e:00:53:11 > 00:00:5e:00:53:12, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 1, length 64
00:00:5e:00:53:12 > 00:00:5e:00:53:11, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 1, length 64
00:00:5e:00:53:11 > 00:00:5e:00:53:12, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 2, length 64
00:00:5e:00:53:12 > 00:00:5e:00:53:11, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 2, length 64
00:00:5e:00:53:11 > 00:00:5e:00:53:12, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 3, length 64
00:00:5e:00:53:12 > 00:00:5e:00:53:11, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 3, length 64
00:00:5e:00:53:12 > 00:00:5e:00:53:11, ethertype ARP (0x0806), length 42: Request who-has 192.0.2.1 tell 192.0.2.254, length 28
00:00:5e:00:53:11 > 00:00:5e:00:53:12, ethertype ARP (0x0806), length 42: Reply 192.0.2.1 is-at 00:00:5e:00:53:11, length 28

 

・routerのgw-veth1インターフェイス

$ sudo ip netns exec router tcpdump -tnel -i gw-veth1 icmp or arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on gw-veth1, link-type EN10MB (Ethernet), capture size 262144 bytes
00:00:5e:00:53:21 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 198.51.100.1 tell 198.51.100.254, length 28
00:00:5e:00:53:22 > 00:00:5e:00:53:21, ethertype ARP (0x0806), length 42: Reply 198.51.100.1 is-at 00:00:5e:00:53:22, length 28
00:00:5e:00:53:21 > 00:00:5e:00:53:22, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 1, length 64
00:00:5e:00:53:22 > 00:00:5e:00:53:21, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 1, length 64
00:00:5e:00:53:21 > 00:00:5e:00:53:22, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 2, length 64
00:00:5e:00:53:22 > 00:00:5e:00:53:21, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 2, length 64
00:00:5e:00:53:21 > 00:00:5e:00:53:22, ethertype IPv4 (0x0800), length 98: 192.0.2.1 > 198.51.100.1: ICMP echo request, id 8300, seq 3, length 64
00:00:5e:00:53:22 > 00:00:5e:00:53:21, ethertype IPv4 (0x0800), length 98: 198.51.100.1 > 192.0.2.1: ICMP echo reply, id 8300, seq 3, length 64
00:00:5e:00:53:22 > 00:00:5e:00:53:21, ethertype ARP (0x0806), length 42: Request who-has 198.51.100.254 tell 198.51.100.1, length 28
00:00:5e:00:53:21 > 00:00:5e:00:53:22, ethertype ARP (0x0806), length 42: Reply 198.51.100.254 is-at 00:00:5e:00:53:21, length 28

 

ふむふむ..前回学習した点を踏まえるとなんだか理解しやすいかもしれないですね.

文字だと長ったらしくなる上に,視覚的にわかりづらいので,自分が理解した範囲で図など書いてみようと思います.

 

 

 

 

~ns1とrouter間のやりとり~

ns1視点

①ん,「198.51.100.1へ疎通確認してほしい」って連絡来たな.

②ルーティングテーブルでどこに送れば良いか確認しなきゃ.

③(ルーティングテーブル確認後)あれ,198.51.100.1が宛先になってるルートないなー.ってことはデフォルトルートに設定されてる192.0.2.254へ送れば良いんか!

④192.0.2.254のMACアドレスないとパケット送れんのやった.(フレーム)

ARPリクエストして教えてもらお.

⑤192.0.2.254に対応しているMACアドレスお持ちの方ーー(ブロードキャスト)

 

router視点

①ん,ARP来たな.どれどれ,192.0.2.254?これ俺やん!ARPリプライで教えな.

 

ns1視点

⑥お.返信きた!これで送れる!!

 

 

~routerとns2間のやりとり~

router視点

①お,フレーム届いたな.宛先はどれどれ.198.51.100.1へ送るんか.

ルーティングテーブル確認しよ.

②ふむふむ.あ,198.51.100.0/24宛あるやん!gw-veth1インターフェイスから送れば良いんか.じゃああとは対応するMACアドレスARPリクエストで聞くだけか!

 

ns2視点

①ん,ARPリクエストきた.これ俺やん!返信せなー

 

router視点

③お,ARPリプライきたわ.なるほどこのMACアドレスか!送るでーー

 

 

今回で複数のセグメントをまたがってフレームがやりとりされるのを無事観察できました!

自分なりに解釈して,図を書いたことで理解もだいぶ深まった気がします!

 

今回はここまで!!

ではまた次回!!