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"
|
2021-06-18 17:55:01 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-09-22 14:37:40 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
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.
|
|
|
|
func newManager(ipsetConf []string) (set Manager, err error) {
|
|
|
|
return newManagerWithDialer(ipsetConf, 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-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
|
|
|
|
|
|
|
|
// 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"))
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unit is a convenient alias for struct{}.
|
|
|
|
type unit = struct{}
|
|
|
|
|
|
|
|
// ipsInIpset is the type of a set of IP-address-to-ipset mappings.
|
|
|
|
type ipsInIpset map[ipInIpsetEntry]unit
|
|
|
|
|
|
|
|
// ipInIpsetEntry is the type for entries in an ipsInIpset set.
|
|
|
|
type ipInIpsetEntry struct {
|
|
|
|
ipsetName string
|
|
|
|
ipArr [net.IPv6len]byte
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2023-10-24 14:17:14 +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 ipsInIpset
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
ipv4Conn ipsetConn
|
|
|
|
ipv6Conn ipsetConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (m *manager) parseIpsetConfig(ipsetConf []string) (err error) {
|
|
|
|
// 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-10-24 14:17:14 +03:00
|
|
|
for _, p := range all {
|
|
|
|
m.nameToIpset[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
|
|
|
|
ipsets, err = m.ipsets(ipsetNames)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ipsets returns currently known ipsets.
|
2023-10-06 13:16:39 +03:00
|
|
|
func (m *manager) ipsets(names []string) (sets []props, err error) {
|
2023-10-24 14:17:14 +03:00
|
|
|
for _, n := range names {
|
|
|
|
p, ok := m.nameToIpset[n]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown ipset %q", n)
|
2021-06-18 17:55:01 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2023-10-06 13:16:39 +03:00
|
|
|
func newManagerWithDialer(ipsetConf []string, 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
|
|
|
|
|
|
|
dial: dial,
|
2023-10-24 14:17:14 +03:00
|
|
|
|
|
|
|
addedIPs: make(ipsInIpset),
|
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.
|
|
|
|
log.Info("ipset: dialing netfilter: warning: %s", err)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
return nil, fmt.Errorf("dialing netfilter: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:17:14 +03:00
|
|
|
err = m.parseIpsetConfig(ipsetConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting ipsets: %w", err)
|
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())
|
|
|
|
|
|
|
|
if _, added := m.addedIPs[e]; added {
|
|
|
|
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 {
|
|
|
|
return 0, fmt.Errorf("adding %q%s to ipset %q: %w", host, ips, set.name, err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
m.addedIPs[e] = unit{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
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:
|
|
|
|
return n, fmt.Errorf("unexpected family %s for ipset %q", set.family, set.name)
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:37:40 +03:00
|
|
|
log.Debug("ipset: added %d ips to set %s", nn, set.name)
|
|
|
|
|
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.
|
|
|
|
func (m *manager) Add(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
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:37:40 +03:00
|
|
|
log.Debug("ipset: found %d sets", len(sets))
|
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
return m.addToSets(host, ip4s, ip6s, sets)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|