2012-03-16 22:30:51 +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.
|
|
|
|
*/
|
|
|
|
|
2012-03-20 15:06:52 +00:00
|
|
|
#include <sys/stat.h>
|
2012-07-18 09:46:30 +00:00
|
|
|
#include "serval.h"
|
2012-12-11 05:29:46 +00:00
|
|
|
#include "conf.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-07-18 09:46:30 +00:00
|
|
|
#include "strbuf.h"
|
2012-08-22 00:51:38 +00:00
|
|
|
#include "overlay_buffer.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_address.h"
|
|
|
|
#include "overlay_packet.h"
|
2012-09-14 02:17:48 +00:00
|
|
|
#include "mdp_client.h"
|
2012-10-19 04:24:41 +00:00
|
|
|
#include "crypto.h"
|
2012-03-17 02:32:09 +00:00
|
|
|
|
2012-08-14 05:01:25 +00:00
|
|
|
struct profile_total mdp_stats={.name="overlay_mdp_poll"};
|
|
|
|
|
|
|
|
struct sched_ent mdp_abstract={
|
|
|
|
.function = overlay_mdp_poll,
|
|
|
|
.stats = &mdp_stats,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sched_ent mdp_named={
|
|
|
|
.function = overlay_mdp_poll,
|
|
|
|
.stats = &mdp_stats,
|
|
|
|
};
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2012-10-17 05:59:40 +00:00
|
|
|
static int overlay_saw_mdp_frame(struct overlay_frame *frame, overlay_mdp_frame *mdp, time_ms_t now);
|
|
|
|
|
2012-03-16 22:30:51 +00:00
|
|
|
int overlay_mdp_setup_sockets()
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
name.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
#ifndef HAVE_LINUX_IF_H
|
|
|
|
/* Abstrack name space (i.e., non-file represented) unix domain sockets are a
|
|
|
|
linux-only thing. */
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_abstract.poll.fd = -1;
|
2012-03-16 22:30:51 +00:00
|
|
|
#else
|
2012-08-14 05:01:25 +00:00
|
|
|
if (mdp_abstract.poll.fd<=0) {
|
2012-03-17 02:32:09 +00:00
|
|
|
/* Abstract name space unix sockets is a special Linux thing, which is
|
|
|
|
convenient for us because Android is Linux, but does not have a shared
|
|
|
|
writable path that is on a UFS partition, so we cannot use traditional
|
|
|
|
named unix domain sockets. So the abstract name space gives us a solution. */
|
|
|
|
name.sun_path[0]=0;
|
|
|
|
/* XXX The 100 should be replaced with the actual maximum allowed.
|
|
|
|
Apparently POSIX requires it to be at least 100, but I would still feel
|
|
|
|
more comfortable with using the appropriate constant. */
|
2012-06-21 05:15:09 +00:00
|
|
|
snprintf(&name.sun_path[1],100,
|
2012-06-22 07:16:30 +00:00
|
|
|
confValueGet("mdp.socket",DEFAULT_MDP_SOCKET_NAME));
|
2012-03-17 02:32:09 +00:00
|
|
|
len = 1+strlen(&name.sun_path[1]) + sizeof(name.sun_family);
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_abstract.poll.fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (mdp_abstract.poll.fd>-1) {
|
2012-05-17 03:43:42 +00:00
|
|
|
int reuseP=1;
|
2012-07-03 08:07:33 +00:00
|
|
|
if (setsockopt( mdp_abstract.poll.fd, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof(reuseP)) == -1) {
|
|
|
|
WARN_perror("setsockopt(SO_REUSEADDR)");
|
|
|
|
WARN("Could not set socket reuse addresses");
|
2012-05-20 06:36:32 +00:00
|
|
|
}
|
2012-07-03 08:07:33 +00:00
|
|
|
if (bind(mdp_abstract.poll.fd, (struct sockaddr *)&name, len) == -1) {
|
|
|
|
WARN_perror("bind");
|
2012-07-02 03:49:54 +00:00
|
|
|
close(mdp_abstract.poll.fd);
|
2012-07-03 08:07:33 +00:00
|
|
|
mdp_abstract.poll.fd = -1;
|
|
|
|
WARN("bind of abstract name space socket failed (not a problem on non-linux systems)");
|
2012-03-17 02:32:09 +00:00
|
|
|
}
|
2012-04-24 05:09:58 +00:00
|
|
|
int send_buffer_size=64*1024;
|
2012-07-03 08:07:33 +00:00
|
|
|
if (setsockopt(mdp_abstract.poll.fd, SOL_SOCKET, SO_SNDBUF, &send_buffer_size, sizeof(send_buffer_size)) == -1)
|
|
|
|
WARN_perror("setsockopt(SO_SNDBUF)");
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_abstract.poll.events = POLLIN;
|
|
|
|
watch(&mdp_abstract);
|
2012-05-17 03:43:42 +00:00
|
|
|
}
|
2012-03-16 22:30:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-08-14 05:01:25 +00:00
|
|
|
if (mdp_named.poll.fd<=0) {
|
2012-07-03 08:07:33 +00:00
|
|
|
if (!form_serval_instance_path(&name.sun_path[0], 100, "mdp.socket"))
|
2012-03-29 03:37:07 +00:00
|
|
|
return WHY("Cannot construct name of unix domain socket.");
|
2012-03-18 23:13:11 +00:00
|
|
|
unlink(&name.sun_path[0]);
|
|
|
|
len = 0+strlen(&name.sun_path[0]) + sizeof(name.sun_family)+1;
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_named.poll.fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (mdp_named.poll.fd>-1) {
|
2012-05-17 03:43:42 +00:00
|
|
|
int reuseP=1;
|
2012-07-03 08:07:33 +00:00
|
|
|
if(setsockopt( mdp_named.poll.fd, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof(reuseP)) == -1) {
|
|
|
|
WARN_perror("setsockopt(SO_REUSEADDR)");
|
|
|
|
WARN("Could not set socket reuse addresses");
|
|
|
|
}
|
|
|
|
if (bind(mdp_named.poll.fd, (struct sockaddr *)&name, len) == -1) {
|
|
|
|
WARN_perror("bind");
|
2012-07-02 03:49:54 +00:00
|
|
|
close(mdp_named.poll.fd);
|
2012-07-03 08:07:33 +00:00
|
|
|
mdp_named.poll.fd = -1;
|
|
|
|
WARN("Could not bind named unix domain socket");
|
2012-03-17 02:32:09 +00:00
|
|
|
}
|
2012-04-24 05:09:58 +00:00
|
|
|
int send_buffer_size=64*1024;
|
2012-07-03 08:07:33 +00:00
|
|
|
if (setsockopt(mdp_named.poll.fd, SOL_SOCKET, SO_RCVBUF, &send_buffer_size, sizeof(send_buffer_size)) == -1)
|
|
|
|
WARN_perror("setsockopt(SO_RCVBUF)");
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_named.function = overlay_mdp_poll;
|
2012-07-02 05:50:30 +00:00
|
|
|
mdp_named.stats = &mdp_stats;
|
2012-07-02 03:49:54 +00:00
|
|
|
mdp_named.poll.events = POLLIN;
|
|
|
|
watch(&mdp_named);
|
2012-03-16 22:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
#define MDP_MAX_BINDINGS 100
|
|
|
|
#define MDP_MAX_SOCKET_NAME_LEN 110
|
2012-08-14 05:01:25 +00:00
|
|
|
|
|
|
|
struct mdp_binding{
|
2012-09-14 02:19:01 +00:00
|
|
|
struct subscriber *subscriber;
|
|
|
|
int port;
|
2012-08-14 05:01:25 +00:00
|
|
|
char socket_name[MDP_MAX_SOCKET_NAME_LEN];
|
|
|
|
int name_len;
|
|
|
|
time_ms_t binding_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mdp_binding mdp_bindings[MDP_MAX_BINDINGS];
|
2012-03-20 06:41:58 +00:00
|
|
|
int mdp_bindings_initialised=0;
|
|
|
|
|
|
|
|
int overlay_mdp_reply_error(int sock,
|
|
|
|
struct sockaddr_un *recvaddr,int recvaddrlen,
|
|
|
|
int error_number,char *message)
|
|
|
|
{
|
|
|
|
overlay_mdp_frame mdpreply;
|
|
|
|
|
|
|
|
mdpreply.packetTypeAndFlags=MDP_ERROR;
|
|
|
|
mdpreply.error.error=error_number;
|
2012-08-27 00:34:59 +00:00
|
|
|
if (error_number)
|
|
|
|
WHYF("MDP error, code #%d %s",error_number, message);
|
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
if (error_number==0||message)
|
|
|
|
snprintf(&mdpreply.error.message[0],128,"%s",message?message:"Success");
|
2012-08-27 00:34:59 +00:00
|
|
|
else{
|
2012-03-20 06:41:58 +00:00
|
|
|
snprintf(&mdpreply.error.message[0],128,"Error code #%d",error_number);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-03-20 06:41:58 +00:00
|
|
|
mdpreply.error.message[127]=0;
|
2012-03-27 08:55:38 +00:00
|
|
|
|
|
|
|
return overlay_mdp_reply(sock,recvaddr,recvaddrlen,&mdpreply);
|
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_mdp_reply(int sock,struct sockaddr_un *recvaddr,int recvaddrlen,
|
|
|
|
overlay_mdp_frame *mdpreply)
|
|
|
|
{
|
|
|
|
int replylen;
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
if (!recvaddr) return WHY("No reply address");
|
2012-05-02 17:30:34 +00:00
|
|
|
|
2012-03-27 08:55:38 +00:00
|
|
|
replylen=overlay_mdp_relevant_bytes(mdpreply);
|
|
|
|
if (replylen<0) return WHY("Invalid MDP frame (could not compute length)");
|
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
errno=0;
|
2012-03-27 08:55:38 +00:00
|
|
|
int r=sendto(sock,(char *)mdpreply,replylen,0,
|
2012-03-20 06:41:58 +00:00
|
|
|
(struct sockaddr *)recvaddr,recvaddrlen);
|
2012-04-21 22:55:48 +00:00
|
|
|
if (r<replylen) {
|
2012-05-08 05:15:26 +00:00
|
|
|
WHY_perror("sendto(d)");
|
2012-05-08 08:18:16 +00:00
|
|
|
return WHYF("sendto() failed when sending MDP reply, sock=%d, r=%d", sock, r);
|
2012-04-21 22:55:48 +00:00
|
|
|
} else
|
2012-05-20 06:36:32 +00:00
|
|
|
if (0) DEBUGF("reply of %d bytes sent",r);
|
2012-03-20 06:41:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_mdp_reply_ok(int sock,
|
|
|
|
struct sockaddr_un *recvaddr,int recvaddrlen,
|
|
|
|
char *message)
|
|
|
|
{
|
|
|
|
return overlay_mdp_reply_error(sock,recvaddr,recvaddrlen,0,message);
|
|
|
|
}
|
|
|
|
|
2012-04-30 20:40:47 +00:00
|
|
|
int overlay_mdp_releasebindings(struct sockaddr_un *recvaddr,int recvaddrlen)
|
|
|
|
{
|
|
|
|
/* Free up any MDP bindings held by this client. */
|
|
|
|
int i;
|
|
|
|
for(i=0;i<MDP_MAX_BINDINGS;i++)
|
2012-08-14 05:01:25 +00:00
|
|
|
if (mdp_bindings[i].name_len==recvaddrlen)
|
|
|
|
if (!memcmp(mdp_bindings[i].socket_name,recvaddr->sun_path,recvaddrlen))
|
2012-09-14 02:19:01 +00:00
|
|
|
mdp_bindings[i].port=0;
|
2012-04-30 20:40:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-14 02:19:01 +00:00
|
|
|
int overlay_mdp_process_bind_request(int sock, struct subscriber *subscriber, int port,
|
|
|
|
int flags, struct sockaddr_un *recvaddr, int recvaddrlen)
|
2012-03-20 06:41:58 +00:00
|
|
|
{
|
|
|
|
int i;
|
2012-09-28 01:58:01 +00:00
|
|
|
|
|
|
|
if (port<=0){
|
|
|
|
return WHYF("Port %d cannot be bound", port);
|
|
|
|
}
|
2012-03-20 06:41:58 +00:00
|
|
|
if (!mdp_bindings_initialised) {
|
|
|
|
/* Mark all slots as unused */
|
2012-08-14 05:01:25 +00:00
|
|
|
for(i=0;i<MDP_MAX_BINDINGS;i++)
|
2012-09-14 02:19:01 +00:00
|
|
|
mdp_bindings[i].port=0;
|
2012-03-20 06:41:58 +00:00
|
|
|
mdp_bindings_initialised=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if binding already exists */
|
|
|
|
int free=-1;
|
|
|
|
for(i=0;i<MDP_MAX_BINDINGS;i++) {
|
|
|
|
/* Look for duplicate bindings */
|
2012-09-27 01:32:58 +00:00
|
|
|
if (mdp_bindings[i].port == port && mdp_bindings[i].subscriber == subscriber) {
|
2012-09-27 06:57:37 +00:00
|
|
|
if (mdp_bindings[i].name_len==recvaddrlen &&
|
|
|
|
!memcmp(mdp_bindings[i].socket_name,recvaddr->sun_path,recvaddrlen)) {
|
2012-09-14 02:19:01 +00:00
|
|
|
// this client already owns this port binding?
|
|
|
|
INFO("Identical binding exists");
|
|
|
|
return 0;
|
|
|
|
}else if(flags&MDP_FORCE){
|
|
|
|
// steal the port binding
|
2012-09-27 01:32:58 +00:00
|
|
|
free=i;
|
2012-09-14 02:19:01 +00:00
|
|
|
break;
|
|
|
|
}else{
|
|
|
|
return WHY("Port already in use");
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 02:19:01 +00:00
|
|
|
/* Look for free slots in case we need one */
|
|
|
|
if ((free==-1)&&(mdp_bindings[i].port==0)) free=i;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
/* Okay, so no binding exists. Make one, and return success.
|
|
|
|
If we have too many bindings, we should return an error.
|
|
|
|
XXX - We don't find out when the socket responsible for a binding has died,
|
|
|
|
so stale bindings can hang around. We really need a solution to this, e.g.,
|
|
|
|
probing the sockets periodically (by sending an MDP NOOP frame perhaps?) and
|
|
|
|
destroying any socket that reports an error.
|
|
|
|
*/
|
|
|
|
if (free==-1) {
|
|
|
|
/* XXX Should we probe for stale bindings here and now, since this is when
|
2012-04-30 20:40:47 +00:00
|
|
|
we want the spare slots ?
|
|
|
|
|
|
|
|
Picking one at random is as good a policy as any.
|
|
|
|
Call listeners don't have a port binding, so are unaffected by this.
|
|
|
|
*/
|
|
|
|
free=random()%MDP_MAX_BINDINGS;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests)
|
2012-10-05 08:15:30 +00:00
|
|
|
DEBUGF("Binding %s:%d", subscriber ? alloca_tohex_sid(subscriber->sid) : "NULL", port);
|
2012-03-20 06:41:58 +00:00
|
|
|
/* Okay, record binding and report success */
|
2012-09-14 02:19:01 +00:00
|
|
|
mdp_bindings[free].port=port;
|
|
|
|
mdp_bindings[free].subscriber=subscriber;
|
|
|
|
|
2012-08-14 05:01:25 +00:00
|
|
|
mdp_bindings[free].name_len=recvaddrlen-2;
|
|
|
|
memcpy(mdp_bindings[free].socket_name,recvaddr->sun_path,
|
|
|
|
mdp_bindings[free].name_len);
|
|
|
|
mdp_bindings[free].binding_time=gettime_ms();
|
2012-09-14 02:19:01 +00:00
|
|
|
return 0;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
static int overlay_mdp_decode_header(struct overlay_buffer *buff, overlay_mdp_frame *mdp)
|
|
|
|
{
|
|
|
|
/* extract MDP port numbers */
|
|
|
|
int port = ob_get_packed_ui32(buff);
|
|
|
|
int same = port&1;
|
|
|
|
port >>=1;
|
|
|
|
mdp->in.dst.port = port;
|
|
|
|
if (!same){
|
|
|
|
port = ob_get_packed_ui32(buff);
|
|
|
|
}
|
|
|
|
mdp->in.src.port = port;
|
|
|
|
|
|
|
|
int len=ob_remaining(buff);
|
|
|
|
|
|
|
|
if (len<0)
|
|
|
|
return WHY("MDP payload is too short");
|
|
|
|
mdp->in.payload_length=len;
|
|
|
|
return ob_get_bytes(buff, &mdp->in.payload[0], len);
|
|
|
|
}
|
|
|
|
|
2012-09-01 02:47:22 +00:00
|
|
|
int overlay_mdp_decrypt(struct overlay_frame *f, overlay_mdp_frame *mdp)
|
2012-03-27 08:55:38 +00:00
|
|
|
{
|
2012-06-25 23:52:51 +00:00
|
|
|
IN();
|
2012-04-14 00:03:29 +00:00
|
|
|
|
2012-09-14 05:11:17 +00:00
|
|
|
/* Indicate MDP message type */
|
|
|
|
mdp->packetTypeAndFlags=MDP_TX;
|
|
|
|
|
2012-11-23 02:56:43 +00:00
|
|
|
switch(f->modifiers&(OF_CRYPTO_CIPHERED|OF_CRYPTO_SIGNED)) {
|
2012-04-13 22:45:29 +00:00
|
|
|
case 0:
|
2012-10-19 04:24:41 +00:00
|
|
|
/* nothing to do, b already points to the plain text */
|
2012-08-14 04:36:59 +00:00
|
|
|
mdp->packetTypeAndFlags|=MDP_NOCRYPT|MDP_NOSIGN;
|
2012-11-28 04:35:06 +00:00
|
|
|
RETURN(overlay_mdp_decode_header(f->payload, mdp));
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2012-04-13 22:45:29 +00:00
|
|
|
case OF_CRYPTO_CIPHERED:
|
2012-09-01 02:47:22 +00:00
|
|
|
RETURN(WHY("decryption not implemented"));
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2012-04-13 22:45:29 +00:00
|
|
|
case OF_CRYPTO_SIGNED:
|
2012-11-28 04:35:06 +00:00
|
|
|
{
|
|
|
|
int len = ob_remaining(f->payload);
|
|
|
|
if (crypto_verify_message(f->source, ob_ptr(f->payload), &len))
|
|
|
|
RETURN(-1);
|
2012-09-10 01:01:01 +00:00
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
mdp->packetTypeAndFlags|=MDP_NOCRYPT;
|
|
|
|
ob_limitsize(f->payload, len + ob_position(f->payload));
|
|
|
|
RETURN(overlay_mdp_decode_header(f->payload, mdp));
|
|
|
|
}
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2012-04-13 22:45:29 +00:00
|
|
|
case OF_CRYPTO_CIPHERED|OF_CRYPTO_SIGNED:
|
2012-04-14 02:47:46 +00:00
|
|
|
{
|
2012-10-19 04:24:41 +00:00
|
|
|
if (0) DEBUGF("crypted MDP frame for %s", alloca_tohex_sid(f->destination->sid));
|
2012-04-14 04:50:49 +00:00
|
|
|
|
2012-11-27 06:09:58 +00:00
|
|
|
int nm=crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES;
|
2012-04-14 02:47:46 +00:00
|
|
|
int nb=crypto_box_curve25519xsalsa20poly1305_NONCEBYTES;
|
2012-04-14 03:03:15 +00:00
|
|
|
int zb=crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
|
2012-11-27 06:09:58 +00:00
|
|
|
int cz=crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
|
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
unsigned char *k=keyring_get_nm_bytes(f->destination->sid, f->source->sid);
|
2012-09-01 02:47:22 +00:00
|
|
|
if (!k)
|
|
|
|
RETURN(WHY("I don't have the private key required to decrypt that"));
|
2012-11-28 04:35:06 +00:00
|
|
|
|
|
|
|
if (0){
|
|
|
|
dump("frame",&f->payload->bytes[f->payload->position],
|
|
|
|
ob_remaining(f->payload));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *nonce=ob_get_bytes_ptr(f->payload, nb);
|
|
|
|
int cipher_len=ob_remaining(f->payload);
|
|
|
|
unsigned char *cipher_text=ob_get_bytes_ptr(f->payload, cipher_len);
|
|
|
|
|
|
|
|
unsigned char plain_block[cipher_len+cz];
|
|
|
|
|
2012-11-27 06:09:58 +00:00
|
|
|
bzero(&plain_block[0],cz);
|
2012-11-28 04:35:06 +00:00
|
|
|
|
|
|
|
bcopy(cipher_text,&plain_block[cz],cipher_len);
|
|
|
|
|
2012-04-14 04:26:53 +00:00
|
|
|
if (0) {
|
2012-11-27 06:09:58 +00:00
|
|
|
dump("nm bytes",k,nm);
|
|
|
|
dump("nonce",nonce,nb);
|
2012-11-28 04:35:06 +00:00
|
|
|
dump("cipher block",plain_block,sizeof(plain_block));
|
2012-04-14 04:26:53 +00:00
|
|
|
}
|
2012-11-28 04:35:06 +00:00
|
|
|
cipher_len+=cz;
|
|
|
|
|
2012-04-14 02:47:46 +00:00
|
|
|
if (crypto_box_curve25519xsalsa20poly1305_open_afternm
|
2012-11-28 04:35:06 +00:00
|
|
|
(plain_block,plain_block,cipher_len,nonce,k)) {
|
2012-12-04 05:49:50 +00:00
|
|
|
RETURN(WHYF("crypto_box_open_afternm() failed (from %s, to %s, len %d)",
|
|
|
|
alloca_tohex_sid(f->source->sid), alloca_tohex_sid(f->destination->sid), cipher_len));
|
2012-06-25 23:52:51 +00:00
|
|
|
}
|
2012-11-28 04:35:06 +00:00
|
|
|
|
|
|
|
if (0) dump("plain block",plain_block,sizeof(plain_block));
|
|
|
|
|
|
|
|
cipher_len -= zb;
|
|
|
|
struct overlay_buffer *plaintext = ob_static(&plain_block[zb], cipher_len);
|
|
|
|
ob_limitsize(plaintext,cipher_len);
|
|
|
|
int ret=overlay_mdp_decode_header(plaintext, mdp);
|
|
|
|
ob_free(plaintext);
|
|
|
|
RETURN(ret);
|
2012-04-14 02:47:46 +00:00
|
|
|
}
|
2012-04-13 22:45:29 +00:00
|
|
|
}
|
2012-11-28 04:35:06 +00:00
|
|
|
RETURN(WHY("Failed to decode mdp payload"));
|
2012-06-25 23:52:51 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
int overlay_saw_mdp_containing_frame(struct overlay_frame *f, time_ms_t now)
|
2012-06-25 23:52:51 +00:00
|
|
|
{
|
|
|
|
IN();
|
|
|
|
/* Take frame source and destination and use them to populate mdp->in->{src,dst}
|
|
|
|
SIDs.
|
|
|
|
Take ports from mdp frame itself.
|
|
|
|
Take payload from mdp frame itself.
|
|
|
|
*/
|
|
|
|
overlay_mdp_frame mdp;
|
2012-09-28 01:58:01 +00:00
|
|
|
bzero(&mdp, sizeof(overlay_mdp_frame));
|
2012-09-07 02:04:00 +00:00
|
|
|
|
2012-10-09 05:39:33 +00:00
|
|
|
mdp.in.queue = f->queue;
|
2012-10-10 05:52:31 +00:00
|
|
|
mdp.in.ttl = f->ttl;
|
2012-10-09 05:39:33 +00:00
|
|
|
|
2012-06-25 23:52:51 +00:00
|
|
|
/* Get source and destination addresses */
|
2012-08-27 00:34:59 +00:00
|
|
|
if (f->destination)
|
|
|
|
bcopy(f->destination->sid,mdp.in.dst.sid,SID_SIZE);
|
|
|
|
else{
|
2012-10-09 06:33:17 +00:00
|
|
|
// pack the broadcast address into the mdp structure, note that we no longer care about the broadcast id
|
|
|
|
memset(mdp.in.dst.sid, 0xFF, SID_SIZE);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
bcopy(f->source->sid,mdp.in.src.sid,SID_SIZE);
|
2012-06-25 23:52:51 +00:00
|
|
|
|
|
|
|
/* copy crypto flags from frame so that we know if we need to decrypt or verify it */
|
2012-09-01 02:47:22 +00:00
|
|
|
if (overlay_mdp_decrypt(f,&mdp))
|
|
|
|
RETURN(-1);
|
2012-04-14 00:03:29 +00:00
|
|
|
|
2012-04-13 22:45:29 +00:00
|
|
|
/* and do something with it! */
|
2012-10-17 05:59:40 +00:00
|
|
|
RETURN(overlay_saw_mdp_frame(f, &mdp,now));
|
2012-03-27 08:55:38 +00:00
|
|
|
}
|
|
|
|
|
2012-04-15 20:36:43 +00:00
|
|
|
int overlay_mdp_swap_src_dst(overlay_mdp_frame *mdp)
|
|
|
|
{
|
|
|
|
sockaddr_mdp temp;
|
|
|
|
bcopy(&mdp->out.dst,&temp,sizeof(sockaddr_mdp));
|
|
|
|
bcopy(&mdp->out.src,&mdp->out.dst,sizeof(sockaddr_mdp));
|
|
|
|
bcopy(&temp,&mdp->out.src,sizeof(sockaddr_mdp));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-17 05:59:40 +00:00
|
|
|
static int overlay_saw_mdp_frame(struct overlay_frame *frame, overlay_mdp_frame *mdp, time_ms_t now)
|
2012-03-16 22:30:51 +00:00
|
|
|
{
|
2012-06-25 23:52:51 +00:00
|
|
|
IN();
|
2012-03-28 00:58:04 +00:00
|
|
|
int i;
|
|
|
|
int match=-1;
|
2012-03-27 10:43:40 +00:00
|
|
|
|
|
|
|
switch(mdp->packetTypeAndFlags&MDP_TYPE_MASK) {
|
|
|
|
case MDP_TX:
|
|
|
|
/* Regular MDP frame addressed to us. Look for matching port binding,
|
|
|
|
and if available, push to client. Else do nothing, or if we feel nice
|
|
|
|
send back a connection refused type message? Silence is probably the
|
2012-04-14 00:03:29 +00:00
|
|
|
more prudent path.
|
|
|
|
*/
|
2012-06-22 08:00:21 +00:00
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests)
|
2012-09-19 06:25:29 +00:00
|
|
|
DEBUGF("Received packet with listener (MDP ports: src=%s*:%d, dst=%d)",
|
2012-07-04 01:00:46 +00:00
|
|
|
alloca_tohex(mdp->out.src.sid, 7),
|
2012-06-25 05:16:55 +00:00
|
|
|
mdp->out.src.port,mdp->out.dst.port);
|
2012-06-22 08:00:21 +00:00
|
|
|
|
2012-09-14 02:19:01 +00:00
|
|
|
// TODO pass in dest subscriber as an argument, we should know it by now
|
|
|
|
struct subscriber *destination = NULL;
|
2012-10-17 05:59:40 +00:00
|
|
|
if (frame)
|
|
|
|
destination = frame->destination;
|
|
|
|
else if (!is_sid_broadcast(mdp->out.dst.sid)){
|
2012-09-14 02:19:01 +00:00
|
|
|
destination = find_subscriber(mdp->out.dst.sid, SID_SIZE, 1);
|
|
|
|
}
|
|
|
|
|
2012-03-28 00:58:04 +00:00
|
|
|
for(i=0;i<MDP_MAX_BINDINGS;i++)
|
|
|
|
{
|
2012-09-14 02:19:01 +00:00
|
|
|
if (mdp_bindings[i].port!=mdp->out.dst.port)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((!destination) || mdp_bindings[i].subscriber == destination){
|
|
|
|
/* exact match, so stop searching */
|
|
|
|
match=i;
|
|
|
|
break;
|
|
|
|
}else if (!mdp_bindings[i].subscriber){
|
|
|
|
/* If we find an "ANY" binding, remember it. But we will prefer an exact match if we find one */
|
|
|
|
match=i;
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-08-14 05:01:25 +00:00
|
|
|
if (match>-1) {
|
2012-03-28 02:40:41 +00:00
|
|
|
struct sockaddr_un addr;
|
2012-06-22 08:00:21 +00:00
|
|
|
|
2012-08-14 05:01:25 +00:00
|
|
|
bcopy(mdp_bindings[match].socket_name,addr.sun_path,mdp_bindings[match].name_len);
|
2012-03-28 02:40:41 +00:00
|
|
|
addr.sun_family=AF_UNIX;
|
2012-06-21 02:51:25 +00:00
|
|
|
errno=0;
|
|
|
|
int len=overlay_mdp_relevant_bytes(mdp);
|
2012-07-02 03:49:54 +00:00
|
|
|
int r=sendto(mdp_named.poll.fd,mdp,len,0,(struct sockaddr*)&addr,sizeof(addr));
|
2012-06-21 02:51:25 +00:00
|
|
|
if (r==overlay_mdp_relevant_bytes(mdp)) {
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(0);
|
2012-06-21 02:51:25 +00:00
|
|
|
}
|
2012-06-25 23:52:51 +00:00
|
|
|
WHY("didn't send mdp packet");
|
2012-06-21 02:51:25 +00:00
|
|
|
if (errno==ENOENT) {
|
|
|
|
/* far-end of socket has died, so drop binding */
|
2012-08-14 05:01:25 +00:00
|
|
|
INFOF("Closing dead MDP client '%s'",mdp_bindings[match].socket_name);
|
|
|
|
overlay_mdp_releasebindings(&addr,mdp_bindings[match].name_len);
|
2012-06-21 02:51:25 +00:00
|
|
|
}
|
2012-05-08 05:15:26 +00:00
|
|
|
WHY_perror("sendto(e)");
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(WHY("Failed to pass received MDP frame to client"));
|
2012-03-28 00:58:04 +00:00
|
|
|
} else {
|
|
|
|
/* No socket is bound, ignore the packet ... except for magic sockets */
|
2012-11-30 01:18:00 +00:00
|
|
|
RETURN(overlay_mdp_try_interal_services(mdp));
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
2012-03-27 10:43:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(WHYF("We should only see MDP_TX frames here (MDP message type = 0x%x)",
|
|
|
|
mdp->packetTypeAndFlags));
|
2012-03-27 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(0);
|
2012-03-16 22:30:51 +00:00
|
|
|
}
|
2012-03-17 02:32:09 +00:00
|
|
|
|
2012-08-06 07:14:36 +00:00
|
|
|
int overlay_mdp_dnalookup_reply(const sockaddr_mdp *dstaddr, const unsigned char *resolved_sid, const char *uri, const char *did, const char *name)
|
2012-07-20 08:47:43 +00:00
|
|
|
{
|
|
|
|
overlay_mdp_frame mdpreply;
|
|
|
|
bzero(&mdpreply, sizeof mdpreply);
|
|
|
|
mdpreply.packetTypeAndFlags = MDP_TX; // outgoing MDP message
|
2012-12-15 23:35:32 +00:00
|
|
|
mdpreply.out.queue=OQ_ORDINARY;
|
2012-08-06 07:14:36 +00:00
|
|
|
memcpy(mdpreply.out.src.sid, resolved_sid, SID_SIZE);
|
|
|
|
mdpreply.out.src.port = MDP_PORT_DNALOOKUP;
|
|
|
|
bcopy(dstaddr, &mdpreply.out.dst, sizeof(sockaddr_mdp));
|
2012-07-20 08:47:43 +00:00
|
|
|
/* build reply as TOKEN|URI|DID|NAME|<NUL> */
|
|
|
|
strbuf b = strbuf_local((char *)mdpreply.out.payload, sizeof mdpreply.out.payload);
|
2012-08-06 07:14:36 +00:00
|
|
|
strbuf_tohex(b, resolved_sid, SID_SIZE);
|
2013-01-16 05:22:06 +00:00
|
|
|
strbuf_sprintf(b, "|%s|%s|%s|", uri, did, name?name:"");
|
2012-07-20 08:47:43 +00:00
|
|
|
if (strbuf_overrun(b))
|
|
|
|
return WHY("MDP payload overrun");
|
|
|
|
mdpreply.out.payload_length = strbuf_len(b) + 1;
|
|
|
|
/* deliver reply */
|
|
|
|
return overlay_mdp_dispatch(&mdpreply, 0 /* system generated */, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2012-09-14 06:20:04 +00:00
|
|
|
int overlay_mdp_check_binding(struct subscriber *subscriber, int port, int userGeneratedFrameP,
|
|
|
|
struct sockaddr_un *recvaddr, int recvaddrlen)
|
2012-03-27 09:15:39 +00:00
|
|
|
{
|
2012-08-06 07:14:36 +00:00
|
|
|
|
2012-09-14 06:20:04 +00:00
|
|
|
/* Check if the address is in the list of bound addresses,
|
2012-03-27 09:15:39 +00:00
|
|
|
and that the recvaddr matches. */
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-03-27 09:15:39 +00:00
|
|
|
int i;
|
2012-08-06 07:14:36 +00:00
|
|
|
for(i = 0; i < MDP_MAX_BINDINGS; ++i) {
|
2012-09-14 06:20:04 +00:00
|
|
|
if (mdp_bindings[i].port != port)
|
2012-09-14 02:19:01 +00:00
|
|
|
continue;
|
|
|
|
if ((!mdp_bindings[i].subscriber) || mdp_bindings[i].subscriber == subscriber) {
|
2012-08-06 07:14:36 +00:00
|
|
|
/* Binding matches, now make sure the sockets match */
|
2012-08-14 05:01:25 +00:00
|
|
|
if ( mdp_bindings[i].name_len == recvaddrlen - sizeof(short)
|
|
|
|
&& memcmp(mdp_bindings[i].socket_name, recvaddr->sun_path, recvaddrlen - sizeof(short)) == 0
|
2012-08-06 07:14:36 +00:00
|
|
|
) {
|
|
|
|
/* Everything matches, so this unix socket and MDP address combination is valid */
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-27 09:15:39 +00:00
|
|
|
}
|
2012-08-06 07:14:36 +00:00
|
|
|
}
|
2012-03-27 09:15:39 +00:00
|
|
|
|
2012-04-15 20:36:43 +00:00
|
|
|
/* Check for build-in port listeners */
|
2012-09-14 06:20:04 +00:00
|
|
|
if (!userGeneratedFrameP){
|
|
|
|
switch(port) {
|
2012-09-14 02:19:01 +00:00
|
|
|
case MDP_PORT_NOREPLY:
|
2012-04-15 20:36:43 +00:00
|
|
|
case MDP_PORT_ECHO:
|
|
|
|
case MDP_PORT_KEYMAPREQUEST:
|
2012-04-18 22:04:12 +00:00
|
|
|
case MDP_PORT_VOMP:
|
2012-04-24 20:47:26 +00:00
|
|
|
case MDP_PORT_DNALOOKUP:
|
2012-11-29 10:38:11 +00:00
|
|
|
case MDP_PORT_RHIZOME_RESPONSE:
|
|
|
|
case MDP_PORT_RHIZOME_REQUEST:
|
2013-02-12 00:04:04 +00:00
|
|
|
case MDP_PORT_RHIZOME_MANIFEST_REQUEST:
|
2012-12-04 04:17:57 +00:00
|
|
|
case MDP_PORT_PROBE:
|
2012-12-07 05:34:40 +00:00
|
|
|
case MDP_PORT_STUNREQ:
|
|
|
|
case MDP_PORT_STUN:
|
2012-04-15 20:36:43 +00:00
|
|
|
return 0;
|
2012-08-06 07:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-15 20:36:43 +00:00
|
|
|
|
2012-08-06 07:14:36 +00:00
|
|
|
return WHYF("No such binding: recvaddr=%p %s addr=%s port=%u (0x%x) -- possible spoofing attack",
|
|
|
|
recvaddr,
|
|
|
|
recvaddr ? alloca_toprint(-1, recvaddr->sun_path, recvaddrlen - sizeof(short)) : "",
|
2012-09-14 06:20:04 +00:00
|
|
|
alloca_tohex_sid(subscriber->sid),
|
|
|
|
port, port
|
2012-08-02 07:46:18 +00:00
|
|
|
);
|
2012-03-27 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
int overlay_mdp_encode_ports(struct overlay_buffer *plaintext, int dst_port, int src_port){
|
|
|
|
int port=dst_port << 1;
|
|
|
|
if (dst_port==src_port)
|
|
|
|
port |= 1;
|
|
|
|
if (ob_append_packed_ui32(plaintext, port))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (dst_port!=src_port){
|
|
|
|
if (ob_append_packed_ui32(plaintext, src_port))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-28 00:58:04 +00:00
|
|
|
/* Construct MDP packet frame from overlay_mdp_frame structure
|
|
|
|
(need to add return address from bindings list, and copy
|
|
|
|
payload etc).
|
|
|
|
This is for use by the SERVER.
|
|
|
|
Clients should use overlay_mdp_send()
|
|
|
|
*/
|
2012-04-14 00:03:29 +00:00
|
|
|
int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
|
|
|
|
struct sockaddr_un *recvaddr,int recvaddrlen)
|
2012-03-28 00:58:04 +00:00
|
|
|
{
|
2012-06-25 23:52:51 +00:00
|
|
|
IN();
|
2012-09-14 06:20:04 +00:00
|
|
|
|
|
|
|
/* Prepare the overlay frame for dispatch */
|
|
|
|
struct overlay_frame *frame = calloc(1,sizeof(struct overlay_frame));
|
2012-09-27 06:57:37 +00:00
|
|
|
if (!frame)
|
|
|
|
FATAL("Couldn't allocate frame buffer");
|
2012-09-14 06:20:04 +00:00
|
|
|
|
|
|
|
if (is_sid_any(mdp->out.src.sid)){
|
|
|
|
/* set source to ourselves */
|
|
|
|
frame->source = my_subscriber;
|
|
|
|
bcopy(frame->source->sid, mdp->out.src.sid, SID_SIZE);
|
2012-10-09 03:19:23 +00:00
|
|
|
}else if (is_sid_broadcast(mdp->out.src.sid)){
|
2012-09-14 06:20:04 +00:00
|
|
|
/* This is rather naughty if it happens, since broadcasting a
|
|
|
|
response can lead to all manner of nasty things.
|
|
|
|
Picture a packet with broadcast as the source address, sent
|
|
|
|
to, say, the MDP echo port on another node, and with a source
|
|
|
|
port also of the echo port. Said echo will get multiplied many,
|
|
|
|
many, many times over before the TTL finally reaches zero.
|
|
|
|
So we just say no to any packet with a broadcast source address.
|
|
|
|
(Of course we have other layers of protection against such
|
|
|
|
shenanigens, such as using BPIs to smart-flood broadcasts, but
|
|
|
|
security comes through depth.)
|
|
|
|
*/
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHY("Packet had broadcast address as source address"));
|
|
|
|
}else{
|
2012-09-18 04:13:00 +00:00
|
|
|
// assume all local identities have already been unlocked and marked as SELF.
|
2012-09-14 06:20:04 +00:00
|
|
|
frame->source = find_subscriber(mdp->out.src.sid, SID_SIZE, 0);
|
|
|
|
if (!frame->source){
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHYF("Possible spoofing attempt, tried to send a packet from %s, which is an unknown SID", alloca_tohex_sid(mdp->out.src.sid)));
|
|
|
|
}
|
|
|
|
if (frame->source->reachable!=REACHABLE_SELF){
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHYF("Possible spoofing attempt, tried to send a packet from %s", alloca_tohex_sid(mdp->out.src.sid)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 00:58:04 +00:00
|
|
|
/* Work out if destination is broadcast or not */
|
2012-09-14 06:20:04 +00:00
|
|
|
if (overlay_mdp_check_binding(frame->source, mdp->out.src.port, userGeneratedFrameP,
|
|
|
|
recvaddr, recvaddrlen)){
|
|
|
|
op_free(frame);
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(overlay_mdp_reply_error
|
2012-07-02 03:49:54 +00:00
|
|
|
(mdp_named.poll.fd,
|
2012-06-25 23:52:51 +00:00
|
|
|
(struct sockaddr_un *)recvaddr,
|
|
|
|
recvaddrlen,8,
|
|
|
|
"Source address is invalid (you must bind to a source address before"
|
|
|
|
" you can send packets"));
|
2012-09-14 06:20:04 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-10-09 03:19:23 +00:00
|
|
|
if (is_sid_broadcast(mdp->out.dst.sid)){
|
2012-08-27 00:34:59 +00:00
|
|
|
/* broadcast packets cannot be encrypted, so complain if MDP_NOCRYPT
|
|
|
|
flag is not set. Also, MDP_NOSIGN must also be applied, until
|
|
|
|
NaCl cryptobox keys can be used for signing. */
|
2012-09-14 06:20:04 +00:00
|
|
|
if (!(mdp->packetTypeAndFlags&MDP_NOCRYPT)){
|
|
|
|
op_free(frame);
|
2012-08-27 00:34:59 +00:00
|
|
|
RETURN(overlay_mdp_reply_error(mdp_named.poll.fd,
|
|
|
|
recvaddr,recvaddrlen,5,
|
|
|
|
"Broadcast packets cannot be encrypted "));
|
2012-09-14 06:20:04 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
overlay_broadcast_generate_address(&frame->broadcast_id);
|
|
|
|
frame->destination = NULL;
|
|
|
|
}else{
|
|
|
|
frame->destination = find_subscriber(mdp->out.dst.sid, SID_SIZE, 1);
|
|
|
|
}
|
2012-10-10 05:52:31 +00:00
|
|
|
|
|
|
|
frame->ttl=mdp->out.ttl;
|
|
|
|
if (frame->ttl==0)
|
|
|
|
frame->ttl=64; /* default TTL */
|
2012-03-28 00:58:04 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (!frame->destination || frame->destination->reachable == REACHABLE_SELF)
|
2012-03-28 00:58:04 +00:00
|
|
|
{
|
|
|
|
/* Packet is addressed such that we should process it. */
|
2012-10-17 05:59:40 +00:00
|
|
|
overlay_saw_mdp_frame(NULL,mdp,gettime_ms());
|
2012-08-27 00:34:59 +00:00
|
|
|
if (frame->destination) {
|
2012-03-28 00:58:04 +00:00
|
|
|
/* Is local, and is not broadcast, so shouldn't get sent out
|
|
|
|
on the wire. */
|
2012-08-27 00:34:59 +00:00
|
|
|
op_free(frame);
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(0);
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:35:32 +00:00
|
|
|
frame->type=OF_TYPE_DATA;
|
2012-03-28 00:58:04 +00:00
|
|
|
frame->prev=NULL;
|
|
|
|
frame->next=NULL;
|
2012-11-28 04:35:06 +00:00
|
|
|
struct overlay_buffer *plaintext=ob_new();
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
if (overlay_mdp_encode_ports(plaintext, mdp->out.dst.port, mdp->out.src.port)){
|
2012-11-28 04:35:06 +00:00
|
|
|
ob_free(plaintext);
|
2012-12-04 04:17:57 +00:00
|
|
|
RETURN (-1);
|
2012-11-28 04:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ob_append_bytes(plaintext, mdp->out.payload, mdp->out.payload_length)){
|
|
|
|
ob_free(plaintext);
|
|
|
|
RETURN(-1);
|
|
|
|
}
|
2012-03-28 00:58:04 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
/* Work out the disposition of the frame-> For now we are only worried
|
2012-03-28 00:58:04 +00:00
|
|
|
about the crypto matters, and not compression that may be applied
|
|
|
|
before encryption (since applying it after is useless as ciphered
|
|
|
|
text should have maximum entropy). */
|
|
|
|
switch(mdp->packetTypeAndFlags&(MDP_NOCRYPT|MDP_NOSIGN)) {
|
2012-04-13 16:44:41 +00:00
|
|
|
case 0: /* crypted and signed (using CryptoBox authcryption primitive) */
|
|
|
|
frame->modifiers=OF_CRYPTO_SIGNED|OF_CRYPTO_CIPHERED;
|
|
|
|
{
|
2012-11-28 04:35:06 +00:00
|
|
|
int nm=crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES;
|
2012-11-27 06:09:58 +00:00
|
|
|
int zb=crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
|
|
|
|
int nb=crypto_box_curve25519xsalsa20poly1305_NONCEBYTES;
|
|
|
|
int cz=crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
|
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
/* generate plain message with zero bytes and get ready to cipher it */
|
|
|
|
int cipher_len=ob_position(plaintext);
|
|
|
|
|
|
|
|
// TODO, add support for leading zero's in overlay_buffer's, then we don't need to copy the plain text again.
|
|
|
|
unsigned char plain[zb+cipher_len];
|
2012-11-27 06:09:58 +00:00
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
/* zero bytes */
|
|
|
|
bzero(&plain[0],zb);
|
|
|
|
bcopy(ob_ptr(plaintext),&plain[zb],cipher_len);
|
|
|
|
|
2012-12-04 05:49:50 +00:00
|
|
|
cipher_len+=zb;
|
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
ob_free(plaintext);
|
|
|
|
|
|
|
|
frame->payload = ob_new();
|
2012-12-04 05:49:50 +00:00
|
|
|
ob_makespace(frame->payload, nb+cipher_len);
|
|
|
|
|
2012-11-28 04:35:06 +00:00
|
|
|
unsigned char *nonce = ob_append_space(frame->payload, nb);
|
|
|
|
if (!nonce)
|
|
|
|
RETURN(-1);
|
2012-11-27 06:09:58 +00:00
|
|
|
if (urandombytes(nonce,nb)) {
|
2012-05-20 06:36:32 +00:00
|
|
|
op_free(frame);
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(WHY("urandombytes() failed to generate nonce"));
|
2012-05-20 06:36:32 +00:00
|
|
|
}
|
2012-11-28 00:48:17 +00:00
|
|
|
// reserve the high bit of the nonce as a flag for transmitting a shorter nonce.
|
|
|
|
nonce[0]&=0x7f;
|
2012-11-27 06:09:58 +00:00
|
|
|
|
2012-04-13 16:44:41 +00:00
|
|
|
/* get pre-computed PKxSK bytes (the slow part of auth-cryption that can be
|
|
|
|
retained and reused, and use that to do the encryption quickly. */
|
2012-10-05 08:15:30 +00:00
|
|
|
unsigned char *k=keyring_get_nm_bytes(mdp->out.src.sid, mdp->out.dst.sid);
|
2012-08-27 00:34:59 +00:00
|
|
|
if (!k) {
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHY("could not compute Curve25519(NxM)"));
|
|
|
|
}
|
2012-04-13 16:44:41 +00:00
|
|
|
/* Get pointer to place in frame where the ciphered text needs to go */
|
2012-04-14 02:47:46 +00:00
|
|
|
unsigned char *cipher_text=ob_append_space(frame->payload,cipher_len);
|
2012-11-28 04:35:06 +00:00
|
|
|
if ((!cipher_text)){
|
2012-08-27 00:34:59 +00:00
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHY("could not make space for ciphered text"));
|
|
|
|
}
|
2012-04-13 16:44:41 +00:00
|
|
|
/* Actually authcrypt the payload */
|
2012-04-14 02:47:46 +00:00
|
|
|
if (crypto_box_curve25519xsalsa20poly1305_afternm
|
2012-08-27 00:34:59 +00:00
|
|
|
(cipher_text,plain,cipher_len,nonce,k)){
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHY("crypto_box_afternm() failed"));
|
|
|
|
}
|
2012-11-28 04:35:06 +00:00
|
|
|
if (0) {
|
|
|
|
DEBUG("authcrypted mdp frame");
|
|
|
|
dump("nm",k,nm);
|
|
|
|
dump("plain text",plain,sizeof(plain));
|
|
|
|
dump("nonce",nonce,nb);
|
|
|
|
dump("cipher text",cipher_text,cipher_len);
|
|
|
|
}
|
2012-11-27 06:09:58 +00:00
|
|
|
/* now shuffle down to get rid of the temporary space that crypto_box
|
2012-11-28 04:35:06 +00:00
|
|
|
uses.
|
|
|
|
TODO extend overlay buffer so we don't need this.
|
|
|
|
*/
|
2012-11-27 06:09:58 +00:00
|
|
|
bcopy(&cipher_text[cz],&cipher_text[0],cipher_len-cz);
|
|
|
|
frame->payload->position-=cz;
|
2012-11-28 04:35:06 +00:00
|
|
|
if (0){
|
2012-04-14 02:47:46 +00:00
|
|
|
dump("frame",&frame->payload->bytes[0],
|
2012-08-22 00:51:38 +00:00
|
|
|
frame->payload->position);
|
2012-04-14 02:47:46 +00:00
|
|
|
}
|
2012-04-13 16:44:41 +00:00
|
|
|
}
|
2012-03-28 00:58:04 +00:00
|
|
|
break;
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2012-03-28 00:58:04 +00:00
|
|
|
case MDP_NOCRYPT:
|
2012-10-19 04:24:41 +00:00
|
|
|
/* Payload is sent unencrypted, but signed. */
|
2012-11-28 04:35:06 +00:00
|
|
|
frame->modifiers=OF_CRYPTO_SIGNED;
|
|
|
|
frame->payload = plaintext;
|
2012-10-19 04:24:41 +00:00
|
|
|
ob_makespace(frame->payload,SIGNATURE_BYTES);
|
|
|
|
if (crypto_sign_message(frame->source, frame->payload->bytes, frame->payload->allocSize, &frame->payload->position)){
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(-1);
|
2012-04-14 17:47:36 +00:00
|
|
|
}
|
2012-03-28 00:58:04 +00:00
|
|
|
break;
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2012-03-28 00:58:04 +00:00
|
|
|
case MDP_NOSIGN|MDP_NOCRYPT: /* clear text and no signature */
|
|
|
|
frame->modifiers=0;
|
2012-11-28 04:35:06 +00:00
|
|
|
frame->payload = plaintext;
|
2012-03-28 00:58:04 +00:00
|
|
|
break;
|
2012-08-27 00:34:59 +00:00
|
|
|
case MDP_NOSIGN:
|
|
|
|
default:
|
|
|
|
/* ciphered, but not signed.
|
|
|
|
This means we don't use CryptoBox, but rather a more compact means
|
|
|
|
of representing the ciphered stream segment.
|
|
|
|
*/
|
|
|
|
op_free(frame);
|
|
|
|
RETURN(WHY("Not implemented"));
|
2012-04-13 22:45:29 +00:00
|
|
|
}
|
2012-03-28 00:58:04 +00:00
|
|
|
|
2012-10-09 05:39:33 +00:00
|
|
|
frame->queue=mdp->out.queue;
|
|
|
|
if (frame->queue==0)
|
|
|
|
frame->queue = OQ_ORDINARY;
|
|
|
|
|
2012-09-28 01:58:01 +00:00
|
|
|
frame->send_copies = mdp->out.send_copies;
|
|
|
|
|
2012-10-09 05:14:37 +00:00
|
|
|
if (overlay_payload_enqueue(frame))
|
2012-08-27 00:34:59 +00:00
|
|
|
op_free(frame);
|
|
|
|
RETURN(0);
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
static int search_subscribers(struct subscriber *subscriber, void *context){
|
2012-10-03 06:51:06 +00:00
|
|
|
overlay_mdp_addrlist *response = context;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-10-03 06:51:06 +00:00
|
|
|
if (response->mode == MDP_ADDRLIST_MODE_SELF && subscriber->reachable != REACHABLE_SELF){
|
2012-08-15 04:06:10 +00:00
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-08-15 04:06:10 +00:00
|
|
|
|
2012-10-03 06:51:06 +00:00
|
|
|
if (response->mode == MDP_ADDRLIST_MODE_ROUTABLE_PEERS &&
|
2012-11-30 03:15:08 +00:00
|
|
|
(!(subscriber->reachable &REACHABLE))){
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:51:06 +00:00
|
|
|
if (response->mode == MDP_ADDRLIST_MODE_ALL_PEERS &&
|
2012-08-27 00:34:59 +00:00
|
|
|
subscriber->reachable == REACHABLE_SELF){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:51:06 +00:00
|
|
|
if (response->server_sid_count++ >= response->first_sid &&
|
2012-10-08 04:29:25 +00:00
|
|
|
response->frame_sid_count < MDP_MAX_SID_REQUEST) {
|
2012-10-03 06:51:06 +00:00
|
|
|
memcpy(response->sids[response->frame_sid_count++], subscriber->sid, SID_SIZE);
|
2012-08-15 04:06:10 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-15 04:06:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:51:06 +00:00
|
|
|
int overlay_mdp_address_list(overlay_mdp_addrlist *request, overlay_mdp_addrlist *response){
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests)
|
2012-10-03 06:51:06 +00:00
|
|
|
DEBUGF("MDP_GETADDRS first_sid=%u mode=%d",
|
|
|
|
request->first_sid,
|
|
|
|
request->mode
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Prepare reply packet */
|
|
|
|
response->mode = request->mode;
|
|
|
|
response->first_sid = request->first_sid;
|
|
|
|
response->frame_sid_count = 0;
|
|
|
|
|
|
|
|
/* ... and constrain list for sanity */
|
|
|
|
if (response->first_sid<0) response->first_sid=0;
|
|
|
|
|
|
|
|
/* Populate with SIDs */
|
|
|
|
enum_subscribers(NULL, search_subscribers, response);
|
|
|
|
|
|
|
|
response->last_sid = response->first_sid + response->frame_sid_count - 1;
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests)
|
2012-10-03 06:51:06 +00:00
|
|
|
DEBUGF("reply MDP_ADDRLIST first_sid=%u last_sid=%u frame_sid_count=%u server_sid_count=%u",
|
|
|
|
response->first_sid,
|
|
|
|
response->last_sid,
|
|
|
|
response->frame_sid_count,
|
|
|
|
response->server_sid_count
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:10:22 +00:00
|
|
|
struct routing_state{
|
|
|
|
struct sockaddr_un *recvaddr_un;
|
|
|
|
socklen_t recvaddrlen;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int routing_table(struct subscriber *subscriber, void *context){
|
|
|
|
struct routing_state *state = (struct routing_state *)context;
|
|
|
|
overlay_mdp_frame reply;
|
|
|
|
bzero(&reply, sizeof(overlay_mdp_frame));
|
|
|
|
|
|
|
|
struct overlay_route_record *r=(struct overlay_route_record *)&reply.out.payload;
|
|
|
|
reply.packetTypeAndFlags=MDP_TX;
|
|
|
|
reply.out.payload_length=sizeof(struct overlay_route_record);
|
|
|
|
memcpy(r->sid, subscriber->sid, SID_SIZE);
|
|
|
|
r->reachable = subscriber->reachable;
|
|
|
|
if (subscriber->reachable==REACHABLE_INDIRECT && subscriber->next_hop)
|
|
|
|
memcpy(r->neighbour, subscriber->next_hop->sid, SID_SIZE);
|
|
|
|
overlay_mdp_reply(mdp_named.poll.fd, state->recvaddr_un, state->recvaddrlen, &reply);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-05 04:43:47 +00:00
|
|
|
struct scan_state{
|
|
|
|
struct sched_ent alarm;
|
|
|
|
overlay_interface *interface;
|
|
|
|
uint32_t current;
|
|
|
|
uint32_t last;
|
|
|
|
};
|
|
|
|
struct scan_state scans[OVERLAY_MAX_INTERFACES];
|
|
|
|
|
|
|
|
static void overlay_mdp_scan(struct sched_ent *alarm)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr={
|
|
|
|
.sin_family=AF_INET,
|
|
|
|
.sin_port=htons(PORT_DNA),
|
|
|
|
};
|
|
|
|
struct scan_state *state = (struct scan_state *)alarm;
|
2012-12-11 03:01:26 +00:00
|
|
|
uint32_t stop = state->last;
|
|
|
|
if (stop - state->current > 25)
|
|
|
|
stop = state->current+25;
|
2012-12-05 04:43:47 +00:00
|
|
|
|
2012-12-11 03:01:26 +00:00
|
|
|
while(state->current <= stop){
|
2012-12-05 04:43:47 +00:00
|
|
|
addr.sin_addr.s_addr=htonl(state->current);
|
2012-12-08 04:39:41 +00:00
|
|
|
if (addr.sin_addr.s_addr != state->interface->address.sin_addr.s_addr){
|
2012-12-15 23:35:32 +00:00
|
|
|
if (overlay_send_probe(NULL, addr, state->interface, OQ_ORDINARY))
|
2012-12-08 04:39:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-05 04:43:47 +00:00
|
|
|
state->current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->current <= state->last){
|
|
|
|
alarm->alarm=gettime_ms()+500;
|
|
|
|
schedule(alarm);
|
|
|
|
}else{
|
2012-12-08 04:39:41 +00:00
|
|
|
DEBUG("Scan completed");
|
2012-12-05 04:43:47 +00:00
|
|
|
state->interface=NULL;
|
|
|
|
state->current=0;
|
|
|
|
state->last=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void overlay_mdp_poll(struct sched_ent *alarm)
|
2012-03-17 02:32:09 +00:00
|
|
|
{
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & POLLIN) {
|
|
|
|
unsigned char buffer[16384];
|
|
|
|
int ttl;
|
|
|
|
unsigned char recvaddrbuffer[1024];
|
|
|
|
struct sockaddr *recvaddr=(struct sockaddr *)&recvaddrbuffer[0];
|
|
|
|
socklen_t recvaddrlen=sizeof(recvaddrbuffer);
|
|
|
|
struct sockaddr_un *recvaddr_un=NULL;
|
|
|
|
|
|
|
|
ttl=-1;
|
|
|
|
bzero((void *)recvaddrbuffer,sizeof(recvaddrbuffer));
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
ssize_t len = recvwithttl(alarm->poll.fd,buffer,sizeof(buffer),&ttl, recvaddr, &recvaddrlen);
|
|
|
|
recvaddr_un=(struct sockaddr_un *)recvaddr;
|
|
|
|
|
|
|
|
if (len>0) {
|
|
|
|
/* Look at overlay_mdp_frame we have received */
|
|
|
|
overlay_mdp_frame *mdp=(overlay_mdp_frame *)&buffer[0];
|
|
|
|
unsigned int mdp_type = mdp->packetTypeAndFlags & MDP_TYPE_MASK;
|
|
|
|
|
|
|
|
switch (mdp_type) {
|
|
|
|
case MDP_GOODBYE:
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests) DEBUG("MDP_GOODBYE");
|
2012-08-22 05:20:14 +00:00
|
|
|
overlay_mdp_releasebindings(recvaddr_un,recvaddrlen);
|
|
|
|
return;
|
2012-10-03 06:51:06 +00:00
|
|
|
|
|
|
|
/* Deprecated. We can replace with a more generic dump of the routing table */
|
2012-08-22 05:20:14 +00:00
|
|
|
case MDP_NODEINFO:
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests) DEBUG("MDP_NODEINFO");
|
2012-10-03 06:51:06 +00:00
|
|
|
|
|
|
|
if (!overlay_route_node_info(&mdp->nodeinfo))
|
|
|
|
overlay_mdp_reply(mdp_named.poll.fd,recvaddr_un,recvaddrlen,mdp);
|
2012-08-22 05:20:14 +00:00
|
|
|
return;
|
2012-10-03 06:51:06 +00:00
|
|
|
|
2012-11-26 04:10:22 +00:00
|
|
|
case MDP_ROUTING_TABLE:
|
|
|
|
{
|
|
|
|
struct routing_state state={
|
|
|
|
.recvaddr_un=recvaddr_un,
|
|
|
|
.recvaddrlen=recvaddrlen,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum_subscribers(NULL, routing_table, &state);
|
|
|
|
|
|
|
|
}
|
|
|
|
return;
|
2012-12-05 04:43:47 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
case MDP_GETADDRS:
|
|
|
|
{
|
|
|
|
overlay_mdp_frame mdpreply;
|
2012-10-08 04:29:25 +00:00
|
|
|
bzero(&mdpreply, sizeof(overlay_mdp_frame));
|
2012-08-22 05:20:14 +00:00
|
|
|
mdpreply.packetTypeAndFlags = MDP_ADDRLIST;
|
2012-10-03 06:51:06 +00:00
|
|
|
if (!overlay_mdp_address_list(&mdp->addrlist, &mdpreply.addrlist))
|
2012-08-22 05:20:14 +00:00
|
|
|
/* Send back to caller */
|
2012-10-03 06:51:06 +00:00
|
|
|
overlay_mdp_reply(alarm->poll.fd,
|
|
|
|
(struct sockaddr_un *)recvaddr,recvaddrlen,
|
|
|
|
&mdpreply);
|
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
return;
|
2012-03-27 08:55:38 +00:00
|
|
|
}
|
2012-08-22 05:20:14 +00:00
|
|
|
break;
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
case MDP_TX: /* Send payload (and don't treat it as system privileged) */
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests) DEBUG("MDP_TX");
|
2012-12-15 23:35:32 +00:00
|
|
|
|
|
|
|
// Dont allow mdp clients to send very high priority payloads
|
|
|
|
if (mdp->out.queue<=OQ_MESH_MANAGEMENT)
|
|
|
|
mdp->out.queue=OQ_ORDINARY;
|
2012-08-22 05:20:14 +00:00
|
|
|
overlay_mdp_dispatch(mdp,1,(struct sockaddr_un*)recvaddr,recvaddrlen);
|
2012-06-22 03:55:41 +00:00
|
|
|
return;
|
2012-08-22 05:20:14 +00:00
|
|
|
break;
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
case MDP_BIND: /* Bind to port */
|
2012-09-14 02:19:01 +00:00
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.mdprequests) DEBUG("MDP_BIND");
|
2012-09-14 02:19:01 +00:00
|
|
|
|
|
|
|
struct subscriber *subscriber=NULL;
|
|
|
|
/* Make sure source address is either all zeros (listen on all), or a valid
|
|
|
|
local address */
|
|
|
|
|
|
|
|
if (!is_sid_any(mdp->bind.sid)){
|
|
|
|
subscriber = find_subscriber(mdp->bind.sid, SID_SIZE, 0);
|
|
|
|
if ((!subscriber) || subscriber->reachable != REACHABLE_SELF){
|
|
|
|
WHYF("Invalid bind request for sid=%s", alloca_tohex_sid(mdp->bind.sid));
|
|
|
|
/* Source address is invalid */
|
|
|
|
overlay_mdp_reply_error(alarm->poll.fd, recvaddr_un, recvaddrlen, 7,
|
|
|
|
"Bind address is not valid (must be a local MDP address, or all zeroes).");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-09-28 01:58:01 +00:00
|
|
|
if (overlay_mdp_process_bind_request(alarm->poll.fd, subscriber, mdp->bind.port,
|
2012-09-14 02:19:01 +00:00
|
|
|
mdp->packetTypeAndFlags, recvaddr_un, recvaddrlen))
|
|
|
|
overlay_mdp_reply_error(alarm->poll.fd,recvaddr_un,recvaddrlen,3, "Port already in use");
|
|
|
|
else
|
|
|
|
overlay_mdp_reply_ok(alarm->poll.fd,recvaddr_un,recvaddrlen,"Port bound");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 05:20:14 +00:00
|
|
|
break;
|
2012-09-14 02:19:01 +00:00
|
|
|
|
2012-12-05 04:43:47 +00:00
|
|
|
case MDP_SCAN:
|
|
|
|
{
|
|
|
|
struct overlay_mdp_scan *scan = (struct overlay_mdp_scan *)&mdp->raw;
|
|
|
|
time_ms_t start=gettime_ms();
|
|
|
|
|
|
|
|
if (scan->addr.s_addr==0){
|
|
|
|
int i=0;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
// skip any interface that is already being scanned
|
|
|
|
if (scans[i].interface)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct overlay_interface *interface = &overlay_interfaces[i];
|
|
|
|
if (interface->state!=INTERFACE_STATE_UP)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
scans[i].interface = interface;
|
2012-12-06 00:12:34 +00:00
|
|
|
scans[i].current = ntohl(interface->address.sin_addr.s_addr & interface->netmask.s_addr)+1;
|
|
|
|
scans[i].last = ntohl(interface->broadcast_address.sin_addr.s_addr)-1;
|
|
|
|
if (scans[i].last - scans[i].current>0x10000){
|
|
|
|
INFOF("Skipping scan on interface %s as the address space is too large",interface->name);
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-05 04:43:47 +00:00
|
|
|
scans[i].alarm.alarm=start;
|
|
|
|
scans[i].alarm.function=overlay_mdp_scan;
|
|
|
|
start+=100;
|
|
|
|
schedule(&scans[i].alarm);
|
|
|
|
}
|
|
|
|
}else{
|
2012-12-07 05:34:40 +00:00
|
|
|
struct overlay_interface *interface = overlay_interface_find(scan->addr, 1);
|
2012-12-05 04:43:47 +00:00
|
|
|
if (!interface){
|
|
|
|
overlay_mdp_reply_error(alarm->poll.fd,recvaddr_un,recvaddrlen, 1, "Unable to find matching interface");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i = interface - overlay_interfaces;
|
|
|
|
|
|
|
|
if (!scans[i].interface){
|
|
|
|
scans[i].interface = interface;
|
|
|
|
scans[i].current = ntohl(scan->addr.s_addr);
|
|
|
|
scans[i].last = ntohl(scan->addr.s_addr);
|
|
|
|
scans[i].alarm.alarm=start;
|
|
|
|
scans[i].alarm.function=overlay_mdp_scan;
|
|
|
|
schedule(&scans[i].alarm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay_mdp_reply_ok(alarm->poll.fd,recvaddr_un,recvaddrlen,"Scan initiated");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
default:
|
|
|
|
/* Client is not allowed to send any other frame type */
|
|
|
|
WARNF("Unsupported MDP frame type: %d", mdp_type);
|
|
|
|
mdp->packetTypeAndFlags=MDP_ERROR;
|
|
|
|
mdp->error.error=2;
|
|
|
|
snprintf(mdp->error.message,128,"Illegal request type. Clients may use only MDP_TX or MDP_BIND.");
|
|
|
|
int len=4+4+strlen(mdp->error.message)+1;
|
|
|
|
errno=0;
|
|
|
|
/* We ignore the result of the following, because it is just sending an
|
|
|
|
error message back to the client. If this fails, where would we report
|
|
|
|
the error to? My point exactly. */
|
|
|
|
sendto(alarm->poll.fd,mdp,len,0,(struct sockaddr *)recvaddr,recvaddrlen);
|
2012-03-19 05:53:05 +00:00
|
|
|
}
|
2012-03-19 05:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-22 05:20:14 +00:00
|
|
|
|
|
|
|
if (alarm->poll.revents & (POLLHUP | POLLERR)) {
|
|
|
|
INFO("Error on mdp socket");
|
|
|
|
}
|
2012-06-22 03:55:41 +00:00
|
|
|
return;
|
2012-03-19 05:36:34 +00:00
|
|
|
}
|
|
|
|
|