Compare commits
3 Commits
38a7441978
...
68a0b9c6ce
Author | SHA1 | Date |
---|---|---|
|
68a0b9c6ce | |
|
ed578af895 | |
|
dae73eaa9c |
|
@ -0,0 +1 @@
|
||||||
|
nftables.test
|
|
@ -21,4 +21,12 @@ the data types/API will be identified as more functionality is added.
|
||||||
|
|
||||||
Contributions are very welcome!
|
Contributions are very welcome!
|
||||||
|
|
||||||
|
### Testing Changes
|
||||||
|
|
||||||
|
Run the following commands to test your changes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go test ./...
|
||||||
|
go test -c github.com/google/nftables
|
||||||
|
sudo ./nftables.test -test.v -run_system_tests
|
||||||
|
```
|
||||||
|
|
43
conn.go
43
conn.go
|
@ -37,16 +37,20 @@ type Conn struct {
|
||||||
TestDial nltest.Func // for testing only; passed to nltest.Dial
|
TestDial nltest.Func // for testing only; passed to nltest.Dial
|
||||||
NetNS int // fd referencing the network namespace netlink will interact with.
|
NetNS int // fd referencing the network namespace netlink will interact with.
|
||||||
|
|
||||||
lasting bool // establish a lasting connection to be used across multiple netlink operations.
|
lasting bool // establish a lasting connection to be used across multiple netlink operations.
|
||||||
mu sync.Mutex // protects the following state
|
mu sync.Mutex // protects the following state
|
||||||
messages []netlink.Message
|
messages []netlink.Message
|
||||||
err error
|
err error
|
||||||
nlconn *netlink.Conn // netlink socket using NETLINK_NETFILTER protocol.
|
nlconn *netlink.Conn // netlink socket using NETLINK_NETFILTER protocol.
|
||||||
|
sockOptions []SockOption
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnOption is an option to change the behavior of the nftables Conn returned by Open.
|
// ConnOption is an option to change the behavior of the nftables Conn returned by Open.
|
||||||
type ConnOption func(*Conn)
|
type ConnOption func(*Conn)
|
||||||
|
|
||||||
|
// SockOption is an option to change the behavior of the netlink socket used by the nftables Conn.
|
||||||
|
type SockOption func(*netlink.Conn) error
|
||||||
|
|
||||||
// New returns a netlink connection for querying and modifying nftables. Some
|
// New returns a netlink connection for querying and modifying nftables. Some
|
||||||
// aspects of the new netlink connection can be configured using the options
|
// aspects of the new netlink connection can be configured using the options
|
||||||
// WithNetNSFd, WithTestDial, and AsLasting.
|
// WithNetNSFd, WithTestDial, and AsLasting.
|
||||||
|
@ -101,6 +105,14 @@ func WithTestDial(f nltest.Func) ConnOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithSockOptions sets the specified socket options when creating a new netlink
|
||||||
|
// connection.
|
||||||
|
func WithSockOptions(opts ...SockOption) ConnOption {
|
||||||
|
return func(cc *Conn) {
|
||||||
|
cc.sockOptions = append(cc.sockOptions, opts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// netlinkCloser is returned by netlinkConn(UnderLock) and must be called after
|
// netlinkCloser is returned by netlinkConn(UnderLock) and must be called after
|
||||||
// being done with the returned netlink connection in order to properly close
|
// being done with the returned netlink connection in order to properly close
|
||||||
// this connection, if necessary.
|
// this connection, if necessary.
|
||||||
|
@ -284,11 +296,28 @@ func (cc *Conn) FlushRuleset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *Conn) dialNetlink() (*netlink.Conn, error) {
|
func (cc *Conn) dialNetlink() (*netlink.Conn, error) {
|
||||||
|
var (
|
||||||
|
conn *netlink.Conn
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
if cc.TestDial != nil {
|
if cc.TestDial != nil {
|
||||||
return nltest.Dial(cc.TestDial), nil
|
conn = nltest.Dial(cc.TestDial)
|
||||||
|
} else {
|
||||||
|
conn, err = netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{NetNS: cc.NetNS})
|
||||||
}
|
}
|
||||||
|
|
||||||
return netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{NetNS: cc.NetNS})
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range cc.sockOptions {
|
||||||
|
if err := opt(conn); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *Conn) setErr(err error) {
|
func (cc *Conn) setErr(err error) {
|
||||||
|
|
|
@ -24,6 +24,15 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NFT_DROP = 0
|
||||||
|
NFT_ACCEPT = 1
|
||||||
|
NFT_STOLEN = 2
|
||||||
|
NFT_QUEUE = 3
|
||||||
|
NFT_REPEAT = 4
|
||||||
|
NFT_STOP = 5
|
||||||
|
)
|
||||||
|
|
||||||
// This code assembles the verdict structure, as expected by the
|
// This code assembles the verdict structure, as expected by the
|
||||||
// nftables netlink API.
|
// nftables netlink API.
|
||||||
// For further information, consult:
|
// For further information, consult:
|
||||||
|
@ -129,3 +138,37 @@ func (e *Verdict) unmarshal(fam byte, data []byte) error {
|
||||||
}
|
}
|
||||||
return ad.Err()
|
return ad.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Verdict) String() string {
|
||||||
|
var v string
|
||||||
|
switch e.Kind {
|
||||||
|
case unix.NFT_RETURN:
|
||||||
|
v = "return" // -0x5
|
||||||
|
case unix.NFT_GOTO:
|
||||||
|
v = "goto" // -0x4
|
||||||
|
case unix.NFT_JUMP:
|
||||||
|
v = "jump" // NFT_JUMP = -0x3
|
||||||
|
case unix.NFT_BREAK:
|
||||||
|
v = "break" // NFT_BREAK = -0x2
|
||||||
|
case unix.NFT_CONTINUE:
|
||||||
|
v = "continue" // NFT_CONTINUE = -0x1
|
||||||
|
case NFT_DROP:
|
||||||
|
v = "drop"
|
||||||
|
case NFT_ACCEPT:
|
||||||
|
v = "accept"
|
||||||
|
case NFT_STOLEN:
|
||||||
|
v = "stolen"
|
||||||
|
case NFT_QUEUE:
|
||||||
|
v = "queue"
|
||||||
|
case NFT_REPEAT:
|
||||||
|
v = "repeat"
|
||||||
|
case NFT_STOP:
|
||||||
|
v = "stop"
|
||||||
|
default:
|
||||||
|
v = fmt.Sprintf("verdict %v", e.Kind)
|
||||||
|
}
|
||||||
|
if e.Chain != "" {
|
||||||
|
return v + " " + e.Chain
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
|
@ -221,12 +221,27 @@ func TestRuleOperations(t *testing.T) {
|
||||||
expr.VerdictDrop,
|
expr.VerdictDrop,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wantStrings := []string{
|
||||||
|
"queue",
|
||||||
|
"accept",
|
||||||
|
"queue",
|
||||||
|
"accept",
|
||||||
|
"drop",
|
||||||
|
"drop",
|
||||||
|
}
|
||||||
|
|
||||||
for i, r := range rules {
|
for i, r := range rules {
|
||||||
rr, _ := r.Exprs[0].(*expr.Verdict)
|
rr, _ := r.Exprs[0].(*expr.Verdict)
|
||||||
|
|
||||||
if rr.Kind != want[i] {
|
if rr.Kind != want[i] {
|
||||||
t.Fatalf("bad verdict kind at %d", i)
|
t.Fatalf("bad verdict kind at %d", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rr.String() != wantStrings[i] {
|
||||||
|
t.Fatalf("bad verdict string at %d: %s (received) vs. %s (expected)", i, rr.String(), wantStrings[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("%s", rr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
go test ./...
|
||||||
|
go test -c github.com/google/nftables
|
||||||
|
sudo ./nftables.test -test.v -run_system_tests
|
Loading…
Reference in New Issue