Commit Graph

250 Commits

Author SHA1 Message Date
Paul Greenberg fc0210d0ce
Merge dae73eaa9c into e2fedeb355 2025-03-13 17:30:29 +01:00
Jan Schär e2fedeb355
Improve safety of ID allocation (#307)
There was an existing mechanism to allocate IDs for sets, but this was
using a global counter without any synchronization to prevent data
races. I replaced this by a new mechanism which uses a connection-scoped
counter, protected by the Conn.mu Mutex. This can then also be used in
other places where IDs need to be allocated.

As an additional safeguard, it will panic instead of allocating the same
ID twice in a transaction. Most likely, your program will run out of
memory before reaching this point.
2025-03-13 10:38:46 +01:00
Michael Stapelberg a24f918d08 go.{mod,sum}: update to latest x/ packages 2025-03-13 09:42:41 +01:00
Michael Stapelberg 3163cd89a9 go.mod: bump language version to go1.23
Our dependencies like golang.org/x/net use go1.23 (the oldest still-supported
version, latest is go1.24), so it is time for us to upgrade, too.
2025-03-13 09:41:52 +01:00
Jan Schär 4eb1370754
Split set elements into batches if needed (#303)
If the number of elements to be added to or removed from a set is large,
they may not all fit into one message, because the size field of a
netlink attribute is a uint16 and would overflow. To support this case,
the elements need to be split into multiple batches.
2025-03-03 12:55:40 +01:00
Jan Schär 385f80f4ef Use const instead of var where possible 2025-02-26 15:11:55 +01:00
Jan Schär 594585af33 Initialize registers in test
Recent kernels disallow reads from uninitialized registers, which breaks
this test.

See 14fb07130c
2025-02-26 15:11:55 +01:00
Marten Seemann 0420ffbf57
fix unmarshalling of expr.Ct source register (#301) 2025-02-21 09:34:44 +01:00
Alexander 6f574e7fd1
added numgen case in exprFromName (#297) 2025-02-03 16:23:31 +01:00
Aleksei Ilin 69f487d596 set: Add set support for size specifier
Handle attribute NFTNL_SET_DESC_SIZE, as done in libnftnl:
https://git.netfilter.org/libnftnl/tree/src/set.c#n424

Example:
nft add set ip filter myset { type ipv4_addr\; size 65535\; flags dynamic\; }
2025-01-24 09:33:09 +01:00
Aleksei Ilin b011eb129e set: Fix missing 'Dynamic' field restore in set unmarshal 2025-01-24 09:33:09 +01:00
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