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