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