8ca5d3b77ba0e8096a00ce927c708fe7684f69af
[librecmc/librecmc.git] /
1 #!/bin/sh
2 # This will add $net to the zone firewall (and remove it from other zones where it is referenced)
3 # It will also setup rules defined in /etc/config/freifunk and /etc/config/profile_<community>
4 # Arg $1 = $net
5
6 net=$1
7 . /lib/functions.sh
8 . $dir/functions.sh
9 config_load firewall
10
11 # Get some variables
12 type="$(uci -q get wireless.$net.type)"
13 vap="$(uci -q get meshwizard.netconfig.$net\_vap)"
14 wan_is_olsr=$(uci -q get meshwizard.netconfig.wan_config)
15
16 # Delete old firewall zone for freifunk
17 handle_fwzone() {
18         config_get name "$1" name
19         config_get network "$1" network
20
21         if [ "$2" == "zoneconf" ]; then
22                 if [ "$name" == "freifunk" ]; then
23                         # rename section if unnamed
24                         if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
25                                 section_rename firewall $1 zone_freifunk
26                         fi
27                 else
28                         if [ ! "$name" == "freifunk" ] && [ -n "$netrenamed" -a -n "$(echo $network | grep $netrenamed)" ]; then
29                                 echo "    Removed $netrenamed from firewall zone $name."
30                                 network_new=$(echo $network | sed -e 's/'$netrenamed'//' -e 's/^ //' -e 's/  / /' -e 's/ $//')
31                                 uci set firewall.$1.network="$network_new"
32                         fi
33                 fi
34         fi
35 }
36
37 config_foreach handle_fwzone zone zoneconf
38
39 # Add $netrenamed and if needed ${netrenamed}dhcp to the "freifunk" zone
40 config_get network zone_freifunk network
41
42 # remove ${netrenamed}dhcp from networks list
43 [ -n "$network" -a -n "$net" ] && network="${network/${netrenamed}dhcp/}"
44 network=$(echo $network) # Removes leading and trailing whitespaces
45
46 [ -n "$netrenamed" ] && [ -z "$(echo $network | grep $netrenamed)" ] && network="$network $netrenamed"
47
48 if [ "$supports_vap" == "1" -a "$vap" == 1 ]; then
49         [ -n "$netrenamed" ] && [ "$network" == "${network/${netrenamed}dhcp/}" ] && network="$network ${netrenamed}dhcp"
50 fi
51
52 uci set firewall.zone_freifunk.network="$network"
53
54 uci_commitverbose "Add '$netrenamed' to freifunk firewall zone" firewall
55
56 currms=$(uci -q get firewall.zone_freifunk.masq_src)
57
58 # If interfaces are outside of the mesh network they should be natted
59
60 if [ "$vap" == 1 ]; then
61         # Get dhcprange and meshnet for the dhcp interface
62         if_ip="$(uci -q get network.${netrenamed}dhcp.ipaddr)"
63         if_mask="$(uci -q get network.${netrenamed}dhcp.netmask)"
64
65         [ -n "$if_ip" -a "$if_mask" ] && export $(ipcalc.sh $if_ip $if_mask)
66         [ -n "$NETWORK" -a "$PREFIX" ] && dhcprange="$NETWORK/$PREFIX"
67
68         if [ -n "$dhcprange" ]; then
69                 meshnet="$(uci get profile_$community.profile.mesh_network)"
70                 # check if the dhcprange is inside meshnet
71                 dhcpinmesh="$($dir/helpers/check-range-in-range.sh $dhcprange $meshnet)"
72                 if [ "$dhcpinmesh" == 1 ]; then
73                         # needed or splash will not work
74                         if [ "$has_luci_splash" == TRUE ]; then
75                                 uci set firewall.zone_freifunk.contrack="1"
76                         fi
77                 else
78                         uci set firewall.zone_freifunk.masq=1
79                         [ -z "$(echo $currms |grep ${netrenamed}dhcp)" ] && uci add_list firewall.zone_freifunk.masq_src="${netrenamed}dhcp"
80                 fi
81         fi
82 fi
83
84 # Get dhcprange and meshnet for the adhoc dhcp interface
85 if_ip="$(uci -q get network.${netrenamed}ahdhcp.ipaddr)"
86 if_mask="$(uci -q get network.${netrenamed}ahdhcp.netmask)"
87
88 [ -n "$if_ip" -a "$if_mask" ] && export $(ipcalc.sh $if_ip $if_mask)
89 [ -n "$NETWORK" -a "$PREFIX" ] && dhcprangeah="$NETWORK/$PREFIX"
90
91 if [ -n "$dhcprangeah" ]; then
92         meshnet="$(uci get profile_$community.profile.mesh_network)"
93         # check if the dhcprange is inside meshnet
94         dhcpinmesh="$($dir/helpers/check-range-in-range.sh $dhcprangeah $meshnet)"
95         if [ "$dhcpinmesh" == 1 ]; then
96                 # needed or splash will not work
97                 if [ "$has_luci_splash" == TRUE ]; then
98                         uci set firewall.zone_freifunk.contrack="1"
99                 fi
100         else
101                 uci set firewall.zone_freifunk.masq=1
102                 [ -z "$(echo $currms |grep ${netrenamed}ahdhcp)" ] && uci add_list firewall.zone_freifunk.masq_src="${netrenamed}ahdhcp"
103         fi
104 fi
105
106
107 for i in IP NETMASK BROADCAST NETWORK PREFIX; do
108         unset $i
109 done
110
111 uci_commitverbose "Setup masquerading rules for '$netrenamed'" firewall