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