]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgpd.c
Merge pull request #12187 from mjstapp/mjs_fix_bgp_allowas
[mirror_frr.git] / bgpd / bgpd.c
1 /* BGP-4, BGP-4+ daemon program
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "thread.h"
25 #include "buffer.h"
26 #include "stream.h"
27 #include "ringbuf.h"
28 #include "command.h"
29 #include "sockunion.h"
30 #include "sockopt.h"
31 #include "network.h"
32 #include "memory.h"
33 #include "filter.h"
34 #include "routemap.h"
35 #include "log.h"
36 #include "plist.h"
37 #include "linklist.h"
38 #include "workqueue.h"
39 #include "queue.h"
40 #include "zclient.h"
41 #include "bfd.h"
42 #include "hash.h"
43 #include "jhash.h"
44 #include "table.h"
45 #include "lib/json.h"
46 #include "lib/sockopt.h"
47 #include "frr_pthread.h"
48 #include "bitfield.h"
49
50 #include "bgpd/bgpd.h"
51 #include "bgpd/bgp_table.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_route.h"
54 #include "bgpd/bgp_dump.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_community.h"
58 #include "bgpd/bgp_community_alias.h"
59 #include "bgpd/bgp_conditional_adv.h"
60 #include "bgpd/bgp_attr.h"
61 #include "bgpd/bgp_regex.h"
62 #include "bgpd/bgp_clist.h"
63 #include "bgpd/bgp_fsm.h"
64 #include "bgpd/bgp_packet.h"
65 #include "bgpd/bgp_zebra.h"
66 #include "bgpd/bgp_open.h"
67 #include "bgpd/bgp_filter.h"
68 #include "bgpd/bgp_nexthop.h"
69 #include "bgpd/bgp_damp.h"
70 #include "bgpd/bgp_mplsvpn.h"
71 #ifdef ENABLE_BGP_VNC
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
74 #endif
75 #include "bgpd/bgp_evpn.h"
76 #include "bgpd/bgp_advertise.h"
77 #include "bgpd/bgp_network.h"
78 #include "bgpd/bgp_vty.h"
79 #include "bgpd/bgp_mpath.h"
80 #include "bgpd/bgp_nht.h"
81 #include "bgpd/bgp_updgrp.h"
82 #include "bgpd/bgp_bfd.h"
83 #include "bgpd/bgp_memory.h"
84 #include "bgpd/bgp_evpn_vty.h"
85 #include "bgpd/bgp_keepalives.h"
86 #include "bgpd/bgp_io.h"
87 #include "bgpd/bgp_ecommunity.h"
88 #include "bgpd/bgp_flowspec.h"
89 #include "bgpd/bgp_labelpool.h"
90 #include "bgpd/bgp_pbr.h"
91 #include "bgpd/bgp_addpath.h"
92 #include "bgpd/bgp_evpn_private.h"
93 #include "bgpd/bgp_evpn_mh.h"
94 #include "bgpd/bgp_mac.h"
95 #include "bgpd/bgp_orr.h"
96
97 DEFINE_MTYPE_STATIC(BGPD, PEER_TX_SHUTDOWN_MSG, "Peer shutdown message (TX)");
98 DEFINE_MTYPE_STATIC(BGPD, BGP_EVPN_INFO, "BGP EVPN instance information");
99 DEFINE_QOBJ_TYPE(bgp_master);
100 DEFINE_QOBJ_TYPE(bgp);
101 DEFINE_QOBJ_TYPE(peer);
102 DEFINE_HOOK(bgp_inst_delete, (struct bgp *bgp), (bgp));
103
104 /* BGP process wide configuration. */
105 static struct bgp_master bgp_master;
106
107 /* BGP process wide configuration pointer to export. */
108 struct bgp_master *bm;
109
110 /* BGP community-list. */
111 struct community_list_handler *bgp_clist;
112
113 unsigned int multipath_num = MULTIPATH_NUM;
114
115 /* Number of bgp instances configured for suppress fib config */
116 unsigned int bgp_suppress_fib_count;
117
118 static void bgp_if_finish(struct bgp *bgp);
119 static void peer_drop_dynamic_neighbor(struct peer *peer);
120
121 extern struct zclient *zclient;
122
123 /* handle main socket creation or deletion */
124 static int bgp_check_main_socket(bool create, struct bgp *bgp)
125 {
126 static int bgp_server_main_created;
127 struct listnode *node;
128 char *address;
129
130 if (create) {
131 if (bgp_server_main_created)
132 return 0;
133 if (list_isempty(bm->addresses)) {
134 if (bgp_socket(bgp, bm->port, NULL) < 0)
135 return BGP_ERR_INVALID_VALUE;
136 } else {
137 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
138 if (bgp_socket(bgp, bm->port, address) < 0)
139 return BGP_ERR_INVALID_VALUE;
140 }
141 bgp_server_main_created = 1;
142 return 0;
143 }
144 if (!bgp_server_main_created)
145 return 0;
146 bgp_close();
147 bgp_server_main_created = 0;
148 return 0;
149 }
150
151 void bgp_session_reset(struct peer *peer)
152 {
153 if (peer->doppelganger && (peer->doppelganger->status != Deleted)
154 && !(CHECK_FLAG(peer->doppelganger->flags, PEER_FLAG_CONFIG_NODE)))
155 peer_delete(peer->doppelganger);
156
157 BGP_EVENT_ADD(peer, BGP_Stop);
158 }
159
160 /*
161 * During session reset, we may delete the doppelganger peer, which would
162 * be the next node to the current node. If the session reset was invoked
163 * during walk of peer list, we would end up accessing the freed next
164 * node. This function moves the next node along.
165 */
166 static void bgp_session_reset_safe(struct peer *peer, struct listnode **nnode)
167 {
168 struct listnode *n;
169 struct peer *npeer;
170
171 n = (nnode) ? *nnode : NULL;
172 npeer = (n) ? listgetdata(n) : NULL;
173
174 if (peer->doppelganger && (peer->doppelganger->status != Deleted)
175 && !(CHECK_FLAG(peer->doppelganger->flags,
176 PEER_FLAG_CONFIG_NODE))) {
177 if (peer->doppelganger == npeer)
178 /* nnode and *nnode are confirmed to be non-NULL here */
179 *nnode = (*nnode)->next;
180 peer_delete(peer->doppelganger);
181 }
182
183 BGP_EVENT_ADD(peer, BGP_Stop);
184 }
185
186 /* BGP global flag manipulation. */
187 int bgp_option_set(int flag)
188 {
189 switch (flag) {
190 case BGP_OPT_NO_FIB:
191 case BGP_OPT_NO_LISTEN:
192 case BGP_OPT_NO_ZEBRA:
193 SET_FLAG(bm->options, flag);
194 break;
195 default:
196 return BGP_ERR_INVALID_FLAG;
197 }
198 return 0;
199 }
200
201 int bgp_option_unset(int flag)
202 {
203 switch (flag) {
204 /* Fall through. */
205 case BGP_OPT_NO_ZEBRA:
206 case BGP_OPT_NO_FIB:
207 UNSET_FLAG(bm->options, flag);
208 break;
209 default:
210 return BGP_ERR_INVALID_FLAG;
211 }
212 return 0;
213 }
214
215 int bgp_option_check(int flag)
216 {
217 return CHECK_FLAG(bm->options, flag);
218 }
219
220 /* set the bgp no-rib option during runtime and remove installed routes */
221 void bgp_option_norib_set_runtime(void)
222 {
223 struct bgp *bgp;
224 struct listnode *node;
225 afi_t afi;
226 safi_t safi;
227
228 if (bgp_option_check(BGP_OPT_NO_FIB))
229 return;
230
231 bgp_option_set(BGP_OPT_NO_FIB);
232
233 zlog_info("Disabled BGP route installation to RIB (Zebra)");
234
235 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
236 FOREACH_AFI_SAFI (afi, safi) {
237 /*
238 * Stop a crash, more work is needed
239 * here to properly add/remove these types of
240 * routes from zebra.
241 */
242 if (!bgp_fibupd_safi(safi))
243 continue;
244
245 bgp_zebra_withdraw_table_all_subtypes(bgp, afi, safi);
246 }
247 }
248
249 zlog_info("All routes have been withdrawn from RIB (Zebra)");
250 }
251
252 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
253 void bgp_option_norib_unset_runtime(void)
254 {
255 struct bgp *bgp;
256 struct listnode *node;
257 afi_t afi;
258 safi_t safi;
259
260 if (!bgp_option_check(BGP_OPT_NO_FIB))
261 return;
262
263 bgp_option_unset(BGP_OPT_NO_FIB);
264
265 zlog_info("Enabled BGP route installation to RIB (Zebra)");
266
267 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
268 FOREACH_AFI_SAFI (afi, safi) {
269 /*
270 * Stop a crash, more work is needed
271 * here to properly add/remove these types
272 * of routes from zebra
273 */
274 if (!bgp_fibupd_safi(safi))
275 continue;
276
277 bgp_zebra_announce_table_all_subtypes(bgp, afi, safi);
278 }
279 }
280
281 zlog_info("All routes have been installed in RIB (Zebra)");
282 }
283
284 /* Internal function to set BGP structure configureation flag. */
285 static void bgp_config_set(struct bgp *bgp, int config)
286 {
287 SET_FLAG(bgp->config, config);
288 }
289
290 static void bgp_config_unset(struct bgp *bgp, int config)
291 {
292 UNSET_FLAG(bgp->config, config);
293 }
294
295 static int bgp_config_check(struct bgp *bgp, int config)
296 {
297 return CHECK_FLAG(bgp->config, config);
298 }
299
300 /* Set BGP router identifier; distinguish between explicit config and other
301 * cases.
302 */
303 static int bgp_router_id_set(struct bgp *bgp, const struct in_addr *id,
304 bool is_config)
305 {
306 struct peer *peer;
307 struct listnode *node, *nnode;
308
309 if (IPV4_ADDR_SAME(&bgp->router_id, id))
310 return 0;
311
312 /* EVPN uses router id in RD, withdraw them */
313 if (is_evpn_enabled())
314 bgp_evpn_handle_router_id_update(bgp, true);
315
316 vpn_handle_router_id_update(bgp, true, is_config);
317
318 IPV4_ADDR_COPY(&bgp->router_id, id);
319
320 /* Set all peer's local identifier with this value. */
321 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
322 IPV4_ADDR_COPY(&peer->local_id, id);
323
324 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
325 peer->last_reset = PEER_DOWN_RID_CHANGE;
326 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
327 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
328 }
329 }
330
331 /* EVPN uses router id in RD, update them */
332 if (is_evpn_enabled())
333 bgp_evpn_handle_router_id_update(bgp, false);
334
335 vpn_handle_router_id_update(bgp, false, is_config);
336
337 return 0;
338 }
339
340 void bgp_router_id_zebra_bump(vrf_id_t vrf_id, const struct prefix *router_id)
341 {
342 struct listnode *node, *nnode;
343 struct bgp *bgp;
344 struct in_addr *addr = NULL;
345
346 if (router_id != NULL)
347 addr = (struct in_addr *)&(router_id->u.prefix4);
348
349 if (vrf_id == VRF_DEFAULT) {
350 /* Router-id change for default VRF has to also update all
351 * views. */
352 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
353 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
354 continue;
355
356 if (addr)
357 bgp->router_id_zebra = *addr;
358 else
359 addr = &bgp->router_id_zebra;
360
361 if (!bgp->router_id_static.s_addr) {
362 /* Router ID is updated if there are no active
363 * peer sessions
364 */
365 if (bgp->established_peers == 0) {
366 if (BGP_DEBUG(zebra, ZEBRA))
367 zlog_debug(
368 "RID change : vrf %s(%u), RTR ID %pI4",
369 bgp->name_pretty,
370 bgp->vrf_id, addr);
371 /*
372 * if old router-id was 0x0, set flag
373 * to use this new value
374 */
375 bgp_router_id_set(bgp, addr,
376 (bgp->router_id.s_addr
377 == INADDR_ANY)
378 ? true
379 : false);
380 }
381 }
382 }
383 } else {
384 bgp = bgp_lookup_by_vrf_id(vrf_id);
385 if (bgp) {
386 if (addr)
387 bgp->router_id_zebra = *addr;
388 else
389 addr = &bgp->router_id_zebra;
390
391 if (!bgp->router_id_static.s_addr) {
392 /* Router ID is updated if there are no active
393 * peer sessions
394 */
395 if (bgp->established_peers == 0) {
396 if (BGP_DEBUG(zebra, ZEBRA))
397 zlog_debug(
398 "RID change : vrf %s(%u), RTR ID %pI4",
399 bgp->name_pretty,
400 bgp->vrf_id, addr);
401 /*
402 * if old router-id was 0x0, set flag
403 * to use this new value
404 */
405 bgp_router_id_set(bgp, addr,
406 (bgp->router_id.s_addr
407 == INADDR_ANY)
408 ? true
409 : false);
410 }
411 }
412
413 }
414 }
415 }
416
417 void bgp_router_id_static_set(struct bgp *bgp, struct in_addr id)
418 {
419 bgp->router_id_static = id;
420 bgp_router_id_set(bgp,
421 id.s_addr != INADDR_ANY ? &id : &bgp->router_id_zebra,
422 true /* is config */);
423 }
424
425 void bm_wait_for_fib_set(bool set)
426 {
427 bool send_msg = false;
428
429 if (bm->wait_for_fib == set)
430 return;
431
432 bm->wait_for_fib = set;
433 if (set) {
434 if (bgp_suppress_fib_count == 0)
435 send_msg = true;
436 bgp_suppress_fib_count++;
437 } else {
438 bgp_suppress_fib_count--;
439 if (bgp_suppress_fib_count == 0)
440 send_msg = true;
441 }
442
443 if (send_msg && zclient)
444 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
445 zclient, set);
446 }
447
448 /* Set the suppress fib pending for the bgp configuration */
449 void bgp_suppress_fib_pending_set(struct bgp *bgp, bool set)
450 {
451 bool send_msg = false;
452
453 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
454 return;
455
456 if (set) {
457 SET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
458 /* Send msg to zebra for the first instance of bgp enabled
459 * with suppress fib
460 */
461 if (bgp_suppress_fib_count == 0)
462 send_msg = true;
463 bgp_suppress_fib_count++;
464 } else {
465 UNSET_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_FIB_PENDING);
466 bgp_suppress_fib_count--;
467
468 /* Send msg to zebra if there are no instances enabled
469 * with suppress fib
470 */
471 if (bgp_suppress_fib_count == 0)
472 send_msg = true;
473 }
474 /* Send route notify request to RIB */
475 if (send_msg) {
476 if (BGP_DEBUG(zebra, ZEBRA))
477 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
478
479 if (zclient)
480 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST,
481 zclient, set);
482 }
483 }
484
485 /* BGP's cluster-id control. */
486 void bgp_cluster_id_set(struct bgp *bgp, struct in_addr *cluster_id)
487 {
488 struct peer *peer;
489 struct listnode *node, *nnode;
490
491 if (bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID)
492 && IPV4_ADDR_SAME(&bgp->cluster_id, cluster_id))
493 return;
494
495 IPV4_ADDR_COPY(&bgp->cluster_id, cluster_id);
496 bgp_config_set(bgp, BGP_CONFIG_CLUSTER_ID);
497
498 /* Clear all IBGP peer. */
499 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
500 if (peer->sort != BGP_PEER_IBGP)
501 continue;
502
503 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
504 peer->last_reset = PEER_DOWN_CLID_CHANGE;
505 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
506 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
507 }
508 }
509 }
510
511 void bgp_cluster_id_unset(struct bgp *bgp)
512 {
513 struct peer *peer;
514 struct listnode *node, *nnode;
515
516 if (!bgp_config_check(bgp, BGP_CONFIG_CLUSTER_ID))
517 return;
518
519 bgp->cluster_id.s_addr = 0;
520 bgp_config_unset(bgp, BGP_CONFIG_CLUSTER_ID);
521
522 /* Clear all IBGP peer. */
523 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
524 if (peer->sort != BGP_PEER_IBGP)
525 continue;
526
527 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
528 peer->last_reset = PEER_DOWN_CLID_CHANGE;
529 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
530 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
531 }
532 }
533 }
534
535 /* BGP timer configuration. */
536 void bgp_timers_set(struct bgp *bgp, uint32_t keepalive, uint32_t holdtime,
537 uint32_t connect_retry, uint32_t delayopen)
538 {
539 bgp->default_keepalive =
540 (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
541 bgp->default_holdtime = holdtime;
542 bgp->default_connect_retry = connect_retry;
543 bgp->default_delayopen = delayopen;
544 }
545
546 /* mostly for completeness - CLI uses its own defaults */
547 void bgp_timers_unset(struct bgp *bgp)
548 {
549 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
550 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
551 bgp->default_connect_retry = BGP_DEFAULT_CONNECT_RETRY;
552 bgp->default_delayopen = BGP_DEFAULT_DELAYOPEN;
553 }
554
555 void bgp_tcp_keepalive_set(struct bgp *bgp, uint16_t keepalive_idle,
556 uint16_t keepalive_intvl, uint16_t keepalive_probes)
557 {
558 bgp->tcp_keepalive_idle = keepalive_idle;
559 bgp->tcp_keepalive_intvl = keepalive_intvl;
560 bgp->tcp_keepalive_probes = keepalive_probes;
561 }
562
563 void bgp_tcp_keepalive_unset(struct bgp *bgp)
564 {
565 bgp->tcp_keepalive_idle = 0;
566 bgp->tcp_keepalive_intvl = 0;
567 bgp->tcp_keepalive_probes = 0;
568 }
569
570 /* BGP confederation configuration. */
571 void bgp_confederation_id_set(struct bgp *bgp, as_t as)
572 {
573 struct peer *peer;
574 struct listnode *node, *nnode;
575 int already_confed;
576
577 if (as == 0)
578 return;
579
580 /* Remember - were we doing confederation before? */
581 already_confed = bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION);
582 bgp->confed_id = as;
583 bgp_config_set(bgp, BGP_CONFIG_CONFEDERATION);
584
585 /* If we were doing confederation already, this is just an external
586 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
587 were not doing confederation before, reset all EBGP sessions. */
588 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
589 enum bgp_peer_sort ptype = peer_sort(peer);
590
591 /* We're looking for peers who's AS is not local or part of our
592 confederation. */
593 if (already_confed) {
594 if (ptype == BGP_PEER_EBGP) {
595 peer->local_as = as;
596 if (BGP_IS_VALID_STATE_FOR_NOTIF(
597 peer->status)) {
598 peer->last_reset =
599 PEER_DOWN_CONFED_ID_CHANGE;
600 bgp_notify_send(
601 peer, BGP_NOTIFY_CEASE,
602 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
603 } else
604 bgp_session_reset_safe(peer, &nnode);
605 }
606 } else {
607 /* Not doign confederation before, so reset every
608 non-local
609 session */
610 if (ptype != BGP_PEER_IBGP) {
611 /* Reset the local_as to be our EBGP one */
612 if (ptype == BGP_PEER_EBGP)
613 peer->local_as = as;
614 if (BGP_IS_VALID_STATE_FOR_NOTIF(
615 peer->status)) {
616 peer->last_reset =
617 PEER_DOWN_CONFED_ID_CHANGE;
618 bgp_notify_send(
619 peer, BGP_NOTIFY_CEASE,
620 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
621 } else
622 bgp_session_reset_safe(peer, &nnode);
623 }
624 }
625 }
626 return;
627 }
628
629 void bgp_confederation_id_unset(struct bgp *bgp)
630 {
631 struct peer *peer;
632 struct listnode *node, *nnode;
633
634 bgp->confed_id = 0;
635 bgp_config_unset(bgp, BGP_CONFIG_CONFEDERATION);
636
637 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
638 /* We're looking for peers who's AS is not local */
639 if (peer_sort(peer) != BGP_PEER_IBGP) {
640 peer->local_as = bgp->as;
641 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
642 peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE;
643 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
644 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
645 }
646
647 else
648 bgp_session_reset_safe(peer, &nnode);
649 }
650 }
651 }
652
653 /* Is an AS part of the confed or not? */
654 bool bgp_confederation_peers_check(struct bgp *bgp, as_t as)
655 {
656 int i;
657
658 if (!bgp)
659 return false;
660
661 for (i = 0; i < bgp->confed_peers_cnt; i++)
662 if (bgp->confed_peers[i] == as)
663 return true;
664
665 return false;
666 }
667
668 /* Add an AS to the confederation set. */
669 void bgp_confederation_peers_add(struct bgp *bgp, as_t as)
670 {
671 struct peer *peer;
672 struct listnode *node, *nnode;
673
674 if (bgp->as == as)
675 return;
676
677 if (bgp_confederation_peers_check(bgp, as))
678 return;
679
680 bgp->confed_peers =
681 XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
682 (bgp->confed_peers_cnt + 1) * sizeof(as_t));
683
684 bgp->confed_peers[bgp->confed_peers_cnt] = as;
685 bgp->confed_peers_cnt++;
686
687 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
688 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
689 if (peer->as == as) {
690 (void)peer_sort(peer);
691 peer->local_as = bgp->as;
692 if (BGP_IS_VALID_STATE_FOR_NOTIF(
693 peer->status)) {
694 peer->last_reset =
695 PEER_DOWN_CONFED_PEER_CHANGE;
696 bgp_notify_send(
697 peer, BGP_NOTIFY_CEASE,
698 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
699 } else
700 bgp_session_reset_safe(peer, &nnode);
701 }
702 }
703 }
704 }
705
706 /* Delete an AS from the confederation set. */
707 void bgp_confederation_peers_remove(struct bgp *bgp, as_t as)
708 {
709 int i;
710 int j;
711 struct peer *peer;
712 struct listnode *node, *nnode;
713
714 if (!bgp)
715 return;
716
717 if (!bgp_confederation_peers_check(bgp, as))
718 return;
719
720 for (i = 0; i < bgp->confed_peers_cnt; i++)
721 if (bgp->confed_peers[i] == as)
722 for (j = i + 1; j < bgp->confed_peers_cnt; j++)
723 bgp->confed_peers[j - 1] = bgp->confed_peers[j];
724
725 bgp->confed_peers_cnt--;
726
727 if (bgp->confed_peers_cnt == 0) {
728 if (bgp->confed_peers)
729 XFREE(MTYPE_BGP_CONFED_LIST, bgp->confed_peers);
730 bgp->confed_peers = NULL;
731 } else
732 bgp->confed_peers =
733 XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
734 bgp->confed_peers_cnt * sizeof(as_t));
735
736 /* Now reset any peer who's remote AS has just been removed from the
737 CONFED */
738 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)) {
739 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
740 if (peer->as == as) {
741 (void)peer_sort(peer);
742 peer->local_as = bgp->confed_id;
743 if (BGP_IS_VALID_STATE_FOR_NOTIF(
744 peer->status)) {
745 peer->last_reset =
746 PEER_DOWN_CONFED_PEER_CHANGE;
747 bgp_notify_send(
748 peer, BGP_NOTIFY_CEASE,
749 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
750 } else
751 bgp_session_reset_safe(peer, &nnode);
752 }
753 }
754 }
755 }
756
757 /* Local preference configuration. */
758 void bgp_default_local_preference_set(struct bgp *bgp, uint32_t local_pref)
759 {
760 if (!bgp)
761 return;
762
763 bgp->default_local_pref = local_pref;
764 }
765
766 void bgp_default_local_preference_unset(struct bgp *bgp)
767 {
768 if (!bgp)
769 return;
770
771 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
772 }
773
774 /* Local preference configuration. */
775 void bgp_default_subgroup_pkt_queue_max_set(struct bgp *bgp,
776 uint32_t queue_size)
777 {
778 if (!bgp)
779 return;
780
781 bgp->default_subgroup_pkt_queue_max = queue_size;
782 }
783
784 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp *bgp)
785 {
786 if (!bgp)
787 return;
788 bgp->default_subgroup_pkt_queue_max =
789 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX;
790 }
791
792 /* Listen limit configuration. */
793 void bgp_listen_limit_set(struct bgp *bgp, int listen_limit)
794 {
795 if (!bgp)
796 return;
797
798 bgp->dynamic_neighbors_limit = listen_limit;
799 }
800
801 void bgp_listen_limit_unset(struct bgp *bgp)
802 {
803 if (!bgp)
804 return;
805
806 bgp->dynamic_neighbors_limit = BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT;
807 }
808
809 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi, iana_safi_t pkt_safi,
810 afi_t *afi, safi_t *safi)
811 {
812 /* Map from IANA values to internal values, return error if
813 * values are unrecognized.
814 */
815 *afi = afi_iana2int(pkt_afi);
816 *safi = safi_iana2int(pkt_safi);
817 if (*afi == AFI_MAX || *safi == SAFI_MAX)
818 return -1;
819
820 return 0;
821 }
822
823 int bgp_map_afi_safi_int2iana(afi_t afi, safi_t safi, iana_afi_t *pkt_afi,
824 iana_safi_t *pkt_safi)
825 {
826 /* Map from internal values to IANA values, return error if
827 * internal values are bad (unexpected).
828 */
829 if (afi == AFI_MAX || safi == SAFI_MAX)
830 return -1;
831 *pkt_afi = afi_int2iana(afi);
832 *pkt_safi = safi_int2iana(safi);
833 return 0;
834 }
835
836 struct peer_af *peer_af_create(struct peer *peer, afi_t afi, safi_t safi)
837 {
838 struct peer_af *af;
839 int afid;
840 struct bgp *bgp;
841
842 if (!peer)
843 return NULL;
844
845 afid = afindex(afi, safi);
846 if (afid >= BGP_AF_MAX)
847 return NULL;
848
849 bgp = peer->bgp;
850 assert(peer->peer_af_array[afid] == NULL);
851
852 /* Allocate new peer af */
853 af = XCALLOC(MTYPE_BGP_PEER_AF, sizeof(struct peer_af));
854
855 peer->peer_af_array[afid] = af;
856 af->afi = afi;
857 af->safi = safi;
858 af->afid = afid;
859 af->peer = peer;
860 bgp->af_peer_count[afi][safi]++;
861
862 return af;
863 }
864
865 struct peer_af *peer_af_find(struct peer *peer, afi_t afi, safi_t safi)
866 {
867 int afid;
868
869 if (!peer)
870 return NULL;
871
872 afid = afindex(afi, safi);
873 if (afid >= BGP_AF_MAX)
874 return NULL;
875
876 return peer->peer_af_array[afid];
877 }
878
879 int peer_af_delete(struct peer *peer, afi_t afi, safi_t safi)
880 {
881 struct peer_af *af;
882 int afid;
883 struct bgp *bgp;
884
885 if (!peer)
886 return -1;
887
888 afid = afindex(afi, safi);
889 if (afid >= BGP_AF_MAX)
890 return -1;
891
892 af = peer->peer_af_array[afid];
893 if (!af)
894 return -1;
895
896 bgp = peer->bgp;
897 bgp_soft_reconfig_table_task_cancel(bgp, bgp->rib[afi][safi], peer);
898
899 bgp_stop_announce_route_timer(af);
900
901 if (PAF_SUBGRP(af)) {
902 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
903 zlog_debug("u%" PRIu64 ":s%" PRIu64 " remove peer %s",
904 af->subgroup->update_group->id,
905 af->subgroup->id, peer->host);
906 }
907
908
909 update_subgroup_remove_peer(af->subgroup, af);
910
911 if (bgp->af_peer_count[afi][safi])
912 bgp->af_peer_count[afi][safi]--;
913
914 peer->peer_af_array[afid] = NULL;
915 XFREE(MTYPE_BGP_PEER_AF, af);
916 return 0;
917 }
918
919 /* Peer comparison function for sorting. */
920 int peer_cmp(struct peer *p1, struct peer *p2)
921 {
922 if (p1->group && !p2->group)
923 return -1;
924
925 if (!p1->group && p2->group)
926 return 1;
927
928 if (p1->group == p2->group) {
929 if (p1->conf_if && !p2->conf_if)
930 return -1;
931
932 if (!p1->conf_if && p2->conf_if)
933 return 1;
934
935 if (p1->conf_if && p2->conf_if)
936 return if_cmp_name_func(p1->conf_if, p2->conf_if);
937 } else
938 return strcmp(p1->group->name, p2->group->name);
939
940 return sockunion_cmp(&p1->su, &p2->su);
941 }
942
943 static unsigned int peer_hash_key_make(const void *p)
944 {
945 const struct peer *peer = p;
946 return sockunion_hash(&peer->su);
947 }
948
949 static bool peer_hash_same(const void *p1, const void *p2)
950 {
951 const struct peer *peer1 = p1;
952 const struct peer *peer2 = p2;
953 return (sockunion_same(&peer1->su, &peer2->su)
954 && CHECK_FLAG(peer1->flags, PEER_FLAG_CONFIG_NODE)
955 == CHECK_FLAG(peer2->flags, PEER_FLAG_CONFIG_NODE));
956 }
957
958 void peer_flag_inherit(struct peer *peer, uint64_t flag)
959 {
960 bool group_val;
961
962 /* Skip if peer is not a peer-group member. */
963 if (!peer_group_active(peer))
964 return;
965
966 /* Unset override flag to signal inheritance from peer-group. */
967 UNSET_FLAG(peer->flags_override, flag);
968
969 /*
970 * Inherit flag state from peer-group. If the flag of the peer-group is
971 * not being inverted, the peer must inherit the inverse of the current
972 * peer-group flag state.
973 */
974 group_val = CHECK_FLAG(peer->group->conf->flags, flag);
975 if (!CHECK_FLAG(peer->group->conf->flags_invert, flag)
976 && CHECK_FLAG(peer->flags_invert, flag))
977 COND_FLAG(peer->flags, flag, !group_val);
978 else
979 COND_FLAG(peer->flags, flag, group_val);
980 }
981
982 int peer_af_flag_check(struct peer *peer, afi_t afi, safi_t safi, uint32_t flag)
983 {
984 return CHECK_FLAG(peer->af_flags[afi][safi], flag);
985 }
986
987 void peer_af_flag_inherit(struct peer *peer, afi_t afi, safi_t safi,
988 uint64_t flag)
989 {
990 bool group_val;
991
992 /* Skip if peer is not a peer-group member. */
993 if (!peer_group_active(peer))
994 return;
995
996 /* Unset override flag to signal inheritance from peer-group. */
997 UNSET_FLAG(peer->af_flags_override[afi][safi], flag);
998
999 /*
1000 * Inherit flag state from peer-group. If the flag of the peer-group is
1001 * not being inverted, the peer must inherit the inverse of the current
1002 * peer-group flag state.
1003 */
1004 group_val = CHECK_FLAG(peer->group->conf->af_flags[afi][safi], flag);
1005 if (!CHECK_FLAG(peer->group->conf->af_flags_invert[afi][safi], flag)
1006 && CHECK_FLAG(peer->af_flags_invert[afi][safi], flag))
1007 COND_FLAG(peer->af_flags[afi][safi], flag, !group_val);
1008 else
1009 COND_FLAG(peer->af_flags[afi][safi], flag, group_val);
1010 }
1011
1012 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1013 static inline enum bgp_peer_sort peer_calc_sort(struct peer *peer)
1014 {
1015 struct bgp *bgp;
1016 as_t local_as;
1017
1018 bgp = peer->bgp;
1019
1020 if (peer->change_local_as)
1021 local_as = peer->change_local_as;
1022 else
1023 local_as = peer->local_as;
1024
1025 /* Peer-group */
1026 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1027 if (peer->as_type == AS_INTERNAL)
1028 return BGP_PEER_IBGP;
1029
1030 else if (peer->as_type == AS_EXTERNAL)
1031 return BGP_PEER_EBGP;
1032
1033 else if (peer->as_type == AS_SPECIFIED && peer->as) {
1034 assert(bgp);
1035 return (local_as == peer->as ? BGP_PEER_IBGP
1036 : BGP_PEER_EBGP);
1037 }
1038
1039 else {
1040 struct peer *peer1;
1041
1042 assert(peer->group);
1043 peer1 = listnode_head(peer->group->peer);
1044
1045 if (peer1)
1046 return peer1->sort;
1047 }
1048 return BGP_PEER_INTERNAL;
1049 }
1050
1051 /* Normal peer */
1052 if (bgp && CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
1053 if (local_as == 0)
1054 return BGP_PEER_INTERNAL;
1055
1056 if (local_as == peer->as) {
1057 if (bgp->as == bgp->confed_id) {
1058 if (local_as == bgp->as)
1059 return BGP_PEER_IBGP;
1060 else
1061 return BGP_PEER_EBGP;
1062 } else {
1063 if (local_as == bgp->confed_id)
1064 return BGP_PEER_EBGP;
1065 else
1066 return BGP_PEER_IBGP;
1067 }
1068 }
1069
1070 if (bgp_confederation_peers_check(bgp, peer->as))
1071 return BGP_PEER_CONFED;
1072
1073 return BGP_PEER_EBGP;
1074 } else {
1075 if (peer->as_type == AS_UNSPECIFIED) {
1076 /* check if in peer-group with AS information */
1077 if (peer->group
1078 && (peer->group->conf->as_type != AS_UNSPECIFIED)) {
1079 if (peer->group->conf->as_type
1080 == AS_SPECIFIED) {
1081 if (local_as == peer->group->conf->as)
1082 return BGP_PEER_IBGP;
1083 else
1084 return BGP_PEER_EBGP;
1085 } else if (peer->group->conf->as_type
1086 == AS_INTERNAL)
1087 return BGP_PEER_IBGP;
1088 else
1089 return BGP_PEER_EBGP;
1090 }
1091 /* no AS information anywhere, let caller know */
1092 return BGP_PEER_UNSPECIFIED;
1093 } else if (peer->as_type != AS_SPECIFIED)
1094 return (peer->as_type == AS_INTERNAL ? BGP_PEER_IBGP
1095 : BGP_PEER_EBGP);
1096
1097 return (local_as == 0 ? BGP_PEER_INTERNAL
1098 : local_as == peer->as ? BGP_PEER_IBGP
1099 : BGP_PEER_EBGP);
1100 }
1101 }
1102
1103 /* Calculate and cache the peer "sort" */
1104 enum bgp_peer_sort peer_sort(struct peer *peer)
1105 {
1106 peer->sort = peer_calc_sort(peer);
1107 return peer->sort;
1108 }
1109
1110 enum bgp_peer_sort peer_sort_lookup(struct peer *peer)
1111 {
1112 return peer->sort;
1113 }
1114
1115 static void peer_free(struct peer *peer)
1116 {
1117 afi_t afi;
1118 safi_t safi;
1119
1120 assert(peer->status == Deleted);
1121
1122 QOBJ_UNREG(peer);
1123
1124 /* this /ought/ to have been done already through bgp_stop earlier,
1125 * but just to be sure..
1126 */
1127 bgp_timer_set(peer);
1128 bgp_reads_off(peer);
1129 bgp_writes_off(peer);
1130 assert(!peer->t_write);
1131 assert(!peer->t_read);
1132 BGP_EVENT_FLUSH(peer);
1133
1134 pthread_mutex_destroy(&peer->io_mtx);
1135
1136 /* Free connected nexthop, if present */
1137 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)
1138 && !peer_dynamic_neighbor(peer))
1139 bgp_delete_connected_nexthop(family2afi(peer->su.sa.sa_family),
1140 peer);
1141
1142 FOREACH_AFI_SAFI (afi, safi) {
1143 if (peer->filter[afi][safi].advmap.aname)
1144 XFREE(MTYPE_BGP_FILTER_NAME,
1145 peer->filter[afi][safi].advmap.aname);
1146 if (peer->filter[afi][safi].advmap.cname)
1147 XFREE(MTYPE_BGP_FILTER_NAME,
1148 peer->filter[afi][safi].advmap.cname);
1149 }
1150
1151 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
1152
1153 XFREE(MTYPE_PEER_DESC, peer->desc);
1154 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
1155 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
1156 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
1157
1158 /* Update source configuration. */
1159 if (peer->update_source) {
1160 sockunion_free(peer->update_source);
1161 peer->update_source = NULL;
1162 }
1163
1164 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
1165
1166 XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
1167 memset(&peer->notify, 0, sizeof(struct bgp_notify));
1168
1169 if (peer->clear_node_queue)
1170 work_queue_free_and_null(&peer->clear_node_queue);
1171
1172 bgp_sync_delete(peer);
1173
1174 XFREE(MTYPE_PEER_CONF_IF, peer->conf_if);
1175
1176 /* Remove BFD configuration. */
1177 if (peer->bfd_config)
1178 bgp_peer_remove_bfd_config(peer);
1179
1180 FOREACH_AFI_SAFI (afi, safi)
1181 bgp_addpath_set_peer_type(peer, afi, safi, BGP_ADDPATH_NONE);
1182
1183 bgp_unlock(peer->bgp);
1184
1185 memset(peer, 0, sizeof(struct peer));
1186
1187 XFREE(MTYPE_BGP_PEER, peer);
1188 }
1189
1190 /* increase reference count on a struct peer */
1191 struct peer *peer_lock_with_caller(const char *name, struct peer *peer)
1192 {
1193 assert(peer && (peer->lock >= 0));
1194
1195 peer->lock++;
1196
1197 return peer;
1198 }
1199
1200 /* decrease reference count on a struct peer
1201 * struct peer is freed and NULL returned if last reference
1202 */
1203 struct peer *peer_unlock_with_caller(const char *name, struct peer *peer)
1204 {
1205 assert(peer && (peer->lock > 0));
1206
1207 peer->lock--;
1208
1209 if (peer->lock == 0) {
1210 peer_free(peer);
1211 return NULL;
1212 }
1213
1214 return peer;
1215 }
1216 /* BGP GR changes */
1217
1218 int bgp_global_gr_init(struct bgp *bgp)
1219 {
1220 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
1221 zlog_debug("%s called ..", __func__);
1222
1223 int local_GLOBAL_GR_FSM[BGP_GLOBAL_GR_MODE][BGP_GLOBAL_GR_EVENT_CMD] = {
1224 /* GLOBAL_HELPER Mode */
1225 {
1226 /*Event -> */
1227 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1228 GLOBAL_GR, GLOBAL_INVALID,
1229 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1230 GLOBAL_DISABLE, GLOBAL_INVALID
1231 },
1232 /* GLOBAL_GR Mode */
1233 {
1234 /*Event -> */
1235 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1236 GLOBAL_GR, GLOBAL_HELPER,
1237 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1238 GLOBAL_DISABLE, GLOBAL_INVALID
1239 },
1240 /* GLOBAL_DISABLE Mode */
1241 {
1242 /*Event -> */
1243 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1244 GLOBAL_GR, GLOBAL_INVALID,
1245 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1246 GLOBAL_INVALID, GLOBAL_HELPER
1247 },
1248 /* GLOBAL_INVALID Mode */
1249 {
1250 /*Event -> */
1251 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1252 GLOBAL_INVALID, GLOBAL_INVALID,
1253 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1254 GLOBAL_INVALID, GLOBAL_INVALID
1255 }
1256 };
1257 memcpy(bgp->GLOBAL_GR_FSM, local_GLOBAL_GR_FSM,
1258 sizeof(local_GLOBAL_GR_FSM));
1259
1260 bgp->global_gr_present_state = GLOBAL_HELPER;
1261 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
1262
1263 return BGP_GR_SUCCESS;
1264 }
1265
1266 int bgp_peer_gr_init(struct peer *peer)
1267 {
1268 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
1269 zlog_debug("%s called ..", __func__);
1270
1271 struct bgp_peer_gr local_Peer_GR_FSM[BGP_PEER_GR_MODE]
1272 [BGP_PEER_GR_EVENT_CMD] = {
1273 {
1274 /* PEER_HELPER Mode */
1275 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1276 { PEER_GR, bgp_peer_gr_action }, {PEER_INVALID, NULL },
1277 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1278 {PEER_DISABLE, bgp_peer_gr_action }, {PEER_INVALID, NULL },
1279 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1280 { PEER_INVALID, NULL }, {PEER_GLOBAL_INHERIT,
1281 bgp_peer_gr_action }
1282 },
1283 {
1284 /* PEER_GR Mode */
1285 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1286 { PEER_INVALID, NULL }, { PEER_GLOBAL_INHERIT,
1287 bgp_peer_gr_action },
1288 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1289 {PEER_DISABLE, bgp_peer_gr_action }, { PEER_INVALID, NULL },
1290 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1291 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1292 },
1293 {
1294 /* PEER_DISABLE Mode */
1295 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1296 { PEER_GR, bgp_peer_gr_action }, { PEER_INVALID, NULL },
1297 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1298 { PEER_INVALID, NULL }, { PEER_GLOBAL_INHERIT,
1299 bgp_peer_gr_action },
1300 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1301 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1302 },
1303 {
1304 /* PEER_INVALID Mode */
1305 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1306 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
1307 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1308 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
1309 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1310 { PEER_INVALID, NULL }, { PEER_INVALID, NULL },
1311 },
1312 {
1313 /* PEER_GLOBAL_INHERIT Mode */
1314 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1315 { PEER_GR, bgp_peer_gr_action }, { PEER_INVALID, NULL },
1316 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1317 { PEER_DISABLE, bgp_peer_gr_action}, { PEER_INVALID, NULL },
1318 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1319 { PEER_HELPER, bgp_peer_gr_action }, { PEER_INVALID, NULL }
1320 }
1321 };
1322 memcpy(&peer->PEER_GR_FSM, local_Peer_GR_FSM,
1323 sizeof(local_Peer_GR_FSM));
1324 peer->peer_gr_present_state = PEER_GLOBAL_INHERIT;
1325 bgp_peer_move_to_gr_mode(peer, PEER_GLOBAL_INHERIT);
1326
1327 return BGP_GR_SUCCESS;
1328 }
1329
1330 static void bgp_srv6_init(struct bgp *bgp)
1331 {
1332 bgp->srv6_enabled = false;
1333 memset(bgp->srv6_locator_name, 0, sizeof(bgp->srv6_locator_name));
1334 bgp->srv6_locator_chunks = list_new();
1335 bgp->srv6_functions = list_new();
1336 }
1337
1338 static void bgp_srv6_cleanup(struct bgp *bgp)
1339 {
1340 if (bgp->srv6_locator_chunks)
1341 list_delete(&bgp->srv6_locator_chunks);
1342 if (bgp->srv6_functions)
1343 list_delete(&bgp->srv6_functions);
1344 }
1345
1346 /* Allocate new peer object, implicitely locked. */
1347 struct peer *peer_new(struct bgp *bgp)
1348 {
1349 afi_t afi;
1350 safi_t safi;
1351 struct peer *peer;
1352 struct servent *sp;
1353
1354 /* bgp argument is absolutely required */
1355 assert(bgp);
1356
1357 /* Allocate new peer. */
1358 peer = XCALLOC(MTYPE_BGP_PEER, sizeof(struct peer));
1359
1360 /* Set default value. */
1361 peer->fd = -1;
1362 peer->v_start = BGP_INIT_START_TIMER;
1363 peer->v_connect = bgp->default_connect_retry;
1364 peer->status = Idle;
1365 peer->ostatus = Idle;
1366 peer->cur_event = peer->last_event = peer->last_major_event = 0;
1367 peer->bgp = bgp_lock(bgp);
1368 peer = peer_lock(peer); /* initial reference */
1369 peer->local_role = ROLE_UNDEFINED;
1370 peer->remote_role = ROLE_UNDEFINED;
1371 peer->password = NULL;
1372 peer->max_packet_size = BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE;
1373
1374 /* Set default flags. */
1375 FOREACH_AFI_SAFI (afi, safi) {
1376 SET_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY);
1377 SET_FLAG(peer->af_flags[afi][safi],
1378 PEER_FLAG_SEND_EXT_COMMUNITY);
1379 SET_FLAG(peer->af_flags[afi][safi],
1380 PEER_FLAG_SEND_LARGE_COMMUNITY);
1381
1382 SET_FLAG(peer->af_flags_invert[afi][safi],
1383 PEER_FLAG_SEND_COMMUNITY);
1384 SET_FLAG(peer->af_flags_invert[afi][safi],
1385 PEER_FLAG_SEND_EXT_COMMUNITY);
1386 SET_FLAG(peer->af_flags_invert[afi][safi],
1387 PEER_FLAG_SEND_LARGE_COMMUNITY);
1388 peer->addpath_type[afi][safi] = BGP_ADDPATH_NONE;
1389 peer->soo[afi][safi] = NULL;
1390 }
1391
1392 /* set nexthop-unchanged for l2vpn evpn by default */
1393 SET_FLAG(peer->af_flags[AFI_L2VPN][SAFI_EVPN],
1394 PEER_FLAG_NEXTHOP_UNCHANGED);
1395
1396 SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1397
1398 /* Initialize per peer bgp GR FSM */
1399 bgp_peer_gr_init(peer);
1400
1401 /* Create buffers. */
1402 peer->ibuf = stream_fifo_new();
1403 peer->obuf = stream_fifo_new();
1404 pthread_mutex_init(&peer->io_mtx, NULL);
1405
1406 /* We use a larger buffer for peer->obuf_work in the event that:
1407 * - We RX a BGP_UPDATE where the attributes alone are just
1408 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1409 * - The user configures an outbound route-map that does many as-path
1410 * prepends or adds many communities. At most they can have
1411 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1412 * large they can make the attributes.
1413 *
1414 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1415 * bounds checking for every single attribute as we construct an
1416 * UPDATE.
1417 */
1418 peer->obuf_work =
1419 stream_new(BGP_MAX_PACKET_SIZE + BGP_MAX_PACKET_SIZE_OVERFLOW);
1420 peer->ibuf_work =
1421 ringbuf_new(BGP_MAX_PACKET_SIZE * BGP_READ_PACKET_MAX);
1422
1423 peer->scratch = stream_new(BGP_MAX_PACKET_SIZE);
1424
1425 bgp_sync_init(peer);
1426
1427 /* Get service port number. */
1428 sp = getservbyname("bgp", "tcp");
1429 peer->port = (sp == NULL) ? BGP_PORT_DEFAULT : ntohs(sp->s_port);
1430
1431 QOBJ_REG(peer, peer);
1432 return peer;
1433 }
1434
1435 /*
1436 * This function is invoked when a duplicate peer structure associated with
1437 * a neighbor is being deleted. If this about-to-be-deleted structure is
1438 * the one with all the config, then we have to copy over the info.
1439 */
1440 void peer_xfer_config(struct peer *peer_dst, struct peer *peer_src)
1441 {
1442 struct peer_af *paf;
1443 afi_t afi;
1444 safi_t safi;
1445 int afidx;
1446
1447 assert(peer_src);
1448 assert(peer_dst);
1449
1450 /* The following function is used by both peer group config copy to
1451 * individual peer and when we transfer config
1452 */
1453 if (peer_src->change_local_as)
1454 peer_dst->change_local_as = peer_src->change_local_as;
1455
1456 /* peer flags apply */
1457 peer_dst->flags = peer_src->flags;
1458
1459 peer_dst->peer_gr_present_state = peer_src->peer_gr_present_state;
1460 peer_dst->peer_gr_new_status_flag = peer_src->peer_gr_new_status_flag;
1461
1462 peer_dst->local_as = peer_src->local_as;
1463 peer_dst->port = peer_src->port;
1464 /* copy tcp_mss value */
1465 peer_dst->tcp_mss = peer_src->tcp_mss;
1466 (void)peer_sort(peer_dst);
1467 peer_dst->rmap_type = peer_src->rmap_type;
1468 peer_dst->local_role = peer_src->local_role;
1469
1470 peer_dst->max_packet_size = peer_src->max_packet_size;
1471
1472 /* Timers */
1473 peer_dst->holdtime = peer_src->holdtime;
1474 peer_dst->keepalive = peer_src->keepalive;
1475 peer_dst->connect = peer_src->connect;
1476 peer_dst->delayopen = peer_src->delayopen;
1477 peer_dst->v_holdtime = peer_src->v_holdtime;
1478 peer_dst->v_keepalive = peer_src->v_keepalive;
1479 peer_dst->routeadv = peer_src->routeadv;
1480 peer_dst->v_routeadv = peer_src->v_routeadv;
1481 peer_dst->v_delayopen = peer_src->v_delayopen;
1482
1483 /* password apply */
1484 if (peer_src->password && !peer_dst->password)
1485 peer_dst->password =
1486 XSTRDUP(MTYPE_PEER_PASSWORD, peer_src->password);
1487
1488 FOREACH_AFI_SAFI (afi, safi) {
1489 peer_dst->afc[afi][safi] = peer_src->afc[afi][safi];
1490 peer_dst->af_flags[afi][safi] = peer_src->af_flags[afi][safi];
1491 peer_dst->allowas_in[afi][safi] =
1492 peer_src->allowas_in[afi][safi];
1493 peer_dst->weight[afi][safi] = peer_src->weight[afi][safi];
1494 peer_dst->addpath_type[afi][safi] =
1495 peer_src->addpath_type[afi][safi];
1496 }
1497
1498 for (afidx = BGP_AF_START; afidx < BGP_AF_MAX; afidx++) {
1499 paf = peer_src->peer_af_array[afidx];
1500 if (paf != NULL) {
1501 if (!peer_af_find(peer_dst, paf->afi, paf->safi))
1502 peer_af_create(peer_dst, paf->afi, paf->safi);
1503 }
1504 }
1505
1506 /* update-source apply */
1507 if (peer_src->update_source) {
1508 if (peer_dst->update_source)
1509 sockunion_free(peer_dst->update_source);
1510 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer_dst->update_if);
1511 peer_dst->update_source =
1512 sockunion_dup(peer_src->update_source);
1513 } else if (peer_src->update_if) {
1514 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer_dst->update_if);
1515 if (peer_dst->update_source) {
1516 sockunion_free(peer_dst->update_source);
1517 peer_dst->update_source = NULL;
1518 }
1519 peer_dst->update_if =
1520 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, peer_src->update_if);
1521 }
1522
1523 if (peer_src->ifname) {
1524 XFREE(MTYPE_BGP_PEER_IFNAME, peer_dst->ifname);
1525
1526 peer_dst->ifname =
1527 XSTRDUP(MTYPE_BGP_PEER_IFNAME, peer_src->ifname);
1528 }
1529 }
1530
1531 static int bgp_peer_conf_if_to_su_update_v4(struct peer *peer,
1532 struct interface *ifp)
1533 {
1534 struct connected *ifc;
1535 struct prefix p;
1536 uint32_t addr;
1537 struct listnode *node;
1538
1539 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1540 * IPv4 address of the other end.
1541 */
1542 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
1543 if (ifc->address && (ifc->address->family == AF_INET)) {
1544 prefix_copy(&p, CONNECTED_PREFIX(ifc));
1545 if (p.prefixlen == 30) {
1546 peer->su.sa.sa_family = AF_INET;
1547 addr = ntohl(p.u.prefix4.s_addr);
1548 if (addr % 4 == 1)
1549 peer->su.sin.sin_addr.s_addr =
1550 htonl(addr + 1);
1551 else if (addr % 4 == 2)
1552 peer->su.sin.sin_addr.s_addr =
1553 htonl(addr - 1);
1554 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1555 peer->su.sin.sin_len =
1556 sizeof(struct sockaddr_in);
1557 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1558 return 1;
1559 } else if (p.prefixlen == 31) {
1560 peer->su.sa.sa_family = AF_INET;
1561 addr = ntohl(p.u.prefix4.s_addr);
1562 if (addr % 2 == 0)
1563 peer->su.sin.sin_addr.s_addr =
1564 htonl(addr + 1);
1565 else
1566 peer->su.sin.sin_addr.s_addr =
1567 htonl(addr - 1);
1568 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1569 peer->su.sin.sin_len =
1570 sizeof(struct sockaddr_in);
1571 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1572 return 1;
1573 } else if (bgp_debug_neighbor_events(peer))
1574 zlog_debug(
1575 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1576 peer->conf_if);
1577 }
1578 }
1579
1580 return 0;
1581 }
1582
1583 static bool bgp_peer_conf_if_to_su_update_v6(struct peer *peer,
1584 struct interface *ifp)
1585 {
1586 struct nbr_connected *ifc_nbr;
1587
1588 /* Have we learnt the peer's IPv6 link-local address? */
1589 if (ifp->nbr_connected
1590 && (ifc_nbr = listnode_head(ifp->nbr_connected))) {
1591 peer->su.sa.sa_family = AF_INET6;
1592 memcpy(&peer->su.sin6.sin6_addr, &ifc_nbr->address->u.prefix,
1593 sizeof(struct in6_addr));
1594 #ifdef SIN6_LEN
1595 peer->su.sin6.sin6_len = sizeof(struct sockaddr_in6);
1596 #endif
1597 peer->su.sin6.sin6_scope_id = ifp->ifindex;
1598 return true;
1599 }
1600
1601 return false;
1602 }
1603
1604 /*
1605 * Set or reset the peer address socketunion structure based on the
1606 * learnt/derived peer address. If the address has changed, update the
1607 * password on the listen socket, if needed.
1608 */
1609 void bgp_peer_conf_if_to_su_update(struct peer *peer)
1610 {
1611 struct interface *ifp;
1612 int prev_family;
1613 int peer_addr_updated = 0;
1614
1615 if (!peer->conf_if)
1616 return;
1617
1618 /*
1619 * Our peer structure is stored in the bgp->peerhash
1620 * release it before we modify anything.
1621 */
1622 hash_release(peer->bgp->peerhash, peer);
1623
1624 prev_family = peer->su.sa.sa_family;
1625 if ((ifp = if_lookup_by_name(peer->conf_if, peer->bgp->vrf_id))) {
1626 peer->ifp = ifp;
1627 /* If BGP unnumbered is not "v6only", we first see if we can
1628 * derive the
1629 * peer's IPv4 address.
1630 */
1631 if (!CHECK_FLAG(peer->flags, PEER_FLAG_IFPEER_V6ONLY))
1632 peer_addr_updated =
1633 bgp_peer_conf_if_to_su_update_v4(peer, ifp);
1634
1635 /* If "v6only" or we can't derive peer's IPv4 address, see if
1636 * we've
1637 * learnt the peer's IPv6 link-local address. This is from the
1638 * source
1639 * IPv6 address in router advertisement.
1640 */
1641 if (!peer_addr_updated)
1642 peer_addr_updated =
1643 bgp_peer_conf_if_to_su_update_v6(peer, ifp);
1644 }
1645 /* If we could derive the peer address, we may need to install the
1646 * password
1647 * configured for the peer, if any, on the listen socket. Otherwise,
1648 * mark
1649 * that peer's address is not available and uninstall the password, if
1650 * needed.
1651 */
1652 if (peer_addr_updated) {
1653 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)
1654 && prev_family == AF_UNSPEC)
1655 bgp_md5_set(peer);
1656 } else {
1657 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)
1658 && prev_family != AF_UNSPEC)
1659 bgp_md5_unset(peer);
1660 peer->su.sa.sa_family = AF_UNSPEC;
1661 memset(&peer->su.sin6.sin6_addr, 0, sizeof(struct in6_addr));
1662 }
1663
1664 /*
1665 * Since our su changed we need to del/add peer to the peerhash
1666 */
1667 (void)hash_get(peer->bgp->peerhash, peer, hash_alloc_intern);
1668 }
1669
1670 void bgp_recalculate_afi_safi_bestpaths(struct bgp *bgp, afi_t afi, safi_t safi)
1671 {
1672 struct bgp_dest *dest, *ndest;
1673 struct bgp_table *table;
1674
1675 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
1676 dest = bgp_route_next(dest)) {
1677 table = bgp_dest_get_bgp_table_info(dest);
1678 if (table != NULL) {
1679 /* Special handling for 2-level routing
1680 * tables. */
1681 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
1682 || safi == SAFI_EVPN) {
1683 for (ndest = bgp_table_top(table); ndest;
1684 ndest = bgp_route_next(ndest))
1685 bgp_process(bgp, ndest, afi, safi);
1686 } else
1687 bgp_process(bgp, dest, afi, safi);
1688 }
1689 }
1690 }
1691
1692 /* Force a bestpath recalculation for all prefixes. This is used
1693 * when 'bgp bestpath' commands are entered.
1694 */
1695 void bgp_recalculate_all_bestpaths(struct bgp *bgp)
1696 {
1697 afi_t afi;
1698 safi_t safi;
1699
1700 FOREACH_AFI_SAFI (afi, safi) {
1701 bgp_recalculate_afi_safi_bestpaths(bgp, afi, safi);
1702 }
1703 }
1704
1705 /*
1706 * Create new BGP peer.
1707 *
1708 * conf_if and su are mutually exclusive if configuring from the cli.
1709 * If we are handing a doppelganger, then we *must* pass in both
1710 * the original peer's su and conf_if, so that we can appropriately
1711 * track the bgp->peerhash( ie we don't want to remove the current
1712 * one from the config ).
1713 */
1714 struct peer *peer_create(union sockunion *su, const char *conf_if,
1715 struct bgp *bgp, as_t local_as, as_t remote_as,
1716 int as_type, struct peer_group *group)
1717 {
1718 int active;
1719 struct peer *peer;
1720 char buf[SU_ADDRSTRLEN];
1721 afi_t afi;
1722 safi_t safi;
1723
1724 peer = peer_new(bgp);
1725 if (conf_if) {
1726 peer->conf_if = XSTRDUP(MTYPE_PEER_CONF_IF, conf_if);
1727 if (su)
1728 peer->su = *su;
1729 else
1730 bgp_peer_conf_if_to_su_update(peer);
1731 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
1732 peer->host = XSTRDUP(MTYPE_BGP_PEER_HOST, conf_if);
1733 } else if (su) {
1734 peer->su = *su;
1735 sockunion2str(su, buf, SU_ADDRSTRLEN);
1736 XFREE(MTYPE_BGP_PEER_HOST, peer->host);
1737 peer->host = XSTRDUP(MTYPE_BGP_PEER_HOST, buf);
1738 }
1739 peer->local_as = local_as;
1740 peer->as = remote_as;
1741 peer->as_type = as_type;
1742 peer->local_id = bgp->router_id;
1743 peer->v_holdtime = bgp->default_holdtime;
1744 peer->v_keepalive = bgp->default_keepalive;
1745 peer->v_routeadv = (peer_sort(peer) == BGP_PEER_IBGP)
1746 ? BGP_DEFAULT_IBGP_ROUTEADV
1747 : BGP_DEFAULT_EBGP_ROUTEADV;
1748 if (bgp_config_inprocess())
1749 peer->shut_during_cfg = true;
1750
1751 peer = peer_lock(peer); /* bgp peer list reference */
1752 peer->group = group;
1753 listnode_add_sort(bgp->peer, peer);
1754 (void)hash_get(bgp->peerhash, peer, hash_alloc_intern);
1755
1756 /* Adjust update-group coalesce timer heuristics for # peers. */
1757 if (bgp->heuristic_coalesce) {
1758 long ct = BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1759 + (bgp->peer->count
1760 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME);
1761 bgp->coalesce_time = MIN(BGP_MAX_SUBGROUP_COALESCE_TIME, ct);
1762 }
1763
1764 active = peer_active(peer);
1765 if (!active) {
1766 if (peer->su.sa.sa_family == AF_UNSPEC)
1767 peer->last_reset = PEER_DOWN_NBR_ADDR;
1768 else
1769 peer->last_reset = PEER_DOWN_NOAFI_ACTIVATED;
1770 }
1771
1772 /* Last read and reset time set */
1773 peer->readtime = peer->resettime = monotime(NULL);
1774
1775 /* Default TTL set. */
1776 peer->ttl = (peer->sort == BGP_PEER_IBGP) ? MAXTTL : BGP_DEFAULT_TTL;
1777
1778 /* Default configured keepalives count for shutdown rtt command */
1779 peer->rtt_keepalive_conf = 1;
1780
1781 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
1782
1783 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1784 * neighbor for the corresponding address family. IPv4 Unicast is
1785 * the only address family enabled by default without expliict
1786 * configuration.
1787 */
1788 FOREACH_AFI_SAFI (afi, safi) {
1789 if (bgp->default_af[afi][safi]) {
1790 peer->afc[afi][safi] = 1;
1791 peer_af_create(peer, afi, safi);
1792 }
1793 }
1794
1795 /* auto shutdown if configured */
1796 if (bgp->autoshutdown)
1797 peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
1798 /* Set up peer's events and timers. */
1799 else if (!active && peer_active(peer))
1800 bgp_timer_set(peer);
1801
1802 bgp_peer_gr_flags_update(peer);
1803 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
1804
1805 return peer;
1806 }
1807
1808 /* Make accept BGP peer. This function is only called from the test code */
1809 struct peer *peer_create_accept(struct bgp *bgp)
1810 {
1811 struct peer *peer;
1812
1813 peer = peer_new(bgp);
1814
1815 peer = peer_lock(peer); /* bgp peer list reference */
1816 listnode_add_sort(bgp->peer, peer);
1817
1818 return peer;
1819 }
1820
1821 /*
1822 * Return true if we have a peer configured to use this afi/safi
1823 */
1824 bool bgp_afi_safi_peer_exists(struct bgp *bgp, afi_t afi, safi_t safi)
1825 {
1826 struct listnode *node;
1827 struct peer *peer;
1828
1829 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
1830 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
1831 continue;
1832
1833 if (peer->afc[afi][safi])
1834 return true;
1835 }
1836
1837 return false;
1838 }
1839
1840 /* Change peer's AS number. */
1841 void peer_as_change(struct peer *peer, as_t as, int as_specified)
1842 {
1843 afi_t afi;
1844 safi_t safi;
1845 enum bgp_peer_sort origtype, newtype;
1846
1847 /* Stop peer. */
1848 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1849 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
1850 peer->last_reset = PEER_DOWN_REMOTE_AS_CHANGE;
1851 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1852 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
1853 } else
1854 bgp_session_reset(peer);
1855 }
1856 origtype = peer_sort_lookup(peer);
1857 peer->as = as;
1858 peer->as_type = as_specified;
1859
1860 if (bgp_config_check(peer->bgp, BGP_CONFIG_CONFEDERATION)
1861 && !bgp_confederation_peers_check(peer->bgp, as)
1862 && peer->bgp->as != as)
1863 peer->local_as = peer->bgp->confed_id;
1864 else
1865 peer->local_as = peer->bgp->as;
1866
1867 newtype = peer_sort(peer);
1868 /* Advertisement-interval reset */
1869 if (!CHECK_FLAG(peer->flags, PEER_FLAG_ROUTEADV)) {
1870 peer->v_routeadv = (newtype == BGP_PEER_IBGP)
1871 ? BGP_DEFAULT_IBGP_ROUTEADV
1872 : BGP_DEFAULT_EBGP_ROUTEADV;
1873 }
1874
1875 /* TTL reset */
1876 if (newtype == BGP_PEER_IBGP)
1877 peer->ttl = MAXTTL;
1878 else if (origtype == BGP_PEER_IBGP)
1879 peer->ttl = BGP_DEFAULT_TTL;
1880
1881 /* reflector-client reset */
1882 if (newtype != BGP_PEER_IBGP) {
1883
1884 FOREACH_AFI_SAFI (afi, safi)
1885 UNSET_FLAG(peer->af_flags[afi][safi],
1886 PEER_FLAG_ORR_GROUP);
1887
1888 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_UNICAST],
1889 PEER_FLAG_REFLECTOR_CLIENT);
1890 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_MULTICAST],
1891 PEER_FLAG_REFLECTOR_CLIENT);
1892 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_LABELED_UNICAST],
1893 PEER_FLAG_REFLECTOR_CLIENT);
1894 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_MPLS_VPN],
1895 PEER_FLAG_REFLECTOR_CLIENT);
1896 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_ENCAP],
1897 PEER_FLAG_REFLECTOR_CLIENT);
1898 UNSET_FLAG(peer->af_flags[AFI_IP][SAFI_FLOWSPEC],
1899 PEER_FLAG_REFLECTOR_CLIENT);
1900 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_UNICAST],
1901 PEER_FLAG_REFLECTOR_CLIENT);
1902 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_MULTICAST],
1903 PEER_FLAG_REFLECTOR_CLIENT);
1904 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_LABELED_UNICAST],
1905 PEER_FLAG_REFLECTOR_CLIENT);
1906 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_MPLS_VPN],
1907 PEER_FLAG_REFLECTOR_CLIENT);
1908 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_ENCAP],
1909 PEER_FLAG_REFLECTOR_CLIENT);
1910 UNSET_FLAG(peer->af_flags[AFI_IP6][SAFI_FLOWSPEC],
1911 PEER_FLAG_REFLECTOR_CLIENT);
1912 UNSET_FLAG(peer->af_flags[AFI_L2VPN][SAFI_EVPN],
1913 PEER_FLAG_REFLECTOR_CLIENT);
1914 }
1915 }
1916
1917 /* If peer does not exist, create new one. If peer already exists,
1918 set AS number to the peer. */
1919 int peer_remote_as(struct bgp *bgp, union sockunion *su, const char *conf_if,
1920 as_t *as, int as_type)
1921 {
1922 struct peer *peer;
1923 as_t local_as;
1924
1925 if (conf_if)
1926 peer = peer_lookup_by_conf_if(bgp, conf_if);
1927 else
1928 peer = peer_lookup(bgp, su);
1929
1930 if (peer) {
1931 /* Not allowed for a dynamic peer. */
1932 if (peer_dynamic_neighbor(peer)) {
1933 *as = peer->as;
1934 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER;
1935 }
1936
1937 /* When this peer is a member of peer-group. */
1938 if (peer->group) {
1939 /* peer-group already has AS number/internal/external */
1940 if (peer->group->conf->as
1941 || peer->group->conf->as_type) {
1942 /* Return peer group's AS number. */
1943 *as = peer->group->conf->as;
1944 return BGP_ERR_PEER_GROUP_MEMBER;
1945 }
1946
1947 enum bgp_peer_sort peer_sort_type =
1948 peer_sort(peer->group->conf);
1949
1950 /* Explicit AS numbers used, compare AS numbers */
1951 if (as_type == AS_SPECIFIED) {
1952 if (((peer_sort_type == BGP_PEER_IBGP)
1953 && (bgp->as != *as))
1954 || ((peer_sort_type == BGP_PEER_EBGP)
1955 && (bgp->as == *as))) {
1956 *as = peer->as;
1957 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
1958 }
1959 } else {
1960 /* internal/external used, compare as-types */
1961 if (((peer_sort_type == BGP_PEER_IBGP)
1962 && (as_type != AS_INTERNAL))
1963 || ((peer_sort_type == BGP_PEER_EBGP)
1964 && (as_type != AS_EXTERNAL))) {
1965 *as = peer->as;
1966 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
1967 }
1968 }
1969 }
1970
1971 /* Existing peer's AS number change. */
1972 if (((peer->as_type == AS_SPECIFIED) && peer->as != *as)
1973 || (peer->as_type != as_type))
1974 peer_as_change(peer, *as, as_type);
1975 } else {
1976 if (conf_if)
1977 return BGP_ERR_NO_INTERFACE_CONFIG;
1978
1979 /* If the peer is not part of our confederation, and its not an
1980 iBGP peer then spoof the source AS */
1981 if (bgp_config_check(bgp, BGP_CONFIG_CONFEDERATION)
1982 && !bgp_confederation_peers_check(bgp, *as)
1983 && bgp->as != *as)
1984 local_as = bgp->confed_id;
1985 else
1986 local_as = bgp->as;
1987
1988 peer_create(su, conf_if, bgp, local_as, *as, as_type, NULL);
1989 }
1990
1991 return 0;
1992 }
1993
1994 const char *bgp_get_name_by_role(uint8_t role)
1995 {
1996 switch (role) {
1997 case ROLE_PROVIDER:
1998 return "provider";
1999 case ROLE_RS_SERVER:
2000 return "rs-server";
2001 case ROLE_RS_CLIENT:
2002 return "rs-client";
2003 case ROLE_CUSTOMER:
2004 return "customer";
2005 case ROLE_PEER:
2006 return "peer";
2007 case ROLE_UNDEFINED:
2008 return "undefined";
2009 }
2010 return "unknown";
2011 }
2012
2013 static void peer_group2peer_config_copy_af(struct peer_group *group,
2014 struct peer *peer, afi_t afi,
2015 safi_t safi)
2016 {
2017 int in = FILTER_IN;
2018 int out = FILTER_OUT;
2019 uint64_t flags_tmp;
2020 uint64_t pflags_ovrd;
2021 uint8_t *pfilter_ovrd;
2022 struct peer *conf;
2023
2024 conf = group->conf;
2025 pflags_ovrd = peer->af_flags_override[afi][safi];
2026 pfilter_ovrd = &peer->filter_override[afi][safi][in];
2027
2028 /* peer af_flags apply */
2029 flags_tmp = conf->af_flags[afi][safi] & ~pflags_ovrd;
2030 flags_tmp ^= conf->af_flags_invert[afi][safi]
2031 ^ peer->af_flags_invert[afi][safi];
2032 flags_tmp &= ~pflags_ovrd;
2033
2034 UNSET_FLAG(peer->af_flags[afi][safi], ~pflags_ovrd);
2035 SET_FLAG(peer->af_flags[afi][safi], flags_tmp);
2036 SET_FLAG(peer->af_flags_invert[afi][safi],
2037 conf->af_flags_invert[afi][safi]);
2038
2039 /* maximum-prefix */
2040 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_MAX_PREFIX)) {
2041 PEER_ATTR_INHERIT(peer, group, pmax[afi][safi]);
2042 PEER_ATTR_INHERIT(peer, group, pmax_threshold[afi][safi]);
2043 PEER_ATTR_INHERIT(peer, group, pmax_restart[afi][safi]);
2044 }
2045
2046 /* maximum-prefix-out */
2047 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_MAX_PREFIX_OUT))
2048 PEER_ATTR_INHERIT(peer, group, pmax_out[afi][safi]);
2049
2050 /* allowas-in */
2051 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_ALLOWAS_IN))
2052 PEER_ATTR_INHERIT(peer, group, allowas_in[afi][safi]);
2053
2054 /* soo */
2055 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_SOO))
2056 PEER_ATTR_INHERIT(peer, group, soo[afi][safi]);
2057
2058 /* weight */
2059 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_WEIGHT))
2060 PEER_ATTR_INHERIT(peer, group, weight[afi][safi]);
2061
2062 /* default-originate route-map */
2063 if (!CHECK_FLAG(pflags_ovrd, PEER_FLAG_DEFAULT_ORIGINATE)) {
2064 PEER_STR_ATTR_INHERIT(peer, group, default_rmap[afi][safi].name,
2065 MTYPE_ROUTE_MAP_NAME);
2066 PEER_ATTR_INHERIT(peer, group, default_rmap[afi][safi].map);
2067 }
2068
2069 /* inbound filter apply */
2070 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_DISTRIBUTE_LIST)) {
2071 PEER_STR_ATTR_INHERIT(peer, group,
2072 filter[afi][safi].dlist[in].name,
2073 MTYPE_BGP_FILTER_NAME);
2074 PEER_ATTR_INHERIT(peer, group,
2075 filter[afi][safi].dlist[in].alist);
2076 }
2077
2078 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_PREFIX_LIST)) {
2079 PEER_STR_ATTR_INHERIT(peer, group,
2080 filter[afi][safi].plist[in].name,
2081 MTYPE_BGP_FILTER_NAME);
2082 PEER_ATTR_INHERIT(peer, group,
2083 filter[afi][safi].plist[in].plist);
2084 }
2085
2086 if (!CHECK_FLAG(pfilter_ovrd[in], PEER_FT_FILTER_LIST)) {
2087 PEER_STR_ATTR_INHERIT(peer, group,
2088 filter[afi][safi].aslist[in].name,
2089 MTYPE_BGP_FILTER_NAME);
2090 PEER_ATTR_INHERIT(peer, group,
2091 filter[afi][safi].aslist[in].aslist);
2092 }
2093
2094 if (!CHECK_FLAG(pfilter_ovrd[RMAP_IN], PEER_FT_ROUTE_MAP)) {
2095 PEER_STR_ATTR_INHERIT(peer, group,
2096 filter[afi][safi].map[in].name,
2097 MTYPE_BGP_FILTER_NAME);
2098 PEER_ATTR_INHERIT(peer, group,
2099 filter[afi][safi].map[RMAP_IN].map);
2100 }
2101
2102 /* outbound filter apply */
2103 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_DISTRIBUTE_LIST)) {
2104 PEER_STR_ATTR_INHERIT(peer, group,
2105 filter[afi][safi].dlist[out].name,
2106 MTYPE_BGP_FILTER_NAME);
2107 PEER_ATTR_INHERIT(peer, group,
2108 filter[afi][safi].dlist[out].alist);
2109 }
2110
2111 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_PREFIX_LIST)) {
2112 PEER_STR_ATTR_INHERIT(peer, group,
2113 filter[afi][safi].plist[out].name,
2114 MTYPE_BGP_FILTER_NAME);
2115 PEER_ATTR_INHERIT(peer, group,
2116 filter[afi][safi].plist[out].plist);
2117 }
2118
2119 if (!CHECK_FLAG(pfilter_ovrd[out], PEER_FT_FILTER_LIST)) {
2120 PEER_STR_ATTR_INHERIT(peer, group,
2121 filter[afi][safi].aslist[out].name,
2122 MTYPE_BGP_FILTER_NAME);
2123 PEER_ATTR_INHERIT(peer, group,
2124 filter[afi][safi].aslist[out].aslist);
2125 }
2126
2127 if (!CHECK_FLAG(pfilter_ovrd[RMAP_OUT], PEER_FT_ROUTE_MAP)) {
2128 PEER_STR_ATTR_INHERIT(peer, group,
2129 filter[afi][safi].map[RMAP_OUT].name,
2130 MTYPE_BGP_FILTER_NAME);
2131 PEER_ATTR_INHERIT(peer, group,
2132 filter[afi][safi].map[RMAP_OUT].map);
2133 }
2134
2135 /* nondirectional filter apply */
2136 if (!CHECK_FLAG(pfilter_ovrd[0], PEER_FT_UNSUPPRESS_MAP)) {
2137 PEER_STR_ATTR_INHERIT(peer, group, filter[afi][safi].usmap.name,
2138 MTYPE_BGP_FILTER_NAME);
2139 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].usmap.map);
2140 }
2141
2142 /* Conditional Advertisements */
2143 if (!CHECK_FLAG(pfilter_ovrd[RMAP_OUT], PEER_FT_ADVERTISE_MAP)) {
2144 PEER_STR_ATTR_INHERIT(peer, group,
2145 filter[afi][safi].advmap.aname,
2146 MTYPE_BGP_FILTER_NAME);
2147 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].advmap.amap);
2148 PEER_STR_ATTR_INHERIT(peer, group,
2149 filter[afi][safi].advmap.cname,
2150 MTYPE_BGP_FILTER_NAME);
2151 PEER_ATTR_INHERIT(peer, group, filter[afi][safi].advmap.cmap);
2152 PEER_ATTR_INHERIT(peer, group,
2153 filter[afi][safi].advmap.condition);
2154 }
2155
2156 if (peer->addpath_type[afi][safi] == BGP_ADDPATH_NONE) {
2157 peer->addpath_type[afi][safi] = conf->addpath_type[afi][safi];
2158 bgp_addpath_type_changed(conf->bgp);
2159 }
2160 }
2161
2162 static int peer_activate_af(struct peer *peer, afi_t afi, safi_t safi)
2163 {
2164 int active;
2165 struct peer *other;
2166
2167 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2168 flog_err(EC_BGP_PEER_GROUP, "%s was called for peer-group %s",
2169 __func__, peer->host);
2170 return 1;
2171 }
2172
2173 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2174 */
2175 if ((safi == SAFI_UNICAST && peer->afc[afi][SAFI_LABELED_UNICAST])
2176 || (safi == SAFI_LABELED_UNICAST && peer->afc[afi][SAFI_UNICAST]))
2177 return BGP_ERR_PEER_SAFI_CONFLICT;
2178
2179 /* Nothing to do if we've already activated this peer */
2180 if (peer->afc[afi][safi])
2181 return 0;
2182
2183 if (peer_af_create(peer, afi, safi) == NULL)
2184 return 1;
2185
2186 active = peer_active(peer);
2187 peer->afc[afi][safi] = 1;
2188
2189 if (peer->group)
2190 peer_group2peer_config_copy_af(peer->group, peer, afi, safi);
2191
2192 if (!active && peer_active(peer)) {
2193 bgp_timer_set(peer);
2194 } else {
2195 if (peer_established(peer)) {
2196 if (CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_RCV)) {
2197 peer->afc_adv[afi][safi] = 1;
2198 bgp_capability_send(peer, afi, safi,
2199 CAPABILITY_CODE_MP,
2200 CAPABILITY_ACTION_SET);
2201 if (peer->afc_recv[afi][safi]) {
2202 peer->afc_nego[afi][safi] = 1;
2203 bgp_announce_route(peer, afi, safi,
2204 false);
2205 }
2206 } else {
2207 peer->last_reset = PEER_DOWN_AF_ACTIVATE;
2208 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2209 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2210 }
2211 }
2212 if (peer->status == OpenSent || peer->status == OpenConfirm) {
2213 peer->last_reset = PEER_DOWN_AF_ACTIVATE;
2214 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2215 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2216 }
2217 /*
2218 * If we are turning on a AFI/SAFI locally and we've
2219 * started bringing a peer up, we need to tell
2220 * the other peer to restart because we might loose
2221 * configuration here because when the doppelganger
2222 * gets to a established state due to how
2223 * we resolve we could just overwrite the afi/safi
2224 * activation.
2225 */
2226 other = peer->doppelganger;
2227 if (other
2228 && (other->status == OpenSent
2229 || other->status == OpenConfirm)) {
2230 other->last_reset = PEER_DOWN_AF_ACTIVATE;
2231 bgp_notify_send(other, BGP_NOTIFY_CEASE,
2232 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2233 }
2234 }
2235
2236 return 0;
2237 }
2238
2239 /* Activate the peer or peer group for specified AFI and SAFI. */
2240 int peer_activate(struct peer *peer, afi_t afi, safi_t safi)
2241 {
2242 int ret = 0;
2243 struct peer_group *group;
2244 struct listnode *node, *nnode;
2245 struct peer *tmp_peer;
2246 struct bgp *bgp;
2247
2248 /* Nothing to do if we've already activated this peer */
2249 if (peer->afc[afi][safi])
2250 return ret;
2251
2252 bgp = peer->bgp;
2253
2254 /* This is a peer-group so activate all of the members of the
2255 * peer-group as well */
2256 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2257
2258 /* Do not activate a peer for both SAFI_UNICAST and
2259 * SAFI_LABELED_UNICAST */
2260 if ((safi == SAFI_UNICAST
2261 && peer->afc[afi][SAFI_LABELED_UNICAST])
2262 || (safi == SAFI_LABELED_UNICAST
2263 && peer->afc[afi][SAFI_UNICAST]))
2264 return BGP_ERR_PEER_SAFI_CONFLICT;
2265
2266 peer->afc[afi][safi] = 1;
2267 group = peer->group;
2268
2269 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, tmp_peer)) {
2270 ret |= peer_activate_af(tmp_peer, afi, safi);
2271 }
2272 } else {
2273 ret |= peer_activate_af(peer, afi, safi);
2274 }
2275
2276 /* If this is the first peer to be activated for this
2277 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2278 if (ret != BGP_ERR_PEER_SAFI_CONFLICT && safi == SAFI_LABELED_UNICAST
2279 && !bgp->allocate_mpls_labels[afi][SAFI_UNICAST]) {
2280
2281 if (BGP_DEBUG(zebra, ZEBRA))
2282 zlog_debug(
2283 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2284
2285 bgp->allocate_mpls_labels[afi][SAFI_UNICAST] = 1;
2286 bgp_recalculate_afi_safi_bestpaths(bgp, afi, SAFI_UNICAST);
2287 }
2288
2289 if (safi == SAFI_FLOWSPEC) {
2290 /* connect to table manager */
2291 bgp_zebra_init_tm_connect(bgp);
2292 }
2293 return ret;
2294 }
2295
2296 static bool non_peergroup_deactivate_af(struct peer *peer, afi_t afi,
2297 safi_t safi)
2298 {
2299 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2300 flog_err(EC_BGP_PEER_GROUP, "%s was called for peer-group %s",
2301 __func__, peer->host);
2302 return true;
2303 }
2304
2305 /* Nothing to do if we've already deactivated this peer */
2306 if (!peer->afc[afi][safi])
2307 return false;
2308
2309 /* De-activate the address family configuration. */
2310 peer->afc[afi][safi] = 0;
2311
2312 if (peer_af_delete(peer, afi, safi) != 0) {
2313 flog_err(EC_BGP_PEER_DELETE,
2314 "couldn't delete af structure for peer %s(%s, %s)",
2315 peer->host, afi2str(afi), safi2str(safi));
2316 return true;
2317 }
2318
2319 if (peer_established(peer)) {
2320 if (CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_RCV)) {
2321 peer->afc_adv[afi][safi] = 0;
2322 peer->afc_nego[afi][safi] = 0;
2323
2324 if (peer_active_nego(peer)) {
2325 bgp_capability_send(peer, afi, safi,
2326 CAPABILITY_CODE_MP,
2327 CAPABILITY_ACTION_UNSET);
2328 bgp_clear_route(peer, afi, safi);
2329 peer->pcount[afi][safi] = 0;
2330 } else {
2331 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2332 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2333 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2334 }
2335 } else {
2336 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2337 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2338 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
2339 }
2340 }
2341
2342 return false;
2343 }
2344
2345 int peer_deactivate(struct peer *peer, afi_t afi, safi_t safi)
2346 {
2347 int ret = 0;
2348 struct peer_group *group;
2349 struct peer *tmp_peer;
2350 struct listnode *node, *nnode;
2351 struct bgp *bgp;
2352
2353 /* Nothing to do if we've already de-activated this peer */
2354 if (!peer->afc[afi][safi])
2355 return ret;
2356
2357 /* This is a peer-group so de-activate all of the members of the
2358 * peer-group as well */
2359 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2360 peer->afc[afi][safi] = 0;
2361 group = peer->group;
2362
2363 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, tmp_peer)) {
2364 ret |= non_peergroup_deactivate_af(tmp_peer, afi, safi);
2365 }
2366 } else {
2367 ret |= non_peergroup_deactivate_af(peer, afi, safi);
2368 }
2369
2370 bgp = peer->bgp;
2371
2372 /* If this is the last peer to be deactivated for this
2373 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2374 if (safi == SAFI_LABELED_UNICAST
2375 && bgp->allocate_mpls_labels[afi][SAFI_UNICAST]
2376 && !bgp_afi_safi_peer_exists(bgp, afi, safi)) {
2377
2378 if (BGP_DEBUG(zebra, ZEBRA))
2379 zlog_debug(
2380 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2381
2382 bgp->allocate_mpls_labels[afi][SAFI_UNICAST] = 0;
2383 bgp_recalculate_afi_safi_bestpaths(bgp, afi, SAFI_UNICAST);
2384 }
2385 return ret;
2386 }
2387
2388 void peer_nsf_stop(struct peer *peer)
2389 {
2390 afi_t afi;
2391 safi_t safi;
2392
2393 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2394 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
2395
2396 FOREACH_AFI_SAFI_NSF (afi, safi) {
2397 peer->nsf[afi][safi] = 0;
2398 THREAD_OFF(peer->t_llgr_stale[afi][safi]);
2399 }
2400
2401 if (peer->t_gr_restart) {
2402 THREAD_OFF(peer->t_gr_restart);
2403 if (bgp_debug_neighbor_events(peer))
2404 zlog_debug("%pBP graceful restart timer stopped", peer);
2405 }
2406 if (peer->t_gr_stale) {
2407 THREAD_OFF(peer->t_gr_stale);
2408 if (bgp_debug_neighbor_events(peer))
2409 zlog_debug(
2410 "%pBP graceful restart stalepath timer stopped",
2411 peer);
2412 }
2413 bgp_clear_route_all(peer);
2414 }
2415
2416 /* Delete peer from confguration.
2417 *
2418 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2419 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2420 *
2421 * This function /should/ take care to be idempotent, to guard against
2422 * it being called multiple times through stray events that come in
2423 * that happen to result in this function being called again. That
2424 * said, getting here for a "Deleted" peer is a bug in the neighbour
2425 * FSM.
2426 */
2427 int peer_delete(struct peer *peer)
2428 {
2429 int i;
2430 afi_t afi;
2431 safi_t safi;
2432 struct bgp *bgp;
2433 struct bgp_filter *filter;
2434 struct listnode *pn;
2435 int accept_peer;
2436
2437 assert(peer->status != Deleted);
2438
2439 bgp = peer->bgp;
2440 accept_peer = CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
2441
2442 bgp_soft_reconfig_table_task_cancel(bgp, NULL, peer);
2443
2444 bgp_keepalives_off(peer);
2445 bgp_reads_off(peer);
2446 bgp_writes_off(peer);
2447 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
2448 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
2449 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON));
2450
2451 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT))
2452 peer_nsf_stop(peer);
2453
2454 SET_FLAG(peer->flags, PEER_FLAG_DELETE);
2455
2456 /* Remove BFD settings. */
2457 if (peer->bfd_config)
2458 bgp_peer_remove_bfd_config(peer);
2459
2460 /* If this peer belongs to peer group, clear up the
2461 relationship. */
2462 if (peer->group) {
2463 if (peer_dynamic_neighbor(peer))
2464 peer_drop_dynamic_neighbor(peer);
2465
2466 if ((pn = listnode_lookup(peer->group->peer, peer))) {
2467 peer = peer_unlock(
2468 peer); /* group->peer list reference */
2469 list_delete_node(peer->group->peer, pn);
2470 }
2471 peer->group = NULL;
2472 }
2473
2474 /* Withdraw all information from routing table. We can not use
2475 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2476 * executed after peer structure is deleted.
2477 */
2478 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
2479 bgp_stop(peer);
2480 UNSET_FLAG(peer->flags, PEER_FLAG_DELETE);
2481
2482 if (peer->doppelganger) {
2483 peer->doppelganger->doppelganger = NULL;
2484 peer->doppelganger = NULL;
2485 }
2486
2487 UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
2488 bgp_fsm_change_status(peer, Deleted);
2489
2490 /* Remove from NHT */
2491 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
2492 bgp_unlink_nexthop_by_peer(peer);
2493
2494 /* Password configuration */
2495 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD)) {
2496 XFREE(MTYPE_PEER_PASSWORD, peer->password);
2497 if (!accept_peer && !BGP_PEER_SU_UNSPEC(peer)
2498 && !CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
2499 && !CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR))
2500 bgp_md5_unset(peer);
2501 }
2502
2503 bgp_timer_set(peer); /* stops all timers for Deleted */
2504
2505 /* Delete from all peer list. */
2506 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
2507 && (pn = listnode_lookup(bgp->peer, peer))) {
2508 peer_unlock(peer); /* bgp peer list reference */
2509 list_delete_node(bgp->peer, pn);
2510 hash_release(bgp->peerhash, peer);
2511 }
2512
2513 /* Buffers. */
2514 if (peer->ibuf) {
2515 stream_fifo_free(peer->ibuf);
2516 peer->ibuf = NULL;
2517 }
2518
2519 if (peer->obuf) {
2520 stream_fifo_free(peer->obuf);
2521 peer->obuf = NULL;
2522 }
2523
2524 if (peer->ibuf_work) {
2525 ringbuf_del(peer->ibuf_work);
2526 peer->ibuf_work = NULL;
2527 }
2528
2529 if (peer->obuf_work) {
2530 stream_free(peer->obuf_work);
2531 peer->obuf_work = NULL;
2532 }
2533
2534 if (peer->scratch) {
2535 stream_free(peer->scratch);
2536 peer->scratch = NULL;
2537 }
2538
2539 /* Local and remote addresses. */
2540 if (peer->su_local) {
2541 sockunion_free(peer->su_local);
2542 peer->su_local = NULL;
2543 }
2544
2545 if (peer->su_remote) {
2546 sockunion_free(peer->su_remote);
2547 peer->su_remote = NULL;
2548 }
2549
2550 /* Free filter related memory. */
2551 FOREACH_AFI_SAFI (afi, safi) {
2552 filter = &peer->filter[afi][safi];
2553
2554 for (i = FILTER_IN; i < FILTER_MAX; i++) {
2555 XFREE(MTYPE_BGP_FILTER_NAME, filter->dlist[i].name);
2556 XFREE(MTYPE_BGP_FILTER_NAME, filter->plist[i].name);
2557 XFREE(MTYPE_BGP_FILTER_NAME, filter->aslist[i].name);
2558 }
2559
2560 for (i = RMAP_IN; i < RMAP_MAX; i++) {
2561 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[i].name);
2562 }
2563
2564 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
2565 XFREE(MTYPE_ROUTE_MAP_NAME, peer->default_rmap[afi][safi].name);
2566 ecommunity_free(&peer->soo[afi][safi]);
2567 }
2568
2569 FOREACH_AFI_SAFI (afi, safi)
2570 peer_af_delete(peer, afi, safi);
2571
2572 XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
2573 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
2574
2575 peer_unlock(peer); /* initial reference */
2576
2577 return 0;
2578 }
2579
2580 static int peer_group_cmp(struct peer_group *g1, struct peer_group *g2)
2581 {
2582 return strcmp(g1->name, g2->name);
2583 }
2584
2585 /* Peer group cofiguration. */
2586 static struct peer_group *peer_group_new(void)
2587 {
2588 return XCALLOC(MTYPE_PEER_GROUP, sizeof(struct peer_group));
2589 }
2590
2591 static void peer_group_free(struct peer_group *group)
2592 {
2593 XFREE(MTYPE_PEER_GROUP, group);
2594 }
2595
2596 struct peer_group *peer_group_lookup(struct bgp *bgp, const char *name)
2597 {
2598 struct peer_group *group;
2599 struct listnode *node, *nnode;
2600
2601 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
2602 if (strcmp(group->name, name) == 0)
2603 return group;
2604 }
2605 return NULL;
2606 }
2607
2608 struct peer_group *peer_group_get(struct bgp *bgp, const char *name)
2609 {
2610 struct peer_group *group;
2611 afi_t afi;
2612 safi_t safi;
2613
2614 group = peer_group_lookup(bgp, name);
2615 if (group)
2616 return group;
2617
2618 group = peer_group_new();
2619 group->bgp = bgp;
2620 XFREE(MTYPE_PEER_GROUP_HOST, group->name);
2621 group->name = XSTRDUP(MTYPE_PEER_GROUP_HOST, name);
2622 group->peer = list_new();
2623 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2624 group->listen_range[afi] = list_new();
2625 group->conf = peer_new(bgp);
2626 FOREACH_AFI_SAFI (afi, safi) {
2627 if (bgp->default_af[afi][safi])
2628 group->conf->afc[afi][safi] = 1;
2629 }
2630 XFREE(MTYPE_BGP_PEER_HOST, group->conf->host);
2631 group->conf->host = XSTRDUP(MTYPE_BGP_PEER_HOST, name);
2632 group->conf->group = group;
2633 group->conf->as = 0;
2634 group->conf->ttl = BGP_DEFAULT_TTL;
2635 group->conf->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
2636 group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV;
2637 SET_FLAG(group->conf->sflags, PEER_STATUS_GROUP);
2638 listnode_add_sort(bgp->group, group);
2639
2640 return group;
2641 }
2642
2643 static void peer_group2peer_config_copy(struct peer_group *group,
2644 struct peer *peer)
2645 {
2646 uint32_t flags_tmp;
2647 struct peer *conf;
2648
2649 conf = group->conf;
2650
2651 /* remote-as */
2652 if (conf->as)
2653 peer->as = conf->as;
2654
2655 /* local-as */
2656 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_LOCAL_AS))
2657 peer->change_local_as = conf->change_local_as;
2658
2659 /* If peer-group has configured TTL then override it */
2660 if (conf->ttl != BGP_DEFAULT_TTL)
2661 peer->ttl = conf->ttl;
2662
2663 /* GTSM hops */
2664 peer->gtsm_hops = conf->gtsm_hops;
2665
2666 /* peer flags apply */
2667 flags_tmp = conf->flags & ~peer->flags_override;
2668 flags_tmp ^= conf->flags_invert ^ peer->flags_invert;
2669 flags_tmp &= ~peer->flags_override;
2670
2671 UNSET_FLAG(peer->flags, ~peer->flags_override);
2672 SET_FLAG(peer->flags, flags_tmp);
2673 SET_FLAG(peer->flags_invert, conf->flags_invert);
2674
2675 /* peer timers apply */
2676 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER)) {
2677 PEER_ATTR_INHERIT(peer, group, holdtime);
2678 PEER_ATTR_INHERIT(peer, group, keepalive);
2679 }
2680
2681 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER_CONNECT)) {
2682 PEER_ATTR_INHERIT(peer, group, connect);
2683 if (CHECK_FLAG(conf->flags, PEER_FLAG_TIMER_CONNECT))
2684 peer->v_connect = conf->connect;
2685 else
2686 peer->v_connect = peer->bgp->default_connect_retry;
2687 }
2688
2689 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_TIMER_DELAYOPEN)) {
2690 PEER_ATTR_INHERIT(peer, group, delayopen);
2691 if (CHECK_FLAG(conf->flags, PEER_FLAG_TIMER_DELAYOPEN))
2692 peer->v_delayopen = conf->delayopen;
2693 else
2694 peer->v_delayopen = peer->bgp->default_delayopen;
2695 }
2696
2697 /* advertisement-interval apply */
2698 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_ROUTEADV)) {
2699 PEER_ATTR_INHERIT(peer, group, routeadv);
2700 if (CHECK_FLAG(conf->flags, PEER_FLAG_ROUTEADV))
2701 peer->v_routeadv = conf->routeadv;
2702 else
2703 peer->v_routeadv = (peer_sort(peer) == BGP_PEER_IBGP)
2704 ? BGP_DEFAULT_IBGP_ROUTEADV
2705 : BGP_DEFAULT_EBGP_ROUTEADV;
2706 }
2707
2708 /* capability extended-nexthop apply */
2709 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_CAPABILITY_ENHE))
2710 if (CHECK_FLAG(conf->flags, PEER_FLAG_CAPABILITY_ENHE))
2711 SET_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE);
2712
2713 /* password apply */
2714 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_PASSWORD))
2715 PEER_STR_ATTR_INHERIT(peer, group, password,
2716 MTYPE_PEER_PASSWORD);
2717
2718 if (!BGP_PEER_SU_UNSPEC(peer))
2719 bgp_md5_set(peer);
2720
2721 /* update-source apply */
2722 if (!CHECK_FLAG(peer->flags_override, PEER_FLAG_UPDATE_SOURCE)) {
2723 if (conf->update_source) {
2724 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
2725 PEER_SU_ATTR_INHERIT(peer, group, update_source);
2726 } else if (conf->update_if) {
2727 sockunion_free(peer->update_source);
2728 PEER_STR_ATTR_INHERIT(peer, group, update_if,
2729 MTYPE_PEER_UPDATE_SOURCE);
2730 }
2731 }
2732
2733 /* role */
2734 PEER_ATTR_INHERIT(peer, group, local_role);
2735
2736 /* Update GR flags for the peer. */
2737 bgp_peer_gr_flags_update(peer);
2738
2739 /* Apply BFD settings from group to peer if it exists. */
2740 if (conf->bfd_config) {
2741 bgp_peer_configure_bfd(peer, false);
2742 bgp_peer_config_apply(peer, group);
2743 }
2744 }
2745
2746 /* Peer group's remote AS configuration. */
2747 int peer_group_remote_as(struct bgp *bgp, const char *group_name, as_t *as,
2748 int as_type)
2749 {
2750 struct peer_group *group;
2751 struct peer *peer;
2752 struct listnode *node, *nnode;
2753
2754 group = peer_group_lookup(bgp, group_name);
2755 if (!group)
2756 return -1;
2757
2758 if ((as_type == group->conf->as_type) && (group->conf->as == *as))
2759 return 0;
2760
2761
2762 /* When we setup peer-group AS number all peer group member's AS
2763 number must be updated to same number. */
2764 peer_as_change(group->conf, *as, as_type);
2765
2766 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2767 if (((peer->as_type == AS_SPECIFIED) && peer->as != *as)
2768 || (peer->as_type != as_type))
2769 peer_as_change(peer, *as, as_type);
2770 }
2771
2772 return 0;
2773 }
2774
2775 void peer_notify_unconfig(struct peer *peer)
2776 {
2777 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
2778 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2779 BGP_NOTIFY_CEASE_PEER_UNCONFIG);
2780 }
2781
2782 static void peer_notify_shutdown(struct peer *peer)
2783 {
2784 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)) {
2785 if (bgp_debug_neighbor_events(peer))
2786 zlog_debug(
2787 "%pBP configured Graceful-Restart, skipping shutdown notification",
2788 peer);
2789 return;
2790 }
2791
2792 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
2793 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2794 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
2795 }
2796
2797 void peer_group_notify_unconfig(struct peer_group *group)
2798 {
2799 struct peer *peer, *other;
2800 struct listnode *node, *nnode;
2801
2802 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2803 other = peer->doppelganger;
2804 if (other && other->status != Deleted) {
2805 other->group = NULL;
2806 peer_notify_unconfig(other);
2807 } else
2808 peer_notify_unconfig(peer);
2809 }
2810 }
2811
2812 int peer_group_delete(struct peer_group *group)
2813 {
2814 struct bgp *bgp;
2815 struct peer *peer;
2816 struct prefix *prefix;
2817 struct peer *other;
2818 struct listnode *node, *nnode;
2819 afi_t afi;
2820
2821 bgp = group->bgp;
2822
2823 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2824 other = peer->doppelganger;
2825
2826 if (CHECK_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE))
2827 bgp_zebra_terminate_radv(bgp, peer);
2828
2829 peer_delete(peer);
2830 if (other && other->status != Deleted) {
2831 other->group = NULL;
2832 peer_delete(other);
2833 }
2834 }
2835 list_delete(&group->peer);
2836
2837 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2838 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode,
2839 prefix)) {
2840 prefix_free(&prefix);
2841 }
2842 list_delete(&group->listen_range[afi]);
2843 }
2844
2845 XFREE(MTYPE_PEER_GROUP_HOST, group->name);
2846 group->name = NULL;
2847
2848 if (group->conf->bfd_config)
2849 bgp_peer_remove_bfd_config(group->conf);
2850
2851 group->conf->group = NULL;
2852 peer_delete(group->conf);
2853
2854 /* Delete from all peer_group list. */
2855 listnode_delete(bgp->group, group);
2856
2857 peer_group_free(group);
2858
2859 return 0;
2860 }
2861
2862 int peer_group_remote_as_delete(struct peer_group *group)
2863 {
2864 struct peer *peer, *other;
2865 struct listnode *node, *nnode;
2866
2867 if ((group->conf->as_type == AS_UNSPECIFIED)
2868 || ((!group->conf->as) && (group->conf->as_type == AS_SPECIFIED)))
2869 return 0;
2870
2871 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2872 other = peer->doppelganger;
2873
2874 if (CHECK_FLAG(peer->flags, PEER_FLAG_CAPABILITY_ENHE))
2875 bgp_zebra_terminate_radv(peer->bgp, peer);
2876
2877 peer_delete(peer);
2878
2879 if (other && other->status != Deleted) {
2880 other->group = NULL;
2881 peer_delete(other);
2882 }
2883 }
2884 list_delete_all_node(group->peer);
2885
2886 group->conf->as = 0;
2887 group->conf->as_type = AS_UNSPECIFIED;
2888
2889 return 0;
2890 }
2891
2892 int peer_group_listen_range_add(struct peer_group *group, struct prefix *range)
2893 {
2894 struct prefix *prefix;
2895 struct listnode *node, *nnode;
2896 afi_t afi;
2897
2898 afi = family2afi(range->family);
2899
2900 /* Group needs remote AS configured. */
2901 if (group->conf->as_type == AS_UNSPECIFIED)
2902 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS;
2903
2904 /* Ensure no duplicates. Currently we don't care about overlaps. */
2905 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode, prefix)) {
2906 if (prefix_same(range, prefix))
2907 return 0;
2908 }
2909
2910 prefix = prefix_new();
2911 prefix_copy(prefix, range);
2912 listnode_add(group->listen_range[afi], prefix);
2913
2914 /* Update passwords for new ranges */
2915 if (group->conf->password)
2916 bgp_md5_set_prefix(group->bgp, prefix, group->conf->password);
2917
2918 return 0;
2919 }
2920
2921 int peer_group_listen_range_del(struct peer_group *group, struct prefix *range)
2922 {
2923 struct prefix *prefix, prefix2;
2924 struct listnode *node, *nnode;
2925 struct peer *peer;
2926 afi_t afi;
2927
2928 afi = family2afi(range->family);
2929
2930 /* Identify the listen range. */
2931 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode, prefix)) {
2932 if (prefix_same(range, prefix))
2933 break;
2934 }
2935
2936 if (!prefix)
2937 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND;
2938
2939 /* Dispose off any dynamic neighbors that exist due to this listen range
2940 */
2941 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2942 if (!peer_dynamic_neighbor(peer))
2943 continue;
2944
2945 if (sockunion2hostprefix(&peer->su, &prefix2)
2946 && prefix_match(prefix, &prefix2)) {
2947 if (bgp_debug_neighbor_events(peer))
2948 zlog_debug(
2949 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
2950 peer->host, group->name, prefix);
2951 peer_delete(peer);
2952 }
2953 }
2954
2955 /* Get rid of the listen range */
2956 listnode_delete(group->listen_range[afi], prefix);
2957
2958 /* Remove passwords for deleted ranges */
2959 if (group->conf->password)
2960 bgp_md5_unset_prefix(group->bgp, prefix);
2961
2962 return 0;
2963 }
2964
2965 /* Bind specified peer to peer group. */
2966 int peer_group_bind(struct bgp *bgp, union sockunion *su, struct peer *peer,
2967 struct peer_group *group, as_t *as)
2968 {
2969 int first_member = 0;
2970 afi_t afi;
2971 safi_t safi;
2972 enum bgp_peer_sort ptype, gtype;
2973
2974 /* Lookup the peer. */
2975 if (!peer)
2976 peer = peer_lookup(bgp, su);
2977
2978 /* The peer exist, bind it to the peer-group */
2979 if (peer) {
2980 /* When the peer already belongs to a peer-group, check the
2981 * consistency. */
2982 if (peer_group_active(peer)) {
2983
2984 /* The peer is already bound to the peer-group,
2985 * nothing to do
2986 */
2987 if (strcmp(peer->group->name, group->name) == 0)
2988 return 0;
2989 else
2990 return BGP_ERR_PEER_GROUP_CANT_CHANGE;
2991 }
2992
2993 /* The peer has not specified a remote-as, inherit it from the
2994 * peer-group */
2995 if (peer->as_type == AS_UNSPECIFIED) {
2996 peer->as_type = group->conf->as_type;
2997 peer->as = group->conf->as;
2998 peer->sort = group->conf->sort;
2999 }
3000
3001 ptype = peer_sort(peer);
3002 if (!group->conf->as && ptype != BGP_PEER_UNSPECIFIED) {
3003 gtype = peer_sort(group->conf);
3004 if ((gtype != BGP_PEER_INTERNAL) && (gtype != ptype)) {
3005 if (as)
3006 *as = peer->as;
3007 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT;
3008 }
3009
3010 if (gtype == BGP_PEER_INTERNAL)
3011 first_member = 1;
3012 }
3013
3014 peer_group2peer_config_copy(group, peer);
3015
3016 FOREACH_AFI_SAFI (afi, safi) {
3017 if (group->conf->afc[afi][safi]) {
3018 peer->afc[afi][safi] = 1;
3019
3020 if (peer_af_find(peer, afi, safi)
3021 || peer_af_create(peer, afi, safi)) {
3022 peer_group2peer_config_copy_af(
3023 group, peer, afi, safi);
3024 }
3025 } else if (peer->afc[afi][safi])
3026 peer_deactivate(peer, afi, safi);
3027 }
3028
3029 if (peer->group) {
3030 assert(group && peer->group == group);
3031 } else {
3032 listnode_delete(bgp->peer, peer);
3033
3034 peer->group = group;
3035 listnode_add_sort(bgp->peer, peer);
3036
3037 peer = peer_lock(peer); /* group->peer list reference */
3038 listnode_add(group->peer, peer);
3039 }
3040
3041 if (first_member) {
3042 gtype = peer_sort(group->conf);
3043 /* Advertisement-interval reset */
3044 if (!CHECK_FLAG(group->conf->flags,
3045 PEER_FLAG_ROUTEADV)) {
3046 group->conf->v_routeadv =
3047 (gtype == BGP_PEER_IBGP)
3048 ? BGP_DEFAULT_IBGP_ROUTEADV
3049 : BGP_DEFAULT_EBGP_ROUTEADV;
3050 }
3051
3052 /* ebgp-multihop reset */
3053 if (gtype == BGP_PEER_IBGP)
3054 group->conf->ttl = MAXTTL;
3055 }
3056
3057 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
3058
3059 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
3060 peer->last_reset = PEER_DOWN_RMAP_BIND;
3061 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
3062 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
3063 } else {
3064 bgp_session_reset(peer);
3065 }
3066 }
3067
3068 /* Create a new peer. */
3069 else {
3070 if ((group->conf->as_type == AS_SPECIFIED)
3071 && (!group->conf->as)) {
3072 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS;
3073 }
3074
3075 peer = peer_create(su, NULL, bgp, bgp->as, group->conf->as,
3076 group->conf->as_type, group);
3077
3078 peer = peer_lock(peer); /* group->peer list reference */
3079 listnode_add(group->peer, peer);
3080
3081 peer_group2peer_config_copy(group, peer);
3082
3083 /* If the peer-group is active for this afi/safi then activate
3084 * for this peer */
3085 FOREACH_AFI_SAFI (afi, safi) {
3086 if (group->conf->afc[afi][safi]) {
3087 peer->afc[afi][safi] = 1;
3088
3089 if (!peer_af_find(peer, afi, safi))
3090 peer_af_create(peer, afi, safi);
3091
3092 peer_group2peer_config_copy_af(group, peer, afi,
3093 safi);
3094 } else if (peer->afc[afi][safi])
3095 peer_deactivate(peer, afi, safi);
3096 }
3097
3098 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
3099
3100 /* Set up peer's events and timers. */
3101 if (peer_active(peer))
3102 bgp_timer_set(peer);
3103 }
3104
3105 return 0;
3106 }
3107
3108 static void bgp_startup_timer_expire(struct thread *thread)
3109 {
3110 struct bgp *bgp;
3111
3112 bgp = THREAD_ARG(thread);
3113 bgp->t_startup = NULL;
3114 }
3115
3116 /*
3117 * On shutdown we call the cleanup function which
3118 * does a free of the link list nodes, free up
3119 * the data we are pointing at too.
3120 */
3121 static void bgp_vrf_string_name_delete(void *data)
3122 {
3123 char *vname = data;
3124
3125 XFREE(MTYPE_TMP, vname);
3126 }
3127
3128 /* BGP instance creation by `router bgp' commands. */
3129 static struct bgp *bgp_create(as_t *as, const char *name,
3130 enum bgp_instance_type inst_type)
3131 {
3132 struct bgp *bgp;
3133 afi_t afi;
3134 safi_t safi;
3135
3136 bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp));
3137
3138 if (BGP_DEBUG(zebra, ZEBRA)) {
3139 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3140 zlog_debug("Creating Default VRF, AS %u", *as);
3141 else
3142 zlog_debug("Creating %s %s, AS %u",
3143 (inst_type == BGP_INSTANCE_TYPE_VRF)
3144 ? "VRF"
3145 : "VIEW",
3146 name, *as);
3147 }
3148
3149 /* Default the EVPN VRF to the default one */
3150 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT && !bgp_master.bgp_evpn) {
3151 bgp_lock(bgp);
3152 bm->bgp_evpn = bgp;
3153 }
3154
3155 bgp_lock(bgp);
3156
3157 bgp->allow_martian = false;
3158 bgp_process_queue_init(bgp);
3159 bgp->heuristic_coalesce = true;
3160 bgp->inst_type = inst_type;
3161 bgp->vrf_id = (inst_type == BGP_INSTANCE_TYPE_DEFAULT) ? VRF_DEFAULT
3162 : VRF_UNKNOWN;
3163 bgp->peer_self = peer_new(bgp);
3164 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->host);
3165 bgp->peer_self->host =
3166 XSTRDUP(MTYPE_BGP_PEER_HOST, "Static announcement");
3167 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->hostname);
3168 if (cmd_hostname_get())
3169 bgp->peer_self->hostname =
3170 XSTRDUP(MTYPE_BGP_PEER_HOST, cmd_hostname_get());
3171
3172 XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->domainname);
3173 if (cmd_domainname_get())
3174 bgp->peer_self->domainname =
3175 XSTRDUP(MTYPE_BGP_PEER_HOST, cmd_domainname_get());
3176 bgp->peer = list_new();
3177 bgp->peer->cmp = (int (*)(void *, void *))peer_cmp;
3178 bgp->peerhash = hash_create(peer_hash_key_make, peer_hash_same,
3179 "BGP Peer Hash");
3180 bgp->peerhash->max_size = BGP_PEER_MAX_HASH_SIZE;
3181
3182 bgp->group = list_new();
3183 bgp->group->cmp = (int (*)(void *, void *))peer_group_cmp;
3184
3185 FOREACH_AFI_SAFI (afi, safi) {
3186 bgp->route[afi][safi] = bgp_table_init(bgp, afi, safi);
3187 bgp->aggregate[afi][safi] = bgp_table_init(bgp, afi, safi);
3188 bgp->rib[afi][safi] = bgp_table_init(bgp, afi, safi);
3189
3190 /* Enable maximum-paths */
3191 bgp_maximum_paths_set(bgp, afi, safi, BGP_PEER_EBGP,
3192 multipath_num, 0);
3193 bgp_maximum_paths_set(bgp, afi, safi, BGP_PEER_IBGP,
3194 multipath_num, 0);
3195 /* Initialize graceful restart info */
3196 bgp->gr_info[afi][safi].eor_required = 0;
3197 bgp->gr_info[afi][safi].eor_received = 0;
3198 bgp->gr_info[afi][safi].t_select_deferral = NULL;
3199 bgp->gr_info[afi][safi].t_route_select = NULL;
3200 bgp->gr_info[afi][safi].gr_deferred = 0;
3201 }
3202
3203 bgp->v_update_delay = bm->v_update_delay;
3204 bgp->v_establish_wait = bm->v_establish_wait;
3205 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
3206 bgp->default_subgroup_pkt_queue_max =
3207 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX;
3208 bgp_tcp_keepalive_unset(bgp);
3209 bgp_timers_unset(bgp);
3210 bgp->default_min_holdtime = 0;
3211 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
3212 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
3213 bgp->select_defer_time = BGP_DEFAULT_SELECT_DEFERRAL_TIME;
3214 bgp->rib_stale_time = BGP_DEFAULT_RIB_STALE_TIME;
3215 bgp->dynamic_neighbors_limit = BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT;
3216 bgp->dynamic_neighbors_count = 0;
3217 bgp->lb_ref_bw = BGP_LINK_BW_REF_BW;
3218 bgp->lb_handling = BGP_LINK_BW_ECMP;
3219 bgp->reject_as_sets = false;
3220 bgp->condition_check_period = DEFAULT_CONDITIONAL_ROUTES_POLL_TIME;
3221 bgp_addpath_init_bgp_data(&bgp->tx_addpath);
3222 bgp->fast_convergence = false;
3223 bgp->as = *as;
3224 bgp->llgr_stale_time = BGP_DEFAULT_LLGR_STALE_TIME;
3225
3226 #ifdef ENABLE_BGP_VNC
3227 if (inst_type != BGP_INSTANCE_TYPE_VRF) {
3228 bgp->rfapi = bgp_rfapi_new(bgp);
3229 assert(bgp->rfapi);
3230 assert(bgp->rfapi_cfg);
3231 }
3232 #endif /* ENABLE_BGP_VNC */
3233
3234 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3235 bgp->vpn_policy[afi].bgp = bgp;
3236 bgp->vpn_policy[afi].afi = afi;
3237 bgp->vpn_policy[afi].tovpn_label = MPLS_LABEL_NONE;
3238 bgp->vpn_policy[afi].tovpn_zebra_vrf_label_last_sent =
3239 MPLS_LABEL_NONE;
3240
3241 bgp->vpn_policy[afi].import_vrf = list_new();
3242 bgp->vpn_policy[afi].import_vrf->del =
3243 bgp_vrf_string_name_delete;
3244 bgp->vpn_policy[afi].export_vrf = list_new();
3245 bgp->vpn_policy[afi].export_vrf->del =
3246 bgp_vrf_string_name_delete;
3247 SET_FLAG(bgp->af_flags[afi][SAFI_MPLS_VPN],
3248 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL);
3249 }
3250 if (name)
3251 bgp->name = XSTRDUP(MTYPE_BGP, name);
3252
3253 thread_add_timer(bm->master, bgp_startup_timer_expire, bgp,
3254 bgp->restart_time, &bgp->t_startup);
3255
3256 /* printable name we can use in debug messages */
3257 if (inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
3258 bgp->name_pretty = XSTRDUP(MTYPE_BGP, "VRF default");
3259 } else {
3260 const char *n;
3261 int len;
3262
3263 if (bgp->name)
3264 n = bgp->name;
3265 else
3266 n = "?";
3267
3268 len = 4 + 1 + strlen(n) + 1; /* "view foo\0" */
3269
3270 bgp->name_pretty = XCALLOC(MTYPE_BGP, len);
3271 snprintf(bgp->name_pretty, len, "%s %s",
3272 (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
3273 ? "VRF"
3274 : "VIEW",
3275 n);
3276 }
3277
3278 atomic_store_explicit(&bgp->wpkt_quanta, BGP_WRITE_PACKET_MAX,
3279 memory_order_relaxed);
3280 atomic_store_explicit(&bgp->rpkt_quanta, BGP_READ_PACKET_MAX,
3281 memory_order_relaxed);
3282 bgp->coalesce_time = BGP_DEFAULT_SUBGROUP_COALESCE_TIME;
3283 bgp->default_af[AFI_IP][SAFI_UNICAST] = true;
3284
3285 QOBJ_REG(bgp, bgp);
3286
3287 update_bgp_group_init(bgp);
3288
3289 /* assign a unique rd id for auto derivation of vrf's RD */
3290 bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
3291
3292 bgp->evpn_info = XCALLOC(MTYPE_BGP_EVPN_INFO,
3293 sizeof(struct bgp_evpn_info));
3294 bgp_evpn_init(bgp);
3295 bgp_evpn_vrf_es_init(bgp);
3296 bgp_pbr_init(bgp);
3297 bgp_srv6_init(bgp);
3298
3299 /*initilize global GR FSM */
3300 bgp_global_gr_init(bgp);
3301
3302 memset(&bgp->ebgprequirespolicywarning, 0,
3303 sizeof(bgp->ebgprequirespolicywarning));
3304
3305 return bgp;
3306 }
3307
3308 /* Return the "default VRF" instance of BGP. */
3309 struct bgp *bgp_get_default(void)
3310 {
3311 struct bgp *bgp;
3312 struct listnode *node, *nnode;
3313
3314 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3315 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3316 return bgp;
3317 return NULL;
3318 }
3319
3320 /* Lookup BGP entry. */
3321 struct bgp *bgp_lookup(as_t as, const char *name)
3322 {
3323 struct bgp *bgp;
3324 struct listnode *node, *nnode;
3325
3326 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3327 if (bgp->as == as
3328 && ((bgp->name == NULL && name == NULL)
3329 || (bgp->name && name && strcmp(bgp->name, name) == 0)))
3330 return bgp;
3331 return NULL;
3332 }
3333
3334 /* Lookup BGP structure by view name. */
3335 struct bgp *bgp_lookup_by_name(const char *name)
3336 {
3337 struct bgp *bgp;
3338 struct listnode *node, *nnode;
3339
3340 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3341 if ((bgp->name == NULL && name == NULL)
3342 || (bgp->name && name && strcmp(bgp->name, name) == 0))
3343 return bgp;
3344 return NULL;
3345 }
3346
3347 /* Lookup BGP instance based on VRF id. */
3348 /* Note: Only to be used for incoming messages from Zebra. */
3349 struct bgp *bgp_lookup_by_vrf_id(vrf_id_t vrf_id)
3350 {
3351 struct vrf *vrf;
3352
3353 /* Lookup VRF (in tree) and follow link. */
3354 vrf = vrf_lookup_by_id(vrf_id);
3355 if (!vrf)
3356 return NULL;
3357 return (vrf->info) ? (struct bgp *)vrf->info : NULL;
3358 }
3359
3360 /* Sets the BGP instance where EVPN is enabled */
3361 void bgp_set_evpn(struct bgp *bgp)
3362 {
3363 if (bm->bgp_evpn == bgp)
3364 return;
3365
3366 /* First, release the reference count we hold on the instance */
3367 if (bm->bgp_evpn)
3368 bgp_unlock(bm->bgp_evpn);
3369
3370 bm->bgp_evpn = bgp;
3371
3372 /* Increase the reference count on this new VRF */
3373 if (bm->bgp_evpn)
3374 bgp_lock(bm->bgp_evpn);
3375 }
3376
3377 /* Returns the BGP instance where EVPN is enabled, if any */
3378 struct bgp *bgp_get_evpn(void)
3379 {
3380 return bm->bgp_evpn;
3381 }
3382
3383 /* handle socket creation or deletion, if necessary
3384 * this is called for all new BGP instances
3385 */
3386 int bgp_handle_socket(struct bgp *bgp, struct vrf *vrf, vrf_id_t old_vrf_id,
3387 bool create)
3388 {
3389 struct listnode *node;
3390 char *address;
3391
3392 /* Create BGP server socket, if listen mode not disabled */
3393 if (!bgp || bgp_option_check(BGP_OPT_NO_LISTEN))
3394 return 0;
3395 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
3396 /*
3397 * suppress vrf socket
3398 */
3399 if (!create) {
3400 bgp_close_vrf_socket(bgp);
3401 return 0;
3402 }
3403 if (vrf == NULL)
3404 return BGP_ERR_INVALID_VALUE;
3405 /* do nothing
3406 * if vrf_id did not change
3407 */
3408 if (vrf->vrf_id == old_vrf_id)
3409 return 0;
3410 if (old_vrf_id != VRF_UNKNOWN) {
3411 /* look for old socket. close it. */
3412 bgp_close_vrf_socket(bgp);
3413 }
3414 /* if backend is not yet identified ( VRF_UNKNOWN) then
3415 * creation will be done later
3416 */
3417 if (vrf->vrf_id == VRF_UNKNOWN)
3418 return 0;
3419 if (list_isempty(bm->addresses)) {
3420 if (bgp_socket(bgp, bm->port, NULL) < 0)
3421 return BGP_ERR_INVALID_VALUE;
3422 } else {
3423 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
3424 if (bgp_socket(bgp, bm->port, address) < 0)
3425 return BGP_ERR_INVALID_VALUE;
3426 }
3427 return 0;
3428 } else
3429 return bgp_check_main_socket(create, bgp);
3430 }
3431
3432 int bgp_lookup_by_as_name_type(struct bgp **bgp_val, as_t *as, const char *name,
3433 enum bgp_instance_type inst_type)
3434 {
3435 struct bgp *bgp;
3436
3437 /* Multiple instance check. */
3438 if (name)
3439 bgp = bgp_lookup_by_name(name);
3440 else
3441 bgp = bgp_get_default();
3442
3443 if (bgp) {
3444 *bgp_val = bgp;
3445 if (bgp->as != *as) {
3446 *as = bgp->as;
3447 return BGP_ERR_AS_MISMATCH;
3448 }
3449 if (bgp->inst_type != inst_type)
3450 return BGP_ERR_INSTANCE_MISMATCH;
3451 return BGP_SUCCESS;
3452 }
3453 *bgp_val = NULL;
3454
3455 return BGP_SUCCESS;
3456 }
3457
3458 /* Called from VTY commands. */
3459 int bgp_get(struct bgp **bgp_val, as_t *as, const char *name,
3460 enum bgp_instance_type inst_type)
3461 {
3462 struct bgp *bgp;
3463 struct vrf *vrf = NULL;
3464 int ret = 0;
3465
3466 ret = bgp_lookup_by_as_name_type(bgp_val, as, name, inst_type);
3467 if (ret || *bgp_val)
3468 return ret;
3469
3470 bgp = bgp_create(as, name, inst_type);
3471
3472 /*
3473 * view instances will never work inside of a vrf
3474 * as such they must always be in the VRF_DEFAULT
3475 * Also we must set this to something useful because
3476 * of the vrf socket code needing an actual useful
3477 * default value to send to the underlying OS.
3478 *
3479 * This code is currently ignoring vrf based
3480 * code using the -Z option( and that is probably
3481 * best addressed elsewhere in the code )
3482 */
3483 if (inst_type == BGP_INSTANCE_TYPE_VIEW)
3484 bgp->vrf_id = VRF_DEFAULT;
3485
3486 bgp_router_id_set(bgp, &bgp->router_id_zebra, true);
3487 bgp_address_init(bgp);
3488 bgp_tip_hash_init(bgp);
3489 bgp_scan_init(bgp);
3490 *bgp_val = bgp;
3491
3492 bgp->t_rmap_def_originate_eval = NULL;
3493
3494 /* If Default instance or VRF, link to the VRF structure, if present. */
3495 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
3496 || bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
3497 vrf = bgp_vrf_lookup_by_instance_type(bgp);
3498 if (vrf)
3499 bgp_vrf_link(bgp, vrf);
3500 }
3501 /* BGP server socket already processed if BGP instance
3502 * already part of the list
3503 */
3504 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, true);
3505 listnode_add(bm->bgp, bgp);
3506
3507 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
3508 if (BGP_DEBUG(zebra, ZEBRA))
3509 zlog_debug("%s: Registering BGP instance %s to zebra",
3510 __func__, name);
3511 bgp_zebra_instance_register(bgp);
3512 }
3513
3514 return BGP_CREATED;
3515 }
3516
3517 static void bgp_zclient_set_redist(afi_t afi, int type, unsigned short instance,
3518 vrf_id_t vrf_id, bool set)
3519 {
3520 if (instance) {
3521 if (set)
3522 redist_add_instance(&zclient->mi_redist[afi][type],
3523 instance);
3524 else
3525 redist_del_instance(&zclient->mi_redist[afi][type],
3526 instance);
3527 } else {
3528 if (set)
3529 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
3530 else
3531 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
3532 }
3533 }
3534
3535 static void bgp_set_redist_vrf_bitmaps(struct bgp *bgp, bool set)
3536 {
3537 afi_t afi;
3538 int i;
3539 struct list *red_list;
3540 struct listnode *node;
3541 struct bgp_redist *red;
3542
3543 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3544 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3545
3546 red_list = bgp->redist[afi][i];
3547 if (!red_list)
3548 continue;
3549
3550 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
3551 bgp_zclient_set_redist(afi, i, red->instance,
3552 bgp->vrf_id, set);
3553 }
3554 }
3555 }
3556
3557 /*
3558 * Make BGP instance "up". Applies only to VRFs (non-default) and
3559 * implies the VRF has been learnt from Zebra.
3560 */
3561 void bgp_instance_up(struct bgp *bgp)
3562 {
3563 struct peer *peer;
3564 struct listnode *node, *next;
3565
3566 bgp_set_redist_vrf_bitmaps(bgp, true);
3567
3568 /* Register with zebra. */
3569 bgp_zebra_instance_register(bgp);
3570
3571 /* Kick off any peers that may have been configured. */
3572 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer)) {
3573 if (!BGP_PEER_START_SUPPRESSED(peer))
3574 BGP_EVENT_ADD(peer, BGP_Start);
3575 }
3576
3577 /* Process any networks that have been configured. */
3578 bgp_static_add(bgp);
3579 }
3580
3581 /*
3582 * Make BGP instance "down". Applies only to VRFs (non-default) and
3583 * implies the VRF has been deleted by Zebra.
3584 */
3585 void bgp_instance_down(struct bgp *bgp)
3586 {
3587 struct peer *peer;
3588 struct listnode *node;
3589 struct listnode *next;
3590
3591 /* Stop timers. */
3592 if (bgp->t_rmap_def_originate_eval) {
3593 THREAD_OFF(bgp->t_rmap_def_originate_eval);
3594 bgp_unlock(bgp); /* TODO - This timer is started with a lock -
3595 why? */
3596 }
3597
3598 /* Bring down peers, so corresponding routes are purged. */
3599 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer)) {
3600 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
3601 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
3602 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
3603 else
3604 bgp_session_reset(peer);
3605 }
3606
3607 /* Purge network and redistributed routes. */
3608 bgp_purge_static_redist_routes(bgp);
3609
3610 /* Cleanup registered nexthops (flags) */
3611 bgp_cleanup_nexthops(bgp);
3612
3613 bgp_zebra_instance_deregister(bgp);
3614
3615 bgp_set_redist_vrf_bitmaps(bgp, false);
3616 }
3617
3618 /* Delete BGP instance. */
3619 int bgp_delete(struct bgp *bgp)
3620 {
3621 struct peer *peer;
3622 struct peer_group *group;
3623 struct listnode *node, *next;
3624 struct vrf *vrf;
3625 afi_t afi;
3626 safi_t safi;
3627 int i;
3628 struct graceful_restart_info *gr_info;
3629
3630 assert(bgp);
3631
3632 bgp_soft_reconfig_table_task_cancel(bgp, NULL, NULL);
3633
3634 /* make sure we withdraw any exported routes */
3635 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP, bgp_get_default(),
3636 bgp);
3637 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6, bgp_get_default(),
3638 bgp);
3639
3640 bgp_vpn_leak_unimport(bgp);
3641
3642 hook_call(bgp_inst_delete, bgp);
3643
3644 THREAD_OFF(bgp->t_condition_check);
3645 THREAD_OFF(bgp->t_startup);
3646 THREAD_OFF(bgp->t_maxmed_onstartup);
3647 THREAD_OFF(bgp->t_update_delay);
3648 THREAD_OFF(bgp->t_establish_wait);
3649
3650 /* Set flag indicating bgp instance delete in progress */
3651 SET_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS);
3652
3653 /* Delete the graceful restart info */
3654 FOREACH_AFI_SAFI (afi, safi) {
3655 struct thread *t;
3656
3657 gr_info = &bgp->gr_info[afi][safi];
3658 if (!gr_info)
3659 continue;
3660 t = gr_info->t_select_deferral;
3661 if (t) {
3662 void *info = THREAD_ARG(t);
3663
3664 XFREE(MTYPE_TMP, info);
3665 }
3666 THREAD_OFF(gr_info->t_select_deferral);
3667
3668 t = gr_info->t_route_select;
3669 if (t) {
3670 void *info = THREAD_ARG(t);
3671
3672 XFREE(MTYPE_TMP, info);
3673 }
3674 THREAD_OFF(gr_info->t_route_select);
3675 }
3676
3677 if (BGP_DEBUG(zebra, ZEBRA)) {
3678 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3679 zlog_debug("Deleting Default VRF");
3680 else
3681 zlog_debug("Deleting %s %s",
3682 (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
3683 ? "VRF"
3684 : "VIEW",
3685 bgp->name);
3686 }
3687
3688 /* unmap from RT list */
3689 bgp_evpn_vrf_delete(bgp);
3690
3691 /* unmap bgp vrf label */
3692 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP);
3693 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP6);
3694
3695 /* Stop timers. */
3696 if (bgp->t_rmap_def_originate_eval) {
3697 THREAD_OFF(bgp->t_rmap_def_originate_eval);
3698 bgp_unlock(bgp); /* TODO - This timer is started with a lock -
3699 why? */
3700 }
3701
3702 /* Inform peers we're going down. */
3703 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer))
3704 peer_notify_shutdown(peer);
3705
3706 /* Delete static routes (networks). */
3707 bgp_static_delete(bgp);
3708
3709 /* Unset redistribution. */
3710 for (afi = AFI_IP; afi < AFI_MAX; afi++)
3711 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3712 if (i != ZEBRA_ROUTE_BGP)
3713 bgp_redistribute_unset(bgp, afi, i, 0);
3714
3715 /* Free peers and peer-groups. */
3716 for (ALL_LIST_ELEMENTS(bgp->group, node, next, group))
3717 peer_group_delete(group);
3718
3719 for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer))
3720 peer_delete(peer);
3721
3722 if (bgp->peer_self) {
3723 peer_delete(bgp->peer_self);
3724 bgp->peer_self = NULL;
3725 }
3726
3727 update_bgp_group_free(bgp);
3728
3729 /* TODO - Other memory may need to be freed - e.g., NHT */
3730
3731 #ifdef ENABLE_BGP_VNC
3732 rfapi_delete(bgp);
3733 #endif
3734 bgp_cleanup_routes(bgp);
3735
3736 for (afi = 0; afi < AFI_MAX; ++afi) {
3737 if (!bgp->vpn_policy[afi].import_redirect_rtlist)
3738 continue;
3739 ecommunity_free(
3740 &bgp->vpn_policy[afi]
3741 .import_redirect_rtlist);
3742 bgp->vpn_policy[afi].import_redirect_rtlist = NULL;
3743 }
3744
3745 /* Free any memory allocated to holding routemap references */
3746 for (afi = 0; afi < AFI_MAX; ++afi) {
3747 for (enum vpn_policy_direction dir = 0;
3748 dir < BGP_VPN_POLICY_DIR_MAX; ++dir) {
3749 if (bgp->vpn_policy[afi].rmap_name[dir])
3750 XFREE(MTYPE_ROUTE_MAP_NAME,
3751 bgp->vpn_policy[afi].rmap_name[dir]);
3752 bgp->vpn_policy[afi].rmap[dir] = NULL;
3753 }
3754 }
3755
3756 /* Deregister from Zebra, if needed */
3757 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
3758 if (BGP_DEBUG(zebra, ZEBRA))
3759 zlog_debug(
3760 "%s: deregistering this bgp %s instance from zebra",
3761 __func__, bgp->name);
3762 bgp_zebra_instance_deregister(bgp);
3763 }
3764
3765 /* Remove visibility via the master list - there may however still be
3766 * routes to be processed still referencing the struct bgp.
3767 */
3768 listnode_delete(bm->bgp, bgp);
3769
3770 /* Free interfaces in this instance. */
3771 bgp_if_finish(bgp);
3772
3773 vrf = bgp_vrf_lookup_by_instance_type(bgp);
3774 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
3775 if (vrf)
3776 bgp_vrf_unlink(bgp, vrf);
3777
3778 /* Update EVPN VRF pointer */
3779 if (bm->bgp_evpn == bgp) {
3780 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
3781 bgp_set_evpn(NULL);
3782 else
3783 bgp_set_evpn(bgp_get_default());
3784 }
3785
3786 if (bgp->process_queue)
3787 work_queue_free_and_null(&bgp->process_queue);
3788
3789 thread_master_free_unused(bm->master);
3790 bgp_unlock(bgp); /* initial reference */
3791
3792 return 0;
3793 }
3794
3795 void bgp_free(struct bgp *bgp)
3796 {
3797 afi_t afi;
3798 safi_t safi;
3799 struct bgp_table *table;
3800 struct bgp_dest *dest;
3801 struct bgp_rmap *rmap;
3802
3803 QOBJ_UNREG(bgp);
3804
3805 list_delete(&bgp->group);
3806 list_delete(&bgp->peer);
3807
3808 if (bgp->peerhash) {
3809 hash_free(bgp->peerhash);
3810 bgp->peerhash = NULL;
3811 }
3812
3813 FOREACH_AFI_SAFI (afi, safi) {
3814 /* Special handling for 2-level routing tables. */
3815 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
3816 || safi == SAFI_EVPN) {
3817 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
3818 dest = bgp_route_next(dest)) {
3819 table = bgp_dest_get_bgp_table_info(dest);
3820 bgp_table_finish(&table);
3821 }
3822 }
3823 if (bgp->route[afi][safi])
3824 bgp_table_finish(&bgp->route[afi][safi]);
3825 if (bgp->aggregate[afi][safi])
3826 bgp_table_finish(&bgp->aggregate[afi][safi]);
3827 if (bgp->rib[afi][safi])
3828 bgp_table_finish(&bgp->rib[afi][safi]);
3829 rmap = &bgp->table_map[afi][safi];
3830 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
3831 }
3832
3833 bgp_scan_finish(bgp);
3834 bgp_address_destroy(bgp);
3835 bgp_tip_hash_destroy(bgp);
3836
3837 /* release the auto RD id */
3838 bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
3839
3840 bgp_evpn_cleanup(bgp);
3841 bgp_pbr_cleanup(bgp);
3842 bgp_srv6_cleanup(bgp);
3843 XFREE(MTYPE_BGP_EVPN_INFO, bgp->evpn_info);
3844
3845 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3846 enum vpn_policy_direction dir;
3847
3848 if (bgp->vpn_policy[afi].import_vrf)
3849 list_delete(&bgp->vpn_policy[afi].import_vrf);
3850 if (bgp->vpn_policy[afi].export_vrf)
3851 list_delete(&bgp->vpn_policy[afi].export_vrf);
3852
3853 dir = BGP_VPN_POLICY_DIR_FROMVPN;
3854 if (bgp->vpn_policy[afi].rtlist[dir])
3855 ecommunity_free(&bgp->vpn_policy[afi].rtlist[dir]);
3856 dir = BGP_VPN_POLICY_DIR_TOVPN;
3857 if (bgp->vpn_policy[afi].rtlist[dir])
3858 ecommunity_free(&bgp->vpn_policy[afi].rtlist[dir]);
3859 }
3860
3861 bgp_orr_cleanup(bgp);
3862
3863 XFREE(MTYPE_BGP, bgp->name);
3864 XFREE(MTYPE_BGP, bgp->name_pretty);
3865 XFREE(MTYPE_BGP, bgp->snmp_stats);
3866
3867 XFREE(MTYPE_BGP, bgp);
3868 }
3869
3870 struct peer *peer_lookup_by_conf_if(struct bgp *bgp, const char *conf_if)
3871 {
3872 struct peer *peer;
3873 struct listnode *node, *nnode;
3874
3875 if (!conf_if)
3876 return NULL;
3877
3878 if (bgp != NULL) {
3879 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3880 if (peer->conf_if && !strcmp(peer->conf_if, conf_if)
3881 && !CHECK_FLAG(peer->sflags,
3882 PEER_STATUS_ACCEPT_PEER))
3883 return peer;
3884 } else if (bm->bgp != NULL) {
3885 struct listnode *bgpnode, *nbgpnode;
3886
3887 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
3888 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3889 if (peer->conf_if
3890 && !strcmp(peer->conf_if, conf_if)
3891 && !CHECK_FLAG(peer->sflags,
3892 PEER_STATUS_ACCEPT_PEER))
3893 return peer;
3894 }
3895 return NULL;
3896 }
3897
3898 struct peer *peer_lookup_by_hostname(struct bgp *bgp, const char *hostname)
3899 {
3900 struct peer *peer;
3901 struct listnode *node, *nnode;
3902
3903 if (!hostname)
3904 return NULL;
3905
3906 if (bgp != NULL) {
3907 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3908 if (peer->hostname && !strcmp(peer->hostname, hostname)
3909 && !CHECK_FLAG(peer->sflags,
3910 PEER_STATUS_ACCEPT_PEER))
3911 return peer;
3912 } else if (bm->bgp != NULL) {
3913 struct listnode *bgpnode, *nbgpnode;
3914
3915 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
3916 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
3917 if (peer->hostname
3918 && !strcmp(peer->hostname, hostname)
3919 && !CHECK_FLAG(peer->sflags,
3920 PEER_STATUS_ACCEPT_PEER))
3921 return peer;
3922 }
3923 return NULL;
3924 }
3925
3926 struct peer *peer_lookup(struct bgp *bgp, union sockunion *su)
3927 {
3928 struct peer *peer = NULL;
3929 struct peer tmp_peer;
3930
3931 memset(&tmp_peer, 0, sizeof(struct peer));
3932
3933 /*
3934 * We do not want to find the doppelganger peer so search for the peer
3935 * in
3936 * the hash that has PEER_FLAG_CONFIG_NODE
3937 */
3938 SET_FLAG(tmp_peer.flags, PEER_FLAG_CONFIG_NODE);
3939
3940 tmp_peer.su = *su;
3941
3942 if (bgp != NULL) {
3943 peer = hash_lookup(bgp->peerhash, &tmp_peer);
3944 } else if (bm->bgp != NULL) {
3945 struct listnode *bgpnode, *nbgpnode;
3946
3947 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp)) {
3948 peer = hash_lookup(bgp->peerhash, &tmp_peer);
3949 if (peer)
3950 break;
3951 }
3952 }
3953
3954 return peer;
3955 }
3956
3957 struct peer *peer_create_bind_dynamic_neighbor(struct bgp *bgp,
3958 union sockunion *su,
3959 struct peer_group *group)
3960 {
3961 struct peer *peer;
3962 afi_t afi;
3963 safi_t safi;
3964
3965 /* Create peer first; we've already checked group config is valid. */
3966 peer = peer_create(su, NULL, bgp, bgp->as, group->conf->as,
3967 group->conf->as_type, group);
3968 if (!peer)
3969 return NULL;
3970
3971 /* Link to group */
3972 peer = peer_lock(peer);
3973 listnode_add(group->peer, peer);
3974
3975 peer_group2peer_config_copy(group, peer);
3976
3977 /*
3978 * Bind peer for all AFs configured for the group. We don't call
3979 * peer_group_bind as that is sub-optimal and does some stuff we don't
3980 * want.
3981 */
3982 FOREACH_AFI_SAFI (afi, safi) {
3983 if (!group->conf->afc[afi][safi])
3984 continue;
3985 peer->afc[afi][safi] = 1;
3986
3987 if (!peer_af_find(peer, afi, safi))
3988 peer_af_create(peer, afi, safi);
3989
3990 peer_group2peer_config_copy_af(group, peer, afi, safi);
3991 }
3992
3993 /* Mark as dynamic, but also as a "config node" for other things to
3994 * work. */
3995 SET_FLAG(peer->flags, PEER_FLAG_DYNAMIC_NEIGHBOR);
3996 SET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
3997
3998 return peer;
3999 }
4000
4001 struct prefix *
4002 peer_group_lookup_dynamic_neighbor_range(struct peer_group *group,
4003 struct prefix *prefix)
4004 {
4005 struct listnode *node, *nnode;
4006 struct prefix *range;
4007 afi_t afi;
4008
4009 afi = family2afi(prefix->family);
4010
4011 if (group->listen_range[afi])
4012 for (ALL_LIST_ELEMENTS(group->listen_range[afi], node, nnode,
4013 range))
4014 if (prefix_match(range, prefix))
4015 return range;
4016
4017 return NULL;
4018 }
4019
4020 struct peer_group *
4021 peer_group_lookup_dynamic_neighbor(struct bgp *bgp, struct prefix *prefix,
4022 struct prefix **listen_range)
4023 {
4024 struct prefix *range = NULL;
4025 struct peer_group *group = NULL;
4026 struct listnode *node, *nnode;
4027
4028 *listen_range = NULL;
4029 if (bgp != NULL) {
4030 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group))
4031 if ((range = peer_group_lookup_dynamic_neighbor_range(
4032 group, prefix)))
4033 break;
4034 } else if (bm->bgp != NULL) {
4035 struct listnode *bgpnode, *nbgpnode;
4036
4037 for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp))
4038 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group))
4039 if ((range = peer_group_lookup_dynamic_neighbor_range(
4040 group, prefix)))
4041 goto found_range;
4042 }
4043
4044 found_range:
4045 *listen_range = range;
4046 return (group && range) ? group : NULL;
4047 }
4048
4049 struct peer *peer_lookup_dynamic_neighbor(struct bgp *bgp, union sockunion *su)
4050 {
4051 struct peer_group *group;
4052 struct bgp *gbgp;
4053 struct peer *peer;
4054 struct prefix prefix;
4055 struct prefix *listen_range;
4056 int dncount;
4057
4058 if (!sockunion2hostprefix(su, &prefix))
4059 return NULL;
4060
4061 /* See if incoming connection matches a configured listen range. */
4062 group = peer_group_lookup_dynamic_neighbor(bgp, &prefix, &listen_range);
4063
4064 if (!group)
4065 return NULL;
4066
4067
4068 gbgp = group->bgp;
4069
4070 if (!gbgp)
4071 return NULL;
4072
4073 if (bgp_debug_neighbor_events(NULL))
4074 zlog_debug(
4075 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4076 &prefix, group->name, listen_range);
4077
4078 /* Are we within the listen limit? */
4079 dncount = gbgp->dynamic_neighbors_count;
4080
4081 if (dncount >= gbgp->dynamic_neighbors_limit) {
4082 if (bgp_debug_neighbor_events(NULL))
4083 zlog_debug(
4084 "Dynamic Neighbor %pFX rejected - at limit %d",
4085 &prefix, gbgp->dynamic_neighbors_limit);
4086 return NULL;
4087 }
4088
4089 /* Ensure group is not disabled. */
4090 if (CHECK_FLAG(group->conf->flags, PEER_FLAG_SHUTDOWN)) {
4091 if (bgp_debug_neighbor_events(NULL))
4092 zlog_debug(
4093 "Dynamic Neighbor %pFX rejected - group %s disabled",
4094 &prefix, group->name);
4095 return NULL;
4096 }
4097
4098 /* Check that at least one AF is activated for the group. */
4099 if (!peer_group_af_configured(group)) {
4100 if (bgp_debug_neighbor_events(NULL))
4101 zlog_debug(
4102 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4103 &prefix, group->name);
4104 return NULL;
4105 }
4106
4107 /* Create dynamic peer and bind to associated group. */
4108 peer = peer_create_bind_dynamic_neighbor(gbgp, su, group);
4109 assert(peer);
4110
4111 gbgp->dynamic_neighbors_count = ++dncount;
4112
4113 if (bgp_debug_neighbor_events(peer))
4114 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4115 peer->host, group->name, dncount);
4116
4117 return peer;
4118 }
4119
4120 static void peer_drop_dynamic_neighbor(struct peer *peer)
4121 {
4122 int dncount = -1;
4123 if (peer->group->bgp) {
4124 dncount = peer->group->bgp->dynamic_neighbors_count;
4125 if (dncount)
4126 peer->group->bgp->dynamic_neighbors_count = --dncount;
4127 }
4128 if (bgp_debug_neighbor_events(peer))
4129 zlog_debug("%s dropped from group %s, count %d", peer->host,
4130 peer->group->name, dncount);
4131 }
4132
4133 /* If peer is configured at least one address family return 1. */
4134 bool peer_active(struct peer *peer)
4135 {
4136 if (BGP_PEER_SU_UNSPEC(peer))
4137 return false;
4138 if (peer->afc[AFI_IP][SAFI_UNICAST] || peer->afc[AFI_IP][SAFI_MULTICAST]
4139 || peer->afc[AFI_IP][SAFI_LABELED_UNICAST]
4140 || peer->afc[AFI_IP][SAFI_MPLS_VPN] || peer->afc[AFI_IP][SAFI_ENCAP]
4141 || peer->afc[AFI_IP][SAFI_FLOWSPEC]
4142 || peer->afc[AFI_IP6][SAFI_UNICAST]
4143 || peer->afc[AFI_IP6][SAFI_MULTICAST]
4144 || peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]
4145 || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
4146 || peer->afc[AFI_IP6][SAFI_ENCAP]
4147 || peer->afc[AFI_IP6][SAFI_FLOWSPEC]
4148 || peer->afc[AFI_L2VPN][SAFI_EVPN])
4149 return true;
4150 return false;
4151 }
4152
4153 /* If peer is negotiated at least one address family return 1. */
4154 bool peer_active_nego(struct peer *peer)
4155 {
4156 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
4157 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
4158 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
4159 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
4160 || peer->afc_nego[AFI_IP][SAFI_ENCAP]
4161 || peer->afc_nego[AFI_IP][SAFI_FLOWSPEC]
4162 || peer->afc_nego[AFI_IP6][SAFI_UNICAST]
4163 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
4164 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
4165 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
4166 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]
4167 || peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC]
4168 || peer->afc_nego[AFI_L2VPN][SAFI_EVPN])
4169 return true;
4170 return false;
4171 }
4172
4173 /* If peer received at least one address family MP, return true */
4174 bool peer_afc_received(struct peer *peer)
4175 {
4176 afi_t afi;
4177 safi_t safi;
4178
4179 FOREACH_AFI_SAFI (afi, safi)
4180 if (peer->afc_recv[afi][safi])
4181 return true;
4182
4183 return false;
4184 }
4185
4186 /* If peer advertised at least one address family MP, return true */
4187 bool peer_afc_advertised(struct peer *peer)
4188 {
4189 afi_t afi;
4190 safi_t safi;
4191
4192 FOREACH_AFI_SAFI (afi, safi)
4193 if (peer->afc_adv[afi][safi])
4194 return true;
4195
4196 return false;
4197 }
4198
4199 void peer_change_action(struct peer *peer, afi_t afi, safi_t safi,
4200 enum peer_change_type type)
4201 {
4202 struct peer_af *paf;
4203
4204 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
4205 return;
4206
4207 if (!peer_established(peer))
4208 return;
4209
4210 if (type == peer_change_reset) {
4211 /* If we're resetting session, we've to delete both peer struct
4212 */
4213 if ((peer->doppelganger)
4214 && (peer->doppelganger->status != Deleted)
4215 && (!CHECK_FLAG(peer->doppelganger->flags,
4216 PEER_FLAG_CONFIG_NODE)))
4217 peer_delete(peer->doppelganger);
4218
4219 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4220 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4221 } else if (type == peer_change_reset_in) {
4222 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
4223 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
4224 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
4225 BGP_ROUTE_REFRESH_NORMAL);
4226 else {
4227 if ((peer->doppelganger)
4228 && (peer->doppelganger->status != Deleted)
4229 && (!CHECK_FLAG(peer->doppelganger->flags,
4230 PEER_FLAG_CONFIG_NODE)))
4231 peer_delete(peer->doppelganger);
4232
4233 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4234 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4235 }
4236 } else if (type == peer_change_reset_out) {
4237 paf = peer_af_find(peer, afi, safi);
4238 if (paf && paf->subgroup)
4239 SET_FLAG(paf->subgroup->sflags,
4240 SUBGRP_STATUS_FORCE_UPDATES);
4241
4242 update_group_adjust_peer(paf);
4243 bgp_announce_route(peer, afi, safi, false);
4244 }
4245 }
4246
4247 struct peer_flag_action {
4248 /* Peer's flag. */
4249 uint64_t flag;
4250
4251 /* This flag can be set for peer-group member. */
4252 uint8_t not_for_member;
4253
4254 /* Action when the flag is changed. */
4255 enum peer_change_type type;
4256 };
4257
4258 static const struct peer_flag_action peer_flag_action_list[] = {
4259 {PEER_FLAG_PASSIVE, 0, peer_change_reset},
4260 {PEER_FLAG_SHUTDOWN, 0, peer_change_reset},
4261 {PEER_FLAG_RTT_SHUTDOWN, 0, peer_change_none},
4262 {PEER_FLAG_DONT_CAPABILITY, 0, peer_change_none},
4263 {PEER_FLAG_OVERRIDE_CAPABILITY, 0, peer_change_none},
4264 {PEER_FLAG_STRICT_CAP_MATCH, 0, peer_change_none},
4265 {PEER_FLAG_DYNAMIC_CAPABILITY, 0, peer_change_reset},
4266 {PEER_FLAG_DISABLE_CONNECTED_CHECK, 0, peer_change_reset},
4267 {PEER_FLAG_CAPABILITY_ENHE, 0, peer_change_reset},
4268 {PEER_FLAG_ENFORCE_FIRST_AS, 0, peer_change_reset_in},
4269 {PEER_FLAG_IFPEER_V6ONLY, 0, peer_change_reset},
4270 {PEER_FLAG_ROUTEADV, 0, peer_change_none},
4271 {PEER_FLAG_TIMER, 0, peer_change_none},
4272 {PEER_FLAG_TIMER_CONNECT, 0, peer_change_none},
4273 {PEER_FLAG_TIMER_DELAYOPEN, 0, peer_change_none},
4274 {PEER_FLAG_PASSWORD, 0, peer_change_none},
4275 {PEER_FLAG_LOCAL_AS, 0, peer_change_reset},
4276 {PEER_FLAG_LOCAL_AS_NO_PREPEND, 0, peer_change_reset},
4277 {PEER_FLAG_LOCAL_AS_REPLACE_AS, 0, peer_change_reset},
4278 {PEER_FLAG_UPDATE_SOURCE, 0, peer_change_none},
4279 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE, 0, peer_change_none},
4280 {PEER_FLAG_EXTENDED_OPT_PARAMS, 0, peer_change_reset},
4281 {PEER_FLAG_ROLE_STRICT_MODE, 0, peer_change_reset},
4282 {PEER_FLAG_ROLE, 0, peer_change_reset},
4283 {PEER_FLAG_PORT, 0, peer_change_reset},
4284 {0, 0, 0}};
4285
4286 static const struct peer_flag_action peer_af_flag_action_list[] = {
4287 {PEER_FLAG_SEND_COMMUNITY, 1, peer_change_reset_out},
4288 {PEER_FLAG_SEND_EXT_COMMUNITY, 1, peer_change_reset_out},
4289 {PEER_FLAG_SEND_LARGE_COMMUNITY, 1, peer_change_reset_out},
4290 {PEER_FLAG_NEXTHOP_SELF, 1, peer_change_reset_out},
4291 {PEER_FLAG_REFLECTOR_CLIENT, 1, peer_change_reset},
4292 {PEER_FLAG_RSERVER_CLIENT, 1, peer_change_reset},
4293 {PEER_FLAG_SOFT_RECONFIG, 0, peer_change_reset_in},
4294 {PEER_FLAG_AS_PATH_UNCHANGED, 1, peer_change_reset_out},
4295 {PEER_FLAG_NEXTHOP_UNCHANGED, 1, peer_change_reset_out},
4296 {PEER_FLAG_MED_UNCHANGED, 1, peer_change_reset_out},
4297 {PEER_FLAG_DEFAULT_ORIGINATE, 0, peer_change_none},
4298 {PEER_FLAG_REMOVE_PRIVATE_AS, 1, peer_change_reset_out},
4299 {PEER_FLAG_ALLOWAS_IN, 0, peer_change_reset_in},
4300 {PEER_FLAG_ALLOWAS_IN_ORIGIN, 0, peer_change_reset_in},
4301 {PEER_FLAG_ORF_PREFIX_SM, 1, peer_change_reset},
4302 {PEER_FLAG_ORF_PREFIX_RM, 1, peer_change_reset},
4303 {PEER_FLAG_MAX_PREFIX, 0, peer_change_none},
4304 {PEER_FLAG_MAX_PREFIX_WARNING, 0, peer_change_none},
4305 {PEER_FLAG_MAX_PREFIX_FORCE, 0, peer_change_none},
4306 {PEER_FLAG_MAX_PREFIX_OUT, 0, peer_change_none},
4307 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED, 0, peer_change_reset_out},
4308 {PEER_FLAG_FORCE_NEXTHOP_SELF, 1, peer_change_reset_out},
4309 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL, 1, peer_change_reset_out},
4310 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE, 1, peer_change_reset_out},
4311 {PEER_FLAG_AS_OVERRIDE, 1, peer_change_reset_out},
4312 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE, 1, peer_change_reset_out},
4313 {PEER_FLAG_WEIGHT, 0, peer_change_reset_in},
4314 {PEER_FLAG_DISABLE_ADDPATH_RX, 0, peer_change_reset},
4315 {PEER_FLAG_SOO, 0, peer_change_reset},
4316 {PEER_FLAG_ACCEPT_OWN, 0, peer_change_reset},
4317 {0, 0, 0}};
4318
4319 /* Proper action set. */
4320 static int peer_flag_action_set(const struct peer_flag_action *action_list,
4321 int size, struct peer_flag_action *action,
4322 uint64_t flag)
4323 {
4324 int i;
4325 int found = 0;
4326 int reset_in = 0;
4327 int reset_out = 0;
4328 const struct peer_flag_action *match = NULL;
4329
4330 /* Check peer's frag action. */
4331 for (i = 0; i < size; i++) {
4332 match = &action_list[i];
4333
4334 if (match->flag == 0)
4335 break;
4336
4337 if (match->flag & flag) {
4338 found = 1;
4339
4340 if (match->type == peer_change_reset_in)
4341 reset_in = 1;
4342 if (match->type == peer_change_reset_out)
4343 reset_out = 1;
4344 if (match->type == peer_change_reset) {
4345 reset_in = 1;
4346 reset_out = 1;
4347 }
4348 if (match->not_for_member)
4349 action->not_for_member = 1;
4350 }
4351 }
4352
4353 /* Set peer clear type. */
4354 if (reset_in && reset_out)
4355 action->type = peer_change_reset;
4356 else if (reset_in)
4357 action->type = peer_change_reset_in;
4358 else if (reset_out)
4359 action->type = peer_change_reset_out;
4360 else
4361 action->type = peer_change_none;
4362
4363 return found;
4364 }
4365
4366 static void peer_flag_modify_action(struct peer *peer, uint32_t flag)
4367 {
4368 if (flag == PEER_FLAG_SHUTDOWN) {
4369 if (CHECK_FLAG(peer->flags, flag)) {
4370 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT))
4371 peer_nsf_stop(peer);
4372
4373 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
4374
4375 if (peer->t_pmax_restart) {
4376 THREAD_OFF(peer->t_pmax_restart);
4377 if (bgp_debug_neighbor_events(peer))
4378 zlog_debug(
4379 "%pBP Maximum-prefix restart timer canceled",
4380 peer);
4381 }
4382
4383 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4384 char *msg = peer->tx_shutdown_message;
4385 size_t msglen;
4386 uint8_t msgbuf[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
4387
4388 if (!msg && peer_group_active(peer))
4389 msg = peer->group->conf
4390 ->tx_shutdown_message;
4391 msglen = msg ? strlen(msg) : 0;
4392 if (msglen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4393 msglen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
4394
4395 if (msglen) {
4396 msgbuf[0] = msglen;
4397 memcpy(msgbuf + 1, msg, msglen);
4398
4399 bgp_notify_send_with_data(
4400 peer, BGP_NOTIFY_CEASE,
4401 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN,
4402 msgbuf, msglen + 1);
4403 } else
4404 bgp_notify_send(
4405 peer, BGP_NOTIFY_CEASE,
4406 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
4407 } else
4408 bgp_session_reset(peer);
4409 } else {
4410 peer->v_start = BGP_INIT_START_TIMER;
4411 BGP_EVENT_ADD(peer, BGP_Stop);
4412 }
4413 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4414 if (flag == PEER_FLAG_DYNAMIC_CAPABILITY)
4415 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4416 else if (flag == PEER_FLAG_PASSIVE)
4417 peer->last_reset = PEER_DOWN_PASSIVE_CHANGE;
4418 else if (flag == PEER_FLAG_DISABLE_CONNECTED_CHECK)
4419 peer->last_reset = PEER_DOWN_MULTIHOP_CHANGE;
4420
4421 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4422 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4423 } else
4424 bgp_session_reset(peer);
4425 }
4426
4427 /* Enable global administrative shutdown of all peers of BGP instance */
4428 void bgp_shutdown_enable(struct bgp *bgp, const char *msg)
4429 {
4430 struct peer *peer;
4431 struct listnode *node;
4432 /* length(1) + message(N) */
4433 uint8_t data[BGP_ADMIN_SHUTDOWN_MSG_LEN + 1];
4434
4435 /* do nothing if already shut down */
4436 if (CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4437 return;
4438
4439 /* informational log message */
4440 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4441 bgp->as);
4442
4443 /* iterate through peers of BGP instance */
4444 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
4445 /* continue, if peer is already in administrative shutdown. */
4446 if (CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN))
4447 continue;
4448
4449 /* send a RFC 4486 notification message if necessary */
4450 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
4451 if (msg) {
4452 size_t datalen = strlen(msg);
4453
4454 if (datalen > BGP_ADMIN_SHUTDOWN_MSG_LEN)
4455 datalen = BGP_ADMIN_SHUTDOWN_MSG_LEN;
4456
4457 data[0] = datalen;
4458 memcpy(data + 1, msg, datalen);
4459
4460 bgp_notify_send_with_data(
4461 peer, BGP_NOTIFY_CEASE,
4462 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, data,
4463 datalen + 1);
4464 } else {
4465 bgp_notify_send(
4466 peer, BGP_NOTIFY_CEASE,
4467 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN);
4468 }
4469 }
4470
4471 /* reset start timer to initial value */
4472 peer->v_start = BGP_INIT_START_TIMER;
4473
4474 /* trigger a RFC 4271 ManualStop event */
4475 BGP_EVENT_ADD(peer, BGP_Stop);
4476 }
4477
4478 /* set the BGP instances shutdown flag */
4479 SET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4480 }
4481
4482 /* Disable global administrative shutdown of all peers of BGP instance */
4483 void bgp_shutdown_disable(struct bgp *bgp)
4484 {
4485 /* do nothing if not shut down. */
4486 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN))
4487 return;
4488
4489 /* informational log message */
4490 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4491 bgp->as);
4492
4493 /* clear the BGP instances shutdown flag */
4494 UNSET_FLAG(bgp->flags, BGP_FLAG_SHUTDOWN);
4495 }
4496
4497 /* Change specified peer flag. */
4498 static int peer_flag_modify(struct peer *peer, uint64_t flag, int set)
4499 {
4500 int found;
4501 int size;
4502 bool invert, member_invert;
4503 struct peer *member;
4504 struct listnode *node, *nnode;
4505 struct peer_flag_action action;
4506
4507 memset(&action, 0, sizeof(struct peer_flag_action));
4508 size = sizeof(peer_flag_action_list) / sizeof(struct peer_flag_action);
4509
4510 invert = CHECK_FLAG(peer->flags_invert, flag);
4511 found = peer_flag_action_set(peer_flag_action_list, size, &action,
4512 flag);
4513
4514 /* Abort if no flag action exists. */
4515 if (!found)
4516 return BGP_ERR_INVALID_FLAG;
4517
4518 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4519 if (set && CHECK_FLAG(peer->flags | flag, PEER_FLAG_STRICT_CAP_MATCH)
4520 && CHECK_FLAG(peer->flags | flag, PEER_FLAG_OVERRIDE_CAPABILITY))
4521 return BGP_ERR_PEER_FLAG_CONFLICT;
4522
4523 /* Handle flag updates where desired state matches current state. */
4524 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4525 if (set && CHECK_FLAG(peer->flags, flag)) {
4526 COND_FLAG(peer->flags_override, flag, !invert);
4527 return 0;
4528 }
4529
4530 if (!set && !CHECK_FLAG(peer->flags, flag)) {
4531 COND_FLAG(peer->flags_override, flag, invert);
4532 return 0;
4533 }
4534 }
4535
4536 /* Inherit from peer-group or set/unset flags accordingly. */
4537 if (peer_group_active(peer) && set == invert)
4538 peer_flag_inherit(peer, flag);
4539 else
4540 COND_FLAG(peer->flags, flag, set);
4541
4542 /* Check if handling a regular peer. */
4543 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4544 /* Update flag override state accordingly. */
4545 COND_FLAG(peer->flags_override, flag, set != invert);
4546
4547 /*
4548 * For the extended next-hop encoding flag we need to turn RAs
4549 * on if flag is being set, but only turn RAs off if the flag
4550 * is being unset on this peer and if this peer is a member of a
4551 * peer-group, the peer-group also doesn't have the flag set.
4552 */
4553 if (flag == PEER_FLAG_CAPABILITY_ENHE) {
4554 if (set) {
4555 bgp_zebra_initiate_radv(peer->bgp, peer);
4556 } else if (peer_group_active(peer)) {
4557 if (!CHECK_FLAG(peer->group->conf->flags,
4558 flag) &&
4559 !peer->conf_if)
4560 bgp_zebra_terminate_radv(peer->bgp,
4561 peer);
4562 } else
4563 bgp_zebra_terminate_radv(peer->bgp, peer);
4564 }
4565
4566 /* Execute flag action on peer. */
4567 if (action.type == peer_change_reset)
4568 peer_flag_modify_action(peer, flag);
4569
4570 /* Skip peer-group mechanics for regular peers. */
4571 return 0;
4572 }
4573
4574 /*
4575 * Update peer-group members, unless they are explicitly overriding
4576 * peer-group configuration.
4577 */
4578 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
4579 /* Skip peers with overridden configuration. */
4580 if (CHECK_FLAG(member->flags_override, flag))
4581 continue;
4582
4583 /* Check if only member without group is inverted. */
4584 member_invert =
4585 CHECK_FLAG(member->flags_invert, flag) && !invert;
4586
4587 /* Skip peers with equivalent configuration. */
4588 if (set != member_invert && CHECK_FLAG(member->flags, flag))
4589 continue;
4590
4591 if (set == member_invert && !CHECK_FLAG(member->flags, flag))
4592 continue;
4593
4594 /* Update flag on peer-group member. */
4595 COND_FLAG(member->flags, flag, set != member_invert);
4596
4597 if (flag == PEER_FLAG_CAPABILITY_ENHE && !member->conf_if)
4598 set ? bgp_zebra_initiate_radv(member->bgp, member)
4599 : bgp_zebra_terminate_radv(member->bgp, member);
4600
4601 /* Execute flag action on peer-group member. */
4602 if (action.type == peer_change_reset)
4603 peer_flag_modify_action(member, flag);
4604 }
4605
4606 return 0;
4607 }
4608
4609 int peer_flag_set(struct peer *peer, uint64_t flag)
4610 {
4611 return peer_flag_modify(peer, flag, 1);
4612 }
4613
4614 int peer_flag_unset(struct peer *peer, uint64_t flag)
4615 {
4616 return peer_flag_modify(peer, flag, 0);
4617 }
4618
4619 static int peer_af_flag_modify(struct peer *peer, afi_t afi, safi_t safi,
4620 uint64_t flag, bool set)
4621 {
4622 int found;
4623 int size;
4624 bool invert, member_invert;
4625 struct peer *member;
4626 struct listnode *node, *nnode;
4627 struct peer_flag_action action;
4628 enum bgp_peer_sort ptype;
4629
4630 memset(&action, 0, sizeof(struct peer_flag_action));
4631 size = sizeof(peer_af_flag_action_list)
4632 / sizeof(struct peer_flag_action);
4633
4634 invert = CHECK_FLAG(peer->af_flags_invert[afi][safi], flag);
4635 found = peer_flag_action_set(peer_af_flag_action_list, size, &action,
4636 flag);
4637
4638 /* Abort if flag action exists. */
4639 if (!found)
4640 return BGP_ERR_INVALID_FLAG;
4641
4642 ptype = peer_sort(peer);
4643 /* Special check for reflector client. */
4644 if (flag & PEER_FLAG_REFLECTOR_CLIENT && ptype != BGP_PEER_IBGP)
4645 return BGP_ERR_NOT_INTERNAL_PEER;
4646
4647 /* Do not remove reflector client when ORR is configured on this peer */
4648 if (flag & PEER_FLAG_REFLECTOR_CLIENT && !set &&
4649 peer_orr_rrclient_check(peer, afi, safi))
4650 return BGP_ERR_PEER_ORR_CONFIGURED;
4651
4652 /* Special check for remove-private-AS. */
4653 if (flag & PEER_FLAG_REMOVE_PRIVATE_AS && ptype == BGP_PEER_IBGP)
4654 return BGP_ERR_REMOVE_PRIVATE_AS;
4655
4656 /* as-override is not allowed for IBGP peers */
4657 if (flag & PEER_FLAG_AS_OVERRIDE && ptype == BGP_PEER_IBGP)
4658 return BGP_ERR_AS_OVERRIDE;
4659
4660 /* Handle flag updates where desired state matches current state. */
4661 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4662 if (set && CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
4663 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4664 !invert);
4665 return 0;
4666 }
4667
4668 if (!set && !CHECK_FLAG(peer->af_flags[afi][safi], flag)) {
4669 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4670 invert);
4671 return 0;
4672 }
4673 }
4674
4675 /*
4676 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4677 * if we are setting/unsetting flags which conflict with this flag
4678 * handle accordingly
4679 */
4680 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
4681 if (set) {
4682
4683 /*
4684 * if we are setting NEXTHOP_SELF, we need to unset the
4685 * NEXTHOP_UNCHANGED flag
4686 */
4687 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4688 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4689 UNSET_FLAG(peer->af_flags[afi][safi],
4690 PEER_FLAG_NEXTHOP_UNCHANGED);
4691 } else {
4692
4693 /*
4694 * if we are unsetting NEXTHOP_SELF, we need to set the
4695 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4696 */
4697 if (CHECK_FLAG(flag, PEER_FLAG_NEXTHOP_SELF) ||
4698 CHECK_FLAG(flag, PEER_FLAG_FORCE_NEXTHOP_SELF))
4699 SET_FLAG(peer->af_flags[afi][safi],
4700 PEER_FLAG_NEXTHOP_UNCHANGED);
4701 }
4702 }
4703
4704 /*
4705 * If the peer is a route server client let's not
4706 * muck with the nexthop on the way out the door
4707 */
4708 if (flag & PEER_FLAG_RSERVER_CLIENT) {
4709 if (set)
4710 SET_FLAG(peer->af_flags[afi][safi],
4711 PEER_FLAG_NEXTHOP_UNCHANGED);
4712 else
4713 UNSET_FLAG(peer->af_flags[afi][safi],
4714 PEER_FLAG_NEXTHOP_UNCHANGED);
4715 }
4716
4717 /* Inherit from peer-group or set/unset flags accordingly. */
4718 if (peer_group_active(peer) && set == invert)
4719 peer_af_flag_inherit(peer, afi, safi, flag);
4720 else
4721 COND_FLAG(peer->af_flags[afi][safi], flag, set);
4722
4723 /* Execute action when peer is established. */
4724 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
4725 && peer_established(peer)) {
4726 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
4727 bgp_clear_adj_in(peer, afi, safi);
4728 else {
4729 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
4730 peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE;
4731 else if (flag == PEER_FLAG_RSERVER_CLIENT)
4732 peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE;
4733 else if (flag == PEER_FLAG_ORF_PREFIX_SM)
4734 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4735 else if (flag == PEER_FLAG_ORF_PREFIX_RM)
4736 peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE;
4737
4738 peer_change_action(peer, afi, safi, action.type);
4739 }
4740 }
4741
4742 /* Check if handling a regular peer. */
4743 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4744 COND_FLAG(peer->af_flags_override[afi][safi], flag,
4745 set != invert);
4746 } else {
4747 /*
4748 * Update peer-group members, unless they are explicitly
4749 * overriding peer-group configuration.
4750 */
4751 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode,
4752 member)) {
4753 /* Skip peers with overridden configuration. */
4754 if (CHECK_FLAG(member->af_flags_override[afi][safi],
4755 flag))
4756 continue;
4757
4758 /* Check if only member without group is inverted. */
4759 member_invert =
4760 CHECK_FLAG(member->af_flags_invert[afi][safi],
4761 flag)
4762 && !invert;
4763
4764 /* Skip peers with equivalent configuration. */
4765 if (set != member_invert
4766 && CHECK_FLAG(member->af_flags[afi][safi], flag))
4767 continue;
4768
4769 if (set == member_invert
4770 && !CHECK_FLAG(member->af_flags[afi][safi], flag))
4771 continue;
4772
4773 /* Update flag on peer-group member. */
4774 COND_FLAG(member->af_flags[afi][safi], flag,
4775 set != member_invert);
4776
4777 /* Execute flag action on peer-group member. */
4778 if (peer_established(member)) {
4779 if (!set && flag == PEER_FLAG_SOFT_RECONFIG)
4780 bgp_clear_adj_in(member, afi, safi);
4781 else {
4782 if (flag == PEER_FLAG_REFLECTOR_CLIENT)
4783 member->last_reset =
4784 PEER_DOWN_RR_CLIENT_CHANGE;
4785 else if (flag
4786 == PEER_FLAG_RSERVER_CLIENT)
4787 member->last_reset =
4788 PEER_DOWN_RS_CLIENT_CHANGE;
4789 else if (flag
4790 == PEER_FLAG_ORF_PREFIX_SM)
4791 member->last_reset =
4792 PEER_DOWN_CAPABILITY_CHANGE;
4793 else if (flag
4794 == PEER_FLAG_ORF_PREFIX_RM)
4795 member->last_reset =
4796 PEER_DOWN_CAPABILITY_CHANGE;
4797
4798 peer_change_action(member, afi, safi,
4799 action.type);
4800 }
4801 }
4802 }
4803 }
4804
4805 return 0;
4806 }
4807
4808 int peer_af_flag_set(struct peer *peer, afi_t afi, safi_t safi, uint64_t flag)
4809 {
4810 return peer_af_flag_modify(peer, afi, safi, flag, 1);
4811 }
4812
4813 int peer_af_flag_unset(struct peer *peer, afi_t afi, safi_t safi, uint64_t flag)
4814 {
4815 return peer_af_flag_modify(peer, afi, safi, flag, 0);
4816 }
4817
4818
4819 void peer_tx_shutdown_message_set(struct peer *peer, const char *msg)
4820 {
4821 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
4822 peer->tx_shutdown_message =
4823 msg ? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG, msg) : NULL;
4824 }
4825
4826 void peer_tx_shutdown_message_unset(struct peer *peer)
4827 {
4828 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG, peer->tx_shutdown_message);
4829 }
4830
4831
4832 /* EBGP multihop configuration. */
4833 int peer_ebgp_multihop_set(struct peer *peer, int ttl)
4834 {
4835 struct peer_group *group;
4836 struct listnode *node, *nnode;
4837 struct peer *peer1;
4838
4839 if (peer->sort == BGP_PEER_IBGP || peer->conf_if)
4840 return 0;
4841
4842 /* is there anything to do? */
4843 if (peer->ttl == ttl)
4844 return 0;
4845
4846 /* see comment in peer_ttl_security_hops_set() */
4847 if (ttl != MAXTTL) {
4848 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4849 group = peer->group;
4850 if (group->conf->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4851 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4852
4853 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
4854 peer1)) {
4855 if (peer1->sort == BGP_PEER_IBGP)
4856 continue;
4857
4858 if (peer1->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4859 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4860 }
4861 } else {
4862 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED)
4863 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4864 }
4865 }
4866
4867 peer->ttl = ttl;
4868
4869 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4870 if (peer->sort != BGP_PEER_IBGP) {
4871 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4872 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4873 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4874 else
4875 bgp_session_reset(peer);
4876
4877 /* Reconfigure BFD peer with new TTL. */
4878 if (peer->bfd_config)
4879 bgp_peer_bfd_update_source(peer);
4880 }
4881 } else {
4882 group = peer->group;
4883 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
4884 if (peer->sort == BGP_PEER_IBGP)
4885 continue;
4886
4887 peer->ttl = group->conf->ttl;
4888
4889 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4890 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4891 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4892 else
4893 bgp_session_reset(peer);
4894
4895 /* Reconfigure BFD peer with new TTL. */
4896 if (peer->bfd_config)
4897 bgp_peer_bfd_update_source(peer);
4898 }
4899 }
4900 return 0;
4901 }
4902
4903 int peer_ebgp_multihop_unset(struct peer *peer)
4904 {
4905 struct peer_group *group;
4906 struct listnode *node, *nnode;
4907 int ttl;
4908
4909 if (peer->sort == BGP_PEER_IBGP)
4910 return 0;
4911
4912 if (peer->gtsm_hops != BGP_GTSM_HOPS_DISABLED && peer->ttl != MAXTTL)
4913 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
4914
4915 if (peer_group_active(peer))
4916 ttl = peer->group->conf->ttl;
4917 else
4918 ttl = BGP_DEFAULT_TTL;
4919
4920 if (ttl == peer->ttl)
4921 return 0;
4922
4923 peer->ttl = ttl;
4924
4925 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4926 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4927 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
4928 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4929 else
4930 bgp_session_reset(peer);
4931
4932 /* Reconfigure BFD peer with new TTL. */
4933 if (peer->bfd_config)
4934 bgp_peer_bfd_update_source(peer);
4935 } else {
4936 group = peer->group;
4937 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
4938 if (peer->sort == BGP_PEER_IBGP)
4939 continue;
4940
4941 peer->ttl = BGP_DEFAULT_TTL;
4942
4943 if (peer->fd >= 0) {
4944 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
4945 bgp_notify_send(
4946 peer, BGP_NOTIFY_CEASE,
4947 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
4948 else
4949 bgp_session_reset(peer);
4950 }
4951
4952 /* Reconfigure BFD peer with new TTL. */
4953 if (peer->bfd_config)
4954 bgp_peer_bfd_update_source(peer);
4955 }
4956 }
4957 return 0;
4958 }
4959
4960 /* Set Open Policy Role and check its correctness */
4961 int peer_role_set(struct peer *peer, uint8_t role, bool strict_mode)
4962 {
4963 struct peer *member;
4964 struct listnode *node, *nnode;
4965
4966 peer_flag_set(peer, PEER_FLAG_ROLE);
4967
4968 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
4969 if (peer->sort != BGP_PEER_EBGP)
4970 return BGP_ERR_INVALID_INTERNAL_ROLE;
4971
4972 if (peer->local_role == role) {
4973 if (CHECK_FLAG(peer->flags,
4974 PEER_FLAG_ROLE_STRICT_MODE) &&
4975 !strict_mode)
4976 /* TODO: Is session restart needed if it was
4977 * down?
4978 */
4979 UNSET_FLAG(peer->flags,
4980 PEER_FLAG_ROLE_STRICT_MODE);
4981 if (!CHECK_FLAG(peer->flags,
4982 PEER_FLAG_ROLE_STRICT_MODE) &&
4983 strict_mode) {
4984 SET_FLAG(peer->flags,
4985 PEER_FLAG_ROLE_STRICT_MODE);
4986 /* Restart session to throw Role Mismatch
4987 * Notification
4988 */
4989 if (peer->remote_role == ROLE_UNDEFINED)
4990 bgp_session_reset(peer);
4991 }
4992 } else {
4993 peer->local_role = role;
4994 if (strict_mode)
4995 SET_FLAG(peer->flags,
4996 PEER_FLAG_ROLE_STRICT_MODE);
4997 else
4998 UNSET_FLAG(peer->flags,
4999 PEER_FLAG_ROLE_STRICT_MODE);
5000 bgp_session_reset(peer);
5001 }
5002
5003 return CMD_SUCCESS;
5004 }
5005
5006 peer->local_role = role;
5007 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5008 if (member->sort != BGP_PEER_EBGP)
5009 return BGP_ERR_INVALID_INTERNAL_ROLE;
5010
5011 if (member->local_role == role) {
5012 if (CHECK_FLAG(member->flags,
5013 PEER_FLAG_ROLE_STRICT_MODE) &&
5014 !strict_mode)
5015 /* TODO: Is session restart needed if it was
5016 * down?
5017 */
5018 UNSET_FLAG(member->flags,
5019 PEER_FLAG_ROLE_STRICT_MODE);
5020 if (!CHECK_FLAG(member->flags,
5021 PEER_FLAG_ROLE_STRICT_MODE) &&
5022 strict_mode) {
5023 SET_FLAG(peer->flags,
5024 PEER_FLAG_ROLE_STRICT_MODE);
5025 SET_FLAG(member->flags,
5026 PEER_FLAG_ROLE_STRICT_MODE);
5027 /* Restart session to throw Role Mismatch
5028 * Notification
5029 */
5030 if (member->remote_role == ROLE_UNDEFINED)
5031 bgp_session_reset(member);
5032 }
5033 } else {
5034 member->local_role = role;
5035
5036 if (strict_mode) {
5037 SET_FLAG(peer->flags,
5038 PEER_FLAG_ROLE_STRICT_MODE);
5039 SET_FLAG(member->flags,
5040 PEER_FLAG_ROLE_STRICT_MODE);
5041 } else {
5042 UNSET_FLAG(member->flags,
5043 PEER_FLAG_ROLE_STRICT_MODE);
5044 }
5045 bgp_session_reset(member);
5046 }
5047 }
5048
5049 return CMD_SUCCESS;
5050 }
5051
5052 int peer_role_unset(struct peer *peer)
5053 {
5054 struct peer *member;
5055 struct listnode *node, *nnode;
5056
5057 peer_flag_unset(peer, PEER_FLAG_ROLE);
5058
5059 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5060 return peer_role_set(peer, ROLE_UNDEFINED, 0);
5061
5062 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member))
5063 peer_role_set(member, ROLE_UNDEFINED, 0);
5064
5065 return CMD_SUCCESS;
5066 }
5067
5068 /* Neighbor description. */
5069 void peer_description_set(struct peer *peer, const char *desc)
5070 {
5071 XFREE(MTYPE_PEER_DESC, peer->desc);
5072
5073 peer->desc = XSTRDUP(MTYPE_PEER_DESC, desc);
5074 }
5075
5076 void peer_description_unset(struct peer *peer)
5077 {
5078 XFREE(MTYPE_PEER_DESC, peer->desc);
5079 }
5080
5081 /* Neighbor update-source. */
5082 int peer_update_source_if_set(struct peer *peer, const char *ifname)
5083 {
5084 struct peer *member;
5085 struct listnode *node, *nnode;
5086
5087 /* Set flag and configuration on peer. */
5088 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
5089 if (peer->update_if) {
5090 if (strcmp(peer->update_if, ifname) == 0)
5091 return 0;
5092 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5093 }
5094 peer->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
5095 sockunion_free(peer->update_source);
5096 peer->update_source = NULL;
5097
5098 /* Check if handling a regular peer. */
5099 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5100 /* Send notification or reset peer depending on state. */
5101 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5102 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5103 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5104 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5105 } else
5106 bgp_session_reset(peer);
5107
5108 /* Apply new source configuration to BFD session. */
5109 if (peer->bfd_config)
5110 bgp_peer_bfd_update_source(peer);
5111
5112 /* Skip peer-group mechanics for regular peers. */
5113 return 0;
5114 }
5115
5116 /*
5117 * Set flag and configuration on all peer-group members, unless they are
5118 * explicitly overriding peer-group configuration.
5119 */
5120 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5121 /* Skip peers with overridden configuration. */
5122 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5123 continue;
5124
5125 /* Skip peers with the same configuration. */
5126 if (member->update_if) {
5127 if (strcmp(member->update_if, ifname) == 0)
5128 continue;
5129 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5130 }
5131
5132 /* Set flag and configuration on peer-group member. */
5133 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5134 member->update_if = XSTRDUP(MTYPE_PEER_UPDATE_SOURCE, ifname);
5135 sockunion_free(member->update_source);
5136 member->update_source = NULL;
5137
5138 /* Send notification or reset peer depending on state. */
5139 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5140 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5141 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5142 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5143 } else
5144 bgp_session_reset(member);
5145
5146 /* Apply new source configuration to BFD session. */
5147 if (member->bfd_config)
5148 bgp_peer_bfd_update_source(member);
5149 }
5150
5151 return 0;
5152 }
5153
5154 void peer_update_source_addr_set(struct peer *peer, const union sockunion *su)
5155 {
5156 struct peer *member;
5157 struct listnode *node, *nnode;
5158
5159 /* Set flag and configuration on peer. */
5160 peer_flag_set(peer, PEER_FLAG_UPDATE_SOURCE);
5161 if (peer->update_source) {
5162 if (sockunion_cmp(peer->update_source, su) == 0)
5163 return;
5164 sockunion_free(peer->update_source);
5165 }
5166 peer->update_source = sockunion_dup(su);
5167 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5168
5169 /* Check if handling a regular peer. */
5170 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5171 /* Send notification or reset peer depending on state. */
5172 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5173 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5174 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5175 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5176 } else
5177 bgp_session_reset(peer);
5178
5179 /* Apply new source configuration to BFD session. */
5180 if (peer->bfd_config)
5181 bgp_peer_bfd_update_source(peer);
5182
5183 /* Skip peer-group mechanics for regular peers. */
5184 return;
5185 }
5186
5187 /*
5188 * Set flag and configuration on all peer-group members, unless they are
5189 * explicitly overriding peer-group configuration.
5190 */
5191 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5192 /* Skip peers with overridden configuration. */
5193 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5194 continue;
5195
5196 /* Skip peers with the same configuration. */
5197 if (member->update_source) {
5198 if (sockunion_cmp(member->update_source, su) == 0)
5199 continue;
5200 sockunion_free(member->update_source);
5201 }
5202
5203 /* Set flag and configuration on peer-group member. */
5204 SET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5205 member->update_source = sockunion_dup(su);
5206 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5207
5208 /* Send notification or reset peer depending on state. */
5209 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5210 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5211 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5212 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5213 } else
5214 bgp_session_reset(member);
5215
5216 /* Apply new source configuration to BFD session. */
5217 if (member->bfd_config)
5218 bgp_peer_bfd_update_source(member);
5219 }
5220 }
5221
5222 void peer_update_source_unset(struct peer *peer)
5223 {
5224 struct peer *member;
5225 struct listnode *node, *nnode;
5226
5227 if (!CHECK_FLAG(peer->flags, PEER_FLAG_UPDATE_SOURCE))
5228 return;
5229
5230 /* Inherit configuration from peer-group if peer is member. */
5231 if (peer_group_active(peer)) {
5232 peer_flag_inherit(peer, PEER_FLAG_UPDATE_SOURCE);
5233 PEER_SU_ATTR_INHERIT(peer, peer->group, update_source);
5234 PEER_STR_ATTR_INHERIT(peer, peer->group, update_if,
5235 MTYPE_PEER_UPDATE_SOURCE);
5236 } else {
5237 /* Otherwise remove flag and configuration from peer. */
5238 peer_flag_unset(peer, PEER_FLAG_UPDATE_SOURCE);
5239 sockunion_free(peer->update_source);
5240 peer->update_source = NULL;
5241 XFREE(MTYPE_PEER_UPDATE_SOURCE, peer->update_if);
5242 }
5243
5244 /* Check if handling a regular peer. */
5245 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5246 /* Send notification or reset peer depending on state. */
5247 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
5248 peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5249 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
5250 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5251 } else
5252 bgp_session_reset(peer);
5253
5254 /* Apply new source configuration to BFD session. */
5255 if (peer->bfd_config)
5256 bgp_peer_bfd_update_source(peer);
5257
5258 /* Skip peer-group mechanics for regular peers. */
5259 return;
5260 }
5261
5262 /*
5263 * Set flag and configuration on all peer-group members, unless they are
5264 * explicitly overriding peer-group configuration.
5265 */
5266 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5267 /* Skip peers with overridden configuration. */
5268 if (CHECK_FLAG(member->flags_override, PEER_FLAG_UPDATE_SOURCE))
5269 continue;
5270
5271 /* Skip peers with the same configuration. */
5272 if (!CHECK_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE)
5273 && !member->update_source && !member->update_if)
5274 continue;
5275
5276 /* Remove flag and configuration on peer-group member. */
5277 UNSET_FLAG(member->flags, PEER_FLAG_UPDATE_SOURCE);
5278 sockunion_free(member->update_source);
5279 member->update_source = NULL;
5280 XFREE(MTYPE_PEER_UPDATE_SOURCE, member->update_if);
5281
5282 /* Send notification or reset peer depending on state. */
5283 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
5284 member->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE;
5285 bgp_notify_send(member, BGP_NOTIFY_CEASE,
5286 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
5287 } else
5288 bgp_session_reset(member);
5289
5290 /* Apply new source configuration to BFD session. */
5291 if (member->bfd_config)
5292 bgp_peer_bfd_update_source(member);
5293 }
5294 }
5295
5296 int peer_default_originate_set(struct peer *peer, afi_t afi, safi_t safi,
5297 const char *rmap, struct route_map *route_map)
5298 {
5299 struct peer *member;
5300 struct listnode *node, *nnode;
5301 struct update_subgroup *subgrp;
5302
5303 /* Set flag and configuration on peer. */
5304 peer_af_flag_set(peer, afi, safi, PEER_FLAG_DEFAULT_ORIGINATE);
5305
5306 subgrp = peer_subgroup(peer, afi, safi);
5307
5308 if (rmap) {
5309 if (!peer->default_rmap[afi][safi].name
5310 || strcmp(rmap, peer->default_rmap[afi][safi].name) != 0) {
5311 if (peer->default_rmap[afi][safi].name)
5312 XFREE(MTYPE_ROUTE_MAP_NAME,
5313 peer->default_rmap[afi][safi].name);
5314
5315 /*
5316 * When there is a change in route-map policy,
5317 * this flow gets triggered. Since, the default
5318 * route is already originated, the flag is set.
5319 * The flag should be unset here,
5320 * to trigger the flow of sending update message.
5321 */
5322 if (subgrp)
5323 UNSET_FLAG(subgrp->sflags,
5324 SUBGRP_STATUS_DEFAULT_ORIGINATE);
5325
5326 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5327 peer->default_rmap[afi][safi].name =
5328 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
5329 peer->default_rmap[afi][safi].map = route_map;
5330 route_map_counter_increment(route_map);
5331 }
5332 } else if (!rmap) {
5333 if (peer->default_rmap[afi][safi].name)
5334 XFREE(MTYPE_ROUTE_MAP_NAME,
5335 peer->default_rmap[afi][safi].name);
5336
5337 /*
5338 * This is triggered in case of route-map deletion.
5339 * The flag needs to be unset, to trigger the flow
5340 * of sending an update message.
5341 */
5342 if (subgrp)
5343 UNSET_FLAG(subgrp->sflags,
5344 SUBGRP_STATUS_DEFAULT_ORIGINATE);
5345
5346 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5347 peer->default_rmap[afi][safi].name = NULL;
5348 peer->default_rmap[afi][safi].map = NULL;
5349 }
5350
5351 /* Check if handling a regular peer. */
5352 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5353 /* Update peer route announcements. */
5354 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
5355 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5356 bgp_default_originate(peer, afi, safi, 0);
5357 bgp_announce_route(peer, afi, safi, false);
5358 }
5359
5360 /* Skip peer-group mechanics for regular peers. */
5361 return 0;
5362 }
5363
5364 /*
5365 * Set flag and configuration on all peer-group members, unless they are
5366 * explicitly overriding peer-group configuration.
5367 */
5368 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5369 /* Skip peers with overridden configuration. */
5370 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5371 PEER_FLAG_DEFAULT_ORIGINATE))
5372 continue;
5373
5374 /* Set flag and configuration on peer-group member. */
5375 SET_FLAG(member->af_flags[afi][safi],
5376 PEER_FLAG_DEFAULT_ORIGINATE);
5377 if (rmap) {
5378 if (member->default_rmap[afi][safi].name)
5379 XFREE(MTYPE_ROUTE_MAP_NAME,
5380 member->default_rmap[afi][safi].name);
5381 route_map_counter_decrement(
5382 member->default_rmap[afi][safi].map);
5383 member->default_rmap[afi][safi].name =
5384 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
5385 member->default_rmap[afi][safi].map = route_map;
5386 route_map_counter_increment(route_map);
5387 }
5388
5389 /* Update peer route announcements. */
5390 if (peer_established(member) && member->afc_nego[afi][safi]) {
5391 update_group_adjust_peer(
5392 peer_af_find(member, afi, safi));
5393 bgp_default_originate(member, afi, safi, 0);
5394 bgp_announce_route(member, afi, safi, false);
5395 }
5396 }
5397
5398 return 0;
5399 }
5400
5401 int peer_default_originate_unset(struct peer *peer, afi_t afi, safi_t safi)
5402 {
5403 struct peer *member;
5404 struct listnode *node, *nnode;
5405
5406 /* Inherit configuration from peer-group if peer is member. */
5407 if (peer_group_active(peer)) {
5408 peer_af_flag_inherit(peer, afi, safi,
5409 PEER_FLAG_DEFAULT_ORIGINATE);
5410 PEER_STR_ATTR_INHERIT(peer, peer->group,
5411 default_rmap[afi][safi].name,
5412 MTYPE_ROUTE_MAP_NAME);
5413 PEER_ATTR_INHERIT(peer, peer->group,
5414 default_rmap[afi][safi].map);
5415 } else {
5416 /* Otherwise remove flag and configuration from peer. */
5417 peer_af_flag_unset(peer, afi, safi,
5418 PEER_FLAG_DEFAULT_ORIGINATE);
5419 if (peer->default_rmap[afi][safi].name)
5420 XFREE(MTYPE_ROUTE_MAP_NAME,
5421 peer->default_rmap[afi][safi].name);
5422 route_map_counter_decrement(peer->default_rmap[afi][safi].map);
5423 peer->default_rmap[afi][safi].name = NULL;
5424 peer->default_rmap[afi][safi].map = NULL;
5425 }
5426
5427 /* Check if handling a regular peer. */
5428 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5429 /* Update peer route announcements. */
5430 if (peer_established(peer) && peer->afc_nego[afi][safi]) {
5431 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5432 bgp_default_originate(peer, afi, safi, 1);
5433 bgp_announce_route(peer, afi, safi, false);
5434 }
5435
5436 /* Skip peer-group mechanics for regular peers. */
5437 return 0;
5438 }
5439
5440 /*
5441 * Remove flag and configuration from all peer-group members, unless
5442 * they are explicitly overriding peer-group configuration.
5443 */
5444 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5445 /* Skip peers with overridden configuration. */
5446 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5447 PEER_FLAG_DEFAULT_ORIGINATE))
5448 continue;
5449
5450 /* Remove flag and configuration on peer-group member. */
5451 UNSET_FLAG(member->af_flags[afi][safi],
5452 PEER_FLAG_DEFAULT_ORIGINATE);
5453 if (member->default_rmap[afi][safi].name)
5454 XFREE(MTYPE_ROUTE_MAP_NAME,
5455 member->default_rmap[afi][safi].name);
5456 route_map_counter_decrement(member->default_rmap[afi][safi].map);
5457 member->default_rmap[afi][safi].name = NULL;
5458 member->default_rmap[afi][safi].map = NULL;
5459
5460 /* Update peer route announcements. */
5461 if (peer_established(member) && member->afc_nego[afi][safi]) {
5462 update_group_adjust_peer(peer_af_find(member, afi, safi));
5463 bgp_default_originate(member, afi, safi, 1);
5464 bgp_announce_route(member, afi, safi, false);
5465 }
5466 }
5467
5468 return 0;
5469 }
5470
5471 void peer_port_set(struct peer *peer, uint16_t port)
5472 {
5473 peer->port = port;
5474 peer_flag_set(peer, PEER_FLAG_PORT);
5475 }
5476
5477 void peer_port_unset(struct peer *peer)
5478 {
5479 peer->port = BGP_PORT_DEFAULT;
5480 peer_flag_unset(peer, PEER_FLAG_PORT);
5481 }
5482
5483 /* Set the TCP-MSS value in the peer structure,
5484 * This gets applied only after connection reset
5485 * So this value will be used in bgp_connect.
5486 */
5487 void peer_tcp_mss_set(struct peer *peer, uint32_t tcp_mss)
5488 {
5489 peer->tcp_mss = tcp_mss;
5490 SET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5491 }
5492
5493 /* Reset the TCP-MSS value in the peer structure,
5494 * This gets applied only after connection reset
5495 * So this value will be used in bgp_connect.
5496 */
5497 void peer_tcp_mss_unset(struct peer *peer)
5498 {
5499 UNSET_FLAG(peer->flags, PEER_FLAG_TCP_MSS);
5500 peer->tcp_mss = 0;
5501 }
5502
5503 /*
5504 * Helper function that is called after the name of the policy
5505 * being used by a peer has changed (AF specific). Automatically
5506 * initiates inbound or outbound processing as needed.
5507 */
5508 void peer_on_policy_change(struct peer *peer, afi_t afi, safi_t safi,
5509 int outbound)
5510 {
5511 if (outbound) {
5512 update_group_adjust_peer(peer_af_find(peer, afi, safi));
5513 if (peer_established(peer))
5514 bgp_announce_route(peer, afi, safi, false);
5515 } else {
5516 if (!peer_established(peer))
5517 return;
5518
5519 if (CHECK_FLAG(peer->af_flags[afi][safi],
5520 PEER_FLAG_SOFT_RECONFIG)) {
5521 bgp_soft_reconfig_in(peer, afi, safi);
5522 } else if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV) ||
5523 CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV)) {
5524 if (CHECK_FLAG(peer->af_cap[afi][safi],
5525 PEER_CAP_ORF_PREFIX_SM_ADV) &&
5526 (CHECK_FLAG(peer->af_cap[afi][safi],
5527 PEER_CAP_ORF_PREFIX_RM_RCV) ||
5528 CHECK_FLAG(peer->af_cap[afi][safi],
5529 PEER_CAP_ORF_PREFIX_RM_OLD_RCV)))
5530 peer_clear_soft(peer, afi, safi,
5531 BGP_CLEAR_SOFT_IN_ORF_PREFIX);
5532 else
5533 bgp_route_refresh_send(
5534 peer, afi, safi, 0, 0, 0,
5535 BGP_ROUTE_REFRESH_NORMAL);
5536 }
5537 }
5538 }
5539
5540
5541 /* neighbor weight. */
5542 int peer_weight_set(struct peer *peer, afi_t afi, safi_t safi, uint16_t weight)
5543 {
5544 struct peer *member;
5545 struct listnode *node, *nnode;
5546
5547 /* Set flag and configuration on peer. */
5548 peer_af_flag_set(peer, afi, safi, PEER_FLAG_WEIGHT);
5549 if (peer->weight[afi][safi] != weight) {
5550 peer->weight[afi][safi] = weight;
5551 peer_on_policy_change(peer, afi, safi, 0);
5552 }
5553
5554 /* Skip peer-group mechanics for regular peers. */
5555 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5556 return 0;
5557
5558 /*
5559 * Set flag and configuration on all peer-group members, unless they are
5560 * explicitly overriding peer-group configuration.
5561 */
5562 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5563 /* Skip peers with overridden configuration. */
5564 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5565 PEER_FLAG_WEIGHT))
5566 continue;
5567
5568 /* Set flag and configuration on peer-group member. */
5569 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5570 if (member->weight[afi][safi] != weight) {
5571 member->weight[afi][safi] = weight;
5572 peer_on_policy_change(member, afi, safi, 0);
5573 }
5574 }
5575
5576 return 0;
5577 }
5578
5579 int peer_weight_unset(struct peer *peer, afi_t afi, safi_t safi)
5580 {
5581 struct peer *member;
5582 struct listnode *node, *nnode;
5583
5584 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5585 return 0;
5586
5587 /* Inherit configuration from peer-group if peer is member. */
5588 if (peer_group_active(peer)) {
5589 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_WEIGHT);
5590 PEER_ATTR_INHERIT(peer, peer->group, weight[afi][safi]);
5591
5592 peer_on_policy_change(peer, afi, safi, 0);
5593 return 0;
5594 }
5595
5596 /* Remove flag and configuration from peer. */
5597 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_WEIGHT);
5598 peer->weight[afi][safi] = 0;
5599 peer_on_policy_change(peer, afi, safi, 0);
5600
5601 /* Skip peer-group mechanics for regular peers. */
5602 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5603 return 0;
5604
5605 /*
5606 * Remove flag and configuration from all peer-group members, unless
5607 * they are explicitly overriding peer-group configuration.
5608 */
5609 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5610 /* Skip peers with overridden configuration. */
5611 if (CHECK_FLAG(member->af_flags_override[afi][safi],
5612 PEER_FLAG_WEIGHT))
5613 continue;
5614
5615 /* Skip peers where flag is already disabled. */
5616 if (!CHECK_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT))
5617 continue;
5618
5619 /* Remove flag and configuration on peer-group member. */
5620 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_WEIGHT);
5621 member->weight[afi][safi] = 0;
5622 peer_on_policy_change(member, afi, safi, 0);
5623 }
5624
5625 return 0;
5626 }
5627
5628 int peer_timers_set(struct peer *peer, uint32_t keepalive, uint32_t holdtime)
5629 {
5630 struct peer *member;
5631 struct listnode *node, *nnode;
5632
5633 if (keepalive > UINT16_MAX)
5634 return BGP_ERR_INVALID_VALUE;
5635
5636 if (holdtime > UINT16_MAX)
5637 return BGP_ERR_INVALID_VALUE;
5638
5639 if (holdtime < 3 && holdtime != 0)
5640 return BGP_ERR_INVALID_VALUE;
5641
5642 /* Set flag and configuration on peer. */
5643 peer_flag_set(peer, PEER_FLAG_TIMER);
5644 peer->holdtime = holdtime;
5645 peer->keepalive = (keepalive < holdtime / 3 ? keepalive : holdtime / 3);
5646
5647 /* Skip peer-group mechanics for regular peers. */
5648 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5649 return 0;
5650
5651 /*
5652 * Set flag and configuration on all peer-group members, unless they are
5653 * explicitly overriding peer-group configuration.
5654 */
5655 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5656 /* Skip peers with overridden configuration. */
5657 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5658 continue;
5659
5660 /* Set flag and configuration on peer-group member. */
5661 SET_FLAG(member->flags, PEER_FLAG_TIMER);
5662 PEER_ATTR_INHERIT(member, peer->group, holdtime);
5663 PEER_ATTR_INHERIT(member, peer->group, keepalive);
5664 }
5665
5666 return 0;
5667 }
5668
5669 int peer_timers_unset(struct peer *peer)
5670 {
5671 struct peer *member;
5672 struct listnode *node, *nnode;
5673
5674 /* Inherit configuration from peer-group if peer is member. */
5675 if (peer_group_active(peer)) {
5676 peer_flag_inherit(peer, PEER_FLAG_TIMER);
5677 PEER_ATTR_INHERIT(peer, peer->group, holdtime);
5678 PEER_ATTR_INHERIT(peer, peer->group, keepalive);
5679 } else {
5680 /* Otherwise remove flag and configuration from peer. */
5681 peer_flag_unset(peer, PEER_FLAG_TIMER);
5682 peer->holdtime = 0;
5683 peer->keepalive = 0;
5684 }
5685
5686 /* Skip peer-group mechanics for regular peers. */
5687 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5688 return 0;
5689
5690 /*
5691 * Remove flag and configuration from all peer-group members, unless
5692 * they are explicitly overriding peer-group configuration.
5693 */
5694 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5695 /* Skip peers with overridden configuration. */
5696 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER))
5697 continue;
5698
5699 /* Remove flag and configuration on peer-group member. */
5700 UNSET_FLAG(member->flags, PEER_FLAG_TIMER);
5701 member->holdtime = 0;
5702 member->keepalive = 0;
5703 }
5704
5705 return 0;
5706 }
5707
5708 int peer_timers_connect_set(struct peer *peer, uint32_t connect)
5709 {
5710 struct peer *member;
5711 struct listnode *node, *nnode;
5712
5713 if (connect > UINT16_MAX)
5714 return BGP_ERR_INVALID_VALUE;
5715
5716 /* Set flag and configuration on peer. */
5717 peer_flag_set(peer, PEER_FLAG_TIMER_CONNECT);
5718 peer->connect = connect;
5719 peer->v_connect = connect;
5720
5721 /* Skip peer-group mechanics for regular peers. */
5722 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5723 if (!peer_established(peer)) {
5724 if (peer_active(peer))
5725 BGP_EVENT_ADD(peer, BGP_Stop);
5726 BGP_EVENT_ADD(peer, BGP_Start);
5727 }
5728 return 0;
5729 }
5730 /*
5731 * Set flag and configuration on all peer-group members, unless they are
5732 * explicitly overriding peer-group configuration.
5733 */
5734 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5735 /* Skip peers with overridden configuration. */
5736 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5737 continue;
5738
5739 /* Set flag and configuration on peer-group member. */
5740 SET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5741 member->connect = connect;
5742 member->v_connect = connect;
5743
5744 if (!peer_established(member)) {
5745 if (peer_active(member))
5746 BGP_EVENT_ADD(member, BGP_Stop);
5747 BGP_EVENT_ADD(member, BGP_Start);
5748 }
5749 }
5750
5751 return 0;
5752 }
5753
5754 int peer_timers_connect_unset(struct peer *peer)
5755 {
5756 struct peer *member;
5757 struct listnode *node, *nnode;
5758
5759 /* Inherit configuration from peer-group if peer is member. */
5760 if (peer_group_active(peer)) {
5761 peer_flag_inherit(peer, PEER_FLAG_TIMER_CONNECT);
5762 PEER_ATTR_INHERIT(peer, peer->group, connect);
5763 } else {
5764 /* Otherwise remove flag and configuration from peer. */
5765 peer_flag_unset(peer, PEER_FLAG_TIMER_CONNECT);
5766 peer->connect = 0;
5767 }
5768
5769 /* Set timer with fallback to default value. */
5770 if (peer->connect)
5771 peer->v_connect = peer->connect;
5772 else
5773 peer->v_connect = peer->bgp->default_connect_retry;
5774
5775 /* Skip peer-group mechanics for regular peers. */
5776 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5777 if (!peer_established(peer)) {
5778 if (peer_active(peer))
5779 BGP_EVENT_ADD(peer, BGP_Stop);
5780 BGP_EVENT_ADD(peer, BGP_Start);
5781 }
5782 return 0;
5783 }
5784 /*
5785 * Remove flag and configuration from all peer-group members, unless
5786 * they are explicitly overriding peer-group configuration.
5787 */
5788 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5789 /* Skip peers with overridden configuration. */
5790 if (CHECK_FLAG(member->flags_override, PEER_FLAG_TIMER_CONNECT))
5791 continue;
5792
5793 /* Remove flag and configuration on peer-group member. */
5794 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_CONNECT);
5795 member->connect = 0;
5796 member->v_connect = peer->bgp->default_connect_retry;
5797
5798 if (!peer_established(member)) {
5799 if (peer_active(member))
5800 BGP_EVENT_ADD(member, BGP_Stop);
5801 BGP_EVENT_ADD(member, BGP_Start);
5802 }
5803 }
5804
5805 return 0;
5806 }
5807
5808 int peer_advertise_interval_set(struct peer *peer, uint32_t routeadv)
5809 {
5810 struct peer *member;
5811 struct listnode *node, *nnode;
5812
5813 if (routeadv > 600)
5814 return BGP_ERR_INVALID_VALUE;
5815
5816 /* Set flag and configuration on peer. */
5817 peer_flag_set(peer, PEER_FLAG_ROUTEADV);
5818 peer->routeadv = routeadv;
5819 peer->v_routeadv = routeadv;
5820
5821 /* Check if handling a regular peer. */
5822 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5823 /* Update peer route announcements. */
5824 update_group_adjust_peer_afs(peer);
5825 if (peer_established(peer))
5826 bgp_announce_route_all(peer);
5827
5828 /* Skip peer-group mechanics for regular peers. */
5829 return 0;
5830 }
5831
5832 /*
5833 * Set flag and configuration on all peer-group members, unless they are
5834 * explicitly overriding peer-group configuration.
5835 */
5836 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5837 /* Skip peers with overridden configuration. */
5838 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5839 continue;
5840
5841 /* Set flag and configuration on peer-group member. */
5842 SET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5843 member->routeadv = routeadv;
5844 member->v_routeadv = routeadv;
5845
5846 /* Update peer route announcements. */
5847 update_group_adjust_peer_afs(member);
5848 if (peer_established(member))
5849 bgp_announce_route_all(member);
5850 }
5851
5852 return 0;
5853 }
5854
5855 int peer_advertise_interval_unset(struct peer *peer)
5856 {
5857 struct peer *member;
5858 struct listnode *node, *nnode;
5859
5860 /* Inherit configuration from peer-group if peer is member. */
5861 if (peer_group_active(peer)) {
5862 peer_flag_inherit(peer, PEER_FLAG_ROUTEADV);
5863 PEER_ATTR_INHERIT(peer, peer->group, routeadv);
5864 } else {
5865 /* Otherwise remove flag and configuration from peer. */
5866 peer_flag_unset(peer, PEER_FLAG_ROUTEADV);
5867 peer->routeadv = 0;
5868 }
5869
5870 /* Set timer with fallback to default value. */
5871 if (peer->routeadv)
5872 peer->v_routeadv = peer->routeadv;
5873 else
5874 peer->v_routeadv = (peer->sort == BGP_PEER_IBGP)
5875 ? BGP_DEFAULT_IBGP_ROUTEADV
5876 : BGP_DEFAULT_EBGP_ROUTEADV;
5877
5878 /* Check if handling a regular peer. */
5879 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
5880 /* Update peer route announcements. */
5881 update_group_adjust_peer_afs(peer);
5882 if (peer_established(peer))
5883 bgp_announce_route_all(peer);
5884
5885 /* Skip peer-group mechanics for regular peers. */
5886 return 0;
5887 }
5888
5889 /*
5890 * Remove flag and configuration from all peer-group members, unless
5891 * they are explicitly overriding peer-group configuration.
5892 */
5893 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
5894 /* Skip peers with overridden configuration. */
5895 if (CHECK_FLAG(member->flags_override, PEER_FLAG_ROUTEADV))
5896 continue;
5897
5898 /* Remove flag and configuration on peer-group member. */
5899 UNSET_FLAG(member->flags, PEER_FLAG_ROUTEADV);
5900 member->routeadv = 0;
5901 member->v_routeadv = (member->sort == BGP_PEER_IBGP)
5902 ? BGP_DEFAULT_IBGP_ROUTEADV
5903 : BGP_DEFAULT_EBGP_ROUTEADV;
5904
5905 /* Update peer route announcements. */
5906 update_group_adjust_peer_afs(member);
5907 if (peer_established(member))
5908 bgp_announce_route_all(member);
5909 }
5910
5911 return 0;
5912 }
5913
5914 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5915 * interval
5916 */
5917 int peer_timers_delayopen_set(struct peer *peer, uint32_t delayopen)
5918 {
5919 struct peer *member;
5920 struct listnode *node;
5921
5922 /* Set peers session attribute flag and timer interval. */
5923 peer_flag_set(peer, PEER_FLAG_TIMER_DELAYOPEN);
5924 peer->delayopen = delayopen;
5925 peer->v_delayopen = delayopen;
5926
5927 /* Skip group mechanics for regular peers. */
5928 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5929 return 0;
5930
5931 /* Set flag and configuration on all peer-group members, unless they are
5932 * explicitly overriding peer-group configuration.
5933 */
5934 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
5935 /* Skip peers with overridden configuration. */
5936 if (CHECK_FLAG(member->flags_override,
5937 PEER_FLAG_TIMER_DELAYOPEN))
5938 continue;
5939
5940 /* Set session attribute flag and timer intervals on peer-group
5941 * member.
5942 */
5943 SET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
5944 member->delayopen = delayopen;
5945 member->v_delayopen = delayopen;
5946 }
5947
5948 return 0;
5949 }
5950
5951 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
5952 * DelayOpenTimer interval to the default value.
5953 */
5954 int peer_timers_delayopen_unset(struct peer *peer)
5955 {
5956 struct peer *member;
5957 struct listnode *node;
5958
5959 /* Inherit configuration from peer-group if peer is member. */
5960 if (peer_group_active(peer)) {
5961 peer_flag_inherit(peer, PEER_FLAG_TIMER_DELAYOPEN);
5962 PEER_ATTR_INHERIT(peer, peer->group, delayopen);
5963 } else {
5964 /* Otherwise remove session attribute flag and set timer
5965 * interval to default value.
5966 */
5967 peer_flag_unset(peer, PEER_FLAG_TIMER_DELAYOPEN);
5968 peer->delayopen = peer->bgp->default_delayopen;
5969 }
5970
5971 /* Set timer value to zero */
5972 peer->v_delayopen = 0;
5973
5974 /* Skip peer-group mechanics for regular peers. */
5975 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
5976 return 0;
5977
5978 /* Remove flag and configuration from all peer-group members, unless
5979 * they are explicitly overriding peer-group configuration.
5980 */
5981 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
5982 /* Skip peers with overridden configuration. */
5983 if (CHECK_FLAG(member->flags_override,
5984 PEER_FLAG_TIMER_DELAYOPEN))
5985 continue;
5986
5987 /* Remove session attribute flag, reset the timer interval to
5988 * the default value and set the timer value to zero.
5989 */
5990 UNSET_FLAG(member->flags, PEER_FLAG_TIMER_DELAYOPEN);
5991 member->delayopen = peer->bgp->default_delayopen;
5992 member->v_delayopen = 0;
5993 }
5994
5995 return 0;
5996 }
5997
5998 /* neighbor interface */
5999 void peer_interface_set(struct peer *peer, const char *str)
6000 {
6001 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
6002 peer->ifname = XSTRDUP(MTYPE_BGP_PEER_IFNAME, str);
6003 }
6004
6005 void peer_interface_unset(struct peer *peer)
6006 {
6007 XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname);
6008 }
6009
6010 /* Allow-as in. */
6011 int peer_allowas_in_set(struct peer *peer, afi_t afi, safi_t safi,
6012 int allow_num, int origin)
6013 {
6014 struct peer *member;
6015 struct listnode *node, *nnode;
6016
6017 if (!origin && (allow_num < 1 || allow_num > 10))
6018 return BGP_ERR_INVALID_VALUE;
6019
6020 /* Set flag and configuration on peer. */
6021 peer_af_flag_set(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6022 if (origin) {
6023 if (peer->allowas_in[afi][safi] != 0
6024 || !CHECK_FLAG(peer->af_flags[afi][safi],
6025 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6026 peer_af_flag_set(peer, afi, safi,
6027 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6028 peer->allowas_in[afi][safi] = 0;
6029 peer_on_policy_change(peer, afi, safi, 0);
6030 }
6031 } else {
6032 if (peer->allowas_in[afi][safi] != allow_num
6033 || CHECK_FLAG(peer->af_flags[afi][safi],
6034 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6035
6036 peer_af_flag_unset(peer, afi, safi,
6037 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6038 peer->allowas_in[afi][safi] = allow_num;
6039 peer_on_policy_change(peer, afi, safi, 0);
6040 }
6041 }
6042
6043 /* Skip peer-group mechanics for regular peers. */
6044 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6045 return 0;
6046
6047 /*
6048 * Set flag and configuration on all peer-group members, unless
6049 * they are explicitly overriding peer-group configuration.
6050 */
6051 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6052 /* Skip peers with overridden configuration. */
6053 if (CHECK_FLAG(member->af_flags_override[afi][safi],
6054 PEER_FLAG_ALLOWAS_IN))
6055 continue;
6056
6057 /* Set flag and configuration on peer-group member. */
6058 SET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
6059 if (origin) {
6060 if (member->allowas_in[afi][safi] != 0
6061 || !CHECK_FLAG(member->af_flags[afi][safi],
6062 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6063 SET_FLAG(member->af_flags[afi][safi],
6064 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6065 member->allowas_in[afi][safi] = 0;
6066 peer_on_policy_change(peer, afi, safi, 0);
6067 }
6068 } else {
6069 if (member->allowas_in[afi][safi] != allow_num
6070 || CHECK_FLAG(member->af_flags[afi][safi],
6071 PEER_FLAG_ALLOWAS_IN_ORIGIN)) {
6072 UNSET_FLAG(member->af_flags[afi][safi],
6073 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6074 member->allowas_in[afi][safi] = allow_num;
6075 peer_on_policy_change(peer, afi, safi, 0);
6076 }
6077 }
6078 }
6079
6080 return 0;
6081 }
6082
6083 int peer_allowas_in_unset(struct peer *peer, afi_t afi, safi_t safi)
6084 {
6085 struct peer *member;
6086 struct listnode *node, *nnode;
6087
6088 /* Skip peer if flag is already disabled. */
6089 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
6090 return 0;
6091
6092 /* Inherit configuration from peer-group if peer is member. */
6093 if (peer_group_active(peer)) {
6094 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6095 peer_af_flag_inherit(peer, afi, safi,
6096 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6097 PEER_ATTR_INHERIT(peer, peer->group, allowas_in[afi][safi]);
6098 peer_on_policy_change(peer, afi, safi, 0);
6099
6100 return 0;
6101 }
6102
6103 /* Remove flag and configuration from peer. */
6104 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN);
6105 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_ALLOWAS_IN_ORIGIN);
6106 peer->allowas_in[afi][safi] = 0;
6107 peer_on_policy_change(peer, afi, safi, 0);
6108
6109 /* Skip peer-group mechanics if handling a regular peer. */
6110 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6111 return 0;
6112
6113 /*
6114 * Remove flags and configuration from all peer-group members, unless
6115 * they are explicitly overriding peer-group configuration.
6116 */
6117 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6118 /* Skip peers with overridden configuration. */
6119 if (CHECK_FLAG(member->af_flags_override[afi][safi],
6120 PEER_FLAG_ALLOWAS_IN))
6121 continue;
6122
6123 /* Remove flags and configuration on peer-group member. */
6124 UNSET_FLAG(member->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN);
6125 UNSET_FLAG(member->af_flags[afi][safi],
6126 PEER_FLAG_ALLOWAS_IN_ORIGIN);
6127 member->allowas_in[afi][safi] = 0;
6128 peer_on_policy_change(member, afi, safi, 0);
6129 }
6130
6131 return 0;
6132 }
6133
6134 int peer_local_as_set(struct peer *peer, as_t as, bool no_prepend,
6135 bool replace_as)
6136 {
6137 bool old_no_prepend, old_replace_as;
6138 struct bgp *bgp = peer->bgp;
6139 struct peer *member;
6140 struct listnode *node, *nnode;
6141
6142 if (bgp->as == as)
6143 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS;
6144
6145 /* Save previous flag states. */
6146 old_no_prepend =
6147 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6148 old_replace_as =
6149 !!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6150
6151 /* Set flag and configuration on peer. */
6152 peer_flag_set(peer, PEER_FLAG_LOCAL_AS);
6153 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND, no_prepend);
6154 peer_flag_modify(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS, replace_as);
6155
6156 if (peer->change_local_as == as && old_no_prepend == no_prepend
6157 && old_replace_as == replace_as)
6158 return 0;
6159 peer->change_local_as = as;
6160 (void)peer_sort(peer);
6161
6162 /* Check if handling a regular peer. */
6163 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
6164 return 0;
6165
6166 /*
6167 * Set flag and configuration on all peer-group members, unless they are
6168 * explicitly overriding peer-group configuration.
6169 */
6170 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6171 /* Skip peers with overridden configuration. */
6172 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
6173 continue;
6174
6175 /* Skip peers with the same configuration. */
6176 old_no_prepend = CHECK_FLAG(member->flags,
6177 PEER_FLAG_LOCAL_AS_NO_PREPEND);
6178 old_replace_as = CHECK_FLAG(member->flags,
6179 PEER_FLAG_LOCAL_AS_REPLACE_AS);
6180 if (member->change_local_as == as
6181 && CHECK_FLAG(member->flags, PEER_FLAG_LOCAL_AS)
6182 && old_no_prepend == no_prepend
6183 && old_replace_as == replace_as)
6184 continue;
6185
6186 /* Set flag and configuration on peer-group member. */
6187 SET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
6188 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND,
6189 no_prepend);
6190 COND_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS,
6191 replace_as);
6192 member->change_local_as = as;
6193 }
6194
6195 return 0;
6196 }
6197
6198 int peer_local_as_unset(struct peer *peer)
6199 {
6200 struct peer *member;
6201 struct listnode *node, *nnode;
6202
6203 if (!CHECK_FLAG(peer->flags, PEER_FLAG_LOCAL_AS))
6204 return 0;
6205
6206 /* Inherit configuration from peer-group if peer is member. */
6207 if (peer_group_active(peer)) {
6208 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS);
6209 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6210 peer_flag_inherit(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6211 PEER_ATTR_INHERIT(peer, peer->group, change_local_as);
6212 } else {
6213 /* Otherwise remove flag and configuration from peer. */
6214 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS);
6215 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6216 peer_flag_unset(peer, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6217 peer->change_local_as = 0;
6218 }
6219
6220 /* Check if handling a regular peer. */
6221 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6222 /* Send notification or stop peer depending on state. */
6223 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status)) {
6224 peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6225 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6226 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6227 } else
6228 BGP_EVENT_ADD(peer, BGP_Stop);
6229
6230 /* Skip peer-group mechanics for regular peers. */
6231 return 0;
6232 }
6233
6234 /*
6235 * Remove flag and configuration from all peer-group members, unless
6236 * they are explicitly overriding peer-group configuration.
6237 */
6238 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6239 /* Skip peers with overridden configuration. */
6240 if (CHECK_FLAG(member->flags_override, PEER_FLAG_LOCAL_AS))
6241 continue;
6242
6243 /* Remove flag and configuration on peer-group member. */
6244 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS);
6245 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND);
6246 UNSET_FLAG(member->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS);
6247 member->change_local_as = 0;
6248
6249 /* Send notification or stop peer depending on state. */
6250 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status)) {
6251 member->last_reset = PEER_DOWN_LOCAL_AS_CHANGE;
6252 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6253 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6254 } else
6255 bgp_session_reset(member);
6256 }
6257
6258 return 0;
6259 }
6260
6261 /* Set password for authenticating with the peer. */
6262 int peer_password_set(struct peer *peer, const char *password)
6263 {
6264 struct peer *member;
6265 struct listnode *node, *nnode;
6266 int len = password ? strlen(password) : 0;
6267 int ret = BGP_SUCCESS;
6268
6269 if ((len < PEER_PASSWORD_MINLEN) || (len > PEER_PASSWORD_MAXLEN))
6270 return BGP_ERR_INVALID_VALUE;
6271
6272 /* Set flag and configuration on peer. */
6273 peer_flag_set(peer, PEER_FLAG_PASSWORD);
6274 if (peer->password && strcmp(peer->password, password) == 0)
6275 return 0;
6276 XFREE(MTYPE_PEER_PASSWORD, peer->password);
6277 peer->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6278
6279 /* Check if handling a regular peer. */
6280 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6281 /* Send notification or reset peer depending on state. */
6282 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6283 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6284 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6285 else
6286 bgp_session_reset(peer);
6287
6288 /*
6289 * Attempt to install password on socket and skip peer-group
6290 * mechanics.
6291 */
6292 if (BGP_PEER_SU_UNSPEC(peer))
6293 return BGP_SUCCESS;
6294 return (bgp_md5_set(peer) >= 0) ? BGP_SUCCESS
6295 : BGP_ERR_TCPSIG_FAILED;
6296 }
6297
6298 /*
6299 * Set flag and configuration on all peer-group members, unless they are
6300 * explicitly overriding peer-group configuration.
6301 */
6302 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6303 /* Skip peers with overridden configuration. */
6304 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
6305 continue;
6306
6307 /* Skip peers with the same password. */
6308 if (member->password && strcmp(member->password, password) == 0)
6309 continue;
6310
6311 /* Set flag and configuration on peer-group member. */
6312 SET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6313 if (member->password)
6314 XFREE(MTYPE_PEER_PASSWORD, member->password);
6315 member->password = XSTRDUP(MTYPE_PEER_PASSWORD, password);
6316
6317 /* Send notification or reset peer depending on state. */
6318 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6319 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6320 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6321 else
6322 bgp_session_reset(member);
6323
6324 /* Attempt to install password on socket. */
6325 if (!BGP_PEER_SU_UNSPEC(member) && bgp_md5_set(member) < 0)
6326 ret = BGP_ERR_TCPSIG_FAILED;
6327 }
6328
6329 /* Set flag and configuration on all peer-group listen ranges */
6330 struct listnode *ln;
6331 struct prefix *lr;
6332
6333 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
6334 bgp_md5_set_prefix(peer->bgp, lr, password);
6335 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
6336 bgp_md5_set_prefix(peer->bgp, lr, password);
6337
6338 return ret;
6339 }
6340
6341 int peer_password_unset(struct peer *peer)
6342 {
6343 struct peer *member;
6344 struct listnode *node, *nnode;
6345
6346 if (!CHECK_FLAG(peer->flags, PEER_FLAG_PASSWORD))
6347 return 0;
6348
6349 /* Inherit configuration from peer-group if peer is member. */
6350 if (peer_group_active(peer)) {
6351 peer_flag_inherit(peer, PEER_FLAG_PASSWORD);
6352 PEER_STR_ATTR_INHERIT(peer, peer->group, password,
6353 MTYPE_PEER_PASSWORD);
6354 } else {
6355 /* Otherwise remove flag and configuration from peer. */
6356 peer_flag_unset(peer, PEER_FLAG_PASSWORD);
6357 XFREE(MTYPE_PEER_PASSWORD, peer->password);
6358 }
6359
6360 /* Check if handling a regular peer. */
6361 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6362 /* Send notification or reset peer depending on state. */
6363 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
6364 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
6365 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6366 else
6367 bgp_session_reset(peer);
6368
6369 /* Attempt to uninstall password on socket. */
6370 if (!BGP_PEER_SU_UNSPEC(peer))
6371 bgp_md5_unset(peer);
6372 /* Skip peer-group mechanics for regular peers. */
6373 return 0;
6374 }
6375
6376 /*
6377 * Remove flag and configuration from all peer-group members, unless
6378 * they are explicitly overriding peer-group configuration.
6379 */
6380 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6381 /* Skip peers with overridden configuration. */
6382 if (CHECK_FLAG(member->flags_override, PEER_FLAG_PASSWORD))
6383 continue;
6384
6385 /* Remove flag and configuration on peer-group member. */
6386 UNSET_FLAG(member->flags, PEER_FLAG_PASSWORD);
6387 XFREE(MTYPE_PEER_PASSWORD, member->password);
6388
6389 /* Send notification or reset peer depending on state. */
6390 if (BGP_IS_VALID_STATE_FOR_NOTIF(member->status))
6391 bgp_notify_send(member, BGP_NOTIFY_CEASE,
6392 BGP_NOTIFY_CEASE_CONFIG_CHANGE);
6393 else
6394 bgp_session_reset(member);
6395
6396 /* Attempt to uninstall password on socket. */
6397 if (!BGP_PEER_SU_UNSPEC(member))
6398 bgp_md5_unset(member);
6399 }
6400
6401 /* Set flag and configuration on all peer-group listen ranges */
6402 struct listnode *ln;
6403 struct prefix *lr;
6404
6405 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP], ln, lr))
6406 bgp_md5_unset_prefix(peer->bgp, lr);
6407 for (ALL_LIST_ELEMENTS_RO(peer->group->listen_range[AFI_IP6], ln, lr))
6408 bgp_md5_unset_prefix(peer->bgp, lr);
6409
6410 return 0;
6411 }
6412
6413
6414 /* Set distribute list to the peer. */
6415 int peer_distribute_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6416 const char *name)
6417 {
6418 struct peer *member;
6419 struct bgp_filter *filter;
6420 struct listnode *node, *nnode;
6421
6422 if (direct != FILTER_IN && direct != FILTER_OUT)
6423 return BGP_ERR_INVALID_VALUE;
6424
6425 /* Set configuration on peer. */
6426 filter = &peer->filter[afi][safi];
6427 if (filter->plist[direct].name)
6428 return BGP_ERR_PEER_FILTER_CONFLICT;
6429 if (filter->dlist[direct].name)
6430 XFREE(MTYPE_BGP_FILTER_NAME, filter->dlist[direct].name);
6431 filter->dlist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6432 filter->dlist[direct].alist = access_list_lookup(afi, name);
6433
6434 /* Check if handling a regular peer. */
6435 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6436 /* Set override-flag and process peer route updates. */
6437 SET_FLAG(peer->filter_override[afi][safi][direct],
6438 PEER_FT_DISTRIBUTE_LIST);
6439 peer_on_policy_change(peer, afi, safi,
6440 (direct == FILTER_OUT) ? 1 : 0);
6441
6442 /* Skip peer-group mechanics for regular peers. */
6443 return 0;
6444 }
6445
6446 /*
6447 * Set configuration on all peer-group members, un less they are
6448 * explicitly overriding peer-group configuration.
6449 */
6450 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6451 /* Skip peers with overridden configuration. */
6452 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6453 PEER_FT_DISTRIBUTE_LIST))
6454 continue;
6455
6456 /* Set configuration on peer-group member. */
6457 filter = &member->filter[afi][safi];
6458 if (filter->dlist[direct].name)
6459 XFREE(MTYPE_BGP_FILTER_NAME,
6460 filter->dlist[direct].name);
6461 filter->dlist[direct].name =
6462 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6463 filter->dlist[direct].alist = access_list_lookup(afi, name);
6464
6465 /* Process peer route updates. */
6466 peer_on_policy_change(member, afi, safi,
6467 (direct == FILTER_OUT) ? 1 : 0);
6468 }
6469
6470 return 0;
6471 }
6472
6473 int peer_distribute_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6474 {
6475 struct peer *member;
6476 struct bgp_filter *filter;
6477 struct listnode *node, *nnode;
6478
6479 if (direct != FILTER_IN && direct != FILTER_OUT)
6480 return BGP_ERR_INVALID_VALUE;
6481
6482 /* Unset override-flag unconditionally. */
6483 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6484 PEER_FT_DISTRIBUTE_LIST);
6485
6486 /* Inherit configuration from peer-group if peer is member. */
6487 if (peer_group_active(peer)) {
6488 PEER_STR_ATTR_INHERIT(peer, peer->group,
6489 filter[afi][safi].dlist[direct].name,
6490 MTYPE_BGP_FILTER_NAME);
6491 PEER_ATTR_INHERIT(peer, peer->group,
6492 filter[afi][safi].dlist[direct].alist);
6493 } else {
6494 /* Otherwise remove configuration from peer. */
6495 filter = &peer->filter[afi][safi];
6496 if (filter->dlist[direct].name)
6497 XFREE(MTYPE_BGP_FILTER_NAME,
6498 filter->dlist[direct].name);
6499 filter->dlist[direct].name = NULL;
6500 filter->dlist[direct].alist = NULL;
6501 }
6502
6503 /* Check if handling a regular peer. */
6504 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6505 /* Process peer route updates. */
6506 peer_on_policy_change(peer, afi, safi,
6507 (direct == FILTER_OUT) ? 1 : 0);
6508
6509 /* Skip peer-group mechanics for regular peers. */
6510 return 0;
6511 }
6512
6513 /*
6514 * Remove configuration on all peer-group members, unless they are
6515 * explicitly overriding peer-group configuration.
6516 */
6517 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6518 /* Skip peers with overridden configuration. */
6519 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6520 PEER_FT_DISTRIBUTE_LIST))
6521 continue;
6522
6523 /* Remove configuration on peer-group member. */
6524 filter = &member->filter[afi][safi];
6525 if (filter->dlist[direct].name)
6526 XFREE(MTYPE_BGP_FILTER_NAME,
6527 filter->dlist[direct].name);
6528 filter->dlist[direct].name = NULL;
6529 filter->dlist[direct].alist = NULL;
6530
6531 /* Process peer route updates. */
6532 peer_on_policy_change(member, afi, safi,
6533 (direct == FILTER_OUT) ? 1 : 0);
6534 }
6535
6536 return 0;
6537 }
6538
6539 /* Update distribute list. */
6540 static void peer_distribute_update(struct access_list *access)
6541 {
6542 afi_t afi;
6543 safi_t safi;
6544 int direct;
6545 struct listnode *mnode, *mnnode;
6546 struct listnode *node, *nnode;
6547 struct bgp *bgp;
6548 struct peer *peer;
6549 struct peer_group *group;
6550 struct bgp_filter *filter;
6551
6552 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6553 if (access->name)
6554 update_group_policy_update(bgp,
6555 BGP_POLICY_DISTRIBUTE_LIST,
6556 access->name, true, 0);
6557 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
6558 FOREACH_AFI_SAFI (afi, safi) {
6559 filter = &peer->filter[afi][safi];
6560
6561 for (direct = FILTER_IN; direct < FILTER_MAX;
6562 direct++) {
6563 if (filter->dlist[direct].name)
6564 filter->dlist[direct]
6565 .alist = access_list_lookup(
6566 afi,
6567 filter->dlist[direct]
6568 .name);
6569 else
6570 filter->dlist[direct].alist =
6571 NULL;
6572 }
6573 }
6574 }
6575 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
6576 FOREACH_AFI_SAFI (afi, safi) {
6577 filter = &group->conf->filter[afi][safi];
6578
6579 for (direct = FILTER_IN; direct < FILTER_MAX;
6580 direct++) {
6581 if (filter->dlist[direct].name)
6582 filter->dlist[direct]
6583 .alist = access_list_lookup(
6584 afi,
6585 filter->dlist[direct]
6586 .name);
6587 else
6588 filter->dlist[direct].alist =
6589 NULL;
6590 }
6591 }
6592 }
6593 #ifdef ENABLE_BGP_VNC
6594 vnc_prefix_list_update(bgp);
6595 #endif
6596 }
6597 }
6598
6599 /* Set prefix list to the peer. */
6600 int peer_prefix_list_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6601 const char *name)
6602 {
6603 struct peer *member;
6604 struct bgp_filter *filter;
6605 struct listnode *node, *nnode;
6606
6607 if (direct != FILTER_IN && direct != FILTER_OUT)
6608 return BGP_ERR_INVALID_VALUE;
6609
6610 /* Set configuration on peer. */
6611 filter = &peer->filter[afi][safi];
6612 if (filter->dlist[direct].name)
6613 return BGP_ERR_PEER_FILTER_CONFLICT;
6614 if (filter->plist[direct].name)
6615 XFREE(MTYPE_BGP_FILTER_NAME, filter->plist[direct].name);
6616 filter->plist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6617 filter->plist[direct].plist = prefix_list_lookup(afi, name);
6618
6619 /* Check if handling a regular peer. */
6620 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6621 /* Set override-flag and process peer route updates. */
6622 SET_FLAG(peer->filter_override[afi][safi][direct],
6623 PEER_FT_PREFIX_LIST);
6624 peer_on_policy_change(peer, afi, safi,
6625 (direct == FILTER_OUT) ? 1 : 0);
6626
6627 /* Skip peer-group mechanics for regular peers. */
6628 return 0;
6629 }
6630
6631 /*
6632 * Set configuration on all peer-group members, unless they are
6633 * explicitly overriding peer-group configuration.
6634 */
6635 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6636 /* Skip peers with overridden configuration. */
6637 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6638 PEER_FT_PREFIX_LIST))
6639 continue;
6640
6641 /* Set configuration on peer-group member. */
6642 filter = &member->filter[afi][safi];
6643 if (filter->plist[direct].name)
6644 XFREE(MTYPE_BGP_FILTER_NAME,
6645 filter->plist[direct].name);
6646 filter->plist[direct].name =
6647 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6648 filter->plist[direct].plist = prefix_list_lookup(afi, name);
6649
6650 /* Process peer route updates. */
6651 peer_on_policy_change(member, afi, safi,
6652 (direct == FILTER_OUT) ? 1 : 0);
6653 }
6654
6655 return 0;
6656 }
6657
6658 int peer_prefix_list_unset(struct peer *peer, afi_t afi, safi_t safi,
6659 int direct)
6660 {
6661 struct peer *member;
6662 struct bgp_filter *filter;
6663 struct listnode *node, *nnode;
6664
6665 if (direct != FILTER_IN && direct != FILTER_OUT)
6666 return BGP_ERR_INVALID_VALUE;
6667
6668 /* Unset override-flag unconditionally. */
6669 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6670 PEER_FT_PREFIX_LIST);
6671
6672 /* Inherit configuration from peer-group if peer is member. */
6673 if (peer_group_active(peer)) {
6674 PEER_STR_ATTR_INHERIT(peer, peer->group,
6675 filter[afi][safi].plist[direct].name,
6676 MTYPE_BGP_FILTER_NAME);
6677 PEER_ATTR_INHERIT(peer, peer->group,
6678 filter[afi][safi].plist[direct].plist);
6679 } else {
6680 /* Otherwise remove configuration from peer. */
6681 filter = &peer->filter[afi][safi];
6682 if (filter->plist[direct].name)
6683 XFREE(MTYPE_BGP_FILTER_NAME,
6684 filter->plist[direct].name);
6685 filter->plist[direct].name = NULL;
6686 filter->plist[direct].plist = NULL;
6687 }
6688
6689 /* Check if handling a regular peer. */
6690 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6691 /* Process peer route updates. */
6692 peer_on_policy_change(peer, afi, safi,
6693 (direct == FILTER_OUT) ? 1 : 0);
6694
6695 /* Skip peer-group mechanics for regular peers. */
6696 return 0;
6697 }
6698
6699 /*
6700 * Remove configuration on all peer-group members, unless they are
6701 * explicitly overriding peer-group configuration.
6702 */
6703 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6704 /* Skip peers with overridden configuration. */
6705 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6706 PEER_FT_PREFIX_LIST))
6707 continue;
6708
6709 /* Remove configuration on peer-group member. */
6710 filter = &member->filter[afi][safi];
6711 if (filter->plist[direct].name)
6712 XFREE(MTYPE_BGP_FILTER_NAME,
6713 filter->plist[direct].name);
6714 filter->plist[direct].name = NULL;
6715 filter->plist[direct].plist = NULL;
6716
6717 /* Process peer route updates. */
6718 peer_on_policy_change(member, afi, safi,
6719 (direct == FILTER_OUT) ? 1 : 0);
6720 }
6721
6722 return 0;
6723 }
6724
6725 /* Update prefix-list list. */
6726 static void peer_prefix_list_update(struct prefix_list *plist)
6727 {
6728 struct listnode *mnode, *mnnode;
6729 struct listnode *node, *nnode;
6730 struct bgp *bgp;
6731 struct peer *peer;
6732 struct peer_group *group;
6733 struct bgp_filter *filter;
6734 afi_t afi;
6735 safi_t safi;
6736 int direct;
6737
6738 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6739
6740 /*
6741 * Update the prefix-list on update groups.
6742 */
6743 update_group_policy_update(
6744 bgp, BGP_POLICY_PREFIX_LIST,
6745 plist ? prefix_list_name(plist) : NULL, true, 0);
6746
6747 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
6748 FOREACH_AFI_SAFI (afi, safi) {
6749 filter = &peer->filter[afi][safi];
6750
6751 for (direct = FILTER_IN; direct < FILTER_MAX;
6752 direct++) {
6753 if (filter->plist[direct].name)
6754 filter->plist[direct]
6755 .plist = prefix_list_lookup(
6756 afi,
6757 filter->plist[direct]
6758 .name);
6759 else
6760 filter->plist[direct].plist =
6761 NULL;
6762 }
6763
6764 /* If we touch prefix-list, we need to process
6765 * new updates. This is important for ORF to
6766 * work correctly as well.
6767 */
6768 if (peer->afc_nego[afi][safi])
6769 peer_on_policy_change(peer, afi, safi,
6770 0);
6771 }
6772 }
6773 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
6774 FOREACH_AFI_SAFI (afi, safi) {
6775 filter = &group->conf->filter[afi][safi];
6776
6777 for (direct = FILTER_IN; direct < FILTER_MAX;
6778 direct++) {
6779 if (filter->plist[direct].name)
6780 filter->plist[direct]
6781 .plist = prefix_list_lookup(
6782 afi,
6783 filter->plist[direct]
6784 .name);
6785 else
6786 filter->plist[direct].plist =
6787 NULL;
6788 }
6789 }
6790 }
6791 }
6792 }
6793
6794 int peer_aslist_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6795 const char *name)
6796 {
6797 struct peer *member;
6798 struct bgp_filter *filter;
6799 struct listnode *node, *nnode;
6800
6801 if (direct != FILTER_IN && direct != FILTER_OUT)
6802 return BGP_ERR_INVALID_VALUE;
6803
6804 /* Set configuration on peer. */
6805 filter = &peer->filter[afi][safi];
6806 if (filter->aslist[direct].name)
6807 XFREE(MTYPE_BGP_FILTER_NAME, filter->aslist[direct].name);
6808 filter->aslist[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6809 filter->aslist[direct].aslist = as_list_lookup(name);
6810
6811 /* Check if handling a regular peer. */
6812 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6813 /* Set override-flag and process peer route updates. */
6814 SET_FLAG(peer->filter_override[afi][safi][direct],
6815 PEER_FT_FILTER_LIST);
6816 peer_on_policy_change(peer, afi, safi,
6817 (direct == FILTER_OUT) ? 1 : 0);
6818
6819 /* Skip peer-group mechanics for regular peers. */
6820 return 0;
6821 }
6822
6823 /*
6824 * Set configuration on all peer-group members, unless they are
6825 * explicitly overriding peer-group configuration.
6826 */
6827 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6828 /* Skip peers with overridden configuration. */
6829 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6830 PEER_FT_FILTER_LIST))
6831 continue;
6832
6833 /* Set configuration on peer-group member. */
6834 filter = &member->filter[afi][safi];
6835 if (filter->aslist[direct].name)
6836 XFREE(MTYPE_BGP_FILTER_NAME,
6837 filter->aslist[direct].name);
6838 filter->aslist[direct].name =
6839 XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
6840 filter->aslist[direct].aslist = as_list_lookup(name);
6841
6842 /* Process peer route updates. */
6843 peer_on_policy_change(member, afi, safi,
6844 (direct == FILTER_OUT) ? 1 : 0);
6845 }
6846
6847 return 0;
6848 }
6849
6850 int peer_aslist_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
6851 {
6852 struct peer *member;
6853 struct bgp_filter *filter;
6854 struct listnode *node, *nnode;
6855
6856 if (direct != FILTER_IN && direct != FILTER_OUT)
6857 return BGP_ERR_INVALID_VALUE;
6858
6859 /* Unset override-flag unconditionally. */
6860 UNSET_FLAG(peer->filter_override[afi][safi][direct],
6861 PEER_FT_FILTER_LIST);
6862
6863 /* Inherit configuration from peer-group if peer is member. */
6864 if (peer_group_active(peer)) {
6865 PEER_STR_ATTR_INHERIT(peer, peer->group,
6866 filter[afi][safi].aslist[direct].name,
6867 MTYPE_BGP_FILTER_NAME);
6868 PEER_ATTR_INHERIT(peer, peer->group,
6869 filter[afi][safi].aslist[direct].aslist);
6870 } else {
6871 /* Otherwise remove configuration from peer. */
6872 filter = &peer->filter[afi][safi];
6873 if (filter->aslist[direct].name)
6874 XFREE(MTYPE_BGP_FILTER_NAME,
6875 filter->aslist[direct].name);
6876 filter->aslist[direct].name = NULL;
6877 filter->aslist[direct].aslist = NULL;
6878 }
6879
6880 /* Check if handling a regular peer. */
6881 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
6882 /* Process peer route updates. */
6883 peer_on_policy_change(peer, afi, safi,
6884 (direct == FILTER_OUT) ? 1 : 0);
6885
6886 /* Skip peer-group mechanics for regular peers. */
6887 return 0;
6888 }
6889
6890 /*
6891 * Remove configuration on all peer-group members, unless they are
6892 * explicitly overriding peer-group configuration.
6893 */
6894 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
6895 /* Skip peers with overridden configuration. */
6896 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
6897 PEER_FT_FILTER_LIST))
6898 continue;
6899
6900 /* Remove configuration on peer-group member. */
6901 filter = &member->filter[afi][safi];
6902 if (filter->aslist[direct].name)
6903 XFREE(MTYPE_BGP_FILTER_NAME,
6904 filter->aslist[direct].name);
6905 filter->aslist[direct].name = NULL;
6906 filter->aslist[direct].aslist = NULL;
6907
6908 /* Process peer route updates. */
6909 peer_on_policy_change(member, afi, safi,
6910 (direct == FILTER_OUT) ? 1 : 0);
6911 }
6912
6913 return 0;
6914 }
6915
6916 static void peer_aslist_update(const char *aslist_name)
6917 {
6918 afi_t afi;
6919 safi_t safi;
6920 int direct;
6921 struct listnode *mnode, *mnnode;
6922 struct listnode *node, *nnode;
6923 struct bgp *bgp;
6924 struct peer *peer;
6925 struct peer_group *group;
6926 struct bgp_filter *filter;
6927
6928 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
6929 update_group_policy_update(bgp, BGP_POLICY_FILTER_LIST,
6930 aslist_name, true, 0);
6931
6932 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
6933 FOREACH_AFI_SAFI (afi, safi) {
6934 filter = &peer->filter[afi][safi];
6935
6936 for (direct = FILTER_IN; direct < FILTER_MAX;
6937 direct++) {
6938 if (filter->aslist[direct].name)
6939 filter->aslist[direct]
6940 .aslist = as_list_lookup(
6941 filter->aslist[direct]
6942 .name);
6943 else
6944 filter->aslist[direct].aslist =
6945 NULL;
6946 }
6947 }
6948 }
6949 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
6950 FOREACH_AFI_SAFI (afi, safi) {
6951 filter = &group->conf->filter[afi][safi];
6952
6953 for (direct = FILTER_IN; direct < FILTER_MAX;
6954 direct++) {
6955 if (filter->aslist[direct].name)
6956 filter->aslist[direct]
6957 .aslist = as_list_lookup(
6958 filter->aslist[direct]
6959 .name);
6960 else
6961 filter->aslist[direct].aslist =
6962 NULL;
6963 }
6964 }
6965 }
6966 }
6967 }
6968
6969 static void peer_aslist_add(char *aslist_name)
6970 {
6971 peer_aslist_update(aslist_name);
6972 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_ADDED);
6973 }
6974
6975 static void peer_aslist_del(const char *aslist_name)
6976 {
6977 peer_aslist_update(aslist_name);
6978 route_map_notify_dependencies(aslist_name, RMAP_EVENT_ASLIST_DELETED);
6979 }
6980
6981
6982 int peer_route_map_set(struct peer *peer, afi_t afi, safi_t safi, int direct,
6983 const char *name, struct route_map *route_map)
6984 {
6985 struct peer *member;
6986 struct bgp_filter *filter;
6987 struct listnode *node, *nnode;
6988
6989 if (direct != RMAP_IN && direct != RMAP_OUT)
6990 return BGP_ERR_INVALID_VALUE;
6991
6992 /* Set configuration on peer. */
6993 filter = &peer->filter[afi][safi];
6994 if (filter->map[direct].name) {
6995 /* If the neighbor is configured with the same route-map
6996 * again then, ignore the duplicate configuration.
6997 */
6998 if (strcmp(filter->map[direct].name, name) == 0)
6999 return 0;
7000
7001 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7002 }
7003 route_map_counter_decrement(filter->map[direct].map);
7004 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7005 filter->map[direct].map = route_map;
7006 route_map_counter_increment(route_map);
7007
7008 /* Check if handling a regular peer. */
7009 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7010 /* Set override-flag and process peer route updates. */
7011 SET_FLAG(peer->filter_override[afi][safi][direct],
7012 PEER_FT_ROUTE_MAP);
7013 peer_on_policy_change(peer, afi, safi,
7014 (direct == RMAP_OUT) ? 1 : 0);
7015
7016 /* Skip peer-group mechanics for regular peers. */
7017 return 0;
7018 }
7019
7020 /*
7021 * Set configuration on all peer-group members, unless they are
7022 * explicitly overriding peer-group configuration.
7023 */
7024 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7025 /* Skip peers with overridden configuration. */
7026 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
7027 PEER_FT_ROUTE_MAP))
7028 continue;
7029
7030 /* Set configuration on peer-group member. */
7031 filter = &member->filter[afi][safi];
7032 if (filter->map[direct].name)
7033 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7034 route_map_counter_decrement(filter->map[direct].map);
7035 filter->map[direct].name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7036 filter->map[direct].map = route_map;
7037 route_map_counter_increment(route_map);
7038
7039 /* Process peer route updates. */
7040 peer_on_policy_change(member, afi, safi,
7041 (direct == RMAP_OUT) ? 1 : 0);
7042 }
7043 return 0;
7044 }
7045
7046 /* Unset route-map from the peer. */
7047 int peer_route_map_unset(struct peer *peer, afi_t afi, safi_t safi, int direct)
7048 {
7049 struct peer *member;
7050 struct bgp_filter *filter;
7051 struct listnode *node, *nnode;
7052
7053 if (direct != RMAP_IN && direct != RMAP_OUT)
7054 return BGP_ERR_INVALID_VALUE;
7055
7056 /* Unset override-flag unconditionally. */
7057 UNSET_FLAG(peer->filter_override[afi][safi][direct], PEER_FT_ROUTE_MAP);
7058
7059 /* Inherit configuration from peer-group if peer is member. */
7060 if (peer_group_active(peer)) {
7061 PEER_STR_ATTR_INHERIT(peer, peer->group,
7062 filter[afi][safi].map[direct].name,
7063 MTYPE_BGP_FILTER_NAME);
7064 PEER_ATTR_INHERIT(peer, peer->group,
7065 filter[afi][safi].map[direct].map);
7066 } else {
7067 /* Otherwise remove configuration from peer. */
7068 filter = &peer->filter[afi][safi];
7069 if (filter->map[direct].name)
7070 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7071 route_map_counter_decrement(filter->map[direct].map);
7072 filter->map[direct].name = NULL;
7073 filter->map[direct].map = NULL;
7074 }
7075
7076 /* Check if handling a regular peer. */
7077 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7078 /* Process peer route updates. */
7079 peer_on_policy_change(peer, afi, safi,
7080 (direct == RMAP_OUT) ? 1 : 0);
7081
7082 /* Skip peer-group mechanics for regular peers. */
7083 return 0;
7084 }
7085
7086 /*
7087 * Remove configuration on all peer-group members, unless they are
7088 * explicitly overriding peer-group configuration.
7089 */
7090 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7091 /* Skip peers with overridden configuration. */
7092 if (CHECK_FLAG(member->filter_override[afi][safi][direct],
7093 PEER_FT_ROUTE_MAP))
7094 continue;
7095
7096 /* Remove configuration on peer-group member. */
7097 filter = &member->filter[afi][safi];
7098 if (filter->map[direct].name)
7099 XFREE(MTYPE_BGP_FILTER_NAME, filter->map[direct].name);
7100 route_map_counter_decrement(filter->map[direct].map);
7101 filter->map[direct].name = NULL;
7102 filter->map[direct].map = NULL;
7103
7104 /* Process peer route updates. */
7105 peer_on_policy_change(member, afi, safi,
7106 (direct == RMAP_OUT) ? 1 : 0);
7107 }
7108
7109 return 0;
7110 }
7111
7112 /* Set unsuppress-map to the peer. */
7113 int peer_unsuppress_map_set(struct peer *peer, afi_t afi, safi_t safi,
7114 const char *name, struct route_map *route_map)
7115 {
7116 struct peer *member;
7117 struct bgp_filter *filter;
7118 struct listnode *node, *nnode;
7119
7120 /* Set configuration on peer. */
7121 filter = &peer->filter[afi][safi];
7122 if (filter->usmap.name)
7123 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7124 route_map_counter_decrement(filter->usmap.map);
7125 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7126 filter->usmap.map = route_map;
7127 route_map_counter_increment(route_map);
7128
7129 /* Check if handling a regular peer. */
7130 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7131 /* Set override-flag and process peer route updates. */
7132 SET_FLAG(peer->filter_override[afi][safi][0],
7133 PEER_FT_UNSUPPRESS_MAP);
7134 peer_on_policy_change(peer, afi, safi, 1);
7135
7136 /* Skip peer-group mechanics for regular peers. */
7137 return 0;
7138 }
7139
7140 /*
7141 * Set configuration on all peer-group members, unless they are
7142 * explicitly overriding peer-group configuration.
7143 */
7144 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7145 /* Skip peers with overridden configuration. */
7146 if (CHECK_FLAG(member->filter_override[afi][safi][0],
7147 PEER_FT_UNSUPPRESS_MAP))
7148 continue;
7149
7150 /* Set configuration on peer-group member. */
7151 filter = &member->filter[afi][safi];
7152 if (filter->usmap.name)
7153 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7154 route_map_counter_decrement(filter->usmap.map);
7155 filter->usmap.name = XSTRDUP(MTYPE_BGP_FILTER_NAME, name);
7156 filter->usmap.map = route_map;
7157 route_map_counter_increment(route_map);
7158
7159 /* Process peer route updates. */
7160 peer_on_policy_change(member, afi, safi, 1);
7161 }
7162
7163 return 0;
7164 }
7165
7166 /* Unset route-map from the peer. */
7167 int peer_unsuppress_map_unset(struct peer *peer, afi_t afi, safi_t safi)
7168 {
7169 struct peer *member;
7170 struct bgp_filter *filter;
7171 struct listnode *node, *nnode;
7172
7173 /* Unset override-flag unconditionally. */
7174 UNSET_FLAG(peer->filter_override[afi][safi][0], PEER_FT_UNSUPPRESS_MAP);
7175
7176 /* Inherit configuration from peer-group if peer is member. */
7177 if (peer_group_active(peer)) {
7178 PEER_STR_ATTR_INHERIT(peer, peer->group,
7179 filter[afi][safi].usmap.name,
7180 MTYPE_BGP_FILTER_NAME);
7181 PEER_ATTR_INHERIT(peer, peer->group,
7182 filter[afi][safi].usmap.map);
7183 } else {
7184 /* Otherwise remove configuration from peer. */
7185 filter = &peer->filter[afi][safi];
7186 if (filter->usmap.name)
7187 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7188 route_map_counter_decrement(filter->usmap.map);
7189 filter->usmap.name = NULL;
7190 filter->usmap.map = NULL;
7191 }
7192
7193 /* Check if handling a regular peer. */
7194 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7195 /* Process peer route updates. */
7196 peer_on_policy_change(peer, afi, safi, 1);
7197
7198 /* Skip peer-group mechanics for regular peers. */
7199 return 0;
7200 }
7201
7202 /*
7203 * Remove configuration on all peer-group members, unless they are
7204 * explicitly overriding peer-group configuration.
7205 */
7206 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7207 /* Skip peers with overridden configuration. */
7208 if (CHECK_FLAG(member->filter_override[afi][safi][0],
7209 PEER_FT_UNSUPPRESS_MAP))
7210 continue;
7211
7212 /* Remove configuration on peer-group member. */
7213 filter = &member->filter[afi][safi];
7214 if (filter->usmap.name)
7215 XFREE(MTYPE_BGP_FILTER_NAME, filter->usmap.name);
7216 route_map_counter_decrement(filter->usmap.map);
7217 filter->usmap.name = NULL;
7218 filter->usmap.map = NULL;
7219
7220 /* Process peer route updates. */
7221 peer_on_policy_change(member, afi, safi, 1);
7222 }
7223
7224 return 0;
7225 }
7226
7227 static bool peer_maximum_prefix_clear_overflow(struct peer *peer)
7228 {
7229 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
7230 return false;
7231
7232 UNSET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
7233 if (peer->t_pmax_restart) {
7234 THREAD_OFF(peer->t_pmax_restart);
7235 if (bgp_debug_neighbor_events(peer))
7236 zlog_debug(
7237 "%pBP Maximum-prefix restart timer cancelled",
7238 peer);
7239 }
7240 BGP_EVENT_ADD(peer, BGP_Start);
7241 return true;
7242 }
7243
7244 int peer_maximum_prefix_set(struct peer *peer, afi_t afi, safi_t safi,
7245 uint32_t max, uint8_t threshold, int warning,
7246 uint16_t restart, bool force)
7247 {
7248 struct peer *member;
7249 struct listnode *node, *nnode;
7250
7251 /* Set flags and configuration on peer. */
7252 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7253
7254 if (force)
7255 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7256 else
7257 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7258
7259 if (warning)
7260 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
7261 else
7262 peer_af_flag_unset(peer, afi, safi,
7263 PEER_FLAG_MAX_PREFIX_WARNING);
7264
7265 peer->pmax[afi][safi] = max;
7266 peer->pmax_threshold[afi][safi] = threshold;
7267 peer->pmax_restart[afi][safi] = restart;
7268
7269 /* Check if handling a regular peer. */
7270 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7271 /* Re-check if peer violates maximum-prefix. */
7272 if ((peer_established(peer)) && (peer->afc[afi][safi]))
7273 bgp_maximum_prefix_overflow(peer, afi, safi, 1);
7274
7275 /* Skip peer-group mechanics for regular peers. */
7276 return 0;
7277 }
7278
7279 /*
7280 * Set flags and configuration on all peer-group members, unless they
7281 * are explicitly overriding peer-group configuration.
7282 */
7283 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7284 /* Skip peers with overridden configuration. */
7285 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7286 PEER_FLAG_MAX_PREFIX))
7287 continue;
7288
7289 /* Set flag and configuration on peer-group member. */
7290 member->pmax[afi][safi] = max;
7291 member->pmax_threshold[afi][safi] = threshold;
7292 member->pmax_restart[afi][safi] = restart;
7293
7294 if (force)
7295 SET_FLAG(member->af_flags[afi][safi],
7296 PEER_FLAG_MAX_PREFIX_FORCE);
7297 else
7298 UNSET_FLAG(member->af_flags[afi][safi],
7299 PEER_FLAG_MAX_PREFIX_FORCE);
7300
7301 if (warning)
7302 SET_FLAG(member->af_flags[afi][safi],
7303 PEER_FLAG_MAX_PREFIX_WARNING);
7304 else
7305 UNSET_FLAG(member->af_flags[afi][safi],
7306 PEER_FLAG_MAX_PREFIX_WARNING);
7307
7308 /* Re-check if peer violates maximum-prefix. */
7309 if ((peer_established(member)) && (member->afc[afi][safi]))
7310 bgp_maximum_prefix_overflow(member, afi, safi, 1);
7311 }
7312
7313 return 0;
7314 }
7315
7316 int peer_maximum_prefix_unset(struct peer *peer, afi_t afi, safi_t safi)
7317 {
7318 /* Inherit configuration from peer-group if peer is member. */
7319 if (peer_group_active(peer)) {
7320 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7321 peer_af_flag_inherit(peer, afi, safi,
7322 PEER_FLAG_MAX_PREFIX_FORCE);
7323 peer_af_flag_inherit(peer, afi, safi,
7324 PEER_FLAG_MAX_PREFIX_WARNING);
7325 PEER_ATTR_INHERIT(peer, peer->group, pmax[afi][safi]);
7326 PEER_ATTR_INHERIT(peer, peer->group, pmax_threshold[afi][safi]);
7327 PEER_ATTR_INHERIT(peer, peer->group, pmax_restart[afi][safi]);
7328
7329 return 0;
7330 }
7331
7332 /* Remove flags and configuration from peer. */
7333 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX);
7334 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_FORCE);
7335 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_WARNING);
7336 peer->pmax[afi][safi] = 0;
7337 peer->pmax_threshold[afi][safi] = 0;
7338 peer->pmax_restart[afi][safi] = 0;
7339
7340 /*
7341 * Remove flags and configuration from all peer-group members, unless
7342 * they are explicitly overriding peer-group configuration.
7343 */
7344 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7345 struct peer *member;
7346 struct listnode *node;
7347
7348 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7349 /* Skip peers with overridden configuration. */
7350 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7351 PEER_FLAG_MAX_PREFIX))
7352 continue;
7353
7354 /* Remove flag and configuration on peer-group member.
7355 */
7356 UNSET_FLAG(member->af_flags[afi][safi],
7357 PEER_FLAG_MAX_PREFIX);
7358 UNSET_FLAG(member->af_flags[afi][safi],
7359 PEER_FLAG_MAX_PREFIX_FORCE);
7360 UNSET_FLAG(member->af_flags[afi][safi],
7361 PEER_FLAG_MAX_PREFIX_WARNING);
7362 member->pmax[afi][safi] = 0;
7363 member->pmax_threshold[afi][safi] = 0;
7364 member->pmax_restart[afi][safi] = 0;
7365
7366 peer_maximum_prefix_clear_overflow(member);
7367 }
7368 } else {
7369 peer_maximum_prefix_clear_overflow(peer);
7370 }
7371
7372 return 0;
7373 }
7374
7375 void peer_maximum_prefix_out_refresh_routes(struct peer *peer, afi_t afi,
7376 safi_t safi)
7377 {
7378 update_group_adjust_peer(peer_af_find(peer, afi, safi));
7379
7380 if (peer_established(peer))
7381 bgp_announce_route(peer, afi, safi, false);
7382 }
7383
7384 int peer_maximum_prefix_out_set(struct peer *peer, afi_t afi, safi_t safi,
7385 uint32_t max)
7386 {
7387 struct peer *member;
7388 struct listnode *node, *nnode;
7389
7390 /* Set flag on peer and peer-group member if any */
7391 peer_af_flag_set(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7392 /* Set configuration on peer. */
7393 peer->pmax_out[afi][safi] = max;
7394
7395 /* Check if handling a regular peer. */
7396 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7397 /* Skip peer-group mechanics for regular peers. */
7398 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7399 return 0;
7400 }
7401
7402 /*
7403 * Set flag and configuration on all peer-group members, unless they
7404 * are explicitly overriding peer-group configuration.
7405 */
7406 for (ALL_LIST_ELEMENTS(peer->group->peer, node, nnode, member)) {
7407 /* Skip peers with overridden configuration. */
7408 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7409 PEER_FLAG_MAX_PREFIX_OUT))
7410 continue;
7411
7412 /* Set configuration on peer-group member. */
7413 member->pmax_out[afi][safi] = max;
7414
7415 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7416 }
7417 return 0;
7418 }
7419
7420 int peer_maximum_prefix_out_unset(struct peer *peer, afi_t afi, safi_t safi)
7421 {
7422 struct peer *member;
7423 struct listnode *node;
7424 /* Inherit configuration from peer-group if peer is member. */
7425 if (peer_group_active(peer)) {
7426 peer_af_flag_inherit(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7427 PEER_ATTR_INHERIT(peer, peer->group, pmax_out[afi][safi]);
7428
7429 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7430 return 0;
7431 }
7432
7433 /* Remove flag and configuration from peer. */
7434 peer_af_flag_unset(peer, afi, safi, PEER_FLAG_MAX_PREFIX_OUT);
7435 peer->pmax_out[afi][safi] = 0;
7436
7437 /* Check if handling a regular peer. */
7438 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7439 /* Skip peer-group mechanics for regular peers. */
7440 peer_maximum_prefix_out_refresh_routes(peer, afi, safi);
7441 return 0;
7442 }
7443
7444 /*
7445 * Remove flag and configuration from all peer-group members, unless
7446 * they are explicitly overriding peer-group configuration.
7447 */
7448 for (ALL_LIST_ELEMENTS_RO(peer->group->peer, node, member)) {
7449 /* Skip peers with overridden configuration. */
7450 if (CHECK_FLAG(member->af_flags_override[afi][safi],
7451 PEER_FLAG_MAX_PREFIX_OUT))
7452 continue;
7453
7454 /* Remove flag and configuration on peer-group member.
7455 */
7456 UNSET_FLAG(member->af_flags[afi][safi],
7457 PEER_FLAG_MAX_PREFIX_OUT);
7458 member->pmax_out[afi][safi] = 0;
7459
7460 peer_maximum_prefix_out_refresh_routes(member, afi, safi);
7461 }
7462 return 0;
7463 }
7464
7465 int is_ebgp_multihop_configured(struct peer *peer)
7466 {
7467 struct peer_group *group;
7468 struct listnode *node, *nnode;
7469 struct peer *peer1;
7470
7471 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7472 group = peer->group;
7473 if ((peer_sort(peer) != BGP_PEER_IBGP)
7474 && (group->conf->ttl != BGP_DEFAULT_TTL))
7475 return 1;
7476
7477 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer1)) {
7478 if ((peer_sort(peer1) != BGP_PEER_IBGP)
7479 && (peer1->ttl != BGP_DEFAULT_TTL))
7480 return 1;
7481 }
7482 } else {
7483 if ((peer_sort(peer) != BGP_PEER_IBGP)
7484 && (peer->ttl != BGP_DEFAULT_TTL))
7485 return 1;
7486 }
7487 return 0;
7488 }
7489
7490 /* Set # of hops between us and BGP peer. */
7491 int peer_ttl_security_hops_set(struct peer *peer, int gtsm_hops)
7492 {
7493 struct peer_group *group;
7494 struct peer *gpeer;
7495 struct listnode *node, *nnode;
7496 int ret;
7497
7498 zlog_debug("%s: set gtsm_hops to %d for %s", __func__, gtsm_hops,
7499 peer->host);
7500
7501 /* We cannot configure ttl-security hops when ebgp-multihop is already
7502 set. For non peer-groups, the check is simple. For peer-groups,
7503 it's
7504 slightly messy, because we need to check both the peer-group
7505 structure
7506 and all peer-group members for any trace of ebgp-multihop
7507 configuration
7508 before actually applying the ttl-security rules. Cisco really made a
7509 mess of this configuration parameter, and OpenBGPD got it right.
7510 */
7511
7512 if ((peer->gtsm_hops == BGP_GTSM_HOPS_DISABLED)
7513 && (peer->sort != BGP_PEER_IBGP)) {
7514 if (is_ebgp_multihop_configured(peer))
7515 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK;
7516
7517 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7518 peer->gtsm_hops = gtsm_hops;
7519
7520 /* Calling ebgp multihop also resets the session.
7521 * On restart, NHT will get setup correctly as will the
7522 * min & max ttls on the socket. The return value is
7523 * irrelevant.
7524 */
7525 ret = peer_ebgp_multihop_set(peer, MAXTTL);
7526
7527 if (ret != 0)
7528 return ret;
7529 } else {
7530 group = peer->group;
7531 group->conf->gtsm_hops = gtsm_hops;
7532 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
7533 gpeer)) {
7534 gpeer->gtsm_hops = group->conf->gtsm_hops;
7535
7536 /* Calling ebgp multihop also resets the
7537 * session.
7538 * On restart, NHT will get setup correctly as
7539 * will the
7540 * min & max ttls on the socket. The return
7541 * value is
7542 * irrelevant.
7543 */
7544 peer_ebgp_multihop_set(gpeer, MAXTTL);
7545 }
7546 }
7547 } else {
7548 /* Post the first gtsm setup or if its ibgp, maxttl setting
7549 * isn't
7550 * necessary, just set the minttl.
7551 */
7552 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7553 peer->gtsm_hops = gtsm_hops;
7554
7555 if (peer->fd >= 0)
7556 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7557 MAXTTL + 1 - gtsm_hops);
7558 if ((peer->status < Established) && peer->doppelganger
7559 && (peer->doppelganger->fd >= 0))
7560 sockopt_minttl(peer->su.sa.sa_family,
7561 peer->doppelganger->fd,
7562 MAXTTL + 1 - gtsm_hops);
7563 } else {
7564 group = peer->group;
7565 group->conf->gtsm_hops = gtsm_hops;
7566 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
7567 gpeer)) {
7568 gpeer->gtsm_hops = group->conf->gtsm_hops;
7569
7570 /* Change setting of existing peer
7571 * established then change value (may break
7572 * connectivity)
7573 * not established yet (teardown session and
7574 * restart)
7575 * no session then do nothing (will get
7576 * handled by next connection)
7577 */
7578 if (gpeer->fd >= 0
7579 && gpeer->gtsm_hops
7580 != BGP_GTSM_HOPS_DISABLED)
7581 sockopt_minttl(
7582 gpeer->su.sa.sa_family,
7583 gpeer->fd,
7584 MAXTTL + 1 - gpeer->gtsm_hops);
7585 if ((gpeer->status < Established)
7586 && gpeer->doppelganger
7587 && (gpeer->doppelganger->fd >= 0))
7588 sockopt_minttl(gpeer->su.sa.sa_family,
7589 gpeer->doppelganger->fd,
7590 MAXTTL + 1 - gtsm_hops);
7591 }
7592 }
7593 }
7594
7595 return 0;
7596 }
7597
7598 int peer_ttl_security_hops_unset(struct peer *peer)
7599 {
7600 struct peer_group *group;
7601 struct listnode *node, *nnode;
7602 int ret = 0;
7603
7604 zlog_debug("%s: set gtsm_hops to zero for %s", __func__, peer->host);
7605
7606 /* if a peer-group member, then reset to peer-group default rather than
7607 * 0 */
7608 if (peer_group_active(peer))
7609 peer->gtsm_hops = peer->group->conf->gtsm_hops;
7610 else
7611 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
7612
7613 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
7614 /* Invoking ebgp_multihop_set will set the TTL back to the
7615 * original
7616 * value as well as restting the NHT and such. The session is
7617 * reset.
7618 */
7619 if (peer->sort == BGP_PEER_EBGP)
7620 ret = peer_ebgp_multihop_unset(peer);
7621 else {
7622 if (peer->fd >= 0)
7623 sockopt_minttl(peer->su.sa.sa_family, peer->fd,
7624 0);
7625
7626 if ((peer->status < Established) && peer->doppelganger
7627 && (peer->doppelganger->fd >= 0))
7628 sockopt_minttl(peer->su.sa.sa_family,
7629 peer->doppelganger->fd, 0);
7630 }
7631 } else {
7632 group = peer->group;
7633 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
7634 peer->gtsm_hops = BGP_GTSM_HOPS_DISABLED;
7635 if (peer->sort == BGP_PEER_EBGP)
7636 ret = peer_ebgp_multihop_unset(peer);
7637 else {
7638 if (peer->fd >= 0)
7639 sockopt_minttl(peer->su.sa.sa_family,
7640 peer->fd, 0);
7641
7642 if ((peer->status < Established)
7643 && peer->doppelganger
7644 && (peer->doppelganger->fd >= 0))
7645 sockopt_minttl(peer->su.sa.sa_family,
7646 peer->doppelganger->fd,
7647 0);
7648 }
7649 }
7650 }
7651
7652 return ret;
7653 }
7654
7655 static void peer_reset_message_stats(struct peer *peer)
7656 {
7657 if (peer) {
7658 atomic_store_explicit(&peer->open_in, 0, memory_order_relaxed);
7659 atomic_store_explicit(&peer->open_out, 0, memory_order_relaxed);
7660 atomic_store_explicit(&peer->update_in, 0,
7661 memory_order_relaxed);
7662 atomic_store_explicit(&peer->update_out, 0,
7663 memory_order_relaxed);
7664 atomic_store_explicit(&peer->keepalive_in, 0,
7665 memory_order_relaxed);
7666 atomic_store_explicit(&peer->keepalive_out, 0,
7667 memory_order_relaxed);
7668 atomic_store_explicit(&peer->notify_in, 0,
7669 memory_order_relaxed);
7670 atomic_store_explicit(&peer->notify_out, 0,
7671 memory_order_relaxed);
7672 atomic_store_explicit(&peer->refresh_in, 0,
7673 memory_order_relaxed);
7674 atomic_store_explicit(&peer->refresh_out, 0,
7675 memory_order_relaxed);
7676 atomic_store_explicit(&peer->dynamic_cap_in, 0,
7677 memory_order_relaxed);
7678 atomic_store_explicit(&peer->dynamic_cap_out, 0,
7679 memory_order_relaxed);
7680 }
7681 }
7682
7683 /*
7684 * If peer clear is invoked in a loop for all peers on the BGP instance,
7685 * it may end up freeing the doppelganger, and if this was the next node
7686 * to the current node, we would end up accessing the freed next node.
7687 * Pass along additional parameter which can be updated if next node
7688 * is freed; only required when walking the peer list on BGP instance.
7689 */
7690 int peer_clear(struct peer *peer, struct listnode **nnode)
7691 {
7692 if (!CHECK_FLAG(peer->flags, PEER_FLAG_SHUTDOWN)
7693 || !CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHUTDOWN)) {
7694 if (peer_maximum_prefix_clear_overflow(peer))
7695 return 0;
7696
7697 peer->v_start = BGP_INIT_START_TIMER;
7698 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
7699 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
7700 BGP_NOTIFY_CEASE_ADMIN_RESET);
7701 else
7702 bgp_session_reset_safe(peer, nnode);
7703 }
7704 return 0;
7705 }
7706
7707 int peer_clear_soft(struct peer *peer, afi_t afi, safi_t safi,
7708 enum bgp_clear_type stype)
7709 {
7710 struct peer_af *paf;
7711
7712 if (!peer_established(peer))
7713 return 0;
7714
7715 if (!peer->afc[afi][safi])
7716 return BGP_ERR_AF_UNCONFIGURED;
7717
7718 peer->rtt = sockopt_tcp_rtt(peer->fd);
7719
7720 if (stype == BGP_CLEAR_SOFT_OUT || stype == BGP_CLEAR_SOFT_BOTH) {
7721 /* Clear the "neighbor x.x.x.x default-originate" flag */
7722 paf = peer_af_find(peer, afi, safi);
7723 if (paf && paf->subgroup
7724 && CHECK_FLAG(paf->subgroup->sflags,
7725 SUBGRP_STATUS_DEFAULT_ORIGINATE))
7726 UNSET_FLAG(paf->subgroup->sflags,
7727 SUBGRP_STATUS_DEFAULT_ORIGINATE);
7728
7729 bgp_announce_route(peer, afi, safi, false);
7730 }
7731
7732 if (stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7733 if (CHECK_FLAG(peer->af_cap[afi][safi],
7734 PEER_CAP_ORF_PREFIX_SM_ADV)
7735 && (CHECK_FLAG(peer->af_cap[afi][safi],
7736 PEER_CAP_ORF_PREFIX_RM_RCV)
7737 || CHECK_FLAG(peer->af_cap[afi][safi],
7738 PEER_CAP_ORF_PREFIX_RM_OLD_RCV))) {
7739 struct bgp_filter *filter = &peer->filter[afi][safi];
7740 uint8_t prefix_type;
7741
7742 if (CHECK_FLAG(peer->af_cap[afi][safi],
7743 PEER_CAP_ORF_PREFIX_RM_RCV))
7744 prefix_type = ORF_TYPE_PREFIX;
7745 else
7746 prefix_type = ORF_TYPE_PREFIX_OLD;
7747
7748 if (filter->plist[FILTER_IN].plist) {
7749 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7750 PEER_STATUS_ORF_PREFIX_SEND))
7751 bgp_route_refresh_send(
7752 peer, afi, safi, prefix_type,
7753 REFRESH_DEFER, 1,
7754 BGP_ROUTE_REFRESH_NORMAL);
7755 bgp_route_refresh_send(
7756 peer, afi, safi, prefix_type,
7757 REFRESH_IMMEDIATE, 0,
7758 BGP_ROUTE_REFRESH_NORMAL);
7759 } else {
7760 if (CHECK_FLAG(peer->af_sflags[afi][safi],
7761 PEER_STATUS_ORF_PREFIX_SEND))
7762 bgp_route_refresh_send(
7763 peer, afi, safi, prefix_type,
7764 REFRESH_IMMEDIATE, 1,
7765 BGP_ROUTE_REFRESH_NORMAL);
7766 else
7767 bgp_route_refresh_send(
7768 peer, afi, safi, 0, 0, 0,
7769 BGP_ROUTE_REFRESH_NORMAL);
7770 }
7771 return 0;
7772 }
7773 }
7774
7775 if (stype == BGP_CLEAR_SOFT_IN || stype == BGP_CLEAR_SOFT_BOTH
7776 || stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) {
7777 /* If neighbor has soft reconfiguration inbound flag.
7778 Use Adj-RIB-In database. */
7779 if (CHECK_FLAG(peer->af_flags[afi][safi],
7780 PEER_FLAG_SOFT_RECONFIG))
7781 bgp_soft_reconfig_in(peer, afi, safi);
7782 else {
7783 /* If neighbor has route refresh capability, send route
7784 refresh
7785 message to the peer. */
7786 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_OLD_RCV)
7787 || CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
7788 bgp_route_refresh_send(
7789 peer, afi, safi, 0, 0, 0,
7790 BGP_ROUTE_REFRESH_NORMAL);
7791 else
7792 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED;
7793 }
7794 }
7795
7796 if (stype == BGP_CLEAR_MESSAGE_STATS)
7797 peer_reset_message_stats(peer);
7798
7799 return 0;
7800 }
7801
7802 /* Display peer uptime.*/
7803 char *peer_uptime(time_t uptime2, char *buf, size_t len, bool use_json,
7804 json_object *json)
7805 {
7806 time_t uptime1, epoch_tbuf;
7807 struct tm tm;
7808
7809 /* If there is no connection has been done before print `never'. */
7810 if (uptime2 == 0) {
7811 if (use_json) {
7812 json_object_string_add(json, "peerUptime", "never");
7813 json_object_int_add(json, "peerUptimeMsec", 0);
7814 } else
7815 snprintf(buf, len, "never");
7816 return buf;
7817 }
7818
7819 /* Get current time. */
7820 uptime1 = monotime(NULL);
7821 uptime1 -= uptime2;
7822 gmtime_r(&uptime1, &tm);
7823
7824 if (uptime1 < ONE_DAY_SECOND)
7825 snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
7826 tm.tm_sec);
7827 else if (uptime1 < ONE_WEEK_SECOND)
7828 snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
7829 tm.tm_min);
7830 else if (uptime1 < ONE_YEAR_SECOND)
7831 snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
7832 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
7833 else
7834 snprintf(buf, len, "%02dy%02dw%dd", tm.tm_year - 70,
7835 tm.tm_yday / 7,
7836 tm.tm_yday - ((tm.tm_yday / 7) * 7));
7837
7838 if (use_json) {
7839 epoch_tbuf = time(NULL) - uptime1;
7840 json_object_string_add(json, "peerUptime", buf);
7841 json_object_int_add(json, "peerUptimeMsec", uptime1 * 1000);
7842 json_object_int_add(json, "peerUptimeEstablishedEpoch",
7843 epoch_tbuf);
7844 }
7845
7846 return buf;
7847 }
7848
7849 void bgp_master_init(struct thread_master *master, const int buffer_size,
7850 struct list *addresses)
7851 {
7852 qobj_init();
7853
7854 memset(&bgp_master, 0, sizeof(bgp_master));
7855
7856 bm = &bgp_master;
7857 bm->bgp = list_new();
7858 bm->listen_sockets = list_new();
7859 bm->port = BGP_PORT_DEFAULT;
7860 bm->addresses = addresses;
7861 bm->master = master;
7862 bm->start_time = monotime(NULL);
7863 bm->t_rmap_update = NULL;
7864 bm->rmap_update_timer = RMAP_DEFAULT_UPDATE_TIMER;
7865 bm->v_update_delay = BGP_UPDATE_DELAY_DEF;
7866 bm->v_establish_wait = BGP_UPDATE_DELAY_DEF;
7867 bm->terminating = false;
7868 bm->socket_buffer = buffer_size;
7869 bm->wait_for_fib = false;
7870 bm->tcp_dscp = IPTOS_PREC_INTERNETCONTROL;
7871
7872 bgp_mac_init();
7873 /* init the rd id space.
7874 assign 0th index in the bitfield,
7875 so that we start with id 1
7876 */
7877 bf_init(bm->rd_idspace, UINT16_MAX);
7878 bf_assign_zero_index(bm->rd_idspace);
7879
7880 /* mpls label dynamic allocation pool */
7881 bgp_lp_init(bm->master, &bm->labelpool);
7882
7883 bgp_l3nhg_init();
7884 bgp_evpn_mh_init();
7885 QOBJ_REG(bm, bgp_master);
7886 }
7887
7888 /*
7889 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7890 * instance delete (non-default only) or BGP exit.
7891 */
7892 static void bgp_if_finish(struct bgp *bgp)
7893 {
7894 struct vrf *vrf;
7895 struct interface *ifp;
7896
7897 vrf = bgp_vrf_lookup_by_instance_type(bgp);
7898
7899 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW || !vrf)
7900 return;
7901
7902 FOR_ALL_INTERFACES (vrf, ifp) {
7903 struct listnode *c_node, *c_nnode;
7904 struct connected *c;
7905
7906 for (ALL_LIST_ELEMENTS(ifp->connected, c_node, c_nnode, c))
7907 bgp_connected_delete(bgp, c);
7908 }
7909 }
7910
7911 static void bgp_viewvrf_autocomplete(vector comps, struct cmd_token *token)
7912 {
7913 struct vrf *vrf = NULL;
7914 struct listnode *next;
7915 struct bgp *bgp;
7916
7917 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
7918 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
7919
7920 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
7921 if (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
7922 continue;
7923
7924 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, bgp->name));
7925 }
7926 }
7927
7928 static void bgp_instasn_autocomplete(vector comps, struct cmd_token *token)
7929 {
7930 struct listnode *next, *next2;
7931 struct bgp *bgp, *bgp2;
7932 char buf[11];
7933
7934 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next, bgp)) {
7935 /* deduplicate */
7936 for (ALL_LIST_ELEMENTS_RO(bm->bgp, next2, bgp2)) {
7937 if (bgp2->as == bgp->as)
7938 break;
7939 if (bgp2 == bgp)
7940 break;
7941 }
7942 if (bgp2 != bgp)
7943 continue;
7944
7945 snprintf(buf, sizeof(buf), "%u", bgp->as);
7946 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, buf));
7947 }
7948 }
7949
7950 static const struct cmd_variable_handler bgp_viewvrf_var_handlers[] = {
7951 {.tokenname = "VIEWVRFNAME", .completions = bgp_viewvrf_autocomplete},
7952 {.varname = "instasn", .completions = bgp_instasn_autocomplete},
7953 {.completions = NULL},
7954 };
7955
7956 struct frr_pthread *bgp_pth_io;
7957 struct frr_pthread *bgp_pth_ka;
7958
7959 static void bgp_pthreads_init(void)
7960 {
7961 assert(!bgp_pth_io);
7962 assert(!bgp_pth_ka);
7963
7964 struct frr_pthread_attr io = {
7965 .start = frr_pthread_attr_default.start,
7966 .stop = frr_pthread_attr_default.stop,
7967 };
7968 struct frr_pthread_attr ka = {
7969 .start = bgp_keepalives_start,
7970 .stop = bgp_keepalives_stop,
7971 };
7972 bgp_pth_io = frr_pthread_new(&io, "BGP I/O thread", "bgpd_io");
7973 bgp_pth_ka = frr_pthread_new(&ka, "BGP Keepalives thread", "bgpd_ka");
7974 }
7975
7976 void bgp_pthreads_run(void)
7977 {
7978 frr_pthread_run(bgp_pth_io, NULL);
7979 frr_pthread_run(bgp_pth_ka, NULL);
7980
7981 /* Wait until threads are ready. */
7982 frr_pthread_wait_running(bgp_pth_io);
7983 frr_pthread_wait_running(bgp_pth_ka);
7984 }
7985
7986 void bgp_pthreads_finish(void)
7987 {
7988 frr_pthread_stop_all();
7989 }
7990
7991 static int peer_unshut_after_cfg(struct bgp *bgp)
7992 {
7993 struct listnode *node;
7994 struct peer *peer;
7995
7996 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
7997 if (!peer->shut_during_cfg)
7998 continue;
7999
8000 if (bgp_debug_neighbor_events(peer))
8001 zlog_debug("%s: released from config-pending hold",
8002 peer->host);
8003
8004 peer->shut_during_cfg = false;
8005 if (peer_active(peer) && peer->status != Established) {
8006 if (peer->status != Idle)
8007 BGP_EVENT_ADD(peer, BGP_Stop);
8008 BGP_EVENT_ADD(peer, BGP_Start);
8009 }
8010 }
8011
8012 return 0;
8013 }
8014
8015 void bgp_init(unsigned short instance)
8016 {
8017 hook_register(bgp_config_end, peer_unshut_after_cfg);
8018
8019 /* allocates some vital data structures used by peer commands in
8020 * vty_init */
8021
8022 /* pre-init pthreads */
8023 bgp_pthreads_init();
8024
8025 /* Init zebra. */
8026 bgp_zebra_init(bm->master, instance);
8027
8028 #ifdef ENABLE_BGP_VNC
8029 vnc_zebra_init(bm->master);
8030 #endif
8031
8032 /* BGP VTY commands installation. */
8033 bgp_vty_init();
8034
8035 /* BGP inits. */
8036 bgp_attr_init();
8037 bgp_debug_init();
8038 bgp_community_alias_init();
8039 bgp_dump_init();
8040 bgp_route_init();
8041 bgp_route_map_init();
8042 bgp_scan_vty_init();
8043 bgp_mplsvpn_init();
8044 #ifdef ENABLE_BGP_VNC
8045 rfapi_init();
8046 #endif
8047 bgp_ethernetvpn_init();
8048 bgp_flowspec_vty_init();
8049
8050 /* Access list initialize. */
8051 access_list_init();
8052 access_list_add_hook(peer_distribute_update);
8053 access_list_delete_hook(peer_distribute_update);
8054
8055 /* Filter list initialize. */
8056 bgp_filter_init();
8057 as_list_add_hook(peer_aslist_add);
8058 as_list_delete_hook(peer_aslist_del);
8059
8060 /* Prefix list initialize.*/
8061 prefix_list_init();
8062 prefix_list_add_hook(peer_prefix_list_update);
8063 prefix_list_delete_hook(peer_prefix_list_update);
8064
8065 /* Community list initialize. */
8066 bgp_clist = community_list_init();
8067
8068 /* BFD init */
8069 bgp_bfd_init(bm->master);
8070
8071 bgp_lp_vty_init();
8072
8073 cmd_variable_handler_register(bgp_viewvrf_var_handlers);
8074 }
8075
8076 void bgp_terminate(void)
8077 {
8078 struct bgp *bgp;
8079 struct peer *peer;
8080 struct listnode *node, *nnode;
8081 struct listnode *mnode, *mnnode;
8082
8083 QOBJ_UNREG(bm);
8084
8085 /* Close the listener sockets first as this prevents peers from
8086 * attempting
8087 * to reconnect on receiving the peer unconfig message. In the presence
8088 * of a large number of peers this will ensure that no peer is left with
8089 * a dangling connection
8090 */
8091
8092 bgp_close();
8093 /* reverse bgp_master_init */
8094 for (ALL_LIST_ELEMENTS(bm->bgp, mnode, mnnode, bgp)) {
8095 bgp_close_vrf_socket(bgp);
8096 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8097 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)) {
8098 if (bgp_debug_neighbor_events(peer))
8099 zlog_debug(
8100 "%pBP configured Graceful-Restart, skipping unconfig notification",
8101 peer);
8102 continue;
8103 }
8104 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer->status))
8105 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
8106 BGP_NOTIFY_CEASE_PEER_UNCONFIG);
8107 }
8108 }
8109
8110 if (bm->listen_sockets)
8111 list_delete(&bm->listen_sockets);
8112
8113 THREAD_OFF(bm->t_rmap_update);
8114
8115 bgp_mac_finish();
8116 }
8117
8118 struct peer *peer_lookup_in_view(struct vty *vty, struct bgp *bgp,
8119 const char *ip_str, bool use_json)
8120 {
8121 int ret;
8122 struct peer *peer;
8123 union sockunion su;
8124
8125 /* Get peer sockunion. */
8126 ret = str2sockunion(ip_str, &su);
8127 if (ret < 0) {
8128 peer = peer_lookup_by_conf_if(bgp, ip_str);
8129 if (!peer) {
8130 peer = peer_lookup_by_hostname(bgp, ip_str);
8131
8132 if (!peer) {
8133 if (use_json) {
8134 json_object *json_no = NULL;
8135 json_no = json_object_new_object();
8136 json_object_string_add(
8137 json_no,
8138 "malformedAddressOrName",
8139 ip_str);
8140 vty_json(vty, json_no);
8141 } else
8142 vty_out(vty,
8143 "%% Malformed address or name: %s\n",
8144 ip_str);
8145 return NULL;
8146 }
8147 }
8148 return peer;
8149 }
8150
8151 /* Peer structure lookup. */
8152 peer = peer_lookup(bgp, &su);
8153 if (!peer) {
8154 if (use_json) {
8155 json_object *json_no = NULL;
8156 json_no = json_object_new_object();
8157 json_object_string_add(json_no, "warning",
8158 "No such neighbor in this view/vrf");
8159 vty_json(vty, json_no);
8160 } else
8161 vty_out(vty, "No such neighbor in this view/vrf\n");
8162 return NULL;
8163 }
8164
8165 return peer;
8166 }
8167
8168 void bgp_gr_apply_running_config(void)
8169 {
8170 struct peer *peer = NULL;
8171 struct bgp *bgp = NULL;
8172 struct listnode *node, *nnode;
8173 bool gr_router_detected = false;
8174
8175 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
8176 zlog_debug("[BGP_GR] %s called !", __func__);
8177
8178 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
8179 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8180 bgp_peer_gr_flags_update(peer);
8181 if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART))
8182 gr_router_detected = true;
8183 }
8184
8185 if (gr_router_detected
8186 && bgp->present_zebra_gr_state == ZEBRA_GR_DISABLE) {
8187 bgp_zebra_send_capabilities(bgp, true);
8188 } else if (!gr_router_detected
8189 && bgp->present_zebra_gr_state == ZEBRA_GR_ENABLE) {
8190 bgp_zebra_send_capabilities(bgp, false);
8191 }
8192
8193 gr_router_detected = false;
8194 }
8195 }
8196
8197 printfrr_ext_autoreg_p("BP", printfrr_bp);
8198 static ssize_t printfrr_bp(struct fbuf *buf, struct printfrr_eargs *ea,
8199 const void *ptr)
8200 {
8201 const struct peer *peer = ptr;
8202
8203 if (!peer)
8204 return bputs(buf, "(null)");
8205
8206 return bprintfrr(buf, "%s(%s)", peer->host,
8207 peer->hostname ? peer->hostname : "Unknown");
8208 }