|
| 1 | +// Copyright (c) 2023-2026, Nubificus LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package localhost |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "net" |
| 21 | + "os/exec" |
| 22 | + "strings" |
| 23 | + "syscall" |
| 24 | + |
| 25 | + "github.com/vishvananda/netlink" |
| 26 | +) |
| 27 | + |
| 28 | +func dockerRules(f *Forwarder) error { |
| 29 | + |
| 30 | + tcpPort, udpPort, err := dockerDNSPorts(f.LoIP) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + lhlog.Debugf("Docker DNS resolver: tcp/%s udp/%s", tcpPort, udpPort) |
| 35 | + |
| 36 | + if err := dnat(f.VirtIP, "udp", net.JoinHostPort(f.LoIP.String(), udpPort)); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if err := dnat(f.VirtIP, "tcp", net.JoinHostPort(f.LoIP.String(), tcpPort)); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + lhlog.Debug("Applied Docker DNAT rules") |
| 43 | + |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func dockerDNSPorts(loIP net.IP) (string, string, error) { |
| 48 | + var tcpPort, udpPort string |
| 49 | + |
| 50 | + // syscall.AF_INET : IPv4 |
| 51 | + tcp, err := netlink.SocketDiagTCPInfo(syscall.AF_INET) |
| 52 | + if err != nil { |
| 53 | + return "", "", fmt.Errorf("could not list TCP sockets in namespace: %w", err) |
| 54 | + } |
| 55 | + for _, s := range tcp { |
| 56 | + if s.InetDiagMsg.ID.Source.String() == loIP.String() && s.InetDiagMsg.ID.Destination.String() == "0.0.0.0" && int(s.InetDiagMsg.State) == netlink.TCP_LISTEN { |
| 57 | + tcpPort = fmt.Sprintf("%d", s.InetDiagMsg.ID.SourcePort) |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + udp, err := netlink.SocketDiagUDPInfo(syscall.AF_INET) |
| 62 | + if err != nil { |
| 63 | + return "", "", fmt.Errorf("could not list UDP sockets in namespace: %w", err) |
| 64 | + } |
| 65 | + for _, s := range udp { |
| 66 | + |
| 67 | + // As udp has no connection states as tcp. by default (which is our case) it should have tcp_close (7) state to read it as "no peer association" |
| 68 | + // TODO: is there any other states that could make this condition fails ? |
| 69 | + if s.InetDiagMsg.ID.Source.String() == loIP.String() && s.InetDiagMsg.ID.Destination.String() == "0.0.0.0" && int(s.InetDiagMsg.State) == netlink.TCP_CLOSE { |
| 70 | + udpPort = fmt.Sprintf("%d", s.InetDiagMsg.ID.SourcePort) |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + if tcpPort == "" || udpPort == "" { |
| 75 | + return "", "", fmt.Errorf("could not find docker embedded resolver ports for %s (tcp=%q udp=%q)", loIP, tcpPort, udpPort) |
| 76 | + } |
| 77 | + return tcpPort, udpPort, nil |
| 78 | +} |
| 79 | + |
| 80 | +// ClearRules removes DNAT rules dockerRules could have added. |
| 81 | +func clearRules() error { |
| 82 | + |
| 83 | + // This will be run from kill() so no memory state could be used. |
| 84 | + // we list all IPtable rules from scratch. |
| 85 | + |
| 86 | + // iptables-save output example: |
| 87 | + // -A PREROUTING -d 192.168.100.100/32 -i lo -p udp -j DNAT --to-destination 127.0.0.11:60269 |
| 88 | + ipt, err := exec.LookPath("iptables-save") |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + out, err := exec.Command(ipt, "-t", "nat").Output() //nolint:gosec |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("iptables-save failed: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + var retErr error |
| 98 | + for line := range strings.SplitSeq(string(out), "\n") { |
| 99 | + fields := strings.Fields(line) |
| 100 | + if len(fields) < 2 || fields[0] != "-A" || fields[1] != "PREROUTING" { |
| 101 | + continue |
| 102 | + } |
| 103 | + if !strings.Contains(line, "-i lo") || !strings.Contains(line, "-j DNAT") { |
| 104 | + continue |
| 105 | + } |
| 106 | + args := append([]string{"-t", "nat", "-D"}, fields[1:]...) |
| 107 | + if err := ipTablesExec(args); err != nil { |
| 108 | + retErr = errors.Join(retErr, err) |
| 109 | + } |
| 110 | + } |
| 111 | + return retErr |
| 112 | +} |
0 commit comments