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