ifreq_linux.go 4.5 KB

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