bgp

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: GPL-2.0 Imports: 10 Imported by: 0

README

bgp

A simple BGP library for originating /32 or /128 prefixes.

The primary use case is to provide route health injection functionality for a load balancer.

Loopback BGP

I have found that it is possible to connect to a local BIRD instance and advertise addresses for re-distribution into the network from there. Here I connect to BIRD using multiprotocol extensions on the loopback interface with 127.0.0.1 as my router ID and the loopback IP addresses for both IPv4 and IPv6 next hop (BIRD will update these when re-advertising):

go run bgp.go -6 ::1 -m 65001 127.0.0.1 127.0.0.1

Using the BIRD configuration below I can then connect to a router and gain the benefits of using BFD. Global IPv6 addresses on the local server and the router are needed for the IPv6 address to be re-advertised successfully.

log syslog all;
protocol device {}
protocol bfd {}

filter vips {
    if net ~ [ 192.168.101.0/24{32,32} ] then accept; # accept /32 prefixes from 192.168.101.0/24
    if net ~ [ fd0b:2b0b:a7b8:1ee7:c0de::/80{128,128} ] then accept; # similar config for IPv6
    reject;
}

protocol bgp core {
    local    as 65001;
    neighbor as 65000;
    neighbor 10.12.34.56;
    ipv4 { export filter vips; import none; next hop self; };
    ipv6 { export filter vips; import none; next hop self; };
    bfd on;
}

protocol bgp lb {
    local    as 65001;  # iBGP - we could use eBGP if we specify 'multihop':
    neighbor as 65001;  # loopback address doesn't count as "directly connected"
    neighbor 127.0.0.1; # load balancer connects on the loopback interface
    passive;            # load balancer always inititates the connection
    ipv4 { export none; import all; };
    ipv6 { export none;	import all; };
}

If you get it working on other implementations then it would be great to have more sample configurations here.

Documentation

Index

Constants

View Source
const (
	M_OPEN         = 1
	M_UPDATE       = 2
	M_NOTIFICATION = 3
	M_KEEPALIVE    = 4

	IGP = 0
	EGP = 1

	//https://www.rfc-editor.org/rfc/rfc3392.txt
	CAPABILITIES_OPTIONAL_PARAMETER = 2 // Capabilities Optional Parameter (Parameter Type 2)

	// https://www.iana.org/assignments/capability-codes/capability-codes.xhtml
	BGP4_MP = 1 //Multiprotocol Extensions for BGP-4

	// Path attribute types
	ORIGIN          = 1
	AS_PATH         = 2
	NEXT_HOP        = 3
	MULTI_EXIT_DISC = 4
	LOCAL_PREF      = 5
	COMMUNITIES     = 8
	MP_REACH_NLRI   = 14 // Multiprotocol Reachable NLRI - MP_REACH_NLRI (Type Code 14)
	MP_UNREACH_NLRI = 15 // Multiprotocol Unreachable NLRI - MP_UNREACH_NLRI (Type Code 15)

	AS_SET      = 1
	AS_SEQUENCE = 2

	// NOTIFICATION ERROR CODES
	MESSAGE_HEADER_ERROR        = 1 // [RFC4271]
	OPEN_MESSAGE_ERROR          = 2 // [RFC4271]
	UPDATE_MESSAGE_ERROR        = 3 // [RFC4271]
	HOLD_TIMER_EXPIRED          = 4 // [RFC4271]
	FSM_ERROR                   = 5 // [RFC4271]
	CEASE                       = 6 // [RFC4271]
	ROUTE_REFRESH_MESSAGE_ERROR = 7 // [RFC7313]

	UNSUPPORTED_VERSION_NUMBER = 1 // OPEN_MESSAGE_ERROR
	BAD_BGP_ID                 = 3 // OPEN_MESSAGE_ERROR
	UNNACEPTABLE_HOLD_TIME     = 6 // OPEN_MESSAGE_ERROR
	BAD_MESSAGE_TYPE           = 3 // MESSAGE_HEADER_ERROR
	ADMINISTRATIVE_SHUTDOWN    = 2 // CEASE
	OUT_OF_RESOURCES           = 8 // CEASE

	WTCR = 64  // (Well-known, Transitive, Complete, Regular length)
	WTCE = 80  // (Well-known, Transitive, Complete, Extended length)
	ONCR = 128 // (Optional, Non-transitive, Complete, Regular length)
	ONCE = 144 // (Optional, Non-transitive, Complete, Extended length)
	OTCR = 192 // (Optional, Transitive, Complete, Regular length)
	OTCE = 208 // (Optional, Transitive, Complete, Extended length)
)
View Source
const (
	IDLE         = "IDLE"
	ACTIVE       = "ACTIVE"
	CONNECT      = "CONNECT"
	OPEN_SENT    = "OPEN_SENT"
	OPEN_CONFIRM = "OPEN_CONFIRM"
	ESTABLISHED  = "ESTABLISHED"
)
View Source
const (
	CONNECTION_FAILED = iota
	REMOTE_SHUTDOWN
	LOCAL_SHUTDOWN
	INVALID_LOCALIP
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BGPNotify

type BGPNotify interface {
	BGPPeer(peer string, params Parameters, add bool)  // Peer added if "add" is true, peer was removed if not
	BGPSession(peer string, local bool, reason string) // Session shutdown was locally requested if "local" is true
}

type Community

type Community uint32

func (*Community) MarshalJSON

func (c *Community) MarshalJSON() ([]byte, error)

func (*Community) UnmarshalJSON

func (c *Community) UnmarshalJSON(data []byte) error

type IP

type IP = [4]byte

type IP4

type IP4 [4]byte

func (IP4) MarshalJSON

func (i IP4) MarshalJSON() ([]byte, error)

func (IP4) String

func (i IP4) String() string

func (*IP4) UnmarshalJSON

func (i *IP4) UnmarshalJSON(d []byte) error

type IP6

type IP6 [16]byte

func (IP6) MarshalJSON

func (i IP6) MarshalJSON() ([]byte, error)

func (IP6) String

func (i IP6) String() string

func (*IP6) UnmarshalJSON

func (i *IP6) UnmarshalJSON(d []byte) error

type IPNet

type IPNet net.IPNet

func (*IPNet) MarshalJSON

func (i *IPNet) MarshalJSON() ([]byte, error)

func (*IPNet) UnmarshalJSON

func (i *IPNet) UnmarshalJSON(data []byte) error

func (*IPNet) UnmarshalText

func (i *IPNet) UnmarshalText(data []byte) error

type Parameters

type Parameters struct {
	// only used at session start
	ASNumber uint16 `json:"as_number,omitempty"`
	HoldTime uint16 `json:"hold_time,omitempty"`
	SourceIP IP4    `json:"source_ip,omitempty"` // not sure that this can be used with Dial()

	NextHop4      IP4  `json:"next_hop_4,omitempty"`
	NextHop6      IP6  `json:"next_hop_6,omitempty"`
	Multiprotocol bool `json:"multiprotocol,omitempty"`

	// can change during session
	MED         uint32      `json:"med,omitempty"`
	LocalPref   uint32      `json:"local_pref,omitempty"`
	Communities []Community `json:"communities,omitempty"`

	Accept []netip.Prefix `json:"accept,omitempty"`
	Reject []netip.Prefix `json:"reject,omitempty"`
}

func (*Parameters) Diff

func (a *Parameters) Diff(b Parameters) bool

type Pool

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

func NewPool

func NewPool(routerid IP, peers map[string]Parameters, rib []netip.Addr, log BGPNotify) *Pool

func (*Pool) Close

func (p *Pool) Close()

func (*Pool) Configure

func (p *Pool) Configure(c map[string]Parameters)

func (*Pool) RIB

func (p *Pool) RIB(r []netip.Addr)

func (*Pool) Status

func (p *Pool) Status() status

type Session

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

func NewSession

func NewSession(id IP, peer string, p Parameters, r []netip.Addr, l BGPNotify) *Session

func (*Session) Close

func (s *Session) Close()

func (*Session) Configure

func (s *Session) Configure(p Parameters)

func (*Session) LocRIB

func (s *Session) LocRIB(r []netip.Addr)

func (*Session) RIB

func (s *Session) RIB(r []netip.Addr)

func (*Session) Start

func (s *Session) Start(id IP, peer string, p Parameters, r []netip.Addr, l BGPNotify)

func (*Session) Status

func (s *Session) Status() Status

func (*Session) Stop

func (s *Session) Stop()

type Status

type Status struct {
	State             string        `json:"state"`
	When              time.Time     `json:"when"`
	Duration          time.Duration `json:"duration_s"`
	UpdateCalculation time.Duration `json:"update_calculation_ms"`
	Advertised        uint64        `json:"advertised_routes"`
	Withdrawn         uint64        `json:"withdrawn_routes"`
	Prefixes          int           `json:"current_routes"`
	Attempts          uint64        `json:"connection_attempts"`
	Connections       uint64        `json:"successful_connections"`
	Established       uint64        `json:"established_sessions"`
	LastError         string        `json:"last_error"`
	HoldTime          uint16        `json:"hold_time"`
	LocalASN          uint16        `json:"local_asn"`
	RemoteASN         uint16        `json:"remote_asn"`
	AdjRIBOut         []string      `json:"adj_rib_out"`
	LocalIP           string        `json:"local_ip"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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