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
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
24 #include "sockunion.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. */
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.
55 bgp_md5_set_socket (int socket
, union sockunion
*su
, const char *password
)
62 #if HAVE_DECL_TCP_MD5SIG
63 ret
= sockopt_tcp_signature (socket
, su
, password
);
65 #endif /* HAVE_TCP_MD5SIG */
68 zlog (NULL
, LOG_WARNING
, "can't set TCP_MD5SIG option on socket %d: %s",
69 socket
, safe_strerror (en
));
74 /* Helper for bgp_connect */
76 bgp_md5_set_connect (int socket
, union sockunion
*su
, const char *password
)
80 #if HAVE_DECL_TCP_MD5SIG
81 if ( bgpd_privs
.change (ZPRIVS_RAISE
) )
83 zlog_err ("%s: could not raise privs", __func__
);
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 */
97 bgp_md5_set (struct peer
*peer
)
99 struct listnode
*node
;
103 if ( bgpd_privs
.change (ZPRIVS_RAISE
) )
105 zlog_err ("%s: could not raise privs", __func__
);
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
);
118 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
119 zlog_err ("%s: could not lower privs", __func__
);
124 /* Accept bgp connection. */
126 bgp_accept (struct thread
*thread
)
131 struct bgp_listener
*listener
= THREAD_ARG(thread
);
134 char buf
[SU_ADDRSTRLEN
];
136 /* Register accept thread. */
137 accept_sock
= THREAD_FD (thread
);
140 zlog_err ("accept_sock is nevative value %d", accept_sock
);
143 listener
->thread
= thread_add_read (master
, bgp_accept
, listener
, accept_sock
);
145 /* Accept client connection. */
146 bgp_sock
= sockunion_accept (accept_sock
, &su
);
149 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno
));
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
))
163 zlog_debug ("[Event] BGP connection IP address %s is not configured",
164 inet_sutop (&su
, buf
));
166 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
167 inet_sutop (&su
, buf
));
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
);
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
);
203 /* BGP socket bind. */
205 bgp_bind (struct peer
*peer
)
207 #ifdef SO_BINDTODEVICE
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");
227 zlog (peer
->log
, LOG_INFO
, "bind to interface %s failed", peer
->ifname
);
230 #endif /* SO_BINDTODEVICE */
235 bgp_bind_address (int sock
, struct in_addr
*addr
)
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
));
254 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
255 zlog_err ("bgp_bind_address: could not lower privs");
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
)
277 /* Update source selection. */
279 bgp_update_source (struct peer
*peer
)
281 struct interface
*ifp
;
282 struct in_addr
*addr
;
284 /* Source is specified with interface name. */
287 ifp
= if_lookup_by_name (peer
->update_if
);
291 addr
= bgp_update_address (ifp
);
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
);
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
);
327 bgp_md5_set_connect (peer
->fd
, &peer
->su
, peer
->password
);
332 /* Update source bind. */
333 bgp_update_source (peer
);
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. */
350 bgp_getsockname (struct peer
*peer
)
354 sockunion_free (peer
->su_local
);
355 peer
->su_local
= NULL
;
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
);
372 bgp_listener (int sock
, struct sockaddr
*sa
, socklen_t salen
)
374 struct bgp_listener
*listener
;
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
);
386 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
387 if (sa
->sa_family
== AF_INET6
) {
389 setsockopt (sock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
390 (void *) &on
, sizeof (on
));
394 if (bgpd_privs
.change (ZPRIVS_RAISE
) )
395 zlog_err ("bgp_socket: could not raise privs");
397 ret
= bind (sock
, sa
, salen
);
399 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
400 zlog_err ("bgp_bind_address: could not lower privs");
404 zlog_err ("bind: %s", safe_strerror (en
));
408 ret
= listen (sock
, 3);
411 zlog_err ("listen: %s", safe_strerror (errno
));
415 listener
= XMALLOC (MTYPE_BGP_LISTENER
, sizeof(*listener
));
417 memcpy(&listener
->su
, sa
, salen
);
418 listener
->thread
= thread_add_read (master
, bgp_accept
, listener
, sock
);
419 listnode_add (bm
->listen_sockets
, listener
);
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
,
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
);
445 zlog_err ("getaddrinfo: %s", gai_strerror (ret
));
450 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
)
454 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
457 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
460 zlog_err ("socket: %s", safe_strerror (errno
));
464 ret
= bgp_listener (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
470 freeaddrinfo (ainfo_save
);
473 zlog_err ("%s: no usable addresses", __func__
);
480 /* Traditional IPv4 only version. */
482 bgp_socket (unsigned short port
, const char *address
)
486 struct sockaddr_in sin
;
489 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
492 zlog_err ("socket: %s", safe_strerror (errno
));
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
));
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
);
519 #endif /* HAVE_IPV6 && !NRL */
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
);