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