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" |
| 25 | #include "memory.h" |
| 26 | #include "log.h" |
| 27 | #include "if.h" |
| 28 | #include "prefix.h" |
| 29 | #include "command.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 30 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
| 32 | #include "bgpd/bgpd.h" |
| 33 | #include "bgpd/bgp_fsm.h" |
| 34 | #include "bgpd/bgp_attr.h" |
| 35 | #include "bgpd/bgp_debug.h" |
| 36 | #include "bgpd/bgp_network.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 37 | |
| 38 | extern struct zebra_privs_t bgpd_privs; |
| 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | /* Accept bgp connection. */ |
| 42 | static int |
| 43 | bgp_accept (struct thread *thread) |
| 44 | { |
| 45 | int bgp_sock; |
| 46 | int accept_sock; |
| 47 | union sockunion su; |
| 48 | struct peer *peer; |
| 49 | struct peer *peer1; |
| 50 | struct bgp *bgp; |
| 51 | char buf[SU_ADDRSTRLEN]; |
| 52 | |
| 53 | /* Regiser accept thread. */ |
| 54 | accept_sock = THREAD_FD (thread); |
| 55 | bgp = THREAD_ARG (thread); |
| 56 | |
| 57 | if (accept_sock < 0) |
| 58 | { |
| 59 | zlog_err ("accept_sock is nevative value %d", accept_sock); |
| 60 | return -1; |
| 61 | } |
| 62 | thread_add_read (master, bgp_accept, bgp, accept_sock); |
| 63 | |
| 64 | /* Accept client connection. */ |
| 65 | bgp_sock = sockunion_accept (accept_sock, &su); |
| 66 | if (bgp_sock < 0) |
| 67 | { |
| 68 | zlog_err ("[Error] BGP socket accept failed (%s)", strerror (errno)); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | if (BGP_DEBUG (events, EVENTS)) |
| 73 | zlog_info ("[Event] BGP connection from host %s", inet_sutop (&su, buf)); |
| 74 | |
| 75 | /* Check remote IP address */ |
| 76 | peer1 = peer_lookup (bgp, &su); |
| 77 | if (! peer1 || peer1->status == Idle) |
| 78 | { |
| 79 | if (BGP_DEBUG (events, EVENTS)) |
| 80 | { |
| 81 | if (! peer1) |
| 82 | zlog_info ("[Event] BGP connection IP address %s is not configured", |
| 83 | inet_sutop (&su, buf)); |
| 84 | else |
| 85 | zlog_info ("[Event] BGP connection IP address %s is Idle state", |
| 86 | inet_sutop (&su, buf)); |
| 87 | } |
| 88 | close (bgp_sock); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | /* In case of peer is EBGP, we should set TTL for this connection. */ |
| 93 | if (peer_sort (peer1) == BGP_PEER_EBGP) |
| 94 | sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl); |
| 95 | |
| 96 | if (! bgp) |
| 97 | bgp = peer1->bgp; |
| 98 | |
| 99 | /* Make dummy peer until read Open packet. */ |
| 100 | if (BGP_DEBUG (events, EVENTS)) |
| 101 | zlog_info ("[Event] Make dummy peer structure until read Open packet"); |
| 102 | |
| 103 | { |
| 104 | char buf[SU_ADDRSTRLEN + 1]; |
| 105 | |
| 106 | peer = peer_create_accept (bgp); |
| 107 | SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER); |
| 108 | peer->su = su; |
| 109 | peer->fd = bgp_sock; |
| 110 | peer->status = Active; |
| 111 | peer->local_id = peer1->local_id; |
| 112 | |
| 113 | /* Make peer's address string. */ |
| 114 | sockunion2str (&su, buf, SU_ADDRSTRLEN); |
| 115 | peer->host = strdup (buf); |
| 116 | } |
| 117 | |
| 118 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* BGP socket bind. */ |
| 124 | int |
| 125 | bgp_bind (struct peer *peer) |
| 126 | { |
| 127 | #ifdef SO_BINDTODEVICE |
| 128 | int ret; |
| 129 | struct ifreq ifreq; |
| 130 | |
| 131 | if (! peer->ifname) |
| 132 | return 0; |
| 133 | |
| 134 | strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name)); |
| 135 | |
| 136 | ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, |
| 137 | &ifreq, sizeof (ifreq)); |
| 138 | if (ret < 0) |
| 139 | { |
| 140 | zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname); |
| 141 | return ret; |
| 142 | } |
| 143 | #endif /* SO_BINDTODEVICE */ |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int |
| 148 | bgp_bind_address (int sock, struct in_addr *addr) |
| 149 | { |
| 150 | int ret; |
| 151 | struct sockaddr_in local; |
| 152 | |
| 153 | memset (&local, 0, sizeof (struct sockaddr_in)); |
| 154 | local.sin_family = AF_INET; |
| 155 | #ifdef HAVE_SIN_LEN |
| 156 | local.sin_len = sizeof(struct sockaddr_in); |
| 157 | #endif /* HAVE_SIN_LEN */ |
| 158 | memcpy (&local.sin_addr, addr, sizeof (struct in_addr)); |
| 159 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 160 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 161 | zlog_err ("bgp_bind_address: could not raise privs"); |
| 162 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in)); |
| 164 | if (ret < 0) |
| 165 | ; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 166 | |
| 167 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 168 | zlog_err ("bgp_bind_address: could not lower privs"); |
| 169 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | struct in_addr * |
| 174 | bgp_update_address (struct interface *ifp) |
| 175 | { |
| 176 | struct prefix_ipv4 *p; |
| 177 | struct connected *connected; |
| 178 | listnode node; |
| 179 | |
| 180 | for (node = listhead (ifp->connected); node; nextnode (node)) |
| 181 | { |
| 182 | connected = getdata (node); |
| 183 | |
| 184 | p = (struct prefix_ipv4 *) connected->address; |
| 185 | |
| 186 | if (p->family == AF_INET) |
| 187 | return &p->prefix; |
| 188 | } |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | /* Update source selection. */ |
| 193 | void |
| 194 | bgp_update_source (struct peer *peer) |
| 195 | { |
| 196 | struct interface *ifp; |
| 197 | struct in_addr *addr; |
| 198 | |
| 199 | /* Source is specified with interface name. */ |
| 200 | if (peer->update_if) |
| 201 | { |
| 202 | ifp = if_lookup_by_name (peer->update_if); |
| 203 | if (! ifp) |
| 204 | return; |
| 205 | |
| 206 | addr = bgp_update_address (ifp); |
| 207 | if (! addr) |
| 208 | return; |
| 209 | |
| 210 | bgp_bind_address (peer->fd, addr); |
| 211 | } |
| 212 | |
| 213 | /* Source is specified with IP address. */ |
| 214 | if (peer->update_source) |
| 215 | sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source); |
| 216 | } |
| 217 | |
| 218 | /* BGP try to connect to the peer. */ |
| 219 | int |
| 220 | bgp_connect (struct peer *peer) |
| 221 | { |
| 222 | unsigned int ifindex = 0; |
| 223 | |
| 224 | /* Make socket for the peer. */ |
| 225 | peer->fd = sockunion_socket (&peer->su); |
| 226 | if (peer->fd < 0) |
| 227 | return -1; |
| 228 | |
| 229 | /* If we can get socket for the peer, adjest TTL and make connection. */ |
| 230 | if (peer_sort (peer) == BGP_PEER_EBGP) |
| 231 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
| 232 | |
| 233 | sockopt_reuseaddr (peer->fd); |
| 234 | sockopt_reuseport (peer->fd); |
| 235 | |
| 236 | /* Bind socket. */ |
| 237 | bgp_bind (peer); |
| 238 | |
| 239 | /* Update source bind. */ |
| 240 | bgp_update_source (peer); |
| 241 | |
| 242 | #ifdef HAVE_IPV6 |
| 243 | if (peer->ifname) |
| 244 | ifindex = if_nametoindex (peer->ifname); |
| 245 | #endif /* HAVE_IPV6 */ |
| 246 | |
| 247 | if (BGP_DEBUG (events, EVENTS)) |
| 248 | plog_info (peer->log, "%s [Event] Connect start to %s fd %d", |
| 249 | peer->host, peer->host, peer->fd); |
| 250 | |
| 251 | /* Connect to the remote peer. */ |
| 252 | return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex); |
| 253 | } |
| 254 | |
| 255 | /* After TCP connection is established. Get local address and port. */ |
| 256 | void |
| 257 | bgp_getsockname (struct peer *peer) |
| 258 | { |
| 259 | if (peer->su_local) |
| 260 | { |
| 261 | XFREE (MTYPE_TMP, peer->su_local); |
| 262 | peer->su_local = NULL; |
| 263 | } |
| 264 | |
| 265 | if (peer->su_remote) |
| 266 | { |
| 267 | XFREE (MTYPE_TMP, peer->su_remote); |
| 268 | peer->su_remote = NULL; |
| 269 | } |
| 270 | |
| 271 | peer->su_local = sockunion_getsockname (peer->fd); |
| 272 | peer->su_remote = sockunion_getpeername (peer->fd); |
| 273 | |
| 274 | bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer); |
| 275 | } |
| 276 | |
| 277 | /* IPv6 supported version of BGP server socket setup. */ |
| 278 | #if defined (HAVE_IPV6) && ! defined (NRL) |
| 279 | int |
| 280 | bgp_socket (struct bgp *bgp, unsigned short port) |
| 281 | { |
| 282 | int ret; |
| 283 | struct addrinfo req; |
| 284 | struct addrinfo *ainfo; |
| 285 | struct addrinfo *ainfo_save; |
| 286 | int sock = 0; |
| 287 | char port_str[BUFSIZ]; |
| 288 | |
| 289 | memset (&req, 0, sizeof (struct addrinfo)); |
| 290 | |
| 291 | req.ai_flags = AI_PASSIVE; |
| 292 | req.ai_family = AF_UNSPEC; |
| 293 | req.ai_socktype = SOCK_STREAM; |
| 294 | sprintf (port_str, "%d", port); |
| 295 | port_str[sizeof (port_str) - 1] = '\0'; |
| 296 | |
| 297 | ret = getaddrinfo (NULL, port_str, &req, &ainfo); |
| 298 | if (ret != 0) |
| 299 | { |
| 300 | zlog_err ("getaddrinfo: %s", gai_strerror (ret)); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | ainfo_save = ainfo; |
| 305 | |
| 306 | do |
| 307 | { |
| 308 | if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) |
| 309 | continue; |
| 310 | |
| 311 | sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol); |
| 312 | if (sock < 0) |
| 313 | { |
| 314 | zlog_err ("socket: %s", strerror (errno)); |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | sockopt_reuseaddr (sock); |
| 319 | sockopt_reuseport (sock); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 320 | |
| 321 | if (bgpd_privs.change (ZPRIVS_RAISE) ) |
| 322 | zlog_err ("bgp_socket: could not raise privs"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | |
| 324 | ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen); |
| 325 | if (ret < 0) |
| 326 | { |
| 327 | zlog_err ("bind: %s", strerror (errno)); |
| 328 | close (sock); |
| 329 | continue; |
| 330 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 331 | |
| 332 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 333 | zlog_err ("bgp_bind_address: could not lower privs"); |
| 334 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | ret = listen (sock, 3); |
| 336 | if (ret < 0) |
| 337 | { |
| 338 | zlog_err ("listen: %s", strerror (errno)); |
| 339 | close (sock); |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | thread_add_read (master, bgp_accept, bgp, sock); |
| 344 | } |
| 345 | while ((ainfo = ainfo->ai_next) != NULL); |
| 346 | |
| 347 | freeaddrinfo (ainfo_save); |
| 348 | |
| 349 | return sock; |
| 350 | } |
| 351 | #else |
| 352 | /* Traditional IPv4 only version. */ |
| 353 | int |
| 354 | bgp_socket (struct bgp *bgp, unsigned short port) |
| 355 | { |
| 356 | int sock; |
| 357 | int socklen; |
| 358 | struct sockaddr_in sin; |
| 359 | int ret; |
| 360 | |
| 361 | sock = socket (AF_INET, SOCK_STREAM, 0); |
| 362 | if (sock < 0) |
| 363 | { |
| 364 | zlog_err ("socket: %s", strerror (errno)); |
| 365 | return sock; |
| 366 | } |
| 367 | |
| 368 | sockopt_reuseaddr (sock); |
| 369 | sockopt_reuseport (sock); |
| 370 | |
| 371 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
| 372 | |
| 373 | sin.sin_family = AF_INET; |
| 374 | sin.sin_port = htons (port); |
| 375 | socklen = sizeof (struct sockaddr_in); |
| 376 | #ifdef HAVE_SIN_LEN |
| 377 | sin.sin_len = socklen; |
| 378 | #endif /* HAVE_SIN_LEN */ |
| 379 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 380 | if ( bgpd_privs.change (ZPRIVS_RAISE) ) |
| 381 | zlog_err ("bgp_socket: could not raise privs"); |
| 382 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | ret = bind (sock, (struct sockaddr *) &sin, socklen); |
| 384 | if (ret < 0) |
| 385 | { |
| 386 | zlog_err ("bind: %s", strerror (errno)); |
| 387 | close (sock); |
| 388 | return ret; |
| 389 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 390 | |
| 391 | if (bgpd_privs.change (ZPRIVS_LOWER) ) |
| 392 | zlog_err ("bgp_socket: could not lower privs"); |
| 393 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 394 | ret = listen (sock, 3); |
| 395 | if (ret < 0) |
| 396 | { |
| 397 | zlog_err ("listen: %s", strerror (errno)); |
| 398 | close (sock); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | thread_add_read (bm->master, bgp_accept, bgp, sock); |
| 403 | |
| 404 | return sock; |
| 405 | } |
| 406 | #endif /* HAVE_IPV6 && !NRL */ |