2022-03-15 20:57:46 +03:00
|
|
|
//go:build linux
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
package arpdb
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net"
|
2022-10-25 15:08:12 +03:00
|
|
|
"net/netip"
|
2022-03-15 20:57:46 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newARPDB() (arp *arpdbs) {
|
|
|
|
// Use the common storage among the implementations.
|
|
|
|
ns := &neighs{
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
ns: make([]Neighbor, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
var parseF parseNeighsFunc
|
|
|
|
if aghos.IsOpenWrt() {
|
|
|
|
parseF = parseArpAWrt
|
|
|
|
} else {
|
|
|
|
parseF = parseArpA
|
|
|
|
}
|
|
|
|
|
|
|
|
return newARPDBs(
|
|
|
|
// Try /proc/net/arp first.
|
2022-03-30 15:11:57 +03:00
|
|
|
&fsysARPDB{
|
|
|
|
ns: ns,
|
|
|
|
fsys: rootDirFS,
|
|
|
|
filename: "proc/net/arp",
|
|
|
|
},
|
2022-04-19 15:01:49 +03:00
|
|
|
// Then, try "arp -a -n".
|
2022-03-30 15:11:57 +03:00
|
|
|
&cmdARPDB{
|
|
|
|
parse: parseF,
|
|
|
|
ns: ns,
|
|
|
|
cmd: "arp",
|
2022-04-19 15:01:49 +03:00
|
|
|
// Use -n flag to avoid resolving the hostnames of the neighbors.
|
|
|
|
// By default ARP attempts to resolve the hostnames via DNS. See
|
|
|
|
// man 8 arp.
|
|
|
|
//
|
|
|
|
// See also https://github.com/AdguardTeam/AdGuardHome/issues/3157.
|
|
|
|
args: []string{"-a", "-n"},
|
2022-03-30 15:11:57 +03:00
|
|
|
},
|
|
|
|
// Finally, try "ip neigh".
|
|
|
|
&cmdARPDB{
|
|
|
|
parse: parseIPNeigh,
|
|
|
|
ns: ns,
|
|
|
|
cmd: "ip",
|
|
|
|
args: []string{"neigh"},
|
|
|
|
},
|
2022-03-15 20:57:46 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fsysARPDB accesses the ARP cache file to update the database.
|
|
|
|
type fsysARPDB struct {
|
|
|
|
ns *neighs
|
|
|
|
fsys fs.FS
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2023-08-24 13:42:17 +03:00
|
|
|
var _ Interface = (*fsysARPDB)(nil)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Refresh implements the [Interface] interface for *fsysARPDB.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *fsysARPDB) Refresh() (err error) {
|
|
|
|
var f fs.File
|
|
|
|
f, err = arp.fsys.Open(arp.filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening %q: %w", arp.filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
// Skip the header.
|
|
|
|
if !sc.Scan() {
|
|
|
|
return nil
|
|
|
|
} else if err = sc.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := make([]Neighbor, 0, arp.ns.len())
|
|
|
|
for sc.Scan() {
|
2023-08-24 13:42:17 +03:00
|
|
|
n := parseNeighbor(sc.Text())
|
|
|
|
if n != nil {
|
|
|
|
ns = append(ns, *n)
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arp.ns.reset(ns)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// parseNeighbor parses line into *Neighbor.
|
|
|
|
func parseNeighbor(line string) (n *Neighbor) {
|
|
|
|
fields := stringutil.SplitTrimmed(line, " ")
|
|
|
|
if len(fields) != 6 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
|
|
|
if err != nil || ip.IsUnspecified() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mac, err := net.ParseMAC(fields[3])
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Neighbor{
|
|
|
|
IP: ip,
|
|
|
|
MAC: mac,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neighbors implements the [Interface] interface for *fsysARPDB.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *fsysARPDB) Neighbors() (ns []Neighbor) {
|
|
|
|
return arp.ns.clone()
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:01:49 +03:00
|
|
|
// parseArpAWrt parses the output of the "arp -a -n" command on OpenWrt. The
|
2022-03-15 20:57:46 +03:00
|
|
|
// expected input format:
|
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// IP address HW type Flags HW address Mask Device
|
|
|
|
// 192.168.11.98 0x1 0x2 5a:92:df:a9:7e:28 * wan
|
2022-03-15 20:57:46 +03:00
|
|
|
func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|
|
|
if !sc.Scan() {
|
|
|
|
// Skip the header.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
2023-08-24 13:42:17 +03:00
|
|
|
if err != nil {
|
2022-10-25 15:08:12 +03:00
|
|
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hwStr := fields[3]
|
2022-10-25 15:08:12 +03:00
|
|
|
mac, err := net.ParseMAC(hwStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
ns = append(ns, Neighbor{
|
|
|
|
IP: ip,
|
|
|
|
MAC: mac,
|
|
|
|
})
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:01:49 +03:00
|
|
|
// parseArpA parses the output of the "arp -a -n" command on Linux. The
|
|
|
|
// expected input format:
|
2022-03-15 20:57:46 +03:00
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// hostname (192.168.1.1) at ab:cd:ef:ab:cd:ef [ether] on enp0s3
|
2022-03-15 20:57:46 +03:00
|
|
|
func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
ipStr := fields[1]
|
|
|
|
if len(ipStr) < 2 {
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
2023-08-24 13:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1])
|
|
|
|
if err != nil {
|
2022-10-25 15:08:12 +03:00
|
|
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hwStr := fields[3]
|
2023-08-24 13:42:17 +03:00
|
|
|
mac, err := net.ParseMAC(hwStr)
|
|
|
|
if err != nil {
|
2022-10-25 15:08:12 +03:00
|
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
ns = append(ns, Neighbor{
|
|
|
|
IP: ip,
|
|
|
|
MAC: mac,
|
|
|
|
Name: validatedHostname(fields[0]),
|
|
|
|
})
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIPNeigh parses the output of the "ip neigh" command on Linux. The
|
|
|
|
// expected input format:
|
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// 192.168.1.1 dev enp0s3 lladdr ab:cd:ef:ab:cd:ef REACHABLE
|
2022-03-15 20:57:46 +03:00
|
|
|
func parseIPNeigh(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) < 5 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n := Neighbor{}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
n.IP = ip
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
mac, err := net.ParseMAC(fields[4])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
n.MAC = mac
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = append(ns, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|