diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-03-19 23:31:48 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-03-19 23:31:48 +0900 |
commit | 448160257ea6ad41ab56a12a5a1edb5e0515cef6 (patch) | |
tree | 19f98a739d1c88c48d3401599785bec77334587b | |
parent | be6a0dbe869766e5ba6c818eb2cdd8bd6e580d67 (diff) |
table: handle IPv4-Mapped IPv6 Address as v6
Currently, Prefix structure wrongly handles IPv4-Mapped IPv6 Address
as v4.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | api/grpc_server.go | 4 | ||||
-rw-r--r-- | table/policy.go | 26 |
2 files changed, 24 insertions, 6 deletions
diff --git a/api/grpc_server.go b/api/grpc_server.go index f167c13f..9fc66bde 100644 --- a/api/grpc_server.go +++ b/api/grpc_server.go @@ -1001,12 +1001,12 @@ func (s *Server) DeleteNeighbor(ctx context.Context, arg *DeleteNeighborRequest) } func NewPrefixFromApiStruct(a *Prefix) (*table.Prefix, error) { - addr, prefix, err := net.ParseCIDR(a.IpPrefix) + _, prefix, err := net.ParseCIDR(a.IpPrefix) if err != nil { return nil, err } rf := bgp.RF_IPv4_UC - if addr.To4() == nil { + if strings.Contains(a.IpPrefix, ":") { rf = bgp.RF_IPv6_UC } return &table.Prefix{ diff --git a/table/policy.go b/table/policy.go index fe1dcaf8..edaf3a3e 100644 --- a/table/policy.go +++ b/table/policy.go @@ -285,14 +285,32 @@ func (lhs *Prefix) Equal(rhs *Prefix) bool { return lhs.Prefix.String() == rhs.Prefix.String() && lhs.MasklengthRangeMin == rhs.MasklengthRangeMin && lhs.MasklengthRangeMax == rhs.MasklengthRangeMax } +func (p *Prefix) PrefixString() string { + isZeros := func(p net.IP) bool { + for i := 0; i < len(p); i++ { + if p[i] != 0 { + return false + } + } + return true + } + + ip := p.Prefix.IP + if p.AddressFamily == bgp.RF_IPv6_UC && isZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff { + m, _ := p.Prefix.Mask.Size() + return fmt.Sprintf("::FFFF:%s/%d", ip.To16(), m) + } + return p.Prefix.String() +} + func NewPrefix(c config.Prefix) (*Prefix, error) { - addr, prefix, err := net.ParseCIDR(c.IpPrefix) + _, prefix, err := net.ParseCIDR(c.IpPrefix) if err != nil { return nil, err } rf := bgp.RF_IPv4_UC - if addr.To4() == nil { + if strings.Contains(c.IpPrefix, ":") { rf = bgp.RF_IPv6_UC } p := &Prefix{ @@ -420,7 +438,7 @@ func (s *PrefixSet) List() []string { s.tree.Walk(func(s string, v interface{}) bool { ps := v.([]*Prefix) for _, p := range ps { - list = append(list, fmt.Sprintf("%s %d..%d", p.Prefix.String(), p.MasklengthRangeMin, p.MasklengthRangeMax)) + list = append(list, fmt.Sprintf("%s %d..%d", p.PrefixString(), p.MasklengthRangeMin, p.MasklengthRangeMax)) } return false }) @@ -432,7 +450,7 @@ func (s *PrefixSet) ToConfig() *config.PrefixSet { s.tree.Walk(func(s string, v interface{}) bool { ps := v.([]*Prefix) for _, p := range ps { - list = append(list, config.Prefix{IpPrefix: p.Prefix.String(), MasklengthRange: fmt.Sprintf("%d..%d", p.MasklengthRangeMin, p.MasklengthRangeMax)}) + list = append(list, config.Prefix{IpPrefix: p.PrefixString(), MasklengthRange: fmt.Sprintf("%d..%d", p.MasklengthRangeMin, p.MasklengthRangeMax)}) } return false }) |