1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netlink
import (
"fmt"
"math"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/binary"
"gvisor.dev/gvisor/pkg/hostarch"
)
// alignPad returns the length of padding required for alignment.
//
// Preconditions: align is a power of two.
func alignPad(length int, align uint) int {
return binary.AlignUp(length, align) - length
}
// Message contains a complete serialized netlink message.
type Message struct {
hdr linux.NetlinkMessageHeader
buf []byte
}
// NewMessage creates a new Message containing the passed header.
//
// The header length will be updated by Finalize.
func NewMessage(hdr linux.NetlinkMessageHeader) *Message {
return &Message{
hdr: hdr,
buf: binary.Marshal(nil, hostarch.ByteOrder, hdr),
}
}
// ParseMessage parses the first message seen at buf, returning the rest of the
// buffer. If message is malformed, ok of false is returned. For last message,
// padding check is loose, if there isn't enought padding, whole buf is consumed
// and ok is set to true.
func ParseMessage(buf []byte) (msg *Message, rest []byte, ok bool) {
b := BytesView(buf)
hdrBytes, ok := b.Extract(linux.NetlinkMessageHeaderSize)
if !ok {
return
}
var hdr linux.NetlinkMessageHeader
binary.Unmarshal(hdrBytes, hostarch.ByteOrder, &hdr)
// Msg portion.
totalMsgLen := int(hdr.Length)
_, ok = b.Extract(totalMsgLen - linux.NetlinkMessageHeaderSize)
if !ok {
return
}
// Padding.
numPad := alignPad(totalMsgLen, linux.NLMSG_ALIGNTO)
// Linux permits the last message not being aligned, just consume all of it.
// Ref: net/netlink/af_netlink.c:netlink_rcv_skb
if numPad > len(b) {
numPad = len(b)
}
_, ok = b.Extract(numPad)
if !ok {
return
}
return &Message{
hdr: hdr,
buf: buf[:totalMsgLen],
}, []byte(b), true
}
// Header returns the header of this message.
func (m *Message) Header() linux.NetlinkMessageHeader {
return m.hdr
}
// GetData unmarshals the payload message header from this netlink message, and
// returns the attributes portion.
func (m *Message) GetData(msg interface{}) (AttrsView, bool) {
b := BytesView(m.buf)
_, ok := b.Extract(linux.NetlinkMessageHeaderSize)
if !ok {
return nil, false
}
size := int(binary.Size(msg))
msgBytes, ok := b.Extract(size)
if !ok {
return nil, false
}
binary.Unmarshal(msgBytes, hostarch.ByteOrder, msg)
numPad := alignPad(linux.NetlinkMessageHeaderSize+size, linux.NLMSG_ALIGNTO)
// Linux permits the last message not being aligned, just consume all of it.
// Ref: net/netlink/af_netlink.c:netlink_rcv_skb
if numPad > len(b) {
numPad = len(b)
}
_, ok = b.Extract(numPad)
if !ok {
return nil, false
}
return AttrsView(b), true
}
// Finalize returns the []byte containing the entire message, with the total
// length set in the message header. The Message must not be modified after
// calling Finalize.
func (m *Message) Finalize() []byte {
// Update length, which is the first 4 bytes of the header.
hostarch.ByteOrder.PutUint32(m.buf, uint32(len(m.buf)))
// Align the message. Note that the message length in the header (set
// above) is the useful length of the message, not the total aligned
// length. See net/netlink/af_netlink.c:__nlmsg_put.
aligned := binary.AlignUp(len(m.buf), linux.NLMSG_ALIGNTO)
m.putZeros(aligned - len(m.buf))
return m.buf
}
// putZeros adds n zeros to the message.
func (m *Message) putZeros(n int) {
for n > 0 {
m.buf = append(m.buf, 0)
n--
}
}
// Put serializes v into the message.
func (m *Message) Put(v interface{}) {
m.buf = binary.Marshal(m.buf, hostarch.ByteOrder, v)
}
// PutAttr adds v to the message as a netlink attribute.
//
// Preconditions: The serialized attribute (linux.NetlinkAttrHeaderSize +
// binary.Size(v) fits in math.MaxUint16 bytes.
func (m *Message) PutAttr(atype uint16, v interface{}) {
l := linux.NetlinkAttrHeaderSize + int(binary.Size(v))
if l > math.MaxUint16 {
panic(fmt.Sprintf("attribute too large: %d", l))
}
m.Put(linux.NetlinkAttrHeader{
Type: atype,
Length: uint16(l),
})
m.Put(v)
// Align the attribute.
aligned := binary.AlignUp(l, linux.NLA_ALIGNTO)
m.putZeros(aligned - l)
}
// PutAttrString adds s to the message as a netlink attribute.
func (m *Message) PutAttrString(atype uint16, s string) {
l := linux.NetlinkAttrHeaderSize + len(s) + 1
m.Put(linux.NetlinkAttrHeader{
Type: atype,
Length: uint16(l),
})
// String + NUL-termination.
m.Put([]byte(s))
m.putZeros(1)
// Align the attribute.
aligned := binary.AlignUp(l, linux.NLA_ALIGNTO)
m.putZeros(aligned - l)
}
// MessageSet contains a series of netlink messages.
type MessageSet struct {
// Multi indicates that this a multi-part message, to be terminated by
// NLMSG_DONE. NLMSG_DONE is sent even if the set contains only one
// Message.
//
// If Multi is set, all added messages will have NLM_F_MULTI set.
Multi bool
// PortID is the destination port for all messages.
PortID int32
// Seq is the sequence counter for all messages in the set.
Seq uint32
// Messages contains the messages in the set.
Messages []*Message
}
// NewMessageSet creates a new MessageSet.
//
// portID is the destination port to set as PortID in all messages.
//
// seq is the sequence counter to set as seq in all messages in the set.
func NewMessageSet(portID int32, seq uint32) *MessageSet {
return &MessageSet{
PortID: portID,
Seq: seq,
}
}
// AddMessage adds a new message to the set and returns it for further
// additions.
//
// The passed header will have Seq, PortID and the multi flag set
// automatically.
func (ms *MessageSet) AddMessage(hdr linux.NetlinkMessageHeader) *Message {
hdr.Seq = ms.Seq
hdr.PortID = uint32(ms.PortID)
if ms.Multi {
hdr.Flags |= linux.NLM_F_MULTI
}
m := NewMessage(hdr)
ms.Messages = append(ms.Messages, m)
return m
}
// AttrsView is a view into the attributes portion of a netlink message.
type AttrsView []byte
// Empty returns whether there is no attribute left in v.
func (v AttrsView) Empty() bool {
return len(v) == 0
}
// ParseFirst parses first netlink attribute at the beginning of v.
func (v AttrsView) ParseFirst() (hdr linux.NetlinkAttrHeader, value []byte, rest AttrsView, ok bool) {
b := BytesView(v)
hdrBytes, ok := b.Extract(linux.NetlinkAttrHeaderSize)
if !ok {
return
}
binary.Unmarshal(hdrBytes, hostarch.ByteOrder, &hdr)
value, ok = b.Extract(int(hdr.Length) - linux.NetlinkAttrHeaderSize)
if !ok {
return
}
_, ok = b.Extract(alignPad(int(hdr.Length), linux.NLA_ALIGNTO))
if !ok {
return
}
return hdr, value, AttrsView(b), ok
}
// BytesView supports extracting data from a byte slice with bounds checking.
type BytesView []byte
// Extract removes the first n bytes from v and returns it. If n is out of
// bounds, it returns false.
func (v *BytesView) Extract(n int) ([]byte, bool) {
if n < 0 || n > len(*v) {
return nil, false
}
extracted := (*v)[:n]
*v = (*v)[n:]
return extracted, true
}
|