]>
Commit | Line | Data |
---|---|---|
d76f5107 | 1 | #!/bin/bash |
1578dae9 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
0e035311 | 5 | # Copyright (C) 2012 IPFire Network Development Team # |
1578dae9 MT |
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 | ############################################################################### | |
d76f5107 | 21 | |
31670741 MT |
22 | # Sets the global wireless country code. Default is 00 = world. |
23 | WIRELESS_REGULATORY_DOMAIN="00" | |
e9df08ad | 24 | NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN" |
31670741 | 25 | |
ab9e0fd0 MT |
26 | WIRELESS_REGULATORY_DOMAIN_DATABASE="/usr/lib/crda/regulatory.bin" |
27 | ||
1c6a4e30 | 28 | wireless_create() { |
d76f5107 | 29 | local device=${1} |
d76f5107 | 30 | assert isset device |
22a61046 | 31 | shift |
d76f5107 | 32 | |
22a61046 | 33 | local address |
9170fc35 | 34 | local channel |
22a61046 MT |
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 | ;; | |
9170fc35 MT |
43 | --channel=*) |
44 | channel=$(cli_get_val "${1}") | |
45 | ;; | |
22a61046 MT |
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 | |
d76f5107 | 63 | |
be5ddfb5 MT |
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 | ||
22a61046 MT |
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=$? | |
d76f5107 | 82 | |
22a61046 MT |
83 | if [ ${ret} -eq ${EXIT_OK} ]; then |
84 | log DEBUG "created wireless device '${device}' (${type})" | |
d76f5107 | 85 | |
22a61046 MT |
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}" | |
d76f5107 MT |
91 | fi |
92 | ||
9170fc35 MT |
93 | # Set the channel |
94 | if isset channel; then | |
95 | wireless_set_channel "${device}" "${channel}" || return $? | |
96 | fi | |
97 | ||
22a61046 | 98 | return ${ret} |
d76f5107 MT |
99 | } |
100 | ||
1c6a4e30 | 101 | wireless_remove() { |
d76f5107 | 102 | local device=${1} |
22a61046 | 103 | assert isset device |
d76f5107 | 104 | |
22a61046 MT |
105 | if ! device_exists ${device}; then |
106 | return ${EXIT_OK} | |
107 | fi | |
d76f5107 | 108 | |
22a61046 | 109 | # Tear down the device (if necessary). |
d76f5107 MT |
110 | device_set_down ${device} |
111 | ||
22a61046 MT |
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} | |
d76f5107 MT |
123 | } |
124 | ||
1c6a4e30 | 125 | wireless_get_reg_domain() { |
31670741 MT |
126 | # Returns the country code for the wireless device. |
127 | # Defaults to 00 = world if unset. | |
128 | print "${WIRELESS_REGULATORY_DOMAIN:-00}" | |
129 | } | |
130 | ||
1c6a4e30 | 131 | wireless_init_reg_domain() { |
31670741 MT |
132 | local country_code="$(wireless_get_reg_domain)" |
133 | ||
06a6f01e | 134 | wireless_set_reg_domain "${country_code}" --no-reset |
31670741 MT |
135 | } |
136 | ||
1c6a4e30 | 137 | wireless_set_reg_domain() { |
06a6f01e MT |
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 | ||
ab9e0fd0 MT |
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 | |
31670741 MT |
161 | |
162 | # Before the wireless reg domain is set, it helps to reset to 00 first. | |
06a6f01e MT |
163 | if enabled reset; then |
164 | iw reg set 00 &>/dev/null | |
165 | fi | |
31670741 MT |
166 | |
167 | log INFO "Setting wireless regulatory domain country to '${country_code}'" | |
168 | iw reg set "${country_code}" | |
169 | } | |
170 | ||
ab9e0fd0 MT |
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 | ||
f8b7f135 | 202 | # http://en.wikipedia.org/wiki/List_of_WLAN_channels |
1c6a4e30 | 203 | wireless_channel_to_frequency() { |
91987cc5 | 204 | local channel=${1} |
91987cc5 | 205 | |
f8b7f135 MT |
206 | # Works only for valid channel numbers |
207 | if ! wireless_channel_is_valid "${channel}"; then | |
208 | log ERROR "Invalid wireless channel: ${channel}" | |
209 | return ${EXIT_ERROR} | |
210 | fi | |
91987cc5 MT |
211 | |
212 | # 2.4 GHz band | |
213 | case "${channel}" in | |
214 | [123456789]|1[0123]) | |
215 | print "$(( 2407 + (${channel} * 5)))" | |
216 | return ${EXIT_OK} | |
217 | ;; | |
218 | 14) | |
219 | print "2484" | |
220 | return ${EXIT_OK} | |
221 | ;; | |
222 | esac | |
223 | ||
224 | # 5 GHz band | |
225 | case "${channel}" in | |
226 | 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15]) | |
227 | print "$(( 5000 + (${channel} * 5)))" | |
228 | return ${EXIT_OK} | |
229 | ;; | |
230 | esac | |
231 | ||
232 | return ${EXIT_ERROR} | |
233 | } | |
234 | ||
97877f23 MT |
235 | wireless_frequency_to_channel() { |
236 | local frequency=${1} | |
237 | ||
238 | assert isinteger frequency | |
239 | ||
240 | # Everything that is too high | |
241 | if [ ${frequency} -gt 5825 ]; then | |
242 | return ${EXIT_ERROR} | |
243 | ||
244 | # 5 GHz Band | |
245 | elif [ ${frequency} -gt 5000 ]; then | |
246 | (( frequency = frequency - 5000 )) | |
247 | ||
248 | # Must be divisible by 5 | |
249 | [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR} | |
250 | ||
251 | print "$(( frequency / 5 ))" | |
252 | ||
253 | # 2.4 GHz Band - Channel 14 | |
254 | elif [ ${frequency} -eq 2484 ]; then | |
255 | print "14" | |
256 | ||
257 | # 2.4 GHz Band | |
258 | elif [ ${frequency} -gt 2407 ]; then | |
259 | (( frequency = frequency - 2407 )) | |
260 | ||
261 | # Must be divisible by 5 | |
262 | [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR} | |
263 | ||
264 | print "$(( frequency / 5 ))" | |
265 | ||
266 | # Everything else | |
267 | else | |
268 | return ${EXIT_ERROR} | |
269 | fi | |
270 | ||
271 | return ${EXIT_OK} | |
272 | } | |
273 | ||
f8b7f135 MT |
274 | wireless_channel_is_valid() { |
275 | local channel=${1} | |
276 | ||
277 | case "${channel}" in | |
278 | # 2.4 GHz Band | |
279 | [123456789]|1[0123]|14) | |
280 | return ${EXIT_TRUE} | |
281 | ;; | |
282 | ||
283 | # 5 GHz Band | |
284 | 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15]) | |
285 | return ${EXIT_TRUE} | |
286 | ;; | |
287 | esac | |
288 | ||
289 | # Invalid channel number given | |
290 | return ${EXIT_FALSE} | |
291 | } | |
292 | ||
1c6a4e30 | 293 | wireless_set_channel() { |
d76f5107 | 294 | local device=${1} |
22a61046 | 295 | local channel=${2} |
d76f5107 | 296 | |
f8b7f135 MT |
297 | # Check if the device exists |
298 | if ! device_exists "${device}"; then | |
299 | log ERROR "No such device: ${device}" | |
300 | return ${EXIT_ERROR} | |
301 | fi | |
302 | ||
303 | # Check if the channel number is valid | |
304 | if ! wireless_channel_is_valid "${channel}"; then | |
305 | log ERROR "Invalid wireless channel: ${channel}" | |
306 | return ${EXIT_ERROR} | |
307 | fi | |
22a61046 | 308 | |
5a38ea84 | 309 | log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'" |
f8b7f135 | 310 | cmd iw dev "${device}" set channel "${channel}" |
d76f5107 | 311 | } |
91987cc5 | 312 | |
1c6a4e30 | 313 | wireless_ibss_join() { |
91987cc5 MT |
314 | local device=${1} |
315 | assert isset device | |
316 | shift | |
317 | ||
318 | local bssid | |
319 | local essid | |
320 | local frequency | |
321 | ||
322 | while [ $# -gt 0 ]; do | |
323 | case "${1}" in | |
324 | --bssid=*) | |
325 | bssid="$(cli_get_val ${1})" | |
326 | ;; | |
327 | --essid=*) | |
328 | essid="$(cli_get_val ${1})" | |
329 | ;; | |
330 | --channel=*) | |
331 | local channel="$(cli_get_val ${1})" | |
332 | ||
333 | # Save the frequency of the channel instead | |
334 | # of the channel itself. | |
335 | if isset channel; then | |
336 | frequency="$(wireless_channel_to_frequency ${channel})" | |
337 | fi | |
338 | ;; | |
339 | esac | |
340 | shift | |
341 | done | |
342 | ||
343 | # Check input. | |
344 | assert ismac bssid | |
345 | assert isset essid | |
346 | assert isinteger frequency | |
347 | ||
348 | # Set device up. | |
349 | device_set_up "${device}" | |
350 | ||
351 | log INFO "${device} joining ibss network: ${essid} (${bssid})" | |
352 | cmd_quiet iw dev "${device}" ibss join "${essid}" \ | |
353 | "${frequency}" fixed-freq "${bssid}" | |
354 | } | |
355 | ||
1c6a4e30 | 356 | wireless_ibss_leave() { |
91987cc5 MT |
357 | local device=${1} |
358 | assert isset device | |
359 | ||
360 | log INFO "${device} leaving ibss network" | |
361 | cmd_quiet iw dev "${device}" ibss leave | |
362 | } | |
646ae5b2 | 363 | |
1c6a4e30 | 364 | wireless_is_radar_frequency() { |
646ae5b2 MT |
365 | local frequency="${1}" |
366 | assert isset frequency | |
367 | ||
368 | [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]] | |
369 | } | |
5a38ea84 | 370 | |
1c6a4e30 | 371 | wireless_monitor() { |
5a38ea84 MT |
372 | local device="${1}" |
373 | assert isset device | |
374 | shift | |
375 | ||
a23fdc0e | 376 | local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")" |
5a38ea84 MT |
377 | |
378 | # Create an 802.11 monitoring device | |
379 | wireless_create "${monitor_device}" --phy="${device}" --type="monitor" | |
380 | local ret=$? | |
381 | ||
382 | case "${ret}" in | |
383 | 0) | |
384 | # Bring up the device | |
385 | device_set_up "${monitor_device}" | |
386 | ||
387 | # Starting tcpdump | |
388 | tcpdump -i "${monitor_device}" "$@" | |
389 | ||
390 | # Remove the monitoring interface. | |
391 | wireless_remove "${monitor_device}" | |
392 | ;; | |
393 | ||
394 | *) | |
395 | log ERROR "Could not create a monitoring interface on ${device}" | |
396 | return ${EXIT_ERROR} | |
397 | ;; | |
398 | esac | |
399 | ||
400 | return ${EXIT_OK} | |
401 | } |