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