paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP network related fucntions |
| 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 |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "thread.h" |
| 24 | #include "sockunion.h" |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 25 | #include "sockopt.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | #include "memory.h" |
| 27 | #include "log.h" |
| 28 | #include "if.h" |
| 29 | #include "prefix.h" |
| 30 | #include "command.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 31 | #include "privs.h" |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 32 | #include "linklist.h" |
Paul Jakma | fdbc8e7 | 2011-04-11 16:31:43 +0100 | [diff] [blame] | 33 | #include "network.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | #include "bgpd/bgpd.h" |
| 36 | #include "bgpd/bgp_fsm.h" |
| 37 | #include "bgpd/bgp_attr.h" |
| 38 | #include "bgpd/bgp_debug.h" |
| 39 | #include "bgpd/bgp_network.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 40 | |
| 41 | extern struct zebra_privs_t bgpd_privs; |
| 42 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 43 | /* BGP listening socket. */ |
| 44 | struct bgp_listener |
| 45 | { |
| 46 | int fd; |
| 47 | union sockunion su; |
| 48 | struct thread *thread; |
| 49 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 50 | |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 51 | /* |
| 52 | * Set MD5 key for the socket, for the given IPv4 peer address. |
| 53 | * If the password is NULL or zero-length, the option will be disabled. |
| 54 | */ |
| 55 | static int |
| 56 | bgp_md5_set_socket (int socket, union sockunion *su, const char *password) |
| 57 | { |
| 58 | int ret = -1; |
| 59 | int en = ENOSYS; |
| 60 | |
| 61 | assert (socket >= 0); |
| 62 | |
| 63 | #if HAVE_DECL_TCP_MD5SIG |
| 64 | ret = sockopt_tcp_signature (socket, su, password); |
| 65 | en = errno; |
| 66 | #endif /* HAVE_TCP_MD5SIG */ |
| 67 | |
| 68 | if (ret < 0) |
| 69 | zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s", |
| 70 | socket, safe_strerror (en)); |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | /* Helper for bgp_connect */ |
| 76 | static int |
| 77 | bgp_md5_set_connect (int socket, union sockunion *su, const char *password) |
| 78 | { |
| 79 | int ret = -1; |
| 80 | |
| 81 | #if HAVE_DECL_TCP_MD5SIG |
| 82 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 83 | { |
| 84 | zlog_err ("%s: could not raise privs", __func__); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | ret = bgp_md5_set_socket (socket, su, password); |
| 89 | |
| 90 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 91 | zlog_err ("%s: could not lower privs", __func__); |
| 92 | #endif /* HAVE_TCP_MD5SIG */ |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | bgp_md5_set (struct peer *peer) |
| 99 | { |
| 100 | struct listnode *node; |
Stephen Hemminger | d1c21ca | 2009-08-25 10:18:15 -0700 | [diff] [blame] | 101 | int ret = 0; |
| 102 | struct bgp_listener *listener; |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 103 | |
| 104 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 105 | { |
| 106 | zlog_err ("%s: could not raise privs", __func__); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | /* Just set the password on the listen socket(s). Outbound connections |
| 111 | * are taken care of in bgp_connect() below. |
| 112 | */ |
Stephen Hemminger | d1c21ca | 2009-08-25 10:18:15 -0700 | [diff] [blame] | 113 | for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener)) |
| 114 | if (listener->su.sa.sa_family == peer->su.sa.sa_family) |
| 115 | { |
| 116 | ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password); |
| 117 | break; |
| 118 | } |
| 119 | |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 120 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 121 | zlog_err ("%s: could not lower privs", __func__); |
| 122 | |
Stephen Hemminger | d1c21ca | 2009-08-25 10:18:15 -0700 | [diff] [blame] | 123 | return ret; |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 124 | } |
Vipin Kumar | 3374bef | 2014-01-09 00:31:22 +0000 | [diff] [blame] | 125 | |
| 126 | /* Update BGP socket send buffer size */ |
| 127 | static void |
| 128 | bgp_update_sock_send_buffer_size (int fd) |
| 129 | { |
| 130 | int size = BGP_SOCKET_SNDBUF_SIZE; |
| 131 | int optval; |
| 132 | socklen_t optlen = sizeof(optval); |
| 133 | |
| 134 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) |
| 135 | { |
| 136 | zlog_err("getsockopt of SO_SNDBUF failed %s\n", safe_strerror(errno)); |
| 137 | return; |
| 138 | } |
| 139 | if (optval < size) |
| 140 | { |
| 141 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0) |
| 142 | { |
| 143 | zlog_err("Couldn't increase send buffer: %s\n", safe_strerror(errno)); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
David Lamparter | ef0b0c3 | 2014-05-19 22:52:04 +0200 | [diff] [blame] | 148 | static void |
| 149 | bgp_set_socket_ttl (struct peer *peer, int bgp_sock) |
| 150 | { |
Pradosh Mohapatra | 5d804b4 | 2013-09-12 03:37:07 +0000 | [diff] [blame] | 151 | char buf[INET_ADDRSTRLEN]; |
| 152 | int ret; |
| 153 | |
| 154 | /* In case of peer is EBGP, we should set TTL for this connection. */ |
| 155 | if (!peer->gtsm_hops && (peer_sort (peer) == BGP_PEER_EBGP)) |
| 156 | { |
| 157 | ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, peer->ttl); |
| 158 | if (ret) |
| 159 | { |
| 160 | zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d", |
| 161 | __func__, |
| 162 | inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)), |
| 163 | errno); |
| 164 | } |
| 165 | } |
| 166 | else if (peer->gtsm_hops) |
| 167 | { |
| 168 | /* On Linux, setting minttl without setting ttl seems to mess with the |
| 169 | outgoing ttl. Therefore setting both. |
| 170 | */ |
| 171 | ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, MAXTTL); |
| 172 | if (ret) |
| 173 | { |
| 174 | zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d", |
| 175 | __func__, |
| 176 | inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)), |
| 177 | errno); |
| 178 | } |
| 179 | ret = sockopt_minttl (peer->su.sa.sa_family, bgp_sock, |
| 180 | MAXTTL + 1 - peer->gtsm_hops); |
| 181 | if (ret) |
| 182 | { |
| 183 | zlog_err ("%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d", |
| 184 | __func__, |
| 185 | inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)), |
| 186 | errno); |
| 187 | } |
| 188 | } |
David Lamparter | ef0b0c3 | 2014-05-19 22:52:04 +0200 | [diff] [blame] | 189 | } |
| 190 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | /* Accept bgp connection. */ |
| 192 | static int |
| 193 | bgp_accept (struct thread *thread) |
| 194 | { |
| 195 | int bgp_sock; |
| 196 | int accept_sock; |
| 197 | union sockunion su; |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 198 | struct bgp_listener *listener = THREAD_ARG(thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | struct peer *peer; |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 200 | struct peer *peer1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | char buf[SU_ADDRSTRLEN]; |
| 202 | |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 203 | /* Register accept thread. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | accept_sock = THREAD_FD (thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | if (accept_sock < 0) |
| 206 | { |
| 207 | zlog_err ("accept_sock is nevative value %d", accept_sock); |
| 208 | return -1; |
| 209 | } |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 210 | listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | |
| 212 | /* Accept client connection. */ |
| 213 | bgp_sock = sockunion_accept (accept_sock, &su); |
| 214 | if (bgp_sock < 0) |
| 215 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 216 | zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
Stephen Hemminger | 3539858 | 2010-08-05 10:26:23 -0700 | [diff] [blame] | 219 | set_nonblocking (bgp_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | |
Vipin Kumar | 3374bef | 2014-01-09 00:31:22 +0000 | [diff] [blame] | 221 | /* Set socket send buffer size */ |
| 222 | bgp_update_sock_send_buffer_size(bgp_sock); |
| 223 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 225 | zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | |
| 227 | /* Check remote IP address */ |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 228 | peer1 = peer_lookup (NULL, &su); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 229 | if (! peer1 || peer1->status == Idle) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | { |
| 231 | if (BGP_DEBUG (events, EVENTS)) |
| 232 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 233 | if (! peer1) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 234 | zlog_debug ("[Event] BGP connection IP address %s is not configured", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 235 | inet_sutop (&su, buf)); |
| 236 | else |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 237 | zlog_debug ("[Event] BGP connection IP address %s is Idle state", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | inet_sutop (&su, buf)); |
| 239 | } |
| 240 | close (bgp_sock); |
| 241 | return -1; |
| 242 | } |
| 243 | |
David Lamparter | ef0b0c3 | 2014-05-19 22:52:04 +0200 | [diff] [blame] | 244 | bgp_set_socket_ttl (peer1, bgp_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 246 | /* Make dummy peer until read Open packet. */ |
| 247 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 248 | zlog_debug ("[Event] Make dummy peer structure until read Open packet"); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 249 | |
| 250 | { |
Jorge Boncompte [DTI2] | 682ca04 | 2012-04-10 16:57:27 +0200 | [diff] [blame] | 251 | char buf[SU_ADDRSTRLEN]; |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 252 | |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 253 | peer = peer_create_accept (peer1->bgp); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 254 | SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER); |
| 255 | peer->su = su; |
| 256 | peer->fd = bgp_sock; |
| 257 | peer->status = Active; |
| 258 | peer->local_id = peer1->local_id; |
Timo Teräs | e8eb000 | 2009-02-17 12:14:23 +0200 | [diff] [blame] | 259 | peer->v_holdtime = peer1->v_holdtime; |
| 260 | peer->v_keepalive = peer1->v_keepalive; |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 261 | |
| 262 | /* Make peer's address string. */ |
| 263 | sockunion2str (&su, buf, SU_ADDRSTRLEN); |
paul | e83e208 | 2005-05-19 02:12:25 +0000 | [diff] [blame] | 264 | peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 265 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | |
| 267 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* BGP socket bind. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 273 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 274 | bgp_bind (struct peer *peer) |
| 275 | { |
| 276 | #ifdef SO_BINDTODEVICE |
| 277 | int ret; |
| 278 | struct ifreq ifreq; |
| 279 | |
| 280 | if (! peer->ifname) |
| 281 | return 0; |
| 282 | |
| 283 | strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name)); |
| 284 | |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 285 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 286 | zlog_err ("bgp_bind: could not raise privs"); |
| 287 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 288 | ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | &ifreq, sizeof (ifreq)); |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 290 | |
| 291 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 292 | zlog_err ("bgp_bind: could not lower privs"); |
| 293 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 294 | if (ret < 0) |
| 295 | { |
| 296 | zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname); |
| 297 | return ret; |
| 298 | } |
| 299 | #endif /* SO_BINDTODEVICE */ |
| 300 | return 0; |
| 301 | } |
| 302 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 303 | static int |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 304 | bgp_update_address (struct interface *ifp, const union sockunion *dst, |
| 305 | union sockunion *addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 306 | { |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 307 | struct prefix *p, *sel, *d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 308 | struct connected *connected; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 309 | struct listnode *node; |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 310 | int common; |
| 311 | |
| 312 | d = sockunion2hostprefix (dst); |
| 313 | sel = NULL; |
| 314 | common = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 316 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 317 | { |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 318 | p = connected->address; |
| 319 | if (p->family != d->family) |
| 320 | continue; |
| 321 | if (prefix_common_bits (p, d) > common) |
| 322 | { |
| 323 | sel = p; |
| 324 | common = prefix_common_bits (sel, d); |
| 325 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | } |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 327 | |
| 328 | prefix_free (d); |
| 329 | if (!sel) |
| 330 | return 1; |
| 331 | |
| 332 | prefix2sockunion (sel, addr); |
| 333 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Update source selection. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 337 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | bgp_update_source (struct peer *peer) |
| 339 | { |
| 340 | struct interface *ifp; |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 341 | union sockunion addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | |
| 343 | /* Source is specified with interface name. */ |
| 344 | if (peer->update_if) |
| 345 | { |
| 346 | ifp = if_lookup_by_name (peer->update_if); |
| 347 | if (! ifp) |
| 348 | return; |
| 349 | |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 350 | if (bgp_update_address (ifp, &peer->su, &addr)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | return; |
| 352 | |
David Lamparter | 1727d2e | 2010-02-02 20:18:23 +0100 | [diff] [blame] | 353 | sockunion_bind (peer->fd, &addr, 0, &addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Source is specified with IP address. */ |
| 357 | if (peer->update_source) |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 358 | sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* BGP try to connect to the peer. */ |
| 362 | int |
| 363 | bgp_connect (struct peer *peer) |
| 364 | { |
| 365 | unsigned int ifindex = 0; |
| 366 | |
| 367 | /* Make socket for the peer. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 368 | peer->fd = sockunion_socket (&peer->su); |
| 369 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 370 | return -1; |
| 371 | |
Vipin Kumar | 48fc05f | 2014-01-09 00:31:22 +0000 | [diff] [blame] | 372 | set_nonblocking (peer->fd); |
| 373 | |
Vipin Kumar | 3374bef | 2014-01-09 00:31:22 +0000 | [diff] [blame] | 374 | /* Set socket send buffer size */ |
| 375 | bgp_update_sock_send_buffer_size(peer->fd); |
| 376 | |
David Lamparter | ef0b0c3 | 2014-05-19 22:52:04 +0200 | [diff] [blame] | 377 | bgp_set_socket_ttl (peer, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 379 | sockopt_reuseaddr (peer->fd); |
| 380 | sockopt_reuseport (peer->fd); |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 381 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 382 | #ifdef IPTOS_PREC_INTERNETCONTROL |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 383 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
| 384 | zlog_err ("%s: could not raise privs", __func__); |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 385 | if (sockunion_family (&peer->su) == AF_INET) |
| 386 | setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL); |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 387 | # ifdef HAVE_IPV6 |
| 388 | else if (sockunion_family (&peer->su) == AF_INET6) |
| 389 | setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL); |
| 390 | # endif |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 391 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
| 392 | zlog_err ("%s: could not lower privs", __func__); |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 393 | #endif |
| 394 | |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 395 | if (peer->password) |
| 396 | bgp_md5_set_connect (peer->fd, &peer->su, peer->password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 397 | |
| 398 | /* Bind socket. */ |
| 399 | bgp_bind (peer); |
| 400 | |
| 401 | /* Update source bind. */ |
| 402 | bgp_update_source (peer); |
| 403 | |
| 404 | #ifdef HAVE_IPV6 |
| 405 | if (peer->ifname) |
| 406 | ifindex = if_nametoindex (peer->ifname); |
| 407 | #endif /* HAVE_IPV6 */ |
| 408 | |
| 409 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 410 | plog_debug (peer->log, "%s [Event] Connect start to %s fd %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 411 | peer->host, peer->host, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | |
| 413 | /* Connect to the remote peer. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 414 | return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* After TCP connection is established. Get local address and port. */ |
| 418 | void |
| 419 | bgp_getsockname (struct peer *peer) |
| 420 | { |
| 421 | if (peer->su_local) |
| 422 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 423 | sockunion_free (peer->su_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 424 | peer->su_local = NULL; |
| 425 | } |
| 426 | |
| 427 | if (peer->su_remote) |
| 428 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 429 | sockunion_free (peer->su_remote); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 430 | peer->su_remote = NULL; |
| 431 | } |
| 432 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 433 | peer->su_local = sockunion_getsockname (peer->fd); |
| 434 | peer->su_remote = sockunion_getpeername (peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | |
| 436 | bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer); |
| 437 | } |
| 438 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 439 | |
| 440 | static int |
| 441 | bgp_listener (int sock, struct sockaddr *sa, socklen_t salen) |
| 442 | { |
| 443 | struct bgp_listener *listener; |
| 444 | int ret, en; |
| 445 | |
| 446 | sockopt_reuseaddr (sock); |
| 447 | sockopt_reuseport (sock); |
| 448 | |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 449 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
| 450 | zlog_err ("%s: could not raise privs", __func__); |
| 451 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 452 | #ifdef IPTOS_PREC_INTERNETCONTROL |
| 453 | if (sa->sa_family == AF_INET) |
| 454 | setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL); |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 455 | # ifdef HAVE_IPV6 |
| 456 | else if (sa->sa_family == AF_INET6) |
| 457 | setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL); |
| 458 | # endif |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 459 | #endif |
| 460 | |
David Lamparter | ca05126 | 2009-10-04 16:21:49 +0200 | [diff] [blame] | 461 | sockopt_v6only (sa->sa_family, sock); |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 462 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 463 | ret = bind (sock, sa, salen); |
| 464 | en = errno; |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 465 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
| 466 | zlog_err ("%s: could not lower privs", __func__); |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 467 | |
| 468 | if (ret < 0) |
| 469 | { |
| 470 | zlog_err ("bind: %s", safe_strerror (en)); |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | ret = listen (sock, 3); |
| 475 | if (ret < 0) |
| 476 | { |
| 477 | zlog_err ("listen: %s", safe_strerror (errno)); |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener)); |
| 482 | listener->fd = sock; |
| 483 | memcpy(&listener->su, sa, salen); |
| 484 | listener->thread = thread_add_read (master, bgp_accept, listener, sock); |
| 485 | listnode_add (bm->listen_sockets, listener); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | /* IPv6 supported version of BGP server socket setup. */ |
David Lamparter | 6d6df30 | 2014-06-28 21:12:37 +0200 | [diff] [blame] | 491 | #ifdef HAVE_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | int |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 493 | bgp_socket (unsigned short port, const char *address) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | struct addrinfo *ainfo; |
| 496 | struct addrinfo *ainfo_save; |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 497 | static const struct addrinfo req = { |
| 498 | .ai_family = AF_UNSPEC, |
| 499 | .ai_flags = AI_PASSIVE, |
| 500 | .ai_socktype = SOCK_STREAM, |
| 501 | }; |
| 502 | int ret, count; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 503 | char port_str[BUFSIZ]; |
| 504 | |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 505 | snprintf (port_str, sizeof(port_str), "%d", port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 506 | port_str[sizeof (port_str) - 1] = '\0'; |
| 507 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 508 | ret = getaddrinfo (address, port_str, &req, &ainfo_save); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 509 | if (ret != 0) |
| 510 | { |
| 511 | zlog_err ("getaddrinfo: %s", gai_strerror (ret)); |
| 512 | return -1; |
| 513 | } |
| 514 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 515 | count = 0; |
| 516 | for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | { |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 518 | int sock; |
| 519 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) |
| 521 | continue; |
| 522 | |
| 523 | sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol); |
| 524 | if (sock < 0) |
| 525 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 526 | zlog_err ("socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 527 | continue; |
| 528 | } |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 529 | |
| 530 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ |
| 531 | sockopt_ttl (ainfo->ai_family, sock, MAXTTL); |
| 532 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 533 | ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen); |
| 534 | if (ret == 0) |
| 535 | ++count; |
| 536 | else |
| 537 | close(sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | freeaddrinfo (ainfo_save); |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 540 | if (count == 0) |
| 541 | { |
| 542 | zlog_err ("%s: no usable addresses", __func__); |
| 543 | return -1; |
| 544 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 546 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | } |
| 548 | #else |
| 549 | /* Traditional IPv4 only version. */ |
| 550 | int |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 551 | bgp_socket (unsigned short port, const char *address) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 552 | { |
| 553 | int sock; |
| 554 | int socklen; |
| 555 | struct sockaddr_in sin; |
hasso | 4a1a271 | 2004-02-12 15:41:38 +0000 | [diff] [blame] | 556 | int ret, en; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 557 | |
| 558 | sock = socket (AF_INET, SOCK_STREAM, 0); |
| 559 | if (sock < 0) |
| 560 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 561 | zlog_err ("socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | return sock; |
| 563 | } |
| 564 | |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 565 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ |
| 566 | sockopt_ttl (AF_INET, sock, MAXTTL); |
| 567 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 569 | sin.sin_family = AF_INET; |
| 570 | sin.sin_port = htons (port); |
| 571 | socklen = sizeof (struct sockaddr_in); |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 572 | |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 573 | if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1)) |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 574 | { |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 575 | zlog_err("bgp_socket: could not parse ip address %s: %s", |
| 576 | address, safe_strerror (errno)); |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 577 | return ret; |
| 578 | } |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 579 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | sin.sin_len = socklen; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 581 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 583 | ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | if (ret < 0) |
| 585 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | close (sock); |
| 587 | return ret; |
| 588 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 589 | return sock; |
| 590 | } |
David Lamparter | 6d6df30 | 2014-06-28 21:12:37 +0200 | [diff] [blame] | 591 | #endif /* HAVE_IPV6 */ |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 592 | |
| 593 | void |
| 594 | bgp_close (void) |
| 595 | { |
| 596 | struct listnode *node, *next; |
| 597 | struct bgp_listener *listener; |
| 598 | |
| 599 | for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener)) |
| 600 | { |
| 601 | thread_cancel (listener->thread); |
| 602 | close (listener->fd); |
| 603 | listnode_delete (bm->listen_sockets, listener); |
| 604 | XFREE (MTYPE_BGP_LISTENER, listener); |
| 605 | } |
| 606 | } |