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