-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameters.go
157 lines (133 loc) · 3.55 KB
/
parameters.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
package main
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
"log"
)
const (
// Open optional parameter types
capabilities = 2
// capability codes I support
// https://www.iana.org/assignments/capability-codes/capability-codes.xhtml
capMpBgp uint8 = 1
cap4Byte uint8 = 65
capAddPath uint8 = 69
capRefresh uint8 = 70 // Only support enhanced refresh
)
type parameters struct {
ASN32 [4]byte
Refresh bool
AddPath []addr
AddrFamilies []addr
Supported []uint8
Unsupported []uint8
}
type addr struct {
AFI uint16
_ uint8
SAFI uint8
}
func decodeOptionalParameters(param *[]byte) parameters {
r := bytes.NewReader(*param)
var par parameters
par.AddrFamilies = []addr{}
for {
// Parameter header contains the optional parameters header and length in total.
var p parameterHeader
binary.Read(r, binary.BigEndian, &p)
if r.Len() == 0 {
break
}
// Pass all capabilties to be decoded. Depending on vendor,
// there could be 1 or more capability per optional parameter.
c := make([]byte, p.Length)
io.ReadFull(r, c)
decodeCapability(c, &par)
}
return par
}
func decodeCapability(cap []byte, p *parameters) {
r := bytes.NewReader(cap)
// There may be 1 or more capabilities per call.
for {
if r.Len() == 0 {
break
}
var cap msgCapability
binary.Read(r, binary.BigEndian, &cap)
buf := bytes.NewBuffer(make([]byte, 0, cap.Length))
switch cap.Code {
case cap4Byte:
log.Printf("4byte ASN supported")
io.CopyN(buf, r, int64(cap.Length))
p.ASN32 = decode4OctetAS(buf)
p.Supported = append(p.Supported, cap.Code)
case capMpBgp:
log.Printf("Multiprotocol Extenstions supported")
io.CopyN(buf, r, int64(cap.Length))
addr := decodeMPBGP(buf)
log.Printf("AFI is %d, SAFI is %d\n", addr.AFI, addr.SAFI)
p.AddrFamilies = append(p.AddrFamilies, addr)
p.Supported = append(p.Supported, cap.Code)
case capRefresh:
log.Printf("Enhanced Route Refresh supported")
p.Refresh = true
p.Supported = append(p.Supported, cap.Code)
case capAddPath:
log.Printf("AddPath advertised")
io.CopyN(buf, r, int64(cap.Length))
addr, ok := decodeAddPath(buf)
if !ok {
log.Printf("Peer is not configured to send multiple paths")
continue
}
log.Printf("AddPath supported")
p.AddPath = append(p.AddPath, addr)
p.Supported = append(p.Supported, cap.Code)
default:
log.Printf("Capability Code %d is unsupported", cap.Code)
p.Unsupported = append(p.Unsupported, cap.Code)
// As capability is not supported, drop the rest of the capability message.
io.CopyN(ioutil.Discard, r, int64(cap.Length))
}
}
}
// parameter 65 is 4-octet AS support
func decode4OctetAS(b *bytes.Buffer) [4]byte {
var ASN [4]byte
binary.Read(b, binary.BigEndian, &ASN)
log.Println(ASN)
return ASN
}
func decodeMPBGP(b *bytes.Buffer) addr {
var afisafi addr
binary.Read(b, binary.BigEndian, &afisafi)
return afisafi
}
// Only support AddPath if peer can send multiple paths, else not supported.
func decodeAddPath(b *bytes.Buffer) (addr, bool) {
var adp struct {
Afi uint16
Safi uint8
SendRec uint8
}
binary.Read(b, binary.BigEndian, &adp)
// 2 means peer can send us multiple paths, 3 means both send and receive.
// 1 means peer can only receive.
if adp.SendRec == 2 || adp.SendRec == 3 {
return addr{
AFI: adp.Afi,
SAFI: adp.Safi,
}, true
}
return addr{}, false
}
// TODO These should be methods attached to the struct
func isIPv4Unicast(a addr) bool {
return a.AFI == 1 && a.SAFI == 1
}
func isIPv6Unicast(a addr) bool {
return a.AFI == 2 && a.SAFI == 1
}