Skip to content

Program routes and policy rules using netlink, not iptables binary #391

Closed

Description

@danderson

The linux router currently programs the linux network stack with ip and iptables. iptables is unavoidable because the corresponding netlink APIs are not safe for direct use. However, everything we do with ip we could just do directly over a netlink socket.

This would in particular help embedded systems that ship with the Busybox version of ip, which is stripped down and doesn't understand advanced commands like policy routing rules, which Tailscale requires.

Activity

  1. added
    L2 Few
    P3 Can't get started
    T8 Crash
    on May 15, 2020
  2. stapelberg commented on May 27, 2020

    @stapelberg
    Contributor

    iptables is unavoidable because the corresponding netlink APIs are not safe for direct use.

    Can you expand on this? Which APIs do you need, and in which way are they unsafe?

    I was about to open an issue suggesting to use https://github.com/google/nftables/ where available to avoid the iptables dependency. This would help make tailscale work on https://gokrazy.org/, where only Go is available (no C userland, i.e. no iptables).

  3. danderson commented on May 27, 2020

    @danderson
    MemberAuthor

    For one, we can't depend on nftables, because we need to support OSes that haven't switched to nftables under the hood (e.g. pre-Buster debian), and we have to be cross-compatible with other software that doesn't support nftables (e.g. Docker, Kubernetes). The main way we can do that safely is to continue using the iptables binary, which will do "the right thing" based on OS/administrator policy.

    And the legacy iptables is unsafe to use over netlink, because the API does replacement of the entire firewall at once, and doesn't have a way of preventing concurrent writes from squashing each other. The userspace tools work around this (poorly) with file-based locks, which already goes wrong in situations like containers.

    If we can (a) positively detect that nftables, and only nftables is in use on the system; (b) we can manipulate the "legacy iptables converted to nftables" rules in nftables without conflicting with iptables itself... Then we could possibly make that work. But it's a bunch of work that, frankly, only makes sense for something like gokrazy, and not really for any other OS. So, that's going to be pretty low priority from my POV.

  4. stapelberg commented on May 27, 2020

    @stapelberg
    Contributor

    Thanks for the additional details, I agree with your view point.

    I expect that using nftables directly might become more attractive in a few years as more and more installations run on the legacy-converted-to-nftables model.

    Would an nftables fallback code path be something that you would accept if people contributed it, or is there a lot of churn and hence maintenance burden in that part of the tailscale code base?

  5. danderson commented on May 27, 2020

    @danderson
    MemberAuthor

    I would love to not depend on iptables where possible. The part I'm unsure about is whether we can reliably detect that yes, it's definitely safe to use nftables directly without breaking the system.

    If we can figure out how to make that part work reliably, I'd be very happy to have logic that uses nftables directly where possible, and falls back to shelling out to iptables otherwise.

    There is definitely a maintenance burden, and given that we support distros like RHEL that take nearly a decade to drop support, I don't think iptables is going anywhere any time soon, sadly. I still think it's worth doing, if you want to open that particular can of worms :). Otherwise, I think we'll get to it "someday", but once we get the iptables logic stable it's going to be low priority.

  6. added
    T0 New feature
    and removed
    T8 Crash
    on May 20, 2021
  7. 137 remaining items

  8. zlepper commented on Aug 24, 2023

    @zlepper

    It is working for me on version v1.48.1-t0e9f04c83 now, with TS_DEBUG_FIREWALL_MODE=auto env set when running on Azure kubernetes (AKS) and the tailscale k8s operator.

  9. added a commit that references this issue on Aug 24, 2023
  10. adrianmihalko commented on Aug 31, 2023

    @adrianmihalko

    In https://tailscale.com/kb/1294/firewall-mode/ guide

    "When neither the iptables binary nor the nftables Netlink API are available, Tailscale will fall back to a degraded operation that may result in reduced performance or increased CPU usage. "

    • this is still true? For me it doesn't fall back to degraded operation, instead it just exiting.

    wgengine.NewUserspaceEngine(tun "tailscale0") error: creating router: exec: "iptables": executable file not found in $PATH

  11. rodrigc commented on Sep 2, 2023

    @rodrigc
    Contributor

    I ran a quick test.

    1. Spun up a Kubernetes cluster in Oracle Cloud

    2. Used image: docker.io/tailscale/tailscale:unstable-v1.49.189

    3. Created a simple tailscale-test.yaml for testing:

    ---
    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: null
      labels:
        run: tailscale
      name: tailscale
    spec:
      containers:
        - args:
            - /bin/sh
            - -c
            - echo hello;sleep 36000
          image: docker.io/tailscale/tailscale:unstable-v1.49.189
          name: tailscale
          securityContext:
            privileged: true
          resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Never
    status: {}
    
    1. Applied the yaml:
    kubectl apply -f tailscale-test.yaml
    
    1. Jumped into the container:
    kubectl exec -t -i tailscale -- sh
    
    1. Set the heuristic to auto and started tailscaled:
    export TS_DEBUG_FIREWALL_MODE=auto
    tailscaled
    
    1. I saw that the heurstic correctly identified nftables:
    logpolicy: using system state directory "/var/lib/tailscale"
    logpolicy.ConfigFromFile /var/lib/tailscale/tailscaled.log.conf: open /var/lib/tailscale/tailscaled.log.conf: no such file or directory
    logpolicy.Config.Validate for /var/lib/tailscale/tailscaled.log.conf: config is nil
    wgengine.NewUserspaceEngine(tun "tailscale0") ...
    router: nftables rule count: 0, iptables rule count: 0
    router: nftables is available
    router: using nftables
    router: disabling tunneled IPv6 due to system IPv6 config: %!w(*errors.errorString=&{disable_ipv6 is set})
    
    1. Verified that this image was being used on the Oracle side:
      Oracle-Linux-8.8-2023.06.30-0-OKE-1.26.2-632
  12. bradfitz commented on May 31, 2024

    @bradfitz
    Member

    Sufficiently done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

L2 FewOS-linuxP3 Can't get startedT0 New featurefr

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions