neterror.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package neterror classifies network errors.
  4. package neterror
  5. import (
  6. "errors"
  7. "fmt"
  8. "runtime"
  9. "syscall"
  10. )
  11. var errEPERM error = syscall.EPERM // box it into interface just once
  12. // TreatAsLostUDP reports whether err is an error from a UDP send
  13. // operation that should be treated as a UDP packet that just got
  14. // lost.
  15. //
  16. // Notably, on Linux this reports true for EPERM errors (from outbound
  17. // firewall blocks) which aren't really send errors; they're just
  18. // sends that are never going to make it because the local OS blocked
  19. // it.
  20. func TreatAsLostUDP(err error) bool {
  21. if err == nil {
  22. return false
  23. }
  24. switch runtime.GOOS {
  25. case "linux":
  26. // Linux, while not documented in the man page,
  27. // returns EPERM when there's an OUTPUT rule with -j
  28. // DROP or -j REJECT. We use this very specific
  29. // Linux+EPERM check rather than something super broad
  30. // like net.Error.Temporary which could be anything.
  31. //
  32. // For now we only do this on Linux, as such outgoing
  33. // firewall violations mapping to syscall errors
  34. // hasn't yet been observed on other OSes.
  35. return errors.Is(err, errEPERM)
  36. }
  37. return false
  38. }
  39. var packetWasTruncated func(error) bool // non-nil on Windows at least
  40. // PacketWasTruncated reports whether err indicates truncation but the RecvFrom
  41. // that generated err was otherwise successful. On Windows, Go's UDP RecvFrom
  42. // calls WSARecvFrom which returns the WSAEMSGSIZE error code when the received
  43. // datagram is larger than the provided buffer. When that happens, both a valid
  44. // size and an error are returned (as per the partial fix for golang/go#14074).
  45. // If the WSAEMSGSIZE error is returned, then we ignore the error to get
  46. // semantics similar to the POSIX operating systems. One caveat is that it
  47. // appears that the source address is not returned when WSAEMSGSIZE occurs, but
  48. // we do not currently look at the source address.
  49. func PacketWasTruncated(err error) bool {
  50. if packetWasTruncated == nil {
  51. return false
  52. }
  53. return packetWasTruncated(err)
  54. }
  55. var shouldDisableUDPGSO func(error) bool // non-nil on Linux
  56. func ShouldDisableUDPGSO(err error) bool {
  57. if shouldDisableUDPGSO == nil {
  58. return false
  59. }
  60. return shouldDisableUDPGSO(err)
  61. }
  62. type ErrUDPGSODisabled struct {
  63. OnLaddr string
  64. RetryErr error
  65. }
  66. func (e ErrUDPGSODisabled) Error() string {
  67. return fmt.Sprintf("disabled UDP GSO on %s, NIC(s) may not support checksum offload", e.OnLaddr)
  68. }
  69. func (e ErrUDPGSODisabled) Unwrap() error {
  70. return e.RetryErr
  71. }