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