-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext_sk_buff.go
119 lines (96 loc) · 2.74 KB
/
context_sk_buff.go
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
package mimic
import (
"encoding/json"
"fmt"
)
func unmarshalSKBuff(name string, ctx json.RawMessage) (Context, error) {
var result LinuxContextSKBuff
err := json.Unmarshal(ctx, &result)
if err != nil {
return nil, err
}
result.Name = name
return &result, nil
}
// LinuxContextSKBuff is a context for skb programs
type LinuxContextSKBuff struct {
Name string `json:"-"`
Packet []byte `json:"packet"`
SK *SK `json:"sock"`
Dev *NetDev `json:"dev"`
FlowKeys *FlowKeys `json:"flowKeys"`
skBuff *SKBuff
}
// GetName return the context name
func (c *LinuxContextSKBuff) GetName() string {
return c.Name
}
// SetName sets the name of the context
func (c *LinuxContextSKBuff) SetName(name string) {
c.Name = name
}
// Load load the context into the memory of the process
func (c *LinuxContextSKBuff) Load(process *Process) error {
if c.skBuff != nil {
return fmt.Errorf("context is already loaded, cleanup first before re-loading")
}
var err error
c.skBuff, err = SKBuffFromBytes(c.Packet)
if err != nil {
return fmt.Errorf("parse sk_buff: %w", err)
}
// If the user specified a custom socket, use that instread
if c.SK != nil {
c.skBuff.sk = c.SK
}
if c.Dev == nil {
c.Dev = &NetDev{}
}
c.skBuff.dev = c.Dev
if c.FlowKeys == nil {
c.FlowKeys = &FlowKeys{}
}
c.skBuff.flowKeys = c.FlowKeys
skBuffentry, err := process.VM.MemoryController.AddEntry(c.skBuff, uint32(c.skBuff.Size()), "sk_buff")
if err != nil {
return fmt.Errorf("memory controller add sk_buff: %w", err)
}
skEntry, err := process.VM.MemoryController.AddEntry(c.skBuff.sk, uint32(c.skBuff.sk.Size()), "sk")
if err != nil {
return fmt.Errorf("memory controller add sk: %w", err)
}
c.skBuff.skAddr = skEntry.Addr
flowKeysEntry, err := process.VM.MemoryController.AddEntry(
c.skBuff.flowKeys,
uint32(c.skBuff.flowKeys.Size()),
"bpf_flow_keys",
)
if err != nil {
return fmt.Errorf("memory controller add flow_keys: %w", err)
}
c.skBuff.flowKeysAddr = flowKeysEntry.Addr
pktEntry, err := process.VM.MemoryController.AddEntry(
c.skBuff.pkt,
uint32(len(c.skBuff.pkt.Backing)),
"sk_buff-pkt",
)
if err != nil {
return fmt.Errorf("memory controller add sk_buff-values: %w", err)
}
c.skBuff.head += pktEntry.Addr
c.skBuff.data += pktEntry.Addr
c.skBuff.tail += pktEntry.Addr
c.skBuff.end += pktEntry.Addr
c.skBuff.computeDataPointers()
process.Registers.R1 = uint64(skBuffentry.Addr)
return nil
}
// Cleanup removes the context from the processes memory and makes the context ready to be re-used/re-loaded.
func (c *LinuxContextSKBuff) Cleanup(process *Process) error {
err := process.VM.MemoryController.DelEntryByObj(c.skBuff)
if err != nil {
return fmt.Errorf("mem ctl, del obj: %w", err)
}
c.skBuff = nil
return nil
}