bpf.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. programName = programName
  56. // Tactics parameters validation ensures BPFProgramInstructions has len >= 1.
  57. listenConfig.Control = func(network, address string, c syscall.RawConn) error {
  58. var setSockOptError error
  59. err := c.Control(func(fd uintptr) {
  60. setSockOptError = unix.SetsockoptSockFprog(
  61. int(fd),
  62. unix.SOL_SOCKET,
  63. unix.SO_ATTACH_FILTER,
  64. &unix.SockFprog{
  65. Len: uint16(len(rawInstructions)),
  66. Filter: (*unix.SockFilter)(unsafe.Pointer(&rawInstructions[0])),
  67. })
  68. })
  69. if err == nil {
  70. err = setSockOptError
  71. }
  72. return errors.Trace(err)
  73. }
  74. }
  75. listener, err := listenConfig.Listen(context.Background(), "tcp", localAddress)
  76. return listener, programName, errors.Trace(err)
  77. }
  78. func getBPFProgram(support *SupportServices) (bool, string, []bpf.RawInstruction, error) {
  79. p, err := support.ServerTacticsParametersCache.Get(NewGeoIPData())
  80. if err != nil {
  81. return false, "", nil, errors.Trace(err)
  82. }
  83. if p.IsNil() {
  84. // No tactics are configured; BPF is disabled.
  85. return false, "", nil, nil
  86. }
  87. seed, err := protocol.DeriveBPFServerProgramPRNGSeed(support.Config.ObfuscatedSSHKey)
  88. if err != nil {
  89. return false, "", nil, errors.Trace(err)
  90. }
  91. PRNG := prng.NewPRNGWithSeed(seed)
  92. if !PRNG.FlipWeightedCoin(
  93. p.Float(parameters.BPFServerTCPProbability)) {
  94. return false, "", nil, nil
  95. }
  96. ok, name, rawInstructions := p.BPFProgram(parameters.BPFServerTCPProgram)
  97. return ok, name, rawInstructions, nil
  98. }