]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.wireless
ports: Drop unused and complicated info function
[people/ms/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_frequency_to_channel() {
214 local frequency=${1}
215
216 assert isinteger frequency
217
218 # Everything that is too high
219 if [ ${frequency} -gt 5825 ]; then
220 return ${EXIT_ERROR}
221
222 # 5 GHz Band
223 elif [ ${frequency} -gt 5000 ]; then
224 (( frequency = frequency - 5000 ))
225
226 # Must be divisible by 5
227 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
228
229 print "$(( frequency / 5 ))"
230
231 # 2.4 GHz Band - Channel 14
232 elif [ ${frequency} -eq 2484 ]; then
233 print "14"
234
235 # 2.4 GHz Band
236 elif [ ${frequency} -gt 2407 ]; then
237 (( frequency = frequency - 2407 ))
238
239 # Must be divisible by 5
240 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
241
242 print "$(( frequency / 5 ))"
243
244 # Everything else
245 else
246 return ${EXIT_ERROR}
247 fi
248
249 return ${EXIT_OK}
250 }
251
252 wireless_set_channel() {
253 local device=${1}
254 assert isset device
255
256 local channel=${2}
257 assert isset channel
258
259 device_exists ${device} || return ${EXIT_ERROR}
260
261 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
262 cmd_quiet iw dev ${device} set channel ${channel}
263 }
264
265 wireless_ibss_join() {
266 local device=${1}
267 assert isset device
268 shift
269
270 local bssid
271 local essid
272 local frequency
273
274 while [ $# -gt 0 ]; do
275 case "${1}" in
276 --bssid=*)
277 bssid="$(cli_get_val ${1})"
278 ;;
279 --essid=*)
280 essid="$(cli_get_val ${1})"
281 ;;
282 --channel=*)
283 local channel="$(cli_get_val ${1})"
284
285 # Save the frequency of the channel instead
286 # of the channel itself.
287 if isset channel; then
288 frequency="$(wireless_channel_to_frequency ${channel})"
289 fi
290 ;;
291 esac
292 shift
293 done
294
295 # Check input.
296 assert ismac bssid
297 assert isset essid
298 assert isinteger frequency
299
300 # Set device up.
301 device_set_up "${device}"
302
303 log INFO "${device} joining ibss network: ${essid} (${bssid})"
304 cmd_quiet iw dev "${device}" ibss join "${essid}" \
305 "${frequency}" fixed-freq "${bssid}"
306 }
307
308 wireless_ibss_leave() {
309 local device=${1}
310 assert isset device
311
312 log INFO "${device} leaving ibss network"
313 cmd_quiet iw dev "${device}" ibss leave
314 }
315
316 wireless_is_radar_frequency() {
317 local frequency="${1}"
318 assert isset frequency
319
320 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
321 }
322
323 wireless_monitor() {
324 local device="${1}"
325 assert isset device
326 shift
327
328 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
329
330 # Create an 802.11 monitoring device
331 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
332 local ret=$?
333
334 case "${ret}" in
335 0)
336 # Bring up the device
337 device_set_up "${monitor_device}"
338
339 # Starting tcpdump
340 tcpdump -i "${monitor_device}" "$@"
341
342 # Remove the monitoring interface.
343 wireless_remove "${monitor_device}"
344 ;;
345
346 *)
347 log ERROR "Could not create a monitoring interface on ${device}"
348 return ${EXIT_ERROR}
349 ;;
350 esac
351
352 return ${EXIT_OK}
353 }