]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wireless
Make the wireless regulatory domain a global configuration option
[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_CONFIG_FILE_PARAMS="${NETWORK_CONFIG_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN"
25
26 init_register wireless_init_reg_domain
27
28 function wireless_create() {
29 local device=${1}
30 assert isset device
31 shift
32
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
59
60 assert isoneof type ibss managed __ap
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=$?
66
67 if [ ${ret} -eq ${EXIT_OK} ]; then
68 log DEBUG "created wireless device '${device}' (${type})"
69
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}"
75 fi
76
77 return ${ret}
78 }
79
80 function wireless_remove() {
81 local device=${1}
82 assert isset device
83
84 if ! device_exists ${device}; then
85 return ${EXIT_OK}
86 fi
87
88 # Tear down the device (if necessary).
89 device_set_down ${device}
90
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}
102 }
103
104 function wireless_get_reg_domain() {
105 # Returns the country code for the wireless device.
106 # Defaults to 00 = world if unset.
107 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
108 }
109
110 function wireless_init_reg_domain() {
111 local country_code="$(wireless_get_reg_domain)"
112
113 wireless_set_reg_domain "${country_code}"
114 }
115
116 function wireless_set_reg_domain() {
117 local country_code="${1}"
118 assert isset country_code
119
120 # Before the wireless reg domain is set, it helps to reset to 00 first.
121 iw reg set 00 &>/dev/null
122
123 log INFO "Setting wireless regulatory domain country to '${country_code}'"
124 iw reg set "${country_code}"
125 }
126
127 function wireless_channel_to_frequency() {
128 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
129
130 local channel=${1}
131 assert isset channel
132
133 # Channel number must be positive.
134 assert [ "${channel}" -gt 0 ]
135
136 # 2.4 GHz band
137 case "${channel}" in
138 [123456789]|1[0123])
139 print "$(( 2407 + (${channel} * 5)))"
140 return ${EXIT_OK}
141 ;;
142 14)
143 print "2484"
144 return ${EXIT_OK}
145 ;;
146 esac
147
148 # 5 GHz band
149 case "${channel}" in
150 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
151 print "$(( 5000 + (${channel} * 5)))"
152 return ${EXIT_OK}
153 ;;
154 esac
155
156 return ${EXIT_ERROR}
157 }
158
159 function wireless_set_channel() {
160 local device=${1}
161 assert isset device
162
163 local channel=${2}
164 assert isset channel
165
166 device_exists ${device} || return ${EXIT_ERROR}
167
168 cmd_quiet iw dev ${device} set channel ${channel}
169 }
170
171 function wireless_ibss_join() {
172 local device=${1}
173 assert isset device
174 shift
175
176 local bssid
177 local essid
178 local frequency
179
180 while [ $# -gt 0 ]; do
181 case "${1}" in
182 --bssid=*)
183 bssid="$(cli_get_val ${1})"
184 ;;
185 --essid=*)
186 essid="$(cli_get_val ${1})"
187 ;;
188 --channel=*)
189 local channel="$(cli_get_val ${1})"
190
191 # Save the frequency of the channel instead
192 # of the channel itself.
193 if isset channel; then
194 frequency="$(wireless_channel_to_frequency ${channel})"
195 fi
196 ;;
197 esac
198 shift
199 done
200
201 # Check input.
202 assert ismac bssid
203 assert isset essid
204 assert isinteger frequency
205
206 # Set device up.
207 device_set_up "${device}"
208
209 log INFO "${device} joining ibss network: ${essid} (${bssid})"
210 cmd_quiet iw dev "${device}" ibss join "${essid}" \
211 "${frequency}" fixed-freq "${bssid}"
212 }
213
214 function wireless_ibss_leave() {
215 local device=${1}
216 assert isset device
217
218 log INFO "${device} leaving ibss network"
219 cmd_quiet iw dev "${device}" ibss leave
220 }
221
222 function wireless_is_radar_frequency() {
223 local frequency="${1}"
224 assert isset frequency
225
226 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
227 }