Skip to content

Commit e2674cc

Browse files
committed
fix(keenetic): use RAW table
1 parent 8fa8d83 commit e2674cc

12 files changed

Lines changed: 3249 additions & 2788 deletions

File tree

docs/content/docs/cli.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Options:
1717
--config <path> Path to JSON config file
1818
--log-level <lvl> Log level: error, warn, info, verbose, debug
1919
--no-api Disable REST API at runtime
20+
--use-raw-prerouting Use raw PREROUTING for IPv4 forwarded traffic (iptables only)
2021
--version Show version and exit
2122
--help Show this help and exit
2223
@@ -38,9 +39,31 @@ The config file is usually `/etc/keen-pbr/config.json` on OpenWrt and Debian, an
3839
| `--config <path>` | Path to the JSON config file. |
3940
| `--log-level <lvl>` | Log verbosity: `error`, `warn`, `info`, `verbose`, or `debug`. |
4041
| `--no-api` | Disable the REST API even if enabled in config. |
42+
| `--use-raw-prerouting` | Opt in to raw-table IPv4 forwarded-traffic classification; available only with iptables. |
4143
| `--version` | Print version and exit. |
4244
| `--help` | Print help and exit. |
4345

46+
### `--use-raw-prerouting`
47+
48+
This disabled-by-default option is intended for systems such as KeeneticOS where
49+
an external firewall manager periodically replaces the `mangle` table. It moves
50+
only IPv4 forwarded-traffic classification from `mangle PREROUTING` to `raw
51+
PREROUTING`; locally generated traffic remains in `mangle OUTPUT`, and IPv6
52+
continues using its existing mangle path.
53+
54+
The Keenetic service loads the matching `iptable_raw.ko` only when the flag is
55+
present in `KEEN_PBR_ARGS` in `/opt/etc/keen-pbr/defaults`. Startup fails instead
56+
of silently falling back if raw is unusable. Raw PREROUTING deliberately does not
57+
use connmark acceleration: each forwarded packet is classified directly.
58+
59+
Troubleshoot the required capability with:
60+
61+
```sh
62+
ls -l "/lib/modules/$(uname -r)/iptable_raw.ko"
63+
grep -x raw /proc/net/ip_tables_names
64+
iptables -t raw -S
65+
```
66+
4467
## Commands
4568

4669
| Command | Description |

packages/keenetic/keen-pbr/files/opt/etc/init.d/S80keen-pbr

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ENABLED=yes
66
PROCS=$(basename "$KEEN_PBR")
77
PIDFILE="/opt/var/run/keen-pbr.pid"
8-
ARGS="--config $CONFIG --pid-file $PIDFILE service"
8+
ARGS="--config $CONFIG --pid-file $PIDFILE ${KEEN_PBR_ARGS:-} service"
99
PREARGS=""
1010
POSTCMD=""
1111
DESC="keen-pbr"
@@ -139,6 +139,32 @@ check_runtime_dependencies() {
139139
ensure_xt_multiport_loaded
140140
}
141141

142+
has_daemon_arg() {
143+
for arg in $ARGS; do
144+
[ "$arg" = "$1" ] && return 0
145+
done
146+
return 1
147+
}
148+
149+
ensure_raw_table_available() {
150+
local raw_module
151+
152+
grep -qx raw /proc/net/ip_tables_names 2>/dev/null && return 0
153+
raw_module="/lib/modules/$(uname -r)/iptable_raw.ko"
154+
if [ ! -f "$raw_module" ]; then
155+
log_error "--use-raw-prerouting was requested, but $raw_module is unavailable"
156+
return 1
157+
fi
158+
if ! insmod "$raw_module" 2>/dev/null && ! grep -qx raw /proc/net/ip_tables_names 2>/dev/null; then
159+
log_error "--use-raw-prerouting was requested, but failed to load $raw_module"
160+
return 1
161+
fi
162+
if ! grep -qx raw /proc/net/ip_tables_names 2>/dev/null || ! iptables -t raw -S >/dev/null 2>&1; then
163+
log_error "--use-raw-prerouting was requested, but raw table probe failed after loading $raw_module"
164+
return 1
165+
fi
166+
}
167+
142168
enable_hwnat() {
143169
log "Enabling HW NAT..."
144170
sysctl -w net.ipv4.netfilter.ip_conntrack_fastnat=1 2>/dev/null || true
@@ -187,6 +213,9 @@ pidof() {
187213

188214
prepare_start() {
189215
check_runtime_dependencies
216+
if has_daemon_arg --use-raw-prerouting; then
217+
ensure_raw_table_available || return 1
218+
fi
190219
disable_hwnat
191220
}
192221

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
KEEN_PBR="/opt/usr/bin/keen-pbr"
22
CONFIG="/opt/etc/keen-pbr/config.json"
33
DNSMASQ_HELPER="/opt/usr/lib/keen-pbr/dnsmasq.sh"
4+
# Extra daemon arguments, for example: --use-raw-prerouting
5+
KEEN_PBR_ARGS=""

packages/keenetic/keen-pbr/files/opt/etc/ndm/netfilter.d/50-keen-pbr-routing.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/opt/bin/sh
22

3-
[ "$table" != "mangle" -a "$table" != "nat" ] && exit 0
3+
[ "$type" = "iptables" ] || exit 0
4+
[ "$table" = "mangle" ] || exit 0
45

56
logger -t "keen-pbr" "Refreshing routing state after netfilter change"
67
/opt/etc/init.d/S80keen-pbr reapply-firewall >/dev/null 2>&1 || exit 0

0 commit comments

Comments
 (0)