]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP network related fucntions |
2 | Copyright (C) 1999 Kunihiro Ishiguro | |
3 | ||
4 | This file is part of GNU Zebra. | |
5 | ||
6 | GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GNU Zebra is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "thread.h" | |
24 | #include "sockunion.h" | |
0df7c91f | 25 | #include "sockopt.h" |
718e3744 | 26 | #include "memory.h" |
27 | #include "log.h" | |
28 | #include "if.h" | |
29 | #include "prefix.h" | |
30 | #include "command.h" | |
edd7c245 | 31 | #include "privs.h" |
0df7c91f | 32 | #include "linklist.h" |
fdbc8e77 | 33 | #include "network.h" |
718e3744 | 34 | |
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" | |
edd7c245 | 40 | |
41 | extern struct zebra_privs_t bgpd_privs; | |
42 | ||
d023aec4 SH |
43 | /* BGP listening socket. */ |
44 | struct bgp_listener | |
45 | { | |
46 | int fd; | |
47 | union sockunion su; | |
48 | struct thread *thread; | |
49 | }; | |
0df7c91f PJ |
50 | \f |
51 | /* | |
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 | */ | |
55 | static int | |
56 | bgp_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 */ | |
76 | static int | |
77 | bgp_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 | ||
97 | int | |
98 | bgp_md5_set (struct peer *peer) | |
99 | { | |
100 | struct listnode *node; | |
d1c21cab SH |
101 | int ret = 0; |
102 | struct bgp_listener *listener; | |
0df7c91f PJ |
103 | |
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 | */ | |
d1c21cab SH |
113 | 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 | ||
0df7c91f PJ |
120 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
121 | zlog_err ("%s: could not lower privs", __func__); | |
122 | ||
d1c21cab | 123 | return ret; |
0df7c91f | 124 | } |
3374bef0 VK |
125 | |
126 | /* Update BGP socket send buffer size */ | |
127 | static void | |
128 | bgp_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 | ||
718e3744 | 148 | /* Accept bgp connection. */ |
149 | static int | |
150 | bgp_accept (struct thread *thread) | |
151 | { | |
152 | int bgp_sock; | |
153 | int accept_sock; | |
154 | union sockunion su; | |
5bd58818 | 155 | struct bgp_listener *listener = THREAD_ARG(thread); |
718e3744 | 156 | struct peer *peer; |
eb821189 | 157 | struct peer *peer1; |
718e3744 | 158 | char buf[SU_ADDRSTRLEN]; |
159 | ||
5bd58818 | 160 | /* Register accept thread. */ |
718e3744 | 161 | accept_sock = THREAD_FD (thread); |
718e3744 | 162 | if (accept_sock < 0) |
163 | { | |
164 | zlog_err ("accept_sock is nevative value %d", accept_sock); | |
165 | return -1; | |
166 | } | |
5bd58818 | 167 | listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock); |
718e3744 | 168 | |
169 | /* Accept client connection. */ | |
170 | bgp_sock = sockunion_accept (accept_sock, &su); | |
171 | if (bgp_sock < 0) | |
172 | { | |
6099b3b5 | 173 | zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno)); |
718e3744 | 174 | return -1; |
175 | } | |
35398589 | 176 | set_nonblocking (bgp_sock); |
718e3744 | 177 | |
3374bef0 VK |
178 | /* Set socket send buffer size */ |
179 | bgp_update_sock_send_buffer_size(bgp_sock); | |
180 | ||
718e3744 | 181 | if (BGP_DEBUG (events, EVENTS)) |
478ba054 | 182 | zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf)); |
718e3744 | 183 | |
184 | /* Check remote IP address */ | |
5bd58818 | 185 | peer1 = peer_lookup (NULL, &su); |
eb821189 | 186 | if (! peer1 || peer1->status == Idle) |
718e3744 | 187 | { |
188 | if (BGP_DEBUG (events, EVENTS)) | |
189 | { | |
eb821189 | 190 | if (! peer1) |
478ba054 | 191 | zlog_debug ("[Event] BGP connection IP address %s is not configured", |
718e3744 | 192 | inet_sutop (&su, buf)); |
193 | else | |
478ba054 | 194 | zlog_debug ("[Event] BGP connection IP address %s is Idle state", |
718e3744 | 195 | inet_sutop (&su, buf)); |
196 | } | |
197 | close (bgp_sock); | |
198 | return -1; | |
199 | } | |
200 | ||
201 | /* In case of peer is EBGP, we should set TTL for this connection. */ | |
6d85b15b | 202 | if (peer1->sort == BGP_PEER_EBGP) { |
eb821189 | 203 | sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl); |
fa411a21 NH |
204 | if (peer1->gtsm_hops) |
205 | sockopt_minttl (peer1->su.sa.sa_family, bgp_sock, MAXTTL + 1 - peer1->gtsm_hops); | |
206 | } | |
718e3744 | 207 | |
eb821189 | 208 | /* Make dummy peer until read Open packet. */ |
209 | if (BGP_DEBUG (events, EVENTS)) | |
478ba054 | 210 | zlog_debug ("[Event] Make dummy peer structure until read Open packet"); |
eb821189 | 211 | |
212 | { | |
682ca04c | 213 | char buf[SU_ADDRSTRLEN]; |
eb821189 | 214 | |
5bd58818 | 215 | peer = peer_create_accept (peer1->bgp); |
eb821189 | 216 | SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER); |
217 | peer->su = su; | |
218 | peer->fd = bgp_sock; | |
219 | peer->status = Active; | |
220 | peer->local_id = peer1->local_id; | |
e8eb000e TT |
221 | peer->v_holdtime = peer1->v_holdtime; |
222 | peer->v_keepalive = peer1->v_keepalive; | |
718e3744 | 223 | |
eb821189 | 224 | /* Make peer's address string. */ |
225 | sockunion2str (&su, buf, SU_ADDRSTRLEN); | |
e83e2080 | 226 | peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf); |
eb821189 | 227 | } |
718e3744 | 228 | |
229 | BGP_EVENT_ADD (peer, TCP_connection_open); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | /* BGP socket bind. */ | |
94f2b392 | 235 | static int |
718e3744 | 236 | bgp_bind (struct peer *peer) |
237 | { | |
238 | #ifdef SO_BINDTODEVICE | |
239 | int ret; | |
240 | struct ifreq ifreq; | |
241 | ||
242 | if (! peer->ifname) | |
243 | return 0; | |
244 | ||
245 | strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name)); | |
246 | ||
98f5163c | 247 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
248 | zlog_err ("bgp_bind: could not raise privs"); | |
249 | ||
eb821189 | 250 | ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, |
718e3744 | 251 | &ifreq, sizeof (ifreq)); |
98f5163c | 252 | |
253 | if (bgpd_privs.change (ZPRIVS_LOWER) ) | |
254 | zlog_err ("bgp_bind: could not lower privs"); | |
255 | ||
718e3744 | 256 | if (ret < 0) |
257 | { | |
258 | zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname); | |
259 | return ret; | |
260 | } | |
261 | #endif /* SO_BINDTODEVICE */ | |
262 | return 0; | |
263 | } | |
264 | ||
1727d2e2 DL |
265 | static int |
266 | bgp_update_address (struct interface *ifp, const union sockunion *dst, | |
267 | union sockunion *addr) | |
718e3744 | 268 | { |
1727d2e2 | 269 | struct prefix *p, *sel, *d; |
718e3744 | 270 | struct connected *connected; |
52dc7ee6 | 271 | struct listnode *node; |
1727d2e2 DL |
272 | int common; |
273 | ||
274 | d = sockunion2hostprefix (dst); | |
275 | sel = NULL; | |
276 | common = -1; | |
718e3744 | 277 | |
1eb8ef25 | 278 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected)) |
718e3744 | 279 | { |
1727d2e2 DL |
280 | p = connected->address; |
281 | if (p->family != d->family) | |
282 | continue; | |
283 | if (prefix_common_bits (p, d) > common) | |
284 | { | |
285 | sel = p; | |
286 | common = prefix_common_bits (sel, d); | |
287 | } | |
718e3744 | 288 | } |
1727d2e2 DL |
289 | |
290 | prefix_free (d); | |
291 | if (!sel) | |
292 | return 1; | |
293 | ||
294 | prefix2sockunion (sel, addr); | |
295 | return 0; | |
718e3744 | 296 | } |
297 | ||
298 | /* Update source selection. */ | |
94f2b392 | 299 | static void |
718e3744 | 300 | bgp_update_source (struct peer *peer) |
301 | { | |
302 | struct interface *ifp; | |
1727d2e2 | 303 | union sockunion addr; |
718e3744 | 304 | |
305 | /* Source is specified with interface name. */ | |
306 | if (peer->update_if) | |
307 | { | |
308 | ifp = if_lookup_by_name (peer->update_if); | |
309 | if (! ifp) | |
310 | return; | |
311 | ||
1727d2e2 | 312 | if (bgp_update_address (ifp, &peer->su, &addr)) |
718e3744 | 313 | return; |
314 | ||
1727d2e2 | 315 | sockunion_bind (peer->fd, &addr, 0, &addr); |
718e3744 | 316 | } |
317 | ||
318 | /* Source is specified with IP address. */ | |
319 | if (peer->update_source) | |
eb821189 | 320 | sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source); |
718e3744 | 321 | } |
322 | ||
323 | /* BGP try to connect to the peer. */ | |
324 | int | |
325 | bgp_connect (struct peer *peer) | |
326 | { | |
327 | unsigned int ifindex = 0; | |
328 | ||
329 | /* Make socket for the peer. */ | |
eb821189 | 330 | peer->fd = sockunion_socket (&peer->su); |
331 | if (peer->fd < 0) | |
718e3744 | 332 | return -1; |
333 | ||
48fc05fb VK |
334 | set_nonblocking (peer->fd); |
335 | ||
3374bef0 VK |
336 | /* Set socket send buffer size */ |
337 | bgp_update_sock_send_buffer_size(peer->fd); | |
338 | ||
718e3744 | 339 | /* If we can get socket for the peer, adjest TTL and make connection. */ |
6d85b15b | 340 | if (peer->sort == BGP_PEER_EBGP) { |
eb821189 | 341 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
fa411a21 NH |
342 | if (peer->gtsm_hops) |
343 | sockopt_minttl (peer->su.sa.sa_family, peer->fd, MAXTTL + 1 - peer->gtsm_hops); | |
344 | } | |
718e3744 | 345 | |
eb821189 | 346 | sockopt_reuseaddr (peer->fd); |
347 | sockopt_reuseport (peer->fd); | |
0df7c91f | 348 | |
1423c809 | 349 | #ifdef IPTOS_PREC_INTERNETCONTROL |
5c88f19d CL |
350 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
351 | zlog_err ("%s: could not raise privs", __func__); | |
1423c809 SH |
352 | if (sockunion_family (&peer->su) == AF_INET) |
353 | setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL); | |
6d0732c8 SH |
354 | # ifdef HAVE_IPV6 |
355 | else if (sockunion_family (&peer->su) == AF_INET6) | |
356 | setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL); | |
357 | # endif | |
5c88f19d CL |
358 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
359 | zlog_err ("%s: could not lower privs", __func__); | |
1423c809 SH |
360 | #endif |
361 | ||
0df7c91f PJ |
362 | if (peer->password) |
363 | bgp_md5_set_connect (peer->fd, &peer->su, peer->password); | |
718e3744 | 364 | |
365 | /* Bind socket. */ | |
366 | bgp_bind (peer); | |
367 | ||
368 | /* Update source bind. */ | |
369 | bgp_update_source (peer); | |
370 | ||
371 | #ifdef HAVE_IPV6 | |
372 | if (peer->ifname) | |
373 | ifindex = if_nametoindex (peer->ifname); | |
374 | #endif /* HAVE_IPV6 */ | |
375 | ||
376 | if (BGP_DEBUG (events, EVENTS)) | |
478ba054 | 377 | plog_debug (peer->log, "%s [Event] Connect start to %s fd %d", |
eb821189 | 378 | peer->host, peer->host, peer->fd); |
718e3744 | 379 | |
380 | /* Connect to the remote peer. */ | |
eb821189 | 381 | return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex); |
718e3744 | 382 | } |
383 | ||
384 | /* After TCP connection is established. Get local address and port. */ | |
385 | void | |
386 | bgp_getsockname (struct peer *peer) | |
387 | { | |
388 | if (peer->su_local) | |
389 | { | |
22db9dec | 390 | sockunion_free (peer->su_local); |
718e3744 | 391 | peer->su_local = NULL; |
392 | } | |
393 | ||
394 | if (peer->su_remote) | |
395 | { | |
22db9dec | 396 | sockunion_free (peer->su_remote); |
718e3744 | 397 | peer->su_remote = NULL; |
398 | } | |
399 | ||
eb821189 | 400 | peer->su_local = sockunion_getsockname (peer->fd); |
401 | peer->su_remote = sockunion_getpeername (peer->fd); | |
718e3744 | 402 | |
403 | bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer); | |
404 | } | |
405 | ||
d023aec4 SH |
406 | |
407 | static int | |
408 | bgp_listener (int sock, struct sockaddr *sa, socklen_t salen) | |
409 | { | |
410 | struct bgp_listener *listener; | |
411 | int ret, en; | |
412 | ||
413 | sockopt_reuseaddr (sock); | |
414 | sockopt_reuseport (sock); | |
415 | ||
5c88f19d CL |
416 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
417 | zlog_err ("%s: could not raise privs", __func__); | |
418 | ||
d023aec4 SH |
419 | #ifdef IPTOS_PREC_INTERNETCONTROL |
420 | if (sa->sa_family == AF_INET) | |
421 | setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL); | |
6d0732c8 SH |
422 | # ifdef HAVE_IPV6 |
423 | else if (sa->sa_family == AF_INET6) | |
424 | setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL); | |
425 | # endif | |
d023aec4 SH |
426 | #endif |
427 | ||
ca051269 | 428 | sockopt_v6only (sa->sa_family, sock); |
d023aec4 | 429 | |
d023aec4 SH |
430 | ret = bind (sock, sa, salen); |
431 | en = errno; | |
5c88f19d CL |
432 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
433 | zlog_err ("%s: could not lower privs", __func__); | |
d023aec4 SH |
434 | |
435 | if (ret < 0) | |
436 | { | |
437 | zlog_err ("bind: %s", safe_strerror (en)); | |
438 | return ret; | |
439 | } | |
440 | ||
441 | ret = listen (sock, 3); | |
442 | if (ret < 0) | |
443 | { | |
444 | zlog_err ("listen: %s", safe_strerror (errno)); | |
445 | return ret; | |
446 | } | |
447 | ||
448 | listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener)); | |
449 | listener->fd = sock; | |
450 | memcpy(&listener->su, sa, salen); | |
451 | listener->thread = thread_add_read (master, bgp_accept, listener, sock); | |
452 | listnode_add (bm->listen_sockets, listener); | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
718e3744 | 457 | /* IPv6 supported version of BGP server socket setup. */ |
458 | #if defined (HAVE_IPV6) && ! defined (NRL) | |
459 | int | |
d023aec4 | 460 | bgp_socket (unsigned short port, const char *address) |
718e3744 | 461 | { |
718e3744 | 462 | struct addrinfo *ainfo; |
463 | struct addrinfo *ainfo_save; | |
d023aec4 SH |
464 | static const struct addrinfo req = { |
465 | .ai_family = AF_UNSPEC, | |
466 | .ai_flags = AI_PASSIVE, | |
467 | .ai_socktype = SOCK_STREAM, | |
468 | }; | |
469 | int ret, count; | |
718e3744 | 470 | char port_str[BUFSIZ]; |
471 | ||
90b68769 | 472 | snprintf (port_str, sizeof(port_str), "%d", port); |
718e3744 | 473 | port_str[sizeof (port_str) - 1] = '\0'; |
474 | ||
d023aec4 | 475 | ret = getaddrinfo (address, port_str, &req, &ainfo_save); |
718e3744 | 476 | if (ret != 0) |
477 | { | |
478 | zlog_err ("getaddrinfo: %s", gai_strerror (ret)); | |
479 | return -1; | |
480 | } | |
481 | ||
d023aec4 SH |
482 | count = 0; |
483 | for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) | |
718e3744 | 484 | { |
d023aec4 SH |
485 | int sock; |
486 | ||
718e3744 | 487 | if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) |
488 | continue; | |
489 | ||
490 | sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol); | |
491 | if (sock < 0) | |
492 | { | |
6099b3b5 | 493 | zlog_err ("socket: %s", safe_strerror (errno)); |
718e3744 | 494 | continue; |
495 | } | |
fa411a21 NH |
496 | |
497 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ | |
498 | sockopt_ttl (ainfo->ai_family, sock, MAXTTL); | |
499 | ||
d023aec4 SH |
500 | ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen); |
501 | if (ret == 0) | |
502 | ++count; | |
503 | else | |
504 | close(sock); | |
718e3744 | 505 | } |
718e3744 | 506 | freeaddrinfo (ainfo_save); |
d023aec4 SH |
507 | if (count == 0) |
508 | { | |
509 | zlog_err ("%s: no usable addresses", __func__); | |
510 | return -1; | |
511 | } | |
718e3744 | 512 | |
d023aec4 | 513 | return 0; |
718e3744 | 514 | } |
515 | #else | |
516 | /* Traditional IPv4 only version. */ | |
517 | int | |
d023aec4 | 518 | bgp_socket (unsigned short port, const char *address) |
718e3744 | 519 | { |
520 | int sock; | |
521 | int socklen; | |
522 | struct sockaddr_in sin; | |
4a1a2716 | 523 | int ret, en; |
718e3744 | 524 | |
525 | sock = socket (AF_INET, SOCK_STREAM, 0); | |
526 | if (sock < 0) | |
527 | { | |
6099b3b5 | 528 | zlog_err ("socket: %s", safe_strerror (errno)); |
718e3744 | 529 | return sock; |
530 | } | |
531 | ||
fa411a21 NH |
532 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ |
533 | sockopt_ttl (AF_INET, sock, MAXTTL); | |
534 | ||
718e3744 | 535 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
718e3744 | 536 | sin.sin_family = AF_INET; |
537 | sin.sin_port = htons (port); | |
538 | socklen = sizeof (struct sockaddr_in); | |
3a02d1f7 | 539 | |
90b68769 | 540 | if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1)) |
3a02d1f7 | 541 | { |
90b68769 PJ |
542 | zlog_err("bgp_socket: could not parse ip address %s: %s", |
543 | address, safe_strerror (errno)); | |
3a02d1f7 PJ |
544 | return ret; |
545 | } | |
6f0e3f6e | 546 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 547 | sin.sin_len = socklen; |
6f0e3f6e | 548 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 549 | |
d023aec4 | 550 | ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen); |
718e3744 | 551 | if (ret < 0) |
552 | { | |
718e3744 | 553 | close (sock); |
554 | return ret; | |
555 | } | |
718e3744 | 556 | return sock; |
557 | } | |
558 | #endif /* HAVE_IPV6 && !NRL */ | |
d023aec4 SH |
559 | |
560 | void | |
561 | bgp_close (void) | |
562 | { | |
563 | struct listnode *node, *next; | |
564 | struct bgp_listener *listener; | |
565 | ||
566 | for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener)) | |
567 | { | |
568 | thread_cancel (listener->thread); | |
569 | close (listener->fd); | |
570 | listnode_delete (bm->listen_sockets, listener); | |
571 | XFREE (MTYPE_BGP_LISTENER, listener); | |
572 | } | |
573 | } |