forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4.go
158 lines (133 loc) · 4.05 KB
/
ipv4.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
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
// Copyright 2016-2017 Authors of Cilium
//
// 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 ctmap
import (
"bytes"
"fmt"
"unsafe"
"github.com/cilium/cilium/common/types"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/u8proto"
)
//CtKey4 represents the key for IPv4 entries in the local BPF conntrack map.
type CtKey4 struct {
daddr types.IPv4
saddr types.IPv4
sport uint16
dport uint16
nexthdr u8proto.U8proto
flags uint8
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey4) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
//NewValue creates a new bpf.MapValue.
func (k *CtKey4) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts CtKey4 ports to network byte order.
func (k *CtKey4) ToNetwork() CtKey {
n := *k
n.sport = byteorder.HostToNetwork(n.sport).(uint16)
n.dport = byteorder.HostToNetwork(n.dport).(uint16)
return &n
}
// ToHost converts CtKey4 ports to host byte order.
func (k *CtKey4) ToHost() CtKey {
n := *k
n.sport = byteorder.NetworkToHost(n.sport).(uint16)
n.dport = byteorder.NetworkToHost(n.dport).(uint16)
return &n
}
func (k *CtKey4) String() string {
return fmt.Sprintf("%s:%d, %d, %d, %d", k.daddr, k.sport, k.dport, k.nexthdr, k.flags)
}
// Dump writes the contents of key to buffer and returns true if the value for
// next header in the key is nonzero.
func (k CtKey4) Dump(buffer *bytes.Buffer) bool {
if k.nexthdr == 0 {
return false
}
if k.flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s %d:%d ",
k.nexthdr.String(),
k.daddr.IP().String(),
k.sport, k.dport),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s %d:%d ",
k.nexthdr.String(),
k.daddr.IP().String(),
k.dport,
k.sport),
)
}
if k.flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
return true
}
//CtKey4Global represents the key for IPv4 entries in the global BPF conntrack
// map.
type CtKey4Global struct {
daddr types.IPv4
saddr types.IPv4
sport uint16
dport uint16
nexthdr u8proto.U8proto
flags uint8
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey4Global) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
//NewValue creates a new bpf.MapValue.
func (k *CtKey4Global) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts CtKey4Global ports to network byte order.
func (k *CtKey4Global) ToNetwork() CtKey {
n := *k
n.sport = byteorder.HostToNetwork(n.sport).(uint16)
n.dport = byteorder.HostToNetwork(n.dport).(uint16)
return &n
}
// ToHost converts CtKey4Global ports to host byte order.
func (k *CtKey4Global) ToHost() CtKey {
n := *k
n.sport = byteorder.NetworkToHost(n.sport).(uint16)
n.dport = byteorder.NetworkToHost(n.dport).(uint16)
return &n
}
func (k *CtKey4Global) String() string {
return fmt.Sprintf("%s:%d --> %s:%d, %d, %d", k.saddr, k.sport, k.daddr, k.dport, k.nexthdr, k.flags)
}
// Dump writes the contents of key to buffer and returns true if the value for
// next header in the key is nonzero.
func (k CtKey4Global) Dump(buffer *bytes.Buffer) bool {
if k.nexthdr == 0 {
return false
}
if k.flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s:%d -> %s:%d ",
k.nexthdr.String(),
k.saddr.IP().String(), k.sport,
k.daddr.IP().String(), k.dport),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s:%d -> %s:%d ",
k.nexthdr.String(),
k.saddr.IP().String(), k.sport,
k.daddr.IP().String(), k.dport),
)
}
if k.flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
return true
}