当运行着Brillo系统的RPi 2B连上路由器时,可以自动从路由器端获取IP地址,配置有线网络,接入到互连网。默认情况下,Brillo系统设置的hostname为localhost, 所以当你打开路由器的配置页面,查看接入设备的相关信息时,Brillo设备Hostname那一栏为空。由于client设置的hostname为localhost, DHCP client在请求IP地址时,不会将hostname发送给DHCP server, DHCP server就无法知道client的hostname。
- DHCP自动分配IP地址过程分析
我们可以在client端通过tcpdump查看使用dhcp server自动分配IP地址的过程:
# tcpdump -i eth0 -s 1500 -vv port 67 or port 68 [ 153.095070] device eth0 entered promiscuous mode tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 1500 bytes [ 170.865935] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1 [ 170.873395] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready 05:34:59.369523 IP (tos 0x0, ttl 64, id 9241, offset 0, flags [none], proto UDP (17), length 351) 0.0.0.0.bootpc > 255.255.255.255.bootps: [udp sum ok] BOOTP/DHCP, Request from b8:27:eb:b4:d8:bb (oui Unknown), length 323, xid 0xe3b7dd91, Flags [none] (0x0000) Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown) Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Discover Client-ID Option 61, length 7: ether b8:27:eb:b4:d8:bb MSZ Option 57, length 2: 1500 Vendor-Class Option 60, length 44: "dhcpcd-6.8.2:Linux-4.1.19-v7+:armv7l:BCM2709" T145 Option 145, length 1: 1 Parameter-Request Option 55, length 15: Subnet-Mask, Classless-Static-Route, Static-Route, Default-Gateway Domain-Name-Server, Hostname, Domain-Name, MTU BR, Lease-Time, Server-ID, RN RB, Option 119, Option 252 05:35:01.874523 IP (tos 0x0, ttl 64, id 29112, offset 0, flags [none], proto UDP (17), length 328) 192.168.5.1.bootps > 192.168.5.127.bootpc: [udp sum ok] BOOTP/DHCP, Reply, length 300, xid 0xe3b7dd91, Flags [none] (0x0000) Your-IP 192.168.5.127 Server-IP 192.168.5.1 Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown) Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Offer Server-ID Option 54, length 4: 192.168.5.1 Lease-Time Option 51, length 4: 43200 RN Option 58, length 4: 21600 RB Option 59, length 4: 37800 Subnet-Mask Option 1, length 4: 255.255.255.0 BR Option 28, length 4: 192.168.5.255 Default-Gateway Option 3, length 4: 192.168.5.1 Domain-Name-Server Option 6, length 4: 192.168.5.1 Domain-Name Option 15, length 3: "lan" 05:35:01.877733 IP (tos 0x0, ttl 64, id 46889, offset 0, flags [none], proto UDP (17), length 363) 0.0.0.0.bootpc > 255.255.255.255.bootps: [udp sum ok] BOOTP/DHCP, Request from b8:27:eb:b4:d8:bb (oui Unknown), length 335, xid 0xe3b7dd91, secs 3, Flags [none] (0x0000) Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown) Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Request Client-ID Option 61, length 7: ether b8:27:eb:b4:d8:bb Requested-IP Option 50, length 4: 192.168.5.127 Server-ID Option 54, length 4: 192.168.5.1 MSZ Option 57, length 2: 1500 Vendor-Class Option 60, length 44: "dhcpcd-6.8.2:Linux-4.1.19-v7+:armv7l:BCM2709" T145 Option 145, length 1: 1 Parameter-Request Option 55, length 15: Subnet-Mask, Classless-Static-Route, Static-Route, Default-Gateway Domain-Name-Server, Hostname, Domain-Name, MTU BR, Lease-Time, Server-ID, RN RB, Option 119, Option 252 05:35:01.879320 IP (tos 0x0, ttl 64, id 29113, offset 0, flags [none], proto UDP (17), length 328) 192.168.5.1.bootps > 192.168.5.127.bootpc: [udp sum ok] BOOTP/DHCP, Reply, length 300, xid 0xe3b7dd91, secs 3, Flags [none] (0x0000) Your-IP 192.168.5.127 Server-IP 192.168.5.1 Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown) Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: ACK Server-ID Option 54, length 4: 192.168.5.1 Lease-Time Option 51, length 4: 43200 RN Option 58, length 4: 21600 RB Option 59, length 4: 37800 Subnet-Mask Option 1, length 4: 255.255.255.0 BR Option 28, length 4: 192.168.5.255 Default-Gateway Option 3, length 4: 192.168.5.1 Domain-Name-Server Option 6, length 4: 192.168.5.1 Domain-Name Option 15, length 3: "lan"
可以看到分为四个阶段:
Discover 1. client ------------------> server Offer 2. client <------------------ server Request 3. client ------------------> server ACK 4. client <------------------ server
在ACK阶段,client就可以从server端获取了IP地址,子网掩码,网关IP,DNS server等信息。
如果在自动获取IP地址之前,我们重新设置了hostname又会怎么样呢?
我们可以通过hostname命令查看与修改hostname:
# hostname localhost # hostname RPi2B
NOTE:关于hostname的命名规范请查看:https://en.wikipedia.org/wiki/Hostname
这时在自动获取IP地址时,DHCP client就会发送hostname信息给DHCP server:
0.0.0.0.bootpc > 255.255.255.255.bootps: [udp sum ok] BOOTP/DHCP, Request from b8:27:eb:b4:d8:bb (oui Unknown), length 330, xid 0x6f097a2b, Flags [none] (0x0000)
Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: Discover
Client-ID Option 61, length 7: ether b8:27:eb:b4:d8:bb
MSZ Option 57, length 2: 1500
Vendor-Class Option 60, length 44: "dhcpcd-6.8.2:Linux-4.1.19-v7+:armv7l:BCM2709"
Hostname Option 12, length 5: "RPi2B"
T145 Option 145, length 1: 1
Parameter-Request Option 55, length 15:
Subnet-Mask, Classless-Static-Route, Static-Route, Default-Gateway
Domain-Name-Server, Hostname, Domain-Name, MTU
BR, Lease-Time, Server-ID, RN
RB, Option 119, Option 252
- 由DHCP server向client提供hostname
a. 路由器中有相关的配置可以指定MAC地址所对应的Hostname, IP address:
这时,DHCP server就会在ACK阶段提供hostname:
192.168.5.1.bootps > 192.168.5.127.bootpc: [udp sum ok] BOOTP/DHCP, Reply, length 304, xid 0xe10f3a39, Flags [none] (0x0000)
Your-IP 192.168.5.127
Server-IP 192.168.5.1
Client-Ethernet-Address b8:27:eb:b4:d8:bb (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message Option 53, length 1: ACK
Server-ID Option 54, length 4: 192.168.5.1
Lease-Time Option 51, length 4: 43200
RN Option 58, length 4: 21600
RB Option 59, length 4: 37800
Subnet-Mask Option 1, length 4: 255.255.255.0
BR Option 28, length 4: 192.168.5.255
Default-Gateway Option 3, length 4: 192.168.5.1
Domain-Name-Server Option 6, length 4: 192.168.5.1
Domain-Name Option 15, length 3: "lan"
Hostname Option 12, length 5: "RPi2B"
b. 修改Brillo系统中shill服务的启动参数(修改system/etc/init/shill.rc)
# For dhcpcd-6.8.2.
on post-fs-data
mkdir /data/misc/dhcp-6.8.2 0755 dhcp dhcp
service shill /system/bin/shill --device-black-list=sit0,dummy0,p2p0 --accept-hostname-from=eth0
class late_start
user root
group system dbus inet
disabled
on property:wifi-setup.complete=1
enable shill
DHCP client接收到hostname的请求之后,会通过shill重新设置hostname。
- 相关的参考文档:
- https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
- https://en.wikipedia.org/wiki/Hostname