663112389fdd04fe0bbcb41bc8de92db0f7f7d7e
[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         
14         config_get lanip   lan ipaddr
15         config_get lanmask lan netmask
16         
17         config_get ffip    ff ipaddr
18         config_get ffmask  ff netmask
19         
20         [ -n "$ffif" ] || return 0
21         
22         
23         ### Creating chains
24         iptables -N luci_freifunk_forwarding
25         iptables -t nat -N luci_freifunk_postrouting
26                 
27         
28         ### Read from config
29         config_load freifunk
30         
31         config_get_bool internal routing internal
32         [ -n "$wanif" ] && config_get_bool internet routing internet
33         
34         
35         ### Freifunk to Freifunk
36         [ "$internal" -gt 0 ] && {
37                 iptables -A luci_freifunk_forwarding -i "$ffif" -o "$ffif" -j ACCEPT
38         }
39         
40         ### Lan to Freifunk     
41         [ -n "$lanif" ] && {
42                 eval "$(ipcalc.sh $lanip $lanmask)"
43                 
44                 iptables -A luci_freifunk_forwarding -i "$lanif" -o "$ffif" -j ACCEPT
45                 iptables -t nat -A luci_freifunk_postrouting -s "$NETWORK/$PREFIX" -o "$ffif" -j MASQUERADE
46         }
47         
48         ### Freifunk to Wan
49         [ "$internet" -gt 0 ] && {              
50                 eval "$(ipcalc.sh $ffip $ffmask)"               
51                 
52                 iptables -A luci_freifunk_forwarding -i "$ffif" -o "$wanif" -j ACCEPT
53                 iptables -t nat -A luci_freifunk_postrouting -s "$NETWORK/$PREFIX" -o "$wanif" -j MASQUERADE
54         }
55         
56         ### Hook in the chains
57         iptables -A forwarding_rule -j luci_freifunk_forwarding
58         iptables -t nat -A postrouting_rule -j luci_freifunk_postrouting
59 }
60
61 stop() {
62         ### Hook out the chains
63         iptables -D forwarding_rule -j luci_freifunk_forwarding
64         iptables -t nat -D postrouting_rule -j luci_freifunk_postrouting
65         
66         ### Clear the chains
67         iptables -F luci_freifunk_forwarding
68         iptables -t nat -F luci_freifunk_postrouting
69         
70         ### Delete chains
71         iptables -X luci_freifunk_forwarding
72         iptables -t nat -X luci_freifunk_postrouting
73 }