]>
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 | ||
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() { | |
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 | |
93 | function device_is_bridge() { | |
94 | [ -d "/sys/class/net/${1}/bridge" ] | |
95 | } | |
96 | ||
81ed640c MT |
97 | function 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 |
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() { | |
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 |
125 | function 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 | |
132 | function device_is_loopback() { | |
133 | local device=$(devicify ${1}) | |
134 | [ "${device}" = "lo" ] | |
135 | } | |
136 | ||
137 | # Check if the device is a physical network interface | |
138 | function 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 | |
163 | function 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 |
189 | function 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 |
209 | function device_get_address() { |
210 | local device=${1} | |
211 | ||
212 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
213 | } | |
214 | ||
215 | function 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 | 246 | function 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 | 263 | function devices_get_all() { |
711ffac1 | 264 | device_get |
1848564d MT |
265 | } |
266 | ||
267 | # Check if a device has a cable plugged in | |
268 | function device_has_carrier() { | |
269 | local device=$(devicify ${1}) | |
270 | [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ] | |
271 | } | |
272 | ||
1e4c26a4 MT |
273 | function device_is_promisc() { |
274 | local device=${1} | |
275 | ||
276 | ip link show ${device} | grep -qE "<.*PROMISC.*>" | |
277 | } | |
278 | ||
1848564d MT |
279 | # Check if the device is free |
280 | function device_is_free() { | |
81ed640c | 281 | ! device_is_used $@ |
1848564d MT |
282 | } |
283 | ||
284 | # Check if the device is used | |
285 | function device_is_used() { | |
286 | local device=$(devicify ${1}) | |
287 | ||
fb02e543 MT |
288 | device_has_virtuals ${device} && \ |
289 | return ${EXIT_OK} | |
1848564d | 290 | device_is_bonded ${device} && \ |
fb02e543 | 291 | return ${EXIT_OK} |
81ed640c MT |
292 | device_is_bridge_attached ${device} && \ |
293 | return ${EXIT_OK} | |
1848564d | 294 | |
fb02e543 | 295 | return ${EXIT_ERROR} |
1848564d MT |
296 | } |
297 | ||
1b7a1578 MT |
298 | function device_hash() { |
299 | local device=${1} | |
300 | ||
37e4ec8b MT |
301 | # Get mac address of device and remove all colons (:) |
302 | # that will result in a hash. | |
303 | device=$(macify ${device}) | |
304 | ||
305 | echo "${device//:/}" | |
1b7a1578 MT |
306 | } |
307 | ||
308 | # Give the device a new name | |
309 | function device_set_name() { | |
1848564d | 310 | local source=$1 |
1578dae9 | 311 | local destination=${2} |
1848564d MT |
312 | |
313 | # Check if devices exists | |
314 | if ! device_exists ${source} || device_exists ${destination}; then | |
315 | return 4 | |
316 | fi | |
317 | ||
318 | local up | |
319 | if device_is_up ${source}; then | |
320 | ip link set ${source} down | |
321 | up=1 | |
322 | fi | |
323 | ||
324 | ip link set ${source} name ${destination} | |
325 | ||
326 | if [ "${up}" = "1" ]; then | |
327 | ip link set ${destination} up | |
328 | fi | |
329 | } | |
330 | ||
1848564d MT |
331 | # Set device up |
332 | function device_set_up() { | |
333 | local device=$(devicify ${1}) | |
334 | ||
711ffac1 MT |
335 | # Silently fail if device was not found |
336 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
337 | ||
1848564d MT |
338 | # Do nothing if device is already up |
339 | device_is_up ${device} && return ${EXIT_OK} | |
340 | ||
81ed640c MT |
341 | device_set_parent_up ${device} |
342 | ||
343 | log DEBUG "Setting up device '${device}'" | |
344 | ||
1848564d MT |
345 | ip link set ${device} up |
346 | } | |
347 | ||
81ed640c MT |
348 | function device_set_parent_up() { |
349 | local device=${1} | |
350 | local parent | |
351 | ||
352 | if device_is_virtual ${device}; then | |
8c6a8966 | 353 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
354 | |
355 | device_is_up ${parent} && return ${EXIT_OK} | |
356 | ||
357 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
358 | ||
359 | device_set_up ${parent} | |
360 | return $? | |
361 | fi | |
362 | ||
363 | return ${EXIT_OK} | |
364 | } | |
365 | ||
1848564d MT |
366 | # Set device down |
367 | function device_set_down() { | |
368 | local device=$(devicify ${1}) | |
369 | ||
81ed640c MT |
370 | local ret=${EXIT_OK} |
371 | ||
372 | if device_is_up ${device}; then | |
373 | log DEBUG "Tearing down device '${device}'" | |
374 | ||
375 | ip link set ${device} down | |
376 | ret=$? | |
377 | fi | |
378 | ||
379 | device_set_parent_down ${device} | |
1848564d | 380 | |
81ed640c MT |
381 | return ${ret} |
382 | } | |
383 | ||
384 | function device_set_parent_down() { | |
385 | local device=${1} | |
386 | local parent | |
387 | ||
388 | if device_is_virtual ${device}; then | |
8c6a8966 | 389 | parent=$(virtual_get_parent ${device}) |
81ed640c MT |
390 | |
391 | device_is_up ${parent} || return ${EXIT_OK} | |
392 | ||
393 | if device_is_free ${parent}; then | |
394 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
395 | ||
396 | device_set_down ${parent} | |
397 | fi | |
398 | fi | |
399 | ||
400 | return ${EXIT_OK} | |
1848564d MT |
401 | } |
402 | ||
1848564d MT |
403 | function device_get_mtu() { |
404 | local device=${1} | |
405 | ||
406 | if ! device_exists ${device}; then | |
407 | error "Device '${device}' does not exist." | |
408 | return ${EXIT_ERROR} | |
409 | fi | |
410 | ||
f3e6fe50 | 411 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
412 | } |
413 | ||
414 | # Set mtu to a device | |
415 | function device_set_mtu() { | |
1b7a1578 | 416 | local device=${1} |
1848564d MT |
417 | local mtu=${2} |
418 | ||
1b7a1578 MT |
419 | if ! device_exists ${device}; then |
420 | error "Device '${device}' does not exist." | |
421 | return ${EXIT_ERROR} | |
422 | fi | |
423 | ||
424 | local oldmtu=$(device_get_mtu ${device}) | |
425 | ||
426 | if [ "${oldmtu}" = "${mtu}" ]; then | |
427 | # No need to set mtu. | |
428 | return ${EXIT_OK} | |
429 | fi | |
430 | ||
431 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
432 | ||
1848564d | 433 | local up |
1b7a1578 MT |
434 | if device_is_up ${device}; then |
435 | device_set_down ${device} | |
1848564d MT |
436 | up=1 |
437 | fi | |
438 | ||
1b7a1578 | 439 | ip link set ${device} mtu ${mtu} |
1848564d MT |
440 | local ret=$? |
441 | ||
442 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
443 | device_set_up ${device} |
444 | fi | |
445 | ||
446 | if [ "${ret}" != "0" ]; then | |
447 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
448 | fi |
449 | ||
450 | return ${ret} | |
451 | } | |
452 | ||
453 | function device_discover() { | |
454 | local device=${1} | |
455 | ||
1b7a1578 MT |
456 | log INFO "Running discovery process on device '${device}'." |
457 | ||
1848564d | 458 | local hook |
d61a01d4 MT |
459 | for hook in $(hook_zone_get_all); do |
460 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
461 | done |
462 | } | |
463 | ||
1848564d MT |
464 | function device_has_ipv4() { |
465 | local device=${1} | |
466 | local addr=${2} | |
467 | ||
468 | if ! device_exists ${device}; then | |
469 | error "Device '${device}' does not exist." | |
470 | return ${EXIT_ERROR} | |
471 | fi | |
472 | ||
473 | ip addr show ${device} | grep -q -e "inet " -e "${addr}" | |
474 | } | |
4231f419 MT |
475 | |
476 | function device_has_ipv6() { | |
477 | local device=${1} | |
478 | local addr=${2} | |
479 | ||
480 | if ! device_exists ${device}; then | |
481 | error "Device '${device}' does not exist." | |
482 | return ${EXIT_ERROR} | |
483 | fi | |
484 | ||
485 | local prefix=${addr##*/} | |
486 | addr=$(ipv6_implode ${addr%%/*}) | |
487 | ||
488 | if [ -n "${prefix}" ]; then | |
489 | addr="${addr}/${prefix}" | |
490 | fi | |
491 | ||
492 | ip addr show ${device} | grep -q "inet6 ${addr}" | |
493 | } | |
711ffac1 | 494 | |
711ffac1 MT |
495 | function __device_get_file() { |
496 | local device=${1} | |
497 | local file=${2} | |
498 | ||
499 | assert isset device | |
500 | assert isset file | |
501 | ||
502 | cat ${SYS_CLASS_NET}/${device}/${file} | |
503 | } | |
504 | ||
505 | function device_get_rx_bytes() { | |
506 | local device=${1} | |
507 | ||
508 | __device_get_file ${device} statistics/rx_bytes | |
509 | } | |
510 | ||
511 | function device_get_tx_bytes() { | |
512 | local device=${1} | |
513 | ||
514 | __device_get_file ${device} statistics/tx_bytes | |
515 | } | |
516 | ||
517 | function device_get_rx_packets() { | |
518 | local device=${1} | |
519 | ||
520 | __device_get_file ${device} statistics/rx_packets | |
521 | } | |
522 | ||
523 | function device_get_tx_packets() { | |
524 | local device=${1} | |
525 | ||
526 | __device_get_file ${device} statistics/tx_packets | |
527 | } | |
528 | ||
529 | function device_get_rx_errors() { | |
530 | local device=${1} | |
531 | ||
532 | __device_get_file ${device} statistics/rx_errors | |
533 | } | |
534 | ||
535 | function device_get_tx_errors() { | |
536 | local device=${1} | |
537 | ||
538 | __device_get_file ${device} statistics/tx_errors | |
539 | } |