]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_network.c
Merge pull request #3041 from qlyoung/doc-cli-dep-period-length
[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
50 extern struct zebra_privs_t bgpd_privs;
51
52 static char *bgp_get_bound_name(struct peer *peer);
53
54 /* BGP listening socket. */
55 struct bgp_listener {
56 int fd;
57 union sockunion su;
58 struct thread *thread;
59 struct bgp *bgp;
60 };
61
62 /*
63 * Set MD5 key for the socket, for the given IPv4 peer address.
64 * If the password is NULL or zero-length, the option will be disabled.
65 */
66 static int bgp_md5_set_socket(int socket, union sockunion *su,
67 const char *password)
68 {
69 int ret = -1;
70 int en = ENOSYS;
71 #if HAVE_DECL_TCP_MD5SIG
72 union sockunion su2;
73 #endif /* HAVE_TCP_MD5SIG */
74
75 assert(socket >= 0);
76
77 #if HAVE_DECL_TCP_MD5SIG
78 /* Ensure there is no extraneous port information. */
79 memcpy(&su2, su, sizeof(union sockunion));
80 if (su2.sa.sa_family == AF_INET)
81 su2.sin.sin_port = 0;
82 else
83 su2.sin6.sin6_port = 0;
84 ret = sockopt_tcp_signature(socket, &su2, password);
85 en = errno;
86 #endif /* HAVE_TCP_MD5SIG */
87
88 if (ret < 0)
89 flog_warn(EC_BGP_NO_TCP_MD5,
90 "can't set TCP_MD5SIG option on socket %d: %s",
91 socket, safe_strerror(en));
92
93 return ret;
94 }
95
96 /* Helper for bgp_connect */
97 static int bgp_md5_set_connect(int socket, union sockunion *su,
98 const char *password)
99 {
100 int ret = -1;
101
102 #if HAVE_DECL_TCP_MD5SIG
103 frr_elevate_privs(&bgpd_privs) {
104 ret = bgp_md5_set_socket(socket, su, password);
105 }
106 #endif /* HAVE_TCP_MD5SIG */
107
108 return ret;
109 }
110
111 static int bgp_md5_set_password(struct peer *peer, const char *password)
112 {
113 struct listnode *node;
114 int ret = 0;
115 struct bgp_listener *listener;
116
117 frr_elevate_privs(&bgpd_privs) {
118 /* Set or unset the password on the listen socket(s). Outbound
119 * connections are taken care of in bgp_connect() below.
120 */
121 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
122 if (listener->su.sa.sa_family
123 == peer->su.sa.sa_family) {
124 ret = bgp_md5_set_socket(listener->fd,
125 &peer->su, password);
126 break;
127 }
128 }
129 return ret;
130 }
131
132 int bgp_md5_set(struct peer *peer)
133 {
134 /* Set the password from listen socket. */
135 return bgp_md5_set_password(peer, peer->password);
136 }
137
138 int bgp_md5_unset(struct peer *peer)
139 {
140 /* Unset the password from listen socket. */
141 return bgp_md5_set_password(peer, NULL);
142 }
143
144 int bgp_set_socket_ttl(struct peer *peer, int bgp_sock)
145 {
146 char buf[INET_ADDRSTRLEN];
147 int ret = 0;
148
149 /* In case of peer is EBGP, we should set TTL for this connection. */
150 if (!peer->gtsm_hops && (peer_sort(peer) == BGP_PEER_EBGP)) {
151 ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, peer->ttl);
152 if (ret) {
153 flog_err(
154 EC_LIB_SOCKET,
155 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
156 __func__,
157 inet_ntop(AF_INET, &peer->remote_id, buf,
158 sizeof(buf)),
159 errno);
160 return ret;
161 }
162 } else if (peer->gtsm_hops) {
163 /* On Linux, setting minttl without setting ttl seems to mess
164 with the
165 outgoing ttl. Therefore setting both.
166 */
167 ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, MAXTTL);
168 if (ret) {
169 flog_err(
170 EC_LIB_SOCKET,
171 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
172 __func__,
173 inet_ntop(AF_INET, &peer->remote_id, buf,
174 sizeof(buf)),
175 errno);
176 return ret;
177 }
178 ret = sockopt_minttl(peer->su.sa.sa_family, bgp_sock,
179 MAXTTL + 1 - peer->gtsm_hops);
180 if (ret) {
181 flog_err(
182 EC_LIB_SOCKET,
183 "%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
184 __func__,
185 inet_ntop(AF_INET, &peer->remote_id, buf,
186 sizeof(buf)),
187 errno);
188 return ret;
189 }
190 }
191
192 return ret;
193 }
194
195 /*
196 * Obtain the BGP instance that the incoming connection should be processed
197 * against. This is important because more than one VRF could be using the
198 * same IP address space. The instance is got by obtaining the device to
199 * which the incoming connection is bound to. This could either be a VRF
200 * or it could be an interface, which in turn determines the VRF.
201 */
202 static int bgp_get_instance_for_inc_conn(int sock, struct bgp **bgp_inst)
203 {
204 #ifndef SO_BINDTODEVICE
205 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
206 * anyway since the assumption is that the interface name returned by
207 * getsockopt() is useful in identifying the VRF, particularly with
208 * Linux's
209 * VRF l3master device. The whole mechanism is specific to Linux, so...
210 * when other platforms add VRF support, this will need handling here as
211 * well. (or, some restructuring) */
212 *bgp_inst = bgp_get_default();
213 return !*bgp_inst;
214
215 #else
216 char name[VRF_NAMSIZ + 1];
217 socklen_t name_len = VRF_NAMSIZ;
218 struct bgp *bgp;
219 int rc;
220 struct listnode *node, *nnode;
221
222 *bgp_inst = NULL;
223 name[0] = '\0';
224 rc = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, name, &name_len);
225 if (rc != 0) {
226 #if defined(HAVE_CUMULUS)
227 flog_err(EC_LIB_SOCKET,
228 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
229 safe_strerror(errno), sock);
230 return -1;
231 #endif
232 }
233
234 if (!strlen(name)) {
235 *bgp_inst = bgp_get_default();
236 return 0; /* default instance. */
237 }
238
239 /* First try match to instance; if that fails, check for interfaces. */
240 bgp = bgp_lookup_by_name(name);
241 if (bgp) {
242 if (!bgp->vrf_id) // unexpected
243 return -1;
244 *bgp_inst = bgp;
245 return 0;
246 }
247
248 /* TODO - This will be optimized once interfaces move into the NS */
249 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
250 struct interface *ifp;
251
252 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
253 continue;
254
255 ifp = if_lookup_by_name(name, bgp->vrf_id);
256 if (ifp) {
257 *bgp_inst = bgp;
258 return 0;
259 }
260 }
261
262 /* We didn't match to either an instance or an interface. */
263 return -1;
264 #endif
265 }
266
267 /* Accept bgp connection. */
268 static int bgp_accept(struct thread *thread)
269 {
270 int bgp_sock;
271 int accept_sock;
272 union sockunion su;
273 struct bgp_listener *listener = THREAD_ARG(thread);
274 struct peer *peer;
275 struct peer *peer1;
276 char buf[SU_ADDRSTRLEN];
277 struct bgp *bgp = NULL;
278
279 sockunion_init(&su);
280
281 /* Register accept thread. */
282 accept_sock = THREAD_FD(thread);
283 if (accept_sock < 0) {
284 flog_err_sys(EC_LIB_SOCKET, "accept_sock is nevative value %d",
285 accept_sock);
286 return -1;
287 }
288 listener->thread = NULL;
289
290 thread_add_read(bm->master, bgp_accept, listener, accept_sock,
291 &listener->thread);
292
293 /* Accept client connection. */
294 bgp_sock = sockunion_accept(accept_sock, &su);
295 if (bgp_sock < 0) {
296 flog_err_sys(EC_LIB_SOCKET,
297 "[Error] BGP socket accept failed (%s)",
298 safe_strerror(errno));
299 return -1;
300 }
301 set_nonblocking(bgp_sock);
302
303 /* Obtain BGP instance this connection is meant for.
304 * - if it is a VRF netns sock, then BGP is in listener structure
305 * - otherwise, the bgp instance need to be demultiplexed
306 */
307 if (listener->bgp)
308 bgp = listener->bgp;
309 else if (bgp_get_instance_for_inc_conn(bgp_sock, &bgp)) {
310 if (bgp_debug_neighbor_events(NULL))
311 zlog_debug(
312 "[Event] Could not get instance for incoming conn from %s",
313 inet_sutop(&su, buf));
314 close(bgp_sock);
315 return -1;
316 }
317
318 /* Set socket send buffer size */
319 setsockopt_so_sendbuf(bgp_sock, BGP_SOCKET_SNDBUF_SIZE);
320
321 /* Check remote IP address */
322 peer1 = peer_lookup(bgp, &su);
323
324 if (!peer1) {
325 peer1 = peer_lookup_dynamic_neighbor(bgp, &su);
326 if (peer1) {
327 /* Dynamic neighbor has been created, let it proceed */
328 peer1->fd = bgp_sock;
329 bgp_fsm_change_status(peer1, Active);
330 BGP_TIMER_OFF(
331 peer1->t_start); /* created in peer_create() */
332
333 if (peer_active(peer1))
334 BGP_EVENT_ADD(peer1, TCP_connection_open);
335
336 return 0;
337 }
338 }
339
340 if (!peer1) {
341 if (bgp_debug_neighbor_events(NULL)) {
342 zlog_debug(
343 "[Event] %s connection rejected - not configured"
344 " and not valid for dynamic",
345 inet_sutop(&su, buf));
346 }
347 close(bgp_sock);
348 return -1;
349 }
350
351 if (CHECK_FLAG(peer1->flags, PEER_FLAG_SHUTDOWN)) {
352 if (bgp_debug_neighbor_events(peer1))
353 zlog_debug(
354 "[Event] connection from %s rejected due to admin shutdown",
355 inet_sutop(&su, buf));
356 close(bgp_sock);
357 return -1;
358 }
359
360 /*
361 * Do not accept incoming connections in Clearing state. This can result
362 * in incorect state transitions - e.g., the connection goes back to
363 * Established and then the Clearing_Completed event is generated. Also,
364 * block incoming connection in Deleted state.
365 */
366 if (peer1->status == Clearing || peer1->status == Deleted) {
367 if (bgp_debug_neighbor_events(peer1))
368 zlog_debug(
369 "[Event] Closing incoming conn for %s (%p) state %d",
370 peer1->host, peer1, peer1->status);
371 close(bgp_sock);
372 return -1;
373 }
374
375 /* Check that at least one AF is activated for the peer. */
376 if (!peer_active(peer1)) {
377 if (bgp_debug_neighbor_events(peer1))
378 zlog_debug(
379 "%s - incoming conn rejected - no AF activated for peer",
380 peer1->host);
381 close(bgp_sock);
382 return -1;
383 }
384
385 if (bgp_debug_neighbor_events(peer1))
386 zlog_debug("[Event] BGP connection from host %s fd %d",
387 inet_sutop(&su, buf), bgp_sock);
388
389 if (peer1->doppelganger) {
390 /* We have an existing connection. Kill the existing one and run
391 with this one.
392 */
393 if (bgp_debug_neighbor_events(peer1))
394 zlog_debug(
395 "[Event] New active connection from peer %s, Killing"
396 " previous active connection",
397 peer1->host);
398 peer_delete(peer1->doppelganger);
399 }
400
401 if (bgp_set_socket_ttl(peer1, bgp_sock) < 0)
402 if (bgp_debug_neighbor_events(peer1))
403 zlog_debug(
404 "[Event] Unable to set min/max TTL on peer %s, Continuing",
405 peer1->host);
406
407 peer = peer_create(&su, peer1->conf_if, peer1->bgp, peer1->local_as,
408 peer1->as, peer1->as_type, 0, 0, NULL);
409 peer->su = su;
410 hash_release(peer->bgp->peerhash, peer);
411 hash_get(peer->bgp->peerhash, peer, hash_alloc_intern);
412
413 peer_xfer_config(peer, peer1);
414 UNSET_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE);
415
416 peer->doppelganger = peer1;
417 peer1->doppelganger = peer;
418 peer->fd = bgp_sock;
419 vrf_bind(peer->bgp->vrf_id, bgp_sock, bgp_get_bound_name(peer));
420 bgp_fsm_change_status(peer, Active);
421 BGP_TIMER_OFF(peer->t_start); /* created in peer_create() */
422
423 SET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
424
425 /* Make dummy peer until read Open packet. */
426 if (peer1->status == Established
427 && CHECK_FLAG(peer1->sflags, PEER_STATUS_NSF_MODE)) {
428 /* If we have an existing established connection with graceful
429 * restart
430 * capability announced with one or more address families, then
431 * drop
432 * existing established connection and move state to connect.
433 */
434 peer1->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
435 SET_FLAG(peer1->sflags, PEER_STATUS_NSF_WAIT);
436 bgp_event_update(peer1, TCP_connection_closed);
437 }
438
439 if (peer_active(peer)) {
440 BGP_EVENT_ADD(peer, TCP_connection_open);
441 }
442
443 return 0;
444 }
445
446 /* BGP socket bind. */
447 static char *bgp_get_bound_name(struct peer *peer)
448 {
449 char *name = NULL;
450
451 if (!peer)
452 return NULL;
453
454 if ((peer->bgp->vrf_id == VRF_DEFAULT) && !peer->ifname
455 && !peer->conf_if)
456 return NULL;
457
458 if (peer->su.sa.sa_family != AF_INET
459 && peer->su.sa.sa_family != AF_INET6)
460 return NULL; // unexpected
461
462 /* For IPv6 peering, interface (unnumbered or link-local with interface)
463 * takes precedence over VRF. For IPv4 peering, explicit interface or
464 * VRF are the situations to bind.
465 */
466 if (peer->su.sa.sa_family == AF_INET6)
467 name = (peer->conf_if ? peer->conf_if
468 : (peer->ifname ? peer->ifname
469 : peer->bgp->name));
470 else
471 name = peer->ifname ? peer->ifname : peer->bgp->name;
472
473 return name;
474 }
475
476 static int bgp_update_address(struct interface *ifp, const union sockunion *dst,
477 union sockunion *addr)
478 {
479 struct prefix *p, *sel, d;
480 struct connected *connected;
481 struct listnode *node;
482 int common;
483
484 sockunion2hostprefix(dst, &d);
485 sel = NULL;
486 common = -1;
487
488 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
489 p = connected->address;
490 if (p->family != d.family)
491 continue;
492 if (prefix_common_bits(p, &d) > common) {
493 sel = p;
494 common = prefix_common_bits(sel, &d);
495 }
496 }
497
498 if (!sel)
499 return 1;
500
501 prefix2sockunion(sel, addr);
502 return 0;
503 }
504
505 /* Update source selection. */
506 static int bgp_update_source(struct peer *peer)
507 {
508 struct interface *ifp;
509 union sockunion addr;
510 int ret = 0;
511
512 sockunion_init(&addr);
513
514 /* Source is specified with interface name. */
515 if (peer->update_if) {
516 ifp = if_lookup_by_name(peer->update_if, peer->bgp->vrf_id);
517 if (!ifp)
518 return -1;
519
520 if (bgp_update_address(ifp, &peer->su, &addr))
521 return -1;
522
523 ret = sockunion_bind(peer->fd, &addr, 0, &addr);
524 }
525
526 /* Source is specified with IP address. */
527 if (peer->update_source)
528 ret = sockunion_bind(peer->fd, peer->update_source, 0,
529 peer->update_source);
530
531 return ret;
532 }
533
534 #define DATAPLANE_MARK 254 /* main table ID */
535
536 /* BGP try to connect to the peer. */
537 int bgp_connect(struct peer *peer)
538 {
539 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
540 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
541 ifindex_t ifindex = 0;
542
543 if (peer->conf_if && BGP_PEER_SU_UNSPEC(peer)) {
544 zlog_debug("Peer address not learnt: Returning from connect");
545 return 0;
546 }
547 frr_elevate_privs(&bgpd_privs) {
548 /* Make socket for the peer. */
549 peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
550 bgp_get_bound_name(peer));
551 }
552 if (peer->fd < 0)
553 return -1;
554
555 set_nonblocking(peer->fd);
556
557 /* Set socket send buffer size */
558 setsockopt_so_sendbuf(peer->fd, BGP_SOCKET_SNDBUF_SIZE);
559
560 if (bgp_set_socket_ttl(peer, peer->fd) < 0)
561 return -1;
562
563 sockopt_reuseaddr(peer->fd);
564 sockopt_reuseport(peer->fd);
565 if (sockopt_mark_default(peer->fd, DATAPLANE_MARK, &bgpd_privs) < 0)
566 flog_warn(EC_BGP_NO_SOCKOPT_MARK,
567 "Unable to set mark on FD for peer %s, err=%s",
568 peer->host, safe_strerror(errno));
569
570 #ifdef IPTOS_PREC_INTERNETCONTROL
571 frr_elevate_privs(&bgpd_privs) {
572 if (sockunion_family(&peer->su) == AF_INET)
573 setsockopt_ipv4_tos(peer->fd,
574 IPTOS_PREC_INTERNETCONTROL);
575 else if (sockunion_family(&peer->su) == AF_INET6)
576 setsockopt_ipv6_tclass(peer->fd,
577 IPTOS_PREC_INTERNETCONTROL);
578 }
579 #endif
580
581 if (peer->password)
582 bgp_md5_set_connect(peer->fd, &peer->su, peer->password);
583
584 /* Update source bind. */
585 if (bgp_update_source(peer) < 0) {
586 return connect_error;
587 }
588
589 if (peer->conf_if || peer->ifname)
590 ifindex = ifname2ifindex(peer->conf_if ? peer->conf_if
591 : peer->ifname,
592 peer->bgp->vrf_id);
593
594 if (bgp_debug_neighbor_events(peer))
595 zlog_debug("%s [Event] Connect start to %s fd %d", peer->host,
596 peer->host, peer->fd);
597
598 /* Connect to the remote peer. */
599 return sockunion_connect(peer->fd, &peer->su, htons(peer->port),
600 ifindex);
601 }
602
603 /* After TCP connection is established. Get local address and port. */
604 int bgp_getsockname(struct peer *peer)
605 {
606 if (peer->su_local) {
607 sockunion_free(peer->su_local);
608 peer->su_local = NULL;
609 }
610
611 if (peer->su_remote) {
612 sockunion_free(peer->su_remote);
613 peer->su_remote = NULL;
614 }
615
616 peer->su_local = sockunion_getsockname(peer->fd);
617 if (!peer->su_local)
618 return -1;
619 peer->su_remote = sockunion_getpeername(peer->fd);
620 if (!peer->su_remote)
621 return -1;
622
623 if (!bgp_zebra_nexthop_set(peer->su_local, peer->su_remote,
624 &peer->nexthop, peer)) {
625 flog_err(EC_BGP_NH_UPD,
626 "%s: nexthop_set failed, resetting connection - intf %p",
627 peer->host, peer->nexthop.ifp);
628 return -1;
629 }
630 return 0;
631 }
632
633
634 static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
635 struct bgp *bgp)
636 {
637 struct bgp_listener *listener;
638 int ret, en;
639
640 sockopt_reuseaddr(sock);
641 sockopt_reuseport(sock);
642
643 frr_elevate_privs(&bgpd_privs) {
644
645 #ifdef IPTOS_PREC_INTERNETCONTROL
646 if (sa->sa_family == AF_INET)
647 setsockopt_ipv4_tos(sock, IPTOS_PREC_INTERNETCONTROL);
648 else if (sa->sa_family == AF_INET6)
649 setsockopt_ipv6_tclass(sock,
650 IPTOS_PREC_INTERNETCONTROL);
651 #endif
652
653 sockopt_v6only(sa->sa_family, sock);
654
655 ret = bind(sock, sa, salen);
656 en = errno;
657 }
658
659 if (ret < 0) {
660 flog_err_sys(EC_LIB_SOCKET, "bind: %s", safe_strerror(en));
661 return ret;
662 }
663
664 ret = listen(sock, SOMAXCONN);
665 if (ret < 0) {
666 flog_err_sys(EC_LIB_SOCKET, "listen: %s", safe_strerror(errno));
667 return ret;
668 }
669
670 listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
671 listener->fd = sock;
672
673 /* this socket needs a change of ns. record bgp back pointer */
674 if (bgp->vrf_id != VRF_DEFAULT && vrf_is_mapped_on_netns(
675 vrf_lookup_by_id(bgp->vrf_id)))
676 listener->bgp = bgp;
677
678 memcpy(&listener->su, sa, salen);
679 listener->thread = NULL;
680 thread_add_read(bm->master, bgp_accept, listener, sock,
681 &listener->thread);
682 listnode_add(bm->listen_sockets, listener);
683
684 return 0;
685 }
686
687 /* IPv6 supported version of BGP server socket setup. */
688 int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
689 {
690 struct addrinfo *ainfo;
691 struct addrinfo *ainfo_save;
692 static const struct addrinfo req = {
693 .ai_family = AF_UNSPEC,
694 .ai_flags = AI_PASSIVE,
695 .ai_socktype = SOCK_STREAM,
696 };
697 int ret, count;
698 char port_str[BUFSIZ];
699
700 snprintf(port_str, sizeof(port_str), "%d", port);
701 port_str[sizeof(port_str) - 1] = '\0';
702
703 frr_elevate_privs(&bgpd_privs) {
704 ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
705 bgp->vrf_id);
706 }
707 if (ret != 0) {
708 flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s",
709 gai_strerror(ret));
710 return -1;
711 }
712
713 count = 0;
714 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) {
715 int sock;
716
717 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
718 continue;
719
720 frr_elevate_privs(&bgpd_privs) {
721 sock = vrf_socket(ainfo->ai_family,
722 ainfo->ai_socktype,
723 ainfo->ai_protocol, bgp->vrf_id,
724 (bgp->inst_type
725 == BGP_INSTANCE_TYPE_VRF
726 ? bgp->name : NULL));
727 }
728 if (sock < 0) {
729 flog_err_sys(EC_LIB_SOCKET, "socket: %s",
730 safe_strerror(errno));
731 continue;
732 }
733
734 /* if we intend to implement ttl-security, this socket needs
735 * ttl=255 */
736 sockopt_ttl(ainfo->ai_family, sock, MAXTTL);
737
738 ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen,
739 bgp);
740 if (ret == 0)
741 ++count;
742 else
743 close(sock);
744 }
745 freeaddrinfo(ainfo_save);
746 if (count == 0 && bgp->inst_type != BGP_INSTANCE_TYPE_VRF) {
747 flog_err(
748 EC_LIB_SOCKET,
749 "%s: no usable addresses please check other programs usage of specified port %d",
750 __func__, port);
751 flog_err_sys(EC_LIB_SOCKET, "%s: Program cannot continue",
752 __func__);
753 exit(-1);
754 }
755
756 return 0;
757 }
758
759 /* this function closes vrf socket
760 * this should be called only for vrf socket with netns backend
761 */
762 void bgp_close_vrf_socket(struct bgp *bgp)
763 {
764 struct listnode *node, *next;
765 struct bgp_listener *listener;
766
767 if (!bgp)
768 return;
769
770 if (bm->listen_sockets == NULL)
771 return;
772
773 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
774 if (listener->bgp == bgp) {
775 thread_cancel(listener->thread);
776 close(listener->fd);
777 listnode_delete(bm->listen_sockets, listener);
778 XFREE(MTYPE_BGP_LISTENER, listener);
779 }
780 }
781 }
782
783 /* this function closes main socket
784 */
785 void bgp_close(void)
786 {
787 struct listnode *node, *next;
788 struct bgp_listener *listener;
789
790 if (bm->listen_sockets == NULL)
791 return;
792
793 for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) {
794 if (listener->bgp)
795 continue;
796 thread_cancel(listener->thread);
797 close(listener->fd);
798 listnode_delete(bm->listen_sockets, listener);
799 XFREE(MTYPE_BGP_LISTENER, listener);
800 }
801 }