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