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