blob: b5c4c602b58a905fd5c4272407994bd38bb328e8 [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"
paul718e3742002-12-13 20:15:29 +000033
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_fsm.h"
36#include "bgpd/bgp_attr.h"
37#include "bgpd/bgp_debug.h"
38#include "bgpd/bgp_network.h"
pauledd7c242003-06-04 13:59:38 +000039
40extern struct zebra_privs_t bgpd_privs;
41
Stephen Hemmingerd023aec2009-07-21 16:27:21 -070042/* BGP listening socket. */
43struct bgp_listener
44{
45 int fd;
46 union sockunion su;
47 struct thread *thread;
48};
paul718e3742002-12-13 20:15:29 +000049
Paul Jakma0df7c912008-07-21 21:02:49 +000050/*
51 * Set MD5 key for the socket, for the given IPv4 peer address.
52 * If the password is NULL or zero-length, the option will be disabled.
53 */
54static int
55bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
56{
57 int ret = -1;
58 int en = ENOSYS;
59
60 assert (socket >= 0);
61
62#if HAVE_DECL_TCP_MD5SIG
63 ret = sockopt_tcp_signature (socket, su, password);
64 en = errno;
65#endif /* HAVE_TCP_MD5SIG */
66
67 if (ret < 0)
68 zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
69 socket, safe_strerror (en));
70
71 return ret;
72}
73
74/* Helper for bgp_connect */
75static int
76bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
77{
78 int ret = -1;
79
80#if HAVE_DECL_TCP_MD5SIG
81 if ( bgpd_privs.change (ZPRIVS_RAISE) )
82 {
83 zlog_err ("%s: could not raise privs", __func__);
84 return ret;
85 }
86
87 ret = bgp_md5_set_socket (socket, su, password);
88
89 if (bgpd_privs.change (ZPRIVS_LOWER) )
90 zlog_err ("%s: could not lower privs", __func__);
91#endif /* HAVE_TCP_MD5SIG */
92
93 return ret;
94}
95
96int
97bgp_md5_set (struct peer *peer)
98{
99 struct listnode *node;
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700100 int ret = 0;
101 struct bgp_listener *listener;
Paul Jakma0df7c912008-07-21 21:02:49 +0000102
103 if ( bgpd_privs.change (ZPRIVS_RAISE) )
104 {
105 zlog_err ("%s: could not raise privs", __func__);
106 return -1;
107 }
108
109 /* Just set the password on the listen socket(s). Outbound connections
110 * are taken care of in bgp_connect() below.
111 */
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700112 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
113 if (listener->su.sa.sa_family == peer->su.sa.sa_family)
114 {
115 ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password);
116 break;
117 }
118
Paul Jakma0df7c912008-07-21 21:02:49 +0000119 if (bgpd_privs.change (ZPRIVS_LOWER) )
120 zlog_err ("%s: could not lower privs", __func__);
121
Stephen Hemmingerd1c21ca2009-08-25 10:18:15 -0700122 return ret;
Paul Jakma0df7c912008-07-21 21:02:49 +0000123}
124
paul718e3742002-12-13 20:15:29 +0000125/* Accept bgp connection. */
126static int
127bgp_accept (struct thread *thread)
128{
129 int bgp_sock;
130 int accept_sock;
131 union sockunion su;
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700132 struct bgp_listener *listener = THREAD_ARG(thread);
paul718e3742002-12-13 20:15:29 +0000133 struct peer *peer;
pauleb821182004-05-01 08:44:08 +0000134 struct peer *peer1;
paul718e3742002-12-13 20:15:29 +0000135 char buf[SU_ADDRSTRLEN];
136
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700137 /* Register accept thread. */
paul718e3742002-12-13 20:15:29 +0000138 accept_sock = THREAD_FD (thread);
paul718e3742002-12-13 20:15:29 +0000139 if (accept_sock < 0)
140 {
141 zlog_err ("accept_sock is nevative value %d", accept_sock);
142 return -1;
143 }
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700144 listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock);
paul718e3742002-12-13 20:15:29 +0000145
146 /* Accept client connection. */
147 bgp_sock = sockunion_accept (accept_sock, &su);
148 if (bgp_sock < 0)
149 {
ajs6099b3b2004-11-20 02:06:59 +0000150 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000151 return -1;
152 }
153
154 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000155 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
paul718e3742002-12-13 20:15:29 +0000156
157 /* Check remote IP address */
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700158 peer1 = peer_lookup (NULL, &su);
pauleb821182004-05-01 08:44:08 +0000159 if (! peer1 || peer1->status == Idle)
paul718e3742002-12-13 20:15:29 +0000160 {
161 if (BGP_DEBUG (events, EVENTS))
162 {
pauleb821182004-05-01 08:44:08 +0000163 if (! peer1)
ajs478ba052004-12-08 20:41:23 +0000164 zlog_debug ("[Event] BGP connection IP address %s is not configured",
paul718e3742002-12-13 20:15:29 +0000165 inet_sutop (&su, buf));
166 else
ajs478ba052004-12-08 20:41:23 +0000167 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
paul718e3742002-12-13 20:15:29 +0000168 inet_sutop (&su, buf));
169 }
170 close (bgp_sock);
171 return -1;
172 }
173
174 /* In case of peer is EBGP, we should set TTL for this connection. */
pauleb821182004-05-01 08:44:08 +0000175 if (peer_sort (peer1) == BGP_PEER_EBGP)
176 sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
paul718e3742002-12-13 20:15:29 +0000177
pauleb821182004-05-01 08:44:08 +0000178 /* Make dummy peer until read Open packet. */
179 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000180 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
pauleb821182004-05-01 08:44:08 +0000181
182 {
183 char buf[SU_ADDRSTRLEN + 1];
184
Stephen Hemminger5bd58812009-08-06 21:05:47 -0700185 peer = peer_create_accept (peer1->bgp);
pauleb821182004-05-01 08:44:08 +0000186 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
187 peer->su = su;
188 peer->fd = bgp_sock;
189 peer->status = Active;
190 peer->local_id = peer1->local_id;
Timo Teräse8eb0002009-02-17 12:14:23 +0200191 peer->v_holdtime = peer1->v_holdtime;
192 peer->v_keepalive = peer1->v_keepalive;
pauleb821182004-05-01 08:44:08 +0000193
194 /* Make peer's address string. */
195 sockunion2str (&su, buf, SU_ADDRSTRLEN);
paule83e2082005-05-19 02:12:25 +0000196 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
pauleb821182004-05-01 08:44:08 +0000197 }
paul718e3742002-12-13 20:15:29 +0000198
199 BGP_EVENT_ADD (peer, TCP_connection_open);
200
201 return 0;
202}
203
204/* BGP socket bind. */
paul94f2b392005-06-28 12:44:16 +0000205static int
paul718e3742002-12-13 20:15:29 +0000206bgp_bind (struct peer *peer)
207{
208#ifdef SO_BINDTODEVICE
209 int ret;
210 struct ifreq ifreq;
211
212 if (! peer->ifname)
213 return 0;
214
215 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
216
paul98f51632004-10-25 14:19:15 +0000217 if ( bgpd_privs.change (ZPRIVS_RAISE) )
218 zlog_err ("bgp_bind: could not raise privs");
219
pauleb821182004-05-01 08:44:08 +0000220 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
paul718e3742002-12-13 20:15:29 +0000221 &ifreq, sizeof (ifreq));
paul98f51632004-10-25 14:19:15 +0000222
223 if (bgpd_privs.change (ZPRIVS_LOWER) )
224 zlog_err ("bgp_bind: could not lower privs");
225
paul718e3742002-12-13 20:15:29 +0000226 if (ret < 0)
227 {
228 zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
229 return ret;
230 }
231#endif /* SO_BINDTODEVICE */
232 return 0;
233}
234
paul94f2b392005-06-28 12:44:16 +0000235static int
David Lamparter5d3c53b2010-02-02 20:18:23 +0100236bgp_update_address (struct interface *ifp, const union sockunion *dst,
237 union sockunion *addr)
paul718e3742002-12-13 20:15:29 +0000238{
David Lamparter5d3c53b2010-02-02 20:18:23 +0100239 struct prefix *p, *sel, *d;
paul718e3742002-12-13 20:15:29 +0000240 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000241 struct listnode *node;
David Lamparter5d3c53b2010-02-02 20:18:23 +0100242 int common;
243
244 d = sockunion2hostprefix (dst);
245 sel = NULL;
246 common = -1;
paul718e3742002-12-13 20:15:29 +0000247
paul1eb8ef22005-04-07 07:30:20 +0000248 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
paul718e3742002-12-13 20:15:29 +0000249 {
David Lamparter5d3c53b2010-02-02 20:18:23 +0100250 p = connected->address;
251 if (p->family != d->family)
252 continue;
253 if (prefix_common_bits (p, d) > common)
254 {
255 sel = p;
256 common = prefix_common_bits (sel, d);
257 }
paul718e3742002-12-13 20:15:29 +0000258 }
David Lamparter5d3c53b2010-02-02 20:18:23 +0100259
260 prefix_free (d);
261 if (!sel)
262 return 1;
263
264 prefix2sockunion (sel, addr);
265 return 0;
paul718e3742002-12-13 20:15:29 +0000266}
267
268/* Update source selection. */
paul94f2b392005-06-28 12:44:16 +0000269static void
paul718e3742002-12-13 20:15:29 +0000270bgp_update_source (struct peer *peer)
271{
272 struct interface *ifp;
David Lamparter5d3c53b2010-02-02 20:18:23 +0100273 union sockunion addr;
paul718e3742002-12-13 20:15:29 +0000274
275 /* Source is specified with interface name. */
276 if (peer->update_if)
277 {
278 ifp = if_lookup_by_name (peer->update_if);
279 if (! ifp)
280 return;
281
David Lamparter5d3c53b2010-02-02 20:18:23 +0100282 if (bgp_update_address (ifp, &peer->su, &addr))
paul718e3742002-12-13 20:15:29 +0000283 return;
284
David Lamparter5d3c53b2010-02-02 20:18:23 +0100285 sockunion_bind (peer->fd, &addr, 0, &addr);
paul718e3742002-12-13 20:15:29 +0000286 }
287
288 /* Source is specified with IP address. */
289 if (peer->update_source)
pauleb821182004-05-01 08:44:08 +0000290 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
paul718e3742002-12-13 20:15:29 +0000291}
292
293/* BGP try to connect to the peer. */
294int
295bgp_connect (struct peer *peer)
296{
297 unsigned int ifindex = 0;
298
299 /* Make socket for the peer. */
pauleb821182004-05-01 08:44:08 +0000300 peer->fd = sockunion_socket (&peer->su);
301 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000302 return -1;
303
304 /* If we can get socket for the peer, adjest TTL and make connection. */
305 if (peer_sort (peer) == BGP_PEER_EBGP)
pauleb821182004-05-01 08:44:08 +0000306 sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
paul718e3742002-12-13 20:15:29 +0000307
pauleb821182004-05-01 08:44:08 +0000308 sockopt_reuseaddr (peer->fd);
309 sockopt_reuseport (peer->fd);
Paul Jakma0df7c912008-07-21 21:02:49 +0000310
Stephen Hemminger1423c802008-08-14 17:59:25 +0100311#ifdef IPTOS_PREC_INTERNETCONTROL
Chris Luke90d181b2011-10-18 17:26:51 +0400312 if (bgpd_privs.change (ZPRIVS_RAISE))
313 zlog_err ("%s: could not raise privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100314 if (sockunion_family (&peer->su) == AF_INET)
315 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemmingerd1e2faa2011-09-28 14:23:35 +0400316# ifdef HAVE_IPV6
317 else if (sockunion_family (&peer->su) == AF_INET6)
318 setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
319# endif
Chris Luke90d181b2011-10-18 17:26:51 +0400320 if (bgpd_privs.change (ZPRIVS_LOWER))
321 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemminger1423c802008-08-14 17:59:25 +0100322#endif
323
Paul Jakma0df7c912008-07-21 21:02:49 +0000324 if (peer->password)
325 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
paul718e3742002-12-13 20:15:29 +0000326
327 /* Bind socket. */
328 bgp_bind (peer);
329
330 /* Update source bind. */
331 bgp_update_source (peer);
332
333#ifdef HAVE_IPV6
334 if (peer->ifname)
335 ifindex = if_nametoindex (peer->ifname);
336#endif /* HAVE_IPV6 */
337
338 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000339 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
pauleb821182004-05-01 08:44:08 +0000340 peer->host, peer->host, peer->fd);
paul718e3742002-12-13 20:15:29 +0000341
342 /* Connect to the remote peer. */
pauleb821182004-05-01 08:44:08 +0000343 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
paul718e3742002-12-13 20:15:29 +0000344}
345
346/* After TCP connection is established. Get local address and port. */
347void
348bgp_getsockname (struct peer *peer)
349{
350 if (peer->su_local)
351 {
paul22db9de2005-05-19 01:50:11 +0000352 sockunion_free (peer->su_local);
paul718e3742002-12-13 20:15:29 +0000353 peer->su_local = NULL;
354 }
355
356 if (peer->su_remote)
357 {
paul22db9de2005-05-19 01:50:11 +0000358 sockunion_free (peer->su_remote);
paul718e3742002-12-13 20:15:29 +0000359 peer->su_remote = NULL;
360 }
361
pauleb821182004-05-01 08:44:08 +0000362 peer->su_local = sockunion_getsockname (peer->fd);
363 peer->su_remote = sockunion_getpeername (peer->fd);
paul718e3742002-12-13 20:15:29 +0000364
365 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
366}
367
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700368
369static int
370bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
371{
372 struct bgp_listener *listener;
373 int ret, en;
374
375 sockopt_reuseaddr (sock);
376 sockopt_reuseport (sock);
377
Chris Luke90d181b2011-10-18 17:26:51 +0400378 if (bgpd_privs.change (ZPRIVS_RAISE))
379 zlog_err ("%s: could not raise privs", __func__);
380
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700381#ifdef IPTOS_PREC_INTERNETCONTROL
382 if (sa->sa_family == AF_INET)
383 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
Stephen Hemmingerd1e2faa2011-09-28 14:23:35 +0400384# ifdef HAVE_IPV6
385 else if (sa->sa_family == AF_INET6)
386 setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
387# endif
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700388#endif
389
David Lamparter7d3d2de2009-10-04 16:21:49 +0200390 sockopt_v6only (sa->sa_family, sock);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700391
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700392 ret = bind (sock, sa, salen);
393 en = errno;
Chris Luke90d181b2011-10-18 17:26:51 +0400394 if (bgpd_privs.change (ZPRIVS_LOWER))
395 zlog_err ("%s: could not lower privs", __func__);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700396
397 if (ret < 0)
398 {
399 zlog_err ("bind: %s", safe_strerror (en));
400 return ret;
401 }
402
403 ret = listen (sock, 3);
404 if (ret < 0)
405 {
406 zlog_err ("listen: %s", safe_strerror (errno));
407 return ret;
408 }
409
410 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
411 listener->fd = sock;
412 memcpy(&listener->su, sa, salen);
413 listener->thread = thread_add_read (master, bgp_accept, listener, sock);
414 listnode_add (bm->listen_sockets, listener);
415
416 return 0;
417}
418
paul718e3742002-12-13 20:15:29 +0000419/* IPv6 supported version of BGP server socket setup. */
420#if defined (HAVE_IPV6) && ! defined (NRL)
421int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700422bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000423{
paul718e3742002-12-13 20:15:29 +0000424 struct addrinfo *ainfo;
425 struct addrinfo *ainfo_save;
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700426 static const struct addrinfo req = {
427 .ai_family = AF_UNSPEC,
428 .ai_flags = AI_PASSIVE,
429 .ai_socktype = SOCK_STREAM,
430 };
431 int ret, count;
paul718e3742002-12-13 20:15:29 +0000432 char port_str[BUFSIZ];
433
Paul Jakma90b68762008-01-29 17:26:34 +0000434 snprintf (port_str, sizeof(port_str), "%d", port);
paul718e3742002-12-13 20:15:29 +0000435 port_str[sizeof (port_str) - 1] = '\0';
436
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700437 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
paul718e3742002-12-13 20:15:29 +0000438 if (ret != 0)
439 {
440 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
441 return -1;
442 }
443
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700444 count = 0;
445 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
paul718e3742002-12-13 20:15:29 +0000446 {
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700447 int sock;
448
paul718e3742002-12-13 20:15:29 +0000449 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
450 continue;
451
452 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
453 if (sock < 0)
454 {
ajs6099b3b2004-11-20 02:06:59 +0000455 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000456 continue;
457 }
458
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700459 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
460 if (ret == 0)
461 ++count;
462 else
463 close(sock);
paul718e3742002-12-13 20:15:29 +0000464 }
paul718e3742002-12-13 20:15:29 +0000465 freeaddrinfo (ainfo_save);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700466 if (count == 0)
467 {
468 zlog_err ("%s: no usable addresses", __func__);
469 return -1;
470 }
paul718e3742002-12-13 20:15:29 +0000471
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700472 return 0;
paul718e3742002-12-13 20:15:29 +0000473}
474#else
475/* Traditional IPv4 only version. */
476int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700477bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000478{
479 int sock;
480 int socklen;
481 struct sockaddr_in sin;
hasso4a1a2712004-02-12 15:41:38 +0000482 int ret, en;
paul718e3742002-12-13 20:15:29 +0000483
484 sock = socket (AF_INET, SOCK_STREAM, 0);
485 if (sock < 0)
486 {
ajs6099b3b2004-11-20 02:06:59 +0000487 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000488 return sock;
489 }
490
paul718e3742002-12-13 20:15:29 +0000491 memset (&sin, 0, sizeof (struct sockaddr_in));
paul718e3742002-12-13 20:15:29 +0000492 sin.sin_family = AF_INET;
493 sin.sin_port = htons (port);
494 socklen = sizeof (struct sockaddr_in);
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000495
Paul Jakma90b68762008-01-29 17:26:34 +0000496 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000497 {
Paul Jakma90b68762008-01-29 17:26:34 +0000498 zlog_err("bgp_socket: could not parse ip address %s: %s",
499 address, safe_strerror (errno));
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000500 return ret;
501 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000502#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000503 sin.sin_len = socklen;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000504#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000505
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700506 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
paul718e3742002-12-13 20:15:29 +0000507 if (ret < 0)
508 {
paul718e3742002-12-13 20:15:29 +0000509 close (sock);
510 return ret;
511 }
paul718e3742002-12-13 20:15:29 +0000512 return sock;
513}
514#endif /* HAVE_IPV6 && !NRL */
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700515
516void
517bgp_close (void)
518{
519 struct listnode *node, *next;
520 struct bgp_listener *listener;
521
522 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
523 {
524 thread_cancel (listener->thread);
525 close (listener->fd);
526 listnode_delete (bm->listen_sockets, listener);
527 XFREE (MTYPE_BGP_LISTENER, listener);
528 }
529}