]>
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 MT |
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 | |
d76f5107 | 59 | |
5a38ea84 | 60 | assert isoneof type ibss managed monitor __ap |
22a61046 MT |
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=$? | |
d76f5107 | 66 | |
22a61046 MT |
67 | if [ ${ret} -eq ${EXIT_OK} ]; then |
68 | log DEBUG "created wireless device '${device}' (${type})" | |
d76f5107 | 69 | |
22a61046 MT |
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}" | |
d76f5107 MT |
75 | fi |
76 | ||
22a61046 | 77 | return ${ret} |
d76f5107 MT |
78 | } |
79 | ||
1c6a4e30 | 80 | wireless_remove() { |
d76f5107 | 81 | local device=${1} |
22a61046 | 82 | assert isset device |
d76f5107 | 83 | |
22a61046 MT |
84 | if ! device_exists ${device}; then |
85 | return ${EXIT_OK} | |
86 | fi | |
d76f5107 | 87 | |
22a61046 | 88 | # Tear down the device (if necessary). |
d76f5107 MT |
89 | device_set_down ${device} |
90 | ||
22a61046 MT |
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} | |
d76f5107 MT |
102 | } |
103 | ||
1c6a4e30 | 104 | wireless_get_reg_domain() { |
31670741 MT |
105 | # Returns the country code for the wireless device. |
106 | # Defaults to 00 = world if unset. | |
107 | print "${WIRELESS_REGULATORY_DOMAIN:-00}" | |
108 | } | |
109 | ||
1c6a4e30 | 110 | wireless_init_reg_domain() { |
31670741 MT |
111 | local country_code="$(wireless_get_reg_domain)" |
112 | ||
06a6f01e | 113 | wireless_set_reg_domain "${country_code}" --no-reset |
31670741 MT |
114 | } |
115 | ||
1c6a4e30 | 116 | wireless_set_reg_domain() { |
06a6f01e MT |
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 | ||
ab9e0fd0 MT |
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 | |
31670741 MT |
140 | |
141 | # Before the wireless reg domain is set, it helps to reset to 00 first. | |
06a6f01e MT |
142 | if enabled reset; then |
143 | iw reg set 00 &>/dev/null | |
144 | fi | |
31670741 MT |
145 | |
146 | log INFO "Setting wireless regulatory domain country to '${country_code}'" | |
147 | iw reg set "${country_code}" | |
148 | } | |
149 | ||
ab9e0fd0 MT |
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 | ||
1c6a4e30 | 181 | wireless_channel_to_frequency() { |
91987cc5 MT |
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 | ||
1c6a4e30 | 213 | wireless_set_channel() { |
d76f5107 | 214 | local device=${1} |
d76f5107 | 215 | assert isset device |
22a61046 MT |
216 | |
217 | local channel=${2} | |
d76f5107 MT |
218 | assert isset channel |
219 | ||
22a61046 MT |
220 | device_exists ${device} || return ${EXIT_ERROR} |
221 | ||
5a38ea84 | 222 | log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'" |
22a61046 | 223 | cmd_quiet iw dev ${device} set channel ${channel} |
d76f5107 | 224 | } |
91987cc5 | 225 | |
1c6a4e30 | 226 | wireless_ibss_join() { |
91987cc5 MT |
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 | ||
1c6a4e30 | 269 | wireless_ibss_leave() { |
91987cc5 MT |
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 | } | |
646ae5b2 | 276 | |
1c6a4e30 | 277 | wireless_is_radar_frequency() { |
646ae5b2 MT |
278 | local frequency="${1}" |
279 | assert isset frequency | |
280 | ||
281 | [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]] | |
282 | } | |
5a38ea84 | 283 | |
1c6a4e30 | 284 | wireless_monitor() { |
5a38ea84 MT |
285 | local device="${1}" |
286 | assert isset device | |
287 | shift | |
288 | ||
a23fdc0e | 289 | local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")" |
5a38ea84 MT |
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 | } |