2012-09-14 02:17:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2012 Paul Gardner-Stephen, Serval Project.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "serval.h"
|
2012-12-11 05:29:46 +00:00
|
|
|
#include "conf.h"
|
2013-09-19 07:56:06 +00:00
|
|
|
#include "log.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-09-14 02:17:48 +00:00
|
|
|
#include "strbuf.h"
|
2013-09-19 07:56:06 +00:00
|
|
|
#include "strbuf_helpers.h"
|
2012-09-14 02:17:48 +00:00
|
|
|
#include "overlay_buffer.h"
|
|
|
|
#include "overlay_address.h"
|
|
|
|
#include "overlay_packet.h"
|
|
|
|
#include "mdp_client.h"
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
2012-11-06 19:48:54 +00:00
|
|
|
if (mdp_sockfd == -1)
|
2013-09-19 07:56:06 +00:00
|
|
|
return -1;
|
|
|
|
// Minimise frame length to save work and prevent accidental disclosure of memory contents.
|
|
|
|
int len = overlay_mdp_relevant_bytes(mdp);
|
|
|
|
if (len < 0)
|
|
|
|
return WHY("MDP frame invalid (could not compute length)");
|
2012-09-14 02:17:48 +00:00
|
|
|
/* Construct name of socket to send to. */
|
2013-09-19 07:56:06 +00:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
2013-09-20 04:37:19 +00:00
|
|
|
if (make_local_sockaddr(&addr, &addrlen, "mdp.socket") == -1)
|
2012-09-14 02:17:48 +00:00
|
|
|
return -1;
|
2013-09-19 07:56:06 +00:00
|
|
|
// Send to that socket
|
2012-11-06 19:48:54 +00:00
|
|
|
set_nonblock(mdp_sockfd);
|
|
|
|
int result = sendto(mdp_sockfd, mdp, len, 0, (struct sockaddr *)&addr, addrlen);
|
|
|
|
set_block(mdp_sockfd);
|
2013-09-19 07:56:06 +00:00
|
|
|
if (result == -1) {
|
2012-09-14 02:17:48 +00:00
|
|
|
mdp->packetTypeAndFlags=MDP_ERROR;
|
|
|
|
mdp->error.error=1;
|
|
|
|
snprintf(mdp->error.message,128,"Error sending frame to MDP server.");
|
|
|
|
return WHY_perror("sendto(f)");
|
|
|
|
} else {
|
|
|
|
if (!(flags&MDP_AWAITREPLY)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
int port=0;
|
|
|
|
if ((mdp->packetTypeAndFlags&MDP_TYPE_MASK) == MDP_TX)
|
2013-02-18 04:01:01 +00:00
|
|
|
port = mdp->out.src.port;
|
2012-12-04 04:17:57 +00:00
|
|
|
|
2012-10-02 06:43:12 +00:00
|
|
|
time_ms_t started = gettime_ms();
|
2012-11-06 19:48:54 +00:00
|
|
|
while(timeout_ms>=0 && overlay_mdp_client_poll(mdp_sockfd, timeout_ms)>0){
|
2012-10-02 06:43:12 +00:00
|
|
|
int ttl=-1;
|
2012-11-06 19:48:54 +00:00
|
|
|
if (!overlay_mdp_recv(mdp_sockfd, mdp, port, &ttl)) {
|
2012-10-02 06:43:12 +00:00
|
|
|
/* If all is well, examine result and return error code provided */
|
|
|
|
if ((mdp->packetTypeAndFlags&MDP_TYPE_MASK)==MDP_ERROR)
|
|
|
|
return mdp->error.error;
|
|
|
|
else
|
|
|
|
/* Something other than an error has been returned */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// work out how much longer we can wait for a valid response
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
timeout_ms -= (now - started);
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
2012-10-02 06:43:12 +00:00
|
|
|
|
|
|
|
/* Timeout */
|
|
|
|
mdp->packetTypeAndFlags=MDP_ERROR;
|
|
|
|
mdp->error.error=1;
|
|
|
|
snprintf(mdp->error.message,128,"Timeout waiting for reply to MDP packet (packet was successfully sent).");
|
|
|
|
return -1; /* WHY("Timeout waiting for server response"); */
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
/** Create a new MDP socket and return its descriptor (-1 on error). */
|
|
|
|
int overlay_mdp_client_socket(void)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
2012-11-06 19:48:54 +00:00
|
|
|
/* Create local per-client socket to MDP server (connection is always local) */
|
|
|
|
int mdp_sockfd;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
uint32_t random_value;
|
|
|
|
if (urandombytes((unsigned char *)&random_value, sizeof random_value) == -1)
|
|
|
|
return WHY("urandombytes() failed");
|
|
|
|
if (make_local_sockaddr(&addr, &addrlen, "mdp.client.%u.%08lx.socket", getpid(), (unsigned long)random_value) == -1)
|
|
|
|
return -1;
|
|
|
|
if ((mdp_sockfd = esocket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
|
|
|
|
return -1;
|
|
|
|
if (socket_bind(mdp_sockfd, (struct sockaddr *)&addr, addrlen) == -1) {
|
|
|
|
close(mdp_sockfd);
|
|
|
|
return -1;
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
2012-11-06 19:48:54 +00:00
|
|
|
socket_set_rcvbufsize(mdp_sockfd, 128 * 1024);
|
|
|
|
return mdp_sockfd;
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_client_close(int mdp_sockfd)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
2012-11-06 19:48:54 +00:00
|
|
|
/* Tell MDP server to release all our bindings */
|
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
mdp.packetTypeAndFlags = MDP_GOODBYE;
|
|
|
|
overlay_mdp_send(mdp_sockfd, &mdp, 0, 0);
|
|
|
|
// get the socket name and unlink it from the filesystem if not abstract
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen = sizeof addr;
|
|
|
|
if (getsockname(mdp_sockfd, (struct sockaddr *)&addr, &addrlen))
|
|
|
|
WHYF_perror("getsockname(%d)", mdp_sockfd);
|
|
|
|
else if (addrlen > sizeof addr.sun_family && addrlen <= sizeof addr && addr.sun_path[0] != '\0') {
|
|
|
|
if (unlink(addr.sun_path) == -1)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(addr.sun_path));
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
2012-11-06 19:48:54 +00:00
|
|
|
close(mdp_sockfd);
|
2013-01-15 05:45:36 +00:00
|
|
|
return 0;
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_client_poll(int mdp_sockfd, time_ms_t timeout_ms)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
|
|
|
fd_set r;
|
|
|
|
int ret;
|
|
|
|
FD_ZERO(&r);
|
2012-11-06 19:48:54 +00:00
|
|
|
FD_SET(mdp_sockfd, &r);
|
2012-09-14 02:17:48 +00:00
|
|
|
if (timeout_ms<0) timeout_ms=0;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (timeout_ms>=0) {
|
|
|
|
tv.tv_sec=timeout_ms/1000;
|
|
|
|
tv.tv_usec=(timeout_ms%1000)*1000;
|
2012-11-06 19:48:54 +00:00
|
|
|
ret=select(mdp_sockfd+1,&r,NULL,&r,&tv);
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
|
|
|
else
|
2012-11-06 19:48:54 +00:00
|
|
|
ret=select(mdp_sockfd+1,&r,NULL,&r,NULL);
|
2012-09-14 02:17:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
2013-09-19 07:56:06 +00:00
|
|
|
/* Construct name of socket to receive from. */
|
|
|
|
struct sockaddr_un mdp_addr;
|
|
|
|
socklen_t mdp_addrlen;
|
2013-09-20 04:37:19 +00:00
|
|
|
if (make_local_sockaddr(&mdp_addr, &mdp_addrlen, "mdp.socket") == -1)
|
2013-09-19 07:56:06 +00:00
|
|
|
return -1;
|
2012-09-14 02:17:48 +00:00
|
|
|
|
|
|
|
/* Check if reply available */
|
2013-09-19 07:56:06 +00:00
|
|
|
struct sockaddr_un recvaddr;
|
|
|
|
socklen_t recvaddrlen = sizeof recvaddr;
|
|
|
|
ssize_t len;
|
|
|
|
mdp->packetTypeAndFlags = 0;
|
2012-11-06 19:48:54 +00:00
|
|
|
set_nonblock(mdp_sockfd);
|
|
|
|
len = recvwithttl(mdp_sockfd, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen);
|
|
|
|
set_block(mdp_sockfd);
|
2013-09-19 07:56:06 +00:00
|
|
|
if (len <= 0)
|
|
|
|
return -1; // no packet received
|
|
|
|
|
|
|
|
// If the received address overflowed the buffer, then it cannot have come from the server, whose
|
|
|
|
// address always fits within a struct sockaddr_un.
|
|
|
|
if (recvaddrlen > sizeof recvaddr)
|
|
|
|
return WHY("reply did not come from server: address overrun");
|
|
|
|
|
2013-09-20 04:37:19 +00:00
|
|
|
// Compare the address of the sender with the address of our server, to ensure they are the same.
|
|
|
|
// If the comparison fails, then try using realpath(3) on the sender address and compare again.
|
|
|
|
if ( cmp_sockaddr((struct sockaddr *)&recvaddr, recvaddrlen, (struct sockaddr *)&mdp_addr, mdp_addrlen) != 0
|
|
|
|
&& ( recvaddr.sun_family != AF_UNIX
|
|
|
|
|| real_sockaddr(&recvaddr, recvaddrlen, &recvaddr, &recvaddrlen) <= 0
|
|
|
|
|| cmp_sockaddr((struct sockaddr *)&recvaddr, recvaddrlen, (struct sockaddr *)&mdp_addr, mdp_addrlen) != 0
|
|
|
|
)
|
|
|
|
)
|
2013-09-19 07:56:06 +00:00
|
|
|
return WHYF("reply did not come from server: %s", alloca_sockaddr(&recvaddr, recvaddrlen));
|
2012-09-14 02:17:48 +00:00
|
|
|
|
2013-09-19 07:56:06 +00:00
|
|
|
// silently drop incoming packets for the wrong port number
|
|
|
|
if (port>0 && port != mdp->in.dst.port){
|
|
|
|
WARNF("Ignoring packet for port %d",mdp->in.dst.port);
|
2012-09-14 02:17:48 +00:00
|
|
|
return -1;
|
2013-09-19 07:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int expected_len = overlay_mdp_relevant_bytes(mdp);
|
|
|
|
if (len < expected_len)
|
2013-10-06 19:24:46 +00:00
|
|
|
return WHYF("Expected packet length of %d, received only %zd bytes", expected_len, len);
|
2012-09-14 02:17:48 +00:00
|
|
|
|
2013-09-19 07:56:06 +00:00
|
|
|
/* Valid packet received */
|
|
|
|
return 0;
|
2012-09-14 02:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a request to servald deamon to add a port binding
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_bind(int mdp_sockfd, const sid_t *localaddr, int port)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
mdp.packetTypeAndFlags=MDP_BIND|MDP_FORCE;
|
2013-02-15 09:04:24 +00:00
|
|
|
bcopy(localaddr->binary, mdp.bind.sid, SID_SIZE);
|
2012-09-28 01:58:01 +00:00
|
|
|
mdp.bind.port=port;
|
2012-11-06 19:48:54 +00:00
|
|
|
int result=overlay_mdp_send(mdp_sockfd, &mdp,MDP_AWAITREPLY,5000);
|
2012-09-14 02:17:48 +00:00
|
|
|
if (result) {
|
|
|
|
if (mdp.packetTypeAndFlags==MDP_ERROR)
|
|
|
|
WHYF("Could not bind to MDP port %d: error=%d, message='%s'",
|
|
|
|
port,mdp.error.error,mdp.error.message);
|
|
|
|
else
|
|
|
|
WHYF("Could not bind to MDP port %d (no reason given)",port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-06 19:48:54 +00:00
|
|
|
int overlay_mdp_getmyaddr(int mdp_sockfd, unsigned index, sid_t *sid)
|
2012-09-14 02:17:48 +00:00
|
|
|
{
|
|
|
|
overlay_mdp_frame a;
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
|
|
|
|
|
|
a.packetTypeAndFlags=MDP_GETADDRS;
|
|
|
|
a.addrlist.mode = MDP_ADDRLIST_MODE_SELF;
|
|
|
|
a.addrlist.first_sid=index;
|
2013-04-12 07:46:50 +00:00
|
|
|
a.addrlist.last_sid=OVERLAY_MDP_ADDRLIST_MAX_SID_COUNT;
|
2012-09-14 02:17:48 +00:00
|
|
|
a.addrlist.frame_sid_count=MDP_MAX_SID_REQUEST;
|
2012-11-06 19:48:54 +00:00
|
|
|
int result=overlay_mdp_send(mdp_sockfd,&a,MDP_AWAITREPLY,5000);
|
2012-09-14 02:17:48 +00:00
|
|
|
if (result) {
|
|
|
|
if (a.packetTypeAndFlags == MDP_ERROR)
|
|
|
|
DEBUGF("MDP Server error #%d: '%s'", a.error.error, a.error.message);
|
|
|
|
return WHY("Failed to get local address list");
|
|
|
|
}
|
|
|
|
if ((a.packetTypeAndFlags&MDP_TYPE_MASK)!=MDP_ADDRLIST)
|
|
|
|
return WHY("MDP Server returned something other than an address list");
|
|
|
|
if (0) DEBUGF("local addr 0 = %s",alloca_tohex_sid(a.addrlist.sids[0]));
|
2013-02-15 09:04:24 +00:00
|
|
|
bcopy(&a.addrlist.sids[0][0], sid->binary, sizeof sid->binary);
|
2012-09-14 02:17:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_mdp_relevant_bytes(overlay_mdp_frame *mdp)
|
|
|
|
{
|
2012-09-28 01:58:01 +00:00
|
|
|
int len;
|
2012-09-14 02:17:48 +00:00
|
|
|
switch(mdp->packetTypeAndFlags&MDP_TYPE_MASK)
|
|
|
|
{
|
2012-11-26 04:10:22 +00:00
|
|
|
case MDP_ROUTING_TABLE:
|
2012-09-14 02:17:48 +00:00
|
|
|
case MDP_GOODBYE:
|
|
|
|
/* no arguments for saying goodbye */
|
2012-09-28 01:58:01 +00:00
|
|
|
len=&mdp->raw[0]-(char *)mdp;
|
2012-09-14 02:17:48 +00:00
|
|
|
break;
|
|
|
|
case MDP_ADDRLIST:
|
2012-09-28 01:58:01 +00:00
|
|
|
len=(&mdp->addrlist.sids[0][0]-(unsigned char *)mdp) + mdp->addrlist.frame_sid_count*SID_SIZE;
|
2012-09-14 02:17:48 +00:00
|
|
|
break;
|
|
|
|
case MDP_GETADDRS:
|
|
|
|
len=&mdp->addrlist.sids[0][0]-(unsigned char *)mdp;
|
|
|
|
break;
|
|
|
|
case MDP_TX:
|
2012-09-28 01:58:01 +00:00
|
|
|
len=(&mdp->out.payload[0]-(unsigned char *)mdp) + mdp->out.payload_length;
|
2012-09-14 02:17:48 +00:00
|
|
|
break;
|
2012-09-28 01:58:01 +00:00
|
|
|
case MDP_BIND:
|
|
|
|
len=(&mdp->raw[0] - (char *)mdp) + sizeof(sockaddr_mdp);
|
2012-09-14 02:17:48 +00:00
|
|
|
break;
|
2012-12-05 04:43:47 +00:00
|
|
|
case MDP_SCAN:
|
|
|
|
len=(&mdp->raw[0] - (char *)mdp) + sizeof(struct overlay_mdp_scan);
|
|
|
|
break;
|
2012-09-14 02:17:48 +00:00
|
|
|
case MDP_ERROR:
|
|
|
|
/* This formulation is used so that we don't copy any bytes after the
|
|
|
|
end of the string, to avoid information leaks */
|
2012-09-28 01:58:01 +00:00
|
|
|
len=(&mdp->error.message[0]-(char *)mdp) + strlen(mdp->error.message)+1;
|
2012-09-14 02:17:48 +00:00
|
|
|
if (mdp->error.error) INFOF("mdp return/error code: %d:%s",mdp->error.error,mdp->error.message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return WHY("Illegal MDP frame type.");
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|