Developing lightweight computation at the DSG edge

qmp_gw.sh 4.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
#    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".
20
21
22
23
#
# Contributors:
#	Simó Albert i Beltran
#
24
25
26
27

QMP_PATH="/etc/qmp"
SOURCE_GW=1

28
29
30
31
32
if [ -z "$SOURCE_OPENWRT_FUNCTIONS" ]
then
	. /lib/functions.sh
	SOURCE_OPENWRT_FUNCTIONS=1
fi
33
34
35
[ -z "$SOURCE_COMMON" ] && . $QMP_PATH/qmp_common.sh
[ -z "$SOURCE_NETWORK" ] &&. $QMP_PATH/qmp_functions.sh

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
qmp_exists_gateway()
{
	local config=$1
	shift
	local ignore=0
	local exists

	args_key_values="$(echo $@ | awk -v RS=' ' 'NR % 2 == 1 && $0 !~ "(ignore|bandwidth)" {a+=1} END {print a}')"
	uci_key_values=$(env | grep -v -e "^CONFIG_${config}_\(TYPE\|ignore\|bandwidth\)=" | grep -c "^CONFIG_${config}_")

	if [ "$args_key_values" != "$uci_key_values" ]
	then
		return
	fi

	while [ $# -ge 2 ]
	do
		if [ "$1" = "ignore" ]
		then
			ignore="$2"
		else
			config_get exists "$config" $1
			if [ "$1" = "bandwidth" ]
			then
				if [ -z "$exists" ]
				then
					return
				fi
			elif [ "$exists" != "$2" ]
			then
				return
			fi
		fi
		shift
		shift
	done

	uci_set qmp "$config" ignore "$ignore"
	uci_commit
	qmp_gateway_found=true
}

qmp_set_gateway()
{
	config_load qmp
	qmp_gateway_found=false
	config_foreach qmp_exists_gateway gateway $@
	if ! $qmp_gateway_found
	then
		local config
		config="$(uci add qmp gateway)"
		while [ $# -ge 2 ]
		do
			uci_set qmp "$config" "$1" "$2"
			shift
			shift
		done
		uci_commit
	fi
}

97
qmp_gw_search_default_ipv4() {
98
99
	qmp_set_gateway ignore 1 type offer network 0.0.0.0/0 bandwidth 100000
	qmp_set_gateway ignore 0 type search network 0.0.0.0/0 maxPrefixLen 0
100
	qmp_gw_masq_wan 0
101
102
103
}

qmp_gw_search_default_ipv6() {
104
105
	qmp_set_gateway ignore 1 type offer network ::/0 bandwidth 100000
	qmp_set_gateway ignore 0 type search network ::/0
106
	qmp_gw_masq_wan 0
107
108
}

109
qmp_gw_offer_default_ipv4() {
110
111
	qmp_set_gateway ignore 1 type search network 0.0.0.0/0 maxPrefixLen 0
	qmp_set_gateway ignore 0 type offer network 0.0.0.0/0 bandwidth 100000
112
	qmp_gw_masq_wan 1
113
114
115
}

qmp_gw_offer_default_ipv6() {
116
117
	qmp_set_gateway ignore 1 type search network ::/0
	qmp_set_gateway ignore 0 type offer network ::/0 bandwidth 100000
118
	qmp_gw_masq_wan 1
119
120
}

121
qmp_gw_disable_default_ipv4() {
122
123
	qmp_set_gateway ignore 1 type search network 0.0.0.0/0 maxPrefixLen 0
	qmp_set_gateway ignore 1 type offer network 0.0.0.0/0 bandwidth 100000
124
125
126
127
	qmp_gw_masq_wan 0
}

qmp_gw_disable_default_ipv6() {
128
129
	qmp_set_gateway ignore 1 type search network ::/0
	qmp_set_gateway ignore 1 type offer network ::/0 bandwidth 100000
130
131
132
	qmp_gw_masq_wan 0
}

133
134
135
136
137
138
139
140
141
142
qmp_gw_default() {
	if [ -n "$2" ]
	then
		qmp_gw_$1_default_$2
	else
		qmp_gw_$1_default_ipv4
		qmp_gw_$1_default_ipv6
	fi
}

143
144
145
146
147
148
149
150
151
152
153
qmp_gw_add_interfaces_to_firewall_zone() {
	local cfg=$1
	local $virtual_interfaces
	for interface in $(qmp_get_devices wan)
	do
		[ -n "$virtual_interfaces" ] && virtual_interfaces="$virtual_interfaces "
		virtual_interfaces="$virtual_interfaces$(qmp_get_virtual_iface $interface)"
	done
	qmp_uci_set_raw firewall.$cfg.network="$virtual_interfaces"
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
qmp_gw_masq_wan() {
	#First parameter is 1/0 (enable/disable masquerade). Default is 1
	[ -z "$1" ] && masq=1 || masq=$1
	j=0
	v="nothing"
	wan=""

	#Looking for a firewall zone with name wan
	while [ ! -z "$v" ]; do
		v=$(qmp_uci_get_raw firewall.@zone[$j].name)
		[ "$v" == "wan" ] && { wan=$j; break; }
		j=$(( $j +1 ))
	done

	if [ -z "$wan" ]; then
	#if not found, we are going to create it
		cfg="$(qmp_uci_add_raw_get_cfg firewall zone)"
		qmp_uci_set_cfg firewall.$cfg.input=ACCEPT
		qmp_uci_set_cfg firewall.$cfg.output=ACCEPT
		qmp_uci_set_cfg firewall.$cfg.forward=ACCEPT
		qmp_uci_set_cfg firewall.$cfg.name=wan
		qmp_uci_set_cfg firewall.$cfg.masq=$masq
		qmp_uci_commit firewall

	else
	#if found we just change parameters
		qmp_uci_set_raw firewall.@zone[$wan].input=ACCEPT
		qmp_uci_set_raw firewall.@zone[$wan].output=ACCEPT
		qmp_uci_set_raw firewall.@zone[$wan].forward=ACCEPT
		qmp_uci_set_raw firewall.@zone[$wan].masq=$masq
184
		cfg=@zone[$wan]
185
	fi
186
187

	qmp_gw_add_interfaces_to_firewall_zone $cfg
188
189
190
191
}

qmp_gw_apply() {
	qmp_configure_bmx6
192
	bmx6 -c --configReload || /etc/init.d/bmx6 restart
193
194
195
	/etc/init.d/firewall restart
}