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