| // |
| 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 vnet |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "crypto/sha256" |
| 20 | "encoding/hex" |
| 21 | "fmt" |
| 22 | "net" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 |
| 26 | "github.com/vishvananda/netlink" |
| 27 | "golang.zx2c4.com/wireguard/tun" |
| 28 | ) |
| 29 |
| 30 | const ( |
| 31 | baseTunName = "utun" |
| 32 | defaultMTU = 1420 |
| 33 | ) |
| 34 |
| 35 | func openTun(_ context.Context, addr string) (tun.Device, error) { |
| 36 | name, err := findNextTunName(baseTunName) |
| 37 | if err != nil { |
| 38 | name = getFallbackTunName(baseTunName, addr) |
| 39 | } |
| 40 |
| 41 | tunDevice, err := tun.CreateTUN(name, defaultMTU) |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("failed to create TUN device '%s': %w", name, err) |
| 44 | } |
| 45 |
| 46 | actualName, err := tunDevice.Name() |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 |
| 51 | ifn, err := net.InterfaceByName(actualName) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 |
| 56 | link, err := netlink.LinkByName(actualName) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 |
| 61 | ip, cidr, err := net.ParseCIDR(addr) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | if err := netlink.AddrAdd(link, &netlink.Addr{ |
| 66 | IPNet: &net.IPNet{ |
| 67 | IP: ip, |
| 68 | Mask: cidr.Mask, |
| 69 | }, |
| 70 | }); err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 |
| 74 | if err := netlink.LinkSetUp(link); err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 |
| 78 | if err = addRoutes(ifn, cidr); err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | return tunDevice, nil |
| 82 | } |
| 83 |
| 84 | func findNextTunName(basename string) (string, error) { |
| 85 | interfaces, err := net.Interfaces() |
| 86 | if err != nil { |
| 87 | return "", fmt.Errorf("failed to get network interfaces: %w", err) |
| 88 | } |
| 89 | maxSuffix := -1 |
| 90 |
| 91 | for _, iface := range interfaces { |
| 92 | name := iface.Name |
| 93 | if strings.HasPrefix(name, basename) { |
| 94 | suffix := name[len(basename):] |
| 95 | if suffix == "" { |
| 96 | continue |
| 97 | } |
| 98 |
| 99 | numSuffix, err := strconv.Atoi(suffix) |
| 100 | if err == nil && numSuffix > maxSuffix { |
| 101 | maxSuffix = numSuffix |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 |
| 106 | nextSuffix := maxSuffix + 1 |
| 107 | name := fmt.Sprintf("%s%d", basename, nextSuffix) |
| 108 | return name, nil |
| 109 | } |
| 110 |
| 111 | func addRoutes(ifn *net.Interface, cidr *net.IPNet) error { |
| 112 | r := netlink.Route{ |
| 113 | Dst: cidr, |
| 114 | LinkIndex: ifn.Index, |
| 115 | } |
| 116 | if err := netlink.RouteReplace(&r); err != nil { |
| 117 | return fmt.Errorf("add route to %v error: %v", r.Dst, err) |
| 118 | } |
| 119 | return nil |
| 120 | } |
| 121 |
| 122 | // getFallbackTunName generates a deterministic fallback TUN device name |
| 123 | // based on the base name and the provided address string using a hash. |
| 124 | func getFallbackTunName(baseName, addr string) string { |
| 125 | hasher := sha256.New() |
| 126 | hasher.Write([]byte(addr)) |
| 127 | hashBytes := hasher.Sum(nil) |
| 128 | // Use first 4 bytes -> 8 hex chars for brevity, respecting IFNAMSIZ limit. |
| 129 | shortHash := hex.EncodeToString(hashBytes[:4]) |
| 130 | return fmt.Sprintf("%s%s", baseName, shortHash) |
| 131 | } |