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