bpf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // +build linux
  2. /*
  3. * Copyright (c) 2020, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package server
  21. import (
  22. "context"
  23. "net"
  24. "syscall"
  25. "unsafe"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  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. tactics, err := support.TacticsServer.GetTactics(
  80. true, common.GeoIPData(NewGeoIPData()), make(common.APIParameters))
  81. if err != nil {
  82. return false, "", nil, errors.Trace(err)
  83. }
  84. if tactics == nil {
  85. // This server isn't configured with tactics.
  86. return false, "", nil, nil
  87. }
  88. seed, err := protocol.DeriveBPFServerProgramPRNGSeed(support.Config.ObfuscatedSSHKey)
  89. if err != nil {
  90. return false, "", nil, errors.Trace(err)
  91. }
  92. PRNG := prng.NewPRNGWithSeed(seed)
  93. if !PRNG.FlipWeightedCoin(tactics.Probability) {
  94. // Skip tactics with the configured probability.
  95. return false, "", nil, nil
  96. }
  97. clientParameters, err := parameters.NewClientParameters(nil)
  98. if err != nil {
  99. return false, "", nil, nil
  100. }
  101. _, err = clientParameters.Set("", false, tactics.Parameters)
  102. if err != nil {
  103. return false, "", nil, nil
  104. }
  105. p := clientParameters.Get()
  106. if !PRNG.FlipWeightedCoin(
  107. p.Float(parameters.BPFServerTCPProbability)) {
  108. return false, "", nil, nil
  109. }
  110. ok, name, rawInstructions := p.BPFProgram(parameters.BPFServerTCPProgram)
  111. return ok, name, rawInstructions, nil
  112. }