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