]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/ports/wireless-ap
modem: Actually stop pppd.
[people/stevee/network.git] / 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
fee7200e 24HOOK_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL COUNTRY_CODE MODE PHY SSID"
25e32463 25HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION KEY"
d76f5107
MT
26
27ADDRESS=$(mac_generate)
28BROADCAST_SSID=on
29CHANNEL=1
30COUNTRY_CODE="US"
25e32463
MT
31ENCRYPTION=""
32KEY=""
d76f5107
MT
33MODE="g"
34SSID=
35
36function _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
93d614f0 44 assert isoneof MODE a b g n
d76f5107
MT
45 assert isset PHY
46 assert ismac PHY
47 assert isset SSID
25e32463
MT
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
d76f5107
MT
56}
57
58function _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 ;;
25e32463
MT
70 --encryption=*)
71 ENCRYPTION=$(cli_get_val ${1})
72 ;;
73 --key=*)
74 KEY=$(cli_get_val ${1})
75 ;;
d76f5107
MT
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
8ee92277 99 local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
d76f5107
MT
100 assert isset port
101
102 config_write $(port_file ${port}) ${HOOK_SETTINGS}
103
104 exit ${EXIT_OK}
105}
106
107function _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 ;;
25e32463
MT
126 --encryption=*)
127 ENCRYPTION=$(cli_get_val ${1})
128 ;;
129 --key=*)
130 KEY=$(cli_get_val ${1})
131 ;;
d76f5107
MT
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
150function _up() {
151 local port=${1}
152
153 assert isset port
154
155 config_read $(port_file ${port})
156
157 if ! device_exists ${port}; then
158 wireless_create ${port} ${PHY} __ap ${ADDRESS}
159 fi
160
161 if ! hostapd_is_running ${port}; then
162 hostapd_start ${port} \
163 --broadcast-ssid="${BROADCAST_SSID}" \
164 --channel="${CHANNEL}" \
165 --country-code="${COUNTRY_CODE}" \
25e32463
MT
166 --encryption="${ENCRYPTION}" \
167 --key="${KEY}" \
d76f5107
MT
168 --mode="${MODE}" \
169 --ssid="${SSID}"
170
171 local ret=$?
172
173 if [ ${ret} -eq ${EXIT_ERROR} ]; then
174 error_log "Could not start '${port}' because hostapd crashed previously."
175 ( _down ${port} )
176 exit ${EXIT_ERROR}
177 fi
178 fi
179
180 exit ${EXIT_OK}
181}
182
183function _down() {
184 local port=${1}
d76f5107
MT
185 assert isset port
186
187 config_read $(port_file ${port})
188
47859d95 189 # Stop the hostapd daemon.
d76f5107 190 hostapd_stop ${port}
47859d95
MT
191
192 # Remove the device if it is still present.
193 if device_exists ${port}; then
194 wireless_remove ${port}
195 fi
d76f5107
MT
196
197 exit ${EXIT_OK}
198}
199
47859d95
MT
200function _hotplug() {
201 local port=${1}
202 local phy=${2}
203
204 assert isset port
205 assert isset phy
206 assert port_exists ${port}
207
208 # Read configuration of port.
209 config_read $(port_file ${port})
210
211 # Get the address of the phy.
212 local phy_address=$(phy_get_address ${phy})
213
214 # Check if the phy is the same we have
215 # read from the configuration file.
216 if [ "${PHY}" = "${phy_address}" ]; then
217 wireless_create ${port} ${PHY} __ap ${ADDRESS}
218 fi
219
220 exit ${EXIT_OK}
221}