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 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [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 | } |
| 125 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | /* Accept bgp connection. */ |
| 127 | static int |
| 128 | bgp_accept (struct thread *thread) |
| 129 | { |
| 130 | int bgp_sock; |
| 131 | int accept_sock; |
| 132 | union sockunion su; |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 133 | struct bgp_listener *listener = THREAD_ARG(thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | struct peer *peer; |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 135 | struct peer *peer1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | char buf[SU_ADDRSTRLEN]; |
| 137 | |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 138 | /* Register accept thread. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 139 | accept_sock = THREAD_FD (thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | if (accept_sock < 0) |
| 141 | { |
| 142 | zlog_err ("accept_sock is nevative value %d", accept_sock); |
| 143 | return -1; |
| 144 | } |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 145 | listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | |
| 147 | /* Accept client connection. */ |
| 148 | bgp_sock = sockunion_accept (accept_sock, &su); |
| 149 | if (bgp_sock < 0) |
| 150 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 151 | zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | return -1; |
| 153 | } |
Stephen Hemminger | 3539858 | 2010-08-05 10:26:23 -0700 | [diff] [blame] | 154 | set_nonblocking (bgp_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | |
| 156 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 157 | zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | |
| 159 | /* Check remote IP address */ |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 160 | peer1 = peer_lookup (NULL, &su); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 161 | if (! peer1 || peer1->status == Idle) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | { |
| 163 | if (BGP_DEBUG (events, EVENTS)) |
| 164 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 165 | if (! peer1) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 166 | zlog_debug ("[Event] BGP connection IP address %s is not configured", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | inet_sutop (&su, buf)); |
| 168 | else |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 169 | zlog_debug ("[Event] BGP connection IP address %s is Idle state", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | inet_sutop (&su, buf)); |
| 171 | } |
| 172 | close (bgp_sock); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | /* In case of peer is EBGP, we should set TTL for this connection. */ |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 177 | if (peer_sort (peer1) == BGP_PEER_EBGP) { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 178 | sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl); |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 179 | if (peer1->gtsm_hops) |
| 180 | sockopt_minttl (peer1->su.sa.sa_family, bgp_sock, MAXTTL + 1 - peer1->gtsm_hops); |
| 181 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 183 | /* Make dummy peer until read Open packet. */ |
| 184 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 185 | zlog_debug ("[Event] Make dummy peer structure until read Open packet"); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 186 | |
| 187 | { |
| 188 | char buf[SU_ADDRSTRLEN + 1]; |
| 189 | |
Stephen Hemminger | 5bd5881 | 2009-08-06 21:05:47 -0700 | [diff] [blame] | 190 | peer = peer_create_accept (peer1->bgp); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 191 | SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER); |
| 192 | peer->su = su; |
| 193 | peer->fd = bgp_sock; |
| 194 | peer->status = Active; |
| 195 | peer->local_id = peer1->local_id; |
Timo Teräs | e8eb000 | 2009-02-17 12:14:23 +0200 | [diff] [blame] | 196 | peer->v_holdtime = peer1->v_holdtime; |
| 197 | peer->v_keepalive = peer1->v_keepalive; |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 198 | |
| 199 | /* Make peer's address string. */ |
| 200 | sockunion2str (&su, buf, SU_ADDRSTRLEN); |
paul | e83e208 | 2005-05-19 02:12:25 +0000 | [diff] [blame] | 201 | peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 202 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | |
| 204 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* BGP socket bind. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 210 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | bgp_bind (struct peer *peer) |
| 212 | { |
| 213 | #ifdef SO_BINDTODEVICE |
| 214 | int ret; |
| 215 | struct ifreq ifreq; |
| 216 | |
| 217 | if (! peer->ifname) |
| 218 | return 0; |
| 219 | |
| 220 | strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name)); |
| 221 | |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 222 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 223 | zlog_err ("bgp_bind: could not raise privs"); |
| 224 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 225 | ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | &ifreq, sizeof (ifreq)); |
paul | 98f5163 | 2004-10-25 14:19:15 +0000 | [diff] [blame] | 227 | |
| 228 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 229 | zlog_err ("bgp_bind: could not lower privs"); |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | if (ret < 0) |
| 232 | { |
| 233 | zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname); |
| 234 | return ret; |
| 235 | } |
| 236 | #endif /* SO_BINDTODEVICE */ |
| 237 | return 0; |
| 238 | } |
| 239 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 240 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | bgp_bind_address (int sock, struct in_addr *addr) |
| 242 | { |
| 243 | int ret; |
| 244 | struct sockaddr_in local; |
| 245 | |
| 246 | memset (&local, 0, sizeof (struct sockaddr_in)); |
| 247 | local.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 248 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | local.sin_len = sizeof(struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 250 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | memcpy (&local.sin_addr, addr, sizeof (struct in_addr)); |
| 252 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 253 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 254 | zlog_err ("bgp_bind_address: could not raise privs"); |
| 255 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in)); |
| 257 | if (ret < 0) |
| 258 | ; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 259 | |
| 260 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 261 | zlog_err ("bgp_bind_address: could not lower privs"); |
| 262 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 266 | static struct in_addr * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | bgp_update_address (struct interface *ifp) |
| 268 | { |
| 269 | struct prefix_ipv4 *p; |
| 270 | struct connected *connected; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 271 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 273 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 274 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 275 | p = (struct prefix_ipv4 *) connected->address; |
| 276 | |
| 277 | if (p->family == AF_INET) |
| 278 | return &p->prefix; |
| 279 | } |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | /* Update source selection. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 284 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | bgp_update_source (struct peer *peer) |
| 286 | { |
| 287 | struct interface *ifp; |
| 288 | struct in_addr *addr; |
| 289 | |
| 290 | /* Source is specified with interface name. */ |
| 291 | if (peer->update_if) |
| 292 | { |
| 293 | ifp = if_lookup_by_name (peer->update_if); |
| 294 | if (! ifp) |
| 295 | return; |
| 296 | |
| 297 | addr = bgp_update_address (ifp); |
| 298 | if (! addr) |
| 299 | return; |
| 300 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 301 | bgp_bind_address (peer->fd, addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Source is specified with IP address. */ |
| 305 | if (peer->update_source) |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 306 | sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* BGP try to connect to the peer. */ |
| 310 | int |
| 311 | bgp_connect (struct peer *peer) |
| 312 | { |
| 313 | unsigned int ifindex = 0; |
| 314 | |
| 315 | /* Make socket for the peer. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 316 | peer->fd = sockunion_socket (&peer->su); |
| 317 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | return -1; |
| 319 | |
| 320 | /* If we can get socket for the peer, adjest TTL and make connection. */ |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 321 | if (peer_sort (peer) == BGP_PEER_EBGP) { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 322 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 323 | if (peer->gtsm_hops) |
| 324 | sockopt_minttl (peer->su.sa.sa_family, peer->fd, MAXTTL + 1 - peer->gtsm_hops); |
| 325 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 327 | sockopt_reuseaddr (peer->fd); |
| 328 | sockopt_reuseport (peer->fd); |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 329 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 330 | #ifdef IPTOS_PREC_INTERNETCONTROL |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 331 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
| 332 | zlog_err ("%s: could not raise privs", __func__); |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 333 | if (sockunion_family (&peer->su) == AF_INET) |
| 334 | setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL); |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 335 | # ifdef HAVE_IPV6 |
| 336 | else if (sockunion_family (&peer->su) == AF_INET6) |
| 337 | setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL); |
| 338 | # endif |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 339 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
| 340 | zlog_err ("%s: could not lower privs", __func__); |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 341 | #endif |
| 342 | |
Paul Jakma | 0df7c91 | 2008-07-21 21:02:49 +0000 | [diff] [blame] | 343 | if (peer->password) |
| 344 | bgp_md5_set_connect (peer->fd, &peer->su, peer->password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 345 | |
| 346 | /* Bind socket. */ |
| 347 | bgp_bind (peer); |
| 348 | |
| 349 | /* Update source bind. */ |
| 350 | bgp_update_source (peer); |
| 351 | |
| 352 | #ifdef HAVE_IPV6 |
| 353 | if (peer->ifname) |
| 354 | ifindex = if_nametoindex (peer->ifname); |
| 355 | #endif /* HAVE_IPV6 */ |
| 356 | |
| 357 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 358 | plog_debug (peer->log, "%s [Event] Connect start to %s fd %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 359 | peer->host, peer->host, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | |
| 361 | /* Connect to the remote peer. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 362 | return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* After TCP connection is established. Get local address and port. */ |
| 366 | void |
| 367 | bgp_getsockname (struct peer *peer) |
| 368 | { |
| 369 | if (peer->su_local) |
| 370 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 371 | sockunion_free (peer->su_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | peer->su_local = NULL; |
| 373 | } |
| 374 | |
| 375 | if (peer->su_remote) |
| 376 | { |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 377 | sockunion_free (peer->su_remote); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | peer->su_remote = NULL; |
| 379 | } |
| 380 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 381 | peer->su_local = sockunion_getsockname (peer->fd); |
| 382 | peer->su_remote = sockunion_getpeername (peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | |
| 384 | bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer); |
| 385 | } |
| 386 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 387 | |
| 388 | static int |
| 389 | bgp_listener (int sock, struct sockaddr *sa, socklen_t salen) |
| 390 | { |
| 391 | struct bgp_listener *listener; |
| 392 | int ret, en; |
| 393 | |
| 394 | sockopt_reuseaddr (sock); |
| 395 | sockopt_reuseport (sock); |
| 396 | |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 397 | if (bgpd_privs.change (ZPRIVS_RAISE)) |
| 398 | zlog_err ("%s: could not raise privs", __func__); |
| 399 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 400 | #ifdef IPTOS_PREC_INTERNETCONTROL |
| 401 | if (sa->sa_family == AF_INET) |
| 402 | setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL); |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 403 | # ifdef HAVE_IPV6 |
| 404 | else if (sa->sa_family == AF_INET6) |
| 405 | setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL); |
| 406 | # endif |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 407 | #endif |
| 408 | |
| 409 | #ifdef IPV6_V6ONLY |
| 410 | /* Want only IPV6 on ipv6 socket (not mapped addresses) */ |
| 411 | if (sa->sa_family == AF_INET6) { |
| 412 | int on = 1; |
| 413 | setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 414 | (void *) &on, sizeof (on)); |
| 415 | } |
| 416 | #endif |
| 417 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 418 | ret = bind (sock, sa, salen); |
| 419 | en = errno; |
Chris Luke | 5c88f19 | 2011-10-18 17:26:51 +0400 | [diff] [blame] | 420 | if (bgpd_privs.change (ZPRIVS_LOWER)) |
| 421 | zlog_err ("%s: could not lower privs", __func__); |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 422 | |
| 423 | if (ret < 0) |
| 424 | { |
| 425 | zlog_err ("bind: %s", safe_strerror (en)); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | ret = listen (sock, 3); |
| 430 | if (ret < 0) |
| 431 | { |
| 432 | zlog_err ("listen: %s", safe_strerror (errno)); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener)); |
| 437 | listener->fd = sock; |
| 438 | memcpy(&listener->su, sa, salen); |
| 439 | listener->thread = thread_add_read (master, bgp_accept, listener, sock); |
| 440 | listnode_add (bm->listen_sockets, listener); |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | /* IPv6 supported version of BGP server socket setup. */ |
| 446 | #if defined (HAVE_IPV6) && ! defined (NRL) |
| 447 | int |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 448 | bgp_socket (unsigned short port, const char *address) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 449 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | struct addrinfo *ainfo; |
| 451 | struct addrinfo *ainfo_save; |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 452 | static const struct addrinfo req = { |
| 453 | .ai_family = AF_UNSPEC, |
| 454 | .ai_flags = AI_PASSIVE, |
| 455 | .ai_socktype = SOCK_STREAM, |
| 456 | }; |
| 457 | int ret, count; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | char port_str[BUFSIZ]; |
| 459 | |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 460 | snprintf (port_str, sizeof(port_str), "%d", port); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 461 | port_str[sizeof (port_str) - 1] = '\0'; |
| 462 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 463 | ret = getaddrinfo (address, port_str, &req, &ainfo_save); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | if (ret != 0) |
| 465 | { |
| 466 | zlog_err ("getaddrinfo: %s", gai_strerror (ret)); |
| 467 | return -1; |
| 468 | } |
| 469 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 470 | count = 0; |
| 471 | for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 472 | { |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 473 | int sock; |
| 474 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 475 | if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) |
| 476 | continue; |
| 477 | |
| 478 | sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol); |
| 479 | if (sock < 0) |
| 480 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 481 | zlog_err ("socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 482 | continue; |
| 483 | } |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 484 | |
| 485 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ |
| 486 | sockopt_ttl (ainfo->ai_family, sock, MAXTTL); |
| 487 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 488 | ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen); |
| 489 | if (ret == 0) |
| 490 | ++count; |
| 491 | else |
| 492 | close(sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 493 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | freeaddrinfo (ainfo_save); |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 495 | if (count == 0) |
| 496 | { |
| 497 | zlog_err ("%s: no usable addresses", __func__); |
| 498 | return -1; |
| 499 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 500 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 501 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 502 | } |
| 503 | #else |
| 504 | /* Traditional IPv4 only version. */ |
| 505 | int |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 506 | bgp_socket (unsigned short port, const char *address) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | { |
| 508 | int sock; |
| 509 | int socklen; |
| 510 | struct sockaddr_in sin; |
hasso | 4a1a271 | 2004-02-12 15:41:38 +0000 | [diff] [blame] | 511 | int ret, en; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | |
| 513 | sock = socket (AF_INET, SOCK_STREAM, 0); |
| 514 | if (sock < 0) |
| 515 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 516 | zlog_err ("socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | return sock; |
| 518 | } |
| 519 | |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 520 | /* if we intend to implement ttl-security, this socket needs ttl=255 */ |
| 521 | sockopt_ttl (AF_INET, sock, MAXTTL); |
| 522 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | sin.sin_family = AF_INET; |
| 525 | sin.sin_port = htons (port); |
| 526 | socklen = sizeof (struct sockaddr_in); |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 527 | |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 528 | if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1)) |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 529 | { |
Paul Jakma | 90b6876 | 2008-01-29 17:26:34 +0000 | [diff] [blame] | 530 | zlog_err("bgp_socket: could not parse ip address %s: %s", |
| 531 | address, safe_strerror (errno)); |
Paul Jakma | 3a02d1f | 2007-11-01 14:29:11 +0000 | [diff] [blame] | 532 | return ret; |
| 533 | } |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 534 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 535 | sin.sin_len = socklen; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 536 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 538 | ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | if (ret < 0) |
| 540 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | close (sock); |
| 542 | return ret; |
| 543 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 544 | return sock; |
| 545 | } |
| 546 | #endif /* HAVE_IPV6 && !NRL */ |
Stephen Hemminger | d023aec | 2009-07-21 16:27:21 -0700 | [diff] [blame] | 547 | |
| 548 | void |
| 549 | bgp_close (void) |
| 550 | { |
| 551 | struct listnode *node, *next; |
| 552 | struct bgp_listener *listener; |
| 553 | |
| 554 | for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener)) |
| 555 | { |
| 556 | thread_cancel (listener->thread); |
| 557 | close (listener->fd); |
| 558 | listnode_delete (bm->listen_sockets, listener); |
| 559 | XFREE (MTYPE_BGP_LISTENER, listener); |
| 560 | } |
| 561 | } |