本文主要是介绍单臂路由实现三层路由数据包转发及数据包格式分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在7242+8328的板子上面,WAN口:port3,LAN口:port2,port4。其端口连接可参见我以前的文章。
eth0上面的ip地址动态获取,用作外部WAN上网的接口,与此通信的数据包不加VLAN头。
eth0.2 :用作内部LAN接口.IP地址固定,数据包经过LAN口时自动添加或者去掉VLAN头。
WAN和LAN之间是不通的。
说明:192.168.100.244为WAN口所接PC IP,192.168.1.20为LAN口所接PC IP。
---------------------------R8327 switch-配置命令如下-----------------------------------------------------
ifconfig eth0 up
ssdk_sh vlan entry create 2
ssdk_sh portVlan ingress set 0 fallback
ssdk_sh portVlan ingress set 2 fallback
ssdk_sh portVlan ingress set 4 fallback
ssdk_sh vlan entry append 2 2 0,2,4 0,2,4 default default default default
ssdk_sh portvlan defaultCVid set 2 2
ssdk_sh portvlan defaultCVid set 4 2
ssdk_sh portvlan egress set 0 untagged
ssdk_sh portvlan egress set 2 untagged
ssdk_sh portvlan egress set 4 untagged
上面的命令是对AR8327 switch进行的有关VLAN方面的配置。即port0,port2,port4由于同一个VLAN,其VLAN
ID=2。数据包出端口时不带VLAN ID。
---------------------------主CPU AR7242-配置命令如下-----------------------------------------------------
vconfig add eth0 2
ifconfig eth0.2 hw ether 00:03:7F:FF:FF:F2 up
udhcpc -i eth0 -p /var/run/udhcpc_wan.pid -s /etc/udhcpc.script
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
主要是通过vconfig添加VLAN及配置dhcpc及三层数据包经过时的NAT规则。
------------------------------------测试1: dhcp上网-------------------------------------------
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 00:03:7F:FF:FF:FF
inet addr:192.168.100.3 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::203:7fff:feff:ffff/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2382 (2.3 KiB) TX bytes:2012 (1.9 KiB)
eth0.2 Link encap:Ethernet HWaddr 00:03:7F:FF:FF:F2
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::203:7fff:feff:fff2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:734 (734.0 B) TX bytes:492 (492.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
WAN接PC1,PC1的ip地址为:192.168.100.244,在PC1上面ping 192.168.100.3 ,在7242+8328的板子eth0上面抓包如下:
00:02:25.389238 IP (tos 0x0, ttl 128, id 61845, offset 0, flags [none], proto ICMP (1), length 60)
192.168.100.244 > 192.168.100.3: ICMP echo request, id 1280, seq 256, length 40
0x0000: 0003 7fff ffff e839 3538 9e4a 0800 4500
0x0010: 003c f195 0000 8001 fee2 c0a8 64f4 c0a8
0x0020: 6403 0800 475c 0500 0100 6162 6364 6566
0x0030: 6768 696a 6b6c 6d6e 6f70 7172 7374 7576
0x0040: 7761 6263
00:02:25.389421 IP (tos 0x0, ttl 64, id 15814, offset 0, flags [none], proto ICMP (1), length 60)
192.168.100.3 > 192.168.100.244: ICMP echo reply, id 1280, seq 256, length 40
0x0000: e839 3538 9e4a 0003 7fff ffff 0800 4500
0x0010: 003c 3dc6 0000 4001 f2b2 c0a8 6403 c0a8
0x0020: 64f4 0000 4f5c 0500 0100 6162 6364 6566
0x0030: 6768 696a 6b6c 6d6e 6f70 7172 7374 7576
0x0040: 7761 6263
有上面的数据可知,与pc1(主机或者服务器)通信时没有VLAN头。
---------------------------------------------------------------------------------------------
LAN口接PC2,PC2的ip地址为:192.168.1.20,在PC2上面ping 192.168.1.2 ,在7242+8328的板子eth0上面抓包如下:
00:08:03.487572 IP (tos 0x0, ttl 128, id 253, offset 0, flags [none], proto ICMP (1), length 60)
192.168.1.20 > 192.168.1.2: ICMP echo request, id 512, seq 9728, length 40
0x0000: 0003 7fff fff2 001a 4b8d 0305 8100 0002
0x0010: 0800 4500 003c 00fd 0000 8001 b65d c0a8
0x0020: 0114 c0a8 0102 0800 255c 0200 2600 6162
0x0030: 6364 6566 6768 696a 6b6c 6d6e 6f70 7172
0x0040: 7374 7576
00:08:03.487725 IP (tos 0x0, ttl 64, id 50979, offset 0, flags [none], proto ICMP (1), length 60)
192.168.1.2 > 192.168.1.20: ICMP echo reply, id 512, seq 9728, length 40
0x0000: 001a 4b8d 0305 0003 7fff fff2 8100 0002
0x0010: 0800 4500 003c c723 0000 4001 3037 c0a8
0x0020: 0102 c0a8 0114 0000 2d5c 0200 2600 6162
0x0030: 6364 6566 6768 696a 6b6c 6d6e 6f70 7172
0x0040: 7374 7576
有上面的数据可知,与pc1(主机或者服务器)通信时有VLAN头其VLAN ID=2。
---------------------------------------------------------------------------------------------
同时在PC2上面ping 192.168.100.244,在eth0抓包如下:
00:15:34.482979 IP (tos 0x0, ttl 128, id 951, offset 0, flags [none], proto ICMP (1), length 60)
192.168.1.20 > 192.168.100.244: ICMP echo request, id 512, seq 42497, length 40
0x0000: 0003 7fff fff2 001a 4b8d 0305 8100 0002
0x0010: 0800 4500 003c 03b7 0000 8001 4fb1 c0a8
0x0020: 0114 c0a8 64f4 0800 a55a 0200 a601 6162
0x0030: 6364 6566 6768 696a 6b6c 6d6e 6f70 7172
0x0040: 7374 7576
00:15:34.483077 IP (tos 0x0, ttl 127, id 951, offset 0, flags [none], proto ICMP (1), length 60)
192.168.100.3 > 192.168.100.244: ICMP echo request, id 512, seq 42497, length 40
0x0000: e839 3538 9e4a 0003 7fff ffff 0800 4500
0x0010: 003c 03b7 0000 7f01 edc1 c0a8 6403 c0a8
0x0020: 64f4 0800 a55a 0200 a601 6162 6364 6566
0x0030: 6768 696a 6b6c 6d6e 6f70 7172 7374 7576
0x0040: 7761 6263
00:15:34.483393 IP (tos 0x0, ttl 128, id 64339, offset 0, flags [none], proto ICMP (1), length 60)
192.168.100.244 > 192.168.100.3: ICMP echo reply, id 512, seq 42497, length 40
0x0000: 0003 7fff ffff e839 3538 9e4a 0800 4500
0x0010: 003c fb53 0000 8001 f524 c0a8 64f4 c0a8
0x0020: 6403 0000 ad5a 0200 a601 6162 6364 6566
0x0030: 6768 696a 6b6c 6d6e 6f70 7172 7374 7576
0x0040: 7761 6263
00:15:34.483458 IP (tos 0x0, ttl 127, id 64339, offset 0, flags [none], proto ICMP (1), length 60)
192.168.100.244 > 192.168.1.20: ICMP echo reply,id 512, seq 42497, length 40
0x0000: 001a 4b8d 0305 0003 7fff fff2 8100 0002
0x0010: 0800 4500 003c fb53 0000 7f01 5914 c0a8
0x0020: 64f4 c0a8 0114 0000 ad5a 0200 a601 6162
0x0030: 6364 6566 6768 696a 6b6c 6d6e 6f70 7172
0x0040: 7374 7576
上面的数据包可以知道,一个内网的数据包经过NAT之后,一个ping包有“俩个去和俩个回”数据包。
这篇关于单臂路由实现三层路由数据包转发及数据包格式分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!