sys_freebsd.mx raw

   1  // Copyright 2016 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  package routebsd
   6  
   7  import (
   8  	"syscall"
   9  	"unsafe"
  10  )
  11  
  12  // MTU returns the interface MTU.
  13  func (m *InterfaceMessage) MTU() int {
  14  	return int(nativeEndian.Uint32(m.raw[m.extOff+8 : m.extOff+12]))
  15  }
  16  
  17  // sizeofIfMsghdr is the size used on FreeBSD 11 for all platforms.
  18  const sizeofIfMsghdr = 0xa8
  19  
  20  func probeRoutingStack() (int, map[int]*wireFormat) {
  21  	var p uintptr
  22  	wordSize := int(unsafe.Sizeof(p))
  23  	align := wordSize
  24  	// In the case of kern.supported_archs="amd64 i386", we need
  25  	// to know the underlying kernel's architecture because the
  26  	// alignment for routing facilities are set at the build time
  27  	// of the kernel.
  28  	conf, _ := syscall.Sysctl("kern.conftxt")
  29  	for i, j := 0, 0; j < len(conf); j++ {
  30  		if conf[j] != '\n' {
  31  			continue
  32  		}
  33  		s := conf[i:j]
  34  		i = j + 1
  35  		if len(s) > len("machine") && s[:len("machine")] == "machine" {
  36  			s = s[len("machine"):]
  37  			for k := 0; k < len(s); k++ {
  38  				if s[k] == ' ' || s[k] == '\t' {
  39  					s = s[1:]
  40  				}
  41  				break
  42  			}
  43  			if s == "amd64" {
  44  				align = 8
  45  			}
  46  			break
  47  		}
  48  	}
  49  	ifm := &wireFormat{extOff: 16, bodyOff: sizeofIfMsghdr}
  50  	ifm.parse = ifm.parseInterfaceMessage
  51  	ifam := &wireFormat{extOff: syscall.SizeofIfaMsghdr, bodyOff: syscall.SizeofIfaMsghdr}
  52  	ifam.parse = ifam.parseInterfaceAddrMessage
  53  	ifmam := &wireFormat{extOff: syscall.SizeofIfmaMsghdr, bodyOff: syscall.SizeofIfmaMsghdr}
  54  	ifmam.parse = ifmam.parseInterfaceMulticastAddrMessage
  55  	return align, map[int]*wireFormat{
  56  		syscall.RTM_NEWADDR:  ifam,
  57  		syscall.RTM_DELADDR:  ifam,
  58  		syscall.RTM_IFINFO:   ifm,
  59  		syscall.RTM_NEWMADDR: ifmam,
  60  		syscall.RTM_DELMADDR: ifmam,
  61  	}
  62  }
  63