diff options
author | Jordan Whited <jordan@tailscale.com> | 2023-03-14 20:02:24 -0700 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2023-03-16 17:45:41 +0100 |
commit | fff53afca779078061128a5a10c31e67a7919d35 (patch) | |
tree | ef321d142b7efb9134b6206c8ae5540b9e276298 | |
parent | 0ad14a89f5f9da577dae6a63ad196015e51a0381 (diff) |
conn: use CmsgSpace() for ancillary data buf sizing
CmsgLen() does not account for data alignment.
Reviewed-by: Adrian Dewhurst <adrian@tailscale.com>
Signed-off-by: Jordan Whited <jordan@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | conn/sticky_linux.go | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/conn/sticky_linux.go b/conn/sticky_linux.go index bf17839..342e739 100644 --- a/conn/sticky_linux.go +++ b/conn/sticky_linux.go @@ -63,8 +63,9 @@ func pktInfoFromBuf[T unix.Inet4Pktinfo | unix.Inet6Pktinfo](buf []byte) (t T) { return t } -// setSrcControl parses the control for PKTINFO and if found updates ep with -// the source information found. +// setSrcControl sets an IP{V6}_PKTINFO in control based on the source address +// and source ifindex found in ep. control's len will be set to 0 in the event +// that ep is a default value. func setSrcControl(control *[]byte, ep *StdNetEndpoint) { *control = (*control)[:cap(*control)] if len(*control) < int(unsafe.Sizeof(unix.Cmsghdr{})) { @@ -93,19 +94,20 @@ func setSrcControl(control *[]byte, ep *StdNetEndpoint) { if ep.SrcIP().IsValid() { info.Spec_dst = ep.SrcIP().As4() } + *control = (*control)[:unix.CmsgSpace(unix.SizeofInet4Pktinfo)] } else { hdr.Level = unix.IPPROTO_IPV6 hdr.Type = unix.IPV6_PKTINFO - hdr.Len = unix.SizeofCmsghdr + unix.SizeofInet6Pktinfo + hdr.SetLen(unix.CmsgLen(unix.SizeofInet6Pktinfo)) info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&(*control)[unix.SizeofCmsghdr])) info.Ifindex = uint32(ep.src.ifidx) if ep.SrcIP().IsValid() { info.Addr = ep.SrcIP().As16() } + *control = (*control)[:unix.CmsgSpace(unix.SizeofInet6Pktinfo)] } - *control = (*control)[:hdr.Len] } -var srcControlSize = unix.CmsgLen(unix.SizeofInet6Pktinfo) +var srcControlSize = unix.CmsgSpace(unix.SizeofInet6Pktinfo) |