pppoe应用概述

PPPOE简述

PPP主要是用来通过拨号或专线方式在两个网络节点之间建立连接、发送数据。PPP是一种分层协议,物理层用来进行实际的点到点连接。由链路控制层(LCP)发起对链路的建立、配置和测试。在LCP初始化完成后,通过一种或多种网络控制协议来传送特定协议族的通信。PPP协议具有处理错误检测、支持多个协议、允许在连接时刻协商IP地址、允许身份认证等特点。PPP协议工作于二层网络即数据链路层,在内核中实现。

PPPoe的英文全称为Point to Point Protocol over Ethernet,它在以太网上承载了点对点协议。可以说PPPoe是将以太网和PPP协议相结合的协议,是在以太网中转播PPP帧信息的技术,它利用以太网将大量主机组成网络,通过一个远端接入设备连入因特网,并对接入的每一台主机实现控制、计费功能,极高的性价比使PPPoE广泛应用在包括小区组网建设等一系列应用中,目前流行的宽带接入方式ADSL就使用了PPPoE协议。

PPPoE将PPP协议封装在以太网帧上进行传输,它的通信过程分为探测(Discovery)和PPP会话(Session)2个阶段。PPPoE Discovery阶段主要是客户机确定AC(Access Concentrator)以及客户机与AC协商Session ID。而PPPoE Session用于完成数据包的接收与发送,同时它也用于完成PPP链路的协商(LCP),以及网络层的控制协商(IPCP)等。

PPPOE服务端搭建

为便于测试pppoe功能,在树莓派上部署PPPoE服务器,以连接网关测试网关PPPoE上网功能,主要参考文档:基于linux环境的PPPOE服务器搭建

树莓派3B+有一个有线网口和一个无线网口,其中无线网口为wan口,连接外网,实现上网功能;有线网口连接网关eth0,网关通过PPPoE借助无线网口实现上网功能,具体部署如下。

1. 确认网口的ip及路由配置。

~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.111  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::1006:e96f:63eb:46e2  prefixlen 64  scopeid 0x20<link>
        inet6 fd51:42f8:caae:d92e::ff  prefixlen 64  scopeid 0x0<global>
        ether b8:27:eb:ef:90:a2  txqueuelen 1000  (Ethernet)
        RX packets 1096  bytes 157745 (154.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 857  bytes 49580 (48.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 390 (390.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 390 (390.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.16.157  netmask 255.255.255.0  broadcast 172.17.16.255
        inet6 fe80::d791:dfeb:23e2:20e8  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:ba:c5:f7  txqueuelen 1000  (Ethernet)
        RX packets 13496  bytes 1509246 (1.4 MiB)
        RX errors 0  dropped 360  overruns 0  frame 0
        TX packets 461  bytes 51453 (50.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    202    0        0 eth0
default         172.17.16.254   0.0.0.0         UG    303    0        0 wlan0
172.17.16.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0
192.168.10.0    0.0.0.0         255.255.255.0   U     202    0        0 eth0

2. 安装pppoe软件

sudo apt install pppoe

安装完成后存在pppoe-server命令。

3. 配置/etc/ppp/options和/etc/ppp/pppoe-server-options,测试可行样例如下。

~$ cat /etc/ppp/options  | grep -v '#'
ms-dns 219.141.136.10
ms-dns 114.114.114.114
ms-dns 8.8.8.8
asyncmap 0
auth
crtscts
local
lock
hide-password
modem
-pap

+chap
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
noipx
~$ cat /etc/ppp/pppoe-server-options 
#PPP options for the PPPoE server

require-chap
lcp-echo-interval 60
lcp-echo-failure 5
logfile /var/log/pppd.log

4.   配置CHAP认证文件/etc/ppp/chap-secrets,暂设置用户名为test,上网口令(密码)为123456。

sudo cat /etc/ppp/chap-secrets 
# Secrets for authentication using CHAP
# client    server    secret            IP addresses
test        *    123456        *

5. 设置树莓派网关实现eth0数据转发到无线wlan0,实现上网功能

1) PPPoE 协议接入控制的主要方式就是通过对IP数据包的封装再转发。所以要配置 PPPoE 服务器必须要开启IP转发功能。开启IP转发功能需要去除/etc/sysctl.conf 文件中 net.ipv4.ip_forward=1 该行的注释,随后运行 sudo sysctl -p 即可生效配置打开 IP 转发功能。

2) 树莓派的默认网关是eth0有线网口,其次是无线网口wlan0,应去除eth0的默认路由。

sudo route del default dev eth0

3) 设置数据转发

sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o wlan0 -j MASQUERADE

6. 开启PPPoE服务。

sudo  pppoe-server  -I eth0 -L 192.168.10.100 -R 192.168.10.5 -N 20

-I 参数为指定pppoe服务器所监听连接请求的网卡名称,可使用 ifconfig 命令查看。

-L 参数PPPoE服务器的IP地址,此IP地址为PPPOE服务器的虚拟IP地址,与网卡enp2s0 地址无关

-R 参数为当有客户连接到服务器上时,pppoe服务器分配给客户端的起始IP地址

-N 参数为指定最多可以连接pppoe服务器的客户端数量

此时PPPoE服务器搭建完成,可通过eth0连接PC,直接在windows下pppoe拨号上网测试服务器搭建是否成功。windows下pppoe拨号只需输入用户名和密码即可。

7. 关闭PPPoE服务

sudo killall pppoe-server

PPPOE客户端应用

1.   pppoe工作于链路层,需要linux内核支持,主要选择“PPP over Ethernet”, 保险起见可都选择。

Device Drivers ->
    Network device support ->
        <M> ppp(point-to-point protocol) support 
        <M> PPP BSD-Compress compression
        <M> PPP Deflate compression
        [*] PPP filtering
        <M> PPP MPPE compression (encryption)
        [*] PPP multilink support
        <M> PPP over Ethernet
        <M> PPP support async serial ports
        <M> PPP support for sync tty ports

2.   安装pppoe软件,Ubuntu下命令如下:

apt install pppoe

安装完整后,在/usr/sbin下有pppoe拨号相关的脚本或可执行程序:

pppoe-setup:配置程序,把输入的账号密码等信息存入配置文件

pppoe-start:拨号程序,连接网络

pppoe-stop:断开

pppoe-status:查看连接状态

pppoe-connect:连接网络,被pppoe-start所调用

pppoe-init:初始化,被其他程序调用

3.   运行pppoe-setup完成初始化配置

>> Enter your PPPoE user name: ——输入拨号帐户的用户名 
>> Enter the Ethernet interface connected to the ADSL modem For Solaris, this is likely to be something like /dev/hme0. For Linux, it will be ethn, where n is a number. (default eth0): ——输eth0 
>> Enter the demand value (default no): ——输no 
>> Enter the DNS information here: ——输SERVER(由PPPoE服务器提供)
>> Please enter your PPPoE password: ——输用户口令
>> Choose a type of firewall (0-2): ——输0 (0为不要防火墙)
>> Accept these settings and adjust configuration files (y/n)? ——输y

相关配置内容会保存到/etc/ppp/pppoe.conf和/etc/chap-secrets、/etc/pap-secrets中,主要的配置内容为用户名和用户口令,这部分也是网关程序需要处理的部分。

4.   运行pppoe-start开始拨号,拨号成功会输出“Connected!”,且可通过pppoe-status确认ppp0网卡信息(且成功获取IP)。

#:/etc/ppp# pppoe-status
pppoe-status: Link is up and running on interface ppp0
ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1492
        inet 192.168.10.13  netmask 255.255.255.255  destination 192.168.10.100
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 8  bytes 100 (100.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 91 (91.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

5.   运行pppoe-stop停止拨号上网

#:/etc/ppp# pppoe-stop 
Killing pppd (4955)
Killing pppoe-connect (4935)

PPPOE网关部署

pppoe在智能网关中可通过systemd管理来开启或关闭pppoe拨号上网。systemd服务配置文件内容:

[Unit]
Description=PPP over ethernet
After=network-online.target systemd-networkd.service
Wants=network-online.target systemd-networkd.service

[Service]
Type=forking
ExecStart=/usr/sbin/pppoe-start
ExecReload=/usr/sbin/pppoe-stop;/usr/sbin/pppoe-start
ExecStop=-/usr/sbin/pppoe-stop
ExecStopPost=-ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Restart=on-failure

[Install]
WantedBy=multi-user.target

可通过systemctl开启、关闭或查询状态:

#:/etc/ppp# systemctl stop pppoe
#:/etc/ppp# systemctl start pppoe
#:/etc/ppp# systemctl status pppoe
● pppoe.service - PPP over ethernet
   Loaded: loaded (/lib/systemd/system/pppoe.service; disabled; vendor preset: e
nabled)
   Active: active (running) since Sat 2019-01-26 09:24:19 UTC; 6s ago
  Process: 5088 ExecStop=/usr/sbin/pppoe-stop (code=exited, status=0/SUCCESS)
  Process: 5116 ExecStart=/usr/sbin/pppoe-start (code=exited, status=0/SUCCESS)
 Main PID: 5120 (pppoe-connect)
   CGroup: /system.slice/pppoe.service
           ├─5120 /bin/sh /usr/sbin/pppoe-connect
           ├─5140 /usr/sbin/pppd pty /usr/sbin/pppoe -p /var/run/pppoe.conf-pppoe.
pid.pppoe -I eth0 -T 80 -U  -m 1412    noipdefault noauth default-asyncmap defau
ltroute hide-password nodetach usepeerdns mtu 1492 mru 1492 noaccomp nodeflate n
opcomp novj novjccomp user test lcp-echo-interval 20 lcp-echo-failure 3
           ├─5141 sh -c /usr/sbin/pppoe -p /var/run/pppoe.conf-pppoe.pid.pppoe -I 
eth0 -T 80 -U  -m 1412   
           └─5146 /usr/sbin/pppoe -p /var/run/pppoe.conf-pppoe.pid.pppoe -I eth0 -
T 80 -U -m 1412

可通过pppoe-status确认网络连通性(成功获取到IP):

#:/etc/ppp# pppoe-status 
pppoe-status: Link is up and running on interface ppp0
ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1492
        inet 192.168.10.6  netmask 255.255.255.255  destination 192.168.10.100
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 8  bytes 100 (100.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 91 (91.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

参考:

1. 基于linux环境的PPPOE服务器搭建

2. 怎样设计并实现一个基于linux的PPPoE内核模式拨号

3. 嵌入式linux实现pppoe拨号上网

4. Arch linux下的ADSL拨号上网

5. chap和pap认证

6. PPP wiki Archlinux

7. linux下ppp的移植及ppp0路由设置

猜你喜欢

转载自www.cnblogs.com/embedded-linux/p/10367140.html