/
ipv6.go
140 lines (107 loc) · 2.79 KB
/
ipv6.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
package iptablesctrl
import (
"net"
"strings"
"github.com/aporeto-inc/go-ipset/ipset"
"go.aporeto.io/trireme-lib/controller/constants"
provider "go.aporeto.io/trireme-lib/controller/pkg/aclprovider"
"go.aporeto.io/trireme-lib/controller/pkg/ipsetmanager"
"go.uber.org/zap"
)
const (
ipv6String = "v6-"
// IPv6DefaultIP is the default IP subnet of ipv6
IPv6DefaultIP = "::/0"
)
type ipv6 struct {
ipt provider.IptablesProvider
ipv6Disabled bool
}
var ipsetV6Param *ipset.Params
func init() {
ipsetV6Param = &ipset.Params{HashFamily: "inet6"}
}
// GetIPv6Impl creates the instance of ipv6 struct which implements
// the interface ipImpl
func GetIPv6Impl() (IPImpl, error) {
ipt, err := provider.NewGoIPTablesProviderV6([]string{"mangle"})
if err != nil {
zap.L().Error("Unable to initialize ipv6 iptables :%s", zap.Error(err))
}
return &ipv6{ipt: ipt, ipv6Disabled: constants.Ipv6Disabled}, nil
}
func (i *ipv6) GetIPSetPrefix() string {
return chainPrefix + ipv6String
}
func (i *ipv6) IPsetVersion() int {
return ipsetmanager.IPsetV6
}
func (i *ipv6) GetIPSetParam() *ipset.Params {
return ipsetV6Param
}
func (i *ipv6) IPFilter() func(net.IP) bool {
ipv6Filter := func(ip net.IP) bool {
return (ip.To4() == nil)
}
return ipv6Filter
}
func (i *ipv6) GetDefaultIP() string {
return IPv6DefaultIP
}
func (i *ipv6) NeedICMP() bool {
return true
}
func (i *ipv6) ProtocolAllowed(proto string) bool {
return !(strings.ToLower(proto) == "icmp")
}
func (i *ipv6) Append(table, chain string, rulespec ...string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.Append(table, chain, rulespec...)
}
func (i *ipv6) Insert(table, chain string, pos int, rulespec ...string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.Insert(table, chain, pos, rulespec...)
}
func (i *ipv6) ListChains(table string) ([]string, error) {
if i.ipv6Disabled || i.ipt == nil {
return nil, nil
}
return i.ipt.ListChains(table)
}
func (i *ipv6) ClearChain(table, chain string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.ClearChain(table, chain)
}
func (i *ipv6) DeleteChain(table, chain string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.DeleteChain(table, chain)
}
func (i *ipv6) NewChain(table, chain string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.NewChain(table, chain)
}
func (i *ipv6) Commit() error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.Commit()
}
func (i *ipv6) Delete(table, chain string, rulespec ...string) error {
if i.ipv6Disabled || i.ipt == nil {
return nil
}
return i.ipt.Delete(table, chain, rulespec...)
}
func (i *ipv6) RetrieveTable() map[string]map[string][]string {
return i.ipt.RetrieveTable()
}