]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/wireless-adhoc
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / hooks / ports / wireless-adhoc
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Michael Tremer #
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 BSSID SSID CHANNEL MTU PHY"
25
26 ADDRESS=$(mac_generate)
27 BSSID=
28 CHANNEL=1
29 MTU=1500
30 PHY=
31 SSID=
32
33 hook_check_settings() {
34 assert isset ADDRESS
35 assert ismac ADDRESS
36 assert isset CHANNEL
37 assert isset BSSID
38 assert ismac BSSID
39 assert isset PHY
40 assert ismac PHY
41 assert isset SSID
42 }
43
44 hook_new() {
45 while [ $# -gt 0 ]; do
46 case "${1}" in
47 --address=*)
48 ADDRESS=$(cli_get_val ${1})
49 ;;
50 --bssid=*)
51 BSSID=$(cli_get_val ${1})
52 ;;
53 --channel=*)
54 CHANNEL=$(cli_get_val ${1})
55 ;;
56 --mtu=*)
57 MTU="$(cli_get_val "${1}")"
58 ;;
59 --phy=*)
60 PHY=$(cli_get_val ${1})
61 ;;
62 --ssid=*)
63 SSID=$(cli_get_val ${1})
64 ;;
65 *)
66 warning "Ignoring unknown argument '${1}'"
67 ;;
68 esac
69 shift
70 done
71
72 # Save address of phy do identify it again
73 PHY=$(phy_get ${PHY})
74 PHY=$(phy_get_address ${PHY})
75
76 local port=$(port_find_free ${PORT_PATTERN_WIRELESS_ADHOC})
77 assert isset port
78
79 port_settings_write "${port}" ${HOOK_SETTINGS}
80
81 exit ${EXIT_OK}
82 }
83
84 hook_edit() {
85 local port=${1}
86 assert isset port
87 shift
88
89 port_settings_read "${port}" ${HOOK_SETTINGS}
90
91 while [ $# -gt 0 ]; do
92 case "${1}" in
93 --bssid=*)
94 BSSID=$(cli_get_val ${1})
95 ;;
96 --channel=*)
97 CHANNEL=$(cli_get_val ${1})
98 ;;
99 --mtu=*)
100 MTU="$(cli_get_val "${1}")"
101 ;;
102 --ssid=*)
103 SSID=$(cli_get_val ${1})
104 ;;
105 *)
106 warning "Unknown argument '${1}'"
107 ;;
108 esac
109 shift
110 done
111
112 port_settings_write "${port}" ${HOOK_SETTINGS}
113
114 exit ${EXIT_OK}
115 }
116
117 hook_create() {
118 local port="${1}"
119 assert isset port
120
121 device_exists "${port}" && exit ${EXIT_OK}
122
123 port_settings_read "${port}" ${HOOK_SETTINGS}
124
125 # Check if the PHY is present.
126 local phy="$(phy_get "${PHY}")"
127 if ! isset phy; then
128 log DEBUG "phy '${PHY}' is not present"
129 exit ${EXIT_ERROR}
130 fi
131
132 # Create the wireless device, if it does not exist, yet.
133 wireless_create "${port}" \
134 --address="${ADDRESS}" \
135 --phy="${phy}" \
136 --type="ibss"
137
138 exit ${EXIT_OK}
139 }
140
141 hook_remove() {
142 local port="${1}"
143 assert isset port
144
145 if device_exists "${port}"; then
146 wireless_remove "${port}"
147 fi
148
149 exit ${EXIT_OK}
150 }
151
152 hook_up() {
153 local port=${1}
154 assert isset port
155
156 port_settings_read "${port}" ${HOOK_SETTINGS}
157
158 # Check if the PHY is present.
159 local phy=$(phy_get ${PHY})
160 if ! isset phy; then
161 log DEBUG "phy '${PHY}' is not present"
162 exit ${EXIT_ERROR}
163 fi
164
165 # Create the wireless device, if it does not exist, yet.
166 if ! device_exists ${port}; then
167 wireless_create ${port} --address="${ADDRESS}" \
168 --phy="${phy}" --type="ibss"
169 fi
170
171 exit ${EXIT_OK}
172 }
173
174 hook_down() {
175 local port="${1}"
176 assert isset port
177
178 # Do nothing if the device already went away
179 if ! device_exists "${port}"; then
180 exit ${EXIT_OK}
181 fi
182
183 # Leave the ad-hoc network.
184 wireless_ibss_leave "${port}"
185
186 exit ${EXIT_OK}
187 }
188
189 hook_hotplug() {
190 local port="${1}"
191 assert isset port
192
193 port_settings_read "${port}" ${HOOK_SETTINGS}
194
195 case "$(hotplug_action)" in
196 add)
197 # Join the adhoc network after the device has
198 # been created...
199 if hotplug_event_port_is_interface "${port}"; then
200 # Set the MTU.
201 if isinteger MTU; then
202 device_set_mtu "${port}" "${MTU}"
203 fi
204
205 wireless_ibss_join "${port}" --channel="${CHANNEL}" \
206 --bssid="${BSSID}" --essid="${SSID}"
207
208 # Bring up the port when the phy is plugged in
209 elif hotplug_event_port_uses_phy "${port}"; then
210 hook_up "${port}"
211 fi
212 ;;
213
214 *)
215 exit ${EXIT_NOT_HANDLED}
216 ;;
217 esac
218
219 exit ${EXIT_OK}
220 }
221
222 hook_find_parent() {
223 local port=${1}
224 assert isset port
225
226 local p child hook
227 for p in $(ports_get); do
228 hook=$(port_get_hook "${p}")
229
230 if [ "${hook}" = "batman-adv" ]; then
231 for child in $(port_get_children "${p}"); do
232 [ "${child}" = "${port}" ] || continue
233
234 print "${p}"
235 return ${EXIT_OK}
236 done
237 fi
238 done
239
240 return ${EXIT_ERROR}
241 }