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