mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-22 16:34:25 +01:00
Compare commits
34 Commits
fips140
...
jay.wren-w
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d4dd26484 | ||
|
|
0a94f9f990 | ||
|
|
433c531ae4 | ||
|
|
4c0aad1b1f | ||
|
|
c8b0281736 | ||
|
|
8281b1699f | ||
|
|
0827a6f1c5 | ||
|
|
273119638d | ||
|
|
484de41b58 | ||
|
|
770147264d | ||
|
|
fa8c013b97 | ||
|
|
2710f2af06 | ||
|
|
ad6d3e6bac | ||
|
|
2b0aa74e85 | ||
|
|
b126d88963 | ||
|
|
45c1d3eab3 | ||
|
|
634181ba66 | ||
|
|
eb89839d13 | ||
|
|
fb7f0c3657 | ||
|
|
b1f53d8d25 | ||
|
|
8824eeaea2 | ||
|
|
071589f7c7 | ||
|
|
f1e992f6dd | ||
|
|
1ea5f776d7 | ||
|
|
4cdeb284ef | ||
|
|
5cccd39465 | ||
|
|
8196c22b5a | ||
|
|
65cc253c19 | ||
|
|
73cfa7b5b1 | ||
|
|
768325c9b4 | ||
|
|
932e329164 | ||
|
|
4bea299265 | ||
|
|
5cff83b282 | ||
|
|
7da79685ff |
4
.github/workflows/gofmt.yml
vendored
4
.github/workflows/gofmt.yml
vendored
@@ -16,9 +16,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Install goimports
|
- name: Install goimports
|
||||||
|
|||||||
12
.github/workflows/release.yml
vendored
12
.github/workflows/release.yml
vendored
@@ -12,9 +12,9 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -35,9 +35,9 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -68,9 +68,9 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Import certificates
|
- name: Import certificates
|
||||||
|
|||||||
4
.github/workflows/smoke-extra.yml
vendored
4
.github/workflows/smoke-extra.yml
vendored
@@ -22,9 +22,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version-file: 'go.mod'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: add hashicorp source
|
- name: add hashicorp source
|
||||||
|
|||||||
12
.github/workflows/smoke.yml
vendored
12
.github/workflows/smoke.yml
vendored
@@ -20,9 +20,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: build
|
- name: build
|
||||||
@@ -52,12 +52,4 @@ jobs:
|
|||||||
working-directory: ./.github/workflows/smoke
|
working-directory: ./.github/workflows/smoke
|
||||||
run: NAME="smoke-p256" ./smoke.sh
|
run: NAME="smoke-p256" ./smoke.sh
|
||||||
|
|
||||||
- name: setup docker image for fips140
|
|
||||||
working-directory: ./.github/workflows/smoke
|
|
||||||
run: NAME="smoke-fips140" CURVE=P256 GOFIPS140=v1.0.0 LDFLAGS=-checklinkname=0 ./build.sh
|
|
||||||
|
|
||||||
- name: run smoke-fips140
|
|
||||||
working-directory: ./.github/workflows/smoke
|
|
||||||
run: NAME="smoke-fips140" ./smoke.sh
|
|
||||||
|
|
||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
|
|||||||
20
.github/workflows/test.yml
vendored
20
.github/workflows/test.yml
vendored
@@ -20,9 +20,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -34,7 +34,7 @@ jobs:
|
|||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v8
|
uses: golangci/golangci-lint-action@v8
|
||||||
with:
|
with:
|
||||||
version: v2.1
|
version: v2.5
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: make test
|
run: make test
|
||||||
@@ -58,9 +58,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -79,9 +79,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.22'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -100,9 +100,9 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build nebula
|
- name: Build nebula
|
||||||
@@ -117,7 +117,7 @@ jobs:
|
|||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v8
|
uses: golangci/golangci-lint-action@v8
|
||||||
with:
|
with:
|
||||||
version: v2.1
|
version: v2.5
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: make test
|
run: make test
|
||||||
|
|||||||
18
Makefile
18
Makefile
@@ -121,12 +121,12 @@ bin-pkcs11: CGO_ENABLED = 1
|
|||||||
bin-pkcs11: bin
|
bin-pkcs11: bin
|
||||||
|
|
||||||
bin:
|
bin:
|
||||||
$(GOENV) go build $(BUILD_ARGS) -ldflags "$(LDFLAGS)" -o ./nebula${NEBULA_CMD_SUFFIX} ${NEBULA_CMD_PATH}
|
go build $(BUILD_ARGS) -ldflags "$(LDFLAGS)" -o ./nebula${NEBULA_CMD_SUFFIX} ${NEBULA_CMD_PATH}
|
||||||
$(GOENV) go build $(BUILD_ARGS) -ldflags "$(LDFLAGS)" -o ./nebula-cert${NEBULA_CMD_SUFFIX} ./cmd/nebula-cert
|
go build $(BUILD_ARGS) -ldflags "$(LDFLAGS)" -o ./nebula-cert${NEBULA_CMD_SUFFIX} ./cmd/nebula-cert
|
||||||
|
|
||||||
install:
|
install:
|
||||||
$(GOENV) go install $(BUILD_ARGS) -ldflags "$(LDFLAGS)" ${NEBULA_CMD_PATH}
|
go install $(BUILD_ARGS) -ldflags "$(LDFLAGS)" ${NEBULA_CMD_PATH}
|
||||||
$(GOENV) go install $(BUILD_ARGS) -ldflags "$(LDFLAGS)" ./cmd/nebula-cert
|
go install $(BUILD_ARGS) -ldflags "$(LDFLAGS)" ./cmd/nebula-cert
|
||||||
|
|
||||||
build/linux-arm-%: GOENV += GOARM=$(word 3, $(subst -, ,$*))
|
build/linux-arm-%: GOENV += GOARM=$(word 3, $(subst -, ,$*))
|
||||||
build/linux-mips-%: GOENV += GOMIPS=$(word 3, $(subst -, ,$*))
|
build/linux-mips-%: GOENV += GOMIPS=$(word 3, $(subst -, ,$*))
|
||||||
@@ -215,14 +215,6 @@ ifeq ($(words $(MAKECMDGOALS)),1)
|
|||||||
@$(MAKE) service ${.DEFAULT_GOAL} --no-print-directory
|
@$(MAKE) service ${.DEFAULT_GOAL} --no-print-directory
|
||||||
endif
|
endif
|
||||||
|
|
||||||
fips140:
|
|
||||||
@echo > $(NULL_FILE)
|
|
||||||
$(eval GOENV += GOFIPS140=v1.0.0)
|
|
||||||
$(eval LDFLAGS += -checklinkname=0)
|
|
||||||
ifeq ($(words $(MAKECMDGOALS)),1)
|
|
||||||
@$(MAKE) fips140 ${.DEFAULT_GOAL} --no-print-directory
|
|
||||||
endif
|
|
||||||
|
|
||||||
bin-docker: bin build/linux-amd64/nebula build/linux-amd64/nebula-cert
|
bin-docker: bin build/linux-amd64/nebula build/linux-amd64/nebula-cert
|
||||||
|
|
||||||
smoke-docker: bin-docker
|
smoke-docker: bin-docker
|
||||||
@@ -244,5 +236,5 @@ smoke-vagrant/%: bin-docker build/%/nebula
|
|||||||
cd .github/workflows/smoke/ && ./smoke-vagrant.sh $*
|
cd .github/workflows/smoke/ && ./smoke-vagrant.sh $*
|
||||||
|
|
||||||
.FORCE:
|
.FORCE:
|
||||||
.PHONY: bench bench-cpu bench-cpu-long bin build-test-mobile e2e e2ev e2evv e2evvv e2evvvv fips140 proto release service smoke-docker smoke-docker-race test test-cov-html smoke-vagrant/%
|
.PHONY: bench bench-cpu bench-cpu-long bin build-test-mobile e2e e2ev e2evv e2evvv e2evvvv proto release service smoke-docker smoke-docker-race test test-cov-html smoke-vagrant/%
|
||||||
.DEFAULT_GOAL := bin
|
.DEFAULT_GOAL := bin
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -143,24 +143,17 @@ To build nebula for a specific platform (ex, Windows):
|
|||||||
|
|
||||||
See the [Makefile](Makefile) for more details on build targets
|
See the [Makefile](Makefile) for more details on build targets
|
||||||
|
|
||||||
## Curve P256, BoringCrypto and FIPS 140-3 mode
|
## Curve P256 and BoringCrypto
|
||||||
|
|
||||||
The default curve used for cryptographic handshakes and signatures is Curve25519. This is the recommended setting for most users. If your deployment has certain compliance requirements, you have the option of creating your CA using `nebula-cert ca -curve P256` to use NIST Curve P256. The CA will then sign certificates using ECDSA P256, and any hosts using these certificates will use P256 for ECDH handshakes.
|
The default curve used for cryptographic handshakes and signatures is Curve25519. This is the recommended setting for most users. If your deployment has certain compliance requirements, you have the option of creating your CA using `nebula-cert ca -curve P256` to use NIST Curve P256. The CA will then sign certificates using ECDSA P256, and any hosts using these certificates will use P256 for ECDH handshakes.
|
||||||
|
|
||||||
Nebula can be built using the [BoringCrypto GOEXPERIMENT](https://github.com/golang/go/blob/go1.20/src/crypto/internal/boring/README.md) by running either of the following make targets:
|
In addition, Nebula can be built using the [BoringCrypto GOEXPERIMENT](https://github.com/golang/go/blob/go1.20/src/crypto/internal/boring/README.md) by running either of the following make targets:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
make bin-boringcrypto
|
make bin-boringcrypto
|
||||||
make release-boringcrypto
|
make release-boringcrypto
|
||||||
```
|
```
|
||||||
|
|
||||||
Nebula can also be built using the [FIPS 140-3](https://go.dev/doc/security/fips140) mode of Go by running either of the following make targets:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
make fips140
|
|
||||||
make fips140 release
|
|
||||||
```
|
|
||||||
|
|
||||||
This is not the recommended default deployment, but may be useful based on your compliance requirements.
|
This is not the recommended default deployment, but may be useful based on your compliance requirements.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|||||||
@@ -84,16 +84,11 @@ func NewCalculatedRemotesFromConfig(c *config.C, k string) (*bart.Table[[]*calcu
|
|||||||
|
|
||||||
calculatedRemotes := new(bart.Table[[]*calculatedRemote])
|
calculatedRemotes := new(bart.Table[[]*calculatedRemote])
|
||||||
|
|
||||||
rawMap, ok := value.(map[any]any)
|
rawMap, ok := value.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
|
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
|
||||||
}
|
}
|
||||||
for rawKey, rawValue := range rawMap {
|
for rawCIDR, rawValue := range rawMap {
|
||||||
rawCIDR, ok := rawKey.(string)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
cidr, err := netip.ParsePrefix(rawCIDR)
|
cidr, err := netip.ParsePrefix(rawCIDR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("config `%s` has invalid CIDR: %s", k, rawCIDR)
|
return nil, fmt.Errorf("config `%s` has invalid CIDR: %s", k, rawCIDR)
|
||||||
@@ -129,7 +124,7 @@ func newCalculatedRemotesListFromConfig(cidr netip.Prefix, raw any) ([]*calculat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newCalculatedRemotesEntryFromConfig(cidr netip.Prefix, raw any) (*calculatedRemote, error) {
|
func newCalculatedRemotesEntryFromConfig(cidr netip.Prefix, raw any) (*calculatedRemote, error) {
|
||||||
rawMap, ok := raw.(map[any]any)
|
rawMap, ok := raw.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid type: %T", raw)
|
return nil, fmt.Errorf("invalid type: %T", raw)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ type Certificate interface {
|
|||||||
// PublicKey is the raw bytes to be used in asymmetric cryptographic operations.
|
// PublicKey is the raw bytes to be used in asymmetric cryptographic operations.
|
||||||
PublicKey() []byte
|
PublicKey() []byte
|
||||||
|
|
||||||
|
// MarshalPublicKeyPEM is the value of PublicKey marshalled to PEM
|
||||||
|
MarshalPublicKeyPEM() []byte
|
||||||
|
|
||||||
// Curve identifies which curve was used for the PublicKey and Signature.
|
// Curve identifies which curve was used for the PublicKey and Signature.
|
||||||
Curve() Curve
|
Curve() Curve
|
||||||
|
|
||||||
@@ -135,8 +138,7 @@ func Recombine(v Version, rawCertBytes, publicKey []byte, curve Curve) (Certific
|
|||||||
case Version2:
|
case Version2:
|
||||||
c, err = unmarshalCertificateV2(rawCertBytes, publicKey, curve)
|
c, err = unmarshalCertificateV2(rawCertBytes, publicKey, curve)
|
||||||
default:
|
default:
|
||||||
//TODO: CERT-V2 make a static var
|
return nil, ErrUnknownVersion
|
||||||
return nil, fmt.Errorf("unknown certificate version %d", v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -83,6 +83,10 @@ func (c *certificateV1) PublicKey() []byte {
|
|||||||
return c.details.publicKey
|
return c.details.publicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *certificateV1) MarshalPublicKeyPEM() []byte {
|
||||||
|
return marshalCertPublicKeyToPEM(c)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *certificateV1) Signature() []byte {
|
func (c *certificateV1) Signature() []byte {
|
||||||
return c.signature
|
return c.signature
|
||||||
}
|
}
|
||||||
@@ -110,8 +114,10 @@ func (c *certificateV1) CheckSignature(key []byte) bool {
|
|||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
return ed25519.Verify(key, b, c.signature)
|
return ed25519.Verify(key, b, c.signature)
|
||||||
case Curve_P256:
|
case Curve_P256:
|
||||||
x, y := elliptic.Unmarshal(elliptic.P256(), key)
|
pubKey, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), key)
|
||||||
pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
hashed := sha256.Sum256(b)
|
hashed := sha256.Sum256(b)
|
||||||
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cert
|
package cert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCertificateV1_Marshal(t *testing.T) {
|
func TestCertificateV1_Marshal(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
||||||
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
||||||
pubKey := []byte("1234567890abcedfghij1234567890ab")
|
pubKey := []byte("1234567890abcedfghij1234567890ab")
|
||||||
@@ -60,6 +62,58 @@ func TestCertificateV1_Marshal(t *testing.T) {
|
|||||||
assert.Equal(t, nc.Groups(), nc2.Groups())
|
assert.Equal(t, nc.Groups(), nc2.Groups())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCertificateV1_PublicKeyPem(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
||||||
|
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
||||||
|
pubKey := ed25519.PublicKey("1234567890abcedfghij1234567890ab")
|
||||||
|
|
||||||
|
nc := certificateV1{
|
||||||
|
details: detailsV1{
|
||||||
|
name: "testing",
|
||||||
|
networks: []netip.Prefix{},
|
||||||
|
unsafeNetworks: []netip.Prefix{},
|
||||||
|
groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||||
|
notBefore: before,
|
||||||
|
notAfter: after,
|
||||||
|
publicKey: pubKey,
|
||||||
|
isCA: false,
|
||||||
|
issuer: "1234567890abcedfghij1234567890ab",
|
||||||
|
},
|
||||||
|
signature: []byte("1234567890abcedfghij1234567890ab"),
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, Version1, nc.Version())
|
||||||
|
assert.Equal(t, Curve_CURVE25519, nc.Curve())
|
||||||
|
pubPem := "-----BEGIN NEBULA X25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA X25519 PUBLIC KEY-----\n"
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem)
|
||||||
|
assert.False(t, nc.IsCA())
|
||||||
|
|
||||||
|
nc.details.isCA = true
|
||||||
|
assert.Equal(t, Curve_CURVE25519, nc.Curve())
|
||||||
|
pubPem = "-----BEGIN NEBULA ED25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA ED25519 PUBLIC KEY-----\n"
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem)
|
||||||
|
assert.True(t, nc.IsCA())
|
||||||
|
|
||||||
|
pubP256KeyPem := []byte(`-----BEGIN NEBULA P256 PUBLIC KEY-----
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
|
-----END NEBULA P256 PUBLIC KEY-----
|
||||||
|
`)
|
||||||
|
pubP256Key, _, _, err := UnmarshalPublicKeyFromPEM(pubP256KeyPem)
|
||||||
|
require.NoError(t, err)
|
||||||
|
nc.details.curve = Curve_P256
|
||||||
|
nc.details.publicKey = pubP256Key
|
||||||
|
assert.Equal(t, Curve_P256, nc.Curve())
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPem))
|
||||||
|
assert.True(t, nc.IsCA())
|
||||||
|
|
||||||
|
nc.details.isCA = false
|
||||||
|
assert.Equal(t, Curve_P256, nc.Curve())
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPem))
|
||||||
|
assert.False(t, nc.IsCA())
|
||||||
|
}
|
||||||
|
|
||||||
func TestCertificateV1_Expired(t *testing.T) {
|
func TestCertificateV1_Expired(t *testing.T) {
|
||||||
nc := certificateV1{
|
nc := certificateV1{
|
||||||
details: detailsV1{
|
details: detailsV1{
|
||||||
|
|||||||
@@ -114,6 +114,10 @@ func (c *certificateV2) PublicKey() []byte {
|
|||||||
return c.publicKey
|
return c.publicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *certificateV2) MarshalPublicKeyPEM() []byte {
|
||||||
|
return marshalCertPublicKeyToPEM(c)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *certificateV2) Signature() []byte {
|
func (c *certificateV2) Signature() []byte {
|
||||||
return c.signature
|
return c.signature
|
||||||
}
|
}
|
||||||
@@ -149,8 +153,10 @@ func (c *certificateV2) CheckSignature(key []byte) bool {
|
|||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
return ed25519.Verify(key, b, c.signature)
|
return ed25519.Verify(key, b, c.signature)
|
||||||
case Curve_P256:
|
case Curve_P256:
|
||||||
x, y := elliptic.Unmarshal(elliptic.P256(), key)
|
pubKey, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), key)
|
||||||
pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
hashed := sha256.Sum256(b)
|
hashed := sha256.Sum256(b)
|
||||||
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCertificateV2_Marshal(t *testing.T) {
|
func TestCertificateV2_Marshal(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
||||||
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
||||||
pubKey := []byte("1234567890abcedfghij1234567890ab")
|
pubKey := []byte("1234567890abcedfghij1234567890ab")
|
||||||
@@ -75,6 +76,58 @@ func TestCertificateV2_Marshal(t *testing.T) {
|
|||||||
assert.Equal(t, nc.Groups(), nc2.Groups())
|
assert.Equal(t, nc.Groups(), nc2.Groups())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCertificateV2_PublicKeyPem(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
before := time.Now().Add(time.Second * -60).Round(time.Second)
|
||||||
|
after := time.Now().Add(time.Second * 60).Round(time.Second)
|
||||||
|
pubKey := ed25519.PublicKey("1234567890abcedfghij1234567890ab")
|
||||||
|
|
||||||
|
nc := certificateV2{
|
||||||
|
details: detailsV2{
|
||||||
|
name: "testing",
|
||||||
|
networks: []netip.Prefix{},
|
||||||
|
unsafeNetworks: []netip.Prefix{},
|
||||||
|
groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||||
|
notBefore: before,
|
||||||
|
notAfter: after,
|
||||||
|
isCA: false,
|
||||||
|
issuer: "1234567890abcedfghij1234567890ab",
|
||||||
|
},
|
||||||
|
publicKey: pubKey,
|
||||||
|
signature: []byte("1234567890abcedfghij1234567890ab"),
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, Version2, nc.Version())
|
||||||
|
assert.Equal(t, Curve_CURVE25519, nc.Curve())
|
||||||
|
pubPem := "-----BEGIN NEBULA X25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA X25519 PUBLIC KEY-----\n"
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem)
|
||||||
|
assert.False(t, nc.IsCA())
|
||||||
|
|
||||||
|
nc.details.isCA = true
|
||||||
|
assert.Equal(t, Curve_CURVE25519, nc.Curve())
|
||||||
|
pubPem = "-----BEGIN NEBULA ED25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA ED25519 PUBLIC KEY-----\n"
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem)
|
||||||
|
assert.True(t, nc.IsCA())
|
||||||
|
|
||||||
|
pubP256KeyPem := []byte(`-----BEGIN NEBULA P256 PUBLIC KEY-----
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
|
-----END NEBULA P256 PUBLIC KEY-----
|
||||||
|
`)
|
||||||
|
pubP256Key, _, _, err := UnmarshalPublicKeyFromPEM(pubP256KeyPem)
|
||||||
|
require.NoError(t, err)
|
||||||
|
nc.curve = Curve_P256
|
||||||
|
nc.publicKey = pubP256Key
|
||||||
|
assert.Equal(t, Curve_P256, nc.Curve())
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPem))
|
||||||
|
assert.True(t, nc.IsCA())
|
||||||
|
|
||||||
|
nc.details.isCA = false
|
||||||
|
assert.Equal(t, Curve_P256, nc.Curve())
|
||||||
|
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPem))
|
||||||
|
assert.False(t, nc.IsCA())
|
||||||
|
}
|
||||||
|
|
||||||
func TestCertificateV2_Expired(t *testing.T) {
|
func TestCertificateV2_Expired(t *testing.T) {
|
||||||
nc := certificateV2{
|
nc := certificateV2{
|
||||||
details: detailsV2{
|
details: detailsV2{
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ var (
|
|||||||
ErrPublicPrivateKeyMismatch = errors.New("public key and private key are not a pair")
|
ErrPublicPrivateKeyMismatch = errors.New("public key and private key are not a pair")
|
||||||
ErrPrivateKeyEncrypted = errors.New("private key must be decrypted")
|
ErrPrivateKeyEncrypted = errors.New("private key must be decrypted")
|
||||||
ErrCaNotFound = errors.New("could not find ca for the certificate")
|
ErrCaNotFound = errors.New("could not find ca for the certificate")
|
||||||
|
ErrUnknownVersion = errors.New("certificate version unrecognized")
|
||||||
|
|
||||||
ErrInvalidPEMBlock = errors.New("input did not contain a valid PEM encoded block")
|
ErrInvalidPEMBlock = errors.New("input did not contain a valid PEM encoded block")
|
||||||
ErrInvalidPEMCertificateBanner = errors.New("bytes did not contain a proper certificate banner")
|
ErrInvalidPEMCertificateBanner = errors.New("bytes did not contain a proper certificate banner")
|
||||||
|
|||||||
52
cert/pem.go
52
cert/pem.go
@@ -7,19 +7,26 @@ import (
|
|||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const ( //cert banners
|
||||||
CertificateBanner = "NEBULA CERTIFICATE"
|
CertificateBanner = "NEBULA CERTIFICATE"
|
||||||
CertificateV2Banner = "NEBULA CERTIFICATE V2"
|
CertificateV2Banner = "NEBULA CERTIFICATE V2"
|
||||||
X25519PrivateKeyBanner = "NEBULA X25519 PRIVATE KEY"
|
)
|
||||||
X25519PublicKeyBanner = "NEBULA X25519 PUBLIC KEY"
|
|
||||||
EncryptedEd25519PrivateKeyBanner = "NEBULA ED25519 ENCRYPTED PRIVATE KEY"
|
|
||||||
Ed25519PrivateKeyBanner = "NEBULA ED25519 PRIVATE KEY"
|
|
||||||
Ed25519PublicKeyBanner = "NEBULA ED25519 PUBLIC KEY"
|
|
||||||
|
|
||||||
P256PrivateKeyBanner = "NEBULA P256 PRIVATE KEY"
|
const ( //key-agreement-key banners
|
||||||
P256PublicKeyBanner = "NEBULA P256 PUBLIC KEY"
|
X25519PrivateKeyBanner = "NEBULA X25519 PRIVATE KEY"
|
||||||
|
X25519PublicKeyBanner = "NEBULA X25519 PUBLIC KEY"
|
||||||
|
P256PrivateKeyBanner = "NEBULA P256 PRIVATE KEY"
|
||||||
|
P256PublicKeyBanner = "NEBULA P256 PUBLIC KEY"
|
||||||
|
)
|
||||||
|
|
||||||
|
/* including "ECDSA" in the P256 banners is a clue that these keys should be used only for signing */
|
||||||
|
const ( //signing key banners
|
||||||
EncryptedECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 ENCRYPTED PRIVATE KEY"
|
EncryptedECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 ENCRYPTED PRIVATE KEY"
|
||||||
ECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 PRIVATE KEY"
|
ECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 PRIVATE KEY"
|
||||||
|
ECDSAP256PublicKeyBanner = "NEBULA ECDSA P256 PUBLIC KEY"
|
||||||
|
EncryptedEd25519PrivateKeyBanner = "NEBULA ED25519 ENCRYPTED PRIVATE KEY"
|
||||||
|
Ed25519PrivateKeyBanner = "NEBULA ED25519 PRIVATE KEY"
|
||||||
|
Ed25519PublicKeyBanner = "NEBULA ED25519 PUBLIC KEY"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnmarshalCertificateFromPEM will try to unmarshal the first pem block in a byte array, returning any non consumed
|
// UnmarshalCertificateFromPEM will try to unmarshal the first pem block in a byte array, returning any non consumed
|
||||||
@@ -51,6 +58,16 @@ func UnmarshalCertificateFromPEM(b []byte) (Certificate, []byte, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func marshalCertPublicKeyToPEM(c Certificate) []byte {
|
||||||
|
if c.IsCA() {
|
||||||
|
return MarshalSigningPublicKeyToPEM(c.Curve(), c.PublicKey())
|
||||||
|
} else {
|
||||||
|
return MarshalPublicKeyToPEM(c.Curve(), c.PublicKey())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalPublicKeyToPEM returns a PEM representation of a public key used for ECDH.
|
||||||
|
// if your public key came from a certificate, prefer Certificate.PublicKeyPEM() if possible, to avoid mistakes!
|
||||||
func MarshalPublicKeyToPEM(curve Curve, b []byte) []byte {
|
func MarshalPublicKeyToPEM(curve Curve, b []byte) []byte {
|
||||||
switch curve {
|
switch curve {
|
||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
@@ -62,6 +79,19 @@ func MarshalPublicKeyToPEM(curve Curve, b []byte) []byte {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalSigningPublicKeyToPEM returns a PEM representation of a public key used for signing.
|
||||||
|
// if your public key came from a certificate, prefer Certificate.PublicKeyPEM() if possible, to avoid mistakes!
|
||||||
|
func MarshalSigningPublicKeyToPEM(curve Curve, b []byte) []byte {
|
||||||
|
switch curve {
|
||||||
|
case Curve_CURVE25519:
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: Ed25519PublicKeyBanner, Bytes: b})
|
||||||
|
case Curve_P256:
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: P256PublicKeyBanner, Bytes: b})
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func UnmarshalPublicKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
func UnmarshalPublicKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
||||||
k, r := pem.Decode(b)
|
k, r := pem.Decode(b)
|
||||||
if k == nil {
|
if k == nil {
|
||||||
@@ -73,7 +103,7 @@ func UnmarshalPublicKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
|||||||
case X25519PublicKeyBanner, Ed25519PublicKeyBanner:
|
case X25519PublicKeyBanner, Ed25519PublicKeyBanner:
|
||||||
expectedLen = 32
|
expectedLen = 32
|
||||||
curve = Curve_CURVE25519
|
curve = Curve_CURVE25519
|
||||||
case P256PublicKeyBanner:
|
case P256PublicKeyBanner, ECDSAP256PublicKeyBanner:
|
||||||
// Uncompressed
|
// Uncompressed
|
||||||
expectedLen = 65
|
expectedLen = 65
|
||||||
curve = Curve_P256
|
curve = Curve_P256
|
||||||
|
|||||||
@@ -177,6 +177,7 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalPublicKeyFromPEM(t *testing.T) {
|
func TestUnmarshalPublicKeyFromPEM(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
pubKey := []byte(`# A good key
|
pubKey := []byte(`# A good key
|
||||||
-----BEGIN NEBULA ED25519 PUBLIC KEY-----
|
-----BEGIN NEBULA ED25519 PUBLIC KEY-----
|
||||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
@@ -230,6 +231,7 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalX25519PublicKey(t *testing.T) {
|
func TestUnmarshalX25519PublicKey(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
pubKey := []byte(`# A good key
|
pubKey := []byte(`# A good key
|
||||||
-----BEGIN NEBULA X25519 PUBLIC KEY-----
|
-----BEGIN NEBULA X25519 PUBLIC KEY-----
|
||||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
@@ -240,6 +242,12 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|||||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||||
AAAAAAAAAAAAAAAAAAAAAAA=
|
AAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
-----END NEBULA P256 PUBLIC KEY-----
|
-----END NEBULA P256 PUBLIC KEY-----
|
||||||
|
`)
|
||||||
|
oldPubP256Key := []byte(`# A good key
|
||||||
|
-----BEGIN NEBULA ECDSA P256 PUBLIC KEY-----
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
|
-----END NEBULA ECDSA P256 PUBLIC KEY-----
|
||||||
`)
|
`)
|
||||||
shortKey := []byte(`# A short key
|
shortKey := []byte(`# A short key
|
||||||
-----BEGIN NEBULA X25519 PUBLIC KEY-----
|
-----BEGIN NEBULA X25519 PUBLIC KEY-----
|
||||||
@@ -256,15 +264,22 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|||||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
-END NEBULA X25519 PUBLIC KEY-----`)
|
-END NEBULA X25519 PUBLIC KEY-----`)
|
||||||
|
|
||||||
keyBundle := appendByteSlices(pubKey, pubP256Key, shortKey, invalidBanner, invalidPem)
|
keyBundle := appendByteSlices(pubKey, pubP256Key, oldPubP256Key, shortKey, invalidBanner, invalidPem)
|
||||||
|
|
||||||
// Success test case
|
// Success test case
|
||||||
k, rest, curve, err := UnmarshalPublicKeyFromPEM(keyBundle)
|
k, rest, curve, err := UnmarshalPublicKeyFromPEM(keyBundle)
|
||||||
assert.Len(t, k, 32)
|
assert.Len(t, k, 32)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, rest, appendByteSlices(pubP256Key, shortKey, invalidBanner, invalidPem))
|
assert.Equal(t, rest, appendByteSlices(pubP256Key, oldPubP256Key, shortKey, invalidBanner, invalidPem))
|
||||||
assert.Equal(t, Curve_CURVE25519, curve)
|
assert.Equal(t, Curve_CURVE25519, curve)
|
||||||
|
|
||||||
|
// Success test case
|
||||||
|
k, rest, curve, err = UnmarshalPublicKeyFromPEM(rest)
|
||||||
|
assert.Len(t, k, 65)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, rest, appendByteSlices(oldPubP256Key, shortKey, invalidBanner, invalidPem))
|
||||||
|
assert.Equal(t, Curve_P256, curve)
|
||||||
|
|
||||||
// Success test case
|
// Success test case
|
||||||
k, rest, curve, err = UnmarshalPublicKeyFromPEM(rest)
|
k, rest, curve, err = UnmarshalPublicKeyFromPEM(rest)
|
||||||
assert.Len(t, k, 65)
|
assert.Len(t, k, 65)
|
||||||
|
|||||||
12
cert/sign.go
12
cert/sign.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -55,15 +54,10 @@ func (t *TBSCertificate) Sign(signer Certificate, curve Curve, key []byte) (Cert
|
|||||||
}
|
}
|
||||||
return t.SignWith(signer, curve, sp)
|
return t.SignWith(signer, curve, sp)
|
||||||
case Curve_P256:
|
case Curve_P256:
|
||||||
pk := &ecdsa.PrivateKey{
|
pk, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), key)
|
||||||
PublicKey: ecdsa.PublicKey{
|
if err != nil {
|
||||||
Curve: elliptic.P256(),
|
return nil, err
|
||||||
},
|
|
||||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L95
|
|
||||||
D: new(big.Int).SetBytes(key),
|
|
||||||
}
|
}
|
||||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L119
|
|
||||||
pk.X, pk.Y = pk.Curve.ScalarBaseMult(key)
|
|
||||||
sp := func(certBytes []byte) ([]byte, error) {
|
sp := func(certBytes []byte) ([]byte, error) {
|
||||||
// We need to hash first for ECDSA
|
// We need to hash first for ECDSA
|
||||||
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ func (cm *connectionManager) makeTrafficDecision(localIndex uint32, now time.Tim
|
|||||||
decision = tryRehandshake
|
decision = tryRehandshake
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if cm.shouldSwapPrimary(hostinfo, primary) {
|
if cm.shouldSwapPrimary(hostinfo) {
|
||||||
decision = swapPrimary
|
decision = swapPrimary
|
||||||
} else {
|
} else {
|
||||||
// migrate the relays to the primary, if in use.
|
// migrate the relays to the primary, if in use.
|
||||||
@@ -447,7 +447,7 @@ func (cm *connectionManager) isInactive(hostinfo *HostInfo, now time.Time) (time
|
|||||||
return inactiveDuration, true
|
return inactiveDuration, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *connectionManager) shouldSwapPrimary(current, primary *HostInfo) bool {
|
func (cm *connectionManager) shouldSwapPrimary(current *HostInfo) bool {
|
||||||
// The primary tunnel is the most recent handshake to complete locally and should work entirely fine.
|
// The primary tunnel is the most recent handshake to complete locally and should work entirely fine.
|
||||||
// If we are here then we have multiple tunnels for a host pair and neither side believes the same tunnel is primary.
|
// If we are here then we have multiple tunnels for a host pair and neither side believes the same tunnel is primary.
|
||||||
// Let's sort this out.
|
// Let's sort this out.
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func newTestLighthouse() *LightHouse {
|
|||||||
addrMap: map[netip.Addr]*RemoteList{},
|
addrMap: map[netip.Addr]*RemoteList{},
|
||||||
queryChan: make(chan netip.Addr, 10),
|
queryChan: make(chan netip.Addr, 10),
|
||||||
}
|
}
|
||||||
lighthouses := map[netip.Addr]struct{}{}
|
lighthouses := []netip.Addr{}
|
||||||
staticList := map[netip.Addr]struct{}{}
|
staticList := map[netip.Addr]struct{}{}
|
||||||
|
|
||||||
lh.lighthouses.Store(&lighthouses)
|
lh.lighthouses.Store(&lighthouses)
|
||||||
@@ -446,6 +446,10 @@ func (d *dummyCert) PublicKey() []byte {
|
|||||||
return d.publicKey
|
return d.publicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dummyCert) MarshalPublicKeyPEM() []byte {
|
||||||
|
return cert.MarshalPublicKeyToPEM(d.curve, d.publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dummyCert) Signature() []byte {
|
func (d *dummyCert) Signature() []byte {
|
||||||
return d.signature
|
return d.signature
|
||||||
}
|
}
|
||||||
|
|||||||
241
firewall_test.go
241
firewall_test.go
@@ -68,6 +68,9 @@ func TestFirewall_AddRule(t *testing.T) {
|
|||||||
ti, err := netip.ParsePrefix("1.2.3.4/32")
|
ti, err := netip.ParsePrefix("1.2.3.4/32")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ti6, err := netip.ParsePrefix("fd12::34/128")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
||||||
// An empty rule is any
|
// An empty rule is any
|
||||||
assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
|
assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
|
||||||
@@ -92,12 +95,24 @@ func TestFirewall_AddRule(t *testing.T) {
|
|||||||
_, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
|
_, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
|
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6, netip.Prefix{}, "", ""))
|
||||||
|
assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
|
||||||
|
_, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6)
|
||||||
|
assert.True(t, ok)
|
||||||
|
|
||||||
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, ti, "", ""))
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, ti, "", ""))
|
||||||
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
|
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
|
||||||
_, ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
|
_, ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
|
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, ti6, "", ""))
|
||||||
|
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
|
||||||
|
_, ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6)
|
||||||
|
assert.True(t, ok)
|
||||||
|
|
||||||
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "ca-name", ""))
|
require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "ca-name", ""))
|
||||||
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
||||||
@@ -117,6 +132,13 @@ func TestFirewall_AddRule(t *testing.T) {
|
|||||||
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, netip.Prefix{}, "", ""))
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, netip.Prefix{}, "", ""))
|
||||||
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
||||||
|
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
|
anyIp6, err := netip.ParsePrefix("::/0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6, netip.Prefix{}, "", ""))
|
||||||
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
||||||
|
|
||||||
// Test error conditions
|
// Test error conditions
|
||||||
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
||||||
require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
||||||
@@ -199,6 +221,82 @@ func TestFirewall_Drop(t *testing.T) {
|
|||||||
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFirewall_DropV6(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
ob := &bytes.Buffer{}
|
||||||
|
l.SetOutput(ob)
|
||||||
|
|
||||||
|
p := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("fd12::34"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("fd12::34"),
|
||||||
|
LocalPort: 10,
|
||||||
|
RemotePort: 90,
|
||||||
|
Protocol: firewall.ProtoUDP,
|
||||||
|
Fragment: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
c := dummyCert{
|
||||||
|
name: "host1",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")},
|
||||||
|
groups: []string{"default-group"},
|
||||||
|
issuer: "signer-shasum",
|
||||||
|
}
|
||||||
|
h := HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{
|
||||||
|
peerCert: &cert.CachedCertificate{
|
||||||
|
Certificate: &c,
|
||||||
|
InvertedGroups: map[string]struct{}{"default-group": {}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")},
|
||||||
|
}
|
||||||
|
h.buildNetworks(c.networks, c.unsafeNetworks)
|
||||||
|
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
|
||||||
|
// Drop outbound
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
|
||||||
|
// Allow inbound
|
||||||
|
resetConntrack(fw)
|
||||||
|
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
||||||
|
// Allow outbound because conntrack
|
||||||
|
require.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
||||||
|
|
||||||
|
// test remote mismatch
|
||||||
|
oldRemote := p.RemoteAddr
|
||||||
|
p.RemoteAddr = netip.MustParseAddr("fd12::56")
|
||||||
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
|
||||||
|
p.RemoteAddr = oldRemote
|
||||||
|
|
||||||
|
// ensure signer doesn't get in the way of group checks
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum"))
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum-bad"))
|
||||||
|
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
|
||||||
|
|
||||||
|
// test caSha doesn't drop on match
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum-bad"))
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum"))
|
||||||
|
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
||||||
|
|
||||||
|
// ensure ca name doesn't get in the way of group checks
|
||||||
|
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good-bad", ""))
|
||||||
|
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
|
||||||
|
|
||||||
|
// test caName doesn't drop on match
|
||||||
|
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
|
||||||
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good-bad", ""))
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
||||||
|
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkFirewallTable_match(b *testing.B) {
|
func BenchmarkFirewallTable_match(b *testing.B) {
|
||||||
f := &Firewall{}
|
f := &Firewall{}
|
||||||
ft := FirewallTable{
|
ft := FirewallTable{
|
||||||
@@ -208,6 +306,10 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|||||||
pfix := netip.MustParsePrefix("172.1.1.1/32")
|
pfix := netip.MustParsePrefix("172.1.1.1/32")
|
||||||
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix, netip.Prefix{}, "", "")
|
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix, netip.Prefix{}, "", "")
|
||||||
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", netip.Prefix{}, pfix, "", "")
|
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", netip.Prefix{}, pfix, "", "")
|
||||||
|
|
||||||
|
pfix6 := netip.MustParsePrefix("fd11::11/128")
|
||||||
|
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6, netip.Prefix{}, "", "")
|
||||||
|
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", netip.Prefix{}, pfix6, "", "")
|
||||||
cp := cert.NewCAPool()
|
cp := cert.NewCAPool()
|
||||||
|
|
||||||
b.Run("fail on proto", func(b *testing.B) {
|
b.Run("fail on proto", func(b *testing.B) {
|
||||||
@@ -239,6 +341,15 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|||||||
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{},
|
||||||
|
}
|
||||||
|
ip := netip.MustParsePrefix("fd99::99/128")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
|
b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
|
||||||
c := &cert.CachedCertificate{
|
c := &cert.CachedCertificate{
|
||||||
@@ -252,6 +363,18 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|||||||
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "nope",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
|
||||||
|
},
|
||||||
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
||||||
|
}
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
|
b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
|
||||||
c := &cert.CachedCertificate{
|
c := &cert.CachedCertificate{
|
||||||
@@ -265,6 +388,18 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|||||||
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "nope",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
|
||||||
|
},
|
||||||
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
||||||
|
}
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
b.Run("pass on group on any local cidr", func(b *testing.B) {
|
b.Run("pass on group on any local cidr", func(b *testing.B) {
|
||||||
c := &cert.CachedCertificate{
|
c := &cert.CachedCertificate{
|
||||||
@@ -289,6 +424,17 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|||||||
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
b.Run("pass on group on specific local cidr6", func(b *testing.B) {
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "nope",
|
||||||
|
},
|
||||||
|
InvertedGroups: map[string]struct{}{"good-group": {}},
|
||||||
|
}
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
b.Run("pass on name", func(b *testing.B) {
|
b.Run("pass on name", func(b *testing.B) {
|
||||||
c := &cert.CachedCertificate{
|
c := &cert.CachedCertificate{
|
||||||
@@ -447,6 +593,42 @@ func TestFirewall_Drop3(t *testing.T) {
|
|||||||
require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFirewall_Drop3V6(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
ob := &bytes.Buffer{}
|
||||||
|
l.SetOutput(ob)
|
||||||
|
|
||||||
|
p := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("fd12::34"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("fd12::34"),
|
||||||
|
LocalPort: 1,
|
||||||
|
RemotePort: 1,
|
||||||
|
Protocol: firewall.ProtoUDP,
|
||||||
|
Fragment: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
network := netip.MustParsePrefix("fd12::34/120")
|
||||||
|
c := cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "host-owner",
|
||||||
|
networks: []netip.Prefix{network},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
h := HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{
|
||||||
|
peerCert: &c,
|
||||||
|
},
|
||||||
|
vpnAddrs: []netip.Addr{network.Addr()},
|
||||||
|
}
|
||||||
|
h.buildNetworks(c.Certificate.Networks(), c.Certificate.UnsafeNetworks())
|
||||||
|
|
||||||
|
// Test a remote address match
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", netip.MustParsePrefix("fd12::34/120"), netip.Prefix{}, "", ""))
|
||||||
|
require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
||||||
|
}
|
||||||
|
|
||||||
func TestFirewall_DropConntrackReload(t *testing.T) {
|
func TestFirewall_DropConntrackReload(t *testing.T) {
|
||||||
l := test.NewLogger()
|
l := test.NewLogger()
|
||||||
ob := &bytes.Buffer{}
|
ob := &bytes.Buffer{}
|
||||||
@@ -510,6 +692,50 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|||||||
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFirewall_DropIPSpoofing(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
ob := &bytes.Buffer{}
|
||||||
|
l.SetOutput(ob)
|
||||||
|
|
||||||
|
c := cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "host-owner",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
c1 := cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "host",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
|
||||||
|
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
h1 := HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{
|
||||||
|
peerCert: &c1,
|
||||||
|
},
|
||||||
|
vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()},
|
||||||
|
}
|
||||||
|
h1.buildNetworks(c1.Certificate.Networks(), c1.Certificate.UnsafeNetworks())
|
||||||
|
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
||||||
|
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
|
||||||
|
// Packet spoofed by `c1`. Note that the remote addr is not a valid one.
|
||||||
|
p := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("192.0.2.1"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("192.0.2.3"),
|
||||||
|
LocalPort: 1,
|
||||||
|
RemotePort: 1,
|
||||||
|
Protocol: firewall.ProtoUDP,
|
||||||
|
Fragment: false,
|
||||||
|
}
|
||||||
|
assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkLookup(b *testing.B) {
|
func BenchmarkLookup(b *testing.B) {
|
||||||
ml := func(m map[string]struct{}, a [][]string) {
|
ml := func(m map[string]struct{}, a [][]string) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
@@ -727,6 +953,21 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|||||||
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
||||||
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: cidr}, mf.lastCall)
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: cidr}, mf.lastCall)
|
||||||
|
|
||||||
|
// Test adding rule with cidr ipv6
|
||||||
|
cidr6 := netip.MustParsePrefix("fd00::/8")
|
||||||
|
conf = config.NewC(l)
|
||||||
|
mf = &mockFirewall{}
|
||||||
|
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}}
|
||||||
|
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
||||||
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6, localIp: netip.Prefix{}}, mf.lastCall)
|
||||||
|
|
||||||
|
// Test adding rule with local_cidr ipv6
|
||||||
|
conf = config.NewC(l)
|
||||||
|
mf = &mockFirewall{}
|
||||||
|
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}}
|
||||||
|
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
||||||
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: cidr6}, mf.lastCall)
|
||||||
|
|
||||||
// Test adding rule with ca_sha
|
// Test adding rule with ca_sha
|
||||||
conf = config.NewC(l)
|
conf = config.NewC(l)
|
||||||
mf = &mockFirewall{}
|
mf = &mockFirewall{}
|
||||||
|
|||||||
43
go.mod
43
go.mod
@@ -1,8 +1,6 @@
|
|||||||
module github.com/slackhq/nebula
|
module github.com/slackhq/nebula
|
||||||
|
|
||||||
go 1.24.0
|
go 1.25
|
||||||
|
|
||||||
toolchain go1.24.1
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.2
|
dario.cat/mergo v1.0.2
|
||||||
@@ -10,32 +8,32 @@ require (
|
|||||||
github.com/armon/go-radix v1.0.0
|
github.com/armon/go-radix v1.0.0
|
||||||
github.com/cyberdelia/go-metrics-graphite v0.0.0-20161219230853-39f87cc3b432
|
github.com/cyberdelia/go-metrics-graphite v0.0.0-20161219230853-39f87cc3b432
|
||||||
github.com/flynn/noise v1.1.0
|
github.com/flynn/noise v1.1.0
|
||||||
github.com/gaissmai/bart v0.20.4
|
github.com/gaissmai/bart v0.25.0
|
||||||
github.com/gogo/protobuf v1.3.2
|
github.com/gogo/protobuf v1.3.2
|
||||||
github.com/google/gopacket v1.1.19
|
github.com/google/gopacket v1.1.19
|
||||||
github.com/kardianos/service v1.2.2
|
github.com/kardianos/service v1.2.4
|
||||||
github.com/miekg/dns v1.1.65
|
github.com/miekg/dns v1.1.68
|
||||||
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b
|
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b
|
||||||
github.com/nbrownus/go-metrics-prometheus v0.0.0-20210712211119-974a6260965f
|
github.com/nbrownus/go-metrics-prometheus v0.0.0-20210712211119-974a6260965f
|
||||||
github.com/prometheus/client_golang v1.22.0
|
github.com/prometheus/client_golang v1.23.2
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
|
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||||
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6
|
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.11.1
|
||||||
github.com/vishvananda/netlink v1.3.1
|
github.com/vishvananda/netlink v1.3.1
|
||||||
golang.org/x/crypto v0.37.0
|
golang.org/x/crypto v0.43.0
|
||||||
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
|
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
|
||||||
golang.org/x/net v0.39.0
|
golang.org/x/net v0.45.0
|
||||||
golang.org/x/sync v0.13.0
|
golang.org/x/sync v0.17.0
|
||||||
golang.org/x/sys v0.32.0
|
golang.org/x/sys v0.37.0
|
||||||
golang.org/x/term v0.31.0
|
golang.org/x/term v0.36.0
|
||||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2
|
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b
|
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb
|
||||||
golang.zx2c4.com/wireguard/windows v0.5.3
|
golang.zx2c4.com/wireguard/windows v0.5.3
|
||||||
google.golang.org/protobuf v1.36.6
|
google.golang.org/protobuf v1.36.8
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe
|
gvisor.dev/gvisor v0.0.0-20250503011706-39ed1f5ac29c
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -45,11 +43,12 @@ require (
|
|||||||
github.com/google/btree v1.1.2 // indirect
|
github.com/google/btree v1.1.2 // indirect
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.2 // indirect
|
||||||
github.com/prometheus/common v0.62.0 // indirect
|
github.com/prometheus/common v0.66.1 // indirect
|
||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.16.1 // indirect
|
||||||
github.com/vishvananda/netns v0.0.5 // indirect
|
github.com/vishvananda/netns v0.0.5 // indirect
|
||||||
golang.org/x/mod v0.23.0 // indirect
|
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||||
golang.org/x/time v0.5.0 // indirect
|
golang.org/x/mod v0.24.0 // indirect
|
||||||
golang.org/x/tools v0.30.0 // indirect
|
golang.org/x/time v0.7.0 // indirect
|
||||||
|
golang.org/x/tools v0.33.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
81
go.sum
81
go.sum
@@ -24,8 +24,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
|
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
|
||||||
github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
|
github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
|
||||||
github.com/gaissmai/bart v0.20.4 h1:Ik47r1fy3jRVU+1eYzKSW3ho2UgBVTVnUS8O993584U=
|
github.com/gaissmai/bart v0.25.0 h1:eqiokVPqM3F94vJ0bTHXHtH91S8zkKL+bKh+BsGOsJM=
|
||||||
github.com/gaissmai/bart v0.20.4/go.mod h1:cEed+ge8dalcbpi8wtS9x9m2hn/fNJH5suhdGQOHnYk=
|
github.com/gaissmai/bart v0.25.0/go.mod h1:GREWQfTLRWz/c5FTOsIw+KkscuFkIV5t8Rp7Nd1Td5c=
|
||||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
||||||
@@ -64,8 +64,8 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/
|
|||||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||||
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
|
github.com/kardianos/service v1.2.4 h1:XNlGtZOYNx2u91urOdg/Kfmc+gfmuIo1Dd3rEi2OgBk=
|
||||||
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
|
github.com/kardianos/service v1.2.4/go.mod h1:E4V9ufUuY82F7Ztlu1eN9VXWIQxg8NoLQlmFe0MtrXc=
|
||||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||||
@@ -83,8 +83,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
github.com/miekg/dns v1.1.65 h1:0+tIPHzUW0GCge7IiK3guGP57VAw7hoPDfApjkMD1Fc=
|
github.com/miekg/dns v1.1.68 h1:jsSRkNozw7G/mnmXULynzMNIsgY2dHC8LO6U6Ij2JEA=
|
||||||
github.com/miekg/dns v1.1.65/go.mod h1:Dzw9769uoKVaLuODMDZz9M6ynFU6Em65csPuoi8G0ck=
|
github.com/miekg/dns v1.1.68/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
|
||||||
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b h1:J/AzCvg5z0Hn1rqZUJjpbzALUmkKX0Zwbc/i4fw7Sfk=
|
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b h1:J/AzCvg5z0Hn1rqZUJjpbzALUmkKX0Zwbc/i4fw7Sfk=
|
||||||
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
|
github.com/miekg/pkcs11 v1.1.2-0.20231115102856-9078ad6b9d4b/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
@@ -106,24 +106,24 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
|
|||||||
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
||||||
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
|
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
|
||||||
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
|
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
|
||||||
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
|
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||||
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
|
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
||||||
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||||
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
||||||
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
|
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
|
||||||
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
|
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
|
||||||
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
|
||||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
|
||||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||||
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||||
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
|
||||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
|
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||||
@@ -143,29 +143,33 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
|||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/vishvananda/netlink v1.3.1 h1:3AEMt62VKqz90r0tmNhog0r/PpWKmrEShJU0wJW6bV0=
|
github.com/vishvananda/netlink v1.3.1 h1:3AEMt62VKqz90r0tmNhog0r/PpWKmrEShJU0wJW6bV0=
|
||||||
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
||||||
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
||||||
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
|
||||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
|
||||||
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
|
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
|
||||||
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
|
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
|
||||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
||||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
|
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||||
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
@@ -176,8 +180,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
|
|||||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
|
||||||
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@@ -185,8 +189,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
|
|||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
|
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||||
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
@@ -197,38 +201,37 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
||||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
|
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
|
||||||
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
|
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
|
||||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
|
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
|
||||||
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
|
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg=
|
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg=
|
||||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI=
|
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
|
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb h1:whnFRlWMcXI9d+ZbWg+4sHnLp52d5yiIPUxMBSt4X9A=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
|
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb/go.mod h1:rpwXGsirqLqN2L0JDJQlwOboGHmptD5ZD6T2VmcqhTw=
|
||||||
golang.zx2c4.com/wireguard/windows v0.5.3 h1:On6j2Rpn3OEMXqBq00QEDC7bWSZrPIHKIus8eIuExIE=
|
golang.zx2c4.com/wireguard/windows v0.5.3 h1:On6j2Rpn3OEMXqBq00QEDC7bWSZrPIHKIus8eIuExIE=
|
||||||
golang.zx2c4.com/wireguard/windows v0.5.3/go.mod h1:9TEe8TJmtwyQebdFwAkEWOPr3prrtqm+REGFifP60hI=
|
golang.zx2c4.com/wireguard/windows v0.5.3/go.mod h1:9TEe8TJmtwyQebdFwAkEWOPr3prrtqm+REGFifP60hI=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
@@ -239,8 +242,8 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
|
|||||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
@@ -254,5 +257,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe h1:fre4i6mv4iBuz5lCMOzHD1rH1ljqHWSICFmZRbbgp3g=
|
gvisor.dev/gvisor v0.0.0-20250503011706-39ed1f5ac29c h1:m/r7OM+Y2Ty1sgBQ7Qb27VgIMBW8ZZhT4gLnUyDIhzI=
|
||||||
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe/go.mod h1:sxc3Uvk/vHcd3tj7/DHVBoR5wvWT/MmRq2pj7HRJnwU=
|
gvisor.dev/gvisor v0.0.0-20250503011706-39ed1f5ac29c/go.mod h1:3r5CMtNQMKIvBlrmM9xWUNamjKBYPOWyXOjmg5Kts3g=
|
||||||
|
|||||||
@@ -459,7 +459,7 @@ func ixHandshakeStage1(f *Interface, addr netip.AddrPort, via *ViaSender, packet
|
|||||||
|
|
||||||
f.connectionManager.AddTrafficWatch(hostinfo)
|
f.connectionManager.AddTrafficWatch(hostinfo)
|
||||||
|
|
||||||
hostinfo.remotes.ResetBlockedRemotes()
|
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -667,7 +667,7 @@ func ixHandshakeStage2(f *Interface, addr netip.AddrPort, via *ViaSender, hh *Ha
|
|||||||
f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
|
f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
|
||||||
}
|
}
|
||||||
|
|
||||||
hostinfo.remotes.ResetBlockedRemotes()
|
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
||||||
f.metricHandshakes.Update(duration)
|
f.metricHandshakes.Update(duration)
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
15
hostmap.go
15
hostmap.go
@@ -17,12 +17,10 @@ import (
|
|||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
// const ProbeLen = 100
|
|
||||||
const defaultPromoteEvery = 1000 // Count of packets sent before we try moving a tunnel to a preferred underlay ip address
|
const defaultPromoteEvery = 1000 // Count of packets sent before we try moving a tunnel to a preferred underlay ip address
|
||||||
const defaultReQueryEvery = 5000 // Count of packets sent before re-querying a hostinfo to the lighthouse
|
const defaultReQueryEvery = 5000 // Count of packets sent before re-querying a hostinfo to the lighthouse
|
||||||
const defaultReQueryWait = time.Minute // Minimum amount of seconds to wait before re-querying a hostinfo the lighthouse. Evaluated every ReQueryEvery
|
const defaultReQueryWait = time.Minute // Minimum amount of seconds to wait before re-querying a hostinfo the lighthouse. Evaluated every ReQueryEvery
|
||||||
const MaxRemotes = 10
|
const MaxRemotes = 10
|
||||||
const maxRecvError = 4
|
|
||||||
|
|
||||||
// MaxHostInfosPerVpnIp is the max number of hostinfos we will track for a given vpn ip
|
// MaxHostInfosPerVpnIp is the max number of hostinfos we will track for a given vpn ip
|
||||||
// 5 allows for an initial handshake and each host pair re-handshaking twice
|
// 5 allows for an initial handshake and each host pair re-handshaking twice
|
||||||
@@ -225,8 +223,7 @@ type HostInfo struct {
|
|||||||
// vpnAddrs is a list of vpn addresses assigned to this host that are within our own vpn networks
|
// vpnAddrs is a list of vpn addresses assigned to this host that are within our own vpn networks
|
||||||
// The host may have other vpn addresses that are outside our
|
// The host may have other vpn addresses that are outside our
|
||||||
// vpn networks but were removed because they are not usable
|
// vpn networks but were removed because they are not usable
|
||||||
vpnAddrs []netip.Addr
|
vpnAddrs []netip.Addr
|
||||||
recvError atomic.Uint32
|
|
||||||
|
|
||||||
// networks are both all vpn and unsafe networks assigned to this host
|
// networks are both all vpn and unsafe networks assigned to this host
|
||||||
networks *bart.Lite
|
networks *bart.Lite
|
||||||
@@ -733,13 +730,6 @@ func (i *HostInfo) SetRemoteIfPreferred(hm *HostMap, newRemote netip.AddrPort) b
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *HostInfo) RecvErrorExceeded() bool {
|
|
||||||
if i.recvError.Add(1) >= maxRecvError {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *HostInfo) buildNetworks(networks, unsafeNetworks []netip.Prefix) {
|
func (i *HostInfo) buildNetworks(networks, unsafeNetworks []netip.Prefix) {
|
||||||
if len(networks) == 1 && len(unsafeNetworks) == 0 {
|
if len(networks) == 1 && len(unsafeNetworks) == 0 {
|
||||||
// Simple case, no CIDRTree needed
|
// Simple case, no CIDRTree needed
|
||||||
@@ -748,7 +738,8 @@ func (i *HostInfo) buildNetworks(networks, unsafeNetworks []netip.Prefix) {
|
|||||||
|
|
||||||
i.networks = new(bart.Lite)
|
i.networks = new(bart.Lite)
|
||||||
for _, network := range networks {
|
for _, network := range networks {
|
||||||
i.networks.Insert(network)
|
nprefix := netip.PrefixFrom(network.Addr(), network.Addr().BitLen())
|
||||||
|
i.networks.Insert(nprefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, network := range unsafeNetworks {
|
for _, network := range unsafeNetworks {
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|||||||
// routes packets from the Nebula addr to the Nebula addr through the Nebula
|
// routes packets from the Nebula addr to the Nebula addr through the Nebula
|
||||||
// TUN device.
|
// TUN device.
|
||||||
if immediatelyForwardToSelf {
|
if immediatelyForwardToSelf {
|
||||||
_, err := f.readers[q].Write(packet)
|
if err := f.writeTun(q, packet); err != nil {
|
||||||
if err != nil {
|
|
||||||
f.l.WithError(err).Error("Failed to forward to tun")
|
f.l.WithError(err).Error("Failed to forward to tun")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,8 +90,7 @@ func (f *Interface) rejectInside(packet []byte, out []byte, q int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := f.readers[q].Write(out)
|
if err := f.writeTun(q, out); err != nil {
|
||||||
if err != nil {
|
|
||||||
f.l.WithError(err).Error("Failed to write to tun")
|
f.l.WithError(err).Error("Failed to write to tun")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
interface.go
103
interface.go
@@ -2,7 +2,6 @@ package nebula
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/fips140"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -48,6 +47,7 @@ type InterfaceConfig struct {
|
|||||||
reQueryWait time.Duration
|
reQueryWait time.Duration
|
||||||
|
|
||||||
ConntrackCacheTimeout time.Duration
|
ConntrackCacheTimeout time.Duration
|
||||||
|
batchSize int
|
||||||
l *logrus.Logger
|
l *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +85,7 @@ type Interface struct {
|
|||||||
version string
|
version string
|
||||||
|
|
||||||
conntrackCacheTimeout time.Duration
|
conntrackCacheTimeout time.Duration
|
||||||
|
batchSize int
|
||||||
|
|
||||||
writers []udp.Conn
|
writers []udp.Conn
|
||||||
readers []io.ReadWriteCloser
|
readers []io.ReadWriteCloser
|
||||||
@@ -111,6 +112,16 @@ type EncWriter interface {
|
|||||||
GetCertState() *CertState
|
GetCertState() *CertState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BatchReader is an interface for readers that support vectorized packet reading
|
||||||
|
type BatchReader interface {
|
||||||
|
BatchRead(buffers [][]byte, sizes []int) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchWriter is an interface for writers that support vectorized packet writing
|
||||||
|
type BatchWriter interface {
|
||||||
|
BatchWrite([][]byte) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
type sendRecvErrorConfig uint8
|
type sendRecvErrorConfig uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -187,6 +198,7 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
relayManager: c.relayManager,
|
relayManager: c.relayManager,
|
||||||
connectionManager: c.connectionManager,
|
connectionManager: c.connectionManager,
|
||||||
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
||||||
|
batchSize: c.batchSize,
|
||||||
|
|
||||||
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
||||||
messageMetrics: c.MessageMetrics,
|
messageMetrics: c.MessageMetrics,
|
||||||
@@ -221,7 +233,6 @@ func (f *Interface) activate() {
|
|||||||
f.l.WithField("interface", f.inside.Name()).WithField("networks", f.myVpnNetworks).
|
f.l.WithField("interface", f.inside.Name()).WithField("networks", f.myVpnNetworks).
|
||||||
WithField("build", f.version).WithField("udpAddr", addr).
|
WithField("build", f.version).WithField("udpAddr", addr).
|
||||||
WithField("boringcrypto", boringEnabled()).
|
WithField("boringcrypto", boringEnabled()).
|
||||||
WithField("fips140", fips140.Enabled()).
|
|
||||||
Info("Nebula interface is active")
|
Info("Nebula interface is active")
|
||||||
|
|
||||||
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
||||||
@@ -271,7 +282,7 @@ func (f *Interface) listenOut(i int) {
|
|||||||
plaintext := make([]byte, udp.MTU)
|
plaintext := make([]byte, udp.MTU)
|
||||||
h := &header.H{}
|
h := &header.H{}
|
||||||
fwPacket := &firewall.Packet{}
|
fwPacket := &firewall.Packet{}
|
||||||
nb := make([]byte, 12, 12)
|
nb := make([]byte, 12)
|
||||||
|
|
||||||
li.ListenOut(func(fromUdpAddr netip.AddrPort, payload []byte) {
|
li.ListenOut(func(fromUdpAddr netip.AddrPort, payload []byte) {
|
||||||
f.readOutsidePackets(fromUdpAddr, nil, plaintext[:0], payload, h, fwPacket, lhh, nb, i, ctCache.Get(f.l))
|
f.readOutsidePackets(fromUdpAddr, nil, plaintext[:0], payload, h, fwPacket, lhh, nb, i, ctCache.Get(f.l))
|
||||||
@@ -281,6 +292,16 @@ func (f *Interface) listenOut(i int) {
|
|||||||
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
|
|
||||||
|
// Check if reader supports batch operations
|
||||||
|
if batchReader, ok := reader.(BatchReader); ok {
|
||||||
|
err := f.listenInBatch(batchReader, i)
|
||||||
|
if err != nil {
|
||||||
|
f.l.WithError(err).Error("Fatal error in batch packet reader, exiting goroutine")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to single-packet mode
|
||||||
packet := make([]byte, mtu)
|
packet := make([]byte, mtu)
|
||||||
out := make([]byte, mtu)
|
out := make([]byte, mtu)
|
||||||
fwPacket := &firewall.Packet{}
|
fwPacket := &firewall.Packet{}
|
||||||
@@ -295,15 +316,85 @@ func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f.l.WithError(err).Error("Error while reading outbound packet")
|
f.l.WithError(err).Error("Fatal error while reading outbound packet, exiting goroutine")
|
||||||
// This only seems to happen when something fatal happens to the fd, so exit.
|
return
|
||||||
os.Exit(2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i, conntrackCache.Get(f.l))
|
f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i, conntrackCache.Get(f.l))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listenInBatch handles vectorized packet reading for improved performance
|
||||||
|
func (f *Interface) listenInBatch(reader BatchReader, i int) error {
|
||||||
|
// Allocate per-packet state and buffers for batch reading
|
||||||
|
batchSize := f.batchSize
|
||||||
|
if batchSize <= 0 {
|
||||||
|
batchSize = 64 // Fallback to default if not configured
|
||||||
|
}
|
||||||
|
fwPackets := make([]*firewall.Packet, batchSize)
|
||||||
|
outBuffers := make([][]byte, batchSize)
|
||||||
|
nbBuffers := make([][]byte, batchSize)
|
||||||
|
packets := make([][]byte, batchSize)
|
||||||
|
sizes := make([]int, batchSize)
|
||||||
|
|
||||||
|
for j := 0; j < batchSize; j++ {
|
||||||
|
fwPackets[j] = &firewall.Packet{}
|
||||||
|
outBuffers[j] = make([]byte, mtu)
|
||||||
|
nbBuffers[j] = make([]byte, 12)
|
||||||
|
packets[j] = make([]byte, mtu)
|
||||||
|
}
|
||||||
|
|
||||||
|
conntrackCache := firewall.NewConntrackCacheTicker(f.conntrackCacheTimeout)
|
||||||
|
|
||||||
|
for {
|
||||||
|
n, err := reader.BatchRead(packets, sizes)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, os.ErrClosed) && f.closed.Load() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("error while batch reading outbound packets: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process each packet in the batch
|
||||||
|
cache := conntrackCache.Get(f.l)
|
||||||
|
for idx := 0; idx < n; idx++ {
|
||||||
|
if sizes[idx] > 0 {
|
||||||
|
// Use modulo to reuse fw packet state if batch is larger than our pre-allocated state
|
||||||
|
stateIdx := idx % len(fwPackets)
|
||||||
|
f.consumeInsidePacket(packets[idx][:sizes[idx]], fwPackets[stateIdx], nbBuffers[stateIdx], outBuffers[stateIdx], i, cache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeTunBatch attempts to write multiple packets to the TUN device using batch operations if supported
|
||||||
|
func (f *Interface) writeTunBatch(q int, packets [][]byte) error {
|
||||||
|
if len(packets) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the reader/writer supports batch operations
|
||||||
|
if batchWriter, ok := f.readers[q].(BatchWriter); ok {
|
||||||
|
_, err := batchWriter.BatchWrite(packets)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to writing packets individually
|
||||||
|
for _, packet := range packets {
|
||||||
|
if _, err := f.readers[q].Write(packet); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeTun writes a single packet to the TUN device
|
||||||
|
func (f *Interface) writeTun(q int, packet []byte) error {
|
||||||
|
_, err := f.readers[q].Write(packet)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
||||||
c.RegisterReloadCallback(f.reloadFirewall)
|
c.RegisterReloadCallback(f.reloadFirewall)
|
||||||
c.RegisterReloadCallback(f.reloadSendRecvError)
|
c.RegisterReloadCallback(f.reloadSendRecvError)
|
||||||
|
|||||||
237
lighthouse.go
237
lighthouse.go
@@ -24,6 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var ErrHostNotKnown = errors.New("host not known")
|
var ErrHostNotKnown = errors.New("host not known")
|
||||||
|
var ErrBadDetailsVpnAddr = errors.New("invalid packet, malformed detailsVpnAddr")
|
||||||
|
|
||||||
type LightHouse struct {
|
type LightHouse struct {
|
||||||
//TODO: We need a timer wheel to kick out vpnAddrs that haven't reported in a long time
|
//TODO: We need a timer wheel to kick out vpnAddrs that haven't reported in a long time
|
||||||
@@ -56,7 +57,7 @@ type LightHouse struct {
|
|||||||
// staticList exists to avoid having a bool in each addrMap entry
|
// staticList exists to avoid having a bool in each addrMap entry
|
||||||
// since static should be rare
|
// since static should be rare
|
||||||
staticList atomic.Pointer[map[netip.Addr]struct{}]
|
staticList atomic.Pointer[map[netip.Addr]struct{}]
|
||||||
lighthouses atomic.Pointer[map[netip.Addr]struct{}]
|
lighthouses atomic.Pointer[[]netip.Addr]
|
||||||
|
|
||||||
interval atomic.Int64
|
interval atomic.Int64
|
||||||
updateCancel context.CancelFunc
|
updateCancel context.CancelFunc
|
||||||
@@ -107,7 +108,7 @@ func NewLightHouseFromConfig(ctx context.Context, l *logrus.Logger, c *config.C,
|
|||||||
queryChan: make(chan netip.Addr, c.GetUint32("handshakes.query_buffer", 64)),
|
queryChan: make(chan netip.Addr, c.GetUint32("handshakes.query_buffer", 64)),
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
lighthouses := make(map[netip.Addr]struct{})
|
lighthouses := make([]netip.Addr, 0)
|
||||||
h.lighthouses.Store(&lighthouses)
|
h.lighthouses.Store(&lighthouses)
|
||||||
staticList := make(map[netip.Addr]struct{})
|
staticList := make(map[netip.Addr]struct{})
|
||||||
h.staticList.Store(&staticList)
|
h.staticList.Store(&staticList)
|
||||||
@@ -143,7 +144,7 @@ func (lh *LightHouse) GetStaticHostList() map[netip.Addr]struct{} {
|
|||||||
return *lh.staticList.Load()
|
return *lh.staticList.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) GetLighthouses() map[netip.Addr]struct{} {
|
func (lh *LightHouse) GetLighthouses() []netip.Addr {
|
||||||
return *lh.lighthouses.Load()
|
return *lh.lighthouses.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,13 +307,12 @@ func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if initial || c.HasChanged("lighthouse.hosts") {
|
if initial || c.HasChanged("lighthouse.hosts") {
|
||||||
lhMap := make(map[netip.Addr]struct{})
|
lhList, err := lh.parseLighthouses(c)
|
||||||
err := lh.parseLighthouses(c, lhMap)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
lh.lighthouses.Store(&lhMap)
|
lh.lighthouses.Store(&lhList)
|
||||||
if !initial {
|
if !initial {
|
||||||
//NOTE: we are not tearing down existing lighthouse connections because they might be used for non lighthouse traffic
|
//NOTE: we are not tearing down existing lighthouse connections because they might be used for non lighthouse traffic
|
||||||
lh.l.Info("lighthouse.hosts has changed")
|
lh.l.Info("lighthouse.hosts has changed")
|
||||||
@@ -346,36 +346,37 @@ func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) parseLighthouses(c *config.C, lhMap map[netip.Addr]struct{}) error {
|
func (lh *LightHouse) parseLighthouses(c *config.C) ([]netip.Addr, error) {
|
||||||
lhs := c.GetStringSlice("lighthouse.hosts", []string{})
|
lhs := c.GetStringSlice("lighthouse.hosts", []string{})
|
||||||
if lh.amLighthouse && len(lhs) != 0 {
|
if lh.amLighthouse && len(lhs) != 0 {
|
||||||
lh.l.Warn("lighthouse.am_lighthouse enabled on node but upstream lighthouses exist in config")
|
lh.l.Warn("lighthouse.am_lighthouse enabled on node but upstream lighthouses exist in config")
|
||||||
}
|
}
|
||||||
|
out := make([]netip.Addr, len(lhs))
|
||||||
|
|
||||||
for i, host := range lhs {
|
for i, host := range lhs {
|
||||||
addr, err := netip.ParseAddr(host)
|
addr, err := netip.ParseAddr(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, err)
|
return nil, util.NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !lh.myVpnNetworksTable.Contains(addr) {
|
if !lh.myVpnNetworksTable.Contains(addr) {
|
||||||
return util.NewContextualError("lighthouse host is not in our networks, invalid", m{"vpnAddr": addr, "networks": lh.myVpnNetworks}, nil)
|
return nil, util.NewContextualError("lighthouse host is not in our networks, invalid", m{"vpnAddr": addr, "networks": lh.myVpnNetworks}, nil)
|
||||||
}
|
}
|
||||||
lhMap[addr] = struct{}{}
|
out[i] = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
if !lh.amLighthouse && len(lhMap) == 0 {
|
if !lh.amLighthouse && len(out) == 0 {
|
||||||
lh.l.Warn("No lighthouse.hosts configured, this host will only be able to initiate tunnels with static_host_map entries")
|
lh.l.Warn("No lighthouse.hosts configured, this host will only be able to initiate tunnels with static_host_map entries")
|
||||||
}
|
}
|
||||||
|
|
||||||
staticList := lh.GetStaticHostList()
|
staticList := lh.GetStaticHostList()
|
||||||
for lhAddr, _ := range lhMap {
|
for i := range out {
|
||||||
if _, ok := staticList[lhAddr]; !ok {
|
if _, ok := staticList[out[i]]; !ok {
|
||||||
return fmt.Errorf("lighthouse %s does not have a static_host_map entry", lhAddr)
|
return nil, fmt.Errorf("lighthouse %s does not have a static_host_map entry", out[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStaticMapCadence(c *config.C) (time.Duration, error) {
|
func getStaticMapCadence(c *config.C) (time.Duration, error) {
|
||||||
@@ -486,7 +487,7 @@ func (lh *LightHouse) QueryCache(vpnAddrs []netip.Addr) *RemoteList {
|
|||||||
lh.Lock()
|
lh.Lock()
|
||||||
defer lh.Unlock()
|
defer lh.Unlock()
|
||||||
// Add an entry if we don't already have one
|
// Add an entry if we don't already have one
|
||||||
return lh.unlockedGetRemoteList(vpnAddrs)
|
return lh.unlockedGetRemoteList(vpnAddrs) //todo CERT-V2 this contains addrmap lookups we could potentially skip
|
||||||
}
|
}
|
||||||
|
|
||||||
// queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing
|
// queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing
|
||||||
@@ -519,11 +520,15 @@ func (lh *LightHouse) queryAndPrepMessage(vpnAddr netip.Addr, f func(*cache) (in
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) DeleteVpnAddrs(allVpnAddrs []netip.Addr) {
|
func (lh *LightHouse) DeleteVpnAddrs(allVpnAddrs []netip.Addr) {
|
||||||
// First we check the static mapping
|
// First we check the static host map. If any of the VpnAddrs to be deleted are present, do nothing.
|
||||||
// and do nothing if it is there
|
staticList := lh.GetStaticHostList()
|
||||||
if _, ok := lh.GetStaticHostList()[allVpnAddrs[0]]; ok {
|
for _, addr := range allVpnAddrs {
|
||||||
return
|
if _, ok := staticList[addr]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// None of the VpnAddrs were present. Now we can do the deletes.
|
||||||
lh.Lock()
|
lh.Lock()
|
||||||
rm, ok := lh.addrMap[allVpnAddrs[0]]
|
rm, ok := lh.addrMap[allVpnAddrs[0]]
|
||||||
if ok {
|
if ok {
|
||||||
@@ -565,7 +570,7 @@ func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, t
|
|||||||
am.unlockedSetHostnamesResults(hr)
|
am.unlockedSetHostnamesResults(hr)
|
||||||
|
|
||||||
for _, addrPort := range hr.GetAddrs() {
|
for _, addrPort := range hr.GetAddrs() {
|
||||||
if !lh.shouldAdd(vpnAddr, addrPort.Addr()) {
|
if !lh.shouldAdd([]netip.Addr{vpnAddr}, addrPort.Addr()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
@@ -627,23 +632,30 @@ func (lh *LightHouse) addCalculatedRemotes(vpnAddr netip.Addr) bool {
|
|||||||
return len(calculatedV4) > 0 || len(calculatedV6) > 0
|
return len(calculatedV4) > 0 || len(calculatedV6) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// unlockedGetRemoteList
|
// unlockedGetRemoteList assumes you have the lh lock
|
||||||
// assumes you have the lh lock
|
|
||||||
func (lh *LightHouse) unlockedGetRemoteList(allAddrs []netip.Addr) *RemoteList {
|
func (lh *LightHouse) unlockedGetRemoteList(allAddrs []netip.Addr) *RemoteList {
|
||||||
am, ok := lh.addrMap[allAddrs[0]]
|
// before we go and make a new remotelist, we need to make sure we don't have one for any of this set of vpnaddrs yet
|
||||||
if !ok {
|
for i, addr := range allAddrs {
|
||||||
am = NewRemoteList(allAddrs, func(a netip.Addr) bool { return lh.shouldAdd(allAddrs[0], a) })
|
am, ok := lh.addrMap[addr]
|
||||||
for _, addr := range allAddrs {
|
if ok {
|
||||||
lh.addrMap[addr] = am
|
if i != 0 {
|
||||||
|
lh.addrMap[allAddrs[0]] = am
|
||||||
|
}
|
||||||
|
return am
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
am := NewRemoteList(allAddrs, lh.shouldAdd)
|
||||||
|
for _, addr := range allAddrs {
|
||||||
|
lh.addrMap[addr] = am
|
||||||
|
}
|
||||||
return am
|
return am
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) shouldAdd(vpnAddr netip.Addr, to netip.Addr) bool {
|
func (lh *LightHouse) shouldAdd(vpnAddrs []netip.Addr, to netip.Addr) bool {
|
||||||
allow := lh.GetRemoteAllowList().Allow(vpnAddr, to)
|
allow := lh.GetRemoteAllowList().AllowAll(vpnAddrs, to)
|
||||||
if lh.l.Level >= logrus.TraceLevel {
|
if lh.l.Level >= logrus.TraceLevel {
|
||||||
lh.l.WithField("vpnAddr", vpnAddr).WithField("udpAddr", to).WithField("allow", allow).
|
lh.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", to).WithField("allow", allow).
|
||||||
Trace("remoteAllowList.Allow")
|
Trace("remoteAllowList.Allow")
|
||||||
}
|
}
|
||||||
if !allow {
|
if !allow {
|
||||||
@@ -698,19 +710,22 @@ func (lh *LightHouse) unlockedShouldAddV6(vpnAddr netip.Addr, to *V6AddrPort) bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) IsLighthouseAddr(vpnAddr netip.Addr) bool {
|
func (lh *LightHouse) IsLighthouseAddr(vpnAddr netip.Addr) bool {
|
||||||
if _, ok := lh.GetLighthouses()[vpnAddr]; ok {
|
l := lh.GetLighthouses()
|
||||||
return true
|
for i := range l {
|
||||||
|
if l[i] == vpnAddr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: CERT-V2 IsLighthouseAddr should be sufficient, we just need to update the vpnAddrs for lighthouses after a handshake
|
func (lh *LightHouse) IsAnyLighthouseAddr(vpnAddrs []netip.Addr) bool {
|
||||||
// so that we know all the lighthouse vpnAddrs, not just the ones we were configured to talk to initially
|
|
||||||
func (lh *LightHouse) IsAnyLighthouseAddr(vpnAddr []netip.Addr) bool {
|
|
||||||
l := lh.GetLighthouses()
|
l := lh.GetLighthouses()
|
||||||
for _, a := range vpnAddr {
|
for i := range vpnAddrs {
|
||||||
if _, ok := l[a]; ok {
|
for j := range l {
|
||||||
return true
|
if l[j] == vpnAddrs[i] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -752,7 +767,7 @@ func (lh *LightHouse) innerQueryServer(addr netip.Addr, nb, out []byte) {
|
|||||||
queried := 0
|
queried := 0
|
||||||
lighthouses := lh.GetLighthouses()
|
lighthouses := lh.GetLighthouses()
|
||||||
|
|
||||||
for lhVpnAddr := range lighthouses {
|
for _, lhVpnAddr := range lighthouses {
|
||||||
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
||||||
if hi != nil {
|
if hi != nil {
|
||||||
v = hi.ConnectionState.myCert.Version()
|
v = hi.ConnectionState.myCert.Version()
|
||||||
@@ -870,7 +885,7 @@ func (lh *LightHouse) SendUpdate() {
|
|||||||
updated := 0
|
updated := 0
|
||||||
lighthouses := lh.GetLighthouses()
|
lighthouses := lh.GetLighthouses()
|
||||||
|
|
||||||
for lhVpnAddr := range lighthouses {
|
for _, lhVpnAddr := range lighthouses {
|
||||||
var v cert.Version
|
var v cert.Version
|
||||||
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
||||||
if hi != nil {
|
if hi != nil {
|
||||||
@@ -928,7 +943,6 @@ func (lh *LightHouse) SendUpdate() {
|
|||||||
V4AddrPorts: v4,
|
V4AddrPorts: v4,
|
||||||
V6AddrPorts: v6,
|
V6AddrPorts: v6,
|
||||||
RelayVpnAddrs: relays,
|
RelayVpnAddrs: relays,
|
||||||
VpnAddr: netAddrToProtoAddr(lh.myVpnNetworks[0].Addr()),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1048,19 +1062,19 @@ func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, fromVpnAddrs []neti
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
useVersion := cert.Version1
|
queryVpnAddr, useVersion, err := n.Details.GetVpnAddrAndVersion()
|
||||||
var queryVpnAddr netip.Addr
|
if err != nil {
|
||||||
if n.Details.OldVpnAddr != 0 {
|
|
||||||
b := [4]byte{}
|
|
||||||
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
|
||||||
queryVpnAddr = netip.AddrFrom4(b)
|
|
||||||
useVersion = 1
|
|
||||||
} else if n.Details.VpnAddr != nil {
|
|
||||||
queryVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
|
||||||
useVersion = 2
|
|
||||||
} else {
|
|
||||||
if lhh.l.Level >= logrus.DebugLevel {
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
lhh.l.WithField("from", fromVpnAddrs).WithField("details", n.Details).Debugln("Dropping malformed HostQuery")
|
lhh.l.WithField("from", fromVpnAddrs).WithField("details", n.Details).
|
||||||
|
Debugln("Dropping malformed HostQuery")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if useVersion == cert.Version1 && queryVpnAddr.Is6() {
|
||||||
|
// this case really shouldn't be possible to represent, but reject it anyway.
|
||||||
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
|
lhh.l.WithField("vpnAddrs", fromVpnAddrs).WithField("queryVpnAddr", queryVpnAddr).
|
||||||
|
Debugln("invalid vpn addr for v1 handleHostQuery")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1069,9 +1083,6 @@ func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, fromVpnAddrs []neti
|
|||||||
n = lhh.resetMeta()
|
n = lhh.resetMeta()
|
||||||
n.Type = NebulaMeta_HostQueryReply
|
n.Type = NebulaMeta_HostQueryReply
|
||||||
if useVersion == cert.Version1 {
|
if useVersion == cert.Version1 {
|
||||||
if !queryVpnAddr.Is4() {
|
|
||||||
return 0, fmt.Errorf("invalid vpn addr for v1 handleHostQuery")
|
|
||||||
}
|
|
||||||
b := queryVpnAddr.As4()
|
b := queryVpnAddr.As4()
|
||||||
n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
|
n.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
|
||||||
} else {
|
} else {
|
||||||
@@ -1116,8 +1127,9 @@ func (lhh *LightHouseHandler) sendHostPunchNotification(n *NebulaMeta, fromVpnAd
|
|||||||
if ok {
|
if ok {
|
||||||
whereToPunch = newDest
|
whereToPunch = newDest
|
||||||
} else {
|
} else {
|
||||||
//TODO: CERT-V2 this means the destination will have no addresses in common with the punch-ee
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
//choosing to do nothing for now, but maybe we return an error?
|
lhh.l.WithField("to", crt.Networks()).Debugln("unable to punch to host, no addresses in common")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1176,19 +1188,17 @@ func (lhh *LightHouseHandler) coalesceAnswers(v cert.Version, c *cache, n *Nebul
|
|||||||
if !r.Is4() {
|
if !r.Is4() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b = r.As4()
|
b = r.As4()
|
||||||
n.Details.OldRelayVpnAddrs = append(n.Details.OldRelayVpnAddrs, binary.BigEndian.Uint32(b[:]))
|
n.Details.OldRelayVpnAddrs = append(n.Details.OldRelayVpnAddrs, binary.BigEndian.Uint32(b[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if v == cert.Version2 {
|
} else if v == cert.Version2 {
|
||||||
for _, r := range c.relay.relay {
|
for _, r := range c.relay.relay {
|
||||||
n.Details.RelayVpnAddrs = append(n.Details.RelayVpnAddrs, netAddrToProtoAddr(r))
|
n.Details.RelayVpnAddrs = append(n.Details.RelayVpnAddrs, netAddrToProtoAddr(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//TODO: CERT-V2 don't panic
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
panic("unsupported version")
|
lhh.l.WithField("version", v).Debug("unsupported protocol version")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1198,18 +1208,16 @@ func (lhh *LightHouseHandler) handleHostQueryReply(n *NebulaMeta, fromVpnAddrs [
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lhh.lh.Lock()
|
certVpnAddr, _, err := n.Details.GetVpnAddrAndVersion()
|
||||||
|
if err != nil {
|
||||||
var certVpnAddr netip.Addr
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
if n.Details.OldVpnAddr != 0 {
|
lhh.l.WithError(err).WithField("vpnAddrs", fromVpnAddrs).Error("dropping malformed HostQueryReply")
|
||||||
b := [4]byte{}
|
}
|
||||||
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
return
|
||||||
certVpnAddr = netip.AddrFrom4(b)
|
|
||||||
} else if n.Details.VpnAddr != nil {
|
|
||||||
certVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
|
||||||
}
|
}
|
||||||
relays := n.Details.GetRelays()
|
relays := n.Details.GetRelays()
|
||||||
|
|
||||||
|
lhh.lh.Lock()
|
||||||
am := lhh.lh.unlockedGetRemoteList([]netip.Addr{certVpnAddr})
|
am := lhh.lh.unlockedGetRemoteList([]netip.Addr{certVpnAddr})
|
||||||
am.Lock()
|
am.Lock()
|
||||||
lhh.lh.Unlock()
|
lhh.lh.Unlock()
|
||||||
@@ -1234,27 +1242,24 @@ func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVp
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// not using GetVpnAddrAndVersion because we don't want to error on a blank detailsVpnAddr
|
||||||
var detailsVpnAddr netip.Addr
|
var detailsVpnAddr netip.Addr
|
||||||
useVersion := cert.Version1
|
var useVersion cert.Version
|
||||||
if n.Details.OldVpnAddr != 0 {
|
if n.Details.OldVpnAddr != 0 { //v1 always sets this field
|
||||||
b := [4]byte{}
|
b := [4]byte{}
|
||||||
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
||||||
detailsVpnAddr = netip.AddrFrom4(b)
|
detailsVpnAddr = netip.AddrFrom4(b)
|
||||||
useVersion = cert.Version1
|
useVersion = cert.Version1
|
||||||
} else if n.Details.VpnAddr != nil {
|
} else if n.Details.VpnAddr != nil { //this field is "optional" in v2, but if it's set, we should enforce it
|
||||||
detailsVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
detailsVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
||||||
useVersion = cert.Version2
|
useVersion = cert.Version2
|
||||||
} else {
|
} else {
|
||||||
if lhh.l.Level >= logrus.DebugLevel {
|
detailsVpnAddr = netip.Addr{}
|
||||||
lhh.l.WithField("details", n.Details).Debugf("dropping invalid HostUpdateNotification")
|
useVersion = cert.Version2
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: CERT-V2 hosts with only v2 certs cannot provide their ipv6 addr when contacting the lighthouse via v4?
|
//Simple check that the host sent this not someone else, if detailsVpnAddr is filled
|
||||||
//TODO: CERT-V2 why do we care about the vpnAddr in the packet? We know where it came from, right?
|
if detailsVpnAddr.IsValid() && !slices.Contains(fromVpnAddrs, detailsVpnAddr) {
|
||||||
//Simple check that the host sent this not someone else
|
|
||||||
if !slices.Contains(fromVpnAddrs, detailsVpnAddr) {
|
|
||||||
if lhh.l.Level >= logrus.DebugLevel {
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
lhh.l.WithField("vpnAddrs", fromVpnAddrs).WithField("answer", detailsVpnAddr).Debugln("Host sent invalid update")
|
lhh.l.WithField("vpnAddrs", fromVpnAddrs).WithField("answer", detailsVpnAddr).Debugln("Host sent invalid update")
|
||||||
}
|
}
|
||||||
@@ -1268,24 +1273,24 @@ func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVp
|
|||||||
am.Lock()
|
am.Lock()
|
||||||
lhh.lh.Unlock()
|
lhh.lh.Unlock()
|
||||||
|
|
||||||
am.unlockedSetV4(fromVpnAddrs[0], detailsVpnAddr, n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4)
|
am.unlockedSetV4(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V4AddrPorts, lhh.lh.unlockedShouldAddV4)
|
||||||
am.unlockedSetV6(fromVpnAddrs[0], detailsVpnAddr, n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6)
|
am.unlockedSetV6(fromVpnAddrs[0], fromVpnAddrs[0], n.Details.V6AddrPorts, lhh.lh.unlockedShouldAddV6)
|
||||||
am.unlockedSetRelay(fromVpnAddrs[0], relays)
|
am.unlockedSetRelay(fromVpnAddrs[0], relays)
|
||||||
am.Unlock()
|
am.Unlock()
|
||||||
|
|
||||||
n = lhh.resetMeta()
|
n = lhh.resetMeta()
|
||||||
n.Type = NebulaMeta_HostUpdateNotificationAck
|
n.Type = NebulaMeta_HostUpdateNotificationAck
|
||||||
|
switch useVersion {
|
||||||
if useVersion == cert.Version1 {
|
case cert.Version1:
|
||||||
if !fromVpnAddrs[0].Is4() {
|
if !fromVpnAddrs[0].Is4() {
|
||||||
lhh.l.WithField("vpnAddrs", fromVpnAddrs).Error("Can not send HostUpdateNotificationAck for a ipv6 vpn ip in a v1 message")
|
lhh.l.WithField("vpnAddrs", fromVpnAddrs).Error("Can not send HostUpdateNotificationAck for a ipv6 vpn ip in a v1 message")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vpnAddrB := fromVpnAddrs[0].As4()
|
vpnAddrB := fromVpnAddrs[0].As4()
|
||||||
n.Details.OldVpnAddr = binary.BigEndian.Uint32(vpnAddrB[:])
|
n.Details.OldVpnAddr = binary.BigEndian.Uint32(vpnAddrB[:])
|
||||||
} else if useVersion == cert.Version2 {
|
case cert.Version2:
|
||||||
n.Details.VpnAddr = netAddrToProtoAddr(fromVpnAddrs[0])
|
// do nothing, we want to send a blank message
|
||||||
} else {
|
default:
|
||||||
lhh.l.WithField("useVersion", useVersion).Error("invalid protocol version")
|
lhh.l.WithField("useVersion", useVersion).Error("invalid protocol version")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1303,13 +1308,20 @@ func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVp
|
|||||||
func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) {
|
func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, fromVpnAddrs []netip.Addr, w EncWriter) {
|
||||||
//It's possible the lighthouse is communicating with us using a non primary vpn addr,
|
//It's possible the lighthouse is communicating with us using a non primary vpn addr,
|
||||||
//which means we need to compare all fromVpnAddrs against all configured lighthouse vpn addrs.
|
//which means we need to compare all fromVpnAddrs against all configured lighthouse vpn addrs.
|
||||||
//maybe one day we'll have a better idea, if it matters.
|
|
||||||
if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) {
|
if !lhh.lh.IsAnyLighthouseAddr(fromVpnAddrs) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
detailsVpnAddr, _, err := n.Details.GetVpnAddrAndVersion()
|
||||||
|
if err != nil {
|
||||||
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
|
lhh.l.WithField("details", n.Details).WithError(err).Debugln("dropping invalid HostPunchNotification")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
empty := []byte{0}
|
empty := []byte{0}
|
||||||
punch := func(vpnPeer netip.AddrPort) {
|
punch := func(vpnPeer netip.AddrPort, logVpnAddr netip.Addr) {
|
||||||
if !vpnPeer.IsValid() {
|
if !vpnPeer.IsValid() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1321,48 +1333,31 @@ func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, fromVpn
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if lhh.l.Level >= logrus.DebugLevel {
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
var logVpnAddr netip.Addr
|
|
||||||
if n.Details.OldVpnAddr != 0 {
|
|
||||||
b := [4]byte{}
|
|
||||||
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
|
||||||
logVpnAddr = netip.AddrFrom4(b)
|
|
||||||
} else if n.Details.VpnAddr != nil {
|
|
||||||
logVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
|
||||||
}
|
|
||||||
lhh.l.Debugf("Punching on %v for %v", vpnPeer, logVpnAddr)
|
lhh.l.Debugf("Punching on %v for %v", vpnPeer, logVpnAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, a := range n.Details.V4AddrPorts {
|
for _, a := range n.Details.V4AddrPorts {
|
||||||
punch(protoV4AddrPortToNetAddrPort(a))
|
punch(protoV4AddrPortToNetAddrPort(a), detailsVpnAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, a := range n.Details.V6AddrPorts {
|
for _, a := range n.Details.V6AddrPorts {
|
||||||
punch(protoV6AddrPortToNetAddrPort(a))
|
punch(protoV6AddrPortToNetAddrPort(a), detailsVpnAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This sends a nebula test packet to the host trying to contact us. In the case
|
// This sends a nebula test packet to the host trying to contact us. In the case
|
||||||
// of a double nat or other difficult scenario, this may help establish
|
// of a double nat or other difficult scenario, this may help establish
|
||||||
// a tunnel.
|
// a tunnel.
|
||||||
if lhh.lh.punchy.GetRespond() {
|
if lhh.lh.punchy.GetRespond() {
|
||||||
var queryVpnAddr netip.Addr
|
|
||||||
if n.Details.OldVpnAddr != 0 {
|
|
||||||
b := [4]byte{}
|
|
||||||
binary.BigEndian.PutUint32(b[:], n.Details.OldVpnAddr)
|
|
||||||
queryVpnAddr = netip.AddrFrom4(b)
|
|
||||||
} else if n.Details.VpnAddr != nil {
|
|
||||||
queryVpnAddr = protoAddrToNetAddr(n.Details.VpnAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(lhh.lh.punchy.GetRespondDelay())
|
time.Sleep(lhh.lh.punchy.GetRespondDelay())
|
||||||
if lhh.l.Level >= logrus.DebugLevel {
|
if lhh.l.Level >= logrus.DebugLevel {
|
||||||
lhh.l.Debugf("Sending a nebula test packet to vpn addr %s", queryVpnAddr)
|
lhh.l.Debugf("Sending a nebula test packet to vpn addr %s", detailsVpnAddr)
|
||||||
}
|
}
|
||||||
//NOTE: we have to allocate a new output buffer here since we are spawning a new goroutine
|
//NOTE: we have to allocate a new output buffer here since we are spawning a new goroutine
|
||||||
// for each punchBack packet. We should move this into a timerwheel or a single goroutine
|
// for each punchBack packet. We should move this into a timerwheel or a single goroutine
|
||||||
// managed by a channel.
|
// managed by a channel.
|
||||||
w.SendMessageToVpnAddr(header.Test, header.TestRequest, queryVpnAddr, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
w.SendMessageToVpnAddr(header.Test, header.TestRequest, detailsVpnAddr, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1441,3 +1436,17 @@ func findNetworkUnion(prefixes []netip.Prefix, addrs []netip.Addr) (netip.Addr,
|
|||||||
}
|
}
|
||||||
return netip.Addr{}, false
|
return netip.Addr{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *NebulaMetaDetails) GetVpnAddrAndVersion() (netip.Addr, cert.Version, error) {
|
||||||
|
if d.OldVpnAddr != 0 {
|
||||||
|
b := [4]byte{}
|
||||||
|
binary.BigEndian.PutUint32(b[:], d.OldVpnAddr)
|
||||||
|
detailsVpnAddr := netip.AddrFrom4(b)
|
||||||
|
return detailsVpnAddr, cert.Version1, nil
|
||||||
|
} else if d.VpnAddr != nil {
|
||||||
|
detailsVpnAddr := protoAddrToNetAddr(d.VpnAddr)
|
||||||
|
return detailsVpnAddr, cert.Version2, nil
|
||||||
|
} else {
|
||||||
|
return netip.Addr{}, cert.Version1, ErrBadDetailsVpnAddr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -493,3 +493,123 @@ func Test_findNetworkUnion(t *testing.T) {
|
|||||||
out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81})
|
out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81})
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLighthouse_Dont_Delete_Static_Hosts(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
|
||||||
|
myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242")
|
||||||
|
|
||||||
|
testSameHostNotStatic := netip.MustParseAddr("10.128.0.41")
|
||||||
|
testStaticHost := netip.MustParseAddr("10.128.0.42")
|
||||||
|
//myVpnIp := netip.MustParseAddr("10.128.0.2")
|
||||||
|
|
||||||
|
c := config.NewC(l)
|
||||||
|
lh1 := "10.128.0.2"
|
||||||
|
c.Settings["lighthouse"] = map[string]any{
|
||||||
|
"hosts": []any{lh1},
|
||||||
|
"interval": "1s",
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Settings["listen"] = map[string]any{"port": 4242}
|
||||||
|
c.Settings["static_host_map"] = map[string]any{
|
||||||
|
lh1: []any{"1.1.1.1:4242"},
|
||||||
|
"10.128.0.42": []any{"1.2.3.4:4242"},
|
||||||
|
}
|
||||||
|
|
||||||
|
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||||
|
nt := new(bart.Lite)
|
||||||
|
nt.Insert(myVpnNet)
|
||||||
|
cs := &CertState{
|
||||||
|
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||||
|
myVpnNetworksTable: nt,
|
||||||
|
}
|
||||||
|
lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
lh.ifce = &mockEncWriter{}
|
||||||
|
|
||||||
|
//test that we actually have the static entry:
|
||||||
|
out := lh.Query(testStaticHost)
|
||||||
|
assert.NotNil(t, out)
|
||||||
|
assert.Equal(t, out.vpnAddrs[0], testStaticHost)
|
||||||
|
out.Rebuild([]netip.Prefix{}) //why tho
|
||||||
|
assert.Equal(t, out.addrs[0], myUdpAddr2)
|
||||||
|
|
||||||
|
//bolt on a lower numbered primary IP
|
||||||
|
am := lh.unlockedGetRemoteList([]netip.Addr{testStaticHost})
|
||||||
|
am.vpnAddrs = []netip.Addr{testSameHostNotStatic, testStaticHost}
|
||||||
|
lh.addrMap[testSameHostNotStatic] = am
|
||||||
|
out.Rebuild([]netip.Prefix{}) //???
|
||||||
|
|
||||||
|
//test that we actually have the static entry:
|
||||||
|
out = lh.Query(testStaticHost)
|
||||||
|
assert.NotNil(t, out)
|
||||||
|
assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic)
|
||||||
|
assert.Equal(t, out.vpnAddrs[1], testStaticHost)
|
||||||
|
assert.Equal(t, out.addrs[0], myUdpAddr2)
|
||||||
|
|
||||||
|
//test that we actually have the static entry for BOTH:
|
||||||
|
out2 := lh.Query(testSameHostNotStatic)
|
||||||
|
assert.Same(t, out2, out)
|
||||||
|
|
||||||
|
//now do the delete
|
||||||
|
lh.DeleteVpnAddrs([]netip.Addr{testSameHostNotStatic, testStaticHost})
|
||||||
|
//verify
|
||||||
|
out = lh.Query(testSameHostNotStatic)
|
||||||
|
assert.NotNil(t, out)
|
||||||
|
if out == nil {
|
||||||
|
t.Fatal("expected non-nil query for the static host")
|
||||||
|
}
|
||||||
|
assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic)
|
||||||
|
assert.Equal(t, out.vpnAddrs[1], testStaticHost)
|
||||||
|
assert.Equal(t, out.addrs[0], myUdpAddr2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLighthouse_DeletesWork(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
|
||||||
|
myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242")
|
||||||
|
testHost := netip.MustParseAddr("10.128.0.42")
|
||||||
|
|
||||||
|
c := config.NewC(l)
|
||||||
|
lh1 := "10.128.0.2"
|
||||||
|
c.Settings["lighthouse"] = map[string]any{
|
||||||
|
"hosts": []any{lh1},
|
||||||
|
"interval": "1s",
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Settings["listen"] = map[string]any{"port": 4242}
|
||||||
|
c.Settings["static_host_map"] = map[string]any{
|
||||||
|
lh1: []any{"1.1.1.1:4242"},
|
||||||
|
}
|
||||||
|
|
||||||
|
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||||
|
nt := new(bart.Lite)
|
||||||
|
nt.Insert(myVpnNet)
|
||||||
|
cs := &CertState{
|
||||||
|
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||||
|
myVpnNetworksTable: nt,
|
||||||
|
}
|
||||||
|
lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
lh.ifce = &mockEncWriter{}
|
||||||
|
|
||||||
|
//insert the host
|
||||||
|
am := lh.unlockedGetRemoteList([]netip.Addr{testHost})
|
||||||
|
am.vpnAddrs = []netip.Addr{testHost}
|
||||||
|
am.addrs = []netip.AddrPort{myUdpAddr2}
|
||||||
|
lh.addrMap[testHost] = am
|
||||||
|
am.Rebuild([]netip.Prefix{}) //???
|
||||||
|
|
||||||
|
//test that we actually have the entry:
|
||||||
|
out := lh.Query(testHost)
|
||||||
|
assert.NotNil(t, out)
|
||||||
|
assert.Equal(t, out.vpnAddrs[0], testHost)
|
||||||
|
out.Rebuild([]netip.Prefix{}) //why tho
|
||||||
|
assert.Equal(t, out.addrs[0], myUdpAddr2)
|
||||||
|
|
||||||
|
//now do the delete
|
||||||
|
lh.DeleteVpnAddrs([]netip.Addr{testHost})
|
||||||
|
//verify
|
||||||
|
out = lh.Query(testHost)
|
||||||
|
assert.Nil(t, out)
|
||||||
|
}
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -242,6 +242,7 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg
|
|||||||
relayManager: NewRelayManager(ctx, l, hostMap, c),
|
relayManager: NewRelayManager(ctx, l, hostMap, c),
|
||||||
punchy: punchy,
|
punchy: punchy,
|
||||||
ConntrackCacheTimeout: conntrackCacheTimeout,
|
ConntrackCacheTimeout: conntrackCacheTimeout,
|
||||||
|
batchSize: c.GetInt("tun.batch_size", 64),
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
55
noise.go
55
noise.go
@@ -25,11 +25,6 @@ func NewNebulaCipherState(s *noise.CipherState) *NebulaCipherState {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type cipherAEADDanger interface {
|
|
||||||
EncryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error)
|
|
||||||
DecryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// EncryptDanger encrypts and authenticates a given payload.
|
// EncryptDanger encrypts and authenticates a given payload.
|
||||||
//
|
//
|
||||||
// out is a destination slice to hold the output of the EncryptDanger operation.
|
// out is a destination slice to hold the output of the EncryptDanger operation.
|
||||||
@@ -40,25 +35,20 @@ type cipherAEADDanger interface {
|
|||||||
// be re-used by callers to minimize garbage collection.
|
// be re-used by callers to minimize garbage collection.
|
||||||
func (s *NebulaCipherState) EncryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error) {
|
func (s *NebulaCipherState) EncryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error) {
|
||||||
if s != nil {
|
if s != nil {
|
||||||
switch ce := s.c.(type) {
|
// TODO: Is this okay now that we have made messageCounter atomic?
|
||||||
case cipherAEADDanger:
|
// Alternative may be to split the counter space into ranges
|
||||||
return ce.EncryptDanger(out, ad, plaintext, n, nb)
|
//if n <= s.n {
|
||||||
default:
|
// return nil, errors.New("CRITICAL: a duplicate counter value was used")
|
||||||
// TODO: Is this okay now that we have made messageCounter atomic?
|
//}
|
||||||
// Alternative may be to split the counter space into ranges
|
//s.n = n
|
||||||
//if n <= s.n {
|
nb[0] = 0
|
||||||
// return nil, errors.New("CRITICAL: a duplicate counter value was used")
|
nb[1] = 0
|
||||||
//}
|
nb[2] = 0
|
||||||
//s.n = n
|
nb[3] = 0
|
||||||
nb[0] = 0
|
noiseEndianness.PutUint64(nb[4:], n)
|
||||||
nb[1] = 0
|
out = s.c.(cipher.AEAD).Seal(out, nb, plaintext, ad)
|
||||||
nb[2] = 0
|
//l.Debugf("Encryption: outlen: %d, nonce: %d, ad: %s, plainlen %d", len(out), n, ad, len(plaintext))
|
||||||
nb[3] = 0
|
return out, nil
|
||||||
noiseEndianness.PutUint64(nb[4:], n)
|
|
||||||
out = s.c.(cipher.AEAD).Seal(out, nb, plaintext, ad)
|
|
||||||
//l.Debugf("Encryption: outlen: %d, nonce: %d, ad: %s, plainlen %d", len(out), n, ad, len(plaintext))
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("no cipher state available to encrypt")
|
return nil, errors.New("no cipher state available to encrypt")
|
||||||
}
|
}
|
||||||
@@ -66,17 +56,12 @@ func (s *NebulaCipherState) EncryptDanger(out, ad, plaintext []byte, n uint64, n
|
|||||||
|
|
||||||
func (s *NebulaCipherState) DecryptDanger(out, ad, ciphertext []byte, n uint64, nb []byte) ([]byte, error) {
|
func (s *NebulaCipherState) DecryptDanger(out, ad, ciphertext []byte, n uint64, nb []byte) ([]byte, error) {
|
||||||
if s != nil {
|
if s != nil {
|
||||||
switch ce := s.c.(type) {
|
nb[0] = 0
|
||||||
case cipherAEADDanger:
|
nb[1] = 0
|
||||||
return ce.DecryptDanger(out, ad, ciphertext, n, nb)
|
nb[2] = 0
|
||||||
default:
|
nb[3] = 0
|
||||||
nb[0] = 0
|
noiseEndianness.PutUint64(nb[4:], n)
|
||||||
nb[1] = 0
|
return s.c.(cipher.AEAD).Open(out, nb, ciphertext, ad)
|
||||||
nb[2] = 0
|
|
||||||
nb[3] = 0
|
|
||||||
noiseEndianness.PutUint64(nb[4:], n)
|
|
||||||
return s.c.(cipher.AEAD).Open(out, nb, ciphertext, ad)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return []byte{}, nil
|
return []byte{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
//go:build fips140v1.0
|
|
||||||
// +build fips140v1.0
|
|
||||||
|
|
||||||
package noiseutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/cipher"
|
|
||||||
"encoding/binary"
|
|
||||||
|
|
||||||
// unsafe needed for go:linkname
|
|
||||||
_ "unsafe"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EncryptLockNeeded indicates if calls to Encrypt need a lock
|
|
||||||
// This is true for fips140 because the Seal function verifies that the
|
|
||||||
// nonce is strictly increasing.
|
|
||||||
const EncryptLockNeeded = true
|
|
||||||
|
|
||||||
// TODO: Use NewGCMWithCounterNonce once available:
|
|
||||||
// - https://github.com/golang/go/issues/73110
|
|
||||||
// Using tls.aeadAESGCM gives us the TLS 1.2 GCM, which also verifies
|
|
||||||
// that the nonce is strictly increasing.
|
|
||||||
//
|
|
||||||
//go:linkname aeadAESGCM crypto/tls.aeadAESGCM
|
|
||||||
func aeadAESGCM(key, noncePrefix []byte) cipher.AEAD
|
|
||||||
|
|
||||||
type cipherFn struct {
|
|
||||||
fn func([32]byte) noise.Cipher
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c cipherFn) Cipher(k [32]byte) noise.Cipher { return c.fn(k) }
|
|
||||||
func (c cipherFn) CipherName() string { return c.name }
|
|
||||||
|
|
||||||
// CipherAESGCM is the AES256-GCM AEAD cipher (using aeadAESGCM when fips140 is enabled)
|
|
||||||
var CipherAESGCM noise.CipherFunc = cipherFn{cipherAESGCM, "AESGCM"}
|
|
||||||
|
|
||||||
// tls.aeadAESGCM uses a 4 byte static prefix and an 8 byte nonce
|
|
||||||
var emptyPrefix = []byte{0, 0, 0, 0}
|
|
||||||
|
|
||||||
func cipherAESGCM(k [32]byte) noise.Cipher {
|
|
||||||
gcm := aeadAESGCM(k[:], emptyPrefix)
|
|
||||||
return aeadCipher{
|
|
||||||
gcm,
|
|
||||||
func(n uint64) []byte {
|
|
||||||
// tls.aeadAESGCM uses a 4 byte static prefix and an 8 byte nonce
|
|
||||||
var nonce [8]byte
|
|
||||||
binary.BigEndian.PutUint64(nonce[:], n)
|
|
||||||
return nonce[:]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type aeadCipher struct {
|
|
||||||
cipher.AEAD
|
|
||||||
nonce func(uint64) []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c aeadCipher) Encrypt(out []byte, n uint64, ad, plaintext []byte) []byte {
|
|
||||||
return c.Seal(out, c.nonce(n), plaintext, ad)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c aeadCipher) Decrypt(out []byte, n uint64, ad, ciphertext []byte) ([]byte, error) {
|
|
||||||
return c.Open(out, c.nonce(n), ciphertext, ad)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c aeadCipher) EncryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error) {
|
|
||||||
binary.BigEndian.PutUint64(nb[4:], n)
|
|
||||||
out = c.Seal(out, nb[4:], plaintext, ad)
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c aeadCipher) DecryptDanger(out, ad, ciphertext []byte, n uint64, nb []byte) ([]byte, error) {
|
|
||||||
binary.BigEndian.PutUint64(nb[4:], n)
|
|
||||||
return c.Open(out, nb[4:], ciphertext, ad)
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
//go:build fips140v1.0
|
|
||||||
// +build fips140v1.0
|
|
||||||
|
|
||||||
package noiseutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/fips140"
|
|
||||||
"encoding/hex"
|
|
||||||
"log"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestEncryptLockNeeded(t *testing.T) {
|
|
||||||
assert.True(t, EncryptLockNeeded)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure NewAESGCM validates the nonce is non-repeating
|
|
||||||
func TestNewAESGCM(t *testing.T) {
|
|
||||||
assert.True(t, fips140.Enabled())
|
|
||||||
|
|
||||||
key, _ := hex.DecodeString("feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308")
|
|
||||||
iv, _ := hex.DecodeString("00000000facedbaddecaf888")
|
|
||||||
plaintext, _ := hex.DecodeString("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
|
|
||||||
aad, _ := hex.DecodeString("feedfacedeadbeeffeedfacedeadbeefabaddad2")
|
|
||||||
expected, _ := hex.DecodeString("72ce2ea385f88c20d856e9d1248c2ca08562bbe8a61459ffae06ec393540518e9b6b4c40a146053f26a3df83c5384a48d273148b15aba64d970107432b2892741359275676441c1572c3fa9e")
|
|
||||||
|
|
||||||
var keyArray [32]byte
|
|
||||||
copy(keyArray[:], key)
|
|
||||||
c := CipherAESGCM.Cipher(keyArray)
|
|
||||||
aead := c.(aeadCipher).AEAD
|
|
||||||
|
|
||||||
dst := aead.Seal([]byte{}, iv, plaintext, aad)
|
|
||||||
log.Printf("%x", dst)
|
|
||||||
assert.Equal(t, expected, dst)
|
|
||||||
|
|
||||||
// We expect this to fail since we are re-encrypting with a repeat IV
|
|
||||||
assert.PanicsWithValue(t, "crypto/cipher: counter decreased", func() {
|
|
||||||
dst = aead.Seal([]byte{}, iv, plaintext, aad)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
//go:build !boringcrypto && !fips140v1.0
|
//go:build !boringcrypto
|
||||||
// +build !boringcrypto,!fips140v1.0
|
// +build !boringcrypto
|
||||||
|
|
||||||
package noiseutil
|
package noiseutil
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//go:build !boringcrypto && !fips140v1.0
|
//go:build !boringcrypto
|
||||||
// +build !boringcrypto,!fips140v1.0
|
// +build !boringcrypto
|
||||||
|
|
||||||
package noiseutil
|
package noiseutil
|
||||||
|
|
||||||
|
|||||||
27
outside.go
27
outside.go
@@ -254,16 +254,18 @@ func (f *Interface) handleHostRoaming(hostinfo *HostInfo, udpAddr netip.AddrPort
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleEncrypted returns true if a packet should be processed, false otherwise
|
||||||
func (f *Interface) handleEncrypted(ci *ConnectionState, addr netip.AddrPort, h *header.H) bool {
|
func (f *Interface) handleEncrypted(ci *ConnectionState, addr netip.AddrPort, h *header.H) bool {
|
||||||
// If connectionstate exists and the replay protector allows, process packet
|
// If connectionstate does not exist, send a recv error, if possible, to encourage a fast reconnect
|
||||||
// Else, send recv errors for 300 seconds after a restart to allow fast reconnection.
|
if ci == nil {
|
||||||
if ci == nil || !ci.window.Check(f.l, h.MessageCounter) {
|
|
||||||
if addr.IsValid() {
|
if addr.IsValid() {
|
||||||
f.maybeSendRecvError(addr, h.RemoteIndex)
|
f.maybeSendRecvError(addr, h.RemoteIndex)
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// If the window check fails, refuse to process the packet, but don't send a recv error
|
||||||
|
if !ci.window.Check(f.l, h.MessageCounter) {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -331,12 +333,13 @@ func parseV6(data []byte, incoming bool, fp *firewall.Packet) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fp.Protocol = uint8(proto)
|
fp.Protocol = uint8(proto)
|
||||||
|
ports := data[offset : offset+4]
|
||||||
if incoming {
|
if incoming {
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[offset : offset+2])
|
fp.RemotePort = binary.BigEndian.Uint16(ports[0:2])
|
||||||
fp.LocalPort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
|
fp.LocalPort = binary.BigEndian.Uint16(ports[2:4])
|
||||||
} else {
|
} else {
|
||||||
fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
|
fp.LocalPort = binary.BigEndian.Uint16(ports[0:2])
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
|
fp.RemotePort = binary.BigEndian.Uint16(ports[2:4])
|
||||||
}
|
}
|
||||||
|
|
||||||
fp.Fragment = false
|
fp.Fragment = false
|
||||||
@@ -537,10 +540,6 @@ func (f *Interface) handleRecvError(addr netip.AddrPort, h *header.H) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hostinfo.RecvErrorExceeded() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if hostinfo.remote.IsValid() && hostinfo.remote != addr {
|
if hostinfo.remote.IsValid() && hostinfo.remote != addr {
|
||||||
f.l.Infoln("Someone spoofing recv_errors? ", addr, hostinfo.remote)
|
f.l.Infoln("Someone spoofing recv_errors? ", addr, hostinfo.remote)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package overlay
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -305,29 +304,3 @@ func parseUnsafeRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) {
|
|||||||
|
|
||||||
return routes, nil
|
return routes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipWithin(o *net.IPNet, i *net.IPNet) bool {
|
|
||||||
// Make sure o contains the lowest form of i
|
|
||||||
if !o.Contains(i.IP.Mask(i.Mask)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the max ip in i
|
|
||||||
ip4 := i.IP.To4()
|
|
||||||
if ip4 == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
last := make(net.IP, len(ip4))
|
|
||||||
copy(last, ip4)
|
|
||||||
for x := range ip4 {
|
|
||||||
last[x] |= ^i.Mask[x]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure o contains the max
|
|
||||||
if !o.Contains(last) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -225,6 +225,7 @@ func Test_parseUnsafeRoutes(t *testing.T) {
|
|||||||
// no mtu
|
// no mtu
|
||||||
c.Settings["tun"] = map[string]any{"unsafe_routes": []any{map[string]any{"via": "127.0.0.1", "route": "1.0.0.0/8"}}}
|
c.Settings["tun"] = map[string]any{"unsafe_routes": []any{map[string]any{"via": "127.0.0.1", "route": "1.0.0.0/8"}}}
|
||||||
routes, err = parseUnsafeRoutes(c, []netip.Prefix{n})
|
routes, err = parseUnsafeRoutes(c, []netip.Prefix{n})
|
||||||
|
require.NoError(t, err)
|
||||||
assert.Len(t, routes, 1)
|
assert.Len(t, routes, 1)
|
||||||
assert.Equal(t, 0, routes[0].MTU)
|
assert.Equal(t, 0, routes[0].MTU)
|
||||||
|
|
||||||
@@ -318,7 +319,7 @@ func Test_makeRouteTree(t *testing.T) {
|
|||||||
|
|
||||||
ip, err = netip.ParseAddr("1.1.0.1")
|
ip, err = netip.ParseAddr("1.1.0.1")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
r, ok = routeTree.Lookup(ip)
|
_, ok = routeTree.Lookup(ip)
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//go:build !ios && !e2e_testing
|
//go:build darwin && !ios && !e2e_testing
|
||||||
// +build !ios,!e2e_testing
|
// +build darwin,!ios,!e2e_testing
|
||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
@@ -7,50 +7,28 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
netroute "golang.org/x/net/route"
|
netroute "golang.org/x/net/route"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
linkAddr *netroute.LinkAddr
|
||||||
Device string
|
|
||||||
vpnNetworks []netip.Prefix
|
|
||||||
DefaultMTU int
|
|
||||||
Routes atomic.Pointer[[]Route]
|
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
|
||||||
linkAddr *netroute.LinkAddr
|
|
||||||
l *logrus.Logger
|
|
||||||
|
|
||||||
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
|
||||||
out []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ioctl structures for Darwin network configuration
|
||||||
type ifReq struct {
|
type ifReq struct {
|
||||||
Name [unix.IFNAMSIZ]byte
|
Name [unix.IFNAMSIZ]byte
|
||||||
Flags uint16
|
Flags uint16
|
||||||
pad [8]byte
|
pad [8]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
_SIOCAIFADDR_IN6 = 2155899162
|
|
||||||
_UTUN_OPT_IFNAME = 2
|
|
||||||
_IN6_IFF_NODAD = 0x0020
|
|
||||||
_IN6_IFF_SECURED = 0x0400
|
|
||||||
utunControlName = "com.apple.net.utun_control"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ifreqMTU struct {
|
type ifreqMTU struct {
|
||||||
Name [16]byte
|
Name [16]byte
|
||||||
MTU int32
|
MTU int32
|
||||||
@@ -80,60 +58,61 @@ type ifreqAlias6 struct {
|
|||||||
Lifetime addrLifetime
|
Lifetime addrLifetime
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
const (
|
||||||
|
_SIOCAIFADDR_IN6 = 2155899162
|
||||||
|
_IN6_IFF_NODAD = 0x0020
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*wgTun, error) {
|
||||||
|
return nil, fmt.Errorf("newTunFromFd not supported on Darwin")
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*wgTun, error) {
|
||||||
name := c.GetString("tun.dev", "")
|
name := c.GetString("tun.dev", "")
|
||||||
ifIndex := -1
|
deviceName := "utun"
|
||||||
|
|
||||||
|
// Parse device name to handle utun[0-9]+ format
|
||||||
if name != "" && name != "utun" {
|
if name != "" && name != "utun" {
|
||||||
|
ifIndex := -1
|
||||||
_, err := fmt.Sscanf(name, "utun%d", &ifIndex)
|
_, err := fmt.Sscanf(name, "utun%d", &ifIndex)
|
||||||
if err != nil || ifIndex < 0 {
|
if err != nil || ifIndex < 0 {
|
||||||
// NOTE: we don't make this error so we don't break existing
|
// NOTE: we don't make this error so we don't break existing
|
||||||
// configs that set a name before it was used.
|
// configs that set a name before it was used.
|
||||||
l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
|
l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
|
||||||
ifIndex = -1
|
} else {
|
||||||
|
deviceName = name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, unix.AF_SYS_CONTROL)
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
|
||||||
|
// Create WireGuard TUN device
|
||||||
|
tunDevice, err := wgtun.CreateTUN(deviceName, mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("system socket: %v", err)
|
return nil, fmt.Errorf("failed to create TUN device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctlInfo = &unix.CtlInfo{}
|
// Get the actual device name
|
||||||
copy(ctlInfo.Name[:], utunControlName)
|
actualName, err := tunDevice.Name()
|
||||||
|
|
||||||
err = unix.IoctlCtlInfo(fd, ctlInfo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("CTLIOCGINFO: %v", err)
|
tunDevice.Close()
|
||||||
|
return nil, fmt.Errorf("failed to get TUN device name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = unix.Connect(fd, &unix.SockaddrCtl{
|
t := &wgTun{
|
||||||
ID: ctlInfo.Id,
|
tunDevice: tunDevice,
|
||||||
Unit: uint32(ifIndex) + 1,
|
vpnNetworks: vpnNetworks,
|
||||||
})
|
MaxMTU: mtu,
|
||||||
if err != nil {
|
DefaultMTU: mtu,
|
||||||
return nil, fmt.Errorf("SYS_CONNECT: %v", err)
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err = unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
|
// Create Darwin-specific route manager
|
||||||
if err != nil {
|
t.routeManager = &tun{}
|
||||||
return nil, fmt.Errorf("failed to retrieve tun name: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = unix.SetNonblock(fd, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("SetNonblock: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t := &tun{
|
|
||||||
ReadWriteCloser: os.NewFile(uintptr(fd), ""),
|
|
||||||
Device: name,
|
|
||||||
vpnNetworks: vpnNetworks,
|
|
||||||
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
|
||||||
l: l,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,216 +123,251 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
l.WithField("name", actualName).Info("Created WireGuard TUN device")
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) deviceBytes() (o [16]byte) {
|
func (rm *tun) Activate(t *wgTun) error {
|
||||||
for i, c := range t.Device {
|
name, err := t.tunDevice.Name()
|
||||||
o[i] = byte(c)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in Darwin")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
|
||||||
if t.ReadWriteCloser != nil {
|
|
||||||
return t.ReadWriteCloser.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Activate() error {
|
|
||||||
devName := t.deviceBytes()
|
|
||||||
|
|
||||||
s, err := unix.Socket(
|
|
||||||
unix.AF_INET,
|
|
||||||
unix.SOCK_DGRAM,
|
|
||||||
unix.IPPROTO_IP,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
}
|
|
||||||
defer unix.Close(s)
|
|
||||||
|
|
||||||
fd := uintptr(s)
|
|
||||||
|
|
||||||
// Set the MTU on the device
|
|
||||||
ifm := ifreqMTU{Name: devName, MTU: int32(t.DefaultMTU)}
|
|
||||||
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
|
||||||
return fmt.Errorf("failed to set tun mtu: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the device flags
|
// Set the MTU
|
||||||
ifrf := ifReq{Name: devName}
|
rm.SetMTU(t, t.MaxMTU)
|
||||||
if err = ioctl(fd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to get tun flags: %s", err)
|
// Add IP addresses
|
||||||
|
for _, network := range t.vpnNetworks {
|
||||||
|
if err := rm.addIP(t, name, network); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
linkAddr, err := getLinkAddr(t.Device)
|
// Bring up the interface using ioctl
|
||||||
|
if err := rm.bringUpInterface(name); err != nil {
|
||||||
|
return fmt.Errorf("failed to bring up interface: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the link address for routing
|
||||||
|
linkAddr, err := getLinkAddr(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to get link address: %w", err)
|
||||||
}
|
}
|
||||||
if linkAddr == nil {
|
if linkAddr == nil {
|
||||||
return fmt.Errorf("unable to discover link_addr for tun interface")
|
return fmt.Errorf("unable to discover link_addr for tun interface")
|
||||||
}
|
}
|
||||||
t.linkAddr = linkAddr
|
rm.linkAddr = linkAddr
|
||||||
|
|
||||||
for _, network := range t.vpnNetworks {
|
// Set the routes
|
||||||
if network.Addr().Is4() {
|
if err := rm.AddRoutes(t, false); err != nil {
|
||||||
err = t.activate4(network)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) bringUpInterface(name string) error {
|
||||||
|
// Open a socket for ioctl
|
||||||
|
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create socket: %w", err)
|
||||||
|
}
|
||||||
|
defer unix.Close(fd)
|
||||||
|
|
||||||
|
// Get current flags
|
||||||
|
var ifrf ifReq
|
||||||
|
copy(ifrf.Name[:], name)
|
||||||
|
|
||||||
|
if err := ioctl(uintptr(fd), unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
||||||
|
return fmt.Errorf("failed to get interface flags: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set IFF_UP and IFF_RUNNING flags
|
||||||
|
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
|
||||||
|
|
||||||
|
if err := ioctl(uintptr(fd), unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
||||||
|
return fmt.Errorf("failed to set interface flags: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) SetMTU(t *wgTun, mtu int) {
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get device name for MTU set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a socket for ioctl
|
||||||
|
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to create socket for MTU set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer unix.Close(fd)
|
||||||
|
|
||||||
|
// Prepare the ioctl request
|
||||||
|
var ifr ifreqMTU
|
||||||
|
copy(ifr.Name[:], name)
|
||||||
|
ifr.MTU = int32(mtu)
|
||||||
|
|
||||||
|
// Set the MTU using ioctl
|
||||||
|
if err := ioctl(uintptr(fd), unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to set tun mtu via ioctl")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) SetDefaultRoute(t *wgTun, cidr netip.Prefix) error {
|
||||||
|
// On Darwin, routes are set via ifconfig and route commands
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) AddRoutes(t *wgTun, logErrors bool) error {
|
||||||
|
routes := *t.Routes.Load()
|
||||||
|
for _, r := range routes {
|
||||||
|
if !r.Install {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := rm.addRoute(r.Cidr)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, unix.EEXIST) {
|
||||||
|
t.l.WithField("route", r.Cidr).
|
||||||
|
Warnf("unable to add unsafe_route, identical route already exists")
|
||||||
|
} else {
|
||||||
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
|
if logErrors {
|
||||||
|
retErr.Log(t.l)
|
||||||
|
} else {
|
||||||
|
return retErr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = t.activate6(network)
|
t.l.WithField("route", r).Info("Added route")
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the interface
|
return nil
|
||||||
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
|
|
||||||
if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to run tun device: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsafe path routes
|
|
||||||
return t.addRoutes(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) activate4(network netip.Prefix) error {
|
func (rm *tun) RemoveRoutes(t *wgTun, routes []Route) {
|
||||||
s, err := unix.Socket(
|
for _, r := range routes {
|
||||||
unix.AF_INET,
|
if !r.Install {
|
||||||
unix.SOCK_DGRAM,
|
continue
|
||||||
unix.IPPROTO_IP,
|
}
|
||||||
)
|
|
||||||
|
err := rm.delRoute(r.Cidr)
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
||||||
|
} else {
|
||||||
|
t.l.WithField("route", r).Info("Removed route")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) NewMultiQueueReader(t *wgTun) (io.ReadWriteCloser, error) {
|
||||||
|
// Darwin doesn't support multi-queue TUN devices in the same way as Linux
|
||||||
|
// Return a reader that wraps the same device
|
||||||
|
return &wgTunReader{
|
||||||
|
parent: t,
|
||||||
|
tunDevice: t.tunDevice,
|
||||||
|
offset: 0,
|
||||||
|
l: t.l,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIP(t *wgTun, name string, network netip.Prefix) error {
|
||||||
|
addr := network.Addr()
|
||||||
|
|
||||||
|
if addr.Is4() {
|
||||||
|
return rm.addIPv4(name, network)
|
||||||
|
} else {
|
||||||
|
return rm.addIPv6(name, network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIPv4(name string, network netip.Prefix) error {
|
||||||
|
// Open an IPv4 socket for ioctl
|
||||||
|
s, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to create IPv4 socket: %w", err)
|
||||||
}
|
}
|
||||||
defer unix.Close(s)
|
defer unix.Close(s)
|
||||||
|
|
||||||
ifr := ifreqAlias4{
|
var ifr ifreqAlias4
|
||||||
Name: t.deviceBytes(),
|
copy(ifr.Name[:], name)
|
||||||
Addr: unix.RawSockaddrInet4{
|
|
||||||
Len: unix.SizeofSockaddrInet4,
|
// Set the address
|
||||||
Family: unix.AF_INET,
|
ifr.Addr = unix.RawSockaddrInet4{
|
||||||
Addr: network.Addr().As4(),
|
Len: unix.SizeofSockaddrInet4,
|
||||||
},
|
Family: unix.AF_INET,
|
||||||
DstAddr: unix.RawSockaddrInet4{
|
Addr: network.Addr().As4(),
|
||||||
Len: unix.SizeofSockaddrInet4,
|
}
|
||||||
Family: unix.AF_INET,
|
|
||||||
Addr: network.Addr().As4(),
|
// Set the destination address (same as address for point-to-point)
|
||||||
},
|
ifr.DstAddr = unix.RawSockaddrInet4{
|
||||||
MaskAddr: unix.RawSockaddrInet4{
|
Len: unix.SizeofSockaddrInet4,
|
||||||
Len: unix.SizeofSockaddrInet4,
|
Family: unix.AF_INET,
|
||||||
Family: unix.AF_INET,
|
Addr: network.Addr().As4(),
|
||||||
Addr: prefixToMask(network).As4(),
|
}
|
||||||
},
|
|
||||||
|
// Set the netmask
|
||||||
|
ifr.MaskAddr = unix.RawSockaddrInet4{
|
||||||
|
Len: unix.SizeofSockaddrInet4,
|
||||||
|
Family: unix.AF_INET,
|
||||||
|
Addr: prefixToMask(network).As4(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
||||||
return fmt.Errorf("failed to set tun v4 address: %s", err)
|
return fmt.Errorf("failed to set IPv4 address via ioctl: %w", err)
|
||||||
}
|
|
||||||
|
|
||||||
err = addRoute(network, t.linkAddr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) activate6(network netip.Prefix) error {
|
func (rm *tun) addIPv6(name string, network netip.Prefix) error {
|
||||||
s, err := unix.Socket(
|
// Open an IPv6 socket for ioctl
|
||||||
unix.AF_INET6,
|
s, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
unix.SOCK_DGRAM,
|
|
||||||
unix.IPPROTO_IP,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to create IPv6 socket: %w", err)
|
||||||
}
|
}
|
||||||
defer unix.Close(s)
|
defer unix.Close(s)
|
||||||
|
|
||||||
ifr := ifreqAlias6{
|
var ifr ifreqAlias6
|
||||||
Name: t.deviceBytes(),
|
copy(ifr.Name[:], name)
|
||||||
Addr: unix.RawSockaddrInet6{
|
|
||||||
Len: unix.SizeofSockaddrInet6,
|
// Set the address
|
||||||
Family: unix.AF_INET6,
|
ifr.Addr = unix.RawSockaddrInet6{
|
||||||
Addr: network.Addr().As16(),
|
Len: unix.SizeofSockaddrInet6,
|
||||||
},
|
Family: unix.AF_INET6,
|
||||||
PrefixMask: unix.RawSockaddrInet6{
|
Addr: network.Addr().As16(),
|
||||||
Len: unix.SizeofSockaddrInet6,
|
|
||||||
Family: unix.AF_INET6,
|
|
||||||
Addr: prefixToMask(network).As16(),
|
|
||||||
},
|
|
||||||
Lifetime: addrLifetime{
|
|
||||||
// never expires
|
|
||||||
Vltime: 0xffffffff,
|
|
||||||
Pltime: 0xffffffff,
|
|
||||||
},
|
|
||||||
//TODO: CERT-V2 should we disable DAD (duplicate address detection) and mark this as a secured address?
|
|
||||||
Flags: _IN6_IFF_NODAD,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the prefix mask
|
||||||
|
ifr.PrefixMask = unix.RawSockaddrInet6{
|
||||||
|
Len: unix.SizeofSockaddrInet6,
|
||||||
|
Family: unix.AF_INET6,
|
||||||
|
Addr: prefixToMask(network).As16(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set lifetime (never expires)
|
||||||
|
ifr.Lifetime = addrLifetime{
|
||||||
|
Vltime: 0xffffffff,
|
||||||
|
Pltime: 0xffffffff,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set flags (no DAD - Duplicate Address Detection)
|
||||||
|
ifr.Flags = _IN6_IFF_NODAD
|
||||||
|
|
||||||
if err := ioctl(uintptr(s), _SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
if err := ioctl(uintptr(s), _SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
||||||
return fmt.Errorf("failed to set tun address: %s", err)
|
return fmt.Errorf("failed to set IPv6 address via ioctl: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !initial && !change {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
routeTree, err := makeRouteTree(t.l, routes, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Teach nebula how to handle the routes before establishing them in the system table
|
|
||||||
oldRoutes := t.Routes.Swap(&routes)
|
|
||||||
t.routeTree.Store(routeTree)
|
|
||||||
|
|
||||||
if !initial {
|
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
||||||
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
||||||
if err != nil {
|
|
||||||
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure any routes we actually want are installed
|
|
||||||
err = t.addRoutes(true)
|
|
||||||
if err != nil {
|
|
||||||
// Catch any stray logs
|
|
||||||
util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
|
||||||
r, ok := t.routeTree.Load().Lookup(ip)
|
|
||||||
if ok {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
return routing.Gateways{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the LinkAddr for the interface of the given name
|
|
||||||
// Is there an easier way to fetch this when we create the interface?
|
|
||||||
// Maybe SIOCGIFINDEX? but this doesn't appear to exist in the darwin headers.
|
|
||||||
func getLinkAddr(name string) (*netroute.LinkAddr, error) {
|
func getLinkAddr(name string) (*netroute.LinkAddr, error) {
|
||||||
rib, err := netroute.FetchRIB(unix.AF_UNSPEC, unix.NET_RT_IFLIST, 0)
|
rib, err := netroute.FetchRIB(unix.AF_UNSPEC, unix.NET_RT_IFLIST, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -379,53 +393,7 @@ func getLinkAddr(name string) (*netroute.LinkAddr, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (rm *tun) addRoute(prefix netip.Prefix) error {
|
||||||
routes := *t.Routes.Load()
|
|
||||||
|
|
||||||
for _, r := range routes {
|
|
||||||
if len(r.Via) == 0 || !r.Install {
|
|
||||||
// We don't allow route MTUs so only install routes with a via
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
err := addRoute(r.Cidr, t.linkAddr)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, unix.EEXIST) {
|
|
||||||
t.l.WithField("route", r.Cidr).
|
|
||||||
Warnf("unable to add unsafe_route, identical route already exists")
|
|
||||||
} else {
|
|
||||||
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
|
||||||
if logErrors {
|
|
||||||
retErr.Log(t.l)
|
|
||||||
} else {
|
|
||||||
return retErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.l.WithField("route", r).Info("Added route")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
|
||||||
for _, r := range routes {
|
|
||||||
if !r.Install {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
err := delRoute(r.Cidr, t.linkAddr)
|
|
||||||
if err != nil {
|
|
||||||
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
|
||||||
} else {
|
|
||||||
t.l.WithField("route", r).Info("Removed route")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|
||||||
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
||||||
@@ -443,13 +411,13 @@ func addRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
route.Addrs = []netroute.Addr{
|
route.Addrs = []netroute.Addr{
|
||||||
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
||||||
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
||||||
unix.RTAX_GATEWAY: gateway,
|
unix.RTAX_GATEWAY: rm.linkAddr,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
route.Addrs = []netroute.Addr{
|
route.Addrs = []netroute.Addr{
|
||||||
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
||||||
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
||||||
unix.RTAX_GATEWAY: gateway,
|
unix.RTAX_GATEWAY: rm.linkAddr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,7 +434,7 @@ func addRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
func (rm *tun) delRoute(prefix netip.Prefix) error {
|
||||||
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
||||||
@@ -483,13 +451,13 @@ func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
route.Addrs = []netroute.Addr{
|
route.Addrs = []netroute.Addr{
|
||||||
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
||||||
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
||||||
unix.RTAX_GATEWAY: gateway,
|
unix.RTAX_GATEWAY: rm.linkAddr,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
route.Addrs = []netroute.Addr{
|
route.Addrs = []netroute.Addr{
|
||||||
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
||||||
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
||||||
unix.RTAX_GATEWAY: gateway,
|
unix.RTAX_GATEWAY: rm.linkAddr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,6 +465,7 @@ func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create route.RouteMessage: %w", err)
|
return fmt.Errorf("failed to create route.RouteMessage: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = unix.Write(sock, data[:])
|
_, err = unix.Write(sock, data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
|
return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
|
||||||
@@ -505,62 +474,34 @@ func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
func ioctl(a1, a2, a3 uintptr) error {
|
||||||
buf := make([]byte, len(to)+4)
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
|
||||||
|
if errno != 0 {
|
||||||
n, err := t.ReadWriteCloser.Read(buf)
|
return errno
|
||||||
|
|
||||||
copy(to, buf[4:])
|
|
||||||
return n - 4, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write is only valid for single threaded use
|
|
||||||
func (t *tun) Write(from []byte) (int, error) {
|
|
||||||
buf := t.out
|
|
||||||
if cap(buf) < len(from)+4 {
|
|
||||||
buf = make([]byte, len(from)+4)
|
|
||||||
t.out = buf
|
|
||||||
}
|
}
|
||||||
buf = buf[:len(from)+4]
|
return nil
|
||||||
|
|
||||||
if len(from) == 0 {
|
|
||||||
return 0, syscall.EIO
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine the IP Family for the NULL L2 Header
|
|
||||||
ipVer := from[0] >> 4
|
|
||||||
if ipVer == 4 {
|
|
||||||
buf[3] = syscall.AF_INET
|
|
||||||
} else if ipVer == 6 {
|
|
||||||
buf[3] = syscall.AF_INET6
|
|
||||||
} else {
|
|
||||||
return 0, fmt.Errorf("unable to determine IP version from packet")
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(buf[4:], from)
|
|
||||||
|
|
||||||
n, err := t.ReadWriteCloser.Write(buf)
|
|
||||||
return n - 4, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Networks() []netip.Prefix {
|
|
||||||
return t.vpnNetworks
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Name() string {
|
|
||||||
return t.Device
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func prefixToMask(prefix netip.Prefix) netip.Addr {
|
func prefixToMask(prefix netip.Prefix) netip.Addr {
|
||||||
pLen := 128
|
bits := prefix.Bits()
|
||||||
if prefix.Addr().Is4() {
|
if prefix.Addr().Is4() {
|
||||||
pLen = 32
|
// Create IPv4 netmask from prefix length
|
||||||
|
mask := ^uint32(0) << (32 - bits)
|
||||||
|
return netip.AddrFrom4([4]byte{
|
||||||
|
byte(mask >> 24),
|
||||||
|
byte(mask >> 16),
|
||||||
|
byte(mask >> 8),
|
||||||
|
byte(mask),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Create IPv6 netmask from prefix length
|
||||||
|
var mask [16]byte
|
||||||
|
for i := 0; i < bits/8; i++ {
|
||||||
|
mask[i] = 0xff
|
||||||
|
}
|
||||||
|
if bits%8 != 0 {
|
||||||
|
mask[bits/8] = ^byte(0) << (8 - bits%8)
|
||||||
|
}
|
||||||
|
return netip.AddrFrom16(mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, _ := netip.AddrFromSlice(net.CIDRMask(prefix.Bits(), pLen))
|
|
||||||
return addr
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,163 +1,77 @@
|
|||||||
//go:build !e2e_testing
|
//go:build freebsd && !e2e_testing
|
||||||
// +build !e2e_testing
|
// +build freebsd,!e2e_testing
|
||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
type tun struct{}
|
||||||
// FIODGNAME is defined in sys/sys/filio.h on FreeBSD
|
|
||||||
// For 32-bit systems, use FIODGNAME_32 (not defined in this file: 0x80086678)
|
|
||||||
FIODGNAME = 0x80106678
|
|
||||||
)
|
|
||||||
|
|
||||||
type fiodgnameArg struct {
|
|
||||||
length int32
|
|
||||||
pad [4]byte
|
|
||||||
buf unsafe.Pointer
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ifreqRename is used for renaming network interfaces on FreeBSD
|
||||||
type ifreqRename struct {
|
type ifreqRename struct {
|
||||||
Name [16]byte
|
Name [unix.IFNAMSIZ]byte
|
||||||
Data uintptr
|
Data uintptr
|
||||||
}
|
}
|
||||||
|
|
||||||
type ifreqDestroy struct {
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*wgTun, error) {
|
||||||
Name [16]byte
|
return nil, fmt.Errorf("newTunFromFd not supported on FreeBSD")
|
||||||
pad [16]byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type tun struct {
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*wgTun, error) {
|
||||||
Device string
|
deviceName := c.GetString("tun.dev", "tun")
|
||||||
vpnNetworks []netip.Prefix
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
MTU int
|
|
||||||
Routes atomic.Pointer[[]Route]
|
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
|
||||||
l *logrus.Logger
|
|
||||||
|
|
||||||
io.ReadWriteCloser
|
// Create WireGuard TUN device
|
||||||
}
|
tunDevice, err := wgtun.CreateTUN(deviceName, mtu)
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
|
||||||
if t.ReadWriteCloser != nil {
|
|
||||||
if err := t.ReadWriteCloser.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer syscall.Close(s)
|
|
||||||
|
|
||||||
ifreq := ifreqDestroy{Name: t.deviceBytes()}
|
|
||||||
|
|
||||||
// Destroy the interface
|
|
||||||
err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifreq)))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
|
||||||
// Try to open existing tun device
|
|
||||||
var file *os.File
|
|
||||||
var err error
|
|
||||||
deviceName := c.GetString("tun.dev", "")
|
|
||||||
if deviceName != "" {
|
|
||||||
file, err = os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
|
|
||||||
}
|
|
||||||
if errors.Is(err, fs.ErrNotExist) || deviceName == "" {
|
|
||||||
// If the device doesn't already exist, request a new one and rename it
|
|
||||||
file, err = os.OpenFile("/dev/tun", os.O_RDWR, 0)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to create TUN device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawConn, err := file.SyscallConn()
|
// Get the actual device name
|
||||||
|
actualName, err := tunDevice.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("SyscallConn: %v", err)
|
tunDevice.Close()
|
||||||
}
|
return nil, fmt.Errorf("failed to get TUN device name: %w", err)
|
||||||
|
|
||||||
var name [16]byte
|
|
||||||
var ctrlErr error
|
|
||||||
rawConn.Control(func(fd uintptr) {
|
|
||||||
// Read the name of the interface
|
|
||||||
arg := fiodgnameArg{length: 16, buf: unsafe.Pointer(&name)}
|
|
||||||
ctrlErr = ioctl(fd, FIODGNAME, uintptr(unsafe.Pointer(&arg)))
|
|
||||||
})
|
|
||||||
if ctrlErr != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ifName := string(bytes.TrimRight(name[:], "\x00"))
|
|
||||||
if deviceName == "" {
|
|
||||||
deviceName = ifName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the name doesn't match the desired interface name, rename it now
|
// If the name doesn't match the desired interface name, rename it now
|
||||||
if ifName != deviceName {
|
if actualName != deviceName && deviceName != "" && deviceName != "tun" {
|
||||||
s, err := syscall.Socket(
|
if err := renameInterface(actualName, deviceName); err != nil {
|
||||||
syscall.AF_INET,
|
tunDevice.Close()
|
||||||
syscall.SOCK_DGRAM,
|
return nil, fmt.Errorf("failed to rename interface from %s to %s: %w", actualName, deviceName, err)
|
||||||
syscall.IPPROTO_IP,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
defer syscall.Close(s)
|
actualName = deviceName
|
||||||
|
|
||||||
fd := uintptr(s)
|
|
||||||
|
|
||||||
var fromName [16]byte
|
|
||||||
var toName [16]byte
|
|
||||||
copy(fromName[:], ifName)
|
|
||||||
copy(toName[:], deviceName)
|
|
||||||
|
|
||||||
ifrr := ifreqRename{
|
|
||||||
Name: fromName,
|
|
||||||
Data: uintptr(unsafe.Pointer(&toName)),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the device name
|
|
||||||
ioctl(fd, syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&ifrr)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &tun{
|
t := &wgTun{
|
||||||
ReadWriteCloser: file,
|
tunDevice: tunDevice,
|
||||||
Device: deviceName,
|
vpnNetworks: vpnNetworks,
|
||||||
vpnNetworks: vpnNetworks,
|
MaxMTU: mtu,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
DefaultMTU: mtu,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create FreeBSD-specific route manager
|
||||||
|
t.routeManager = &tun{}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,141 +82,194 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
l.WithField("name", actualName).Info("Created WireGuard TUN device")
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addIp(cidr netip.Prefix) error {
|
func (rm *tun) Activate(t *wgTun) error {
|
||||||
var err error
|
name, err := t.tunDevice.Name()
|
||||||
// TODO use syscalls instead of exec.Command
|
if err != nil {
|
||||||
cmd := exec.Command("/sbin/ifconfig", t.Device, cidr.String(), cidr.Addr().String())
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/route", "-n", "add", "-net", cidr.String(), "-interface", t.Device)
|
// Set the MTU
|
||||||
t.l.Debug("command: ", cmd.String())
|
rm.SetMTU(t, t.MaxMTU)
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'route add': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
|
// Add IP addresses
|
||||||
t.l.Debug("command: ", cmd.String())
|
for _, network := range t.vpnNetworks {
|
||||||
if err = cmd.Run(); err != nil {
|
if err := rm.addIP(t, name, network); err != nil {
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsafe path routes
|
|
||||||
return t.addRoutes(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Activate() error {
|
|
||||||
for i := range t.vpnNetworks {
|
|
||||||
err := t.addIp(t.vpnNetworks[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
// Bring up the interface
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
if err := runCommandBSD("ifconfig", name, "up"); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("failed to bring up interface: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the routes
|
||||||
|
if err := rm.AddRoutes(t, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !initial && !change {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
routeTree, err := makeRouteTree(t.l, routes, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Teach nebula how to handle the routes before establishing them in the system table
|
|
||||||
oldRoutes := t.Routes.Swap(&routes)
|
|
||||||
t.routeTree.Store(routeTree)
|
|
||||||
|
|
||||||
if !initial {
|
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
||||||
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
||||||
if err != nil {
|
|
||||||
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure any routes we actually want are installed
|
|
||||||
err = t.addRoutes(true)
|
|
||||||
if err != nil {
|
|
||||||
// Catch any stray logs
|
|
||||||
util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
func (rm *tun) SetMTU(t *wgTun, mtu int) {
|
||||||
r, _ := t.routeTree.Load().Lookup(ip)
|
name, err := t.tunDevice.Name()
|
||||||
return r
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get device name for MTU set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := runCommandBSD("ifconfig", name, "mtu", strconv.Itoa(mtu)); err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to set tun mtu")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Networks() []netip.Prefix {
|
func (rm *tun) SetDefaultRoute(t *wgTun, cidr netip.Prefix) error {
|
||||||
return t.vpnNetworks
|
// On FreeBSD, routes are set via ifconfig and route commands
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Name() string {
|
func (rm *tun) AddRoutes(t *wgTun, logErrors bool) error {
|
||||||
return t.Device
|
name, err := t.tunDevice.Name()
|
||||||
}
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
for _, r := range routes {
|
|
||||||
if len(r.Via) == 0 || !r.Install {
|
|
||||||
// We don't allow route MTUs so only install routes with a via
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
|
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
retErr := util.NewContextualError("failed to run 'route add' for unsafe_route", map[string]any{"route": r}, err)
|
|
||||||
if logErrors {
|
|
||||||
retErr.Log(t.l)
|
|
||||||
} else {
|
|
||||||
return retErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("/sbin/route", "-n", "delete", "-net", r.Cidr.String(), "-interface", t.Device)
|
// Add route using route command
|
||||||
t.l.Debug("command: ", cmd.String())
|
args := []string{"add"}
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
|
if r.Cidr.Addr().Is6() {
|
||||||
|
args = append(args, "-inet6")
|
||||||
|
} else {
|
||||||
|
args = append(args, "-inet")
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, r.Cidr.String(), "-interface", name)
|
||||||
|
|
||||||
|
if r.Metric > 0 {
|
||||||
|
// FreeBSD doesn't support route metrics directly like Linux
|
||||||
|
t.l.WithField("route", r).Warn("Route metrics are not fully supported on FreeBSD")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := runCommandBSD("route", args...)
|
||||||
|
if err != nil {
|
||||||
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
|
if logErrors {
|
||||||
|
retErr.Log(t.l)
|
||||||
|
} else {
|
||||||
|
return retErr
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.l.WithField("route", r).Info("Added route")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) RemoveRoutes(t *wgTun, routes []Route) {
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get device name for route removal")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range routes {
|
||||||
|
if !r.Install {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{"delete"}
|
||||||
|
|
||||||
|
if r.Cidr.Addr().Is6() {
|
||||||
|
args = append(args, "-inet6")
|
||||||
|
} else {
|
||||||
|
args = append(args, "-inet")
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, r.Cidr.String(), "-interface", name)
|
||||||
|
|
||||||
|
err := runCommandBSD("route", args...)
|
||||||
|
if err != nil {
|
||||||
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("route", r).Info("Removed route")
|
t.l.WithField("route", r).Info("Removed route")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) NewMultiQueueReader(t *wgTun) (io.ReadWriteCloser, error) {
|
||||||
|
// FreeBSD doesn't support multi-queue TUN devices in the same way as Linux
|
||||||
|
// Return a reader that wraps the same device
|
||||||
|
return &wgTunReader{
|
||||||
|
parent: t,
|
||||||
|
tunDevice: t.tunDevice,
|
||||||
|
offset: 0,
|
||||||
|
l: t.l,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIP(t *wgTun, name string, network netip.Prefix) error {
|
||||||
|
addr := network.Addr()
|
||||||
|
|
||||||
|
if addr.Is4() {
|
||||||
|
// For IPv4: ifconfig tun0 10.0.0.1/24
|
||||||
|
if err := runCommandBSD("ifconfig", name, network.String()); err != nil {
|
||||||
|
return fmt.Errorf("failed to add IPv4 address: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// For IPv6: ifconfig tun0 inet6 add 2001:db8::1/64
|
||||||
|
if err := runCommandBSD("ifconfig", name, "inet6", "add", network.String()); err != nil {
|
||||||
|
return fmt.Errorf("failed to add IPv6 address: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) deviceBytes() (o [16]byte) {
|
func runCommandBSD(name string, args ...string) error {
|
||||||
for i, c := range t.Device {
|
cmd := exec.Command(name, args...)
|
||||||
o[i] = byte(c)
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%s %s failed: %w\nOutput: %s", name, strings.Join(args, " "), err, string(output))
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func renameInterface(fromName, toName string) error {
|
||||||
|
s, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create socket: %w", err)
|
||||||
|
}
|
||||||
|
defer syscall.Close(s)
|
||||||
|
|
||||||
|
fd := uintptr(s)
|
||||||
|
|
||||||
|
var fromNameBytes [unix.IFNAMSIZ]byte
|
||||||
|
var toNameBytes [unix.IFNAMSIZ]byte
|
||||||
|
copy(fromNameBytes[:], fromName)
|
||||||
|
copy(toNameBytes[:], toName)
|
||||||
|
|
||||||
|
ifrr := ifreqRename{
|
||||||
|
Name: fromNameBytes,
|
||||||
|
Data: uintptr(unsafe.Pointer(&toNameBytes)),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the device name using SIOCSIFNAME ioctl
|
||||||
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&ifrr)))
|
||||||
|
if errno != 0 {
|
||||||
|
return fmt.Errorf("SIOCSIFNAME ioctl failed: %w", errno)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//go:build !android && !e2e_testing
|
//go:build linux && !android && !e2e_testing
|
||||||
// +build !android,!e2e_testing
|
// +build linux,!android,!e2e_testing
|
||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
@@ -9,133 +9,105 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
deviceIndex int
|
||||||
fd int
|
ioctlFd uintptr
|
||||||
Device string
|
txQueueLen int
|
||||||
vpnNetworks []netip.Prefix
|
|
||||||
MaxMTU int
|
|
||||||
DefaultMTU int
|
|
||||||
TXQueueLen int
|
|
||||||
deviceIndex int
|
|
||||||
ioctlFd uintptr
|
|
||||||
|
|
||||||
Routes atomic.Pointer[[]Route]
|
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
|
||||||
routeChan chan struct{}
|
|
||||||
useSystemRoutes bool
|
useSystemRoutes bool
|
||||||
useSystemRoutesBufferSize int
|
useSystemRoutesBufferSize int
|
||||||
|
|
||||||
l *logrus.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Networks() []netip.Prefix {
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*wgTun, error) {
|
||||||
return t.vpnNetworks
|
deviceName := c.GetString("tun.dev", "")
|
||||||
}
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
|
||||||
type ifReq struct {
|
// Create WireGuard TUN device
|
||||||
Name [16]byte
|
tunDevice, err := wgtun.CreateTUN(deviceName, mtu)
|
||||||
Flags uint16
|
|
||||||
pad [8]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type ifreqMTU struct {
|
|
||||||
Name [16]byte
|
|
||||||
MTU int32
|
|
||||||
pad [8]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type ifreqQLEN struct {
|
|
||||||
Name [16]byte
|
|
||||||
Value int32
|
|
||||||
pad [8]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFromFd(c *config.C, l *logrus.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
|
||||||
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
|
||||||
|
|
||||||
t, err := newTunGeneric(c, l, file, vpnNetworks)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to create TUN device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Device = "tun0"
|
// Get the actual device name
|
||||||
|
actualName, err := tunDevice.Name()
|
||||||
return t, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) {
|
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If /dev/net/tun doesn't exist, try to create it (will happen in docker)
|
tunDevice.Close()
|
||||||
if os.IsNotExist(err) {
|
return nil, fmt.Errorf("failed to get TUN device name: %w", err)
|
||||||
err = os.MkdirAll("/dev/net", 0755)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("/dev/net/tun doesn't exist, failed to mkdir -p /dev/net: %w", err)
|
|
||||||
}
|
|
||||||
err = unix.Mknod("/dev/net/tun", unix.S_IFCHR|0600, int(unix.Mkdev(10, 200)))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create /dev/net/tun: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fd, err = unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("created /dev/net/tun, but still failed: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var req ifReq
|
t := &wgTun{
|
||||||
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI)
|
tunDevice: tunDevice,
|
||||||
if multiqueue {
|
vpnNetworks: vpnNetworks,
|
||||||
req.Flags |= unix.IFF_MULTI_QUEUE
|
MaxMTU: mtu,
|
||||||
}
|
DefaultMTU: mtu,
|
||||||
copy(req.Name[:], c.GetString("tun.dev", ""))
|
l: l,
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
name := strings.Trim(string(req.Name[:]), "\x00")
|
|
||||||
|
|
||||||
file := os.NewFile(uintptr(fd), "/dev/net/tun")
|
|
||||||
t, err := newTunGeneric(c, l, file, vpnNetworks)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Device = name
|
// Create Linux-specific route manager
|
||||||
|
routeManager := &tun{
|
||||||
return t, nil
|
txQueueLen: c.GetInt("tun.tx_queue", 500),
|
||||||
}
|
|
||||||
|
|
||||||
func newTunGeneric(c *config.C, l *logrus.Logger, file *os.File, vpnNetworks []netip.Prefix) (*tun, error) {
|
|
||||||
t := &tun{
|
|
||||||
ReadWriteCloser: file,
|
|
||||||
fd: int(file.Fd()),
|
|
||||||
vpnNetworks: vpnNetworks,
|
|
||||||
TXQueueLen: c.GetInt("tun.tx_queue", 500),
|
|
||||||
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
||||||
useSystemRoutesBufferSize: c.GetInt("tun.use_system_route_table_buffer_size", 0),
|
useSystemRoutesBufferSize: c.GetInt("tun.use_system_route_table_buffer_size", 0),
|
||||||
l: l,
|
}
|
||||||
|
t.routeManager = routeManager
|
||||||
|
|
||||||
|
err = t.reload(c, true)
|
||||||
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err := t.reload(c, true)
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
|
err := t.reload(c, false)
|
||||||
|
if err != nil {
|
||||||
|
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
l.WithField("name", actualName).Info("Created WireGuard TUN device")
|
||||||
|
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTunFromFd(c *config.C, l *logrus.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*wgTun, error) {
|
||||||
|
// Create TUN device from file descriptor
|
||||||
|
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
||||||
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
tunDevice, err := wgtun.CreateTUNFromFile(file, mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create TUN device from fd: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t := &wgTun{
|
||||||
|
tunDevice: tunDevice,
|
||||||
|
vpnNetworks: vpnNetworks,
|
||||||
|
MaxMTU: mtu,
|
||||||
|
DefaultMTU: mtu,
|
||||||
|
l: l,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Linux-specific route manager
|
||||||
|
routeManager := &tun{
|
||||||
|
txQueueLen: c.GetInt("tun.tx_queue", 500),
|
||||||
|
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
||||||
|
useSystemRoutesBufferSize: c.GetInt("tun.use_system_route_table_buffer_size", 0),
|
||||||
|
}
|
||||||
|
t.routeManager = routeManager
|
||||||
|
|
||||||
|
err = t.reload(c, true)
|
||||||
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,269 +121,105 @@ func newTunGeneric(c *config.C, l *logrus.Logger, file *os.File, vpnNetworks []n
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (rm *tun) Activate(t *wgTun) error {
|
||||||
routeChange, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
name, err := t.tunDevice.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !initial && !routeChange && !c.HasChanged("tun.mtu") {
|
if t.routeManager.useSystemRoutes {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
routeTree, err := makeRouteTree(t.l, routes, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
oldDefaultMTU := t.DefaultMTU
|
|
||||||
oldMaxMTU := t.MaxMTU
|
|
||||||
newDefaultMTU := c.GetInt("tun.mtu", DefaultMTU)
|
|
||||||
newMaxMTU := newDefaultMTU
|
|
||||||
for i, r := range routes {
|
|
||||||
if r.MTU == 0 {
|
|
||||||
routes[i].MTU = newDefaultMTU
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.MTU > t.MaxMTU {
|
|
||||||
newMaxMTU = r.MTU
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t.MaxMTU = newMaxMTU
|
|
||||||
t.DefaultMTU = newDefaultMTU
|
|
||||||
|
|
||||||
// Teach nebula how to handle the routes before establishing them in the system table
|
|
||||||
oldRoutes := t.Routes.Swap(&routes)
|
|
||||||
t.routeTree.Store(routeTree)
|
|
||||||
|
|
||||||
if !initial {
|
|
||||||
if oldMaxMTU != newMaxMTU {
|
|
||||||
t.setMTU()
|
|
||||||
t.l.Infof("Set max MTU to %v was %v", t.MaxMTU, oldMaxMTU)
|
|
||||||
}
|
|
||||||
|
|
||||||
if oldDefaultMTU != newDefaultMTU {
|
|
||||||
for i := range t.vpnNetworks {
|
|
||||||
err := t.setDefaultRoute(t.vpnNetworks[i])
|
|
||||||
if err != nil {
|
|
||||||
t.l.Warn(err)
|
|
||||||
} else {
|
|
||||||
t.l.Infof("Set default MTU to %v was %v", t.DefaultMTU, oldDefaultMTU)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
||||||
t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
||||||
|
|
||||||
// Ensure any routes we actually want are installed
|
|
||||||
err = t.addRoutes(true)
|
|
||||||
if err != nil {
|
|
||||||
// This should never be called since addRoutes should log its own errors in a reload condition
|
|
||||||
util.LogWithContextIfNeeded("Failed to refresh routes", err, t.l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var req ifReq
|
|
||||||
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
|
|
||||||
copy(req.Name[:], t.Device)
|
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := os.NewFile(uintptr(fd), "/dev/net/tun")
|
|
||||||
|
|
||||||
return file, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
|
||||||
r, _ := t.routeTree.Load().Lookup(ip)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Write(b []byte) (int, error) {
|
|
||||||
var nn int
|
|
||||||
maximum := len(b)
|
|
||||||
|
|
||||||
for {
|
|
||||||
n, err := unix.Write(t.fd, b[nn:maximum])
|
|
||||||
if n > 0 {
|
|
||||||
nn += n
|
|
||||||
}
|
|
||||||
if nn == len(b) {
|
|
||||||
return nn, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nn, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
return nn, io.ErrUnexpectedEOF
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) deviceBytes() (o [16]byte) {
|
|
||||||
for i, c := range t.Device {
|
|
||||||
o[i] = byte(c)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasNetlinkAddr(al []*netlink.Addr, x netlink.Addr) bool {
|
|
||||||
for i := range al {
|
|
||||||
if al[i].Equal(x) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// addIPs uses netlink to add all addresses that don't exist, then it removes ones that should not be there
|
|
||||||
func (t *tun) addIPs(link netlink.Link) error {
|
|
||||||
newAddrs := make([]*netlink.Addr, len(t.vpnNetworks))
|
|
||||||
for i := range t.vpnNetworks {
|
|
||||||
newAddrs[i] = &netlink.Addr{
|
|
||||||
IPNet: &net.IPNet{
|
|
||||||
IP: t.vpnNetworks[i].Addr().AsSlice(),
|
|
||||||
Mask: net.CIDRMask(t.vpnNetworks[i].Bits(), t.vpnNetworks[i].Addr().BitLen()),
|
|
||||||
},
|
|
||||||
Label: t.vpnNetworks[i].Addr().Zone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//add all new addresses
|
|
||||||
for i := range newAddrs {
|
|
||||||
//TODO: CERT-V2 do we want to stack errors and try as many ops as possible?
|
|
||||||
//AddrReplace still adds new IPs, but if their properties change it will change them as well
|
|
||||||
if err := netlink.AddrReplace(link, newAddrs[i]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//iterate over remainder, remove whoever shouldn't be there
|
|
||||||
al, err := netlink.AddrList(link, netlink.FAMILY_ALL)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get tun address list: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range al {
|
|
||||||
if hasNetlinkAddr(newAddrs, al[i]) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = netlink.AddrDel(link, &al[i])
|
|
||||||
if err != nil {
|
|
||||||
t.l.WithError(err).Error("failed to remove address from tun address list")
|
|
||||||
} else {
|
|
||||||
t.l.WithField("removed", al[i].String()).Info("removed address not listed in cert(s)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Activate() error {
|
|
||||||
devName := t.deviceBytes()
|
|
||||||
|
|
||||||
if t.useSystemRoutes {
|
|
||||||
t.watchRoutes()
|
t.watchRoutes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the netlink device
|
||||||
|
link, err := netlink.LinkByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get tun device link: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rm.deviceIndex = link.Attrs().Index
|
||||||
|
|
||||||
|
// Open socket for ioctl operations
|
||||||
s, err := unix.Socket(
|
s, err := unix.Socket(
|
||||||
unix.AF_INET, //because everything we use t.ioctlFd for is address family independent, this is fine
|
unix.AF_INET,
|
||||||
unix.SOCK_DGRAM,
|
unix.SOCK_DGRAM,
|
||||||
unix.IPPROTO_IP,
|
unix.IPPROTO_IP,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.ioctlFd = uintptr(s)
|
rm.ioctlFd = uintptr(s)
|
||||||
|
|
||||||
// Set the device name
|
rm.SetMTU(t, t.MaxMTU)
|
||||||
ifrf := ifReq{Name: devName}
|
|
||||||
if err = ioctl(t.ioctlFd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to set tun device name: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
link, err := netlink.LinkByName(t.Device)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get tun device link: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.deviceIndex = link.Attrs().Index
|
|
||||||
|
|
||||||
// Setup our default MTU
|
|
||||||
t.setMTU()
|
|
||||||
|
|
||||||
// Set the transmit queue length
|
// Set the transmit queue length
|
||||||
ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)}
|
devName := deviceBytes(name)
|
||||||
if err = ioctl(t.ioctlFd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
|
ifrq := ifreqQLEN{Name: devName, Value: int32(rm.txQueueLen)}
|
||||||
|
if err = ioctl(t.routeManager.ioctlFd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
|
||||||
// If we can't set the queue length nebula will still work but it may lead to packet loss
|
// If we can't set the queue length nebula will still work but it may lead to packet loss
|
||||||
t.l.WithError(err).Error("Failed to set tun tx queue length")
|
t.l.WithError(err).Error("Failed to set tun tx queue length")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = t.addIPs(link); err != nil {
|
// Disable IPv6 link-local address generation
|
||||||
|
const modeNone = 1
|
||||||
|
if err = netlink.LinkSetIP6AddrGenMode(link, modeNone); err != nil {
|
||||||
|
t.l.WithError(err).Warn("Failed to disable link local address generation")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add IP addresses
|
||||||
|
if err = t.routeManager.addIPs(t, link); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bring up the interface
|
// Bring up the interface
|
||||||
ifrf.Flags = ifrf.Flags | unix.IFF_UP
|
if err = netlink.LinkSetUp(link); err != nil {
|
||||||
if err = ioctl(t.ioctlFd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to bring the tun device up: %s", err)
|
return fmt.Errorf("failed to bring the tun device up: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//set route MTU
|
// Set route MTU
|
||||||
for i := range t.vpnNetworks {
|
for i := range t.vpnNetworks {
|
||||||
if err = t.setDefaultRoute(t.vpnNetworks[i]); err != nil {
|
if err = t.routeManager.SetDefaultRoute(t, t.vpnNetworks[i]); err != nil {
|
||||||
return fmt.Errorf("failed to set default route MTU: %w", err)
|
return fmt.Errorf("failed to set default route MTU: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the routes
|
// Set the routes
|
||||||
if err = t.addRoutes(false); err != nil {
|
if err = t.routeManager.AddRoutes(t, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the interface
|
|
||||||
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
|
|
||||||
if err = ioctl(t.ioctlFd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to run tun device: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) setMTU() {
|
func (rm *tun) SetMTU(t *wgTun, mtu int) {
|
||||||
// Set the MTU on the device
|
name, err := t.tunDevice.Name()
|
||||||
ifm := ifreqMTU{Name: t.deviceBytes(), MTU: int32(t.MaxMTU)}
|
if err != nil {
|
||||||
if err := ioctl(t.ioctlFd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
t.l.WithError(err).Error("Failed to get device name for MTU set")
|
||||||
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
link, err := netlink.LinkByName(name)
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get link for MTU set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := netlink.LinkSetMTU(link, mtu); err != nil {
|
||||||
t.l.WithError(err).Error("Failed to set tun mtu")
|
t.l.WithError(err).Error("Failed to set tun mtu")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
func (rm *tun) SetDefaultRoute(t *wgTun, cidr netip.Prefix) error {
|
||||||
dr := &net.IPNet{
|
dr := &net.IPNet{
|
||||||
IP: cidr.Masked().Addr().AsSlice(),
|
IP: cidr.Masked().Addr().AsSlice(),
|
||||||
Mask: net.CIDRMask(cidr.Bits(), cidr.Addr().BitLen()),
|
Mask: net.CIDRMask(cidr.Bits(), cidr.Addr().BitLen()),
|
||||||
}
|
}
|
||||||
|
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: t.deviceIndex,
|
LinkIndex: t.routeManager.deviceIndex,
|
||||||
Dst: dr,
|
Dst: dr,
|
||||||
MTU: t.DefaultMTU,
|
MTU: t.DefaultMTU,
|
||||||
AdvMSS: t.advMSS(Route{}),
|
AdvMSS: advMSS(Route{}, t.DefaultMTU, t.MaxMTU),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
Src: net.IP(cidr.Addr().AsSlice()),
|
Src: net.IP(cidr.Addr().AsSlice()),
|
||||||
Protocol: unix.RTPROT_KERNEL,
|
Protocol: unix.RTPROT_KERNEL,
|
||||||
@@ -421,7 +229,7 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
err := netlink.RouteReplace(&nr)
|
err := netlink.RouteReplace(&nr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.l.WithError(err).WithField("cidr", cidr).Warn("Failed to set default route MTU, retrying")
|
t.l.WithError(err).WithField("cidr", cidr).Warn("Failed to set default route MTU, retrying")
|
||||||
//retry twice more -- on some systems there appears to be a race condition where if we set routes too soon, netlink says `invalid argument`
|
// Retry twice more
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
err = netlink.RouteReplace(&nr)
|
err = netlink.RouteReplace(&nr)
|
||||||
@@ -439,8 +247,7 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (rm *tun) AddRoutes(t *wgTun, logErrors bool) error {
|
||||||
// Path routes
|
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
@@ -453,10 +260,10 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: t.deviceIndex,
|
LinkIndex: t.routeManager.deviceIndex,
|
||||||
Dst: dr,
|
Dst: dr,
|
||||||
MTU: r.MTU,
|
MTU: r.MTU,
|
||||||
AdvMSS: t.advMSS(r),
|
AdvMSS: advMSS(r, t.DefaultMTU, t.MaxMTU),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -480,7 +287,7 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) {
|
func (rm *tun) RemoveRoutes(t *wgTun, routes []Route) {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
continue
|
continue
|
||||||
@@ -492,10 +299,10 @@ func (t *tun) removeRoutes(routes []Route) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: t.deviceIndex,
|
LinkIndex: t.routeManager.deviceIndex,
|
||||||
Dst: dr,
|
Dst: dr,
|
||||||
MTU: r.MTU,
|
MTU: r.MTU,
|
||||||
AdvMSS: t.advMSS(r),
|
AdvMSS: advMSS(r, t.DefaultMTU, t.MaxMTU),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -512,30 +319,105 @@ func (t *tun) removeRoutes(routes []Route) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Name() string {
|
func (rm *tun) NewMultiQueueReader(t *wgTun) (io.ReadWriteCloser, error) {
|
||||||
return t.Device
|
// For Linux with WireGuard TUN, we can reuse the same device
|
||||||
|
// The vectorized I/O will handle batching
|
||||||
|
return &wgTunReader{
|
||||||
|
parent: t,
|
||||||
|
tunDevice: t.tunDevice,
|
||||||
|
offset: 0,
|
||||||
|
l: t.l,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) advMSS(r Route) int {
|
func deviceBytes(name string) [16]byte {
|
||||||
|
var o [16]byte
|
||||||
|
for i, c := range name {
|
||||||
|
if i >= 16 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
o[i] = byte(c)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func advMSS(r Route, defaultMTU, maxMTU int) int {
|
||||||
mtu := r.MTU
|
mtu := r.MTU
|
||||||
if r.MTU == 0 {
|
if r.MTU == 0 {
|
||||||
mtu = t.DefaultMTU
|
mtu = defaultMTU
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only need to set advmss if the route MTU does not match the device MTU
|
// We only need to set advmss if the route MTU does not match the device MTU
|
||||||
if mtu != t.MaxMTU {
|
if mtu != maxMTU {
|
||||||
return mtu - 40
|
return mtu - 40
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) watchRoutes() {
|
type ifreqQLEN struct {
|
||||||
|
Name [16]byte
|
||||||
|
Value int32
|
||||||
|
pad [8]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasNetlinkAddr(al []*netlink.Addr, x netlink.Addr) bool {
|
||||||
|
for i := range al {
|
||||||
|
if al[i].Equal(x) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIPs(t *wgTun, link netlink.Link) error {
|
||||||
|
newAddrs := make([]*netlink.Addr, len(t.vpnNetworks))
|
||||||
|
for i := range t.vpnNetworks {
|
||||||
|
newAddrs[i] = &netlink.Addr{
|
||||||
|
IPNet: &net.IPNet{
|
||||||
|
IP: t.vpnNetworks[i].Addr().AsSlice(),
|
||||||
|
Mask: net.CIDRMask(t.vpnNetworks[i].Bits(), t.vpnNetworks[i].Addr().BitLen()),
|
||||||
|
},
|
||||||
|
Label: t.vpnNetworks[i].Addr().Zone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all new addresses
|
||||||
|
for i := range newAddrs {
|
||||||
|
if err := netlink.AddrReplace(link, newAddrs[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over remainder, remove whoever shouldn't be there
|
||||||
|
al, err := netlink.AddrList(link, netlink.FAMILY_ALL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get tun address list: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range al {
|
||||||
|
if hasNetlinkAddr(newAddrs, al[i]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = netlink.AddrDel(link, &al[i])
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("failed to remove address from tun address list")
|
||||||
|
} else {
|
||||||
|
t.l.WithField("removed", al[i].String()).Info("removed address not listed in cert(s)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// watchRoutes monitors system route changes
|
||||||
|
func (t *wgTun) watchRoutes() {
|
||||||
|
|
||||||
rch := make(chan netlink.RouteUpdate)
|
rch := make(chan netlink.RouteUpdate)
|
||||||
doneChan := make(chan struct{})
|
doneChan := make(chan struct{})
|
||||||
|
|
||||||
netlinkOptions := netlink.RouteSubscribeOptions{
|
netlinkOptions := netlink.RouteSubscribeOptions{
|
||||||
ReceiveBufferSize: t.useSystemRoutesBufferSize,
|
ReceiveBufferSize: t.routeManager.useSystemRoutesBufferSize,
|
||||||
ReceiveBufferForceSize: t.useSystemRoutesBufferSize != 0,
|
ReceiveBufferForceSize: t.routeManager.useSystemRoutesBufferSize != 0,
|
||||||
ErrorCallback: func(e error) { t.l.WithError(e).Errorf("netlink error") },
|
ErrorCallback: func(e error) { t.l.WithError(e).Errorf("netlink error") },
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -553,91 +435,28 @@ func (t *tun) watchRoutes() {
|
|||||||
if ok {
|
if ok {
|
||||||
t.updateRoutes(r)
|
t.updateRoutes(r)
|
||||||
} else {
|
} else {
|
||||||
// may be should do something here as
|
|
||||||
// netlink stops sending updates
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-doneChan:
|
case <-doneChan:
|
||||||
// netlink.RouteSubscriber will close the rch for us
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) isGatewayInVpnNetworks(gwAddr netip.Addr) bool {
|
func (t *wgTun) updateRoutes(r netlink.RouteUpdate) {
|
||||||
withinNetworks := false
|
gateways := t.getGatewaysFromRoute(&r.Route, t.routeManager.deviceIndex)
|
||||||
for i := range t.vpnNetworks {
|
|
||||||
if t.vpnNetworks[i].Contains(gwAddr) {
|
|
||||||
withinNetworks = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return withinNetworks
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) getGatewaysFromRoute(r *netlink.Route) routing.Gateways {
|
|
||||||
|
|
||||||
var gateways routing.Gateways
|
|
||||||
|
|
||||||
link, err := netlink.LinkByName(t.Device)
|
|
||||||
if err != nil {
|
|
||||||
t.l.WithField("Devicename", t.Device).Error("Ignoring route update: failed to get link by name")
|
|
||||||
return gateways
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this route is relevant to our interface and there is a gateway then add it
|
|
||||||
if r.LinkIndex == link.Attrs().Index && len(r.Gw) > 0 {
|
|
||||||
gwAddr, ok := netip.AddrFromSlice(r.Gw)
|
|
||||||
if !ok {
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, invalid gateway address")
|
|
||||||
} else {
|
|
||||||
gwAddr = gwAddr.Unmap()
|
|
||||||
|
|
||||||
if !t.isGatewayInVpnNetworks(gwAddr) {
|
|
||||||
// Gateway isn't in our overlay network, ignore
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, not in our network")
|
|
||||||
} else {
|
|
||||||
gateways = append(gateways, routing.NewGateway(gwAddr, 1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range r.MultiPath {
|
|
||||||
// If this route is relevant to our interface and there is a gateway then add it
|
|
||||||
if p.LinkIndex == link.Attrs().Index && len(p.Gw) > 0 {
|
|
||||||
gwAddr, ok := netip.AddrFromSlice(p.Gw)
|
|
||||||
if !ok {
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring multipath route update, invalid gateway address")
|
|
||||||
} else {
|
|
||||||
gwAddr = gwAddr.Unmap()
|
|
||||||
|
|
||||||
if !t.isGatewayInVpnNetworks(gwAddr) {
|
|
||||||
// Gateway isn't in our overlay network, ignore
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, not in our network")
|
|
||||||
} else {
|
|
||||||
// p.Hops+1 = weight of the route
|
|
||||||
gateways = append(gateways, routing.NewGateway(gwAddr, p.Hops+1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
routing.CalculateBucketsForGateways(gateways)
|
|
||||||
return gateways
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|
||||||
|
|
||||||
gateways := t.getGatewaysFromRoute(&r.Route)
|
|
||||||
|
|
||||||
if len(gateways) == 0 {
|
if len(gateways) == 0 {
|
||||||
// No gateways relevant to our network, no routing changes required.
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, no gateways")
|
t.l.WithField("route", r).Debug("Ignoring route update, no gateways")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.Dst == nil {
|
||||||
|
t.l.WithField("route", r).Debug("Ignoring route update, no destination address")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
dstAddr, ok := netip.AddrFromSlice(r.Dst.IP)
|
dstAddr, ok := netip.AddrFromSlice(r.Dst.IP)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, invalid destination address")
|
t.l.WithField("route", r).Debug("Ignoring route update, invalid destination address")
|
||||||
@@ -652,7 +471,6 @@ func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|||||||
if r.Type == unix.RTM_NEWROUTE {
|
if r.Type == unix.RTM_NEWROUTE {
|
||||||
t.l.WithField("destination", dst).WithField("via", gateways).Info("Adding route")
|
t.l.WithField("destination", dst).WithField("via", gateways).Info("Adding route")
|
||||||
newTree.Insert(dst, gateways)
|
newTree.Insert(dst, gateways)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("destination", dst).WithField("via", gateways).Info("Removing route")
|
t.l.WithField("destination", dst).WithField("via", gateways).Info("Removing route")
|
||||||
newTree.Delete(dst)
|
newTree.Delete(dst)
|
||||||
@@ -660,18 +478,71 @@ func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|||||||
t.routeTree.Store(newTree)
|
t.routeTree.Store(newTree)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
func (t *wgTun) getGatewaysFromRoute(r *netlink.Route, deviceIndex int) routing.Gateways {
|
||||||
if t.routeChan != nil {
|
var gateways routing.Gateways
|
||||||
close(t.routeChan)
|
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
t.l.Error("Ignoring route update: failed to get device name")
|
||||||
|
return gateways
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.ReadWriteCloser != nil {
|
link, err := netlink.LinkByName(name)
|
||||||
_ = t.ReadWriteCloser.Close()
|
if err != nil {
|
||||||
|
t.l.WithField("DeviceName", name).Error("Ignoring route update: failed to get link by name")
|
||||||
|
return gateways
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.ioctlFd > 0 {
|
// If this route is relevant to our interface and there is a gateway then add it
|
||||||
_ = os.NewFile(t.ioctlFd, "ioctlFd").Close()
|
if r.LinkIndex == link.Attrs().Index && len(r.Gw) > 0 {
|
||||||
|
gwAddr, ok := netip.AddrFromSlice(r.Gw)
|
||||||
|
if !ok {
|
||||||
|
t.l.WithField("route", r).Debug("Ignoring route update, invalid gateway address")
|
||||||
|
} else {
|
||||||
|
gwAddr = gwAddr.Unmap()
|
||||||
|
|
||||||
|
if !t.isGatewayInVpnNetworks(gwAddr) {
|
||||||
|
t.l.WithField("route", r).Debug("Ignoring route update, not in our network")
|
||||||
|
} else {
|
||||||
|
gateways = append(gateways, routing.NewGateway(gwAddr, 1))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, p := range r.MultiPath {
|
||||||
|
if p.LinkIndex == link.Attrs().Index && len(p.Gw) > 0 {
|
||||||
|
gwAddr, ok := netip.AddrFromSlice(p.Gw)
|
||||||
|
if !ok {
|
||||||
|
t.l.WithField("route", r).Debug("Ignoring multipath route update, invalid gateway address")
|
||||||
|
} else {
|
||||||
|
gwAddr = gwAddr.Unmap()
|
||||||
|
|
||||||
|
if !t.isGatewayInVpnNetworks(gwAddr) {
|
||||||
|
t.l.WithField("route", r).Debug("Ignoring route update, not in our network")
|
||||||
|
} else {
|
||||||
|
gateways = append(gateways, routing.NewGateway(gwAddr, p.Hops+1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
routing.CalculateBucketsForGateways(gateways)
|
||||||
|
return gateways
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) isGatewayInVpnNetworks(gwAddr netip.Addr) bool {
|
||||||
|
for i := range t.vpnNetworks {
|
||||||
|
if t.vpnNetworks[i].Contains(gwAddr) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ioctl(a1, a2, a3 uintptr) error {
|
||||||
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
|
||||||
|
if errno != 0 {
|
||||||
|
return errno
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,26 +6,27 @@ package overlay
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
var runAdvMSSTests = []struct {
|
var runAdvMSSTests = []struct {
|
||||||
name string
|
name string
|
||||||
tun *tun
|
defaultMTU int
|
||||||
r Route
|
maxMTU int
|
||||||
expected int
|
r Route
|
||||||
|
expected int
|
||||||
}{
|
}{
|
||||||
// Standard case, default MTU is the device max MTU
|
// Standard case, default MTU is the device max MTU
|
||||||
{"default", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0},
|
{"default", 1440, 1440, Route{}, 0},
|
||||||
{"default-min", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0},
|
{"default-min", 1440, 1440, Route{MTU: 1440}, 0},
|
||||||
{"default-low", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160},
|
{"default-low", 1440, 1440, Route{MTU: 1200}, 1160},
|
||||||
|
|
||||||
// Case where we have a route MTU set higher than the default
|
// Case where we have a route MTU set higher than the default
|
||||||
{"route", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400},
|
{"route", 1440, 8941, Route{}, 1400},
|
||||||
{"route-min", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400},
|
{"route-min", 1440, 8941, Route{MTU: 1440}, 1400},
|
||||||
{"route-high", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0},
|
{"route-high", 1440, 8941, Route{MTU: 8941}, 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTunAdvMSS(t *testing.T) {
|
func TestTunAdvMSS(t *testing.T) {
|
||||||
for _, tt := range runAdvMSSTests {
|
for _, tt := range runAdvMSSTests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
o := tt.tun.advMSS(tt.r)
|
o := advMSS(tt.r, tt.defaultMTU, tt.maxMTU)
|
||||||
if o != tt.expected {
|
if o != tt.expected {
|
||||||
t.Errorf("got %d, want %d", o, tt.expected)
|
t.Errorf("got %d, want %d", o, tt.expected)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,12 @@
|
|||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -20,11 +19,42 @@ import (
|
|||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
|
netroute "golang.org/x/net/route"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ifreqDestroy struct {
|
const (
|
||||||
Name [16]byte
|
SIOCAIFADDR_IN6 = 0x8080696b
|
||||||
pad [16]byte
|
TUNSIFHEAD = 0x80047442
|
||||||
|
TUNSIFMODE = 0x80047458
|
||||||
|
)
|
||||||
|
|
||||||
|
type ifreqAlias4 struct {
|
||||||
|
Name [unix.IFNAMSIZ]byte
|
||||||
|
Addr unix.RawSockaddrInet4
|
||||||
|
DstAddr unix.RawSockaddrInet4
|
||||||
|
MaskAddr unix.RawSockaddrInet4
|
||||||
|
}
|
||||||
|
|
||||||
|
type ifreqAlias6 struct {
|
||||||
|
Name [unix.IFNAMSIZ]byte
|
||||||
|
Addr unix.RawSockaddrInet6
|
||||||
|
DstAddr unix.RawSockaddrInet6
|
||||||
|
PrefixMask unix.RawSockaddrInet6
|
||||||
|
Flags uint32
|
||||||
|
Lifetime addrLifetime
|
||||||
|
}
|
||||||
|
|
||||||
|
type ifreq struct {
|
||||||
|
Name [unix.IFNAMSIZ]byte
|
||||||
|
data int
|
||||||
|
}
|
||||||
|
|
||||||
|
type addrLifetime struct {
|
||||||
|
Expire uint64
|
||||||
|
Preferred uint64
|
||||||
|
Vltime uint32
|
||||||
|
Pltime uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
@@ -34,40 +64,18 @@ type tun struct {
|
|||||||
Routes atomic.Pointer[[]Route]
|
Routes atomic.Pointer[[]Route]
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
l *logrus.Logger
|
l *logrus.Logger
|
||||||
|
f *os.File
|
||||||
io.ReadWriteCloser
|
fd int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
if t.ReadWriteCloser != nil {
|
|
||||||
if err := t.ReadWriteCloser.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer syscall.Close(s)
|
|
||||||
|
|
||||||
ifreq := ifreqDestroy{Name: t.deviceBytes()}
|
|
||||||
|
|
||||||
err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifreq)))
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in NetBSD")
|
return nil, fmt.Errorf("newTunFromFd not supported in NetBSD")
|
||||||
}
|
}
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
||||||
// Try to open tun device
|
// Try to open tun device
|
||||||
var file *os.File
|
|
||||||
var err error
|
var err error
|
||||||
deviceName := c.GetString("tun.dev", "")
|
deviceName := c.GetString("tun.dev", "")
|
||||||
if deviceName == "" {
|
if deviceName == "" {
|
||||||
@@ -77,17 +85,23 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err = os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
|
fd, err := unix.Open("/dev/"+deviceName, os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = unix.SetNonblock(fd, true)
|
||||||
|
if err != nil {
|
||||||
|
l.WithError(err).Warn("Failed to set the tun device as nonblocking")
|
||||||
|
}
|
||||||
|
|
||||||
t := &tun{
|
t := &tun{
|
||||||
ReadWriteCloser: file,
|
f: os.NewFile(uintptr(fd), ""),
|
||||||
Device: deviceName,
|
fd: fd,
|
||||||
vpnNetworks: vpnNetworks,
|
Device: deviceName,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
vpnNetworks: vpnNetworks,
|
||||||
l: l,
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
@@ -105,40 +119,225 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tun) Close() error {
|
||||||
|
if t.f != nil {
|
||||||
|
if err := t.f.Close(); err != nil {
|
||||||
|
return fmt.Errorf("error closing tun file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// t.f.Close should have handled it for us but let's be extra sure
|
||||||
|
_ = unix.Close(t.fd)
|
||||||
|
|
||||||
|
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(s)
|
||||||
|
|
||||||
|
ifr := ifreq{Name: t.deviceBytes()}
|
||||||
|
err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifr)))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read(to []byte) (int, error) {
|
||||||
|
rc, err := t.f.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to get syscall conn for tun: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var errno syscall.Errno
|
||||||
|
var n uintptr
|
||||||
|
err = rc.Read(func(fd uintptr) bool {
|
||||||
|
// first 4 bytes is protocol family, in network byte order
|
||||||
|
head := [4]byte{}
|
||||||
|
iovecs := []syscall.Iovec{
|
||||||
|
{&head[0], 4},
|
||||||
|
{&to[0], uint64(len(to))},
|
||||||
|
}
|
||||||
|
|
||||||
|
n, _, errno = syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&iovecs[0])), uintptr(2))
|
||||||
|
if errno.Temporary() {
|
||||||
|
// We got an EAGAIN, EINTR, or EWOULDBLOCK, go again
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if err == syscall.EBADF || err.Error() == "use of closed file" {
|
||||||
|
// Go doesn't export poll.ErrFileClosing but happily reports it to us so here we are
|
||||||
|
// https://github.com/golang/go/blob/master/src/internal/poll/fd_poll_runtime.go#L121
|
||||||
|
return 0, os.ErrClosed
|
||||||
|
}
|
||||||
|
return 0, fmt.Errorf("failed to make read call for tun: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if errno != 0 {
|
||||||
|
return 0, fmt.Errorf("failed to make inner read call for tun: %w", errno)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fix bytes read number to exclude header
|
||||||
|
bytesRead := int(n)
|
||||||
|
if bytesRead < 0 {
|
||||||
|
return bytesRead, nil
|
||||||
|
} else if bytesRead < 4 {
|
||||||
|
return 0, nil
|
||||||
|
} else {
|
||||||
|
return bytesRead - 4, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write is only valid for single threaded use
|
||||||
|
func (t *tun) Write(from []byte) (int, error) {
|
||||||
|
if len(from) <= 1 {
|
||||||
|
return 0, syscall.EIO
|
||||||
|
}
|
||||||
|
|
||||||
|
ipVer := from[0] >> 4
|
||||||
|
var head [4]byte
|
||||||
|
// first 4 bytes is protocol family, in network byte order
|
||||||
|
if ipVer == 4 {
|
||||||
|
head[3] = syscall.AF_INET
|
||||||
|
} else if ipVer == 6 {
|
||||||
|
head[3] = syscall.AF_INET6
|
||||||
|
} else {
|
||||||
|
return 0, fmt.Errorf("unable to determine IP version from packet")
|
||||||
|
}
|
||||||
|
|
||||||
|
rc, err := t.f.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var errno syscall.Errno
|
||||||
|
var n uintptr
|
||||||
|
err = rc.Write(func(fd uintptr) bool {
|
||||||
|
iovecs := []syscall.Iovec{
|
||||||
|
{&head[0], 4},
|
||||||
|
{&from[0], uint64(len(from))},
|
||||||
|
}
|
||||||
|
|
||||||
|
n, _, errno = syscall.Syscall(syscall.SYS_WRITEV, fd, uintptr(unsafe.Pointer(&iovecs[0])), uintptr(2))
|
||||||
|
// According to NetBSD documentation for TUN, writes will only return errors in which
|
||||||
|
// this packet will never be delivered so just go on living life.
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if errno != 0 {
|
||||||
|
return 0, errno
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(n) - 4, err
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tun) addIp(cidr netip.Prefix) error {
|
func (t *tun) addIp(cidr netip.Prefix) error {
|
||||||
var err error
|
if cidr.Addr().Is4() {
|
||||||
|
var req ifreqAlias4
|
||||||
|
req.Name = t.deviceBytes()
|
||||||
|
req.Addr = unix.RawSockaddrInet4{
|
||||||
|
Len: unix.SizeofSockaddrInet4,
|
||||||
|
Family: unix.AF_INET,
|
||||||
|
Addr: cidr.Addr().As4(),
|
||||||
|
}
|
||||||
|
req.DstAddr = unix.RawSockaddrInet4{
|
||||||
|
Len: unix.SizeofSockaddrInet4,
|
||||||
|
Family: unix.AF_INET,
|
||||||
|
Addr: cidr.Addr().As4(),
|
||||||
|
}
|
||||||
|
req.MaskAddr = unix.RawSockaddrInet4{
|
||||||
|
Len: unix.SizeofSockaddrInet4,
|
||||||
|
Family: unix.AF_INET,
|
||||||
|
Addr: prefixToMask(cidr).As4(),
|
||||||
|
}
|
||||||
|
|
||||||
// TODO use syscalls instead of exec.Command
|
s, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
cmd := exec.Command("/sbin/ifconfig", t.Device, cidr.String(), cidr.Addr().String())
|
if err != nil {
|
||||||
t.l.Debug("command: ", cmd.String())
|
return err
|
||||||
if err = cmd.Run(); err != nil {
|
}
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
defer syscall.Close(s)
|
||||||
|
|
||||||
|
if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&req))); err != nil {
|
||||||
|
return fmt.Errorf("failed to set tun address %s: %s", cidr.Addr(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/route", "-n", "add", "-net", cidr.String(), cidr.Addr().String())
|
if cidr.Addr().Is6() {
|
||||||
t.l.Debug("command: ", cmd.String())
|
var req ifreqAlias6
|
||||||
if err = cmd.Run(); err != nil {
|
req.Name = t.deviceBytes()
|
||||||
return fmt.Errorf("failed to run 'route add': %s", err)
|
req.Addr = unix.RawSockaddrInet6{
|
||||||
|
Len: unix.SizeofSockaddrInet6,
|
||||||
|
Family: unix.AF_INET6,
|
||||||
|
Addr: cidr.Addr().As16(),
|
||||||
|
}
|
||||||
|
req.PrefixMask = unix.RawSockaddrInet6{
|
||||||
|
Len: unix.SizeofSockaddrInet6,
|
||||||
|
Family: unix.AF_INET6,
|
||||||
|
Addr: prefixToMask(cidr).As16(),
|
||||||
|
}
|
||||||
|
req.Lifetime = addrLifetime{
|
||||||
|
Vltime: 0xffffffff,
|
||||||
|
Pltime: 0xffffffff,
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(s)
|
||||||
|
|
||||||
|
if err := ioctl(uintptr(s), SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&req))); err != nil {
|
||||||
|
return fmt.Errorf("failed to set tun address %s: %s", cidr.Addr().String(), err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
|
return fmt.Errorf("unknown address type %v", cidr)
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsafe path routes
|
|
||||||
return t.addRoutes(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Activate() error {
|
func (t *tun) Activate() error {
|
||||||
|
mode := int32(unix.IFF_BROADCAST)
|
||||||
|
err := ioctl(uintptr(t.fd), TUNSIFMODE, uintptr(unsafe.Pointer(&mode)))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set tun device mode: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v := 1
|
||||||
|
err = ioctl(uintptr(t.fd), TUNSIFHEAD, uintptr(unsafe.Pointer(&v)))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set tun device head: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.doIoctlByName(unix.SIOCSIFMTU, uint32(t.MTU))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set tun mtu: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
for i := range t.vpnNetworks {
|
for i := range t.vpnNetworks {
|
||||||
err := t.addIp(t.vpnNetworks[i])
|
err = t.addIp(t.vpnNetworks[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return t.addRoutes(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) doIoctlByName(ctl uintptr, value uint32) error {
|
||||||
|
s, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer syscall.Close(s)
|
||||||
|
|
||||||
|
ir := ifreq{Name: t.deviceBytes(), data: int(value)}
|
||||||
|
err = ioctl(uintptr(s), ctl, uintptr(unsafe.Pointer(&ir)))
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
@@ -197,21 +396,23 @@ func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (t *tun) addRoutes(logErrors bool) error {
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if len(r.Via) == 0 || !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
// We don't allow route MTUs so only install routes with a via
|
// We don't allow route MTUs so only install routes with a via
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), t.vpnNetworks[0].Addr().String())
|
err := addRoute(r.Cidr, t.vpnNetworks)
|
||||||
t.l.Debug("command: ", cmd.String())
|
if err != nil {
|
||||||
if err := cmd.Run(); err != nil {
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
retErr := util.NewContextualError("failed to run 'route add' for unsafe_route", map[string]any{"route": r}, err)
|
|
||||||
if logErrors {
|
if logErrors {
|
||||||
retErr.Log(t.l)
|
retErr.Log(t.l)
|
||||||
} else {
|
} else {
|
||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
t.l.WithField("route", r).Info("Added route")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,10 +425,8 @@ func (t *tun) removeRoutes(routes []Route) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: CERT-V2 is this right?
|
err := delRoute(r.Cidr, t.vpnNetworks)
|
||||||
cmd := exec.Command("/sbin/route", "-n", "delete", "-net", r.Cidr.String(), t.vpnNetworks[0].Addr().String())
|
if err != nil {
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("route", r).Info("Removed route")
|
t.l.WithField("route", r).Info("Removed route")
|
||||||
@@ -242,3 +441,147 @@ func (t *tun) deviceBytes() (o [16]byte) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addRoute(prefix netip.Prefix, gateways []netip.Prefix) error {
|
||||||
|
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
||||||
|
}
|
||||||
|
defer unix.Close(sock)
|
||||||
|
|
||||||
|
route := &netroute.RouteMessage{
|
||||||
|
Version: unix.RTM_VERSION,
|
||||||
|
Type: unix.RTM_ADD,
|
||||||
|
Flags: unix.RTF_UP | unix.RTF_GATEWAY,
|
||||||
|
Seq: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if prefix.Addr().Is4() {
|
||||||
|
gw, err := selectGateway(prefix, gateways)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
route.Addrs = []netroute.Addr{
|
||||||
|
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
||||||
|
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
||||||
|
unix.RTAX_GATEWAY: &netroute.Inet4Addr{IP: gw.Addr().As4()},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gw, err := selectGateway(prefix, gateways)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
route.Addrs = []netroute.Addr{
|
||||||
|
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
||||||
|
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
||||||
|
unix.RTAX_GATEWAY: &netroute.Inet6Addr{IP: gw.Addr().As16()},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := route.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create route.RouteMessage: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = unix.Write(sock, data[:])
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, unix.EEXIST) {
|
||||||
|
// Try to do a change
|
||||||
|
route.Type = unix.RTM_CHANGE
|
||||||
|
data, err = route.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create route.RouteMessage for change: %w", err)
|
||||||
|
}
|
||||||
|
_, err = unix.Write(sock, data[:])
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func delRoute(prefix netip.Prefix, gateways []netip.Prefix) error {
|
||||||
|
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create AF_ROUTE socket: %v", err)
|
||||||
|
}
|
||||||
|
defer unix.Close(sock)
|
||||||
|
|
||||||
|
route := netroute.RouteMessage{
|
||||||
|
Version: unix.RTM_VERSION,
|
||||||
|
Type: unix.RTM_DELETE,
|
||||||
|
Seq: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if prefix.Addr().Is4() {
|
||||||
|
gw, err := selectGateway(prefix, gateways)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
route.Addrs = []netroute.Addr{
|
||||||
|
unix.RTAX_DST: &netroute.Inet4Addr{IP: prefix.Masked().Addr().As4()},
|
||||||
|
unix.RTAX_NETMASK: &netroute.Inet4Addr{IP: prefixToMask(prefix).As4()},
|
||||||
|
unix.RTAX_GATEWAY: &netroute.Inet4Addr{IP: gw.Addr().As4()},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gw, err := selectGateway(prefix, gateways)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
route.Addrs = []netroute.Addr{
|
||||||
|
unix.RTAX_DST: &netroute.Inet6Addr{IP: prefix.Masked().Addr().As16()},
|
||||||
|
unix.RTAX_NETMASK: &netroute.Inet6Addr{IP: prefixToMask(prefix).As16()},
|
||||||
|
unix.RTAX_GATEWAY: &netroute.Inet6Addr{IP: gw.Addr().As16()},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := route.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create route.RouteMessage: %w", err)
|
||||||
|
}
|
||||||
|
_, err = unix.Write(sock, data[:])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write route.RouteMessage to socket: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ioctl(a1, a2, a3 uintptr) error {
|
||||||
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, a1, a2, a3)
|
||||||
|
if errno != 0 {
|
||||||
|
return errno
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func prefixToMask(prefix netip.Prefix) netip.Addr {
|
||||||
|
bits := prefix.Bits()
|
||||||
|
if prefix.Addr().Is4() {
|
||||||
|
mask := ^uint32(0) << (32 - bits)
|
||||||
|
return netip.AddrFrom4([4]byte{
|
||||||
|
byte(mask >> 24),
|
||||||
|
byte(mask >> 16),
|
||||||
|
byte(mask >> 8),
|
||||||
|
byte(mask),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
var mask [16]byte
|
||||||
|
for i := 0; i < bits/8; i++ {
|
||||||
|
mask[i] = 0xff
|
||||||
|
}
|
||||||
|
if bits%8 != 0 {
|
||||||
|
mask[bits/8] = ^byte(0) << (8 - bits%8)
|
||||||
|
}
|
||||||
|
return netip.AddrFrom16(mask)
|
||||||
|
}
|
||||||
|
|
||||||
|
func selectGateway(prefix netip.Prefix, gateways []netip.Prefix) (netip.Prefix, error) {
|
||||||
|
for _, gw := range gateways {
|
||||||
|
if prefix.Addr().Is4() == gw.Addr().Is4() {
|
||||||
|
return gw, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return netip.Prefix{}, fmt.Errorf("no suitable gateway found for prefix %v", prefix)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
//go:build !windows
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import "syscall"
|
|
||||||
|
|
||||||
func ioctl(a1, a2, a3 uintptr) error {
|
|
||||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, a1, a2, a3)
|
|
||||||
if errno != 0 {
|
|
||||||
return errno
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
//go:build !e2e_testing
|
//go:build openbsd && !e2e_testing
|
||||||
// +build !e2e_testing
|
// +build openbsd,!e2e_testing
|
||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
@@ -7,73 +7,53 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"strings"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct{}
|
||||||
Device string
|
|
||||||
vpnNetworks []netip.Prefix
|
|
||||||
MTU int
|
|
||||||
Routes atomic.Pointer[[]Route]
|
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
|
||||||
l *logrus.Logger
|
|
||||||
|
|
||||||
io.ReadWriteCloser
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*wgTun, error) {
|
||||||
|
return nil, fmt.Errorf("newTunFromFd not supported on OpenBSD")
|
||||||
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
|
||||||
out []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*wgTun, error) {
|
||||||
if t.ReadWriteCloser != nil {
|
deviceName := c.GetString("tun.dev", "tun")
|
||||||
return t.ReadWriteCloser.Close()
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
// Create WireGuard TUN device
|
||||||
}
|
tunDevice, err := wgtun.CreateTUN(deviceName, mtu)
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in OpenBSD")
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
|
||||||
deviceName := c.GetString("tun.dev", "")
|
|
||||||
if deviceName == "" {
|
|
||||||
return nil, fmt.Errorf("a device name in the format of tunN must be specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !deviceNameRE.MatchString(deviceName) {
|
|
||||||
return nil, fmt.Errorf("a device name in the format of tunN must be specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to create TUN device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &tun{
|
// Get the actual device name
|
||||||
ReadWriteCloser: file,
|
actualName, err := tunDevice.Name()
|
||||||
Device: deviceName,
|
if err != nil {
|
||||||
vpnNetworks: vpnNetworks,
|
tunDevice.Close()
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
return nil, fmt.Errorf("failed to get TUN device name: %w", err)
|
||||||
l: l,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := &wgTun{
|
||||||
|
tunDevice: tunDevice,
|
||||||
|
vpnNetworks: vpnNetworks,
|
||||||
|
MaxMTU: mtu,
|
||||||
|
DefaultMTU: mtu,
|
||||||
|
l: l,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create OpenBSD-specific route manager
|
||||||
|
t.routeManager = &tun{}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,172 +64,166 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
l.WithField("name", actualName).Info("Created WireGuard TUN device")
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (rm *tun) Activate(t *wgTun) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
name, err := t.tunDevice.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !initial && !change {
|
// Set the MTU
|
||||||
return nil
|
rm.SetMTU(t, t.MaxMTU)
|
||||||
}
|
|
||||||
|
|
||||||
routeTree, err := makeRouteTree(t.l, routes, false)
|
// Add IP addresses
|
||||||
if err != nil {
|
for _, network := range t.vpnNetworks {
|
||||||
return err
|
if err := rm.addIP(t, name, network); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// Teach nebula how to handle the routes before establishing them in the system table
|
|
||||||
oldRoutes := t.Routes.Swap(&routes)
|
|
||||||
t.routeTree.Store(routeTree)
|
|
||||||
|
|
||||||
if !initial {
|
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
||||||
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
||||||
if err != nil {
|
|
||||||
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure any routes we actually want are installed
|
|
||||||
err = t.addRoutes(true)
|
|
||||||
if err != nil {
|
|
||||||
// Catch any stray logs
|
|
||||||
util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) addIp(cidr netip.Prefix) error {
|
|
||||||
var err error
|
|
||||||
// TODO use syscalls instead of exec.Command
|
|
||||||
cmd := exec.Command("/sbin/ifconfig", t.Device, cidr.String(), cidr.Addr().String())
|
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
|
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = exec.Command("/sbin/route", "-n", "add", "-inet", cidr.String(), cidr.Addr().String())
|
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("failed to run 'route add': %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsafe path routes
|
|
||||||
return t.addRoutes(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Activate() error {
|
|
||||||
for i := range t.vpnNetworks {
|
|
||||||
err := t.addIp(t.vpnNetworks[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bring up the interface
|
||||||
|
if err := runCommandBSD("ifconfig", name, "up"); err != nil {
|
||||||
|
return fmt.Errorf("failed to bring up interface: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the routes
|
||||||
|
if err := rm.AddRoutes(t, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
func (rm *tun) SetMTU(t *wgTun, mtu int) {
|
||||||
r, _ := t.routeTree.Load().Lookup(ip)
|
name, err := t.tunDevice.Name()
|
||||||
return r
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get device name for MTU set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := runCommandBSD("ifconfig", name, "mtu", strconv.Itoa(mtu)); err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to set tun mtu")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (rm *tun) SetDefaultRoute(t *wgTun, cidr netip.Prefix) error {
|
||||||
|
// On OpenBSD, routes are set via ifconfig and route commands
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) AddRoutes(t *wgTun, logErrors bool) error {
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get device name: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if len(r.Via) == 0 || !r.Install {
|
if !r.Install {
|
||||||
// We don't allow route MTUs so only install routes with a via
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//TODO: CERT-V2 is this right?
|
|
||||||
cmd := exec.Command("/sbin/route", "-n", "add", "-inet", r.Cidr.String(), t.vpnNetworks[0].Addr().String())
|
// Add route using route command
|
||||||
t.l.Debug("command: ", cmd.String())
|
args := []string{"add"}
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
retErr := util.NewContextualError("failed to run 'route add' for unsafe_route", map[string]any{"route": r}, err)
|
if r.Cidr.Addr().Is6() {
|
||||||
|
args = append(args, "-inet6")
|
||||||
|
} else {
|
||||||
|
args = append(args, "-inet")
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, r.Cidr.String(), "-interface", name)
|
||||||
|
|
||||||
|
if r.Metric > 0 {
|
||||||
|
// OpenBSD doesn't support route metrics directly like Linux
|
||||||
|
t.l.WithField("route", r).Warn("Route metrics are not fully supported on OpenBSD")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := runCommandBSD("route", args...)
|
||||||
|
if err != nil {
|
||||||
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
if logErrors {
|
if logErrors {
|
||||||
retErr.Log(t.l)
|
retErr.Log(t.l)
|
||||||
} else {
|
} else {
|
||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
t.l.WithField("route", r).Info("Added route")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (rm *tun) RemoveRoutes(t *wgTun, routes []Route) {
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get device name for route removal")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//TODO: CERT-V2 is this right?
|
|
||||||
cmd := exec.Command("/sbin/route", "-n", "delete", "-inet", r.Cidr.String(), t.vpnNetworks[0].Addr().String())
|
args := []string{"delete"}
|
||||||
t.l.Debug("command: ", cmd.String())
|
|
||||||
if err := cmd.Run(); err != nil {
|
if r.Cidr.Addr().Is6() {
|
||||||
|
args = append(args, "-inet6")
|
||||||
|
} else {
|
||||||
|
args = append(args, "-inet")
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, r.Cidr.String(), "-interface", name)
|
||||||
|
|
||||||
|
err := runCommandBSD("route", args...)
|
||||||
|
if err != nil {
|
||||||
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("route", r).Info("Removed route")
|
t.l.WithField("route", r).Info("Removed route")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) NewMultiQueueReader(t *wgTun) (io.ReadWriteCloser, error) {
|
||||||
|
// OpenBSD doesn't support multi-queue TUN devices in the same way as Linux
|
||||||
|
// Return a reader that wraps the same device
|
||||||
|
return &wgTunReader{
|
||||||
|
parent: t,
|
||||||
|
tunDevice: t.tunDevice,
|
||||||
|
offset: 0,
|
||||||
|
l: t.l,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIP(t *wgTun, name string, network netip.Prefix) error {
|
||||||
|
addr := network.Addr()
|
||||||
|
|
||||||
|
if addr.Is4() {
|
||||||
|
// For IPv4: ifconfig tun0 10.0.0.1/24
|
||||||
|
if err := runCommandBSD("ifconfig", name, network.String()); err != nil {
|
||||||
|
return fmt.Errorf("failed to add IPv4 address: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// For IPv6: ifconfig tun0 inet6 add 2001:db8::1/64
|
||||||
|
if err := runCommandBSD("ifconfig", name, "inet6", "add", network.String()); err != nil {
|
||||||
|
return fmt.Errorf("failed to add IPv6 address: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Networks() []netip.Prefix {
|
func runCommandBSD(name string, args ...string) error {
|
||||||
return t.vpnNetworks
|
cmd := exec.Command(name, args...)
|
||||||
}
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
func (t *tun) Name() string {
|
return fmt.Errorf("%s %s failed: %w\nOutput: %s", name, strings.Join(args, " "), err, string(output))
|
||||||
return t.Device
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
|
||||||
buf := make([]byte, len(to)+4)
|
|
||||||
|
|
||||||
n, err := t.ReadWriteCloser.Read(buf)
|
|
||||||
|
|
||||||
copy(to, buf[4:])
|
|
||||||
return n - 4, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write is only valid for single threaded use
|
|
||||||
func (t *tun) Write(from []byte) (int, error) {
|
|
||||||
buf := t.out
|
|
||||||
if cap(buf) < len(from)+4 {
|
|
||||||
buf = make([]byte, len(from)+4)
|
|
||||||
t.out = buf
|
|
||||||
}
|
}
|
||||||
buf = buf[:len(from)+4]
|
return nil
|
||||||
|
|
||||||
if len(from) == 0 {
|
|
||||||
return 0, syscall.EIO
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine the IP Family for the NULL L2 Header
|
|
||||||
ipVer := from[0] >> 4
|
|
||||||
if ipVer == 4 {
|
|
||||||
buf[3] = syscall.AF_INET
|
|
||||||
} else if ipVer == 6 {
|
|
||||||
buf[3] = syscall.AF_INET6
|
|
||||||
} else {
|
|
||||||
return 0, fmt.Errorf("unable to determine IP version from packet")
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(buf[4:], from)
|
|
||||||
|
|
||||||
n, err := t.ReadWriteCloser.Write(buf)
|
|
||||||
return n - 4, err
|
|
||||||
}
|
}
|
||||||
|
|||||||
242
overlay/tun_wg.go
Normal file
242
overlay/tun_wg.go
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
//go:build !android && !netbsd && !e2e_testing
|
||||||
|
// +build !android,!netbsd,!e2e_testing
|
||||||
|
|
||||||
|
package overlay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/netip"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/gaissmai/bart"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/routing"
|
||||||
|
"github.com/slackhq/nebula/util"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
|
)
|
||||||
|
|
||||||
|
// wgTun wraps a WireGuard TUN device and implements the overlay.Device interface
|
||||||
|
type wgTun struct {
|
||||||
|
tunDevice wgtun.Device
|
||||||
|
vpnNetworks []netip.Prefix
|
||||||
|
MaxMTU int
|
||||||
|
DefaultMTU int
|
||||||
|
|
||||||
|
Routes atomic.Pointer[[]Route]
|
||||||
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
|
routeChan chan struct{}
|
||||||
|
|
||||||
|
// Platform-specific route management
|
||||||
|
routeManager *tun
|
||||||
|
|
||||||
|
l *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchReader interface for readers that support vectorized I/O
|
||||||
|
type BatchReader interface {
|
||||||
|
BatchRead(buffers [][]byte, sizes []int) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchWriter interface for writers that support vectorized I/O
|
||||||
|
type BatchWriter interface {
|
||||||
|
BatchWrite(packets [][]byte) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wgTunReader wraps a single TUN queue for multi-queue support
|
||||||
|
type wgTunReader struct {
|
||||||
|
parent *wgTun
|
||||||
|
tunDevice wgtun.Device
|
||||||
|
offset int
|
||||||
|
l *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) Networks() []netip.Prefix {
|
||||||
|
return t.vpnNetworks
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) Name() string {
|
||||||
|
name, err := t.tunDevice.Name()
|
||||||
|
if err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to get TUN device name")
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) RoutesFor(ip netip.Addr) routing.Gateways {
|
||||||
|
r, _ := t.routeTree.Load().Lookup(ip)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) Activate() error {
|
||||||
|
if t.routeManager == nil {
|
||||||
|
return fmt.Errorf("route manager not initialized")
|
||||||
|
}
|
||||||
|
return t.routeManager.Activate(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read implements single-packet read for backward compatibility
|
||||||
|
func (t *wgTun) Read(b []byte) (int, error) {
|
||||||
|
bufs := [][]byte{b}
|
||||||
|
sizes := []int{0}
|
||||||
|
n, err := t.tunDevice.Read(bufs, sizes, 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return 0, io.ErrNoProgress
|
||||||
|
}
|
||||||
|
return sizes[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write implements single-packet write for backward compatibility
|
||||||
|
func (t *wgTun) Write(b []byte) (int, error) {
|
||||||
|
bufs := [][]byte{b}
|
||||||
|
offset := 0
|
||||||
|
|
||||||
|
// WireGuard TUN expects the packet data to start at offset 0
|
||||||
|
n, err := t.tunDevice.Write(bufs, offset)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return 0, io.ErrShortWrite
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) Close() error {
|
||||||
|
if t.routeChan != nil {
|
||||||
|
close(t.routeChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.tunDevice != nil {
|
||||||
|
return t.tunDevice.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
|
// For WireGuard TUN, we need to create separate TUN device instances for multi-queue
|
||||||
|
// The platform-specific implementation will handle this
|
||||||
|
if t.routeManager == nil {
|
||||||
|
return nil, fmt.Errorf("route manager not initialized for multi-queue reader")
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.routeManager.NewMultiQueueReader(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *wgTun) reload(c *config.C, initial bool) error {
|
||||||
|
routeChange, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !initial && !routeChange && !c.HasChanged("tun.mtu") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
routeTree, err := makeRouteTree(t.l, routes, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldDefaultMTU := t.DefaultMTU
|
||||||
|
oldMaxMTU := t.MaxMTU
|
||||||
|
newDefaultMTU := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
newMaxMTU := newDefaultMTU
|
||||||
|
for i, r := range routes {
|
||||||
|
if r.MTU == 0 {
|
||||||
|
routes[i].MTU = newDefaultMTU
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.MTU > t.MaxMTU {
|
||||||
|
newMaxMTU = r.MTU
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.MaxMTU = newMaxMTU
|
||||||
|
t.DefaultMTU = newDefaultMTU
|
||||||
|
|
||||||
|
// Teach nebula how to handle the routes before establishing them in the system table
|
||||||
|
oldRoutes := t.Routes.Swap(&routes)
|
||||||
|
t.routeTree.Store(routeTree)
|
||||||
|
|
||||||
|
if !initial && t.routeManager != nil {
|
||||||
|
if oldMaxMTU != newMaxMTU {
|
||||||
|
t.routeManager.SetMTU(t, t.MaxMTU)
|
||||||
|
t.l.Infof("Set max MTU to %v was %v", t.MaxMTU, oldMaxMTU)
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldDefaultMTU != newDefaultMTU {
|
||||||
|
for i := range t.vpnNetworks {
|
||||||
|
err := t.routeManager.SetDefaultRoute(t, t.vpnNetworks[i])
|
||||||
|
if err != nil {
|
||||||
|
t.l.Warn(err)
|
||||||
|
} else {
|
||||||
|
t.l.Infof("Set default MTU to %v was %v", t.DefaultMTU, oldDefaultMTU)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
||||||
|
t.routeManager.RemoveRoutes(t, findRemovedRoutes(routes, *oldRoutes))
|
||||||
|
|
||||||
|
// Ensure any routes we actually want are installed
|
||||||
|
err = t.routeManager.AddRoutes(t, true)
|
||||||
|
if err != nil {
|
||||||
|
// This should never be called since AddRoutes should log its own errors in a reload condition
|
||||||
|
util.LogWithContextIfNeeded("Failed to refresh routes", err, t.l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchRead reads multiple packets from the TUN device using vectorized I/O
|
||||||
|
// The caller provides buffers and sizes slices, and this function returns the number of packets read.
|
||||||
|
func (r *wgTunReader) BatchRead(buffers [][]byte, sizes []int) (int, error) {
|
||||||
|
return r.tunDevice.Read(buffers, sizes, r.offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read implements io.Reader for wgTunReader (single packet for compatibility)
|
||||||
|
func (r *wgTunReader) Read(b []byte) (int, error) {
|
||||||
|
bufs := [][]byte{b}
|
||||||
|
sizes := []int{0}
|
||||||
|
n, err := r.tunDevice.Read(bufs, sizes, r.offset)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return 0, io.ErrNoProgress
|
||||||
|
}
|
||||||
|
return sizes[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write implements io.Writer for wgTunReader
|
||||||
|
func (r *wgTunReader) Write(b []byte) (int, error) {
|
||||||
|
bufs := [][]byte{b}
|
||||||
|
n, err := r.tunDevice.Write(bufs, r.offset)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return 0, io.ErrShortWrite
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchWrite writes multiple packets to the TUN device using vectorized I/O
|
||||||
|
func (r *wgTunReader) BatchWrite(packets [][]byte) (int, error) {
|
||||||
|
return r.tunDevice.Write(packets, r.offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *wgTunReader) Close() error {
|
||||||
|
if r.tunDevice != nil {
|
||||||
|
return r.tunDevice.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,84 +1,77 @@
|
|||||||
//go:build !e2e_testing
|
//go:build windows && !e2e_testing
|
||||||
// +build !e2e_testing
|
// +build windows,!e2e_testing
|
||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
"github.com/slackhq/nebula/wintun"
|
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||||
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tunGUIDLabel = "Fixed Nebula Windows GUID v1"
|
const tunGUIDLabel = "Fixed Nebula Windows GUID v1"
|
||||||
|
|
||||||
type winTun struct {
|
type tun struct {
|
||||||
Device string
|
luid winipcfg.LUID
|
||||||
vpnNetworks []netip.Prefix
|
|
||||||
MTU int
|
|
||||||
Routes atomic.Pointer[[]Route]
|
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
|
||||||
l *logrus.Logger
|
|
||||||
|
|
||||||
tun *wintun.NativeTun
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (Device, error) {
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ []netip.Prefix) (*wgTun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
|
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*winTun, error) {
|
func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (*wgTun, error) {
|
||||||
err := checkWinTunExists()
|
deviceName := c.GetString("tun.dev", "Nebula")
|
||||||
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
|
||||||
|
// Create WireGuard TUN device
|
||||||
|
tunDevice, err := wgtun.CreateTUN(deviceName, mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can not load the wintun driver: %w", err)
|
return nil, fmt.Errorf("failed to create TUN device: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceName := c.GetString("tun.dev", "")
|
// Get the actual device name
|
||||||
guid, err := generateGUIDByDeviceName(deviceName)
|
actualName, err := tunDevice.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("generate GUID failed: %w", err)
|
tunDevice.Close()
|
||||||
|
return nil, fmt.Errorf("failed to get TUN device name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &winTun{
|
t := &wgTun{
|
||||||
Device: deviceName,
|
tunDevice: tunDevice,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MaxMTU: mtu,
|
||||||
|
DefaultMTU: mtu,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create Windows-specific route manager
|
||||||
|
rm := &tun{}
|
||||||
|
|
||||||
|
// Get LUID from the TUN device
|
||||||
|
// The WireGuard TUN device on Windows should provide a LUID() method
|
||||||
|
if nativeTun, ok := tunDevice.(interface{ LUID() uint64 }); ok {
|
||||||
|
rm.luid = winipcfg.LUID(nativeTun.LUID())
|
||||||
|
} else {
|
||||||
|
tunDevice.Close()
|
||||||
|
return nil, fmt.Errorf("failed to get LUID from TUN device")
|
||||||
|
}
|
||||||
|
t.routeManager = rm
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
tunDevice.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var tunDevice wintun.Device
|
|
||||||
tunDevice, err = wintun.CreateTUNWithRequestedGUID(deviceName, guid, t.MTU)
|
|
||||||
if err != nil {
|
|
||||||
// Windows 10 has an issue with unclean shutdowns not fully cleaning up the wintun device.
|
|
||||||
// Trying a second time resolves the issue.
|
|
||||||
l.WithError(err).Debug("Failed to create wintun device, retrying")
|
|
||||||
tunDevice, err = wintun.CreateTUNWithRequestedGUID(deviceName, guid, t.MTU)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("create TUN device failed: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.tun = tunDevice.(*wintun.NativeTun)
|
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
err := t.reload(c, false)
|
err := t.reload(c, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -86,206 +79,140 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, _ bool) (
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
l.WithField("name", actualName).Info("Created WireGuard TUN device")
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) reload(c *config.C, initial bool) error {
|
func (rm *tun) Activate(t *wgTun) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
// Set MTU
|
||||||
|
err := rm.setMTU(t, t.MaxMTU)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to set MTU: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !initial && !change {
|
// Add IP addresses
|
||||||
return nil
|
for _, network := range t.vpnNetworks {
|
||||||
}
|
if err := rm.addIP(t, network); err != nil {
|
||||||
|
return err
|
||||||
routeTree, err := makeRouteTree(t.l, routes, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Teach nebula how to handle the routes before establishing them in the system table
|
|
||||||
oldRoutes := t.Routes.Swap(&routes)
|
|
||||||
t.routeTree.Store(routeTree)
|
|
||||||
|
|
||||||
if !initial {
|
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
|
||||||
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
|
||||||
if err != nil {
|
|
||||||
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure any routes we actually want are installed
|
|
||||||
err = t.addRoutes(true)
|
|
||||||
if err != nil {
|
|
||||||
// Catch any stray logs
|
|
||||||
util.LogWithContextIfNeeded("Failed to add routes", err, t.l)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// Add routes
|
||||||
}
|
if err := rm.AddRoutes(t, false); err != nil {
|
||||||
|
|
||||||
func (t *winTun) Activate() error {
|
|
||||||
luid := winipcfg.LUID(t.tun.LUID())
|
|
||||||
|
|
||||||
err := luid.SetIPAddresses(t.vpnNetworks)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to set address: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = t.addRoutes(false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) addRoutes(logErrors bool) error {
|
func (rm *tun) SetMTU(t *wgTun, mtu int) {
|
||||||
luid := winipcfg.LUID(t.tun.LUID())
|
if err := rm.setMTU(t, mtu); err != nil {
|
||||||
|
t.l.WithError(err).Error("Failed to set MTU")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) setMTU(t *wgTun, mtu int) error {
|
||||||
|
// Set MTU using winipcfg
|
||||||
|
// Note: MTU setting on Windows TUN devices may be handled by the driver
|
||||||
|
// For now, we'll skip explicit MTU setting as the WireGuard TUN handles it
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) SetDefaultRoute(t *wgTun, cidr netip.Prefix) error {
|
||||||
|
// On Windows, routes are managed differently
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) AddRoutes(t *wgTun, logErrors bool) error {
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
foundDefault4 := false
|
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if len(r.Via) == 0 || !r.Install {
|
if !r.Install {
|
||||||
// We don't allow route MTUs so only install routes with a via
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add our unsafe route
|
if r.MTU > 0 {
|
||||||
// Windows does not support multipath routes natively, so we install only a single route.
|
// Windows route MTU is not directly supported
|
||||||
// This is not a problem as traffic will always be sent to Nebula which handles the multipath routing internally.
|
t.l.WithField("route", r).Debug("Route MTU is not supported on Windows")
|
||||||
// In effect this provides multipath routing support to windows supporting loadbalancing and redundancy.
|
}
|
||||||
err := luid.AddRoute(r.Cidr, r.Via[0].Addr(), uint32(r.Metric))
|
|
||||||
|
// Use winipcfg to add the route
|
||||||
|
// The rm.luid should have the AddRoute method from winipcfg
|
||||||
|
if len(r.Via) == 0 {
|
||||||
|
t.l.WithField("route", r).Warn("Route has no via address, skipping")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := rm.luid.AddRoute(r.Cidr, r.Via[0].Addr(), uint32(r.Metric))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
if logErrors {
|
if logErrors {
|
||||||
retErr.Log(t.l)
|
retErr.Log(t.l)
|
||||||
continue
|
|
||||||
} else {
|
} else {
|
||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("route", r).Info("Added route")
|
t.l.WithField("route", r).Info("Added route")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !foundDefault4 {
|
|
||||||
if r.Cidr.Bits() == 0 && r.Cidr.Addr().BitLen() == 32 {
|
|
||||||
foundDefault4 = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ipif, err := luid.IPInterface(windows.AF_INET)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get ip interface: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ipif.NLMTU = uint32(t.MTU)
|
|
||||||
if foundDefault4 {
|
|
||||||
ipif.UseAutomaticMetric = false
|
|
||||||
ipif.Metric = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ipif.Set(); err != nil {
|
|
||||||
return fmt.Errorf("failed to set ip interface: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) removeRoutes(routes []Route) error {
|
func (rm *tun) RemoveRoutes(t *wgTun, routes []Route) {
|
||||||
luid := winipcfg.LUID(t.tun.LUID())
|
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// See comment on luid.AddRoute
|
if len(r.Via) == 0 {
|
||||||
err := luid.DeleteRoute(r.Cidr, r.Via[0].Addr())
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := rm.luid.DeleteRoute(r.Cidr, r.Via[0].Addr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
t.l.WithError(err).WithField("route", r).Error("Failed to remove route")
|
||||||
} else {
|
} else {
|
||||||
t.l.WithField("route", r).Info("Removed route")
|
t.l.WithField("route", r).Info("Removed route")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) NewMultiQueueReader(t *wgTun) (io.ReadWriteCloser, error) {
|
||||||
|
// Windows doesn't support multi-queue TUN devices
|
||||||
|
// Return a reader that wraps the same device
|
||||||
|
return &wgTunReader{
|
||||||
|
parent: t,
|
||||||
|
tunDevice: t.tunDevice,
|
||||||
|
offset: 0,
|
||||||
|
l: t.l,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *tun) addIP(t *wgTun, network netip.Prefix) error {
|
||||||
|
// Add IP address using winipcfg
|
||||||
|
// SetIPAddresses expects a slice of prefixes
|
||||||
|
err := rm.luid.SetIPAddresses([]netip.Prefix{network})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to add IP address %s: %w", network, err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) RoutesFor(ip netip.Addr) routing.Gateways {
|
// generateGUIDByDeviceName generates a GUID based on the device name
|
||||||
r, _ := t.routeTree.Load().Lookup(ip)
|
func generateGUIDByDeviceName(deviceName string) (*windows.GUID, error) {
|
||||||
return r
|
// Hash the device name to create a deterministic GUID
|
||||||
}
|
h := crypto.SHA256.New()
|
||||||
|
h.Write([]byte(tunGUIDLabel))
|
||||||
|
h.Write([]byte(deviceName))
|
||||||
|
sum := h.Sum(nil)
|
||||||
|
|
||||||
func (t *winTun) Networks() []netip.Prefix {
|
guid := &windows.GUID{
|
||||||
return t.vpnNetworks
|
Data1: binary.LittleEndian.Uint32(sum[0:4]),
|
||||||
}
|
Data2: binary.LittleEndian.Uint16(sum[4:6]),
|
||||||
|
Data3: binary.LittleEndian.Uint16(sum[6:8]),
|
||||||
func (t *winTun) Name() string {
|
|
||||||
return t.Device
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) Read(b []byte) (int, error) {
|
|
||||||
return t.tun.Read(b, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) Write(b []byte) (int, error) {
|
|
||||||
return t.tun.Write(b, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) Close() error {
|
|
||||||
// It seems that the Windows networking stack doesn't like it when we destroy interfaces that have active routes,
|
|
||||||
// so to be certain, just remove everything before destroying.
|
|
||||||
luid := winipcfg.LUID(t.tun.LUID())
|
|
||||||
_ = luid.FlushRoutes(windows.AF_INET)
|
|
||||||
_ = luid.FlushIPAddresses(windows.AF_INET)
|
|
||||||
|
|
||||||
_ = luid.FlushRoutes(windows.AF_INET6)
|
|
||||||
_ = luid.FlushIPAddresses(windows.AF_INET6)
|
|
||||||
|
|
||||||
_ = luid.FlushDNS(windows.AF_INET)
|
|
||||||
_ = luid.FlushDNS(windows.AF_INET6)
|
|
||||||
|
|
||||||
return t.tun.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateGUIDByDeviceName(name string) (*windows.GUID, error) {
|
|
||||||
// GUID is 128 bit
|
|
||||||
hash := crypto.MD5.New()
|
|
||||||
|
|
||||||
_, err := hash.Write([]byte(tunGUIDLabel))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
copy(guid.Data4[:], sum[8:16])
|
||||||
|
|
||||||
_, err = hash.Write([]byte(name))
|
return guid, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sum := hash.Sum(nil)
|
|
||||||
|
|
||||||
return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkWinTunExists() error {
|
|
||||||
myPath, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
arch := runtime.GOARCH
|
|
||||||
switch arch {
|
|
||||||
case "386":
|
|
||||||
//NOTE: wintun bundles 386 as x86
|
|
||||||
arch = "x86"
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,6 +180,7 @@ func (c *PKClient) DeriveNoise(peerPubKey []byte) ([]byte, error) {
|
|||||||
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, true),
|
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, true),
|
||||||
pkcs11.NewAttribute(pkcs11.CKA_WRAP, true),
|
pkcs11.NewAttribute(pkcs11.CKA_WRAP, true),
|
||||||
pkcs11.NewAttribute(pkcs11.CKA_UNWRAP, true),
|
pkcs11.NewAttribute(pkcs11.CKA_UNWRAP, true),
|
||||||
|
pkcs11.NewAttribute(pkcs11.CKA_VALUE_LEN, NoiseKeySize),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the parameters which include the peer's public key
|
// Set up the parameters which include the peer's public key
|
||||||
|
|||||||
5
pki.go
5
pki.go
@@ -173,7 +173,6 @@ func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
|||||||
|
|
||||||
p.cs.Store(newState)
|
p.cs.Store(newState)
|
||||||
|
|
||||||
//TODO: CERT-V2 newState needs a stringer that does json
|
|
||||||
if initial {
|
if initial {
|
||||||
p.l.WithField("cert", newState).Debug("Client nebula certificate(s)")
|
p.l.WithField("cert", newState).Debug("Client nebula certificate(s)")
|
||||||
} else {
|
} else {
|
||||||
@@ -359,7 +358,9 @@ func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, p
|
|||||||
return nil, util.NewContextualError("v1 and v2 curve are not the same, ignoring", nil, nil)
|
return nil, util.NewContextualError("v1 and v2 curve are not the same, ignoring", nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: CERT-V2 make sure v2 has v1s address
|
if v1.Networks()[0] != v2.Networks()[0] {
|
||||||
|
return nil, util.NewContextualError("v1 and v2 networks are not the same", nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
cs.initiatingVersion = dv
|
cs.initiatingVersion = dv
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ type RemoteList struct {
|
|||||||
// The full list of vpn addresses assigned to this host
|
// The full list of vpn addresses assigned to this host
|
||||||
vpnAddrs []netip.Addr
|
vpnAddrs []netip.Addr
|
||||||
|
|
||||||
// A deduplicated set of addresses. Any accessor should lock beforehand.
|
// A deduplicated set of underlay addresses. Any accessor should lock beforehand.
|
||||||
addrs []netip.AddrPort
|
addrs []netip.AddrPort
|
||||||
|
|
||||||
// A set of relay addresses. VpnIp addresses that the remote identified as relays.
|
// A set of relay addresses. VpnIp addresses that the remote identified as relays.
|
||||||
@@ -201,8 +201,10 @@ type RemoteList struct {
|
|||||||
// For learned addresses, this is the vpnIp that sent the packet
|
// For learned addresses, this is the vpnIp that sent the packet
|
||||||
cache map[netip.Addr]*cache
|
cache map[netip.Addr]*cache
|
||||||
|
|
||||||
hr *hostnamesResults
|
hr *hostnamesResults
|
||||||
shouldAdd func(netip.Addr) bool
|
|
||||||
|
// shouldAdd is a nillable function that decides if x should be added to addrs.
|
||||||
|
shouldAdd func(vpnAddrs []netip.Addr, x netip.Addr) bool
|
||||||
|
|
||||||
// This is a list of remotes that we have tried to handshake with and have returned from the wrong vpn ip.
|
// This is a list of remotes that we have tried to handshake with and have returned from the wrong vpn ip.
|
||||||
// They should not be tried again during a handshake
|
// They should not be tried again during a handshake
|
||||||
@@ -213,7 +215,7 @@ type RemoteList struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRemoteList creates a new empty RemoteList
|
// NewRemoteList creates a new empty RemoteList
|
||||||
func NewRemoteList(vpnAddrs []netip.Addr, shouldAdd func(netip.Addr) bool) *RemoteList {
|
func NewRemoteList(vpnAddrs []netip.Addr, shouldAdd func([]netip.Addr, netip.Addr) bool) *RemoteList {
|
||||||
r := &RemoteList{
|
r := &RemoteList{
|
||||||
vpnAddrs: make([]netip.Addr, len(vpnAddrs)),
|
vpnAddrs: make([]netip.Addr, len(vpnAddrs)),
|
||||||
addrs: make([]netip.AddrPort, 0),
|
addrs: make([]netip.AddrPort, 0),
|
||||||
@@ -368,6 +370,15 @@ func (r *RemoteList) CopyBlockedRemotes() []netip.AddrPort {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RefreshFromHandshake locks and updates the RemoteList to account for data learned upon a completed handshake
|
||||||
|
func (r *RemoteList) RefreshFromHandshake(vpnAddrs []netip.Addr) {
|
||||||
|
r.Lock()
|
||||||
|
r.badRemotes = nil
|
||||||
|
r.vpnAddrs = make([]netip.Addr, len(vpnAddrs))
|
||||||
|
copy(r.vpnAddrs, vpnAddrs)
|
||||||
|
r.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// ResetBlockedRemotes locks and clears the blocked remotes list
|
// ResetBlockedRemotes locks and clears the blocked remotes list
|
||||||
func (r *RemoteList) ResetBlockedRemotes() {
|
func (r *RemoteList) ResetBlockedRemotes() {
|
||||||
r.Lock()
|
r.Lock()
|
||||||
@@ -577,7 +588,7 @@ func (r *RemoteList) unlockedCollect() {
|
|||||||
|
|
||||||
dnsAddrs := r.hr.GetAddrs()
|
dnsAddrs := r.hr.GetAddrs()
|
||||||
for _, addr := range dnsAddrs {
|
for _, addr := range dnsAddrs {
|
||||||
if r.shouldAdd == nil || r.shouldAdd(addr.Addr()) {
|
if r.shouldAdd == nil || r.shouldAdd(r.vpnAddrs, addr.Addr()) {
|
||||||
if !r.unlockedIsBad(addr) {
|
if !r.unlockedIsBad(addr) {
|
||||||
addrs = append(addrs, addr)
|
addrs = append(addrs, addr)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user