-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
iptables_linux.go
119 lines (105 loc) · 3.35 KB
/
iptables_linux.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
// Copyright Istio Authors
//
// 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 iptables
import (
"errors"
"fmt"
"net"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
func AddInpodMarkIPRule(cfg *Config) error {
err := forEachInpodMarkIPRule(cfg, netlink.RuleAdd)
if errors.Is(err, unix.EEXIST) {
log.Debugf("Ignoring exists error adding inpod mark ip rule: %v", err)
return nil
}
return err
}
func DelInpodMarkIPRule(cfg *Config) error {
return forEachInpodMarkIPRule(cfg, netlink.RuleDel)
}
func forEachInpodMarkIPRule(cfg *Config, f func(*netlink.Rule) error) error {
var rules []*netlink.Rule
families := []int{unix.AF_INET}
if cfg.EnableIPv6 {
families = append(families, unix.AF_INET6)
}
for _, family := range families {
// Equiv:
// ip rule add fwmark 0x111/0xfff pref 32764 lookup 100
//
// Adds in-pod rules for marking packets with the istio-specific TPROXY mark.
// A very similar mechanism is used for sidecar TPROXY.
//
// TODO largely identical/copied from tools/istio-iptables/pkg/capture/run_linux.go
inpodMarkRule := netlink.NewRule()
inpodMarkRule.Family = family
inpodMarkRule.Table = RouteTableInbound
inpodMarkRule.Mark = InpodTProxyMark
inpodMarkRule.Mask = InpodTProxyMask
inpodMarkRule.Priority = 32764
rules = append(rules, inpodMarkRule)
}
for _, rule := range rules {
log.Debugf("Iterating netlink rule : %+v", rule)
if err := f(rule); err != nil {
return fmt.Errorf("failed to configure netlink rule: %w", err)
}
}
return nil
}
func AddLoopbackRoutes(cfg *Config) error {
return forEachLoopbackRoute(cfg, netlink.RouteReplace)
}
func DelLoopbackRoutes(cfg *Config) error {
return forEachLoopbackRoute(cfg, netlink.RouteDel)
}
func forEachLoopbackRoute(cfg *Config, f func(*netlink.Route) error) error {
loopbackLink, err := netlink.LinkByName("lo")
if err != nil {
return fmt.Errorf("failed to find 'lo' link: %v", err)
}
// Set up netlink routes for localhost
cidrs := []string{"0.0.0.0/0"}
if cfg.EnableIPv6 {
cidrs = append(cidrs, "0::0/0")
}
for _, fullCIDR := range cidrs {
_, localhostDst, err := net.ParseCIDR(fullCIDR)
if err != nil {
return fmt.Errorf("parse CIDR: %v", err)
}
netlinkRoutes := []*netlink.Route{
// In routing table ${INBOUND_TPROXY_ROUTE_TABLE}, create a single default rule to route all traffic to
// the loopback interface.
// Equiv: "ip route add local 0.0.0.0/0 dev lo table 100"
{
Dst: localhostDst,
Scope: netlink.SCOPE_HOST,
Type: unix.RTN_LOCAL,
Table: RouteTableInbound,
LinkIndex: loopbackLink.Attrs().Index,
},
}
for _, route := range netlinkRoutes {
log.Debugf("Iterating netlink route : %+v", route)
if err := f(route); err != nil {
log.Errorf("Failed to add netlink route : %+v", route)
return fmt.Errorf("failed to add route: %v", err)
}
}
}
return nil
}