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