1// +build linux
2
3package libcontainer
4
5import (
6	"github.com/vishvananda/netlink/nl"
7	"golang.org/x/sys/unix"
8)
9
10// list of known message types we want to send to bootstrap program
11// The number is randomly chosen to not conflict with known netlink types
12const (
13	InitMsg          uint16 = 62000
14	CloneFlagsAttr   uint16 = 27281
15	NsPathsAttr      uint16 = 27282
16	UidmapAttr       uint16 = 27283
17	GidmapAttr       uint16 = 27284
18	SetgroupAttr     uint16 = 27285
19	OomScoreAdjAttr  uint16 = 27286
20	RootlessEUIDAttr uint16 = 27287
21	UidmapPathAttr   uint16 = 27288
22	GidmapPathAttr   uint16 = 27289
23)
24
25type Int32msg struct {
26	Type  uint16
27	Value uint32
28}
29
30// Serialize serializes the message.
31// Int32msg has the following representation
32// | nlattr len | nlattr type |
33// | uint32 value             |
34func (msg *Int32msg) Serialize() []byte {
35	buf := make([]byte, msg.Len())
36	native := nl.NativeEndian()
37	native.PutUint16(buf[0:2], uint16(msg.Len()))
38	native.PutUint16(buf[2:4], msg.Type)
39	native.PutUint32(buf[4:8], msg.Value)
40	return buf
41}
42
43func (msg *Int32msg) Len() int {
44	return unix.NLA_HDRLEN + 4
45}
46
47// Bytemsg has the following representation
48// | nlattr len | nlattr type |
49// | value              | pad |
50type Bytemsg struct {
51	Type  uint16
52	Value []byte
53}
54
55func (msg *Bytemsg) Serialize() []byte {
56	l := msg.Len()
57	buf := make([]byte, (l+unix.NLA_ALIGNTO-1) & ^(unix.NLA_ALIGNTO-1))
58	native := nl.NativeEndian()
59	native.PutUint16(buf[0:2], uint16(l))
60	native.PutUint16(buf[2:4], msg.Type)
61	copy(buf[4:], msg.Value)
62	return buf
63}
64
65func (msg *Bytemsg) Len() int {
66	return unix.NLA_HDRLEN + len(msg.Value) + 1 // null-terminated
67}
68
69type Boolmsg struct {
70	Type  uint16
71	Value bool
72}
73
74func (msg *Boolmsg) Serialize() []byte {
75	buf := make([]byte, msg.Len())
76	native := nl.NativeEndian()
77	native.PutUint16(buf[0:2], uint16(msg.Len()))
78	native.PutUint16(buf[2:4], msg.Type)
79	if msg.Value {
80		native.PutUint32(buf[4:8], uint32(1))
81	} else {
82		native.PutUint32(buf[4:8], uint32(0))
83	}
84	return buf
85}
86
87func (msg *Boolmsg) Len() int {
88	return unix.NLA_HDRLEN + 4 // alignment
89}
90