2019-05-03 16:54:09 -05:00
|
|
|
// Copyright 2018 Google LLC. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package nftables
|
|
|
|
|
|
|
|
import (
|
2019-06-19 06:14:23 -05:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2019-05-03 16:54:09 -05:00
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/google/nftables/binaryutil"
|
|
|
|
"github.com/mdlayher/netlink"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainHook specifies at which step in packet processing the Chain should be
|
|
|
|
// executed. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_hooks
|
|
|
|
type ChainHook uint32
|
|
|
|
|
|
|
|
// Possible ChainHook values.
|
|
|
|
const (
|
|
|
|
ChainHookPrerouting ChainHook = unix.NF_INET_PRE_ROUTING
|
|
|
|
ChainHookInput ChainHook = unix.NF_INET_LOCAL_IN
|
|
|
|
ChainHookForward ChainHook = unix.NF_INET_FORWARD
|
|
|
|
ChainHookOutput ChainHook = unix.NF_INET_LOCAL_OUT
|
|
|
|
ChainHookPostrouting ChainHook = unix.NF_INET_POST_ROUTING
|
|
|
|
ChainHookIngress ChainHook = unix.NF_NETDEV_INGRESS
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainPriority orders the chain relative to Netfilter internal operations. See
|
|
|
|
// also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_priority
|
|
|
|
type ChainPriority int32
|
|
|
|
|
|
|
|
// Possible ChainPriority values.
|
|
|
|
const ( // from /usr/include/linux/netfilter_ipv4.h
|
|
|
|
ChainPriorityFirst ChainPriority = math.MinInt32
|
|
|
|
ChainPriorityConntrackDefrag ChainPriority = -400
|
|
|
|
ChainPriorityRaw ChainPriority = -300
|
|
|
|
ChainPrioritySELinuxFirst ChainPriority = -225
|
|
|
|
ChainPriorityConntrack ChainPriority = -200
|
|
|
|
ChainPriorityMangle ChainPriority = -150
|
|
|
|
ChainPriorityNATDest ChainPriority = -100
|
|
|
|
ChainPriorityFilter ChainPriority = 0
|
|
|
|
ChainPrioritySecurity ChainPriority = 50
|
|
|
|
ChainPriorityNATSource ChainPriority = 100
|
|
|
|
ChainPrioritySELinuxLast ChainPriority = 225
|
|
|
|
ChainPriorityConntrackHelper ChainPriority = 300
|
|
|
|
ChainPriorityConntrackConfirm ChainPriority = math.MaxInt32
|
|
|
|
ChainPriorityLast ChainPriority = math.MaxInt32
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainType defines what this chain will be used for. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_types
|
|
|
|
type ChainType string
|
|
|
|
|
|
|
|
// Possible ChainType values.
|
|
|
|
const (
|
|
|
|
ChainTypeFilter ChainType = "filter"
|
|
|
|
ChainTypeRoute ChainType = "route"
|
|
|
|
ChainTypeNAT ChainType = "nat"
|
|
|
|
)
|
|
|
|
|
2019-10-16 04:43:00 -05:00
|
|
|
// ChainPolicy defines what this chain default policy will be.
|
|
|
|
type ChainPolicy uint32
|
|
|
|
|
|
|
|
// Possible ChainPolicy values.
|
|
|
|
const (
|
|
|
|
ChainPolicyDrop ChainPolicy = iota
|
|
|
|
ChainPolicyAccept
|
|
|
|
)
|
|
|
|
|
2019-05-03 16:54:09 -05:00
|
|
|
// A Chain contains Rules. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains
|
|
|
|
type Chain struct {
|
|
|
|
Name string
|
|
|
|
Table *Table
|
|
|
|
Hooknum ChainHook
|
|
|
|
Priority ChainPriority
|
|
|
|
Type ChainType
|
2019-10-16 04:43:47 -05:00
|
|
|
Policy *ChainPolicy
|
2019-05-03 16:54:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddChain adds the specified Chain. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Adding_base_chains
|
|
|
|
func (cc *Conn) AddChain(c *Chain) *Chain {
|
2022-05-09 06:25:29 -05:00
|
|
|
cc.mu.Lock()
|
|
|
|
defer cc.mu.Unlock()
|
2019-05-03 16:54:09 -05:00
|
|
|
data := cc.marshalAttr([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_CHAIN_TABLE, Data: []byte(c.Table.Name + "\x00")},
|
|
|
|
{Type: unix.NFTA_CHAIN_NAME, Data: []byte(c.Name + "\x00")},
|
|
|
|
})
|
|
|
|
|
2019-06-24 11:00:28 -05:00
|
|
|
if c.Type != "" {
|
2019-07-20 11:32:43 -05:00
|
|
|
hookAttr := []netlink.Attribute{
|
2019-06-24 11:00:28 -05:00
|
|
|
{Type: unix.NFTA_HOOK_HOOKNUM, Data: binaryutil.BigEndian.PutUint32(uint32(c.Hooknum))},
|
|
|
|
{Type: unix.NFTA_HOOK_PRIORITY, Data: binaryutil.BigEndian.PutUint32(uint32(c.Priority))},
|
2019-07-20 11:32:43 -05:00
|
|
|
}
|
|
|
|
data = append(data, cc.marshalAttr([]netlink.Attribute{
|
|
|
|
{Type: unix.NLA_F_NESTED | unix.NFTA_CHAIN_HOOK, Data: cc.marshalAttr(hookAttr)},
|
|
|
|
})...)
|
|
|
|
}
|
|
|
|
|
2019-10-16 04:43:47 -05:00
|
|
|
if c.Policy != nil {
|
2019-07-20 11:32:43 -05:00
|
|
|
data = append(data, cc.marshalAttr([]netlink.Attribute{
|
2019-10-16 04:43:47 -05:00
|
|
|
{Type: unix.NFTA_CHAIN_POLICY, Data: binaryutil.BigEndian.PutUint32(uint32(*c.Policy))},
|
2019-07-20 11:32:43 -05:00
|
|
|
})...)
|
|
|
|
}
|
|
|
|
if c.Type != "" {
|
2019-06-24 11:00:28 -05:00
|
|
|
data = append(data, cc.marshalAttr([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_CHAIN_TYPE, Data: []byte(c.Type + "\x00")},
|
|
|
|
})...)
|
|
|
|
}
|
2019-05-03 16:54:09 -05:00
|
|
|
cc.messages = append(cc.messages, netlink.Message{
|
|
|
|
Header: netlink.Header{
|
|
|
|
Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWCHAIN),
|
|
|
|
Flags: netlink.Request | netlink.Acknowledge | netlink.Create,
|
|
|
|
},
|
|
|
|
Data: append(extraHeader(uint8(c.Table.Family), 0), data...),
|
|
|
|
})
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
2019-06-19 06:14:23 -05:00
|
|
|
|
2019-07-11 07:30:38 -05:00
|
|
|
// DelChain deletes the specified Chain. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Deleting_chains
|
|
|
|
func (cc *Conn) DelChain(c *Chain) {
|
2022-05-09 06:25:29 -05:00
|
|
|
cc.mu.Lock()
|
|
|
|
defer cc.mu.Unlock()
|
2019-07-11 07:30:38 -05:00
|
|
|
data := cc.marshalAttr([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_CHAIN_TABLE, Data: []byte(c.Table.Name + "\x00")},
|
|
|
|
{Type: unix.NFTA_CHAIN_NAME, Data: []byte(c.Name + "\x00")},
|
|
|
|
})
|
|
|
|
|
|
|
|
cc.messages = append(cc.messages, netlink.Message{
|
|
|
|
Header: netlink.Header{
|
|
|
|
Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELCHAIN),
|
|
|
|
Flags: netlink.Request | netlink.Acknowledge,
|
|
|
|
},
|
|
|
|
Data: append(extraHeader(uint8(c.Table.Family), 0), data...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:18:45 -05:00
|
|
|
// FlushChain removes all rules within the specified Chain. See also
|
|
|
|
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Flushing_chain
|
|
|
|
func (cc *Conn) FlushChain(c *Chain) {
|
2022-05-09 06:25:29 -05:00
|
|
|
cc.mu.Lock()
|
|
|
|
defer cc.mu.Unlock()
|
2019-10-23 07:18:45 -05:00
|
|
|
data := cc.marshalAttr([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_RULE_TABLE, Data: []byte(c.Table.Name + "\x00")},
|
|
|
|
{Type: unix.NFTA_RULE_CHAIN, Data: []byte(c.Name + "\x00")},
|
|
|
|
})
|
|
|
|
cc.messages = append(cc.messages, netlink.Message{
|
|
|
|
Header: netlink.Header{
|
|
|
|
Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELRULE),
|
|
|
|
Flags: netlink.Request | netlink.Acknowledge,
|
|
|
|
},
|
|
|
|
Data: append(extraHeader(uint8(c.Table.Family), 0), data...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:14:23 -05:00
|
|
|
// ListChains returns currently configured chains in the kernel
|
|
|
|
func (cc *Conn) ListChains() ([]*Chain, error) {
|
2022-05-09 06:25:29 -05:00
|
|
|
conn, closer, err := cc.netlinkConn()
|
2019-06-19 06:14:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-09 06:25:29 -05:00
|
|
|
defer func() { _ = closer() }()
|
2019-06-19 06:14:23 -05:00
|
|
|
|
|
|
|
msg := netlink.Message{
|
|
|
|
Header: netlink.Header{
|
|
|
|
Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_GETCHAIN),
|
|
|
|
Flags: netlink.Request | netlink.Dump,
|
|
|
|
},
|
2022-05-15 16:16:05 -05:00
|
|
|
Data: extraHeader(uint8(TableFamilyUnspecified), 0),
|
2019-06-19 06:14:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := conn.Execute(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chains []*Chain
|
|
|
|
for _, m := range response {
|
|
|
|
c, err := chainFromMsg(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chains = append(chains, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chains, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func chainFromMsg(msg netlink.Message) (*Chain, error) {
|
|
|
|
chainHeaderType := netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWCHAIN)
|
|
|
|
if got, want := msg.Header.Type, chainHeaderType; got != want {
|
|
|
|
return nil, fmt.Errorf("unexpected header type: got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Chain
|
|
|
|
|
|
|
|
ad, err := netlink.NewAttributeDecoder(msg.Data[4:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for ad.Next() {
|
|
|
|
switch ad.Type() {
|
|
|
|
case unix.NFTA_CHAIN_NAME:
|
|
|
|
c.Name = ad.String()
|
|
|
|
case unix.NFTA_TABLE_NAME:
|
|
|
|
c.Table = &Table{Name: ad.String()}
|
2019-08-12 01:51:06 -05:00
|
|
|
// msg[0] carries TableFamily byte indicating whether it is IPv4, IPv6 or something else
|
|
|
|
c.Table.Family = TableFamily(msg.Data[0])
|
2019-06-19 06:14:23 -05:00
|
|
|
case unix.NFTA_CHAIN_TYPE:
|
|
|
|
c.Type = ChainType(ad.String())
|
2019-07-20 11:32:43 -05:00
|
|
|
case unix.NFTA_CHAIN_POLICY:
|
2022-02-06 11:44:06 -06:00
|
|
|
policy := ChainPolicy(binaryutil.BigEndian.Uint32(ad.Bytes()))
|
2019-10-16 04:43:47 -05:00
|
|
|
c.Policy = &policy
|
2019-06-19 06:14:23 -05:00
|
|
|
case unix.NFTA_CHAIN_HOOK:
|
|
|
|
ad.Do(func(b []byte) error {
|
|
|
|
c.Hooknum, c.Priority, err = hookFromMsg(b)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hookFromMsg(b []byte) (ChainHook, ChainPriority, error) {
|
|
|
|
ad, err := netlink.NewAttributeDecoder(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ad.ByteOrder = binary.BigEndian
|
|
|
|
|
|
|
|
var hooknum ChainHook
|
|
|
|
var prio ChainPriority
|
|
|
|
|
|
|
|
for ad.Next() {
|
|
|
|
switch ad.Type() {
|
|
|
|
case unix.NFTA_HOOK_HOOKNUM:
|
|
|
|
hooknum = ChainHook(ad.Uint32())
|
|
|
|
case unix.NFTA_HOOK_PRIORITY:
|
|
|
|
prio = ChainPriority(ad.Uint32())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hooknum, prio, nil
|
|
|
|
}
|