blob: ade0fbc695842190adf32eb03a918d0ccc9bcc4f [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP network related fucntions
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "thread.h"
24#include "sockunion.h"
Paul Jakma0df7c912008-07-21 21:02:49 +000025#include "sockopt.h"
paul718e3742002-12-13 20:15:29 +000026#include "memory.h"
27#include "log.h"
28#include "if.h"
29#include "prefix.h"
30#include "command.h"
pauledd7c242003-06-04 13:59:38 +000031#include "privs.h"
Paul Jakma0df7c912008-07-21 21:02:49 +000032#include "linklist.h"
paul718e3742002-12-13 20:15:29 +000033
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"
pauledd7c242003-06-04 13:59:38 +000039
40extern struct zebra_privs_t bgpd_privs;
41
Stephen Hemmingerd023aec2009-07-21 16:27:21 -070042/* BGP listening socket. */
43struct bgp_listener
44{
45 int fd;
46 union sockunion su;
47 struct thread *thread;
48};
paul718e3742002-12-13 20:15:29 +000049
Paul Jakma0df7c912008-07-21 21:02:49 +000050/*
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 */
54static int
55bgp_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 */
75static int
76bgp_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
96int
97bgp_md5_set (struct peer *peer)
98{
99 struct listnode *node;
100 int fret = 0, ret;
101 int *socket;
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 */
112 for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket))
113 {
Chris Caputo54a15182009-07-18 05:42:34 +0000114 ret = bgp_md5_set_socket ((int)(long)socket, &peer->su, peer->password);
Paul Jakma0df7c912008-07-21 21:02:49 +0000115 if (ret < 0)
116 fret = ret;
117 }
118 if (bgpd_privs.change (ZPRIVS_LOWER) )
119 zlog_err ("%s: could not lower privs", __func__);
120
121 return fret;
122}
123
paul718e3742002-12-13 20:15:29 +0000124/* Accept bgp connection. */
125static int
126bgp_accept (struct thread *thread)
127{
128 int bgp_sock;
129 int accept_sock;
130 union sockunion su;
131 struct peer *peer;
pauleb821182004-05-01 08:44:08 +0000132 struct peer *peer1;
paul718e3742002-12-13 20:15:29 +0000133 struct bgp *bgp;
134 char buf[SU_ADDRSTRLEN];
135
136 /* Regiser accept thread. */
137 accept_sock = THREAD_FD (thread);
138 bgp = THREAD_ARG (thread);
139
140 if (accept_sock < 0)
141 {
142 zlog_err ("accept_sock is nevative value %d", accept_sock);
143 return -1;
144 }
145 thread_add_read (master, bgp_accept, bgp, accept_sock);
146
147 /* Accept client connection. */
148 bgp_sock = sockunion_accept (accept_sock, &su);
149 if (bgp_sock < 0)
150 {
ajs6099b3b2004-11-20 02:06:59 +0000151 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000152 return -1;
153 }
154
155 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000156 zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
paul718e3742002-12-13 20:15:29 +0000157
158 /* Check remote IP address */
pauleb821182004-05-01 08:44:08 +0000159 peer1 = peer_lookup (bgp, &su);
160 if (! peer1 || peer1->status == Idle)
paul718e3742002-12-13 20:15:29 +0000161 {
162 if (BGP_DEBUG (events, EVENTS))
163 {
pauleb821182004-05-01 08:44:08 +0000164 if (! peer1)
ajs478ba052004-12-08 20:41:23 +0000165 zlog_debug ("[Event] BGP connection IP address %s is not configured",
paul718e3742002-12-13 20:15:29 +0000166 inet_sutop (&su, buf));
167 else
ajs478ba052004-12-08 20:41:23 +0000168 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
paul718e3742002-12-13 20:15:29 +0000169 inet_sutop (&su, buf));
170 }
171 close (bgp_sock);
172 return -1;
173 }
174
175 /* In case of peer is EBGP, we should set TTL for this connection. */
pauleb821182004-05-01 08:44:08 +0000176 if (peer_sort (peer1) == BGP_PEER_EBGP)
177 sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
paul718e3742002-12-13 20:15:29 +0000178
179 if (! bgp)
pauleb821182004-05-01 08:44:08 +0000180 bgp = peer1->bgp;
paul718e3742002-12-13 20:15:29 +0000181
pauleb821182004-05-01 08:44:08 +0000182 /* Make dummy peer until read Open packet. */
183 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000184 zlog_debug ("[Event] Make dummy peer structure until read Open packet");
pauleb821182004-05-01 08:44:08 +0000185
186 {
187 char buf[SU_ADDRSTRLEN + 1];
188
189 peer = peer_create_accept (bgp);
190 SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
191 peer->su = su;
192 peer->fd = bgp_sock;
193 peer->status = Active;
194 peer->local_id = peer1->local_id;
Timo Teräse8eb0002009-02-17 12:14:23 +0200195 peer->v_holdtime = peer1->v_holdtime;
196 peer->v_keepalive = peer1->v_keepalive;
pauleb821182004-05-01 08:44:08 +0000197
198 /* Make peer's address string. */
199 sockunion2str (&su, buf, SU_ADDRSTRLEN);
paule83e2082005-05-19 02:12:25 +0000200 peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
pauleb821182004-05-01 08:44:08 +0000201 }
paul718e3742002-12-13 20:15:29 +0000202
203 BGP_EVENT_ADD (peer, TCP_connection_open);
204
205 return 0;
206}
207
208/* BGP socket bind. */
paul94f2b392005-06-28 12:44:16 +0000209static int
paul718e3742002-12-13 20:15:29 +0000210bgp_bind (struct peer *peer)
211{
212#ifdef SO_BINDTODEVICE
213 int ret;
214 struct ifreq ifreq;
215
216 if (! peer->ifname)
217 return 0;
218
219 strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
220
paul98f51632004-10-25 14:19:15 +0000221 if ( bgpd_privs.change (ZPRIVS_RAISE) )
222 zlog_err ("bgp_bind: could not raise privs");
223
pauleb821182004-05-01 08:44:08 +0000224 ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
paul718e3742002-12-13 20:15:29 +0000225 &ifreq, sizeof (ifreq));
paul98f51632004-10-25 14:19:15 +0000226
227 if (bgpd_privs.change (ZPRIVS_LOWER) )
228 zlog_err ("bgp_bind: could not lower privs");
229
paul718e3742002-12-13 20:15:29 +0000230 if (ret < 0)
231 {
232 zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
233 return ret;
234 }
235#endif /* SO_BINDTODEVICE */
236 return 0;
237}
238
paul94f2b392005-06-28 12:44:16 +0000239static int
paul718e3742002-12-13 20:15:29 +0000240bgp_bind_address (int sock, struct in_addr *addr)
241{
242 int ret;
243 struct sockaddr_in local;
244
245 memset (&local, 0, sizeof (struct sockaddr_in));
246 local.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000247#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000248 local.sin_len = sizeof(struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000249#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000250 memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
251
pauledd7c242003-06-04 13:59:38 +0000252 if ( bgpd_privs.change (ZPRIVS_RAISE) )
253 zlog_err ("bgp_bind_address: could not raise privs");
254
paul718e3742002-12-13 20:15:29 +0000255 ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
256 if (ret < 0)
257 ;
pauledd7c242003-06-04 13:59:38 +0000258
259 if (bgpd_privs.change (ZPRIVS_LOWER) )
260 zlog_err ("bgp_bind_address: could not lower privs");
261
paul718e3742002-12-13 20:15:29 +0000262 return 0;
263}
264
paul94f2b392005-06-28 12:44:16 +0000265static struct in_addr *
paul718e3742002-12-13 20:15:29 +0000266bgp_update_address (struct interface *ifp)
267{
268 struct prefix_ipv4 *p;
269 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000270 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000271
paul1eb8ef22005-04-07 07:30:20 +0000272 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
paul718e3742002-12-13 20:15:29 +0000273 {
paul718e3742002-12-13 20:15:29 +0000274 p = (struct prefix_ipv4 *) connected->address;
275
276 if (p->family == AF_INET)
277 return &p->prefix;
278 }
279 return NULL;
280}
281
282/* Update source selection. */
paul94f2b392005-06-28 12:44:16 +0000283static void
paul718e3742002-12-13 20:15:29 +0000284bgp_update_source (struct peer *peer)
285{
286 struct interface *ifp;
287 struct in_addr *addr;
288
289 /* Source is specified with interface name. */
290 if (peer->update_if)
291 {
292 ifp = if_lookup_by_name (peer->update_if);
293 if (! ifp)
294 return;
295
296 addr = bgp_update_address (ifp);
297 if (! addr)
298 return;
299
pauleb821182004-05-01 08:44:08 +0000300 bgp_bind_address (peer->fd, addr);
paul718e3742002-12-13 20:15:29 +0000301 }
302
303 /* Source is specified with IP address. */
304 if (peer->update_source)
pauleb821182004-05-01 08:44:08 +0000305 sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
paul718e3742002-12-13 20:15:29 +0000306}
307
308/* BGP try to connect to the peer. */
309int
310bgp_connect (struct peer *peer)
311{
312 unsigned int ifindex = 0;
313
314 /* Make socket for the peer. */
pauleb821182004-05-01 08:44:08 +0000315 peer->fd = sockunion_socket (&peer->su);
316 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000317 return -1;
318
319 /* If we can get socket for the peer, adjest TTL and make connection. */
320 if (peer_sort (peer) == BGP_PEER_EBGP)
pauleb821182004-05-01 08:44:08 +0000321 sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
paul718e3742002-12-13 20:15:29 +0000322
pauleb821182004-05-01 08:44:08 +0000323 sockopt_reuseaddr (peer->fd);
324 sockopt_reuseport (peer->fd);
Paul Jakma0df7c912008-07-21 21:02:49 +0000325
Stephen Hemminger1423c802008-08-14 17:59:25 +0100326#ifdef IPTOS_PREC_INTERNETCONTROL
327 if (sockunion_family (&peer->su) == AF_INET)
328 setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
329#endif
330
Paul Jakma0df7c912008-07-21 21:02:49 +0000331 if (peer->password)
332 bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
paul718e3742002-12-13 20:15:29 +0000333
334 /* Bind socket. */
335 bgp_bind (peer);
336
337 /* Update source bind. */
338 bgp_update_source (peer);
339
340#ifdef HAVE_IPV6
341 if (peer->ifname)
342 ifindex = if_nametoindex (peer->ifname);
343#endif /* HAVE_IPV6 */
344
345 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000346 plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
pauleb821182004-05-01 08:44:08 +0000347 peer->host, peer->host, peer->fd);
paul718e3742002-12-13 20:15:29 +0000348
349 /* Connect to the remote peer. */
pauleb821182004-05-01 08:44:08 +0000350 return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
paul718e3742002-12-13 20:15:29 +0000351}
352
353/* After TCP connection is established. Get local address and port. */
354void
355bgp_getsockname (struct peer *peer)
356{
357 if (peer->su_local)
358 {
paul22db9de2005-05-19 01:50:11 +0000359 sockunion_free (peer->su_local);
paul718e3742002-12-13 20:15:29 +0000360 peer->su_local = NULL;
361 }
362
363 if (peer->su_remote)
364 {
paul22db9de2005-05-19 01:50:11 +0000365 sockunion_free (peer->su_remote);
paul718e3742002-12-13 20:15:29 +0000366 peer->su_remote = NULL;
367 }
368
pauleb821182004-05-01 08:44:08 +0000369 peer->su_local = sockunion_getsockname (peer->fd);
370 peer->su_remote = sockunion_getpeername (peer->fd);
paul718e3742002-12-13 20:15:29 +0000371
372 bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
373}
374
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700375
376static int
377bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
378{
379 struct bgp_listener *listener;
380 int ret, en;
381
382 sockopt_reuseaddr (sock);
383 sockopt_reuseport (sock);
384
385#ifdef IPTOS_PREC_INTERNETCONTROL
386 if (sa->sa_family == AF_INET)
387 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
388#endif
389
390#ifdef IPV6_V6ONLY
391 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
392 if (sa->sa_family == AF_INET6) {
393 int on = 1;
394 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
395 (void *) &on, sizeof (on));
396 }
397#endif
398
399 if (bgpd_privs.change (ZPRIVS_RAISE) )
400 zlog_err ("bgp_socket: could not raise privs");
401
402 ret = bind (sock, sa, salen);
403 en = errno;
404 if (bgpd_privs.change (ZPRIVS_LOWER) )
405 zlog_err ("bgp_bind_address: could not lower privs");
406
407 if (ret < 0)
408 {
409 zlog_err ("bind: %s", safe_strerror (en));
410 return ret;
411 }
412
413 ret = listen (sock, 3);
414 if (ret < 0)
415 {
416 zlog_err ("listen: %s", safe_strerror (errno));
417 return ret;
418 }
419
420 listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
421 listener->fd = sock;
422 memcpy(&listener->su, sa, salen);
423 listener->thread = thread_add_read (master, bgp_accept, listener, sock);
424 listnode_add (bm->listen_sockets, listener);
425
426 return 0;
427}
428
paul718e3742002-12-13 20:15:29 +0000429/* IPv6 supported version of BGP server socket setup. */
430#if defined (HAVE_IPV6) && ! defined (NRL)
431int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700432bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000433{
paul718e3742002-12-13 20:15:29 +0000434 struct addrinfo *ainfo;
435 struct addrinfo *ainfo_save;
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700436 static const struct addrinfo req = {
437 .ai_family = AF_UNSPEC,
438 .ai_flags = AI_PASSIVE,
439 .ai_socktype = SOCK_STREAM,
440 };
441 int ret, count;
paul718e3742002-12-13 20:15:29 +0000442 char port_str[BUFSIZ];
443
Paul Jakma90b68762008-01-29 17:26:34 +0000444 snprintf (port_str, sizeof(port_str), "%d", port);
paul718e3742002-12-13 20:15:29 +0000445 port_str[sizeof (port_str) - 1] = '\0';
446
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700447 ret = getaddrinfo (address, port_str, &req, &ainfo_save);
paul718e3742002-12-13 20:15:29 +0000448 if (ret != 0)
449 {
450 zlog_err ("getaddrinfo: %s", gai_strerror (ret));
451 return -1;
452 }
453
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700454 count = 0;
455 for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
paul718e3742002-12-13 20:15:29 +0000456 {
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700457 int sock;
458
paul718e3742002-12-13 20:15:29 +0000459 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
460 continue;
461
462 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
463 if (sock < 0)
464 {
ajs6099b3b2004-11-20 02:06:59 +0000465 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000466 continue;
467 }
468
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700469 ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
470 if (ret == 0)
471 ++count;
472 else
473 close(sock);
paul718e3742002-12-13 20:15:29 +0000474 }
paul718e3742002-12-13 20:15:29 +0000475 freeaddrinfo (ainfo_save);
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700476 if (count == 0)
477 {
478 zlog_err ("%s: no usable addresses", __func__);
479 return -1;
480 }
paul718e3742002-12-13 20:15:29 +0000481
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700482 return 0;
paul718e3742002-12-13 20:15:29 +0000483}
484#else
485/* Traditional IPv4 only version. */
486int
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700487bgp_socket (unsigned short port, const char *address)
paul718e3742002-12-13 20:15:29 +0000488{
489 int sock;
490 int socklen;
491 struct sockaddr_in sin;
hasso4a1a2712004-02-12 15:41:38 +0000492 int ret, en;
paul718e3742002-12-13 20:15:29 +0000493
494 sock = socket (AF_INET, SOCK_STREAM, 0);
495 if (sock < 0)
496 {
ajs6099b3b2004-11-20 02:06:59 +0000497 zlog_err ("socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000498 return sock;
499 }
500
paul718e3742002-12-13 20:15:29 +0000501 memset (&sin, 0, sizeof (struct sockaddr_in));
paul718e3742002-12-13 20:15:29 +0000502 sin.sin_family = AF_INET;
503 sin.sin_port = htons (port);
504 socklen = sizeof (struct sockaddr_in);
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000505
Paul Jakma90b68762008-01-29 17:26:34 +0000506 if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000507 {
Paul Jakma90b68762008-01-29 17:26:34 +0000508 zlog_err("bgp_socket: could not parse ip address %s: %s",
509 address, safe_strerror (errno));
Paul Jakma3a02d1f2007-11-01 14:29:11 +0000510 return ret;
511 }
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000512#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000513 sin.sin_len = socklen;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000514#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000515
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700516 ret = bgp_listener (sock, (struct sockaddr *) &sin, socklen);
paul718e3742002-12-13 20:15:29 +0000517 if (ret < 0)
518 {
paul718e3742002-12-13 20:15:29 +0000519 close (sock);
520 return ret;
521 }
paul718e3742002-12-13 20:15:29 +0000522 return sock;
523}
524#endif /* HAVE_IPV6 && !NRL */
Stephen Hemmingerd023aec2009-07-21 16:27:21 -0700525
526void
527bgp_close (void)
528{
529 struct listnode *node, *next;
530 struct bgp_listener *listener;
531
532 for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
533 {
534 thread_cancel (listener->thread);
535 close (listener->fd);
536 listnode_delete (bm->listen_sockets, listener);
537 XFREE (MTYPE_BGP_LISTENER, listener);
538 }
539}