]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/wireless-ap
Bump version to 009
[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_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL MODE PHY SSID"
25 HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION KEY"
26
27 ADDRESS=$(mac_generate)
28 BROADCAST_SSID=on
29 CHANNEL=1
30 ENCRYPTION=""
31 KEY=""
32 MODE="g"
33 SSID=
34
35 hook_check_settings() {
36 assert isset ADDRESS
37 assert ismac ADDRESS
38 assert isset BROADCAST_SSID
39 assert isbool BROADCAST_SSID
40 assert isset CHANNEL
41 assert isset MODE
42 assert isoneof MODE a b g n
43 assert isset PHY
44 assert ismac PHY
45 assert isset SSID
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
54 }
55
56 hook_parse_cmdline() {
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 ;;
65 --encryption=*)
66 ENCRYPTION=$(cli_get_val ${1})
67 ;;
68 --key=*)
69 KEY=$(cli_get_val ${1})
70 ;;
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 }
94
95 hook_new() {
96 if ! hook_parse_cmdline $@; then
97 return ${EXIT_ERROR}
98 fi
99
100 local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
101 assert isset port
102
103 port_settings_write "${port}" ${HOOK_SETTINGS}
104
105 exit ${EXIT_OK}
106 }
107
108 hook_edit() {
109 local port=${1}
110 assert isset port
111
112 if ! hook_default_edit $@; then
113 return ${EXIT_ERROR}
114 fi
115
116 # To apply all changes, we need to restart the port
117 port_restart "${port}"
118 }
119
120 hook_create() {
121 local port="${1}"
122 assert isset port
123
124 device_exists "${port}" && exit ${EXIT_OK}
125
126 port_settings_read "${port}" ${HOOK_SETTINGS}
127
128 # Check if the PHY is present.
129 local phy=$(phy_get ${PHY})
130 if ! isset phy; then
131 log DEBUG "phy '${PHY}' is not present"
132 exit ${EXIT_ERROR}
133 fi
134
135 # Create the wireless device
136 wireless_create "${port}" \
137 --phy="${phy}" \
138 --type="ap" \
139 --address="${ADDRESS}"
140
141 exit ${EXIT_OK}
142 }
143
144 hook_remove() {
145 local port="${1}"
146 assert isset port
147
148 # Remove the device if present
149 if device_exists "${port}"; then
150 wireless_remove "${port}"
151 fi
152
153 exit ${EXIT_OK}
154 }
155
156 hook_up() {
157 local port="${1}"
158 assert isset port
159
160 # The port must already exist before
161 # hostapd is started. Otherwise it will
162 # fail horribly over and over again.
163 assert device_exists "${port}"
164
165 hostapd_start "${port}"
166 }
167
168 hook_down() {
169 local port="${1}"
170 assert isset port
171
172 hostapd_stop "${port}"
173 }
174
175 hook_hotplug() {
176 local port="${1}"
177 assert isset port
178
179 case "$(hotplug_action)" in
180 add)
181 # Create the port when the phy is plugged in
182 if hotplug_event_port_uses_phy "${port}"; then
183 hook_create "${port}"
184 fi
185 ;;
186
187 remove)
188 # Stop hostapd
189 if hotplug_event_port_is_interface "${port}"; then
190 hostapd_stop "${port}"
191
192 exit ${EXIT_OK}
193 fi
194 ;;
195 esac
196
197 exit ${EXIT_NOT_HANDLED}
198 }