]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.wireless
zone: Don't handle hotplug events for zones
[people/stevee/network.git] / src / functions / functions.wireless
CommitLineData
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.
23WIRELESS_REGULATORY_DOMAIN="00"
e9df08ad 24NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN"
31670741 25
d76f5107
MT
26function wireless_create() {
27 local device=${1}
d76f5107 28 assert isset device
22a61046 29 shift
d76f5107 30
22a61046
MT
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
d76f5107 57
5a38ea84 58 assert isoneof type ibss managed monitor __ap
22a61046
MT
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=$?
d76f5107 64
22a61046
MT
65 if [ ${ret} -eq ${EXIT_OK} ]; then
66 log DEBUG "created wireless device '${device}' (${type})"
d76f5107 67
22a61046
MT
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}"
d76f5107
MT
73 fi
74
22a61046 75 return ${ret}
d76f5107
MT
76}
77
78function wireless_remove() {
79 local device=${1}
22a61046 80 assert isset device
d76f5107 81
22a61046
MT
82 if ! device_exists ${device}; then
83 return ${EXIT_OK}
84 fi
d76f5107 85
22a61046 86 # Tear down the device (if necessary).
d76f5107
MT
87 device_set_down ${device}
88
22a61046
MT
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}
d76f5107
MT
100}
101
31670741
MT
102function 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
108function wireless_init_reg_domain() {
109 local country_code="$(wireless_get_reg_domain)"
110
06a6f01e 111 wireless_set_reg_domain "${country_code}" --no-reset
31670741
MT
112}
113
114function wireless_set_reg_domain() {
06a6f01e
MT
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
31670741
MT
133 assert isset country_code
134
135 # Before the wireless reg domain is set, it helps to reset to 00 first.
06a6f01e
MT
136 if enabled reset; then
137 iw reg set 00 &>/dev/null
138 fi
31670741
MT
139
140 log INFO "Setting wireless regulatory domain country to '${country_code}'"
141 iw reg set "${country_code}"
142}
143
91987cc5
MT
144function 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
d76f5107
MT
176function wireless_set_channel() {
177 local device=${1}
d76f5107 178 assert isset device
22a61046
MT
179
180 local channel=${2}
d76f5107
MT
181 assert isset channel
182
22a61046
MT
183 device_exists ${device} || return ${EXIT_ERROR}
184
5a38ea84 185 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
22a61046 186 cmd_quiet iw dev ${device} set channel ${channel}
d76f5107 187}
91987cc5
MT
188
189function 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
232function 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}
646ae5b2
MT
239
240function wireless_is_radar_frequency() {
241 local frequency="${1}"
242 assert isset frequency
243
244 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
245}
5a38ea84
MT
246
247function wireless_monitor() {
248 local device="${1}"
249 assert isset device
250 shift
251
252 local monitor_device="mon$$"
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}