-
Notifications
You must be signed in to change notification settings - Fork 77
/
bgp_vty.c
18495 lines (16155 loc) · 541 KB
/
bgp_vty.c
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
/* BGP VTY interface.
* Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "command.h"
#include "lib/json.h"
#include "lib_errors.h"
#include "lib/zclient.h"
#include "prefix.h"
#include "plist.h"
#include "buffer.h"
#include "linklist.h"
#include "stream.h"
#include "thread.h"
#include "log.h"
#include "memory.h"
#include "lib_vty.h"
#include "hash.h"
#include "queue.h"
#include "filter.h"
#include "frrstr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_attr_evpn.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_lcommunity.h"
#include "bgpd/bgp_damp.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_open.h"
#include "bgpd/bgp_regex.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_io.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_evpn_vty.h"
#include "bgpd/bgp_evpn_mh.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_mac.h"
#include "bgpd/bgp_flowspec.h"
#ifdef ENABLE_BGP_VNC
#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#endif
FRR_CFG_DEFAULT_BOOL(BGP_IMPORT_CHECK,
{
.val_bool = false,
.match_profile = "traditional",
.match_version = "< 7.4",
},
{ .val_bool = true },
)
FRR_CFG_DEFAULT_BOOL(BGP_SHOW_HOSTNAME,
{ .val_bool = true, .match_profile = "datacenter", },
{ .val_bool = false },
)
FRR_CFG_DEFAULT_BOOL(BGP_SHOW_NEXTHOP_HOSTNAME,
{ .val_bool = true, .match_profile = "datacenter", },
{ .val_bool = false },
)
FRR_CFG_DEFAULT_BOOL(BGP_LOG_NEIGHBOR_CHANGES,
{ .val_bool = true, .match_profile = "datacenter", },
{ .val_bool = false },
)
FRR_CFG_DEFAULT_BOOL(BGP_DETERMINISTIC_MED,
{ .val_bool = true, .match_profile = "datacenter", },
{ .val_bool = false },
)
FRR_CFG_DEFAULT_ULONG(BGP_CONNECT_RETRY,
{ .val_ulong = 10, .match_profile = "datacenter", },
{ .val_ulong = 120 },
)
FRR_CFG_DEFAULT_ULONG(BGP_HOLDTIME,
{ .val_ulong = 9, .match_profile = "datacenter", },
{ .val_ulong = 180 },
)
FRR_CFG_DEFAULT_ULONG(BGP_KEEPALIVE,
{ .val_ulong = 3, .match_profile = "datacenter", },
{ .val_ulong = 60 },
)
FRR_CFG_DEFAULT_BOOL(BGP_EBGP_REQUIRES_POLICY,
{ .val_bool = false, .match_profile = "datacenter", },
{ .val_bool = false, .match_version = "< 7.4", },
{ .val_bool = true },
)
DEFINE_HOOK(bgp_inst_config_write,
(struct bgp *bgp, struct vty *vty),
(bgp, vty))
#define GR_NO_OPER \
"The Graceful Restart No Operation was executed as cmd same as previous one."
#define GR_INVALID \
"The Graceful Restart command used is not valid at this moment."
static struct peer_group *listen_range_exists(struct bgp *bgp,
struct prefix *range, int exact);
/* Show BGP peer's information. */
enum show_type {
show_all,
show_peer,
show_ipv4_all,
show_ipv6_all,
show_ipv4_peer,
show_ipv6_peer
};
static struct peer_group *listen_range_exists(struct bgp *bgp,
struct prefix *range, int exact);
static void bgp_show_global_graceful_restart_mode_vty(struct vty *vty,
struct bgp *bgp,
bool use_json,
json_object *json);
static int bgp_show_neighbor_graceful_restart_afi_all(struct vty *vty,
enum show_type type,
const char *ip_str,
afi_t afi, bool use_json);
static enum node_type bgp_node_type(afi_t afi, safi_t safi)
{
switch (afi) {
case AFI_IP:
switch (safi) {
case SAFI_UNICAST:
return BGP_IPV4_NODE;
case SAFI_MULTICAST:
return BGP_IPV4M_NODE;
case SAFI_LABELED_UNICAST:
return BGP_IPV4L_NODE;
case SAFI_MPLS_VPN:
return BGP_VPNV4_NODE;
case SAFI_FLOWSPEC:
return BGP_FLOWSPECV4_NODE;
default:
/* not expected */
return BGP_IPV4_NODE;
}
break;
case AFI_IP6:
switch (safi) {
case SAFI_UNICAST:
return BGP_IPV6_NODE;
case SAFI_MULTICAST:
return BGP_IPV6M_NODE;
case SAFI_LABELED_UNICAST:
return BGP_IPV6L_NODE;
case SAFI_MPLS_VPN:
return BGP_VPNV6_NODE;
case SAFI_FLOWSPEC:
return BGP_FLOWSPECV6_NODE;
default:
/* not expected */
return BGP_IPV4_NODE;
}
break;
case AFI_L2VPN:
return BGP_EVPN_NODE;
case AFI_UNSPEC:
case AFI_MAX:
// We should never be here but to clarify the switch statement..
return BGP_IPV4_NODE;
}
// Impossible to happen
return BGP_IPV4_NODE;
}
static const char *get_afi_safi_vty_str(afi_t afi, safi_t safi)
{
if (afi == AFI_IP && safi == SAFI_UNICAST)
return "IPv4 Unicast";
else if (afi == AFI_IP && safi == SAFI_MULTICAST)
return "IPv4 Multicast";
else if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST)
return "IPv4 Labeled Unicast";
else if (afi == AFI_IP && safi == SAFI_MPLS_VPN)
return "IPv4 VPN";
else if (afi == AFI_IP && safi == SAFI_ENCAP)
return "IPv4 Encap";
else if (afi == AFI_IP && safi == SAFI_FLOWSPEC)
return "IPv4 Flowspec";
else if (afi == AFI_IP6 && safi == SAFI_UNICAST)
return "IPv6 Unicast";
else if (afi == AFI_IP6 && safi == SAFI_MULTICAST)
return "IPv6 Multicast";
else if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST)
return "IPv6 Labeled Unicast";
else if (afi == AFI_IP6 && safi == SAFI_MPLS_VPN)
return "IPv6 VPN";
else if (afi == AFI_IP6 && safi == SAFI_ENCAP)
return "IPv6 Encap";
else if (afi == AFI_IP6 && safi == SAFI_FLOWSPEC)
return "IPv6 Flowspec";
else if (afi == AFI_L2VPN && safi == SAFI_EVPN)
return "L2VPN EVPN";
else
return "Unknown";
}
/*
* Please note that we have intentionally camelCased
* the return strings here. So if you want
* to use this function, please ensure you
* are doing this within json output
*/
static const char *get_afi_safi_json_str(afi_t afi, safi_t safi)
{
if (afi == AFI_IP && safi == SAFI_UNICAST)
return "ipv4Unicast";
else if (afi == AFI_IP && safi == SAFI_MULTICAST)
return "ipv4Multicast";
else if (afi == AFI_IP && safi == SAFI_LABELED_UNICAST)
return "ipv4LabeledUnicast";
else if (afi == AFI_IP && safi == SAFI_MPLS_VPN)
return "ipv4Vpn";
else if (afi == AFI_IP && safi == SAFI_ENCAP)
return "ipv4Encap";
else if (afi == AFI_IP && safi == SAFI_FLOWSPEC)
return "ipv4Flowspec";
else if (afi == AFI_IP6 && safi == SAFI_UNICAST)
return "ipv6Unicast";
else if (afi == AFI_IP6 && safi == SAFI_MULTICAST)
return "ipv6Multicast";
else if (afi == AFI_IP6 && safi == SAFI_LABELED_UNICAST)
return "ipv6LabeledUnicast";
else if (afi == AFI_IP6 && safi == SAFI_MPLS_VPN)
return "ipv6Vpn";
else if (afi == AFI_IP6 && safi == SAFI_ENCAP)
return "ipv6Encap";
else if (afi == AFI_IP6 && safi == SAFI_FLOWSPEC)
return "ipv6Flowspec";
else if (afi == AFI_L2VPN && safi == SAFI_EVPN)
return "l2VpnEvpn";
else
return "Unknown";
}
/* Utility function to get address family from current node. */
afi_t bgp_node_afi(struct vty *vty)
{
afi_t afi;
switch (vty->node) {
case BGP_IPV6_NODE:
case BGP_IPV6M_NODE:
case BGP_IPV6L_NODE:
case BGP_VPNV6_NODE:
case BGP_FLOWSPECV6_NODE:
afi = AFI_IP6;
break;
case BGP_EVPN_NODE:
afi = AFI_L2VPN;
break;
default:
afi = AFI_IP;
break;
}
return afi;
}
/* Utility function to get subsequent address family from current
node. */
safi_t bgp_node_safi(struct vty *vty)
{
safi_t safi;
switch (vty->node) {
case BGP_VPNV4_NODE:
case BGP_VPNV6_NODE:
safi = SAFI_MPLS_VPN;
break;
case BGP_IPV4M_NODE:
case BGP_IPV6M_NODE:
safi = SAFI_MULTICAST;
break;
case BGP_EVPN_NODE:
safi = SAFI_EVPN;
break;
case BGP_IPV4L_NODE:
case BGP_IPV6L_NODE:
safi = SAFI_LABELED_UNICAST;
break;
case BGP_FLOWSPECV4_NODE:
case BGP_FLOWSPECV6_NODE:
safi = SAFI_FLOWSPEC;
break;
default:
safi = SAFI_UNICAST;
break;
}
return safi;
}
/**
* Converts an AFI in string form to afi_t
*
* @param afi string, one of
* - "ipv4"
* - "ipv6"
* - "l2vpn"
* @return the corresponding afi_t
*/
afi_t bgp_vty_afi_from_str(const char *afi_str)
{
afi_t afi = AFI_MAX; /* unknown */
if (strmatch(afi_str, "ipv4"))
afi = AFI_IP;
else if (strmatch(afi_str, "ipv6"))
afi = AFI_IP6;
else if (strmatch(afi_str, "l2vpn"))
afi = AFI_L2VPN;
return afi;
}
int argv_find_and_parse_afi(struct cmd_token **argv, int argc, int *index,
afi_t *afi)
{
int ret = 0;
if (argv_find(argv, argc, "ipv4", index)) {
ret = 1;
if (afi)
*afi = AFI_IP;
} else if (argv_find(argv, argc, "ipv6", index)) {
ret = 1;
if (afi)
*afi = AFI_IP6;
} else if (argv_find(argv, argc, "l2vpn", index)) {
ret = 1;
if (afi)
*afi = AFI_L2VPN;
}
return ret;
}
/* supports <unicast|multicast|vpn|labeled-unicast> */
safi_t bgp_vty_safi_from_str(const char *safi_str)
{
safi_t safi = SAFI_MAX; /* unknown */
if (strmatch(safi_str, "multicast"))
safi = SAFI_MULTICAST;
else if (strmatch(safi_str, "unicast"))
safi = SAFI_UNICAST;
else if (strmatch(safi_str, "vpn"))
safi = SAFI_MPLS_VPN;
else if (strmatch(safi_str, "evpn"))
safi = SAFI_EVPN;
else if (strmatch(safi_str, "labeled-unicast"))
safi = SAFI_LABELED_UNICAST;
else if (strmatch(safi_str, "flowspec"))
safi = SAFI_FLOWSPEC;
return safi;
}
int argv_find_and_parse_safi(struct cmd_token **argv, int argc, int *index,
safi_t *safi)
{
int ret = 0;
if (argv_find(argv, argc, "unicast", index)) {
ret = 1;
if (safi)
*safi = SAFI_UNICAST;
} else if (argv_find(argv, argc, "multicast", index)) {
ret = 1;
if (safi)
*safi = SAFI_MULTICAST;
} else if (argv_find(argv, argc, "labeled-unicast", index)) {
ret = 1;
if (safi)
*safi = SAFI_LABELED_UNICAST;
} else if (argv_find(argv, argc, "vpn", index)) {
ret = 1;
if (safi)
*safi = SAFI_MPLS_VPN;
} else if (argv_find(argv, argc, "evpn", index)) {
ret = 1;
if (safi)
*safi = SAFI_EVPN;
} else if (argv_find(argv, argc, "flowspec", index)) {
ret = 1;
if (safi)
*safi = SAFI_FLOWSPEC;
}
return ret;
}
int bgp_get_vty(struct bgp **bgp, as_t *as, const char *name,
enum bgp_instance_type inst_type)
{
int ret = bgp_get(bgp, as, name, inst_type);
if (ret == BGP_CREATED) {
bgp_timers_set(*bgp, DFLT_BGP_KEEPALIVE, DFLT_BGP_HOLDTIME,
DFLT_BGP_CONNECT_RETRY);
if (DFLT_BGP_IMPORT_CHECK)
SET_FLAG((*bgp)->flags, BGP_FLAG_IMPORT_CHECK);
if (DFLT_BGP_SHOW_HOSTNAME)
SET_FLAG((*bgp)->flags, BGP_FLAG_SHOW_HOSTNAME);
if (DFLT_BGP_SHOW_NEXTHOP_HOSTNAME)
SET_FLAG((*bgp)->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME);
if (DFLT_BGP_LOG_NEIGHBOR_CHANGES)
SET_FLAG((*bgp)->flags, BGP_FLAG_LOG_NEIGHBOR_CHANGES);
if (DFLT_BGP_DETERMINISTIC_MED)
SET_FLAG((*bgp)->flags, BGP_FLAG_DETERMINISTIC_MED);
if (DFLT_BGP_EBGP_REQUIRES_POLICY)
SET_FLAG((*bgp)->flags, BGP_FLAG_EBGP_REQUIRES_POLICY);
ret = BGP_SUCCESS;
}
return ret;
}
/*
* bgp_vty_find_and_parse_afi_safi_bgp
*
* For a given 'show ...' command, correctly parse the afi/safi/bgp out from it
* This function *assumes* that the calling function pre-sets the afi/safi/bgp
* to appropriate values for the calling function. This is to allow the
* calling function to make decisions appropriate for the show command
* that is being parsed.
*
* The show commands are generally of the form:
* "show [ip] bgp [<view|vrf> VIEWVRFNAME] [<ipv4|ipv6>
* [<unicast|multicast|vpn|labeled-unicast>]] ..."
*
* Since we use argv_find if the show command in particular doesn't have:
* [ip]
* [<view|vrf> VIEWVRFNAME]
* [<ipv4|ipv6> [<unicast|multicast|vpn|labeled-unicast>]]
* The command parsing should still be ok.
*
* vty -> The vty for the command so we can output some useful data in
* the event of a parse error in the vrf.
* argv -> The command tokens
* argc -> How many command tokens we have
* idx -> The current place in the command, generally should be 0 for this
* function
* afi -> The parsed afi if it was included in the show command, returned here
* safi -> The parsed safi if it was included in the show command, returned here
* bgp -> Pointer to the bgp data structure we need to fill in.
* use_json -> json is configured or not
*
* The function returns the correct location in the parse tree for the
* last token found.
*
* Returns 0 for failure to parse correctly, else the idx position of where
* it found the last token.
*/
int bgp_vty_find_and_parse_afi_safi_bgp(struct vty *vty,
struct cmd_token **argv, int argc,
int *idx, afi_t *afi, safi_t *safi,
struct bgp **bgp, bool use_json)
{
char *vrf_name = NULL;
assert(afi);
assert(safi);
assert(bgp);
if (argv_find(argv, argc, "ip", idx))
*afi = AFI_IP;
if (argv_find(argv, argc, "view", idx))
vrf_name = argv[*idx + 1]->arg;
else if (argv_find(argv, argc, "vrf", idx)) {
vrf_name = argv[*idx + 1]->arg;
if (strmatch(vrf_name, VRF_DEFAULT_NAME))
vrf_name = NULL;
}
if (vrf_name) {
if (strmatch(vrf_name, "all"))
*bgp = NULL;
else {
*bgp = bgp_lookup_by_name(vrf_name);
if (!*bgp) {
if (use_json) {
json_object *json = NULL;
json = json_object_new_object();
json_object_string_add(
json, "warning",
"View/Vrf is unknown");
vty_out(vty, "%s\n",
json_object_to_json_string_ext(json,
JSON_C_TO_STRING_PRETTY));
json_object_free(json);
}
else
vty_out(vty, "View/Vrf %s is unknown\n",
vrf_name);
*idx = 0;
return 0;
}
}
} else {
*bgp = bgp_get_default();
if (!*bgp) {
if (use_json) {
json_object *json = NULL;
json = json_object_new_object();
json_object_string_add(
json, "warning",
"Default BGP instance not found");
vty_out(vty, "%s\n",
json_object_to_json_string_ext(json,
JSON_C_TO_STRING_PRETTY));
json_object_free(json);
}
else
vty_out(vty,
"Default BGP instance not found\n");
*idx = 0;
return 0;
}
}
if (argv_find_and_parse_afi(argv, argc, idx, afi))
argv_find_and_parse_safi(argv, argc, idx, safi);
*idx += 1;
return *idx;
}
static bool peer_address_self_check(struct bgp *bgp, union sockunion *su)
{
struct interface *ifp = NULL;
if (su->sa.sa_family == AF_INET)
ifp = if_lookup_by_ipv4_exact(&su->sin.sin_addr, bgp->vrf_id);
else if (su->sa.sa_family == AF_INET6)
ifp = if_lookup_by_ipv6_exact(&su->sin6.sin6_addr,
su->sin6.sin6_scope_id,
bgp->vrf_id);
if (ifp)
return true;
return false;
}
/* Utility function for looking up peer from VTY. */
/* This is used only for configuration, so disallow if attempted on
* a dynamic neighbor.
*/
static struct peer *peer_lookup_vty(struct vty *vty, const char *ip_str)
{
struct bgp *bgp = VTY_GET_CONTEXT(bgp);
int ret;
union sockunion su;
struct peer *peer;
if (!bgp) {
return NULL;
}
ret = str2sockunion(ip_str, &su);
if (ret < 0) {
peer = peer_lookup_by_conf_if(bgp, ip_str);
if (!peer) {
if ((peer = peer_lookup_by_hostname(bgp, ip_str))
== NULL) {
vty_out(vty,
"%% Malformed address or name: %s\n",
ip_str);
return NULL;
}
}
} else {
peer = peer_lookup(bgp, &su);
if (!peer) {
vty_out(vty,
"%% Specify remote-as or peer-group commands first\n");
return NULL;
}
if (peer_dynamic_neighbor(peer)) {
vty_out(vty,
"%% Operation not allowed on a dynamic neighbor\n");
return NULL;
}
}
return peer;
}
/* Utility function for looking up peer or peer group. */
/* This is used only for configuration, so disallow if attempted on
* a dynamic neighbor.
*/
struct peer *peer_and_group_lookup_vty(struct vty *vty, const char *peer_str)
{
struct bgp *bgp = VTY_GET_CONTEXT(bgp);
int ret;
union sockunion su;
struct peer *peer = NULL;
struct peer_group *group = NULL;
if (!bgp) {
return NULL;
}
ret = str2sockunion(peer_str, &su);
if (ret == 0) {
/* IP address, locate peer. */
peer = peer_lookup(bgp, &su);
} else {
/* Not IP, could match either peer configured on interface or a
* group. */
peer = peer_lookup_by_conf_if(bgp, peer_str);
if (!peer)
group = peer_group_lookup(bgp, peer_str);
}
if (peer) {
if (peer_dynamic_neighbor(peer)) {
vty_out(vty,
"%% Operation not allowed on a dynamic neighbor\n");
return NULL;
}
return peer;
}
if (group)
return group->conf;
vty_out(vty, "%% Specify remote-as or peer-group commands first\n");
return NULL;
}
int bgp_vty_return(struct vty *vty, int ret)
{
const char *str = NULL;
switch (ret) {
case BGP_ERR_INVALID_VALUE:
str = "Invalid value";
break;
case BGP_ERR_INVALID_FLAG:
str = "Invalid flag";
break;
case BGP_ERR_PEER_GROUP_SHUTDOWN:
str = "Peer-group has been shutdown. Activate the peer-group first";
break;
case BGP_ERR_PEER_FLAG_CONFLICT:
str = "Can't set override-capability and strict-capability-match at the same time";
break;
case BGP_ERR_PEER_GROUP_NO_REMOTE_AS:
str = "Specify remote-as or peer-group remote AS first";
break;
case BGP_ERR_PEER_GROUP_CANT_CHANGE:
str = "Cannot change the peer-group. Deconfigure first";
break;
case BGP_ERR_PEER_GROUP_MISMATCH:
str = "Peer is not a member of this peer-group";
break;
case BGP_ERR_PEER_FILTER_CONFLICT:
str = "Prefix/distribute list can not co-exist";
break;
case BGP_ERR_NOT_INTERNAL_PEER:
str = "Invalid command. Not an internal neighbor";
break;
case BGP_ERR_REMOVE_PRIVATE_AS:
str = "remove-private-AS cannot be configured for IBGP peers";
break;
case BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP:
str = "Local-AS allowed only for EBGP peers";
break;
case BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS:
str = "Cannot have local-as same as BGP AS number";
break;
case BGP_ERR_TCPSIG_FAILED:
str = "Error while applying TCP-Sig to session(s)";
break;
case BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK:
str = "ebgp-multihop and ttl-security cannot be configured together";
break;
case BGP_ERR_NO_IBGP_WITH_TTLHACK:
str = "ttl-security only allowed for EBGP peers";
break;
case BGP_ERR_AS_OVERRIDE:
str = "as-override cannot be configured for IBGP peers";
break;
case BGP_ERR_INVALID_DYNAMIC_NEIGHBORS_LIMIT:
str = "Invalid limit for number of dynamic neighbors";
break;
case BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_EXISTS:
str = "Dynamic neighbor listen range already exists";
break;
case BGP_ERR_INVALID_FOR_DYNAMIC_PEER:
str = "Operation not allowed on a dynamic neighbor";
break;
case BGP_ERR_INVALID_FOR_DIRECT_PEER:
str = "Operation not allowed on a directly connected neighbor";
break;
case BGP_ERR_PEER_SAFI_CONFLICT:
str = "Cannot activate peer for both 'ipv4 unicast' and 'ipv4 labeled-unicast'";
break;
case BGP_ERR_GR_INVALID_CMD:
str = "The Graceful Restart command used is not valid at this moment.";
break;
case BGP_ERR_GR_OPERATION_FAILED:
str = "The Graceful Restart Operation failed due to an err.";
break;
}
if (str) {
vty_out(vty, "%% %s\n", str);
return CMD_WARNING_CONFIG_FAILED;
}
return CMD_SUCCESS;
}
/* BGP clear sort. */
enum clear_sort {
clear_all,
clear_peer,
clear_group,
clear_external,
clear_as
};
static void bgp_clear_vty_error(struct vty *vty, struct peer *peer, afi_t afi,
safi_t safi, int error)
{
switch (error) {
case BGP_ERR_AF_UNCONFIGURED:
vty_out(vty,
"%%BGP: Enable %s address family for the neighbor %s\n",
get_afi_safi_str(afi, safi, false), peer->host);
break;
case BGP_ERR_SOFT_RECONFIG_UNCONFIGURED:
vty_out(vty,
"%%BGP: Inbound soft reconfig for %s not possible as it\n has neither refresh capability, nor inbound soft reconfig\n",
peer->host);
break;
default:
break;
}
}
static int bgp_peer_clear(struct peer *peer, afi_t afi, safi_t safi,
struct listnode **nnode, enum bgp_clear_type stype)
{
int ret = 0;
/* if afi/.safi not specified, spin thru all of them */
if ((afi == AFI_UNSPEC) && (safi == SAFI_UNSPEC)) {
afi_t tmp_afi;
safi_t tmp_safi;
FOREACH_AFI_SAFI (tmp_afi, tmp_safi) {
if (!peer->afc[tmp_afi][tmp_safi])
continue;
if (stype == BGP_CLEAR_SOFT_NONE)
ret = peer_clear(peer, nnode);
else
ret = peer_clear_soft(peer, tmp_afi, tmp_safi,
stype);
}
/* if afi specified and safi not, spin thru safis on this afi */
} else if (safi == SAFI_UNSPEC) {
safi_t tmp_safi;
for (tmp_safi = SAFI_UNICAST;
tmp_safi < SAFI_MAX; tmp_safi++) {
if (!peer->afc[afi][tmp_safi])
continue;
if (stype == BGP_CLEAR_SOFT_NONE)
ret = peer_clear(peer, nnode);
else
ret = peer_clear_soft(peer, afi,
tmp_safi, stype);
}
/* both afi/safi specified, let the caller know if not defined */
} else {
if (!peer->afc[afi][safi])
return 1;
if (stype == BGP_CLEAR_SOFT_NONE)
ret = peer_clear(peer, nnode);
else
ret = peer_clear_soft(peer, afi, safi, stype);
}
return ret;
}
/* `clear ip bgp' functions. */
static int bgp_clear(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
enum clear_sort sort, enum bgp_clear_type stype,
const char *arg)
{
int ret = 0;
bool found = false;
struct peer *peer;
VTY_BGP_GR_DEFINE_LOOP_VARIABLE;
/* Clear all neighbors. */
/*
* Pass along pointer to next node to peer_clear() when walking all
* nodes on the BGP instance as that may get freed if it is a
* doppelganger
*/
if (sort == clear_all) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
bgp_peer_gr_flags_update(peer);
if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
gr_router_detected = true;
ret = bgp_peer_clear(peer, afi, safi, &nnode,
stype);
if (ret < 0)
bgp_clear_vty_error(vty, peer, afi, safi, ret);
}
if (gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
bgp_zebra_send_capabilities(bgp, false);
} else if (!gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
bgp_zebra_send_capabilities(bgp, true);
}
/* This is to apply read-only mode on this clear. */
if (stype == BGP_CLEAR_SOFT_NONE)
bgp->update_delay_over = 0;
return CMD_SUCCESS;
}
/* Clear specified neighbor. */
if (sort == clear_peer) {
union sockunion su;
/* Make sockunion for lookup. */
ret = str2sockunion(arg, &su);
if (ret < 0) {
peer = peer_lookup_by_conf_if(bgp, arg);
if (!peer) {
peer = peer_lookup_by_hostname(bgp, arg);
if (!peer) {
vty_out(vty,
"Malformed address or name: %s\n",
arg);
return CMD_WARNING;
}
}
} else {
peer = peer_lookup(bgp, &su);
if (!peer) {
vty_out(vty,
"%%BGP: Unknown neighbor - \"%s\"\n",
arg);
return CMD_WARNING;
}
}
VTY_BGP_GR_ROUTER_DETECT(bgp, peer, peer->bgp->peer);
VTY_SEND_BGP_GR_CAPABILITY_TO_ZEBRA(peer->bgp, ret);
ret = bgp_peer_clear(peer, afi, safi, NULL, stype);
/* if afi/safi not defined for this peer, let caller know */
if (ret == 1)
ret = BGP_ERR_AF_UNCONFIGURED;
if (ret < 0)
bgp_clear_vty_error(vty, peer, afi, safi, ret);
return CMD_SUCCESS;
}
/* Clear all neighbors belonging to a specific peer-group. */
if (sort == clear_group) {
struct peer_group *group;
group = peer_group_lookup(bgp, arg);
if (!group) {
vty_out(vty, "%%BGP: No such peer-group %s\n", arg);
return CMD_WARNING;
}
for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
ret = bgp_peer_clear(peer, afi, safi, &nnode, stype);
if (ret < 0)
bgp_clear_vty_error(vty, peer, afi, safi, ret);
else
found = true;
}
if (!found)
vty_out(vty,
"%%BGP: No %s peer belonging to peer-group %s is configured\n",
get_afi_safi_str(afi, safi, false), arg);
return CMD_SUCCESS;
}
/* Clear all external (eBGP) neighbors. */
if (sort == clear_external) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->sort == BGP_PEER_IBGP)
continue;
bgp_peer_gr_flags_update(peer);
if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
gr_router_detected = true;
ret = bgp_peer_clear(peer, afi, safi, &nnode, stype);
if (ret < 0)
bgp_clear_vty_error(vty, peer, afi, safi, ret);
else
found = true;
}
if (gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
bgp_zebra_send_capabilities(bgp, false);
} else if (!gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
bgp_zebra_send_capabilities(bgp, true);
}
if (!found)
vty_out(vty,
"%%BGP: No external %s peer is configured\n",
get_afi_safi_str(afi, safi, false));
return CMD_SUCCESS;
}
/* Clear all neighbors belonging to a specific AS. */
if (sort == clear_as) {
as_t as = strtoul(arg, NULL, 10);
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->as != as)
continue;
bgp_peer_gr_flags_update(peer);
if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
gr_router_detected = true;
ret = bgp_peer_clear(peer, afi, safi, &nnode, stype);
if (ret < 0)
bgp_clear_vty_error(vty, peer, afi, safi, ret);
else
found = true;
}
if (gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
bgp_zebra_send_capabilities(bgp, false);
} else if (!gr_router_detected
&& bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
bgp_zebra_send_capabilities(bgp, true);
}
if (!found)
vty_out(vty,