blob: 05bb053516da56f815c15a9274d158de697786b7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#!/sbin/runscript
# Copyright 2014 Nicholas Vinson
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
extra_commands="clear list panic save"
extra_started_commands="reload"
depend() {
need localmount #434774
before net
}
start_pre() {
checkkernel || return 1
checkconfig || return 1
return 0
}
clear() {
if use_legacy; then
clear_legacy
return 0
fi
nft flush ruleset
}
list() {
if use_legacy; then
list_legacy
return 0
fi
nft list ruleset
}
panic() {
checkkernel || return 1
if service_started ${RC_SVCNAME}; then
rc-service ${RC_SVCNAME} stop
fi
ebegin "Dropping all packets"
clear
if nft create table ip filter >/dev/null 2>&1; then
#nft -f /var/lib/nftables/rules-panic.ip
nft -f /dev/stdin <<-EOF
table ip filter {
chain input {
type filter hook input priority 0;
drop
}
chain forward {
type filter hook forward priority 0;
drop
}
chain output {
type filter hook output priority 0;
drop
}
}
EOF
fi
if nft create table ip6 filter >/dev/null 2>&1; then
#nft -f /var/lib/nftables/rules-panic.ip6
nft -f /dev/stdin <<-EOF
table ip6 filter {
chain input {
type filter hook input priority 0;
drop
}
chain forward {
type filter hook forward priority 0;
drop
}
chain output {
type filter hook output priority 0;
drop
}
}
EOF
fi
}
reload() {
checkkernel || return 1
ebegin "Flushing firewall"
clear
start
}
save() {
ebegin "Saving nftables state"
checkpath -q -d "$(dirname "${NFTABLES_SAVE}")"
checkpath -q -m 0600 -f "${NFTABLES_SAVE}"
local tmp_save="${NFTABLES_SAVE}.tmp"
if use_legacy; then
save_legacy ${tmp_save}
else
nft list ruleset > ${tmp_save}
fi
mv ${tmp_save} ${NFTABLES_SAVE}
}
start() {
ebegin "Loading nftables state and starting firewall"
clear
nft -f ${NFTABLES_SAVE}
eend $?
}
stop() {
if yesno ${SAVE_ON_STOP:-yes}; then
save || return 1
fi
ebegin "Stopping firewall"
clear
eend $?
}
################################################################################
#
# SUPPORT FUNCTIONS
#
################################################################################
checkconfig() {
if [ ! -f ${NFTABLES_SAVE} ]; then
eerror "Not starting nftables. First create some rules then run:"
eerror "rc-service nftables save"
return 1
fi
return 0
}
checkkernel() {
if ! nft list tables >/dev/null 2>&1; then
eerror "Your kernel lacks nftables support, please load"
eerror "appropriate modules and try again."
return 1
fi
return 0
}
use_legacy() {
local major_ver minor_ver
major_ver=`uname -r | cut -d '.' -f1`
minor_ver=`uname -r | cut -d '.' -f2`
[[ $major_ver -ge 4 || $major_ver -eq 3 && $minor_ver -ge 18 ]] && return 1
return 0
}
################################################################################
#
# LEGACY COMMAND FUNCTIONS
#
################################################################################
clear_legacy() {
local l3f line table chain first_line
first_line=1
if manualwalk; then
for l3f in $(getfamilies); do
nft list tables ${l3f} | while read line; do
table=$(echo ${line} | sed "s/table[ \t]*//")
deletetable ${l3f} ${table}
done
done
else
nft list tables | while read line; do
l3f=$(echo ${line} | cut -d ' ' -f2)
table=$(echo ${line} | cut -d ' ' -f3)
deletetable ${l3f} ${table}
done
fi
}
list_legacy() {
local l3f
if manualwalk; then
for l3f in $(getfamilies); do
nft list tables ${l3f} | while read line; do
line=$(echo ${line} | sed "s/table/table ${l3f}/")
echo "$(nft list ${line})"
done
done
else
nft list tables | while read line; do
echo "$(nft list ${line})"
done
fi
}
save_legacy() {
tmp_save=$1
touch "${tmp_save}"
if manualwalk; then
for l3f in $(getfamilies); do
nft list tables ${l3f} | while read line; do
line=$(echo ${line} | sed "s/table/table ${l3f}/")
nft ${SAVE_OPTIONS} list ${line} >> ${tmp_save}
done
done
else
nft list tables | while read line; do
nft ${SAVE_OPTIONS} list ${line} >> "${tmp_save}"
done
fi
}
################################################################################
#
# LEGACY SUPPORT FUNCTIONS
#
################################################################################
CHECK_TABLE_NAME="GENTOO_CHECK_TABLE"
getfamilies() {
local l3f families
for l3f in ip arp ip6 bridge inet; do
if nft create table ${l3f} ${CHECK_TABLE_NAME} > /dev/null 2>&1; then
families="${families}${l3f} "
nft delete table ${l3f} ${CHECK_TABLE_NAME}
fi
done
echo ${families}
}
manualwalk() {
local result l3f=`getfamilies | cut -d ' ' -f1`
nft create table ${l3f} ${CHECK_TABLE_NAME}
nft list tables | read line
if [ $(echo $line | wc -w) -lt 3 ]; then
result=0
fi
result=1
nft delete table ${l3f} ${CHECK_TABLE_NAME}
return $result
}
deletetable() {
# family is $1
# table name is $2
nft flush table $1 $2
nft list table $1 $2 | while read l; do
chain=$(echo $l | grep -o 'chain [^[:space:]]\+' | cut -d ' ' -f2)
if [ -n "${chain}" ]; then
nft flush chain $1 $2 ${chain}
nft delete chain $1 $2 ${chain}
fi
done
nft delete table $1 $2
}
|