-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip-array_ipset_functions
1003 lines (981 loc) · 28.4 KB
/
ip-array_ipset_functions
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ------------------------------------------------------------------------- #
#*# ###### #
# # # # # ##### ##### ## # #
# # # # # # # # # # # # #
# ###### ##### # # # # # # # # #
# # ####### ##### ##### ###### #
# # # # # # # # # # #
### # # # # # # # # # #
# ------------------------------------------------------------------------- #
#
# Copyright (C) 2005-2018 Mart Frauenlob aka AllKind
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ------------------------------------------------------------------------- #
#
# IP-ARRAY IPSET FUNCTIONS
#
# ------------------------------------------------------------------------- #
# ------------------------------------------------------------------------- #
# IPSET
# ------------------------------------------------------------------------- #
val_setname() { # validate name of an ipset set
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ ${1:0:1} = : && $IPSET_SYNTAX = old ]] && {
log -E "Invalid setname \`$1' - must not begin with a colon."
return 1
}
[[ $1 != *+([[:space:]])* ]] || {
log -E "Invalid setname \`$1' - must not contain spaces."
return 1
}
} # -------------------------------------------------------------------------
val_ipset_port_spec() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_p="$1"
case "$1" in
@(tcp|sctp|udp|udplite):\[*+([[:alpha:]])*\]-\[*+([[:alpha:]])*\])
val_port_range_word "${1#@(tcp|sctp|udp|udplite):}" || return
;;
@(tcp|sctp|udp|udplite):\[*+([[:alpha:]])*\]-+([[:word:]]))
val_port_range_word "${1#@(tcp|sctp|udp|udplite):}" || return
;;
@(tcp|sctp|udp|udplite):+([[:word:]])-\[*+([[:alpha:]])*\])
val_port_range_word "${1#@(tcp|sctp|udp|udplite):}" || return
;;
@(tcp|sctp|udp|udplite):@(+([[:word:]])-+([[:word:]])|+([[:digit:]])-+([[:digit:]])))
val_port_range_word "${1#@(tcp|sctp|udp|udplite):}" || return
;;
@(tcp|sctp|udp|udplite):@(+([[:word:]])|+([[:digit:]])))
val_port "${1#@(tcp|sctp|udp|udplite):}" || return
;;
@(tcp|sctp|udp|udplite):\[*+([[:alpha:]])*\])
str_p="${1#@(tcp|sctp|udp|udplite):\[}"
str_p="${str_p%\]}"
val_port "$str_p" || return
;;
icmp:*)
val_icmp_type "${1#icmp:}" || return
;;
+([[:word:]])-+([[:word:]])|+([[:digit:]])-+([[:digit:]]))
val_port_range_word "$1" || return
;;
+([[:word:]])-+([[:word:]]))
val_port_range_word "$1" || return
;;
\[*\]-\[*\]|\[*\]-+([[:word:]])|+([[:word:]])-\[*\])
val_port_range_word "$1" || return
;;
+([[:digit:]]):0|*+([[:alpha:]])*:0)
val_proto "${1%:0}" || return
;;
+([[:digit:]])|+([[:word:]]))
val_port "$1" || return
;;
\[*+([[:alpha:]])*-*+([[:alpha:]])*\])
str_p="${str_p#[}"
str_p="${str_p%]}"
val_port "$str_p" || return
;;
*) log -E "Invalid entry \`$1' expecting: [proto:]port."
return 1
esac
} # -------------------------------------------------------------------------
_ipset_check_common_create_opts() {
if [[ $option = @(comment|counters|skbinfo) ]]; then
if ((IPSET_VERSION[0] < 6)); then
log -E "Option \`$option' is not supported: ipset version is too old."
return 1
fi
fi
case "$option" in
comment)
# ipset -ge v6.20 has comment flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 20)); then
log -E "Option \`$option' is not supported: ipset version is too old."
return 1
fi
;;
counters)
# ipset -ge v6.19 has counters flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 19)); then
log -E "Option \`$option' is not supported: ipset version is too old."
return 1
fi
;;
skbinfo)
# ipset -ge v6.22 has skbinfo flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 22)); then
log -E "Option \`$option' is not supported: ipset version is too old."
return 1
fi
;;
timeout)
val_numeric "$opt_arg" || return 1
;;
esac
} # -------------------------------------------------------------------------
ipset_create() { # create an ipset set (cache commands)
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
((USE_IPSET)) || {
log -W "ipset usage is disabled. set creation command will be ignored."
return 0
}
local str_set="$1" str_type="$2" str_create="-N" str_tmp
shift 2
local -a arr_opts
local option opt_arg
local strERR="Failed creating set \`$str_set'."
local str_failcmd='eval log -F "$strERR"; raise_cfg_err_count; return 0'
val_setname "$str_set" || { raise_cfg_err_count; return $ER_CONF; }
check_set_exist "$str_set" && {
log -W "ipset set \`$str_set' has already been created."
return 0
}
lsearch "$str_type" $UNSUPPORTED_SETLIST && {
log -E "ipset set type \`$str_type' is not supported."
$str_failcmd
}
if ((${#CREATED_SETS_ARRAY[@]} == $MAX_SETS)); then
log -E "The maximum amount of sets that can be created ($MAX_SETS) has been reached."
$str_failcmd
fi
if [[ $IPSET_SYNTAX = new ]]; then
str_create=create
fi
case "$str_type" in
bitmap:ip|bitmap:ip,mac|bitmap:port)
local -a arr_opts=("$@")
while (($#)); do
option="$1" opt_arg="$2"
case "$option" in
range|netmask|timeout)
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
;;
esac
_ipset_check_common_create_opts || $str_failcmd
case "$option" in
comment|counters|skbinfo) : ;;
timeout) shift ;;
range)
if [[ $str_type = bitmap:port ]]; then
if ((IPSET_VERSION[0] > 6)) || ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] >= 20)); then
# ipset versions greater or equal to 6.20 allow proto:port specification
val_ipset_port_spec "$opt_arg" || $str_failcmd
else
val_port_range_word "$opt_arg" || $str_failcmd
fi
else
val_ip4_addr "$opt_arg" || $str_failcmd
fi
shift
;;
netmask)
if [[ $str_type = bitmap:ip ]]; then
val_numeric "$opt_arg" || $str_failcmd
((opt_arg >= 1 && opt_arg <= 32)) || {
log -E "$strERR Invalid value \`$opt_arg' with \`$option' for \`$str_type' set. Valid range: 1-32."
$str_failcmd
}
else
log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
fi
shift
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
;;
esac
shift
done
;;
hash:ip|hash:net|hash:ip,port|hash:net,port|hash:ip,port,ip|hash:ip,port,net|hash:net,iface|hash:net,port,net|hash:ip,mark|hash:mac)
local -a arr_opts=("$@")
while (($#)); do
option="$1" opt_arg="$2"
if [[ $option = @(family|maxelem|hashsize|timeout|markmask) ]]; then
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
fi
_ipset_check_common_create_opts || $str_failcmd
case "$option" in
comment|counters|skbinfo) : ;;
timeout) shift ;;
forceadd)
if ((IPSET_VERSION[0] < 6)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
# ipset -ge v6.22 has forceadd flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 22)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
;;
markmask)
if ((IPSET_VERSION[0] < 6)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
# ipset -ge v6.21 has markmask flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 21)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
if [[ $str_type != hash:ip,mark ]]; then
log -E "Option \`$option' is only available for hash:ip,mark type of sets."
$str_failcmd
fi
val_32bit_off1 "$opt_arg" || {
log -E "$strERR Value \`$opt_arg' is not valid for the markmask option must be between 1 and 4294967295."
$str_failcmd
}
shift
;;
family)
if [[ $str_type = hash:mac ]]; then
log -E "$strERR Option \`$option' is not available for hash:mac type of sets."
$str_failcmd
fi
if [[ $opt_arg = inet6 ]]; then
log -E "Option argument \`$opt_arg' is not supported."
$str_failcmd
elif [[ $opt_arg = inet ]]; then :
else
log -E "$strERR Invalid value \`$opt_arg' with \`$option' for \`$str_type' set. Valid values: inet|inet6."
$str_failcmd
fi
shift
;;
hashsize)
val_numeric "$opt_arg" || $str_failcmd
shift
;;
maxelem)
val_numeric "$opt_arg" || $str_failcmd
shift
;;
netmask)
case "$str_type" in
hash:ip)
((opt_arg >= 1 && opt_arg <= 32)) || {
log -E "$strERR Invalid value \`$opt_arg' with \`$option' for \`$str_type' set. Valid range: 1-32."
$str_failcmd
}
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift
done
;;
list:set)
local -a arr_opts=("$@")
while (($#)); do
option="$1" opt_arg="$2"
if [[ $option = @(timeout|size) ]]; then
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
fi
_ipset_check_common_create_opts || $str_failcmd
case "$option" in
comment|counters|skbinfo) : ;;
timeout) shift ;;
size)
val_numeric "$opt_arg" || $str_failcmd
shift
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift
done
;;
ipmap)
local str_from="" str_to="" str_netmask=""
while (($#)); do
option="$1" opt_arg="$2"
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
case "$option" in
from)
val_ip4_ip "$opt_arg" || $str_failcmd
str_from="--$option $opt_arg"
;;
to)
val_ip4_ip "$opt_arg" || $str_failcmd
str_to="--$option $opt_arg"
;;
network)
val_ip4_cidr_net "$opt_arg" || $str_failcmd
str_network="--$option $opt_arg"
;;
netmask)
((opt_arg >= 1 opt_arg && <= 31)) || {
log -E "$strERR Invalid value \`$opt_arg' with \`$option' for \`$str_type' set. Valid range: 1-31."
$str_failcmd
}
str_netmask="--$option $opt_arg"
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift 2
done
if [[ $str_network ]]; then
if [[ $str_netmask ]]; then arr_opts=($str_network $str_netmask)
else arr_opts=($str_network)
fi
fi
if [[ $str_from ]]; then
if [[ $str_to ]]; then
if [[ $str_netmask ]]; then arr_opts=($str_from $str_to $str_netmask)
else arr_opts=($str_from $str_to)
fi
else
log -E "$strErr \`$str_type' needs \`from value to value' options."
$str_failcmd
fi
fi
if [[ $str_netmask ]] && [[ -z $str_from || -z $str_to ]]; then
log -E "$strErr \`$str_type' option \`netmask' needs \`from value to value' options."
$str_failcmd
fi
;;
macipmap)
local str_from="" str_to=""
while (($#)); do
option="$1" opt_arg="$2"
case "$option" in
from|to|network)
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
;;
esac
case "$option" in
from)
val_ip4_ip "$opt_arg" || $str_failcmd
str_from="--$option $opt_arg"
shift
;;
to)
val_ip4_ip "$opt_arg" || $str_failcmd
str_to="--$option $opt_arg"
shift
;;
network)
val_ip4_cidr_net "$opt_arg" || $str_failcmd
arr_opts=(--$option $opt_arg)
shift
;;
matchunset)
arr_opts[${#arr_opts[@]}]="--$option"
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift
done
if [[ $str_from && $str_to ]]; then
arr_opts=($str_from $str_to)
elif [[ $str_from && -z $str_to ]]; then
log -E "$strErr \`$str_type' needs \`from value to value' options."
$str_failcmd
fi
;;
portmap)
if (($# == 4)); then
if [[ $1 = from && $2 = +([[:digit:]]) && $3 = to && $4 = +([[:digit:]]) ]]; then
for rvar in $2 $4; do
val_port $rvar || $str_failcmd
done
arr_opts=(--from $2 --to $4)
else
log -E "$strErr \`$str_type' needs \`from value to value' options."
$str_failcmd
fi
else
log -E "$strErr \`$str_type' needs \`from value to value' options."
$str_failcmd
fi
;;
iphash)
while (($#)); do
option="$1" opt_arg="$2"
case "$option" in
hashsize|probes)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
resize)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
netmask)
((opt_arg >= 1 && opt_arg <= 31)) || {
log -E "$strERR Invalid value \`$opt_arg' with \`$option' for \`$str_type' set. Valid range: 1-31."
$str_failcmd
}
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift 2
done
;;
nethash)
while (($#)); do
option="$1" opt_arg="$2"
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
case "$option" in
hashsize|probes)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
resize)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift 2
done
;;
ipporthash|ipportiphash|ipportnethash)
local str_from="" str_to=""
while (($#)); do
option="$1" opt_arg="$2"
if [[ -z $opt_arg ]]; then
log -E "$strERR Missing value to option \`$option' for \`$str_type' set."
$str_failcmd
fi
case "$option" in
from)
val_ip4_ip "$opt_arg" || $str_failcmd
str_from="--$option $opt_arg"
;;
to)
val_ip4_ip "$opt_arg" || $str_failcmd
str_to="--$option $opt_arg"
;;
network)
val_ip4_cidr_net "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
hashsize|probes)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
resize)
val_numeric "$opt_arg" || $str_failcmd
arr_opts[${#arr_opts[@]}]="--$option $opt_arg"
;;
*) log -E "$strERR Invalid option \`$option' for \`$str_type' set."
$str_failcmd
esac
shift 2
done
if [[ $str_from && $str_to ]]; then
arr_opts[${#arr_opts[@]}]="$str_from $str_to"
else
if [[ $str_from && -z $str_to ]] || [[ -z $str_from && $str_to ]]; then
log -E "$strErr \`$str_type' needs \`from value to value' options."
$str_failcmd
fi
fi
;;
iptree|iptreemap|setlist)
local i_tmp=0
if [[ $1 ]]; then
case "$str_type" in
iptree) [[ $1 = timeout ]] && i_tmp=1 ;;
iptreemap) [[ $1 = gc ]] && i_tmp=1 ;;
setlist) [[ $1 = size ]] && i_tmp=1 ;;
esac
if ((i_tmp)); then
if [[ $2 ]]; then
if [[ $2 = +([[:digit:]]) ]]; then
arr_opts=(--$1 $2)
else
log -E "$strERR Invalid value \`$2' with option \`$1' for \`$str_type' set."
$str_failcmd
fi
else
log -E "$strERR Missing value to option \`$1' for \`$str_type' set."
$str_failcmd
fi
else
log -E "$strERR Invalid option \`$1' for \`$str_type' set."
$str_failcmd
fi
fi
;;
*) log -E "Unknown set type \`$str_type'."
$str_failcmd
esac
log -e "creating set: \`$str_set' of type: \`$str_type'"
IPSET_SETS_ARRAY[${#IPSET_SETS_ARRAY[@]}]="$str_create $str_set $str_type ${arr_opts[@]}"
CREATED_SETS_ARRAY[${#CREATED_SETS_ARRAY[@]}]="$str_set $str_type"
} # -------------------------------------------------------------------------
ipset_add() { # add an entry (cache) to an ipset set
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
((USE_IPSET)) || {
log -W "ipset usage is disabled. ipset commands will be ignored."
return 0
}
local str_set="$1" str_in="$2" str_entry="$2" str_cmd="-A" str_type str_in option opt_arg
local strERR="adding entry to set: \`$*'"
local str_failcmd='eval log -F "$strERR"; raise_cfg_err_count; return 0'
shift 2
local arr_opts=("$@")
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=add
fi
check_set_exist "$str_set" || {
log -E "ipset set \`$str_set' has not been created."
$str_failcmd
}
str_type=$(printf "%s\n" "${CREATED_SETS_ARRAY[@]}" | lsearch_echo "$str_set")
str_type=${str_type#$str_set }
case $str_type in
ipmap|bitmap:ip|iphash|hash:ip)
val_ip4_addr "$str_in" || $str_failcmd
;;
nethash|hash:net)
val_ip4_cidr_net "$str_in" || $str_failcmd
;;
macipmap|bitmap:ip,mac)
val_ip4_ip "${str_in%,*}" || $str_failcmd
val_mac "${str_in#*,}" || $str_failcmd
;;
portmap|bitmap:port)
case "$str_in" in
*-*|\[*\]-\[*\]) val_port_range_word "$str_in" || $str_failcmd ;;
*) val_port "$str_in" || $str_failcmd
esac
;;
iptree)
case "$str_in" in
*,*)
val_ip4_ip "${str_in%,*}" || $str_failcmd
val_numeric "${str_in#*,}" || $str_failcmd # timeout
;;
*) val_ip4_ip "$str_in" || $str_failcmd
esac
;;
iptreemap)
case "$str_in" in
*,*)
val_ip4_addr "${str_in%,*}" || $str_failcmd
val_numeric "${str_in#*,}" || $str_failcmd # timeout
;;
*) val_ip4_addr "$str_in" || $str_failcmd
esac
;;
ipporthash)
case "$str_in" in
*,*)
val_ip4_ip "${str_in%,*}" || $str_failcmd
val_port "${str_in#*,}" || $str_failcmd
;;
*) log -E "Invalid entry \`$str_in' expecting: ip,port."
$str_failcmd
esac
;;
ipportiphash)
case "$str_in" in
*,*,*)
val_ip4_ip "${str_in%%,*}" || $str_failcmd
val_ip4_ip "${str_in##*,}" || $str_failcmd
str_in="${str_in%,*}"
val_port "${str_in#*,}" || $str_failcmd
;;
*) log -E "Invalid entry \`$str_in' expecting: ip,port."
$str_failcmd
esac
;;
ipportnethash)
case "$str_in" in
*,*,*)
val_ip4_ip "${str_in%%,*}" || $str_failcmd
val_ip4_cidr_net "${str_in##*,}" || $str_failcmd
str_in="${str_in%,*}"
val_port "${str_in#*,}" || $str_failcmd
;;
*) log -E "Invalid entry \`$str_in' expecting: ip,port."
$str_failcmd
esac
;;
hash:ip,port|hash:net,port)
case "$str_in" in
*,*)
case "$str_type" in
hash:ip,port)
val_ip4_ip "${str_in%,*}" || $str_failcmd
;;
hash:net,port)
val_ip4_cidr_net "${str_in%,*}" || $str_failcmd
;;
esac
val_ipset_port_spec "${str_in#*,}" || $str_failcmd
;;
*) log -E "Invalid entry \`$str_in' expecting: ip,[proto:]port."
$str_failcmd
esac
;;
hash:ip,port,ip|hash:ip,port,net|hash:net,port,net)
case "$str_in" in
*,*,*)
case "$str_type" in
hash:ip,port,ip)
val_ip4_ip "${str_in%%,*}" || $str_failcmd
val_ip4_ip "${str_in##*,}" || $str_failcmd
;;
hash:ip,port,net)
val_ip4_ip "${str_in%%,*}" || $str_failcmd
val_ip4_cidr_net "${str_in##*,}" || $str_failcmd
;;
hash:net,port,net)
val_ip4_cidr_net "${str_in%%,*}" || $str_failcmd
val_ip4_cidr_net "${str_in##*,}" || $str_failcmd
;;
esac
str_in="${str_in%,*}"; str_in="${str_in#*,}"
val_ipset_port_spec "$str_in" || $str_failcmd
;;
*) log -E "Invalid entry \`$str_in' expecting: (ip|net),[proto:]port,[ip|net]."
$str_failcmd
esac
;;
hash:net,iface)
val_ip4_cidr_net "${str_in%,*}" || $str_failcmd
check_iface "${str_in#*,}" || $str_failcmd
;;
hash:ip,mark)
val_ip4_ip "${str_in%,*}" || $str_failcmd
val_32bit "${str_in#*,}" || $str_failcmd
;;
hash:mac)
val_mac "$str_in" || $str_failcmd
;;
setlist|list:set)
case "$str_in" in
*,@(before|after),*)
if [[ $IPSET_SYNTAX = old ]]; then
str_in="${str_in/,@(before|after),/ }"
else
log -E "Expecting \`before|after' as seperate option."
$str_failcmd
fi
;;
esac
for str_tmp in $str_in; do
check_set_exist "$str_tmp" || {
log -E "ipset set \`$str_tmp' has not been created."
$str_failcmd
}
done
;;
"")
log -U $FUNCNAME
log -f "$strERR"
raise_gen_err_count
return 1
;;
esac
while (($#)); do # remaining options
option="$1" opt_arg="$2"
shift
if [[ $IPSET_SYNTAX = new ]]; then
if [[ $option = @(bytes|packets|comment|nomatch|skbmark|skbprio|skbqueue) ]]; then
if ((IPSET_VERSION[0] < 6)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
fi
case "$option" in
comment)
# ipset -ge v6.20 has comment flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 20)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
shift
;;
bytes|packets)
# ipset -ge v6.19 has counters flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 19)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
val_numeric "$opt_arg" || $str_failcmd
shift
;;
skbmark|skbprio|skbqueue)
# ipset -ge v6.22 has comment flag
if ((IPSET_VERSION[0] == 6 && IPSET_VERSION[1] < 22)); then
log -E "Option \`$option' is not supported: ipset version is too old."
$str_failcmd
fi
shift
;;
timeout)
val_numeric "$opt_arg" || $str_failcmd
shift
;;
nomatch)
[[ $str_type = @(hash:net|hash:net,port|hash:ip,port,net|hash:net,iface|hash:net,port,net) ]] || {
log -E "Option \`$option' is not valid with set of type \`$str_type'."
$str_failcmd
}
;;
before|after)
[[ $str_type = list:set ]] || {
log -E "Option \`$option' is not valid with set of type \`$str_type'."
$str_failcmd
}
check_set_exist "$opt_arg" || {
log -E "ipset set \`$opt_arg' has not been created."
$str_failcmd
}
shift
;;
-\!|-exist|-q|-quiet) : ;;
*) log -E "Option \`$option' is unknown."
$str_failcmd
esac
else
case "$option" in
-q|--quiet) : ;;
quiet)
for opt_arg in ${!arr_opts[@]}; do
if [[ ${arr_opts[opt_arg]} = quiet ]]; then
arr_opts[opt_arg]=--quiet
break
fi
done
;;
*) log -E "Option \`$option' is unknown."
$str_failcmd
esac
fi
done
log -e "adding to set: $str_set $str_entry ${arr_opts[@]}"
IPSET_ENTRIES_ARRAY[${#IPSET_ENTRIES_ARRAY[@]}]="$str_cmd $str_set $str_entry ${arr_opts[@]}"
} # -------------------------------------------------------------------------
ipset_destroy() {
local str_set="$1" str_cmd="-X"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=destroy
fi
if [[ $str_set ]]; then
val_setname "$str_set" || return
run_ipset $str_cmd "$str_set"
else
run_ipset $str_cmd
fi
} # -------------------------------------------------------------------------
ipset_list() {
local str_set="$1" str_cmd="-L"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=list
fi
if [[ $str_set ]]; then
val_setname "$str_set" || return
run_ipset $str_cmd "$str_set"
else
run_ipset $str_cmd
fi
} # -------------------------------------------------------------------------
ipset_flush() {
local str_set="$1" str_cmd="-F"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=flush
fi
if [[ $str_set ]]; then
val_setname "$str_set" || return
run_ipset $str_cmd "$str_set"
else
run_ipset $str_cmd
fi
} # -------------------------------------------------------------------------
ipset_save() {
local str_set="$1" str_cmd="-S"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=save
fi
if [[ $str_set ]]; then
val_setname "$str_set" || return
run_ipset $str_cmd "$str_set"
else
run_ipset $str_cmd
fi
} # -------------------------------------------------------------------------
ipset_restore() {
local str_cmd="-R"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=restore
fi
run_ipset $str_cmd
} # -------------------------------------------------------------------------
ipset_test() {
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
local str_set="$1" str_entry="$2" str_cmd="-T"
if [[ $IPSET_SYNTAX = new ]]; then
str_cmd=test
fi
val_setname "$str_set" || return
run_ipset $str_cmd "$str_set" "$str_entry"
} # -------------------------------------------------------------------------
ipset_version_check() {
local str_typename
log -e "Checking for ipset version"
if ! [[ $IPSET && -x $IPSET ]]; then
USE_IPSET=0
return 0
else
if dn run_ipset list; then
IPSET_SYNTAX=new
IPSET_VERSION="$(run_ipset version)"
else
ISET_SYNTAX=old
IPSET_VERSION="$(run_ipset --version)"
fi
IPSET_VERSION="${IPSET_VERSION#ipset v}"
IPSET_VERSION="${IPSET_VERSION%,+([[:blank:]])protocol*}"
read -a IPSET_VERSION <<< ${IPSET_VERSION//./ }
[[ ${IPSET_VERSION[0]} = +([[:digit:]]) ]] || {
log -f "retrieving ipset version"
return $ER_FAIL
}
readonly IPSET_VERSION
[[ $DEBUG_INFO ]] && dbg_arr IPSET_VERSION
for str_typename in $(show_array_column IPARRAY_IPSET_SETTYPE_LIST); do
if ((IPSET_VERSION[0] >= 6)) && [[ $str_typename != *:* ]]; then
log -w "Adding \`$str_typename' set type to list of unsupported set types."
UNSUPPORTED_SETLIST+=" $str_typename"
continue
elif ((IPSET_VERSION[0] < 6)) && [[ $str_typename = *:* ]]; then
log -w "Adding \`$str_typename' set type to list of unsupported set types."
UNSUPPORTED_SETLIST+=" $str_typename"
continue
fi
done
if ((NO_IPT_COMPAT_CHECK)); then
readonly UNSUPPORTED_SETLIST
fi
fi
} # -------------------------------------------------------------------------
test_ipset() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ ${IPSET} && -x ${IPSET} ]] || return 0
((NO_IPT_COMPAT_CHECK == 0)) || return 0
local -i idx
local str_typename str_create="-N" str_destroy="-X" str_opt="ipmap --network 192.168.0.0/24" str_dn="dn"
[[ $DEBUG_INFO ]] && str_dn=""
if [[ $IPSET_SYNTAX = new ]]; then
str_create="create" str_destroy="destroy" str_opt="hash:ip"
fi
case "$1" in
create)
if ((USE_IPSET)); then
log -I "Checking for ipset set types"
for idx in ${!IPARRAY_IPSET_SETTYPE_LIST[@]}; do
set -- ${IPARRAY_IPSET_SETTYPE_LIST[idx]}
str_typename="$1"
shift
if ((IPSET_VERSION[0] >= 6)) && [[ $str_typename != *:* ]]; then
continue
elif ((IPSET_VERSION[0] < 6)) && [[ $str_typename = *:* ]]; then
continue
fi
log -e "${PF}checking for $str_typename type"
if $str_dn run_ipset $str_create ${ME}_TEST_SET $str_typename $@; then
$str_dn run_ipset $str_destroy ${ME}_TEST_SET
else
UNSUPPORTED_SETLIST+=" $str_typename"
log -f "creating set: \`${ME}_TEST_SET $str_typename $@'"
fi
done
readonly UNSUPPORTED_SETLIST
fi
# to make the test for the set match and SET target available,
# we don't honour USE_IPSET here.
log -w "creating ipset test set"
$str_dn run_ipset $str_create ${ME}_TEST_SET $str_opt || {
log -f "creating test set: ${ME}_TEST_SET $str_opt"
}
;;
delete)
# if ((IPSET_VERSION[0])); then # commented out as we bail out in version check
log -w "deleting ipset test set"
$str_dn run_ipset $str_destroy ${ME}_TEST_SET || {
log -f "deleting test set: \`${ME}_TEST_SET'"
}
# fi
;;
*) log -o "$1" $FUNCNAME; return 1
esac
} # -------------------------------------------------------------------------
ipset_flush_destroy() {
if [[ $IPSET_SYNTAX = new ]]; then
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]="flush"
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]="destroy"
else
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]="-F"
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]="-X"
fi
} # -------------------------------------------------------------------------
ipset_merge_arrays() { # join entries to their according sets
local str_set
local -i idx eidx
ipset_flush_destroy
for idx in ${!IPSET_SETS_ARRAY[@]}; do
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]=${IPSET_SETS_ARRAY[idx]}
set -- ${IPSET_SETS_ARRAY[idx]}
str_set=$2
for eidx in ${!IPSET_ENTRIES_ARRAY[@]}; do
set -- ${IPSET_ENTRIES_ARRAY[eidx]}
if [[ $2 = $str_set ]]; then
IPSET_CMD_ARRAY[${#IPSET_CMD_ARRAY[@]}]=$@
unset IPSET_ENTRIES_ARRAY[eidx]
fi
done
done