Skip to content

net/netns: add functionality to bind outgoing sockets based on route table - #7065

Merged
andrew-d merged 1 commit into
mainfrom
andrew/netns-bind-by-route
Jan 27, 2023
Merged

andrew-d merged 1 commit into
mainfrom
andrew/netns-bind-by-route

Conversation

@andrew-d

@andrew-d andrew-d commented Jan 25, 2023 •

Copy link
Copy Markdown
Member

When turned on via environment variable (off by default), this will use the BSD routing APIs to query what interface index a socket should be bound to, rather than binding to the default interface in all cases.

Updates #5719
Updates #5940

Signed-off-by: Andrew Dunham [email protected]
Change-Id: Ib4c919471f377b7a08cd3413f8e8caacb29fee0b

@bradfitz

Copy link
Copy Markdown
Member

This is the sort of commit that really needs to reference an issue :)

@andrew-d
andrew-d force-pushed the andrew/netns-bind-by-route branch from 8933b58 to 8f3711e Compare January 25, 2023 18:33
Comment thread net/netns/netns_darwin.go Outdated
}
}

var bindToDeviceByRoute = envknob.RegisterBool("TS_BIND_TO_DEVICE_BY_ROUTE")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variables are annoying to set on macOS and ~impossible on iOS. Should we also add a node attribute that can be used to enable this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good idea; will add.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #7077 too now that you reminded me :)

Comment thread net/netns/netns_darwin.go Resolved
Comment thread net/netns/netns_darwin.go Resolved
Comment thread net/netns/netns_darwin.go Outdated

// interfaceIndexFor returns the interface index that we should bind to in
// order to send traffic to the provided address.
func interfaceIndexFor(addr netip.Addr) (int, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how expensive this is, we could add a cache for this (and invalidate it via LinkMonitor RegisterChangeCallback callback).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty expensive, yeah, which is why I'm not planning on enabling this by default. In the long term, I want to move ~all of this to the LinkMonitor by having it maintain a copy of the system's route table so we don't need to re-parse everything.

In the mean time, happy to do this; any suggestions or thoughts on where the callback should be registered, since we don't really have persistent state conveniently available?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now we don't have to do any of it, was just speculating.

When we do start to do it, triggering the cache invalidation from LocalBackend's linkChange seems like a reasonable place (as far as LocalBackend also enabling this code path to start with).

@andrew-d
andrew-d force-pushed the andrew/netns-bind-by-route branch 2 times, most recently from 0a0bf39 to 990d3db Compare January 25, 2023 22:47
@andrew-d
andrew-d requested a review from mihaip January 25, 2023 22:55
Comment thread net/netns/netns_darwin.go Resolved
Comment thread net/netns/netns_darwin.go Outdated

// interfaceIndexFor returns the interface index that we should bind to in
// order to send traffic to the provided address.
func interfaceIndexFor(addr netip.Addr) (int, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now we don't have to do any of it, was just speculating.

When we do start to do it, triggering the cache invalidation from LocalBackend's linkChange seems like a reasonable place (as far as LocalBackend also enabling this code path to start with).

@andrew-d
andrew-d force-pushed the andrew/netns-bind-by-route branch from 990d3db to e100711 Compare January 25, 2023 23:47
@andrew-d

Copy link
Copy Markdown
Member Author

@mihaip - I wrote a test and refactored this a fair bit; during my testing, I found that it's sometimes possible that the first call to the route APIs returns the gateway IP rather than the interface, and we need to recursively call ourselves (exactly once) to get the interface for a given gateway IP. Mind re-reviewing this?

@andrew-d
andrew-d requested a review from mihaip January 25, 2023 23:49
Comment thread net/netns/netns_darwin_test.go Resolved
…table

When turned on via environment variable (off by default), this will use
the BSD routing APIs to query what interface index a socket should be
bound to, rather than binding to the default interface in all cases.

Updates #5719
Updates #5940

Signed-off-by: Andrew Dunham <[email protected]>
Change-Id: Ib4c919471f377b7a08cd3413f8e8caacb29fee0b
@andrew-d
andrew-d force-pushed the andrew/netns-bind-by-route branch from e100711 to c3eac0e Compare January 26, 2023 00:41
@andrew-d
andrew-d merged commit 2703d69 into main Jan 27, 2023
@andrew-d
andrew-d deleted the andrew/netns-bind-by-route branch January 27, 2023 01:59
Comment thread net/netns/netns_darwin.go
}

rm := route.RouteMessage{
Version: unix.RTM_VERSION,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pin this to a fixed version with comment?

Comment thread net/netns/netns_darwin.go
Comment on lines +135 to +136
var buf [2048]byte
n, err := unix.Read(fd, buf[:])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// safe to do single read because X and smaller than 4KB?

@bradfitz bradfitz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

andrew-d added a commit that referenced this pull request Jan 27, 2023
Follow-up to #7065 with some comments from Brad's review.

Signed-off-by: Andrew Dunham <[email protected]>
Change-Id: Ia1219f4fa25479b2dada38ffe421065b408c5954
andrew-d added a commit that referenced this pull request Jan 27, 2023
Follow-up to #7065 with some comments from Brad's review.

Signed-off-by: Andrew Dunham <[email protected]>
Change-Id: Ia1219f4fa25479b2dada38ffe421065b408c5954
coadler pushed a commit to coder/tailscale that referenced this pull request Feb 2, 2023
Follow-up to tailscale#7065 with some comments from Brad's review.

Signed-off-by: Andrew Dunham <[email protected]>
Change-Id: Ia1219f4fa25479b2dada38ffe421065b408c5954
@tunix

tunix commented Sep 2, 2024

Copy link
Copy Markdown

Hi @bradfitz , I've installed tailscale on my Mac via homebrew. I want to enable this feature but couldn't figure out whether I have this feature on my computer (I'm on version 1.72.2) or what to write into nodeAttrs. What's the value to use inside attr part to enable this?

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

Successfully merging this pull request may close these issues.

4 participants