Commit Graph

238 Commits

Author SHA1 Message Date
turekt 1c789726cf
Fix Fib parsing (#296) 2025-01-16 09:15:33 +01:00
Antonio Ojea c9a67ccd0c Add integration tests for nftables package
This commit adds integration tests to the nftables package to verify
that the Go code correctly programs nftables rules. The tests use
external nftables scripts to define the expected state and compare
it with the state produced by the Go code.

Change-Id: I9c8439ee462b4882b221e6244f53379b822446dc
Signed-off-by: Antonio Ojea <aojea@google.com>
2025-01-15 12:42:22 +01:00
Antonio Ojea 3cae4777b1 adapt tests to avoid complains with the format
Change-Id: Ie90d72f48e42c9d896ba302685352295efacc099
Signed-off-by: Antonio Ojea <aojea@google.com>
2025-01-15 12:42:22 +01:00
shiningw 4d2aea87f8
add support for comments in set elements (#293) 2025-01-15 09:36:42 +01:00
dependabot[bot] 85aee131ff
Bump golang.org/x/net from 0.23.0 to 0.33.0 in the go_modules group (#295)
Bumps the go_modules group with 1 update: [golang.org/x/net](https://github.com/golang/net).


Updates `golang.org/x/net` from 0.23.0 to 0.33.0
- [Commits](https://github.com/golang/net/compare/v0.23.0...v0.33.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
  dependency-group: go_modules
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-01-14 09:46:28 +01:00
Michael Stapelberg 45dc72792b pull in latest mdlayher/netlink to drop josharian/native dep
Turns out we cannot make github.com/google/nftables/binaryutil
forward to encoding/binary because it’s not an API-compatible
drop-in replacement: The PutUint* methods allocate in our API,
but do not allocate in encoding/binary.
2025-01-13 21:22:41 +01:00
turekt e99829fb4f
Support set comments (#290) 2024-12-19 10:24:56 +01:00
patryk4815 eb34035740
Fix incorrect size check in NFGenMsg (#287)
Fix incorrect size check in NFGenMsg, resolving TableFamily issue for rules in monitor
2024-12-13 07:30:25 +01:00
patryk4815 c96bb6363f
Fix Deadlock in `Flush` Function Due to ENOBUFS (#286)
* fix: resolve deadlock in `Flush` function when handling ENOBUFS error

* Simulate deadlock issue using reduced read/write buffers to verify the fix and ensure no regressions
2024-11-29 08:34:52 +01:00
_|+ 51c44dcf05
Implement AddGenerationalMonitor to deliver monitor events in batches (#283) 2024-11-09 12:07:36 +01:00
_|+ ed578af895
Add WithSockOptions to modify netlink socket options (#277) 2024-09-23 17:19:43 +02:00
_|+ 583cd2bdea
Fix: NFT_DYNSET_F_EXPR not supported for kernels < 5.11-rc3 (#276)
Note that this will fix support for single expressions on older kernels but multiple expressions on older kernels will remain unsupported as NFT_DYNSET_F_EXPR flag should not be omitted for dynsets with multiple expressions.
2024-09-13 08:54:50 +02:00
turekt 5cbea79940
Added ct timeout obj (#275) 2024-09-11 08:27:40 +02:00
turekt aca62a1d00
Add secmark obj support (#274) 2024-09-09 22:56:09 +02:00
turekt 2fecffcfe1
Add ct expect support (#272) 2024-09-09 08:35:05 +02:00
Asutorufa 9a9f2ce6b3
set: add set support auto-merge (#271)
Signed-off-by: Asutorufa <16442314+Asutorufa@users.noreply.github.com>
2024-09-02 18:48:06 +02:00
Aleksei Ilin 7a6c4ef3e7
nat: Add flag for specified range in DNAT (#270)
Example: meta l4proto tcp dnat to 10.200.45.6:9080

  [ meta load l4proto => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ immediate reg 1 0x062dc80a ]
  [ immediate reg 2 0x00007823 ]
  [ nat dnat ip addr_min reg 1 proto_min reg 2 flags 0x2 ]
2024-08-30 09:01:02 +02:00
turekt adb98462ee
Add synproxy obj (#269) 2024-08-27 09:06:28 +02:00
turekt 6ddeb7caed
Implement cthelper object expr (#268) 2024-08-15 08:47:35 +02:00
turekt 38e481bfc4
Added GetNamedObjects and ResetNamedObjects (#267) 2024-08-14 08:10:09 +02:00
turekt 4d451ef75f
Fix staticcheck issues (#266) 2024-08-11 10:00:48 +02:00
Aleksei Ilin b76fdc8f90
ct: Specify direction for saddr, daddr, proto-src, proto-dst (#264)
The CT keys require direction parameter.
2024-08-05 08:38:34 +02:00
turekt 3b928008ad
refactor nftable Object handling (NamedObj type) (#259)
* Refactored obj.go to a more generic approach
* Added object support for already implemented expressions
* Added test for limit object

fixes https://github.com/google/nftables/issues/253
2024-07-29 08:43:58 +02:00
turekt 912dee68b1
Refactor expr for obj reimplementation (#265)
Added marshalData func to expressions
Prepare parseexprfunc for obj implementation refactor

related to #253
2024-07-24 08:26:13 +02:00
TheDiveO aa8348f790
feat: add xt.Comment (#260)
Signed-off-by: thediveo <thediveo@gmx.eu>
2024-04-22 08:53:34 +02:00
dependabot[bot] 20edd38e22
Bump golang.org/x/net from 0.22.0 to 0.23.0 (#261)
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.22.0 to 0.23.0.
- [Commits](https://github.com/golang/net/compare/v0.22.0...v0.23.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-04-19 19:12:50 +02:00
turekt 5e242ec578
List table or chain by name (#258)
Adds functionality to list table or chain by specifying its name
2024-04-14 11:19:27 +02:00
Daniel Mack 8ffcbc2d36 Chain: add 'device' as hook attribute
NETDEV tables can specify a device the apply to.
Add support for this by augmenting the `Chain` struct.
2024-04-01 11:26:10 +02:00
Daniel Mack 4dbe06f125 chain: add ChainHookEgress
NETDEV tables can have egress hooks. Add the definition to enable that case.
2024-04-01 11:26:10 +02:00
Lionel Jouin 0b98a7af91
Replace TODO const by golang.org/x/sys/unix (#256)
Replace some hardcoded consts by the ones provided in
golang.org/x/sys/unix
2024-03-18 08:32:45 +01:00
Michael Stapelberg 5494b7c55f bump go.mod to go1.21, update deps
fixes https://github.com/google/nftables/issues/254
2024-03-10 13:09:31 +01:00
Francesco Cheinasso 33ee8df9d8 NAT: prefix test 2024-01-12 21:30:04 +01:00
Francesco Cheinasso ef45dd3322
NAT: prefix support (#251) 2023-12-22 08:33:18 +01:00
Michael Stapelberg 6d4c531bb6 monitor: add example and documentation 2023-12-13 08:31:13 +01:00
singchia 5555df300c
feat: add monitor on table chain rule set setelem and obj events (#250)
fixes https://github.com/google/nftables/issues/224
2023-12-13 08:23:07 +01:00
turekt 0f60df61a2
Support for quota as object (#244)
Fixes https://github.com/google/nftables/issues/238
2023-12-12 21:49:45 +01:00
black-desk 32bfbb6627
feat: add Conn.CreateTable (#246)
`Conn.AddTable` use netlink.Create which will not emit an error
if the table we want to create already existed,
just like the `nft add table ...` command works.

The caller should use netlink.Excl to
get an EEXIST error for that already existed,

So I add another method `Conn.CreateTable`
which works just like `nft create table ...` command.

Related: #245

Signed-off-by: black-desk <me@black-desk.cn>
2023-10-24 08:57:23 +02:00
black-desk 6df7a82bbd
fix: correct error handling in receiveAckAware (#243)
Related: #242

After 7879d7ecf6, it seems that
any multi-message operation performed without CAP_SYS_ADMIN will
leads to forever block inside nftables.Conn.Flush.

For example:

```go
package main

import "github.com/google/nftables"

func main() {
	conn, err := nftables.New()
	if err != nil {
		panic(err)
	}

	t := conn.AddTable(&nftables.Table{})

	err = conn.AddSet(&nftables.Set{Table: t}, []nftables.SetElement{})
	if err != nil {
		panic(err)
	}

	conn.AddSet(&nftables.Set{Table: t}, []nftables.SetElement{})
	if err != nil {
		panic(err)
	}

	err = conn.Flush()
	if err != nil {
		panic(err)
	}

	return
}
```

That's because that although we send multiple messages on netlink
socket, kernel will only sends one permission error message as reply.

Signed-off-by: black-desk <me@black-desk.cn>
2023-10-21 22:11:55 +02:00
dependabot[bot] 4efd25beb8
Bump golang.org/x/net from 0.7.0 to 0.17.0 (#241)
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.7.0 to 0.17.0.
- [Commits](https://github.com/golang/net/compare/v0.7.0...v0.17.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-12 08:09:03 +02:00
Ronak Jain 7879d7ecf6
Flush: fix error leak when flushing multiple messages (#239)
When you flush multiple messages/ops on a connection, and if flush fails
to apply, the netlink connection returns errors per command. Since we
are returning on noticing the first error, the rest of the errors are
buffered and leaks into the result of next flush.

This pull request invokes `conn.Receive()` * number of messages to drain
any buffered errors in the connection.
2023-10-01 23:06:11 +02:00
turekt 0d9bfa4d18
Fix overflow in Flush by using receiveAckAware and handling the overrun flag (#237)
Fixes https://github.com/google/nftables/issues/235
Added support for messages having overrun flag
Changed `conn.Receive` call to `receiveAckAware` in `Flush`
2023-09-23 17:08:35 +02:00
Lars Lehtonen 6a61933ab7
Fix dropped error (#240)
Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com>
2023-09-16 08:51:22 +02:00
Rafael Campos Las Heras d27cc52f15
hash: Fix hash seed conditional (#234)
Fix the marshall of the hash seed to be conditional, only if it is
explicitly set, we need to add it to the kernel as stated on the
libnftl and nftables projects.
Refence: https://git.netfilter.org/nftables/tree/src/netlink_linearize.c?id=25e7b99cc450490c38becb03d8bddd0199cfd3f9#n174

Otherwise, having a hash expression similar to this:
```
ip daddr set jhash tcp sport mod 2 seed 0x0 map { 0 : 192.168.0.1, 1 : 192.168.2.2 }
```
end up setting only the first IP and ignoring the second one.

Signed-off-by: Rafael Campos <methril@gmail.com>
2023-08-03 08:14:23 +02:00
black-desk 8a10f68900
Add address field for expr tproxy (#231)
* Add address field for expr tproxy

Signed-off-by: black-desk <me@black-desk.cn>
Co-authored-by: MrRedhat <Redhatminzhe@Gmail.com>
2023-07-10 08:38:01 +02:00
Rafael Campos 8f2d395e10 set: Parse bytes length on sets
Parse the bytes length for key and data types when reading the sets.

Signed-off-by: Rafael Campos <methril@gmail.com>
2023-06-14 20:10:15 +02:00
Rafael Campos 98de8c8ac9 expr: Add Hash to the expr type
Add the Hash type to the expr read parser.

Signed-off-by: Rafael Campos <methril@gmail.com>
2023-06-14 20:10:15 +02:00
Ronak Jain 45645ffbf5
Parse set element counters (#230) 2023-05-20 18:23:52 +02:00
Michael Stapelberg cf9b0d57e8 GitHub Actions: switch to setup-go@v4 which enables caching 2023-05-20 14:47:36 +02:00
turekt d746ecb0e4
Implement set KeyByteOrder (#226)
Fixes https://github.com/google/nftables/issues/225
Introduced KeyByteOrder in sets which fills UDATA with endianess information
2023-05-18 18:05:49 +02:00
black-desk b18665a6ec
Implement socket expr (#222)
Signed-off-by: black-desk <me@black-desk.cn>
2023-05-06 10:49:33 +02:00