2021-03-26 20:44:55 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-08-17 17:21:52 +00:00
|
|
|
//go:build ts_macext && (darwin || ios)
|
|
|
|
// +build ts_macext
|
|
|
|
// +build darwin ios
|
2021-03-26 20:44:55 +00:00
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
2021-04-06 20:38:47 +00:00
|
|
|
"errors"
|
2021-03-26 20:44:55 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/interfaces"
|
2021-06-23 04:53:43 +00:00
|
|
|
"tailscale.com/net/netns"
|
2021-03-26 20:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
initListenConfig = initListenConfigNetworkExtension
|
2021-04-06 20:38:47 +00:00
|
|
|
peerDialControlFunc = peerDialControlFuncNetworkExtension
|
2021-03-26 20:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initListenConfigNetworkExtension configures nc for listening on IP
|
|
|
|
// through the iOS/macOS Network/System Extension (Packet Tunnel
|
|
|
|
// Provider) sandbox.
|
|
|
|
func initListenConfigNetworkExtension(nc *net.ListenConfig, ip netaddr.IP, st *interfaces.State, tunIfName string) error {
|
|
|
|
tunIf, ok := st.Interface[tunIfName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("no interface with name %q", tunIfName)
|
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
return netns.SetListenConfigInterfaceIndex(nc, tunIf.Index)
|
2021-04-06 20:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func peerDialControlFuncNetworkExtension(b *LocalBackend) func(network, address string, c syscall.RawConn) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
st := b.prevIfState
|
|
|
|
pas := b.peerAPIServer
|
|
|
|
index := -1
|
|
|
|
if st != nil && pas != nil && pas.tunName != "" {
|
|
|
|
if tunIf, ok := st.Interface[pas.tunName]; ok {
|
|
|
|
index = tunIf.Index
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
var lc net.ListenConfig
|
|
|
|
netns.SetListenConfigInterfaceIndex(&lc, index)
|
2021-04-06 20:38:47 +00:00
|
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
|
|
if index == -1 {
|
|
|
|
return errors.New("failed to find TUN interface to bind to")
|
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
return lc.Control(network, address, c)
|
2021-04-06 20:38:47 +00:00
|
|
|
}
|
|
|
|
}
|