From afbe6ed893d176449112c7c5dc0f2915cdd986d4 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 24 May 2018 21:47:16 -0700 Subject: [PATCH] Initial commit --- CONTRIBUTING.md | 23 +++ LICENSE | 202 ++++++++++++++++++++++ expr/expr.go | 141 +++++++++++++++ internal/binaryutil/binaryutil.go | 65 +++++++ nativeendian.go | 21 +++ nftables.go | 278 ++++++++++++++++++++++++++++++ nftables_test.go | 115 ++++++++++++ 7 files changed, 845 insertions(+) create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 expr/expr.go create mode 100644 internal/binaryutil/binaryutil.go create mode 100644 nativeendian.go create mode 100644 nftables.go create mode 100644 nftables_test.go diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ae319c7 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,23 @@ +# How to Contribute + +We'd love to accept your patches and contributions to this project. There are +just a few small guidelines you need to follow. + +## Contributor License Agreement + +Contributions to this project must be accompanied by a Contributor License +Agreement. You (or your employer) retain the copyright to your contribution, +this simply gives us permission to use and redistribute your contributions as +part of the project. Head over to to see +your current agreements on file or to sign a new one. + +You generally only need to submit a CLA once, so if you've already submitted one +(even if it was for a different project), you probably don't need to do it +again. + +## Code reviews + +All submissions, including submissions by project members, require review. We +use GitHub pull requests for this purpose. Consult +[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more +information on using pull requests. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/expr/expr.go b/expr/expr.go new file mode 100644 index 0000000..e161e14 --- /dev/null +++ b/expr/expr.go @@ -0,0 +1,141 @@ +// 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 expr provides nftables rule expressions. +package expr + +import ( + "github.com/google/nftables/internal/binaryutil" + "github.com/mdlayher/netlink" + "golang.org/x/sys/unix" +) + +// Marshal serializes the specified expression into a byte slice. +func Marshal(e Any) ([]byte, error) { + return e.marshal() +} + +// Any is an interface implemented by any expression type. +type Any interface { + marshal() ([]byte, error) +} + +// MetaKey specifies which piece of meta information should be loaded. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Matching_packet_metainformation +type MetaKey uint32 + +// Possible MetaKey values. +const ( + MetaKeyLEN MetaKey = unix.NFT_META_LEN + MetaKeyPROTOCOL MetaKey = unix.NFT_META_PROTOCOL + MetaKeyPRIORITY MetaKey = unix.NFT_META_PRIORITY + MetaKeyMARK MetaKey = unix.NFT_META_MARK + MetaKeyIIF MetaKey = unix.NFT_META_IIF + MetaKeyOIF MetaKey = unix.NFT_META_OIF + MetaKeyIIFNAME MetaKey = unix.NFT_META_IIFNAME + MetaKeyOIFNAME MetaKey = unix.NFT_META_OIFNAME + MetaKeyIIFTYPE MetaKey = unix.NFT_META_IIFTYPE + MetaKeyOIFTYPE MetaKey = unix.NFT_META_OIFTYPE + MetaKeySKUID MetaKey = unix.NFT_META_SKUID + MetaKeySKGID MetaKey = unix.NFT_META_SKGID + MetaKeyNFTRACE MetaKey = unix.NFT_META_NFTRACE + MetaKeyRTCLASSID MetaKey = unix.NFT_META_RTCLASSID + MetaKeySECMARK MetaKey = unix.NFT_META_SECMARK + MetaKeyNFPROTO MetaKey = unix.NFT_META_NFPROTO + MetaKeyL4PROTO MetaKey = unix.NFT_META_L4PROTO + MetaKeyBRIIIFNAME MetaKey = unix.NFT_META_BRI_IIFNAME + MetaKeyBRIOIFNAME MetaKey = unix.NFT_META_BRI_OIFNAME + MetaKeyPKTTYPE MetaKey = unix.NFT_META_PKTTYPE + MetaKeyCPU MetaKey = unix.NFT_META_CPU + MetaKeyIIFGROUP MetaKey = unix.NFT_META_IIFGROUP + MetaKeyOIFGROUP MetaKey = unix.NFT_META_OIFGROUP + MetaKeyCGROUP MetaKey = unix.NFT_META_CGROUP + MetaKeyPRANDOM MetaKey = unix.NFT_META_PRANDOM +) + +// Meta loads packet meta information for later comparisons. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Matching_packet_metainformation +type Meta struct { + Key MetaKey + Register uint32 +} + +func (e *Meta) marshal() ([]byte, error) { + exprData, err := netlink.MarshalAttributes( + []netlink.Attribute{ + {Type: unix.NFTA_META_KEY, Data: binaryutil.BigEndian.PutUint32(uint32(e.Key))}, + {Type: unix.NFTA_META_DREG, Data: binaryutil.BigEndian.PutUint32(e.Register)}, + }, + ) + if err != nil { + return nil, err + } + return netlink.MarshalAttributes([]netlink.Attribute{ + {Type: unix.NFTA_EXPR_NAME, Data: []byte("meta\x00")}, + {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: exprData}, + }) +} + +// Masq (Masquerade) is a special case of SNAT, where the source address is +// automagically set to the address of the output interface. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)#Masquerading +type Masq struct{} + +func (e *Masq) marshal() ([]byte, error) { + return netlink.MarshalAttributes([]netlink.Attribute{ + {Type: unix.NFTA_EXPR_NAME, Data: []byte("masq\x00")}, + {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: nil}, + }) +} + +// CmpOp specifies which type of comparison should be performed. +type CmpOp uint32 + +// Possible CmpOp values. +const ( + CmpOpEq CmpOp = unix.NFT_CMP_EQ + CmpOpNeq CmpOp = unix.NFT_CMP_NEQ + CmpOpLt CmpOp = unix.NFT_CMP_LT + CmpOpLte CmpOp = unix.NFT_CMP_LTE + CmpOpGt CmpOp = unix.NFT_CMP_GT + CmpOpGte CmpOp = unix.NFT_CMP_GTE +) + +// Cmp compares a register with the specified data. +type Cmp struct { + Op CmpOp + Register uint32 + Data uint32 +} + +func (e *Cmp) marshal() ([]byte, error) { + cmpData, err := netlink.MarshalAttributes([]netlink.Attribute{ + {Type: unix.NFTA_DATA_VALUE, Data: binaryutil.NativeEndian.PutUint32(e.Data)}, + }) + if err != nil { + return nil, err + } + exprData, err := netlink.MarshalAttributes([]netlink.Attribute{ + {Type: unix.NFTA_CMP_SREG, Data: binaryutil.BigEndian.PutUint32(e.Register)}, + {Type: unix.NFTA_CMP_OP, Data: binaryutil.BigEndian.PutUint32(unix.NFT_CMP_EQ)}, + {Type: unix.NLA_F_NESTED | unix.NFTA_CMP_DATA, Data: cmpData}, + }) + if err != nil { + return nil, err + } + return netlink.MarshalAttributes([]netlink.Attribute{ + {Type: unix.NFTA_EXPR_NAME, Data: []byte("cmp\x00")}, + {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: exprData}, + }) +} diff --git a/internal/binaryutil/binaryutil.go b/internal/binaryutil/binaryutil.go new file mode 100644 index 0000000..3e404de --- /dev/null +++ b/internal/binaryutil/binaryutil.go @@ -0,0 +1,65 @@ +// 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 binaryutil contains convenience wrappers around encoding/binary. +package binaryutil + +import ( + "encoding/binary" + + "github.com/koneu/natend" +) + +// ByteOrder is like binary.ByteOrder, but allocates memory and returns byte +// slices, for convenience. +type ByteOrder interface { + PutUint16(v uint16) []byte + PutUint32(v uint32) []byte +} + +// NativeEndian is either little endian or big endian, depending on the native +// endian-ness, and allocates memory and returns byte slices, for convenience. +var NativeEndian ByteOrder = &nativeEndian{} + +type nativeEndian struct{} + +func (nativeEndian) PutUint16(v uint16) []byte { + buf := make([]byte, 2) + natend.NativeEndian.PutUint16(buf, v) + return buf +} + +func (nativeEndian) PutUint32(v uint32) []byte { + buf := make([]byte, 4) + natend.NativeEndian.PutUint32(buf, v) + return buf +} + +// BigEndian is like binary.BigEndian, but allocates memory and returns byte +// slices, for convenience. +var BigEndian ByteOrder = &bigEndian{} + +type bigEndian struct{} + +func (bigEndian) PutUint16(v uint16) []byte { + buf := make([]byte, 2) + binary.BigEndian.PutUint16(buf, v) + return buf +} + +func (bigEndian) PutUint32(v uint32) []byte { + buf := make([]byte, 4) + binary.BigEndian.PutUint32(buf, v) + return buf +} diff --git a/nativeendian.go b/nativeendian.go new file mode 100644 index 0000000..5bdf5c8 --- /dev/null +++ b/nativeendian.go @@ -0,0 +1,21 @@ +// 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 "github.com/google/nftables/internal/binaryutil" + +// NativeEndian is either little endian or big endian, depending on the +// native endian-ness. +var NativeEndian binaryutil.ByteOrder = binaryutil.NativeEndian diff --git a/nftables.go b/nftables.go new file mode 100644 index 0000000..ebf78e9 --- /dev/null +++ b/nftables.go @@ -0,0 +1,278 @@ +// 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 manipulates Linux nftables (the iptables successor). +package nftables + +import ( + "fmt" + "math" + + "github.com/google/nftables/expr" + "github.com/google/nftables/internal/binaryutil" + "github.com/mdlayher/netlink" + "github.com/mdlayher/netlink/nltest" + "golang.org/x/sys/unix" +) + +func extraHeader(family uint8, resID uint16) []byte { + return append([]byte{ + family, + unix.NFNETLINK_V0, + }, binaryutil.NativeEndian.PutUint16(resID)...) +} + +func batch(messages []netlink.Message) []netlink.Message { + batch := []netlink.Message{ + { + Header: netlink.Header{ + Type: netlink.HeaderType(unix.NFNL_MSG_BATCH_BEGIN), + Flags: netlink.HeaderFlagsRequest, + }, + Data: extraHeader(0, unix.NFNL_SUBSYS_NFTABLES), + }, + } + + batch = append(batch, messages...) + + batch = append(batch, netlink.Message{ + Header: netlink.Header{ + Type: netlink.HeaderType(unix.NFNL_MSG_BATCH_END), + Flags: netlink.HeaderFlagsRequest, + }, + Data: extraHeader(0, unix.NFNL_SUBSYS_NFTABLES), + }) + + return batch +} + +// 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" +) + +// 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 +} + +// 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 { + chainHook := cc.marshalAttr([]netlink.Attribute{ + {Type: unix.NFTA_HOOK_HOOKNUM, Data: binaryutil.BigEndian.PutUint32(uint32(c.Hooknum))}, + {Type: unix.NFTA_HOOK_PRIORITY, Data: binaryutil.BigEndian.PutUint32(uint32(c.Priority))}, + }) + + 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")}, + {Type: unix.NLA_F_NESTED | unix.NFTA_CHAIN_HOOK, Data: chainHook}, + {Type: unix.NFTA_CHAIN_TYPE, Data: []byte(c.Type + "\x00")}, + }) + + cc.messages = append(cc.messages, netlink.Message{ + Header: netlink.Header{ + Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWCHAIN), + Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge | netlink.HeaderFlagsCreate, + }, + Data: append(extraHeader(unix.AF_INET, 0), data...), + }) + + return c +} + +func (cc *Conn) setErr(err error) { + if cc.err != nil { + return + } + cc.err = err +} + +func (cc *Conn) marshalAttr(attrs []netlink.Attribute) []byte { + b, err := netlink.MarshalAttributes(attrs) + if err != nil { + cc.setErr(err) + return nil + } + return b +} + +func (cc *Conn) marshalExpr(e expr.Any) []byte { + b, err := expr.Marshal(e) + if err != nil { + cc.setErr(err) + return nil + } + return b +} + +// A Rule does something with a packet. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Simple_rule_management +type Rule struct { + Table *Table + Chain *Chain + Exprs []expr.Any +} + +// AddRule adds the specified Rule. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Simple_rule_management +func (cc *Conn) AddRule(r *Rule) *Rule { + exprAttrs := make([]netlink.Attribute, len(r.Exprs)) + for idx, expr := range r.Exprs { + exprAttrs[idx] = netlink.Attribute{ + Type: unix.NLA_F_NESTED | unix.NFTA_LIST_ELEM, + Data: cc.marshalExpr(expr), + } + } + + data := cc.marshalAttr([]netlink.Attribute{ + {Type: unix.NFTA_RULE_TABLE, Data: []byte(r.Table.Name + "\x00")}, + {Type: unix.NFTA_RULE_CHAIN, Data: []byte(r.Chain.Name + "\x00")}, + {Type: unix.NLA_F_NESTED | unix.NFTA_RULE_EXPRESSIONS, Data: cc.marshalAttr(exprAttrs)}, + }) + + cc.messages = append(cc.messages, netlink.Message{ + Header: netlink.Header{ + Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWRULE), + Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge | netlink.HeaderFlagsCreate, + }, + Data: append(extraHeader(unix.AF_INET, 0), data...), + }) + + return r +} + +// TableFamily specifies the address family for this table. +type TableFamily byte + +// Possible TableFamily values. +const ( + TableFamilyIPv4 TableFamily = unix.AF_INET + TableFamilyIPv6 TableFamily = unix.AF_INET6 +) + +// A Table contains Chains. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_tables +type Table struct { + Name string + Family TableFamily +} + +// A Conn represents a netlink connection of the nftables family. +// +// All methods return their input, so that variables can be defined from string +// literals when desired. +// +// Commands are buffered. Flush sends all buffered commands in a single batch. +type Conn struct { + TestDial nltest.Func // for testing only; passed to nltest.Dial + messages []netlink.Message + err error +} + +// AddTable adds the specified Table. See also +// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_tables +func (cc *Conn) AddTable(t *Table) *Table { + data := cc.marshalAttr([]netlink.Attribute{ + {Type: unix.NFTA_TABLE_NAME, Data: []byte(t.Name + "\x00")}, + {Type: unix.NFTA_TABLE_FLAGS, Data: []byte{0, 0, 0, 0}}, + }) + cc.messages = append(cc.messages, netlink.Message{ + Header: netlink.Header{ + Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWTABLE), + Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge | netlink.HeaderFlagsCreate, + }, + Data: append(extraHeader(uint8(t.Family), 0), data...), + }) + return t +} + +// Flush sends all buffered commands in a single batch to nftables. +func (cc *Conn) Flush() error { + if cc.err != nil { + return cc.err // serialization error + } + var conn *netlink.Conn + var err error + if cc.TestDial == nil { + conn, err = netlink.Dial(unix.NETLINK_NETFILTER, nil) + } else { + conn = nltest.Dial(cc.TestDial) + } + if err != nil { + return err + } + + if _, err := conn.SendMessages(batch(cc.messages)); err != nil { + return fmt.Errorf("SendMessages: %v", err) + } + + if _, err := conn.Receive(); err != nil { + return fmt.Errorf("Receive: %v", err) + } + + cc.messages = nil + + return nil +} diff --git a/nftables_test.go b/nftables_test.go new file mode 100644 index 0000000..34c5d1d --- /dev/null +++ b/nftables_test.go @@ -0,0 +1,115 @@ +// 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_test + +import ( + "bytes" + "testing" + + "github.com/google/nftables" + "github.com/google/nftables/expr" + "github.com/mdlayher/netlink" +) + +func TestConfigureNAT(t *testing.T) { + // TODO: skip this test as non-root + // TODO: run this in its own network namespace to not mess with the host + + // The want byte sequences come from stracing nft(8), e.g.: + // strace -f -v -x -s 2048 -eraw=sendto nft add table ip nat + // + // The nft(8) command sequence was taken from: + // https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT) + want := [][]byte{ + // batch begin + []byte("\x00\x00\x0a\x00"), + // nft add table ip nat + []byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x08\x00\x02\x00\x00\x00\x00\x00"), + // nft add chain nat prerouting '{' type nat hook prerouting priority 0 \; '}' + []byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x0f\x00\x03\x00\x70\x72\x65\x72\x6f\x75\x74\x69\x6e\x67\x00\x00\x14\x00\x04\x80\x08\x00\x01\x00\x00\x00\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00\x08\x00\x07\x00\x6e\x61\x74\x00"), + // nft add chain nat postrouting { type nat hook postrouting priority 100 \; } + []byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x10\x00\x03\x00\x70\x6f\x73\x74\x72\x6f\x75\x74\x69\x6e\x67\x00\x14\x00\x04\x80\x08\x00\x01\x00\x00\x00\x00\x04\x08\x00\x02\x00\x00\x00\x00\x64\x08\x00\x07\x00\x6e\x61\x74\x00"), + // nft add rule nat postrouting oif wlp4s0 masquerade + []byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x10\x00\x02\x00\x70\x6f\x73\x74\x72\x6f\x75\x74\x69\x6e\x67\x00\x68\x00\x04\x80\x24\x00\x01\x80\x09\x00\x01\x00\x6d\x65\x74\x61\x00\x00\x00\x00\x14\x00\x02\x80\x08\x00\x02\x00\x00\x00\x00\x05\x08\x00\x01\x00\x00\x00\x00\x01\x2c\x00\x01\x80\x08\x00\x01\x00\x63\x6d\x70\x00\x20\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x08\x00\x02\x00\x00\x00\x00\x00\x0c\x00\x03\x80\x08\x00\x01\x00\x03\x00\x00\x00\x14\x00\x01\x80\x09\x00\x01\x00\x6d\x61\x73\x71\x00\x00\x00\x00\x04\x00\x02\x80"), + // batch end + []byte("\x00\x00\x0a\x00"), + } + + c := &nftables.Conn{ + TestDial: func(req []netlink.Message) ([]netlink.Message, error) { + for idx, msg := range req { + b, err := msg.MarshalBinary() + if err != nil { + t.Fatal(err) + } + if len(b) < 16 { + continue + } + b = b[16:] + if len(want) == 0 { + t.Errorf("no want entry for message %d: %x", idx, b) + continue + } + if got, want := b, want[0]; !bytes.Equal(got, want) { + t.Errorf("message %d: got %x, want %x", idx, got, want) + } + want = want[1:] + } + return req, nil + }, + } + + nat := c.AddTable(&nftables.Table{ + Family: nftables.TableFamilyIPv4, + Name: "nat", + }) + + c.AddChain(&nftables.Chain{ + Name: "prerouting", + Hooknum: nftables.ChainHookPrerouting, + Priority: nftables.ChainPriorityFilter, + Table: nat, + Type: nftables.ChainTypeNAT, + }) + + postrouting := c.AddChain(&nftables.Chain{ + Name: "postrouting", + Hooknum: nftables.ChainHookPostrouting, + Priority: nftables.ChainPriorityNATSource, + Table: nat, + Type: nftables.ChainTypeNAT, + }) + + c.AddRule(&nftables.Rule{ + Table: nat, + Chain: postrouting, + Exprs: []expr.Any{ + // meta load oif => reg 1 + &expr.Meta{Key: expr.MetaKeyOIF, Register: 1}, + // cmp eq reg 1 0x00000003 + &expr.Cmp{ + Op: expr.CmpOpEq, + Register: 1, + Data: 3, /* wlp4s0 */ + }, + // masq + &expr.Masq{}, + }, + }) + + if err := c.Flush(); err != nil { + t.Fatal(err) + } +}