* Fixed firewall scripts
[project/luci.git] / contrib / init.d / luci_fw
1 #!/bin/sh /etc/rc.common
2 START=46
3
4 apply_portfw() {
5 local cfg="$1"
6 config_get proto "$cfg" proto
7 config_get dport "$cfg" dport
8 config_get iface "$cfg" iface
9 config_get to "$cfg" to
10
11 ports=$(echo $to | cut -sd: -f2)
12 [ -n "$ports" ] && ports="--dport $(echo $ports | sed -e 's/-/:/')"
13
14 ip=$(echo $to | cut -d: -f1)
15
16 if ([ "$proto" == "tcpudp" ] || [ "$proto" == "tcp" ]); then
17 iptables -t nat -A luci_prerouting -i "$iface" -p tcp --dport "$dport" -j DNAT --to "$to"
18 iptables -t nat -A luci_postrouting -p tcp -d "$ip" $ports -j MASQUERADE
19 iptables -A luci_forward -i "$iface" -p tcp -d "$ip" $ports -j ACCEPT
20 fi
21
22 if ([ "$proto" == "tcpudp" ] || [ "$proto" == "udp" ]); then
23 iptables -t nat -A luci_prerouting -i "$iface" -p udp --dport "$dport" -j DNAT --to "$to"
24 iptables -t nat -A luci_postrouting -p udp -d "$ip" $ports -j MASQUERADE
25 iptables -A luci_forward -i "$iface" -p udp -d "$ip" $ports -j ACCEPT
26 fi
27 }
28
29 apply_rule() {
30 local cfg="$1"
31 local cmd=""
32
33 config_get chain "$cfg" chain
34 [ -n "$chain" ] || return 0
35 [ "$chain" == "forward" ] && cmd="$cmd -A luci_forward"
36 [ "$chain" == "input" ] && cmd="$cmd -A luci_input"
37 [ "$chain" == "output" ] && cmd="$cmd -A luci_output"
38 [ "$chain" == "prerouting" ] && cmd="$cmd -t nat -A luci_prerouting"
39 [ "$chain" == "postrouting" ] && cmd="$cmd -t nat -A luci_postrouting"
40
41 config_get iface "$cfg" iface
42 [ -n "$iface" ] && cmd="$cmd -i $iface"
43
44 config_get oface "$cfg" oface
45 [ -n "$oface" ] && cmd="$cmd -o $oface"
46
47 config_get proto "$cfg" proto
48 [ -n "$proto" ] && cmd="$cmd -p $proto"
49
50 config_get source "$cfg" source
51 [ -n "$source" ] && cmd="$cmd -s $source"
52
53 config_get destination "$cfg" destination
54 [ -n "$destination" ] && cmd="$cmd -d $destination"
55
56 config_get sport "$cfg" sport
57 [ -n "$sport" ] && cmd="$cmd --sport $sport"
58
59 config_get dport "$cfg" dport
60 [ -n "$dport" ] && cmd="$cmd --dport $dport"
61
62 config_get todest "$cfg" todest
63 [ -n "$todest" ] && cmd="$cmd --to-destination $todest"
64
65 config_get tosrc "$cfg" tosrc
66 [ -n "$tosrc" ] && cmd="$cmd --to-source $tosrc"
67
68 config_get jump "$cfg" jump
69 [ -n "$jump" ] && cmd="$cmd -j $jump"
70
71 config_get command "$cfg" command
72 [ -n "$command" ] && cmd="$cmd $command"
73
74 iptables $cmd
75 }
76
77 start() {
78 ### Create subchains
79 iptables -N luci_input
80 iptables -N luci_output
81 iptables -N luci_forward
82 iptables -t nat -N luci_prerouting
83 iptables -t nat -N luci_postrouting
84
85 ### Hook in the chains
86 iptables -A input_rule -j luci_input
87 iptables -A output_rule -j luci_output
88 iptables -A forwarding_rule -j luci_forward
89 iptables -t nat -A prerouting_rule -j luci_prerouting
90 iptables -t nat -A postrouting_rule -j luci_postrouting
91
92 ### Read chains from config
93 config_load luci_fw
94 config_foreach apply_portfw portfw
95 config_foreach apply_rule rule
96 }
97
98 stop() {
99 ### Hook out the chains
100 iptables -D input_rule -j luci_input
101 iptables -D output_rule -j luci_output
102 iptables -D forwarding_rule -j luci_forward
103 iptables -t nat -D prerouting_rule -j luci_prerouting
104 iptables -t nat -D postrouting_rule -j luci_postrouting
105
106 ### Clear subchains
107 iptables -F luci_input
108 iptables -F luci_output
109 iptables -F luci_forward
110 iptables -t nat -F luci_prerouting
111 iptables -t nat -F luci_postrouting
112
113 ### Delete subchains
114 iptables -X luci_input
115 iptables -X luci_output
116 iptables -X luci_forward
117 iptables -t nat -X luci_prerouting
118 iptables -t nat -X luci_postrouting
119 }