Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter on IPv6 (or IPv4) hides events from the other IP family #53

Closed
gtataranni opened this issue Oct 10, 2023 · 4 comments
Closed

filter on IPv6 (or IPv4) hides events from the other IP family #53

gtataranni opened this issue Oct 10, 2023 · 4 comments

Comments

@gtataranni
Copy link
Contributor

I am playing with the library, using the example code provided, but using RegisterFiltered

var filterNoIPv6Loopback = []ct.ConnAttr{
	{ct.AttrOrigIPv6Src,
		net.IPv6loopback
		net.CIDRMask(128, net.IPv6len*8),
		true,
	},
}

func ExampleNfctRegister(ctx context.Context) {
	nfct, err := ct.Open(&ct.Config{AddConntrackInformation: true})
	if err != nil {
		log.Fatal(err)
		return
	}
	go func() {
		<-ctx.Done()
		nfct.Close()
	}()

	if err := nfct.RegisterFiltered(ctx, ct.Conntrack, ct.NetlinkCtNew, filterNoIPv6Loopback, printer); err != nil {
		fmt.Println("could not register callback:", err)
		return
	}
	fmt.Println("registered")
}

func printer(c ct.Con) int {
	jsonOut, _ := json.MarshalIndent(c, "", "  ")
	fmt.Println(string(jsonOut))
	fmt.Println("-------------------------")
	return 0
}

func main() {
	ctx, ctxCancel := context.WithCancel(context.Background())
	ExampleNfctRegister(ctx)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for sig := range c {
			_ = sig
			ctxCancel()
			return
		}
	}()

	fmt.Println("waiting for SIGINT signal")
	<-ctx.Done()
}

Seems that by registering a filter for an IPv6 IP, only IPv6 events are received, but no IPv4 event.
The opposite is also true: registering a IPv4 filter will exclude all IPv6 events.

By registering no filter, both IPv4 and IPv6 events are received.

Is this expected?

@florianl
Copy link
Owner

Hi @gtataranni

the situation you describe is expected behaviour. It originates in the construction of BPF filters (not eBPF!) on the socket. So my recommendation would be just to open two netlink sockets, one for IPv6 and one for IPv4.

@gtataranni
Copy link
Contributor Author

Ok, thanks for the answer! Can we add a documentation comment about it? Do you want me to open a PR?

@florianl
Copy link
Owner

Sure, improving documentation sounds good to me. Feel free to open a PR.

@florianl
Copy link
Owner

fixed with #58.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants