1 |
commit: 4ebf4b667f24cf4384e12413e73307766cec6413 |
2 |
Author: Francisco Blas (klondike) Izquierdo Riera <klondike <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Jan 8 02:37:48 2019 +0000 |
4 |
Commit: Matthew Thode <prometheanfire <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Jan 8 02:53:52 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=4ebf4b66 |
7 |
|
8 |
net-firewall/nftables: Add atomic nftables updates for modern kernels |
9 |
|
10 |
Signed-off-by: Francisco Blas Izquierdo Riera <klondike <AT> gentoo.org> |
11 |
Package-Manager: Portage-2.3.51, Repoman-2.3.11 |
12 |
Signed-off-by: Matthew Thode <prometheanfire <AT> gentoo.org> |
13 |
|
14 |
net-firewall/nftables/files/libexec/nftables-mk.sh | 59 ++++++++++++ |
15 |
net-firewall/nftables/files/nftables-mk.confd | 26 ++++++ |
16 |
net-firewall/nftables/files/nftables-mk.init | 104 +++++++++++++++++++++ |
17 |
net-firewall/nftables/metadata.xml | 5 + |
18 |
net-firewall/nftables/nftables-0.9.0-r2.ebuild | 94 +++++++++++++++++++ |
19 |
5 files changed, 288 insertions(+) |
20 |
|
21 |
diff --git a/net-firewall/nftables/files/libexec/nftables-mk.sh b/net-firewall/nftables/files/libexec/nftables-mk.sh |
22 |
new file mode 100644 |
23 |
index 00000000000..b6ad37867b6 |
24 |
--- /dev/null |
25 |
+++ b/net-firewall/nftables/files/libexec/nftables-mk.sh |
26 |
@@ -0,0 +1,59 @@ |
27 |
+#!/bin/sh |
28 |
+ |
29 |
+main() { |
30 |
+ local NFTABLES_SAVE=${2:-'/var/lib/nftables/rules-save'} |
31 |
+ case "$1" in |
32 |
+ "check") |
33 |
+ nft -c -f "${NFTABLES_SAVE}" |
34 |
+ ;; |
35 |
+ "clear") |
36 |
+ nft flush ruleset |
37 |
+ ;; |
38 |
+ "list") |
39 |
+ nft ${SAVE_OPTIONS} list ruleset |
40 |
+ ;; |
41 |
+ "load") |
42 |
+ ( echo "flush ruleset;"; cat "${NFTABLES_SAVE}" ) | nft -f - |
43 |
+ ;; |
44 |
+ "panic") |
45 |
+ panic hard | nft -f - |
46 |
+ ;; |
47 |
+ "soft_panic") |
48 |
+ panic soft | nft -f - |
49 |
+ ;; |
50 |
+ "store") |
51 |
+ local tmp_save="${NFTABLES_SAVE}.tmp" |
52 |
+ umask 600; |
53 |
+ ( |
54 |
+ echo "#!/sbin/nft -f" |
55 |
+ echo "flush ruleset;" |
56 |
+ nft ${SAVE_OPTIONS} list ruleset |
57 |
+ ) > "$tmp_save" && mv ${tmp_save} ${NFTABLES_SAVE} |
58 |
+ ;; |
59 |
+ esac |
60 |
+} |
61 |
+ |
62 |
+panic() { |
63 |
+ local erule; |
64 |
+ [ "$1" = soft ] && erule="ct state established,related accept;" || erule=""; |
65 |
+ cat <<EOF |
66 |
+table inet filter { |
67 |
+ chain input { |
68 |
+ type filter hook input priority 0; |
69 |
+ $erule |
70 |
+ drop |
71 |
+ } |
72 |
+ chain forward { |
73 |
+ type filter hook forward priority 0; |
74 |
+ drop |
75 |
+ } |
76 |
+ chain output { |
77 |
+ type filter hook output priority 0; |
78 |
+ $erule |
79 |
+ drop |
80 |
+ } |
81 |
+} |
82 |
+EOF |
83 |
+} |
84 |
+ |
85 |
+main "$@" |
86 |
|
87 |
diff --git a/net-firewall/nftables/files/nftables-mk.confd b/net-firewall/nftables/files/nftables-mk.confd |
88 |
new file mode 100644 |
89 |
index 00000000000..5cda24030f9 |
90 |
--- /dev/null |
91 |
+++ b/net-firewall/nftables/files/nftables-mk.confd |
92 |
@@ -0,0 +1,26 @@ |
93 |
+# /etc/conf.d/nftables |
94 |
+ |
95 |
+# Location in which nftables initscript will save set rules on |
96 |
+# service shutdown |
97 |
+NFTABLES_SAVE="/var/lib/nftables/rules-save" |
98 |
+ |
99 |
+# Options to pass to nft on save |
100 |
+SAVE_OPTIONS="-n" |
101 |
+ |
102 |
+# Save state on stopping nftables |
103 |
+SAVE_ON_STOP="yes" |
104 |
+ |
105 |
+# Only for OpenRC systems. |
106 |
+# Set to "hard" or "soft" to panic when stopping instead of |
107 |
+# clearing the rules |
108 |
+# Soft panic loads a ruleset dropping any new or invalid connections |
109 |
+# Hard panic loads a ruleset dropping all traffic |
110 |
+PANIC_ON_STOP="" |
111 |
+ |
112 |
+# If you need to log nftables messages as soon as nftables starts, |
113 |
+# AND your logger does NOT depend on the network, then you may wish |
114 |
+# to uncomment the next line. |
115 |
+# If your logger depends on the network, and you uncomment this line |
116 |
+# you will create an unresolvable circular dependency during startup. |
117 |
+# After commenting or uncommenting this line, you must run 'rc-update -u'. |
118 |
+#rc_use="logger" |
119 |
|
120 |
diff --git a/net-firewall/nftables/files/nftables-mk.init b/net-firewall/nftables/files/nftables-mk.init |
121 |
new file mode 100644 |
122 |
index 00000000000..f7e3dce8ada |
123 |
--- /dev/null |
124 |
+++ b/net-firewall/nftables/files/nftables-mk.init |
125 |
@@ -0,0 +1,104 @@ |
126 |
+#!/sbin/openrc-run |
127 |
+# Copyright 1999-2019 Gentoo Authors |
128 |
+# Distributed under the terms of the GNU General Public License v2 |
129 |
+ |
130 |
+extra_commands="check clear list panic save soft_panic" |
131 |
+extra_started_commands="reload" |
132 |
+ |
133 |
+depend() { |
134 |
+ need localmount #434774 |
135 |
+ before net |
136 |
+} |
137 |
+ |
138 |
+checkkernel() { |
139 |
+ if ! /sbin/nft list ruleset >/dev/null 2>/dev/null ; then |
140 |
+ eerror "Your kernel lacks nftables support, please load" |
141 |
+ eerror "appropriate modules and try again." |
142 |
+ return 1 |
143 |
+ fi |
144 |
+ return 0 |
145 |
+} |
146 |
+ |
147 |
+checkconfig() { |
148 |
+ if [ -z "${NFTABLES_SAVE}" -o ! -f "${NFTABLES_SAVE}" ] ; then |
149 |
+ eerror "Not starting nftables. First create some rules then run:" |
150 |
+ eerror "/etc/init.d/${SVCNAME} save" |
151 |
+ return 1 |
152 |
+ fi |
153 |
+ return 0 |
154 |
+} |
155 |
+ |
156 |
+start_pre() { |
157 |
+ checkconfig || return 1 |
158 |
+ checkkernel || return 1 |
159 |
+ check || return 1 |
160 |
+} |
161 |
+ |
162 |
+start() { |
163 |
+ ebegin "Loading ${SVCNAME} state and starting firewall" |
164 |
+ /usr/libexec/nftables/nftables.sh load "${NFTABLES_SAVE}" |
165 |
+ eend $? |
166 |
+} |
167 |
+ |
168 |
+stop() { |
169 |
+ if [ "${SAVE_ON_STOP}" = "yes" ] ; then |
170 |
+ save || return 1 |
171 |
+ fi |
172 |
+ |
173 |
+ ebegin "Stopping firewall" |
174 |
+ if [ "${PANIC_ON_STOP}" = "hard" ]; then |
175 |
+ /usr/libexec/nftables/nftables.sh panic |
176 |
+ elif [ "${PANIC_ON_STOP}" = "soft" ]; then |
177 |
+ /usr/libexec/nftables/nftables.sh soft_panic |
178 |
+ else |
179 |
+ /usr/libexec/nftables/nftables.sh clear |
180 |
+ fi |
181 |
+ eend $? |
182 |
+} |
183 |
+ |
184 |
+reload() { |
185 |
+ start_pre || return 1 |
186 |
+ start |
187 |
+} |
188 |
+ |
189 |
+clear() { |
190 |
+ ebegin "Clearing rules" |
191 |
+ /usr/libexec/nftables/nftables.sh clear |
192 |
+ eend $? |
193 |
+} |
194 |
+ |
195 |
+list() { |
196 |
+ /usr/libexec/nftables/nftables.sh list |
197 |
+} |
198 |
+ |
199 |
+check() { |
200 |
+ ebegin "Checking rules" |
201 |
+ /usr/libexec/nftables/nftables.sh check "${NFTABLES_SAVE}" |
202 |
+ eend $? |
203 |
+} |
204 |
+ |
205 |
+save() { |
206 |
+ ebegin "Saving ${SVCNAME} state" |
207 |
+ checkpath -q -d "$(dirname "${NFTABLES_SAVE}")" |
208 |
+ checkpath -q -m 0600 -f "${NFTABLES_SAVE}" |
209 |
+ /usr/libexec/nftables/nftables.sh store "${NFTABLES_SAVE}" |
210 |
+ eend $? |
211 |
+} |
212 |
+ |
213 |
+panic() { |
214 |
+ if service_started ${SVCNAME}; then |
215 |
+ rc-service ${SVCNAME} zap |
216 |
+ fi |
217 |
+ ebegin "Dropping all packets" |
218 |
+ /usr/libexec/nftables/nftables.sh panic |
219 |
+ eend $? |
220 |
+} |
221 |
+ |
222 |
+soft_panic() { |
223 |
+ if service_started ${SVCNAME}; then |
224 |
+ rc-service ${SVCNAME} zap |
225 |
+ fi |
226 |
+ ebegin "Dropping new connections" |
227 |
+ /usr/libexec/nftables/nftables.sh soft_panic |
228 |
+ eend $? |
229 |
+} |
230 |
|
231 |
diff --git a/net-firewall/nftables/metadata.xml b/net-firewall/nftables/metadata.xml |
232 |
index 1a891a6e533..dcc71d66d6f 100644 |
233 |
--- a/net-firewall/nftables/metadata.xml |
234 |
+++ b/net-firewall/nftables/metadata.xml |
235 |
@@ -9,7 +9,12 @@ |
236 |
<email>prometheanfire@g.o</email> |
237 |
<name>Matthew Thode</name> |
238 |
</maintainer> |
239 |
+ <maintainer type="person"> |
240 |
+ <email>klondike@g.o</email> |
241 |
+ <name>Francisco Blas Izquierdo Riera</name> |
242 |
+ </maintainer> |
243 |
<use> |
244 |
<flag name="json">Enable JSON support via <pkg>dev-libs/jansson</pkg></flag> |
245 |
+ <flag name="modern_kernel">Install init scripts for 3.18 or higher kernels with atomic rule updates</flag> |
246 |
</use> |
247 |
</pkgmetadata> |
248 |
|
249 |
diff --git a/net-firewall/nftables/nftables-0.9.0-r2.ebuild b/net-firewall/nftables/nftables-0.9.0-r2.ebuild |
250 |
new file mode 100644 |
251 |
index 00000000000..346d321bb37 |
252 |
--- /dev/null |
253 |
+++ b/net-firewall/nftables/nftables-0.9.0-r2.ebuild |
254 |
@@ -0,0 +1,94 @@ |
255 |
+# Copyright 1999-2019 Gentoo Authors |
256 |
+# Distributed under the terms of the GNU General Public License v2 |
257 |
+ |
258 |
+EAPI=6 |
259 |
+ |
260 |
+inherit autotools linux-info systemd |
261 |
+ |
262 |
+DESCRIPTION="Linux kernel (3.13+) firewall, NAT and packet mangling tools" |
263 |
+HOMEPAGE="https://netfilter.org/projects/nftables/" |
264 |
+SRC_URI="https://git.netfilter.org/nftables/snapshot/v${PV}.tar.gz -> ${P}.tar.gz" |
265 |
+ |
266 |
+LICENSE="GPL-2" |
267 |
+SLOT="0" |
268 |
+KEYWORDS="~amd64 ~arm ~ia64 ~x86" |
269 |
+IUSE="debug doc +gmp json +modern_kernel +readline" |
270 |
+ |
271 |
+RDEPEND=">=net-libs/libmnl-1.0.3:0= |
272 |
+ gmp? ( dev-libs/gmp:0= ) |
273 |
+ json? ( dev-libs/jansson ) |
274 |
+ readline? ( sys-libs/readline:0= ) |
275 |
+ >=net-libs/libnftnl-1.1.1:0=" |
276 |
+ |
277 |
+DEPEND="${RDEPEND} |
278 |
+ >=app-text/docbook2X-0.8.8-r4 |
279 |
+ doc? ( >=app-text/dblatex-0.3.7 ) |
280 |
+ sys-devel/bison |
281 |
+ sys-devel/flex |
282 |
+ virtual/pkgconfig" |
283 |
+ |
284 |
+S="${WORKDIR}/v${PV}" |
285 |
+ |
286 |
+pkg_setup() { |
287 |
+ if kernel_is ge 3 13; then |
288 |
+ if use modern_kernel && kernel_is lt 3 18; then |
289 |
+ eerror "The modern_kernel USE flag requires kernel version 3.18 or newer to work properly." |
290 |
+ fi |
291 |
+ CONFIG_CHECK="~NF_TABLES" |
292 |
+ linux-info_pkg_setup |
293 |
+ else |
294 |
+ eerror "This package requires kernel version 3.13 or newer to work properly." |
295 |
+ fi |
296 |
+} |
297 |
+ |
298 |
+src_prepare() { |
299 |
+ default |
300 |
+ eautoreconf |
301 |
+} |
302 |
+ |
303 |
+src_configure() { |
304 |
+ local myeconfargs=( |
305 |
+ --sbindir="${EPREFIX}"/sbin |
306 |
+ $(use_enable debug) |
307 |
+ $(use_enable doc pdf-doc) |
308 |
+ $(use_with !gmp mini_gmp) |
309 |
+ $(use_with json) |
310 |
+ $(use_with readline cli) |
311 |
+ ) |
312 |
+ econf "${myeconfargs[@]}" |
313 |
+} |
314 |
+ |
315 |
+src_install() { |
316 |
+ default |
317 |
+ |
318 |
+ local mksuffix="" |
319 |
+ use modern_kernel && mksuffix="-mk" |
320 |
+ |
321 |
+ exeinto /usr/libexec/${PN} |
322 |
+ newexe "${FILESDIR}"/libexec/${PN}${mksuffix}.sh ${PN}.sh |
323 |
+ newconfd "${FILESDIR}"/${PN}${mksuffix}.confd ${PN} |
324 |
+ newinitd "${FILESDIR}"/${PN}${mksuffix}.init ${PN} |
325 |
+ keepdir /var/lib/nftables |
326 |
+ |
327 |
+ systemd_dounit "${FILESDIR}"/systemd/${PN}-restore.service |
328 |
+ systemd_enable_service basic.target ${PN}-restore.service |
329 |
+ |
330 |
+ docinto /usr/share/doc/${PF}/skels |
331 |
+ dodoc "${D}"/etc/nftables/* |
332 |
+ rm -R "${D}"/etc/nftables |
333 |
+} |
334 |
+ |
335 |
+pkg_postinst() { |
336 |
+ local save_file |
337 |
+ save_file="${EROOT%/}/var/lib/nftables/rules-save" |
338 |
+ |
339 |
+ # In order for the nftables-restore systemd service to start |
340 |
+ # the save_file must exist. |
341 |
+ if [[ ! -f ${save_file} ]]; then |
342 |
+ touch ${save_file} |
343 |
+ fi |
344 |
+ |
345 |
+ elog "If you are creating firewall rules before the next system restart " |
346 |
+ elog "the nftables-restore service must be manually started in order to " |
347 |
+ elog "save those rules on shutdown." |
348 |
+} |