]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/batman-adv-port
b14351e052c52d18bfb58976004cc97532862b6e
[people/stevee/network.git] / src / hooks / ports / batman-adv-port
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 MESH_ID SSID CHANNEL PHY"
25
26 ADDRESS=$(mac_generate)
27 CHANNEL=1
28 MESH_ID=
29 SSID=
30
31 # batman-adv inserts an additional header of 28 bytes, so we set the MTU
32 # to 1560, that normal ethernet packets with 1500 bytes can pass the network.
33 MTU=1560
34
35 function hook_check() {
36 assert isset ADDRESS
37 assert ismac ADDRESS
38 assert isset CHANNEL
39 assert isset MESH_ID
40 assert ismac MESH_ID
41 assert isset PHY
42 assert ismac PHY
43 assert isset SSID
44 }
45
46 function hook_create() {
47 while [ $# -gt 0 ]; do
48 case "${1}" in
49 --address=*)
50 ADDRESS=$(cli_get_val ${1})
51 ;;
52 --channel=*)
53 CHANNEL=$(cli_get_val ${1})
54 ;;
55 --phy=*)
56 PHY=$(cli_get_val ${1})
57 ;;
58 --ssid=*)
59 SSID=$(cli_get_val ${1})
60 ;;
61 --mesh-id=*)
62 MESH_ID=$(cli_get_val ${1})
63 ;;
64 *)
65 warning "Ignoring unknown argument '${1}'"
66 ;;
67 esac
68 shift
69 done
70
71 # Save address of phy do identify it again
72 PHY=$(phy_get ${PHY})
73 PHY=$(phy_get_address ${PHY})
74
75 local port=$(port_find_free ${PORT_PATTERN_BATMAN_ADV_PORT})
76 assert isset port
77
78 config_write $(port_file ${port}) ${HOOK_SETTINGS}
79
80 exit ${EXIT_OK}
81 }
82
83 function hook_edit() {
84 local port=${1}
85 assert isset port
86 shift
87
88 config_read $(port_file ${port})
89
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 --channel=*)
93 CHANNEL=$(cli_get_val ${1})
94 ;;
95 --mesh-id=*)
96 MESH_ID=$(cli_get_val ${1})
97 ;;
98 --ssid=*)
99 SSID=$(cli_get_val ${1})
100 ;;
101 *)
102 warning "Unknown argument '${1}'"
103 ;;
104 esac
105 shift
106 done
107
108 config_write $(port_file ${port}) ${HOOK_SETTINGS}
109
110 exit ${EXIT_OK}
111 }
112
113 function hook_up() {
114 local port=${1}
115 assert isset port
116
117 config_read $(port_file ${port})
118
119 # Check if the PHY is present.
120 local phy=$(phy_get ${PHY})
121 if ! isset phy; then
122 log DEBUG "phy '${PHY}' is not present"
123 exit ${EXIT_ERROR}
124 fi
125
126 # Create the wireless device, if it does not exist, yet.
127 if ! device_exists ${port}; then
128 wireless_create ${port} \
129 --address="${ADDRESS}" \
130 --phy="${phy}" \
131 --type="ibss"
132 fi
133
134 # Set the MTU.
135 device_set_mtu "${port}" "${MTU}"
136
137 # Join the ad-hoc network.
138 wireless_ibss_join "${port}" --channel="${CHANNEL}" \
139 --bssid="${MESH_ID}" --essid="${SSID}"
140
141 # Add the device as a batman-adv device.
142 local parent="$(hook_find_parent ${port})"
143 if isset parent; then
144 batman_adv_interface_add "${parent}" "${port}"
145 fi
146
147 exit ${EXIT_OK}
148 }
149
150 function hook_down() {
151 local port=${1}
152 assert isset port
153
154 # Remove the batman-adv device.
155 batman_adv_interface_del "${port}"
156
157 # Leave the ad-hoc network.
158 wireless_ibss_leave "${port}"
159
160 # Remove the device if it is still present.
161 if device_exists ${port}; then
162 wireless_remove ${port}
163 fi
164
165 exit ${EXIT_OK}
166 }
167
168 function hook_hotplug() {
169 local port=${1}
170 assert isset port
171
172 local phy=${2}
173 assert isset phy
174
175 assert port_exists ${port}
176
177 # Read configuration of port.
178 config_read $(port_file ${port})
179
180 # Get the address of the phy.
181 local phy_address=$(phy_get_address ${phy})
182
183 # Check if the phy is the same we have
184 # read from the configuration file.
185 if [ "${PHY}" = "${phy_address}" ]; then
186 # Bring up the device.
187 port_up "${port}"
188 fi
189
190 exit ${EXIT_OK}
191 }
192
193 function hook_find_parent() {
194 local port=${1}
195 assert isset port
196
197 local p child hook
198 for p in $(ports_get); do
199 hook=$(port_get_hook "${p}")
200
201 if [ "${hook}" = "batman-adv" ]; then
202 for child in $(port_get_children "${p}"); do
203 [ "${child}" = "${port}" ] || continue
204
205 print "${p}"
206 return ${EXIT_OK}
207 done
208 fi
209 done
210
211 return ${EXIT_ERROR}
212 }