ndp

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 20 Imported by: 28

README

ndp Test Status Go Reference Go Report Card

Package ndp implements the Neighbor Discovery Protocol, as described in RFC 4861. MIT Licensed.

The command ndp is a utility for working with the Neighbor Discovery Protocol.

To learn more about NDP, and how to use this package, check out my blog: Network Protocol Breakdown: NDP and Go.

Examples

Listen for incoming NDP messages on interface eth0 to one of the interface's global unicast addresses.

$ sudo ndp -i eth0 -a global listen
$ sudo ndp -i eth0 -a 2001:db8::1 listen

Send router solicitations on interface eth0 from the interface's link-local address until a router advertisement is received.

$ sudo ndp -i eth0 -a linklocal rs

Send neighbor solicitations on interface eth0 to a neighbor's link-local address until a neighbor advertisement is received.

$ sudo ndp -i eth0 -a linklocal -t fe80::1 ns

An example of the tool sending a router solicitation and receiving a router advertisement on the WAN interface of a Ubiquiti router:

$ sudo ndp -i eth1 -a linklocal rs
ndp> interface: eth1, link-layer address: 04:18:d6:a1:ce:b8, IPv6 address: fe80::618:d6ff:fea1:ceb8
ndp rs> router solicitation:
    - source link-layer address: 04:18:d6:a1:ce:b8

ndp rs> router advertisement from: fe80::201:5cff:fe69:f246:
    - hop limit:        0
    - flags:            [MO]
    - preference:       0
    - router lifetime:  2h30m0s
    - reachable time:   1h0m0s
    - retransmit timer: 0s
    - options:
        - prefix information: 2600:6c4a:7002:100::/64, flags: [], valid: 720h0m0s, preferred: 168h0m0s

Documentation

Overview

Package ndp implements the Neighbor Discovery Protocol, as described in RFC 4861.

Index

Constants

View Source
const HopLimit = 255

HopLimit is the expected IPv6 hop limit for all NDP messages.

View Source
const Infinity = time.Duration(0xffffffff) * time.Second

Infinity indicates that a prefix is valid for an infinite amount of time, unless a new, finite, value is received in a subsequent router advertisement.

View Source
const Unrestricted = "urn:ietf:params:capport:unrestricted"

Unrestricted is the IANA-assigned URI for a network with no captive portal restrictions, as specified in RFC 8910, Section 2.

Variables

This section is empty.

Functions

func MarshalMessage

func MarshalMessage(m Message) ([]byte, error)

MarshalMessage marshals a Message into its binary form and prepends an ICMPv6 message with the correct type.

It is assumed that the operating system or caller will calculate and place the ICMPv6 checksum in the result.

func MarshalMessageChecksum

func MarshalMessageChecksum(m Message, source, destination netip.Addr) ([]byte, error)

MarshalMessageChecksum marshals a Message into its binary form and prepends an ICMPv6 message with the correct type.

The source and destination IP addresses are used to compute an IPv6 pseudo header for checksum calculation.

func SolicitedNodeMulticast

func SolicitedNodeMulticast(ip netip.Addr) (netip.Addr, error)

SolicitedNodeMulticast returns the solicited-node multicast address for an IPv6 address.

Types

type Addr

type Addr string

An Addr is an IPv6 unicast address.

const (
	Unspecified Addr = "unspecified"
	LinkLocal   Addr = "linklocal"
	UniqueLocal Addr = "uniquelocal"
	Global      Addr = "global"
)

Possible Addr types for an IPv6 unicast address.

type CaptivePortal

type CaptivePortal struct {
	URI string
}

A CaptivePortal is a Captive-Portal option, as described in RFC 8910, Section 2.3.

func NewCaptivePortal

func NewCaptivePortal(uri string) (*CaptivePortal, error)

NewCaptivePortal produces a CaptivePortal Option for the input URI string. As a special case, if uri is empty, Unrestricted is used as the CaptivePortal OptionURI.

If uri is an IP address literal, an error is returned. Per RFC 8910, uri "SHOULD NOT" be an IP address, but there are circumstances where this behavior may be useful. In that case, the caller can bypass NewCaptivePortal and construct a CaptivePortal Option directly.

func (*CaptivePortal) Code

func (*CaptivePortal) Code() byte

Code implements Option.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

A Conn is a Neighbor Discovery Protocol connection.

func Listen

func Listen(ifi *net.Interface, addr Addr) (*Conn, netip.Addr, error)

Listen creates a NDP connection using the specified interface and address type.

As a special case, literal IPv6 addresses may be specified to bind to a specific address for an interface. If the IPv6 address does not exist on the interface, an error will be returned.

Listen returns a Conn and the chosen IPv6 address of the interface.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the Conn's underlying connection.

func (*Conn) JoinGroup

func (c *Conn) JoinGroup(group netip.Addr) error

JoinGroup joins the specified multicast group. If group contains an IPv6 zone, it is overwritten by the zone of the network interface which backs Conn.

func (*Conn) LeaveGroup

func (c *Conn) LeaveGroup(group netip.Addr) error

LeaveGroup leaves the specified multicast group. If group contains an IPv6 zone, it is overwritten by the zone of the network interface which backs Conn.

func (*Conn) ReadFrom

func (c *Conn) ReadFrom() (Message, *ipv6.ControlMessage, netip.Addr, error)

ReadFrom reads a Message from the Conn and returns its control message and source network address. Messages sourced from this machine and malformed or unrecognized ICMPv6 messages are filtered.

If more control and/or a more efficient low-level API are required, see ReadRaw.

func (*Conn) ReadRaw

func (c *Conn) ReadRaw(b []byte) (int, *ipv6.ControlMessage, netip.Addr, error)

ReadRaw reads ICMPv6 message bytes into b from the Conn and returns the number of bytes read, the control message, and the source network address.

Most callers should use ReadFrom instead, which parses bytes into Messages and also handles malformed and unrecognized ICMPv6 messages.

func (*Conn) SetControlMessage

func (c *Conn) SetControlMessage(cf ipv6.ControlFlags, on bool) error

SetControlMessage enables the reception of *ipv6.ControlMessages based on the specified flags.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines for Conn. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

func (*Conn) SetICMPFilter

func (c *Conn) SetICMPFilter(f *ipv6.ICMPFilter) error

SetICMPFilter applies the specified ICMP filter. This option can be used to ensure a Conn only accepts certain kinds of NDP messages.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets a deadline for the next NDP message to arrive.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets a deadline for the next NDP message to be written.

func (*Conn) WriteTo

func (c *Conn) WriteTo(m Message, cm *ipv6.ControlMessage, dst netip.Addr) error

WriteTo writes a Message to the Conn, with an optional control message and destination network address. If dst contains an IPv6 zone, it is overwritten by the zone of the network interface which backs Conn.

If cm is nil, a default control message will be sent.

type DNSSearchList

type DNSSearchList struct {
	Lifetime    time.Duration
	DomainNames []string
}

A DNSSearchList is a DNS search list option, as described in RFC 8106, Section 5.2.

func (*DNSSearchList) Code

func (*DNSSearchList) Code() byte

Code implements Option.

type Direction

type Direction int

A Direction specifies the direction of a LinkLayerAddress Option as a source or target.

const (
	Source Direction = optSourceLLA
	Target Direction = optTargetLLA
)

Possible Direction values.

type LinkLayerAddress

type LinkLayerAddress struct {
	Direction Direction
	Addr      net.HardwareAddr
}

A LinkLayerAddress is a Source or Target Link-Layer Address option, as described in RFC 4861, Section 4.6.1.

func (*LinkLayerAddress) Code

func (lla *LinkLayerAddress) Code() byte

Code implements Option.

type MTU

type MTU struct {
	MTU uint32
}

An MTU is an MTU option, as described in RFC 4861, Section 4.6.1.

func NewMTU

func NewMTU(mtu uint32) *MTU

NewMTU creates an MTU Option from an MTU value.

func (*MTU) Code

func (*MTU) Code() byte

Code implements Option.

type Message

type Message interface {
	// Type specifies the ICMPv6 type for a Message.
	Type() ipv6.ICMPType
	// contains filtered or unexported methods
}

A Message is a Neighbor Discovery Protocol message.

func ParseMessage

func ParseMessage(b []byte) (Message, error)

ParseMessage parses a Message from its binary form after determining its type from a leading ICMPv6 message.

type NeighborAdvertisement

type NeighborAdvertisement struct {
	Router        bool
	Solicited     bool
	Override      bool
	TargetAddress netip.Addr
	Options       []Option
}

A NeighborAdvertisement is a Neighbor Advertisement message as described in RFC 4861, Section 4.4.

func (*NeighborAdvertisement) Type

func (na *NeighborAdvertisement) Type() ipv6.ICMPType

Type implements Message.

type NeighborSolicitation

type NeighborSolicitation struct {
	TargetAddress netip.Addr
	Options       []Option
}

A NeighborSolicitation is a Neighbor Solicitation message as described in RFC 4861, Section 4.3.

func (*NeighborSolicitation) Type

func (ns *NeighborSolicitation) Type() ipv6.ICMPType

Type implements Message.

type Nonce added in v0.10.0

type Nonce struct {
	// contains filtered or unexported fields
}

A Nonce is a Nonce option, as described in RFC 3971, Section 5.3.2.

func NewNonce added in v0.10.0

func NewNonce() *Nonce

NewNonce creates a Nonce option with an opaque random value.

func (*Nonce) Code added in v0.10.0

func (*Nonce) Code() byte

Code implements Option.

func (*Nonce) Equal added in v0.10.0

func (n *Nonce) Equal(x *Nonce) bool

Equal reports whether n and x are the same nonce.

func (*Nonce) String added in v0.10.0

func (n *Nonce) String() string

String returns the string representation of a Nonce.

type Option

type Option interface {
	// Code specifies the NDP option code for an Option.
	Code() uint8
	// contains filtered or unexported methods
}

An Option is a Neighbor Discovery Protocol option.

type PREF64 added in v1.1.0

type PREF64 struct {
	Lifetime time.Duration
	Prefix   netip.Prefix
}

PREF64 is a PREF64 option, as described in RFC 8781, Section 4. The prefix must have a prefix length of 96, 64, 56, 40, or 32. The lifetime is used to indicate to clients how long the PREF64 prefix is valid for. A lifetime of 0 indicates the prefix is no longer valid. If unsure, refer to RFC 8781 Section 4.1 for how to calculate an appropriate lifetime.

func (*PREF64) Code added in v1.1.0

func (p *PREF64) Code() byte

type Preference

type Preference int

A Preference is a NDP router selection or route preference value as described in RFC 4191, Section 2.1.

const (
	Medium Preference = 0
	High   Preference = 1

	Low Preference = 3
)

Possible Preference values.

func (Preference) String

func (i Preference) String() string

type PrefixInformation

type PrefixInformation struct {
	PrefixLength                   uint8
	OnLink                         bool
	AutonomousAddressConfiguration bool
	ValidLifetime                  time.Duration
	PreferredLifetime              time.Duration
	Prefix                         netip.Addr
}

A PrefixInformation is a a Prefix Information option, as described in RFC 4861, Section 4.6.1.

func (*PrefixInformation) Code

func (*PrefixInformation) Code() byte

Code implements Option.

type RAFlags added in v1.1.0

type RAFlags []byte

RAFlags is a bitmask of Router Advertisement flags contained within an RAFlagsExtension.

type RAFlagsExtension added in v1.1.0

type RAFlagsExtension struct {
	Flags RAFlags
}

A RAFlagsExtension is a Router Advertisement Flags Extension (or Expansion) option, as described in RFC 5175, Section 4.

func (*RAFlagsExtension) Code added in v1.1.0

func (*RAFlagsExtension) Code() byte

Code implements Option.

type RawOption

type RawOption struct {
	Type   uint8
	Length uint8
	Value  []byte
}

A RawOption is an Option in its raw and unprocessed format. Options which are not recognized by this package can be represented using a RawOption.

func (*RawOption) Code

func (r *RawOption) Code() byte

Code implements Option.

type RecursiveDNSServer

type RecursiveDNSServer struct {
	Lifetime time.Duration
	Servers  []netip.Addr
}

A RecursiveDNSServer is a Recursive DNS Server option, as described in RFC 8106, Section 5.1.

func (*RecursiveDNSServer) Code

func (*RecursiveDNSServer) Code() byte

Code implements Option.

type RouteInformation

type RouteInformation struct {
	PrefixLength  uint8
	Preference    Preference
	RouteLifetime time.Duration
	Prefix        netip.Addr
}

A RouteInformation is a Route Information option, as described in RFC 4191, Section 2.3.

func (*RouteInformation) Code

func (*RouteInformation) Code() byte

Code implements Option.

type RouterAdvertisement

type RouterAdvertisement struct {
	CurrentHopLimit           uint8
	ManagedConfiguration      bool
	OtherConfiguration        bool
	MobileIPv6HomeAgent       bool
	RouterSelectionPreference Preference
	NeighborDiscoveryProxy    bool
	RouterLifetime            time.Duration
	ReachableTime             time.Duration
	RetransmitTimer           time.Duration
	Options                   []Option
}

A RouterAdvertisement is a Router Advertisement message as described in RFC 4861, Section 4.1.

func (*RouterAdvertisement) Type

func (ra *RouterAdvertisement) Type() ipv6.ICMPType

Type implements Message.

type RouterSolicitation

type RouterSolicitation struct {
	Options []Option
}

A RouterSolicitation is a Router Solicitation message as described in RFC 4861, Section 4.1.

func (*RouterSolicitation) Type

func (rs *RouterSolicitation) Type() ipv6.ICMPType

Type implements Message.

Directories

Path Synopsis
cmd
ndp
Command ndp is a utility for working with the Neighbor Discovery Protocol.
Command ndp is a utility for working with the Neighbor Discovery Protocol.
internal
ndpcmd
Package ndpcmd provides the commands for the ndp utility.
Package ndpcmd provides the commands for the ndp utility.
ndptest
Package ndptest provides test functions and types for package ndp.
Package ndptest provides test functions and types for package ndp.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL