]>
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 | ||
e369be1a MT |
71 | function device_has_flag() { |
72 | local device=${1} | |
73 | local flag=${2} | |
74 | ||
75 | local flags=$(__device_get_file ${device} flags) | |
76 | ||
77 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
78 | return ${EXIT_FALSE} | |
79 | else | |
80 | return ${EXIT_TRUE} | |
81 | fi | |
82 | } | |
83 | ||
1848564d MT |
84 | # Check if the device is up |
85 | function device_is_up() { | |
86 | local device=${1} | |
87 | ||
88 | device_exists ${device} || return ${EXIT_ERROR} | |
89 | ||
e369be1a | 90 | device_has_flag ${device} 0x1 |
1848564d MT |
91 | } |
92 | ||
93 | # Check if the device is a bonding device | |
94 | function device_is_bonding() { | |
95 | [ -d "/sys/class/net/${1}/bonding" ] | |
96 | } | |
97 | ||
98 | # Check if the device bonded in a bonding device | |
99 | function device_is_bonded() { | |
711ffac1 | 100 | local device=${1} |
1848564d | 101 | |
711ffac1 | 102 | [ -d "${SYS_CLASS_NET}/${device}/master" ] |
1848564d MT |
103 | } |
104 | ||
105 | # Check if the device is a bridge | |
106 | function device_is_bridge() { | |
107 | [ -d "/sys/class/net/${1}/bridge" ] | |
108 | } | |
109 | ||
81ed640c MT |
110 | function device_is_bridge_attached() { |
111 | local device=${1} | |
112 | ||
113 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] | |
114 | } | |
115 | ||
1848564d MT |
116 | # Check if the device is a virtual device |
117 | function device_is_virtual() { | |
118 | local device=${1} | |
119 | ||
120 | [ -e "/proc/net/vlan/${device}" ] | |
121 | } | |
122 | ||
123 | # Check if the device has virtual devices | |
124 | function device_has_virtuals() { | |
fb02e543 MT |
125 | local device=${1} |
126 | ||
127 | if device_is_virtual ${device}; then | |
ec63256a | 128 | return ${EXIT_FALSE} |
fb02e543 MT |
129 | fi |
130 | ||
ec63256a MT |
131 | local virtuals=$(device_get_virtuals ${device}) |
132 | [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
133 | } | |
134 | ||
135 | function device_get_virtuals() { | |
136 | local device=${1} | |
137 | ||
138 | local dev spacer1 id spacer2 parent | |
139 | while read dev spacer1 id spacer2 parent; do | |
140 | [ "${parent}" = "${device}" ] && echo "${dev}" | |
141 | done < /proc/net/vlan/config | sort | |
1848564d MT |
142 | } |
143 | ||
1848564d MT |
144 | # Check if the device is a ppp device |
145 | function device_is_ppp() { | |
146 | local device=${1} | |
147 | ||
55b802cc | 148 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 149 | |
e369be1a MT |
150 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
151 | } | |
55b802cc | 152 | |
e369be1a MT |
153 | # Check if the device is a pointopoint device. |
154 | function device_is_ptp() { | |
155 | local device=${1} | |
156 | ||
157 | device_has_flag ${device} 0x10 | |
1848564d MT |
158 | } |
159 | ||
160 | # Check if the device is a loopback device | |
161 | function device_is_loopback() { | |
5bb2429a MT |
162 | local device=${1} |
163 | ||
1848564d MT |
164 | [ "${device}" = "lo" ] |
165 | } | |
166 | ||
a508c27e MT |
167 | # Check if the device is a wireless device |
168 | function device_is_wireless() { | |
169 | local device=${1} | |
170 | ||
171 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
172 | } | |
173 | ||
1848564d | 174 | # Check if the device is a physical network interface |
ec63256a | 175 | function device_is_ethernet() { |
1848564d MT |
176 | local device=${1} |
177 | ||
178 | device_is_loopback ${device} && \ | |
179 | return ${EXIT_ERROR} | |
180 | ||
181 | device_is_bonding ${device} && \ | |
182 | return ${EXIT_ERROR} | |
183 | ||
184 | device_is_bridge ${device} && \ | |
185 | return ${EXIT_ERROR} | |
186 | ||
187 | device_is_ppp ${device} && \ | |
188 | return ${EXIT_ERROR} | |
189 | ||
190 | device_is_virtual ${device} && \ | |
191 | return ${EXIT_ERROR} | |
192 | ||
419b4cd0 MT |
193 | [ "$(__device_get_file ${device} type)" != "1" ] && \ |
194 | return ${EXIT_ERROR} | |
195 | ||
1848564d MT |
196 | return ${EXIT_OK} |
197 | } | |
198 | ||
199 | # Get the device type | |
200 | function device_get_type() { | |
5bb2429a | 201 | local device=${1} |
1848564d | 202 | |
8c6a8966 | 203 | if device_is_virtual ${device}; then |
1848564d MT |
204 | echo "vlan" |
205 | ||
206 | elif device_is_bonding ${device}; then | |
207 | echo "bonding" | |
208 | ||
209 | elif device_is_bridge ${device}; then | |
210 | echo "bridge" | |
211 | ||
212 | elif device_is_ppp ${device}; then | |
213 | echo "ppp" | |
214 | ||
215 | elif device_is_loopback ${device}; then | |
216 | echo "loopback" | |
217 | ||
a508c27e MT |
218 | elif device_is_wireless ${device}; then |
219 | echo "wireless" | |
220 | ||
ec63256a MT |
221 | elif device_is_ethernet ${device}; then |
222 | echo "ethernet" | |
1848564d MT |
223 | |
224 | else | |
225 | echo "unknown" | |
226 | fi | |
227 | } | |
228 | ||
711ffac1 MT |
229 | function device_get_status() { |
230 | local device=${1} | |
711ffac1 MT |
231 | assert isset device |
232 | ||
3cb2fc42 | 233 | local status=${STATUS_DOWN} |
711ffac1 | 234 | |
3cb2fc42 | 235 | if device_is_up ${device}; then |
711ffac1 | 236 | status=${STATUS_UP} |
711ffac1 | 237 | |
3cb2fc42 MT |
238 | if ! device_has_carrier ${device}; then |
239 | status=${STATUS_NOCARRIER} | |
240 | fi | |
241 | fi | |
711ffac1 MT |
242 | |
243 | echo "${status}" | |
244 | } | |
245 | ||
1848564d MT |
246 | function device_get_address() { |
247 | local device=${1} | |
248 | ||
249 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
250 | } | |
251 | ||
252 | function device_set_address() { | |
1b7a1578 MT |
253 | local device=${1} |
254 | local addr=${2} | |
255 | ||
256 | if ! device_exists ${device}; then | |
257 | error "Device '${device}' does not exist." | |
258 | return ${EXIT_ERROR} | |
259 | fi | |
260 | ||
261 | log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})." | |
262 | ||
263 | local up | |
264 | if device_is_up ${device}; then | |
265 | device_set_down ${device} | |
266 | up=1 | |
267 | fi | |
268 | ||
269 | ip link set ${device} address ${addr} | |
270 | local ret=$? | |
271 | ||
272 | if [ "${up}" = "1" ]; then | |
273 | device_set_up ${device} | |
274 | fi | |
275 | ||
276 | if [ "${ret}" != "0" ]; then | |
277 | error_log "Could not set address '${addr}' on device '${device}'." | |
278 | fi | |
279 | ||
280 | return ${ret} | |
1848564d MT |
281 | } |
282 | ||
711ffac1 | 283 | function device_get() { |
2ae0fb8d | 284 | local device |
711ffac1 MT |
285 | local devices |
286 | ||
2ae0fb8d MT |
287 | for device in ${SYS_CLASS_NET}/*; do |
288 | device=$(basename ${device}) | |
711ffac1 | 289 | |
2ae0fb8d MT |
290 | # bonding_masters is no device |
291 | [ "${device}" = "bonding_masters" ] && continue | |
292 | ||
293 | devices="${devices} ${device}" | |
294 | done | |
711ffac1 MT |
295 | |
296 | echo ${devices} | |
297 | return ${EXIT_OK} | |
298 | } | |
299 | ||
1848564d | 300 | function devices_get_all() { |
711ffac1 | 301 | device_get |
1848564d MT |
302 | } |
303 | ||
304 | # Check if a device has a cable plugged in | |
305 | function device_has_carrier() { | |
5bb2429a MT |
306 | local device=${1} |
307 | assert isset device | |
308 | ||
ec63256a MT |
309 | local carrier=$(__device_get_file ${device} carrier) |
310 | [ "${carrier}" = "1" ] | |
1848564d MT |
311 | } |
312 | ||
1e4c26a4 MT |
313 | function device_is_promisc() { |
314 | local device=${1} | |
315 | ||
e369be1a | 316 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
317 | } |
318 | ||
cf6e4606 MT |
319 | function device_set_promisc() { |
320 | local device=${1} | |
321 | local state=${2} | |
322 | ||
323 | assert device_exists ${device} | |
324 | assert isset state | |
325 | assert isoneof state on off | |
326 | ||
327 | ip link set ${device} promisc ${state} | |
328 | } | |
329 | ||
1848564d MT |
330 | # Check if the device is free |
331 | function device_is_free() { | |
81ed640c | 332 | ! device_is_used $@ |
1848564d MT |
333 | } |
334 | ||
335 | # Check if the device is used | |
336 | function device_is_used() { | |
5bb2429a | 337 | local device=${1} |
1848564d | 338 | |
fb02e543 MT |
339 | device_has_virtuals ${device} && \ |
340 | return ${EXIT_OK} | |
1848564d | 341 | device_is_bonded ${device} && \ |
fb02e543 | 342 | return ${EXIT_OK} |
81ed640c MT |
343 | device_is_bridge_attached ${device} && \ |
344 | return ${EXIT_OK} | |
1848564d | 345 | |
fb02e543 | 346 | return ${EXIT_ERROR} |
1848564d MT |
347 | } |
348 | ||
1b7a1578 MT |
349 | function device_hash() { |
350 | local device=${1} | |
351 | ||
37e4ec8b MT |
352 | # Get mac address of device and remove all colons (:) |
353 | # that will result in a hash. | |
354 | device=$(macify ${device}) | |
355 | ||
356 | echo "${device//:/}" | |
1b7a1578 MT |
357 | } |
358 | ||
359 | # Give the device a new name | |
360 | function device_set_name() { | |
1848564d | 361 | local source=$1 |
1578dae9 | 362 | local destination=${2} |
1848564d MT |
363 | |
364 | # Check if devices exists | |
365 | if ! device_exists ${source} || device_exists ${destination}; then | |
366 | return 4 | |
367 | fi | |
368 | ||
369 | local up | |
370 | if device_is_up ${source}; then | |
371 | ip link set ${source} down | |
372 | up=1 | |
373 | fi | |
374 | ||
375 | ip link set ${source} name ${destination} | |
376 | ||
377 | if [ "${up}" = "1" ]; then | |
378 | ip link set ${destination} up | |
379 | fi | |
380 | } | |
381 | ||
1848564d MT |
382 | # Set device up |
383 | function device_set_up() { | |
5bb2429a | 384 | local device=${1} |
1848564d | 385 | |
711ffac1 MT |
386 | # Silently fail if device was not found |
387 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
388 | ||
1848564d MT |
389 | # Do nothing if device is already up |
390 | device_is_up ${device} && return ${EXIT_OK} | |
391 | ||
81ed640c MT |
392 | device_set_parent_up ${device} |
393 | ||
394 | log DEBUG "Setting up device '${device}'" | |
395 | ||
1848564d MT |
396 | ip link set ${device} up |
397 | } | |
398 | ||
81ed640c MT |
399 | function device_set_parent_up() { |
400 | local device=${1} | |
401 | local parent | |
402 | ||
403 | if device_is_virtual ${device}; then | |
8c6a8966 | 404 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
405 | |
406 | device_is_up ${parent} && return ${EXIT_OK} | |
407 | ||
408 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
409 | ||
410 | device_set_up ${parent} | |
411 | return $? | |
412 | fi | |
413 | ||
414 | return ${EXIT_OK} | |
415 | } | |
416 | ||
1848564d MT |
417 | # Set device down |
418 | function device_set_down() { | |
5bb2429a MT |
419 | local device=${1} |
420 | assert isset device | |
1848564d | 421 | |
81ed640c MT |
422 | local ret=${EXIT_OK} |
423 | ||
424 | if device_is_up ${device}; then | |
425 | log DEBUG "Tearing down device '${device}'" | |
426 | ||
427 | ip link set ${device} down | |
428 | ret=$? | |
429 | fi | |
430 | ||
431 | device_set_parent_down ${device} | |
1848564d | 432 | |
81ed640c MT |
433 | return ${ret} |
434 | } | |
435 | ||
436 | function device_set_parent_down() { | |
437 | local device=${1} | |
438 | local parent | |
439 | ||
440 | if device_is_virtual ${device}; then | |
8c6a8966 | 441 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
442 | |
443 | device_is_up ${parent} || return ${EXIT_OK} | |
444 | ||
445 | if device_is_free ${parent}; then | |
446 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
447 | ||
448 | device_set_down ${parent} | |
449 | fi | |
450 | fi | |
451 | ||
452 | return ${EXIT_OK} | |
1848564d MT |
453 | } |
454 | ||
1848564d MT |
455 | function device_get_mtu() { |
456 | local device=${1} | |
457 | ||
458 | if ! device_exists ${device}; then | |
459 | error "Device '${device}' does not exist." | |
460 | return ${EXIT_ERROR} | |
461 | fi | |
462 | ||
f3e6fe50 | 463 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
464 | } |
465 | ||
466 | # Set mtu to a device | |
467 | function device_set_mtu() { | |
1b7a1578 | 468 | local device=${1} |
1848564d MT |
469 | local mtu=${2} |
470 | ||
1b7a1578 MT |
471 | if ! device_exists ${device}; then |
472 | error "Device '${device}' does not exist." | |
473 | return ${EXIT_ERROR} | |
474 | fi | |
475 | ||
476 | local oldmtu=$(device_get_mtu ${device}) | |
477 | ||
478 | if [ "${oldmtu}" = "${mtu}" ]; then | |
479 | # No need to set mtu. | |
480 | return ${EXIT_OK} | |
481 | fi | |
482 | ||
483 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
484 | ||
1848564d | 485 | local up |
1b7a1578 MT |
486 | if device_is_up ${device}; then |
487 | device_set_down ${device} | |
1848564d MT |
488 | up=1 |
489 | fi | |
490 | ||
1b7a1578 | 491 | ip link set ${device} mtu ${mtu} |
1848564d MT |
492 | local ret=$? |
493 | ||
494 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
495 | device_set_up ${device} |
496 | fi | |
497 | ||
498 | if [ "${ret}" != "0" ]; then | |
499 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
500 | fi |
501 | ||
502 | return ${ret} | |
503 | } | |
504 | ||
505 | function device_discover() { | |
506 | local device=${1} | |
507 | ||
1b7a1578 MT |
508 | log INFO "Running discovery process on device '${device}'." |
509 | ||
1848564d | 510 | local hook |
d61a01d4 MT |
511 | for hook in $(hook_zone_get_all); do |
512 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
513 | done |
514 | } | |
515 | ||
38f61548 | 516 | function device_has_ip() { |
1848564d MT |
517 | local device=${1} |
518 | local addr=${2} | |
519 | ||
38f61548 MT |
520 | assert isset addr |
521 | assert device_exists ${device} | |
522 | ||
523 | # IPv6 addresses must be fully imploded | |
524 | local protocol=$(ip_detect_protocol ${addr}) | |
525 | case "${protocol}" in | |
526 | ipv6) | |
527 | addr=$(ipv6_implode ${addr}) | |
528 | ;; | |
529 | esac | |
1848564d | 530 | |
38f61548 | 531 | listmatch ${addr} $(device_get_addresses ${device}) |
1848564d | 532 | } |
4231f419 | 533 | |
38f61548 | 534 | function device_get_addresses() { |
4231f419 | 535 | local device=${1} |
4231f419 | 536 | |
38f61548 | 537 | assert device_exists ${device} |
4231f419 | 538 | |
38f61548 MT |
539 | local prot |
540 | local addr | |
541 | local line | |
542 | ip addr show ${device} | \ | |
543 | while read prot addr line; do | |
544 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
545 | done | |
4231f419 | 546 | } |
711ffac1 | 547 | |
711ffac1 MT |
548 | function __device_get_file() { |
549 | local device=${1} | |
550 | local file=${2} | |
551 | ||
552 | assert isset device | |
553 | assert isset file | |
554 | ||
e369be1a MT |
555 | local path="${SYS_CLASS_NET}/${device}/${file}" |
556 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
557 | ||
558 | echo "$(<${path})" | |
711ffac1 MT |
559 | } |
560 | ||
561 | function device_get_rx_bytes() { | |
562 | local device=${1} | |
563 | ||
564 | __device_get_file ${device} statistics/rx_bytes | |
565 | } | |
566 | ||
567 | function device_get_tx_bytes() { | |
568 | local device=${1} | |
569 | ||
570 | __device_get_file ${device} statistics/tx_bytes | |
571 | } | |
572 | ||
573 | function device_get_rx_packets() { | |
574 | local device=${1} | |
575 | ||
576 | __device_get_file ${device} statistics/rx_packets | |
577 | } | |
578 | ||
579 | function device_get_tx_packets() { | |
580 | local device=${1} | |
581 | ||
582 | __device_get_file ${device} statistics/tx_packets | |
583 | } | |
584 | ||
585 | function device_get_rx_errors() { | |
586 | local device=${1} | |
587 | ||
588 | __device_get_file ${device} statistics/rx_errors | |
589 | } | |
590 | ||
591 | function device_get_tx_errors() { | |
592 | local device=${1} | |
593 | ||
594 | __device_get_file ${device} statistics/tx_errors | |
595 | } | |
ec63256a MT |
596 | |
597 | function device_get_speed() { | |
598 | local device=${1} | |
599 | ||
600 | __device_get_file ${device} speed | |
601 | } | |
602 | ||
603 | function device_get_duplex() { | |
604 | local device=${1} | |
605 | ||
606 | __device_get_file ${device} duplex | |
607 | } |