forked from blind-oracle/dnstap-bgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgp.go
189 lines (154 loc) · 3.24 KB
/
bgp.go
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
package main
import (
"context"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/api"
gobgp "github.com/osrg/gobgp/pkg/server"
)
type bgpCfg struct {
AS uint32
RouterID string
NextHop string
SourceIP string
SourceIF string
Peers []string
IPv6 bool
}
type bgpServer struct {
s *gobgp.BgpServer
c *bgpCfg
}
func newBgp(c *bgpCfg) (b *bgpServer, err error) {
if c.AS == 0 {
return nil, fmt.Errorf("You need to provide AS")
}
if c.SourceIP != "" && c.SourceIF != "" {
return nil, fmt.Errorf("SourceIP and SourceIF are mutually exclusive")
}
if len(c.Peers) == 0 {
return nil, fmt.Errorf("You need to provide at least one peer")
}
b = &bgpServer{
s: gobgp.NewBgpServer(),
c: c,
}
go b.s.Serve()
if err = b.s.StartBgp(context.Background(), &api.StartBgpRequest{
Global: &api.Global{
As: c.AS,
RouterId: c.RouterID,
ListenPort: -1,
},
}); err != nil {
return
}
if err = b.s.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(p *api.Peer) { log.Println(p) }); err != nil {
return
}
for _, p := range c.Peers {
if err = b.addPeer(p); err != nil {
return
}
}
return
}
func (b *bgpServer) addPeer(addr string) (err error) {
port := 179
if t := strings.SplitN(addr, ":", 2); len(t) == 2 {
addr = t[0]
if port, err = strconv.Atoi(t[1]); err != nil {
return fmt.Errorf("Unable to parse port '%s' as int: %s", t[1], err)
}
}
p := &api.Peer{
Conf: &api.PeerConf{
NeighborAddress: addr,
PeerAs: b.c.AS,
},
Timers: &api.Timers{
Config: &api.TimersConfig{
ConnectRetry: 10,
},
},
Transport: &api.Transport{
MtuDiscovery: true,
RemoteAddress: addr,
RemotePort: uint32(port),
},
}
if b.c.SourceIP != "" {
p.Transport.LocalAddress = b.c.SourceIP
}
if b.c.SourceIF != "" {
p.Transport.BindInterface = b.c.SourceIF
}
return b.s.AddPeer(context.Background(), &api.AddPeerRequest{
Peer: p,
})
}
func (b *bgpServer) getPath(ip net.IP) *api.Path {
var pfxLen uint32 = 32
if ip.To4() == nil {
if !b.c.IPv6 {
return nil
}
pfxLen = 128
}
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
Prefix: ip.String(),
PrefixLen: pfxLen,
})
a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
Origin: 0,
})
var nh string
if b.c.NextHop != "" {
nh = b.c.NextHop
} else if b.c.SourceIP != "" {
nh = b.c.SourceIP
} else {
nh = b.c.RouterID
}
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
NextHop: nh,
})
return &api.Path{
Family: &api.Family{
Afi: api.Family_AFI_IP,
Safi: api.Family_SAFI_UNICAST,
},
Nlri: nlri,
Pattrs: []*any.Any{a1, a2},
}
}
func (b *bgpServer) addHost(ip net.IP) (err error) {
p := b.getPath(ip)
if p == nil {
return
}
_, err = b.s.AddPath(context.Background(), &api.AddPathRequest{
Path: p,
})
return
}
func (b *bgpServer) delHost(ip net.IP) (err error) {
p := b.getPath(ip)
if p == nil {
return
}
return b.s.DeletePath(context.Background(), &api.DeletePathRequest{
Path: p,
})
}
func (b *bgpServer) close() error {
ctx, cf := context.WithTimeout(context.Background(), 5*time.Second)
defer cf()
return b.s.StopBgp(ctx, &api.StopBgpRequest{})
}