-
Notifications
You must be signed in to change notification settings - Fork 51
/
ipv4.go
107 lines (81 loc) · 2.24 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
package iptablesctrl
import (
"fmt"
"net"
"strings"
"github.com/aporeto-inc/go-ipset/ipset"
provider "go.aporeto.io/trireme-lib/v11/controller/pkg/aclprovider"
"go.aporeto.io/trireme-lib/v11/controller/pkg/ipsetmanager"
)
const (
ipv4String = "v4-"
// IPv4DefaultIP is the default ip address of ipv4 subnets
IPv4DefaultIP = "0.0.0.0/0"
)
var ipsetV4Param *ipset.Params
type ipv4 struct {
ipt provider.IptablesProvider
}
func init() {
ipsetV4Param = &ipset.Params{}
}
// GetIPv4Impl creates the instance of ipv4 struct which implements the interface
// ipImpl
func GetIPv4Impl() (IPImpl, error) {
ipt, err := provider.NewGoIPTablesProviderV4([]string{"mangle"})
if err != nil {
return nil, fmt.Errorf("unable to initialize iptables provider: %s", err)
}
return &ipv4{ipt: ipt}, nil
}
func (i *ipv4) GetIPSetPrefix() string {
return chainPrefix + ipv4String
}
func (i *ipv4) IPsetVersion() int {
return ipsetmanager.IPsetV4
}
func (i *ipv4) GetIPSetParam() *ipset.Params {
return ipsetV4Param
}
func (i *ipv4) IPFilter() func(net.IP) bool {
ipv4Filter := func(ip net.IP) bool {
return (ip.To4() != nil)
}
return ipv4Filter
}
func (i *ipv4) GetDefaultIP() string {
return IPv4DefaultIP
}
func (i *ipv4) NeedICMP() bool {
return false
}
func (i *ipv4) ProtocolAllowed(proto string) bool {
return !(strings.ToLower(proto) == "icmpv6")
}
func (i *ipv4) Append(table, chain string, rulespec ...string) error {
return i.ipt.Append(table, chain, rulespec...)
}
func (i *ipv4) Insert(table, chain string, pos int, rulespec ...string) error {
return i.ipt.Insert(table, chain, pos, rulespec...)
}
func (i *ipv4) ListChains(table string) ([]string, error) {
return i.ipt.ListChains(table)
}
func (i *ipv4) ClearChain(table, chain string) error {
return i.ipt.ClearChain(table, chain)
}
func (i *ipv4) DeleteChain(table, chain string) error {
return i.ipt.DeleteChain(table, chain)
}
func (i *ipv4) NewChain(table, chain string) error {
return i.ipt.NewChain(table, chain)
}
func (i *ipv4) Commit() error {
return i.ipt.Commit()
}
func (i *ipv4) Delete(table, chain string, rulespec ...string) error {
return i.ipt.Delete(table, chain, rulespec...)
}
func (i *ipv4) RetrieveTable() map[string]map[string][]string {
return i.ipt.RetrieveTable()
}