build: Add QuaggaId to README.NetBSD
[jleu-quagga.git] / bgpd / bgp_network.c
blob9e3427d2a4ee821da36be2b42a7bef2301fdc0eb
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 ret = 0;
101 struct bgp_listener *listener;
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, listener))
113 if (listener->su.sa.sa_family == peer->su.sa.sa_family)
115 ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password);
116 break;
119 if (bgpd_privs.change (ZPRIVS_LOWER) )
120 zlog_err ("%s: could not lower privs", __func__);
122 return ret;
125 /* Accept bgp connection. */
126 static int
127 bgp_accept (struct thread *thread)
129 int bgp_sock;
130 int accept_sock;
131 union sockunion su;
132 struct bgp_listener *listener = THREAD_ARG(thread);
133 struct peer *peer;
134 struct peer *peer1;
135 char buf[SU_ADDRSTRLEN];
137 /* Register accept thread. */
138 accept_sock = THREAD_FD (thread);
139 if (accept_sock < 0)
141 zlog_err ("accept_sock is nevative value %d", accept_sock);
142 return -1;
144 listener->thread = thread_add_read (master, bgp_accept, listener, accept_sock);
146 /* Accept client connection. */
147 bgp_sock = sockunion_accept (accept_sock, &su);
148 if (bgp_sock < 0)
150 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
151 return -1;
154 if (BGP_DEBUG (events, EVENTS))
155 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
157 /* Check remote IP address */
158 peer1 = peer_lookup (NULL, &su);
159 if (! peer1 || peer1->status == Idle)
161 if (BGP_DEBUG (events, EVENTS))
163 if (! peer1)
164 zlog_debug ("[Event] BGP connection IP address %s is not configured",
165 inet_sutop (&su, buf));
166 else
167 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
168 inet_sutop (&su, buf));
170 close (bgp_sock);
171 return -1;
174 /* In case of peer is EBGP, we should set TTL for this connection. */
175 if (peer_sort (peer1) == BGP_PEER_EBGP)
176 sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
178 /* Make dummy peer until read Open packet. */
179 if (BGP_DEBUG (events, EVENTS))
180 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
183 char buf[SU_ADDRSTRLEN + 1];
185 peer = peer_create_accept (peer1->bgp);
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;
191 peer->v_holdtime = peer1->v_holdtime;
192 peer->v_keepalive = peer1->v_keepalive;
194 /* Make peer's address string. */
195 sockunion2str (&su, buf, SU_ADDRSTRLEN);
196 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
199 BGP_EVENT_ADD (peer, TCP_connection_open);
201 return 0;
204 /* BGP socket bind. */
205 static int
206 bgp_bind (struct peer *peer)
208 #ifdef SO_BINDTODEVICE
209 int ret;
210 struct ifreq ifreq;
212 if (! peer->ifname)
213 return 0;
215 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
217 if ( bgpd_privs.change (ZPRIVS_RAISE) )
218 zlog_err ("bgp_bind: could not raise privs");
220 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
221 &ifreq, sizeof (ifreq));
223 if (bgpd_privs.change (ZPRIVS_LOWER) )
224 zlog_err ("bgp_bind: could not lower privs");
226 if (ret < 0)
228 zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
229 return ret;
231 #endif /* SO_BINDTODEVICE */
232 return 0;
235 static int
236 bgp_bind_address (int sock, struct in_addr *addr)
238 int ret;
239 struct sockaddr_in local;
241 memset (&local, 0, sizeof (struct sockaddr_in));
242 local.sin_family = AF_INET;
243 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
244 local.sin_len = sizeof(struct sockaddr_in);
245 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
246 memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
248 if ( bgpd_privs.change (ZPRIVS_RAISE) )
249 zlog_err ("bgp_bind_address: could not raise privs");
251 ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
252 if (ret < 0)
255 if (bgpd_privs.change (ZPRIVS_LOWER) )
256 zlog_err ("bgp_bind_address: could not lower privs");
258 return 0;
261 static struct in_addr *
262 bgp_update_address (struct interface *ifp)
264 struct prefix_ipv4 *p;
265 struct connected *connected;
266 struct listnode *node;
268 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
270 p = (struct prefix_ipv4 *) connected->address;
272 if (p->family == AF_INET)
273 return &p->prefix;
275 return NULL;
278 /* Update source selection. */
279 static void
280 bgp_update_source (struct peer *peer)
282 struct interface *ifp;
283 struct in_addr *addr;
285 /* Source is specified with interface name. */
286 if (peer->update_if)
288 ifp = if_lookup_by_name (peer->update_if);
289 if (! ifp)
290 return;
292 addr = bgp_update_address (ifp);
293 if (! addr)
294 return;
296 bgp_bind_address (peer->fd, addr);
299 /* Source is specified with IP address. */
300 if (peer->update_source)
301 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
304 /* BGP try to connect to the peer. */
306 bgp_connect (struct peer *peer)
308 unsigned int ifindex = 0;
310 /* Make socket for the peer. */
311 peer->fd = sockunion_socket (&peer->su);
312 if (peer->fd < 0)
313 return -1;
315 /* If we can get socket for the peer, adjest TTL and make connection. */
316 if (peer_sort (peer) == BGP_PEER_EBGP)
317 sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
319 sockopt_reuseaddr (peer->fd);
320 sockopt_reuseport (peer->fd);
322 #ifdef IPTOS_PREC_INTERNETCONTROL
323 if (sockunion_family (&peer->su) == AF_INET)
324 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
325 #endif
327 if (peer->password)
328 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
330 /* Bind socket. */
331 bgp_bind (peer);
333 /* Update source bind. */
334 bgp_update_source (peer);
336 #ifdef HAVE_IPV6
337 if (peer->ifname)
338 ifindex = if_nametoindex (peer->ifname);
339 #endif /* HAVE_IPV6 */
341 if (BGP_DEBUG (events, EVENTS))
342 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
343 peer->host, peer->host, peer->fd);
345 /* Connect to the remote peer. */
346 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
349 /* After TCP connection is established. Get local address and port. */
350 void
351 bgp_getsockname (struct peer *peer)
353 if (peer->su_local)
355 sockunion_free (peer->su_local);
356 peer->su_local = NULL;
359 if (peer->su_remote)
361 sockunion_free (peer->su_remote);
362 peer->su_remote = NULL;
365 peer->su_local = sockunion_getsockname (peer->fd);
366 peer->su_remote = sockunion_getpeername (peer->fd);
368 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
372 static int
373 bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
375 struct bgp_listener *listener;
376 int ret, en;
378 sockopt_reuseaddr (sock);
379 sockopt_reuseport (sock);
381 #ifdef IPTOS_PREC_INTERNETCONTROL
382 if (sa->sa_family == AF_INET)
383 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
384 #endif
386 #ifdef IPV6_V6ONLY
387 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
388 if (sa->sa_family == AF_INET6) {
389 int on = 1;
390 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
391 (void *) &on, sizeof (on));
393 #endif
395 if (bgpd_privs.change (ZPRIVS_RAISE) )
396 zlog_err ("bgp_socket: could not raise privs");
398 ret = bind (sock, sa, salen);
399 en = errno;
400 if (bgpd_privs.change (ZPRIVS_LOWER) )
401 zlog_err ("bgp_bind_address: could not lower privs");
403 if (ret < 0)
405 zlog_err ("bind: %s", safe_strerror (en));
406 return ret;
409 ret = listen (sock, 3);
410 if (ret < 0)
412 zlog_err ("listen: %s", safe_strerror (errno));
413 return ret;
416 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
417 listener->fd = sock;
418 memcpy(&listener->su, sa, salen);
419 listener->thread = thread_add_read (master, bgp_accept, listener, sock);
420 listnode_add (bm->listen_sockets, listener);
422 return 0;
425 /* IPv6 supported version of BGP server socket setup. */
426 #if defined (HAVE_IPV6) && ! defined (NRL)
428 bgp_socket (unsigned short port, const char *address)
430 struct addrinfo *ainfo;
431 struct addrinfo *ainfo_save;
432 static const struct addrinfo req = {
433 .ai_family = AF_UNSPEC,
434 .ai_flags = AI_PASSIVE,
435 .ai_socktype = SOCK_STREAM,
437 int ret, count;
438 char port_str[BUFSIZ];
440 snprintf (port_str, sizeof(port_str), "%d", port);
441 port_str[sizeof (port_str) - 1] = '\0';
443 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
444 if (ret != 0)
446 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
447 return -1;
450 count = 0;
451 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
453 int sock;
455 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
456 continue;
458 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
459 if (sock < 0)
461 zlog_err ("socket: %s", safe_strerror (errno));
462 continue;
465 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
466 if (ret == 0)
467 ++count;
468 else
469 close(sock);
471 freeaddrinfo (ainfo_save);
472 if (count == 0)
474 zlog_err ("%s: no usable addresses", __func__);
475 return -1;
478 return 0;
480 #else
481 /* Traditional IPv4 only version. */
483 bgp_socket (unsigned short port, const char *address)
485 int sock;
486 int socklen;
487 struct sockaddr_in sin;
488 int ret, en;
490 sock = socket (AF_INET, SOCK_STREAM, 0);
491 if (sock < 0)
493 zlog_err ("socket: %s", safe_strerror (errno));
494 return sock;
497 memset (&sin, 0, sizeof (struct sockaddr_in));
498 sin.sin_family = AF_INET;
499 sin.sin_port = htons (port);
500 socklen = sizeof (struct sockaddr_in);
502 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
504 zlog_err("bgp_socket: could not parse ip address %s: %s",
505 address, safe_strerror (errno));
506 return ret;
508 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
509 sin.sin_len = socklen;
510 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
512 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
513 if (ret < 0)
515 close (sock);
516 return ret;
518 return sock;
520 #endif /* HAVE_IPV6 && !NRL */
522 void
523 bgp_close (void)
525 struct listnode *node, *next;
526 struct bgp_listener *listener;
528 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
530 thread_cancel (listener->thread);
531 close (listener->fd);
532 listnode_delete (bm->listen_sockets, listener);
533 XFREE (MTYPE_BGP_LISTENER, listener);