-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
routing.go
228 lines (198 loc) · 6.81 KB
/
routing.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
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
// Copyright 2020 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 linuxrouting
import (
"errors"
"fmt"
"net"
"github.com/cilium/cilium/pkg/datapath/linux/linux_defaults"
"github.com/cilium/cilium/pkg/datapath/linux/route"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/mac"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "linux-routing")
)
// Configure sets up the rules and routes needed when running in ENI or
// Azure IPAM mode.
// These rules and routes direct egress traffic out of the interface and
// ingress traffic back to the endpoint (`ip`).
//
// ip: The endpoint IP address to direct traffic out / from interface.
// info: The interface routing info used to create rules and routes.
// mtu: The interface MTU.
// masq: Whether masquerading is enabled.
func (info *RoutingInfo) Configure(ip net.IP, mtu int, masq bool) error {
if ip.To4() == nil {
log.WithFields(logrus.Fields{
"endpointIP": ip,
}).Warning("Unable to configure rules and routes because IP is not an IPv4 address")
return errors.New("IP not compatible")
}
ifindex, err := retrieveIfIndexFromMAC(info.MasterIfMAC, mtu)
if err != nil {
return fmt.Errorf("unable to find ifindex for interface MAC: %s", err)
}
ipWithMask := net.IPNet{
IP: ip,
Mask: net.CIDRMask(32, 32),
}
// Route all traffic to the interface address via the main routing table
if err := route.ReplaceRule(route.Rule{
Priority: linux_defaults.RulePriorityIngress,
To: &ipWithMask,
Table: route.MainTable,
}); err != nil {
return fmt.Errorf("unable to install ip rule: %s", err)
}
if masq {
// Lookup a VPC specific table for all traffic from an endpoint to the
// CIDR configured for the VPC on which the endpoint has the IP on.
for _, cidr := range info.IPv4CIDRs {
if err := route.ReplaceRule(route.Rule{
Priority: linux_defaults.RulePriorityEgress,
From: &ipWithMask,
To: &cidr,
Table: ifindex,
}); err != nil {
return fmt.Errorf("unable to install ip rule: %s", err)
}
}
} else {
// Lookup a VPC specific table for all traffic from an endpoint.
if err := route.ReplaceRule(route.Rule{
Priority: linux_defaults.RulePriorityEgress,
From: &ipWithMask,
Table: ifindex,
}); err != nil {
return fmt.Errorf("unable to install ip rule: %s", err)
}
}
// Nexthop route to the VPC or subnet gateway
//
// Note: This is a /32 route to avoid any L2. The endpoint does no L2
// either.
if err := netlink.RouteReplace(&netlink.Route{
LinkIndex: ifindex,
Dst: &net.IPNet{IP: info.IPv4Gateway, Mask: net.CIDRMask(32, 32)},
Scope: netlink.SCOPE_LINK,
Table: ifindex,
}); err != nil {
return fmt.Errorf("unable to add L2 nexthop route: %s", err)
}
// Default route to the VPC or subnet gateway
if err := netlink.RouteReplace(&netlink.Route{
Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)},
Table: ifindex,
Gw: info.IPv4Gateway,
}); err != nil {
return fmt.Errorf("unable to add L2 nexthop route: %s", err)
}
return nil
}
// Delete removes the ingress and egress rules that control traffic for
// endpoints. Note that the routes within these rules are not deleted as they
// can be reused when another endpoint is created on the same node. The reason
// for this is that ENI devices under-the-hood are simply network interfaces
// and all network interfaces have an ifindex. This index is then used as the
// table ID when these rules are created. The routes are created inside a table
// with this ID, and because this table ID equals the ENI ifindex, it's stable
// to rely on and therefore can be reused.
func Delete(ip net.IP) error {
if ip.To4() == nil {
log.WithFields(logrus.Fields{
"endpointIP": ip,
}).Warning("Unable to delete rules because IP is not an IPv4 address")
return errors.New("IP not compatible")
}
ipWithMask := net.IPNet{
IP: ip,
Mask: net.CIDRMask(32, 32),
}
scopedLog := log.WithFields(logrus.Fields{
"ip": ipWithMask.String(),
})
// Ingress rules
ingress := route.Rule{
Priority: linux_defaults.RulePriorityIngress,
To: &ipWithMask,
Table: route.MainTable,
}
if err := deleteRule(ingress); err != nil {
return fmt.Errorf("unable to delete ingress rule from main table with ip %s: %v", ipWithMask.String(), err)
}
scopedLog.WithField("rule", ingress).Debug("Deleted ingress rule")
// Egress rules
egress := route.Rule{
Priority: linux_defaults.RulePriorityEgress,
From: &ipWithMask,
}
if err := deleteRule(egress); err != nil {
return fmt.Errorf("unable to delete egress rule with ip %s: %v", ipWithMask.String(), err)
}
scopedLog.WithField("rule", egress).Debug("Deleted egress rule")
return nil
}
func deleteRule(r route.Rule) error {
rules, err := route.ListRules(netlink.FAMILY_V4, &r)
if err != nil {
return err
}
length := len(rules)
switch {
case length > 1:
log.WithFields(logrus.Fields{
"candidates": rules,
"rule": r,
}).Warning("Found too many rules matching, skipping deletion")
return errors.New("unexpected number of rules found to delete")
case length == 1:
return route.DeleteRule(r)
}
log.WithFields(logrus.Fields{
"rule": r,
}).Warning("No rule matching found")
return errors.New("no rule found to delete")
}
// retrieveIfIndexFromMAC finds the corresponding device index (ifindex) for a
// given MAC address. This is useful for creating rules and routes in order to
// specify the table. When the ifindex is found, the device is brought up and
// its MTU is set.
func retrieveIfIndexFromMAC(mac mac.MAC, mtu int) (index int, err error) {
var links []netlink.Link
links, err = netlink.LinkList()
if err != nil {
err = fmt.Errorf("unable to list interfaces: %s", err)
return
}
for _, link := range links {
if link.Attrs().HardwareAddr.String() == mac.String() {
index = link.Attrs().Index
if err = netlink.LinkSetMTU(link, mtu); err != nil {
err = fmt.Errorf("unable to change MTU of link %s to %d: %s", link.Attrs().Name, mtu, err)
return
}
if err = netlink.LinkSetUp(link); err != nil {
err = fmt.Errorf("unable to up link %s: %s", link.Attrs().Name, err)
return
}
return
}
}
err = fmt.Errorf("interface with MAC %s not found", mac)
return
}