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