]>
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 | ||
6c74a64c MT |
68 | # Check for a normal network device. |
69 | [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK} | |
70 | ||
71 | # If the check above, did not find a result, | |
72 | # we check for serial devices. | |
73 | serial_exists ${device} | |
1848564d MT |
74 | } |
75 | ||
99be6026 MT |
76 | function device_delete() { |
77 | local device=${1} | |
78 | assert isset device | |
79 | ||
80 | # Nothing to do, it device does not exist. | |
81 | device_exists ${device} || return ${EXIT_OK} | |
82 | ||
83 | # Delete the device. | |
84 | cmd_quiet ip link delete ${device} | |
85 | local ret=$? | |
86 | ||
87 | if [ ${ret} -ne ${EXIT_OK} ]; then | |
88 | log ERROR "device: Could not delete device '${device}': ${ret}" | |
89 | return ${EXIT_ERROR} | |
90 | fi | |
91 | ||
92 | return ${ret} | |
93 | } | |
94 | ||
e369be1a MT |
95 | function device_has_flag() { |
96 | local device=${1} | |
97 | local flag=${2} | |
98 | ||
99 | local flags=$(__device_get_file ${device} flags) | |
100 | ||
101 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
102 | return ${EXIT_FALSE} | |
103 | else | |
104 | return ${EXIT_TRUE} | |
105 | fi | |
106 | } | |
107 | ||
1848564d MT |
108 | # Check if the device is up |
109 | function device_is_up() { | |
110 | local device=${1} | |
111 | ||
112 | device_exists ${device} || return ${EXIT_ERROR} | |
113 | ||
e369be1a | 114 | device_has_flag ${device} 0x1 |
1848564d MT |
115 | } |
116 | ||
99be6026 MT |
117 | function device_ifindex_to_name() { |
118 | local idx=${1} | |
119 | assert isset idx | |
120 | ||
121 | local device device_idx | |
122 | for device in ${SYS_CLASS_NET}/*; do | |
123 | device=$(basename ${device}) | |
124 | device_exists ${device} || continue | |
125 | ||
126 | device_idx=$(device_get_ifindex ${device}) | |
127 | ||
128 | if [ "${device_idx}" = "${idx}" ]; then | |
129 | print "${device}" | |
130 | return ${EXIT_OK} | |
131 | fi | |
132 | done | |
133 | ||
134 | return ${EXIT_ERROR} | |
135 | } | |
136 | ||
137 | function device_get_ifindex() { | |
138 | local device=${1} | |
139 | assert isset device | |
140 | ||
141 | local path="${SYS_CLASS_NET}/${1}/ifindex" | |
142 | ||
143 | # Check if file can be read. | |
144 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
145 | ||
146 | print "$(<${path})" | |
147 | } | |
148 | ||
e6993835 MT |
149 | # Check if the device is a batman-adv bridge |
150 | function device_is_batman_adv() { | |
151 | [ -d "${SYS_CLASS_NET}/${1}/mesh" ] | |
152 | } | |
153 | ||
154 | # Check if the device is a batman-adv bridge port | |
155 | function device_is_batman_adv_port() { | |
7b192cf4 MT |
156 | local device="${1}" |
157 | ||
158 | if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then | |
159 | local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)" | |
160 | ||
161 | case "${status}" in | |
162 | "active") | |
163 | return ${EXIT_TRUE} | |
164 | ;; | |
165 | *) | |
166 | return ${EXIT_FALSE} | |
167 | ;; | |
168 | esac | |
169 | fi | |
170 | ||
171 | return ${EXIT_FALSE} | |
e6993835 MT |
172 | } |
173 | ||
1848564d MT |
174 | # Check if the device is a bonding device |
175 | function device_is_bonding() { | |
176 | [ -d "/sys/class/net/${1}/bonding" ] | |
177 | } | |
178 | ||
179 | # Check if the device bonded in a bonding device | |
180 | function device_is_bonded() { | |
711ffac1 | 181 | local device=${1} |
1848564d | 182 | |
0959482b | 183 | [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] |
1848564d MT |
184 | } |
185 | ||
186 | # Check if the device is a bridge | |
187 | function device_is_bridge() { | |
188 | [ -d "/sys/class/net/${1}/bridge" ] | |
189 | } | |
190 | ||
81ed640c MT |
191 | function device_is_bridge_attached() { |
192 | local device=${1} | |
81ed640c MT |
193 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] |
194 | } | |
195 | ||
99be6026 MT |
196 | function device_get_bridge() { |
197 | local device=${1} | |
198 | assert isset device | |
199 | ||
200 | # Check if device is attached to a bridge. | |
201 | device_is_bridge_attached ${device} || return ${EXIT_ERROR} | |
202 | ||
203 | local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex" | |
204 | [ -r "${ifindex_path}" ] || return ${EXIT_ERROR} | |
205 | ||
206 | local ifindex=$(<${ifindex_path}) | |
207 | assert isset ifindex | |
208 | ||
209 | device_ifindex_to_name ${ifindex} | |
210 | } | |
211 | ||
7951525a MT |
212 | # Check if the device is a vlan device |
213 | function device_is_vlan() { | |
1848564d | 214 | local device=${1} |
7951525a | 215 | assert isset device |
1848564d | 216 | |
7951525a | 217 | [ -e "${PROC_NET_VLAN}/${device}" ] |
1848564d MT |
218 | } |
219 | ||
7951525a MT |
220 | # Check if the device has vlan devices |
221 | function device_has_vlans() { | |
fb02e543 | 222 | local device=${1} |
7951525a | 223 | assert isset device |
fb02e543 | 224 | |
7951525a | 225 | if device_is_vlan ${device}; then |
ec63256a | 226 | return ${EXIT_FALSE} |
fb02e543 MT |
227 | fi |
228 | ||
7951525a MT |
229 | local vlans=$(device_get_vlans ${device}) |
230 | [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
ec63256a MT |
231 | } |
232 | ||
7951525a | 233 | function device_get_vlans() { |
ec63256a | 234 | local device=${1} |
7951525a | 235 | assert isset device |
ec63256a | 236 | |
8357a7ff MT |
237 | # If no 8021q module has been loaded into the kernel, |
238 | # we cannot do anything. | |
7951525a | 239 | [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} |
8357a7ff | 240 | |
ec63256a MT |
241 | local dev spacer1 id spacer2 parent |
242 | while read dev spacer1 id spacer2 parent; do | |
7951525a MT |
243 | [ "${parent}" = "${device}" ] || continue |
244 | ||
245 | print "${dev}" | |
246 | done < ${PROC_NET_VLAN_CONFIG} | |
1848564d MT |
247 | } |
248 | ||
1848564d MT |
249 | # Check if the device is a ppp device |
250 | function device_is_ppp() { | |
251 | local device=${1} | |
252 | ||
55b802cc | 253 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 254 | |
e369be1a MT |
255 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
256 | } | |
55b802cc | 257 | |
e369be1a MT |
258 | # Check if the device is a pointopoint device. |
259 | function device_is_ptp() { | |
260 | local device=${1} | |
261 | ||
262 | device_has_flag ${device} 0x10 | |
1848564d MT |
263 | } |
264 | ||
265 | # Check if the device is a loopback device | |
266 | function device_is_loopback() { | |
5bb2429a MT |
267 | local device=${1} |
268 | ||
1848564d MT |
269 | [ "${device}" = "lo" ] |
270 | } | |
271 | ||
a508c27e MT |
272 | # Check if the device is a wireless device |
273 | function device_is_wireless() { | |
274 | local device=${1} | |
275 | ||
276 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
277 | } | |
278 | ||
4733a336 MT |
279 | function device_get_phy() { |
280 | local device="${1}" | |
281 | ||
282 | if device_is_wireless "${device}"; then | |
283 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
284 | return ${EXIT_OK} | |
285 | fi | |
286 | ||
287 | return ${EXIT_ERROR} | |
288 | } | |
289 | ||
6c74a64c MT |
290 | function device_is_serial() { |
291 | serial_exists $@ | |
292 | } | |
293 | ||
1848564d | 294 | # Check if the device is a physical network interface |
ec63256a | 295 | function device_is_ethernet() { |
1848564d MT |
296 | local device=${1} |
297 | ||
298 | device_is_loopback ${device} && \ | |
299 | return ${EXIT_ERROR} | |
300 | ||
301 | device_is_bonding ${device} && \ | |
302 | return ${EXIT_ERROR} | |
303 | ||
304 | device_is_bridge ${device} && \ | |
305 | return ${EXIT_ERROR} | |
306 | ||
307 | device_is_ppp ${device} && \ | |
308 | return ${EXIT_ERROR} | |
309 | ||
7951525a | 310 | device_is_vlan ${device} && \ |
1848564d MT |
311 | return ${EXIT_ERROR} |
312 | ||
419b4cd0 MT |
313 | [ "$(__device_get_file ${device} type)" != "1" ] && \ |
314 | return ${EXIT_ERROR} | |
315 | ||
1848564d MT |
316 | return ${EXIT_OK} |
317 | } | |
318 | ||
319 | # Get the device type | |
320 | function device_get_type() { | |
5bb2429a | 321 | local device=${1} |
1848564d | 322 | |
7951525a | 323 | if device_is_vlan ${device}; then |
1848564d MT |
324 | echo "vlan" |
325 | ||
326 | elif device_is_bonding ${device}; then | |
327 | echo "bonding" | |
328 | ||
329 | elif device_is_bridge ${device}; then | |
330 | echo "bridge" | |
331 | ||
332 | elif device_is_ppp ${device}; then | |
333 | echo "ppp" | |
334 | ||
e6993835 MT |
335 | elif device_is_batman_adv ${device}; then |
336 | echo "batman-adv" | |
337 | ||
338 | elif device_is_batman_adv_port ${device}; then | |
339 | echo "batman-adv-port" | |
340 | ||
1848564d MT |
341 | elif device_is_loopback ${device}; then |
342 | echo "loopback" | |
343 | ||
a508c27e MT |
344 | elif device_is_wireless ${device}; then |
345 | echo "wireless" | |
346 | ||
ec63256a MT |
347 | elif device_is_ethernet ${device}; then |
348 | echo "ethernet" | |
1848564d | 349 | |
6c74a64c MT |
350 | elif device_is_serial ${device}; then |
351 | echo "serial" | |
352 | ||
1848564d MT |
353 | else |
354 | echo "unknown" | |
355 | fi | |
356 | } | |
357 | ||
711ffac1 MT |
358 | function device_get_status() { |
359 | local device=${1} | |
711ffac1 MT |
360 | assert isset device |
361 | ||
3cb2fc42 | 362 | local status=${STATUS_DOWN} |
711ffac1 | 363 | |
3cb2fc42 | 364 | if device_is_up ${device}; then |
711ffac1 | 365 | status=${STATUS_UP} |
711ffac1 | 366 | |
3cb2fc42 MT |
367 | if ! device_has_carrier ${device}; then |
368 | status=${STATUS_NOCARRIER} | |
369 | fi | |
370 | fi | |
711ffac1 MT |
371 | |
372 | echo "${status}" | |
373 | } | |
374 | ||
1848564d MT |
375 | function device_get_address() { |
376 | local device=${1} | |
377 | ||
378 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
379 | } | |
380 | ||
381 | function device_set_address() { | |
08c5b789 MT |
382 | assert [ $# -eq 2 ] |
383 | ||
384 | local device="${1}" | |
385 | local addr="${2}" | |
1b7a1578 | 386 | |
08c5b789 | 387 | if ! device_exists "${device}"; then |
1b7a1578 MT |
388 | error "Device '${device}' does not exist." |
389 | return ${EXIT_ERROR} | |
390 | fi | |
391 | ||
08c5b789 MT |
392 | # Do nothing if the address has not changed |
393 | local old_addr="$(device_get_address "${device}")" | |
394 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
395 | return ${EXIT_OK} | |
396 | fi | |
397 | ||
398 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
399 | |
400 | local up | |
08c5b789 MT |
401 | if device_is_up "${device}"; then |
402 | device_set_down "${device}" | |
1b7a1578 MT |
403 | up=1 |
404 | fi | |
405 | ||
08c5b789 | 406 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
407 | local ret=$? |
408 | ||
409 | if [ "${up}" = "1" ]; then | |
08c5b789 | 410 | device_set_up "${device}" |
1b7a1578 MT |
411 | fi |
412 | ||
413 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 414 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
415 | fi |
416 | ||
417 | return ${ret} | |
1848564d MT |
418 | } |
419 | ||
711ffac1 | 420 | function device_get() { |
2ae0fb8d | 421 | local device |
711ffac1 MT |
422 | local devices |
423 | ||
2ae0fb8d MT |
424 | for device in ${SYS_CLASS_NET}/*; do |
425 | device=$(basename ${device}) | |
711ffac1 | 426 | |
2ae0fb8d MT |
427 | # bonding_masters is no device |
428 | [ "${device}" = "bonding_masters" ] && continue | |
429 | ||
430 | devices="${devices} ${device}" | |
431 | done | |
711ffac1 MT |
432 | |
433 | echo ${devices} | |
434 | return ${EXIT_OK} | |
435 | } | |
436 | ||
1848564d | 437 | function devices_get_all() { |
711ffac1 | 438 | device_get |
1848564d MT |
439 | } |
440 | ||
441 | # Check if a device has a cable plugged in | |
442 | function device_has_carrier() { | |
5bb2429a MT |
443 | local device=${1} |
444 | assert isset device | |
445 | ||
ec63256a MT |
446 | local carrier=$(__device_get_file ${device} carrier) |
447 | [ "${carrier}" = "1" ] | |
1848564d MT |
448 | } |
449 | ||
1e4c26a4 MT |
450 | function device_is_promisc() { |
451 | local device=${1} | |
452 | ||
e369be1a | 453 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
454 | } |
455 | ||
cf6e4606 MT |
456 | function device_set_promisc() { |
457 | local device=${1} | |
458 | local state=${2} | |
459 | ||
460 | assert device_exists ${device} | |
461 | assert isset state | |
462 | assert isoneof state on off | |
463 | ||
464 | ip link set ${device} promisc ${state} | |
465 | } | |
466 | ||
1848564d MT |
467 | # Check if the device is free |
468 | function device_is_free() { | |
81ed640c | 469 | ! device_is_used $@ |
1848564d MT |
470 | } |
471 | ||
472 | # Check if the device is used | |
473 | function device_is_used() { | |
5bb2429a | 474 | local device=${1} |
1848564d | 475 | |
7951525a | 476 | device_has_vlans ${device} && \ |
fb02e543 | 477 | return ${EXIT_OK} |
1848564d | 478 | device_is_bonded ${device} && \ |
fb02e543 | 479 | return ${EXIT_OK} |
81ed640c MT |
480 | device_is_bridge_attached ${device} && \ |
481 | return ${EXIT_OK} | |
1848564d | 482 | |
fb02e543 | 483 | return ${EXIT_ERROR} |
1848564d MT |
484 | } |
485 | ||
1b7a1578 MT |
486 | function device_hash() { |
487 | local device=${1} | |
488 | ||
37e4ec8b MT |
489 | # Get mac address of device and remove all colons (:) |
490 | # that will result in a hash. | |
491 | device=$(macify ${device}) | |
492 | ||
493 | echo "${device//:/}" | |
1b7a1578 MT |
494 | } |
495 | ||
496 | # Give the device a new name | |
497 | function device_set_name() { | |
1848564d | 498 | local source=$1 |
1578dae9 | 499 | local destination=${2} |
1848564d MT |
500 | |
501 | # Check if devices exists | |
502 | if ! device_exists ${source} || device_exists ${destination}; then | |
503 | return 4 | |
504 | fi | |
505 | ||
506 | local up | |
507 | if device_is_up ${source}; then | |
508 | ip link set ${source} down | |
509 | up=1 | |
510 | fi | |
511 | ||
512 | ip link set ${source} name ${destination} | |
513 | ||
514 | if [ "${up}" = "1" ]; then | |
515 | ip link set ${destination} up | |
516 | fi | |
517 | } | |
518 | ||
1848564d MT |
519 | # Set device up |
520 | function device_set_up() { | |
5bb2429a | 521 | local device=${1} |
1848564d | 522 | |
711ffac1 MT |
523 | # Silently fail if device was not found |
524 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
525 | ||
1848564d MT |
526 | # Do nothing if device is already up |
527 | device_is_up ${device} && return ${EXIT_OK} | |
528 | ||
81ed640c MT |
529 | device_set_parent_up ${device} |
530 | ||
531 | log DEBUG "Setting up device '${device}'" | |
532 | ||
1848564d MT |
533 | ip link set ${device} up |
534 | } | |
535 | ||
81ed640c MT |
536 | function device_set_parent_up() { |
537 | local device=${1} | |
538 | local parent | |
539 | ||
7951525a MT |
540 | if device_is_vlan ${device}; then |
541 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
542 | |
543 | device_is_up ${parent} && return ${EXIT_OK} | |
544 | ||
545 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
546 | ||
547 | device_set_up ${parent} | |
548 | return $? | |
549 | fi | |
550 | ||
551 | return ${EXIT_OK} | |
552 | } | |
553 | ||
1848564d MT |
554 | # Set device down |
555 | function device_set_down() { | |
5bb2429a MT |
556 | local device=${1} |
557 | assert isset device | |
1848564d | 558 | |
81ed640c MT |
559 | local ret=${EXIT_OK} |
560 | ||
561 | if device_is_up ${device}; then | |
562 | log DEBUG "Tearing down device '${device}'" | |
563 | ||
564 | ip link set ${device} down | |
565 | ret=$? | |
566 | fi | |
567 | ||
568 | device_set_parent_down ${device} | |
1848564d | 569 | |
81ed640c MT |
570 | return ${ret} |
571 | } | |
572 | ||
573 | function device_set_parent_down() { | |
574 | local device=${1} | |
575 | local parent | |
576 | ||
7951525a MT |
577 | if device_is_vlan ${device}; then |
578 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
579 | |
580 | device_is_up ${parent} || return ${EXIT_OK} | |
581 | ||
582 | if device_is_free ${parent}; then | |
583 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
584 | ||
585 | device_set_down ${parent} | |
586 | fi | |
587 | fi | |
588 | ||
589 | return ${EXIT_OK} | |
1848564d MT |
590 | } |
591 | ||
1848564d MT |
592 | function device_get_mtu() { |
593 | local device=${1} | |
594 | ||
595 | if ! device_exists ${device}; then | |
596 | error "Device '${device}' does not exist." | |
597 | return ${EXIT_ERROR} | |
598 | fi | |
599 | ||
f3e6fe50 | 600 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
601 | } |
602 | ||
603 | # Set mtu to a device | |
604 | function device_set_mtu() { | |
1b7a1578 | 605 | local device=${1} |
1848564d MT |
606 | local mtu=${2} |
607 | ||
1b7a1578 MT |
608 | if ! device_exists ${device}; then |
609 | error "Device '${device}' does not exist." | |
610 | return ${EXIT_ERROR} | |
611 | fi | |
612 | ||
613 | local oldmtu=$(device_get_mtu ${device}) | |
614 | ||
615 | if [ "${oldmtu}" = "${mtu}" ]; then | |
616 | # No need to set mtu. | |
617 | return ${EXIT_OK} | |
618 | fi | |
619 | ||
620 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
621 | ||
1848564d | 622 | local up |
1b7a1578 MT |
623 | if device_is_up ${device}; then |
624 | device_set_down ${device} | |
1848564d MT |
625 | up=1 |
626 | fi | |
627 | ||
1b7a1578 | 628 | ip link set ${device} mtu ${mtu} |
1848564d MT |
629 | local ret=$? |
630 | ||
631 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
632 | device_set_up ${device} |
633 | fi | |
634 | ||
635 | if [ "${ret}" != "0" ]; then | |
636 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
637 | fi |
638 | ||
639 | return ${ret} | |
640 | } | |
641 | ||
3ee5ccb1 MT |
642 | function device_adjust_mtu() { |
643 | assert [ $# -eq 2 ] | |
644 | ||
645 | local device="${1}" | |
646 | local other_device="${2}" | |
647 | ||
648 | local mtu="$(device_get_mtu "${other_device}")" | |
649 | device_set_mtu "${device}" "${mtu}" | |
650 | } | |
651 | ||
1848564d MT |
652 | function device_discover() { |
653 | local device=${1} | |
654 | ||
1b7a1578 MT |
655 | log INFO "Running discovery process on device '${device}'." |
656 | ||
1848564d | 657 | local hook |
d61a01d4 MT |
658 | for hook in $(hook_zone_get_all); do |
659 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
660 | done |
661 | } | |
662 | ||
38f61548 | 663 | function device_has_ip() { |
1848564d MT |
664 | local device=${1} |
665 | local addr=${2} | |
666 | ||
38f61548 MT |
667 | assert isset addr |
668 | assert device_exists ${device} | |
669 | ||
670 | # IPv6 addresses must be fully imploded | |
671 | local protocol=$(ip_detect_protocol ${addr}) | |
672 | case "${protocol}" in | |
673 | ipv6) | |
674 | addr=$(ipv6_implode ${addr}) | |
675 | ;; | |
676 | esac | |
1848564d | 677 | |
38f61548 | 678 | listmatch ${addr} $(device_get_addresses ${device}) |
1848564d | 679 | } |
4231f419 | 680 | |
38f61548 | 681 | function device_get_addresses() { |
4231f419 | 682 | local device=${1} |
4231f419 | 683 | |
38f61548 | 684 | assert device_exists ${device} |
4231f419 | 685 | |
38f61548 MT |
686 | local prot |
687 | local addr | |
688 | local line | |
689 | ip addr show ${device} | \ | |
690 | while read prot addr line; do | |
691 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
692 | done | |
4231f419 | 693 | } |
711ffac1 | 694 | |
711ffac1 MT |
695 | function __device_get_file() { |
696 | local device=${1} | |
697 | local file=${2} | |
698 | ||
699 | assert isset device | |
700 | assert isset file | |
701 | ||
e369be1a MT |
702 | local path="${SYS_CLASS_NET}/${device}/${file}" |
703 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
704 | ||
705 | echo "$(<${path})" | |
711ffac1 MT |
706 | } |
707 | ||
2c083d57 MT |
708 | function __device_set_file() { |
709 | assert [ $# -eq 3 ] | |
710 | ||
711 | local device="${1}" | |
712 | local file="${2}" | |
713 | local value="${3}" | |
714 | ||
715 | local path="${SYS_CLASS_NET}/${device}/${file}" | |
716 | if [ ! -w "${path}" ]; then | |
717 | log DEBUG "Cannot write to file '${file}' (${value})" | |
718 | return ${EXIT_ERROR} | |
719 | fi | |
720 | ||
721 | echo "${value}" > "${path}" | |
722 | } | |
723 | ||
711ffac1 MT |
724 | function device_get_rx_bytes() { |
725 | local device=${1} | |
726 | ||
727 | __device_get_file ${device} statistics/rx_bytes | |
728 | } | |
729 | ||
730 | function device_get_tx_bytes() { | |
731 | local device=${1} | |
732 | ||
733 | __device_get_file ${device} statistics/tx_bytes | |
734 | } | |
735 | ||
736 | function device_get_rx_packets() { | |
737 | local device=${1} | |
738 | ||
739 | __device_get_file ${device} statistics/rx_packets | |
740 | } | |
741 | ||
742 | function device_get_tx_packets() { | |
743 | local device=${1} | |
744 | ||
745 | __device_get_file ${device} statistics/tx_packets | |
746 | } | |
747 | ||
748 | function device_get_rx_errors() { | |
749 | local device=${1} | |
750 | ||
751 | __device_get_file ${device} statistics/rx_errors | |
752 | } | |
753 | ||
754 | function device_get_tx_errors() { | |
755 | local device=${1} | |
756 | ||
757 | __device_get_file ${device} statistics/tx_errors | |
758 | } | |
ec63256a MT |
759 | |
760 | function device_get_speed() { | |
761 | local device=${1} | |
762 | ||
763 | __device_get_file ${device} speed | |
764 | } | |
765 | ||
766 | function device_get_duplex() { | |
767 | local device=${1} | |
768 | ||
769 | __device_get_file ${device} duplex | |
770 | } |