bpf.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/errors"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  30. "golang.org/x/net/bpf"
  31. "golang.org/x/sys/unix"
  32. )
  33. // ServerBPFEnabled indicates if BPF functionality is enabled.
  34. func ServerBPFEnabled() bool {
  35. return true
  36. }
  37. // newTCPListenerWithBPF creates a TCP net.Listener, optionally attaching
  38. // the BPF program specified by the tactics parameter BPFServerTCPProgram.
  39. func newTCPListenerWithBPF(
  40. support *SupportServices,
  41. localAddress string) (net.Listener, string, error) {
  42. // Limitations:
  43. // - BPFServerTCPProgram must be set unconditionally as neither client GeoIP
  44. // nor API parameters are checked before the BPF is attached.
  45. // - Currently, lhe listener BPF is not reset upon tactics hot reload.
  46. havePBFProgram, programName, rawInstructions, err := getBPFProgram(support)
  47. if err != nil {
  48. log.WithTraceFields(
  49. LogFields{"error": err}).Warning("failed to get BPF program for listener")
  50. // If tactics is somehow misconfigured, keep running.
  51. }
  52. listenConfig := &net.ListenConfig{}
  53. if havePBFProgram {
  54. programName = programName
  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. seed, err := protocol.DeriveBPFServerProgramPRNGSeed(support.Config.ObfuscatedSSHKey)
  87. if err != nil {
  88. return false, "", nil, errors.Trace(err)
  89. }
  90. PRNG := prng.NewPRNGWithSeed(seed)
  91. if !PRNG.FlipWeightedCoin(
  92. p.Float(parameters.BPFServerTCPProbability)) {
  93. return false, "", nil, nil
  94. }
  95. ok, name, rawInstructions := p.BPFProgram(parameters.BPFServerTCPProgram)
  96. return ok, name, rawInstructions, nil
  97. }