]>
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 | ||
e369be1a MT |
76 | function device_has_flag() { |
77 | local device=${1} | |
78 | local flag=${2} | |
79 | ||
80 | local flags=$(__device_get_file ${device} flags) | |
81 | ||
82 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
83 | return ${EXIT_FALSE} | |
84 | else | |
85 | return ${EXIT_TRUE} | |
86 | fi | |
87 | } | |
88 | ||
1848564d MT |
89 | # Check if the device is up |
90 | function device_is_up() { | |
91 | local device=${1} | |
92 | ||
93 | device_exists ${device} || return ${EXIT_ERROR} | |
94 | ||
e369be1a | 95 | device_has_flag ${device} 0x1 |
1848564d MT |
96 | } |
97 | ||
98 | # Check if the device is a bonding device | |
99 | function device_is_bonding() { | |
100 | [ -d "/sys/class/net/${1}/bonding" ] | |
101 | } | |
102 | ||
103 | # Check if the device bonded in a bonding device | |
104 | function device_is_bonded() { | |
711ffac1 | 105 | local device=${1} |
1848564d | 106 | |
711ffac1 | 107 | [ -d "${SYS_CLASS_NET}/${device}/master" ] |
1848564d MT |
108 | } |
109 | ||
110 | # Check if the device is a bridge | |
111 | function device_is_bridge() { | |
112 | [ -d "/sys/class/net/${1}/bridge" ] | |
113 | } | |
114 | ||
81ed640c MT |
115 | function device_is_bridge_attached() { |
116 | local device=${1} | |
117 | ||
118 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] | |
119 | } | |
120 | ||
1848564d MT |
121 | # Check if the device is a virtual device |
122 | function device_is_virtual() { | |
123 | local device=${1} | |
124 | ||
125 | [ -e "/proc/net/vlan/${device}" ] | |
126 | } | |
127 | ||
128 | # Check if the device has virtual devices | |
129 | function device_has_virtuals() { | |
fb02e543 MT |
130 | local device=${1} |
131 | ||
132 | if device_is_virtual ${device}; then | |
ec63256a | 133 | return ${EXIT_FALSE} |
fb02e543 MT |
134 | fi |
135 | ||
ec63256a MT |
136 | local virtuals=$(device_get_virtuals ${device}) |
137 | [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
138 | } | |
139 | ||
140 | function device_get_virtuals() { | |
141 | local device=${1} | |
142 | ||
8357a7ff MT |
143 | # If no 8021q module has been loaded into the kernel, |
144 | # we cannot do anything. | |
145 | [ -r "/proc/net/vlan/config" ] || return ${EXIT_OK} | |
146 | ||
ec63256a MT |
147 | local dev spacer1 id spacer2 parent |
148 | while read dev spacer1 id spacer2 parent; do | |
149 | [ "${parent}" = "${device}" ] && echo "${dev}" | |
150 | done < /proc/net/vlan/config | sort | |
1848564d MT |
151 | } |
152 | ||
1848564d MT |
153 | # Check if the device is a ppp device |
154 | function device_is_ppp() { | |
155 | local device=${1} | |
156 | ||
55b802cc | 157 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 158 | |
e369be1a MT |
159 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
160 | } | |
55b802cc | 161 | |
e369be1a MT |
162 | # Check if the device is a pointopoint device. |
163 | function device_is_ptp() { | |
164 | local device=${1} | |
165 | ||
166 | device_has_flag ${device} 0x10 | |
1848564d MT |
167 | } |
168 | ||
169 | # Check if the device is a loopback device | |
170 | function device_is_loopback() { | |
5bb2429a MT |
171 | local device=${1} |
172 | ||
1848564d MT |
173 | [ "${device}" = "lo" ] |
174 | } | |
175 | ||
a508c27e MT |
176 | # Check if the device is a wireless device |
177 | function device_is_wireless() { | |
178 | local device=${1} | |
179 | ||
180 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
181 | } | |
182 | ||
6c74a64c MT |
183 | function device_is_serial() { |
184 | serial_exists $@ | |
185 | } | |
186 | ||
1848564d | 187 | # Check if the device is a physical network interface |
ec63256a | 188 | function device_is_ethernet() { |
1848564d MT |
189 | local device=${1} |
190 | ||
191 | device_is_loopback ${device} && \ | |
192 | return ${EXIT_ERROR} | |
193 | ||
194 | device_is_bonding ${device} && \ | |
195 | return ${EXIT_ERROR} | |
196 | ||
197 | device_is_bridge ${device} && \ | |
198 | return ${EXIT_ERROR} | |
199 | ||
200 | device_is_ppp ${device} && \ | |
201 | return ${EXIT_ERROR} | |
202 | ||
203 | device_is_virtual ${device} && \ | |
204 | return ${EXIT_ERROR} | |
205 | ||
419b4cd0 MT |
206 | [ "$(__device_get_file ${device} type)" != "1" ] && \ |
207 | return ${EXIT_ERROR} | |
208 | ||
1848564d MT |
209 | return ${EXIT_OK} |
210 | } | |
211 | ||
212 | # Get the device type | |
213 | function device_get_type() { | |
5bb2429a | 214 | local device=${1} |
1848564d | 215 | |
8c6a8966 | 216 | if device_is_virtual ${device}; then |
1848564d MT |
217 | echo "vlan" |
218 | ||
219 | elif device_is_bonding ${device}; then | |
220 | echo "bonding" | |
221 | ||
222 | elif device_is_bridge ${device}; then | |
223 | echo "bridge" | |
224 | ||
225 | elif device_is_ppp ${device}; then | |
226 | echo "ppp" | |
227 | ||
228 | elif device_is_loopback ${device}; then | |
229 | echo "loopback" | |
230 | ||
a508c27e MT |
231 | elif device_is_wireless ${device}; then |
232 | echo "wireless" | |
233 | ||
ec63256a MT |
234 | elif device_is_ethernet ${device}; then |
235 | echo "ethernet" | |
1848564d | 236 | |
6c74a64c MT |
237 | elif device_is_serial ${device}; then |
238 | echo "serial" | |
239 | ||
1848564d MT |
240 | else |
241 | echo "unknown" | |
242 | fi | |
243 | } | |
244 | ||
711ffac1 MT |
245 | function device_get_status() { |
246 | local device=${1} | |
711ffac1 MT |
247 | assert isset device |
248 | ||
3cb2fc42 | 249 | local status=${STATUS_DOWN} |
711ffac1 | 250 | |
3cb2fc42 | 251 | if device_is_up ${device}; then |
711ffac1 | 252 | status=${STATUS_UP} |
711ffac1 | 253 | |
3cb2fc42 MT |
254 | if ! device_has_carrier ${device}; then |
255 | status=${STATUS_NOCARRIER} | |
256 | fi | |
257 | fi | |
711ffac1 MT |
258 | |
259 | echo "${status}" | |
260 | } | |
261 | ||
1848564d MT |
262 | function device_get_address() { |
263 | local device=${1} | |
264 | ||
265 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
266 | } | |
267 | ||
268 | function device_set_address() { | |
1b7a1578 MT |
269 | local device=${1} |
270 | local addr=${2} | |
271 | ||
272 | if ! device_exists ${device}; then | |
273 | error "Device '${device}' does not exist." | |
274 | return ${EXIT_ERROR} | |
275 | fi | |
276 | ||
277 | log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})." | |
278 | ||
279 | local up | |
280 | if device_is_up ${device}; then | |
281 | device_set_down ${device} | |
282 | up=1 | |
283 | fi | |
284 | ||
285 | ip link set ${device} address ${addr} | |
286 | local ret=$? | |
287 | ||
288 | if [ "${up}" = "1" ]; then | |
289 | device_set_up ${device} | |
290 | fi | |
291 | ||
292 | if [ "${ret}" != "0" ]; then | |
293 | error_log "Could not set address '${addr}' on device '${device}'." | |
294 | fi | |
295 | ||
296 | return ${ret} | |
1848564d MT |
297 | } |
298 | ||
711ffac1 | 299 | function device_get() { |
2ae0fb8d | 300 | local device |
711ffac1 MT |
301 | local devices |
302 | ||
2ae0fb8d MT |
303 | for device in ${SYS_CLASS_NET}/*; do |
304 | device=$(basename ${device}) | |
711ffac1 | 305 | |
2ae0fb8d MT |
306 | # bonding_masters is no device |
307 | [ "${device}" = "bonding_masters" ] && continue | |
308 | ||
309 | devices="${devices} ${device}" | |
310 | done | |
711ffac1 MT |
311 | |
312 | echo ${devices} | |
313 | return ${EXIT_OK} | |
314 | } | |
315 | ||
1848564d | 316 | function devices_get_all() { |
711ffac1 | 317 | device_get |
1848564d MT |
318 | } |
319 | ||
320 | # Check if a device has a cable plugged in | |
321 | function device_has_carrier() { | |
5bb2429a MT |
322 | local device=${1} |
323 | assert isset device | |
324 | ||
ec63256a MT |
325 | local carrier=$(__device_get_file ${device} carrier) |
326 | [ "${carrier}" = "1" ] | |
1848564d MT |
327 | } |
328 | ||
1e4c26a4 MT |
329 | function device_is_promisc() { |
330 | local device=${1} | |
331 | ||
e369be1a | 332 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
333 | } |
334 | ||
cf6e4606 MT |
335 | function device_set_promisc() { |
336 | local device=${1} | |
337 | local state=${2} | |
338 | ||
339 | assert device_exists ${device} | |
340 | assert isset state | |
341 | assert isoneof state on off | |
342 | ||
343 | ip link set ${device} promisc ${state} | |
344 | } | |
345 | ||
1848564d MT |
346 | # Check if the device is free |
347 | function device_is_free() { | |
81ed640c | 348 | ! device_is_used $@ |
1848564d MT |
349 | } |
350 | ||
351 | # Check if the device is used | |
352 | function device_is_used() { | |
5bb2429a | 353 | local device=${1} |
1848564d | 354 | |
fb02e543 MT |
355 | device_has_virtuals ${device} && \ |
356 | return ${EXIT_OK} | |
1848564d | 357 | device_is_bonded ${device} && \ |
fb02e543 | 358 | return ${EXIT_OK} |
81ed640c MT |
359 | device_is_bridge_attached ${device} && \ |
360 | return ${EXIT_OK} | |
1848564d | 361 | |
fb02e543 | 362 | return ${EXIT_ERROR} |
1848564d MT |
363 | } |
364 | ||
1b7a1578 MT |
365 | function device_hash() { |
366 | local device=${1} | |
367 | ||
37e4ec8b MT |
368 | # Get mac address of device and remove all colons (:) |
369 | # that will result in a hash. | |
370 | device=$(macify ${device}) | |
371 | ||
372 | echo "${device//:/}" | |
1b7a1578 MT |
373 | } |
374 | ||
375 | # Give the device a new name | |
376 | function device_set_name() { | |
1848564d | 377 | local source=$1 |
1578dae9 | 378 | local destination=${2} |
1848564d MT |
379 | |
380 | # Check if devices exists | |
381 | if ! device_exists ${source} || device_exists ${destination}; then | |
382 | return 4 | |
383 | fi | |
384 | ||
385 | local up | |
386 | if device_is_up ${source}; then | |
387 | ip link set ${source} down | |
388 | up=1 | |
389 | fi | |
390 | ||
391 | ip link set ${source} name ${destination} | |
392 | ||
393 | if [ "${up}" = "1" ]; then | |
394 | ip link set ${destination} up | |
395 | fi | |
396 | } | |
397 | ||
1848564d MT |
398 | # Set device up |
399 | function device_set_up() { | |
5bb2429a | 400 | local device=${1} |
1848564d | 401 | |
711ffac1 MT |
402 | # Silently fail if device was not found |
403 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
404 | ||
1848564d MT |
405 | # Do nothing if device is already up |
406 | device_is_up ${device} && return ${EXIT_OK} | |
407 | ||
81ed640c MT |
408 | device_set_parent_up ${device} |
409 | ||
410 | log DEBUG "Setting up device '${device}'" | |
411 | ||
1848564d MT |
412 | ip link set ${device} up |
413 | } | |
414 | ||
81ed640c MT |
415 | function device_set_parent_up() { |
416 | local device=${1} | |
417 | local parent | |
418 | ||
419 | if device_is_virtual ${device}; then | |
8c6a8966 | 420 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
421 | |
422 | device_is_up ${parent} && return ${EXIT_OK} | |
423 | ||
424 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
425 | ||
426 | device_set_up ${parent} | |
427 | return $? | |
428 | fi | |
429 | ||
430 | return ${EXIT_OK} | |
431 | } | |
432 | ||
1848564d MT |
433 | # Set device down |
434 | function device_set_down() { | |
5bb2429a MT |
435 | local device=${1} |
436 | assert isset device | |
1848564d | 437 | |
81ed640c MT |
438 | local ret=${EXIT_OK} |
439 | ||
440 | if device_is_up ${device}; then | |
441 | log DEBUG "Tearing down device '${device}'" | |
442 | ||
443 | ip link set ${device} down | |
444 | ret=$? | |
445 | fi | |
446 | ||
447 | device_set_parent_down ${device} | |
1848564d | 448 | |
81ed640c MT |
449 | return ${ret} |
450 | } | |
451 | ||
452 | function device_set_parent_down() { | |
453 | local device=${1} | |
454 | local parent | |
455 | ||
456 | if device_is_virtual ${device}; then | |
8c6a8966 | 457 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
458 | |
459 | device_is_up ${parent} || return ${EXIT_OK} | |
460 | ||
461 | if device_is_free ${parent}; then | |
462 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
463 | ||
464 | device_set_down ${parent} | |
465 | fi | |
466 | fi | |
467 | ||
468 | return ${EXIT_OK} | |
1848564d MT |
469 | } |
470 | ||
1848564d MT |
471 | function device_get_mtu() { |
472 | local device=${1} | |
473 | ||
474 | if ! device_exists ${device}; then | |
475 | error "Device '${device}' does not exist." | |
476 | return ${EXIT_ERROR} | |
477 | fi | |
478 | ||
f3e6fe50 | 479 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
480 | } |
481 | ||
482 | # Set mtu to a device | |
483 | function device_set_mtu() { | |
1b7a1578 | 484 | local device=${1} |
1848564d MT |
485 | local mtu=${2} |
486 | ||
1b7a1578 MT |
487 | if ! device_exists ${device}; then |
488 | error "Device '${device}' does not exist." | |
489 | return ${EXIT_ERROR} | |
490 | fi | |
491 | ||
492 | local oldmtu=$(device_get_mtu ${device}) | |
493 | ||
494 | if [ "${oldmtu}" = "${mtu}" ]; then | |
495 | # No need to set mtu. | |
496 | return ${EXIT_OK} | |
497 | fi | |
498 | ||
499 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
500 | ||
1848564d | 501 | local up |
1b7a1578 MT |
502 | if device_is_up ${device}; then |
503 | device_set_down ${device} | |
1848564d MT |
504 | up=1 |
505 | fi | |
506 | ||
1b7a1578 | 507 | ip link set ${device} mtu ${mtu} |
1848564d MT |
508 | local ret=$? |
509 | ||
510 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
511 | device_set_up ${device} |
512 | fi | |
513 | ||
514 | if [ "${ret}" != "0" ]; then | |
515 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
516 | fi |
517 | ||
518 | return ${ret} | |
519 | } | |
520 | ||
521 | function device_discover() { | |
522 | local device=${1} | |
523 | ||
1b7a1578 MT |
524 | log INFO "Running discovery process on device '${device}'." |
525 | ||
1848564d | 526 | local hook |
d61a01d4 MT |
527 | for hook in $(hook_zone_get_all); do |
528 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
529 | done |
530 | } | |
531 | ||
38f61548 | 532 | function device_has_ip() { |
1848564d MT |
533 | local device=${1} |
534 | local addr=${2} | |
535 | ||
38f61548 MT |
536 | assert isset addr |
537 | assert device_exists ${device} | |
538 | ||
539 | # IPv6 addresses must be fully imploded | |
540 | local protocol=$(ip_detect_protocol ${addr}) | |
541 | case "${protocol}" in | |
542 | ipv6) | |
543 | addr=$(ipv6_implode ${addr}) | |
544 | ;; | |
545 | esac | |
1848564d | 546 | |
38f61548 | 547 | listmatch ${addr} $(device_get_addresses ${device}) |
1848564d | 548 | } |
4231f419 | 549 | |
38f61548 | 550 | function device_get_addresses() { |
4231f419 | 551 | local device=${1} |
4231f419 | 552 | |
38f61548 | 553 | assert device_exists ${device} |
4231f419 | 554 | |
38f61548 MT |
555 | local prot |
556 | local addr | |
557 | local line | |
558 | ip addr show ${device} | \ | |
559 | while read prot addr line; do | |
560 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
561 | done | |
4231f419 | 562 | } |
711ffac1 | 563 | |
711ffac1 MT |
564 | function __device_get_file() { |
565 | local device=${1} | |
566 | local file=${2} | |
567 | ||
568 | assert isset device | |
569 | assert isset file | |
570 | ||
e369be1a MT |
571 | local path="${SYS_CLASS_NET}/${device}/${file}" |
572 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
573 | ||
574 | echo "$(<${path})" | |
711ffac1 MT |
575 | } |
576 | ||
577 | function device_get_rx_bytes() { | |
578 | local device=${1} | |
579 | ||
580 | __device_get_file ${device} statistics/rx_bytes | |
581 | } | |
582 | ||
583 | function device_get_tx_bytes() { | |
584 | local device=${1} | |
585 | ||
586 | __device_get_file ${device} statistics/tx_bytes | |
587 | } | |
588 | ||
589 | function device_get_rx_packets() { | |
590 | local device=${1} | |
591 | ||
592 | __device_get_file ${device} statistics/rx_packets | |
593 | } | |
594 | ||
595 | function device_get_tx_packets() { | |
596 | local device=${1} | |
597 | ||
598 | __device_get_file ${device} statistics/tx_packets | |
599 | } | |
600 | ||
601 | function device_get_rx_errors() { | |
602 | local device=${1} | |
603 | ||
604 | __device_get_file ${device} statistics/rx_errors | |
605 | } | |
606 | ||
607 | function device_get_tx_errors() { | |
608 | local device=${1} | |
609 | ||
610 | __device_get_file ${device} statistics/tx_errors | |
611 | } | |
ec63256a MT |
612 | |
613 | function device_get_speed() { | |
614 | local device=${1} | |
615 | ||
616 | __device_get_file ${device} speed | |
617 | } | |
618 | ||
619 | function device_get_duplex() { | |
620 | local device=${1} | |
621 | ||
622 | __device_get_file ${device} duplex | |
623 | } |