]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_network.c
*: Change thread->func to return void instead of int
[mirror_frr.git] / bgpd / bgp_network.c
1 /* BGP network related fucntions
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 */
20
21 #include <zebra.h>
22
23 #include "thread.h"
24 #include "sockunion.h"
25 #include "sockopt.h"
26 #include "memory.h"
27 #include "log.h"
28 #include "if.h"
29 #include "prefix.h"
30 #include "command.h"
31 #include "privs.h"
32 #include "linklist.h"
33 #include "network.h"
34 #include "queue.h"
35 #include "hash.h"
36 #include "filter.h"
37 #include "ns.h"
38 #include "lib_errors.h"
39 #include "nexthop.h"
40
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_open.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_attr.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_network.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nht.h"
50
51 extern struct zebra_privs_t bgpd_privs;
52
53 static char *bgp_get_bound_name(struct peer *peer);
54
55 void 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
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 */
76 static int bgp_md5_set_socket(int socket, union sockunion *su,
77 uint16_t prefixlen, const char *password)
78 {
79 int ret = -1;
80 int en = ENOSYS;
81 #if HAVE_DECL_TCP_MD5SIG
82 union sockunion su2;
83 #endif /* HAVE_TCP_MD5SIG */
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;
94
95 /* For addresses, use the non-extended signature functionality */
96 if ((su2.sa.sa_family == AF_INET && prefixlen == IPV4_MAX_BITLEN)
97 || (su2.sa.sa_family == AF_INET6 && prefixlen == IPV6_MAX_BITLEN))
98 ret = sockopt_tcp_signature(socket, &su2, password);
99 else
100 ret = sockopt_tcp_signature_ext(socket, &su2, prefixlen,
101 password);
102 en = errno;
103 #endif /* HAVE_TCP_MD5SIG */
104
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 }
123
124 return ret;
125 }
126
127 /* Helper for bgp_connect */
128 static int bgp_md5_set_connect(int socket, union sockunion *su,
129 uint16_t prefixlen, const char *password)
130 {
131 int ret = -1;
132
133 #if HAVE_DECL_TCP_MD5SIG
134 frr_with_privs(&bgpd_privs) {
135 ret = bgp_md5_set_socket(socket, su, prefixlen, password);
136 }
137 #endif /* HAVE_TCP_MD5SIG */
138
139 return ret;
140 }
141
142 static int bgp_md5_set_password(struct peer *peer, const char *password)
143 {
144 struct listnode *node;
145 int ret = 0;
146 struct bgp_listener *listener;
147
148 /*
149 * Set or unset the password on the listen socket(s). Outbound
150 * connections are taken care of in bgp_connect() below.
151 */
152 frr_with_privs(&bgpd_privs) {
153 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
154 if (listener->su.sa.sa_family ==
155 peer->su.sa.sa_family) {
156 uint16_t prefixlen =
157 peer->su.sa.sa_family == AF_INET
158 ? IPV4_MAX_BITLEN
159 : IPV6_MAX_BITLEN;
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) {
168 if (peer->bgp->vrf_id != VRF_DEFAULT)
169 continue;
170 } else if (listener->bgp != peer->bgp)
171 continue;
172
173 ret = bgp_md5_set_socket(listener->fd,
174 &peer->su, prefixlen,
175 password);
176 break;
177 }
178 }
179 return ret;
180 }
181
182 int bgp_md5_set_prefix(struct bgp *bgp, struct prefix *p, const char *password)
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). */
190 frr_with_privs(&bgpd_privs) {
191 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
192 if (listener->su.sa.sa_family == p->family
193 && ((bgp->vrf_id == VRF_DEFAULT)
194 || (listener->bgp == bgp))) {
195 prefix2sockunion(p, &su);
196 ret = bgp_md5_set_socket(listener->fd, &su,
197 p->prefixlen,
198 password);
199 break;
200 }
201 }
202
203 return ret;
204 }
205
206 int bgp_md5_unset_prefix(struct bgp *bgp, struct prefix *p)
207 {
208 return bgp_md5_set_prefix(bgp, p, NULL);
209 }
210
211 int bgp_md5_set(struct peer *peer)
212 {
213 /* Set the password from listen socket. */
214 return bgp_md5_set_password(peer, peer->password);
215 }
216
217 int bgp_md5_unset(struct peer *peer)
218 {
219 /* Unset the password from listen socket. */
220 return bgp_md5_set_password(peer, NULL);
221 }
222
223 int bgp_set_socket_ttl(struct peer *peer, int bgp_sock)
224 {
225 int ret = 0;
226
227 /* In case of peer is EBGP, we should set TTL for this connection. */
228 if (!peer->gtsm_hops && (peer_sort_lookup(peer) == BGP_PEER_EBGP)) {
229 ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, peer->ttl);
230 if (ret) {
231 flog_err(
232 EC_LIB_SOCKET,
233 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
234 __func__, &peer->remote_id, errno);
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) {
244 flog_err(
245 EC_LIB_SOCKET,
246 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
247 __func__, &peer->remote_id, errno);
248 return ret;
249 }
250 ret = sockopt_minttl(peer->su.sa.sa_family, bgp_sock,
251 MAXTTL + 1 - peer->gtsm_hops);
252 if (ret) {
253 flog_err(
254 EC_LIB_SOCKET,
255 "%s: Can't set MinTTL on peer (rtrid %pI4) socket, err = %d",
256 __func__, &peer->remote_id, errno);
257 return ret;
258 }
259 }
260
261 return ret;
262 }
263
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 */
271 static int bgp_get_instance_for_inc_conn(int sock, struct bgp **bgp_inst)
272 {
273 #ifndef SO_BINDTODEVICE
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;
283
284 #else
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)
296 flog_err(EC_LIB_SOCKET,
297 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
298 safe_strerror(errno), sock);
299 return -1;
300 #endif
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
324 ifp = if_lookup_by_name(name, bgp->vrf_id);
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;
333 #endif
334 }
335
336 static 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
344 /* Accept bgp connection. */
345 static void bgp_accept(struct thread *thread)
346 {
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
358 bgp = bgp_lookup_by_name(listener->name);
359
360 /* Register accept thread. */
361 accept_sock = THREAD_FD(thread);
362 if (accept_sock < 0) {
363 flog_err_sys(EC_LIB_SOCKET,
364 "[Error] BGP accept socket fd is negative: %d",
365 accept_sock);
366 return;
367 }
368
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);
374 int save_errno = errno;
375 if (bgp_sock < 0) {
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 }
405 return;
406 }
407 set_nonblocking(bgp_sock);
408
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)) {
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);
421 return;
422 }
423
424 bgp_socket_set_buffer_size(bgp_sock);
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;
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
439 bgp_fsm_change_status(peer1, Active);
440 BGP_TIMER_OFF(
441 peer1->t_start); /* created in peer_create() */
442
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 }
453
454 return;
455 }
456 }
457
458 if (!peer1) {
459 if (bgp_debug_neighbor_events(NULL)) {
460 zlog_debug(
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)));
464 }
465 close(bgp_sock);
466 return;
467 }
468
469 if (CHECK_FLAG(peer1->flags, PEER_FLAG_SHUTDOWN)
470 || CHECK_FLAG(peer1->bgp->flags, BGP_FLAG_SHUTDOWN)) {
471 if (bgp_debug_neighbor_events(peer1))
472 zlog_debug(
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)));
476 close(bgp_sock);
477 return;
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);
492 return;
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);
502 return;
503 }
504
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)) {
510 if (bgp_debug_neighbor_events(peer1))
511 zlog_debug(
512 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
513 peer1->host);
514 close(bgp_sock);
515 return;
516 }
517
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(
528 "[Event] New active connection from peer %s, Killing previous active connection",
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,
540 peer1->as, peer1->as_type, NULL);
541 hash_release(peer->bgp->peerhash, peer);
542 hash_get(peer->bgp->peerhash, peer, hash_alloc_intern);
543
544 peer_xfer_config(peer, peer1);
545 bgp_peer_gr_flags_update(peer);
546
547 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
548 peer->bgp->peer);
549
550 if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) {
551
552 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
553
554 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
555 peer_nsf_stop(peer);
556 }
557 }
558
559 UNSET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
560
561 peer->doppelganger = peer1;
562 peer1->doppelganger = peer;
563 peer->fd = bgp_sock;
564 frr_with_privs(&bgpd_privs) {
565 vrf_bind(peer->bgp->vrf_id, bgp_sock, bgp_get_bound_name(peer));
566 }
567 bgp_peer_reg_with_nht(peer);
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);
572 /* Make dummy peer until read Open packet. */
573 if (peer_established(peer1)
574 && CHECK_FLAG(peer1->sflags, PEER_STATUS_NSF_MODE)) {
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;
582
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);
587
588 bgp_event_update(peer1, TCP_connection_closed);
589 }
590
591 if (peer_active(peer)) {
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);
596 }
597
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);
603 }
604
605 /* BGP socket bind. */
606 static char *bgp_get_bound_name(struct peer *peer)
607 {
608 if (!peer)
609 return NULL;
610
611 if ((peer->bgp->vrf_id == VRF_DEFAULT) && !peer->ifname
612 && !peer->conf_if)
613 return NULL;
614
615 if (peer->su.sa.sa_family != AF_INET
616 && peer->su.sa.sa_family != AF_INET6)
617 return NULL; // unexpected
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 */
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;
631
632 return peer->bgp->name;
633 }
634
635 static int bgp_update_address(struct interface *ifp, const union sockunion *dst,
636 union sockunion *addr)
637 {
638 struct prefix *p, *sel, d;
639 struct connected *connected;
640 struct listnode *node;
641 int common;
642
643 if (!sockunion2hostprefix(dst, &d))
644 return 1;
645
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;
664 }
665
666 /* Update source selection. */
667 static int bgp_update_source(struct peer *peer)
668 {
669 struct interface *ifp;
670 union sockunion addr;
671 int ret = 0;
672
673 sockunion_init(&addr);
674
675 /* Source is specified with interface name. */
676 if (peer->update_if) {
677 ifp = if_lookup_by_name(peer->update_if, peer->bgp->vrf_id);
678 if (!ifp)
679 return -1;
680
681 if (bgp_update_address(ifp, &peer->su, &addr))
682 return -1;
683
684 ret = sockunion_bind(peer->fd, &addr, 0, &addr);
685 }
686
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);
691
692 return ret;
693 }
694
695 /* BGP try to connect to the peer. */
696 int bgp_connect(struct peer *peer)
697 {
698 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
699 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
700 ifindex_t ifindex = 0;
701
702 if (peer->conf_if && BGP_PEER_SU_UNSPEC(peer)) {
703 if (bgp_debug_neighbor_events(peer))
704 zlog_debug("Peer address not learnt: Returning from connect");
705 return 0;
706 }
707 frr_with_privs(&bgpd_privs) {
708 /* Make socket for the peer. */
709 peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
710 bgp_get_bound_name(peer));
711 }
712 if (peer->fd < 0) {
713 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
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);
718 return -1;
719 }
720
721 set_nonblocking(peer->fd);
722
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
727 bgp_socket_set_buffer_size(peer->fd);
728
729 if (bgp_set_socket_ttl(peer, peer->fd) < 0) {
730 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
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);
735 return -1;
736 }
737
738 sockopt_reuseaddr(peer->fd);
739 sockopt_reuseport(peer->fd);
740
741 #ifdef IPTOS_PREC_INTERNETCONTROL
742 frr_with_privs(&bgpd_privs) {
743 if (sockunion_family(&peer->su) == AF_INET)
744 setsockopt_ipv4_tos(peer->fd,
745 IPTOS_PREC_INTERNETCONTROL);
746 else if (sockunion_family(&peer->su) == AF_INET6)
747 setsockopt_ipv6_tclass(peer->fd,
748 IPTOS_PREC_INTERNETCONTROL);
749 }
750 #endif
751
752 if (peer->password) {
753 uint16_t prefixlen = peer->su.sa.sa_family == AF_INET
754 ? IPV4_MAX_BITLEN
755 : IPV6_MAX_BITLEN;
756
757 bgp_md5_set_connect(peer->fd, &peer->su, prefixlen,
758 peer->password);
759 }
760
761 /* Update source bind. */
762 if (bgp_update_source(peer) < 0) {
763 peer->last_reset = PEER_DOWN_SOCKET_ERROR;
764 return connect_error;
765 }
766
767 if (peer->conf_if || peer->ifname)
768 ifindex = ifname2ifindex(peer->conf_if ? peer->conf_if
769 : peer->ifname,
770 peer->bgp->vrf_id);
771
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);
775
776 /* Connect to the remote peer. */
777 return sockunion_connect(peer->fd, &peer->su, htons(peer->port),
778 ifindex);
779 }
780
781 /* After TCP connection is established. Get local address and port. */
782 int bgp_getsockname(struct peer *peer)
783 {
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
801 if (!bgp_zebra_nexthop_set(peer->su_local, peer->su_remote,
802 &peer->nexthop, peer)) {
803 flog_err(EC_BGP_NH_UPD,
804 "%s: nexthop_set failed, resetting connection - intf %p",
805 peer->host, peer->nexthop.ifp);
806 return -1;
807 }
808 return 0;
809 }
810
811
812 static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
813 struct bgp *bgp)
814 {
815 struct bgp_listener *listener;
816 int ret, en;
817
818 sockopt_reuseaddr(sock);
819 sockopt_reuseport(sock);
820
821 frr_with_privs(&bgpd_privs) {
822
823 #ifdef IPTOS_PREC_INTERNETCONTROL
824 if (sa->sa_family == AF_INET)
825 setsockopt_ipv4_tos(sock, IPTOS_PREC_INTERNETCONTROL);
826 else if (sa->sa_family == AF_INET6)
827 setsockopt_ipv6_tclass(sock,
828 IPTOS_PREC_INTERNETCONTROL);
829 #endif
830
831 sockopt_v6only(sa->sa_family, sock);
832
833 ret = bind(sock, sa, salen);
834 en = errno;
835 }
836
837 if (ret < 0) {
838 flog_err_sys(EC_LIB_SOCKET, "bind: %s", safe_strerror(en));
839 return ret;
840 }
841
842 ret = listen(sock, SOMAXCONN);
843 if (ret < 0) {
844 flog_err_sys(EC_LIB_SOCKET, "listen: %s", safe_strerror(errno));
845 return ret;
846 }
847
848 listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
849 listener->fd = sock;
850 listener->name = XSTRDUP(MTYPE_BGP_LISTENER, bgp->name);
851
852 /* this socket is in a vrf record bgp back pointer */
853 if (bgp->vrf_id != VRF_DEFAULT)
854 listener->bgp = bgp;
855
856 memcpy(&listener->su, sa, salen);
857 thread_add_read(bm->master, bgp_accept, listener, sock,
858 &listener->thread);
859 listnode_add(bm->listen_sockets, listener);
860
861 return 0;
862 }
863
864 /* IPv6 supported version of BGP server socket setup. */
865 int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
866 {
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
880 frr_with_privs(&bgpd_privs) {
881 ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
882 bgp->vrf_id);
883 }
884 if (ret != 0) {
885 flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s",
886 gai_strerror(ret));
887 return -1;
888 }
889 if (bgp_option_check(BGP_OPT_NO_ZEBRA) &&
890 bgp->vrf_id != VRF_DEFAULT) {
891 freeaddrinfo(ainfo_save);
892 return -1;
893 }
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
901 frr_with_privs(&bgpd_privs) {
902 sock = vrf_socket(ainfo->ai_family,
903 ainfo->ai_socktype,
904 ainfo->ai_protocol,
905 bgp->vrf_id,
906 (bgp->inst_type
907 == BGP_INSTANCE_TYPE_VRF
908 ? bgp->name : NULL));
909 }
910 if (sock < 0) {
911 flog_err_sys(EC_LIB_SOCKET, "socket: %s",
912 safe_strerror(errno));
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
920 ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen,
921 bgp);
922 if (ret == 0)
923 ++count;
924 else
925 close(sock);
926 }
927 freeaddrinfo(ainfo_save);
928 if (count == 0 && bgp->inst_type != BGP_INSTANCE_TYPE_VRF) {
929 flog_err(
930 EC_LIB_SOCKET,
931 "%s: no usable addresses please check other programs usage of specified port %d",
932 __func__, port);
933 flog_err_sys(EC_LIB_SOCKET, "%s: Program cannot continue",
934 __func__);
935 exit(-1);
936 }
937
938 return 0;
939 }
940
941 /* this function closes vrf socket
942 * this should be called only for vrf socket with netns backend
943 */
944 void 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) {
957 THREAD_OFF(listener->thread);
958 close(listener->fd);
959 listnode_delete(bm->listen_sockets, listener);
960 XFREE(MTYPE_BGP_LISTENER, listener->name);
961 XFREE(MTYPE_BGP_LISTENER, listener);
962 }
963 }
964 }
965
966 /* this function closes main socket
967 */
968 void bgp_close(void)
969 {
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)) {
977 if (listener->bgp)
978 continue;
979 THREAD_OFF(listener->thread);
980 close(listener->fd);
981 listnode_delete(bm->listen_sockets, listener);
982 XFREE(MTYPE_BGP_LISTENER, listener->name);
983 XFREE(MTYPE_BGP_LISTENER, listener);
984 }
985 }