#include "queue.h"
#include "hash.h"
#include "filter.h"
+#include "ns.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_open.h"
int fd;
union sockunion su;
struct thread *thread;
+ struct bgp *bgp;
};
/*
return -1;
}
listener->thread = NULL;
+
thread_add_read(bm->master, bgp_accept, listener, accept_sock,
&listener->thread);
}
set_nonblocking(bgp_sock);
- /* Obtain BGP instance this connection is meant for. */
- if (bgp_get_instance_for_inc_conn(bgp_sock, &bgp)) {
+ /* Obtain BGP instance this connection is meant for.
+ * - if it is a VRF netns sock, then BGP is in listener structure
+ * - otherwise, the bgp instance need to be demultiplexed
+ */
+ if (listener->bgp)
+ bgp = listener->bgp;
+ else if (bgp_get_instance_for_inc_conn(bgp_sock, &bgp)) {
if (bgp_debug_neighbor_events(NULL))
zlog_debug(
"[Event] Could not get instance for incoming conn from %s",
int myerrno;
char *name = NULL;
- /* If not bound to an interface or part of a VRF, we don't care. */
- if (!peer->bgp->vrf_id && !peer->ifname && !peer->conf_if)
+ /* If not bound to an interface or part of a VRF lite, we don't care. */
+ if ((peer->bgp->vrf_id == VRF_DEFAULT) &&
+ !peer->ifname && !peer->conf_if)
+ return 0;
+ if (vrf_is_mapped_on_netns(peer->bgp->vrf_id))
return 0;
-
if (peer->su.sa.sa_family != AF_INET
&& peer->su.sa.sa_family != AF_INET6)
return 0; // unexpected
zlog_debug("Peer address not learnt: Returning from connect");
return 0;
}
+ if (bgpd_privs.change(ZPRIVS_RAISE))
+ zlog_err("Can't raise privileges");
/* Make socket for the peer. */
- peer->fd = sockunion_socket(&peer->su);
+ peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id);
+ if (bgpd_privs.change(ZPRIVS_LOWER))
+ zlog_err("Can't lower privileges");
if (peer->fd < 0)
return -1;
return -1;
#endif
}
-
return 0;
}
-static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen)
+static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
+ struct bgp *bgp)
{
struct bgp_listener *listener;
int ret, en;
return ret;
}
- listener = XMALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
+ listener = XCALLOC(MTYPE_BGP_LISTENER, sizeof(*listener));
listener->fd = sock;
+
+ /* this socket needs a change of ns. record bgp back pointer */
+ if (bgp->vrf_id != VRF_DEFAULT &&
+ vrf_is_mapped_on_netns(bgp->vrf_id))
+ listener->bgp = bgp;
+
memcpy(&listener->su, sa, salen);
listener->thread = NULL;
thread_add_read(bm->master, bgp_accept, listener, sock,
}
/* IPv6 supported version of BGP server socket setup. */
-int bgp_socket(unsigned short port, const char *address)
+int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
{
struct addrinfo *ainfo;
struct addrinfo *ainfo_save;
snprintf(port_str, sizeof(port_str), "%d", port);
port_str[sizeof(port_str) - 1] = '\0';
- ret = getaddrinfo(address, port_str, &req, &ainfo_save);
+ if (bgpd_privs.change(ZPRIVS_RAISE))
+ zlog_err("Can't raise privileges");
+ ret = vrf_getaddrinfo(address, port_str, &req,
+ &ainfo_save, bgp->vrf_id);
+ if (bgpd_privs.change(ZPRIVS_LOWER))
+ zlog_err("Can't lower privileges");
if (ret != 0) {
zlog_err("getaddrinfo: %s", gai_strerror(ret));
return -1;
if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
continue;
- sock = socket(ainfo->ai_family, ainfo->ai_socktype,
- ainfo->ai_protocol);
+ if (bgpd_privs.change(ZPRIVS_RAISE))
+ zlog_err("Can't raise privileges");
+ sock = vrf_socket(ainfo->ai_family, ainfo->ai_socktype,
+ ainfo->ai_protocol, bgp->vrf_id);
+ if (bgpd_privs.change(ZPRIVS_LOWER))
+ zlog_err("Can't lower privileges");
if (sock < 0) {
zlog_err("socket: %s", safe_strerror(errno));
continue;
* ttl=255 */
sockopt_ttl(ainfo->ai_family, sock, MAXTTL);
- ret = bgp_listener(sock, ainfo->ai_addr, ainfo->ai_addrlen);
+ ret = bgp_listener(sock, ainfo->ai_addr,
+ ainfo->ai_addrlen, bgp);
if (ret == 0)
++count;
else
/* Create BGP server socket, if first instance. */
if (list_isempty(bm->bgp) && !bgp_option_check(BGP_OPT_NO_LISTEN)) {
- if (bgp_socket(bm->port, bm->address) < 0)
+ if (bgp_socket(bgp, bm->port, bm->address) < 0)
return BGP_ERR_INVALID_VALUE;
}
struct listnode *bgpnode, *nbgpnode;
for (ALL_LIST_ELEMENTS(bm->bgp, bgpnode, nbgpnode, bgp)) {
- /* Skip VRFs, this function will not be invoked without
- * an instance
+ /* Skip VRFs Lite only, this function will not be
+ * invoked without an instance
* when examining VRFs.
*/
- if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
+ if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF) &&
+ !vrf_is_mapped_on_netns(bgp->vrf_id))
continue;
peer = hash_lookup(bgp->peerhash, &tmp_peer);