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