* Added NAT rule for Freifunk DHCP
[project/luci.git] / module / admin-core / contrib / init.d / luci_freifunk
1 #!/bin/sh /etc/rc.common
2 START=70
3
4 start() {
5         include /lib/network
6         scan_interfaces
7         
8         
9         ### Read interface names
10         config_get wanif wan ifname
11         config_get lanif lan ifname
12         config_get ffif  ff  ifname
13         config_get ffdif ffdhcp  ifname
14         
15         config_get lanip   lan ipaddr
16         config_get lanmask lan netmask
17         
18         config_get ffip    ff ipaddr
19         config_get ffmask  ff netmask
20         
21         config_get ffdip   ffdhcp ipaddr
22         config_get ffdmask ffdhcp netmask
23         
24         [ -n "$ffif" ] || return 0
25         
26         
27         ### Creating chains
28         iptables -N luci_freifunk_forwarding
29         iptables -t nat -N luci_freifunk_postrouting
30                 
31         
32         ### Read from config
33         config_load freifunk
34         
35         config_get_bool internal routing internal
36         [ -n "$wanif" ] && config_get_bool internet routing internet
37         
38         
39         ### Freifunk to Freifunk
40         [ "$internal" -gt 0 ] && {
41                 iptables -A luci_freifunk_forwarding -i "$ffif" -o "$ffif" -j ACCEPT
42         }
43         
44         ### Lan to Freifunk     
45         [ -n "$lanif" ] && {
46                 eval "$(ipcalc.sh $lanip $lanmask)"
47                 
48                 iptables -A luci_freifunk_forwarding -i "$lanif" -o "$ffif" -j ACCEPT
49                 iptables -t nat -A luci_freifunk_postrouting -s "$NETWORK/$PREFIX" -o "$ffif" -j MASQUERADE
50         }
51         
52         ### Freifunk to Wan
53         [ "$internet" -gt 0 ] && {              
54                 eval "$(ipcalc.sh $ffip $ffmask)"               
55                 
56                 iptables -A luci_freifunk_forwarding -i "$ffif" -o "$wanif" -j ACCEPT
57                 iptables -t nat -A luci_freifunk_postrouting -s "$NETWORK/$PREFIX" -o "$wanif" -j MASQUERADE
58         }
59         
60         ### Freifunk DHCP to Wan
61         [ "$internet" -gt 0 -a -n "$ffdif" ] && {               
62                 eval "$(ipcalc.sh $ffdip $ffdmask)"             
63                 
64                 iptables -t nat -A luci_freifunk_postrouting -s "$NETWORK/$PREFIX" -o "$wanif" -j MASQUERADE
65         }
66         
67         ### Hook in the chains
68         iptables -A forwarding_rule -j luci_freifunk_forwarding
69         iptables -t nat -A postrouting_rule -j luci_freifunk_postrouting
70 }
71
72 stop() {
73         ### Hook out the chains
74         iptables -D forwarding_rule -j luci_freifunk_forwarding
75         iptables -t nat -D postrouting_rule -j luci_freifunk_postrouting
76         
77         ### Clear the chains
78         iptables -F luci_freifunk_forwarding
79         iptables -t nat -F luci_freifunk_postrouting
80         
81         ### Delete chains
82         iptables -X luci_freifunk_forwarding
83         iptables -t nat -X luci_freifunk_postrouting
84 }