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