blob: b9cb622333591a3d73b1e7ac5d76240f41cfc0da [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};
paul718e3742002-12-13 20:15:29 +000050
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}
125
paul718e3742002-12-13 20:15:29 +0000126/* Accept bgp connection. */
127static int
128bgp_accept (struct thread *thread)
129{
130 int bgp_sock;
131 int accept_sock;
132 union sockunion su;
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700133 struct bgp_listener *listener = THREAD_ARG(thread);
paul718e3742002-12-13 20:15:29 +0000134 struct peer *peer;
pauleb821182004-05-01 08:44:08 +0000135 struct peer *peer1;
paul718e3742002-12-13 20:15:29 +0000136 char buf[SU_ADDRSTRLEN];
137
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700138 /* Register accept thread. */
paul718e3742002-12-13 20:15:29 +0000139 accept_sock = THREAD_FD (thread);
paul718e3742002-12-13 20:15:29 +0000140 if (accept_sock < 0)
141 {
142 zlog_err ("accept_sock is nevative value %d", accept_sock);
143 return -1;
144 }
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700145 listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock);
paul718e3742002-12-13 20:15:29 +0000146
147 /* Accept client connection. */
148 bgp_sock = sockunion_accept (accept_sock, &su);
149 if (bgp_sock < 0)
150 {
ajs6099b3b2004-11-20 02:06:59 +0000151 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000152 return -1;
153 }
Stephen Hemminger35398582010-08-05 10:26:23 -0700154 set_nonblocking (bgp_sock);
paul718e3742002-12-13 20:15:29 +0000155
156 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000157 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
paul718e3742002-12-13 20:15:29 +0000158
159 /* Check remote IP address */
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700160 peer1 = peer_lookup (NULL, &su);
pauleb821182004-05-01 08:44:08 +0000161 if (! peer1 || peer1->status == Idle)
paul718e3742002-12-13 20:15:29 +0000162 {
163 if (BGP_DEBUG (events, EVENTS))
164 {
pauleb821182004-05-01 08:44:08 +0000165 if (! peer1)
ajs478ba052004-12-08 20:41:23 +0000166 zlog_debug ("[Event] BGP connection IP address %s is not configured",
paul718e3742002-12-13 20:15:29 +0000167 inet_sutop (&su, buf));
168 else
ajs478ba052004-12-08 20:41:23 +0000169 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
paul718e3742002-12-13 20:15:29 +0000170 inet_sutop (&su, buf));
171 }
172 close (bgp_sock);
173 return -1;
174 }
175
176 /* In case of peer is EBGP, we should set TTL for this connection. */
Nick Hilliardfa411a22011-03-23 15:33:17 +0000177 if (peer_sort (peer1) == BGP_PEER_EBGP) {
pauleb821182004-05-01 08:44:08 +0000178 sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
Nick Hilliardfa411a22011-03-23 15:33:17 +0000179 if (peer1->gtsm_hops)
180 sockopt_minttl (peer1->su.sa.sa_family, bgp_sock, MAXTTL + 1 - peer1->gtsm_hops);
181 }
paul718e3742002-12-13 20:15:29 +0000182
pauleb821182004-05-01 08:44:08 +0000183 /* Make dummy peer until read Open packet. */
184 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000185 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
pauleb821182004-05-01 08:44:08 +0000186
187 {
188 char buf[SU_ADDRSTRLEN + 1];
189
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700190 peer = peer_create_accept (peer1->bgp);
pauleb821182004-05-01 08:44:08 +0000191 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
192 peer->su = su;
193 peer->fd = bgp_sock;
194 peer->status = Active;
195 peer->local_id = peer1->local_id;
Timo Teräse8eb0002009-02-17 12:14:23 +0200196 peer->v_holdtime = peer1->v_holdtime;
197 peer->v_keepalive = peer1->v_keepalive;
pauleb821182004-05-01 08:44:08 +0000198
199 /* Make peer's address string. */
200 sockunion2str (&su, buf, SU_ADDRSTRLEN);
paule83e2082005-05-19 02:12:25 +0000201 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
pauleb821182004-05-01 08:44:08 +0000202 }
paul718e3742002-12-13 20:15:29 +0000203
204 BGP_EVENT_ADD (peer, TCP_connection_open);
205
206 return 0;
207}
208
209/* BGP socket bind. */
paul94f2b392005-06-28 12:44:16 +0000210static int
paul718e3742002-12-13 20:15:29 +0000211bgp_bind (struct peer *peer)
212{
213#ifdef SO_BINDTODEVICE
214 int ret;
215 struct ifreq ifreq;
216
217 if (! peer->ifname)
218 return 0;
219
220 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
221
paul98f51632004-10-25 14:19:15 +0000222 if ( bgpd_privs.change (ZPRIVS_RAISE) )
223 zlog_err ("bgp_bind: could not raise privs");
224
pauleb821182004-05-01 08:44:08 +0000225 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
paul718e3742002-12-13 20:15:29 +0000226 &ifreq, sizeof (ifreq));
paul98f51632004-10-25 14:19:15 +0000227
228 if (bgpd_privs.change (ZPRIVS_LOWER) )
229 zlog_err ("bgp_bind: could not lower privs");
230
paul718e3742002-12-13 20:15:29 +0000231 if (ret < 0)
232 {
233 zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
234 return ret;
235 }
236#endif /* SO_BINDTODEVICE */
237 return 0;
238}
239
paul94f2b392005-06-28 12:44:16 +0000240static int
paul718e3742002-12-13 20:15:29 +0000241bgp_bind_address (int sock, struct in_addr *addr)
242{
243 int ret;
244 struct sockaddr_in local;
245
246 memset (&local, 0, sizeof (struct sockaddr_in));
247 local.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000248#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000249 local.sin_len = sizeof(struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000250#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000251 memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
252
pauledd7c242003-06-04 13:59:38 +0000253 if ( bgpd_privs.change (ZPRIVS_RAISE) )
254 zlog_err ("bgp_bind_address: could not raise privs");
255
paul718e3742002-12-13 20:15:29 +0000256 ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
257 if (ret < 0)
258 ;
pauledd7c242003-06-04 13:59:38 +0000259
260 if (bgpd_privs.change (ZPRIVS_LOWER) )
261 zlog_err ("bgp_bind_address: could not lower privs");
262
paul718e3742002-12-13 20:15:29 +0000263 return 0;
264}
265
David Lamparter1727d2e2010-02-02 20:18:23 +0100266static int
267bgp_update_address (struct interface *ifp, const union sockunion *dst,
268 union sockunion *addr)
paul718e3742002-12-13 20:15:29 +0000269{
David Lamparter1727d2e2010-02-02 20:18:23 +0100270 struct prefix *p, *sel, *d;
paul718e3742002-12-13 20:15:29 +0000271 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000272 struct listnode *node;
David Lamparter1727d2e2010-02-02 20:18:23 +0100273 int common;
274
275 d = sockunion2hostprefix (dst);
276 sel = NULL;
277 common = -1;
paul718e3742002-12-13 20:15:29 +0000278
paul1eb8ef22005-04-07 07:30:20 +0000279 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
paul718e3742002-12-13 20:15:29 +0000280 {
David Lamparter1727d2e2010-02-02 20:18:23 +0100281 p = connected->address;
282 if (p->family != d->family)
283 continue;
284 if (prefix_common_bits (p, d) > common)
285 {
286 sel = p;
287 common = prefix_common_bits (sel, d);
288 }
paul718e3742002-12-13 20:15:29 +0000289 }
David Lamparter1727d2e2010-02-02 20:18:23 +0100290
291 prefix_free (d);
292 if (!sel)
293 return 1;
294
295 prefix2sockunion (sel, addr);
296 return 0;
paul718e3742002-12-13 20:15:29 +0000297}
298
299/* Update source selection. */
paul94f2b392005-06-28 12:44:16 +0000300static void
paul718e3742002-12-13 20:15:29 +0000301bgp_update_source (struct peer *peer)
302{
303 struct interface *ifp;
David Lamparter1727d2e2010-02-02 20:18:23 +0100304 union sockunion addr;
paul718e3742002-12-13 20:15:29 +0000305
306 /* Source is specified with interface name. */
307 if (peer->update_if)
308 {
309 ifp = if_lookup_by_name (peer->update_if);
310 if (! ifp)
311 return;
312
David Lamparter1727d2e2010-02-02 20:18:23 +0100313 if (bgp_update_address (ifp, &peer->su, &addr))
paul718e3742002-12-13 20:15:29 +0000314 return;
315
David Lamparter1727d2e2010-02-02 20:18:23 +0100316 sockunion_bind (peer->fd, &addr, 0, &addr);
paul718e3742002-12-13 20:15:29 +0000317 }
318
319 /* Source is specified with IP address. */
320 if (peer->update_source)
pauleb821182004-05-01 08:44:08 +0000321 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
paul718e3742002-12-13 20:15:29 +0000322}
323
324/* BGP try to connect to the peer. */
325int
326bgp_connect (struct peer *peer)
327{
328 unsigned int ifindex = 0;
329
330 /* Make socket for the peer. */
pauleb821182004-05-01 08:44:08 +0000331 peer->fd = sockunion_socket (&peer->su);
332 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000333 return -1;
334
335 /* If we can get socket for the peer, adjest TTL and make connection. */
Nick Hilliardfa411a22011-03-23 15:33:17 +0000336 if (peer_sort (peer) == BGP_PEER_EBGP) {
pauleb821182004-05-01 08:44:08 +0000337 sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
Nick Hilliardfa411a22011-03-23 15:33:17 +0000338 if (peer->gtsm_hops)
339 sockopt_minttl (peer->su.sa.sa_family, peer->fd, MAXTTL + 1 - peer->gtsm_hops);
340 }
paul718e3742002-12-13 20:15:29 +0000341
pauleb821182004-05-01 08:44:08 +0000342 sockopt_reuseaddr (peer->fd);
343 sockopt_reuseport (peer->fd);
Paul Jakma0df7c912008-07-21 21:02:49 +0000344
Stephen Hemminger1423c802008-08-14 17:59:25 +0100345#ifdef IPTOS_PREC_INTERNETCONTROL
Chris Luke5c88f192011-10-18 17:26:51 +0400346 if (bgpd_privs.change (ZPRIVS_RAISE))
347 zlog_err ("%s: could not raise privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100348 if (sockunion_family (&peer->su) == AF_INET)
349 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400350# ifdef HAVE_IPV6
351 else if (sockunion_family (&peer->su) == AF_INET6)
352 setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
353# endif
Chris Luke5c88f192011-10-18 17:26:51 +0400354 if (bgpd_privs.change (ZPRIVS_LOWER))
355 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100356#endif
357
Paul Jakma0df7c912008-07-21 21:02:49 +0000358 if (peer->password)
359 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
paul718e3742002-12-13 20:15:29 +0000360
361 /* Bind socket. */
362 bgp_bind (peer);
363
364 /* Update source bind. */
365 bgp_update_source (peer);
366
367#ifdef HAVE_IPV6
368 if (peer->ifname)
369 ifindex = if_nametoindex (peer->ifname);
370#endif /* HAVE_IPV6 */
371
372 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000373 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
pauleb821182004-05-01 08:44:08 +0000374 peer->host, peer->host, peer->fd);
paul718e3742002-12-13 20:15:29 +0000375
376 /* Connect to the remote peer. */
pauleb821182004-05-01 08:44:08 +0000377 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
paul718e3742002-12-13 20:15:29 +0000378}
379
380/* After TCP connection is established. Get local address and port. */
381void
382bgp_getsockname (struct peer *peer)
383{
384 if (peer->su_local)
385 {
paul22db9de2005-05-19 01:50:11 +0000386 sockunion_free (peer->su_local);
paul718e3742002-12-13 20:15:29 +0000387 peer->su_local = NULL;
388 }
389
390 if (peer->su_remote)
391 {
paul22db9de2005-05-19 01:50:11 +0000392 sockunion_free (peer->su_remote);
paul718e3742002-12-13 20:15:29 +0000393 peer->su_remote = NULL;
394 }
395
pauleb821182004-05-01 08:44:08 +0000396 peer->su_local = sockunion_getsockname (peer->fd);
397 peer->su_remote = sockunion_getpeername (peer->fd);
paul718e3742002-12-13 20:15:29 +0000398
399 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
400}
401
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700402
403static int
404bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
405{
406 struct bgp_listener *listener;
407 int ret, en;
408
409 sockopt_reuseaddr (sock);
410 sockopt_reuseport (sock);
411
Chris Luke5c88f192011-10-18 17:26:51 +0400412 if (bgpd_privs.change (ZPRIVS_RAISE))
413 zlog_err ("%s: could not raise privs", __func__);
414
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700415#ifdef IPTOS_PREC_INTERNETCONTROL
416 if (sa->sa_family == AF_INET)
417 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400418# ifdef HAVE_IPV6
419 else if (sa->sa_family == AF_INET6)
420 setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
421# endif
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700422#endif
423
424#ifdef IPV6_V6ONLY
425 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
426 if (sa->sa_family == AF_INET6) {
427 int on = 1;
428 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
429 (void *) &on, sizeof (on));
430 }
431#endif
432
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700433 ret = bind (sock, sa, salen);
434 en = errno;
Chris Luke5c88f192011-10-18 17:26:51 +0400435 if (bgpd_privs.change (ZPRIVS_LOWER))
436 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700437
438 if (ret < 0)
439 {
440 zlog_err ("bind: %s", safe_strerror (en));
441 return ret;
442 }
443
444 ret = listen (sock, 3);
445 if (ret < 0)
446 {
447 zlog_err ("listen: %s", safe_strerror (errno));
448 return ret;
449 }
450
451 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
452 listener->fd = sock;
453 memcpy(&listener->su, sa, salen);
454 listener->thread = thread_add_read (master, bgp_accept, listener, sock);
455 listnode_add (bm->listen_sockets, listener);
456
457 return 0;
458}
459
paul718e3742002-12-13 20:15:29 +0000460/* IPv6 supported version of BGP server socket setup. */
461#if defined (HAVE_IPV6) && ! defined (NRL)
462int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700463bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000464{
paul718e3742002-12-13 20:15:29 +0000465 struct addrinfo *ainfo;
466 struct addrinfo *ainfo_save;
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700467 static const struct addrinfo req = {
468 .ai_family = AF_UNSPEC,
469 .ai_flags = AI_PASSIVE,
470 .ai_socktype = SOCK_STREAM,
471 };
472 int ret, count;
paul718e3742002-12-13 20:15:29 +0000473 char port_str[BUFSIZ];
474
Paul Jakma90b68762008-01-29 17:26:34 +0000475 snprintf (port_str, sizeof(port_str), "%d", port);
paul718e3742002-12-13 20:15:29 +0000476 port_str[sizeof (port_str) - 1] = '\0';
477
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700478 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
paul718e3742002-12-13 20:15:29 +0000479 if (ret != 0)
480 {
481 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
482 return -1;
483 }
484
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700485 count = 0;
486 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
paul718e3742002-12-13 20:15:29 +0000487 {
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700488 int sock;
489
paul718e3742002-12-13 20:15:29 +0000490 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
491 continue;
492
493 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
494 if (sock < 0)
495 {
ajs6099b3b2004-11-20 02:06:59 +0000496 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000497 continue;
498 }
Nick Hilliardfa411a22011-03-23 15:33:17 +0000499
500 /* if we intend to implement ttl-security, this socket needs ttl=255 */
501 sockopt_ttl (ainfo->ai_family, sock, MAXTTL);
502
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700503 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
504 if (ret == 0)
505 ++count;
506 else
507 close(sock);
paul718e3742002-12-13 20:15:29 +0000508 }
paul718e3742002-12-13 20:15:29 +0000509 freeaddrinfo (ainfo_save);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700510 if (count == 0)
511 {
512 zlog_err ("%s: no usable addresses", __func__);
513 return -1;
514 }
paul718e3742002-12-13 20:15:29 +0000515
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700516 return 0;
paul718e3742002-12-13 20:15:29 +0000517}
518#else
519/* Traditional IPv4 only version. */
520int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700521bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000522{
523 int sock;
524 int socklen;
525 struct sockaddr_in sin;
hasso4a1a2712004-02-12 15:41:38 +0000526 int ret, en;
paul718e3742002-12-13 20:15:29 +0000527
528 sock = socket (AF_INET, SOCK_STREAM, 0);
529 if (sock < 0)
530 {
ajs6099b3b2004-11-20 02:06:59 +0000531 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000532 return sock;
533 }
534
Nick Hilliardfa411a22011-03-23 15:33:17 +0000535 /* if we intend to implement ttl-security, this socket needs ttl=255 */
536 sockopt_ttl (AF_INET, sock, MAXTTL);
537
paul718e3742002-12-13 20:15:29 +0000538 memset (&sin, 0, sizeof (struct sockaddr_in));
paul718e3742002-12-13 20:15:29 +0000539 sin.sin_family = AF_INET;
540 sin.sin_port = htons (port);
541 socklen = sizeof (struct sockaddr_in);
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000542
Paul Jakma90b68762008-01-29 17:26:34 +0000543 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000544 {
Paul Jakma90b68762008-01-29 17:26:34 +0000545 zlog_err("bgp_socket: could not parse ip address %s: %s",
546 address, safe_strerror (errno));
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000547 return ret;
548 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000549#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000550 sin.sin_len = socklen;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000551#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000552
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700553 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
paul718e3742002-12-13 20:15:29 +0000554 if (ret < 0)
555 {
paul718e3742002-12-13 20:15:29 +0000556 close (sock);
557 return ret;
558 }
paul718e3742002-12-13 20:15:29 +0000559 return sock;
560}
561#endif /* HAVE_IPV6 && !NRL */
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700562
563void
564bgp_close (void)
565{
566 struct listnode *node, *next;
567 struct bgp_listener *listener;
568
569 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
570 {
571 thread_cancel (listener->thread);
572 close (listener->fd);
573 listnode_delete (bm->listen_sockets, listener);
574 XFREE (MTYPE_BGP_LISTENER, listener);
575 }
576}