lsf_linux.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Linux socket filter
  5. package syscall
  6. import (
  7. "unsafe"
  8. )
  9. // Deprecated: Use golang.org/x/net/bpf instead.
  10. func LsfStmt(code, k int) *SockFilter {
  11. return &SockFilter{Code: uint16(code), K: uint32(k)}
  12. }
  13. // Deprecated: Use golang.org/x/net/bpf instead.
  14. func LsfJump(code, k, jt, jf int) *SockFilter {
  15. return &SockFilter{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
  16. }
  17. // Deprecated: Use golang.org/x/net/bpf instead.
  18. func LsfSocket(ifindex, proto int) (int, error) {
  19. var lsall SockaddrLinklayer
  20. // This is missing SOCK_CLOEXEC, but adding the flag
  21. // could break callers.
  22. s, e := Socket(AF_PACKET, SOCK_RAW, proto)
  23. if e != nil {
  24. return 0, e
  25. }
  26. p := (*[2]byte)(unsafe.Pointer(&lsall.Protocol))
  27. p[0] = byte(proto >> 8)
  28. p[1] = byte(proto)
  29. lsall.Ifindex = ifindex
  30. e = Bind(s, &lsall)
  31. if e != nil {
  32. Close(s)
  33. return 0, e
  34. }
  35. return s, nil
  36. }
  37. type iflags struct {
  38. name [IFNAMSIZ]byte
  39. flags uint16
  40. }
  41. // Deprecated: Use golang.org/x/net/bpf instead.
  42. func SetLsfPromisc(name string, m bool) error {
  43. s, e := cloexecSocket(AF_INET, SOCK_DGRAM, 0)
  44. if e != nil {
  45. return e
  46. }
  47. defer Close(s)
  48. var ifl iflags
  49. copy(ifl.name[:], []byte(name))
  50. _, _, ep := Syscall(SYS_IOCTL, uintptr(s), SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
  51. if ep != 0 {
  52. return Errno(ep)
  53. }
  54. if m {
  55. ifl.flags |= uint16(IFF_PROMISC)
  56. } else {
  57. ifl.flags &^= uint16(IFF_PROMISC)
  58. }
  59. _, _, ep = Syscall(SYS_IOCTL, uintptr(s), SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
  60. if ep != 0 {
  61. return Errno(ep)
  62. }
  63. return nil
  64. }
  65. // Deprecated: Use golang.org/x/net/bpf instead.
  66. func AttachLsf(fd int, i []SockFilter) error {
  67. var p SockFprog
  68. p.Len = uint16(len(i))
  69. p.Filter = (*SockFilter)(unsafe.Pointer(&i[0]))
  70. return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, unsafe.Pointer(&p), Socklen_t(unsafe.Sizeof(p)))
  71. }
  72. // Deprecated: Use golang.org/x/net/bpf instead.
  73. func DetachLsf(fd int) error {
  74. var dummy int
  75. return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, unsafe.Pointer(&dummy), Socklen_t(unsafe.Sizeof(dummy)))
  76. }