]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/templates/default/nftables.erb
imagery: Bump leaflet versions
[chef.git] / cookbooks / networking / templates / default / nftables.erb
1 #!/bin/sh -eu
2
3 usage() {
4   echo "Usage: $0 {start|stop|reload|restart|block|drop|deny|unblock|allow|flush} [address ...]"
5   exit 1
6 }
7
8 start() {
9   /usr/sbin/nft -f /etc/nftables.conf
10   [ -f /var/lib/nftables/ip-blocklist.nft ] && /usr/sbin/nft -f /var/lib/nftables/ip-blocklist.nft || :
11   [ -f /var/lib/nftables/ip6-blocklist.nft ] && /usr/sbin/nft -f /var/lib/nftables/ip6-blocklist.nft || :
12 }
13
14 stop() {
15   /usr/sbin/nft list set inet chef-filter ip-blocklist > /var/lib/nftables/ip-blocklist.nft
16   /usr/sbin/nft list set inet chef-filter ip6-blocklist > /var/lib/nftables/ip6-blocklist.nft
17   /usr/sbin/nft delete table inet chef-filter
18 <% if node[:roles].include?("gateway") -%>
19   /usr/sbin/nft delete table ip chef-nat
20 <% end -%>
21 }
22
23 reload() {
24   stop
25   start
26 }
27
28 block() {
29   for address in "$@"
30   do
31     case "$address" in
32       *.*) /usr/sbin/nft --check add element inet chef-filter ip-blocklist "{ $address }" && /usr/sbin/nft add element inet chef-filter ip-blocklist "{ $address }" ;;
33       *:*) /usr/sbin/nft --check add element inet chef-filter ip6-blocklist "{ $address }" && /usr/sbin/nft add element inet chef-filter ip6-blocklist "{ $address }" ;;
34     esac
35   done
36 }
37
38 unblock() {
39   for address in "$@"
40   do
41     case "$address" in
42       *.*) /usr/sbin/nft --check delete element inet chef-filter ip-blocklist "{ $address }" && /usr/sbin/nft delete element inet chef-filter ip-blocklist "{ $address }" ;;
43       *:*) /usr/sbin/nft --check delete element inet chef-filter ip6-blocklist "{ $address }" && /usr/sbin/nft delete element inet chef-filter ip6-blocklist "{ $address }" ;;
44     esac
45   done
46 }
47
48 flush() {
49   /usr/sbin/nft --check flush set inet chef-filter ip-blocklist && /usr/sbin/nft flush set inet chef-filter ip-blocklist
50   /usr/sbin/nft --check flush set inet chef-filter ip6-blocklist && /usr/sbin/nft flush set inet chef-filter ip6-blocklist
51 }
52
53 command="${1:-}"
54 if [ -n "$command" ]; then
55   shift
56 fi
57
58 if [ -z "$command" ]; then
59   usage
60 fi
61
62 case "$command" in
63   start) start;;
64   stop) stop;;
65   reload|restart) reload;;
66   block|drop|deny) block "$@";;
67   unblock|allow) unblock "$@";;
68   flush) flush;;
69   *)
70     usage
71     ;;
72 esac
73
74 exit 0