]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_network.c
Merge pull request #12780 from opensourcerouting/spdx-license-id
[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
8#include "thread.h"
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. */
cc9f21da 341static void bgp_accept(struct thread *thread)
718e3744 342{
d62a17ae 343 int bgp_sock;
344 int accept_sock;
345 union sockunion su;
346 struct bgp_listener *listener = THREAD_ARG(thread);
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. */
357 accept_sock = THREAD_FD(thread);
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
d62a17ae 365 thread_add_read(bm->master, bgp_accept, listener, accept_sock,
366 &listener->thread);
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));
394 THREAD_OFF(listener->thread);
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);
fa5806c3 439 THREAD_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)) {
497b686a
MS
509 if (bgp_debug_neighbor_events(peer1))
510 zlog_debug(
3efd0893 511 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
497b686a
MS
512 peer1->host);
513 close(bgp_sock);
cc9f21da 514 return;
497b686a
MS
515 }
516
d62a17ae 517 if (bgp_debug_neighbor_events(peer1))
031c24f2 518 zlog_debug(
519 "[Event] connection from %s fd %d, active peer status %d fd %d",
520 inet_sutop(&su, buf), bgp_sock, peer1->status,
521 peer1->fd);
d62a17ae 522
523 if (peer1->doppelganger) {
524 /* We have an existing connection. Kill the existing one and run
525 with this one.
526 */
527 if (bgp_debug_neighbor_events(peer1))
528 zlog_debug(
3efd0893 529 "[Event] New active connection from peer %s, Killing previous active connection",
d62a17ae 530 peer1->host);
531 peer_delete(peer1->doppelganger);
532 }
533
534 if (bgp_set_socket_ttl(peer1, bgp_sock) < 0)
535 if (bgp_debug_neighbor_events(peer1))
536 zlog_debug(
537 "[Event] Unable to set min/max TTL on peer %s, Continuing",
538 peer1->host);
539
540 peer = peer_create(&su, peer1->conf_if, peer1->bgp, peer1->local_as,
534db980 541 peer1->as, peer1->as_type, NULL, false);
d62a17ae 542
543 peer_xfer_config(peer, peer1);
d7b3cda6 544 bgp_peer_gr_flags_update(peer);
545
36235319
QY
546 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
547 peer->bgp->peer);
0f0444fb 548
d7b3cda6 549 if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) {
550
551 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
552
36235319 553 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
d7b3cda6 554 peer_nsf_stop(peer);
555 }
556 }
557
d62a17ae 558 peer->doppelganger = peer1;
559 peer1->doppelganger = peer;
560 peer->fd = bgp_sock;
0def0c9f
IR
561 frr_with_privs(&bgpd_privs) {
562 vrf_bind(peer->bgp->vrf_id, bgp_sock, bgp_get_bound_name(peer));
563 }
996319e6 564 bgp_peer_reg_with_nht(peer);
d62a17ae 565 bgp_fsm_change_status(peer, Active);
fa5806c3 566 THREAD_OFF(peer->t_start); /* created in peer_create() */
d62a17ae 567
568 SET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
d62a17ae 569 /* Make dummy peer until read Open packet. */
feb17238 570 if (peer_established(peer1)
36235319 571 && CHECK_FLAG(peer1->sflags, PEER_STATUS_NSF_MODE)) {
d62a17ae 572 /* If we have an existing established connection with graceful
573 * restart
574 * capability announced with one or more address families, then
575 * drop
576 * existing established connection and move state to connect.
577 */
578 peer1->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
d7b3cda6 579
36235319
QY
580 if (CHECK_FLAG(peer1->flags, PEER_FLAG_GRACEFUL_RESTART)
581 || CHECK_FLAG(peer1->flags,
582 PEER_FLAG_GRACEFUL_RESTART_HELPER))
583 SET_FLAG(peer1->sflags, PEER_STATUS_NSF_WAIT);
d7b3cda6 584
d62a17ae 585 bgp_event_update(peer1, TCP_connection_closed);
586 }
587
588 if (peer_active(peer)) {
6c537a18
DS
589 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
590 BGP_EVENT_ADD(peer, TCP_connection_open_w_delay);
591 else
592 BGP_EVENT_ADD(peer, TCP_connection_open);
d62a17ae 593 }
594
dcd5ba55
DS
595 /*
596 * If we are doing nht for a peer that is v6 LL based
597 * massage the event system to make things happy
598 */
599 bgp_nht_interface_events(peer);
718e3744 600}
601
602/* BGP socket bind. */
97896a91 603static char *bgp_get_bound_name(struct peer *peer)
718e3744 604{
a2bce1c8
DS
605 if (!peer)
606 return NULL;
607
996c9314
LB
608 if ((peer->bgp->vrf_id == VRF_DEFAULT) && !peer->ifname
609 && !peer->conf_if)
97896a91
PG
610 return NULL;
611
d62a17ae 612 if (peer->su.sa.sa_family != AF_INET
613 && peer->su.sa.sa_family != AF_INET6)
97896a91 614 return NULL; // unexpected
d62a17ae 615
616 /* For IPv6 peering, interface (unnumbered or link-local with interface)
617 * takes precedence over VRF. For IPv4 peering, explicit interface or
618 * VRF are the situations to bind.
619 */
7224dcd8
IR
620 if (peer->su.sa.sa_family == AF_INET6 && peer->conf_if)
621 return peer->conf_if;
622
623 if (peer->ifname)
624 return peer->ifname;
625
626 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
627 return NULL;
d62a17ae 628
7224dcd8 629 return peer->bgp->name;
718e3744 630}
631
09f267ec 632int bgp_update_address(struct interface *ifp, const union sockunion *dst,
d62a17ae 633 union sockunion *addr)
718e3744 634{
d62a17ae 635 struct prefix *p, *sel, d;
636 struct connected *connected;
637 struct listnode *node;
638 int common;
639
0154d8ce
DS
640 if (!sockunion2hostprefix(dst, &d))
641 return 1;
642
d62a17ae 643 sel = NULL;
644 common = -1;
645
646 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
647 p = connected->address;
648 if (p->family != d.family)
649 continue;
650 if (prefix_common_bits(p, &d) > common) {
651 sel = p;
652 common = prefix_common_bits(sel, &d);
653 }
654 }
655
656 if (!sel)
657 return 1;
658
659 prefix2sockunion(sel, addr);
660 return 0;
718e3744 661}
662
663/* Update source selection. */
d62a17ae 664static int bgp_update_source(struct peer *peer)
718e3744 665{
d62a17ae 666 struct interface *ifp;
667 union sockunion addr;
668 int ret = 0;
718e3744 669
d62a17ae 670 sockunion_init(&addr);
dd793e4a 671
d62a17ae 672 /* Source is specified with interface name. */
673 if (peer->update_if) {
a36898e7 674 ifp = if_lookup_by_name(peer->update_if, peer->bgp->vrf_id);
d62a17ae 675 if (!ifp)
676 return -1;
718e3744 677
d62a17ae 678 if (bgp_update_address(ifp, &peer->su, &addr))
679 return -1;
718e3744 680
d62a17ae 681 ret = sockunion_bind(peer->fd, &addr, 0, &addr);
682 }
718e3744 683
d62a17ae 684 /* Source is specified with IP address. */
685 if (peer->update_source)
686 ret = sockunion_bind(peer->fd, peer->update_source, 0,
687 peer->update_source);
49067496 688
d62a17ae 689 return ret;
718e3744 690}
691
692/* BGP try to connect to the peer. */
d62a17ae 693int bgp_connect(struct peer *peer)
718e3744 694{
b750b0ba
QY
695 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
696 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
d62a17ae 697 ifindex_t ifindex = 0;
698
699 if (peer->conf_if && BGP_PEER_SU_UNSPEC(peer)) {
888e727c
DS
700 if (bgp_debug_neighbor_events(peer))
701 zlog_debug("Peer address not learnt: Returning from connect");
d62a17ae 702 return 0;
703 }
0cf6db21 704 frr_with_privs(&bgpd_privs) {
d62a17ae 705 /* Make socket for the peer. */
01b9e3fd
DL
706 peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
707 bgp_get_bound_name(peer));
708 }
888e727c 709 if (peer->fd < 0) {
c5fe9095 710 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
888e727c
DS
711 if (bgp_debug_neighbor_events(peer))
712 zlog_debug("%s: Failure to create socket for connection to %s, error received: %s(%d)",
713 __func__, peer->host, safe_strerror(errno),
714 errno);
d62a17ae 715 return -1;
888e727c 716 }
d62a17ae 717
718 set_nonblocking(peer->fd);
719
4ab46701
AR
720 /* Set the user configured MSS to TCP socket */
721 if (CHECK_FLAG(peer->flags, PEER_FLAG_TCP_MSS))
722 sockopt_tcp_mss_set(peer->fd, peer->tcp_mss);
723
c2d020ad 724 bgp_socket_set_buffer_size(peer->fd);
d62a17ae 725
d1adb448
PG
726 /* Set TCP keepalive when TCP keepalive is enabled */
727 bgp_update_setsockopt_tcp_keepalive(peer->bgp, peer->fd);
728
888e727c 729 if (bgp_set_socket_ttl(peer, peer->fd) < 0) {
c5fe9095 730 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
888e727c
DS
731 if (bgp_debug_neighbor_events(peer))
732 zlog_debug("%s: Failure to set socket ttl for connection to %s, error received: %s(%d)",
733 __func__, peer->host, safe_strerror(errno),
734 errno);
d1adb448 735
d62a17ae 736 return -1;
888e727c 737 }
d62a17ae 738
739 sockopt_reuseaddr(peer->fd);
740 sockopt_reuseport(peer->fd);
d62a17ae 741
1423c809 742#ifdef IPTOS_PREC_INTERNETCONTROL
0cf6db21 743 frr_with_privs(&bgpd_privs) {
01b9e3fd 744 if (sockunion_family(&peer->su) == AF_INET)
425bd64b 745 setsockopt_ipv4_tos(peer->fd, bm->tcp_dscp);
01b9e3fd 746 else if (sockunion_family(&peer->su) == AF_INET6)
425bd64b 747 setsockopt_ipv6_tclass(peer->fd, bm->tcp_dscp);
01b9e3fd 748 }
1423c809
SH
749#endif
750
9e7d9a61
QY
751 if (peer->password) {
752 uint16_t prefixlen = peer->su.sa.sa_family == AF_INET
936fbaef 753 ? IPV4_MAX_BITLEN
f4d81e55 754 : IPV6_MAX_BITLEN;
9e7d9a61
QY
755
756 bgp_md5_set_connect(peer->fd, &peer->su, prefixlen,
757 peer->password);
758 }
718e3744 759
d62a17ae 760 /* Update source bind. */
761 if (bgp_update_source(peer) < 0) {
c5fe9095 762 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
d62a17ae 763 return connect_error;
764 }
718e3744 765
d62a17ae 766 if (peer->conf_if || peer->ifname)
767 ifindex = ifname2ifindex(peer->conf_if ? peer->conf_if
768 : peer->ifname,
769 peer->bgp->vrf_id);
718e3744 770
d62a17ae 771 if (bgp_debug_neighbor_events(peer))
772 zlog_debug("%s [Event] Connect start to %s fd %d", peer->host,
773 peer->host, peer->fd);
718e3744 774
d62a17ae 775 /* Connect to the remote peer. */
776 return sockunion_connect(peer->fd, &peer->su, htons(peer->port),
777 ifindex);
718e3744 778}
779
780/* After TCP connection is established. Get local address and port. */
d62a17ae 781int bgp_getsockname(struct peer *peer)
718e3744 782{
d62a17ae 783 if (peer->su_local) {
784 sockunion_free(peer->su_local);
785 peer->su_local = NULL;
786 }
787
788 if (peer->su_remote) {
789 sockunion_free(peer->su_remote);
790 peer->su_remote = NULL;
791 }
792
793 peer->su_local = sockunion_getsockname(peer->fd);
794 if (!peer->su_local)
795 return -1;
796 peer->su_remote = sockunion_getpeername(peer->fd);
797 if (!peer->su_remote)
798 return -1;
799
17cdd31e
DS
800 if (!bgp_zebra_nexthop_set(peer->su_local, peer->su_remote,
801 &peer->nexthop, peer)) {
2564f080 802 flog_err(EC_BGP_NH_UPD,
17cdd31e
DS
803 "%s: nexthop_set failed, resetting connection - intf %p",
804 peer->host, peer->nexthop.ifp);
d62a17ae 805 return -1;
d62a17ae 806 }
d62a17ae 807 return 0;
718e3744 808}
809
d023aec4 810
61cf4b37
PG
811static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
812 struct bgp *bgp)
d023aec4 813{
d62a17ae 814 struct bgp_listener *listener;
815 int ret, en;
d023aec4 816
d62a17ae 817 sockopt_reuseaddr(sock);
818 sockopt_reuseport(sock);
d023aec4 819
0cf6db21 820 frr_with_privs(&bgpd_privs) {
5c88f19d 821
d023aec4 822#ifdef IPTOS_PREC_INTERNETCONTROL
01b9e3fd 823 if (sa->sa_family == AF_INET)
425bd64b 824 setsockopt_ipv4_tos(sock, bm->tcp_dscp);
01b9e3fd 825 else if (sa->sa_family == AF_INET6)
425bd64b 826 setsockopt_ipv6_tclass(sock, bm->tcp_dscp);
d023aec4
SH
827#endif
828
01b9e3fd 829 sockopt_v6only(sa->sa_family, sock);
d62a17ae 830
01b9e3fd
DL
831 ret = bind(sock, sa, salen);
832 en = errno;
833 }
d62a17ae 834
835 if (ret < 0) {
450971aa 836 flog_err_sys(EC_LIB_SOCKET, "bind: %s", safe_strerror(en));
d62a17ae 837 return ret;
838 }
839
48522088 840 ret = listen(sock, SOMAXCONN);
d62a17ae 841 if (ret < 0) {
1c50c1c0 842 flog_err_sys(EC_LIB_SOCKET, "listen: %s", safe_strerror(errno));
d62a17ae 843 return ret;
844 }
845
61cf4b37 846 listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
d62a17ae 847 listener->fd = sock;
2a0e69ae 848 listener->name = XSTRDUP(MTYPE_BGP_LISTENER, bgp->name);
61cf4b37 849
a4faae3a 850 /* this socket is in a vrf record bgp back pointer */
2c1eba8e 851 if (bgp->vrf_id != VRF_DEFAULT)
61cf4b37
PG
852 listener->bgp = bgp;
853
d62a17ae 854 memcpy(&listener->su, sa, salen);
d62a17ae 855 thread_add_read(bm->master, bgp_accept, listener, sock,
856 &listener->thread);
857 listnode_add(bm->listen_sockets, listener);
858
859 return 0;
d023aec4
SH
860}
861
718e3744 862/* IPv6 supported version of BGP server socket setup. */
61cf4b37 863int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
718e3744 864{
d62a17ae 865 struct addrinfo *ainfo;
866 struct addrinfo *ainfo_save;
867 static const struct addrinfo req = {
868 .ai_family = AF_UNSPEC,
869 .ai_flags = AI_PASSIVE,
870 .ai_socktype = SOCK_STREAM,
871 };
872 int ret, count;
873 char port_str[BUFSIZ];
874
875 snprintf(port_str, sizeof(port_str), "%d", port);
876 port_str[sizeof(port_str) - 1] = '\0';
877
0cf6db21 878 frr_with_privs(&bgpd_privs) {
01b9e3fd
DL
879 ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
880 bgp->vrf_id);
881 }
d62a17ae 882 if (ret != 0) {
450971aa 883 flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s",
09c866e3 884 gai_strerror(ret));
d62a17ae 885 return -1;
886 }
0b014ea6 887 if (bgp_option_check(BGP_OPT_NO_ZEBRA) &&
449cff3e
A
888 bgp->vrf_id != VRF_DEFAULT) {
889 freeaddrinfo(ainfo_save);
0b014ea6 890 return -1;
449cff3e 891 }
d62a17ae 892 count = 0;
893 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) {
894 int sock;
895
896 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
897 continue;
898
0cf6db21 899 frr_with_privs(&bgpd_privs) {
01b9e3fd
DL
900 sock = vrf_socket(ainfo->ai_family,
901 ainfo->ai_socktype,
67684738 902 ainfo->ai_protocol,
2c1eba8e 903 bgp->vrf_id,
633fc9b1
DL
904 (bgp->inst_type
905 == BGP_INSTANCE_TYPE_VRF
906 ? bgp->name : NULL));
01b9e3fd 907 }
d62a17ae 908 if (sock < 0) {
450971aa 909 flog_err_sys(EC_LIB_SOCKET, "socket: %s",
09c866e3 910 safe_strerror(errno));
d62a17ae 911 continue;
912 }
913
914 /* if we intend to implement ttl-security, this socket needs
915 * ttl=255 */
916 sockopt_ttl(ainfo->ai_family, sock, MAXTTL);
917
996c9314
LB
918 ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen,
919 bgp);
d62a17ae 920 if (ret == 0)
921 ++count;
922 else
923 close(sock);
924 }
925 freeaddrinfo(ainfo_save);
06969768 926 if (count == 0 && bgp->inst_type != BGP_INSTANCE_TYPE_VRF) {
af4c2728 927 flog_err(
450971aa 928 EC_LIB_SOCKET,
996c9314
LB
929 "%s: no usable addresses please check other programs usage of specified port %d",
930 __func__, port);
450971aa 931 flog_err_sys(EC_LIB_SOCKET, "%s: Program cannot continue",
09c866e3 932 __func__);
b3b78f6e 933 exit(-1);
d62a17ae 934 }
935
936 return 0;
718e3744 937}
d023aec4 938
e5619c28
PG
939/* this function closes vrf socket
940 * this should be called only for vrf socket with netns backend
941 */
942void bgp_close_vrf_socket(struct bgp *bgp)
943{
944 struct listnode *node, *next;
945 struct bgp_listener *listener;
946
947 if (!bgp)
948 return;
949
950 if (bm->listen_sockets == NULL)
951 return;
952
953 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
954 if (listener->bgp == bgp) {
2f9db11b 955 THREAD_OFF(listener->thread);
e5619c28
PG
956 close(listener->fd);
957 listnode_delete(bm->listen_sockets, listener);
2a0e69ae 958 XFREE(MTYPE_BGP_LISTENER, listener->name);
e5619c28
PG
959 XFREE(MTYPE_BGP_LISTENER, listener);
960 }
961 }
962}
963
964/* this function closes main socket
965 */
d62a17ae 966void bgp_close(void)
d023aec4 967{
d62a17ae 968 struct listnode *node, *next;
969 struct bgp_listener *listener;
970
971 if (bm->listen_sockets == NULL)
972 return;
973
974 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
e5619c28
PG
975 if (listener->bgp)
976 continue;
e2d409a8 977 THREAD_OFF(listener->thread);
d62a17ae 978 close(listener->fd);
979 listnode_delete(bm->listen_sockets, listener);
2a0e69ae 980 XFREE(MTYPE_BGP_LISTENER, listener->name);
d62a17ae 981 XFREE(MTYPE_BGP_LISTENER, listener);
982 }
d023aec4 983}