]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wireless
wireless monitor: Use proper port name
[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 function wireless_create() {
27 local device=${1}
28 assert isset device
29 shift
30
31 local address
32 local phy
33 local type="managed"
34
35 while [ $# -gt 0 ]; do
36 case "${1}" in
37 --address=*)
38 address=$(cli_get_val ${1})
39 ;;
40 --phy=*)
41 phy=$(cli_get_val ${1})
42 phy=$(phy_get ${phy})
43 ;;
44 --type=*)
45 type=$(cli_get_val ${1})
46
47 # ap --> __ap
48 [ "${type}" = "ap" ] && type="__ap"
49 ;;
50 *)
51 error "Unrecognized argument: ${1}"
52 return ${EXIT_ERROR}
53 ;;
54 esac
55 shift
56 done
57
58 assert isoneof type ibss managed monitor __ap
59 assert phy_exists ${phy}
60 isset address || address=$(mac_generate)
61
62 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
63 local ret=$?
64
65 if [ ${ret} -eq ${EXIT_OK} ]; then
66 log DEBUG "created wireless device '${device}' (${type})"
67
68 if isset address; then
69 device_set_address ${device} ${address}
70 fi
71 else
72 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
73 fi
74
75 return ${ret}
76 }
77
78 function wireless_remove() {
79 local device=${1}
80 assert isset device
81
82 if ! device_exists ${device}; then
83 return ${EXIT_OK}
84 fi
85
86 # Tear down the device (if necessary).
87 device_set_down ${device}
88
89 # Remove it.
90 cmd_quiet iw dev ${device} del
91 local ret=$?
92
93 if [ ${ret} -eq ${EXIT_OK} ]; then
94 log DEBUG "removed wireless device '${device}'"
95 else
96 log ERROR "could not remove wireless device '${device}': ${ret}"
97 fi
98
99 return ${ret}
100 }
101
102 function wireless_get_reg_domain() {
103 # Returns the country code for the wireless device.
104 # Defaults to 00 = world if unset.
105 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
106 }
107
108 function wireless_init_reg_domain() {
109 local country_code="$(wireless_get_reg_domain)"
110
111 wireless_set_reg_domain "${country_code}" --no-reset
112 }
113
114 function wireless_set_reg_domain() {
115 local country_code
116 local reset="true"
117
118 while [ $# -gt 0 ]; do
119 case "${1}" in
120 --no-reset)
121 reset="false"
122 ;;
123 -*)
124 log ERROR "Ignoring invalid option: ${1}"
125 ;;
126 *)
127 country_code="${1}"
128 ;;
129 esac
130 shift
131 done
132
133 assert isset country_code
134
135 # Before the wireless reg domain is set, it helps to reset to 00 first.
136 if enabled reset; then
137 iw reg set 00 &>/dev/null
138 fi
139
140 log INFO "Setting wireless regulatory domain country to '${country_code}'"
141 iw reg set "${country_code}"
142 }
143
144 function wireless_channel_to_frequency() {
145 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
146
147 local channel=${1}
148 assert isset channel
149
150 # Channel number must be positive.
151 assert [ "${channel}" -gt 0 ]
152
153 # 2.4 GHz band
154 case "${channel}" in
155 [123456789]|1[0123])
156 print "$(( 2407 + (${channel} * 5)))"
157 return ${EXIT_OK}
158 ;;
159 14)
160 print "2484"
161 return ${EXIT_OK}
162 ;;
163 esac
164
165 # 5 GHz band
166 case "${channel}" in
167 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
168 print "$(( 5000 + (${channel} * 5)))"
169 return ${EXIT_OK}
170 ;;
171 esac
172
173 return ${EXIT_ERROR}
174 }
175
176 function wireless_set_channel() {
177 local device=${1}
178 assert isset device
179
180 local channel=${2}
181 assert isset channel
182
183 device_exists ${device} || return ${EXIT_ERROR}
184
185 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
186 cmd_quiet iw dev ${device} set channel ${channel}
187 }
188
189 function wireless_ibss_join() {
190 local device=${1}
191 assert isset device
192 shift
193
194 local bssid
195 local essid
196 local frequency
197
198 while [ $# -gt 0 ]; do
199 case "${1}" in
200 --bssid=*)
201 bssid="$(cli_get_val ${1})"
202 ;;
203 --essid=*)
204 essid="$(cli_get_val ${1})"
205 ;;
206 --channel=*)
207 local channel="$(cli_get_val ${1})"
208
209 # Save the frequency of the channel instead
210 # of the channel itself.
211 if isset channel; then
212 frequency="$(wireless_channel_to_frequency ${channel})"
213 fi
214 ;;
215 esac
216 shift
217 done
218
219 # Check input.
220 assert ismac bssid
221 assert isset essid
222 assert isinteger frequency
223
224 # Set device up.
225 device_set_up "${device}"
226
227 log INFO "${device} joining ibss network: ${essid} (${bssid})"
228 cmd_quiet iw dev "${device}" ibss join "${essid}" \
229 "${frequency}" fixed-freq "${bssid}"
230 }
231
232 function wireless_ibss_leave() {
233 local device=${1}
234 assert isset device
235
236 log INFO "${device} leaving ibss network"
237 cmd_quiet iw dev "${device}" ibss leave
238 }
239
240 function wireless_is_radar_frequency() {
241 local frequency="${1}"
242 assert isset frequency
243
244 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
245 }
246
247 function wireless_monitor() {
248 local device="${1}"
249 assert isset device
250 shift
251
252 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
253
254 # Create an 802.11 monitoring device
255 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
256 local ret=$?
257
258 case "${ret}" in
259 0)
260 # Bring up the device
261 device_set_up "${monitor_device}"
262
263 # Starting tcpdump
264 tcpdump -i "${monitor_device}" "$@"
265
266 # Remove the monitoring interface.
267 wireless_remove "${monitor_device}"
268 ;;
269
270 *)
271 log ERROR "Could not create a monitoring interface on ${device}"
272 return ${EXIT_ERROR}
273 ;;
274 esac
275
276 return ${EXIT_OK}
277 }