]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wireless
wireless: Validate regulatory domain before setting and saving
[people/stevee/network.git] / src / functions / functions.wireless
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 # Sets the global wireless country code. Default is 00 = world.
23 WIRELESS_REGULATORY_DOMAIN="00"
24 NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN"
25
26 WIRELESS_REGULATORY_DOMAIN_DATABASE="/usr/lib/crda/regulatory.bin"
27
28 wireless_create() {
29 local device=${1}
30 assert isset device
31 shift
32
33 local address
34 local phy
35 local type="managed"
36
37 while [ $# -gt 0 ]; do
38 case "${1}" in
39 --address=*)
40 address=$(cli_get_val ${1})
41 ;;
42 --phy=*)
43 phy=$(cli_get_val ${1})
44 phy=$(phy_get ${phy})
45 ;;
46 --type=*)
47 type=$(cli_get_val ${1})
48
49 # ap --> __ap
50 [ "${type}" = "ap" ] && type="__ap"
51 ;;
52 *)
53 error "Unrecognized argument: ${1}"
54 return ${EXIT_ERROR}
55 ;;
56 esac
57 shift
58 done
59
60 assert isoneof type ibss managed monitor __ap
61 assert phy_exists ${phy}
62 isset address || address=$(mac_generate)
63
64 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
65 local ret=$?
66
67 if [ ${ret} -eq ${EXIT_OK} ]; then
68 log DEBUG "created wireless device '${device}' (${type})"
69
70 if isset address; then
71 device_set_address ${device} ${address}
72 fi
73 else
74 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
75 fi
76
77 return ${ret}
78 }
79
80 wireless_remove() {
81 local device=${1}
82 assert isset device
83
84 if ! device_exists ${device}; then
85 return ${EXIT_OK}
86 fi
87
88 # Tear down the device (if necessary).
89 device_set_down ${device}
90
91 # Remove it.
92 cmd_quiet iw dev ${device} del
93 local ret=$?
94
95 if [ ${ret} -eq ${EXIT_OK} ]; then
96 log DEBUG "removed wireless device '${device}'"
97 else
98 log ERROR "could not remove wireless device '${device}': ${ret}"
99 fi
100
101 return ${ret}
102 }
103
104 wireless_get_reg_domain() {
105 # Returns the country code for the wireless device.
106 # Defaults to 00 = world if unset.
107 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
108 }
109
110 wireless_init_reg_domain() {
111 local country_code="$(wireless_get_reg_domain)"
112
113 wireless_set_reg_domain "${country_code}" --no-reset
114 }
115
116 wireless_set_reg_domain() {
117 local country_code
118 local reset="true"
119
120 while [ $# -gt 0 ]; do
121 case "${1}" in
122 --no-reset)
123 reset="false"
124 ;;
125 -*)
126 log ERROR "Ignoring invalid option: ${1}"
127 ;;
128 *)
129 country_code="${1}"
130 ;;
131 esac
132 shift
133 done
134
135 # Check if configuration value is valid
136 if ! wireless_valid_reg_domain "${country_code}"; then
137 log ERROR "Invalid wireless regulatory domain: ${country_code}"
138 return ${EXIT_ERROR}
139 fi
140
141 # Before the wireless reg domain is set, it helps to reset to 00 first.
142 if enabled reset; then
143 iw reg set 00 &>/dev/null
144 fi
145
146 log INFO "Setting wireless regulatory domain country to '${country_code}'"
147 iw reg set "${country_code}"
148 }
149
150 wireless_valid_reg_domain() {
151 local country_code="${1}"
152
153 # Empty country codes are invalid
154 isset country_code || return ${EXIT_FALSE}
155
156 local valid_country_codes="$(wireless_list_reg_domains)"
157
158 if list_match "${country_code}" ${valid_country_codes}; then
159 return ${EXIT_TRUE}
160 fi
161
162 return ${EXIT_FALSE}
163 }
164
165 wireless_list_reg_domains() {
166 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
167 log ERROR "Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
168 return ${EXIT_ERROR}
169 fi
170
171 local line
172 while read line; do
173 # Check if line starts with "country"
174 [ "${line:0:7}" = "country" ] || continue
175
176 # Print country code
177 print "${line:8:2}"
178 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
179 }
180
181 wireless_channel_to_frequency() {
182 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
183
184 local channel=${1}
185 assert isset channel
186
187 # Channel number must be positive.
188 assert [ "${channel}" -gt 0 ]
189
190 # 2.4 GHz band
191 case "${channel}" in
192 [123456789]|1[0123])
193 print "$(( 2407 + (${channel} * 5)))"
194 return ${EXIT_OK}
195 ;;
196 14)
197 print "2484"
198 return ${EXIT_OK}
199 ;;
200 esac
201
202 # 5 GHz band
203 case "${channel}" in
204 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
205 print "$(( 5000 + (${channel} * 5)))"
206 return ${EXIT_OK}
207 ;;
208 esac
209
210 return ${EXIT_ERROR}
211 }
212
213 wireless_set_channel() {
214 local device=${1}
215 assert isset device
216
217 local channel=${2}
218 assert isset channel
219
220 device_exists ${device} || return ${EXIT_ERROR}
221
222 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
223 cmd_quiet iw dev ${device} set channel ${channel}
224 }
225
226 wireless_ibss_join() {
227 local device=${1}
228 assert isset device
229 shift
230
231 local bssid
232 local essid
233 local frequency
234
235 while [ $# -gt 0 ]; do
236 case "${1}" in
237 --bssid=*)
238 bssid="$(cli_get_val ${1})"
239 ;;
240 --essid=*)
241 essid="$(cli_get_val ${1})"
242 ;;
243 --channel=*)
244 local channel="$(cli_get_val ${1})"
245
246 # Save the frequency of the channel instead
247 # of the channel itself.
248 if isset channel; then
249 frequency="$(wireless_channel_to_frequency ${channel})"
250 fi
251 ;;
252 esac
253 shift
254 done
255
256 # Check input.
257 assert ismac bssid
258 assert isset essid
259 assert isinteger frequency
260
261 # Set device up.
262 device_set_up "${device}"
263
264 log INFO "${device} joining ibss network: ${essid} (${bssid})"
265 cmd_quiet iw dev "${device}" ibss join "${essid}" \
266 "${frequency}" fixed-freq "${bssid}"
267 }
268
269 wireless_ibss_leave() {
270 local device=${1}
271 assert isset device
272
273 log INFO "${device} leaving ibss network"
274 cmd_quiet iw dev "${device}" ibss leave
275 }
276
277 wireless_is_radar_frequency() {
278 local frequency="${1}"
279 assert isset frequency
280
281 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
282 }
283
284 wireless_monitor() {
285 local device="${1}"
286 assert isset device
287 shift
288
289 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
290
291 # Create an 802.11 monitoring device
292 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
293 local ret=$?
294
295 case "${ret}" in
296 0)
297 # Bring up the device
298 device_set_up "${monitor_device}"
299
300 # Starting tcpdump
301 tcpdump -i "${monitor_device}" "$@"
302
303 # Remove the monitoring interface.
304 wireless_remove "${monitor_device}"
305 ;;
306
307 *)
308 log ERROR "Could not create a monitoring interface on ${device}"
309 return ${EXIT_ERROR}
310 ;;
311 esac
312
313 return ${EXIT_OK}
314 }