ifreq_linux.go 4.3 KB

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