bpf.go 3.7 KB

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