Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/modules/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ conntrack 会把后续包当作 invalid 丢掉。
| API `net.dial_restore` | POST `{}`:按已应用的 `multiwan.dial_order` 只重拨乱序 / 晚到的线路,返回 `redialled`、`failed` 和 `reason`;scope `operate` |
| API `net.wan_mac` | POST `{"wan":"wan2","device":"wan","vlan":0}`:为同一物理口的第 2 条及后续 PPPoE WAN 返回本地管理单播 MAC;scope `operate` |

**停止、重拨、关机时结束 PPPoE 会话。** `mr-pppoe.<wan>` 停止时 pppd 先发 LCP Terminate-Request,收到 Ack(或等 3 秒)后发 PADT,
运营商才会马上释放这个会话;没收到 PADT 的旧会话会继续占着名额(AIS 这类按并发数限制的运营商:下一次拨号就落进 walled garden,
见“PPPoE 公网地址保护”)。`reboot` / `poweroff` 走 OpenRC 的 `shutdown` 运行级,`mr-pppoe.*` 在防火墙卸载、链路关闭之前最先停止
(`sysupgrade` 的 kexec 路径也先停所有 `mr-pppoe.*`)。两处保证 PADT 来得及发出:`pppoe-dial` 给 pppd 加 `lcp-max-terminate 1`
(运营商不应答 Terminate-Request 时 3 秒后照样发 PADT;默认发两次,要 6 秒),`mr-pppoe` 设 `retry="SIGTERM/10"`
(supervise-daemon 默认只等 5 秒就宣布服务已停止,OpenRC 接着关机,pppd 还没发出的 PADT 随重启丢掉)。断电、死机、硬件看门狗复位
发不出 PADT,会话要等运营商自己超时。实验室场景 `15-pppoe-reboot-padt`(两条会话,运营商应答 / 不应答 Terminate-Request 两种情况下,
ISP 一侧都看到每条会话的 LCP Terminate 和 PADT)。

## 对家里配置(examples/router.yaml)输出的改动(都是有意的)

1. `network.sh` 多了 `ip rule ... lookup main suppress_prefixlength 0 pref 5299`(v4/v6),并且 5300 的 fwmark 规则先清空再加:
Expand Down
39 changes: 39 additions & 0 deletions mr/mod_net_pppoe_stop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"os"
"regexp"
"strconv"
"testing"
)

// A stopped PPPoE service must have time to send the PADT (a reboot, `rc-service restart`, a guard redial):
// pppd sends LCP Terminate-Request and waits lcp-restart (3 s, pppd's default) for the Terminate-Ack, up to
// lcp-max-terminate times. supervise-daemon declares the service stopped after `retry` seconds (default 5);
// OpenRC then goes on while pppd is still counting, and a reboot kills the PADT. Lab: scenario 15.
func TestPPPoEStopLeavesTimeForPADT(t *testing.T) {
read := func(p string) string {
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}
const lcpRestart = 3 // seconds, pppd's default
retry := regexp.MustCompile(`(?m)^retry="SIGTERM/(\d+)"$`).FindStringSubmatch(read("../rootfs/etc/init.d/mr-pppoe"))
if retry == nil {
t.Fatal(`mr-pppoe: no retry="SIGTERM/N": supervise-daemon would stop waiting for pppd after 5 s`)
}
term := regexp.MustCompile(`(?m)^exec /usr/sbin/pppd call "\$1" lcp-max-terminate (\d+)$`).FindStringSubmatch(read("../rootfs/usr/libexec/mr/pppoe-dial"))
if term == nil {
t.Fatal("pppoe-dial: pppd is not started with lcp-max-terminate N (the default of 2 needs 6 s before the PADT)")
}
wait, _ := strconv.Atoi(retry[1])
n, _ := strconv.Atoi(term[1])
if n < 1 {
t.Fatalf("lcp-max-terminate %d: pppd would not send an LCP Terminate-Request", n)
}
if need := n*lcpRestart + 2; wait < need {
t.Fatalf("supervise-daemon waits %d s for pppd, which needs %d x %d s for LCP terminate plus the PADT (%d s)", wait, n, lcpRestart, need)
}
}
5 changes: 5 additions & 0 deletions rootfs/etc/init.d/mr-pppoe
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ supervisor=supervise-daemon
# pppoe-dial waits for the WANs before this one in multiwan.dial_order, then execs `pppd call ${WAN}`
command=/usr/libexec/mr/pppoe-dial
command_args="${WAN}"
# stopping (service restart, reboot, shutdown): pppd sends LCP Terminate-Request and, once that is answered or has
# timed out (pppoe-dial: lcp-max-terminate 1 -> 3 s), the PADT that frees the session at the ISP. supervise-daemon
# stops waiting after 5 s by default, which a slow or silent ISP could outlast; the reboot then went on without
# the PADT and the ISP kept counting the old session (AIS: the next dial lands in a walled garden)
retry="SIGTERM/10"
respawn_delay=5
# never gives up: the uplink / WiFi is the way back in (a router far from home)
respawn_max=0
Expand Down
5 changes: 4 additions & 1 deletion rootfs/usr/libexec/mr/pppoe-dial
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
# runs here, not in start_pre, so OpenRC never blocks on it (a serial boot starts every WAN at once) and a
# respawn waits the same way. mr failing (no config) never keeps pppd from starting.
"${MR_BIN:-/usr/sbin/mr}" wan dial-wait "$1" </dev/null
exec /usr/sbin/pppd call "$1"
# lcp-max-terminate 1: when pppd is stopped it sends one LCP Terminate-Request and waits lcp-restart (3 s) for the
# Terminate-Ack; with the default of two requests it needs 6 s before it gives up and sends the PADT, longer than
# a shutdown waits. One request: the PADT leaves after at most 3 s, at once when the ISP answers.
exec /usr/sbin/pppd call "$1" lcp-max-terminate 1
5 changes: 4 additions & 1 deletion tools/lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ MRLAB_FORCE=1 $L reset $ID # only after checking that nobod
Scenarios (TAP output, log in `/tmp/mrlab<ID>-<name>.log`): `$L run $ID 01-fresh-dhcp`,
`02-pppoe-pd`, `03-move-dhcp-samesubnet`, `04-powercut-confirm`, `05-isp-switch-live`,
`06-lan-renumber`, `07-require-public-ip`, `08-dhcp-slaac-relay`, `09-first-run-wizard`, `11-dns-unblock`,
`12-dns-unblock-detect`, `14-policy-fallback-walled`.
`12-dns-unblock-detect`, `14-policy-fallback-walled`, `15-pppoe-reboot-padt`.
The longer scenarios have these purposes and waits:

- `05-isp-switch-live` changes a live PPPoE line to down, back to PPPoE, then DHCP and back; it
Expand All @@ -71,6 +71,9 @@ The longer scenarios have these purposes and waits:
first stays online through wan within a minute, the second stays cut off, both go back to wan2 when the ISP
releases a slot; then the same with `require_public_ip` off and the multiwan health check as the only detector
(about 90 s).
- `15-pppoe-reboot-padt` reboots a router with two PPPoE sessions (own MACs on one port) and reads the ISP's wire log: both
sessions must end with LCP Terminate-Request + PADT, with `mr-pppoe.*` stopped before the firewall unloads; then again with
the ISP's pppd frozen (it never answers the Terminate-Request). About 2 minutes.
- `08-dhcp-slaac-relay` enables and disables IPv6 relay on a DHCP-only SLAAC upstream; probe and
proxy checks retry for up to 70 s.
- `09-first-run-wizard` checks deferred install discovery and the session/plan/apply/confirm/
Expand Down
61 changes: 61 additions & 0 deletions tools/lab/scenarios/15-pppoe-reboot-padt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
# A reboot must end the PPPoE sessions at the ISP: LCP Terminate-Request + PADT from every session. An ISP that
# never heard a session end keeps counting it (AIS: the next dial lands in the walled garden). Two sessions on
# one port (wan on the port's MAC, wan2 on macvlan with its own MAC), both public.
# A) the ISP answers the Terminate-Request: both sessions end, and OpenRC stops mr-pppoe.* before it unloads the
# firewall or touches a link.
# B) the ISP does not answer (its pppd is stopped): pppd waits for the Terminate-Ack; supervise-daemon's default
# 5 s kill used to come before pppd gave up and sent the PADT (mr-pppoe: retry + lcp-max-terminate 1).
NAME=15-pppoe-reboot-padt
# shellcheck source=lib.sh
. "$(dirname "$0")/lib.sh"
WAN_MAC=52:54:00:$(printf '%02x' "$ID"):01:01
CONSOLE=$(dirname "$LAB_SH")/state/mrlab$ID/console.log
isp_pppds() { ip netns pids "mrlab$ID-isp1" 2> /dev/null | xargs -r ps -o pid=,args= -p 2> /dev/null | awk '/pppd pty/ {print $1}'; }
# ended_cleanly <wire lines before the reboot> <mac>: after them the router sent an LCP Terminate-Request and a PADT
# for that MAC, and the PADT came before the MAC's next PADI (the session was ended, not just dropped by the reboot)
ended_cleanly() {
wire | tail -n +"$(($1 + 1))" | awk -v m="$2" '
$2 == "LCP-TERM-REQ" && $3 == "from=" m { term = 1 }
$2 == "PADT" && $3 == "from=" m && !padi { padt = 1 }
$2 == "PADI" && $3 == "from=" m { padi = 1 }
END { exit !(term && padt) }'
}
both_public() { [ -n "$(wan_addr pppoe-wan)" ] && [ -n "$(wan_addr pppoe-wan2)" ] && lab console "$ID" "ip -4 addr show pppoe-wan2 | grep -q 'inet 203.0.113.'"; }
# the last shutdown: line numbers in the serial console
stopped_before() { # <first message> <second message>
a=$(grep -n "$1" "$CONSOLE" | tail -n 1 | cut -d: -f1)
b=$(grep -n "$2" "$CONSOLE" | tail -n 1 | cut -d: -f1)
[ -n "$a" ] && [ -n "$b" ] && [ "$a" -lt "$b" ]
}

step lab reset "$ID"
step lab isp "$ID" 1 pppoe --echo
check "install.sh completes (wan: PPPoE)" lab install "$ID" --wan-proto pppoe
step lab lan "$ID" dhcp
check "API: wan2, a second PPPoE session on the same port (own MAC)" \
lab change "$ID" "[{\"op\":\"add\",\"path\":\"wan\",\"value\":$(wan2_json 0)}]"
check "both lines have a public address" retry 90 both_public

# A) a responsive ISP
base=$(wire | wc -l)
step lab reboot "$ID"
check "A: wan's session ended with LCP Terminate + PADT" ended_cleanly "$base" "$WAN_MAC"
check "A: wan2's session ended with LCP Terminate + PADT" ended_cleanly "$base" "$WAN2_MAC"
check "A: mr-pppoe.wan2 was stopped before the firewall was unloaded" stopped_before "Stopping mr-pppoe.wan2" "Unloading firewall"
check "A: both lines are back with public addresses" retry 120 both_public

# B) an ISP that does not answer the Terminate-Request
pids=$(isp_pppds)
note "ISP pppd pids frozen: $pids"
# shellcheck disable=SC2086
step kill -STOP $pids
base=$(wire | wc -l)
step lab reboot "$ID"
# shellcheck disable=SC2086
step kill -CONT $pids
info sh -c "wire() { cat '$ISP_DIR/pppoe-wire.log'; }; wire | tail -n +$((base + 1)) | head -20"
check "B: wan's session ended with PADT although the ISP never answered" ended_cleanly "$base" "$WAN_MAC"
check "B: wan2's session ended with PADT although the ISP never answered" ended_cleanly "$base" "$WAN2_MAC"
check "B: both lines are back with public addresses" retry 120 both_public
finish
Loading