]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_network.c
Merge pull request #12837 from donaldsharp/unlikely_routemap
[mirror_frr.git] / bgpd / bgp_network.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/* BGP network related fucntions
896014f4 3 * Copyright (C) 1999 Kunihiro Ishiguro
896014f4 4 */
718e3744 5
6#include <zebra.h>
7
24a58196 8#include "frrevent.h"
718e3744 9#include "sockunion.h"
0df7c91f 10#include "sockopt.h"
718e3744 11#include "memory.h"
12#include "log.h"
13#include "if.h"
14#include "prefix.h"
15#include "command.h"
edd7c245 16#include "privs.h"
0df7c91f 17#include "linklist.h"
fdbc8e77 18#include "network.h"
3f9c7369 19#include "queue.h"
66e373ae 20#include "hash.h"
039f3a34 21#include "filter.h"
61cf4b37 22#include "ns.h"
174482ef 23#include "lib_errors.h"
17cdd31e 24#include "nexthop.h"
718e3744 25
26#include "bgpd/bgpd.h"
1ff9a340 27#include "bgpd/bgp_open.h"
718e3744 28#include "bgpd/bgp_fsm.h"
29#include "bgpd/bgp_attr.h"
30#include "bgpd/bgp_debug.h"
14454c9f 31#include "bgpd/bgp_errors.h"
718e3744 32#include "bgpd/bgp_network.h"
17cdd31e 33#include "bgpd/bgp_zebra.h"
dcd5ba55 34#include "bgpd/bgp_nht.h"
edd7c245 35
36extern struct zebra_privs_t bgpd_privs;
37
97896a91 38static char *bgp_get_bound_name(struct peer *peer);
f2345335 39
2a0e69ae
DS
40void bgp_dump_listener_info(struct vty *vty)
41{
42 struct listnode *node;
43 struct bgp_listener *listener;
44
45 vty_out(vty, "Name fd Address\n");
46 vty_out(vty, "---------------------------\n");
47e12884
DA
47 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
48 vty_out(vty, "%-16s %d %pSU\n",
2a0e69ae 49 listener->name ? listener->name : VRF_DEFAULT_NAME,
47e12884 50 listener->fd, &listener->su);
2a0e69ae
DS
51}
52
0df7c91f
PJ
53/*
54 * Set MD5 key for the socket, for the given IPv4 peer address.
55 * If the password is NULL or zero-length, the option will be disabled.
56 */
d62a17ae 57static int bgp_md5_set_socket(int socket, union sockunion *su,
9e7d9a61 58 uint16_t prefixlen, const char *password)
0df7c91f 59{
d62a17ae 60 int ret = -1;
61 int en = ENOSYS;
ea8b7c71 62#if HAVE_DECL_TCP_MD5SIG
d62a17ae 63 union sockunion su2;
ea8b7c71 64#endif /* HAVE_TCP_MD5SIG */
d62a17ae 65
66 assert(socket >= 0);
67
68#if HAVE_DECL_TCP_MD5SIG
69 /* Ensure there is no extraneous port information. */
70 memcpy(&su2, su, sizeof(union sockunion));
71 if (su2.sa.sa_family == AF_INET)
72 su2.sin.sin_port = 0;
73 else
74 su2.sin6.sin6_port = 0;
9e7d9a61
QY
75
76 /* For addresses, use the non-extended signature functionality */
936fbaef 77 if ((su2.sa.sa_family == AF_INET && prefixlen == IPV4_MAX_BITLEN)
f4d81e55 78 || (su2.sa.sa_family == AF_INET6 && prefixlen == IPV6_MAX_BITLEN))
9e7d9a61
QY
79 ret = sockopt_tcp_signature(socket, &su2, password);
80 else
81 ret = sockopt_tcp_signature_ext(socket, &su2, prefixlen,
82 password);
d62a17ae 83 en = errno;
0df7c91f 84#endif /* HAVE_TCP_MD5SIG */
0df7c91f 85
9e7d9a61 86 if (ret < 0) {
9e7d9a61
QY
87 switch (ret) {
88 case -2:
89 flog_warn(
90 EC_BGP_NO_TCP_MD5,
47e12884
DA
91 "Unable to set TCP MD5 option on socket for peer %pSU (sock=%d): This platform does not support MD5 auth for prefixes",
92 su, socket);
9e7d9a61
QY
93 break;
94 default:
95 flog_warn(
96 EC_BGP_NO_TCP_MD5,
47e12884
DA
97 "Unable to set TCP MD5 option on socket for peer %pSU (sock=%d): %s",
98 su, socket, safe_strerror(en));
9e7d9a61
QY
99 }
100 }
d62a17ae 101
102 return ret;
0df7c91f
PJ
103}
104
105/* Helper for bgp_connect */
d62a17ae 106static int bgp_md5_set_connect(int socket, union sockunion *su,
9e7d9a61 107 uint16_t prefixlen, const char *password)
0df7c91f 108{
d62a17ae 109 int ret = -1;
110
111#if HAVE_DECL_TCP_MD5SIG
0cf6db21 112 frr_with_privs(&bgpd_privs) {
9e7d9a61 113 ret = bgp_md5_set_socket(socket, su, prefixlen, password);
d62a17ae 114 }
0df7c91f 115#endif /* HAVE_TCP_MD5SIG */
d62a17ae 116
117 return ret;
0df7c91f
PJ
118}
119
d62a17ae 120static int bgp_md5_set_password(struct peer *peer, const char *password)
0df7c91f 121{
d62a17ae 122 struct listnode *node;
123 int ret = 0;
124 struct bgp_listener *listener;
125
9e7d9a61
QY
126 /*
127 * Set or unset the password on the listen socket(s). Outbound
6bb30c2c 128 * connections are taken care of in bgp_connect() below.
d62a17ae 129 */
0cf6db21 130 frr_with_privs(&bgpd_privs) {
6bb30c2c 131 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
a4faae3a
PR
132 if (listener->su.sa.sa_family ==
133 peer->su.sa.sa_family) {
9e7d9a61
QY
134 uint16_t prefixlen =
135 peer->su.sa.sa_family == AF_INET
936fbaef 136 ? IPV4_MAX_BITLEN
f4d81e55 137 : IPV6_MAX_BITLEN;
a4faae3a
PR
138
139 /*
140 * if we have stored a BGP vrf instance in the
141 * listener it must match the bgp instance in
142 * the peer otherwise the peer bgp instance
143 * must be the default vrf or a view instance
144 */
145 if (!listener->bgp) {
2c1eba8e 146 if (peer->bgp->vrf_id != VRF_DEFAULT)
a4faae3a
PR
147 continue;
148 } else if (listener->bgp != peer->bgp)
149 continue;
9e7d9a61 150
6bb30c2c 151 ret = bgp_md5_set_socket(listener->fd,
9e7d9a61
QY
152 &peer->su, prefixlen,
153 password);
154 break;
155 }
156 }
157 return ret;
158}
159
a4faae3a 160int bgp_md5_set_prefix(struct bgp *bgp, struct prefix *p, const char *password)
9e7d9a61
QY
161{
162 int ret = 0;
163 union sockunion su;
164 struct listnode *node;
165 struct bgp_listener *listener;
166
167 /* Set or unset the password on the listen socket(s). */
0cf6db21 168 frr_with_privs(&bgpd_privs) {
9e7d9a61 169 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
a4faae3a
PR
170 if (listener->su.sa.sa_family == p->family
171 && ((bgp->vrf_id == VRF_DEFAULT)
172 || (listener->bgp == bgp))) {
9e7d9a61
QY
173 prefix2sockunion(p, &su);
174 ret = bgp_md5_set_socket(listener->fd, &su,
175 p->prefixlen,
176 password);
6bb30c2c
DL
177 break;
178 }
179 }
9e7d9a61 180
d62a17ae 181 return ret;
0df7c91f 182}
3374bef0 183
a4faae3a 184int bgp_md5_unset_prefix(struct bgp *bgp, struct prefix *p)
9e7d9a61 185{
a4faae3a 186 return bgp_md5_set_prefix(bgp, p, NULL);
9e7d9a61
QY
187}
188
d62a17ae 189int bgp_md5_set(struct peer *peer)
89ca90fa 190{
d62a17ae 191 /* Set the password from listen socket. */
192 return bgp_md5_set_password(peer, peer->password);
89ca90fa 193}
194
d1adb448
PG
195static void bgp_update_setsockopt_tcp_keepalive(struct bgp *bgp, int fd)
196{
197 if (!bgp)
198 return;
199 if (bgp->tcp_keepalive_idle != 0) {
200 int ret;
201
202 ret = setsockopt_tcp_keepalive(fd, bgp->tcp_keepalive_idle,
203 bgp->tcp_keepalive_intvl,
204 bgp->tcp_keepalive_probes);
205 if (ret < 0)
206 zlog_err(
207 "Can't set TCP keepalive on socket %d, idle %u intvl %u probes %u",
208 fd, bgp->tcp_keepalive_idle,
209 bgp->tcp_keepalive_intvl,
210 bgp->tcp_keepalive_probes);
211 }
212}
213
d62a17ae 214int bgp_md5_unset(struct peer *peer)
89ca90fa 215{
d62a17ae 216 /* Unset the password from listen socket. */
217 return bgp_md5_set_password(peer, NULL);
89ca90fa 218}
219
d62a17ae 220int bgp_set_socket_ttl(struct peer *peer, int bgp_sock)
ef0b0c3e 221{
d62a17ae 222 int ret = 0;
223
69ecbc6a 224 if (!peer->gtsm_hops) {
d62a17ae 225 ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, peer->ttl);
226 if (ret) {
af4c2728 227 flog_err(
450971aa 228 EC_LIB_SOCKET,
c0d72166
DS
229 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
230 __func__, &peer->remote_id, errno);
d62a17ae 231 return ret;
232 }
69ecbc6a 233 } else {
d62a17ae 234 /* On Linux, setting minttl without setting ttl seems to mess
235 with the
236 outgoing ttl. Therefore setting both.
237 */
238 ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, MAXTTL);
239 if (ret) {
af4c2728 240 flog_err(
450971aa 241 EC_LIB_SOCKET,
c0d72166
DS
242 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
243 __func__, &peer->remote_id, errno);
d62a17ae 244 return ret;
245 }
246 ret = sockopt_minttl(peer->su.sa.sa_family, bgp_sock,
247 MAXTTL + 1 - peer->gtsm_hops);
248 if (ret) {
af4c2728 249 flog_err(
450971aa 250 EC_LIB_SOCKET,
c0d72166
DS
251 "%s: Can't set MinTTL on peer (rtrid %pI4) socket, err = %d",
252 __func__, &peer->remote_id, errno);
d62a17ae 253 return ret;
254 }
255 }
256
257 return ret;
ef0b0c3e
DL
258}
259
8dee0396 260/*
261 * Obtain the BGP instance that the incoming connection should be processed
262 * against. This is important because more than one VRF could be using the
263 * same IP address space. The instance is got by obtaining the device to
264 * which the incoming connection is bound to. This could either be a VRF
265 * or it could be an interface, which in turn determines the VRF.
266 */
d62a17ae 267static int bgp_get_instance_for_inc_conn(int sock, struct bgp **bgp_inst)
8dee0396 268{
120c6587 269#ifndef SO_BINDTODEVICE
d62a17ae 270 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
271 * anyway since the assumption is that the interface name returned by
272 * getsockopt() is useful in identifying the VRF, particularly with
273 * Linux's
274 * VRF l3master device. The whole mechanism is specific to Linux, so...
275 * when other platforms add VRF support, this will need handling here as
276 * well. (or, some restructuring) */
277 *bgp_inst = bgp_get_default();
278 return !*bgp_inst;
120c6587
DL
279
280#else
d62a17ae 281 char name[VRF_NAMSIZ + 1];
282 socklen_t name_len = VRF_NAMSIZ;
283 struct bgp *bgp;
284 int rc;
285 struct listnode *node, *nnode;
286
287 *bgp_inst = NULL;
288 name[0] = '\0';
289 rc = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, name, &name_len);
290 if (rc != 0) {
291#if defined(HAVE_CUMULUS)
1c50c1c0
QY
292 flog_err(EC_LIB_SOCKET,
293 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
294 safe_strerror(errno), sock);
d62a17ae 295 return -1;
b5826a12 296#endif
d62a17ae 297 }
298
299 if (!strlen(name)) {
300 *bgp_inst = bgp_get_default();
301 return 0; /* default instance. */
302 }
303
304 /* First try match to instance; if that fails, check for interfaces. */
305 bgp = bgp_lookup_by_name(name);
306 if (bgp) {
307 if (!bgp->vrf_id) // unexpected
308 return -1;
309 *bgp_inst = bgp;
310 return 0;
311 }
312
313 /* TODO - This will be optimized once interfaces move into the NS */
314 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
315 struct interface *ifp;
316
317 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
318 continue;
319
a36898e7 320 ifp = if_lookup_by_name(name, bgp->vrf_id);
d62a17ae 321 if (ifp) {
322 *bgp_inst = bgp;
323 return 0;
324 }
325 }
326
327 /* We didn't match to either an instance or an interface. */
328 return -1;
120c6587 329#endif
8dee0396 330}
331
c2d020ad
DS
332static void bgp_socket_set_buffer_size(const int fd)
333{
334 if (getsockopt_so_sendbuf(fd) < (int)bm->socket_buffer)
335 setsockopt_so_sendbuf(fd, bm->socket_buffer);
336 if (getsockopt_so_recvbuf(fd) < (int)bm->socket_buffer)
337 setsockopt_so_recvbuf(fd, bm->socket_buffer);
338}
339
718e3744 340/* Accept bgp connection. */
e6685141 341static void bgp_accept(struct event *thread)
718e3744 342{
d62a17ae 343 int bgp_sock;
344 int accept_sock;
345 union sockunion su;
e16d030c 346 struct bgp_listener *listener = EVENT_ARG(thread);
d62a17ae 347 struct peer *peer;
348 struct peer *peer1;
349 char buf[SU_ADDRSTRLEN];
350 struct bgp *bgp = NULL;
351
352 sockunion_init(&su);
353
2f9db11b
QY
354 bgp = bgp_lookup_by_name(listener->name);
355
d62a17ae 356 /* Register accept thread. */
e16d030c 357 accept_sock = EVENT_FD(thread);
d62a17ae 358 if (accept_sock < 0) {
2f9db11b
QY
359 flog_err_sys(EC_LIB_SOCKET,
360 "[Error] BGP accept socket fd is negative: %d",
09c866e3 361 accept_sock);
cc9f21da 362 return;
d62a17ae 363 }
61cf4b37 364
907a2395
DS
365 event_add_read(bm->master, bgp_accept, listener, accept_sock,
366 &listener->thread);
d62a17ae 367
368 /* Accept client connection. */
369 bgp_sock = sockunion_accept(accept_sock, &su);
2f9db11b 370 int save_errno = errno;
d62a17ae 371 if (bgp_sock < 0) {
2f9db11b
QY
372 if (save_errno == EINVAL) {
373 struct vrf *vrf =
374 bgp ? vrf_lookup_by_id(bgp->vrf_id) : NULL;
375
376 /*
377 * It appears that sometimes, when VRFs are deleted on
378 * the system, it takes a little while for us to get
379 * notified about that. In the meantime we endlessly
380 * loop on accept(), because the socket, having been
381 * bound to a now-deleted VRF device, is in some weird
382 * state which causes accept() to fail.
383 *
384 * To avoid this, if we see accept() fail with EINVAL,
385 * we cancel ourselves and trust that when the VRF
386 * deletion notification comes in the event handler for
387 * that will take care of cleaning us up.
388 */
389 flog_err_sys(
390 EC_LIB_SOCKET,
391 "[Error] accept() failed with error \"%s\" on BGP listener socket %d for BGP instance in VRF \"%s\"; refreshing socket",
392 safe_strerror(save_errno), accept_sock,
393 VRF_LOGNAME(vrf));
e16d030c 394 EVENT_OFF(listener->thread);
2f9db11b
QY
395 } else {
396 flog_err_sys(
397 EC_LIB_SOCKET,
398 "[Error] BGP socket accept failed (%s); retrying",
399 safe_strerror(save_errno));
400 }
cc9f21da 401 return;
d62a17ae 402 }
403 set_nonblocking(bgp_sock);
404
61cf4b37
PG
405 /* Obtain BGP instance this connection is meant for.
406 * - if it is a VRF netns sock, then BGP is in listener structure
407 * - otherwise, the bgp instance need to be demultiplexed
408 */
409 if (listener->bgp)
410 bgp = listener->bgp;
411 else if (bgp_get_instance_for_inc_conn(bgp_sock, &bgp)) {
d62a17ae 412 if (bgp_debug_neighbor_events(NULL))
413 zlog_debug(
414 "[Event] Could not get instance for incoming conn from %s",
415 inet_sutop(&su, buf));
416 close(bgp_sock);
cc9f21da 417 return;
d62a17ae 418 }
419
c2d020ad 420 bgp_socket_set_buffer_size(bgp_sock);
d62a17ae 421
d1adb448
PG
422 /* Set TCP keepalive when TCP keepalive is enabled */
423 bgp_update_setsockopt_tcp_keepalive(bgp, bgp_sock);
424
d62a17ae 425 /* Check remote IP address */
426 peer1 = peer_lookup(bgp, &su);
427
428 if (!peer1) {
429 peer1 = peer_lookup_dynamic_neighbor(bgp, &su);
430 if (peer1) {
431 /* Dynamic neighbor has been created, let it proceed */
432 peer1->fd = bgp_sock;
4ab46701
AR
433
434 /* Set the user configured MSS to TCP socket */
435 if (CHECK_FLAG(peer1->flags, PEER_FLAG_TCP_MSS))
436 sockopt_tcp_mss_set(bgp_sock, peer1->tcp_mss);
437
d62a17ae 438 bgp_fsm_change_status(peer1, Active);
e16d030c 439 EVENT_OFF(
d62a17ae 440 peer1->t_start); /* created in peer_create() */
441
6c537a18
DS
442 if (peer_active(peer1)) {
443 if (CHECK_FLAG(peer1->flags,
444 PEER_FLAG_TIMER_DELAYOPEN))
445 BGP_EVENT_ADD(
446 peer1,
447 TCP_connection_open_w_delay);
448 else
449 BGP_EVENT_ADD(peer1,
450 TCP_connection_open);
451 }
d62a17ae 452
cc9f21da 453 return;
d62a17ae 454 }
455 }
456
457 if (!peer1) {
458 if (bgp_debug_neighbor_events(NULL)) {
459 zlog_debug(
45069622
DS
460 "[Event] %s connection rejected(%s:%u:%s) - not configured and not valid for dynamic",
461 inet_sutop(&su, buf), bgp->name_pretty, bgp->as,
462 VRF_LOGNAME(vrf_lookup_by_id(bgp->vrf_id)));
d62a17ae 463 }
464 close(bgp_sock);
cc9f21da 465 return;
d62a17ae 466 }
467
cb9196e7
DS
468 if (CHECK_FLAG(peer1->flags, PEER_FLAG_SHUTDOWN)
469 || CHECK_FLAG(peer1->bgp->flags, BGP_FLAG_SHUTDOWN)) {
d62a17ae 470 if (bgp_debug_neighbor_events(peer1))
471 zlog_debug(
45069622
DS
472 "[Event] connection from %s rejected(%s:%u:%s) due to admin shutdown",
473 inet_sutop(&su, buf), bgp->name_pretty, bgp->as,
474 VRF_LOGNAME(vrf_lookup_by_id(bgp->vrf_id)));
d62a17ae 475 close(bgp_sock);
cc9f21da 476 return;
d62a17ae 477 }
478
479 /*
480 * Do not accept incoming connections in Clearing state. This can result
481 * in incorect state transitions - e.g., the connection goes back to
482 * Established and then the Clearing_Completed event is generated. Also,
483 * block incoming connection in Deleted state.
484 */
485 if (peer1->status == Clearing || peer1->status == Deleted) {
486 if (bgp_debug_neighbor_events(peer1))
487 zlog_debug(
488 "[Event] Closing incoming conn for %s (%p) state %d",
489 peer1->host, peer1, peer1->status);
490 close(bgp_sock);
cc9f21da 491 return;
d62a17ae 492 }
493
494 /* Check that at least one AF is activated for the peer. */
495 if (!peer_active(peer1)) {
496 if (bgp_debug_neighbor_events(peer1))
497 zlog_debug(
498 "%s - incoming conn rejected - no AF activated for peer",
499 peer1->host);
500 close(bgp_sock);
cc9f21da 501 return;
d62a17ae 502 }
503
d091d9ad
DA
504 /* Do not try to reconnect if the peer reached maximum
505 * prefixes, restart timer is still running or the peer
506 * is shutdown.
507 */
508 if (BGP_PEER_START_SUPPRESSED(peer1)) {
7c5062fb
DS
509 if (bgp_debug_neighbor_events(peer1)) {
510 if (peer1->shut_during_cfg)
511 zlog_debug(
512 "[Event] Incoming BGP connection rejected from %s due to configuration being currently read in",
513 peer1->host);
514 else
515 zlog_debug(
516 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
517 peer1->host);
518 }
497b686a 519 close(bgp_sock);
cc9f21da 520 return;
497b686a
MS
521 }
522
d62a17ae 523 if (bgp_debug_neighbor_events(peer1))
031c24f2 524 zlog_debug(
525 "[Event] connection from %s fd %d, active peer status %d fd %d",
526 inet_sutop(&su, buf), bgp_sock, peer1->status,
527 peer1->fd);
d62a17ae 528
529 if (peer1->doppelganger) {
530 /* We have an existing connection. Kill the existing one and run
531 with this one.
532 */
533 if (bgp_debug_neighbor_events(peer1))
534 zlog_debug(
3efd0893 535 "[Event] New active connection from peer %s, Killing previous active connection",
d62a17ae 536 peer1->host);
537 peer_delete(peer1->doppelganger);
538 }
539
540 if (bgp_set_socket_ttl(peer1, bgp_sock) < 0)
541 if (bgp_debug_neighbor_events(peer1))
542 zlog_debug(
543 "[Event] Unable to set min/max TTL on peer %s, Continuing",
544 peer1->host);
545
546 peer = peer_create(&su, peer1->conf_if, peer1->bgp, peer1->local_as,
de76ed8a 547 peer1->as, peer1->as_type, NULL, false, NULL);
d62a17ae 548
549 peer_xfer_config(peer, peer1);
d7b3cda6 550 bgp_peer_gr_flags_update(peer);
551
36235319
QY
552 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
553 peer->bgp->peer);
0f0444fb 554
d7b3cda6 555 if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) {
556
557 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
558
36235319 559 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
d7b3cda6 560 peer_nsf_stop(peer);
561 }
562 }
563
d62a17ae 564 peer->doppelganger = peer1;
565 peer1->doppelganger = peer;
566 peer->fd = bgp_sock;
0def0c9f
IR
567 frr_with_privs(&bgpd_privs) {
568 vrf_bind(peer->bgp->vrf_id, bgp_sock, bgp_get_bound_name(peer));
569 }
996319e6 570 bgp_peer_reg_with_nht(peer);
d62a17ae 571 bgp_fsm_change_status(peer, Active);
e16d030c 572 EVENT_OFF(peer->t_start); /* created in peer_create() */
d62a17ae 573
574 SET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
d62a17ae 575 /* Make dummy peer until read Open packet. */
feb17238 576 if (peer_established(peer1)
36235319 577 && CHECK_FLAG(peer1->sflags, PEER_STATUS_NSF_MODE)) {
d62a17ae 578 /* If we have an existing established connection with graceful
579 * restart
580 * capability announced with one or more address families, then
581 * drop
582 * existing established connection and move state to connect.
583 */
584 peer1->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
d7b3cda6 585
36235319
QY
586 if (CHECK_FLAG(peer1->flags, PEER_FLAG_GRACEFUL_RESTART)
587 || CHECK_FLAG(peer1->flags,
588 PEER_FLAG_GRACEFUL_RESTART_HELPER))
589 SET_FLAG(peer1->sflags, PEER_STATUS_NSF_WAIT);
d7b3cda6 590
d62a17ae 591 bgp_event_update(peer1, TCP_connection_closed);
592 }
593
594 if (peer_active(peer)) {
6c537a18
DS
595 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
596 BGP_EVENT_ADD(peer, TCP_connection_open_w_delay);
597 else
598 BGP_EVENT_ADD(peer, TCP_connection_open);
d62a17ae 599 }
600
dcd5ba55
DS
601 /*
602 * If we are doing nht for a peer that is v6 LL based
603 * massage the event system to make things happy
604 */
605 bgp_nht_interface_events(peer);
718e3744 606}
607
608/* BGP socket bind. */
97896a91 609static char *bgp_get_bound_name(struct peer *peer)
718e3744 610{
a2bce1c8
DS
611 if (!peer)
612 return NULL;
613
996c9314
LB
614 if ((peer->bgp->vrf_id == VRF_DEFAULT) && !peer->ifname
615 && !peer->conf_if)
97896a91
PG
616 return NULL;
617
d62a17ae 618 if (peer->su.sa.sa_family != AF_INET
619 && peer->su.sa.sa_family != AF_INET6)
97896a91 620 return NULL; // unexpected
d62a17ae 621
622 /* For IPv6 peering, interface (unnumbered or link-local with interface)
623 * takes precedence over VRF. For IPv4 peering, explicit interface or
624 * VRF are the situations to bind.
625 */
7224dcd8
IR
626 if (peer->su.sa.sa_family == AF_INET6 && peer->conf_if)
627 return peer->conf_if;
628
629 if (peer->ifname)
630 return peer->ifname;
631
632 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
633 return NULL;
d62a17ae 634
7224dcd8 635 return peer->bgp->name;
718e3744 636}
637
09f267ec 638int bgp_update_address(struct interface *ifp, const union sockunion *dst,
d62a17ae 639 union sockunion *addr)
718e3744 640{
d62a17ae 641 struct prefix *p, *sel, d;
642 struct connected *connected;
643 struct listnode *node;
644 int common;
645
0154d8ce
DS
646 if (!sockunion2hostprefix(dst, &d))
647 return 1;
648
d62a17ae 649 sel = NULL;
650 common = -1;
651
652 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
653 p = connected->address;
654 if (p->family != d.family)
655 continue;
656 if (prefix_common_bits(p, &d) > common) {
657 sel = p;
658 common = prefix_common_bits(sel, &d);
659 }
660 }
661
662 if (!sel)
663 return 1;
664
665 prefix2sockunion(sel, addr);
666 return 0;
718e3744 667}
668
669/* Update source selection. */
d62a17ae 670static int bgp_update_source(struct peer *peer)
718e3744 671{
d62a17ae 672 struct interface *ifp;
673 union sockunion addr;
674 int ret = 0;
718e3744 675
d62a17ae 676 sockunion_init(&addr);
dd793e4a 677
d62a17ae 678 /* Source is specified with interface name. */
679 if (peer->update_if) {
a36898e7 680 ifp = if_lookup_by_name(peer->update_if, peer->bgp->vrf_id);
d62a17ae 681 if (!ifp)
682 return -1;
718e3744 683
d62a17ae 684 if (bgp_update_address(ifp, &peer->su, &addr))
685 return -1;
718e3744 686
d62a17ae 687 ret = sockunion_bind(peer->fd, &addr, 0, &addr);
688 }
718e3744 689
d62a17ae 690 /* Source is specified with IP address. */
691 if (peer->update_source)
692 ret = sockunion_bind(peer->fd, peer->update_source, 0,
693 peer->update_source);
49067496 694
d62a17ae 695 return ret;
718e3744 696}
697
698/* BGP try to connect to the peer. */
d62a17ae 699int bgp_connect(struct peer *peer)
718e3744 700{
b750b0ba
QY
701 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
702 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
d62a17ae 703 ifindex_t ifindex = 0;
704
705 if (peer->conf_if && BGP_PEER_SU_UNSPEC(peer)) {
888e727c
DS
706 if (bgp_debug_neighbor_events(peer))
707 zlog_debug("Peer address not learnt: Returning from connect");
d62a17ae 708 return 0;
709 }
0cf6db21 710 frr_with_privs(&bgpd_privs) {
d62a17ae 711 /* Make socket for the peer. */
01b9e3fd
DL
712 peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
713 bgp_get_bound_name(peer));
714 }
888e727c 715 if (peer->fd < 0) {
c5fe9095 716 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
888e727c
DS
717 if (bgp_debug_neighbor_events(peer))
718 zlog_debug("%s: Failure to create socket for connection to %s, error received: %s(%d)",
719 __func__, peer->host, safe_strerror(errno),
720 errno);
d62a17ae 721 return -1;
888e727c 722 }
d62a17ae 723
724 set_nonblocking(peer->fd);
725
4ab46701
AR
726 /* Set the user configured MSS to TCP socket */
727 if (CHECK_FLAG(peer->flags, PEER_FLAG_TCP_MSS))
728 sockopt_tcp_mss_set(peer->fd, peer->tcp_mss);
729
c2d020ad 730 bgp_socket_set_buffer_size(peer->fd);
d62a17ae 731
d1adb448
PG
732 /* Set TCP keepalive when TCP keepalive is enabled */
733 bgp_update_setsockopt_tcp_keepalive(peer->bgp, peer->fd);
734
888e727c 735 if (bgp_set_socket_ttl(peer, peer->fd) < 0) {
c5fe9095 736 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
888e727c
DS
737 if (bgp_debug_neighbor_events(peer))
738 zlog_debug("%s: Failure to set socket ttl for connection to %s, error received: %s(%d)",
739 __func__, peer->host, safe_strerror(errno),
740 errno);
d1adb448 741
d62a17ae 742 return -1;
888e727c 743 }
d62a17ae 744
745 sockopt_reuseaddr(peer->fd);
746 sockopt_reuseport(peer->fd);
d62a17ae 747
1423c809 748#ifdef IPTOS_PREC_INTERNETCONTROL
0cf6db21 749 frr_with_privs(&bgpd_privs) {
01b9e3fd 750 if (sockunion_family(&peer->su) == AF_INET)
425bd64b 751 setsockopt_ipv4_tos(peer->fd, bm->tcp_dscp);
01b9e3fd 752 else if (sockunion_family(&peer->su) == AF_INET6)
425bd64b 753 setsockopt_ipv6_tclass(peer->fd, bm->tcp_dscp);
01b9e3fd 754 }
1423c809
SH
755#endif
756
9e7d9a61
QY
757 if (peer->password) {
758 uint16_t prefixlen = peer->su.sa.sa_family == AF_INET
936fbaef 759 ? IPV4_MAX_BITLEN
f4d81e55 760 : IPV6_MAX_BITLEN;
9e7d9a61 761
28040084
DA
762 if (!BGP_PEER_SU_UNSPEC(peer))
763 bgp_md5_set(peer);
764
9e7d9a61
QY
765 bgp_md5_set_connect(peer->fd, &peer->su, prefixlen,
766 peer->password);
767 }
718e3744 768
d62a17ae 769 /* Update source bind. */
770 if (bgp_update_source(peer) < 0) {
c5fe9095 771 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
d62a17ae 772 return connect_error;
773 }
718e3744 774
d62a17ae 775 if (peer->conf_if || peer->ifname)
776 ifindex = ifname2ifindex(peer->conf_if ? peer->conf_if
777 : peer->ifname,
778 peer->bgp->vrf_id);
718e3744 779
d62a17ae 780 if (bgp_debug_neighbor_events(peer))
781 zlog_debug("%s [Event] Connect start to %s fd %d", peer->host,
782 peer->host, peer->fd);
718e3744 783
d62a17ae 784 /* Connect to the remote peer. */
785 return sockunion_connect(peer->fd, &peer->su, htons(peer->port),
786 ifindex);
718e3744 787}
788
789/* After TCP connection is established. Get local address and port. */
d62a17ae 790int bgp_getsockname(struct peer *peer)
718e3744 791{
d62a17ae 792 if (peer->su_local) {
793 sockunion_free(peer->su_local);
794 peer->su_local = NULL;
795 }
796
797 if (peer->su_remote) {
798 sockunion_free(peer->su_remote);
799 peer->su_remote = NULL;
800 }
801
802 peer->su_local = sockunion_getsockname(peer->fd);
803 if (!peer->su_local)
804 return -1;
805 peer->su_remote = sockunion_getpeername(peer->fd);
806 if (!peer->su_remote)
807 return -1;
808
17cdd31e
DS
809 if (!bgp_zebra_nexthop_set(peer->su_local, peer->su_remote,
810 &peer->nexthop, peer)) {
50e049db
DS
811 flog_err(
812 EC_BGP_NH_UPD,
813 "%s: nexthop_set failed, resetting connection - intf %s",
814 peer->host,
815 peer->nexthop.ifp ? peer->nexthop.ifp->name
816 : "(Unknown)");
d62a17ae 817 return -1;
d62a17ae 818 }
d62a17ae 819 return 0;
718e3744 820}
821
d023aec4 822
61cf4b37
PG
823static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
824 struct bgp *bgp)
d023aec4 825{
d62a17ae 826 struct bgp_listener *listener;
827 int ret, en;
d023aec4 828
d62a17ae 829 sockopt_reuseaddr(sock);
830 sockopt_reuseport(sock);
d023aec4 831
0cf6db21 832 frr_with_privs(&bgpd_privs) {
5c88f19d 833
d023aec4 834#ifdef IPTOS_PREC_INTERNETCONTROL
01b9e3fd 835 if (sa->sa_family == AF_INET)
425bd64b 836 setsockopt_ipv4_tos(sock, bm->tcp_dscp);
01b9e3fd 837 else if (sa->sa_family == AF_INET6)
425bd64b 838 setsockopt_ipv6_tclass(sock, bm->tcp_dscp);
d023aec4
SH
839#endif
840
01b9e3fd 841 sockopt_v6only(sa->sa_family, sock);
d62a17ae 842
01b9e3fd
DL
843 ret = bind(sock, sa, salen);
844 en = errno;
845 }
d62a17ae 846
847 if (ret < 0) {
450971aa 848 flog_err_sys(EC_LIB_SOCKET, "bind: %s", safe_strerror(en));
d62a17ae 849 return ret;
850 }
851
48522088 852 ret = listen(sock, SOMAXCONN);
d62a17ae 853 if (ret < 0) {
1c50c1c0 854 flog_err_sys(EC_LIB_SOCKET, "listen: %s", safe_strerror(errno));
d62a17ae 855 return ret;
856 }
857
61cf4b37 858 listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
d62a17ae 859 listener->fd = sock;
2a0e69ae 860 listener->name = XSTRDUP(MTYPE_BGP_LISTENER, bgp->name);
61cf4b37 861
a4faae3a 862 /* this socket is in a vrf record bgp back pointer */
2c1eba8e 863 if (bgp->vrf_id != VRF_DEFAULT)
61cf4b37
PG
864 listener->bgp = bgp;
865
d62a17ae 866 memcpy(&listener->su, sa, salen);
907a2395
DS
867 event_add_read(bm->master, bgp_accept, listener, sock,
868 &listener->thread);
d62a17ae 869 listnode_add(bm->listen_sockets, listener);
870
871 return 0;
d023aec4
SH
872}
873
718e3744 874/* IPv6 supported version of BGP server socket setup. */
61cf4b37 875int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
718e3744 876{
d62a17ae 877 struct addrinfo *ainfo;
878 struct addrinfo *ainfo_save;
879 static const struct addrinfo req = {
880 .ai_family = AF_UNSPEC,
881 .ai_flags = AI_PASSIVE,
882 .ai_socktype = SOCK_STREAM,
883 };
884 int ret, count;
885 char port_str[BUFSIZ];
886
887 snprintf(port_str, sizeof(port_str), "%d", port);
888 port_str[sizeof(port_str) - 1] = '\0';
889
0cf6db21 890 frr_with_privs(&bgpd_privs) {
01b9e3fd
DL
891 ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
892 bgp->vrf_id);
893 }
d62a17ae 894 if (ret != 0) {
450971aa 895 flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s",
09c866e3 896 gai_strerror(ret));
d62a17ae 897 return -1;
898 }
0b014ea6 899 if (bgp_option_check(BGP_OPT_NO_ZEBRA) &&
449cff3e
A
900 bgp->vrf_id != VRF_DEFAULT) {
901 freeaddrinfo(ainfo_save);
0b014ea6 902 return -1;
449cff3e 903 }
d62a17ae 904 count = 0;
905 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) {
906 int sock;
907
908 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
909 continue;
910
0cf6db21 911 frr_with_privs(&bgpd_privs) {
01b9e3fd
DL
912 sock = vrf_socket(ainfo->ai_family,
913 ainfo->ai_socktype,
67684738 914 ainfo->ai_protocol,
2c1eba8e 915 bgp->vrf_id,
633fc9b1
DL
916 (bgp->inst_type
917 == BGP_INSTANCE_TYPE_VRF
918 ? bgp->name : NULL));
01b9e3fd 919 }
d62a17ae 920 if (sock < 0) {
450971aa 921 flog_err_sys(EC_LIB_SOCKET, "socket: %s",
09c866e3 922 safe_strerror(errno));
d62a17ae 923 continue;
924 }
925
926 /* if we intend to implement ttl-security, this socket needs
927 * ttl=255 */
928 sockopt_ttl(ainfo->ai_family, sock, MAXTTL);
929
996c9314
LB
930 ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen,
931 bgp);
d62a17ae 932 if (ret == 0)
933 ++count;
934 else
935 close(sock);
936 }
937 freeaddrinfo(ainfo_save);
06969768 938 if (count == 0 && bgp->inst_type != BGP_INSTANCE_TYPE_VRF) {
af4c2728 939 flog_err(
450971aa 940 EC_LIB_SOCKET,
996c9314
LB
941 "%s: no usable addresses please check other programs usage of specified port %d",
942 __func__, port);
450971aa 943 flog_err_sys(EC_LIB_SOCKET, "%s: Program cannot continue",
09c866e3 944 __func__);
b3b78f6e 945 exit(-1);
d62a17ae 946 }
947
948 return 0;
718e3744 949}
d023aec4 950
e5619c28
PG
951/* this function closes vrf socket
952 * this should be called only for vrf socket with netns backend
953 */
954void bgp_close_vrf_socket(struct bgp *bgp)
955{
956 struct listnode *node, *next;
957 struct bgp_listener *listener;
958
959 if (!bgp)
960 return;
961
962 if (bm->listen_sockets == NULL)
963 return;
964
965 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
966 if (listener->bgp == bgp) {
e16d030c 967 EVENT_OFF(listener->thread);
e5619c28
PG
968 close(listener->fd);
969 listnode_delete(bm->listen_sockets, listener);
2a0e69ae 970 XFREE(MTYPE_BGP_LISTENER, listener->name);
e5619c28
PG
971 XFREE(MTYPE_BGP_LISTENER, listener);
972 }
973 }
974}
975
976/* this function closes main socket
977 */
d62a17ae 978void bgp_close(void)
d023aec4 979{
d62a17ae 980 struct listnode *node, *next;
981 struct bgp_listener *listener;
982
983 if (bm->listen_sockets == NULL)
984 return;
985
986 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
e5619c28
PG
987 if (listener->bgp)
988 continue;
e16d030c 989 EVENT_OFF(listener->thread);
d62a17ae 990 close(listener->fd);
991 listnode_delete(bm->listen_sockets, listener);
2a0e69ae 992 XFREE(MTYPE_BGP_LISTENER, listener->name);
d62a17ae 993 XFREE(MTYPE_BGP_LISTENER, listener);
994 }
d023aec4 995}