ifreq_linux.go

  1// Copyright 2021 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
  5//go:build linux
  6
  7package unix
  8
  9import (
 10	"unsafe"
 11)
 12
 13// Helpers for dealing with ifreq since it contains a union and thus requires a
 14// lot of unsafe.Pointer casts to use properly.
 15
 16// An Ifreq is a type-safe wrapper around the raw ifreq struct. An Ifreq
 17// contains an interface name and a union of arbitrary data which can be
 18// accessed using the Ifreq's methods. To create an Ifreq, use the NewIfreq
 19// function.
 20//
 21// Use the Name method to access the stored interface name. The union data
 22// fields can be get and set using the following methods:
 23//   - Uint16/SetUint16: flags
 24//   - Uint32/SetUint32: ifindex, metric, mtu
 25type Ifreq struct{ raw ifreq }
 26
 27// NewIfreq creates an Ifreq with the input network interface name after
 28// validating the name does not exceed IFNAMSIZ-1 (trailing NULL required)
 29// bytes.
 30func NewIfreq(name string) (*Ifreq, error) {
 31	// Leave room for terminating NULL byte.
 32	if len(name) >= IFNAMSIZ {
 33		return nil, EINVAL
 34	}
 35
 36	var ifr ifreq
 37	copy(ifr.Ifrn[:], name)
 38
 39	return &Ifreq{raw: ifr}, nil
 40}
 41
 42// TODO(mdlayher): get/set methods for hardware address sockaddr, char array, etc.
 43
 44// Name returns the interface name associated with the Ifreq.
 45func (ifr *Ifreq) Name() string {
 46	return ByteSliceToString(ifr.raw.Ifrn[:])
 47}
 48
 49// According to netdevice(7), only AF_INET addresses are returned for numerous
 50// sockaddr ioctls. For convenience, we expose these as Inet4Addr since the Port
 51// field and other data is always empty.
 52
 53// Inet4Addr returns the Ifreq union data from an embedded sockaddr as a C
 54// in_addr/Go []byte (4-byte IPv4 address) value. If the sockaddr family is not
 55// AF_INET, an error is returned.
 56func (ifr *Ifreq) Inet4Addr() ([]byte, error) {
 57	raw := *(*RawSockaddrInet4)(unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]))
 58	if raw.Family != AF_INET {
 59		// Cannot safely interpret raw.Addr bytes as an IPv4 address.
 60		return nil, EINVAL
 61	}
 62
 63	return raw.Addr[:], nil
 64}
 65
 66// SetInet4Addr sets a C in_addr/Go []byte (4-byte IPv4 address) value in an
 67// embedded sockaddr within the Ifreq's union data. v must be 4 bytes in length
 68// or an error will be returned.
 69func (ifr *Ifreq) SetInet4Addr(v []byte) error {
 70	if len(v) != 4 {
 71		return EINVAL
 72	}
 73
 74	var addr [4]byte
 75	copy(addr[:], v)
 76
 77	ifr.clear()
 78	*(*RawSockaddrInet4)(
 79		unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]),
 80	) = RawSockaddrInet4{
 81		// Always set IP family as ioctls would require it anyway.
 82		Family: AF_INET,
 83		Addr:   addr,
 84	}
 85
 86	return nil
 87}
 88
 89// Uint16 returns the Ifreq union data as a C short/Go uint16 value.
 90func (ifr *Ifreq) Uint16() uint16 {
 91	return *(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0]))
 92}
 93
 94// SetUint16 sets a C short/Go uint16 value as the Ifreq's union data.
 95func (ifr *Ifreq) SetUint16(v uint16) {
 96	ifr.clear()
 97	*(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0])) = v
 98}
 99
100// Uint32 returns the Ifreq union data as a C int/Go uint32 value.
101func (ifr *Ifreq) Uint32() uint32 {
102	return *(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0]))
103}
104
105// SetUint32 sets a C int/Go uint32 value as the Ifreq's union data.
106func (ifr *Ifreq) SetUint32(v uint32) {
107	ifr.clear()
108	*(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0])) = v
109}
110
111// clear zeroes the ifreq's union field to prevent trailing garbage data from
112// being sent to the kernel if an ifreq is reused.
113func (ifr *Ifreq) clear() {
114	for i := range ifr.raw.Ifru {
115		ifr.raw.Ifru[i] = 0
116	}
117}
118
119// TODO(mdlayher): export as IfreqData? For now we can provide helpers such as
120// IoctlGetEthtoolDrvinfo which use these APIs under the hood.
121
122// An ifreqData is an Ifreq which carries pointer data. To produce an ifreqData,
123// use the Ifreq.withData method.
124type ifreqData struct {
125	name [IFNAMSIZ]byte
126	// A type separate from ifreq is required in order to comply with the
127	// unsafe.Pointer rules since the "pointer-ness" of data would not be
128	// preserved if it were cast into the byte array of a raw ifreq.
129	data unsafe.Pointer
130	// Pad to the same size as ifreq.
131	_ [len(ifreq{}.Ifru) - SizeofPtr]byte
132}
133
134// withData produces an ifreqData with the pointer p set for ioctls which require
135// arbitrary pointer data.
136func (ifr Ifreq) withData(p unsafe.Pointer) ifreqData {
137	return ifreqData{
138		name: ifr.raw.Ifrn,
139		data: p,
140	}
141}