|
| 1 | +package conn |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | + "net/netip" |
| 7 | + "runtime" |
| 8 | + "slices" |
| 9 | + "unsafe" |
| 10 | + |
| 11 | + "golang.org/x/sys/unix" |
| 12 | +) |
| 13 | + |
| 14 | +const socketControlMessageBufferSize = unix.SizeofCmsghdr + max(alignedSizeofInet4Addr, alignedSizeofInet6Pktinfo) + |
| 15 | + unix.SizeofCmsghdr + alignedSizeofDstPort |
| 16 | + |
| 17 | +const ( |
| 18 | + sizeofInet4Addr = 4 // sizeof(struct in_addr) |
| 19 | + sizeofDstPort = 2 // sizeof(u_int16_t) |
| 20 | +) |
| 21 | + |
| 22 | +func cmsgAlign(n int) int { |
| 23 | + salign := unix.SizeofPtr |
| 24 | + // OpenBSD armv7 requires 64-bit alignment. |
| 25 | + if runtime.GOARCH == "arm" { |
| 26 | + salign = 8 |
| 27 | + } |
| 28 | + return (n + salign - 1) & ^(salign - 1) |
| 29 | +} |
| 30 | + |
| 31 | +func parseSocketControlMessage(cmsg []byte) (m SocketControlMessage, err error) { |
| 32 | + for len(cmsg) >= unix.SizeofCmsghdr { |
| 33 | + cmsghdr := (*unix.Cmsghdr)(unsafe.Pointer(unsafe.SliceData(cmsg))) |
| 34 | + msgSize := cmsgAlign(int(cmsghdr.Len)) |
| 35 | + if cmsghdr.Len < unix.SizeofCmsghdr || msgSize > len(cmsg) { |
| 36 | + return m, fmt.Errorf("invalid control message length %d", cmsghdr.Len) |
| 37 | + } |
| 38 | + |
| 39 | + switch cmsghdr.Level { |
| 40 | + case unix.IPPROTO_IP: |
| 41 | + switch cmsghdr.Type { |
| 42 | + case unix.IP_RECVDSTADDR: |
| 43 | + if len(cmsg) < unix.SizeofCmsghdr+sizeofInet4Addr { |
| 44 | + return m, fmt.Errorf("invalid IP_RECVDSTADDR control message length %d", cmsghdr.Len) |
| 45 | + } |
| 46 | + addr := [sizeofInet4Addr]byte(cmsg[unix.SizeofCmsghdr:]) |
| 47 | + m.PktinfoAddr = netip.AddrFrom4(addr) |
| 48 | + // OpenBSD also uses this for transparent proxies. |
| 49 | + m.OriginalDestinationAddrPort = netip.AddrPortFrom(m.PktinfoAddr, m.OriginalDestinationAddrPort.Port()) |
| 50 | + |
| 51 | + case unix.IP_RECVDSTPORT: |
| 52 | + if len(cmsg) < unix.SizeofCmsghdr+sizeofDstPort { |
| 53 | + return m, fmt.Errorf("invalid IP_RECVDSTPORT control message length %d", cmsghdr.Len) |
| 54 | + } |
| 55 | + port := binary.BigEndian.Uint16(cmsg[unix.SizeofCmsghdr:]) |
| 56 | + m.OriginalDestinationAddrPort = netip.AddrPortFrom(m.PktinfoAddr, port) |
| 57 | + } |
| 58 | + |
| 59 | + case unix.IPPROTO_IPV6: |
| 60 | + switch cmsghdr.Type { |
| 61 | + case unix.IPV6_PKTINFO: |
| 62 | + if len(cmsg) < unix.SizeofCmsghdr+unix.SizeofInet6Pktinfo { |
| 63 | + return m, fmt.Errorf("invalid IPV6_PKTINFO control message length %d", cmsghdr.Len) |
| 64 | + } |
| 65 | + var pktinfo unix.Inet6Pktinfo |
| 66 | + _ = copy(unsafe.Slice((*byte)(unsafe.Pointer(&pktinfo)), unix.SizeofInet6Pktinfo), cmsg[unix.SizeofCmsghdr:]) |
| 67 | + m.PktinfoAddr = netip.AddrFrom16(pktinfo.Addr) |
| 68 | + m.PktinfoIfindex = pktinfo.Ifindex |
| 69 | + // OpenBSD also uses this for transparent proxies. |
| 70 | + m.OriginalDestinationAddrPort = netip.AddrPortFrom(m.PktinfoAddr, m.OriginalDestinationAddrPort.Port()) |
| 71 | + |
| 72 | + case unix.IPV6_RECVDSTPORT: |
| 73 | + if len(cmsg) < unix.SizeofCmsghdr+sizeofDstPort { |
| 74 | + return m, fmt.Errorf("invalid IPV6_RECVDSTPORT control message length %d", cmsghdr.Len) |
| 75 | + } |
| 76 | + port := binary.BigEndian.Uint16(cmsg[unix.SizeofCmsghdr:]) |
| 77 | + m.OriginalDestinationAddrPort = netip.AddrPortFrom(m.PktinfoAddr, port) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + cmsg = cmsg[msgSize:] |
| 82 | + } |
| 83 | + |
| 84 | + return m, nil |
| 85 | +} |
| 86 | + |
| 87 | +const ( |
| 88 | + alignedSizeofInet4Addr = (sizeofInet4Addr + unix.SizeofPtr - 1) & ^(unix.SizeofPtr - 1) |
| 89 | + alignedSizeofInet6Pktinfo = (unix.SizeofInet6Pktinfo + unix.SizeofPtr - 1) & ^(unix.SizeofPtr - 1) |
| 90 | + alignedSizeofDstPort = (sizeofDstPort + unix.SizeofPtr - 1) & ^(unix.SizeofPtr - 1) |
| 91 | +) |
| 92 | + |
| 93 | +func (m SocketControlMessage) appendTo(b []byte) []byte { |
| 94 | + switch { |
| 95 | + case m.PktinfoAddr.Is4(): |
| 96 | + bLen := len(b) |
| 97 | + b = slices.Grow(b, unix.SizeofCmsghdr+alignedSizeofInet4Addr)[:bLen+unix.SizeofCmsghdr+alignedSizeofInet4Addr] |
| 98 | + msgBuf := b[bLen:] |
| 99 | + cmsghdr := (*unix.Cmsghdr)(unsafe.Pointer(unsafe.SliceData(msgBuf))) |
| 100 | + *cmsghdr = unix.Cmsghdr{ |
| 101 | + Len: unix.SizeofCmsghdr + sizeofInet4Addr, |
| 102 | + Level: unix.IPPROTO_IP, |
| 103 | + Type: unix.IP_SENDSRCADDR, |
| 104 | + } |
| 105 | + addr := m.PktinfoAddr.As4() |
| 106 | + _ = copy(msgBuf[unix.SizeofCmsghdr:], addr[:]) |
| 107 | + |
| 108 | + case m.PktinfoAddr.Is6(): |
| 109 | + bLen := len(b) |
| 110 | + b = slices.Grow(b, unix.SizeofCmsghdr+alignedSizeofInet6Pktinfo)[:bLen+unix.SizeofCmsghdr+alignedSizeofInet6Pktinfo] |
| 111 | + msgBuf := b[bLen:] |
| 112 | + cmsghdr := (*unix.Cmsghdr)(unsafe.Pointer(unsafe.SliceData(msgBuf))) |
| 113 | + *cmsghdr = unix.Cmsghdr{ |
| 114 | + Len: unix.SizeofCmsghdr + unix.SizeofInet6Pktinfo, |
| 115 | + Level: unix.IPPROTO_IPV6, |
| 116 | + Type: unix.IPV6_PKTINFO, |
| 117 | + } |
| 118 | + pktinfo := unix.Inet6Pktinfo{ |
| 119 | + Addr: m.PktinfoAddr.As16(), |
| 120 | + Ifindex: m.PktinfoIfindex, |
| 121 | + } |
| 122 | + _ = copy(msgBuf[unix.SizeofCmsghdr:], unsafe.Slice((*byte)(unsafe.Pointer(&pktinfo)), unix.SizeofInet6Pktinfo)) |
| 123 | + } |
| 124 | + |
| 125 | + return b |
| 126 | +} |
0 commit comments