forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf.go
137 lines (112 loc) · 3.94 KB
/
bpf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// +build linux
/*
* Copyright (c) 2020, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"context"
"net"
"syscall"
"unsafe"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
"golang.org/x/net/bpf"
"golang.org/x/sys/unix"
)
// ServerBPFEnabled indicates if BPF functionality is enabled.
func ServerBPFEnabled() bool {
return true
}
// newTCPListenerWithBPF creates a TCP net.Listener, optionally attaching
// the BPF program specified by the tactics parameter BPFServerTCPProgram.
func newTCPListenerWithBPF(
support *SupportServices,
localAddress string) (net.Listener, string, error) {
// Limitations:
// - BPFServerTCPProgram must be set unconditionally as neither client GeoIP
// nor API parameters are checked before the BPF is attached.
// - Currently, lhe listener BPF is not reset upon tactics hot reload.
havePBFProgram, programName, rawInstructions, err := getBPFProgram(support)
if err != nil {
log.WithTraceFields(
LogFields{"error": err}).Warning("failed to get BPF program for listener")
// If tactics is somehow misconfigured, keep running.
}
listenConfig := &net.ListenConfig{}
if havePBFProgram {
programName = programName
// Tactics parameters validation ensures BPFProgramInstructions has len >= 1.
listenConfig.Control = func(network, address string, c syscall.RawConn) error {
var setSockOptError error
err := c.Control(func(fd uintptr) {
setSockOptError = unix.SetsockoptSockFprog(
int(fd),
unix.SOL_SOCKET,
unix.SO_ATTACH_FILTER,
&unix.SockFprog{
Len: uint16(len(rawInstructions)),
Filter: (*unix.SockFilter)(unsafe.Pointer(&rawInstructions[0])),
})
})
if err == nil {
err = setSockOptError
}
return errors.Trace(err)
}
}
listener, err := listenConfig.Listen(context.Background(), "tcp", localAddress)
return listener, programName, errors.Trace(err)
}
func getBPFProgram(support *SupportServices) (bool, string, []bpf.RawInstruction, error) {
tactics, err := support.TacticsServer.GetTactics(
true, common.GeoIPData(NewGeoIPData()), make(common.APIParameters))
if err != nil {
return false, "", nil, errors.Trace(err)
}
if tactics == nil {
// This server isn't configured with tactics.
return false, "", nil, nil
}
seed, err := protocol.DeriveBPFServerProgramPRNGSeed(support.Config.ObfuscatedSSHKey)
if err != nil {
return false, "", nil, errors.Trace(err)
}
PRNG := prng.NewPRNGWithSeed(seed)
if !PRNG.FlipWeightedCoin(tactics.Probability) {
// Skip tactics with the configured probability.
return false, "", nil, nil
}
clientParameters, err := parameters.NewClientParameters(nil)
if err != nil {
return false, "", nil, nil
}
_, err = clientParameters.Set("", false, tactics.Parameters)
if err != nil {
return false, "", nil, nil
}
p := clientParameters.Get()
if !PRNG.FlipWeightedCoin(
p.Float(parameters.BPFServerTCPProbability)) {
return false, "", nil, nil
}
ok, name, rawInstructions := p.BPFProgram(parameters.BPFServerTCPProgram)
return ok, name, rawInstructions, nil
}