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