-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhelpers.go
More file actions
198 lines (167 loc) · 4.61 KB
/
helpers.go
File metadata and controls
198 lines (167 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/v3/api"
log "github.com/sirupsen/logrus"
)
// IPNet is a extension of net.IPNet with some addons
type IPNet net.IPNet
func (i IPNet) String() string {
j, _ := i.Mask.Size()
return fmt.Sprintf("%v/%v", i.IP, j)
}
// Plen returns the prefix len as uint
func (i IPNet) Plen() uint32 {
j, _ := i.Mask.Size()
return uint32(j)
}
// IPNetFromAddr reads net.Addr and converts it to IPNet
func IPNetFromAddr(a net.Addr) (*IPNet, error) {
_, p, err := net.ParseCIDR(a.String())
if err != nil {
return nil, err
}
return &IPNet{
IP: p.IP,
Mask: p.Mask,
}, nil
}
// parse bgp community from string to uint32
func parseCommunity(c string) (uint32, error) {
s := strings.SplitN(c, ":", 2)
var i []uint32
for _, o := range s {
u, err := strconv.ParseUint(o, 10, 32)
if err != nil {
return 0, err
}
i = append(i, uint32(u))
}
parsed := i[0]*65536 + i[1]
log.WithFields(log.Fields{"Topic": "parseCommunity", "Community": c}).Tracef("converted to: %v", parsed)
return parsed, nil
}
// compile goBGP path type from prefix, next hop and bgp community
func getPath(p IPNet, nh string, myCom string) (*api.Path, error) {
// convert human readable community to uint32
c, err := parseCommunity(myCom)
if err != nil {
return nil, fmt.Errorf("failed to parse community: %w", err)
}
nhvar := []string{}
if nh != "" {
nhvar = append(nhvar, nh)
}
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
Prefix: p.IP.String(),
PrefixLen: p.Plen(),
})
var family *api.Family
if p.IP.To4() == nil {
family = &api.Family{
Afi: api.Family_AFI_IP6,
Safi: api.Family_SAFI_UNICAST,
}
} else {
family = &api.Family{
Afi: api.Family_AFI_IP,
Safi: api.Family_SAFI_UNICAST,
}
}
attrs, _ := ptypes.MarshalAny(&api.MpReachNLRIAttribute{
Family: family,
NextHops: nhvar,
Nlris: []*any.Any{nlri},
})
com, _ := ptypes.MarshalAny(&api.CommunitiesAttribute{
Communities: []uint32{c},
})
origin, _ := ptypes.MarshalAny(&api.OriginAttribute{
Origin: 2, // needs to be 2 for static route redistribution
})
log.WithFields(log.Fields{"Topic": "Helper", "Route": p}).
Tracef("generated path NLRI %v with community %v", family.Afi, myCom)
return &api.Path{
Family: family,
Nlri: nlri,
Pattrs: []*any.Any{origin, attrs, com},
}, nil
}
//get all local IPs elegible to be elastic IP
func getIPs(v6Mask int, allIfs bool) (*[]IPNet, error) {
var addrs []net.Addr
var err error
if allIfs {
addrs, err = net.InterfaceAddrs()
} else {
lo, err := net.InterfaceByName("lo")
if err != nil {
return nil, err
}
addrs, err = lo.Addrs()
}
if err != nil {
return nil, err
}
sendMask := net.CIDRMask(v6Mask, 128)
// var ips []IPNet
ips := make(map[string]*IPNet)
for _, addr := range addrs {
ip, err := IPNetFromAddr(addr)
if err != nil {
log.WithFields(log.Fields{"Topic": "Helper", "Route": addr, "Error": "invalid IP"}).Warn("invalid IP")
continue
}
// ignore loopback IPs
if ip.IP.IsLoopback() {
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip, "Warn": "not acceptable elastic IP"}).
Trace("ignoring loopback IPs")
continue
}
// ignore link local IPs
if ip.IP.IsLinkLocalUnicast() {
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip, "Warn": "not acceptable elastic IP"}).
Trace("ignoring linklocal IPs")
continue
}
// for ipv4 only a /32 is acceptable
if ip.IP.To4() != nil && ip.Plen() != 32 {
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip, "Warn": "not accepted prefix length"}).
Warn("not accepted prefix length")
continue
}
// for ipv6 lets find the greater subnet we're part of, make it a /64 (or if asked a /56) and advertise that
if ip.IP.To4() == nil {
if ip.Plen() != 64 && ip.Plen() != 56 {
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip, "Warn": "fixing prefix length"}).
Warnf("fixing prefix lenth length to /%d", v6Mask)
ip.Mask = sendMask
}
_, ipNew, err := net.ParseCIDR(ip.String())
if err != nil {
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip, "Error": "invalid IP"}).
Warnf("unable to supernet")
continue
}
ip = &IPNet{
IP: ipNew.IP,
Mask: ipNew.Mask,
}
}
ips[ip.String()] = ip
log.WithFields(log.Fields{"Topic": "Helper", "Route": ip}).Debug("handling prefix")
}
var uniqIPs []IPNet
for _, ip := range ips {
uniqIPs = append(uniqIPs, *ip)
}
if len(uniqIPs) == 0 {
return nil, fmt.Errorf("didn't find any configured elastic IPs")
}
return &uniqIPs, nil
}