]>
Commit | Line | Data |
---|---|---|
1848564d MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
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 | ||
1b7a1578 | 22 | function devicify() { |
1848564d MT |
23 | local device=${1} |
24 | ||
711ffac1 MT |
25 | assert isset device |
26 | ||
1848564d MT |
27 | if device_exists ${device}; then |
28 | echo "${device}" | |
29 | return ${EXIT_OK} | |
30 | fi | |
31 | ||
32 | local d | |
33 | for d in $(devices_get_all); do | |
34 | if [ "$(device_get_address ${d})" = "${device}" ]; then | |
35 | echo "${d}" | |
36 | return ${EXIT_OK} | |
37 | fi | |
38 | done | |
39 | ||
40 | return ${EXIT_ERROR} | |
41 | } | |
42 | ||
43 | function macify() { | |
44 | local device=${1} | |
45 | ||
711ffac1 MT |
46 | assert isset device |
47 | ||
1848564d MT |
48 | if mac_is_valid ${device}; then |
49 | echo "${device}" | |
50 | return ${EXIT_OK} | |
51 | fi | |
52 | ||
53 | if device_exists ${device}; then | |
54 | device_get_address ${device} | |
55 | return ${EXIT_OK} | |
56 | fi | |
57 | ||
58 | return ${EXIT_ERROR} | |
59 | } | |
60 | ||
61 | # Check if the device exists | |
62 | function device_exists() { | |
63 | local device=${1} | |
64 | ||
65 | # If device name was not found, exit. | |
66 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
67 | ||
68 | [ -d "${SYS_CLASS_NET}/${device}" ] | |
69 | } | |
70 | ||
71 | # Check if the device is up | |
72 | function device_is_up() { | |
73 | local device=${1} | |
74 | ||
75 | device_exists ${device} || return ${EXIT_ERROR} | |
76 | ||
77 | ip link show ${device} 2>/dev/null | grep -qE "<.*UP.*>" | |
78 | } | |
79 | ||
80 | # Check if the device is a bonding device | |
81 | function device_is_bonding() { | |
82 | [ -d "/sys/class/net/${1}/bonding" ] | |
83 | } | |
84 | ||
85 | # Check if the device bonded in a bonding device | |
86 | function device_is_bonded() { | |
711ffac1 | 87 | local device=${1} |
1848564d | 88 | |
711ffac1 | 89 | [ -d "${SYS_CLASS_NET}/${device}/master" ] |
1848564d MT |
90 | } |
91 | ||
92 | # Check if the device is a bridge | |
93 | function device_is_bridge() { | |
94 | [ -d "/sys/class/net/${1}/bridge" ] | |
95 | } | |
96 | ||
81ed640c MT |
97 | function device_is_bridge_attached() { |
98 | local device=${1} | |
99 | ||
100 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] | |
101 | } | |
102 | ||
1848564d MT |
103 | # Check if the device is a virtual device |
104 | function device_is_virtual() { | |
105 | local device=${1} | |
106 | ||
107 | [ -e "/proc/net/vlan/${device}" ] | |
108 | } | |
109 | ||
110 | # Check if the device has virtual devices | |
111 | function device_has_virtuals() { | |
fb02e543 MT |
112 | local device=${1} |
113 | ||
114 | if device_is_virtual ${device}; then | |
115 | return 1 | |
116 | fi | |
117 | ||
1848564d MT |
118 | if [ ! -e "/proc/net/vlan/config" ]; then |
119 | return 1 | |
120 | fi | |
121 | grep -q "${1}$" /proc/net/vlan/config | |
122 | } | |
123 | ||
1848564d MT |
124 | # Check if the device is a ppp device |
125 | function device_is_ppp() { | |
126 | local device=${1} | |
127 | ||
128 | ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>" | |
129 | } | |
130 | ||
131 | # Check if the device is a loopback device | |
132 | function device_is_loopback() { | |
133 | local device=$(devicify ${1}) | |
134 | [ "${device}" = "lo" ] | |
135 | } | |
136 | ||
137 | # Check if the device is a physical network interface | |
138 | function device_is_real() { | |
139 | local device=${1} | |
140 | ||
141 | device_is_loopback ${device} && \ | |
142 | return ${EXIT_ERROR} | |
143 | ||
144 | device_is_bonding ${device} && \ | |
145 | return ${EXIT_ERROR} | |
146 | ||
147 | device_is_bridge ${device} && \ | |
148 | return ${EXIT_ERROR} | |
149 | ||
150 | device_is_ppp ${device} && \ | |
151 | return ${EXIT_ERROR} | |
152 | ||
153 | device_is_virtual ${device} && \ | |
154 | return ${EXIT_ERROR} | |
155 | ||
156 | return ${EXIT_OK} | |
157 | } | |
158 | ||
159 | # Get the device type | |
160 | function device_get_type() { | |
161 | local device=$(devicify ${1}) | |
162 | ||
8c6a8966 | 163 | if device_is_virtual ${device}; then |
1848564d MT |
164 | echo "vlan" |
165 | ||
166 | elif device_is_bonding ${device}; then | |
167 | echo "bonding" | |
168 | ||
169 | elif device_is_bridge ${device}; then | |
170 | echo "bridge" | |
171 | ||
172 | elif device_is_ppp ${device}; then | |
173 | echo "ppp" | |
174 | ||
175 | elif device_is_loopback ${device}; then | |
176 | echo "loopback" | |
177 | ||
178 | elif device_is_real ${device}; then | |
179 | echo "real" | |
180 | ||
181 | else | |
182 | echo "unknown" | |
183 | fi | |
184 | } | |
185 | ||
711ffac1 MT |
186 | function device_get_status() { |
187 | local device=${1} | |
188 | ||
189 | assert isset device | |
190 | ||
191 | local status=${STATUS_UNKNOWN} | |
192 | ||
193 | if ! device_has_carrier ${device}; then | |
194 | status=${STATUS_NOCARRIER} | |
195 | elif device_is_up ${device}; then | |
196 | status=${STATUS_UP} | |
197 | elif device_is_down ${device}; then | |
198 | status=${STATUS_DOWN} | |
199 | fi | |
200 | ||
201 | assert isset status | |
202 | ||
203 | echo "${status}" | |
204 | } | |
205 | ||
1848564d MT |
206 | function device_get_address() { |
207 | local device=${1} | |
208 | ||
209 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
210 | } | |
211 | ||
212 | function device_set_address() { | |
1b7a1578 MT |
213 | local device=${1} |
214 | local addr=${2} | |
215 | ||
216 | if ! device_exists ${device}; then | |
217 | error "Device '${device}' does not exist." | |
218 | return ${EXIT_ERROR} | |
219 | fi | |
220 | ||
221 | log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})." | |
222 | ||
223 | local up | |
224 | if device_is_up ${device}; then | |
225 | device_set_down ${device} | |
226 | up=1 | |
227 | fi | |
228 | ||
229 | ip link set ${device} address ${addr} | |
230 | local ret=$? | |
231 | ||
232 | if [ "${up}" = "1" ]; then | |
233 | device_set_up ${device} | |
234 | fi | |
235 | ||
236 | if [ "${ret}" != "0" ]; then | |
237 | error_log "Could not set address '${addr}' on device '${device}'." | |
238 | fi | |
239 | ||
240 | return ${ret} | |
1848564d MT |
241 | } |
242 | ||
711ffac1 | 243 | function device_get() { |
2ae0fb8d | 244 | local device |
711ffac1 MT |
245 | local devices |
246 | ||
2ae0fb8d MT |
247 | for device in ${SYS_CLASS_NET}/*; do |
248 | device=$(basename ${device}) | |
711ffac1 | 249 | |
2ae0fb8d MT |
250 | # bonding_masters is no device |
251 | [ "${device}" = "bonding_masters" ] && continue | |
252 | ||
253 | devices="${devices} ${device}" | |
254 | done | |
711ffac1 MT |
255 | |
256 | echo ${devices} | |
257 | return ${EXIT_OK} | |
258 | } | |
259 | ||
1848564d | 260 | function devices_get_all() { |
711ffac1 | 261 | device_get |
1848564d MT |
262 | } |
263 | ||
264 | # Check if a device has a cable plugged in | |
265 | function device_has_carrier() { | |
266 | local device=$(devicify ${1}) | |
267 | [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ] | |
268 | } | |
269 | ||
1e4c26a4 MT |
270 | function device_is_promisc() { |
271 | local device=${1} | |
272 | ||
273 | ip link show ${device} | grep -qE "<.*PROMISC.*>" | |
274 | } | |
275 | ||
1848564d MT |
276 | # Check if the device is free |
277 | function device_is_free() { | |
81ed640c | 278 | ! device_is_used $@ |
1848564d MT |
279 | } |
280 | ||
281 | # Check if the device is used | |
282 | function device_is_used() { | |
283 | local device=$(devicify ${1}) | |
284 | ||
fb02e543 MT |
285 | device_has_virtuals ${device} && \ |
286 | return ${EXIT_OK} | |
1848564d | 287 | device_is_bonded ${device} && \ |
fb02e543 | 288 | return ${EXIT_OK} |
81ed640c MT |
289 | device_is_bridge_attached ${device} && \ |
290 | return ${EXIT_OK} | |
1848564d | 291 | |
fb02e543 | 292 | return ${EXIT_ERROR} |
1848564d MT |
293 | } |
294 | ||
1b7a1578 MT |
295 | function device_hash() { |
296 | local device=${1} | |
297 | ||
37e4ec8b MT |
298 | # Get mac address of device and remove all colons (:) |
299 | # that will result in a hash. | |
300 | device=$(macify ${device}) | |
301 | ||
302 | echo "${device//:/}" | |
1b7a1578 MT |
303 | } |
304 | ||
305 | # Give the device a new name | |
306 | function device_set_name() { | |
1848564d | 307 | local source=$1 |
1578dae9 | 308 | local destination=${2} |
1848564d MT |
309 | |
310 | # Check if devices exists | |
311 | if ! device_exists ${source} || device_exists ${destination}; then | |
312 | return 4 | |
313 | fi | |
314 | ||
315 | local up | |
316 | if device_is_up ${source}; then | |
317 | ip link set ${source} down | |
318 | up=1 | |
319 | fi | |
320 | ||
321 | ip link set ${source} name ${destination} | |
322 | ||
323 | if [ "${up}" = "1" ]; then | |
324 | ip link set ${destination} up | |
325 | fi | |
326 | } | |
327 | ||
1848564d MT |
328 | # Set device up |
329 | function device_set_up() { | |
330 | local device=$(devicify ${1}) | |
331 | ||
711ffac1 MT |
332 | # Silently fail if device was not found |
333 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
334 | ||
1848564d MT |
335 | # Do nothing if device is already up |
336 | device_is_up ${device} && return ${EXIT_OK} | |
337 | ||
81ed640c MT |
338 | device_set_parent_up ${device} |
339 | ||
340 | log DEBUG "Setting up device '${device}'" | |
341 | ||
1848564d MT |
342 | ip link set ${device} up |
343 | } | |
344 | ||
81ed640c MT |
345 | function device_set_parent_up() { |
346 | local device=${1} | |
347 | local parent | |
348 | ||
349 | if device_is_virtual ${device}; then | |
8c6a8966 | 350 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
351 | |
352 | device_is_up ${parent} && return ${EXIT_OK} | |
353 | ||
354 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
355 | ||
356 | device_set_up ${parent} | |
357 | return $? | |
358 | fi | |
359 | ||
360 | return ${EXIT_OK} | |
361 | } | |
362 | ||
1848564d MT |
363 | # Set device down |
364 | function device_set_down() { | |
365 | local device=$(devicify ${1}) | |
366 | ||
81ed640c MT |
367 | local ret=${EXIT_OK} |
368 | ||
369 | if device_is_up ${device}; then | |
370 | log DEBUG "Tearing down device '${device}'" | |
371 | ||
372 | ip link set ${device} down | |
373 | ret=$? | |
374 | fi | |
375 | ||
376 | device_set_parent_down ${device} | |
1848564d | 377 | |
81ed640c MT |
378 | return ${ret} |
379 | } | |
380 | ||
381 | function device_set_parent_down() { | |
382 | local device=${1} | |
383 | local parent | |
384 | ||
385 | if device_is_virtual ${device}; then | |
8c6a8966 | 386 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
387 | |
388 | device_is_up ${parent} || return ${EXIT_OK} | |
389 | ||
390 | if device_is_free ${parent}; then | |
391 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
392 | ||
393 | device_set_down ${parent} | |
394 | fi | |
395 | fi | |
396 | ||
397 | return ${EXIT_OK} | |
1848564d MT |
398 | } |
399 | ||
1848564d MT |
400 | function device_get_mtu() { |
401 | local device=${1} | |
402 | ||
403 | if ! device_exists ${device}; then | |
404 | error "Device '${device}' does not exist." | |
405 | return ${EXIT_ERROR} | |
406 | fi | |
407 | ||
f3e6fe50 | 408 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
409 | } |
410 | ||
411 | # Set mtu to a device | |
412 | function device_set_mtu() { | |
1b7a1578 | 413 | local device=${1} |
1848564d MT |
414 | local mtu=${2} |
415 | ||
1b7a1578 MT |
416 | if ! device_exists ${device}; then |
417 | error "Device '${device}' does not exist." | |
418 | return ${EXIT_ERROR} | |
419 | fi | |
420 | ||
421 | local oldmtu=$(device_get_mtu ${device}) | |
422 | ||
423 | if [ "${oldmtu}" = "${mtu}" ]; then | |
424 | # No need to set mtu. | |
425 | return ${EXIT_OK} | |
426 | fi | |
427 | ||
428 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
429 | ||
1848564d | 430 | local up |
1b7a1578 MT |
431 | if device_is_up ${device}; then |
432 | device_set_down ${device} | |
1848564d MT |
433 | up=1 |
434 | fi | |
435 | ||
1b7a1578 | 436 | ip link set ${device} mtu ${mtu} |
1848564d MT |
437 | local ret=$? |
438 | ||
439 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
440 | device_set_up ${device} |
441 | fi | |
442 | ||
443 | if [ "${ret}" != "0" ]; then | |
444 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
445 | fi |
446 | ||
447 | return ${ret} | |
448 | } | |
449 | ||
450 | function device_discover() { | |
451 | local device=${1} | |
452 | ||
1b7a1578 MT |
453 | log INFO "Running discovery process on device '${device}'." |
454 | ||
1848564d | 455 | local hook |
d61a01d4 MT |
456 | for hook in $(hook_zone_get_all); do |
457 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
458 | done |
459 | } | |
460 | ||
1848564d MT |
461 | function device_has_ipv4() { |
462 | local device=${1} | |
463 | local addr=${2} | |
464 | ||
465 | if ! device_exists ${device}; then | |
466 | error "Device '${device}' does not exist." | |
467 | return ${EXIT_ERROR} | |
468 | fi | |
469 | ||
470 | ip addr show ${device} | grep -q -e "inet " -e "${addr}" | |
471 | } | |
4231f419 MT |
472 | |
473 | function device_has_ipv6() { | |
474 | local device=${1} | |
475 | local addr=${2} | |
476 | ||
477 | if ! device_exists ${device}; then | |
478 | error "Device '${device}' does not exist." | |
479 | return ${EXIT_ERROR} | |
480 | fi | |
481 | ||
482 | local prefix=${addr##*/} | |
483 | addr=$(ipv6_implode ${addr%%/*}) | |
484 | ||
485 | if [ -n "${prefix}" ]; then | |
486 | addr="${addr}/${prefix}" | |
487 | fi | |
488 | ||
489 | ip addr show ${device} | grep -q "inet6 ${addr}" | |
490 | } | |
711ffac1 | 491 | |
711ffac1 MT |
492 | function __device_get_file() { |
493 | local device=${1} | |
494 | local file=${2} | |
495 | ||
496 | assert isset device | |
497 | assert isset file | |
498 | ||
499 | cat ${SYS_CLASS_NET}/${device}/${file} | |
500 | } | |
501 | ||
502 | function device_get_rx_bytes() { | |
503 | local device=${1} | |
504 | ||
505 | __device_get_file ${device} statistics/rx_bytes | |
506 | } | |
507 | ||
508 | function device_get_tx_bytes() { | |
509 | local device=${1} | |
510 | ||
511 | __device_get_file ${device} statistics/tx_bytes | |
512 | } | |
513 | ||
514 | function device_get_rx_packets() { | |
515 | local device=${1} | |
516 | ||
517 | __device_get_file ${device} statistics/rx_packets | |
518 | } | |
519 | ||
520 | function device_get_tx_packets() { | |
521 | local device=${1} | |
522 | ||
523 | __device_get_file ${device} statistics/tx_packets | |
524 | } | |
525 | ||
526 | function device_get_rx_errors() { | |
527 | local device=${1} | |
528 | ||
529 | __device_get_file ${device} statistics/rx_errors | |
530 | } | |
531 | ||
532 | function device_get_tx_errors() { | |
533 | local device=${1} | |
534 | ||
535 | __device_get_file ${device} statistics/tx_errors | |
536 | } |