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