blob: b364eeaba5dc0570ea3204bf5cc49fb5a967806f [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2011 The Go Authors. All rights reserved.
Mikio Hara12376c92011-05-26 20:02:03 -04002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Joel Sing8f3f4c92013-08-24 01:51:25 +10005// +build darwin dragonfly freebsd netbsd openbsd
Russ Cox27159562011-09-15 16:48:57 -04006
Mikio Hara12376c92011-05-26 20:02:03 -04007package syscall
8
Mikio Hara69275ee2015-02-10 12:24:11 +09009import (
10 "runtime"
11 "unsafe"
12)
13
14var (
15 freebsdConfArch string // "machine $arch" line in kern.conftxt on freebsd
16 minRoutingSockaddrLen = rsaAlignOf(0)
17)
Mikio Hara12376c92011-05-26 20:02:03 -040018
Robert Hencke3fbd4782011-05-30 18:02:59 +100019// Round the length of a raw sockaddr up to align it properly.
Mikio Hara12376c92011-05-26 20:02:03 -040020func rsaAlignOf(salen int) int {
21 salign := sizeofPtr
Mikio Hara9a794722013-08-25 08:44:31 +090022 if darwin64Bit {
Mikio Hara69275ee2015-02-10 12:24:11 +090023 // Darwin kernels require 32-bit aligned access to
24 // routing facilities.
Mikio Hara12376c92011-05-26 20:02:03 -040025 salign = 4
Mikio Hara9a794722013-08-25 08:44:31 +090026 } else if netbsd32Bit {
Mikio Hara69275ee2015-02-10 12:24:11 +090027 // NetBSD 6 and beyond kernels require 64-bit aligned
28 // access to routing facilities.
Mikio Hara9a794722013-08-25 08:44:31 +090029 salign = 8
Mikio Hara69275ee2015-02-10 12:24:11 +090030 } else if runtime.GOOS == "freebsd" {
31 // In the case of kern.supported_archs="amd64 i386",
32 // we need to know the underlying kernel's
33 // architecture because the alignment for routing
34 // facilities are set at the build time of the kernel.
35 if freebsdConfArch == "amd64" {
36 salign = 8
37 }
Mikio Hara12376c92011-05-26 20:02:03 -040038 }
39 if salen == 0 {
40 return salign
41 }
42 return (salen + salign - 1) & ^(salign - 1)
43}
44
Mikio Hara69275ee2015-02-10 12:24:11 +090045// parseSockaddrLink parses b as a datalink socket address.
46func parseSockaddrLink(b []byte) (*SockaddrDatalink, error) {
Mikio Hara707b6192015-09-24 15:23:26 +090047 if len(b) < 8 {
48 return nil, EINVAL
49 }
Mikio Hara69275ee2015-02-10 12:24:11 +090050 sa, _, err := parseLinkLayerAddr(b[4:])
51 if err != nil {
52 return nil, err
53 }
54 rsa := (*RawSockaddrDatalink)(unsafe.Pointer(&b[0]))
55 sa.Len = rsa.Len
56 sa.Family = rsa.Family
57 sa.Index = rsa.Index
58 return sa, nil
59}
60
61// parseLinkLayerAddr parses b as a datalink socket address in
62// conventional BSD kernel form.
63func parseLinkLayerAddr(b []byte) (*SockaddrDatalink, int, error) {
Ainar Garipov7f9f70e2015-06-11 16:49:38 +030064 // The encoding looks like the following:
Mikio Hara69275ee2015-02-10 12:24:11 +090065 // +----------------------------+
66 // | Type (1 octet) |
67 // +----------------------------+
68 // | Name length (1 octet) |
69 // +----------------------------+
70 // | Address length (1 octet) |
71 // +----------------------------+
72 // | Selector length (1 octet) |
73 // +----------------------------+
74 // | Data (variable) |
75 // +----------------------------+
76 type linkLayerAddr struct {
77 Type byte
78 Nlen byte
79 Alen byte
80 Slen byte
81 }
82 lla := (*linkLayerAddr)(unsafe.Pointer(&b[0]))
Mikio Hara707b6192015-09-24 15:23:26 +090083 l := 4 + int(lla.Nlen) + int(lla.Alen) + int(lla.Slen)
Mikio Hara69275ee2015-02-10 12:24:11 +090084 if len(b) < l {
85 return nil, 0, EINVAL
86 }
87 b = b[4:]
88 sa := &SockaddrDatalink{Type: lla.Type, Nlen: lla.Nlen, Alen: lla.Alen, Slen: lla.Slen}
Mikio Hara707b6192015-09-24 15:23:26 +090089 for i := 0; len(sa.Data) > i && i < l-4; i++ {
Mikio Hara69275ee2015-02-10 12:24:11 +090090 sa.Data[i] = int8(b[i])
91 }
Mikio Hara707b6192015-09-24 15:23:26 +090092 return sa, rsaAlignOf(l), nil
Mikio Hara69275ee2015-02-10 12:24:11 +090093}
94
95// parseSockaddrInet parses b as an internet socket address.
96func parseSockaddrInet(b []byte, family byte) (Sockaddr, error) {
97 switch family {
98 case AF_INET:
99 if len(b) < SizeofSockaddrInet4 {
100 return nil, EINVAL
101 }
102 rsa := (*RawSockaddrAny)(unsafe.Pointer(&b[0]))
103 return anyToSockaddr(rsa)
104 case AF_INET6:
105 if len(b) < SizeofSockaddrInet6 {
106 return nil, EINVAL
107 }
108 rsa := (*RawSockaddrAny)(unsafe.Pointer(&b[0]))
109 return anyToSockaddr(rsa)
110 default:
111 return nil, EINVAL
112 }
113}
114
115const (
116 offsetofInet4 = int(unsafe.Offsetof(RawSockaddrInet4{}.Addr))
117 offsetofInet6 = int(unsafe.Offsetof(RawSockaddrInet6{}.Addr))
118)
119
120// parseNetworkLayerAddr parses b as an internet socket address in
121// conventional BSD kernel form.
122func parseNetworkLayerAddr(b []byte, family byte) (Sockaddr, error) {
123 // The encoding looks similar to the NLRI encoding.
124 // +----------------------------+
125 // | Length (1 octet) |
126 // +----------------------------+
127 // | Address prefix (variable) |
128 // +----------------------------+
129 //
130 // The differences between the kernel form and the NLRI
131 // encoding are:
132 //
133 // - The length field of the kernel form indicates the prefix
134 // length in bytes, not in bits
135 //
136 // - In the kernel form, zero value of the length field
137 // doesn't mean 0.0.0.0/0 or ::/0
138 //
139 // - The kernel form appends leading bytes to the prefix field
140 // to make the <length, prefix> tuple to be conformed with
Martin Möhrmannfdd01792016-02-24 11:55:20 +0100141 // the routing message boundary
Mikio Hara69275ee2015-02-10 12:24:11 +0900142 l := int(rsaAlignOf(int(b[0])))
143 if len(b) < l {
144 return nil, EINVAL
145 }
Mikio Hara59cc5a12015-03-01 23:58:28 +0900146 // Don't reorder case expressions.
147 // The case expressions for IPv6 must come first.
148 switch {
149 case b[0] == SizeofSockaddrInet6:
150 sa := &SockaddrInet6{}
151 copy(sa.Addr[:], b[offsetofInet6:])
152 return sa, nil
153 case family == AF_INET6:
Mikio Hara69275ee2015-02-10 12:24:11 +0900154 sa := &SockaddrInet6{}
155 if l-1 < offsetofInet6 {
156 copy(sa.Addr[:], b[1:l])
157 } else {
158 copy(sa.Addr[:], b[l-offsetofInet6:l])
159 }
160 return sa, nil
Mikio Hara59cc5a12015-03-01 23:58:28 +0900161 case b[0] == SizeofSockaddrInet4:
162 sa := &SockaddrInet4{}
163 copy(sa.Addr[:], b[offsetofInet4:])
164 return sa, nil
Mikio Hara69275ee2015-02-10 12:24:11 +0900165 default: // an old fashion, AF_UNSPEC or unknown means AF_INET
166 sa := &SockaddrInet4{}
167 if l-1 < offsetofInet4 {
168 copy(sa.Addr[:], b[1:l])
169 } else {
170 copy(sa.Addr[:], b[l-offsetofInet4:l])
171 }
172 return sa, nil
173 }
174}
175
Mikio Hara12376c92011-05-26 20:02:03 -0400176// RouteRIB returns routing information base, as known as RIB,
177// which consists of network facility information, states and
178// parameters.
Mikio Hara6d668192016-04-23 22:36:41 +0900179//
180// Deprecated: Use golang.org/x/net/route instead.
Russ Coxc017a822011-11-13 22:44:52 -0500181func RouteRIB(facility, param int) ([]byte, error) {
Mikio Hara12376c92011-05-26 20:02:03 -0400182 mib := []_C_int{CTL_NET, AF_ROUTE, 0, 0, _C_int(facility), _C_int(param)}
Mikio Hara12376c92011-05-26 20:02:03 -0400183 // Find size.
184 n := uintptr(0)
Russ Coxc017a822011-11-13 22:44:52 -0500185 if err := sysctl(mib, nil, &n, nil, 0); err != nil {
186 return nil, err
Mikio Hara12376c92011-05-26 20:02:03 -0400187 }
188 if n == 0 {
Russ Coxc017a822011-11-13 22:44:52 -0500189 return nil, nil
Mikio Hara12376c92011-05-26 20:02:03 -0400190 }
Russ Coxc017a822011-11-13 22:44:52 -0500191 tab := make([]byte, n)
192 if err := sysctl(mib, &tab[0], &n, nil, 0); err != nil {
193 return nil, err
Mikio Hara12376c92011-05-26 20:02:03 -0400194 }
Russ Coxc017a822011-11-13 22:44:52 -0500195 return tab[:n], nil
Mikio Hara12376c92011-05-26 20:02:03 -0400196}
197
198// RoutingMessage represents a routing message.
Mikio Hara6d668192016-04-23 22:36:41 +0900199//
200// Deprecated: Use golang.org/x/net/route instead.
Mikio Hara12376c92011-05-26 20:02:03 -0400201type RoutingMessage interface {
Mikio Hara69275ee2015-02-10 12:24:11 +0900202 sockaddr() ([]Sockaddr, error)
Mikio Hara12376c92011-05-26 20:02:03 -0400203}
204
Russ Cox61f4ec12011-06-17 17:07:21 -0400205const anyMessageLen = int(unsafe.Sizeof(anyMessage{}))
Mikio Hara12376c92011-05-26 20:02:03 -0400206
207type anyMessage struct {
208 Msglen uint16
209 Version uint8
210 Type uint8
211}
212
Mikio Hara12376c92011-05-26 20:02:03 -0400213// RouteMessage represents a routing message containing routing
214// entries.
Mikio Hara6d668192016-04-23 22:36:41 +0900215//
216// Deprecated: Use golang.org/x/net/route instead.
Mikio Hara12376c92011-05-26 20:02:03 -0400217type RouteMessage struct {
218 Header RtMsghdr
219 Data []byte
220}
221
Mikio Hara69275ee2015-02-10 12:24:11 +0900222func (m *RouteMessage) sockaddr() ([]Sockaddr, error) {
223 var sas [RTAX_MAX]Sockaddr
Mikio Harac5f5df42013-02-24 12:36:44 +0900224 b := m.Data[:]
Mikio Hara69275ee2015-02-10 12:24:11 +0900225 family := uint8(AF_UNSPEC)
226 for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
227 if m.Header.Addrs&(1<<i) == 0 {
Mikio Hara0c6581c2011-08-31 13:05:49 -0400228 continue
229 }
Mikio Harac5f5df42013-02-24 12:36:44 +0900230 rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
Mikio Hara69275ee2015-02-10 12:24:11 +0900231 switch rsa.Family {
232 case AF_LINK:
233 sa, err := parseSockaddrLink(b)
Mikio Hara83610562011-12-21 21:39:00 +0900234 if err != nil {
Mikio Hara69275ee2015-02-10 12:24:11 +0900235 return nil, err
Mikio Hara0c6581c2011-08-31 13:05:49 -0400236 }
237 sas[i] = sa
Mikio Hara69275ee2015-02-10 12:24:11 +0900238 b = b[rsaAlignOf(int(rsa.Len)):]
239 case AF_INET, AF_INET6:
240 sa, err := parseSockaddrInet(b, rsa.Family)
241 if err != nil {
242 return nil, err
Mikio Hara0c6581c2011-08-31 13:05:49 -0400243 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900244 sas[i] = sa
245 b = b[rsaAlignOf(int(rsa.Len)):]
246 family = rsa.Family
247 default:
248 sa, err := parseNetworkLayerAddr(b, family)
249 if err != nil {
250 return nil, err
251 }
252 sas[i] = sa
253 b = b[rsaAlignOf(int(b[0])):]
Mikio Hara0c6581c2011-08-31 13:05:49 -0400254 }
Mikio Hara0c6581c2011-08-31 13:05:49 -0400255 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900256 return sas[:], nil
Mikio Hara12376c92011-05-26 20:02:03 -0400257}
258
259// InterfaceMessage represents a routing message containing
260// network interface entries.
Mikio Hara6d668192016-04-23 22:36:41 +0900261//
262// Deprecated: Use golang.org/x/net/route instead.
Mikio Hara12376c92011-05-26 20:02:03 -0400263type InterfaceMessage struct {
264 Header IfMsghdr
265 Data []byte
266}
267
Mikio Hara69275ee2015-02-10 12:24:11 +0900268func (m *InterfaceMessage) sockaddr() ([]Sockaddr, error) {
269 var sas [RTAX_MAX]Sockaddr
Mikio Hara12376c92011-05-26 20:02:03 -0400270 if m.Header.Addrs&RTA_IFP == 0 {
Mikio Hara69275ee2015-02-10 12:24:11 +0900271 return nil, nil
Mikio Hara12376c92011-05-26 20:02:03 -0400272 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900273 sa, err := parseSockaddrLink(m.Data[:])
Mikio Hara83610562011-12-21 21:39:00 +0900274 if err != nil {
Mikio Hara69275ee2015-02-10 12:24:11 +0900275 return nil, err
Mikio Hara12376c92011-05-26 20:02:03 -0400276 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900277 sas[RTAX_IFP] = sa
278 return sas[:], nil
Mikio Hara12376c92011-05-26 20:02:03 -0400279}
280
281// InterfaceAddrMessage represents a routing message containing
282// network interface address entries.
Mikio Hara6d668192016-04-23 22:36:41 +0900283//
284// Deprecated: Use golang.org/x/net/route instead.
Mikio Hara12376c92011-05-26 20:02:03 -0400285type InterfaceAddrMessage struct {
286 Header IfaMsghdr
287 Data []byte
288}
289
Mikio Hara69275ee2015-02-10 12:24:11 +0900290func (m *InterfaceAddrMessage) sockaddr() ([]Sockaddr, error) {
291 var sas [RTAX_MAX]Sockaddr
Mikio Harac5f5df42013-02-24 12:36:44 +0900292 b := m.Data[:]
Mikio Hara69275ee2015-02-10 12:24:11 +0900293 family := uint8(AF_UNSPEC)
294 for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
Russ Cox96e9e812014-10-28 11:35:00 -0400295 if m.Header.Addrs&(1<<i) == 0 {
Mikio Hara12376c92011-05-26 20:02:03 -0400296 continue
297 }
Mikio Harac5f5df42013-02-24 12:36:44 +0900298 rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
Mikio Hara69275ee2015-02-10 12:24:11 +0900299 switch rsa.Family {
300 case AF_LINK:
301 sa, err := parseSockaddrLink(b)
Mikio Hara83610562011-12-21 21:39:00 +0900302 if err != nil {
Mikio Hara69275ee2015-02-10 12:24:11 +0900303 return nil, err
Mikio Hara12376c92011-05-26 20:02:03 -0400304 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900305 sas[i] = sa
306 b = b[rsaAlignOf(int(rsa.Len)):]
307 case AF_INET, AF_INET6:
308 sa, err := parseSockaddrInet(b, rsa.Family)
Mikio Hara83610562011-12-21 21:39:00 +0900309 if err != nil {
Mikio Hara69275ee2015-02-10 12:24:11 +0900310 return nil, err
Mikio Hara83610562011-12-21 21:39:00 +0900311 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900312 sas[i] = sa
313 b = b[rsaAlignOf(int(rsa.Len)):]
314 family = rsa.Family
315 default:
316 sa, err := parseNetworkLayerAddr(b, family)
317 if err != nil {
318 return nil, err
319 }
320 sas[i] = sa
321 b = b[rsaAlignOf(int(b[0])):]
Mikio Hara12376c92011-05-26 20:02:03 -0400322 }
Mikio Hara12376c92011-05-26 20:02:03 -0400323 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900324 return sas[:], nil
Mikio Hara12376c92011-05-26 20:02:03 -0400325}
326
Mikio Harac5f5df42013-02-24 12:36:44 +0900327// ParseRoutingMessage parses b as routing messages and returns the
328// slice containing the RoutingMessage interfaces.
Mikio Hara6d668192016-04-23 22:36:41 +0900329//
330// Deprecated: Use golang.org/x/net/route instead.
Mikio Harac5f5df42013-02-24 12:36:44 +0900331func ParseRoutingMessage(b []byte) (msgs []RoutingMessage, err error) {
Mikio Hara69275ee2015-02-10 12:24:11 +0900332 nmsgs, nskips := 0, 0
Mikio Harac5f5df42013-02-24 12:36:44 +0900333 for len(b) >= anyMessageLen {
Mikio Hara69275ee2015-02-10 12:24:11 +0900334 nmsgs++
Mikio Harac5f5df42013-02-24 12:36:44 +0900335 any := (*anyMessage)(unsafe.Pointer(&b[0]))
Mikio Hara12376c92011-05-26 20:02:03 -0400336 if any.Version != RTM_VERSION {
Joel Sing517e49e2013-12-11 00:03:46 +1100337 b = b[any.Msglen:]
338 continue
Mikio Hara12376c92011-05-26 20:02:03 -0400339 }
Mikio Hara69275ee2015-02-10 12:24:11 +0900340 if m := any.toRoutingMessage(b); m == nil {
341 nskips++
342 } else {
343 msgs = append(msgs, m)
344 }
Mikio Harac5f5df42013-02-24 12:36:44 +0900345 b = b[any.Msglen:]
Mikio Hara12376c92011-05-26 20:02:03 -0400346 }
Joel Sing517e49e2013-12-11 00:03:46 +1100347 // We failed to parse any of the messages - version mismatch?
Mikio Hara69275ee2015-02-10 12:24:11 +0900348 if nmsgs != len(msgs)+nskips {
Joel Sing517e49e2013-12-11 00:03:46 +1100349 return nil, EINVAL
350 }
Russ Coxc017a822011-11-13 22:44:52 -0500351 return msgs, nil
Mikio Hara12376c92011-05-26 20:02:03 -0400352}
353
Yann Kerhervé5c248322015-12-04 10:07:44 -0800354// ParseRoutingSockaddr parses msg's payload as raw sockaddrs and
Mikio Hara12376c92011-05-26 20:02:03 -0400355// returns the slice containing the Sockaddr interfaces.
Mikio Hara6d668192016-04-23 22:36:41 +0900356//
357// Deprecated: Use golang.org/x/net/route instead.
Mikio Hara69275ee2015-02-10 12:24:11 +0900358func ParseRoutingSockaddr(msg RoutingMessage) ([]Sockaddr, error) {
359 sas, err := msg.sockaddr()
360 if err != nil {
361 return nil, err
362 }
363 return sas, nil
Mikio Hara12376c92011-05-26 20:02:03 -0400364}