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