]>
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 | ||
a508c27e MT |
301 | # Check if the device is a wireless device |
302 | function device_is_wireless() { | |
303 | local device=${1} | |
304 | ||
305 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
306 | } | |
307 | ||
4733a336 MT |
308 | function device_get_phy() { |
309 | local device="${1}" | |
310 | ||
311 | if device_is_wireless "${device}"; then | |
312 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
313 | return ${EXIT_OK} | |
314 | fi | |
315 | ||
316 | return ${EXIT_ERROR} | |
317 | } | |
318 | ||
6c74a64c MT |
319 | function device_is_serial() { |
320 | serial_exists $@ | |
321 | } | |
322 | ||
1848564d | 323 | # Check if the device is a physical network interface |
ec63256a | 324 | function device_is_ethernet() { |
1848564d MT |
325 | local device=${1} |
326 | ||
327 | device_is_loopback ${device} && \ | |
328 | return ${EXIT_ERROR} | |
329 | ||
330 | device_is_bonding ${device} && \ | |
331 | return ${EXIT_ERROR} | |
332 | ||
333 | device_is_bridge ${device} && \ | |
334 | return ${EXIT_ERROR} | |
335 | ||
336 | device_is_ppp ${device} && \ | |
337 | return ${EXIT_ERROR} | |
338 | ||
7951525a | 339 | device_is_vlan ${device} && \ |
1848564d MT |
340 | return ${EXIT_ERROR} |
341 | ||
419b4cd0 MT |
342 | [ "$(__device_get_file ${device} type)" != "1" ] && \ |
343 | return ${EXIT_ERROR} | |
344 | ||
1848564d MT |
345 | return ${EXIT_OK} |
346 | } | |
347 | ||
348 | # Get the device type | |
349 | function device_get_type() { | |
5bb2429a | 350 | local device=${1} |
1848564d | 351 | |
b9f27baf MT |
352 | # If the device does not exist (happens on udev remove events), |
353 | # we do not bother to run all checks. | |
354 | if ! device_exists "${device}"; then | |
355 | echo "unknown" | |
356 | ||
357 | elif device_is_vlan ${device}; then | |
1848564d MT |
358 | echo "vlan" |
359 | ||
360 | elif device_is_bonding ${device}; then | |
361 | echo "bonding" | |
362 | ||
363 | elif device_is_bridge ${device}; then | |
364 | echo "bridge" | |
365 | ||
366 | elif device_is_ppp ${device}; then | |
367 | echo "ppp" | |
368 | ||
e6993835 MT |
369 | elif device_is_batman_adv ${device}; then |
370 | echo "batman-adv" | |
371 | ||
1848564d MT |
372 | elif device_is_loopback ${device}; then |
373 | echo "loopback" | |
374 | ||
b8026986 MT |
375 | elif device_is_wireless_adhoc ${device}; then |
376 | echo "wireless-adhoc" | |
377 | ||
a508c27e MT |
378 | elif device_is_wireless ${device}; then |
379 | echo "wireless" | |
380 | ||
ec63256a MT |
381 | elif device_is_ethernet ${device}; then |
382 | echo "ethernet" | |
1848564d | 383 | |
6c74a64c MT |
384 | elif device_is_serial ${device}; then |
385 | echo "serial" | |
386 | ||
1848564d MT |
387 | else |
388 | echo "unknown" | |
389 | fi | |
390 | } | |
391 | ||
711ffac1 MT |
392 | function device_get_status() { |
393 | local device=${1} | |
711ffac1 MT |
394 | assert isset device |
395 | ||
3cb2fc42 | 396 | local status=${STATUS_DOWN} |
711ffac1 | 397 | |
3cb2fc42 | 398 | if device_is_up ${device}; then |
711ffac1 | 399 | status=${STATUS_UP} |
711ffac1 | 400 | |
3cb2fc42 MT |
401 | if ! device_has_carrier ${device}; then |
402 | status=${STATUS_NOCARRIER} | |
403 | fi | |
404 | fi | |
711ffac1 MT |
405 | |
406 | echo "${status}" | |
407 | } | |
408 | ||
1848564d MT |
409 | function device_get_address() { |
410 | local device=${1} | |
411 | ||
412 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
413 | } | |
414 | ||
415 | function device_set_address() { | |
08c5b789 MT |
416 | assert [ $# -eq 2 ] |
417 | ||
418 | local device="${1}" | |
419 | local addr="${2}" | |
1b7a1578 | 420 | |
08c5b789 | 421 | if ! device_exists "${device}"; then |
1b7a1578 MT |
422 | error "Device '${device}' does not exist." |
423 | return ${EXIT_ERROR} | |
424 | fi | |
425 | ||
08c5b789 MT |
426 | # Do nothing if the address has not changed |
427 | local old_addr="$(device_get_address "${device}")" | |
428 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
429 | return ${EXIT_OK} | |
430 | fi | |
431 | ||
432 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
433 | |
434 | local up | |
08c5b789 MT |
435 | if device_is_up "${device}"; then |
436 | device_set_down "${device}" | |
1b7a1578 MT |
437 | up=1 |
438 | fi | |
439 | ||
08c5b789 | 440 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
441 | local ret=$? |
442 | ||
443 | if [ "${up}" = "1" ]; then | |
08c5b789 | 444 | device_set_up "${device}" |
1b7a1578 MT |
445 | fi |
446 | ||
447 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 448 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
449 | fi |
450 | ||
451 | return ${ret} | |
1848564d MT |
452 | } |
453 | ||
711ffac1 | 454 | function device_get() { |
2ae0fb8d | 455 | local device |
711ffac1 MT |
456 | local devices |
457 | ||
2ae0fb8d MT |
458 | for device in ${SYS_CLASS_NET}/*; do |
459 | device=$(basename ${device}) | |
711ffac1 | 460 | |
2ae0fb8d MT |
461 | # bonding_masters is no device |
462 | [ "${device}" = "bonding_masters" ] && continue | |
463 | ||
464 | devices="${devices} ${device}" | |
465 | done | |
711ffac1 MT |
466 | |
467 | echo ${devices} | |
468 | return ${EXIT_OK} | |
469 | } | |
470 | ||
1848564d | 471 | function devices_get_all() { |
711ffac1 | 472 | device_get |
1848564d MT |
473 | } |
474 | ||
475 | # Check if a device has a cable plugged in | |
476 | function device_has_carrier() { | |
5bb2429a MT |
477 | local device=${1} |
478 | assert isset device | |
479 | ||
ec63256a MT |
480 | local carrier=$(__device_get_file ${device} carrier) |
481 | [ "${carrier}" = "1" ] | |
1848564d MT |
482 | } |
483 | ||
1e4c26a4 MT |
484 | function device_is_promisc() { |
485 | local device=${1} | |
486 | ||
e369be1a | 487 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
488 | } |
489 | ||
cf6e4606 MT |
490 | function device_set_promisc() { |
491 | local device=${1} | |
492 | local state=${2} | |
493 | ||
494 | assert device_exists ${device} | |
495 | assert isset state | |
496 | assert isoneof state on off | |
497 | ||
498 | ip link set ${device} promisc ${state} | |
499 | } | |
500 | ||
1848564d MT |
501 | # Check if the device is free |
502 | function device_is_free() { | |
81ed640c | 503 | ! device_is_used $@ |
1848564d MT |
504 | } |
505 | ||
506 | # Check if the device is used | |
507 | function device_is_used() { | |
5bb2429a | 508 | local device=${1} |
1848564d | 509 | |
7951525a | 510 | device_has_vlans ${device} && \ |
fb02e543 | 511 | return ${EXIT_OK} |
1848564d | 512 | device_is_bonded ${device} && \ |
fb02e543 | 513 | return ${EXIT_OK} |
81ed640c MT |
514 | device_is_bridge_attached ${device} && \ |
515 | return ${EXIT_OK} | |
1848564d | 516 | |
fb02e543 | 517 | return ${EXIT_ERROR} |
1848564d MT |
518 | } |
519 | ||
1b7a1578 MT |
520 | function device_hash() { |
521 | local device=${1} | |
522 | ||
37e4ec8b MT |
523 | # Get mac address of device and remove all colons (:) |
524 | # that will result in a hash. | |
525 | device=$(macify ${device}) | |
526 | ||
527 | echo "${device//:/}" | |
1b7a1578 MT |
528 | } |
529 | ||
530 | # Give the device a new name | |
531 | function device_set_name() { | |
1848564d | 532 | local source=$1 |
1578dae9 | 533 | local destination=${2} |
1848564d MT |
534 | |
535 | # Check if devices exists | |
536 | if ! device_exists ${source} || device_exists ${destination}; then | |
537 | return 4 | |
538 | fi | |
539 | ||
540 | local up | |
541 | if device_is_up ${source}; then | |
542 | ip link set ${source} down | |
543 | up=1 | |
544 | fi | |
545 | ||
546 | ip link set ${source} name ${destination} | |
547 | ||
548 | if [ "${up}" = "1" ]; then | |
549 | ip link set ${destination} up | |
550 | fi | |
551 | } | |
552 | ||
1848564d MT |
553 | # Set device up |
554 | function device_set_up() { | |
5bb2429a | 555 | local device=${1} |
1848564d | 556 | |
711ffac1 MT |
557 | # Silently fail if device was not found |
558 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
559 | ||
1848564d MT |
560 | # Do nothing if device is already up |
561 | device_is_up ${device} && return ${EXIT_OK} | |
562 | ||
81ed640c MT |
563 | device_set_parent_up ${device} |
564 | ||
565 | log DEBUG "Setting up device '${device}'" | |
566 | ||
1848564d MT |
567 | ip link set ${device} up |
568 | } | |
569 | ||
81ed640c MT |
570 | function device_set_parent_up() { |
571 | local device=${1} | |
572 | local parent | |
573 | ||
7951525a MT |
574 | if device_is_vlan ${device}; then |
575 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
576 | |
577 | device_is_up ${parent} && return ${EXIT_OK} | |
578 | ||
579 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
580 | ||
581 | device_set_up ${parent} | |
582 | return $? | |
583 | fi | |
584 | ||
585 | return ${EXIT_OK} | |
586 | } | |
587 | ||
1848564d MT |
588 | # Set device down |
589 | function device_set_down() { | |
5bb2429a MT |
590 | local device=${1} |
591 | assert isset device | |
1848564d | 592 | |
81ed640c MT |
593 | local ret=${EXIT_OK} |
594 | ||
595 | if device_is_up ${device}; then | |
596 | log DEBUG "Tearing down device '${device}'" | |
597 | ||
598 | ip link set ${device} down | |
599 | ret=$? | |
600 | fi | |
601 | ||
602 | device_set_parent_down ${device} | |
1848564d | 603 | |
81ed640c MT |
604 | return ${ret} |
605 | } | |
606 | ||
607 | function device_set_parent_down() { | |
608 | local device=${1} | |
609 | local parent | |
610 | ||
7951525a MT |
611 | if device_is_vlan ${device}; then |
612 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
613 | |
614 | device_is_up ${parent} || return ${EXIT_OK} | |
615 | ||
616 | if device_is_free ${parent}; then | |
617 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
618 | ||
619 | device_set_down ${parent} | |
620 | fi | |
621 | fi | |
622 | ||
623 | return ${EXIT_OK} | |
1848564d MT |
624 | } |
625 | ||
1848564d MT |
626 | function device_get_mtu() { |
627 | local device=${1} | |
628 | ||
629 | if ! device_exists ${device}; then | |
630 | error "Device '${device}' does not exist." | |
631 | return ${EXIT_ERROR} | |
632 | fi | |
633 | ||
f3e6fe50 | 634 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
635 | } |
636 | ||
637 | # Set mtu to a device | |
638 | function device_set_mtu() { | |
1b7a1578 | 639 | local device=${1} |
1848564d MT |
640 | local mtu=${2} |
641 | ||
1b7a1578 MT |
642 | if ! device_exists ${device}; then |
643 | error "Device '${device}' does not exist." | |
644 | return ${EXIT_ERROR} | |
645 | fi | |
646 | ||
647 | local oldmtu=$(device_get_mtu ${device}) | |
648 | ||
649 | if [ "${oldmtu}" = "${mtu}" ]; then | |
650 | # No need to set mtu. | |
651 | return ${EXIT_OK} | |
652 | fi | |
653 | ||
654 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
655 | ||
1848564d | 656 | local up |
1b7a1578 MT |
657 | if device_is_up ${device}; then |
658 | device_set_down ${device} | |
1848564d MT |
659 | up=1 |
660 | fi | |
661 | ||
1b7a1578 | 662 | ip link set ${device} mtu ${mtu} |
1848564d MT |
663 | local ret=$? |
664 | ||
665 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
666 | device_set_up ${device} |
667 | fi | |
668 | ||
669 | if [ "${ret}" != "0" ]; then | |
670 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
671 | fi |
672 | ||
673 | return ${ret} | |
674 | } | |
675 | ||
3ee5ccb1 MT |
676 | function device_adjust_mtu() { |
677 | assert [ $# -eq 2 ] | |
678 | ||
679 | local device="${1}" | |
680 | local other_device="${2}" | |
681 | ||
682 | local mtu="$(device_get_mtu "${other_device}")" | |
683 | device_set_mtu "${device}" "${mtu}" | |
684 | } | |
685 | ||
1848564d MT |
686 | function device_discover() { |
687 | local device=${1} | |
688 | ||
1b7a1578 MT |
689 | log INFO "Running discovery process on device '${device}'." |
690 | ||
1848564d | 691 | local hook |
d61a01d4 MT |
692 | for hook in $(hook_zone_get_all); do |
693 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
694 | done |
695 | } | |
696 | ||
38f61548 | 697 | function device_has_ip() { |
1848564d MT |
698 | local device=${1} |
699 | local addr=${2} | |
700 | ||
38f61548 MT |
701 | assert isset addr |
702 | assert device_exists ${device} | |
703 | ||
704 | # IPv6 addresses must be fully imploded | |
705 | local protocol=$(ip_detect_protocol ${addr}) | |
706 | case "${protocol}" in | |
707 | ipv6) | |
708 | addr=$(ipv6_implode ${addr}) | |
709 | ;; | |
710 | esac | |
1848564d | 711 | |
38f61548 | 712 | listmatch ${addr} $(device_get_addresses ${device}) |
1848564d | 713 | } |
4231f419 | 714 | |
38f61548 | 715 | function device_get_addresses() { |
4231f419 | 716 | local device=${1} |
4231f419 | 717 | |
38f61548 | 718 | assert device_exists ${device} |
4231f419 | 719 | |
38f61548 MT |
720 | local prot |
721 | local addr | |
722 | local line | |
723 | ip addr show ${device} | \ | |
724 | while read prot addr line; do | |
725 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
726 | done | |
4231f419 | 727 | } |
711ffac1 | 728 | |
711ffac1 MT |
729 | function __device_get_file() { |
730 | local device=${1} | |
731 | local file=${2} | |
732 | ||
733 | assert isset device | |
734 | assert isset file | |
735 | ||
e369be1a MT |
736 | local path="${SYS_CLASS_NET}/${device}/${file}" |
737 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
738 | ||
739 | echo "$(<${path})" | |
711ffac1 MT |
740 | } |
741 | ||
2c083d57 MT |
742 | function __device_set_file() { |
743 | assert [ $# -eq 3 ] | |
744 | ||
745 | local device="${1}" | |
746 | local file="${2}" | |
747 | local value="${3}" | |
748 | ||
749 | local path="${SYS_CLASS_NET}/${device}/${file}" | |
750 | if [ ! -w "${path}" ]; then | |
751 | log DEBUG "Cannot write to file '${file}' (${value})" | |
752 | return ${EXIT_ERROR} | |
753 | fi | |
754 | ||
755 | echo "${value}" > "${path}" | |
756 | } | |
757 | ||
711ffac1 MT |
758 | function device_get_rx_bytes() { |
759 | local device=${1} | |
760 | ||
761 | __device_get_file ${device} statistics/rx_bytes | |
762 | } | |
763 | ||
764 | function device_get_tx_bytes() { | |
765 | local device=${1} | |
766 | ||
767 | __device_get_file ${device} statistics/tx_bytes | |
768 | } | |
769 | ||
770 | function device_get_rx_packets() { | |
771 | local device=${1} | |
772 | ||
773 | __device_get_file ${device} statistics/rx_packets | |
774 | } | |
775 | ||
776 | function device_get_tx_packets() { | |
777 | local device=${1} | |
778 | ||
779 | __device_get_file ${device} statistics/tx_packets | |
780 | } | |
781 | ||
782 | function device_get_rx_errors() { | |
783 | local device=${1} | |
784 | ||
785 | __device_get_file ${device} statistics/rx_errors | |
786 | } | |
787 | ||
788 | function device_get_tx_errors() { | |
789 | local device=${1} | |
790 | ||
791 | __device_get_file ${device} statistics/tx_errors | |
792 | } | |
ec63256a MT |
793 | |
794 | function device_get_speed() { | |
795 | local device=${1} | |
796 | ||
797 | __device_get_file ${device} speed | |
798 | } | |
799 | ||
800 | function device_get_duplex() { | |
801 | local device=${1} | |
802 | ||
803 | __device_get_file ${device} duplex | |
804 | } | |
657540d8 MT |
805 | |
806 | function device_get_link_string() { | |
807 | local device="${1}" | |
808 | assert isset device | |
809 | ||
810 | local s | |
811 | ||
812 | local speed="$(device_get_speed "${device}")" | |
813 | if isset speed; then | |
814 | list_append s "${speed} MBit/s" | |
815 | fi | |
816 | ||
817 | local duplex="$(device_get_duplex "${device}")" | |
818 | if isset duplex; then | |
819 | list_append s "${duplex} duplex" | |
820 | fi | |
821 | ||
822 | print "${s}" | |
823 | } |