通読: Linuxで動かしながら学ぶTCP/IPネットワーク入門
実際にネットワークデバイスを追加して仮想的にL2ネットワークを配線したり、ip routeサブコマンドを使ってルーティングテーブルを操作してL3の動きを学んだりする。アプリケーションプロトコル(L7)に入るとdig,dnsmasq,dhclientでDNS(udp:53),DHCP(udp:67)も体験できる。もちろんHTTP/1.x(tcp:80)も触れる。iptablesを使ってNATも試してみる。とても具体的に進むので理解が進むし基本が身につくので他のネットワーク本を読む足がかりに良い。
ipコマンドによるリソース操作
出てきたやつを拾ってメモ。
code: ip-x.sh
# したみたいに名前空間を定義して仮想デバイスを作って登録したりIPアドレスを与えたりMACアドレスを変更したりする。
# ネットワークデバイスの名前空間を切り出す
ip netns add ns1
ip netns add ns2
# veth はPIPEみたいなネットワークデバイスでLANケーブルで接続された2枚のNICみたいなもの
ip link add ns1-veth0 type veth peer name ns2-veth0
# 作ったNICをそれぞれの名前空間に移動させる
ip link set ns1-veth0 netns ns1
ip link set ns2-veth0 netns ns2
# NICを設定したりしてる
sudo ip netns exec ns1 ip link set ns1-veth0 up
sudo ip netns exec ns2 ip link set ns2-veth0 up
sudo ip netns exec ns1 ip address add 192.0.2.1/24 dev ns1-veth0
sudo ip netns exec ns1 ip link ns1-veth0 address 00:00:5E:00:00:01
sudo ip netns exec ns2 ip address add 192.0.2.2/24 dev ns2-veth0
sudo ip netns exec ns2 ip link ns2-veth0 address 00:00:5E:00:00:02
# ブリッジを作ったりもできる
ip link add br0 type bridge
ip link set dummy-br0 master br0
# ルーティングテーブルを追加する
ip route list
ip linkの種類
code:iproute2-version.sh
vagrant@ubuntu-xenial:~$ dpkg -l | grep iproute2
ii iproute2 4.3.0-1ubuntu3.16.04.5 amd64 networking and traffic control tools
vagrant@ubuntu-xenial:~$ apt-cache madison iproute2
vagrant@ubuntu-xenial:~$ ip -V
ip utility, iproute2-ss151103
この環境でman ip linkをgrepして整形したものがこちら。
code: man-ip-link-type.sh
Link types:
bridge - Ethernet Bridge device
bond - Bonding device can - Controller Area Network interface
dummy - Dummy network interface
hsr - High-availability Seamless Redundancy device
ifb - Intermediate Functional Block device
ipoib - IP over Infiniband device
macvlan - Virtual interface base on link layer address (MAC)
macvtap - Virtual interface based on link layer address (MAC) and TAP.
vcan - Virtual Controller Area Network interface
veth - Virtual ethernet interface
vlan - 802.1q tagged virtual LAN interface
vxlan - Virtual eXtended LAN
ip6tnl - Virtual tunnel interface IPv4|IPv6 over IPv6
ipip - Virtual tunnel interface IPv4 over IPv4
sit - Virtual tunnel interface IPv6 over IPv4
gre - Virtual tunnel interface GRE over IPv4
gretap - Virtual L2 tunnel interface GRE over IPv4
ip6gre - Virtual tunnel interface GRE over IPv6
ip6gretap - Virtual L2 tunnel interface GRE over IPv6
vti - Virtual tunnel interface
nlmon - Netlink monitoring device
ipvlan - Interface for L3 (IPv6/IPv4) based VLANs
lowpan - Interface for 6LoWPAN (IPv6) over IEEE 802.15.4 / Bluetooth
geneve - GEneric NEtwork Virtualization Encapsulation
DHCPのコマンド
dnsmasqを使ってDHCPサーバー(udp:67)を立てて実験した。
code:dhcp-sc.sh
# サーバーサイド
sudo ip netns exec server \
dnsmasq \
--dhcp-range=192.0.2.100,192.0.2.200,255.255.255.0 \
--interface=s-veth0 \
--no-daemon
# クライアントサイド
sudo ip netns exec client dhclient c-veth0
NAPTの用語
書き換えたIP, portの書き換え前後の対応関係をセッションと呼ぶ。NATではiptablesを使ってターゲットを追加する。
code: NAT.sh
# SNAT(MASQUERADE)
sudo iptables -t nat \
-A POSTROUTING \
-s 192.0.2.0/24 \
-o gw-veth1 \
-j MASQUERADE
# DNAT
sudo iptables -t nat \
-A PREROUTING \
-p tcp
--dport 54321 \
-d 203.0.113.254 \
-j DNAT \
--to-destination 192.0.2.1
そのほか
tcpdump の-eオプションでMACアドレスを表示する
ビッグエンディアンをネットワークバイトオーダーと呼ぶ