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