]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/wireless-ap
network fix parameter passing when using ""
[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="HOOK 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=1
32 ENCRYPTION=""
33 KEY=""
34 MODE="g"
35 SSID=
36
37 hook_check_settings() {
38 assert isset ADDRESS
39 assert ismac ADDRESS
40 assert isset BROADCAST_SSID
41 assert isbool BROADCAST_SSID
42 assert isset CHANNEL
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 hook_parse_cmdline() {
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 --encryption=*)
68 ENCRYPTION=$(cli_get_val "${1}")
69 ;;
70 --key=*)
71 KEY=$(cli_get_val "${1}")
72 ;;
73 --mac=*)
74 ADDRESS=$(cli_get_val "${1}")
75 ;;
76 --mode=*)
77 MODE=$(cli_get_val "${1}")
78 ;;
79 --phy=*)
80 PHY=$(cli_get_val "${1}")
81 ;;
82 --ssid=*)
83 SSID=$(cli_get_val "${1}")
84 ;;
85 *)
86 warning "Ignoring unknown argument '${1}'"
87 ;;
88 esac
89 shift
90 done
91
92 # Save address of phy do identify it again
93 PHY=$(phy_get ${PHY})
94 PHY=$(phy_get_address ${PHY})
95 }
96
97 hook_edit() {
98 local port=${1}
99 assert isset port
100
101 if ! hook_default_edit "$@"; then
102 return ${EXIT_ERROR}
103 fi
104
105 # To apply all changes, we need to restart the port
106 port_restart "${port}"
107 }
108
109 hook_create() {
110 local port="${1}"
111 assert isset port
112
113 device_exists "${port}" && exit ${EXIT_OK}
114
115 port_settings_read "${port}" ${HOOK_SETTINGS}
116
117 # Check if the PHY is present.
118 local phy=$(phy_get ${PHY})
119 if ! isset phy; then
120 log DEBUG "phy '${PHY}' is not present"
121 exit ${EXIT_ERROR}
122 fi
123
124 # Create the wireless device
125 wireless_create "${port}" \
126 --phy="${phy}" \
127 --type="ap" \
128 --address="${ADDRESS}"
129
130 exit ${EXIT_OK}
131 }
132
133 hook_remove() {
134 local port="${1}"
135 assert isset port
136
137 # Remove the device if present
138 if device_exists "${port}"; then
139 wireless_remove "${port}"
140 fi
141
142 exit ${EXIT_OK}
143 }
144
145 hook_up() {
146 local port="${1}"
147 assert isset port
148
149 # The port must already exist before
150 # hostapd is started. Otherwise it will
151 # fail horribly over and over again.
152 assert device_exists "${port}"
153
154 hostapd_start "${port}"
155 }
156
157 hook_down() {
158 local port="${1}"
159 assert isset port
160
161 hostapd_stop "${port}"
162 }
163
164 hook_hotplug() {
165 local port="${1}"
166 assert isset port
167
168 case "$(hotplug_action)" in
169 add)
170 # Create the port when the phy is plugged in
171 if hotplug_event_port_uses_phy "${port}"; then
172 hook_create "${port}"
173 fi
174 ;;
175
176 remove)
177 # Stop hostapd
178 if hotplug_event_port_is_interface "${port}"; then
179 hostapd_stop "${port}"
180
181 exit ${EXIT_OK}
182 fi
183 ;;
184 esac
185
186 exit ${EXIT_NOT_HANDLED}
187 }