Repository navigation
Expand file tree
/
Copy pathexpr.go
More file actions
2998 lines (2709 loc) · 67.3 KB
/
Copy pathexpr.go
File metadata and controls
2998 lines (2709 loc) · 67.3 KB
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
package nftnl
import (
"encoding/binary"
"fmt"
"golang.org/x/sys/unix"
)
// Register IDs
//
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L14
type Reg uint32
const (
RegVerdict Reg = iota
Reg1
Reg2
Reg3
Reg4
Reg32_00 Reg = iota + 3
Reg32_01
Reg32_02
Reg32_03
Reg32_04
Reg32_05
Reg32_06
Reg32_07
Reg32_08
Reg32_09
Reg32_10
Reg32_11
Reg32_12
Reg32_13
Reg32_14
Reg32_15
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L545
const (
nftaExprName = unix.NFTA_EXPR_NAME
nftaExprData = unix.NFTA_EXPR_DATA
)
type exprName string
const (
exprNameBitwise exprName = "bitwise"
exprNameByteorder exprName = "byteorder"
exprNameCmp exprName = "cmp"
exprNameConnlimit exprName = "connlimit"
exprNameCounter exprName = "counter"
exprNameCt exprName = "ct"
exprNameData exprName = "data"
exprNameDup exprName = "dup"
exprNameDynset exprName = "dynset"
exprNameExthdr exprName = "exthdr"
exprNameFib exprName = "fib"
exprNameFlowOffload exprName = "flow_offload"
exprNameFwd exprName = "fwd"
exprNameHash exprName = "hash"
exprNameImmediate exprName = "immediate"
exprNameInner exprName = "inner"
exprNameLast exprName = "last"
exprNameLimit exprName = "limit"
exprNameLog exprName = "log"
exprNameLookup exprName = "lookup"
exprNameMasq exprName = "masq"
exprNameMeta exprName = "meta"
exprNameNat exprName = "nat"
exprNameNumgen exprName = "numgen"
exprNameObjref exprName = "objref"
exprNameOsf exprName = "osf"
exprNamePayload exprName = "payload"
exprNameQueue exprName = "queue"
exprNameQuota exprName = "quota"
exprNameRange exprName = "range"
exprNameRedir exprName = "redir"
exprNameReject exprName = "reject"
exprNameRt exprName = "rt"
exprNameSocket exprName = "socket"
exprNameSynproxy exprName = "synproxy"
exprNameTproxy exprName = "tproxy"
exprNameTunnel exprName = "tunnel"
exprNameVerdict exprName = "verdict"
exprNameXfrm exprName = "xfrm"
)
// Expr is implemented by each expression type. The interface methods are
// unexported so only types in this package can satisfy it.
type Expr interface {
exprName() exprName
marshal() ([]byte, error)
unmarshal(data []byte) error
}
func marshalExpr(a Expr) ([]byte, error) {
ae := newAttributeEncoder()
ae.String(nftaExprName, string(a.exprName()))
data, err := a.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaExprData, data)
return ae.Encode()
}
func unmarshalExpr(data []byte) (Expr, error) {
ad, err := newAttributeDecoder(data)
if err != nil {
return nil, err
}
var name exprName
var result Expr
for ad.Next() {
switch ad.Type() {
case nftaExprName:
name = exprName(ad.String())
case nftaExprData:
e, err := exprDataFactory(name)
if err != nil {
return nil, err
}
if err := e.unmarshal(ad.Bytes()); err != nil {
return nil, err
}
result = e
}
}
if err := ad.Err(); err != nil {
return nil, err
}
return result, nil
}
func exprDataFactory(name exprName) (Expr, error) {
switch name {
case exprNameBitwise:
return &ExprBitwise{}, nil
case exprNameByteorder:
return &ExprByteorder{}, nil
case exprNameCmp:
return &ExprCmp{}, nil
case exprNameConnlimit:
return &ExprConnlimit{}, nil
case exprNameCounter:
return &ExprCounter{}, nil
case exprNameCt:
return &ExprCt{}, nil
case exprNameData:
return &ExprData{}, nil
case exprNameDup:
return &ExprDup{}, nil
case exprNameDynset:
return &ExprDynset{}, nil
case exprNameExthdr:
return &ExprExthdr{}, nil
case exprNameFib:
return &ExprFib{}, nil
case exprNameFlowOffload:
return &ExprFlowOffload{}, nil
case exprNameFwd:
return &ExprFwd{}, nil
case exprNameHash:
return &ExprHash{}, nil
case exprNameImmediate:
return &ExprImmediate{}, nil
case exprNameInner:
return &ExprInner{}, nil
case exprNameLast:
return &ExprLast{}, nil
case exprNameLimit:
return &ExprLimit{}, nil
case exprNameLog:
return &ExprLog{}, nil
case exprNameLookup:
return &ExprLookup{}, nil
case exprNameMasq:
return &ExprMasq{}, nil
case exprNameMeta:
return &ExprMeta{}, nil
case exprNameNat:
return &ExprNat{}, nil
case exprNameNumgen:
return &ExprNumgen{}, nil
case exprNameObjref:
return &ExprObjref{}, nil
case exprNameOsf:
return &ExprOsf{}, nil
case exprNamePayload:
return &ExprPayload{}, nil
case exprNameQueue:
return &ExprQueue{}, nil
case exprNameQuota:
return &ExprQuota{}, nil
case exprNameRange:
return &ExprRange{}, nil
case exprNameRedir:
return &ExprRedir{}, nil
case exprNameReject:
return &ExprReject{}, nil
case exprNameRt:
return &ExprRt{}, nil
case exprNameSocket:
return &ExprSocket{}, nil
case exprNameSynproxy:
return &ExprSynproxy{}, nil
case exprNameTproxy:
return &ExprTproxy{}, nil
case exprNameTunnel:
return &ExprTunnel{}, nil
case exprNameVerdict:
return &ExprVerdict{}, nil
case exprNameXfrm:
return &ExprXfrm{}, nil
default:
return nil, fmt.Errorf("unknown expr name %q", name)
}
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L597
const (
nftaBitwiseSreg = unix.NFTA_BITWISE_SREG
nftaBitwiseDreg = unix.NFTA_BITWISE_DREG
nftaBitwiseLen = unix.NFTA_BITWISE_LEN
nftaBitwiseMask = unix.NFTA_BITWISE_MASK
nftaBitwiseXor = unix.NFTA_BITWISE_XOR
nftaBitwiseOp = 0x06
nftaBitwiseData = 0x07
nftaBitwiseSreg2 = 0x08
)
// Bitwise operations for ExprBitwise.Op
//
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L573
type BitwiseOp uint32
const (
BitwiseMaskXor BitwiseOp = iota
BitwiseLshift
BitwiseRshift
BitwiseAnd
BitwiseOr
BitwiseXor
)
// Bitwise expression attributes
//
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L597
type ExprBitwise struct {
// Source register
SReg *Reg
// Destination register
DReg *Reg
// Length of operants
Len *uint32
// Mask value
Mask *ExprData
// XOR value
Xor *ExprData
// Type of operation
Op *BitwiseOp
// Argument for non-boolean operations
Data *ExprData
// Second source register
Sreg2 *Reg
}
func (ExprBitwise) exprName() exprName { return exprNameBitwise }
func (a *ExprBitwise) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.SReg != nil {
ae.Uint32(nftaBitwiseSreg, uint32(*a.SReg))
}
if a.DReg != nil {
ae.Uint32(nftaBitwiseDreg, uint32(*a.DReg))
}
if a.Len != nil {
ae.Uint32(nftaBitwiseLen, *a.Len)
}
if a.Mask != nil {
b, err := a.Mask.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaBitwiseMask, b)
}
if a.Xor != nil {
b, err := a.Xor.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaBitwiseXor, b)
}
if a.Op != nil {
ae.Uint32(nftaBitwiseOp, uint32(*a.Op))
}
if a.Data != nil {
b, err := a.Data.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaBitwiseData, b)
}
if a.Sreg2 != nil {
ae.Uint32(nftaBitwiseSreg2, uint32(*a.Sreg2))
}
return ae.Encode()
}
func (a *ExprBitwise) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaBitwiseSreg:
a.SReg = new(Reg(ad.Uint32()))
case nftaBitwiseDreg:
a.DReg = new(Reg(ad.Uint32()))
case nftaBitwiseLen:
a.Len = new(ad.Uint32())
case nftaBitwiseMask:
a.Mask = &ExprData{}
if err := a.Mask.unmarshal(ad.Bytes()); err != nil {
return err
}
case nftaBitwiseXor:
a.Xor = &ExprData{}
if err := a.Xor.unmarshal(ad.Bytes()); err != nil {
return err
}
case nftaBitwiseOp:
v := BitwiseOp(ad.Uint32())
a.Op = &v
case nftaBitwiseData:
a.Data = &ExprData{}
if err := a.Data.unmarshal(ad.Bytes()); err != nil {
return err
}
case nftaBitwiseSreg2:
v := Reg(ad.Uint32())
a.Sreg2 = &v
}
}
return ad.Err()
}
// Byteorder operators for ExprByteorder.Op
//
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L637
type ByteorderOp uint32
const (
// Network to host operator
ByteorderNtoh ByteorderOp = iota
// Host to network operator
ByteorderHton
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L648
const (
nftaByteorderSreg = unix.NFTA_BYTEORDER_SREG
nftaByteorderDreg = unix.NFTA_BYTEORDER_DREG
nftaByteorderOp = unix.NFTA_BYTEORDER_OP
nftaByteorderLen = unix.NFTA_BYTEORDER_LEN
nftaByteorderSize = unix.NFTA_BYTEORDER_SIZE
)
// Byte order expression attributes
//
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L648
type ExprByteorder struct {
// Source register
SReg *Reg
// Destination register
DReg *Reg
// Byteorder operators
Op *ByteorderOp
// Length of the data
Len *uint32
// Data size in bytes, 2 or 4
Size *uint32
}
func (ExprByteorder) exprName() exprName { return exprNameByteorder }
func (a *ExprByteorder) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.SReg != nil {
ae.Uint32(nftaByteorderSreg, uint32(*a.SReg))
}
if a.DReg != nil {
ae.Uint32(nftaByteorderDreg, uint32(*a.DReg))
}
if a.Op != nil {
ae.Uint32(nftaByteorderOp, uint32(*a.Op))
}
if a.Len != nil {
ae.Uint32(nftaByteorderLen, *a.Len)
}
if a.Size != nil {
ae.Uint32(nftaByteorderSize, *a.Size)
}
return ae.Encode()
}
func (a *ExprByteorder) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaByteorderSreg:
a.SReg = new(Reg(ad.Uint32()))
case nftaByteorderDreg:
a.DReg = new(Reg(ad.Uint32()))
case nftaByteorderOp:
a.Op = new(ByteorderOp(ad.Uint32()))
case nftaByteorderLen:
a.Len = new(ad.Uint32())
case nftaByteorderSize:
a.Size = new(ad.Uint32())
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L668
type CmpOp uint32
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L668
const (
CmpEq CmpOp = iota
CmpNeq
CmpLt
CmpLte
CmpGt
CmpGte
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L687
const (
nftaCmpSreg = unix.NFTA_CMP_SREG
nftaCmpOp = unix.NFTA_CMP_OP
nftaCmpData = unix.NFTA_CMP_DATA
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L687
type ExprCmp struct {
SReg *Reg
Op *CmpOp
Data *ExprData
}
func (ExprCmp) exprName() exprName { return exprNameCmp }
func (a *ExprCmp) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.SReg != nil {
ae.Uint32(nftaCmpSreg, uint32(*a.SReg))
}
if a.Op != nil {
ae.Uint32(nftaCmpOp, uint32(*a.Op))
}
if a.Data != nil {
b, err := a.Data.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaCmpData, b)
}
return ae.Encode()
}
func (a *ExprCmp) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaCmpSreg:
a.SReg = new(Reg(ad.Uint32()))
case nftaCmpOp:
a.Op = new(CmpOp(ad.Uint32()))
case nftaCmpData:
a.Data = &ExprData{}
if err := a.Data.unmarshal(ad.Bytes()); err != nil {
return err
}
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1222
type ConnlimitFlags uint32
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1222
const (
ConnlimitInv ConnlimitFlags = 1 << 0
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1227
const (
nftaConnlimitCount = 0x01
nftaConnlimitFlags = 0x02
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1227
type ExprConnlimit struct {
Count *uint32
Flags *ConnlimitFlags
}
func (ExprConnlimit) exprName() exprName { return exprNameConnlimit }
func (a *ExprConnlimit) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.Count != nil {
ae.Uint32(nftaConnlimitCount, *a.Count)
}
if a.Flags != nil {
ae.Uint32(nftaConnlimitFlags, uint32(*a.Flags))
}
return ae.Encode()
}
func (a *ExprConnlimit) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaConnlimitCount:
v := ad.Uint32()
a.Count = &v
case nftaConnlimitFlags:
v := ConnlimitFlags(ad.Uint32())
a.Flags = &v
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1266
const (
nftaCounterBytes = unix.NFTA_COUNTER_BYTES
nftaCounterPackets = unix.NFTA_COUNTER_PACKETS
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1266
type ExprCounter struct {
Bytes *uint64
Packets *uint64
}
func (ExprCounter) exprName() exprName { return exprNameCounter }
func (a *ExprCounter) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.Bytes != nil {
ae.Uint64(nftaCounterBytes, *a.Bytes)
}
if a.Packets != nil {
ae.Uint64(nftaCounterPackets, *a.Packets)
}
return ae.Encode()
}
func (a *ExprCounter) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaCounterBytes:
a.Bytes = new(ad.Uint64())
case nftaCounterPackets:
a.Packets = new(ad.Uint64())
}
}
return ad.Err()
}
// Pre-computed conntrack state bits for use with NFT_CT_STATE key.
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_conntrack_common.h#L7
type CTState uint32
const (
CTStateInvalid CTState = 1 << 0 // NF_CT_STATE_INVALID_BIT
CTStateEstablished CTState = 1 << 1 // NfCtStateBit(IP_CT_ESTABLISHED)
CTStateRelated CTState = 1 << 2 // NfCtStateBit(IP_CT_RELATED)
CTStateNew CTState = 1 << 3 // NfCtStateBit(IP_CT_NEW)
CTStateUntracked CTState = 1 << 6 // NF_CT_STATE_UNTRACKED_BIT
)
// Bytes encodes the CTState bitmask as native-endian bytes for use in ExprData.Value.
func (s CTState) Bytes() []byte {
b := make([]byte, 4)
binary.NativeEndian.PutUint32(b, uint32(s))
return b
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1132
type CTKey uint32
const (
CTKeyState CTKey = iota
CTKeyDirection
CTKeyStatus
CTKeyMark
CTKeySecmark
CTKeyExpiration
CTKeyHelper
CTKeyL3Protocol
CTKeySrc
CTKeyDst
CTKeyProtocol
CTKeyProtoSrc
CTKeyProtoDst
CTKeyLabels
CTKeyPkts
CTKeyBytes
CTKeyAvgPkt
CTKeyZone
CTKeyEventMask
CTKeySrcIP
CTKeyDstIP
CTKeySrcIP6
CTKeyDstIP6
CTKeyID
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1189
const (
nftaCtDreg = unix.NFTA_CT_DREG
nftaCtKey = unix.NFTA_CT_KEY
nftaCtDirection = unix.NFTA_CT_DIRECTION
nftaCtSreg = unix.NFTA_CT_SREG
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1189
type ExprCt struct {
Key *CTKey
DReg *Reg
SReg *Reg
Direction *uint8
}
func (ExprCt) exprName() exprName { return exprNameCt }
func (a *ExprCt) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.Key != nil {
ae.Uint32(nftaCtKey, uint32(*a.Key))
}
if a.DReg != nil {
ae.Uint32(nftaCtDreg, uint32(*a.DReg))
}
if a.SReg != nil {
ae.Uint32(nftaCtSreg, uint32(*a.SReg))
}
if a.Direction != nil {
ae.Uint8(nftaCtDirection, *a.Direction)
}
return ae.Encode()
}
func (a *ExprCt) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaCtKey:
a.Key = new(CTKey(ad.Uint32()))
case nftaCtDreg:
v := Reg(ad.Uint32())
a.DReg = &v
case nftaCtSreg:
v := Reg(ad.Uint32())
a.SReg = &v
case nftaCtDirection:
v := ad.Uint8()
a.Direction = &v
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L512
const (
nftaDataValue = unix.NFTA_DATA_VALUE
nftaDataVerdict = unix.NFTA_DATA_VERDICT
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L512
type ExprData struct {
Value []byte
Verdict *ExprVerdict
}
func (ExprData) exprName() exprName { return exprNameData }
func (a *ExprData) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if len(a.Value) > 0 {
ae.Bytes(nftaDataValue, a.Value)
}
if a.Verdict != nil {
b, err := a.Verdict.marshal()
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaDataVerdict, b)
}
return ae.Encode()
}
func (a *ExprData) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaDataValue:
a.Value = ad.Bytes()
case nftaDataVerdict:
a.Verdict = &ExprVerdict{}
if err := a.Verdict.unmarshal(ad.Bytes()); err != nil {
return err
}
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1533
const (
nftaDupSregAddr = unix.NFTA_DUP_SREG_ADDR
nftaDupSregDev = unix.NFTA_DUP_SREG_DEV
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L1533
type ExprDup struct {
SregAddr *uint32
SregDev *uint32
}
func (ExprDup) exprName() exprName { return exprNameDup }
func (a *ExprDup) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.SregAddr != nil {
ae.Uint32(nftaDupSregAddr, *a.SregAddr)
}
if a.SregDev != nil {
ae.Uint32(nftaDupSregDev, *a.SregDev)
}
return ae.Encode()
}
func (a *ExprDup) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaDupSregAddr:
v := ad.Uint32()
a.SregAddr = &v
case nftaDupSregDev:
v := ad.Uint32()
a.SregDev = &v
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L767
const (
nftaDynsetSetName = unix.NFTA_DYNSET_SET_NAME
nftaDynsetSetID = unix.NFTA_DYNSET_SET_ID
nftaDynsetOp = unix.NFTA_DYNSET_OP
nftaDynsetSregKey = unix.NFTA_DYNSET_SREG_KEY
nftaDynsetSregData = unix.NFTA_DYNSET_SREG_DATA
nftaDynsetTimeout = unix.NFTA_DYNSET_TIMEOUT
nftaDynsetExpr = unix.NFTA_DYNSET_EXPR
nftaDynsetFlags = unix.NFTA_DYNSET_FLAGS
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L755
type DynsetOp uint32
const (
DynsetAdd DynsetOp = iota
DynsetUpdate
DynsetDelete
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L761
type DynsetFlags uint32
const (
DynsetInv DynsetFlags = 1 << 0
DynsetExpr DynsetFlags = 1 << 1
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L767
type ExprDynset struct {
SetName *string
Op *DynsetOp
SregKey *uint32
SetID *uint32
SregData *uint32
Timeout *uint64
Flags *DynsetFlags
Expr Expr
}
func (ExprDynset) exprName() exprName { return exprNameDynset }
func (a *ExprDynset) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.SetName != nil {
ae.String(nftaDynsetSetName, *a.SetName)
}
if a.Op != nil {
ae.Uint32(nftaDynsetOp, uint32(*a.Op))
}
if a.SregKey != nil {
ae.Uint32(nftaDynsetSregKey, *a.SregKey)
}
if a.SetID != nil {
ae.Uint32(nftaDynsetSetID, *a.SetID)
}
if a.SregData != nil {
ae.Uint32(nftaDynsetSregData, *a.SregData)
}
if a.Timeout != nil {
ae.Uint64(nftaDynsetTimeout, *a.Timeout)
}
if a.Flags != nil {
ae.Uint32(nftaDynsetFlags, uint32(*a.Flags))
}
if a.Expr != nil {
b, err := marshalExpr(a.Expr)
if err != nil {
return nil, err
}
ae.Bytes(unix.NLA_F_NESTED|nftaDynsetExpr, b)
}
return ae.Encode()
}
func (a *ExprDynset) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaDynsetSetName:
a.SetName = new(ad.String())
case nftaDynsetOp:
a.Op = new(DynsetOp(ad.Uint32()))
case nftaDynsetSregKey:
a.SregKey = new(ad.Uint32())
case nftaDynsetSetID:
v := ad.Uint32()
a.SetID = &v
case nftaDynsetSregData:
v := ad.Uint32()
a.SregData = &v
case nftaDynsetTimeout:
v := ad.Uint64()
a.Timeout = &v
case nftaDynsetFlags:
v := DynsetFlags(ad.Uint32())
a.Flags = &v
case nftaDynsetExpr:
e, err := unmarshalExpr(ad.Bytes())
if err != nil {
return err
}
a.Expr = e
}
}
return ad.Err()
}
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L880
type ExthdrFlags uint32
const (
ExthdrPresent ExthdrFlags = 1 << 0
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L885
type ExthdrOp uint32
const (
ExthdrIPv6 ExthdrOp = iota
ExthdrTCPOpt
ExthdrIPv4
ExthdrSCTP
ExthdrDCCP
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L904
const (
nftaExthdrDreg = unix.NFTA_EXTHDR_DREG
nftaExthdrType = unix.NFTA_EXTHDR_TYPE
nftaExthdrOffset = unix.NFTA_EXTHDR_OFFSET
nftaExthdrLen = unix.NFTA_EXTHDR_LEN
nftaExthdrFlags = unix.NFTA_EXTHDR_FLAGS
nftaExthdrOp = unix.NFTA_EXTHDR_OP
nftaExthdrSreg = unix.NFTA_EXTHDR_SREG
)
// https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/netfilter/nf_tables.h#L904
type ExprExthdr struct {
Type *uint8
Offset *uint32
Len *uint32
DReg *Reg
Flags *ExthdrFlags
Op *ExthdrOp
SReg *Reg
}
func (ExprExthdr) exprName() exprName { return exprNameExthdr }
func (a *ExprExthdr) marshal() ([]byte, error) {
ae := newAttributeEncoder()
if a.Type != nil {
ae.Uint8(nftaExthdrType, *a.Type)
}
if a.Offset != nil {
ae.Uint32(nftaExthdrOffset, *a.Offset)
}
if a.Len != nil {
ae.Uint32(nftaExthdrLen, *a.Len)
}
if a.DReg != nil {
ae.Uint32(nftaExthdrDreg, uint32(*a.DReg))
}
if a.Flags != nil {
ae.Uint32(nftaExthdrFlags, uint32(*a.Flags))
}
if a.Op != nil {
ae.Uint32(nftaExthdrOp, uint32(*a.Op))
}
if a.SReg != nil {
ae.Uint32(nftaExthdrSreg, uint32(*a.SReg))
}
return ae.Encode()
}
func (a *ExprExthdr) unmarshal(data []byte) error {
ad, err := newAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case nftaExthdrType:
v := ad.Uint8()
a.Type = &v
case nftaExthdrOffset:
v := ad.Uint32()
a.Offset = &v
case nftaExthdrLen:
v := ad.Uint32()
a.Len = &v
case nftaExthdrDreg:
v := Reg(ad.Uint32())
a.DReg = &v
case nftaExthdrFlags:
v := ExthdrFlags(ad.Uint32())
a.Flags = &v
case nftaExthdrOp:
v := ExthdrOp(ad.Uint32())
a.Op = &v
case nftaExthdrSreg:
v := Reg(ad.Uint32())
a.SReg = &v
}
}