diff options
-rw-r--r-- | client/client.go | 7 | ||||
-rw-r--r-- | server/rpki.go | 75 | ||||
-rw-r--r-- | server/rpki_test.go | 27 | ||||
-rw-r--r-- | server/server.go | 2 | ||||
-rw-r--r-- | table/roa.go | 60 |
5 files changed, 96 insertions, 75 deletions
diff --git a/client/client.go b/client/client.go index 7a7b4606..734e7ec4 100644 --- a/client/client.go +++ b/client/client.go @@ -25,7 +25,6 @@ import ( api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" - "github.com/osrg/gobgp/server" "github.com/osrg/gobgp/table" "golang.org/x/net/context" "google.golang.org/grpc" @@ -775,21 +774,21 @@ func (cli *GoBGPClient) GetRPKI() ([]*config.RpkiServer, error) { return servers, nil } -func (cli *GoBGPClient) GetROA(family bgp.RouteFamily) ([]*server.ROA, error) { +func (cli *GoBGPClient) GetROA(family bgp.RouteFamily) ([]*table.ROA, error) { rsp, err := cli.cli.GetRoa(context.Background(), &api.GetRoaRequest{ Family: uint32(family), }) if err != nil { return nil, err } - roas := make([]*server.ROA, 0, len(rsp.Roas)) + roas := make([]*table.ROA, 0, len(rsp.Roas)) for _, r := range rsp.Roas { ip := net.ParseIP(r.Prefix) if ip.To4() != nil { ip = ip.To4() } afi, _ := bgp.RouteFamilyToAfiSafi(family) - roa := server.NewROA(int(afi), []byte(ip), uint8(r.Prefixlen), uint8(r.Maxlen), r.As, net.JoinHostPort(r.Conf.Address, r.Conf.RemotePort)) + roa := table.NewROA(int(afi), []byte(ip), uint8(r.Prefixlen), uint8(r.Maxlen), r.As, net.JoinHostPort(r.Conf.Address, r.Conf.RemotePort)) roas = append(roas, roa) } return roas, nil diff --git a/server/rpki.go b/server/rpki.go index cbef5dee..7f771f27 100644 --- a/server/rpki.go +++ b/server/rpki.go @@ -37,51 +37,12 @@ func before(a, b uint32) bool { return int32(a-b) < 0 } -type ipPrefix struct { - Prefix net.IP - Length uint8 -} - -func (p *ipPrefix) String() string { - return fmt.Sprintf("%s/%d", p.Prefix, p.Length) -} - type roaBucket struct { - Prefix *ipPrefix - entries []*ROA -} - -type ROA struct { - Family int - Prefix *ipPrefix - MaxLen uint8 - AS uint32 - Src string -} - -func NewROA(family int, prefixByte []byte, prefixLen uint8, maxLen uint8, as uint32, src string) *ROA { - p := make([]byte, len(prefixByte)) - copy(p, prefixByte) - return &ROA{ - Family: family, - Prefix: &ipPrefix{ - Prefix: p, - Length: prefixLen, - }, - MaxLen: maxLen, - AS: as, - Src: src, - } -} - -func (r *ROA) Equal(roa *ROA) bool { - if r.MaxLen == roa.MaxLen && r.Src == roa.Src && r.AS == roa.AS { - return true - } - return false + Prefix *table.IPPrefix + entries []*table.ROA } -type roas []*ROA +type roas []*table.ROA func (r roas) Len() int { return len(r) @@ -185,7 +146,7 @@ func (m *roaManager) deleteAllROA(network string) { deleteKeys := make([]string, 0, tree.Len()) tree.Walk(func(s string, v interface{}) bool { b, _ := v.(*roaBucket) - newEntries := make([]*ROA, 0, len(b.entries)) + newEntries := make([]*table.ROA, 0, len(b.entries)) for _, r := range b.entries { if r.Src != network { newEntries = append(newEntries, r) @@ -275,7 +236,7 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) { client.state.Downtime = time.Now().Unix() // clear state client.endOfData = false - client.pendingROAs = make([]*ROA, 0) + client.pendingROAs = make([]*table.ROA, 0) client.state.RpkiMessages = config.RpkiMessages{} client.conn = nil client.t = tomb.Tomb{} @@ -307,7 +268,7 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) { } } -func (m *roaManager) roa2tree(roa *ROA) (*radix.Tree, string) { +func (m *roaManager) roa2tree(roa *table.ROA) (*radix.Tree, string) { tree := m.Roas[bgp.RF_IPv4_UC] if roa.Family == bgp.AFI_IP6 { tree = m.Roas[bgp.RF_IPv6_UC] @@ -315,12 +276,12 @@ func (m *roaManager) roa2tree(roa *ROA) (*radix.Tree, string) { return tree, table.IpToRadixkey(roa.Prefix.Prefix, roa.Prefix.Length) } -func (m *roaManager) deleteROA(roa *ROA) { +func (m *roaManager) deleteROA(roa *table.ROA) { tree, key := m.roa2tree(roa) b, _ := tree.Get(key) if b != nil { bucket := b.(*roaBucket) - newEntries := make([]*ROA, 0, len(bucket.entries)) + newEntries := make([]*table.ROA, 0, len(bucket.entries)) for _, r := range bucket.entries { if !r.Equal(roa) { newEntries = append(newEntries, r) @@ -343,14 +304,14 @@ func (m *roaManager) deleteROA(roa *ROA) { }).Info("Can't withdraw a ROA") } -func (m *roaManager) addROA(roa *ROA) { +func (m *roaManager) addROA(roa *table.ROA) { tree, key := m.roa2tree(roa) b, _ := tree.Get(key) var bucket *roaBucket if b == nil { bucket = &roaBucket{ Prefix: roa.Prefix, - entries: make([]*ROA, 0), + entries: make([]*table.ROA, 0), } tree.Insert(key, bucket) } else { @@ -394,7 +355,7 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta family = bgp.AFI_IP6 received.Ipv6Prefix++ } - roa := NewROA(family, msg.Prefix, msg.PrefixLen, msg.MaxLen, msg.AS, client.host) + roa := table.NewROA(family, msg.Prefix, msg.PrefixLen, msg.MaxLen, msg.AS, client.host) if (msg.Flags & 1) == 1 { if client.endOfData { c.addROA(roa) @@ -421,7 +382,7 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta for _, roa := range client.pendingROAs { c.addROA(roa) } - client.pendingROAs = make([]*ROA, 0) + client.pendingROAs = make([]*table.ROA, 0) case *rtr.RTRCacheReset: client.softReset() received.CacheReset++ @@ -496,9 +457,9 @@ func (c *roaManager) GetServers() []*config.RpkiServer { return l } -func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*ROA, error) { +func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*table.ROA, error) { if len(c.clientMap) == 0 { - return []*ROA{}, fmt.Errorf("RPKI server isn't configured.") + return []*table.ROA{}, fmt.Errorf("RPKI server isn't configured.") } var rfList []bgp.RouteFamily switch family { @@ -509,7 +470,7 @@ func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*ROA, error) { default: rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC, bgp.RF_IPv6_UC} } - l := make([]*ROA, 0) + l := make([]*table.ROA, 0) for _, rf := range rfList { if tree, ok := c.Roas[rf]; ok { tree.Walk(func(s string, v interface{}) bool { @@ -598,7 +559,7 @@ type roaClient struct { timer *time.Timer lifetime int64 endOfData bool - pendingROAs []*ROA + pendingROAs []*table.ROA } func NewRoaClient(address, port string, ch chan *ROAEvent, lifetime int64) *roaClient { @@ -606,7 +567,7 @@ func NewRoaClient(address, port string, ch chan *ROAEvent, lifetime int64) *roaC host: net.JoinHostPort(address, port), eventCh: ch, lifetime: lifetime, - pendingROAs: make([]*ROA, 0), + pendingROAs: make([]*table.ROA, 0), } } @@ -633,7 +594,7 @@ func (c *roaClient) softReset() error { } c.state.RpkiMessages.RpkiSent.ResetQuery++ c.endOfData = false - c.pendingROAs = make([]*ROA, 0) + c.pendingROAs = make([]*table.ROA, 0) } return nil } diff --git a/server/rpki_test.go b/server/rpki_test.go index ee4689af..8405738b 100644 --- a/server/rpki_test.go +++ b/server/rpki_test.go @@ -24,6 +24,7 @@ import ( "github.com/armon/go-radix" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/table" "github.com/stretchr/testify/assert" ) @@ -61,8 +62,8 @@ func TestValidate0(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 32, 100, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 24, 200, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 32, 100, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 24, 200, "")) var r config.RpkiValidationResultType @@ -90,7 +91,7 @@ func TestValidate1(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) var r config.RpkiValidationResultType @@ -121,7 +122,7 @@ func TestValidate3(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) var r config.RpkiValidationResultType @@ -133,7 +134,7 @@ func TestValidate3(t *testing.T) { assert.Equal(r, config.RPKI_VALIDATION_RESULT_TYPE_INVALID) manager, _ = NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) tree = manager.Roas[bgp.RF_IPv4_UC] r = validateOne(tree, "10.0.0.0/17", "65000") @@ -144,8 +145,8 @@ func TestValidate4(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65001, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65001, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -160,8 +161,8 @@ func TestValidate5(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 17, 17, 65000, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.128.0").To4(), 17, 17, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 17, 17, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.128.0").To4(), 17, 17, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -173,7 +174,7 @@ func TestValidate6(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 8, 32, 0, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 8, 32, 0, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -191,7 +192,7 @@ func TestValidate7(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] @@ -209,8 +210,8 @@ func TestValidate8(t *testing.T) { assert := assert.New(t) manager, _ := NewROAManager(0) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 0, "")) - manager.addROA(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 0, "")) + manager.addROA(table.NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) var r config.RpkiValidationResultType tree := manager.Roas[bgp.RF_IPv4_UC] diff --git a/server/server.go b/server/server.go index 6b336849..d71e45b2 100644 --- a/server/server.go +++ b/server/server.go @@ -2265,7 +2265,7 @@ func (s *BgpServer) GetRpki() (l []*config.RpkiServer, err error) { return l, err } -func (s *BgpServer) GetRoa(family bgp.RouteFamily) (l []*ROA, err error) { +func (s *BgpServer) GetRoa(family bgp.RouteFamily) (l []*table.ROA, err error) { ch := make(chan struct{}) defer func() { <-ch }() diff --git a/table/roa.go b/table/roa.go new file mode 100644 index 00000000..fe08fe54 --- /dev/null +++ b/table/roa.go @@ -0,0 +1,60 @@ +// Copyright (C) 2016 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package table + +import ( + "fmt" + "net" +) + +type IPPrefix struct { + Prefix net.IP + Length uint8 +} + +func (p *IPPrefix) String() string { + return fmt.Sprintf("%s/%d", p.Prefix, p.Length) +} + +type ROA struct { + Family int + Prefix *IPPrefix + MaxLen uint8 + AS uint32 + Src string +} + +func NewROA(family int, prefixByte []byte, prefixLen uint8, maxLen uint8, as uint32, src string) *ROA { + p := make([]byte, len(prefixByte)) + copy(p, prefixByte) + return &ROA{ + Family: family, + Prefix: &IPPrefix{ + Prefix: p, + Length: prefixLen, + }, + MaxLen: maxLen, + AS: as, + Src: src, + } +} + +func (r *ROA) Equal(roa *ROA) bool { + if r.MaxLen == roa.MaxLen && r.Src == roa.Src && r.AS == roa.AS { + return true + } + return false +} |