forked from osrg/gobgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
2145 lines (2004 loc) · 61.6 KB
/
global.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"net/netip"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
api "github.com/osrg/gobgp/v3/api"
"github.com/osrg/gobgp/v3/internal/pkg/table"
"github.com/osrg/gobgp/v3/pkg/apiutil"
"github.com/osrg/gobgp/v3/pkg/packet/bgp"
)
type extCommType int
const (
ctAccept extCommType = iota
ctDiscard
ctRate
ctRedirect
ctMark
ctAction
ctRT
ctEncap
ctESILabel
ctRouterMAC
ctDefaultGateway
ctValid
ctNotFound
ctInvalid
ctColor
ctLb
ctMup
)
var extCommNameMap = map[extCommType]string{
ctAccept: "accept",
ctDiscard: "discard",
ctRate: "rate-limit",
ctRedirect: "redirect",
ctMark: "mark",
ctAction: "action",
ctRT: "rt",
ctEncap: "encap",
ctESILabel: "esi-label",
ctRouterMAC: "router-mac",
ctDefaultGateway: "default-gateway",
ctValid: "valid",
ctNotFound: "not-found",
ctInvalid: "invalid",
ctColor: "color",
ctLb: "lb",
ctMup: "mup",
}
var extCommValueMap = map[string]extCommType{
extCommNameMap[ctAccept]: ctAccept,
extCommNameMap[ctDiscard]: ctDiscard,
extCommNameMap[ctRate]: ctRate,
extCommNameMap[ctRedirect]: ctRedirect,
extCommNameMap[ctMark]: ctMark,
extCommNameMap[ctAction]: ctAction,
extCommNameMap[ctRT]: ctRT,
extCommNameMap[ctEncap]: ctEncap,
extCommNameMap[ctESILabel]: ctESILabel,
extCommNameMap[ctRouterMAC]: ctRouterMAC,
extCommNameMap[ctDefaultGateway]: ctDefaultGateway,
extCommNameMap[ctValid]: ctValid,
extCommNameMap[ctNotFound]: ctNotFound,
extCommNameMap[ctInvalid]: ctInvalid,
extCommNameMap[ctColor]: ctColor,
extCommNameMap[ctLb]: ctLb,
extCommNameMap[ctMup]: ctMup,
}
func rateLimitParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
exp := regexp.MustCompile(fmt.Sprintf("^(%s|(%s) (\\d+)(\\.(\\d+))?)( as (\\d+))?$", extCommNameMap[ctDiscard], extCommNameMap[ctRate]))
elems := exp.FindStringSubmatch(strings.Join(args, " "))
if len(elems) != 8 {
return nil, fmt.Errorf("invalid rate-limit")
}
var rate float32
var as uint64
if elems[2] == extCommNameMap[ctRate] {
f, err := strconv.ParseFloat(elems[3]+elems[4], 32)
if err != nil {
return nil, err
}
rate = float32(f)
}
if elems[7] != "" {
var err error
as, err = strconv.ParseUint(elems[7], 10, 16)
if err != nil {
return nil, err
}
}
return []bgp.ExtendedCommunityInterface{bgp.NewTrafficRateExtended(uint16(as), rate)}, nil
}
func redirectParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctRedirect] {
return nil, fmt.Errorf("invalid redirect")
}
rt, err := bgp.ParseRouteTarget(strings.Join(args[1:], " "))
if err != nil {
return nil, err
}
switch r := rt.(type) {
case *bgp.TwoOctetAsSpecificExtended:
return []bgp.ExtendedCommunityInterface{bgp.NewRedirectTwoOctetAsSpecificExtended(r.AS, r.LocalAdmin)}, nil
case *bgp.IPv4AddressSpecificExtended:
return []bgp.ExtendedCommunityInterface{bgp.NewRedirectIPv4AddressSpecificExtended(r.IPv4.String(), r.LocalAdmin)}, nil
case *bgp.FourOctetAsSpecificExtended:
return []bgp.ExtendedCommunityInterface{bgp.NewRedirectFourOctetAsSpecificExtended(r.AS, r.LocalAdmin)}, nil
case *bgp.IPv6AddressSpecificExtended:
return []bgp.ExtendedCommunityInterface{bgp.NewRedirectIPv6AddressSpecificExtended(r.IPv6.String(), r.LocalAdmin)}, nil
}
return nil, fmt.Errorf("invalid redirect")
}
func markParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctMark] {
return nil, fmt.Errorf("invalid mark")
}
dscp, err := strconv.ParseUint(args[1], 10, 8)
if err != nil {
return nil, fmt.Errorf("invalid mark")
}
return []bgp.ExtendedCommunityInterface{bgp.NewTrafficRemarkExtended(uint8(dscp))}, nil
}
func actionParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctAction] {
return nil, fmt.Errorf("invalid action")
}
sample := false
terminal := false
switch args[1] {
case "sample":
sample = true
case "terminal":
terminal = true
case "terminal-sample", "sample-terminal":
sample = true
terminal = true
default:
return nil, fmt.Errorf("invalid action")
}
return []bgp.ExtendedCommunityInterface{bgp.NewTrafficActionExtended(terminal, sample)}, nil
}
func rtParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctRT] {
return nil, fmt.Errorf("invalid rt")
}
exts := make([]bgp.ExtendedCommunityInterface, 0, len(args[1:]))
for _, arg := range args[1:] {
rt, err := bgp.ParseRouteTarget(arg)
if err != nil {
return nil, err
}
exts = append(exts, rt)
}
return exts, nil
}
func encapParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctEncap] {
return nil, fmt.Errorf("invalid encap")
}
var typ bgp.TunnelType
switch args[1] {
case "l2tpv3":
typ = bgp.TUNNEL_TYPE_L2TP3
case "gre":
typ = bgp.TUNNEL_TYPE_GRE
case "ip-in-ip":
typ = bgp.TUNNEL_TYPE_IP_IN_IP
case "vxlan":
typ = bgp.TUNNEL_TYPE_VXLAN
case "nvgre":
typ = bgp.TUNNEL_TYPE_NVGRE
case "mpls":
typ = bgp.TUNNEL_TYPE_MPLS
case "mpls-in-gre":
typ = bgp.TUNNEL_TYPE_MPLS_IN_GRE
case "mpls-in-udp":
typ = bgp.TUNNEL_TYPE_MPLS_IN_UDP
case "vxlan-gre":
typ = bgp.TUNNEL_TYPE_VXLAN_GRE
case "geneve":
typ = bgp.TUNNEL_TYPE_GENEVE
default:
return nil, fmt.Errorf("invalid encap type")
}
return []bgp.ExtendedCommunityInterface{bgp.NewEncapExtended(typ)}, nil
}
func esiLabelParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctESILabel] {
return nil, fmt.Errorf("invalid esi-label")
}
label, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return nil, err
}
isSingleActive := false
if len(args) > 2 {
switch args[2] {
case "single-active":
isSingleActive = true
case "all-active":
// isSingleActive = false
default:
return nil, fmt.Errorf("invalid esi-label")
}
}
o := &bgp.ESILabelExtended{
Label: uint32(label),
IsSingleActive: isSingleActive,
}
return []bgp.ExtendedCommunityInterface{o}, nil
}
func routerMacParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 2 || args[0] != extCommNameMap[ctRouterMAC] {
return nil, fmt.Errorf("invalid router's mac")
}
hw, err := net.ParseMAC(args[1])
if err != nil {
return nil, err
}
o := &bgp.RouterMacExtended{Mac: hw}
return []bgp.ExtendedCommunityInterface{o}, nil
}
func defaultGatewayParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 1 || args[0] != extCommNameMap[ctDefaultGateway] {
return nil, fmt.Errorf("invalid default-gateway")
}
return []bgp.ExtendedCommunityInterface{bgp.NewDefaultGatewayExtended()}, nil
}
func validationParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) < 1 {
return nil, fmt.Errorf("invalid validation state")
}
var typ bgp.ValidationState
switch args[0] {
case "valid":
typ = bgp.VALIDATION_STATE_VALID
case "not-found":
typ = bgp.VALIDATION_STATE_NOT_FOUND
case "invalid":
typ = bgp.VALIDATION_STATE_INVALID
default:
return nil, fmt.Errorf("invalid validation state")
}
return []bgp.ExtendedCommunityInterface{bgp.NewValidationExtended(typ)}, nil
}
func colorParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) != 2 || args[0] != extCommNameMap[ctColor] {
return nil, fmt.Errorf("invalid color")
}
color, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid color")
}
return []bgp.ExtendedCommunityInterface{bgp.NewColorExtended(uint32(color))}, nil
}
func lbParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) != 2 || args[0] != extCommNameMap[ctLb] {
return nil, fmt.Errorf("invalid link-bandwidth")
}
as, err := strconv.ParseUint(args[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid lb ASN")
}
bw, err := strconv.ParseFloat(args[2], 32)
if err != nil {
return nil, fmt.Errorf("invalid lb bandwidth")
}
return []bgp.ExtendedCommunityInterface{bgp.NewLinkBandwidthExtended(uint16(as), float32(bw))}, nil
}
func mupParser(args []string) ([]bgp.ExtendedCommunityInterface, error) {
if len(args) != 2 || args[0] != extCommNameMap[ctMup] {
return nil, fmt.Errorf("invalid mup")
}
a := strings.Split(args[1], ":")
sid2, err := strconv.ParseUint(a[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid mup segment ID")
}
sid4, err := strconv.ParseUint(a[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid mup segment ID")
}
return []bgp.ExtendedCommunityInterface{bgp.NewMUPExtended(uint16(sid2), uint32(sid4))}, nil
}
var extCommParserMap = map[extCommType]func([]string) ([]bgp.ExtendedCommunityInterface, error){
ctAccept: nil,
ctDiscard: rateLimitParser,
ctRate: rateLimitParser,
ctRedirect: redirectParser,
ctMark: markParser,
ctAction: actionParser,
ctRT: rtParser,
ctEncap: encapParser,
ctESILabel: esiLabelParser,
ctRouterMAC: routerMacParser,
ctDefaultGateway: defaultGatewayParser,
ctValid: validationParser,
ctNotFound: validationParser,
ctInvalid: validationParser,
ctColor: colorParser,
ctLb: lbParser,
ctMup: mupParser,
}
func parseExtendedCommunities(args []string) ([]bgp.ExtendedCommunityInterface, error) {
idxs := make([]struct {
t extCommType
i int
}, 0, len(extCommNameMap))
for idx, v := range args {
if t, ok := extCommValueMap[v]; ok {
idxs = append(idxs, struct {
t extCommType
i int
}{t, idx})
}
}
exts := make([]bgp.ExtendedCommunityInterface, 0, len(idxs))
for i, idx := range idxs {
var a []string
f := extCommParserMap[idx.t]
if i < len(idxs)-1 {
a = args[:idxs[i+1].i-idx.i]
args = args[(idxs[i+1].i - idx.i):]
} else {
a = args
args = nil
}
if f == nil {
continue
}
ext, err := f(a)
if err != nil {
return nil, err
}
exts = append(exts, ext...)
}
if len(args) > 0 {
return nil, fmt.Errorf("failed to parse %v", args)
}
return exts, nil
}
func parseFlowSpecArgs(rf bgp.RouteFamily, args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// match <rule>... [then <action>...] [rd <rd>] [rt <rt>...]
req := 3 // match <key1> <arg1> [<key2> <arg2>...]
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"match": paramList,
"then": paramList,
"rd": paramSingle,
"rt": paramList})
if err != nil {
return nil, nil, err
}
if len(m["match"]) == 0 {
return nil, nil, fmt.Errorf("specify filtering rules with keyword 'match'")
}
var rd bgp.RouteDistinguisherInterface
extcomms := m["then"]
switch rf {
case bgp.RF_FS_IPv4_VPN, bgp.RF_FS_IPv6_VPN, bgp.RF_FS_L2_VPN:
if len(m["rd"]) == 0 {
return nil, nil, fmt.Errorf("specify rd")
}
var err error
if rd, err = bgp.ParseRouteDistinguisher(m["rd"][0]); err != nil {
return nil, nil, fmt.Errorf("invalid rd: %s", m["rd"][0])
}
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
default:
if len(m["rd"]) > 0 {
return nil, nil, fmt.Errorf("cannot specify rd for %s", rf.String())
}
if len(m["rt"]) > 0 {
return nil, nil, fmt.Errorf("cannot specify rt for %s", rf.String())
}
}
rules, err := bgp.ParseFlowSpecComponents(rf, strings.Join(m["match"], " "))
if err != nil {
return nil, nil, err
}
var nlri bgp.AddrPrefixInterface
switch rf {
case bgp.RF_FS_IPv4_UC:
nlri = bgp.NewFlowSpecIPv4Unicast(rules)
case bgp.RF_FS_IPv6_UC:
nlri = bgp.NewFlowSpecIPv6Unicast(rules)
case bgp.RF_FS_IPv4_VPN:
nlri = bgp.NewFlowSpecIPv4VPN(rd, rules)
case bgp.RF_FS_IPv6_VPN:
nlri = bgp.NewFlowSpecIPv6VPN(rd, rules)
case bgp.RF_FS_L2_VPN:
nlri = bgp.NewFlowSpecL2VPN(rd, rules)
default:
return nil, nil, fmt.Errorf("invalid route family")
}
return nlri, extcomms, nil
}
func parseEvpnEthernetAutoDiscoveryArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// esi <esi> etag <etag> label <label> rd <rd> [rt <rt>...] [encap <encap type>] [esi-label <esi-label> [single-active | all-active]]
req := 8
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"esi": paramList,
"etag": paramSingle,
"label": paramSingle,
"rd": paramSingle,
"rt": paramList,
"encap": paramSingle,
"esi-label": paramSingle})
if err != nil {
return nil, nil, err
}
for _, f := range []string{"esi", "etag", "label", "rd"} {
for len(m[f]) == 0 {
return nil, nil, fmt.Errorf("specify %s", f)
}
}
esi, err := bgp.ParseEthernetSegmentIdentifier(m["esi"])
if err != nil {
return nil, nil, err
}
e, err := strconv.ParseUint(m["etag"][0], 10, 32)
if err != nil {
return nil, nil, err
}
etag := uint32(e)
l, err := strconv.ParseUint(m["label"][0], 10, 32)
if err != nil {
return nil, nil, err
}
label := uint32(l)
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
if len(m["esi-label"]) > 0 {
extcomms = append(extcomms, "esi-label")
extcomms = append(extcomms, m["esi-label"]...)
}
r := &bgp.EVPNEthernetAutoDiscoveryRoute{
RD: rd,
ESI: esi,
ETag: etag,
Label: label,
}
return bgp.NewEVPNNLRI(bgp.EVPN_ROUTE_TYPE_ETHERNET_AUTO_DISCOVERY, r), extcomms, nil
}
func parseEvpnMacAdvArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// <mac address> <ip address> [esi <esi>] etag <etag> label <label> rd <rd> [rt <rt>...] [encap <encap type>] [router-mac <mac address>] [default-gateway]
// or
// <mac address> <ip address> <etag> [esi <esi>] label <label> rd <rd> [rt <rt>...] [encap <encap type>] [router-mac <mac address>] [default-gateway]
// or
// <mac address> <ip address> <etag> <label> [esi <esi>] rd <rd> [rt <rt>...] [encap <encap type>] [router-mac <mac address>] [default-gateway]
req := 6
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"esi": paramList,
"etag": paramSingle,
"label": paramSingle,
"rd": paramSingle,
"rt": paramList,
"encap": paramSingle,
"router-mac": paramSingle})
if err != nil {
return nil, nil, err
}
if len(m[""]) < 2 {
return nil, nil, fmt.Errorf("specify mac and ip address")
}
macStr := m[""][0]
ipStr := m[""][1]
eTagStr := ""
labelStr := ""
if len(m[""]) == 2 {
if len(m["etag"]) == 0 || len(m["label"]) == 0 {
return nil, nil, fmt.Errorf("specify etag and label")
}
eTagStr = m["etag"][0]
labelStr = m["label"][0]
} else if len(m[""]) == 3 {
if len(m["label"]) == 0 {
return nil, nil, fmt.Errorf("specify label")
}
eTagStr = m[""][2]
labelStr = m["label"][0]
} else {
eTagStr = m[""][2]
labelStr = m[""][3]
}
if len(m["rd"]) == 0 {
return nil, nil, fmt.Errorf("specify rd")
}
mac, err := net.ParseMAC(macStr)
if err != nil {
return nil, nil, fmt.Errorf("invalid mac address: %s", macStr)
}
ip := net.ParseIP(ipStr)
ipLen := 0
if ip == nil {
return nil, nil, fmt.Errorf("invalid ip address: %s", ipStr)
} else if ip.IsUnspecified() {
ip = nil
} else if ip.To4() != nil {
ipLen = net.IPv4len * 8
} else {
ipLen = net.IPv6len * 8
}
esi, err := bgp.ParseEthernetSegmentIdentifier(m["esi"])
if err != nil {
return nil, nil, err
}
eTag, err := strconv.ParseUint(eTagStr, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", eTagStr, err)
}
var labels []uint32
for _, l := range strings.SplitN(labelStr, ",", 2) {
label, err := strconv.ParseUint(l, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid label: %s: %s", labelStr, err)
}
labels = append(labels, uint32(label))
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
if len(m["router-mac"]) != 0 {
_, err := net.ParseMAC(m["router-mac"][0])
if err != nil {
return nil, nil, fmt.Errorf("invalid router-mac address: %s", m["router-mac"][0])
}
extcomms = append(extcomms, "router-mac", m["router-mac"][0])
}
for _, a := range args {
if a == "default-gateway" {
extcomms = append(extcomms, "default-gateway")
break
}
}
r := &bgp.EVPNMacIPAdvertisementRoute{
RD: rd,
ESI: esi,
MacAddressLength: 48,
MacAddress: mac,
IPAddressLength: uint8(ipLen),
IPAddress: ip,
Labels: labels,
ETag: uint32(eTag),
}
return bgp.NewEVPNNLRI(bgp.EVPN_ROUTE_TYPE_MAC_IP_ADVERTISEMENT, r), extcomms, nil
}
func parseEvpnMulticastArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// <ip address> etag <etag> rd <rd> [rt <rt>...] [encap <encap type>]
// or
// <ip address> <etag> rd <rd> [rt <rt>...] [encap <encap type>]
req := 4
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"etag": paramSingle,
"rd": paramSingle,
"rt": paramList,
"encap": paramSingle})
if err != nil {
return nil, nil, err
}
if len(m[""]) < 1 {
return nil, nil, fmt.Errorf("specify ip address")
}
ipStr := m[""][0]
eTagStr := ""
if len(m[""]) == 1 {
if len(m["etag"]) == 0 {
return nil, nil, fmt.Errorf("specify etag")
}
eTagStr = m["etag"][0]
} else {
eTagStr = m[""][1]
}
if len(m["rd"]) == 0 {
return nil, nil, fmt.Errorf("specify rd")
}
ip := net.ParseIP(ipStr)
ipLen := 0
if ip == nil {
return nil, nil, fmt.Errorf("invalid ip address: %s", ipStr)
} else if ip.IsUnspecified() {
ip = nil
} else if ip.To4() != nil {
ipLen = net.IPv4len * 8
} else {
ipLen = net.IPv6len * 8
}
eTag, err := strconv.ParseUint(eTagStr, 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", eTagStr, err)
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
r := &bgp.EVPNMulticastEthernetTagRoute{
RD: rd,
IPAddressLength: uint8(ipLen),
IPAddress: ip,
ETag: uint32(eTag),
}
return bgp.NewEVPNNLRI(bgp.EVPN_INCLUSIVE_MULTICAST_ETHERNET_TAG, r), extcomms, nil
}
func parseEvpnEthernetSegmentArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// <ip address> esi <esi> rd <rd> [rt <rt>...] [encap <encap type>]
req := 5
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"esi": paramList,
"rd": paramSingle,
"rt": paramList,
"encap": paramSingle})
if err != nil {
return nil, nil, err
}
if len(m[""]) < 1 {
return nil, nil, fmt.Errorf("specify ip address")
}
for _, f := range []string{"esi", "rd"} {
for len(m[f]) == 0 {
return nil, nil, fmt.Errorf("specify %s", f)
}
}
ip := net.ParseIP(m[""][0])
ipLen := 0
if ip == nil {
return nil, nil, fmt.Errorf("invalid ip address: %s", m[""][0])
} else if ip.IsUnspecified() {
ip = nil
} else if ip.To4() != nil {
ipLen = net.IPv4len * 8
} else {
ipLen = net.IPv6len * 8
}
esi, err := bgp.ParseEthernetSegmentIdentifier(m["esi"])
if err != nil {
return nil, nil, err
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
r := &bgp.EVPNEthernetSegmentRoute{
RD: rd,
ESI: esi,
IPAddressLength: uint8(ipLen),
IPAddress: ip,
}
return bgp.NewEVPNNLRI(bgp.EVPN_ETHERNET_SEGMENT_ROUTE, r), extcomms, nil
}
func parseEvpnIPPrefixArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// <ip prefix> [gw <gateway>] [esi <esi>] etag <etag> [label <label>] rd <rd> [rt <rt>...] [encap <encap type>]
req := 5
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"gw": paramSingle,
"esi": paramList,
"etag": paramSingle,
"label": paramSingle,
"rd": paramSingle,
"rt": paramList,
"encap": paramSingle,
"router-mac": paramSingle})
if err != nil {
return nil, nil, err
}
if len(m[""]) < 1 {
return nil, nil, fmt.Errorf("specify prefix")
}
for _, f := range []string{"etag", "rd"} {
for len(m[f]) == 0 {
return nil, nil, fmt.Errorf("specify %s", f)
}
}
_, nw, err := net.ParseCIDR(m[""][0])
if err != nil {
return nil, nil, err
}
ones, _ := nw.Mask.Size()
var gw net.IP
if len(m["gw"]) > 0 {
gw = net.ParseIP(m["gw"][0])
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
esi, err := bgp.ParseEthernetSegmentIdentifier(m["esi"])
if err != nil {
return nil, nil, err
}
e, err := strconv.ParseUint(m["etag"][0], 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", m["etag"][0], err)
}
etag := uint32(e)
var label uint32
if len(m["label"]) > 0 {
e, err := strconv.ParseUint(m["label"][0], 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid label: %s: %s", m["label"][0], err)
}
label = uint32(e)
}
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
if len(m["router-mac"]) > 0 {
extcomms = append(extcomms, "router-mac", m["router-mac"][0])
}
r := &bgp.EVPNIPPrefixRoute{
RD: rd,
ESI: esi,
ETag: etag,
IPPrefixLength: uint8(ones),
IPPrefix: nw.IP,
GWIPAddress: gw,
Label: label,
}
return bgp.NewEVPNNLRI(bgp.EVPN_IP_PREFIX, r), extcomms, nil
}
func parseEvpnIPMSIArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
// Format:
// etag <etag> rd <rd> [rt <rt>...] [encap <encap type>]
req := 4
if len(args) < req {
return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"etag": paramSingle,
"rd": paramSingle,
"rt": paramSingle,
"encap": paramSingle})
if err != nil {
return nil, nil, err
}
for _, f := range []string{"etag", "rd"} {
for len(m[f]) == 0 {
return nil, nil, fmt.Errorf("specify %s", f)
}
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, err
}
e, err := strconv.ParseUint(m["etag"][0], 10, 32)
if err != nil {
return nil, nil, fmt.Errorf("invalid etag: %s: %s", m["etag"][0], err)
}
etag := uint32(e)
extcomms := make([]string, 0)
if len(m["rt"]) > 0 {
extcomms = append(extcomms, "rt")
extcomms = append(extcomms, m["rt"]...)
}
ec, err := bgp.ParseExtendedCommunity(bgp.EC_SUBTYPE_SOURCE_AS, m["rt"][0])
if err != nil {
return nil, nil, fmt.Errorf("route target parse failed")
}
if len(m["encap"]) > 0 {
extcomms = append(extcomms, "encap", m["encap"][0])
}
r := &bgp.EVPNIPMSIRoute{
RD: rd,
ETag: etag,
EC: ec,
}
return bgp.NewEVPNNLRI(bgp.EVPN_I_PMSI, r), extcomms, nil
}
func parseEvpnArgs(args []string) (bgp.AddrPrefixInterface, []string, error) {
if len(args) < 1 {
return nil, nil, fmt.Errorf("lack of args. need 1 but %d", len(args))
}
subtype := args[0]
args = args[1:]
switch subtype {
case "a-d":
return parseEvpnEthernetAutoDiscoveryArgs(args)
case "macadv":
return parseEvpnMacAdvArgs(args)
case "multicast":
return parseEvpnMulticastArgs(args)
case "esi":
return parseEvpnEthernetSegmentArgs(args)
case "prefix":
return parseEvpnIPPrefixArgs(args)
case "i-pmsi":
return parseEvpnIPMSIArgs(args)
}
return nil, nil, fmt.Errorf("invalid subtype. expect [macadv|multicast|prefix] but %s", subtype)
}
func parseMUPInterworkSegmentDiscoveryRouteArgs(args []string, afi uint16, nexthop string) (bgp.AddrPrefixInterface, *bgp.PathAttributePrefixSID, []string, error) {
// Format:
// <ip prefix> rd <rd> prefix <prefix> locator-node-length <locator-node-length> function-length <function-length> behavior <behavior> [rt <rt>...]
req := 13
if len(args) < req {
return nil, nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args))
}
m, err := extractReserved(args, map[string]int{
"rd": paramSingle,
"prefix": paramSingle,
"locator-node-length": paramSingle,
"function-length": paramSingle,
"behavior": paramSingle,
"rt": paramSingle,
})
if err != nil {
return nil, nil, nil, err
}
if len(m[""]) < 1 {
return nil, nil, nil, fmt.Errorf("specify prefix")
}
for _, f := range []string{"rd", "prefix", "locator-node-length", "function-length", "rt"} {
for len(m[f]) == 0 {
return nil, nil, nil, fmt.Errorf("specify %s", f)
}
}
rd, err := bgp.ParseRouteDistinguisher(m["rd"][0])
if err != nil {
return nil, nil, nil, err
}
prefix, err := netip.ParsePrefix(m[""][0])
if err != nil {
return nil, nil, nil, err
}
nh, err := netip.ParseAddr(nexthop)
if err != nil {
return nil, nil, nil, err
}
if nh.Is4() {
return nil, nil, nil, fmt.Errorf("nexthop should be IPv6 address: %s", nexthop)
}
sid, err := netip.ParsePrefix(m["prefix"][0])
if err != nil {
return nil, nil, nil, err