]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.wireless
Configure wireless reg domain when wireless hardware is plugged in
[people/ms/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_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_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 __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 cmd_quiet iw dev ${device} set channel ${channel}
186 }
187
188 function wireless_ibss_join() {
189 local device=${1}
190 assert isset device
191 shift
192
193 local bssid
194 local essid
195 local frequency
196
197 while [ $# -gt 0 ]; do
198 case "${1}" in
199 --bssid=*)
200 bssid="$(cli_get_val ${1})"
201 ;;
202 --essid=*)
203 essid="$(cli_get_val ${1})"
204 ;;
205 --channel=*)
206 local channel="$(cli_get_val ${1})"
207
208 # Save the frequency of the channel instead
209 # of the channel itself.
210 if isset channel; then
211 frequency="$(wireless_channel_to_frequency ${channel})"
212 fi
213 ;;
214 esac
215 shift
216 done
217
218 # Check input.
219 assert ismac bssid
220 assert isset essid
221 assert isinteger frequency
222
223 # Set device up.
224 device_set_up "${device}"
225
226 log INFO "${device} joining ibss network: ${essid} (${bssid})"
227 cmd_quiet iw dev "${device}" ibss join "${essid}" \
228 "${frequency}" fixed-freq "${bssid}"
229 }
230
231 function wireless_ibss_leave() {
232 local device=${1}
233 assert isset device
234
235 log INFO "${device} leaving ibss network"
236 cmd_quiet iw dev "${device}" ibss leave
237 }
238
239 function wireless_is_radar_frequency() {
240 local frequency="${1}"
241 assert isset frequency
242
243 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
244 }