|
| 1 | +// Copyright 2018 Google LLC. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package nftables |
| 16 | + |
| 17 | +import ( |
| 18 | + "math" |
| 19 | + |
| 20 | + "github.com/google/nftables/binaryutil" |
| 21 | + "github.com/mdlayher/netlink" |
| 22 | + "golang.org/x/sys/unix" |
| 23 | +) |
| 24 | + |
| 25 | +// ChainHook specifies at which step in packet processing the Chain should be |
| 26 | +// executed. See also |
| 27 | +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_hooks |
| 28 | +type ChainHook uint32 |
| 29 | + |
| 30 | +// Possible ChainHook values. |
| 31 | +const ( |
| 32 | + ChainHookPrerouting ChainHook = unix.NF_INET_PRE_ROUTING |
| 33 | + ChainHookInput ChainHook = unix.NF_INET_LOCAL_IN |
| 34 | + ChainHookForward ChainHook = unix.NF_INET_FORWARD |
| 35 | + ChainHookOutput ChainHook = unix.NF_INET_LOCAL_OUT |
| 36 | + ChainHookPostrouting ChainHook = unix.NF_INET_POST_ROUTING |
| 37 | + ChainHookIngress ChainHook = unix.NF_NETDEV_INGRESS |
| 38 | +) |
| 39 | + |
| 40 | +// ChainPriority orders the chain relative to Netfilter internal operations. See |
| 41 | +// also |
| 42 | +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_priority |
| 43 | +type ChainPriority int32 |
| 44 | + |
| 45 | +// Possible ChainPriority values. |
| 46 | +const ( // from /usr/include/linux/netfilter_ipv4.h |
| 47 | + ChainPriorityFirst ChainPriority = math.MinInt32 |
| 48 | + ChainPriorityConntrackDefrag ChainPriority = -400 |
| 49 | + ChainPriorityRaw ChainPriority = -300 |
| 50 | + ChainPrioritySELinuxFirst ChainPriority = -225 |
| 51 | + ChainPriorityConntrack ChainPriority = -200 |
| 52 | + ChainPriorityMangle ChainPriority = -150 |
| 53 | + ChainPriorityNATDest ChainPriority = -100 |
| 54 | + ChainPriorityFilter ChainPriority = 0 |
| 55 | + ChainPrioritySecurity ChainPriority = 50 |
| 56 | + ChainPriorityNATSource ChainPriority = 100 |
| 57 | + ChainPrioritySELinuxLast ChainPriority = 225 |
| 58 | + ChainPriorityConntrackHelper ChainPriority = 300 |
| 59 | + ChainPriorityConntrackConfirm ChainPriority = math.MaxInt32 |
| 60 | + ChainPriorityLast ChainPriority = math.MaxInt32 |
| 61 | +) |
| 62 | + |
| 63 | +// ChainType defines what this chain will be used for. See also |
| 64 | +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_types |
| 65 | +type ChainType string |
| 66 | + |
| 67 | +// Possible ChainType values. |
| 68 | +const ( |
| 69 | + ChainTypeFilter ChainType = "filter" |
| 70 | + ChainTypeRoute ChainType = "route" |
| 71 | + ChainTypeNAT ChainType = "nat" |
| 72 | +) |
| 73 | + |
| 74 | +// A Chain contains Rules. See also |
| 75 | +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains |
| 76 | +type Chain struct { |
| 77 | + Name string |
| 78 | + Table *Table |
| 79 | + Hooknum ChainHook |
| 80 | + Priority ChainPriority |
| 81 | + Type ChainType |
| 82 | +} |
| 83 | + |
| 84 | +// AddChain adds the specified Chain. See also |
| 85 | +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Adding_base_chains |
| 86 | +func (cc *Conn) AddChain(c *Chain) *Chain { |
| 87 | + chainHook := cc.marshalAttr([]netlink.Attribute{ |
| 88 | + {Type: unix.NFTA_HOOK_HOOKNUM, Data: binaryutil.BigEndian.PutUint32(uint32(c.Hooknum))}, |
| 89 | + {Type: unix.NFTA_HOOK_PRIORITY, Data: binaryutil.BigEndian.PutUint32(uint32(c.Priority))}, |
| 90 | + }) |
| 91 | + |
| 92 | + data := cc.marshalAttr([]netlink.Attribute{ |
| 93 | + {Type: unix.NFTA_CHAIN_TABLE, Data: []byte(c.Table.Name + "\x00")}, |
| 94 | + {Type: unix.NFTA_CHAIN_NAME, Data: []byte(c.Name + "\x00")}, |
| 95 | + {Type: unix.NLA_F_NESTED | unix.NFTA_CHAIN_HOOK, Data: chainHook}, |
| 96 | + {Type: unix.NFTA_CHAIN_TYPE, Data: []byte(c.Type + "\x00")}, |
| 97 | + }) |
| 98 | + |
| 99 | + cc.messages = append(cc.messages, netlink.Message{ |
| 100 | + Header: netlink.Header{ |
| 101 | + Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWCHAIN), |
| 102 | + Flags: netlink.Request | netlink.Acknowledge | netlink.Create, |
| 103 | + }, |
| 104 | + Data: append(extraHeader(uint8(c.Table.Family), 0), data...), |
| 105 | + }) |
| 106 | + |
| 107 | + return c |
| 108 | +} |
0 commit comments