-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_message.go
145 lines (134 loc) · 3.46 KB
/
update_message.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
package main
import (
"fmt"
"net"
)
type WithdrawnRoute struct {
AF AddressFamily
Prefix *net.IPNet
}
func UpdateMessageToRIBEntries(m UpdateMessage, source *Peer) ([]WithdrawnRoute, []*RIBEntry, error) {
var (
origin Origin
asPath ASPath
nextHop NextHop
others []PathAttribute
mpReach MPReachNLRI
mpUnreach MPUnreachNLRI
err error
)
for _, a := range m.PathAttributes {
switch a.TypeCode {
case AttributeTypeOrigin:
origin, err = OriginFromPathAttribute(a)
case AttributeTypeASPath:
asPath, err = ASPathFromPathAttribute(a)
case AttributeTypeNextHop:
nextHop, err = NextHopFromPathAttribute(a)
case AttributeTypeMPReachNLRI:
mpReach, err = MPReachNLRIFromPathAttribute(a)
case AttributeTypeMPUnreachNLRI:
mpUnreach, err = MPUnreachNLRIFromPathAttribute(a)
default:
others = append(others, a)
}
if err != nil {
return nil, nil, err
}
}
withdrawns := make([]WithdrawnRoute, 0, len(m.WirhdrawnRoutes)+len(mpUnreach.WithdrawnRoutes))
for _, r := range m.WirhdrawnRoutes {
withdrawns = append(withdrawns, WithdrawnRoute{
AF: IPv4Unicast,
Prefix: r,
})
}
for _, r := range mpUnreach.WithdrawnRoutes {
withdrawns = append(withdrawns, WithdrawnRoute{
AF: mpUnreach.AF,
Prefix: r,
})
}
entries := make([]*RIBEntry, 0, len(mpReach.NLRI)+len(m.NLRI))
for _, r := range mpReach.NLRI {
entries = append(entries, &RIBEntry{
AF: mpReach.AF,
Prefix: r,
Origin: origin,
ASPath: asPath,
NextHop: mpReach.NextHop[0], // TODO: Select best
OtherAttributes: others,
Source: source,
})
}
for _, r := range m.NLRI {
entries = append(entries, &RIBEntry{
AF: IPv4Unicast,
Prefix: r,
Origin: origin,
ASPath: asPath,
NextHop: net.IP(nextHop),
OtherAttributes: others, // TODO: Copy other attributes?
Source: source,
})
}
return withdrawns, entries, nil
}
func CreateWithdrawnMessage(r *net.IPNet) UpdateMessage {
switch len(r.IP) {
case 4: // IPv4
return UpdateMessage{
WirhdrawnRoutes: []*net.IPNet{r},
}
case 16: // IPv6
return UpdateMessage{
PathAttributes: []PathAttribute{
MPUnreachNLRI{
AF: IPv6Unicast,
WithdrawnRoutes: []*net.IPNet{r},
}.ToPathAttribute(),
},
}
default:
panic(fmt.Errorf("unexpected withdrawn prefix: %v", r))
}
}
func CreateUpdateMessage(e *RIBEntry, prependAS uint16, selfNextHop net.IP) UpdateMessage {
nextHop := e.NextHop
if nextHop == nil {
nextHop = selfNextHop
}
switch len(e.Prefix.IP) {
case 4:
pathAttributes := []PathAttribute{
e.Origin.ToPathAttribute(),
ASPath{
Sequence: e.ASPath.Sequence,
Segments: append([]uint16{prependAS}, e.ASPath.Segments...),
}.ToPathAttribute(),
NextHop(nextHop).ToPathAttribute(),
}
return UpdateMessage{
PathAttributes: append(pathAttributes, e.OtherAttributes...),
NLRI: []*net.IPNet{e.Prefix},
}
case 16:
pathAttributes := []PathAttribute{
e.Origin.ToPathAttribute(),
ASPath{
Sequence: e.ASPath.Sequence,
Segments: append([]uint16{prependAS}, e.ASPath.Segments...),
}.ToPathAttribute(),
MPReachNLRI{
AF: IPv6Unicast,
NextHop: []net.IP{nextHop},
NLRI: []*net.IPNet{e.Prefix},
}.ToPathAttribute(),
}
return UpdateMessage{
PathAttributes: append(pathAttributes, e.OtherAttributes...),
}
default:
panic(fmt.Errorf("unexpected rib entry: %v", e))
}
}