]>
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 | ||
f90dd58c MT |
22 | function device_list() { |
23 | local devices | |
24 | ||
25 | # Add all interfaces | |
26 | list_append devices $(devices_get_all) | |
27 | ||
28 | # Add all PHYs | |
29 | list_append devices $(phy_list) | |
30 | ||
31 | # Add all serial devices | |
32 | list_append devices $(serial_list) | |
33 | ||
34 | # Return a sorted result | |
35 | list_sort ${devices} | |
36 | } | |
37 | ||
1848564d MT |
38 | # Check if the device exists |
39 | function device_exists() { | |
40 | local device=${1} | |
41 | ||
42 | # If device name was not found, exit. | |
43 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
44 | ||
6c74a64c MT |
45 | # Check for a normal network device. |
46 | [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK} | |
47 | ||
f90dd58c MT |
48 | # If the check above did not find a result, |
49 | # we check for PHYs. | |
50 | phy_exists "${device}" && return ${EXIT_OK} | |
51 | ||
52 | # If the check above did not find a result, | |
6c74a64c MT |
53 | # we check for serial devices. |
54 | serial_exists ${device} | |
1848564d MT |
55 | } |
56 | ||
a23fdc0e MT |
57 | function device_matches_pattern() { |
58 | local device="${1}" | |
59 | assert isset device | |
60 | ||
61 | local pattern="${2}" | |
62 | assert isset pattern | |
63 | ||
64 | pattern="^${pattern//N/[[:digit:]]+}$" | |
65 | ||
66 | [[ ${device} =~ ${pattern} ]] \ | |
67 | && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
68 | } | |
69 | ||
99be6026 MT |
70 | function device_delete() { |
71 | local device=${1} | |
72 | assert isset device | |
73 | ||
74 | # Nothing to do, it device does not exist. | |
75 | device_exists ${device} || return ${EXIT_OK} | |
76 | ||
77 | # Delete the device. | |
78 | cmd_quiet ip link delete ${device} | |
79 | local ret=$? | |
80 | ||
81 | if [ ${ret} -ne ${EXIT_OK} ]; then | |
82 | log ERROR "device: Could not delete device '${device}': ${ret}" | |
83 | return ${EXIT_ERROR} | |
84 | fi | |
85 | ||
86 | return ${ret} | |
87 | } | |
88 | ||
e369be1a MT |
89 | function device_has_flag() { |
90 | local device=${1} | |
91 | local flag=${2} | |
92 | ||
93 | local flags=$(__device_get_file ${device} flags) | |
94 | ||
95 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
96 | return ${EXIT_FALSE} | |
97 | else | |
98 | return ${EXIT_TRUE} | |
99 | fi | |
100 | } | |
101 | ||
1848564d MT |
102 | # Check if the device is up |
103 | function device_is_up() { | |
104 | local device=${1} | |
105 | ||
106 | device_exists ${device} || return ${EXIT_ERROR} | |
107 | ||
e369be1a | 108 | device_has_flag ${device} 0x1 |
1848564d MT |
109 | } |
110 | ||
99be6026 MT |
111 | function device_ifindex_to_name() { |
112 | local idx=${1} | |
113 | assert isset idx | |
114 | ||
115 | local device device_idx | |
116 | for device in ${SYS_CLASS_NET}/*; do | |
117 | device=$(basename ${device}) | |
118 | device_exists ${device} || continue | |
119 | ||
120 | device_idx=$(device_get_ifindex ${device}) | |
121 | ||
122 | if [ "${device_idx}" = "${idx}" ]; then | |
123 | print "${device}" | |
124 | return ${EXIT_OK} | |
125 | fi | |
126 | done | |
127 | ||
128 | return ${EXIT_ERROR} | |
129 | } | |
130 | ||
131 | function device_get_ifindex() { | |
132 | local device=${1} | |
133 | assert isset device | |
134 | ||
135 | local path="${SYS_CLASS_NET}/${1}/ifindex" | |
136 | ||
137 | # Check if file can be read. | |
138 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
139 | ||
140 | print "$(<${path})" | |
141 | } | |
142 | ||
e6993835 MT |
143 | # Check if the device is a batman-adv bridge |
144 | function device_is_batman_adv() { | |
145 | [ -d "${SYS_CLASS_NET}/${1}/mesh" ] | |
146 | } | |
147 | ||
b8026986 MT |
148 | # Check if the device is a batman-adv slave port |
149 | function device_is_batman_adv_slave() { | |
7b192cf4 MT |
150 | local device="${1}" |
151 | ||
152 | if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then | |
153 | local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)" | |
154 | ||
155 | case "${status}" in | |
156 | "active") | |
157 | return ${EXIT_TRUE} | |
158 | ;; | |
159 | *) | |
160 | return ${EXIT_FALSE} | |
161 | ;; | |
162 | esac | |
163 | fi | |
164 | ||
165 | return ${EXIT_FALSE} | |
e6993835 MT |
166 | } |
167 | ||
1848564d MT |
168 | # Check if the device is a bonding device |
169 | function device_is_bonding() { | |
170 | [ -d "/sys/class/net/${1}/bonding" ] | |
171 | } | |
172 | ||
173 | # Check if the device bonded in a bonding device | |
174 | function device_is_bonded() { | |
711ffac1 | 175 | local device=${1} |
1848564d | 176 | |
0959482b | 177 | [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] |
1848564d MT |
178 | } |
179 | ||
180 | # Check if the device is a bridge | |
181 | function device_is_bridge() { | |
182 | [ -d "/sys/class/net/${1}/bridge" ] | |
183 | } | |
184 | ||
81ed640c MT |
185 | function device_is_bridge_attached() { |
186 | local device=${1} | |
81ed640c MT |
187 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] |
188 | } | |
189 | ||
a23fdc0e MT |
190 | function device_is_wireless_monitor() { |
191 | local device="${1}" | |
192 | assert isset device | |
193 | ||
194 | device_is_wireless "${device}" && \ | |
195 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}" | |
196 | } | |
197 | ||
b8026986 MT |
198 | function device_is_wireless_adhoc() { |
199 | local device="${1}" | |
200 | assert isset device | |
201 | ||
202 | device_is_wireless "${device}" && \ | |
203 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}" | |
204 | } | |
205 | ||
99be6026 MT |
206 | function device_get_bridge() { |
207 | local device=${1} | |
208 | assert isset device | |
209 | ||
210 | # Check if device is attached to a bridge. | |
211 | device_is_bridge_attached ${device} || return ${EXIT_ERROR} | |
212 | ||
213 | local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex" | |
214 | [ -r "${ifindex_path}" ] || return ${EXIT_ERROR} | |
215 | ||
216 | local ifindex=$(<${ifindex_path}) | |
217 | assert isset ifindex | |
218 | ||
219 | device_ifindex_to_name ${ifindex} | |
220 | } | |
221 | ||
7951525a MT |
222 | # Check if the device is a vlan device |
223 | function device_is_vlan() { | |
1848564d | 224 | local device=${1} |
7951525a | 225 | assert isset device |
1848564d | 226 | |
7951525a | 227 | [ -e "${PROC_NET_VLAN}/${device}" ] |
1848564d MT |
228 | } |
229 | ||
7951525a MT |
230 | # Check if the device has vlan devices |
231 | function device_has_vlans() { | |
fb02e543 | 232 | local device=${1} |
7951525a | 233 | assert isset device |
fb02e543 | 234 | |
7951525a | 235 | if device_is_vlan ${device}; then |
ec63256a | 236 | return ${EXIT_FALSE} |
fb02e543 MT |
237 | fi |
238 | ||
7951525a MT |
239 | local vlans=$(device_get_vlans ${device}) |
240 | [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
ec63256a MT |
241 | } |
242 | ||
7951525a | 243 | function device_get_vlans() { |
ec63256a | 244 | local device=${1} |
7951525a | 245 | assert isset device |
ec63256a | 246 | |
8357a7ff MT |
247 | # If no 8021q module has been loaded into the kernel, |
248 | # we cannot do anything. | |
7951525a | 249 | [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} |
8357a7ff | 250 | |
ec63256a MT |
251 | local dev spacer1 id spacer2 parent |
252 | while read dev spacer1 id spacer2 parent; do | |
7951525a MT |
253 | [ "${parent}" = "${device}" ] || continue |
254 | ||
255 | print "${dev}" | |
256 | done < ${PROC_NET_VLAN_CONFIG} | |
1848564d MT |
257 | } |
258 | ||
1848564d MT |
259 | # Check if the device is a ppp device |
260 | function device_is_ppp() { | |
261 | local device=${1} | |
262 | ||
55b802cc | 263 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 264 | |
e369be1a MT |
265 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
266 | } | |
55b802cc | 267 | |
e369be1a MT |
268 | # Check if the device is a pointopoint device. |
269 | function device_is_ptp() { | |
270 | local device=${1} | |
271 | ||
272 | device_has_flag ${device} 0x10 | |
1848564d MT |
273 | } |
274 | ||
275 | # Check if the device is a loopback device | |
276 | function device_is_loopback() { | |
5bb2429a MT |
277 | local device=${1} |
278 | ||
1848564d MT |
279 | [ "${device}" = "lo" ] |
280 | } | |
281 | ||
0067696a MT |
282 | # Check if the device is a dummy device |
283 | # This is the worst possible check, but all I could come up with | |
284 | function device_is_dummy() { | |
285 | local device="${1}" | |
286 | ||
287 | [[ ${device} =~ ^dummy[0-9]+$ ]] | |
288 | } | |
289 | ||
a508c27e MT |
290 | # Check if the device is a wireless device |
291 | function device_is_wireless() { | |
292 | local device=${1} | |
293 | ||
294 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
295 | } | |
296 | ||
4733a336 MT |
297 | function device_get_phy() { |
298 | local device="${1}" | |
299 | ||
300 | if device_is_wireless "${device}"; then | |
301 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
302 | return ${EXIT_OK} | |
303 | fi | |
304 | ||
305 | return ${EXIT_ERROR} | |
306 | } | |
307 | ||
f90dd58c MT |
308 | function device_is_phy() { |
309 | phy_exists $@ | |
310 | } | |
311 | ||
6c74a64c MT |
312 | function device_is_serial() { |
313 | serial_exists $@ | |
314 | } | |
315 | ||
1848564d | 316 | # Check if the device is a physical network interface |
ec63256a | 317 | function device_is_ethernet() { |
1848564d MT |
318 | local device=${1} |
319 | ||
0067696a MT |
320 | device_is_ethernet_compatible "${device}" || \ |
321 | return ${EXIT_ERROR} | |
322 | ||
1848564d MT |
323 | device_is_loopback ${device} && \ |
324 | return ${EXIT_ERROR} | |
325 | ||
326 | device_is_bonding ${device} && \ | |
327 | return ${EXIT_ERROR} | |
328 | ||
329 | device_is_bridge ${device} && \ | |
330 | return ${EXIT_ERROR} | |
331 | ||
332 | device_is_ppp ${device} && \ | |
333 | return ${EXIT_ERROR} | |
334 | ||
7951525a | 335 | device_is_vlan ${device} && \ |
1848564d MT |
336 | return ${EXIT_ERROR} |
337 | ||
0067696a | 338 | device_is_dummy ${device} && \ |
419b4cd0 MT |
339 | return ${EXIT_ERROR} |
340 | ||
1848564d MT |
341 | return ${EXIT_OK} |
342 | } | |
343 | ||
344 | # Get the device type | |
345 | function device_get_type() { | |
5bb2429a | 346 | local device=${1} |
1848564d | 347 | |
b9f27baf MT |
348 | # If the device does not exist (happens on udev remove events), |
349 | # we do not bother to run all checks. | |
350 | if ! device_exists "${device}"; then | |
351 | echo "unknown" | |
352 | ||
353 | elif device_is_vlan ${device}; then | |
1848564d MT |
354 | echo "vlan" |
355 | ||
356 | elif device_is_bonding ${device}; then | |
357 | echo "bonding" | |
358 | ||
359 | elif device_is_bridge ${device}; then | |
360 | echo "bridge" | |
361 | ||
362 | elif device_is_ppp ${device}; then | |
363 | echo "ppp" | |
364 | ||
e6993835 MT |
365 | elif device_is_batman_adv ${device}; then |
366 | echo "batman-adv" | |
367 | ||
1848564d MT |
368 | elif device_is_loopback ${device}; then |
369 | echo "loopback" | |
370 | ||
b8026986 MT |
371 | elif device_is_wireless_adhoc ${device}; then |
372 | echo "wireless-adhoc" | |
373 | ||
a508c27e MT |
374 | elif device_is_wireless ${device}; then |
375 | echo "wireless" | |
376 | ||
0067696a MT |
377 | elif device_is_dummy ${device}; then |
378 | echo "dummy" | |
379 | ||
ec63256a MT |
380 | elif device_is_ethernet ${device}; then |
381 | echo "ethernet" | |
1848564d | 382 | |
6c74a64c MT |
383 | elif device_is_serial ${device}; then |
384 | echo "serial" | |
385 | ||
f90dd58c MT |
386 | elif device_is_phy ${device}; then |
387 | echo "phy" | |
388 | ||
1848564d MT |
389 | else |
390 | echo "unknown" | |
391 | fi | |
392 | } | |
393 | ||
a4f7ad26 MT |
394 | function device_is_ethernet_compatible() { |
395 | local device="${1}" | |
396 | ||
397 | # /sys/class/net/*/type must equal 1 for ethernet compatible devices | |
398 | local type="$(__device_get_file "${device}" "type")" | |
399 | [[ "${type}" = "1" ]] | |
400 | } | |
401 | ||
711ffac1 MT |
402 | function device_get_status() { |
403 | local device=${1} | |
711ffac1 MT |
404 | assert isset device |
405 | ||
3cb2fc42 | 406 | local status=${STATUS_DOWN} |
711ffac1 | 407 | |
3cb2fc42 | 408 | if device_is_up ${device}; then |
711ffac1 | 409 | status=${STATUS_UP} |
711ffac1 | 410 | |
3cb2fc42 MT |
411 | if ! device_has_carrier ${device}; then |
412 | status=${STATUS_NOCARRIER} | |
413 | fi | |
414 | fi | |
711ffac1 MT |
415 | |
416 | echo "${status}" | |
417 | } | |
418 | ||
1848564d MT |
419 | function device_get_address() { |
420 | local device=${1} | |
421 | ||
422 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
423 | } | |
424 | ||
425 | function device_set_address() { | |
08c5b789 MT |
426 | assert [ $# -eq 2 ] |
427 | ||
428 | local device="${1}" | |
429 | local addr="${2}" | |
1b7a1578 | 430 | |
08c5b789 | 431 | if ! device_exists "${device}"; then |
1b7a1578 MT |
432 | error "Device '${device}' does not exist." |
433 | return ${EXIT_ERROR} | |
434 | fi | |
435 | ||
08c5b789 MT |
436 | # Do nothing if the address has not changed |
437 | local old_addr="$(device_get_address "${device}")" | |
438 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
439 | return ${EXIT_OK} | |
440 | fi | |
441 | ||
442 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
443 | |
444 | local up | |
08c5b789 MT |
445 | if device_is_up "${device}"; then |
446 | device_set_down "${device}" | |
1b7a1578 MT |
447 | up=1 |
448 | fi | |
449 | ||
08c5b789 | 450 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
451 | local ret=$? |
452 | ||
453 | if [ "${up}" = "1" ]; then | |
08c5b789 | 454 | device_set_up "${device}" |
1b7a1578 MT |
455 | fi |
456 | ||
457 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 458 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
459 | fi |
460 | ||
461 | return ${ret} | |
1848564d MT |
462 | } |
463 | ||
711ffac1 | 464 | function device_get() { |
2ae0fb8d | 465 | local device |
711ffac1 MT |
466 | local devices |
467 | ||
2ae0fb8d MT |
468 | for device in ${SYS_CLASS_NET}/*; do |
469 | device=$(basename ${device}) | |
711ffac1 | 470 | |
2ae0fb8d MT |
471 | # bonding_masters is no device |
472 | [ "${device}" = "bonding_masters" ] && continue | |
473 | ||
474 | devices="${devices} ${device}" | |
475 | done | |
711ffac1 MT |
476 | |
477 | echo ${devices} | |
478 | return ${EXIT_OK} | |
479 | } | |
480 | ||
1848564d | 481 | function devices_get_all() { |
711ffac1 | 482 | device_get |
1848564d MT |
483 | } |
484 | ||
485 | # Check if a device has a cable plugged in | |
486 | function device_has_carrier() { | |
5bb2429a MT |
487 | local device=${1} |
488 | assert isset device | |
489 | ||
ec63256a MT |
490 | local carrier=$(__device_get_file ${device} carrier) |
491 | [ "${carrier}" = "1" ] | |
1848564d MT |
492 | } |
493 | ||
1e4c26a4 MT |
494 | function device_is_promisc() { |
495 | local device=${1} | |
496 | ||
e369be1a | 497 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
498 | } |
499 | ||
cf6e4606 MT |
500 | function device_set_promisc() { |
501 | local device=${1} | |
502 | local state=${2} | |
503 | ||
504 | assert device_exists ${device} | |
505 | assert isset state | |
506 | assert isoneof state on off | |
507 | ||
508 | ip link set ${device} promisc ${state} | |
509 | } | |
510 | ||
1848564d MT |
511 | # Check if the device is free |
512 | function device_is_free() { | |
81ed640c | 513 | ! device_is_used $@ |
1848564d MT |
514 | } |
515 | ||
516 | # Check if the device is used | |
517 | function device_is_used() { | |
5bb2429a | 518 | local device=${1} |
1848564d | 519 | |
7951525a | 520 | device_has_vlans ${device} && \ |
fb02e543 | 521 | return ${EXIT_OK} |
1848564d | 522 | device_is_bonded ${device} && \ |
fb02e543 | 523 | return ${EXIT_OK} |
81ed640c MT |
524 | device_is_bridge_attached ${device} && \ |
525 | return ${EXIT_OK} | |
1848564d | 526 | |
fb02e543 | 527 | return ${EXIT_ERROR} |
1848564d MT |
528 | } |
529 | ||
1b7a1578 MT |
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 | } |