5 IPTABLES
=$(which iptables)
6 IP6TABLES
=$(which ip6tables)
9 function decommentcat
(){
10 sed 's/\s*#.*$//;/^\s*$/d' "$@"
13 function create_set
(){
16 if ! $IPSET list
"${set_name}" >/dev
/null
2>&1
18 echo "creating set '${set_name}'"
19 $IPSET create
"${set_name}" "$@"
23 function create_drop_chain
(){
26 if ! $IPTABLES -L "${chain}" >/dev
/null
2>&1
28 echo "initializing chain '${chain}'"
29 $IPTABLES -N "${chain}" || $IPTABLES -F "${chain}"
30 $IPTABLES -A "${chain}" -m conntrack
--ctstate ESTABLISHED
,RELATED
-j RETURN
31 $IPTABLES -A "${chain}" -j REJECT
--reject-with icmp
-port-unreachable
32 $IPTABLES -v -L "${chain}"
35 if ! $IP6TABLES -L "${chain}" >/dev
/null
2>&1
37 echo "initializing chain '${chain}' ipv6"
38 $IP6TABLES -N "${chain}" || $IP6TABLES -F "${chain}"
39 $IP6TABLES -A "${chain}" -m conntrack
--ctstate ESTABLISHED
,RELATED
-j RETURN
40 $IP6TABLES -A "${chain}" -j REJECT
--reject-with icmp6
-port-unreachable
41 $IP6TABLES -v -L "${chain}"
45 function insert_setmatch_rules
(){
47 if [ "x$1" = "x-single-set" ]
52 local ipt set_name
="$1"
56 eval ipt
="\$IP${v}TABLES"
61 if ! $ipt -C INPUT
-m set --match-set "${set_name}${v}" src
"$@" >/dev
/null
2>&1
63 echo "initializing rule '${set_name}${v}'"
64 $ipt -I INPUT
-m set --match-set "${set_name}${v}" src
"$@"
69 function reload_cidr_sets
(){
72 # init new temporary sets
73 echo "updating set '${set_name}'"
75 create_set
"${set_name}-tmp" hash:net
76 create_set
"${set_name}6-tmp" hash:net family inet6
79 for sfx
in '' .
$(hostname -s)
81 cidrfile
="${set_name}.cidr${sfx}"
82 if [ -e "${cidrfile}" ]
84 for s
in $(decommentcat "${cidrfile}")
87 *.
*) table
="${set_name}-tmp" ;;
88 *:*) table
="${set_name}6-tmp" ;;
90 echo "unknown entry '${s}' in '${cidrfile}'" 1>&2
94 $IPSET add
"${table}" "${s}"
103 $IPSET swap
"${n}-tmp" "${n}"
104 $IPSET destroy
"${n}-tmp"
105 $IPSET list
-t "${n}"
109 function add_service_entry
(){
111 port
=$(echo "$1" | cut -d/ -f1)
112 proto
=$(echo "$1" | cut -d/ -f2)
113 $IPSET -exist add allowed_
${proto} ${port}
116 function allow_services
(){
121 */*) add_service_entry
"${s}"
123 *) for svc
in $(getent services "${s}" | awk '{print $2}')
125 add_service_entry
"${svc}"