/
nwfilter_conf.c
3514 lines (2988 loc) · 103 KB
/
nwfilter_conf.c
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
/*
* nwfilter_conf.c: network filter XML processing
* (derived from storage_conf.c)
*
* Copyright (C) 2006-2013 Red Hat, Inc.
* Copyright (C) 2006-2008 Daniel P. Berrange
*
* Copyright (C) 2010-2011 IBM Corporation
* Copyright (C) 2010-2011 Stefan Berger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Stefan Berger <stefanb@us.ibm.com>
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#if HAVE_NET_ETHERNET_H
# include <net/ethernet.h>
#endif
#include <unistd.h>
#include "internal.h"
#include "viruuid.h"
#include "viralloc.h"
#include "virerror.h"
#include "datatypes.h"
#include "nwfilter_params.h"
#include "nwfilter_conf.h"
#include "domain_conf.h"
#include "c-ctype.h"
#include "virfile.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NWFILTER
VIR_ENUM_IMPL(virNWFilterRuleAction, VIR_NWFILTER_RULE_ACTION_LAST,
"drop",
"accept",
"reject",
"return",
"continue");
VIR_ENUM_IMPL(virNWFilterJumpTarget, VIR_NWFILTER_RULE_ACTION_LAST,
"DROP",
"ACCEPT",
"REJECT",
"RETURN",
"CONTINUE");
VIR_ENUM_IMPL(virNWFilterRuleDirection, VIR_NWFILTER_RULE_DIRECTION_LAST,
"in",
"out",
"inout");
VIR_ENUM_IMPL(virNWFilterChainPolicy, VIR_NWFILTER_CHAIN_POLICY_LAST,
"ACCEPT",
"DROP");
VIR_ENUM_IMPL(virNWFilterEbtablesTable, VIR_NWFILTER_EBTABLES_TABLE_LAST,
"filter",
"nat",
"broute");
VIR_ENUM_IMPL(virNWFilterChainSuffix, VIR_NWFILTER_CHAINSUFFIX_LAST,
"root",
"mac",
"vlan",
"stp",
"arp",
"rarp",
"ipv4",
"ipv6");
VIR_ENUM_IMPL(virNWFilterRuleProtocol, VIR_NWFILTER_RULE_PROTOCOL_LAST,
"none",
"mac",
"vlan",
"stp",
"arp",
"rarp",
"ip",
"ipv6",
"tcp",
"icmp",
"igmp",
"udp",
"udplite",
"esp",
"ah",
"sctp",
"all",
"tcp-ipv6",
"icmpv6",
"udp-ipv6",
"udplite-ipv6",
"esp-ipv6",
"ah-ipv6",
"sctp-ipv6",
"all-ipv6");
/*
* a map entry for a simple static int-to-string map
*/
struct int_map {
int32_t attr;
const char *val;
};
#define INTMAP_ENTRY(ATT, VAL) { .attr = ATT, .val = VAL }
#define INTMAP_ENTRY_LAST { .val = NULL }
static const struct int_map chain_priorities[] = {
INTMAP_ENTRY(NWFILTER_ROOT_FILTER_PRI, "root"),
INTMAP_ENTRY(NWFILTER_MAC_FILTER_PRI, "mac"),
INTMAP_ENTRY(NWFILTER_VLAN_FILTER_PRI, "vlan"),
INTMAP_ENTRY(NWFILTER_IPV4_FILTER_PRI, "ipv4"),
INTMAP_ENTRY(NWFILTER_IPV6_FILTER_PRI, "ipv6"),
INTMAP_ENTRY(NWFILTER_ARP_FILTER_PRI, "arp"),
INTMAP_ENTRY(NWFILTER_RARP_FILTER_PRI, "rarp"),
INTMAP_ENTRY_LAST,
};
/*
* only one filter update allowed
*/
static virMutex updateMutex;
static bool initialized = false;
void
virNWFilterLockFilterUpdates(void) {
virMutexLock(&updateMutex);
}
void
virNWFilterUnlockFilterUpdates(void) {
virMutexUnlock(&updateMutex);
}
/*
* attribute names for the rules XML
*/
static const char srcmacaddr_str[] = "srcmacaddr";
static const char srcmacmask_str[] = "srcmacmask";
static const char dstmacaddr_str[] = "dstmacaddr";
static const char dstmacmask_str[] = "dstmacmask";
static const char arpsrcmacaddr_str[]= "arpsrcmacaddr";
static const char arpdstmacaddr_str[]= "arpdstmacaddr";
static const char arpsrcipaddr_str[] = "arpsrcipaddr";
static const char arpdstipaddr_str[] = "arpdstipaddr";
static const char srcipaddr_str[] = "srcipaddr";
static const char srcipmask_str[] = "srcipmask";
static const char dstipaddr_str[] = "dstipaddr";
static const char dstipmask_str[] = "dstipmask";
static const char srcipfrom_str[] = "srcipfrom";
static const char srcipto_str[] = "srcipto";
static const char dstipfrom_str[] = "dstipfrom";
static const char dstipto_str[] = "dstipto";
static const char srcportstart_str[] = "srcportstart";
static const char srcportend_str[] = "srcportend";
static const char dstportstart_str[] = "dstportstart";
static const char dstportend_str[] = "dstportend";
static const char dscp_str[] = "dscp";
static const char state_str[] = "state";
static const char ipset_str[] = "ipset";
static const char ipsetflags_str[] = "ipsetflags";
#define SRCMACADDR srcmacaddr_str
#define SRCMACMASK srcmacmask_str
#define DSTMACADDR dstmacaddr_str
#define DSTMACMASK dstmacmask_str
#define ARPSRCMACADDR arpsrcmacaddr_str
#define ARPDSTMACADDR arpdstmacaddr_str
#define ARPSRCIPADDR arpsrcipaddr_str
#define ARPDSTIPADDR arpdstipaddr_str
#define SRCIPADDR srcipaddr_str
#define SRCIPMASK srcipmask_str
#define DSTIPADDR dstipaddr_str
#define DSTIPMASK dstipmask_str
#define SRCIPFROM srcipfrom_str
#define SRCIPTO srcipto_str
#define DSTIPFROM dstipfrom_str
#define DSTIPTO dstipto_str
#define SRCPORTSTART srcportstart_str
#define SRCPORTEND srcportend_str
#define DSTPORTSTART dstportstart_str
#define DSTPORTEND dstportend_str
#define DSCP dscp_str
#define STATE state_str
#define IPSET ipset_str
#define IPSETFLAGS ipsetflags_str
/**
* intMapGetByInt:
* @intmap: Pointer to int-to-string map
* @attr: The attribute to look up
* @res: Pointer to string pointer for result
*
* Returns 0 if value was found with result returned, -1 otherwise.
*
* lookup a map entry given the integer.
*/
static int
intMapGetByInt(const struct int_map *intmap, int32_t attr, const char **res)
{
size_t i = 0;
bool found = false;
while (intmap[i].val && !found) {
if (intmap[i].attr == attr) {
*res = intmap[i].val;
found = true;
}
i++;
}
return (found) ? 0 : -1;
}
/**
* intMapGetByString:
* @intmap: Pointer to int-to-string map
* @str: Pointer to string for which to find the entry
* @casecmp : Whether to ignore case when doing string matching
* @result: Pointer to int for result
*
* Returns 0 if entry was found, -1 otherwise.
*
* Do a lookup in the map trying to find an integer key using the string
* value. Returns 0 if entry was found with result returned, -1 otherwise.
*/
static int
intMapGetByString(const struct int_map *intmap, const char *str, int casecmp,
int32_t *result)
{
size_t i = 0;
bool found = false;
while (intmap[i].val && !found) {
if ((casecmp && STRCASEEQ(intmap[i].val, str)) ||
STREQ(intmap[i].val, str)) {
*result = intmap[i].attr;
found = true;
}
i++;
}
return (found) ? 0 : -1;
}
void
virNWFilterRuleDefFree(virNWFilterRuleDefPtr def) {
size_t i;
if (!def)
return;
for (i = 0; i < def->nVarAccess; i++)
virNWFilterVarAccessFree(def->varAccess[i]);
for (i = 0; i < def->nstrings; i++)
VIR_FREE(def->strings[i]);
VIR_FREE(def->varAccess);
VIR_FREE(def->strings);
VIR_FREE(def);
}
static void
virNWFilterIncludeDefFree(virNWFilterIncludeDefPtr inc) {
if (!inc)
return;
virNWFilterHashTableFree(inc->params);
VIR_FREE(inc->filterref);
VIR_FREE(inc);
}
static void
virNWFilterEntryFree(virNWFilterEntryPtr entry) {
if (!entry)
return;
virNWFilterRuleDefFree(entry->rule);
virNWFilterIncludeDefFree(entry->include);
VIR_FREE(entry);
}
void
virNWFilterDefFree(virNWFilterDefPtr def) {
size_t i;
if (!def)
return;
VIR_FREE(def->name);
for (i = 0; i < def->nentries; i++)
virNWFilterEntryFree(def->filterEntries[i]);
VIR_FREE(def->filterEntries);
VIR_FREE(def->chainsuffix);
VIR_FREE(def);
}
void
virNWFilterObjFree(virNWFilterObjPtr obj)
{
if (!obj)
return;
virNWFilterDefFree(obj->def);
virNWFilterDefFree(obj->newDef);
VIR_FREE(obj->configFile);
virMutexDestroy(&obj->lock);
VIR_FREE(obj);
}
void
virNWFilterObjListFree(virNWFilterObjListPtr nwfilters)
{
size_t i;
for (i = 0; i < nwfilters->count; i++)
virNWFilterObjFree(nwfilters->objs[i]);
VIR_FREE(nwfilters->objs);
nwfilters->count = 0;
}
static int
virNWFilterRuleDefAddVar(virNWFilterRuleDefPtr nwf,
nwItemDesc *item,
const char *var)
{
size_t i = 0;
virNWFilterVarAccessPtr varAccess;
varAccess = virNWFilterVarAccessParse(var);
if (varAccess == NULL)
return -1;
if (nwf->varAccess) {
for (i = 0; i < nwf->nVarAccess; i++)
if (virNWFilterVarAccessEqual(nwf->varAccess[i], varAccess)) {
virNWFilterVarAccessFree(varAccess);
item->varAccess = nwf->varAccess[i];
return 0;
}
}
if (VIR_EXPAND_N(nwf->varAccess, nwf->nVarAccess, 1) < 0) {
virNWFilterVarAccessFree(varAccess);
return -1;
}
nwf->varAccess[nwf->nVarAccess - 1] = varAccess;
item->varAccess = varAccess;
return 0;
}
static char *
virNWFilterRuleDefAddString(virNWFilterRuleDefPtr nwf,
const char *string,
size_t maxstrlen)
{
if (VIR_REALLOC_N(nwf->strings, nwf->nstrings+1) < 0)
return NULL;
if (VIR_STRNDUP(nwf->strings[nwf->nstrings], string, maxstrlen) < 0)
return NULL;
nwf->nstrings++;
return nwf->strings[nwf->nstrings-1];
}
void
virNWFilterObjRemove(virNWFilterObjListPtr nwfilters,
virNWFilterObjPtr nwfilter)
{
size_t i;
virNWFilterObjUnlock(nwfilter);
for (i = 0; i < nwfilters->count; i++) {
virNWFilterObjLock(nwfilters->objs[i]);
if (nwfilters->objs[i] == nwfilter) {
virNWFilterObjUnlock(nwfilters->objs[i]);
virNWFilterObjFree(nwfilters->objs[i]);
if (i < (nwfilters->count - 1))
memmove(nwfilters->objs + i, nwfilters->objs + i + 1,
sizeof(*(nwfilters->objs)) * (nwfilters->count - (i + 1)));
if (VIR_REALLOC_N(nwfilters->objs, nwfilters->count - 1) < 0) {
; /* Failure to reduce memory allocation isn't fatal */
}
nwfilters->count--;
break;
}
virNWFilterObjUnlock(nwfilters->objs[i]);
}
}
union data {
void *v;
char *c;
unsigned char *uc;
unsigned int ui;
};
typedef bool (*valueValidator)(enum attrDatatype datatype, union data *valptr,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item);
typedef bool (*valueFormatter)(virBufferPtr buf,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item);
typedef struct _virXMLAttr2Struct virXMLAttr2Struct;
struct _virXMLAttr2Struct
{
const char *name; /* attribute name */
enum attrDatatype datatype;
int dataIdx; /* offset of the hasXYZ boolean */
valueValidator validator; /* beyond-standard checkers */
valueFormatter formatter; /* beyond-standard formatter */
size_t maxstrlen;
};
static const struct int_map macProtoMap[] = {
INTMAP_ENTRY(ETHERTYPE_ARP, "arp"),
INTMAP_ENTRY(ETHERTYPE_REVARP, "rarp"),
INTMAP_ENTRY(ETHERTYPE_IP, "ipv4"),
INTMAP_ENTRY(ETHERTYPE_IPV6, "ipv6"),
INTMAP_ENTRY(ETHERTYPE_VLAN, "vlan"),
INTMAP_ENTRY_LAST
};
static bool
checkMacProtocolID(enum attrDatatype datatype, union data *value,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
int32_t res = -1;
if (datatype == DATATYPE_STRING) {
if (intMapGetByString(macProtoMap, value->c, 1, &res) < 0)
res = -1;
datatype = DATATYPE_UINT16;
} else if (datatype == DATATYPE_UINT16 ||
datatype == DATATYPE_UINT16_HEX) {
res = value->ui;
if (res < 0x600)
res = -1;
}
if (res != -1) {
nwf->p.ethHdrFilter.dataProtocolID.u.u16 = res;
nwf->p.ethHdrFilter.dataProtocolID.datatype = datatype;
return true;
}
return false;
}
static bool
macProtocolIDFormatter(virBufferPtr buf,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
const char *str = NULL;
bool asHex = true;
if (intMapGetByInt(macProtoMap,
nwf->p.ethHdrFilter.dataProtocolID.u.u16,
&str) == 0) {
virBufferAdd(buf, str, -1);
} else {
if (nwf->p.ethHdrFilter.dataProtocolID.datatype == DATATYPE_UINT16)
asHex = false;
virBufferAsprintf(buf, asHex ? "0x%x" : "%d",
nwf->p.ethHdrFilter.dataProtocolID.u.u16);
}
return true;
}
static bool
checkVlanVlanID(enum attrDatatype datatype, union data *value,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
int32_t res;
res = value->ui;
if (res < 0 || res > 4095) {
res = -1;
}
if (res != -1) {
nwf->p.vlanHdrFilter.dataVlanID.u.u16 = res;
nwf->p.vlanHdrFilter.dataVlanID.datatype = datatype;
return true;
}
return false;
}
static bool
checkVlanProtocolID(enum attrDatatype datatype, union data *value,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
int32_t res = -1;
if (datatype == DATATYPE_STRING) {
if (intMapGetByString(macProtoMap, value->c, 1, &res) < 0)
res = -1;
datatype = DATATYPE_UINT16;
} else if (datatype == DATATYPE_UINT16 ||
datatype == DATATYPE_UINT16_HEX) {
res = value->ui;
if (res < 0x3c)
res = -1;
}
if (res != -1) {
nwf->p.vlanHdrFilter.dataVlanEncap.u.u16 = res;
nwf->p.vlanHdrFilter.dataVlanEncap.datatype = datatype;
return true;
}
return false;
}
static bool
vlanProtocolIDFormatter(virBufferPtr buf,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
const char *str = NULL;
bool asHex = true;
if (intMapGetByInt(macProtoMap,
nwf->p.vlanHdrFilter.dataVlanEncap.u.u16,
&str) == 0) {
virBufferAdd(buf, str, -1);
} else {
if (nwf->p.vlanHdrFilter.dataVlanEncap.datatype == DATATYPE_UINT16)
asHex = false;
virBufferAsprintf(buf, asHex ? "0x%x" : "%d",
nwf->p.vlanHdrFilter.dataVlanEncap.u.u16);
}
return true;
}
/* generic function to check for a valid (ipv4,ipv6, mac) mask
* A mask is valid of there is a sequence of 1's followed by a sequence
* of 0s or only 1s or only 0s
*/
static bool
checkValidMask(unsigned char *data, int len)
{
uint32_t idx = 0;
uint8_t mask = 0x80;
bool checkones = true;
while ((idx >> 3) < len) {
if (checkones) {
if (!(data[idx>>3] & mask))
checkones = false;
} else {
if ((data[idx>>3] & mask))
return false;
}
idx++;
mask >>= 1;
if (!mask)
mask = 0x80;
}
return true;
}
static bool
checkMACMask(enum attrDatatype datatype ATTRIBUTE_UNUSED,
union data *macMask,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
return checkValidMask(macMask->uc, 6);
}
/*
* supported arp opcode -- see 'ebtables -h arp' for the naming
*/
static const struct int_map arpOpcodeMap[] = {
INTMAP_ENTRY(1, "Request"),
INTMAP_ENTRY(2, "Reply"),
INTMAP_ENTRY(3, "Request_Reverse"),
INTMAP_ENTRY(4, "Reply_Reverse"),
INTMAP_ENTRY(5, "DRARP_Request"),
INTMAP_ENTRY(6, "DRARP_Reply"),
INTMAP_ENTRY(7, "DRARP_Error"),
INTMAP_ENTRY(8, "InARP_Request"),
INTMAP_ENTRY(9, "ARP_NAK"),
INTMAP_ENTRY_LAST
};
static bool
arpOpcodeValidator(enum attrDatatype datatype,
union data *value,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
int32_t res = -1;
if (datatype == DATATYPE_STRING) {
if (intMapGetByString(arpOpcodeMap, value->c, 1, &res) < 0)
res = -1;
datatype = DATATYPE_UINT16;
} else if (datatype == DATATYPE_UINT16 ||
datatype == DATATYPE_UINT16_HEX) {
res = (uint32_t)value->ui;
}
if (res != -1) {
nwf->p.arpHdrFilter.dataOpcode.u.u16 = res;
nwf->p.arpHdrFilter.dataOpcode.datatype = datatype;
return true;
}
return false;
}
static bool
arpOpcodeFormatter(virBufferPtr buf,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
const char *str = NULL;
if (intMapGetByInt(arpOpcodeMap,
nwf->p.arpHdrFilter.dataOpcode.u.u16,
&str) == 0) {
virBufferAdd(buf, str, -1);
} else {
virBufferAsprintf(buf, "%d", nwf->p.arpHdrFilter.dataOpcode.u.u16);
}
return true;
}
static const struct int_map ipProtoMap[] = {
INTMAP_ENTRY(IPPROTO_TCP, "tcp"),
INTMAP_ENTRY(IPPROTO_UDP, "udp"),
#ifdef IPPROTO_UDPLITE
INTMAP_ENTRY(IPPROTO_UDPLITE, "udplite"),
#endif
INTMAP_ENTRY(IPPROTO_ESP, "esp"),
INTMAP_ENTRY(IPPROTO_AH, "ah"),
INTMAP_ENTRY(IPPROTO_ICMP, "icmp"),
INTMAP_ENTRY(IPPROTO_IGMP, "igmp"),
#ifdef IPPROTO_SCTP
INTMAP_ENTRY(IPPROTO_SCTP, "sctp"),
#endif
INTMAP_ENTRY(IPPROTO_ICMPV6, "icmpv6"),
INTMAP_ENTRY_LAST
};
static bool
checkIPProtocolID(enum attrDatatype datatype,
union data *value,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
int32_t res = -1;
if (datatype == DATATYPE_STRING) {
if (intMapGetByString(ipProtoMap, value->c, 1, &res) < 0)
res = -1;
datatype = DATATYPE_UINT8_HEX;
} else if (datatype == DATATYPE_UINT8 ||
datatype == DATATYPE_UINT8_HEX) {
res = (uint32_t)value->ui;
}
if (res != -1) {
nwf->p.ipHdrFilter.ipHdr.dataProtocolID.u.u8 = res;
nwf->p.ipHdrFilter.ipHdr.dataProtocolID.datatype = datatype;
return true;
}
return false;
}
static bool
formatIPProtocolID(virBufferPtr buf,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
const char *str = NULL;
bool asHex = true;
if (intMapGetByInt(ipProtoMap,
nwf->p.ipHdrFilter.ipHdr.dataProtocolID.u.u8,
&str) == 0) {
virBufferAdd(buf, str, -1);
} else {
if (nwf->p.ipHdrFilter.ipHdr.dataProtocolID.datatype == DATATYPE_UINT8)
asHex = false;
virBufferAsprintf(buf, asHex ? "0x%x" : "%d",
nwf->p.ipHdrFilter.ipHdr.dataProtocolID.u.u8);
}
return true;
}
static bool
dscpValidator(enum attrDatatype datatype, union data *val,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item ATTRIBUTE_UNUSED)
{
uint8_t dscp = val->ui;
if (dscp > 63)
return false;
nwf->p.ipHdrFilter.ipHdr.dataDSCP.datatype = datatype;
return true;
}
static const struct int_map stateMatchMap[] = {
INTMAP_ENTRY(RULE_FLAG_STATE_NEW, "NEW"),
INTMAP_ENTRY(RULE_FLAG_STATE_ESTABLISHED, "ESTABLISHED"),
INTMAP_ENTRY(RULE_FLAG_STATE_RELATED, "RELATED"),
INTMAP_ENTRY(RULE_FLAG_STATE_INVALID, "INVALID"),
INTMAP_ENTRY(RULE_FLAG_STATE_NONE, "NONE"),
INTMAP_ENTRY_LAST,
};
static int
parseStringItems(const struct int_map *int_map,
const char *input, int32_t *flags, char sep)
{
int rc = 0;
size_t i, j;
bool found;
i = 0;
while (input[i]) {
found = false;
while (c_isspace(input[i]) || input[i] == sep)
i++;
if (!input[i])
break;
for (j = 0; int_map[j].val; j++) {
if (STRCASEEQLEN(&input[i], int_map[j].val,
strlen(int_map[j].val))) {
*flags |= int_map[j].attr;
i += strlen(int_map[j].val);
found = true;
break;
}
}
if (!found) {
rc = -1;
break;
}
}
return rc;
}
static int
printStringItems(virBufferPtr buf, const struct int_map *int_map,
int32_t flags, const char *sep)
{
size_t i;
unsigned int c = 0;
int32_t mask = 0x1;
while (mask) {
if ((mask & flags)) {
for (i = 0; int_map[i].val; i++) {
if (mask == int_map[i].attr) {
if (c >= 1)
virBufferAdd(buf, sep, -1);
virBufferAdd(buf, int_map[i].val, -1);
c++;
}
}
flags ^= mask;
}
if (!flags)
break;
mask <<= 1;
}
return 0;
}
static int
parseStateMatch(const char *statematch, int32_t *flags)
{
int rc = parseStringItems(stateMatchMap, statematch, flags, ',');
if ((*flags & RULE_FLAG_STATE_NONE))
*flags = RULE_FLAG_STATE_NONE;
return rc;
}
void
virNWFilterPrintStateMatchFlags(virBufferPtr buf, const char *prefix,
int32_t flags, bool disp_none)
{
if (!disp_none && (flags & RULE_FLAG_STATE_NONE))
return;
virBufferAdd(buf, prefix, -1);
printStringItems(buf, stateMatchMap, flags, ",");
}
static bool
stateValidator(enum attrDatatype datatype ATTRIBUTE_UNUSED, union data *val,
virNWFilterRuleDefPtr nwf,
nwItemDesc *item)
{
char *input = val->c;
int32_t flags = 0;
if (parseStateMatch(input, &flags) < 0)
return false;
item->u.u16 = flags;
nwf->flags |= flags;
item->datatype = DATATYPE_UINT16;
return true;
}
static bool
stateFormatter(virBufferPtr buf,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item)
{
virNWFilterPrintStateMatchFlags(buf, "", item->u.u16, true);
return true;
}
static const struct int_map tcpFlags[] = {
INTMAP_ENTRY(0x1, "FIN"),
INTMAP_ENTRY(0x2, "SYN"),
INTMAP_ENTRY(0x4, "RST"),
INTMAP_ENTRY(0x8, "PSH"),
INTMAP_ENTRY(0x10, "ACK"),
INTMAP_ENTRY(0x20, "URG"),
INTMAP_ENTRY(0x3F, "ALL"),
INTMAP_ENTRY(0x0, "NONE"),
INTMAP_ENTRY_LAST
};
static bool
tcpFlagsValidator(enum attrDatatype datatype ATTRIBUTE_UNUSED, union data *val,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item)
{
bool rc = false;
char *s_mask = val->c;
char *sep = strchr(val->c, '/');
char *s_flags;
int32_t mask = 0, flags = 0;
if (!sep)
return false;
s_flags = sep + 1;
*sep = '\0';
if (parseStringItems(tcpFlags, s_mask, &mask, ',') == 0 &&
parseStringItems(tcpFlags, s_flags, &flags, ',') == 0) {
item->u.tcpFlags.mask = mask & 0x3f;
item->u.tcpFlags.flags = flags & 0x3f;
rc = true;
}
*sep = '/';
return rc;
}
static void
printTCPFlags(virBufferPtr buf, uint8_t flags)
{
if (flags == 0)
virBufferAddLit(buf, "NONE");
else if (flags == 0x3f)
virBufferAddLit(buf, "ALL");
else
printStringItems(buf, tcpFlags, flags, ",");
}
void
virNWFilterPrintTCPFlags(virBufferPtr buf,
uint8_t mask, char sep, uint8_t flags)
{
printTCPFlags(buf, mask);
virBufferAddChar(buf, sep);
printTCPFlags(buf, flags);
}
static bool
tcpFlagsFormatter(virBufferPtr buf,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item)
{
virNWFilterPrintTCPFlags(buf,
item->u.tcpFlags.mask,
'/',
item->u.tcpFlags.flags);
return true;
}
static bool
ipsetValidator(enum attrDatatype datatype ATTRIBUTE_UNUSED, union data *val,
virNWFilterRuleDefPtr nwf ATTRIBUTE_UNUSED,
nwItemDesc *item)
{
const char *errmsg = NULL;
if (virStrcpy(item->u.ipset.setname, val->c,
sizeof(item->u.ipset.setname)) == NULL) {
errmsg = _("ipset name is too long");
goto arg_err_exit;
}
if (item->u.ipset.setname[strspn(item->u.ipset.setname,
VALID_IPSETNAME)] != 0) {
errmsg = _("ipset name contains invalid characters");
goto arg_err_exit;
}