2021-06-18 17:55:01 +03:00
|
|
|
//go:build linux
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
package ipset
|
2021-06-18 17:55:01 +03:00
|
|
|
|
|
|
|
import (
|
2023-10-24 14:17:14 +03:00
|
|
|
"bytes"
|
2024-08-26 13:30:00 +03:00
|
|
|
"context"
|
2021-06-18 17:55:01 +03:00
|
|
|
"fmt"
|
2024-08-26 13:30:00 +03:00
|
|
|
"log/slog"
|
2021-06-18 17:55:01 +03:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2024-04-03 13:44:51 +03:00
|
|
|
"github.com/AdguardTeam/golibs/container"
|
2021-06-18 17:55:01 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-08-26 13:30:00 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2021-06-18 17:55:01 +03:00
|
|
|
"github.com/digineo/go-ipset/v2"
|
|
|
|
"github.com/mdlayher/netlink"
|
|
|
|
"github.com/ti-mo/netfilter"
|
2021-12-27 20:54:00 +03:00
|
|
|
"golang.org/x/sys/unix"
|
2021-06-18 17:55:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// How to test on a real Linux machine:
|
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 1. Run "sudo ipset create example_set hash:ip family ipv4".
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 2. Run "sudo ipset list example_set". The Members field should be empty.
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 3. Add the line "example.com/example_set" to your AdGuardHome.yaml.
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 4. Start AdGuardHome.
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 5. Make requests to example.com and its subdomains.
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-21 19:21:13 +03:00
|
|
|
// 6. Run "sudo ipset list example_set". The Members field should contain the
|
|
|
|
// resolved IP addresses.
|
2021-06-18 17:55:01 +03:00
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// newManager returns a new Linux ipset manager.
|
2024-08-26 13:30:00 +03:00
|
|
|
func newManager(ctx context.Context, conf *Config) (set Manager, err error) {
|
|
|
|
return newManagerWithDialer(ctx, conf, defaultDial)
|
2021-12-27 20:54:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultDial is the default netfilter dialing function.
|
|
|
|
func defaultDial(pf netfilter.ProtoFamily, conf *netlink.Config) (conn ipsetConn, err error) {
|
2023-10-24 14:17:14 +03:00
|
|
|
c, err := ipset.Dial(pf, conf)
|
2021-12-27 20:54:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
return &queryConn{c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryConn is the [ipsetConn] implementation with listAll method, which
|
|
|
|
// returns the list of properties of all available ipsets.
|
|
|
|
type queryConn struct {
|
|
|
|
*ipset.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ ipsetConn = (*queryConn)(nil)
|
|
|
|
|
|
|
|
// listAll returns the list of properties of all available ipsets.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use https://github.com/vishvananda/netlink.
|
|
|
|
func (qc *queryConn) listAll() (sets []props, err error) {
|
|
|
|
msg, err := netfilter.MarshalNetlink(
|
|
|
|
netfilter.Header{
|
|
|
|
// The family doesn't seem to matter. See TODO on parseIpsetConfig.
|
|
|
|
Family: qc.Conn.Family,
|
|
|
|
SubsystemID: netfilter.NFSubsysIPSet,
|
|
|
|
MessageType: netfilter.MessageType(ipset.CmdList),
|
|
|
|
Flags: netlink.Request | netlink.Dump,
|
|
|
|
},
|
|
|
|
[]netfilter.Attribute{{
|
|
|
|
Type: uint16(ipset.AttrProtocol),
|
|
|
|
Data: []byte{ipset.Protocol},
|
|
|
|
}},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshaling netlink msg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume it's OK to call a method of an unexported type
|
|
|
|
// [ipset.connector], since there is no negative effects.
|
|
|
|
ms, err := qc.Conn.Conn.Query(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("querying netlink msg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range ms {
|
|
|
|
p := props{}
|
|
|
|
err = p.unmarshalMessage(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshaling netlink msg at index %d: %w", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sets = append(sets, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sets, nil
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ipsetConn is the ipset conn interface.
|
|
|
|
type ipsetConn interface {
|
|
|
|
Add(name string, entries ...*ipset.Entry) (err error)
|
|
|
|
Close() (err error)
|
2023-11-30 16:53:39 +03:00
|
|
|
Header(name string) (p *ipset.HeaderPolicy, err error)
|
2023-10-24 14:17:14 +03:00
|
|
|
listAll() (sets []props, err error)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// dialer creates an ipsetConn.
|
|
|
|
type dialer func(pf netfilter.ProtoFamily, conf *netlink.Config) (conn ipsetConn, err error)
|
2021-06-18 17:55:01 +03:00
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// props contains one Linux Netfilter ipset properties.
|
|
|
|
type props struct {
|
2023-10-24 14:17:14 +03:00
|
|
|
// name of the ipset.
|
|
|
|
name string
|
|
|
|
|
2023-11-30 16:53:39 +03:00
|
|
|
// typeName of the ipset.
|
|
|
|
typeName string
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
// family of the IP addresses in the ipset.
|
2021-06-18 17:55:01 +03:00
|
|
|
family netfilter.ProtoFamily
|
2023-10-24 14:17:14 +03:00
|
|
|
|
|
|
|
// isPersistent indicates that ipset has no timeout parameter and all
|
|
|
|
// entries are added permanently.
|
|
|
|
isPersistent bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalMessage unmarshals netlink message and sets the properties of the
|
|
|
|
// ipset.
|
|
|
|
func (p *props) unmarshalMessage(msg netlink.Message) (err error) {
|
|
|
|
_, attrs, err := netfilter.UnmarshalNetlink(msg)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default ipset has no timeout parameter.
|
|
|
|
p.isPersistent = true
|
|
|
|
|
|
|
|
for _, a := range attrs {
|
|
|
|
p.parseAttribute(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAttribute parses netfilter attribute and sets the name and family of
|
|
|
|
// the ipset.
|
|
|
|
func (p *props) parseAttribute(a netfilter.Attribute) {
|
|
|
|
switch ipset.AttributeType(a.Type) {
|
|
|
|
case ipset.AttrData:
|
|
|
|
p.parseAttrData(a)
|
|
|
|
case ipset.AttrSetName:
|
|
|
|
// Trim the null character.
|
|
|
|
p.name = string(bytes.Trim(a.Data, "\x00"))
|
2023-11-30 16:53:39 +03:00
|
|
|
case ipset.AttrTypeName:
|
|
|
|
p.typeName = string(bytes.Trim(a.Data, "\x00"))
|
2023-10-24 14:17:14 +03:00
|
|
|
case ipset.AttrFamily:
|
|
|
|
p.family = netfilter.ProtoFamily(a.Data[0])
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAttrData parses attribute data and sets the timeout of the ipset.
|
|
|
|
func (p *props) parseAttrData(a netfilter.Attribute) {
|
|
|
|
for _, a := range a.Children {
|
|
|
|
switch ipset.AttributeType(a.Type) {
|
|
|
|
case ipset.AttrTimeout:
|
|
|
|
timeout := a.Uint32()
|
|
|
|
p.isPersistent = timeout == 0
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// manager is the Linux Netfilter ipset manager.
|
|
|
|
type manager struct {
|
|
|
|
nameToIpset map[string]props
|
|
|
|
domainToIpsets map[string][]props
|
2021-06-18 17:55:01 +03:00
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
logger *slog.Logger
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
dial dialer
|
2021-06-18 17:55:01 +03:00
|
|
|
|
|
|
|
// mu protects all properties below.
|
|
|
|
mu *sync.Mutex
|
|
|
|
|
2024-04-03 13:44:51 +03:00
|
|
|
// TODO(a.garipov): Currently, the ipset list is static, and we don't read
|
|
|
|
// the IPs already in sets, so we can assume that all incoming IPs are
|
|
|
|
// either added to all corresponding ipsets or not. When that stops being
|
|
|
|
// the case, for example if we add dynamic reconfiguration of ipsets, this
|
|
|
|
// map will need to become a per-ipset-name one.
|
|
|
|
addedIPs *container.MapSet[ipInIpsetEntry]
|
2023-10-24 14:17:14 +03:00
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
ipv4Conn ipsetConn
|
|
|
|
ipv6Conn ipsetConn
|
|
|
|
}
|
|
|
|
|
2024-04-03 13:44:51 +03:00
|
|
|
// ipInIpsetEntry is the type for entries in [manager.addIPs].
|
|
|
|
type ipInIpsetEntry struct {
|
|
|
|
ipsetName string
|
|
|
|
// TODO(schzen): Use netip.Addr.
|
|
|
|
ipArr [net.IPv6len]byte
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
// dialNetfilter establishes connections to Linux's netfilter module.
|
2023-10-06 13:16:39 +03:00
|
|
|
func (m *manager) dialNetfilter(conf *netlink.Config) (err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
// The kernel API does not actually require two sockets but package
|
|
|
|
// github.com/digineo/go-ipset does.
|
|
|
|
//
|
2021-12-27 20:54:00 +03:00
|
|
|
// TODO(a.garipov): Perhaps we can ditch package ipset altogether and just
|
|
|
|
// use packages netfilter and netlink.
|
2021-06-18 17:55:01 +03:00
|
|
|
m.ipv4Conn, err = m.dial(netfilter.ProtoIPv4, conf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing v4: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.ipv6Conn, err = m.dial(netfilter.ProtoIPv6, conf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing v6: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
// parseIpsetConfigLine parses one ipset configuration line.
|
|
|
|
func parseIpsetConfigLine(confStr string) (hosts, ipsetNames []string, err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
confStr = strings.TrimSpace(confStr)
|
|
|
|
hostsAndNames := strings.Split(confStr, "/")
|
|
|
|
if len(hostsAndNames) != 2 {
|
|
|
|
return nil, nil, fmt.Errorf("invalid value %q: expected one slash", confStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts = strings.Split(hostsAndNames[0], ",")
|
|
|
|
ipsetNames = strings.Split(hostsAndNames[1], ",")
|
|
|
|
|
|
|
|
if len(ipsetNames) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ipsetNames {
|
|
|
|
ipsetNames[i] = strings.TrimSpace(ipsetNames[i])
|
|
|
|
if len(ipsetNames[i]) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("invalid value %q: empty ipset name", confStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range hosts {
|
|
|
|
hosts[i] = strings.ToLower(strings.TrimSpace(hosts[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hosts, ipsetNames, nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
// parseIpsetConfig parses the ipset configuration and stores ipsets. It
|
|
|
|
// returns an error if the configuration can't be used.
|
2024-08-26 13:30:00 +03:00
|
|
|
func (m *manager) parseIpsetConfig(ctx context.Context, ipsetConf []string) (err error) {
|
2023-10-24 14:17:14 +03:00
|
|
|
// The family doesn't seem to matter when we use a header query, so query
|
|
|
|
// only the IPv4 one.
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Find out if this is a bug or a feature.
|
2023-10-24 14:17:14 +03:00
|
|
|
all, err := m.ipv4Conn.listAll()
|
2021-06-18 17:55:01 +03:00
|
|
|
if err != nil {
|
2023-10-24 14:17:14 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
currentlyKnown := map[string]props{}
|
2023-10-24 14:17:14 +03:00
|
|
|
for _, p := range all {
|
2023-12-06 19:28:56 +03:00
|
|
|
currentlyKnown[p.name] = p
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
for i, confStr := range ipsetConf {
|
|
|
|
var hosts, ipsetNames []string
|
|
|
|
hosts, ipsetNames, err = parseIpsetConfigLine(confStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("config line at idx %d: %w", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipsets []props
|
2024-08-26 13:30:00 +03:00
|
|
|
ipsets, err = m.ipsets(ctx, ipsetNames, currentlyKnown)
|
2023-10-24 14:17:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting ipsets from config line at idx %d: %w", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, host := range hosts {
|
|
|
|
m.domainToIpsets[host] = append(m.domainToIpsets[host], ipsets...)
|
|
|
|
}
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
return nil
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:53:39 +03:00
|
|
|
// ipsetProps returns the properties of an ipset with the given name.
|
|
|
|
//
|
|
|
|
// Additional header data query. See https://github.com/AdguardTeam/AdGuardHome/issues/6420.
|
2023-12-06 19:28:56 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use *props.
|
|
|
|
func (m *manager) ipsetProps(name string) (p props, err error) {
|
2023-11-30 16:53:39 +03:00
|
|
|
// The family doesn't seem to matter when we use a header query, so
|
|
|
|
// query only the IPv4 one.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Find out if this is a bug or a feature.
|
|
|
|
var res *ipset.HeaderPolicy
|
2023-12-06 19:28:56 +03:00
|
|
|
res, err = m.ipv4Conn.Header(name)
|
2023-11-30 16:53:39 +03:00
|
|
|
if err != nil {
|
2023-12-06 19:28:56 +03:00
|
|
|
return props{}, err
|
2023-11-30 16:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if res == nil || res.Family == nil {
|
2023-12-06 19:28:56 +03:00
|
|
|
return props{}, errors.Error("empty response or no family data")
|
2023-11-30 16:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
family := netfilter.ProtoFamily(res.Family.Value)
|
|
|
|
if family != netfilter.ProtoIPv4 && family != netfilter.ProtoIPv6 {
|
2023-12-06 19:28:56 +03:00
|
|
|
return props{}, fmt.Errorf("unexpected ipset family %q", family)
|
2023-11-30 16:53:39 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
typeName := res.TypeName.Get()
|
2023-11-30 16:53:39 +03:00
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
return props{
|
|
|
|
name: name,
|
|
|
|
typeName: typeName,
|
|
|
|
family: family,
|
|
|
|
isPersistent: false,
|
|
|
|
}, nil
|
2023-11-30 16:53:39 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
// ipsets returns ipset properties of currently known ipsets. It also makes an
|
|
|
|
// additional ipset header data query if needed.
|
2024-08-26 13:30:00 +03:00
|
|
|
func (m *manager) ipsets(
|
|
|
|
ctx context.Context,
|
|
|
|
names []string,
|
|
|
|
currentlyKnown map[string]props,
|
|
|
|
) (sets []props, err error) {
|
2023-10-24 14:17:14 +03:00
|
|
|
for _, n := range names {
|
2023-12-06 19:28:56 +03:00
|
|
|
p, ok := currentlyKnown[n]
|
2023-10-24 14:17:14 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown ipset %q", n)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 16:22:04 +03:00
|
|
|
if p.family != netfilter.ProtoIPv4 && p.family != netfilter.ProtoIPv6 {
|
2024-08-26 13:30:00 +03:00
|
|
|
m.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"got unexpected ipset family while getting set properties",
|
|
|
|
"set_name", p.name,
|
|
|
|
"set_type", p.typeName,
|
|
|
|
"set_family", p.family,
|
2023-11-30 16:53:39 +03:00
|
|
|
)
|
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
p, err = m.ipsetProps(n)
|
2023-11-30 16:53:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%q %q making header query: %w", p.name, p.typeName, err)
|
|
|
|
}
|
2023-11-21 16:22:04 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:28:56 +03:00
|
|
|
m.nameToIpset[n] = p
|
2023-10-24 14:17:14 +03:00
|
|
|
sets = append(sets, p)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sets, nil
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// newManagerWithDialer returns a new Linux ipset manager using the provided
|
2021-06-18 17:55:01 +03:00
|
|
|
// dialer.
|
2024-08-26 13:30:00 +03:00
|
|
|
func newManagerWithDialer(ctx context.Context, conf *Config, dial dialer) (mgr Manager, err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "ipset: %w") }()
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
m := &manager{
|
2021-06-18 17:55:01 +03:00
|
|
|
mu: &sync.Mutex{},
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
nameToIpset: make(map[string]props),
|
|
|
|
domainToIpsets: make(map[string][]props),
|
2021-06-18 17:55:01 +03:00
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
logger: conf.Logger,
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
dial: dial,
|
2023-10-24 14:17:14 +03:00
|
|
|
|
2024-04-03 13:44:51 +03:00
|
|
|
addedIPs: container.NewMapSet[ipInIpsetEntry](),
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = m.dialNetfilter(&netlink.Config{})
|
|
|
|
if err != nil {
|
2021-12-27 20:54:00 +03:00
|
|
|
if errors.Is(err, unix.EPROTONOSUPPORT) {
|
|
|
|
// The implementation doesn't support this protocol version. Just
|
|
|
|
// issue a warning.
|
2024-08-26 13:30:00 +03:00
|
|
|
m.logger.WarnContext(ctx, "dialing netfilter", slogutil.KeyError, err)
|
2021-12-27 20:54:00 +03:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
return nil, fmt.Errorf("dialing netfilter: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
err = m.parseIpsetConfig(ctx, conf.Lines)
|
2023-10-24 14:17:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting ipsets: %w", err)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
m.logger.DebugContext(ctx, "initialized")
|
2023-11-30 16:53:39 +03:00
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupHost find the ipsets for the host, taking subdomain wildcards into
|
|
|
|
// account.
|
2023-10-06 13:16:39 +03:00
|
|
|
func (m *manager) lookupHost(host string) (sets []props) {
|
2021-06-18 17:55:01 +03:00
|
|
|
// Search for matching ipset hosts starting with most specific domain.
|
|
|
|
// We could use a trie here but the simple, inefficient solution isn't
|
|
|
|
// that expensive: ~10 ns for TLD + SLD vs. ~140 ns for 10 subdomains on
|
|
|
|
// an AMD Ryzen 7 PRO 4750U CPU; ~120 ns vs. ~ 1500 ns on a Raspberry
|
|
|
|
// Pi's ARMv7 rev 4 CPU.
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
host = host[i:]
|
|
|
|
sets = m.domainToIpsets[host]
|
|
|
|
if sets != nil {
|
|
|
|
return sets
|
|
|
|
}
|
|
|
|
|
|
|
|
i = strings.Index(host, ".")
|
|
|
|
if i == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the root catch-all one.
|
|
|
|
return m.domainToIpsets[""]
|
|
|
|
}
|
|
|
|
|
|
|
|
// addIPs adds the IP addresses for the host to the ipset. set must be same
|
|
|
|
// family as set's family.
|
2023-10-06 13:16:39 +03:00
|
|
|
func (m *manager) addIPs(host string, set props, ips []net.IP) (n int, err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
if len(ips) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries []*ipset.Entry
|
2023-10-24 14:17:14 +03:00
|
|
|
var newAddedEntries []ipInIpsetEntry
|
2021-06-18 17:55:01 +03:00
|
|
|
for _, ip := range ips {
|
2023-10-24 14:17:14 +03:00
|
|
|
e := ipInIpsetEntry{
|
|
|
|
ipsetName: set.name,
|
|
|
|
}
|
|
|
|
copy(e.ipArr[:], ip.To16())
|
|
|
|
|
2024-04-03 13:44:51 +03:00
|
|
|
if m.addedIPs.Has(e) {
|
2023-10-24 14:17:14 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
entries = append(entries, ipset.NewEntry(ipset.EntryIP(ip)))
|
2023-10-24 14:17:14 +03:00
|
|
|
newAddedEntries = append(newAddedEntries, e)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
n = len(entries)
|
|
|
|
if n == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn ipsetConn
|
|
|
|
switch set.family {
|
|
|
|
case netfilter.ProtoIPv4:
|
|
|
|
conn = m.ipv4Conn
|
|
|
|
case netfilter.ProtoIPv6:
|
|
|
|
conn = m.ipv6Conn
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unexpected family %s for ipset %q", set.family, set.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.Add(set.name, entries...)
|
|
|
|
if err != nil {
|
2023-11-30 16:53:39 +03:00
|
|
|
return 0, fmt.Errorf("adding %q%s to %q %q: %w", host, ips, set.name, set.typeName, err)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
// Only add these to the cache once we're sure that all of them were
|
|
|
|
// actually sent to the ipset.
|
|
|
|
for _, e := range newAddedEntries {
|
|
|
|
s := m.nameToIpset[e.ipsetName]
|
|
|
|
if s.isPersistent {
|
2024-04-03 13:44:51 +03:00
|
|
|
m.addedIPs.Add(e)
|
2023-10-24 14:17:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addToSets adds the IP addresses to the corresponding ipset.
|
2023-10-06 13:16:39 +03:00
|
|
|
func (m *manager) addToSets(
|
2024-08-26 13:30:00 +03:00
|
|
|
ctx context.Context,
|
2021-06-18 17:55:01 +03:00
|
|
|
host string,
|
|
|
|
ip4s []net.IP,
|
|
|
|
ip6s []net.IP,
|
2023-10-06 13:16:39 +03:00
|
|
|
sets []props,
|
2021-06-18 17:55:01 +03:00
|
|
|
) (n int, err error) {
|
|
|
|
for _, set := range sets {
|
|
|
|
var nn int
|
|
|
|
switch set.family {
|
|
|
|
case netfilter.ProtoIPv4:
|
|
|
|
nn, err = m.addIPs(host, set, ip4s)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
case netfilter.ProtoIPv6:
|
|
|
|
nn, err = m.addIPs(host, set, ip6s)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
default:
|
2023-11-30 16:53:39 +03:00
|
|
|
return n, fmt.Errorf("%q %q unexpected family %q", set.name, set.typeName, set.family)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
m.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"added ips to set",
|
|
|
|
"ips_num", nn,
|
|
|
|
"set_name", set.name,
|
|
|
|
"set_type", set.typeName,
|
|
|
|
)
|
2021-09-22 14:37:40 +03:00
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
n += nn
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// Add implements the [Manager] interface for *manager.
|
2024-08-26 13:30:00 +03:00
|
|
|
func (m *manager) Add(ctx context.Context, host string, ip4s, ip6s []net.IP) (n int, err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
sets := m.lookupHost(host)
|
|
|
|
if len(sets) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
m.logger.DebugContext(ctx, "found sets", "set_num", len(sets))
|
2021-09-22 14:37:40 +03:00
|
|
|
|
2024-08-26 13:30:00 +03:00
|
|
|
return m.addToSets(ctx, host, ip4s, ip6s, sets)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
2023-10-06 13:16:39 +03:00
|
|
|
// Close implements the [Manager] interface for *manager.
|
|
|
|
func (m *manager) Close() (err error) {
|
2021-06-18 17:55:01 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
// Close both and collect errors so that the errors from closing one
|
|
|
|
// don't interfere with closing the other.
|
|
|
|
err = m.ipv4Conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.ipv6Conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:09:08 +03:00
|
|
|
return errors.Annotate(errors.Join(errs...), "closing ipsets: %w")
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|