blob: d11cbc3e70a8f91132a7eb47b28964f58b27af53 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP network related fucntions
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "thread.h"
24#include "sockunion.h"
Paul Jakma0df7c912008-07-21 21:02:49 +000025#include "sockopt.h"
paul718e3742002-12-13 20:15:29 +000026#include "memory.h"
27#include "log.h"
28#include "if.h"
29#include "prefix.h"
30#include "command.h"
pauledd7c242003-06-04 13:59:38 +000031#include "privs.h"
Paul Jakma0df7c912008-07-21 21:02:49 +000032#include "linklist.h"
Paul Jakmafdbc8e72011-04-11 16:31:43 +010033#include "network.h"
Donald Sharp04907292016-01-07 10:03:01 -050034#include "filter.h"
paul718e3742002-12-13 20:15:29 +000035
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_fsm.h"
38#include "bgpd/bgp_attr.h"
39#include "bgpd/bgp_debug.h"
40#include "bgpd/bgp_network.h"
pauledd7c242003-06-04 13:59:38 +000041
42extern struct zebra_privs_t bgpd_privs;
43
Stephen Hemmingerd023aec2009-07-21 16:27:21 -070044/* BGP listening socket. */
45struct bgp_listener
46{
47 int fd;
48 union sockunion su;
49 struct thread *thread;
50};
David Lamparter6b0655a2014-06-04 06:53:35 +020051
Paul Jakma0df7c912008-07-21 21:02:49 +000052/*
53 * Set MD5 key for the socket, for the given IPv4 peer address.
54 * If the password is NULL or zero-length, the option will be disabled.
55 */
56static int
57bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
58{
59 int ret = -1;
60 int en = ENOSYS;
61
62 assert (socket >= 0);
63
64#if HAVE_DECL_TCP_MD5SIG
65 ret = sockopt_tcp_signature (socket, su, password);
66 en = errno;
67#endif /* HAVE_TCP_MD5SIG */
68
69 if (ret < 0)
70 zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
71 socket, safe_strerror (en));
72
73 return ret;
74}
75
76/* Helper for bgp_connect */
77static int
78bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
79{
80 int ret = -1;
81
82#if HAVE_DECL_TCP_MD5SIG
83 if ( bgpd_privs.change (ZPRIVS_RAISE) )
84 {
85 zlog_err ("%s: could not raise privs", __func__);
86 return ret;
87 }
88
89 ret = bgp_md5_set_socket (socket, su, password);
90
91 if (bgpd_privs.change (ZPRIVS_LOWER) )
92 zlog_err ("%s: could not lower privs", __func__);
93#endif /* HAVE_TCP_MD5SIG */
94
95 return ret;
96}
97
98int
99bgp_md5_set (struct peer *peer)
100{
101 struct listnode *node;
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700102 int ret = 0;
103 struct bgp_listener *listener;
Paul Jakma0df7c912008-07-21 21:02:49 +0000104
105 if ( bgpd_privs.change (ZPRIVS_RAISE) )
106 {
107 zlog_err ("%s: could not raise privs", __func__);
108 return -1;
109 }
110
111 /* Just set the password on the listen socket(s). Outbound connections
112 * are taken care of in bgp_connect() below.
113 */
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700114 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
115 if (listener->su.sa.sa_family == peer->su.sa.sa_family)
116 {
117 ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password);
118 break;
119 }
120
Paul Jakma0df7c912008-07-21 21:02:49 +0000121 if (bgpd_privs.change (ZPRIVS_LOWER) )
122 zlog_err ("%s: could not lower privs", __func__);
123
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700124 return ret;
Paul Jakma0df7c912008-07-21 21:02:49 +0000125}
Vipin Kumar3374bef2014-01-09 00:31:22 +0000126
127/* Update BGP socket send buffer size */
128static void
129bgp_update_sock_send_buffer_size (int fd)
130{
131 int size = BGP_SOCKET_SNDBUF_SIZE;
132 int optval;
133 socklen_t optlen = sizeof(optval);
134
135 if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0)
136 {
137 zlog_err("getsockopt of SO_SNDBUF failed %s\n", safe_strerror(errno));
138 return;
139 }
140 if (optval < size)
141 {
142 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
143 {
144 zlog_err("Couldn't increase send buffer: %s\n", safe_strerror(errno));
145 }
146 }
147}
148
David Lamparteref0b0c32014-05-19 22:52:04 +0200149static void
150bgp_set_socket_ttl (struct peer *peer, int bgp_sock)
151{
Pradosh Mohapatra5d804b42013-09-12 03:37:07 +0000152 char buf[INET_ADDRSTRLEN];
153 int ret;
154
155 /* In case of peer is EBGP, we should set TTL for this connection. */
156 if (!peer->gtsm_hops && (peer_sort (peer) == BGP_PEER_EBGP))
157 {
158 ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, peer->ttl);
159 if (ret)
160 {
161 zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
162 __func__,
163 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
164 errno);
165 }
166 }
167 else if (peer->gtsm_hops)
168 {
169 /* On Linux, setting minttl without setting ttl seems to mess with the
170 outgoing ttl. Therefore setting both.
171 */
172 ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, MAXTTL);
173 if (ret)
174 {
175 zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
176 __func__,
177 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
178 errno);
179 }
180 ret = sockopt_minttl (peer->su.sa.sa_family, bgp_sock,
181 MAXTTL + 1 - peer->gtsm_hops);
182 if (ret)
183 {
184 zlog_err ("%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
185 __func__,
186 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
187 errno);
188 }
189 }
David Lamparteref0b0c32014-05-19 22:52:04 +0200190}
191
paul718e3742002-12-13 20:15:29 +0000192/* Accept bgp connection. */
193static int
194bgp_accept (struct thread *thread)
195{
196 int bgp_sock;
197 int accept_sock;
198 union sockunion su;
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700199 struct bgp_listener *listener = THREAD_ARG(thread);
paul718e3742002-12-13 20:15:29 +0000200 struct peer *peer;
pauleb821182004-05-01 08:44:08 +0000201 struct peer *peer1;
paul718e3742002-12-13 20:15:29 +0000202 char buf[SU_ADDRSTRLEN];
203
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700204 /* Register accept thread. */
paul718e3742002-12-13 20:15:29 +0000205 accept_sock = THREAD_FD (thread);
paul718e3742002-12-13 20:15:29 +0000206 if (accept_sock < 0)
207 {
208 zlog_err ("accept_sock is nevative value %d", accept_sock);
209 return -1;
210 }
Donald Sharp774914f2015-10-14 08:50:39 -0400211 listener->thread = thread_add_read (bm->master, bgp_accept, listener, accept_sock);
paul718e3742002-12-13 20:15:29 +0000212
213 /* Accept client connection. */
214 bgp_sock = sockunion_accept (accept_sock, &su);
215 if (bgp_sock < 0)
216 {
ajs6099b3b2004-11-20 02:06:59 +0000217 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000218 return -1;
219 }
Stephen Hemminger35398582010-08-05 10:26:23 -0700220 set_nonblocking (bgp_sock);
paul718e3742002-12-13 20:15:29 +0000221
Vipin Kumar3374bef2014-01-09 00:31:22 +0000222 /* Set socket send buffer size */
223 bgp_update_sock_send_buffer_size(bgp_sock);
224
paul718e3742002-12-13 20:15:29 +0000225 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000226 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
paul718e3742002-12-13 20:15:29 +0000227
228 /* Check remote IP address */
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700229 peer1 = peer_lookup (NULL, &su);
pauleb821182004-05-01 08:44:08 +0000230 if (! peer1 || peer1->status == Idle)
paul718e3742002-12-13 20:15:29 +0000231 {
232 if (BGP_DEBUG (events, EVENTS))
233 {
pauleb821182004-05-01 08:44:08 +0000234 if (! peer1)
ajs478ba052004-12-08 20:41:23 +0000235 zlog_debug ("[Event] BGP connection IP address %s is not configured",
paul718e3742002-12-13 20:15:29 +0000236 inet_sutop (&su, buf));
237 else
ajs478ba052004-12-08 20:41:23 +0000238 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
paul718e3742002-12-13 20:15:29 +0000239 inet_sutop (&su, buf));
240 }
241 close (bgp_sock);
242 return -1;
243 }
244
David Lamparteref0b0c32014-05-19 22:52:04 +0200245 bgp_set_socket_ttl (peer1, bgp_sock);
paul718e3742002-12-13 20:15:29 +0000246
pauleb821182004-05-01 08:44:08 +0000247 /* Make dummy peer until read Open packet. */
248 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000249 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
pauleb821182004-05-01 08:44:08 +0000250
251 {
Jorge Boncompte [DTI2]682ca042012-04-10 16:57:27 +0200252 char buf[SU_ADDRSTRLEN];
pauleb821182004-05-01 08:44:08 +0000253
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700254 peer = peer_create_accept (peer1->bgp);
pauleb821182004-05-01 08:44:08 +0000255 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
256 peer->su = su;
257 peer->fd = bgp_sock;
258 peer->status = Active;
259 peer->local_id = peer1->local_id;
Timo Teräse8eb0002009-02-17 12:14:23 +0200260 peer->v_holdtime = peer1->v_holdtime;
261 peer->v_keepalive = peer1->v_keepalive;
pauleb821182004-05-01 08:44:08 +0000262
263 /* Make peer's address string. */
264 sockunion2str (&su, buf, SU_ADDRSTRLEN);
paule83e2082005-05-19 02:12:25 +0000265 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
pauleb821182004-05-01 08:44:08 +0000266 }
paul718e3742002-12-13 20:15:29 +0000267
268 BGP_EVENT_ADD (peer, TCP_connection_open);
269
270 return 0;
271}
272
273/* BGP socket bind. */
paul94f2b392005-06-28 12:44:16 +0000274static int
paul718e3742002-12-13 20:15:29 +0000275bgp_bind (struct peer *peer)
276{
277#ifdef SO_BINDTODEVICE
278 int ret;
279 struct ifreq ifreq;
Lou Berger056f3762013-04-10 12:30:04 -0700280 int myerrno;
paul718e3742002-12-13 20:15:29 +0000281
282 if (! peer->ifname)
283 return 0;
284
285 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
286
paul98f51632004-10-25 14:19:15 +0000287 if ( bgpd_privs.change (ZPRIVS_RAISE) )
288 zlog_err ("bgp_bind: could not raise privs");
289
pauleb821182004-05-01 08:44:08 +0000290 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
paul718e3742002-12-13 20:15:29 +0000291 &ifreq, sizeof (ifreq));
Lou Berger056f3762013-04-10 12:30:04 -0700292 myerrno = errno;
293
paul98f51632004-10-25 14:19:15 +0000294 if (bgpd_privs.change (ZPRIVS_LOWER) )
295 zlog_err ("bgp_bind: could not lower privs");
296
paul718e3742002-12-13 20:15:29 +0000297 if (ret < 0)
298 {
Lou Berger056f3762013-04-10 12:30:04 -0700299 zlog (peer->log, LOG_INFO, "bind to interface %s failed, errno=%d",
300 peer->ifname, myerrno);
paul718e3742002-12-13 20:15:29 +0000301 return ret;
302 }
303#endif /* SO_BINDTODEVICE */
304 return 0;
305}
306
paul94f2b392005-06-28 12:44:16 +0000307static int
David Lamparter1727d2e2010-02-02 20:18:23 +0100308bgp_update_address (struct interface *ifp, const union sockunion *dst,
309 union sockunion *addr)
paul718e3742002-12-13 20:15:29 +0000310{
Timo Teräsc1c69e42015-05-22 13:40:57 +0300311 struct prefix *p, *sel, d;
paul718e3742002-12-13 20:15:29 +0000312 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000313 struct listnode *node;
David Lamparter1727d2e2010-02-02 20:18:23 +0100314 int common;
315
Timo Teräsc1c69e42015-05-22 13:40:57 +0300316 sockunion2hostprefix (dst, &d);
David Lamparter1727d2e2010-02-02 20:18:23 +0100317 sel = NULL;
318 common = -1;
paul718e3742002-12-13 20:15:29 +0000319
paul1eb8ef22005-04-07 07:30:20 +0000320 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
paul718e3742002-12-13 20:15:29 +0000321 {
David Lamparter1727d2e2010-02-02 20:18:23 +0100322 p = connected->address;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300323 if (p->family != d.family)
David Lamparter1727d2e2010-02-02 20:18:23 +0100324 continue;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300325 if (prefix_common_bits (p, &d) > common)
David Lamparter1727d2e2010-02-02 20:18:23 +0100326 {
327 sel = p;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300328 common = prefix_common_bits (sel, &d);
David Lamparter1727d2e2010-02-02 20:18:23 +0100329 }
paul718e3742002-12-13 20:15:29 +0000330 }
David Lamparter1727d2e2010-02-02 20:18:23 +0100331
David Lamparter1727d2e2010-02-02 20:18:23 +0100332 if (!sel)
333 return 1;
334
335 prefix2sockunion (sel, addr);
336 return 0;
paul718e3742002-12-13 20:15:29 +0000337}
338
339/* Update source selection. */
paul94f2b392005-06-28 12:44:16 +0000340static void
paul718e3742002-12-13 20:15:29 +0000341bgp_update_source (struct peer *peer)
342{
343 struct interface *ifp;
David Lamparter1727d2e2010-02-02 20:18:23 +0100344 union sockunion addr;
paul718e3742002-12-13 20:15:29 +0000345
346 /* Source is specified with interface name. */
347 if (peer->update_if)
348 {
349 ifp = if_lookup_by_name (peer->update_if);
350 if (! ifp)
351 return;
352
David Lamparter1727d2e2010-02-02 20:18:23 +0100353 if (bgp_update_address (ifp, &peer->su, &addr))
paul718e3742002-12-13 20:15:29 +0000354 return;
355
David Lamparter1727d2e2010-02-02 20:18:23 +0100356 sockunion_bind (peer->fd, &addr, 0, &addr);
paul718e3742002-12-13 20:15:29 +0000357 }
358
359 /* Source is specified with IP address. */
360 if (peer->update_source)
pauleb821182004-05-01 08:44:08 +0000361 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
paul718e3742002-12-13 20:15:29 +0000362}
363
364/* BGP try to connect to the peer. */
365int
366bgp_connect (struct peer *peer)
367{
368 unsigned int ifindex = 0;
369
370 /* Make socket for the peer. */
pauleb821182004-05-01 08:44:08 +0000371 peer->fd = sockunion_socket (&peer->su);
372 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000373 return -1;
374
Vipin Kumar48fc05f2014-01-09 00:31:22 +0000375 set_nonblocking (peer->fd);
376
Vipin Kumar3374bef2014-01-09 00:31:22 +0000377 /* Set socket send buffer size */
378 bgp_update_sock_send_buffer_size(peer->fd);
379
David Lamparteref0b0c32014-05-19 22:52:04 +0200380 bgp_set_socket_ttl (peer, peer->fd);
paul718e3742002-12-13 20:15:29 +0000381
pauleb821182004-05-01 08:44:08 +0000382 sockopt_reuseaddr (peer->fd);
383 sockopt_reuseport (peer->fd);
Paul Jakma0df7c912008-07-21 21:02:49 +0000384
Stephen Hemminger1423c802008-08-14 17:59:25 +0100385#ifdef IPTOS_PREC_INTERNETCONTROL
Chris Luke5c88f192011-10-18 17:26:51 +0400386 if (bgpd_privs.change (ZPRIVS_RAISE))
387 zlog_err ("%s: could not raise privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100388 if (sockunion_family (&peer->su) == AF_INET)
389 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400390# ifdef HAVE_IPV6
391 else if (sockunion_family (&peer->su) == AF_INET6)
392 setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
393# endif
Chris Luke5c88f192011-10-18 17:26:51 +0400394 if (bgpd_privs.change (ZPRIVS_LOWER))
395 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100396#endif
397
Paul Jakma0df7c912008-07-21 21:02:49 +0000398 if (peer->password)
399 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
paul718e3742002-12-13 20:15:29 +0000400
401 /* Bind socket. */
402 bgp_bind (peer);
403
404 /* Update source bind. */
405 bgp_update_source (peer);
406
407#ifdef HAVE_IPV6
408 if (peer->ifname)
Feng Lu395828e2015-05-22 11:39:55 +0200409 ifindex = ifname2ifindex (peer->ifname);
paul718e3742002-12-13 20:15:29 +0000410#endif /* HAVE_IPV6 */
411
412 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000413 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
pauleb821182004-05-01 08:44:08 +0000414 peer->host, peer->host, peer->fd);
paul718e3742002-12-13 20:15:29 +0000415
416 /* Connect to the remote peer. */
pauleb821182004-05-01 08:44:08 +0000417 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
paul718e3742002-12-13 20:15:29 +0000418}
419
420/* After TCP connection is established. Get local address and port. */
421void
422bgp_getsockname (struct peer *peer)
423{
424 if (peer->su_local)
425 {
paul22db9de2005-05-19 01:50:11 +0000426 sockunion_free (peer->su_local);
paul718e3742002-12-13 20:15:29 +0000427 peer->su_local = NULL;
428 }
429
430 if (peer->su_remote)
431 {
paul22db9de2005-05-19 01:50:11 +0000432 sockunion_free (peer->su_remote);
paul718e3742002-12-13 20:15:29 +0000433 peer->su_remote = NULL;
434 }
435
pauleb821182004-05-01 08:44:08 +0000436 peer->su_local = sockunion_getsockname (peer->fd);
437 peer->su_remote = sockunion_getpeername (peer->fd);
paul718e3742002-12-13 20:15:29 +0000438
439 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
440}
441
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700442
443static int
444bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
445{
446 struct bgp_listener *listener;
447 int ret, en;
448
449 sockopt_reuseaddr (sock);
450 sockopt_reuseport (sock);
451
Chris Luke5c88f192011-10-18 17:26:51 +0400452 if (bgpd_privs.change (ZPRIVS_RAISE))
453 zlog_err ("%s: could not raise privs", __func__);
454
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700455#ifdef IPTOS_PREC_INTERNETCONTROL
456 if (sa->sa_family == AF_INET)
457 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400458# ifdef HAVE_IPV6
459 else if (sa->sa_family == AF_INET6)
460 setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
461# endif
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700462#endif
463
David Lamparterca051262009-10-04 16:21:49 +0200464 sockopt_v6only (sa->sa_family, sock);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700465
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700466 ret = bind (sock, sa, salen);
467 en = errno;
Chris Luke5c88f192011-10-18 17:26:51 +0400468 if (bgpd_privs.change (ZPRIVS_LOWER))
469 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700470
471 if (ret < 0)
472 {
473 zlog_err ("bind: %s", safe_strerror (en));
474 return ret;
475 }
476
477 ret = listen (sock, 3);
478 if (ret < 0)
479 {
480 zlog_err ("listen: %s", safe_strerror (errno));
481 return ret;
482 }
483
484 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
485 listener->fd = sock;
486 memcpy(&listener->su, sa, salen);
Donald Sharp774914f2015-10-14 08:50:39 -0400487 listener->thread = thread_add_read (bm->master, bgp_accept, listener, sock);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700488 listnode_add (bm->listen_sockets, listener);
489
490 return 0;
491}
492
paul718e3742002-12-13 20:15:29 +0000493/* IPv6 supported version of BGP server socket setup. */
David Lamparter6d6df302014-06-28 21:12:37 +0200494#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000495int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700496bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000497{
paul718e3742002-12-13 20:15:29 +0000498 struct addrinfo *ainfo;
499 struct addrinfo *ainfo_save;
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700500 static const struct addrinfo req = {
501 .ai_family = AF_UNSPEC,
502 .ai_flags = AI_PASSIVE,
503 .ai_socktype = SOCK_STREAM,
504 };
505 int ret, count;
paul718e3742002-12-13 20:15:29 +0000506 char port_str[BUFSIZ];
507
Paul Jakma90b68762008-01-29 17:26:34 +0000508 snprintf (port_str, sizeof(port_str), "%d", port);
paul718e3742002-12-13 20:15:29 +0000509 port_str[sizeof (port_str) - 1] = '\0';
510
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700511 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
paul718e3742002-12-13 20:15:29 +0000512 if (ret != 0)
513 {
514 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
515 return -1;
516 }
517
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700518 count = 0;
519 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
paul718e3742002-12-13 20:15:29 +0000520 {
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700521 int sock;
522
paul718e3742002-12-13 20:15:29 +0000523 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
524 continue;
525
526 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
527 if (sock < 0)
528 {
ajs6099b3b2004-11-20 02:06:59 +0000529 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000530 continue;
531 }
Nick Hilliardfa411a22011-03-23 15:33:17 +0000532
533 /* if we intend to implement ttl-security, this socket needs ttl=255 */
534 sockopt_ttl (ainfo->ai_family, sock, MAXTTL);
535
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700536 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
537 if (ret == 0)
538 ++count;
539 else
540 close(sock);
paul718e3742002-12-13 20:15:29 +0000541 }
paul718e3742002-12-13 20:15:29 +0000542 freeaddrinfo (ainfo_save);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700543 if (count == 0)
544 {
545 zlog_err ("%s: no usable addresses", __func__);
546 return -1;
547 }
paul718e3742002-12-13 20:15:29 +0000548
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700549 return 0;
paul718e3742002-12-13 20:15:29 +0000550}
551#else
552/* Traditional IPv4 only version. */
553int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700554bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000555{
556 int sock;
557 int socklen;
558 struct sockaddr_in sin;
Lou Berger4d805602016-01-12 13:41:52 -0500559 int ret;
paul718e3742002-12-13 20:15:29 +0000560
561 sock = socket (AF_INET, SOCK_STREAM, 0);
562 if (sock < 0)
563 {
ajs6099b3b2004-11-20 02:06:59 +0000564 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000565 return sock;
566 }
567
Nick Hilliardfa411a22011-03-23 15:33:17 +0000568 /* if we intend to implement ttl-security, this socket needs ttl=255 */
569 sockopt_ttl (AF_INET, sock, MAXTTL);
570
paul718e3742002-12-13 20:15:29 +0000571 memset (&sin, 0, sizeof (struct sockaddr_in));
paul718e3742002-12-13 20:15:29 +0000572 sin.sin_family = AF_INET;
573 sin.sin_port = htons (port);
574 socklen = sizeof (struct sockaddr_in);
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000575
Paul Jakma90b68762008-01-29 17:26:34 +0000576 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000577 {
Paul Jakma90b68762008-01-29 17:26:34 +0000578 zlog_err("bgp_socket: could not parse ip address %s: %s",
579 address, safe_strerror (errno));
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000580 return ret;
581 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000582#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000583 sin.sin_len = socklen;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000584#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000585
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700586 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
paul718e3742002-12-13 20:15:29 +0000587 if (ret < 0)
588 {
paul718e3742002-12-13 20:15:29 +0000589 close (sock);
590 return ret;
591 }
paul718e3742002-12-13 20:15:29 +0000592 return sock;
593}
David Lamparter6d6df302014-06-28 21:12:37 +0200594#endif /* HAVE_IPV6 */
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700595
596void
597bgp_close (void)
598{
599 struct listnode *node, *next;
600 struct bgp_listener *listener;
601
602 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
603 {
604 thread_cancel (listener->thread);
605 close (listener->fd);
606 listnode_delete (bm->listen_sockets, listener);
607 XFREE (MTYPE_BGP_LISTENER, listener);
608 }
609}