Skip to content

Commit

Permalink
add static route definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Mar 20, 2021
1 parent bb904fb commit ff9d2fb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ bcg can build [keepalived](https://github.com/acassen/keepalived) configs for VR
| asn | ASN of this router |
| router-id | Router ID of this router |
| prefixes | List of prefixes to originate |
| statics | Map of static route to nexthop |
| irrdb | IRRDB to query prefix sets from (default is rr.ntt.net which includes generated route objects from RPKI ROAs) |
| rtr-server | IP address or hostname of RPKI RTR server (default is 127.0.0.1) |
| keep-filtered | Should BIRD keep filtered routes |
Expand Down
68 changes: 50 additions & 18 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,27 @@ type VRRPInstance struct {

// Config contains global configuration about this router and BCG instance
type Config struct {
Asn uint `yaml:"asn" toml:"ASN" json:"asn"`
RouterId string `yaml:"router-id" toml:"Router-ID" json:"router-id"`
Prefixes []string `yaml:"prefixes" toml:"Prefixes" json:"prefixes"`
Peers map[string]*Peer `yaml:"peers" toml:"Peers" json:"peers"`
VRRPInstances []*VRRPInstance `yaml:"vrrp" toml:"VRRP" json:"vrrp"`
IrrDb string `yaml:"irrdb" toml:"IRRDB" json:"irrdb"`
RtrServer string `yaml:"rtr-server" toml:"RTR-Server" json:"rtr-server"`
RtrPort int `yaml:"rtr-port" toml:"RTR-Port" json:"rtr-port"`
KeepFiltered bool `yaml:"keep-filtered" toml:"KeepFiltered" json:"keep-filtered"`
MergePaths bool `yaml:"merge-paths" toml:"MergePaths" json:"merge-paths"`
PrefSrc4 string `yaml:"pref-src4" toml:"PrefSrc4" json:"PrefSrc4"`
PrefSrc6 string `yaml:"pref-src6" toml:"PrefSrc6" json:"PrefSrc6"`
FilterDefault bool `yaml:"filter-default" toml:"FilterDefault" json:"filter-default"`
DefaultEnabled bool `yaml:"enable-default" toml:"EnableDefault" json:"enable-default"`

OriginSet4 []string `yaml:"-" toml:"-" json:"-"`
OriginSet6 []string `yaml:"-" toml:"-" json:"-"`
Hostname string `yaml:"-" toml:"-" json:"-"`
Asn uint `yaml:"asn" toml:"ASN" json:"asn"`
RouterId string `yaml:"router-id" toml:"Router-ID" json:"router-id"`
Prefixes []string `yaml:"prefixes" toml:"Prefixes" json:"prefixes"`
Statics map[string]string `yaml:"statics" toml:"Statics" json:"statics"`
Peers map[string]*Peer `yaml:"peers" toml:"Peers" json:"peers"`
VRRPInstances []*VRRPInstance `yaml:"vrrp" toml:"VRRP" json:"vrrp"`
IrrDb string `yaml:"irrdb" toml:"IRRDB" json:"irrdb"`
RtrServer string `yaml:"rtr-server" toml:"RTR-Server" json:"rtr-server"`
RtrPort int `yaml:"rtr-port" toml:"RTR-Port" json:"rtr-port"`
KeepFiltered bool `yaml:"keep-filtered" toml:"KeepFiltered" json:"keep-filtered"`
MergePaths bool `yaml:"merge-paths" toml:"MergePaths" json:"merge-paths"`
PrefSrc4 string `yaml:"pref-src4" toml:"PrefSrc4" json:"PrefSrc4"`
PrefSrc6 string `yaml:"pref-src6" toml:"PrefSrc6" json:"PrefSrc6"`
FilterDefault bool `yaml:"filter-default" toml:"FilterDefault" json:"filter-default"`
DefaultEnabled bool `yaml:"enable-default" toml:"EnableDefault" json:"enable-default"`

OriginSet4 []string `yaml:"-" toml:"-" json:"-"`
OriginSet6 []string `yaml:"-" toml:"-" json:"-"`
Static4 map[string]string `yaml:"-" toml:"-" json:"-"`
Static6 map[string]string `yaml:"-" toml:"-" json:"-"`
Hostname string `yaml:"-" toml:"-" json:"-"`
}

// Wrapper stores a Peer and Config passed to the template
Expand Down Expand Up @@ -195,6 +198,35 @@ func Load(filename string) (*Config, error) {
setPeerDefaults(name, peer)
}

// Parse static routes
for prefix, nexthop := range config.Statics {
// Initialize static maps
config.Static4 = map[string]string{}
config.Static6 = map[string]string{}

pfx, _, err := net.ParseCIDR(prefix)
if err != nil {
return nil, errorx.Decorate(err, "Invalid static prefix: "+prefix)
}
if net.ParseIP(nexthop) == nil {
return nil, errorx.Decorate(err, "Invalid static next hop: "+nexthop)
}

if pfx.To4() == nil { // If IPv6
config.Static6[prefix] = nexthop
} else { // If IPv4
config.Static4[prefix] = nexthop
}

// Print static routes
if len(config.Static4) > 0 {
log.Infof("IPv4 statics: %+v", config.Static4)
}
if len(config.Static6) > 0 {
log.Infof("IPv6 statics: %+v", config.Static6)
}
}

// Parse VRRP configs
for _, vrrpInstance := range config.VRRPInstances {
// Sort VIPs by address family
Expand Down
6 changes: 6 additions & 0 deletions templates/global.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ protocol static static4 {
{{- range $i, $prefix := .OriginSet4 }}
route {{ $prefix }} reject;
{{- end }}
{{- range $prefix, $nexthop := .Static4 }}
route {{ $prefix }} via {{ $nexthop }};
{{- end }}
}
{{- end }}

Expand All @@ -27,6 +30,9 @@ protocol static static6 {
{{- range $i, $prefix := .OriginSet6 }}
route {{ $prefix }} reject;
{{- end }}
{{- range $prefix, $nexthop := .Static6 }}
route {{ $prefix }} via {{ $nexthop }};
{{- end }}
}
{{- end }}

Expand Down

0 comments on commit ff9d2fb

Please sign in to comment.