]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/wireless-adhoc
Major rewrite of hotplug handling
[people/ms/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 function hook_check() {
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 function hook_create() {
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 function 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 function hook_up() {
118 local port=${1}
119 assert isset port
120
121 port_settings_read "${port}" ${HOOK_SETTINGS}
122
123 # Check if the PHY is present.
124 local phy=$(phy_get ${PHY})
125 if ! isset phy; then
126 log DEBUG "phy '${PHY}' is not present"
127 exit ${EXIT_ERROR}
128 fi
129
130 # Create the wireless device, if it does not exist, yet.
131 if ! device_exists ${port}; then
132 wireless_create ${port} --address="${ADDRESS}" \
133 --phy="${phy}" --type="ibss"
134 fi
135
136 exit ${EXIT_OK}
137 }
138
139 function hook_down() {
140 local port="${1}"
141 assert isset port
142
143 # Do nothing if the device already went away
144 if ! device_exists "${port}"; then
145 exit ${EXIT_OK}
146 fi
147
148 # Leave the ad-hoc network.
149 wireless_ibss_leave "${port}"
150
151 # Remove the device if it is still present.
152 wireless_remove "${port}"
153
154 exit ${EXIT_OK}
155 }
156
157 function hook_hotplug() {
158 local port="${1}"
159 assert isset port
160
161 port_settings_read "${port}" ${HOOK_SETTINGS}
162
163 case "$(hotplug_action)" in
164 add)
165 # Join the adhoc network after the device has
166 # been created...
167 if hotplug_event_port_is_interface "${port}"; then
168 # Set the MTU.
169 if isinteger MTU; then
170 device_set_mtu "${port}" "${MTU}"
171 fi
172
173 wireless_ibss_join "${port}" --channel="${CHANNEL}" \
174 --bssid="${BSSID}" --essid="${SSID}"
175
176 # Bring up the port when the phy is plugged in
177 elif hotplug_event_port_uses_phy "${port}"; then
178 hook_up "${port}"
179 fi
180 ;;
181
182 *)
183 exit ${EXIT_NOT_HANDLED}
184 ;;
185 esac
186
187 exit ${EXIT_OK}
188 }
189
190 function hook_find_parent() {
191 local port=${1}
192 assert isset port
193
194 local p child hook
195 for p in $(ports_get); do
196 hook=$(port_get_hook "${p}")
197
198 if [ "${hook}" = "batman-adv" ]; then
199 for child in $(port_get_children "${p}"); do
200 [ "${child}" = "${port}" ] || continue
201
202 print "${p}"
203 return ${EXIT_OK}
204 done
205 fi
206 done
207
208 return ${EXIT_ERROR}
209 }