]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/wireless-ap
wireless-ap: Allow setting the wireless environment (indoor/outdoor)
[people/ms/network.git] / src / hooks / ports / wireless-ap
CommitLineData
d76f5107
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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
8ee92277 22. /usr/lib/network/header-port
d76f5107 23
54bae947
MT
24HOOK_PORT_PATTERN="${PORT_PATTERN_ACCESSPOINT}"
25
40c95a6b 26HOOK_SETTINGS="ADDRESS BROADCAST_SSID CHANNEL CHANNEL_BANDWIDTH DFS MODE PHY"
7842c2ce 27HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION ENVIRONMENT KEY SSID"
d76f5107
MT
28
29ADDRESS=$(mac_generate)
30BROADCAST_SSID=on
1b4aa2ca 31CHANNEL=
54094fc7 32CHANNEL_BANDWIDTH=
25e32463
MT
33ENCRYPTION=""
34KEY=""
d76f5107
MT
35SSID=
36
7b297fb2
MT
37# Perform radar detection by default when possible
38DFS="on"
39
7842c2ce
MT
40ENVIRONMENT="${WIRELESS_DEFAULT_ENVIRONMENT}"
41
1c6a4e30 42hook_check_settings() {
d76f5107
MT
43 assert isset ADDRESS
44 assert ismac ADDRESS
45 assert isset BROADCAST_SSID
46 assert isbool BROADCAST_SSID
47 assert isset CHANNEL
7b297fb2 48 assert isbool DFS
d76f5107 49 assert isset MODE
6c262922 50 assert isoneof MODE ${HOSTAPD_SUPPORTED_MODES}
d76f5107
MT
51 assert isset PHY
52 assert ismac PHY
53 assert isset SSID
25e32463
MT
54
55 if isset ENCRYPTION; then
56 assert isoneof ENCRYPTION WPA WPA2 WPA/WPA2
57
58 assert isset KEY
59 assert [ ${#KEY} -ge 8 ]
60 assert [ ${#KEY} -le 63 ]
61 fi
7842c2ce
MT
62
63 assert wireless_environment_is_valid "${ENVIRONMENT}"
d76f5107
MT
64}
65
270aab39 66hook_parse_cmdline() {
d76f5107
MT
67 while [ $# -gt 0 ]; do
68 case "${1}" in
69 --broadcast-ssid=*)
2212045f 70 BROADCAST_SSID=$(cli_get_val "${1}")
d76f5107
MT
71 ;;
72 --channel=*)
2212045f 73 CHANNEL=$(cli_get_val "${1}")
d76f5107 74 ;;
54094fc7
MT
75 --channel-bandwidth=*)
76 CHANNEL_BANDWIDTH="$(cli_get_val "${1}")"
77 ;;
7b297fb2
MT
78 --dfs=*)
79 DFS="$(cli_get_val "${1}")"
80
81 if enabled DFS; then
82 DFS="on"
83 elif disabled DFS; then
84 DFS="off"
85 else
86 error "Invalid value for DFS: ${DFS}"
87 return ${EXIT_ERROR}
88 fi
89 ;;
25e32463 90 --encryption=*)
2212045f 91 ENCRYPTION=$(cli_get_val "${1}")
25e32463 92 ;;
7842c2ce
MT
93 --environment=*)
94 ENVIRONMENT="$(cli_get_val "${1}")"
95
96 if ! wireless_environment_is_valid "${ENVIRONMENT}"; then
97 error "Invalid wireless environment: ${ENVIRONMENT}"
98 return ${EXIT_ERROR}
99 fi
100 ;;
25e32463 101 --key=*)
2212045f 102 KEY=$(cli_get_val "${1}")
25e32463 103 ;;
d76f5107 104 --mac=*)
2212045f 105 ADDRESS=$(cli_get_val "${1}")
d76f5107
MT
106 ;;
107 --mode=*)
2212045f 108 MODE=$(cli_get_val "${1}")
6c262922
MT
109
110 if ! isoneof MODE ${HOSTAPD_SUPPORTED_MODES}; then
111 error "Unsupported mode: ${MODE}"
112 error "Mode must be one of ${HOSTAPD_SUPPORTED_MODES}"
113 return ${EXIT_ERROR}
114 fi
d76f5107
MT
115 ;;
116 --phy=*)
2212045f 117 PHY=$(cli_get_val "${1}")
d76f5107
MT
118 ;;
119 --ssid=*)
2212045f 120 SSID=$(cli_get_val "${1}")
d76f5107
MT
121 ;;
122 *)
123 warning "Ignoring unknown argument '${1}'"
124 ;;
125 esac
126 shift
127 done
128
8578e61d
MT
129 # Generate a random MAC address if none is set
130 if ! isset ADDRESS; then
131 ADDRESS="$(mac_generate)"
132 fi
133
6c262922
MT
134 # MODE must be set
135 if ! isset MODE; then
136 error "--mode is not set"
137 return ${EXIT_ERROR}
138 fi
139
1b4aa2ca
MT
140 # Automatically enable ACS if no channel is set and ACS is available
141 if ! isset CHANNEL && phy_supports_acs "${PHY}"; then
142 CHANNEL="0"
143
144 log INFO "Automatic Channel Selection (ACS) enabled"
145 fi
146
54094fc7
MT
147 # Channel bandwidth must match the mode
148 if isset CHANNEL_BANDWIDTH && ! wireless_channel_bandwidth_is_valid "${MODE}" "${CHANNEL_BANDWIDTH}"; then
f9e980d9 149 error "Channel Bandwidth '${CHANNEL_BANDWIDTH}' is not supported for ${MODE}"
54094fc7
MT
150 return ${EXIT_ERROR}
151 fi
152
d76f5107
MT
153 # Save address of phy do identify it again
154 PHY=$(phy_get ${PHY})
155 PHY=$(phy_get_address ${PHY})
270aab39
MT
156}
157
1c6a4e30 158hook_edit() {
d76f5107 159 local port=${1}
d76f5107
MT
160 assert isset port
161
2212045f 162 if ! hook_default_edit "$@"; then
270aab39
MT
163 return ${EXIT_ERROR}
164 fi
d76f5107 165
270aab39
MT
166 # To apply all changes, we need to restart the port
167 port_restart "${port}"
d76f5107
MT
168}
169
1c6a4e30 170hook_create() {
1ba6a2bb 171 local port="${1}"
d76f5107
MT
172 assert isset port
173
1ba6a2bb
MT
174 device_exists "${port}" && exit ${EXIT_OK}
175
e9df08ad 176 port_settings_read "${port}" ${HOOK_SETTINGS}
d76f5107 177
49ec20d8
MT
178 # Check if the PHY is present.
179 local phy=$(phy_get ${PHY})
180 if ! isset phy; then
181 log DEBUG "phy '${PHY}' is not present"
182 exit ${EXIT_ERROR}
183 fi
184
1ba6a2bb
MT
185 # Create the wireless device
186 wireless_create "${port}" \
187 --phy="${phy}" \
188 --type="ap" \
189 --address="${ADDRESS}"
d76f5107
MT
190
191 exit ${EXIT_OK}
192}
193
1c6a4e30 194hook_remove() {
1ba6a2bb 195 local port="${1}"
d76f5107
MT
196 assert isset port
197
b8026986
MT
198 # Remove the device if present
199 if device_exists "${port}"; then
200 wireless_remove "${port}"
47859d95 201 fi
d76f5107
MT
202
203 exit ${EXIT_OK}
204}
205
1c6a4e30 206hook_up() {
1ba6a2bb
MT
207 local port="${1}"
208 assert isset port
209
210 # The port must already exist before
211 # hostapd is started. Otherwise it will
212 # fail horribly over and over again.
213 assert device_exists "${port}"
214
215 hostapd_start "${port}"
216}
217
1c6a4e30 218hook_down() {
1ba6a2bb
MT
219 local port="${1}"
220 assert isset port
221
222 hostapd_stop "${port}"
223}
224
1c6a4e30 225hook_hotplug() {
b8026986 226 local port="${1}"
47859d95 227 assert isset port
49ec20d8 228
b8026986
MT
229 case "$(hotplug_action)" in
230 add)
1ba6a2bb
MT
231 # Create the port when the phy is plugged in
232 if hotplug_event_port_uses_phy "${port}"; then
233 hook_create "${port}"
b8026986
MT
234 fi
235 ;;
236
237 remove)
238 # Stop hostapd
239 if hotplug_event_port_is_interface "${port}"; then
240 hostapd_stop "${port}"
b8026986 241
1ba6a2bb
MT
242 exit ${EXIT_OK}
243 fi
b8026986
MT
244 ;;
245 esac
47859d95 246
1ba6a2bb 247 exit ${EXIT_NOT_HANDLED}
47859d95 248}