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
;
101 struct bgp_listener
*listener
;
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
, 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
);
119 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
120 zlog_err ("%s: could not lower privs", __func__
);
125 /* Accept bgp connection. */
127 bgp_accept (struct thread
*thread
)
132 struct bgp_listener
*listener
= THREAD_ARG(thread
);
135 char buf
[SU_ADDRSTRLEN
];
137 /* Register accept thread. */
138 accept_sock
= THREAD_FD (thread
);
141 zlog_err ("accept_sock is nevative value %d", accept_sock
);
144 listener
->thread
= thread_add_read (master
, bgp_accept
, listener
, accept_sock
);
146 /* Accept client connection. */
147 bgp_sock
= sockunion_accept (accept_sock
, &su
);
150 zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno
));
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
))
164 zlog_debug ("[Event] BGP connection IP address %s is not configured",
165 inet_sutop (&su
, buf
));
167 zlog_debug ("[Event] BGP connection IP address %s is Idle state",
168 inet_sutop (&su
, buf
));
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
);
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
);
204 /* BGP socket bind. */
206 bgp_bind (struct peer
*peer
)
208 #ifdef SO_BINDTODEVICE
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");
228 zlog (peer
->log
, LOG_INFO
, "bind to interface %s failed", peer
->ifname
);
231 #endif /* SO_BINDTODEVICE */
236 bgp_bind_address (int sock
, struct in_addr
*addr
)
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
));
255 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
256 zlog_err ("bgp_bind_address: could not lower privs");
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
)
278 /* Update source selection. */
280 bgp_update_source (struct peer
*peer
)
282 struct interface
*ifp
;
283 struct in_addr
*addr
;
285 /* Source is specified with interface name. */
288 ifp
= if_lookup_by_name (peer
->update_if
);
292 addr
= bgp_update_address (ifp
);
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
);
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
);
328 bgp_md5_set_connect (peer
->fd
, &peer
->su
, peer
->password
);
333 /* Update source bind. */
334 bgp_update_source (peer
);
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. */
351 bgp_getsockname (struct peer
*peer
)
355 sockunion_free (peer
->su_local
);
356 peer
->su_local
= NULL
;
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
);
373 bgp_listener (int sock
, struct sockaddr
*sa
, socklen_t salen
)
375 struct bgp_listener
*listener
;
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
);
387 /* Want only IPV6 on ipv6 socket (not mapped addresses) */
388 if (sa
->sa_family
== AF_INET6
) {
390 setsockopt (sock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
391 (void *) &on
, sizeof (on
));
395 if (bgpd_privs
.change (ZPRIVS_RAISE
) )
396 zlog_err ("bgp_socket: could not raise privs");
398 ret
= bind (sock
, sa
, salen
);
400 if (bgpd_privs
.change (ZPRIVS_LOWER
) )
401 zlog_err ("bgp_bind_address: could not lower privs");
405 zlog_err ("bind: %s", safe_strerror (en
));
409 ret
= listen (sock
, 3);
412 zlog_err ("listen: %s", safe_strerror (errno
));
416 listener
= XMALLOC (MTYPE_BGP_LISTENER
, sizeof(*listener
));
418 memcpy(&listener
->su
, sa
, salen
);
419 listener
->thread
= thread_add_read (master
, bgp_accept
, listener
, sock
);
420 listnode_add (bm
->listen_sockets
, listener
);
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
,
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
);
446 zlog_err ("getaddrinfo: %s", gai_strerror (ret
));
451 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
)
455 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
458 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
461 zlog_err ("socket: %s", safe_strerror (errno
));
465 ret
= bgp_listener (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
471 freeaddrinfo (ainfo_save
);
474 zlog_err ("%s: no usable addresses", __func__
);
481 /* Traditional IPv4 only version. */
483 bgp_socket (unsigned short port
, const char *address
)
487 struct sockaddr_in sin
;
490 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
493 zlog_err ("socket: %s", safe_strerror (errno
));
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
));
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
);
520 #endif /* HAVE_IPV6 && !NRL */
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
);