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.
5 // +build darwin dragonfly freebsd netbsd openbsd
7 // Package route provides basic functions for the manipulation of
8 // packet routing facilities on BSD variants.
10 // The package supports any version of Darwin, any version of
11 // DragonFly BSD, FreeBSD 7 through 11, NetBSD 6 and above, and
12 // OpenBSD 5.6 and above.
22 errUnsupportedMessage = errors.New("unsupported message")
23 errMessageMismatch = errors.New("message mismatch")
24 errMessageTooShort = errors.New("message too short")
25 errInvalidMessage = errors.New("invalid message")
26 errInvalidAddr = errors.New("invalid address")
27 errShortBuffer = errors.New("short buffer")
30 // A RouteMessage represents a message conveying an address prefix, a
31 // nexthop address and an output interface.
33 // Unlike other messages, this message can be used to query adjacency
34 // information for the given address prefix, to add a new route, and
35 // to delete or modify the existing route from the routing information
36 // base inside the kernel by writing and reading route messages on a
39 // For the manipulation of routing information, the route message must
40 // contain appropriate fields that include:
42 // Version = <must be specified>
43 // Type = <must be specified>
44 // Flags = <must be specified>
45 // Index = <must be specified if necessary>
46 // ID = <must be specified>
47 // Seq = <must be specified>
48 // Addrs = <must be specified>
50 // The Type field specifies a type of manipulation, the Flags field
51 // specifies a class of target information and the Addrs field
52 // specifies target information like the following:
54 // route.RouteMessage{
55 // Version: RTM_VERSION,
57 // Flags: RTF_UP | RTF_HOST,
58 // ID: uintptr(os.Getpid()),
60 // Addrs: []route.Addrs{
61 // RTAX_DST: &route.Inet4Addr{ ... },
62 // RTAX_IFP: &route.LinkAddr{ ... },
63 // RTAX_BRD: &route.Inet4Addr{ ... },
67 // The values for the above fields depend on the implementation of
68 // each operating system.
70 // The Err field on a response message contains an error value on the
71 // requested operation. If non-nil, the requested operation is failed.
72 type RouteMessage struct {
73 Version int // message version
74 Type int // message type
75 Flags int // route flags
76 Index int // interface index when atatched
77 ID uintptr // sender's identifier; usually process ID
78 Seq int // sequence number
79 Err error // error on requested operation
80 Addrs []Addr // addresses
82 extOff int // offset of header extension
83 raw []byte // raw message
86 // Marshal returns the binary encoding of m.
87 func (m *RouteMessage) Marshal() ([]byte, error) {
91 // A RIBType reprensents a type of routing information base.
95 RIBTypeRoute RIBType = syscall.NET_RT_DUMP
96 RIBTypeInterface RIBType = syscall.NET_RT_IFLIST
99 // FetchRIB fetches a routing information base from the operating
102 // The provided af must be an address family.
104 // The provided arg must be a RIBType-specific argument.
105 // When RIBType is related to routes, arg might be a set of route
106 // flags. When RIBType is related to network interfaces, arg might be
107 // an interface index or a set of interface flags. In most cases, zero
109 func FetchRIB(af int, typ RIBType, arg int) ([]byte, error) {
110 mib := [6]int32{sysCTL_NET, sysAF_ROUTE, 0, int32(af), int32(typ), int32(arg)}
112 if err := sysctl(mib[:], nil, &n, nil, 0); err != nil {
113 return nil, os.NewSyscallError("sysctl", err)
119 if err := sysctl(mib[:], &b[0], &n, nil, 0); err != nil {
120 return nil, os.NewSyscallError("sysctl", err)