Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packetfilter: add support for IPV6 #3253

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/packetfilter/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ type packetFilter struct {
}

func New() (packetfilter.Driver, error) {
ipt, err := iptables.New(iptables.IPFamily(iptables.ProtocolIPv4), iptables.Timeout(5))
return newiptables(iptables.ProtocolIPv4)
}

func NewV6() (packetfilter.Driver, error) {
return newiptables(iptables.ProtocolIPv6)
}

func newiptables(proto iptables.Protocol) (packetfilter.Driver, error) {
ipt, err := iptables.New(iptables.IPFamily(proto), iptables.Timeout(5))
if err != nil {
return nil, errors.Wrap(err, "error creating IP tables")
return nil, errors.Wrapf(err, "error creating IP tables for protocol %d", proto)
}

ipSetIface := ipset.New()
Expand Down
3 changes: 3 additions & 0 deletions pkg/packetfilter/iptables/namedset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type namedSet struct {

func (p *packetFilter) NewNamedSet(set *packetfilter.SetInfo) packetfilter.NamedSet {
hashFamily := ipset.ProtocolFamilyIPV4
if set.Family == packetfilter.SetFamilyV6 {
hashFamily = ipset.ProtocolFamilyIPV6
}

return &namedSet{
ipSetIface: p.ipSetIface,
Expand Down
24 changes: 20 additions & 4 deletions pkg/packetfilter/packetfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ type ChainIPHook struct {
type SetFamily uint32

const (
// curently only IPV4 sets are supported.
// IPV4 and IPV6 sets are supported.
SetFamilyV4 SetFamily = iota
SetFamilyV6
)

// named set.
Expand Down Expand Up @@ -307,22 +308,37 @@ type Interface interface {
UpdateChainRules(table TableType, chain string, rules []*Rule) error
}

var newDriverFn func() (Driver, error)
var (
newDriverFn func() (Driver, error)
newDriverFnV6 func() (Driver, error)
)

func SetNewDriverFn(f func() (Driver, error)) {
newDriverFn = f
}

func SetNewDriverFnV6(f func() (Driver, error)) {
newDriverFnV6 = f
}

type Adapter struct {
Driver
}

func New() (Interface, error) {
if newDriverFn == nil {
return newImpl(newDriverFn)
}

func NewV6() (Interface, error) {
return newImpl(newDriverFnV6)
}

func newImpl(f func() (Driver, error)) (Interface, error) {
if f == nil {
return nil, errors.New("no driver registered")
}

driver, err := newDriverFn()
driver, err := f()
if err != nil {
return nil, errors.Wrap(err, "error creating packet filter Driver")
}
Expand Down
Loading