]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/ports/batman-adv-port
Rename all "config" to "settings"
[people/stevee/network.git] / src / hooks / ports / batman-adv-port
CommitLineData
e6993835
MT
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
31670741 24HOOK_SETTINGS="HOOK ADDRESS MESH_ID SSID CHANNEL PHY"
e6993835
MT
25
26ADDRESS=$(mac_generate)
27CHANNEL=1
e6993835
MT
28MESH_ID=
29SSID=
30
31# batman-adv inserts an additional header of 28 bytes, so we set the MTU
2da210b5
MT
32# to 1560, that normal ethernet packets with 1500 bytes can pass the network.
33MTU=1560
e6993835 34
2181765d 35function hook_check() {
e6993835
MT
36 assert isset ADDRESS
37 assert ismac ADDRESS
38 assert isset CHANNEL
e6993835
MT
39 assert isset MESH_ID
40 assert ismac MESH_ID
41 assert isset PHY
42 assert ismac PHY
43 assert isset SSID
44}
45
2181765d 46function hook_create() {
e6993835
MT
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 ;;
e6993835
MT
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
e9df08ad 78 port_settings_write "${port}" ${HOOK_SETTINGS}
e6993835
MT
79
80 exit ${EXIT_OK}
81}
82
2181765d 83function hook_edit() {
e6993835
MT
84 local port=${1}
85 assert isset port
86 shift
87
e9df08ad 88 port_settings_read "${port}" ${HOOK_SETTINGS}
e6993835
MT
89
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 --channel=*)
93 CHANNEL=$(cli_get_val ${1})
94 ;;
e6993835
MT
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
e9df08ad 108 port_settings_write "${port}" ${HOOK_SETTINGS}
e6993835
MT
109
110 exit ${EXIT_OK}
111}
112
2181765d 113function hook_up() {
e6993835
MT
114 local port=${1}
115 assert isset port
116
e9df08ad 117 port_settings_read "${port}" ${HOOK_SETTINGS}
e6993835
MT
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.
cb1475f6 142 local parent="$(hook_find_parent ${port})"
e6993835
MT
143 if isset parent; then
144 batman_adv_interface_add "${parent}" "${port}"
145 fi
146
147 exit ${EXIT_OK}
148}
149
2181765d 150function hook_down() {
e6993835
MT
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
2181765d 168function hook_hotplug() {
e6993835
MT
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.
e9df08ad 178 port_settings_read "${port}" ${HOOK_SETTINGS}
e6993835
MT
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
2181765d 193function hook_find_parent() {
e6993835
MT
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
e6993835
MT
203 [ "${child}" = "${port}" ] || continue
204
205 print "${p}"
206 return ${EXIT_OK}
207 done
208 fi
209 done
210
211 return ${EXIT_ERROR}
212}