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