]>
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 | ||
124 | function device_is_vlan() { # XXX Compat function | |
125 | log DEBUG "Deprecated function device_is_vlan() was used." | |
126 | ||
127 | device_is_virtual $@ | |
128 | } | |
129 | ||
130 | # Check if the device is a ppp device | |
131 | function device_is_ppp() { | |
132 | local device=${1} | |
133 | ||
134 | ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>" | |
135 | } | |
136 | ||
137 | # Check if the device is a loopback device | |
138 | function device_is_loopback() { | |
139 | local device=$(devicify ${1}) | |
140 | [ "${device}" = "lo" ] | |
141 | } | |
142 | ||
143 | # Check if the device is a physical network interface | |
144 | function device_is_real() { | |
145 | local device=${1} | |
146 | ||
147 | device_is_loopback ${device} && \ | |
148 | return ${EXIT_ERROR} | |
149 | ||
150 | device_is_bonding ${device} && \ | |
151 | return ${EXIT_ERROR} | |
152 | ||
153 | device_is_bridge ${device} && \ | |
154 | return ${EXIT_ERROR} | |
155 | ||
156 | device_is_ppp ${device} && \ | |
157 | return ${EXIT_ERROR} | |
158 | ||
159 | device_is_virtual ${device} && \ | |
160 | return ${EXIT_ERROR} | |
161 | ||
162 | return ${EXIT_OK} | |
163 | } | |
164 | ||
165 | # Get the device type | |
166 | function device_get_type() { | |
167 | local device=$(devicify ${1}) | |
168 | ||
169 | if device_is_vlan ${device}; then | |
170 | echo "vlan" | |
171 | ||
172 | elif device_is_bonding ${device}; then | |
173 | echo "bonding" | |
174 | ||
175 | elif device_is_bridge ${device}; then | |
176 | echo "bridge" | |
177 | ||
178 | elif device_is_ppp ${device}; then | |
179 | echo "ppp" | |
180 | ||
181 | elif device_is_loopback ${device}; then | |
182 | echo "loopback" | |
183 | ||
184 | elif device_is_real ${device}; then | |
185 | echo "real" | |
186 | ||
187 | else | |
188 | echo "unknown" | |
189 | fi | |
190 | } | |
191 | ||
711ffac1 MT |
192 | function device_get_status() { |
193 | local device=${1} | |
194 | ||
195 | assert isset device | |
196 | ||
197 | local status=${STATUS_UNKNOWN} | |
198 | ||
199 | if ! device_has_carrier ${device}; then | |
200 | status=${STATUS_NOCARRIER} | |
201 | elif device_is_up ${device}; then | |
202 | status=${STATUS_UP} | |
203 | elif device_is_down ${device}; then | |
204 | status=${STATUS_DOWN} | |
205 | fi | |
206 | ||
207 | assert isset status | |
208 | ||
209 | echo "${status}" | |
210 | } | |
211 | ||
1848564d MT |
212 | function device_get_address() { |
213 | local device=${1} | |
214 | ||
215 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
216 | } | |
217 | ||
218 | function device_set_address() { | |
1b7a1578 MT |
219 | local device=${1} |
220 | local addr=${2} | |
221 | ||
222 | if ! device_exists ${device}; then | |
223 | error "Device '${device}' does not exist." | |
224 | return ${EXIT_ERROR} | |
225 | fi | |
226 | ||
227 | log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})." | |
228 | ||
229 | local up | |
230 | if device_is_up ${device}; then | |
231 | device_set_down ${device} | |
232 | up=1 | |
233 | fi | |
234 | ||
235 | ip link set ${device} address ${addr} | |
236 | local ret=$? | |
237 | ||
238 | if [ "${up}" = "1" ]; then | |
239 | device_set_up ${device} | |
240 | fi | |
241 | ||
242 | if [ "${ret}" != "0" ]; then | |
243 | error_log "Could not set address '${addr}' on device '${device}'." | |
244 | fi | |
245 | ||
246 | return ${ret} | |
1848564d MT |
247 | } |
248 | ||
711ffac1 MT |
249 | function device_get() { |
250 | local from_config | |
251 | ||
252 | while [ $# -gt 0 ]; do | |
253 | case "${1}" in | |
254 | --from-config) | |
255 | from_config=1 | |
256 | ;; | |
257 | --no-config) | |
258 | from_config=0 | |
259 | ;; | |
260 | esac | |
261 | shift | |
262 | done | |
263 | ||
264 | local devices | |
265 | ||
266 | if [ "${from_config}" != "0" ]; then | |
267 | devices="${devices} $(device_config_list)" | |
268 | fi | |
269 | ||
270 | if [ "${from_config}" != "1" ]; then | |
271 | local device | |
272 | for device in ${SYS_CLASS_NET}/*; do | |
273 | devices="${devices} $(basename ${device})" | |
274 | done | |
275 | fi | |
276 | ||
277 | echo ${devices} | |
278 | return ${EXIT_OK} | |
279 | } | |
280 | ||
1848564d | 281 | function devices_get_all() { |
711ffac1 | 282 | device_get |
1848564d MT |
283 | } |
284 | ||
285 | # Check if a device has a cable plugged in | |
286 | function device_has_carrier() { | |
287 | local device=$(devicify ${1}) | |
288 | [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ] | |
289 | } | |
290 | ||
1e4c26a4 MT |
291 | function device_is_promisc() { |
292 | local device=${1} | |
293 | ||
294 | ip link show ${device} | grep -qE "<.*PROMISC.*>" | |
295 | } | |
296 | ||
1848564d MT |
297 | # Check if the device is free |
298 | function device_is_free() { | |
81ed640c | 299 | ! device_is_used $@ |
1848564d MT |
300 | } |
301 | ||
302 | # Check if the device is used | |
303 | function device_is_used() { | |
304 | local device=$(devicify ${1}) | |
305 | ||
fb02e543 MT |
306 | device_has_virtuals ${device} && \ |
307 | return ${EXIT_OK} | |
1848564d | 308 | device_is_bonded ${device} && \ |
fb02e543 | 309 | return ${EXIT_OK} |
81ed640c MT |
310 | device_is_bridge_attached ${device} && \ |
311 | return ${EXIT_OK} | |
1848564d | 312 | |
fb02e543 | 313 | return ${EXIT_ERROR} |
1848564d MT |
314 | } |
315 | ||
316 | # XXX to be removed I think | |
317 | function device_get_free() { | |
318 | local destination=${1} | |
319 | ||
320 | # Replace + by a valid number | |
321 | if grep -q "+$" <<<${destination}; then | |
322 | local number=0 | |
323 | destination=$(sed -e "s/+//" <<<$destination) | |
324 | while [ "${number}" -le "100" ]; do | |
325 | if ! device_exists "${destination}${number}"; then | |
326 | destination="${destination}${number}" | |
327 | break | |
328 | fi | |
329 | number=$(($number + 1)) | |
330 | done | |
331 | fi | |
332 | echo "${destination}" | |
333 | } | |
334 | ||
1848564d | 335 | function device_rename() { |
1b7a1578 MT |
336 | warning_log "Called deprecated function 'device_rename'" |
337 | ||
338 | device_set_name $@ | |
339 | } | |
340 | ||
341 | function device_hash() { | |
342 | local device=${1} | |
343 | ||
37e4ec8b MT |
344 | # Get mac address of device and remove all colons (:) |
345 | # that will result in a hash. | |
346 | device=$(macify ${device}) | |
347 | ||
348 | echo "${device//:/}" | |
1b7a1578 MT |
349 | } |
350 | ||
351 | # Give the device a new name | |
352 | function device_set_name() { | |
1848564d MT |
353 | local source=$1 |
354 | local destination=$(device_get_free ${2}) | |
355 | ||
356 | # Check if devices exists | |
357 | if ! device_exists ${source} || device_exists ${destination}; then | |
358 | return 4 | |
359 | fi | |
360 | ||
361 | local up | |
362 | if device_is_up ${source}; then | |
363 | ip link set ${source} down | |
364 | up=1 | |
365 | fi | |
366 | ||
367 | ip link set ${source} name ${destination} | |
368 | ||
369 | if [ "${up}" = "1" ]; then | |
370 | ip link set ${destination} up | |
371 | fi | |
372 | } | |
373 | ||
1848564d MT |
374 | # Set device up |
375 | function device_set_up() { | |
376 | local device=$(devicify ${1}) | |
377 | ||
711ffac1 MT |
378 | # Silently fail if device was not found |
379 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
380 | ||
1848564d MT |
381 | # Do nothing if device is already up |
382 | device_is_up ${device} && return ${EXIT_OK} | |
383 | ||
81ed640c MT |
384 | device_set_parent_up ${device} |
385 | ||
386 | log DEBUG "Setting up device '${device}'" | |
387 | ||
1848564d MT |
388 | ip link set ${device} up |
389 | } | |
390 | ||
81ed640c MT |
391 | function device_set_parent_up() { |
392 | local device=${1} | |
393 | local parent | |
394 | ||
395 | if device_is_virtual ${device}; then | |
396 | parent=$(device_virtual_get_parent ${device}) | |
397 | ||
398 | device_is_up ${parent} && return ${EXIT_OK} | |
399 | ||
400 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
401 | ||
402 | device_set_up ${parent} | |
403 | return $? | |
404 | fi | |
405 | ||
406 | return ${EXIT_OK} | |
407 | } | |
408 | ||
1848564d MT |
409 | # Set device down |
410 | function device_set_down() { | |
411 | local device=$(devicify ${1}) | |
412 | ||
81ed640c MT |
413 | local ret=${EXIT_OK} |
414 | ||
415 | if device_is_up ${device}; then | |
416 | log DEBUG "Tearing down device '${device}'" | |
417 | ||
418 | ip link set ${device} down | |
419 | ret=$? | |
420 | fi | |
421 | ||
422 | device_set_parent_down ${device} | |
1848564d | 423 | |
81ed640c MT |
424 | return ${ret} |
425 | } | |
426 | ||
427 | function device_set_parent_down() { | |
428 | local device=${1} | |
429 | local parent | |
430 | ||
431 | if device_is_virtual ${device}; then | |
432 | parent=$(device_virtual_get_parent ${device}) | |
433 | ||
434 | device_is_up ${parent} || return ${EXIT_OK} | |
435 | ||
436 | if device_is_free ${parent}; then | |
437 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
438 | ||
439 | device_set_down ${parent} | |
440 | fi | |
441 | fi | |
442 | ||
443 | return ${EXIT_OK} | |
1848564d MT |
444 | } |
445 | ||
1848564d MT |
446 | function device_get_mtu() { |
447 | local device=${1} | |
448 | ||
449 | if ! device_exists ${device}; then | |
450 | error "Device '${device}' does not exist." | |
451 | return ${EXIT_ERROR} | |
452 | fi | |
453 | ||
f3e6fe50 | 454 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
455 | } |
456 | ||
457 | # Set mtu to a device | |
458 | function device_set_mtu() { | |
1b7a1578 | 459 | local device=${1} |
1848564d MT |
460 | local mtu=${2} |
461 | ||
1b7a1578 MT |
462 | if ! device_exists ${device}; then |
463 | error "Device '${device}' does not exist." | |
464 | return ${EXIT_ERROR} | |
465 | fi | |
466 | ||
467 | local oldmtu=$(device_get_mtu ${device}) | |
468 | ||
469 | if [ "${oldmtu}" = "${mtu}" ]; then | |
470 | # No need to set mtu. | |
471 | return ${EXIT_OK} | |
472 | fi | |
473 | ||
474 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
475 | ||
1848564d | 476 | local up |
1b7a1578 MT |
477 | if device_is_up ${device}; then |
478 | device_set_down ${device} | |
1848564d MT |
479 | up=1 |
480 | fi | |
481 | ||
1b7a1578 | 482 | ip link set ${device} mtu ${mtu} |
1848564d MT |
483 | local ret=$? |
484 | ||
485 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
486 | device_set_up ${device} |
487 | fi | |
488 | ||
489 | if [ "${ret}" != "0" ]; then | |
490 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
491 | fi |
492 | ||
493 | return ${ret} | |
494 | } | |
495 | ||
496 | function device_discover() { | |
497 | local device=${1} | |
498 | ||
1b7a1578 MT |
499 | log INFO "Running discovery process on device '${device}'." |
500 | ||
1848564d | 501 | local hook |
d61a01d4 MT |
502 | for hook in $(hook_zone_get_all); do |
503 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
504 | done |
505 | } | |
506 | ||
1848564d MT |
507 | function device_has_ipv4() { |
508 | local device=${1} | |
509 | local addr=${2} | |
510 | ||
511 | if ! device_exists ${device}; then | |
512 | error "Device '${device}' does not exist." | |
513 | return ${EXIT_ERROR} | |
514 | fi | |
515 | ||
516 | ip addr show ${device} | grep -q -e "inet " -e "${addr}" | |
517 | } | |
4231f419 MT |
518 | |
519 | function device_has_ipv6() { | |
520 | local device=${1} | |
521 | local addr=${2} | |
522 | ||
523 | if ! device_exists ${device}; then | |
524 | error "Device '${device}' does not exist." | |
525 | return ${EXIT_ERROR} | |
526 | fi | |
527 | ||
528 | local prefix=${addr##*/} | |
529 | addr=$(ipv6_implode ${addr%%/*}) | |
530 | ||
531 | if [ -n "${prefix}" ]; then | |
532 | addr="${addr}/${prefix}" | |
533 | fi | |
534 | ||
535 | ip addr show ${device} | grep -q "inet6 ${addr}" | |
536 | } | |
711ffac1 | 537 | |
711ffac1 MT |
538 | function __device_get_file() { |
539 | local device=${1} | |
540 | local file=${2} | |
541 | ||
542 | assert isset device | |
543 | assert isset file | |
544 | ||
545 | cat ${SYS_CLASS_NET}/${device}/${file} | |
546 | } | |
547 | ||
548 | function device_get_rx_bytes() { | |
549 | local device=${1} | |
550 | ||
551 | __device_get_file ${device} statistics/rx_bytes | |
552 | } | |
553 | ||
554 | function device_get_tx_bytes() { | |
555 | local device=${1} | |
556 | ||
557 | __device_get_file ${device} statistics/tx_bytes | |
558 | } | |
559 | ||
560 | function device_get_rx_packets() { | |
561 | local device=${1} | |
562 | ||
563 | __device_get_file ${device} statistics/rx_packets | |
564 | } | |
565 | ||
566 | function device_get_tx_packets() { | |
567 | local device=${1} | |
568 | ||
569 | __device_get_file ${device} statistics/tx_packets | |
570 | } | |
571 | ||
572 | function device_get_rx_errors() { | |
573 | local device=${1} | |
574 | ||
575 | __device_get_file ${device} statistics/rx_errors | |
576 | } | |
577 | ||
578 | function device_get_tx_errors() { | |
579 | local device=${1} | |
580 | ||
581 | __device_get_file ${device} statistics/tx_errors | |
582 | } |