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