| 1 |
//go:build linux |
| 2 |
|
| 3 |
package socket |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"os" |
| 8 |
"unsafe" |
| 9 |
|
| 10 |
"golang.org/x/net/bpf" |
| 11 |
"golang.org/x/sys/unix" |
| 12 |
) |
| 13 |
|
| 14 |
// IoctlKCMClone wraps ioctl(2) for unix.KCMClone values, but returns a Conn |
| 15 |
// rather than a raw file descriptor. |
| 16 |
func (c *Conn) IoctlKCMClone() (*Conn, error) { |
| 17 |
info, err := controlT(c, "ioctl", unix.IoctlKCMClone) |
| 18 |
if err != nil { |
| 19 |
return nil, err |
| 20 |
} |
| 21 |
|
| 22 |
// Successful clone, wrap in a Conn for use by the caller. |
| 23 |
return New(int(info.Fd), c.name) |
| 24 |
} |
| 25 |
|
| 26 |
// IoctlKCMAttach wraps ioctl(2) for unix.KCMAttach values. |
| 27 |
func (c *Conn) IoctlKCMAttach(info unix.KCMAttach) error { |
| 28 |
return c.control("ioctl", func(fd int) error { |
| 29 |
return unix.IoctlKCMAttach(fd, info) |
| 30 |
}) |
| 31 |
} |
| 32 |
|
| 33 |
// IoctlKCMUnattach wraps ioctl(2) for unix.KCMUnattach values. |
| 34 |
func (c *Conn) IoctlKCMUnattach(info unix.KCMUnattach) error { |
| 35 |
return c.control("ioctl", func(fd int) error { |
| 36 |
return unix.IoctlKCMUnattach(fd, info) |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
// PidfdGetfd wraps pidfd_getfd(2) for a Conn which wraps a pidfd, but returns a |
| 41 |
// Conn rather than a raw file descriptor. |
| 42 |
func (c *Conn) PidfdGetfd(targetFD, flags int) (*Conn, error) { |
| 43 |
outFD, err := controlT(c, "pidfd_getfd", func(fd int) (int, error) { |
| 44 |
return unix.PidfdGetfd(fd, targetFD, flags) |
| 45 |
}) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
// Successful getfd, wrap in a Conn for use by the caller. |
| 51 |
return New(outFD, c.name) |
| 52 |
} |
| 53 |
|
| 54 |
// PidfdSendSignal wraps pidfd_send_signal(2) for a Conn which wraps a Linux |
| 55 |
// pidfd. |
| 56 |
func (c *Conn) PidfdSendSignal(sig unix.Signal, info *unix.Siginfo, flags int) error { |
| 57 |
return c.control("pidfd_send_signal", func(fd int) error { |
| 58 |
return unix.PidfdSendSignal(fd, sig, info, flags) |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
// SetBPF attaches an assembled BPF program to a Conn. |
| 63 |
func (c *Conn) SetBPF(filter []bpf.RawInstruction) error { |
| 64 |
// We can't point to the first instruction in the array if no instructions |
| 65 |
// are present. |
| 66 |
if len(filter) == 0 { |
| 67 |
return os.NewSyscallError("setsockopt", unix.EINVAL) |
| 68 |
} |
| 69 |
|
| 70 |
prog := unix.SockFprog{ |
| 71 |
Len: uint16(len(filter)), |
| 72 |
Filter: (*unix.SockFilter)(unsafe.Pointer(&filter[0])), |
| 73 |
} |
| 74 |
|
| 75 |
return c.SetsockoptSockFprog(unix.SOL_SOCKET, unix.SO_ATTACH_FILTER, &prog) |
| 76 |
} |
| 77 |
|
| 78 |
// RemoveBPF removes a BPF filter from a Conn. |
| 79 |
func (c *Conn) RemoveBPF() error { |
| 80 |
// 0 argument is ignored. |
| 81 |
return c.SetsockoptInt(unix.SOL_SOCKET, unix.SO_DETACH_FILTER, 0) |
| 82 |
} |
| 83 |
|
| 84 |
// SetsockoptPacketMreq wraps setsockopt(2) for unix.PacketMreq values. |
| 85 |
func (c *Conn) SetsockoptPacketMreq(level, opt int, mreq *unix.PacketMreq) error { |
| 86 |
return c.control("setsockopt", func(fd int) error { |
| 87 |
return unix.SetsockoptPacketMreq(fd, level, opt, mreq) |
| 88 |
}) |
| 89 |
} |
| 90 |
|
| 91 |
// SetsockoptSockFprog wraps setsockopt(2) for unix.SockFprog values. |
| 92 |
func (c *Conn) SetsockoptSockFprog(level, opt int, fprog *unix.SockFprog) error { |
| 93 |
return c.control("setsockopt", func(fd int) error { |
| 94 |
return unix.SetsockoptSockFprog(fd, level, opt, fprog) |
| 95 |
}) |
| 96 |
} |
| 97 |
|
| 98 |
// GetsockoptBytes wraps getsockopt(2) for raw byte slice values on Linux. The |
| 99 |
// existing contents of b are passed to the kernel as the input buffer with |
| 100 |
// optlen set to len(b), and the kernel overwrites b in place. The returned int |
| 101 |
// is the optlen value reported by the kernel on return, which may be smaller |
| 102 |
// than len(b). These in/out semantics are required for socket options which |
| 103 |
// both read from and write to the same buffer. |
| 104 |
// |
| 105 |
// No allocation or copying is performed and b is passed to the kernel |
| 106 |
// verbatim. If len(b) is zero, a nil buffer with optlen 0 is passed. |
| 107 |
func (c *Conn) GetsockoptBytes(level, opt int, b []byte) (int, error) { |
| 108 |
return controlT(c, "getsockopt", func(fd int) (int, error) { |
| 109 |
var p unsafe.Pointer |
| 110 |
if len(b) > 0 { |
| 111 |
p = unsafe.Pointer(&b[0]) |
| 112 |
} |
| 113 |
|
| 114 |
optlen := uint32(len(b)) |
| 115 |
_, _, errno := unix.Syscall6( |
| 116 |
unix.SYS_GETSOCKOPT, |
| 117 |
uintptr(fd), |
| 118 |
uintptr(level), |
| 119 |
uintptr(opt), |
| 120 |
uintptr(p), |
| 121 |
uintptr(unsafe.Pointer(&optlen)), |
| 122 |
0, |
| 123 |
) |
| 124 |
if errno != 0 { |
| 125 |
return 0, errno |
| 126 |
} |
| 127 |
|
| 128 |
return int(optlen), nil |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
// SetsockoptBytes wraps setsockopt(2) for raw byte slice values on Linux. The |
| 133 |
// bytes of b are passed to the kernel verbatim with optlen set to len(b) and no |
| 134 |
// NUL handling of any kind is performed. If len(b) is zero, a nil buffer with |
| 135 |
// optlen 0 is passed. |
| 136 |
func (c *Conn) SetsockoptBytes(level, opt int, b []byte) error { |
| 137 |
return c.control("setsockopt", func(fd int) error { |
| 138 |
var p unsafe.Pointer |
| 139 |
if len(b) > 0 { |
| 140 |
p = unsafe.Pointer(&b[0]) |
| 141 |
} |
| 142 |
|
| 143 |
_, _, errno := unix.Syscall6( |
| 144 |
unix.SYS_SETSOCKOPT, |
| 145 |
uintptr(fd), |
| 146 |
uintptr(level), |
| 147 |
uintptr(opt), |
| 148 |
uintptr(p), |
| 149 |
uintptr(len(b)), |
| 150 |
0, |
| 151 |
) |
| 152 |
if errno != 0 { |
| 153 |
return errno |
| 154 |
} |
| 155 |
|
| 156 |
return nil |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
// GetsockoptTpacketStats wraps getsockopt(2) for unix.TpacketStats values. |
| 161 |
func (c *Conn) GetsockoptTpacketStats(level, name int) (*unix.TpacketStats, error) { |
| 162 |
return controlT(c, "getsockopt", func(fd int) (*unix.TpacketStats, error) { |
| 163 |
return unix.GetsockoptTpacketStats(fd, level, name) |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
// GetsockoptTpacketStatsV3 wraps getsockopt(2) for unix.TpacketStatsV3 values. |
| 168 |
func (c *Conn) GetsockoptTpacketStatsV3(level, name int) (*unix.TpacketStatsV3, error) { |
| 169 |
return controlT(c, "getsockopt", func(fd int) (*unix.TpacketStatsV3, error) { |
| 170 |
return unix.GetsockoptTpacketStatsV3(fd, level, name) |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
// Waitid wraps waitid(2). |
| 175 |
func (c *Conn) Waitid(idType int, info *unix.Siginfo, options int, rusage *unix.Rusage) error { |
| 176 |
return c.read(context.Background(), "waitid", func(fd int) error { |
| 177 |
return unix.Waitid(idType, fd, info, options, rusage) |
| 178 |
}) |
| 179 |
} |