pax_global_header00006660000000000000000000000064144232227030014511gustar00rootroot0000000000000052 comment=f9999b41d9cfb0586e75467db1c81cfde4f965ba golang-github-mdlayher-packet-1.1.2/000077500000000000000000000000001442322270300173115ustar00rootroot00000000000000golang-github-mdlayher-packet-1.1.2/.github/000077500000000000000000000000001442322270300206515ustar00rootroot00000000000000golang-github-mdlayher-packet-1.1.2/.github/workflows/000077500000000000000000000000001442322270300227065ustar00rootroot00000000000000golang-github-mdlayher-packet-1.1.2/.github/workflows/linux-test.yml000066400000000000000000000014711442322270300255500ustar00rootroot00000000000000name: Linux Test on: push: branches: - "*" pull_request: branches: - "*" jobs: build: strategy: fail-fast: false matrix: go-version: ["1.18", "1.19", "1.20"] runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v3 - name: Build test binary run: go test -c -race # These tests run on Linux only. Apply capabilities and verify we can # actually use real AF_PACKET sockets. - name: Apply CAP_NET_RAW run: sudo setcap cap_net_raw+ep ./packet.test - name: Run tests with capabilities run: ./packet.test -test.v golang-github-mdlayher-packet-1.1.2/.github/workflows/static-analysis.yml000066400000000000000000000015511442322270300265430ustar00rootroot00000000000000name: Static Analysis on: push: branches: - "*" pull_request: branches: - "*" jobs: build: strategy: matrix: go-version: ["1.20"] runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v3 - name: Install staticcheck run: go install honnef.co/go/tools/cmd/staticcheck@latest - name: Print staticcheck version run: staticcheck -version - name: Run staticcheck run: staticcheck ./... - name: Install enumcheck run: go install loov.dev/enumcheck@latest - name: Run enumcheck run: enumcheck ./... - name: Run go vet run: go vet ./... golang-github-mdlayher-packet-1.1.2/.github/workflows/test.yml000066400000000000000000000013021442322270300244040ustar00rootroot00000000000000name: Test on: push: branches: - "*" pull_request: branches: - "*" jobs: build: strategy: fail-fast: false matrix: go-version: ["1.20"] os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - name: Set up Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v3 # Run basic tests, we just want to make sure there is parity on Linux and # macOS, and back to the oldest version of Go this library supports. - name: Run tests run: go test ./... golang-github-mdlayher-packet-1.1.2/.github/workflows/unsupported.yml000066400000000000000000000012551442322270300260240ustar00rootroot00000000000000name: Unsupported on: push: branches: - "*" pull_request: branches: - "*" jobs: build: strategy: fail-fast: false matrix: go-version: ["1.20"] runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v3 # Although this package doesn't support Windows, we want to verify that # everything builds properly. - name: Verify build for non-UNIX platforms run: go build env: GOOS: windows golang-github-mdlayher-packet-1.1.2/.gitignore000066400000000000000000000000131442322270300212730ustar00rootroot00000000000000cmd/packet golang-github-mdlayher-packet-1.1.2/CHANGELOG.md000066400000000000000000000015411442322270300211230ustar00rootroot00000000000000# CHANGELOG # v1.1.2 - [Improvement]: updated dependencies, test with Go 1.20. # v1.1.1 - [Bug Fix]: fix test compilation on big endian machines. # v1.1.0 **This is the first release of package packet that only supports Go 1.18+. Users on older versions of Go must use v1.0.0.** - [Improvement]: drop support for older versions of Go so we can begin using modern versions of `x/sys` and other dependencies. ## v1.0.0 **This is the last release of package vsock that supports Go 1.17 and below.** - Initial stable commit! The API is mostly a direct translation of the previous `github.com/mdlayher/raw` package APIs, with some updates to make everything focused explicitly on Linux and `AF_PACKET` sockets. Functionally, the two packages are equivalent, and `*raw.Conn` is now backed by `*packet.Conn` in the latest version of the `raw` package. golang-github-mdlayher-packet-1.1.2/LICENSE.md000066400000000000000000000020561442322270300207200ustar00rootroot00000000000000# MIT License Copyright (C) 2022 Matt Layher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-mdlayher-packet-1.1.2/README.md000066400000000000000000000034571442322270300206010ustar00rootroot00000000000000# packet [![Test Status](https://github.com/mdlayher/packet/workflows/Test/badge.svg)](https://github.com/mdlayher/packet/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/mdlayher/packet.svg)](https://pkg.go.dev/github.com/mdlayher/packet) [![Go Report Card](https://goreportcard.com/badge/github.com/mdlayher/packet)](https://goreportcard.com/report/github.com/mdlayher/packet) Package `packet` provides access to Linux packet sockets (`AF_PACKET`). MIT Licensed. ## Stability See the [CHANGELOG](./CHANGELOG.md) file for a description of changes between releases. This package has a stable v1 API and any future breaking changes will prompt the release of a new major version. Features and bug fixes will continue to occur in the v1.x.x series. This package only supports the two most recent major versions of Go, mirroring Go's own release policy. Older versions of Go may lack critical features and bug fixes which are necessary for this package to function correctly. ## History One of my first major Go networking projects was [`github.com/mdlayher/raw`](https://github.com/mdlayher/raw), which provided access to Linux `AF_PACKET` sockets and *BSD equivalent mechanisms for sending and receiving Ethernet frames. However, the *BSD support languished and I lack the expertise and time to properly maintain code for operating systems I do not use on a daily basis. Package `packet` is a successor to package `raw`, but exclusively focused on Linux and `AF_PACKET` sockets. The APIs are nearly identical, but with a few changes which take into account some of the lessons learned while working on `raw`. Users are highly encouraged to migrate any existing Linux uses of `raw` to package `packet` instead. This package will be supported for the foreseeable future and will receive continued updates as necessary. golang-github-mdlayher-packet-1.1.2/doc.go000066400000000000000000000001261442322270300204040ustar00rootroot00000000000000// Package packet provides access to Linux packet sockets (AF_PACKET). package packet golang-github-mdlayher-packet-1.1.2/go.mod000066400000000000000000000004001442322270300204110ustar00rootroot00000000000000module github.com/mdlayher/packet go 1.20 require ( github.com/google/go-cmp v0.5.9 github.com/josharian/native v1.1.0 github.com/mdlayher/socket v0.4.1 golang.org/x/net v0.9.0 golang.org/x/sys v0.7.0 ) require golang.org/x/sync v0.1.0 // indirect golang-github-mdlayher-packet-1.1.2/go.sum000066400000000000000000000017061442322270300204500ustar00rootroot00000000000000github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang-github-mdlayher-packet-1.1.2/packet.go000066400000000000000000000156531442322270300211210ustar00rootroot00000000000000package packet import ( "net" "syscall" "time" "golang.org/x/net/bpf" ) const ( // network is the network reported in net.OpError. network = "packet" // Operation names which may be returned in net.OpError. opClose = "close" opGetsockopt = "getsockopt" opListen = "listen" opRawControl = "raw-control" opRawRead = "raw-read" opRawWrite = "raw-write" opRead = "read" opSet = "set" opSetsockopt = "setsockopt" opSyscallConn = "syscall-conn" opWrite = "write" ) // Config contains options for a Conn. type Config struct { // Filter is an optional assembled BPF filter which can be applied to the // Conn before bind(2) is called. // // The Conn.SetBPF method serves the same purpose once a Conn has already // been opened, but setting Filter applies the BPF filter before the Conn is // bound. This ensures that unexpected packets will not be captured before // the Conn is opened. Filter []bpf.RawInstruction } // Type is a socket type used when creating a Conn with Listen. //enumcheck:exhaustive type Type int // Possible Type values. Note that the zero value is not valid: callers must // always specify one of Raw or Datagram when calling Listen. const ( _ Type = iota Raw Datagram ) // Listen opens a packet sockets connection on the specified interface, using // the given socket type and protocol values. // // The socket type must be one of the Type constants: Raw or Datagram. // // The Config specifies optional configuration for the Conn. A nil *Config // applies the default configuration. func Listen(ifi *net.Interface, socketType Type, protocol int, cfg *Config) (*Conn, error) { l, err := listen(ifi, socketType, protocol, cfg) if err != nil { return nil, opError(opListen, err, &Addr{HardwareAddr: ifi.HardwareAddr}) } return l, nil } // TODO(mdlayher): we want to support FileConn for advanced use cases, but this // library would also need a big endian protocol value and an interface index. // For now we won't bother, but reconsider in the future. var ( _ net.PacketConn = &Conn{} _ syscall.Conn = &Conn{} _ bpf.Setter = &Conn{} ) // A Conn is an Linux packet sockets (AF_PACKET) implementation of a // net.PacketConn. type Conn struct { c *conn // Metadata about the local connection. addr *Addr ifIndex int protocol uint16 } // Close closes the connection. func (c *Conn) Close() error { return c.opError(opClose, c.c.Close()) } // LocalAddr returns the local network address. The Addr returned is shared by // all invocations of LocalAddr, so do not modify it. func (c *Conn) LocalAddr() net.Addr { return c.addr } // ReadFrom implements the net.PacketConn ReadFrom method. func (c *Conn) ReadFrom(b []byte) (int, net.Addr, error) { return c.readFrom(b) } // WriteTo implements the net.PacketConn WriteTo method. func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error) { return c.writeTo(b, addr) } // SetDeadline implements the net.PacketConn SetDeadline method. func (c *Conn) SetDeadline(t time.Time) error { return c.opError(opSet, c.c.SetDeadline(t)) } // SetReadDeadline implements the net.PacketConn SetReadDeadline method. func (c *Conn) SetReadDeadline(t time.Time) error { return c.opError(opSet, c.c.SetReadDeadline(t)) } // SetWriteDeadline implements the net.PacketConn SetWriteDeadline method. func (c *Conn) SetWriteDeadline(t time.Time) error { return c.opError(opSet, c.c.SetWriteDeadline(t)) } // SetBPF attaches an assembled BPF program to the Conn. func (c *Conn) SetBPF(filter []bpf.RawInstruction) error { return c.opError(opSetsockopt, c.c.SetBPF(filter)) } // SetPromiscuous enables or disables promiscuous mode on the Conn, allowing it // to receive traffic that is not addressed to the Conn's network interface. func (c *Conn) SetPromiscuous(enable bool) error { return c.setPromiscuous(enable) } // Stats contains statistics about a Conn reported by the Linux kernel. type Stats struct { // The total number of packets received. Packets uint32 // The number of packets dropped. Drops uint32 // The total number of times that a receive queue is frozen. May be zero if // the Linux kernel is not new enough to support TPACKET_V3 statistics. FreezeQueueCount uint32 } // Stats retrieves statistics about the Conn from the Linux kernel. // // Note that calling Stats will reset the kernel's internal counters for this // Conn. If you want to maintain cumulative statistics by polling Stats over // time, you must do so in your calling code. func (c *Conn) Stats() (*Stats, error) { return c.stats() } // SyscallConn returns a raw network connection. This implements the // syscall.Conn interface. func (c *Conn) SyscallConn() (syscall.RawConn, error) { rc, err := c.c.SyscallConn() if err != nil { return nil, c.opError(opSyscallConn, err) } return &rawConn{ rc: rc, addr: c.addr, }, nil } // opError is a convenience for the function opError that also passes the local // and remote addresses of the Conn. func (c *Conn) opError(op string, err error) error { return opError(op, err, c.addr) } // TODO(mdlayher): see if we can port smarter net.OpError logic into // socket.Conn's SyscallConn type to avoid the need for this wrapper. var _ syscall.RawConn = &rawConn{} // A rawConn is a syscall.RawConn that wraps an internal syscall.RawConn in order // to produce net.OpError error values. type rawConn struct { rc syscall.RawConn addr *Addr } // Control implements the syscall.RawConn Control method. func (rc *rawConn) Control(fn func(fd uintptr)) error { return rc.opError(opRawControl, rc.rc.Control(fn)) } // Control implements the syscall.RawConn Read method. func (rc *rawConn) Read(fn func(fd uintptr) (done bool)) error { return rc.opError(opRawRead, rc.rc.Read(fn)) } // Control implements the syscall.RawConn Write method. func (rc *rawConn) Write(fn func(fd uintptr) (done bool)) error { return rc.opError(opRawWrite, rc.rc.Write(fn)) } // opError is a convenience for the function opError that also passes the // address of the rawConn. func (rc *rawConn) opError(op string, err error) error { return opError(op, err, rc.addr) } var _ net.Addr = &Addr{} // TODO(mdlayher): expose sll_hatype and sll_pkttype on receive Addr only. // An Addr is a physical-layer address. type Addr struct { HardwareAddr net.HardwareAddr } // Network returns the address's network name, "packet". func (a *Addr) Network() string { return network } // String returns the string representation of an Addr. func (a *Addr) String() string { return a.HardwareAddr.String() } // opError unpacks err if possible, producing a net.OpError with the input // parameters in order to implement net.PacketConn. As a convenience, opError // returns nil if the input error is nil. func opError(op string, err error, local net.Addr) error { if err == nil { return nil } // TODO(mdlayher): try to comply with net.PacketConn as best as we can; land // a nettest.TestPacketConn API upstream. return &net.OpError{ Op: op, Net: network, Addr: local, Err: err, } } golang-github-mdlayher-packet-1.1.2/packet_internal_test.go000066400000000000000000000025011442322270300240400ustar00rootroot00000000000000//go:build linux // +build linux package packet import ( "encoding/binary" "fmt" "math" "testing" "github.com/google/go-cmp/cmp" "github.com/josharian/native" ) func Test_htons(t *testing.T) { tests := []struct { name string i int vLE, vBE uint16 ok bool }{ { name: "negative", i: -1, }, { name: "too large", i: math.MaxUint16 + 1, }, { name: "IPv4", i: 0x0800, vLE: 0x0008, vBE: 0x0800, ok: true, }, { name: "IPv6", i: 0x86dd, vLE: 0xdd86, vBE: 0x86dd, ok: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v, err := htons(tt.i) if tt.ok && err != nil { t.Fatalf("failed to perform htons: %v", err) } if !tt.ok && err == nil { t.Fatal("expected an error, but none occurred") } if err != nil { t.Logf("err: %v", err) return } // Depending on our GOARCH, the result may be big or little endian. var want uint16 if native.Endian == binary.ByteOrder(binary.LittleEndian) { want = tt.vLE } else { want = tt.vBE } if diff := cmp.Diff(hex(want), hex(v)); diff != "" { t.Fatalf("unexpected output for %s GOARCH (-want +got):\n%s", native.Endian.String(), diff) } }) } } func hex(v uint16) string { return fmt.Sprintf("%#04x", v) } golang-github-mdlayher-packet-1.1.2/packet_linux.go000066400000000000000000000151461442322270300223350ustar00rootroot00000000000000//go:build linux // +build linux package packet import ( "context" "encoding/binary" "errors" "math" "net" "os" "github.com/josharian/native" "github.com/mdlayher/socket" "golang.org/x/sys/unix" ) // A conn is the net.PacketConn implementation for packet sockets. We can use // socket.Conn directly on Linux to implement most of the necessary methods. type conn = socket.Conn // readFrom implements the net.PacketConn ReadFrom method using recvfrom(2). func (c *Conn) readFrom(b []byte) (int, net.Addr, error) { // From net.PacketConn documentation: // // "[ReadFrom] returns the number of bytes read (0 <= n <= len(p)) and any // error encountered. Callers should always process the n > 0 bytes returned // before considering the error err." // // c.opError will return nil if no error, but either way we return all the // information that we have. n, sa, err := c.c.Recvfrom(context.Background(), b, 0) return n, fromSockaddr(sa), c.opError(opRead, err) } // writeTo implements the net.PacketConn WriteTo method. func (c *Conn) writeTo(b []byte, addr net.Addr) (int, error) { sa, err := c.toSockaddr("sendto", addr) if err != nil { return 0, c.opError(opWrite, err) } // TODO(mdlayher): it's curious that unix.Sendto does not return the number // of bytes actually sent. Fake it for now, but investigate upstream. if err := c.c.Sendto(context.Background(), b, 0, sa); err != nil { return 0, c.opError(opWrite, err) } return len(b), nil } // setPromiscuous wraps setsockopt(2) for the unix.PACKET_MR_PROMISC option. func (c *Conn) setPromiscuous(enable bool) error { mreq := unix.PacketMreq{ Ifindex: int32(c.ifIndex), Type: unix.PACKET_MR_PROMISC, } membership := unix.PACKET_DROP_MEMBERSHIP if enable { membership = unix.PACKET_ADD_MEMBERSHIP } return c.opError( opSetsockopt, c.c.SetsockoptPacketMreq(unix.SOL_PACKET, membership, &mreq), ) } // stats wraps getsockopt(2) for tpacket_stats* types. func (c *Conn) stats() (*Stats, error) { const ( level = unix.SOL_PACKET name = unix.PACKET_STATISTICS ) // Try to fetch V3 statistics first, they contain more detailed information. if stats, err := c.c.GetsockoptTpacketStatsV3(level, name); err == nil { return &Stats{ Packets: stats.Packets, Drops: stats.Drops, FreezeQueueCount: stats.Freeze_q_cnt, }, nil } // There was an error fetching v3 stats, try to fall back. stats, err := c.c.GetsockoptTpacketStats(level, name) if err != nil { return nil, c.opError(opGetsockopt, err) } return &Stats{ Packets: stats.Packets, Drops: stats.Drops, // FreezeQueueCount is not present. }, nil } // listen is the entry point for Listen on Linux. func listen(ifi *net.Interface, socketType Type, protocol int, cfg *Config) (*Conn, error) { if cfg == nil { // Default configuration. cfg = &Config{} } // Convert Type to the matching SOCK_* constant. var typ int switch socketType { case Raw: typ = unix.SOCK_RAW case Datagram: typ = unix.SOCK_DGRAM default: return nil, errors.New("packet: invalid Type value") } // Protocol is intentionally zero in call to socket(2); we can set it on // bind(2) instead. Package raw notes: "Do not specify a protocol to avoid // capturing packets which to not match cfg.Filter." c, err := socket.Socket(unix.AF_PACKET, typ, 0, network, nil) if err != nil { return nil, err } conn, err := bind(c, ifi.Index, protocol, cfg) if err != nil { _ = c.Close() return nil, err } return conn, nil } // bind binds the *socket.Conn to finalize *Conn setup. func bind(c *socket.Conn, ifIndex, protocol int, cfg *Config) (*Conn, error) { if len(cfg.Filter) > 0 { // The caller wants to apply a BPF filter before bind(2). if err := c.SetBPF(cfg.Filter); err != nil { return nil, err } } // packet(7) says we sll_protocol must be in network byte order. pnet, err := htons(protocol) if err != nil { return nil, err } // TODO(mdlayher): investigate the possibility of sll_ifindex = 0 because we // could bind to any interface. err = c.Bind(&unix.SockaddrLinklayer{ Protocol: pnet, Ifindex: ifIndex, }) if err != nil { return nil, err } lsa, err := c.Getsockname() if err != nil { return nil, err } // Parse the physical layer address; sll_halen tells us how many bytes of // sll_addr we should treat as valid. lsall := lsa.(*unix.SockaddrLinklayer) addr := make(net.HardwareAddr, lsall.Halen) copy(addr, lsall.Addr[:]) return &Conn{ c: c, addr: &Addr{HardwareAddr: addr}, ifIndex: ifIndex, protocol: pnet, }, nil } // fromSockaddr converts an opaque unix.Sockaddr to *Addr. If sa is nil, it // returns nil. It panics if sa is not of type *unix.SockaddrLinklayer. func fromSockaddr(sa unix.Sockaddr) *Addr { if sa == nil { return nil } sall := sa.(*unix.SockaddrLinklayer) return &Addr{ // The syscall already allocated sa; just slice into it with the // appropriate length and type conversion rather than making a copy. HardwareAddr: net.HardwareAddr(sall.Addr[:sall.Halen]), } } // toSockaddr converts a net.Addr to an opaque unix.Sockaddr. It returns an // error if the fields cannot be packed into a *unix.SockaddrLinklayer. func (c *Conn) toSockaddr( op string, addr net.Addr, ) (unix.Sockaddr, error) { // The typical error convention for net.Conn types is // net.OpError(os.SyscallError(syscall.Errno)), so all calls here should // return os.SyscallError(syscall.Errno) so the caller can apply the final // net.OpError wrapper. // Ensure the correct Addr type. a, ok := addr.(*Addr) if !ok || a.HardwareAddr == nil { return nil, os.NewSyscallError(op, unix.EINVAL) } // Pack Addr and Conn metadata into the appropriate sockaddr fields. From // packet(7): // // "When you send packets it is enough to specify sll_family, sll_addr, // sll_halen, sll_ifindex, and sll_protocol. The other fields should be 0." // // sll_family is set on the conversion to unix.RawSockaddrLinklayer. sa := unix.SockaddrLinklayer{ Ifindex: c.ifIndex, Protocol: c.protocol, } // Ensure the input address does not exceed the amount of space available; // for example an IPoIB address is 20 bytes. if len(a.HardwareAddr) > len(sa.Addr) { return nil, os.NewSyscallError(op, unix.EINVAL) } sa.Halen = uint8(len(a.HardwareAddr)) copy(sa.Addr[:], a.HardwareAddr) return &sa, nil } // htons converts a short (uint16) from host-to-network byte order. func htons(i int) (uint16, error) { if i < 0 || i > math.MaxUint16 { return 0, errors.New("packet: protocol value out of range") } // Store as big endian, retrieve as native endian. var b [2]byte binary.BigEndian.PutUint16(b[:], uint16(i)) return native.Endian.Uint16(b[:]), nil } golang-github-mdlayher-packet-1.1.2/packet_linux_test.go000066400000000000000000000073241442322270300233730ustar00rootroot00000000000000//go:build go1.16 // +build go1.16 // Just because the library builds and runs on older versions of Go doesn't mean // we have to apply the same restrictions for tests. Go 1.16 is the oldest // upstream supported version of Go as of February 2022. package packet_test import ( "encoding/binary" "errors" "net" "os" "testing" "time" "github.com/mdlayher/packet" "golang.org/x/sys/unix" ) func TestConnListen(t *testing.T) { // Open a connection on an Ethernet interface and begin listening for // incoming Ethernet frames. We assume that this interface will receive some // sort of traffic in the next 30 seconds and we will capture that traffic // by looking for any EtherType value (ETH_P_ALL). c, ifi := testConn(t) t.Logf("interface: %q, MTU: %d", ifi.Name, ifi.MTU) if err := c.SetReadDeadline(time.Now().Add(30 * time.Second)); err != nil { t.Fatalf("failed to set read deadline: %v", err) } b := make([]byte, ifi.MTU) n, addr, err := c.ReadFrom(b) if err != nil { t.Fatalf("failed to read Ethernet frame: %v", err) } // Received some data, assume some Stats were populated. stats, err := c.Stats() if err != nil { t.Fatalf("failed to fetch stats: %v", err) } if stats.Packets == 0 { t.Fatal("stats indicated 0 received packets") } t.Logf(" - packets: %d, drops: %d, freeze queue count: %d", stats.Packets, stats.Drops, stats.FreezeQueueCount) // TODO(mdlayher): we could import github.com/mdlayher/ethernet, but parsing // an Ethernet frame header is fairly easy and this keeps the go.mod tidy. // Need at least destination MAC, source MAC, and EtherType. const header = 6 + 6 + 2 if n < header { t.Fatalf("did not read a complete Ethernet frame from %v, only %d bytes read", addr, n) } // Parse the header to provide tidy log output. var ( dst = net.HardwareAddr(b[0:6]) src = net.HardwareAddr(b[6:12]) et = binary.BigEndian.Uint16(b[12:14]) ) // Check for the most likely EtherType values. var ets string switch et { case 0x0800: ets = "IPv4" case 0x0806: ets = "ARP" case 0x86dd: ets = "IPv6" default: ets = "unknown" } // And finally print what we found for the user. t.Log("Ethernet frame:") t.Logf(" - destination: %s", dst) t.Logf(" - source: %s", src) t.Logf(" - ethertype: %#04x (%s)", et, ets) t.Logf(" - payload: %d bytes", n-header) } // testConn produces a *packet.Conn bound to the returned *net.Interface. The // caller does not need to call Close on the *packet.Conn. func testConn(t *testing.T) (*packet.Conn, *net.Interface) { t.Helper() // TODO(mdlayher): probably parameterize the EtherType. ifi := testInterface(t) c, err := packet.Listen(ifi, packet.Raw, unix.ETH_P_ALL, nil) if err != nil { if errors.Is(err, os.ErrPermission) { t.Skipf("skipping, permission denied (try setting CAP_NET_RAW capability): %v", err) } t.Fatalf("failed to listen: %v", err) } t.Cleanup(func() { c.Close() }) return c, ifi } // testInterface looks for a suitable Ethernet interface to bind a *packet.Conn. func testInterface(t *testing.T) *net.Interface { ifis, err := net.Interfaces() if err != nil { t.Fatalf("failed to get network interfaces: %v", err) } if len(ifis) == 0 { t.Skip("skipping, no network interfaces found") } // Try to find a suitable network interface for tests. var tried []string for _, ifi := range ifis { tried = append(tried, ifi.Name) // true is used to line up other checks. ok := true && // Look for an Ethernet interface. len(ifi.HardwareAddr) == 6 && // Look for up, multicast, broadcast. ifi.Flags&(net.FlagUp|net.FlagMulticast|net.FlagBroadcast) != 0 if ok { return &ifi } } t.Skipf("skipping, could not find a usable network interface, tried: %s", tried) panic("unreachable") } golang-github-mdlayher-packet-1.1.2/packet_others.go000066400000000000000000000023431442322270300224750ustar00rootroot00000000000000//go:build !linux // +build !linux package packet import ( "fmt" "net" "runtime" "syscall" "time" "golang.org/x/net/bpf" ) // errUnimplemented is returned by all functions on non-Linux platforms. var errUnimplemented = fmt.Errorf("packet: not implemented on %s", runtime.GOOS) func listen(_ *net.Interface, _ Type, _ int, _ *Config) (*Conn, error) { return nil, errUnimplemented } func (*Conn) readFrom(_ []byte) (int, net.Addr, error) { return 0, nil, errUnimplemented } func (*Conn) writeTo(_ []byte, _ net.Addr) (int, error) { return 0, errUnimplemented } func (*Conn) setPromiscuous(_ bool) error { return errUnimplemented } func (*Conn) stats() (*Stats, error) { return nil, errUnimplemented } type conn struct{} func (*conn) Close() error { return errUnimplemented } func (*conn) SetDeadline(_ time.Time) error { return errUnimplemented } func (*conn) SetReadDeadline(_ time.Time) error { return errUnimplemented } func (*conn) SetWriteDeadline(_ time.Time) error { return errUnimplemented } func (*conn) SetBPF(_ []bpf.RawInstruction) error { return errUnimplemented } func (*conn) SyscallConn() (syscall.RawConn, error) { return nil, errUnimplemented }