blob: 3c5e6c5d527bc212aa0468291f84f4f917997b03 [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"
paul718e3742002-12-13 20:15:29 +000034
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_fsm.h"
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_debug.h"
39#include "bgpd/bgp_network.h"
pauledd7c242003-06-04 13:59:38 +000040
41extern struct zebra_privs_t bgpd_privs;
42
Stephen Hemmingerd023aec2009-07-21 16:27:21 -070043/* BGP listening socket. */
44struct bgp_listener
45{
46 int fd;
47 union sockunion su;
48 struct thread *thread;
49};
David Lamparter6b0655a2014-06-04 06:53:35 +020050
Paul Jakma0df7c912008-07-21 21:02:49 +000051/*
52 * Set MD5 key for the socket, for the given IPv4 peer address.
53 * If the password is NULL or zero-length, the option will be disabled.
54 */
55static int
56bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
57{
58 int ret = -1;
59 int en = ENOSYS;
60
61 assert (socket >= 0);
62
63#if HAVE_DECL_TCP_MD5SIG
64 ret = sockopt_tcp_signature (socket, su, password);
65 en = errno;
66#endif /* HAVE_TCP_MD5SIG */
67
68 if (ret < 0)
69 zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
70 socket, safe_strerror (en));
71
72 return ret;
73}
74
75/* Helper for bgp_connect */
76static int
77bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
78{
79 int ret = -1;
80
81#if HAVE_DECL_TCP_MD5SIG
82 if ( bgpd_privs.change (ZPRIVS_RAISE) )
83 {
84 zlog_err ("%s: could not raise privs", __func__);
85 return ret;
86 }
87
88 ret = bgp_md5_set_socket (socket, su, password);
89
90 if (bgpd_privs.change (ZPRIVS_LOWER) )
91 zlog_err ("%s: could not lower privs", __func__);
92#endif /* HAVE_TCP_MD5SIG */
93
94 return ret;
95}
96
97int
98bgp_md5_set (struct peer *peer)
99{
100 struct listnode *node;
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700101 int ret = 0;
102 struct bgp_listener *listener;
Paul Jakma0df7c912008-07-21 21:02:49 +0000103
104 if ( bgpd_privs.change (ZPRIVS_RAISE) )
105 {
106 zlog_err ("%s: could not raise privs", __func__);
107 return -1;
108 }
109
110 /* Just set the password on the listen socket(s). Outbound connections
111 * are taken care of in bgp_connect() below.
112 */
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700113 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
114 if (listener->su.sa.sa_family == peer->su.sa.sa_family)
115 {
116 ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password);
117 break;
118 }
119
Paul Jakma0df7c912008-07-21 21:02:49 +0000120 if (bgpd_privs.change (ZPRIVS_LOWER) )
121 zlog_err ("%s: could not lower privs", __func__);
122
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700123 return ret;
Paul Jakma0df7c912008-07-21 21:02:49 +0000124}
Vipin Kumar3374bef2014-01-09 00:31:22 +0000125
126/* Update BGP socket send buffer size */
127static void
128bgp_update_sock_send_buffer_size (int fd)
129{
130 int size = BGP_SOCKET_SNDBUF_SIZE;
131 int optval;
132 socklen_t optlen = sizeof(optval);
133
134 if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0)
135 {
136 zlog_err("getsockopt of SO_SNDBUF failed %s\n", safe_strerror(errno));
137 return;
138 }
139 if (optval < size)
140 {
141 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
142 {
143 zlog_err("Couldn't increase send buffer: %s\n", safe_strerror(errno));
144 }
145 }
146}
147
David Lamparteref0b0c32014-05-19 22:52:04 +0200148static void
149bgp_set_socket_ttl (struct peer *peer, int bgp_sock)
150{
Pradosh Mohapatra5d804b42013-09-12 03:37:07 +0000151 char buf[INET_ADDRSTRLEN];
152 int ret;
153
154 /* In case of peer is EBGP, we should set TTL for this connection. */
155 if (!peer->gtsm_hops && (peer_sort (peer) == BGP_PEER_EBGP))
156 {
157 ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, peer->ttl);
158 if (ret)
159 {
160 zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
161 __func__,
162 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
163 errno);
164 }
165 }
166 else if (peer->gtsm_hops)
167 {
168 /* On Linux, setting minttl without setting ttl seems to mess with the
169 outgoing ttl. Therefore setting both.
170 */
171 ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, MAXTTL);
172 if (ret)
173 {
174 zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
175 __func__,
176 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
177 errno);
178 }
179 ret = sockopt_minttl (peer->su.sa.sa_family, bgp_sock,
180 MAXTTL + 1 - peer->gtsm_hops);
181 if (ret)
182 {
183 zlog_err ("%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
184 __func__,
185 inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
186 errno);
187 }
188 }
David Lamparteref0b0c32014-05-19 22:52:04 +0200189}
190
paul718e3742002-12-13 20:15:29 +0000191/* Accept bgp connection. */
192static int
193bgp_accept (struct thread *thread)
194{
195 int bgp_sock;
196 int accept_sock;
197 union sockunion su;
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700198 struct bgp_listener *listener = THREAD_ARG(thread);
paul718e3742002-12-13 20:15:29 +0000199 struct peer *peer;
pauleb821182004-05-01 08:44:08 +0000200 struct peer *peer1;
paul718e3742002-12-13 20:15:29 +0000201 char buf[SU_ADDRSTRLEN];
202
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700203 /* Register accept thread. */
paul718e3742002-12-13 20:15:29 +0000204 accept_sock = THREAD_FD (thread);
paul718e3742002-12-13 20:15:29 +0000205 if (accept_sock < 0)
206 {
207 zlog_err ("accept_sock is nevative value %d", accept_sock);
208 return -1;
209 }
Donald Sharp774914f2015-10-14 08:50:39 -0400210 listener->thread = thread_add_read (bm->master, bgp_accept, listener, accept_sock);
paul718e3742002-12-13 20:15:29 +0000211
212 /* Accept client connection. */
213 bgp_sock = sockunion_accept (accept_sock, &su);
214 if (bgp_sock < 0)
215 {
ajs6099b3b2004-11-20 02:06:59 +0000216 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000217 return -1;
218 }
Stephen Hemminger35398582010-08-05 10:26:23 -0700219 set_nonblocking (bgp_sock);
paul718e3742002-12-13 20:15:29 +0000220
Vipin Kumar3374bef2014-01-09 00:31:22 +0000221 /* Set socket send buffer size */
222 bgp_update_sock_send_buffer_size(bgp_sock);
223
paul718e3742002-12-13 20:15:29 +0000224 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000225 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
paul718e3742002-12-13 20:15:29 +0000226
227 /* Check remote IP address */
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700228 peer1 = peer_lookup (NULL, &su);
pauleb821182004-05-01 08:44:08 +0000229 if (! peer1 || peer1->status == Idle)
paul718e3742002-12-13 20:15:29 +0000230 {
231 if (BGP_DEBUG (events, EVENTS))
232 {
pauleb821182004-05-01 08:44:08 +0000233 if (! peer1)
ajs478ba052004-12-08 20:41:23 +0000234 zlog_debug ("[Event] BGP connection IP address %s is not configured",
paul718e3742002-12-13 20:15:29 +0000235 inet_sutop (&su, buf));
236 else
ajs478ba052004-12-08 20:41:23 +0000237 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
paul718e3742002-12-13 20:15:29 +0000238 inet_sutop (&su, buf));
239 }
240 close (bgp_sock);
241 return -1;
242 }
243
David Lamparteref0b0c32014-05-19 22:52:04 +0200244 bgp_set_socket_ttl (peer1, bgp_sock);
paul718e3742002-12-13 20:15:29 +0000245
pauleb821182004-05-01 08:44:08 +0000246 /* Make dummy peer until read Open packet. */
247 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000248 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
pauleb821182004-05-01 08:44:08 +0000249
250 {
Jorge Boncompte [DTI2]682ca042012-04-10 16:57:27 +0200251 char buf[SU_ADDRSTRLEN];
pauleb821182004-05-01 08:44:08 +0000252
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700253 peer = peer_create_accept (peer1->bgp);
pauleb821182004-05-01 08:44:08 +0000254 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
255 peer->su = su;
256 peer->fd = bgp_sock;
257 peer->status = Active;
258 peer->local_id = peer1->local_id;
Timo Teräse8eb0002009-02-17 12:14:23 +0200259 peer->v_holdtime = peer1->v_holdtime;
260 peer->v_keepalive = peer1->v_keepalive;
pauleb821182004-05-01 08:44:08 +0000261
262 /* Make peer's address string. */
263 sockunion2str (&su, buf, SU_ADDRSTRLEN);
paule83e2082005-05-19 02:12:25 +0000264 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
pauleb821182004-05-01 08:44:08 +0000265 }
paul718e3742002-12-13 20:15:29 +0000266
267 BGP_EVENT_ADD (peer, TCP_connection_open);
268
269 return 0;
270}
271
272/* BGP socket bind. */
paul94f2b392005-06-28 12:44:16 +0000273static int
paul718e3742002-12-13 20:15:29 +0000274bgp_bind (struct peer *peer)
275{
276#ifdef SO_BINDTODEVICE
277 int ret;
278 struct ifreq ifreq;
Lou Berger056f3762013-04-10 12:30:04 -0700279 int myerrno;
paul718e3742002-12-13 20:15:29 +0000280
281 if (! peer->ifname)
282 return 0;
283
284 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
285
paul98f51632004-10-25 14:19:15 +0000286 if ( bgpd_privs.change (ZPRIVS_RAISE) )
287 zlog_err ("bgp_bind: could not raise privs");
288
pauleb821182004-05-01 08:44:08 +0000289 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
paul718e3742002-12-13 20:15:29 +0000290 &ifreq, sizeof (ifreq));
Lou Berger056f3762013-04-10 12:30:04 -0700291 myerrno = errno;
292
paul98f51632004-10-25 14:19:15 +0000293 if (bgpd_privs.change (ZPRIVS_LOWER) )
294 zlog_err ("bgp_bind: could not lower privs");
295
paul718e3742002-12-13 20:15:29 +0000296 if (ret < 0)
297 {
Lou Berger056f3762013-04-10 12:30:04 -0700298 zlog (peer->log, LOG_INFO, "bind to interface %s failed, errno=%d",
299 peer->ifname, myerrno);
paul718e3742002-12-13 20:15:29 +0000300 return ret;
301 }
302#endif /* SO_BINDTODEVICE */
303 return 0;
304}
305
paul94f2b392005-06-28 12:44:16 +0000306static int
David Lamparter1727d2e2010-02-02 20:18:23 +0100307bgp_update_address (struct interface *ifp, const union sockunion *dst,
308 union sockunion *addr)
paul718e3742002-12-13 20:15:29 +0000309{
Timo Teräsc1c69e42015-05-22 13:40:57 +0300310 struct prefix *p, *sel, d;
paul718e3742002-12-13 20:15:29 +0000311 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000312 struct listnode *node;
David Lamparter1727d2e2010-02-02 20:18:23 +0100313 int common;
314
Timo Teräsc1c69e42015-05-22 13:40:57 +0300315 sockunion2hostprefix (dst, &d);
David Lamparter1727d2e2010-02-02 20:18:23 +0100316 sel = NULL;
317 common = -1;
paul718e3742002-12-13 20:15:29 +0000318
paul1eb8ef22005-04-07 07:30:20 +0000319 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
paul718e3742002-12-13 20:15:29 +0000320 {
David Lamparter1727d2e2010-02-02 20:18:23 +0100321 p = connected->address;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300322 if (p->family != d.family)
David Lamparter1727d2e2010-02-02 20:18:23 +0100323 continue;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300324 if (prefix_common_bits (p, &d) > common)
David Lamparter1727d2e2010-02-02 20:18:23 +0100325 {
326 sel = p;
Timo Teräsc1c69e42015-05-22 13:40:57 +0300327 common = prefix_common_bits (sel, &d);
David Lamparter1727d2e2010-02-02 20:18:23 +0100328 }
paul718e3742002-12-13 20:15:29 +0000329 }
David Lamparter1727d2e2010-02-02 20:18:23 +0100330
David Lamparter1727d2e2010-02-02 20:18:23 +0100331 if (!sel)
332 return 1;
333
334 prefix2sockunion (sel, addr);
335 return 0;
paul718e3742002-12-13 20:15:29 +0000336}
337
338/* Update source selection. */
paul94f2b392005-06-28 12:44:16 +0000339static void
paul718e3742002-12-13 20:15:29 +0000340bgp_update_source (struct peer *peer)
341{
342 struct interface *ifp;
David Lamparter1727d2e2010-02-02 20:18:23 +0100343 union sockunion addr;
paul718e3742002-12-13 20:15:29 +0000344
345 /* Source is specified with interface name. */
346 if (peer->update_if)
347 {
348 ifp = if_lookup_by_name (peer->update_if);
349 if (! ifp)
350 return;
351
David Lamparter1727d2e2010-02-02 20:18:23 +0100352 if (bgp_update_address (ifp, &peer->su, &addr))
paul718e3742002-12-13 20:15:29 +0000353 return;
354
David Lamparter1727d2e2010-02-02 20:18:23 +0100355 sockunion_bind (peer->fd, &addr, 0, &addr);
paul718e3742002-12-13 20:15:29 +0000356 }
357
358 /* Source is specified with IP address. */
359 if (peer->update_source)
pauleb821182004-05-01 08:44:08 +0000360 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
paul718e3742002-12-13 20:15:29 +0000361}
362
363/* BGP try to connect to the peer. */
364int
365bgp_connect (struct peer *peer)
366{
367 unsigned int ifindex = 0;
368
369 /* Make socket for the peer. */
pauleb821182004-05-01 08:44:08 +0000370 peer->fd = sockunion_socket (&peer->su);
371 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000372 return -1;
373
Vipin Kumar48fc05f2014-01-09 00:31:22 +0000374 set_nonblocking (peer->fd);
375
Vipin Kumar3374bef2014-01-09 00:31:22 +0000376 /* Set socket send buffer size */
377 bgp_update_sock_send_buffer_size(peer->fd);
378
David Lamparteref0b0c32014-05-19 22:52:04 +0200379 bgp_set_socket_ttl (peer, peer->fd);
paul718e3742002-12-13 20:15:29 +0000380
pauleb821182004-05-01 08:44:08 +0000381 sockopt_reuseaddr (peer->fd);
382 sockopt_reuseport (peer->fd);
Paul Jakma0df7c912008-07-21 21:02:49 +0000383
Stephen Hemminger1423c802008-08-14 17:59:25 +0100384#ifdef IPTOS_PREC_INTERNETCONTROL
Chris Luke5c88f192011-10-18 17:26:51 +0400385 if (bgpd_privs.change (ZPRIVS_RAISE))
386 zlog_err ("%s: could not raise privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100387 if (sockunion_family (&peer->su) == AF_INET)
388 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400389# ifdef HAVE_IPV6
390 else if (sockunion_family (&peer->su) == AF_INET6)
391 setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
392# endif
Chris Luke5c88f192011-10-18 17:26:51 +0400393 if (bgpd_privs.change (ZPRIVS_LOWER))
394 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100395#endif
396
Paul Jakma0df7c912008-07-21 21:02:49 +0000397 if (peer->password)
398 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
paul718e3742002-12-13 20:15:29 +0000399
400 /* Bind socket. */
401 bgp_bind (peer);
402
403 /* Update source bind. */
404 bgp_update_source (peer);
405
406#ifdef HAVE_IPV6
407 if (peer->ifname)
Feng Lu395828e2015-05-22 11:39:55 +0200408 ifindex = ifname2ifindex (peer->ifname);
paul718e3742002-12-13 20:15:29 +0000409#endif /* HAVE_IPV6 */
410
411 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000412 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
pauleb821182004-05-01 08:44:08 +0000413 peer->host, peer->host, peer->fd);
paul718e3742002-12-13 20:15:29 +0000414
415 /* Connect to the remote peer. */
pauleb821182004-05-01 08:44:08 +0000416 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
paul718e3742002-12-13 20:15:29 +0000417}
418
419/* After TCP connection is established. Get local address and port. */
420void
421bgp_getsockname (struct peer *peer)
422{
423 if (peer->su_local)
424 {
paul22db9de2005-05-19 01:50:11 +0000425 sockunion_free (peer->su_local);
paul718e3742002-12-13 20:15:29 +0000426 peer->su_local = NULL;
427 }
428
429 if (peer->su_remote)
430 {
paul22db9de2005-05-19 01:50:11 +0000431 sockunion_free (peer->su_remote);
paul718e3742002-12-13 20:15:29 +0000432 peer->su_remote = NULL;
433 }
434
pauleb821182004-05-01 08:44:08 +0000435 peer->su_local = sockunion_getsockname (peer->fd);
436 peer->su_remote = sockunion_getpeername (peer->fd);
paul718e3742002-12-13 20:15:29 +0000437
438 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
439}
440
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700441
442static int
443bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
444{
445 struct bgp_listener *listener;
446 int ret, en;
447
448 sockopt_reuseaddr (sock);
449 sockopt_reuseport (sock);
450
Chris Luke5c88f192011-10-18 17:26:51 +0400451 if (bgpd_privs.change (ZPRIVS_RAISE))
452 zlog_err ("%s: could not raise privs", __func__);
453
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700454#ifdef IPTOS_PREC_INTERNETCONTROL
455 if (sa->sa_family == AF_INET)
456 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400457# ifdef HAVE_IPV6
458 else if (sa->sa_family == AF_INET6)
459 setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
460# endif
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700461#endif
462
David Lamparterca051262009-10-04 16:21:49 +0200463 sockopt_v6only (sa->sa_family, sock);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700464
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700465 ret = bind (sock, sa, salen);
466 en = errno;
Chris Luke5c88f192011-10-18 17:26:51 +0400467 if (bgpd_privs.change (ZPRIVS_LOWER))
468 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700469
470 if (ret < 0)
471 {
472 zlog_err ("bind: %s", safe_strerror (en));
473 return ret;
474 }
475
476 ret = listen (sock, 3);
477 if (ret < 0)
478 {
479 zlog_err ("listen: %s", safe_strerror (errno));
480 return ret;
481 }
482
483 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
484 listener->fd = sock;
485 memcpy(&listener->su, sa, salen);
Donald Sharp774914f2015-10-14 08:50:39 -0400486 listener->thread = thread_add_read (bm->master, bgp_accept, listener, sock);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700487 listnode_add (bm->listen_sockets, listener);
488
489 return 0;
490}
491
paul718e3742002-12-13 20:15:29 +0000492/* IPv6 supported version of BGP server socket setup. */
David Lamparter6d6df302014-06-28 21:12:37 +0200493#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000494int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700495bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000496{
paul718e3742002-12-13 20:15:29 +0000497 struct addrinfo *ainfo;
498 struct addrinfo *ainfo_save;
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700499 static const struct addrinfo req = {
500 .ai_family = AF_UNSPEC,
501 .ai_flags = AI_PASSIVE,
502 .ai_socktype = SOCK_STREAM,
503 };
504 int ret, count;
paul718e3742002-12-13 20:15:29 +0000505 char port_str[BUFSIZ];
506
Paul Jakma90b68762008-01-29 17:26:34 +0000507 snprintf (port_str, sizeof(port_str), "%d", port);
paul718e3742002-12-13 20:15:29 +0000508 port_str[sizeof (port_str) - 1] = '\0';
509
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700510 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
paul718e3742002-12-13 20:15:29 +0000511 if (ret != 0)
512 {
513 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
514 return -1;
515 }
516
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700517 count = 0;
518 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
paul718e3742002-12-13 20:15:29 +0000519 {
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700520 int sock;
521
paul718e3742002-12-13 20:15:29 +0000522 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
523 continue;
524
525 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
526 if (sock < 0)
527 {
ajs6099b3b2004-11-20 02:06:59 +0000528 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000529 continue;
530 }
Nick Hilliardfa411a22011-03-23 15:33:17 +0000531
532 /* if we intend to implement ttl-security, this socket needs ttl=255 */
533 sockopt_ttl (ainfo->ai_family, sock, MAXTTL);
534
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700535 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
536 if (ret == 0)
537 ++count;
538 else
539 close(sock);
paul718e3742002-12-13 20:15:29 +0000540 }
paul718e3742002-12-13 20:15:29 +0000541 freeaddrinfo (ainfo_save);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700542 if (count == 0)
543 {
544 zlog_err ("%s: no usable addresses", __func__);
545 return -1;
546 }
paul718e3742002-12-13 20:15:29 +0000547
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700548 return 0;
paul718e3742002-12-13 20:15:29 +0000549}
550#else
551/* Traditional IPv4 only version. */
552int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700553bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000554{
555 int sock;
556 int socklen;
557 struct sockaddr_in sin;
hasso4a1a2712004-02-12 15:41:38 +0000558 int ret, en;
paul718e3742002-12-13 20:15:29 +0000559
560 sock = socket (AF_INET, SOCK_STREAM, 0);
561 if (sock < 0)
562 {
ajs6099b3b2004-11-20 02:06:59 +0000563 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000564 return sock;
565 }
566
Nick Hilliardfa411a22011-03-23 15:33:17 +0000567 /* if we intend to implement ttl-security, this socket needs ttl=255 */
568 sockopt_ttl (AF_INET, sock, MAXTTL);
569
paul718e3742002-12-13 20:15:29 +0000570 memset (&sin, 0, sizeof (struct sockaddr_in));
paul718e3742002-12-13 20:15:29 +0000571 sin.sin_family = AF_INET;
572 sin.sin_port = htons (port);
573 socklen = sizeof (struct sockaddr_in);
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000574
Paul Jakma90b68762008-01-29 17:26:34 +0000575 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000576 {
Paul Jakma90b68762008-01-29 17:26:34 +0000577 zlog_err("bgp_socket: could not parse ip address %s: %s",
578 address, safe_strerror (errno));
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000579 return ret;
580 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000581#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000582 sin.sin_len = socklen;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000583#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000584
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700585 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
paul718e3742002-12-13 20:15:29 +0000586 if (ret < 0)
587 {
paul718e3742002-12-13 20:15:29 +0000588 close (sock);
589 return ret;
590 }
paul718e3742002-12-13 20:15:29 +0000591 return sock;
592}
David Lamparter6d6df302014-06-28 21:12:37 +0200593#endif /* HAVE_IPV6 */
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700594
595void
596bgp_close (void)
597{
598 struct listnode *node, *next;
599 struct bgp_listener *listener;
600
601 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
602 {
603 thread_cancel (listener->thread);
604 close (listener->fd);
605 listnode_delete (bm->listen_sockets, listener);
606 XFREE (MTYPE_BGP_LISTENER, listener);
607 }
608}