-
Notifications
You must be signed in to change notification settings - Fork 48
/
m_ct.go
149 lines (141 loc) · 4.09 KB
/
m_ct.go
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
package tc
import (
"fmt"
"net"
"github.com/mdlayher/netlink"
)
const (
tcaCtUnspec = iota
tcaCtParms
tcaCtTm
tcaCtAction /* u16 */
tcaCtZone /* u16 */
tcaCtMark /* u32 */
tcaCtMarkMask /* u32 */
tcaCtLabels /* u128 */
tcaCtLabelsMask /* u128 */
tcaCtNatIPv4Min /* be32 */
tcaCtNatIPv4Max /* be32 */
tcaCtNatIPv6Min /* struct in6_addr */
tcaCtNatIPv6Max /* struct in6_addr */
tcaCtNatPortMin /* be16 */
tcaCtNatPortMax /* be16 */
tcaCtPad
)
// Ct contains attributes of the ct discipline
type Ct struct {
Parms *CtParms
Tm *Tcft
Action *uint16
Zone *uint16
Mark *uint32
MarkMask *uint32
NatIPv4Min *net.IP
NatIPv4Max *net.IP
NatPortMin *uint16
NatPortMax *uint16
}
// CtParms contains further ct attributes.
type CtParms struct {
Index uint32
Capab uint32
Action uint32
RefCnt uint32
BindCnt uint32
}
// unmarshalCt parses the ct-encoded data and stores the result in the value pointed to by info.
func unmarshalCt(data []byte, info *Ct) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var multiError error
for ad.Next() {
switch ad.Type() {
case tcaCtParms:
parms := &CtParms{}
err = unmarshalStruct(ad.Bytes(), parms)
multiError = concatError(multiError, err)
info.Parms = parms
case tcaCtTm:
tcft := &Tcft{}
err = unmarshalStruct(ad.Bytes(), tcft)
multiError = concatError(multiError, err)
info.Tm = tcft
case tcaCtAction:
info.Action = uint16Ptr(ad.Uint16())
case tcaCtZone:
info.Zone = uint16Ptr(ad.Uint16())
case tcaCtMark:
info.Mark = uint32Ptr(ad.Uint32())
case tcaCtMarkMask:
info.MarkMask = uint32Ptr(ad.Uint32())
case tcaCtNatIPv4Min:
tmp := uint32ToIP(ad.Uint32())
info.NatIPv4Min = &tmp
case tcaCtNatIPv4Max:
tmp := uint32ToIP(ad.Uint32())
info.NatIPv4Max = &tmp
case tcaCtNatPortMin:
tmp := endianSwapUint16(ad.Uint16())
info.NatPortMin = &tmp
case tcaCtNatPortMax:
tmp := endianSwapUint16(ad.Uint16())
info.NatPortMax = &tmp
case tcaCtPad:
// padding does not contain data, we just skip it
default:
return fmt.Errorf("UnmarshalCt()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return concatError(multiError, ad.Err())
}
// marshalCt returns the binary encoding of Ct
func marshalCt(info *Ct) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Ct: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.Tm != nil {
return []byte{}, ErrNoArgAlter
}
var multiError error
if info.Parms != nil {
data, err := marshalStruct(info.Parms)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaCtParms, Data: data})
}
if info.Action != nil {
options = append(options, tcOption{Interpretation: vtUint16, Type: tcaCtAction, Data: uint16Value(info.Action)})
}
if info.Zone != nil {
options = append(options, tcOption{Interpretation: vtUint16, Type: tcaCtZone, Data: uint16Value(info.Zone)})
}
if info.Mark != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCtMark, Data: uint32Value(info.Mark)})
}
if info.MarkMask != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCtMarkMask, Data: uint32Value(info.MarkMask)})
}
if info.NatIPv4Min != nil {
tmp, err := ipToUint32(*info.NatIPv4Min)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCtNatIPv4Min, Data: tmp})
}
if info.NatIPv4Max != nil {
tmp, err := ipToUint32(*info.NatIPv4Max)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCtNatIPv4Max, Data: tmp})
}
if info.NatPortMin != nil {
options = append(options, tcOption{Interpretation: vtUint16Be, Type: tcaCtNatPortMin, Data: uint16Value(info.NatPortMin)})
}
if info.NatPortMax != nil {
options = append(options, tcOption{Interpretation: vtUint16Be, Type: tcaCtNatPortMax, Data: uint16Value(info.NatPortMax)})
}
if multiError != nil {
return []byte{}, multiError
}
return marshalAttributes(options)
}