]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wireless
Use autotools.
[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 function wireless_create() {
23 local device=${1}
24 assert isset device
25 shift
26
27 local address
28 local phy
29 local type="managed"
30
31 while [ $# -gt 0 ]; do
32 case "${1}" in
33 --address=*)
34 address=$(cli_get_val ${1})
35 ;;
36 --phy=*)
37 phy=$(cli_get_val ${1})
38 phy=$(phy_get ${phy})
39 ;;
40 --type=*)
41 type=$(cli_get_val ${1})
42
43 # ap --> __ap
44 [ "${type}" = "ap" ] && type="__ap"
45 ;;
46 *)
47 error "Unrecognized argument: ${1}"
48 return ${EXIT_ERROR}
49 ;;
50 esac
51 shift
52 done
53
54 assert isoneof type ibss managed __ap
55 assert phy_exists ${phy}
56 isset address || address=$(mac_generate)
57
58 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
59 local ret=$?
60
61 if [ ${ret} -eq ${EXIT_OK} ]; then
62 log DEBUG "created wireless device '${device}' (${type})"
63
64 if isset address; then
65 device_set_address ${device} ${address}
66 fi
67 else
68 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
69 fi
70
71 return ${ret}
72 }
73
74 function wireless_remove() {
75 local device=${1}
76 assert isset device
77
78 if ! device_exists ${device}; then
79 return ${EXIT_OK}
80 fi
81
82 # Tear down the device (if necessary).
83 device_set_down ${device}
84
85 # Remove it.
86 cmd_quiet iw dev ${device} del
87 local ret=$?
88
89 if [ ${ret} -eq ${EXIT_OK} ]; then
90 log DEBUG "removed wireless device '${device}'"
91 else
92 log ERROR "could not remove wireless device '${device}': ${ret}"
93 fi
94
95 return ${ret}
96 }
97
98 function wireless_channel_to_frequency() {
99 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
100
101 local channel=${1}
102 assert isset channel
103
104 # Channel number must be positive.
105 assert [ "${channel}" -gt 0 ]
106
107 # 2.4 GHz band
108 case "${channel}" in
109 [123456789]|1[0123])
110 print "$(( 2407 + (${channel} * 5)))"
111 return ${EXIT_OK}
112 ;;
113 14)
114 print "2484"
115 return ${EXIT_OK}
116 ;;
117 esac
118
119 # 5 GHz band
120 case "${channel}" in
121 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
122 print "$(( 5000 + (${channel} * 5)))"
123 return ${EXIT_OK}
124 ;;
125 esac
126
127 return ${EXIT_ERROR}
128 }
129
130 function wireless_set_channel() {
131 local device=${1}
132 assert isset device
133
134 local channel=${2}
135 assert isset channel
136
137 device_exists ${device} || return ${EXIT_ERROR}
138
139 cmd_quiet iw dev ${device} set channel ${channel}
140 }
141
142 function wireless_ibss_join() {
143 local device=${1}
144 assert isset device
145 shift
146
147 local bssid
148 local essid
149 local frequency
150
151 while [ $# -gt 0 ]; do
152 case "${1}" in
153 --bssid=*)
154 bssid="$(cli_get_val ${1})"
155 ;;
156 --essid=*)
157 essid="$(cli_get_val ${1})"
158 ;;
159 --channel=*)
160 local channel="$(cli_get_val ${1})"
161
162 # Save the frequency of the channel instead
163 # of the channel itself.
164 if isset channel; then
165 frequency="$(wireless_channel_to_frequency ${channel})"
166 fi
167 ;;
168 esac
169 shift
170 done
171
172 # Check input.
173 assert ismac bssid
174 assert isset essid
175 assert isinteger frequency
176
177 # Set device up.
178 device_set_up "${device}"
179
180 log INFO "${device} joining ibss network: ${essid} (${bssid})"
181 cmd_quiet iw dev "${device}" ibss join "${essid}" \
182 "${frequency}" fixed-freq "${bssid}"
183 }
184
185 function wireless_ibss_leave() {
186 local device=${1}
187 assert isset device
188
189 log INFO "${device} leaving ibss network"
190 cmd_quiet iw dev "${device}" ibss leave
191 }
192
193 function wireless_is_radar_frequency() {
194 local frequency="${1}"
195 assert isset frequency
196
197 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
198 }