Compare commits
5 Commits
ca25212b5a
...
93929b0b6d
Author | SHA1 | Date |
---|---|---|
|
93929b0b6d | |
|
f8c01f0bf3 | |
|
0a7196fb65 | |
|
8a8ad2be81 | |
|
68e1406c13 |
28
conn.go
28
conn.go
|
@ -250,10 +250,10 @@ func (cc *Conn) Flush() error {
|
||||||
}
|
}
|
||||||
defer func() { _ = closer() }()
|
defer func() { _ = closer() }()
|
||||||
|
|
||||||
if err = cc.enlargeWriteBuffer(conn); err != nil {
|
if err := cc.enlargeWriteBuffer(conn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = cc.enlargeReadBuffer(conn); err != nil {
|
if err := cc.enlargeReadBuffer(conn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,6 +440,22 @@ func (cc *Conn) getMessageSize() int {
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// canEnlargeBuffers returns true if the connection can automatically enlarge
|
||||||
|
// the write and read buffers of the netlink connection.
|
||||||
|
func (cc *Conn) canEnlargeBuffers() bool {
|
||||||
|
// If there are sock options, we assume that the user has already set the
|
||||||
|
// buffers to a fixed size.
|
||||||
|
if len(cc.sockOptions) > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if cc.TestDial != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// enlargeWriteBuffer automatically sets the write buffer of the given
|
// enlargeWriteBuffer automatically sets the write buffer of the given
|
||||||
// connection to the accumulated message size. This is only done if the current
|
// connection to the accumulated message size. This is only done if the current
|
||||||
// write buffer is smaller than the message size.
|
// write buffer is smaller than the message size.
|
||||||
|
@ -453,6 +469,10 @@ func (cc *Conn) getMessageSize() int {
|
||||||
// TODO: Update this function to mimic the behavior of nftables once our
|
// TODO: Update this function to mimic the behavior of nftables once our
|
||||||
// socket library supports multiple iovec entries.
|
// socket library supports multiple iovec entries.
|
||||||
func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error {
|
func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error {
|
||||||
|
if !cc.canEnlargeBuffers() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
messageSize := cc.getMessageSize()
|
messageSize := cc.getMessageSize()
|
||||||
writeBuffer, err := conn.WriteBuffer()
|
writeBuffer, err := conn.WriteBuffer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -481,6 +501,10 @@ func (cc *Conn) getDefaultEchoReadBuffer() int {
|
||||||
//
|
//
|
||||||
// See https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n426
|
// See https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n426
|
||||||
func (cc *Conn) enlargeReadBuffer(conn *netlink.Conn) error {
|
func (cc *Conn) enlargeReadBuffer(conn *netlink.Conn) error {
|
||||||
|
if !cc.canEnlargeBuffers() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var bufferSize int
|
var bufferSize int
|
||||||
|
|
||||||
// If there are any messages with the Echo flag, we initialize the buffer size
|
// If there are any messages with the Echo flag, we initialize the buffer size
|
||||||
|
|
26
expr/ct.go
26
expr/ct.go
|
@ -110,6 +110,12 @@ const (
|
||||||
CtStateUDPREPLIED
|
CtStateUDPREPLIED
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// https://git.netfilter.org/libnftnl/tree/src/expr/ct.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n31
|
||||||
|
CtDirOriginal = iota
|
||||||
|
CtDirReply
|
||||||
|
)
|
||||||
|
|
||||||
// https://git.netfilter.org/libnftnl/tree/src/obj/ct_timeout.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n57
|
// https://git.netfilter.org/libnftnl/tree/src/obj/ct_timeout.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n57
|
||||||
var CtStateUDPTimeoutDefaults CtStatePolicyTimeout = map[uint16]uint32{
|
var CtStateUDPTimeoutDefaults CtStatePolicyTimeout = map[uint16]uint32{
|
||||||
CtStateUDPUNREPLIED: 30,
|
CtStateUDPUNREPLIED: 30,
|
||||||
|
@ -122,6 +128,7 @@ type Ct struct {
|
||||||
SourceRegister bool
|
SourceRegister bool
|
||||||
Key CtKey
|
Key CtKey
|
||||||
Direction uint32
|
Direction uint32
|
||||||
|
OptDirection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Ct) marshal(fam byte) ([]byte, error) {
|
func (e *Ct) marshal(fam byte) ([]byte, error) {
|
||||||
|
@ -165,10 +172,16 @@ func (e *Ct) marshalData(fam byte) ([]byte, error) {
|
||||||
exprData = append(exprData, regData...)
|
exprData = append(exprData, regData...)
|
||||||
|
|
||||||
switch e.Key {
|
switch e.Key {
|
||||||
|
case CtKeyPKTS, CtKeyBYTES, CtKeyAVGPKT, CtKeyL3PROTOCOL, CtKeyPROTOCOL:
|
||||||
|
if !e.OptDirection {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fallthrough
|
||||||
case CtKeySRC, CtKeyDST, CtKeyPROTOSRC, CtKeyPROTODST, CtKeySRCIP, CtKeyDSTIP, CtKeySRCIP6, CtKeyDSTIP6:
|
case CtKeySRC, CtKeyDST, CtKeyPROTOSRC, CtKeyPROTODST, CtKeySRCIP, CtKeyDSTIP, CtKeySRCIP6, CtKeyDSTIP6:
|
||||||
regData, err = netlink.MarshalAttributes(
|
regData, err = netlink.MarshalAttributes(
|
||||||
[]netlink.Attribute{
|
[]netlink.Attribute{
|
||||||
{Type: unix.NFTA_CT_DIRECTION, Data: binaryutil.BigEndian.PutUint32(e.Direction)},
|
{Type: unix.NFTA_CT_DIRECTION, Data: []byte{uint8(e.Direction)}},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -186,6 +199,8 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ad.ByteOrder = binary.BigEndian
|
ad.ByteOrder = binary.BigEndian
|
||||||
|
|
||||||
|
var hasDirection bool
|
||||||
for ad.Next() {
|
for ad.Next() {
|
||||||
switch ad.Type() {
|
switch ad.Type() {
|
||||||
case unix.NFTA_CT_KEY:
|
case unix.NFTA_CT_KEY:
|
||||||
|
@ -193,12 +208,19 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
|
||||||
case unix.NFTA_CT_DREG:
|
case unix.NFTA_CT_DREG:
|
||||||
e.Register = ad.Uint32()
|
e.Register = ad.Uint32()
|
||||||
case unix.NFTA_CT_DIRECTION:
|
case unix.NFTA_CT_DIRECTION:
|
||||||
e.Direction = ad.Uint32()
|
e.Direction = uint32(ad.Uint8())
|
||||||
|
hasDirection = true
|
||||||
case unix.NFTA_CT_SREG:
|
case unix.NFTA_CT_SREG:
|
||||||
e.SourceRegister = true
|
e.SourceRegister = true
|
||||||
e.Register = ad.Uint32()
|
e.Register = ad.Uint32()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch e.Key {
|
||||||
|
case CtKeyPKTS, CtKeyBYTES, CtKeyAVGPKT, CtKeyL3PROTOCOL, CtKeyPROTOCOL:
|
||||||
|
e.OptDirection = hasDirection
|
||||||
|
}
|
||||||
|
|
||||||
return ad.Err()
|
return ad.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,22 @@ func TestCt(t *testing.T) {
|
||||||
Direction: 1,
|
Direction: 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Unmarshal Ct packets direction original case",
|
||||||
|
ct: Ct{
|
||||||
|
Register: 1,
|
||||||
|
Key: CtKeyPKTS,
|
||||||
|
Direction: CtDirOriginal,
|
||||||
|
OptDirection: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unmarshal Ct bytes without direction case",
|
||||||
|
ct: Ct{
|
||||||
|
Register: 1,
|
||||||
|
Key: CtKeyBYTES,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -12,6 +12,6 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mdlayher/socket v0.5.0 // indirect
|
github.com/mdlayher/socket v0.5.0 // indirect
|
||||||
golang.org/x/net v0.37.0 // indirect
|
golang.org/x/net v0.38.0 // indirect
|
||||||
golang.org/x/sync v0.6.0 // indirect
|
golang.org/x/sync v0.6.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -8,8 +8,8 @@ github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQ
|
||||||
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
|
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
|
||||||
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
||||||
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
||||||
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|
Loading…
Reference in New Issue