-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
bgpd.c
8886 lines (7505 loc) · 245 KB
/
bgpd.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
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP-4, BGP-4+ daemon program
* Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
*/
#include <zebra.h>
#include "prefix.h"
#include "frrevent.h"
#include "buffer.h"
#include "stream.h"
#include "ringbuf.h"
#include "command.h"
#include "sockunion.h"
#include "sockopt.h"
#include "network.h"
#include "memory.h"
#include "filter.h"
#include "routemap.h"
#include "log.h"
#include "plist.h"
#include "linklist.h"
#include "workqueue.h"
#include "queue.h"
#include "zclient.h"
#include "bfd.h"
#include "hash.h"
#include "jhash.h"
#include "table.h"
#include "lib/json.h"
#include "lib/sockopt.h"
#include "frr_pthread.h"
#include "bitfield.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_dump.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_community_alias.h"
#include "bgpd/bgp_conditional_adv.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_regex.h"
#include "bgpd/bgp_clist.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_open.h"
#include "bgpd/bgp_filter.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_damp.h"
#include "bgpd/bgp_mplsvpn.h"
#ifdef ENABLE_BGP_VNC
#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#include "bgpd/rfapi/rfapi_backend.h"
#endif
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_network.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_nhg.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_memory.h"
#include "bgpd/bgp_evpn_vty.h"
#include "bgpd/bgp_keepalives.h"
#include "bgpd/bgp_io.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_flowspec.h"
#include "bgpd/bgp_labelpool.h"
#include "bgpd/bgp_pbr.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_evpn_private.h"
#include "bgpd/bgp_evpn_mh.h"
#include "bgpd/bgp_mac.h"
#include "bgp_trace.h"
DEFINE_MTYPE_STATIC(BGPD, PEER_TX_SHUTDOWN_MSG, "Peer shutdown message (TX)");
DEFINE_QOBJ_TYPE(bgp_master);
DEFINE_QOBJ_TYPE(bgp);
DEFINE_QOBJ_TYPE(peer);
DEFINE_HOOK(bgp_inst_delete, (struct bgp *bgp), (bgp));
DEFINE_HOOK(bgp_instance_state, (struct bgp *bgp), (bgp));
/* BGP process wide configuration. */
static struct bgp_master bgp_master;
/* BGP process wide configuration pointer to export. */
struct bgp_master *bm;
/* BGP community-list. */
struct community_list_handler *bgp_clist;
unsigned int multipath_num = MULTIPATH_NUM;
/* Number of bgp instances configured for suppress fib config */
unsigned int bgp_suppress_fib_count;
static void bgp_if_finish(struct bgp *bgp);
static void peer_drop_dynamic_neighbor(struct peer *peer);
extern struct zclient *zclient;
/* handle main socket creation or deletion */
static int bgp_check_main_socket(bool create, struct bgp *bgp)
{
static int bgp_server_main_created;
struct listnode *node;
char *address;
if (create) {
if (bgp_server_main_created)
return 0;
if (list_isempty(bm->addresses)) {
if (bgp_socket(bgp, bm->port, NULL) < 0)
return BGP_ERR_INVALID_VALUE;
} else {
for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
if (bgp_socket(bgp, bm->port, address) < 0)
return BGP_ERR_INVALID_VALUE;
}
bgp_server_main_created = 1;
return 0;
}
if (!bgp_server_main_created)
return 0;
bgp_close();
bgp_server_main_created = 0;
return 0;
}
void bgp_session_reset(struct peer *peer)
{
if (peer->doppelganger &&
(peer->doppelganger->connection->status != Deleted) &&
!(CHECK_FLAG(peer->doppelganger->flags, PEER_FLAG_CONFIG_NODE)))
peer_delete(peer->doppelganger);
BGP_EVENT_ADD(peer->connection, BGP_Stop);
}
/*
* During session reset, we may delete the doppelganger peer, which would
* be the next node to the current node. If the session reset was invoked
* during walk of peer list, we would end up accessing the freed next
* node. This function moves the next node along.
*/
void bgp_session_reset_safe(struct peer *peer, struct listnode **nnode)
{
struct listnode *n;
struct peer *npeer;
n = (nnode) ? *nnode : NULL;
npeer = (n) ? listgetdata(n) : NULL;
if (peer->doppelganger &&
(peer->doppelganger->connection->status != Deleted) &&
!(CHECK_FLAG(peer->doppelganger->flags, PEER_FLAG_CONFIG_NODE))) {
if (peer->doppelganger == npeer)
/* nnode and *nnode are confirmed to be non-NULL here */
*nnode = (*nnode)->next;
peer_delete(peer->doppelganger);
}
BGP_EVENT_ADD(peer->connection, BGP_Stop);
}
/* BGP global flag manipulation. */
int bgp_option_set(int flag)
{
switch (flag) {
case BGP_OPT_NO_FIB:
case BGP_OPT_NO_LISTEN:
case BGP_OPT_NO_ZEBRA:
SET_FLAG(bm->options, flag);
break;
default:
return BGP_ERR_INVALID_FLAG;
}
return 0;
}
int bgp_option_unset(int flag)
{
switch (flag) {
case BGP_OPT_NO_ZEBRA:
case BGP_OPT_NO_FIB:
UNSET_FLAG(bm->options, flag);
break;
default:
return BGP_ERR_INVALID_FLAG;
}
return 0;
}
int bgp_option_check(int flag)
{
return CHECK_FLAG(bm->options, flag);
}
/* set the bgp no-rib option during runtime and remove installed routes */
void bgp_option_norib_set_runtime(void)
{
struct bgp *bgp;
struct listnode *node;
afi_t afi;
safi_t safi;
if (bgp_option_check(BGP_OPT_NO_FIB))
return;
bgp_option_set(BGP_OPT_NO_FIB);
zlog_info("Disabled BGP route installation to RIB (Zebra)");
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
FOREACH_AFI_SAFI (afi, safi) {
/*
* Stop a crash, more work is needed
* here to properly add/remove these types of
* routes from zebra.
*/
if (!bgp_fibupd_safi(safi))
continue;
bgp_zebra_withdraw_table_all_subtypes(bgp, afi, safi);
}
}
zlog_info("All routes have been withdrawn from RIB (Zebra)");
}
/* unset the bgp no-rib option during runtime and announce routes to Zebra */
void bgp_option_norib_unset_runtime(void)
{
struct bgp *bgp;
struct listnode *node;
afi_t afi;
safi_t safi;
if (!bgp_option_check(BGP_OPT_NO_FIB))
return;
bgp_option_unset(BGP_OPT_NO_FIB);
zlog_info("Enabled BGP route installation to RIB (Zebra)");
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
FOREACH_AFI_SAFI (afi, safi) {
/*
* Stop a crash, more work is needed
* here to properly add/remove these types
* of routes from zebra
*/
if (!bgp_fibupd_safi(safi))
continue;
bgp_zebra_announce_table_all_subtypes(bgp, afi, safi);
}
}
zlog_info("All routes have been installed in RIB (Zebra)");
}
/* Internal function to set BGP structure configureation flag. */
static void bgp_config_set(struct bgp *bgp, int config)
{
SET_FLAG(bgp->config, config);
}
static void bgp_config_unset(struct bgp *bgp, int config)
{
UNSET_FLAG(bgp->config, config);
}
static int bgp_config_check(struct bgp *bgp, int config)
{
return CHECK_FLAG(bgp->config, config);
}
/* Set BGP router identifier; distinguish between explicit config and other
* cases.
*/
static int bgp_router_id_set(struct bgp *bgp, const struct in_addr *id,
bool is_config)
{
struct peer *peer;
struct listnode *node, *nnode;
if (IPV4_ADDR_SAME(&bgp->router_id, id))
return 0;
/* EVPN uses router id in RD, withdraw them */
if (is_evpn_enabled())
bgp_evpn_handle_router_id_update(bgp, true);
vpn_handle_router_id_update(bgp, true, is_config);
IPV4_ADDR_COPY(&bgp->router_id, id);
/* Set all peer's local identifier with this value. */
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
IPV4_ADDR_COPY(&peer->local_id, id);
peer->last_reset = PEER_DOWN_RID_CHANGE;
peer_notify_config_change(peer->connection);
}
/* EVPN uses router id in RD, update them */
if (is_evpn_enabled())
bgp_evpn_handle_router_id_update(bgp, false);
vpn_handle_router_id_update(bgp, false, is_config);
return 0;
}
void bgp_router_id_zebra_bump(vrf_id_t vrf_id, const struct prefix *router_id)
{
struct listnode *node, *nnode;
struct bgp *bgp;
struct in_addr *addr = NULL;
if (router_id != NULL)
addr = (struct in_addr *)&(router_id->u.prefix4);
if (vrf_id == VRF_DEFAULT) {
/* Router-id change for default VRF has to also update all
* views. */
for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
continue;
if (addr)
bgp->router_id_zebra = *addr;
else
addr = &bgp->router_id_zebra;
if (!bgp->router_id_static.s_addr) {
/* Router ID is updated if there are no active
* peer sessions
*/
if (bgp->established_peers == 0) {
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug(
"RID change : vrf %s(%u), RTR ID %pI4",
bgp->name_pretty,
bgp->vrf_id, addr);
/*
* if old router-id was 0x0, set flag
* to use this new value
*/
bgp_router_id_set(bgp, addr,
(bgp->router_id.s_addr
== INADDR_ANY)
? true
: false);
}
}
}
} else {
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (bgp) {
if (addr)
bgp->router_id_zebra = *addr;
else
addr = &bgp->router_id_zebra;
if (!bgp->router_id_static.s_addr) {
/* Router ID is updated if there are no active
* peer sessions
*/
if (bgp->established_peers == 0) {
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug(
"RID change : vrf %s(%u), RTR ID %pI4",
bgp->name_pretty,
bgp->vrf_id, addr);
/*
* if old router-id was 0x0, set flag
* to use this new value
*/
bgp_router_id_set(bgp, addr,
(bgp->router_id.s_addr
== INADDR_ANY)
? true
: false);
}
}
}
}
}
void bgp_router_id_static_set(struct bgp *bgp, struct in_addr id)
{
bgp->router_id_static = id;
bgp_router_id_set(bgp,
id.s_addr != INADDR_ANY ? &id : &bgp->router_id_zebra,
true /* is config */);
}
void bm_wait_for_fib_set(bool set)
{
bool send_msg = false;
struct bgp *bgp;
struct peer *peer;
struct listnode *next, *node;
if (bm->wait_for_fib == set)
return;
bm->wait_for_fib = set;
if (set) {
if (bgp_suppress_fib_count == 0)
send_msg = true;
bgp_suppress_fib_count++;
} else {
bgp_suppress_fib_count--;
if (bgp_suppress_fib_count == 0)
send_msg = true;
}
if (send_msg && zclient)
zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
zclient, set);
/*
* If this is configed at a time when peers are already set
* FRR needs to reset the connection(s) as that some installs
* may have already happened in some shape fashion or form
* let's just start over
*/
for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
peer->last_reset = PEER_DOWN_SUPPRESS_FIB_PENDING;
if (!BGP_IS_VALID_STATE_FOR_NOTIF(
peer->connection->status))
continue;
peer_notify_config_change(peer->connection);
}
}
}
/* Set the suppress fib pending for the bgp configuration */
void bgp_suppress_fib_pending_set(struct bgp *bgp, bool set)
{
bool send_msg = false;
struct peer *peer;
struct listnode *node;
if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
return;
/* Do nothing if already in a desired state */
if (set == !!CHECK_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING))
return;
if (set) {
SET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
/* Send msg to zebra for the first instance of bgp enabled
* with suppress fib
*/
if (bgp_suppress_fib_count == 0)
send_msg = true;
bgp_suppress_fib_count++;
} else {
UNSET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
bgp_suppress_fib_count--;
/* Send msg to zebra if there are no instances enabled
* with suppress fib
*/
if (bgp_suppress_fib_count == 0)
send_msg = true;
}
/* Send route notify request to RIB */
if (send_msg) {
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
if (zclient)
zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
zclient, set);
}
/*
* If this is configed at a time when peers are already set
* FRR needs to reset the connection as that some installs
* may have already happened in some shape fashion or form
* let's just start over
*/
for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
peer->last_reset = PEER_DOWN_SUPPRESS_FIB_PENDING;
if (!BGP_IS_VALID_STATE_FOR_NOTIF(peer->connection->status))
continue;
peer_notify_config_change(peer->connection);
}
}
/* BGP's cluster-id control. */
void bgp_cluster_id_set(struct bgp *bgp, struct in_addr *cluster_id)
{
struct peer *peer;
struct listnode *node, *nnode;
if (bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID)
&& IPV4_ADDR_SAME(&bgp->cluster_id, cluster_id))
return;
IPV4_ADDR_COPY(&bgp->cluster_id, cluster_id);
bgp_config_set(bgp, BGP_CONFIG_CLUSTER_ID);
/* Clear all IBGP peer. */
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->sort != BGP_PEER_IBGP)
continue;
peer->last_reset = PEER_DOWN_CLID_CHANGE;
peer_notify_config_change(peer->connection);
}
}
void bgp_cluster_id_unset(struct bgp *bgp)
{
struct peer *peer;
struct listnode *node, *nnode;
if (!bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID))
return;
bgp->cluster_id.s_addr = 0;
bgp_config_unset(bgp, BGP_CONFIG_CLUSTER_ID);
/* Clear all IBGP peer. */
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->sort != BGP_PEER_IBGP)
continue;
peer->last_reset = PEER_DOWN_CLID_CHANGE;
peer_notify_config_change(peer->connection);
}
}
/* BGP timer configuration. */
void bgp_timers_set(struct vty *vty, struct bgp *bgp, uint32_t keepalive,
uint32_t holdtime, uint32_t connect_retry,
uint32_t delayopen)
{
uint32_t default_keepalive = holdtime / 3;
if (keepalive > default_keepalive) {
if (vty)
vty_out(vty,
"%% keepalive value %u is larger than 1/3 of the holdtime, setting to %u\n",
keepalive, default_keepalive);
} else {
default_keepalive = keepalive;
}
bgp->default_keepalive = default_keepalive;
bgp->default_holdtime = holdtime;
bgp->default_connect_retry = connect_retry;
bgp->default_delayopen = delayopen;
}
/* mostly for completeness - CLI uses its own defaults */
void bgp_timers_unset(struct bgp *bgp)
{
bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
bgp->default_connect_retry = BGP_DEFAULT_CONNECT_RETRY;
bgp->default_delayopen = BGP_DEFAULT_DELAYOPEN;
}
void bgp_tcp_keepalive_set(struct bgp *bgp, uint16_t keepalive_idle,
uint16_t keepalive_intvl, uint16_t keepalive_probes)
{
bgp->tcp_keepalive_idle = keepalive_idle;
bgp->tcp_keepalive_intvl = keepalive_intvl;
bgp->tcp_keepalive_probes = keepalive_probes;
}
void bgp_tcp_keepalive_unset(struct bgp *bgp)
{
bgp->tcp_keepalive_idle = 0;
bgp->tcp_keepalive_intvl = 0;
bgp->tcp_keepalive_probes = 0;
}
/* BGP confederation configuration. */
void bgp_confederation_id_set(struct bgp *bgp, as_t as, const char *as_str)
{
struct peer *peer;
struct listnode *node, *nnode;
int already_confed;
if (as == 0)
return;
/* Remember - were we doing confederation before? */
already_confed = bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION);
bgp->confed_id = as;
if (bgp->confed_id_pretty)
XFREE(MTYPE_BGP_NAME, bgp->confed_id_pretty);
bgp->confed_id_pretty = XSTRDUP(MTYPE_BGP_NAME, as_str);
bgp_config_set(bgp, BGP_CONFIG_CONFEDERATION);
/* If we were doing confederation already, this is just an external
AS change. Just Reset EBGP sessions, not CONFED sessions. If we
were not doing confederation before, reset all EBGP sessions. */
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
enum bgp_peer_sort ptype = peer_sort(peer);
/* We're looking for peers who's AS is not local or part of our
confederation. */
if (already_confed) {
if (ptype == BGP_PEER_EBGP) {
peer->local_as = as;
if (peer_notify_config_change(peer->connection))
peer->last_reset =
PEER_DOWN_CONFED_ID_CHANGE;
else
bgp_session_reset_safe(peer, &nnode);
}
} else {
/* Not doign confederation before, so reset every
non-local
session */
if (ptype != BGP_PEER_IBGP) {
/* Reset the local_as to be our EBGP one */
if (ptype == BGP_PEER_EBGP)
peer->local_as = as;
if (peer_notify_config_change(peer->connection))
peer->last_reset =
PEER_DOWN_CONFED_ID_CHANGE;
else
bgp_session_reset_safe(peer, &nnode);
}
}
}
return;
}
void bgp_confederation_id_unset(struct bgp *bgp)
{
struct peer *peer;
struct listnode *node, *nnode;
bgp->confed_id = 0;
XFREE(MTYPE_BGP_NAME, bgp->confed_id_pretty);
bgp_config_unset(bgp, BGP_CONFIG_CONFEDERATION);
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
/* We're looking for peers who's AS is not local */
if (peer_sort(peer) != BGP_PEER_IBGP) {
peer->local_as = bgp->as;
peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
if (!peer_notify_config_change(peer->connection))
bgp_session_reset_safe(peer, &nnode);
}
}
}
/* Is an AS part of the confed or not? */
bool bgp_confederation_peers_check(struct bgp *bgp, as_t as)
{
int i;
if (!bgp)
return false;
for (i = 0; i < bgp->confed_peers_cnt; i++)
if (bgp->confed_peers[i].as == as)
return true;
return false;
}
/* Add an AS to the confederation set. */
void bgp_confederation_peers_add(struct bgp *bgp, as_t as, const char *as_str)
{
struct peer *peer;
struct listnode *node, *nnode;
if (!bgp)
return;
if (bgp_confederation_peers_check(bgp, as))
return;
bgp->confed_peers = XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
(bgp->confed_peers_cnt + 1) *
sizeof(struct as_confed));
bgp->confed_peers[bgp->confed_peers_cnt].as = as;
bgp->confed_peers[bgp->confed_peers_cnt].as_pretty =
XSTRDUP(MTYPE_BGP_NAME, as_str);
bgp->confed_peers_cnt++;
if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->as == as) {
peer->local_as = bgp->as;
(void)peer_sort(peer);
if (peer_notify_config_change(peer->connection))
peer->last_reset =
PEER_DOWN_CONFED_PEER_CHANGE;
else
bgp_session_reset_safe(peer, &nnode);
}
}
}
}
/* Delete an AS from the confederation set. */
void bgp_confederation_peers_remove(struct bgp *bgp, as_t as)
{
int i;
int j;
struct peer *peer;
struct listnode *node, *nnode;
if (!bgp)
return;
if (!bgp_confederation_peers_check(bgp, as))
return;
for (i = 0; i < bgp->confed_peers_cnt; i++)
if (bgp->confed_peers[i].as == as) {
XFREE(MTYPE_BGP_NAME, bgp->confed_peers[i].as_pretty);
for (j = i + 1; j < bgp->confed_peers_cnt; j++) {
bgp->confed_peers[j - 1].as =
bgp->confed_peers[j].as;
bgp->confed_peers[j - 1].as_pretty =
bgp->confed_peers[j].as_pretty;
}
}
bgp->confed_peers_cnt--;
if (bgp->confed_peers_cnt == 0) {
if (bgp->confed_peers)
XFREE(MTYPE_BGP_CONFED_LIST, bgp->confed_peers);
bgp->confed_peers = NULL;
} else
bgp->confed_peers = XREALLOC(
MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
bgp->confed_peers_cnt * sizeof(struct as_confed));
/* Now reset any peer who's remote AS has just been removed from the
CONFED */
if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->as == as) {
peer->local_as = bgp->confed_id;
(void)peer_sort(peer);
if (peer_notify_config_change(peer->connection))
peer->last_reset =
PEER_DOWN_CONFED_PEER_CHANGE;
else
bgp_session_reset_safe(peer, &nnode);
}
}
}
}
/* Local preference configuration. */
void bgp_default_local_preference_set(struct bgp *bgp, uint32_t local_pref)
{
if (!bgp)
return;
bgp->default_local_pref = local_pref;
}
void bgp_default_local_preference_unset(struct bgp *bgp)
{
if (!bgp)
return;
bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
}
/* Local preference configuration. */
void bgp_default_subgroup_pkt_queue_max_set(struct bgp *bgp,
uint32_t queue_size)
{
if (!bgp)
return;
bgp->default_subgroup_pkt_queue_max = queue_size;
}
void bgp_default_subgroup_pkt_queue_max_unset(struct bgp *bgp)
{
if (!bgp)
return;
bgp->default_subgroup_pkt_queue_max =
BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX;
}
/* Listen limit configuration. */
void bgp_listen_limit_set(struct bgp *bgp, int listen_limit)
{
if (!bgp)
return;
bgp->dynamic_neighbors_limit = listen_limit;
}
void bgp_listen_limit_unset(struct bgp *bgp)
{
if (!bgp)
return;
bgp->dynamic_neighbors_limit = BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT;
}
int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi, iana_safi_t pkt_safi,
afi_t *afi, safi_t *safi)
{
/* Map from IANA values to internal values, return error if
* values are unrecognized.
*/
*afi = afi_iana2int(pkt_afi);
*safi = safi_iana2int(pkt_safi);
if (*afi == AFI_MAX || *safi == SAFI_MAX)
return -1;
return 0;
}
int bgp_map_afi_safi_int2iana(afi_t afi, safi_t safi, iana_afi_t *pkt_afi,
iana_safi_t *pkt_safi)
{
/* Map from internal values to IANA values, return error if
* internal values are bad (unexpected).
*/
if (afi == AFI_MAX || safi == SAFI_MAX)
return -1;
*pkt_afi = afi_int2iana(afi);
*pkt_safi = safi_int2iana(safi);
return 0;
}
struct peer_af *peer_af_create(struct peer *peer, afi_t afi, safi_t safi)
{
struct peer_af *af;
int afid;
struct bgp *bgp;
if (!peer)
return NULL;
afid = afindex(afi, safi);
if (afid >= BGP_AF_MAX)
return NULL;
bgp = peer->bgp;
assert(peer->peer_af_array[afid] == NULL);
/* Allocate new peer af */
af = XCALLOC(MTYPE_BGP_PEER_AF, sizeof(struct peer_af));
peer->peer_af_array[afid] = af;
af->afi = afi;
af->safi = safi;
af->afid = afid;
af->peer = peer;
bgp->af_peer_count[afi][safi]++;
return af;
}
struct peer_af *peer_af_find(struct peer *peer, afi_t afi, safi_t safi)
{
int afid;
if (!peer)
return NULL;
afid = afindex(afi, safi);
if (afid >= BGP_AF_MAX)
return NULL;
return peer->peer_af_array[afid];
}
int peer_af_delete(struct peer *peer, afi_t afi, safi_t safi)
{
struct peer_af *af;
int afid;
struct bgp *bgp;
if (!peer)
return -1;
afid = afindex(afi, safi);
if (afid >= BGP_AF_MAX)
return -1;
af = peer->peer_af_array[afid];
if (!af)
return -1;
bgp = peer->bgp;
bgp_soft_reconfig_table_task_cancel(bgp, bgp->rib[afi][safi], peer);
bgp_stop_announce_route_timer(af);
if (PAF_SUBGRP(af)) {
if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
zlog_debug("u%" PRIu64 ":s%" PRIu64 " remove peer %s",
af->subgroup->update_group->id,
af->subgroup->id, peer->host);
}
update_subgroup_remove_peer(af->subgroup, af);
if (bgp->af_peer_count[afi][safi])
bgp->af_peer_count[afi][safi]--;
peer->peer_af_array[afid] = NULL;
XFREE(MTYPE_BGP_PEER_AF, af);
return 0;
}
/* Peer comparison function for sorting. */
int peer_cmp(struct peer *p1, struct peer *p2)
{
if (p1->group && !p2->group)
return -1;
if (!p1->group && p2->group)
return 1;
if (p1->group == p2->group) {
if (p1->conf_if && !p2->conf_if)
return -1;
if (!p1->conf_if && p2->conf_if)
return 1;
if (p1->conf_if && p2->conf_if)
return if_cmp_name_func(p1->conf_if, p2->conf_if);
} else
return strcmp(p1->group->name, p2->group->name);
return sockunion_cmp(&p1->connection->su, &p2->connection->su);
}
static unsigned int peer_hash_key_make(const void *p)
{
const struct peer *peer = p;
return sockunion_hash(&peer->connection->su);
}
static bool peer_hash_same(const void *p1, const void *p2)
{
const struct peer *peer1 = p1;
const struct peer *peer2 = p2;
return (sockunion_same(&peer1->connection->su, &peer2->connection->su) &&
CHECK_FLAG(peer1->flags, PEER_FLAG_CONFIG_NODE) ==
CHECK_FLAG(peer2->flags, PEER_FLAG_CONFIG_NODE));
}
void peer_flag_inherit(struct peer *peer, uint64_t flag)
{
bool group_val;
/* Skip if peer is not a peer-group member. */
if (!peer_group_active(peer))
return;
/* Unset override flag to signal inheritance from peer-group. */
UNSET_FLAG(peer->flags_override, flag);
/*
* Inherit flag state from peer-group. If the flag of the peer-group is
* not being inverted, the peer must inherit the inverse of the current
* peer-group flag state.
*/
group_val = CHECK_FLAG(peer->group->conf->flags, flag);
if (!CHECK_FLAG(peer->group->conf->flags_invert, flag)