ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / bgpd / bgp_network.c
blob4cb6f7d848d787d33ca40151202138b401d85f43
1 /* BGP network related fucntions
2 Copyright (C) 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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.
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.
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. */
21 #include <zebra.h>
23 #include "thread.h"
24 #include "sockunion.h"
25 #include "sockopt.h"
26 #include "memory.h"
27 #include "log.h"
28 #include "if.h"
29 #include "prefix.h"
30 #include "command.h"
31 #include "privs.h"
32 #include "linklist.h"
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"
40 extern struct zebra_privs_t bgpd_privs;
42 /* BGP listening socket. */
43 struct bgp_listener
45 int fd;
46 union sockunion su;
47 struct thread *thread;
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.
54 static int
55 bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
57 int ret = -1;
58 int en = ENOSYS;
60 assert (socket >= 0);
62 #if HAVE_DECL_TCP_MD5SIG
63 ret = sockopt_tcp_signature (socket, su, password);
64 en = errno;
65 #endif /* HAVE_TCP_MD5SIG */
67 if (ret < 0)
68 zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
69 socket, safe_strerror (en));
71 return ret;
74 /* Helper for bgp_connect */
75 static int
76 bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
78 int ret = -1;
80 #if HAVE_DECL_TCP_MD5SIG
81 if ( bgpd_privs.change (ZPRIVS_RAISE) )
83 zlog_err ("%s: could not raise privs", __func__);
84 return ret;
87 ret = bgp_md5_set_socket (socket, su, password);
89 if (bgpd_privs.change (ZPRIVS_LOWER) )
90 zlog_err ("%s: could not lower privs", __func__);
91 #endif /* HAVE_TCP_MD5SIG */
93 return ret;
96 int
97 bgp_md5_set (struct peer *peer)
99 struct listnode *node;
100 int fret = 0, ret;
101 int *socket;
103 if ( bgpd_privs.change (ZPRIVS_RAISE) )
105 zlog_err ("%s: could not raise privs", __func__);
106 return -1;
109 /* Just set the password on the listen socket(s). Outbound connections
110 * are taken care of in bgp_connect() below.
112 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket))
114 ret = bgp_md5_set_socket ((int)(long)socket, &peer->su, peer->password);
115 if (ret < 0)
116 fret = ret;
118 if (bgpd_privs.change (ZPRIVS_LOWER) )
119 zlog_err ("%s: could not lower privs", __func__);
121 return fret;
124 /* Accept bgp connection. */
125 static int
126 bgp_accept (struct thread *thread)
128 int bgp_sock;
129 int accept_sock;
130 union sockunion su;
131 struct bgp_listener *listener = THREAD_ARG(thread);
132 struct peer *peer;
133 struct peer *peer1;
134 char buf[SU_ADDRSTRLEN];
136 /* Register accept thread. */
137 accept_sock = THREAD_FD (thread);
138 if (accept_sock < 0)
140 zlog_err ("accept_sock is nevative value %d", accept_sock);
141 return -1;
143 listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock);
145 /* Accept client connection. */
146 bgp_sock = sockunion_accept (accept_sock, &su);
147 if (bgp_sock < 0)
149 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
150 return -1;
153 if (BGP_DEBUG (events, EVENTS))
154 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
156 /* Check remote IP address */
157 peer1 = peer_lookup (NULL, &su);
158 if (! peer1 || peer1->status == Idle)
160 if (BGP_DEBUG (events, EVENTS))
162 if (! peer1)
163 zlog_debug ("[Event] BGP connection IP address %s is not configured",
164 inet_sutop (&su, buf));
165 else
166 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
167 inet_sutop (&su, buf));
169 close (bgp_sock);
170 return -1;
173 /* In case of peer is EBGP, we should set TTL for this connection. */
174 if (peer_sort (peer1) == BGP_PEER_EBGP)
175 sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
177 /* Make dummy peer until read Open packet. */
178 if (BGP_DEBUG (events, EVENTS))
179 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
182 char buf[SU_ADDRSTRLEN + 1];
184 peer = peer_create_accept (peer1->bgp);
185 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
186 peer->su = su;
187 peer->fd = bgp_sock;
188 peer->status = Active;
189 peer->local_id = peer1->local_id;
190 peer->v_holdtime = peer1->v_holdtime;
191 peer->v_keepalive = peer1->v_keepalive;
193 /* Make peer's address string. */
194 sockunion2str (&su, buf, SU_ADDRSTRLEN);
195 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
198 BGP_EVENT_ADD (peer, TCP_connection_open);
200 return 0;
203 /* BGP socket bind. */
204 static int
205 bgp_bind (struct peer *peer)
207 #ifdef SO_BINDTODEVICE
208 int ret;
209 struct ifreq ifreq;
211 if (! peer->ifname)
212 return 0;
214 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
216 if ( bgpd_privs.change (ZPRIVS_RAISE) )
217 zlog_err ("bgp_bind: could not raise privs");
219 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
220 &ifreq, sizeof (ifreq));
222 if (bgpd_privs.change (ZPRIVS_LOWER) )
223 zlog_err ("bgp_bind: could not lower privs");
225 if (ret < 0)
227 zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
228 return ret;
230 #endif /* SO_BINDTODEVICE */
231 return 0;
234 static int
235 bgp_bind_address (int sock, struct in_addr *addr)
237 int ret;
238 struct sockaddr_in local;
240 memset (&local, 0, sizeof (struct sockaddr_in));
241 local.sin_family = AF_INET;
242 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
243 local.sin_len = sizeof(struct sockaddr_in);
244 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
245 memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
247 if ( bgpd_privs.change (ZPRIVS_RAISE) )
248 zlog_err ("bgp_bind_address: could not raise privs");
250 ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
251 if (ret < 0)
254 if (bgpd_privs.change (ZPRIVS_LOWER) )
255 zlog_err ("bgp_bind_address: could not lower privs");
257 return 0;
260 static struct in_addr *
261 bgp_update_address (struct interface *ifp)
263 struct prefix_ipv4 *p;
264 struct connected *connected;
265 struct listnode *node;
267 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
269 p = (struct prefix_ipv4 *) connected->address;
271 if (p->family == AF_INET)
272 return &p->prefix;
274 return NULL;
277 /* Update source selection. */
278 static void
279 bgp_update_source (struct peer *peer)
281 struct interface *ifp;
282 struct in_addr *addr;
284 /* Source is specified with interface name. */
285 if (peer->update_if)
287 ifp = if_lookup_by_name (peer->update_if);
288 if (! ifp)
289 return;
291 addr = bgp_update_address (ifp);
292 if (! addr)
293 return;
295 bgp_bind_address (peer->fd, addr);
298 /* Source is specified with IP address. */
299 if (peer->update_source)
300 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
303 /* BGP try to connect to the peer. */
305 bgp_connect (struct peer *peer)
307 unsigned int ifindex = 0;
309 /* Make socket for the peer. */
310 peer->fd = sockunion_socket (&peer->su);
311 if (peer->fd < 0)
312 return -1;
314 /* If we can get socket for the peer, adjest TTL and make connection. */
315 if (peer_sort (peer) == BGP_PEER_EBGP)
316 sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
318 sockopt_reuseaddr (peer->fd);
319 sockopt_reuseport (peer->fd);
321 #ifdef IPTOS_PREC_INTERNETCONTROL
322 if (sockunion_family (&peer->su) == AF_INET)
323 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
324 #endif
326 if (peer->password)
327 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
329 /* Bind socket. */
330 bgp_bind (peer);
332 /* Update source bind. */
333 bgp_update_source (peer);
335 #ifdef HAVE_IPV6
336 if (peer->ifname)
337 ifindex = if_nametoindex (peer->ifname);
338 #endif /* HAVE_IPV6 */
340 if (BGP_DEBUG (events, EVENTS))
341 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
342 peer->host, peer->host, peer->fd);
344 /* Connect to the remote peer. */
345 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
348 /* After TCP connection is established. Get local address and port. */
349 void
350 bgp_getsockname (struct peer *peer)
352 if (peer->su_local)
354 sockunion_free (peer->su_local);
355 peer->su_local = NULL;
358 if (peer->su_remote)
360 sockunion_free (peer->su_remote);
361 peer->su_remote = NULL;
364 peer->su_local = sockunion_getsockname (peer->fd);
365 peer->su_remote = sockunion_getpeername (peer->fd);
367 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
371 static int
372 bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
374 struct bgp_listener *listener;
375 int ret, en;
377 sockopt_reuseaddr (sock);
378 sockopt_reuseport (sock);
380 #ifdef IPTOS_PREC_INTERNETCONTROL
381 if (sa->sa_family == AF_INET)
382 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
383 #endif
385 #ifdef IPV6_V6ONLY
386 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
387 if (sa->sa_family == AF_INET6) {
388 int on = 1;
389 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
390 (void *) &on, sizeof (on));
392 #endif
394 if (bgpd_privs.change (ZPRIVS_RAISE) )
395 zlog_err ("bgp_socket: could not raise privs");
397 ret = bind (sock, sa, salen);
398 en = errno;
399 if (bgpd_privs.change (ZPRIVS_LOWER) )
400 zlog_err ("bgp_bind_address: could not lower privs");
402 if (ret < 0)
404 zlog_err ("bind: %s", safe_strerror (en));
405 return ret;
408 ret = listen (sock, 3);
409 if (ret < 0)
411 zlog_err ("listen: %s", safe_strerror (errno));
412 return ret;
415 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
416 listener->fd = sock;
417 memcpy(&listener->su, sa, salen);
418 listener->thread = thread_add_read (master, bgp_accept, listener, sock);
419 listnode_add (bm->listen_sockets, listener);
421 return 0;
424 /* IPv6 supported version of BGP server socket setup. */
425 #if defined (HAVE_IPV6) && ! defined (NRL)
427 bgp_socket (unsigned short port, const char *address)
429 struct addrinfo *ainfo;
430 struct addrinfo *ainfo_save;
431 static const struct addrinfo req = {
432 .ai_family = AF_UNSPEC,
433 .ai_flags = AI_PASSIVE,
434 .ai_socktype = SOCK_STREAM,
436 int ret, count;
437 char port_str[BUFSIZ];
439 snprintf (port_str, sizeof(port_str), "%d", port);
440 port_str[sizeof (port_str) - 1] = '\0';
442 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
443 if (ret != 0)
445 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
446 return -1;
449 count = 0;
450 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
452 int sock;
454 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
455 continue;
457 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
458 if (sock < 0)
460 zlog_err ("socket: %s", safe_strerror (errno));
461 continue;
464 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
465 if (ret == 0)
466 ++count;
467 else
468 close(sock);
470 freeaddrinfo (ainfo_save);
471 if (count == 0)
473 zlog_err ("%s: no usable addresses", __func__);
474 return -1;
477 return 0;
479 #else
480 /* Traditional IPv4 only version. */
482 bgp_socket (unsigned short port, const char *address)
484 int sock;
485 int socklen;
486 struct sockaddr_in sin;
487 int ret, en;
489 sock = socket (AF_INET, SOCK_STREAM, 0);
490 if (sock < 0)
492 zlog_err ("socket: %s", safe_strerror (errno));
493 return sock;
496 memset (&sin, 0, sizeof (struct sockaddr_in));
497 sin.sin_family = AF_INET;
498 sin.sin_port = htons (port);
499 socklen = sizeof (struct sockaddr_in);
501 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
503 zlog_err("bgp_socket: could not parse ip address %s: %s",
504 address, safe_strerror (errno));
505 return ret;
507 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
508 sin.sin_len = socklen;
509 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
511 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
512 if (ret < 0)
514 close (sock);
515 return ret;
517 return sock;
519 #endif /* HAVE_IPV6 && !NRL */
521 void
522 bgp_close (void)
524 struct listnode *node, *next;
525 struct bgp_listener *listener;
527 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
529 thread_cancel (listener->thread);
530 close (listener->fd);
531 listnode_delete (bm->listen_sockets, listener);
532 XFREE (MTYPE_BGP_LISTENER, listener);