-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ipmasq.go
183 lines (157 loc) · 4.1 KB
/
ipmasq.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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipmasq
import (
"net"
"sync"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/ebpf"
"github.com/cilium/cilium/pkg/ip"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/types"
)
const (
MapNameIPv4 = "cilium_ipmasq_v4"
MaxEntriesIPv4 = 16384
MapNameIPv6 = "cilium_ipmasq_v6"
MaxEntriesIPv6 = 16384
)
type Key4 struct {
PrefixLen uint32
Address types.IPv4
}
func (k *Key4) String() string { return k.Address.String() }
func (k *Key4) New() bpf.MapKey { return &Key4{} }
type Key6 struct {
PrefixLen uint32
Address types.IPv6
}
func (k *Key6) String() string { return k.Address.String() }
func (k *Key6) New() bpf.MapKey { return &Key6{} }
type Value struct {
Pad uint8 // not used
}
func (v *Value) String() string { return "" }
func (v *Value) New() bpf.MapValue { return &Value{} }
var (
ipMasq4Map *bpf.Map
onceIPv4 sync.Once
ipMasq6Map *bpf.Map
onceIPv6 sync.Once
)
func IPMasq4Map() *bpf.Map {
onceIPv4.Do(func() {
ipMasq4Map = bpf.NewMap(
MapNameIPv4,
ebpf.LPMTrie,
&Key4{},
&Value{},
MaxEntriesIPv4,
bpf.BPF_F_NO_PREALLOC,
).WithCache().WithPressureMetric().
WithEvents(option.Config.GetEventBufferConfig(MapNameIPv4))
})
return ipMasq4Map
}
func IPMasq6Map() *bpf.Map {
onceIPv6.Do(func() {
ipMasq6Map = bpf.NewMap(
MapNameIPv6,
ebpf.LPMTrie,
&Key6{},
&Value{},
MaxEntriesIPv6,
bpf.BPF_F_NO_PREALLOC,
).WithCache().WithPressureMetric().
WithEvents(option.Config.GetEventBufferConfig(MapNameIPv6))
})
return ipMasq6Map
}
type IPMasqBPFMap struct{}
func (*IPMasqBPFMap) Update(cidr net.IPNet) error {
if ip.IsIPv4(cidr.IP) {
if option.Config.EnableIPv4Masquerade {
return IPMasq4Map().Update(keyIPv4(cidr), &Value{})
}
} else {
if option.Config.EnableIPv6Masquerade {
return IPMasq6Map().Update(keyIPv6(cidr), &Value{})
}
}
return nil
}
func (*IPMasqBPFMap) Delete(cidr net.IPNet) error {
if ip.IsIPv4(cidr.IP) {
if option.Config.EnableIPv4Masquerade {
return IPMasq4Map().Delete(keyIPv4(cidr))
}
} else {
if option.Config.EnableIPv6Masquerade {
return IPMasq6Map().Delete(keyIPv6(cidr))
}
}
return nil
}
// DumpForProtocols dumps the contents of the ip-masq-agent maps for IPv4
// and/or IPv6, as requested by the caller.
// Given that the package does not expose the maps directly, it's necessary to
// specify which protocol we need when ipMasq4Map/ipMasq6Map, or config
// options, have not been set, as is the case when calling from the CLI, for
// example.
func (*IPMasqBPFMap) DumpForProtocols(ipv4Needed, ipv6Needed bool) ([]net.IPNet, error) {
cidrs := []net.IPNet{}
if ipv4Needed {
if err := IPMasq4Map().DumpWithCallback(
func(keyIPv4 bpf.MapKey, _ bpf.MapValue) {
cidrs = append(cidrs, keyToIPNetIPv4(keyIPv4.(*Key4)))
}); err != nil {
return nil, err
}
}
if ipv6Needed {
if err := IPMasq6Map().DumpWithCallback(
func(keyIPv6 bpf.MapKey, _ bpf.MapValue) {
cidrs = append(cidrs, keyToIPNetIPv6(keyIPv6.(*Key6)))
}); err != nil {
return nil, err
}
}
return cidrs, nil
}
// Dump dumps the contents of the ip-masq-agent maps for IPv4 and/or IPv6, as
// required based on configuration options.
func (*IPMasqBPFMap) Dump() ([]net.IPNet, error) {
return (&IPMasqBPFMap{}).DumpForProtocols(option.Config.EnableIPv4Masquerade, option.Config.EnableIPv6Masquerade)
}
func keyIPv4(cidr net.IPNet) *Key4 {
ones, _ := cidr.Mask.Size()
key := &Key4{PrefixLen: uint32(ones)}
copy(key.Address[:], cidr.IP.To4())
return key
}
func keyToIPNetIPv4(key *Key4) net.IPNet {
var (
cidr net.IPNet
ip types.IPv4
)
cidr.Mask = net.CIDRMask(int(key.PrefixLen), 32)
key.Address.DeepCopyInto(&ip)
cidr.IP = ip.IP()
return cidr
}
func keyIPv6(cidr net.IPNet) *Key6 {
ones, _ := cidr.Mask.Size()
key := &Key6{PrefixLen: uint32(ones)}
copy(key.Address[:], cidr.IP.To16())
return key
}
func keyToIPNetIPv6(key *Key6) net.IPNet {
var (
cidr net.IPNet
ip types.IPv6
)
cidr.Mask = net.CIDRMask(int(key.PrefixLen), 128)
key.Address.DeepCopyInto(&ip)
cidr.IP = ip.IP()
return cidr
}