FreeScript
bgpfix
by bgpfix · version 2026.11.13 · updated 3 days ago
No reviews yet
- Downloads
- 0
- Reviews
- 0
- Size
- 446 KB
- Code check
- Clean
- Licence
- MIT
- Released
- Nov 2026
BGP library in Go
BGPFix Golang Library
A generic-purpose, high-performance Golang library for bridging the gaps in BGP.
For a stand-alone tool, see bgpipe: BGP pipeline processor.
Summary
BGPFix can "fix" or "extend" BGP sessions in-flight, possibly adding new features or security layers to proprietary BGP speakers (think big router vendors). The project vision is to allow implementing:
The overall idea is presented below. You don't need to use the whole library, eg. you may stick to the basic BGP message marshal / unmarshal procedures.
The above explains the concept of a Pipe: it has two directions used to exchange BGP messages between 2 speakers on the left (L) and right (R) hand side of the picture.
Each Msg sent to the In channel of a particular direction will go through a set of callbacks (think "plugins") configured in the pipe Options. Each callback can read, write, modify, synthesize, or drop messages before they reach the Out channel. In addition to BGP messages, callbacks may emit Events - such as the standard events of the Pipe - which event handlers may subscribe to in the pipe Options.
Packages
Example
A basic example on how to establish a BGP session with a router, and print all messages as JSON to stdout:
```go
package main
import (
"context"
"flag"
"fmt"
"io"
"net"
"net/netip"
"os"
"github.com/bgpfix/bgpfix/meta"
"github.com/bgpfix/bgpfix/msg"
"github.com/bgpfix/bgpfix/pipe"
"github.com/bgpfix/bgpfix/speaker"
)
var (
opt_active = flag.Bool("active", false, "send OPEN first")
opt_asn = flag.Int("asn", 65055, "local ASN number")
opt_hold = flag.Int("hold", 60, "local hold timer")
opt_id = flag.String("id", "1.1.1.1", "local Id (must be IPv4 address)")
)
func main() {
// parse flags
flag.Parse()
if flag.NArg() == 0 {
fmt.Printf("usage: bgpfix [OPTIONS] \n")
os.Exit(1)
}
// create a Pipe, add callback and event handlers
p := pipe.NewPipe(context.Background())
p.OnMsg(print, meta.DIR_LR) // call print() on every message in any direction
p.OnEvent(event) // call event() on any pipe event
// L side: a TCP target, sending to R
conn, err := net.Dial("tcp", flag.Arg(0)) // assumes a ":179" suffix
if err != nil {
panic(err)
}
// R side: a local speaker, sending to L
spk := speaker.NewSpeaker(context.Background())
spk.Options.Passive = !*opt_active
spk.Options.LocalASN = *opt_asn
spk.Options.LocalHoldTime = *opt_hold
spk.Options.LocalId = netip.MustParseAddr(*opt_id)
spk.Attach(p, meta.DIR_L)
// copy from conn -> R
go func() {
io.Copy(p.R, conn)
p.Stop()
}()
// copy from L -> conn
go func() {
io.Copy(conn, p.L)
p.Stop()
}()
// start and wait till all processing is done
p.Start()
p.Wait()
}
func print(m *msg.Msg) bool {
os.Stdout.Write(m.GetJSON())
return true
}
func event(ev *pipe.Event) bool {
switch ev.Type {
case pipe.
…
A generic-purpose, high-performance Golang library for bridging the gaps in BGP.
For a stand-alone tool, see bgpipe: BGP pipeline processor.
Summary
BGPFix can "fix" or "extend" BGP sessions in-flight, possibly adding new features or security layers to proprietary BGP speakers (think big router vendors). The project vision is to allow implementing:
- bidirectional BGP session to JSON translation, replacing exabgp for some use-cases,
- transparent BGP proxy, optionally rewriting and filtering messages in-flight,
- streaming MRT files to BGP routers, adding the necessary OPEN negotiation beforehand,
- Flowspec data plane firewalls using Linux Netfilter,
- passive inspection (and storage) of ongoing BGP sessions, like in tcpdump,
- cool new BGP extensions for legacy speakers, eg. RPKI and ASPA validation, Only To Customer (OTC) attribute, or even BGPSec,
- protecting from grave flaws in BGP error handling, and possibly other flaws found using BGP fuzzing projects
- academic research ideas, eg. Pretty Good BGP or protection against distributed prefix de-aggregation attacks.
The overall idea is presented below. You don't need to use the whole library, eg. you may stick to the basic BGP message marshal / unmarshal procedures.
The above explains the concept of a Pipe: it has two directions used to exchange BGP messages between 2 speakers on the left (L) and right (R) hand side of the picture.
Each Msg sent to the In channel of a particular direction will go through a set of callbacks (think "plugins") configured in the pipe Options. Each callback can read, write, modify, synthesize, or drop messages before they reach the Out channel. In addition to BGP messages, callbacks may emit Events - such as the standard events of the Pipe - which event handlers may subscribe to in the pipe Options.
Packages
- msg, attrs, caps, nlri: BGP messages, path attributes, capabilities, and prefixes - wire and JSON
- pipe: the bidirectional message pipeline, with callbacks and events
- speaker: a basic BGP speaker (OPEN, KEEPALIVE, hold timers)
- filter: a filter language for BGP messages, eg.
prefix ~ 8.0.0.0/8 && aspath_len > 5 - mrt, bmp, exa: MRT (incl. RIB dumps), BMP / OpenBMP, and ExaBGP formats
- rpki, rtr: RPKI cache with ROV and ASPA validation, fed by an RTR v0/v1/v2 client or JSON/CSV files
go test ./msg -run XXX -fuzz FuzzMsgWire), and a parser panic becomes an error instead of a crash.Example
A basic example on how to establish a BGP session with a router, and print all messages as JSON to stdout:
```go
package main
import (
"context"
"flag"
"fmt"
"io"
"net"
"net/netip"
"os"
"github.com/bgpfix/bgpfix/meta"
"github.com/bgpfix/bgpfix/msg"
"github.com/bgpfix/bgpfix/pipe"
"github.com/bgpfix/bgpfix/speaker"
)
var (
opt_active = flag.Bool("active", false, "send OPEN first")
opt_asn = flag.Int("asn", 65055, "local ASN number")
opt_hold = flag.Int("hold", 60, "local hold timer")
opt_id = flag.String("id", "1.1.1.1", "local Id (must be IPv4 address)")
)
func main() {
// parse flags
flag.Parse()
if flag.NArg() == 0 {
fmt.Printf("usage: bgpfix [OPTIONS] \n")
os.Exit(1)
}
// create a Pipe, add callback and event handlers
p := pipe.NewPipe(context.Background())
p.OnMsg(print, meta.DIR_LR) // call print() on every message in any direction
p.OnEvent(event) // call event() on any pipe event
// L side: a TCP target, sending to R
conn, err := net.Dial("tcp", flag.Arg(0)) // assumes a ":179" suffix
if err != nil {
panic(err)
}
// R side: a local speaker, sending to L
spk := speaker.NewSpeaker(context.Background())
spk.Options.Passive = !*opt_active
spk.Options.LocalASN = *opt_asn
spk.Options.LocalHoldTime = *opt_hold
spk.Options.LocalId = netip.MustParseAddr(*opt_id)
spk.Attach(p, meta.DIR_L)
// copy from conn -> R
go func() {
io.Copy(p.R, conn)
p.Stop()
}()
// copy from L -> conn
go func() {
io.Copy(conn, p.L)
p.Stop()
}()
// start and wait till all processing is done
p.Start()
p.Wait()
}
func print(m *msg.Msg) bool {
os.Stdout.Write(m.GetJSON())
return true
}
func event(ev *pipe.Event) bool {
switch ev.Type {
case pipe.
…