summaryrefslogtreecommitdiffstats
path: root/package/iptables/files/firewall.awk
blob: 1b63f838620d2fe61071b86f01fc78af3de59e1d (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
BEGIN {
	print "proto=\"$wan_proto\""
	print "[ -z \"$proto\" -o \"$proto\" = \"none\" ] && exit"
	print "ifname=\"$wan_ifname\""
	print "[ -z \"$ifname\" ] && exit"
	print ""
	print "iptables -X input_$ifname 2>&- >&-"
	print "iptables -N input_$ifname"
	print "iptables -X forward_$ifname 2>&- >&-"
	print "iptables -N forward_$ifname"
	print "iptables -t nat -X prerouting_$ifname 2>&- >&-"
	print "iptables -t nat -N prerouting_$ifname"
	print ""
	print "iptables -A input_rule -i \"$ifname\" -j input_$ifname"
	print "iptables -A forwarding_rule -i \"$ifname\" -j forward_$ifname"
	print "iptables -t nat -A prerouting_rule -i \"$ifname\" -j prerouting_$ifname"
	print ""
	FS=":"
}

($1 == "accept") || ($1 == "drop") || ($1 == "forward") {
	delete _opt
	str2data($2)
	if ((_l["proto"] == "") && (_l["sport"] _l["dport"] != "")) {
		_opt[0] = " -p tcp"
		_opt[1] = " -p udp"
	} else {
		_opt[0] = ""
	}
}

($1 == "accept") {
	target = " -j ACCEPT"
	for (o in _opt) {
		print "iptables -t nat -A prerouting_$ifname" _opt[o] str2ipt($2) target
		print "iptables        -A input_$ifname     " _opt[o] str2ipt($2) target
		print ""
	}
}

($1 == "drop") {
	for (o in _opt) {
		print "iptables -t nat -A prerouting_$ifname" _opt[o] str2ipt($2) " -j DROP"
		print ""
	}
}

($1 == "forward") {
	target = " -j DNAT --to " $3
	fwopts = ""
	if ($4 != "") {
		if ((_l["proto"] == "tcp") || (_l["proto"] == "udp") || (_l["proto"] == "")) {
			if (_l["proto"] != "") fwopts = " -p " _l["proto"]
			fwopts = fwopts " --dport " $4
			target = target ":" $4
		}
		else fwopts = ""
	}
	for (o in _opt) {
		print "iptables -t nat -A prerouting_$ifname" _opt[o] str2ipt($2) target
		print "iptables        -A forward_$ifname   " _opt[o] " -d " $3 fwopts " -j ACCEPT"
		print ""
	}
}