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