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