]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wireless
wireless: Allow creating mesh points
[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 channel
35 local phy
36 local type="managed"
37
38 while [ $# -gt 0 ]; do
39 case "${1}" in
40 --address=*)
41 address=$(cli_get_val ${1})
42 ;;
43 --channel=*)
44 channel=$(cli_get_val "${1}")
45 ;;
46 --phy=*)
47 phy=$(cli_get_val ${1})
48 phy=$(phy_get ${phy})
49 ;;
50 --type=*)
51 type=$(cli_get_val ${1})
52
53 # ap --> __ap
54 [ "${type}" = "ap" ] && type="__ap"
55 ;;
56 *)
57 error "Unrecognized argument: ${1}"
58 return ${EXIT_ERROR}
59 ;;
60 esac
61 shift
62 done
63
64 case "${type}" in
65 ibss|managed|monitor|__ap)
66 ;;
67 mesh-point)
68 type="mp"
69 ;;
70 *)
71 log ERROR "Unknown type: ${type}"
72 return ${EXIT_ERROR}
73 ;;
74
75 esac
76
77 assert phy_exists ${phy}
78 isset address || address=$(mac_generate)
79
80 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
81 local ret=$?
82
83 if [ ${ret} -eq ${EXIT_OK} ]; then
84 log DEBUG "created wireless device '${device}' (${type})"
85
86 if isset address; then
87 device_set_address ${device} ${address}
88 fi
89 else
90 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
91 fi
92
93 # Set the channel
94 if isset channel; then
95 wireless_set_channel "${device}" "${channel}" || return $?
96 fi
97
98 return ${ret}
99 }
100
101 wireless_remove() {
102 local device=${1}
103 assert isset device
104
105 if ! device_exists ${device}; then
106 return ${EXIT_OK}
107 fi
108
109 # Tear down the device (if necessary).
110 device_set_down ${device}
111
112 # Remove it.
113 cmd_quiet iw dev ${device} del
114 local ret=$?
115
116 if [ ${ret} -eq ${EXIT_OK} ]; then
117 log DEBUG "removed wireless device '${device}'"
118 else
119 log ERROR "could not remove wireless device '${device}': ${ret}"
120 fi
121
122 return ${ret}
123 }
124
125 wireless_get_reg_domain() {
126 # Returns the country code for the wireless device.
127 # Defaults to 00 = world if unset.
128 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
129 }
130
131 wireless_init_reg_domain() {
132 local country_code="$(wireless_get_reg_domain)"
133
134 wireless_set_reg_domain "${country_code}" --no-reset
135 }
136
137 wireless_set_reg_domain() {
138 local country_code
139 local reset="true"
140
141 while [ $# -gt 0 ]; do
142 case "${1}" in
143 --no-reset)
144 reset="false"
145 ;;
146 -*)
147 log ERROR "Ignoring invalid option: ${1}"
148 ;;
149 *)
150 country_code="${1}"
151 ;;
152 esac
153 shift
154 done
155
156 # Check if configuration value is valid
157 if ! wireless_valid_reg_domain "${country_code}"; then
158 log ERROR "Invalid wireless regulatory domain: ${country_code}"
159 return ${EXIT_ERROR}
160 fi
161
162 # Before the wireless reg domain is set, it helps to reset to 00 first.
163 if enabled reset; then
164 iw reg set 00 &>/dev/null
165 fi
166
167 log INFO "Setting wireless regulatory domain country to '${country_code}'"
168 iw reg set "${country_code}"
169 }
170
171 wireless_valid_reg_domain() {
172 local country_code="${1}"
173
174 # Empty country codes are invalid
175 isset country_code || return ${EXIT_FALSE}
176
177 local valid_country_codes="$(wireless_list_reg_domains)"
178
179 if list_match "${country_code}" ${valid_country_codes}; then
180 return ${EXIT_TRUE}
181 fi
182
183 return ${EXIT_FALSE}
184 }
185
186 wireless_list_reg_domains() {
187 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
188 log ERROR "Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
189 return ${EXIT_ERROR}
190 fi
191
192 local line
193 while read line; do
194 # Check if line starts with "country"
195 [ "${line:0:7}" = "country" ] || continue
196
197 # Print country code
198 print "${line:8:2}"
199 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
200 }
201
202 wireless_channel_to_frequency() {
203 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
204
205 local channel=${1}
206 assert isset channel
207
208 # Channel number must be positive.
209 assert [ "${channel}" -gt 0 ]
210
211 # 2.4 GHz band
212 case "${channel}" in
213 [123456789]|1[0123])
214 print "$(( 2407 + (${channel} * 5)))"
215 return ${EXIT_OK}
216 ;;
217 14)
218 print "2484"
219 return ${EXIT_OK}
220 ;;
221 esac
222
223 # 5 GHz band
224 case "${channel}" in
225 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
226 print "$(( 5000 + (${channel} * 5)))"
227 return ${EXIT_OK}
228 ;;
229 esac
230
231 return ${EXIT_ERROR}
232 }
233
234 wireless_frequency_to_channel() {
235 local frequency=${1}
236
237 assert isinteger frequency
238
239 # Everything that is too high
240 if [ ${frequency} -gt 5825 ]; then
241 return ${EXIT_ERROR}
242
243 # 5 GHz Band
244 elif [ ${frequency} -gt 5000 ]; then
245 (( frequency = frequency - 5000 ))
246
247 # Must be divisible by 5
248 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
249
250 print "$(( frequency / 5 ))"
251
252 # 2.4 GHz Band - Channel 14
253 elif [ ${frequency} -eq 2484 ]; then
254 print "14"
255
256 # 2.4 GHz Band
257 elif [ ${frequency} -gt 2407 ]; then
258 (( frequency = frequency - 2407 ))
259
260 # Must be divisible by 5
261 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
262
263 print "$(( frequency / 5 ))"
264
265 # Everything else
266 else
267 return ${EXIT_ERROR}
268 fi
269
270 return ${EXIT_OK}
271 }
272
273 wireless_set_channel() {
274 local device=${1}
275 assert isset device
276
277 local channel=${2}
278 assert isset channel
279
280 device_exists ${device} || return ${EXIT_ERROR}
281
282 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
283 cmd_quiet iw dev ${device} set channel ${channel}
284 }
285
286 wireless_ibss_join() {
287 local device=${1}
288 assert isset device
289 shift
290
291 local bssid
292 local essid
293 local frequency
294
295 while [ $# -gt 0 ]; do
296 case "${1}" in
297 --bssid=*)
298 bssid="$(cli_get_val ${1})"
299 ;;
300 --essid=*)
301 essid="$(cli_get_val ${1})"
302 ;;
303 --channel=*)
304 local channel="$(cli_get_val ${1})"
305
306 # Save the frequency of the channel instead
307 # of the channel itself.
308 if isset channel; then
309 frequency="$(wireless_channel_to_frequency ${channel})"
310 fi
311 ;;
312 esac
313 shift
314 done
315
316 # Check input.
317 assert ismac bssid
318 assert isset essid
319 assert isinteger frequency
320
321 # Set device up.
322 device_set_up "${device}"
323
324 log INFO "${device} joining ibss network: ${essid} (${bssid})"
325 cmd_quiet iw dev "${device}" ibss join "${essid}" \
326 "${frequency}" fixed-freq "${bssid}"
327 }
328
329 wireless_ibss_leave() {
330 local device=${1}
331 assert isset device
332
333 log INFO "${device} leaving ibss network"
334 cmd_quiet iw dev "${device}" ibss leave
335 }
336
337 wireless_is_radar_frequency() {
338 local frequency="${1}"
339 assert isset frequency
340
341 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
342 }
343
344 wireless_monitor() {
345 local device="${1}"
346 assert isset device
347 shift
348
349 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
350
351 # Create an 802.11 monitoring device
352 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
353 local ret=$?
354
355 case "${ret}" in
356 0)
357 # Bring up the device
358 device_set_up "${monitor_device}"
359
360 # Starting tcpdump
361 tcpdump -i "${monitor_device}" "$@"
362
363 # Remove the monitoring interface.
364 wireless_remove "${monitor_device}"
365 ;;
366
367 *)
368 log ERROR "Could not create a monitoring interface on ${device}"
369 return ${EXIT_ERROR}
370 ;;
371 esac
372
373 return ${EXIT_OK}
374 }