WAN Ethernet : DHCP/Static/PPPoE

以太网接口可以分为IP模式和桥接模式,在IP模式下就得先有一个IP地址,以太网接口获取IP地址的方法,我知道的有三种:StaticDHCPPPPoE。如下例子中使用的eth4WAN Ethernet的接口名字

Static

  • 通过ifconfig命令配置接口的IP地址192.168.88.15/24、网关地址192.168.88.1DNS地址1.1.1.2,1.1.1.7MAC地址F6:D9:53:7B:C4:1DMTU1490

ifconfig eth4 0.0.0.0

ifconfig eth4 down

ifconfig eth4 mtu 1490

ifconfig eth4 hw ether F6:D9:53:7B:C4:1D

ifconfig eth4 192.168.88.15 netmask 255.255.255.0

ifconfig eth4 up

  • 写入DNS值到文件/etc/resolv.conf

# cat /etc/resolv.conf

nameserver 1.1.1.2

nameserver 1.1.1.7

  • 添加route网络

route del -net 192.168.88.0 netmask 255.255.255.0 eth4

route add -net 192.168.88.0 netmask 255.255.255.0 eth4

  • 如果以eth4作为默认路由,则为它建立默认路由规则

route del default gw 192.168.88.1 dev eth4

route add default gw 192.168.88.1 dev eth4

  • 启动dns relay相关进程dnsmasq,配置中br0LAN Ethernet,而listen-address是指br0IP地址。

# dnsmasq -C /var/dnsmasq.conf

# cat /var/dnsmasq.conf

no-resolv

no-dhcp-interface=br0

listen-address=192.168.1.1

server=1.1.1.2

server=1.1.1.7

DHCP

以太网接口可以通过busybox自带的udhcpc进程获取动态IP地址,其框架是:可执行文件udhcpc、脚本文件/etc/default.scriptDNS配置文件/etc/resolv.conf

  • udhcpc的部分参数说明
-i,--interface IFACE    Interface to use (default eth0)
-p,--pidfile FILE       Create pidfile
-s,--script PROG      Run PROG at DHCP events (default /etc/default.script)
  • 脚本/etc/default.script的内容大致如下
 1 #!/bin/sh
 2 # udhcpc script edited by Tim Riker <Tim@Rikers.org>
 3 [ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
 4 RESOLV_CONF="/etc/resolv.conf"
 5 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
 6 [ -n "$subnet" ] && NETMASK="netmask $subnet"
 7 case "$1" in
 8     deconfig)
 9         if [ $? -eq 0 ]; then
10             /sbin/ifconfig $interface up
11         fi
12         if [ $? -eq 0 ]; then
13             /sbin/ifconfig $interface 0.0.0.0
14         fi;;
15     renew|bound)
16         /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
17         if [ -n "$router" ] ; then
18             echo "deleting routers"
19             while route del default gw 0.0.0.0 dev $interface ; do
20                 :
21             done
22             for i in $router ; do
23                 route add default gw $i dev $interface
24             done
25         fi
26         echo -n > $RESOLV_CONF
27         [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
28         for i in $dns ; do
29             echo adding dns $i
30             echo nameserver $i >> $RESOLV_CONF
31         done
32         ;;
33 esac
34 exit 0
View Code
  •  启动udhcpc进程
busybox udhcpc  -i eth4 -p /var/run/udhcpc.pid -s /etc/default.script
  • 完成上述步骤后,可以看到接口eth4获取到了动态的IP地址,掩码并且把网关配置进了默认路由,把获得到的DNS值写入了dnsmasq会用到的配置文件/etc/resolve.conf
  • 从上面的步骤可以大概之后,udhcpc进程启动后会依次进入脚本default.scriptdefconfigrenew流程,脚本default.script会通过读取udhcpc设置的环境变量,得到动态的IP地址、网关、DNS等信息,并由ifconfig指令在适当的时候建立相应的接口,用route指令在适当的时候建立相关的路由,并把获取到的DNS值写入配置文件/etc/resolve.conf
  • 用同Static一样的方式去启动dns relay相关进程dnsmasq

PPPoE

PPPoE是同DHCP一样通过获取动态IP地址的一种协议,个人人为最大的不同之处在于PPPoE需要认证,即客户端需要向Server提供正确的用户名和密码才可以获得Server分配的IP地址、网关、掩码、DNS值等信息,而DHCP是不需要认证的,任何以太网接口都可以向处于同一局域网的Server请求分配动态IP地址。

  • 相关脚本和配置文件
# ls -l /etc/ppp/
-rwxr-xr-x    1 root     root          1094 Jan  1 00:01 ip-down
-rwxr-xr-x    1 root     root          1611 Jan  1 00:01 ip-up
-rw-r--r--    1 root     root           400 Jan  1 00:01 options.1
-rwxr-xr-x    1 root     root          1623 Jan  1 00:01 ppp-status
-rw-r--r--    1 root     root            20 Jan  1 00:36 resolv.conf.1
  • pppd启动用到的配置文件内容
# cat /etc/ppp/options.1 
noauth nodetach
lcp-echo-failure 1
lcp-echo-interval 30
lcp-echo-failure-2 6
lcp-echo-interval-2 6
lcp-timeout-1 10
lcp-timeout-2 10
ipcp-accept-remote ipcp-accept-local
mtu 1480
linkname 1
ipparam 1
xmlpath InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.
refuse-eap
user test
password test
persist
holdoff 30
usepeerdns
noipdefault
kpppoe pppoe_device eth4
req_name ppp1
  • 启动pppd进程
pppd file /etc/ppp/options.1
  • udhcpc类似地,在pppd启动后的各个协议阶段中会分别调用脚本,另外和udhcpc建立接口的方式不同的是,ppp1接口的建立和/etc/ppp/resolv.conf.1文件内容更新是在pppd进程内部完成的,并不是用外面的shell脚本来实现。
启动pppd后,pppd在内部会分阶段调用如下脚本:
/etc/ppp/ppp-status "1" "ERROR_NONE" "0" "0" “$xmlpath”
/etc/ppp/ppp-status "1" "mainloop" "0" "0" “$xmlpath”
/etc/ppp/ppp-status "1" "connecting" "0" "0" “$xmlpath”
/etc/ppp/ppp-status "1" "authSucceeded:MS-CHAPV2" "8960" "1480" “$xmlpath”
/etc/ppp/ppp-status "1" "connected" "8960" "1480" “$xmlpath”
/etc/ppp/ip-up 'ppp1' 'eth4' '0' '10.0.0.15' '10.0.0.16' '1' “$xmlpath”
退出pppd后,pppd在内部会分阶段调用如下脚本:
/etc/ppp/ip-down 'ppp1' 'eth4' '0' '10.0.0.16' '10.0.0.15' '1' “$xmlpath”
/etc/ppp/ppp-status "1" "disconnecting" "10240" "1492" “$xmlpath”
/etc/ppp/ppp-status "1" "disconnected" "10240" "1492" “$xmlpath”
/etc/ppp/ppp-status "1" "cleanup" "10240" "1492" “$xmlpath”

走到流程/etc/ppp/ip-up的时候,该shell脚本一般应该建立路由规则,更新/etc/resolve.conf启动DNS Relay相关进程dnsmasq,建立跟ppp1接口相关的iptables规则。

走到流程/etc/ppp/ip-down的时候,该shell脚本一般是删除相应路由规则,停止DNS Relay相关进程等,清除跟ppp1接口相关的iptables规则。

 

posted on 2020-09-02 10:36  LiveWithACat  阅读(1852)  评论(0编辑  收藏  举报