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