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