]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* BGP network related fucntions |
896014f4 | 3 | * Copyright (C) 1999 Kunihiro Ishiguro |
896014f4 | 4 | */ |
718e3744 | 5 | |
6 | #include <zebra.h> | |
7 | ||
cb37cb33 | 8 | #include "event.h" |
718e3744 | 9 | #include "sockunion.h" |
0df7c91f | 10 | #include "sockopt.h" |
718e3744 | 11 | #include "memory.h" |
12 | #include "log.h" | |
13 | #include "if.h" | |
14 | #include "prefix.h" | |
15 | #include "command.h" | |
edd7c245 | 16 | #include "privs.h" |
0df7c91f | 17 | #include "linklist.h" |
fdbc8e77 | 18 | #include "network.h" |
3f9c7369 | 19 | #include "queue.h" |
66e373ae | 20 | #include "hash.h" |
039f3a34 | 21 | #include "filter.h" |
61cf4b37 | 22 | #include "ns.h" |
174482ef | 23 | #include "lib_errors.h" |
17cdd31e | 24 | #include "nexthop.h" |
718e3744 | 25 | |
26 | #include "bgpd/bgpd.h" | |
1ff9a340 | 27 | #include "bgpd/bgp_open.h" |
718e3744 | 28 | #include "bgpd/bgp_fsm.h" |
29 | #include "bgpd/bgp_attr.h" | |
30 | #include "bgpd/bgp_debug.h" | |
14454c9f | 31 | #include "bgpd/bgp_errors.h" |
718e3744 | 32 | #include "bgpd/bgp_network.h" |
17cdd31e | 33 | #include "bgpd/bgp_zebra.h" |
dcd5ba55 | 34 | #include "bgpd/bgp_nht.h" |
edd7c245 | 35 | |
36 | extern struct zebra_privs_t bgpd_privs; | |
37 | ||
97896a91 | 38 | static char *bgp_get_bound_name(struct peer *peer); |
f2345335 | 39 | |
2a0e69ae DS |
40 | void bgp_dump_listener_info(struct vty *vty) |
41 | { | |
42 | struct listnode *node; | |
43 | struct bgp_listener *listener; | |
44 | ||
45 | vty_out(vty, "Name fd Address\n"); | |
46 | vty_out(vty, "---------------------------\n"); | |
47e12884 DA |
47 | for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener)) |
48 | vty_out(vty, "%-16s %d %pSU\n", | |
2a0e69ae | 49 | listener->name ? listener->name : VRF_DEFAULT_NAME, |
47e12884 | 50 | listener->fd, &listener->su); |
2a0e69ae DS |
51 | } |
52 | ||
0df7c91f PJ |
53 | /* |
54 | * Set MD5 key for the socket, for the given IPv4 peer address. | |
55 | * If the password is NULL or zero-length, the option will be disabled. | |
56 | */ | |
d62a17ae | 57 | static int bgp_md5_set_socket(int socket, union sockunion *su, |
9e7d9a61 | 58 | uint16_t prefixlen, const char *password) |
0df7c91f | 59 | { |
d62a17ae | 60 | int ret = -1; |
61 | int en = ENOSYS; | |
ea8b7c71 | 62 | #if HAVE_DECL_TCP_MD5SIG |
d62a17ae | 63 | union sockunion su2; |
ea8b7c71 | 64 | #endif /* HAVE_TCP_MD5SIG */ |
d62a17ae | 65 | |
66 | assert(socket >= 0); | |
67 | ||
68 | #if HAVE_DECL_TCP_MD5SIG | |
69 | /* Ensure there is no extraneous port information. */ | |
70 | memcpy(&su2, su, sizeof(union sockunion)); | |
71 | if (su2.sa.sa_family == AF_INET) | |
72 | su2.sin.sin_port = 0; | |
73 | else | |
74 | su2.sin6.sin6_port = 0; | |
9e7d9a61 QY |
75 | |
76 | /* For addresses, use the non-extended signature functionality */ | |
936fbaef | 77 | if ((su2.sa.sa_family == AF_INET && prefixlen == IPV4_MAX_BITLEN) |
f4d81e55 | 78 | || (su2.sa.sa_family == AF_INET6 && prefixlen == IPV6_MAX_BITLEN)) |
9e7d9a61 QY |
79 | ret = sockopt_tcp_signature(socket, &su2, password); |
80 | else | |
81 | ret = sockopt_tcp_signature_ext(socket, &su2, prefixlen, | |
82 | password); | |
d62a17ae | 83 | en = errno; |
0df7c91f | 84 | #endif /* HAVE_TCP_MD5SIG */ |
0df7c91f | 85 | |
9e7d9a61 | 86 | if (ret < 0) { |
9e7d9a61 QY |
87 | switch (ret) { |
88 | case -2: | |
89 | flog_warn( | |
90 | EC_BGP_NO_TCP_MD5, | |
47e12884 DA |
91 | "Unable to set TCP MD5 option on socket for peer %pSU (sock=%d): This platform does not support MD5 auth for prefixes", |
92 | su, socket); | |
9e7d9a61 QY |
93 | break; |
94 | default: | |
95 | flog_warn( | |
96 | EC_BGP_NO_TCP_MD5, | |
47e12884 DA |
97 | "Unable to set TCP MD5 option on socket for peer %pSU (sock=%d): %s", |
98 | su, socket, safe_strerror(en)); | |
9e7d9a61 QY |
99 | } |
100 | } | |
d62a17ae | 101 | |
102 | return ret; | |
0df7c91f PJ |
103 | } |
104 | ||
105 | /* Helper for bgp_connect */ | |
d62a17ae | 106 | static int bgp_md5_set_connect(int socket, union sockunion *su, |
9e7d9a61 | 107 | uint16_t prefixlen, const char *password) |
0df7c91f | 108 | { |
d62a17ae | 109 | int ret = -1; |
110 | ||
111 | #if HAVE_DECL_TCP_MD5SIG | |
0cf6db21 | 112 | frr_with_privs(&bgpd_privs) { |
9e7d9a61 | 113 | ret = bgp_md5_set_socket(socket, su, prefixlen, password); |
d62a17ae | 114 | } |
0df7c91f | 115 | #endif /* HAVE_TCP_MD5SIG */ |
d62a17ae | 116 | |
117 | return ret; | |
0df7c91f PJ |
118 | } |
119 | ||
d62a17ae | 120 | static int bgp_md5_set_password(struct peer *peer, const char *password) |
0df7c91f | 121 | { |
d62a17ae | 122 | struct listnode *node; |
123 | int ret = 0; | |
124 | struct bgp_listener *listener; | |
125 | ||
9e7d9a61 QY |
126 | /* |
127 | * Set or unset the password on the listen socket(s). Outbound | |
6bb30c2c | 128 | * connections are taken care of in bgp_connect() below. |
d62a17ae | 129 | */ |
0cf6db21 | 130 | frr_with_privs(&bgpd_privs) { |
6bb30c2c | 131 | for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener)) |
a4faae3a PR |
132 | if (listener->su.sa.sa_family == |
133 | peer->su.sa.sa_family) { | |
9e7d9a61 QY |
134 | uint16_t prefixlen = |
135 | peer->su.sa.sa_family == AF_INET | |
936fbaef | 136 | ? IPV4_MAX_BITLEN |
f4d81e55 | 137 | : IPV6_MAX_BITLEN; |
a4faae3a PR |
138 | |
139 | /* | |
140 | * if we have stored a BGP vrf instance in the | |
141 | * listener it must match the bgp instance in | |
142 | * the peer otherwise the peer bgp instance | |
143 | * must be the default vrf or a view instance | |
144 | */ | |
145 | if (!listener->bgp) { | |
2c1eba8e | 146 | if (peer->bgp->vrf_id != VRF_DEFAULT) |
a4faae3a PR |
147 | continue; |
148 | } else if (listener->bgp != peer->bgp) | |
149 | continue; | |
9e7d9a61 | 150 | |
6bb30c2c | 151 | ret = bgp_md5_set_socket(listener->fd, |
9e7d9a61 QY |
152 | &peer->su, prefixlen, |
153 | password); | |
154 | break; | |
155 | } | |
156 | } | |
157 | return ret; | |
158 | } | |
159 | ||
a4faae3a | 160 | int bgp_md5_set_prefix(struct bgp *bgp, struct prefix *p, const char *password) |
9e7d9a61 QY |
161 | { |
162 | int ret = 0; | |
163 | union sockunion su; | |
164 | struct listnode *node; | |
165 | struct bgp_listener *listener; | |
166 | ||
167 | /* Set or unset the password on the listen socket(s). */ | |
0cf6db21 | 168 | frr_with_privs(&bgpd_privs) { |
9e7d9a61 | 169 | for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener)) |
a4faae3a PR |
170 | if (listener->su.sa.sa_family == p->family |
171 | && ((bgp->vrf_id == VRF_DEFAULT) | |
172 | || (listener->bgp == bgp))) { | |
9e7d9a61 QY |
173 | prefix2sockunion(p, &su); |
174 | ret = bgp_md5_set_socket(listener->fd, &su, | |
175 | p->prefixlen, | |
176 | password); | |
6bb30c2c DL |
177 | break; |
178 | } | |
179 | } | |
9e7d9a61 | 180 | |
d62a17ae | 181 | return ret; |
0df7c91f | 182 | } |
3374bef0 | 183 | |
a4faae3a | 184 | int bgp_md5_unset_prefix(struct bgp *bgp, struct prefix *p) |
9e7d9a61 | 185 | { |
a4faae3a | 186 | return bgp_md5_set_prefix(bgp, p, NULL); |
9e7d9a61 QY |
187 | } |
188 | ||
d62a17ae | 189 | int bgp_md5_set(struct peer *peer) |
89ca90fa | 190 | { |
d62a17ae | 191 | /* Set the password from listen socket. */ |
192 | return bgp_md5_set_password(peer, peer->password); | |
89ca90fa | 193 | } |
194 | ||
d1adb448 PG |
195 | static void bgp_update_setsockopt_tcp_keepalive(struct bgp *bgp, int fd) |
196 | { | |
197 | if (!bgp) | |
198 | return; | |
199 | if (bgp->tcp_keepalive_idle != 0) { | |
200 | int ret; | |
201 | ||
202 | ret = setsockopt_tcp_keepalive(fd, bgp->tcp_keepalive_idle, | |
203 | bgp->tcp_keepalive_intvl, | |
204 | bgp->tcp_keepalive_probes); | |
205 | if (ret < 0) | |
206 | zlog_err( | |
207 | "Can't set TCP keepalive on socket %d, idle %u intvl %u probes %u", | |
208 | fd, bgp->tcp_keepalive_idle, | |
209 | bgp->tcp_keepalive_intvl, | |
210 | bgp->tcp_keepalive_probes); | |
211 | } | |
212 | } | |
213 | ||
d62a17ae | 214 | int bgp_md5_unset(struct peer *peer) |
89ca90fa | 215 | { |
d62a17ae | 216 | /* Unset the password from listen socket. */ |
217 | return bgp_md5_set_password(peer, NULL); | |
89ca90fa | 218 | } |
219 | ||
d62a17ae | 220 | int bgp_set_socket_ttl(struct peer *peer, int bgp_sock) |
ef0b0c3e | 221 | { |
d62a17ae | 222 | int ret = 0; |
223 | ||
69ecbc6a | 224 | if (!peer->gtsm_hops) { |
d62a17ae | 225 | ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, peer->ttl); |
226 | if (ret) { | |
af4c2728 | 227 | flog_err( |
450971aa | 228 | EC_LIB_SOCKET, |
c0d72166 DS |
229 | "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d", |
230 | __func__, &peer->remote_id, errno); | |
d62a17ae | 231 | return ret; |
232 | } | |
69ecbc6a | 233 | } else { |
d62a17ae | 234 | /* On Linux, setting minttl without setting ttl seems to mess |
235 | with the | |
236 | outgoing ttl. Therefore setting both. | |
237 | */ | |
238 | ret = sockopt_ttl(peer->su.sa.sa_family, bgp_sock, MAXTTL); | |
239 | if (ret) { | |
af4c2728 | 240 | flog_err( |
450971aa | 241 | EC_LIB_SOCKET, |
c0d72166 DS |
242 | "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d", |
243 | __func__, &peer->remote_id, errno); | |
d62a17ae | 244 | return ret; |
245 | } | |
246 | ret = sockopt_minttl(peer->su.sa.sa_family, bgp_sock, | |
247 | MAXTTL + 1 - peer->gtsm_hops); | |
248 | if (ret) { | |
af4c2728 | 249 | flog_err( |
450971aa | 250 | EC_LIB_SOCKET, |
c0d72166 DS |
251 | "%s: Can't set MinTTL on peer (rtrid %pI4) socket, err = %d", |
252 | __func__, &peer->remote_id, errno); | |
d62a17ae | 253 | return ret; |
254 | } | |
255 | } | |
256 | ||
257 | return ret; | |
ef0b0c3e DL |
258 | } |
259 | ||
8dee0396 | 260 | /* |
261 | * Obtain the BGP instance that the incoming connection should be processed | |
262 | * against. This is important because more than one VRF could be using the | |
263 | * same IP address space. The instance is got by obtaining the device to | |
264 | * which the incoming connection is bound to. This could either be a VRF | |
265 | * or it could be an interface, which in turn determines the VRF. | |
266 | */ | |
d62a17ae | 267 | static int bgp_get_instance_for_inc_conn(int sock, struct bgp **bgp_inst) |
8dee0396 | 268 | { |
120c6587 | 269 | #ifndef SO_BINDTODEVICE |
d62a17ae | 270 | /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here |
271 | * anyway since the assumption is that the interface name returned by | |
272 | * getsockopt() is useful in identifying the VRF, particularly with | |
273 | * Linux's | |
274 | * VRF l3master device. The whole mechanism is specific to Linux, so... | |
275 | * when other platforms add VRF support, this will need handling here as | |
276 | * well. (or, some restructuring) */ | |
277 | *bgp_inst = bgp_get_default(); | |
278 | return !*bgp_inst; | |
120c6587 DL |
279 | |
280 | #else | |
d62a17ae | 281 | char name[VRF_NAMSIZ + 1]; |
282 | socklen_t name_len = VRF_NAMSIZ; | |
283 | struct bgp *bgp; | |
284 | int rc; | |
285 | struct listnode *node, *nnode; | |
286 | ||
287 | *bgp_inst = NULL; | |
288 | name[0] = '\0'; | |
289 | rc = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, name, &name_len); | |
290 | if (rc != 0) { | |
291 | #if defined(HAVE_CUMULUS) | |
1c50c1c0 QY |
292 | flog_err(EC_LIB_SOCKET, |
293 | "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d", | |
294 | safe_strerror(errno), sock); | |
d62a17ae | 295 | return -1; |
b5826a12 | 296 | #endif |
d62a17ae | 297 | } |
298 | ||
299 | if (!strlen(name)) { | |
300 | *bgp_inst = bgp_get_default(); | |
301 | return 0; /* default instance. */ | |
302 | } | |
303 | ||
304 | /* First try match to instance; if that fails, check for interfaces. */ | |
305 | bgp = bgp_lookup_by_name(name); | |
306 | if (bgp) { | |
307 | if (!bgp->vrf_id) // unexpected | |
308 | return -1; | |
309 | *bgp_inst = bgp; | |
310 | return 0; | |
311 | } | |
312 | ||
313 | /* TODO - This will be optimized once interfaces move into the NS */ | |
314 | for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) { | |
315 | struct interface *ifp; | |
316 | ||
317 | if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW) | |
318 | continue; | |
319 | ||
a36898e7 | 320 | ifp = if_lookup_by_name(name, bgp->vrf_id); |
d62a17ae | 321 | if (ifp) { |
322 | *bgp_inst = bgp; | |
323 | return 0; | |
324 | } | |
325 | } | |
326 | ||
327 | /* We didn't match to either an instance or an interface. */ | |
328 | return -1; | |
120c6587 | 329 | #endif |
8dee0396 | 330 | } |
331 | ||
c2d020ad DS |
332 | static void bgp_socket_set_buffer_size(const int fd) |
333 | { | |
334 | if (getsockopt_so_sendbuf(fd) < (int)bm->socket_buffer) | |
335 | setsockopt_so_sendbuf(fd, bm->socket_buffer); | |
336 | if (getsockopt_so_recvbuf(fd) < (int)bm->socket_buffer) | |
337 | setsockopt_so_recvbuf(fd, bm->socket_buffer); | |
338 | } | |
339 | ||
718e3744 | 340 | /* Accept bgp connection. */ |
cc9f21da | 341 | static void bgp_accept(struct thread *thread) |
718e3744 | 342 | { |
d62a17ae | 343 | int bgp_sock; |
344 | int accept_sock; | |
345 | union sockunion su; | |
346 | struct bgp_listener *listener = THREAD_ARG(thread); | |
347 | struct peer *peer; | |
348 | struct peer *peer1; | |
349 | char buf[SU_ADDRSTRLEN]; | |
350 | struct bgp *bgp = NULL; | |
351 | ||
352 | sockunion_init(&su); | |
353 | ||
2f9db11b QY |
354 | bgp = bgp_lookup_by_name(listener->name); |
355 | ||
d62a17ae | 356 | /* Register accept thread. */ |
357 | accept_sock = THREAD_FD(thread); | |
358 | if (accept_sock < 0) { | |
2f9db11b QY |
359 | flog_err_sys(EC_LIB_SOCKET, |
360 | "[Error] BGP accept socket fd is negative: %d", | |
09c866e3 | 361 | accept_sock); |
cc9f21da | 362 | return; |
d62a17ae | 363 | } |
61cf4b37 | 364 | |
d62a17ae | 365 | thread_add_read(bm->master, bgp_accept, listener, accept_sock, |
366 | &listener->thread); | |
367 | ||
368 | /* Accept client connection. */ | |
369 | bgp_sock = sockunion_accept(accept_sock, &su); | |
2f9db11b | 370 | int save_errno = errno; |
d62a17ae | 371 | if (bgp_sock < 0) { |
2f9db11b QY |
372 | if (save_errno == EINVAL) { |
373 | struct vrf *vrf = | |
374 | bgp ? vrf_lookup_by_id(bgp->vrf_id) : NULL; | |
375 | ||
376 | /* | |
377 | * It appears that sometimes, when VRFs are deleted on | |
378 | * the system, it takes a little while for us to get | |
379 | * notified about that. In the meantime we endlessly | |
380 | * loop on accept(), because the socket, having been | |
381 | * bound to a now-deleted VRF device, is in some weird | |
382 | * state which causes accept() to fail. | |
383 | * | |
384 | * To avoid this, if we see accept() fail with EINVAL, | |
385 | * we cancel ourselves and trust that when the VRF | |
386 | * deletion notification comes in the event handler for | |
387 | * that will take care of cleaning us up. | |
388 | */ | |
389 | flog_err_sys( | |
390 | EC_LIB_SOCKET, | |
391 | "[Error] accept() failed with error \"%s\" on BGP listener socket %d for BGP instance in VRF \"%s\"; refreshing socket", | |
392 | safe_strerror(save_errno), accept_sock, | |
393 | VRF_LOGNAME(vrf)); | |
394 | THREAD_OFF(listener->thread); | |
395 | } else { | |
396 | flog_err_sys( | |
397 | EC_LIB_SOCKET, | |
398 | "[Error] BGP socket accept failed (%s); retrying", | |
399 | safe_strerror(save_errno)); | |
400 | } | |
cc9f21da | 401 | return; |
d62a17ae | 402 | } |
403 | set_nonblocking(bgp_sock); | |
404 | ||
61cf4b37 PG |
405 | /* Obtain BGP instance this connection is meant for. |
406 | * - if it is a VRF netns sock, then BGP is in listener structure | |
407 | * - otherwise, the bgp instance need to be demultiplexed | |
408 | */ | |
409 | if (listener->bgp) | |
410 | bgp = listener->bgp; | |
411 | else if (bgp_get_instance_for_inc_conn(bgp_sock, &bgp)) { | |
d62a17ae | 412 | if (bgp_debug_neighbor_events(NULL)) |
413 | zlog_debug( | |
414 | "[Event] Could not get instance for incoming conn from %s", | |
415 | inet_sutop(&su, buf)); | |
416 | close(bgp_sock); | |
cc9f21da | 417 | return; |
d62a17ae | 418 | } |
419 | ||
c2d020ad | 420 | bgp_socket_set_buffer_size(bgp_sock); |
d62a17ae | 421 | |
d1adb448 PG |
422 | /* Set TCP keepalive when TCP keepalive is enabled */ |
423 | bgp_update_setsockopt_tcp_keepalive(bgp, bgp_sock); | |
424 | ||
d62a17ae | 425 | /* Check remote IP address */ |
426 | peer1 = peer_lookup(bgp, &su); | |
427 | ||
428 | if (!peer1) { | |
429 | peer1 = peer_lookup_dynamic_neighbor(bgp, &su); | |
430 | if (peer1) { | |
431 | /* Dynamic neighbor has been created, let it proceed */ | |
432 | peer1->fd = bgp_sock; | |
4ab46701 AR |
433 | |
434 | /* Set the user configured MSS to TCP socket */ | |
435 | if (CHECK_FLAG(peer1->flags, PEER_FLAG_TCP_MSS)) | |
436 | sockopt_tcp_mss_set(bgp_sock, peer1->tcp_mss); | |
437 | ||
d62a17ae | 438 | bgp_fsm_change_status(peer1, Active); |
fa5806c3 | 439 | THREAD_OFF( |
d62a17ae | 440 | peer1->t_start); /* created in peer_create() */ |
441 | ||
6c537a18 DS |
442 | if (peer_active(peer1)) { |
443 | if (CHECK_FLAG(peer1->flags, | |
444 | PEER_FLAG_TIMER_DELAYOPEN)) | |
445 | BGP_EVENT_ADD( | |
446 | peer1, | |
447 | TCP_connection_open_w_delay); | |
448 | else | |
449 | BGP_EVENT_ADD(peer1, | |
450 | TCP_connection_open); | |
451 | } | |
d62a17ae | 452 | |
cc9f21da | 453 | return; |
d62a17ae | 454 | } |
455 | } | |
456 | ||
457 | if (!peer1) { | |
458 | if (bgp_debug_neighbor_events(NULL)) { | |
459 | zlog_debug( | |
45069622 DS |
460 | "[Event] %s connection rejected(%s:%u:%s) - not configured and not valid for dynamic", |
461 | inet_sutop(&su, buf), bgp->name_pretty, bgp->as, | |
462 | VRF_LOGNAME(vrf_lookup_by_id(bgp->vrf_id))); | |
d62a17ae | 463 | } |
464 | close(bgp_sock); | |
cc9f21da | 465 | return; |
d62a17ae | 466 | } |
467 | ||
cb9196e7 DS |
468 | if (CHECK_FLAG(peer1->flags, PEER_FLAG_SHUTDOWN) |
469 | || CHECK_FLAG(peer1->bgp->flags, BGP_FLAG_SHUTDOWN)) { | |
d62a17ae | 470 | if (bgp_debug_neighbor_events(peer1)) |
471 | zlog_debug( | |
45069622 DS |
472 | "[Event] connection from %s rejected(%s:%u:%s) due to admin shutdown", |
473 | inet_sutop(&su, buf), bgp->name_pretty, bgp->as, | |
474 | VRF_LOGNAME(vrf_lookup_by_id(bgp->vrf_id))); | |
d62a17ae | 475 | close(bgp_sock); |
cc9f21da | 476 | return; |
d62a17ae | 477 | } |
478 | ||
479 | /* | |
480 | * Do not accept incoming connections in Clearing state. This can result | |
481 | * in incorect state transitions - e.g., the connection goes back to | |
482 | * Established and then the Clearing_Completed event is generated. Also, | |
483 | * block incoming connection in Deleted state. | |
484 | */ | |
485 | if (peer1->status == Clearing || peer1->status == Deleted) { | |
486 | if (bgp_debug_neighbor_events(peer1)) | |
487 | zlog_debug( | |
488 | "[Event] Closing incoming conn for %s (%p) state %d", | |
489 | peer1->host, peer1, peer1->status); | |
490 | close(bgp_sock); | |
cc9f21da | 491 | return; |
d62a17ae | 492 | } |
493 | ||
494 | /* Check that at least one AF is activated for the peer. */ | |
495 | if (!peer_active(peer1)) { | |
496 | if (bgp_debug_neighbor_events(peer1)) | |
497 | zlog_debug( | |
498 | "%s - incoming conn rejected - no AF activated for peer", | |
499 | peer1->host); | |
500 | close(bgp_sock); | |
cc9f21da | 501 | return; |
d62a17ae | 502 | } |
503 | ||
d091d9ad DA |
504 | /* Do not try to reconnect if the peer reached maximum |
505 | * prefixes, restart timer is still running or the peer | |
506 | * is shutdown. | |
507 | */ | |
508 | if (BGP_PEER_START_SUPPRESSED(peer1)) { | |
7c5062fb DS |
509 | if (bgp_debug_neighbor_events(peer1)) { |
510 | if (peer1->shut_during_cfg) | |
511 | zlog_debug( | |
512 | "[Event] Incoming BGP connection rejected from %s due to configuration being currently read in", | |
513 | peer1->host); | |
514 | else | |
515 | zlog_debug( | |
516 | "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown", | |
517 | peer1->host); | |
518 | } | |
497b686a | 519 | close(bgp_sock); |
cc9f21da | 520 | return; |
497b686a MS |
521 | } |
522 | ||
d62a17ae | 523 | if (bgp_debug_neighbor_events(peer1)) |
031c24f2 | 524 | zlog_debug( |
525 | "[Event] connection from %s fd %d, active peer status %d fd %d", | |
526 | inet_sutop(&su, buf), bgp_sock, peer1->status, | |
527 | peer1->fd); | |
d62a17ae | 528 | |
529 | if (peer1->doppelganger) { | |
530 | /* We have an existing connection. Kill the existing one and run | |
531 | with this one. | |
532 | */ | |
533 | if (bgp_debug_neighbor_events(peer1)) | |
534 | zlog_debug( | |
3efd0893 | 535 | "[Event] New active connection from peer %s, Killing previous active connection", |
d62a17ae | 536 | peer1->host); |
537 | peer_delete(peer1->doppelganger); | |
538 | } | |
539 | ||
540 | if (bgp_set_socket_ttl(peer1, bgp_sock) < 0) | |
541 | if (bgp_debug_neighbor_events(peer1)) | |
542 | zlog_debug( | |
543 | "[Event] Unable to set min/max TTL on peer %s, Continuing", | |
544 | peer1->host); | |
545 | ||
546 | peer = peer_create(&su, peer1->conf_if, peer1->bgp, peer1->local_as, | |
de76ed8a | 547 | peer1->as, peer1->as_type, NULL, false, NULL); |
d62a17ae | 548 | |
549 | peer_xfer_config(peer, peer1); | |
d7b3cda6 | 550 | bgp_peer_gr_flags_update(peer); |
551 | ||
36235319 QY |
552 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
553 | peer->bgp->peer); | |
0f0444fb | 554 | |
d7b3cda6 | 555 | if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) { |
556 | ||
557 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
558 | ||
36235319 | 559 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { |
d7b3cda6 | 560 | peer_nsf_stop(peer); |
561 | } | |
562 | } | |
563 | ||
d62a17ae | 564 | peer->doppelganger = peer1; |
565 | peer1->doppelganger = peer; | |
566 | peer->fd = bgp_sock; | |
0def0c9f IR |
567 | frr_with_privs(&bgpd_privs) { |
568 | vrf_bind(peer->bgp->vrf_id, bgp_sock, bgp_get_bound_name(peer)); | |
569 | } | |
996319e6 | 570 | bgp_peer_reg_with_nht(peer); |
d62a17ae | 571 | bgp_fsm_change_status(peer, Active); |
fa5806c3 | 572 | THREAD_OFF(peer->t_start); /* created in peer_create() */ |
d62a17ae | 573 | |
574 | SET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER); | |
d62a17ae | 575 | /* Make dummy peer until read Open packet. */ |
feb17238 | 576 | if (peer_established(peer1) |
36235319 | 577 | && CHECK_FLAG(peer1->sflags, PEER_STATUS_NSF_MODE)) { |
d62a17ae | 578 | /* If we have an existing established connection with graceful |
579 | * restart | |
580 | * capability announced with one or more address families, then | |
581 | * drop | |
582 | * existing established connection and move state to connect. | |
583 | */ | |
584 | peer1->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
d7b3cda6 | 585 | |
36235319 QY |
586 | if (CHECK_FLAG(peer1->flags, PEER_FLAG_GRACEFUL_RESTART) |
587 | || CHECK_FLAG(peer1->flags, | |
588 | PEER_FLAG_GRACEFUL_RESTART_HELPER)) | |
589 | SET_FLAG(peer1->sflags, PEER_STATUS_NSF_WAIT); | |
d7b3cda6 | 590 | |
d62a17ae | 591 | bgp_event_update(peer1, TCP_connection_closed); |
592 | } | |
593 | ||
594 | if (peer_active(peer)) { | |
6c537a18 DS |
595 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN)) |
596 | BGP_EVENT_ADD(peer, TCP_connection_open_w_delay); | |
597 | else | |
598 | BGP_EVENT_ADD(peer, TCP_connection_open); | |
d62a17ae | 599 | } |
600 | ||
dcd5ba55 DS |
601 | /* |
602 | * If we are doing nht for a peer that is v6 LL based | |
603 | * massage the event system to make things happy | |
604 | */ | |
605 | bgp_nht_interface_events(peer); | |
718e3744 | 606 | } |
607 | ||
608 | /* BGP socket bind. */ | |
97896a91 | 609 | static char *bgp_get_bound_name(struct peer *peer) |
718e3744 | 610 | { |
a2bce1c8 DS |
611 | if (!peer) |
612 | return NULL; | |
613 | ||
996c9314 LB |
614 | if ((peer->bgp->vrf_id == VRF_DEFAULT) && !peer->ifname |
615 | && !peer->conf_if) | |
97896a91 PG |
616 | return NULL; |
617 | ||
d62a17ae | 618 | if (peer->su.sa.sa_family != AF_INET |
619 | && peer->su.sa.sa_family != AF_INET6) | |
97896a91 | 620 | return NULL; // unexpected |
d62a17ae | 621 | |
622 | /* For IPv6 peering, interface (unnumbered or link-local with interface) | |
623 | * takes precedence over VRF. For IPv4 peering, explicit interface or | |
624 | * VRF are the situations to bind. | |
625 | */ | |
7224dcd8 IR |
626 | if (peer->su.sa.sa_family == AF_INET6 && peer->conf_if) |
627 | return peer->conf_if; | |
628 | ||
629 | if (peer->ifname) | |
630 | return peer->ifname; | |
631 | ||
632 | if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW) | |
633 | return NULL; | |
d62a17ae | 634 | |
7224dcd8 | 635 | return peer->bgp->name; |
718e3744 | 636 | } |
637 | ||
09f267ec | 638 | int bgp_update_address(struct interface *ifp, const union sockunion *dst, |
d62a17ae | 639 | union sockunion *addr) |
718e3744 | 640 | { |
d62a17ae | 641 | struct prefix *p, *sel, d; |
642 | struct connected *connected; | |
643 | struct listnode *node; | |
644 | int common; | |
645 | ||
0154d8ce DS |
646 | if (!sockunion2hostprefix(dst, &d)) |
647 | return 1; | |
648 | ||
d62a17ae | 649 | sel = NULL; |
650 | common = -1; | |
651 | ||
652 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) { | |
653 | p = connected->address; | |
654 | if (p->family != d.family) | |
655 | continue; | |
656 | if (prefix_common_bits(p, &d) > common) { | |
657 | sel = p; | |
658 | common = prefix_common_bits(sel, &d); | |
659 | } | |
660 | } | |
661 | ||
662 | if (!sel) | |
663 | return 1; | |
664 | ||
665 | prefix2sockunion(sel, addr); | |
666 | return 0; | |
718e3744 | 667 | } |
668 | ||
669 | /* Update source selection. */ | |
d62a17ae | 670 | static int bgp_update_source(struct peer *peer) |
718e3744 | 671 | { |
d62a17ae | 672 | struct interface *ifp; |
673 | union sockunion addr; | |
674 | int ret = 0; | |
718e3744 | 675 | |
d62a17ae | 676 | sockunion_init(&addr); |
dd793e4a | 677 | |
d62a17ae | 678 | /* Source is specified with interface name. */ |
679 | if (peer->update_if) { | |
a36898e7 | 680 | ifp = if_lookup_by_name(peer->update_if, peer->bgp->vrf_id); |
d62a17ae | 681 | if (!ifp) |
682 | return -1; | |
718e3744 | 683 | |
d62a17ae | 684 | if (bgp_update_address(ifp, &peer->su, &addr)) |
685 | return -1; | |
718e3744 | 686 | |
d62a17ae | 687 | ret = sockunion_bind(peer->fd, &addr, 0, &addr); |
688 | } | |
718e3744 | 689 | |
d62a17ae | 690 | /* Source is specified with IP address. */ |
691 | if (peer->update_source) | |
692 | ret = sockunion_bind(peer->fd, peer->update_source, 0, | |
693 | peer->update_source); | |
49067496 | 694 | |
d62a17ae | 695 | return ret; |
718e3744 | 696 | } |
697 | ||
698 | /* BGP try to connect to the peer. */ | |
d62a17ae | 699 | int bgp_connect(struct peer *peer) |
718e3744 | 700 | { |
b750b0ba QY |
701 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON)); |
702 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON)); | |
d62a17ae | 703 | ifindex_t ifindex = 0; |
704 | ||
705 | if (peer->conf_if && BGP_PEER_SU_UNSPEC(peer)) { | |
888e727c DS |
706 | if (bgp_debug_neighbor_events(peer)) |
707 | zlog_debug("Peer address not learnt: Returning from connect"); | |
d62a17ae | 708 | return 0; |
709 | } | |
0cf6db21 | 710 | frr_with_privs(&bgpd_privs) { |
d62a17ae | 711 | /* Make socket for the peer. */ |
01b9e3fd DL |
712 | peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id, |
713 | bgp_get_bound_name(peer)); | |
714 | } | |
888e727c | 715 | if (peer->fd < 0) { |
c5fe9095 | 716 | peer->last_reset = PEER_DOWN_SOCKET_ERROR; |
888e727c DS |
717 | if (bgp_debug_neighbor_events(peer)) |
718 | zlog_debug("%s: Failure to create socket for connection to %s, error received: %s(%d)", | |
719 | __func__, peer->host, safe_strerror(errno), | |
720 | errno); | |
d62a17ae | 721 | return -1; |
888e727c | 722 | } |
d62a17ae | 723 | |
724 | set_nonblocking(peer->fd); | |
725 | ||
4ab46701 AR |
726 | /* Set the user configured MSS to TCP socket */ |
727 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TCP_MSS)) | |
728 | sockopt_tcp_mss_set(peer->fd, peer->tcp_mss); | |
729 | ||
c2d020ad | 730 | bgp_socket_set_buffer_size(peer->fd); |
d62a17ae | 731 | |
d1adb448 PG |
732 | /* Set TCP keepalive when TCP keepalive is enabled */ |
733 | bgp_update_setsockopt_tcp_keepalive(peer->bgp, peer->fd); | |
734 | ||
888e727c | 735 | if (bgp_set_socket_ttl(peer, peer->fd) < 0) { |
c5fe9095 | 736 | peer->last_reset = PEER_DOWN_SOCKET_ERROR; |
888e727c DS |
737 | if (bgp_debug_neighbor_events(peer)) |
738 | zlog_debug("%s: Failure to set socket ttl for connection to %s, error received: %s(%d)", | |
739 | __func__, peer->host, safe_strerror(errno), | |
740 | errno); | |
d1adb448 | 741 | |
d62a17ae | 742 | return -1; |
888e727c | 743 | } |
d62a17ae | 744 | |
745 | sockopt_reuseaddr(peer->fd); | |
746 | sockopt_reuseport(peer->fd); | |
d62a17ae | 747 | |
1423c809 | 748 | #ifdef IPTOS_PREC_INTERNETCONTROL |
0cf6db21 | 749 | frr_with_privs(&bgpd_privs) { |
01b9e3fd | 750 | if (sockunion_family(&peer->su) == AF_INET) |
425bd64b | 751 | setsockopt_ipv4_tos(peer->fd, bm->tcp_dscp); |
01b9e3fd | 752 | else if (sockunion_family(&peer->su) == AF_INET6) |
425bd64b | 753 | setsockopt_ipv6_tclass(peer->fd, bm->tcp_dscp); |
01b9e3fd | 754 | } |
1423c809 SH |
755 | #endif |
756 | ||
9e7d9a61 QY |
757 | if (peer->password) { |
758 | uint16_t prefixlen = peer->su.sa.sa_family == AF_INET | |
936fbaef | 759 | ? IPV4_MAX_BITLEN |
f4d81e55 | 760 | : IPV6_MAX_BITLEN; |
9e7d9a61 QY |
761 | |
762 | bgp_md5_set_connect(peer->fd, &peer->su, prefixlen, | |
763 | peer->password); | |
764 | } | |
718e3744 | 765 | |
d62a17ae | 766 | /* Update source bind. */ |
767 | if (bgp_update_source(peer) < 0) { | |
c5fe9095 | 768 | peer->last_reset = PEER_DOWN_SOCKET_ERROR; |
d62a17ae | 769 | return connect_error; |
770 | } | |
718e3744 | 771 | |
d62a17ae | 772 | if (peer->conf_if || peer->ifname) |
773 | ifindex = ifname2ifindex(peer->conf_if ? peer->conf_if | |
774 | : peer->ifname, | |
775 | peer->bgp->vrf_id); | |
718e3744 | 776 | |
d62a17ae | 777 | if (bgp_debug_neighbor_events(peer)) |
778 | zlog_debug("%s [Event] Connect start to %s fd %d", peer->host, | |
779 | peer->host, peer->fd); | |
718e3744 | 780 | |
d62a17ae | 781 | /* Connect to the remote peer. */ |
782 | return sockunion_connect(peer->fd, &peer->su, htons(peer->port), | |
783 | ifindex); | |
718e3744 | 784 | } |
785 | ||
786 | /* After TCP connection is established. Get local address and port. */ | |
d62a17ae | 787 | int bgp_getsockname(struct peer *peer) |
718e3744 | 788 | { |
d62a17ae | 789 | if (peer->su_local) { |
790 | sockunion_free(peer->su_local); | |
791 | peer->su_local = NULL; | |
792 | } | |
793 | ||
794 | if (peer->su_remote) { | |
795 | sockunion_free(peer->su_remote); | |
796 | peer->su_remote = NULL; | |
797 | } | |
798 | ||
799 | peer->su_local = sockunion_getsockname(peer->fd); | |
800 | if (!peer->su_local) | |
801 | return -1; | |
802 | peer->su_remote = sockunion_getpeername(peer->fd); | |
803 | if (!peer->su_remote) | |
804 | return -1; | |
805 | ||
17cdd31e DS |
806 | if (!bgp_zebra_nexthop_set(peer->su_local, peer->su_remote, |
807 | &peer->nexthop, peer)) { | |
50e049db DS |
808 | flog_err( |
809 | EC_BGP_NH_UPD, | |
810 | "%s: nexthop_set failed, resetting connection - intf %s", | |
811 | peer->host, | |
812 | peer->nexthop.ifp ? peer->nexthop.ifp->name | |
813 | : "(Unknown)"); | |
d62a17ae | 814 | return -1; |
d62a17ae | 815 | } |
d62a17ae | 816 | return 0; |
718e3744 | 817 | } |
818 | ||
d023aec4 | 819 | |
61cf4b37 PG |
820 | static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen, |
821 | struct bgp *bgp) | |
d023aec4 | 822 | { |
d62a17ae | 823 | struct bgp_listener *listener; |
824 | int ret, en; | |
d023aec4 | 825 | |
d62a17ae | 826 | sockopt_reuseaddr(sock); |
827 | sockopt_reuseport(sock); | |
d023aec4 | 828 | |
0cf6db21 | 829 | frr_with_privs(&bgpd_privs) { |
5c88f19d | 830 | |
d023aec4 | 831 | #ifdef IPTOS_PREC_INTERNETCONTROL |
01b9e3fd | 832 | if (sa->sa_family == AF_INET) |
425bd64b | 833 | setsockopt_ipv4_tos(sock, bm->tcp_dscp); |
01b9e3fd | 834 | else if (sa->sa_family == AF_INET6) |
425bd64b | 835 | setsockopt_ipv6_tclass(sock, bm->tcp_dscp); |
d023aec4 SH |
836 | #endif |
837 | ||
01b9e3fd | 838 | sockopt_v6only(sa->sa_family, sock); |
d62a17ae | 839 | |
01b9e3fd DL |
840 | ret = bind(sock, sa, salen); |
841 | en = errno; | |
842 | } | |
d62a17ae | 843 | |
844 | if (ret < 0) { | |
450971aa | 845 | flog_err_sys(EC_LIB_SOCKET, "bind: %s", safe_strerror(en)); |
d62a17ae | 846 | return ret; |
847 | } | |
848 | ||
48522088 | 849 | ret = listen(sock, SOMAXCONN); |
d62a17ae | 850 | if (ret < 0) { |
1c50c1c0 | 851 | flog_err_sys(EC_LIB_SOCKET, "listen: %s", safe_strerror(errno)); |
d62a17ae | 852 | return ret; |
853 | } | |
854 | ||
61cf4b37 | 855 | listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener)); |
d62a17ae | 856 | listener->fd = sock; |
2a0e69ae | 857 | listener->name = XSTRDUP(MTYPE_BGP_LISTENER, bgp->name); |
61cf4b37 | 858 | |
a4faae3a | 859 | /* this socket is in a vrf record bgp back pointer */ |
2c1eba8e | 860 | if (bgp->vrf_id != VRF_DEFAULT) |
61cf4b37 PG |
861 | listener->bgp = bgp; |
862 | ||
d62a17ae | 863 | memcpy(&listener->su, sa, salen); |
d62a17ae | 864 | thread_add_read(bm->master, bgp_accept, listener, sock, |
865 | &listener->thread); | |
866 | listnode_add(bm->listen_sockets, listener); | |
867 | ||
868 | return 0; | |
d023aec4 SH |
869 | } |
870 | ||
718e3744 | 871 | /* IPv6 supported version of BGP server socket setup. */ |
61cf4b37 | 872 | int bgp_socket(struct bgp *bgp, unsigned short port, const char *address) |
718e3744 | 873 | { |
d62a17ae | 874 | struct addrinfo *ainfo; |
875 | struct addrinfo *ainfo_save; | |
876 | static const struct addrinfo req = { | |
877 | .ai_family = AF_UNSPEC, | |
878 | .ai_flags = AI_PASSIVE, | |
879 | .ai_socktype = SOCK_STREAM, | |
880 | }; | |
881 | int ret, count; | |
882 | char port_str[BUFSIZ]; | |
883 | ||
884 | snprintf(port_str, sizeof(port_str), "%d", port); | |
885 | port_str[sizeof(port_str) - 1] = '\0'; | |
886 | ||
0cf6db21 | 887 | frr_with_privs(&bgpd_privs) { |
01b9e3fd DL |
888 | ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save, |
889 | bgp->vrf_id); | |
890 | } | |
d62a17ae | 891 | if (ret != 0) { |
450971aa | 892 | flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s", |
09c866e3 | 893 | gai_strerror(ret)); |
d62a17ae | 894 | return -1; |
895 | } | |
0b014ea6 | 896 | if (bgp_option_check(BGP_OPT_NO_ZEBRA) && |
449cff3e A |
897 | bgp->vrf_id != VRF_DEFAULT) { |
898 | freeaddrinfo(ainfo_save); | |
0b014ea6 | 899 | return -1; |
449cff3e | 900 | } |
d62a17ae | 901 | count = 0; |
902 | for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) { | |
903 | int sock; | |
904 | ||
905 | if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) | |
906 | continue; | |
907 | ||
0cf6db21 | 908 | frr_with_privs(&bgpd_privs) { |
01b9e3fd DL |
909 | sock = vrf_socket(ainfo->ai_family, |
910 | ainfo->ai_socktype, | |
67684738 | 911 | ainfo->ai_protocol, |
2c1eba8e | 912 | bgp->vrf_id, |
633fc9b1 DL |
913 | (bgp->inst_type |
914 | == BGP_INSTANCE_TYPE_VRF | |
915 | ? bgp->name : NULL)); | |
01b9e3fd | 916 | } |
d62a17ae | 917 | if (sock < 0) { |
450971aa | 918 | flog_err_sys(EC_LIB_SOCKET, "socket: %s", |
09c866e3 | 919 | safe_strerror(errno)); |
d62a17ae | 920 | continue; |
921 | } | |
922 | ||
923 | /* if we intend to implement ttl-security, this socket needs | |
924 | * ttl=255 */ | |
925 | sockopt_ttl(ainfo->ai_family, sock, MAXTTL); | |
926 | ||
996c9314 LB |
927 | ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen, |
928 | bgp); | |
d62a17ae | 929 | if (ret == 0) |
930 | ++count; | |
931 | else | |
932 | close(sock); | |
933 | } | |
934 | freeaddrinfo(ainfo_save); | |
06969768 | 935 | if (count == 0 && bgp->inst_type != BGP_INSTANCE_TYPE_VRF) { |
af4c2728 | 936 | flog_err( |
450971aa | 937 | EC_LIB_SOCKET, |
996c9314 LB |
938 | "%s: no usable addresses please check other programs usage of specified port %d", |
939 | __func__, port); | |
450971aa | 940 | flog_err_sys(EC_LIB_SOCKET, "%s: Program cannot continue", |
09c866e3 | 941 | __func__); |
b3b78f6e | 942 | exit(-1); |
d62a17ae | 943 | } |
944 | ||
945 | return 0; | |
718e3744 | 946 | } |
d023aec4 | 947 | |
e5619c28 PG |
948 | /* this function closes vrf socket |
949 | * this should be called only for vrf socket with netns backend | |
950 | */ | |
951 | void bgp_close_vrf_socket(struct bgp *bgp) | |
952 | { | |
953 | struct listnode *node, *next; | |
954 | struct bgp_listener *listener; | |
955 | ||
956 | if (!bgp) | |
957 | return; | |
958 | ||
959 | if (bm->listen_sockets == NULL) | |
960 | return; | |
961 | ||
962 | for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) { | |
963 | if (listener->bgp == bgp) { | |
2f9db11b | 964 | THREAD_OFF(listener->thread); |
e5619c28 PG |
965 | close(listener->fd); |
966 | listnode_delete(bm->listen_sockets, listener); | |
2a0e69ae | 967 | XFREE(MTYPE_BGP_LISTENER, listener->name); |
e5619c28 PG |
968 | XFREE(MTYPE_BGP_LISTENER, listener); |
969 | } | |
970 | } | |
971 | } | |
972 | ||
973 | /* this function closes main socket | |
974 | */ | |
d62a17ae | 975 | void bgp_close(void) |
d023aec4 | 976 | { |
d62a17ae | 977 | struct listnode *node, *next; |
978 | struct bgp_listener *listener; | |
979 | ||
980 | if (bm->listen_sockets == NULL) | |
981 | return; | |
982 | ||
983 | for (ALL_LIST_ELEMENTS(bm->listen_sockets, node, next, listener)) { | |
e5619c28 PG |
984 | if (listener->bgp) |
985 | continue; | |
e2d409a8 | 986 | THREAD_OFF(listener->thread); |
d62a17ae | 987 | close(listener->fd); |
988 | listnode_delete(bm->listen_sockets, listener); | |
2a0e69ae | 989 | XFREE(MTYPE_BGP_LISTENER, listener->name); |
d62a17ae | 990 | XFREE(MTYPE_BGP_LISTENER, listener); |
991 | } | |
d023aec4 | 992 | } |