]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/ports/wireless-ap
Rename all "config" to "settings"
[people/stevee/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
31670741 24HOOK_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL MODE PHY SSID"
25e32463 25HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION KEY"
d76f5107
MT
26
27ADDRESS=$(mac_generate)
28BROADCAST_SSID=on
29CHANNEL=1
25e32463
MT
30ENCRYPTION=""
31KEY=""
d76f5107
MT
32MODE="g"
33SSID=
34
2181765d 35function hook_check() {
d76f5107
MT
36 assert isset ADDRESS
37 assert ismac ADDRESS
38 assert isset BROADCAST_SSID
39 assert isbool BROADCAST_SSID
40 assert isset CHANNEL
d76f5107 41 assert isset MODE
93d614f0 42 assert isoneof MODE a b g n
d76f5107
MT
43 assert isset PHY
44 assert ismac PHY
45 assert isset SSID
25e32463
MT
46
47 if isset ENCRYPTION; then
48 assert isoneof ENCRYPTION WPA WPA2 WPA/WPA2
49
50 assert isset KEY
51 assert [ ${#KEY} -ge 8 ]
52 assert [ ${#KEY} -le 63 ]
53 fi
d76f5107
MT
54}
55
2181765d 56function hook_create() {
d76f5107
MT
57 while [ $# -gt 0 ]; do
58 case "${1}" in
59 --broadcast-ssid=*)
60 BROADCAST_SSID=$(cli_get_val ${1})
61 ;;
62 --channel=*)
63 CHANNEL=$(cli_get_val ${1})
64 ;;
25e32463
MT
65 --encryption=*)
66 ENCRYPTION=$(cli_get_val ${1})
67 ;;
68 --key=*)
69 KEY=$(cli_get_val ${1})
70 ;;
d76f5107
MT
71 --mac=*)
72 ADDRESS=$(cli_get_val ${1})
73 ;;
74 --mode=*)
75 MODE=$(cli_get_val ${1})
76 ;;
77 --phy=*)
78 PHY=$(cli_get_val ${1})
79 ;;
80 --ssid=*)
81 SSID=$(cli_get_val ${1})
82 ;;
83 *)
84 warning "Ignoring unknown argument '${1}'"
85 ;;
86 esac
87 shift
88 done
89
90 # Save address of phy do identify it again
91 PHY=$(phy_get ${PHY})
92 PHY=$(phy_get_address ${PHY})
93
8ee92277 94 local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
d76f5107
MT
95 assert isset port
96
e9df08ad 97 port_settings_write "${port}" ${HOOK_SETTINGS}
d76f5107
MT
98
99 exit ${EXIT_OK}
100}
101
2181765d 102function hook_edit() {
d76f5107
MT
103 local port=${1}
104 shift
105
106 assert isset port
107
e9df08ad 108 port_settings_read "${port}" ${HOOK_SETTINGS}
d76f5107
MT
109
110 while [ $# -gt 0 ]; do
111 case "${1}" in
112 --broadcast-ssid=*)
113 BROADCAST_SSID=$(cli_get_val ${1})
114 ;;
115 --channel=*)
116 CHANNEL=$(cli_get_val ${1})
117 ;;
25e32463
MT
118 --encryption=*)
119 ENCRYPTION=$(cli_get_val ${1})
120 ;;
121 --key=*)
122 KEY=$(cli_get_val ${1})
123 ;;
d76f5107
MT
124 --ssid=*)
125 SSID=$(cli_get_val ${1})
126 ;;
127 --mode=*)
128 MODE=$(cli_get_val ${1})
129 ;;
130 *)
131 warning "Unknown argument '${1}'"
132 ;;
133 esac
134 shift
135 done
136
e9df08ad 137 port_settings_write "${port}" ${HOOK_SETTINGS}
d76f5107
MT
138
139 exit ${EXIT_OK}
140}
141
2181765d 142function hook_up() {
d76f5107 143 local port=${1}
d76f5107
MT
144 assert isset port
145
e9df08ad 146 port_settings_read "${port}" ${HOOK_SETTINGS}
d76f5107 147
49ec20d8
MT
148 # Check if the PHY is present.
149 local phy=$(phy_get ${PHY})
150 if ! isset phy; then
151 log DEBUG "phy '${PHY}' is not present"
152 exit ${EXIT_ERROR}
153 fi
154
155 # Create the wireless device, if it does not exist, yet.
d76f5107 156 if ! device_exists ${port}; then
49ec20d8
MT
157 wireless_create ${port} --phy="${phy}" --type="ap" \
158 --address="${ADDRESS}"
d76f5107
MT
159 fi
160
49ec20d8
MT
161 # Start the hostapd service.
162 hostapd_start ${port}
163 local ret=$?
164
165 if [ ${ret} -ne ${EXIT_OK} ]; then
166 log ERROR "Could not start hostapd on port '${port}': ${ret}"
167 exit ${EXIT_ERROR}
d76f5107
MT
168 fi
169
170 exit ${EXIT_OK}
171}
172
2181765d 173function hook_down() {
d76f5107 174 local port=${1}
d76f5107
MT
175 assert isset port
176
47859d95 177 # Stop the hostapd daemon.
d76f5107 178 hostapd_stop ${port}
47859d95
MT
179
180 # Remove the device if it is still present.
181 if device_exists ${port}; then
182 wireless_remove ${port}
183 fi
d76f5107
MT
184
185 exit ${EXIT_OK}
186}
187
2181765d 188function hook_hotplug() {
47859d95 189 local port=${1}
47859d95 190 assert isset port
49ec20d8
MT
191
192 local phy=${2}
47859d95 193 assert isset phy
49ec20d8 194
47859d95
MT
195 assert port_exists ${port}
196
197 # Read configuration of port.
e9df08ad 198 port_settings_read "${port}" ${HOOK_SETTINGS}
47859d95
MT
199
200 # Get the address of the phy.
201 local phy_address=$(phy_get_address ${phy})
202
203 # Check if the phy is the same we have
204 # read from the configuration file.
205 if [ "${PHY}" = "${phy_address}" ]; then
49ec20d8
MT
206 wireless_create ${port} --phy="${phy_address}" --type="ap" \
207 --address="${ADDRESS}"
47859d95
MT
208 fi
209
210 exit ${EXIT_OK}
211}