2012-03-16 22:30:51 +00:00
|
|
|
/*
|
2013-11-21 02:12:59 +00:00
|
|
|
Serval DNA MDP overlay network
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2012-2013 Serval Project Inc.
|
2013-11-21 02:12:59 +00:00
|
|
|
Copyright (C) 2010-2012 Paul Gardner-Stephen
|
2012-03-16 22:30:51 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 17:38:14 +00:00
|
|
|
/*
|
|
|
|
Portions Copyright (C) 2013 Petter Reinholdtsen
|
|
|
|
Some rights reserved
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-01-15 12:42:58 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
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"
|
2013-09-18 18:43:06 +00:00
|
|
|
#include "strbuf_helpers.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"
|
2013-12-09 07:15:47 +00:00
|
|
|
#include "overlay_interface.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#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"
|
2013-10-16 03:00:00 +00:00
|
|
|
#include "keyring.h"
|
2013-11-25 01:45:20 +00:00
|
|
|
#include "socket.h"
|
2014-05-29 07:11:27 +00:00
|
|
|
#include "server.h"
|
2016-05-09 06:51:46 +00:00
|
|
|
#include "route_link.h"
|
2012-03-17 02:32:09 +00:00
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
uint16_t mdp_loopback_port;
|
|
|
|
|
2013-10-09 05:04:41 +00:00
|
|
|
static void overlay_mdp_poll(struct sched_ent *alarm);
|
|
|
|
static void mdp_poll2(struct sched_ent *alarm);
|
|
|
|
|
2013-09-18 07:06:28 +00:00
|
|
|
static struct profile_total mdp_stats = { .name="overlay_mdp_poll" };
|
2013-10-09 05:04:41 +00:00
|
|
|
static struct sched_ent mdp_sock = {
|
|
|
|
.function = overlay_mdp_poll,
|
|
|
|
.stats = &mdp_stats,
|
2013-10-13 20:55:24 +00:00
|
|
|
.poll={.fd = -1},
|
2013-10-09 05:04:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct profile_total mdp_stats2 = { .name="mdp_poll2" };
|
|
|
|
static struct sched_ent mdp_sock2 = {
|
|
|
|
.function = mdp_poll2,
|
|
|
|
.stats = &mdp_stats2,
|
2013-10-13 20:55:24 +00:00
|
|
|
.poll={.fd = -1},
|
2013-10-09 05:04:41 +00:00
|
|
|
};
|
2014-02-21 06:09:47 +00:00
|
|
|
static struct sched_ent mdp_sock2_inet = {
|
|
|
|
.function = mdp_poll2,
|
|
|
|
.stats = &mdp_stats2,
|
|
|
|
.poll={.fd = -1},
|
|
|
|
};
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
struct mdp_binding{
|
|
|
|
struct mdp_binding *_next;
|
|
|
|
struct subscriber *subscriber;
|
|
|
|
mdp_port_t port;
|
|
|
|
uint8_t version;
|
|
|
|
uint8_t flags;
|
|
|
|
struct socket_address client;
|
|
|
|
time_ms_t binding_time;
|
|
|
|
};
|
|
|
|
|
2016-05-09 06:51:46 +00:00
|
|
|
static struct mdp_binding *mdp_bindings=NULL;
|
|
|
|
static mdp_port_t next_port_binding=256;
|
|
|
|
static struct subscriber internal[0];
|
2016-05-04 06:12:10 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
static int overlay_saw_mdp_frame(
|
|
|
|
struct internal_mdp_header *header,
|
|
|
|
struct overlay_buffer *payload);
|
|
|
|
|
2014-11-11 02:27:15 +00:00
|
|
|
static int mdp_send2(struct __sourceloc, const struct socket_address *client, const struct mdp_header *header,
|
2013-11-25 01:45:20 +00:00
|
|
|
const uint8_t *payload, size_t payload_len);
|
2012-10-17 05:59:40 +00:00
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
static uint8_t has_dead_clients=0;
|
|
|
|
static int mark_dead_client(const struct socket_address *client)
|
|
|
|
{
|
|
|
|
struct mdp_binding *binding = mdp_bindings;
|
|
|
|
while(binding){
|
|
|
|
if (cmp_sockaddr(&binding->client, client)==0){
|
|
|
|
binding->port = 0;
|
|
|
|
has_dead_clients = 1;
|
|
|
|
}
|
|
|
|
binding = binding->_next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int free_dead_clients(){
|
|
|
|
if (!has_dead_clients)
|
|
|
|
return 0;
|
|
|
|
//TODO send dummy frame?
|
|
|
|
struct mdp_binding **binding = &mdp_bindings;
|
|
|
|
while(*binding){
|
|
|
|
struct mdp_binding *b = (*binding);
|
|
|
|
if (b->port==0){
|
|
|
|
(*binding) = b->_next;
|
|
|
|
free(b);
|
|
|
|
}else{
|
|
|
|
binding = &b->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
has_dead_clients=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-09 06:51:46 +00:00
|
|
|
static int mdp_reply2(struct __sourceloc __whence, const struct socket_address *client, const struct mdp_header *header,
|
|
|
|
int flags, const uint8_t *payload, size_t payload_len)
|
|
|
|
{
|
|
|
|
struct mdp_header response_header;
|
|
|
|
bcopy(header, &response_header, sizeof(response_header));
|
|
|
|
response_header.flags = flags;
|
|
|
|
return mdp_send2(__WHENCE__, client, &response_header, payload, payload_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define mdp_reply_error(A,B) mdp_reply2(__WHENCE__,(A),(B),MDP_FLAG_ERROR,NULL,0)
|
|
|
|
#define mdp_reply_ok(A,B) mdp_reply2(__WHENCE__,(A),(B),MDP_FLAG_CLOSE,NULL,0)
|
|
|
|
|
2013-01-15 12:42:58 +00:00
|
|
|
/* Delete all UNIX socket files in instance directory. */
|
2013-12-11 06:11:57 +00:00
|
|
|
void overlay_mdp_clean_socket_files()
|
2013-01-15 12:42:58 +00:00
|
|
|
{
|
2014-03-26 05:05:43 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
if (FORMF_SERVAL_RUN_PATH(path, NULL)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dp;
|
|
|
|
if ((dir = opendir(path)) == NULL) {
|
|
|
|
WARNF_perror("opendir(%s)", alloca_str_toprint(path));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((dp = readdir(dir)) != NULL) {
|
|
|
|
path[0] = '\0';
|
|
|
|
if (!FORMF_SERVAL_RUN_PATH(path, "%s", dp->d_name))
|
|
|
|
continue;
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path, &st)) {
|
|
|
|
WARNF_perror("stat(%s)", alloca_str_toprint(path));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISSOCK(st.st_mode))
|
|
|
|
unlink(path);
|
2013-01-15 12:42:58 +00:00
|
|
|
}
|
2014-03-26 05:05:43 +00:00
|
|
|
closedir(dir);
|
2013-01-15 12:42:58 +00:00
|
|
|
}
|
2015-05-25 02:16:37 +00:00
|
|
|
mdp_loopback_port=0;
|
2013-01-15 12:42:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 04:56:56 +00:00
|
|
|
static void overlay_mdp_fill_legacy(
|
2014-01-23 06:01:56 +00:00
|
|
|
const struct internal_mdp_header *header,
|
|
|
|
struct overlay_buffer *payload,
|
|
|
|
overlay_mdp_frame *mdp)
|
|
|
|
{
|
|
|
|
mdp->out.src.sid = header->source->sid;
|
|
|
|
mdp->out.src.port = header->source_port;
|
|
|
|
mdp->out.dst.sid = header->destination?header->destination->sid:SID_BROADCAST;
|
|
|
|
mdp->out.dst.port = header->destination_port;
|
|
|
|
mdp->out.payload_length = ob_remaining(payload);
|
|
|
|
ob_get_bytes(payload, mdp->out.payload, mdp->out.payload_length);
|
|
|
|
mdp->out.ttl = header->ttl;
|
|
|
|
mdp->out.queue = header->qos;
|
|
|
|
mdp->packetTypeAndFlags=MDP_TX;
|
2014-01-31 01:45:56 +00:00
|
|
|
if (header->crypt_flags & MDP_FLAG_NO_CRYPT)
|
|
|
|
mdp->packetTypeAndFlags|=MDP_NOCRYPT;
|
|
|
|
if (header->crypt_flags & MDP_FLAG_NO_SIGN)
|
|
|
|
mdp->packetTypeAndFlags|=MDP_NOSIGN;
|
2014-01-23 06:01:56 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 05:04:41 +00:00
|
|
|
static int mdp_bind_socket(const char *name)
|
2012-03-16 22:30:51 +00:00
|
|
|
{
|
2013-11-25 01:45:20 +00:00
|
|
|
struct socket_address addr;
|
2013-10-09 05:04:41 +00:00
|
|
|
int sock;
|
2012-03-16 22:30:51 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
if (make_local_sockaddr(&addr, "%s", name) == -1)
|
2013-10-09 05:04:41 +00:00
|
|
|
return -1;
|
2013-11-25 01:45:20 +00:00
|
|
|
if ((sock = esocket(addr.addr.sa_family, SOCK_DGRAM, 0)) == -1)
|
2013-10-09 05:04:41 +00:00
|
|
|
return -1;
|
|
|
|
if (socket_set_reuseaddr(sock, 1) == -1)
|
|
|
|
WARN("Could not set socket to reuse addresses");
|
2014-02-20 04:14:38 +00:00
|
|
|
if (socket_bind(sock, &addr) == -1) {
|
2013-10-09 05:04:41 +00:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
socket_set_rcvbufsize(sock, 64 * 1024);
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
INFOF("Socket %s: fd=%d %s", name, sock, alloca_socket_address(&addr));
|
2013-10-09 05:04:41 +00:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_mdp_setup_sockets()
|
|
|
|
{
|
2013-09-18 07:06:28 +00:00
|
|
|
/* Delete stale socket files from instance directory. */
|
2013-01-15 12:42:58 +00:00
|
|
|
overlay_mdp_clean_socket_files();
|
2013-09-18 07:06:28 +00:00
|
|
|
|
|
|
|
if (mdp_sock.poll.fd == -1) {
|
2013-10-09 05:04:41 +00:00
|
|
|
mdp_sock.poll.fd = mdp_bind_socket("mdp.socket");
|
2014-06-06 06:19:16 +00:00
|
|
|
if (mdp_sock.poll.fd == -1)
|
|
|
|
return -1;
|
2012-07-30 01:49:25 +00:00
|
|
|
mdp_sock.poll.events = POLLIN;
|
|
|
|
watch(&mdp_sock);
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mdp_sock2.poll.fd == -1) {
|
|
|
|
mdp_sock2.poll.fd = mdp_bind_socket("mdp.2.socket");
|
2014-06-06 06:19:16 +00:00
|
|
|
if (mdp_sock2.poll.fd == -1)
|
|
|
|
return -1;
|
2013-10-09 05:04:41 +00:00
|
|
|
mdp_sock2.poll.events = POLLIN;
|
|
|
|
watch(&mdp_sock2);
|
2012-03-16 22:30:51 +00:00
|
|
|
}
|
2014-02-21 06:09:47 +00:00
|
|
|
|
2014-02-24 06:17:16 +00:00
|
|
|
if (mdp_sock2_inet.poll.fd == -1 && config.mdp.enable_inet) {
|
|
|
|
int fd = esocket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd>=0){
|
|
|
|
// try to find a free UDP port somewhere between 4210 & 4260
|
|
|
|
uint16_t start_port = 4210;
|
|
|
|
|
|
|
|
struct socket_address addr;
|
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
addr.inet.sin_family = AF_INET;
|
|
|
|
addr.inet.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
|
|
|
const char *port_str = getenv("SERVAL_MDP_INET_PORT");
|
|
|
|
if (port_str)
|
|
|
|
start_port = atoi(port_str);
|
|
|
|
|
|
|
|
uint16_t end_port = start_port+50;
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
for (port = start_port; port<=end_port; port++){
|
|
|
|
addr.inet.sin_port = htons(port);
|
|
|
|
if (bind(fd, &addr.addr, addr.addrlen)!=-1){
|
2014-02-21 06:09:47 +00:00
|
|
|
mdp_sock2_inet.poll.fd = fd;
|
2014-02-24 06:17:16 +00:00
|
|
|
fd = -1;
|
2014-02-21 06:09:47 +00:00
|
|
|
mdp_sock2_inet.poll.events = POLLIN;
|
|
|
|
watch(&mdp_sock2_inet);
|
2015-05-25 02:16:37 +00:00
|
|
|
mdp_loopback_port = port;
|
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
INFOF("Socket mdp.2.inet: fd=%d %s", fd, alloca_socket_address(&addr));
|
2015-05-25 02:16:37 +00:00
|
|
|
|
2014-02-24 06:17:16 +00:00
|
|
|
break;
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
2014-02-24 06:17:16 +00:00
|
|
|
|
|
|
|
if (errno != EADDRINUSE)
|
|
|
|
WHY_perror("bind");
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
2014-02-24 06:17:16 +00:00
|
|
|
|
2014-06-06 06:19:16 +00:00
|
|
|
if (fd!=-1){
|
2014-02-24 06:17:16 +00:00
|
|
|
close(fd);
|
2014-06-06 06:19:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-16 22:30:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
#define MDP_MAX_SOCKET_NAME_LEN 110
|
2012-08-14 05:01:25 +00:00
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
static int overlay_mdp_reply(int sock, struct socket_address *client,
|
|
|
|
overlay_mdp_frame *mdpreply)
|
|
|
|
{
|
|
|
|
if (!client) return WHY("No reply address");
|
|
|
|
|
|
|
|
ssize_t replylen = overlay_mdp_relevant_bytes(mdpreply);
|
|
|
|
if (replylen<0) return WHY("Invalid MDP frame (could not compute length)");
|
|
|
|
|
|
|
|
ssize_t r=sendto(sock,(char *)mdpreply,replylen,0, &client->addr, client->addrlen);
|
|
|
|
if (r == -1){
|
|
|
|
WHYF_perror("sendto(fd=%d,len=%zu,addr=%s)", sock, (size_t)replylen, alloca_socket_address(client));
|
|
|
|
if (errno == ENOENT){
|
|
|
|
/* far-end of socket has died, so drop binding */
|
|
|
|
INFOF("Closing dead MDP client '%s'", alloca_socket_address(client));
|
2016-05-04 06:12:10 +00:00
|
|
|
mark_dead_client(client);
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r != replylen)
|
|
|
|
return WHYF("sendto() sent %zu bytes of MDP reply (%zu) to %s", (size_t)r, (size_t)replylen, alloca_socket_address(client));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int overlay_mdp_reply_error(int sock, struct socket_address *client,
|
|
|
|
int error_number, char *message)
|
2012-03-20 06:41:58 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
return overlay_mdp_reply(sock, client, &mdpreply);
|
2012-03-27 08:55:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
static int overlay_mdp_reply_ok(int sock, struct socket_address *client,
|
2012-03-20 06:41:58 +00:00
|
|
|
char *message)
|
|
|
|
{
|
2013-11-25 01:45:20 +00:00
|
|
|
return overlay_mdp_reply_error(sock, client, 0, message);
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 07:52:18 +00:00
|
|
|
static int overlay_mdp_process_bind_request(struct subscriber *subscriber, mdp_port_t port,
|
2013-11-25 01:45:20 +00:00
|
|
|
int flags, struct socket_address *client)
|
2012-03-20 06:41:58 +00:00
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Bind request %s:%"PRImdp_port_t, subscriber ? alloca_tohex_sid_t(subscriber->sid) : "NULL", port);
|
2012-09-28 01:58:01 +00:00
|
|
|
|
2013-10-09 08:52:51 +00:00
|
|
|
if (port == 0){
|
2012-09-28 01:58:01 +00:00
|
|
|
return WHYF("Port %d cannot be bound", port);
|
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
|
2012-03-20 06:41:58 +00:00
|
|
|
/* See if binding already exists */
|
2016-05-04 06:12:10 +00:00
|
|
|
struct mdp_binding *b = mdp_bindings;
|
|
|
|
while(b){
|
|
|
|
/* Look for duplicate bindings */
|
|
|
|
if (b->port == port && b->subscriber == subscriber) {
|
|
|
|
if (cmp_sockaddr(&b->client, client)==0) {
|
|
|
|
// this client already owns this port binding?
|
|
|
|
INFO("Identical binding exists");
|
|
|
|
return 0;
|
|
|
|
}else if(flags&MDP_FORCE){
|
|
|
|
// steal the port binding
|
|
|
|
break;
|
|
|
|
}else if((flags & MDP_FLAG_REUSE) && (b->flags & MDP_FLAG_REUSE)){
|
|
|
|
// allow mutliple bindings
|
|
|
|
}else{
|
|
|
|
return WHY("Port already in use");
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
b=b->_next;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
2016-05-04 06:12:10 +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.
|
|
|
|
*/
|
2016-05-04 06:12:10 +00:00
|
|
|
if (!b){
|
|
|
|
b = emalloc_zero(sizeof(struct mdp_binding));
|
|
|
|
b->_next = mdp_bindings;
|
|
|
|
mdp_bindings = b;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
/* Okay, record binding and report success */
|
2016-05-04 06:12:10 +00:00
|
|
|
b->port=port;
|
|
|
|
b->subscriber=subscriber;
|
|
|
|
b->version=0;
|
|
|
|
b->flags = flags & MDP_FLAG_REUSE;
|
|
|
|
b->client.addrlen = client->addrlen;
|
|
|
|
memcpy(&b->client.addr, &client->addr, client->addrlen);
|
|
|
|
b->binding_time=gettime_ms();
|
2012-09-14 02:19:01 +00:00
|
|
|
return 0;
|
2012-03-20 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
static void overlay_mdp_decode_header(struct internal_mdp_header *header, struct overlay_buffer *buff)
|
2012-11-28 04:35:06 +00:00
|
|
|
{
|
|
|
|
/* extract MDP port numbers */
|
2013-10-09 08:52:51 +00:00
|
|
|
mdp_port_t port = ob_get_packed_ui32(buff);
|
|
|
|
int same = port&1;
|
2012-11-28 04:35:06 +00:00
|
|
|
port >>=1;
|
2014-01-23 06:01:56 +00:00
|
|
|
header->destination_port = port;
|
2013-10-09 08:52:51 +00:00
|
|
|
if (!same)
|
2012-11-28 04:35:06 +00:00
|
|
|
port = ob_get_packed_ui32(buff);
|
2014-01-23 06:01:56 +00:00
|
|
|
header->source_port = port;
|
2012-11-28 04:35:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
static struct overlay_buffer *overlay_mdp_decrypt(struct internal_mdp_header *header, struct overlay_buffer *payload)
|
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 */
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *ret=NULL;
|
2014-01-31 01:45:56 +00:00
|
|
|
switch(header->crypt_flags) {
|
|
|
|
case MDP_FLAG_NO_CRYPT|MDP_FLAG_NO_SIGN:
|
2012-10-19 04:24:41 +00:00
|
|
|
/* nothing to do, b already points to the plain text */
|
2014-01-23 06:01:56 +00:00
|
|
|
overlay_mdp_decode_header(header, payload);
|
|
|
|
ret = ob_slice(payload, ob_position(payload), ob_remaining(payload));
|
|
|
|
ob_limitsize(ret, ob_remaining(payload));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-01-31 01:45:56 +00:00
|
|
|
case MDP_FLAG_NO_SIGN:
|
2014-01-23 06:01:56 +00:00
|
|
|
WHY("decryption not implemented");
|
|
|
|
break;
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
case MDP_FLAG_NO_CRYPT:
|
2012-11-28 04:35:06 +00:00
|
|
|
{
|
2016-04-12 06:13:56 +00:00
|
|
|
size_t len = ob_remaining(payload);
|
2014-01-23 06:01:56 +00:00
|
|
|
if (crypto_verify_message(header->source, ob_current_ptr(payload), &len))
|
|
|
|
break;
|
2012-09-10 01:01:01 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
ret = ob_slice(payload, ob_position(payload), len);
|
|
|
|
ob_limitsize(ret, len);
|
|
|
|
overlay_mdp_decode_header(header, ret);
|
|
|
|
break;
|
2012-11-28 04:35:06 +00:00
|
|
|
}
|
2012-10-19 04:24:41 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
case 0:
|
2012-04-14 02:47:46 +00:00
|
|
|
{
|
2016-05-31 03:20:32 +00:00
|
|
|
unsigned char *k=keyring_get_nm_bytes(header->destination->identity->box_sk,
|
|
|
|
header->destination->identity->box_pk,
|
|
|
|
&header->source->sid);
|
2014-01-23 06:01:56 +00:00
|
|
|
if (!k){
|
|
|
|
WHY("I don't have the private key required to decrypt that");
|
|
|
|
break;
|
|
|
|
}
|
2016-04-12 06:13:56 +00:00
|
|
|
|
|
|
|
unsigned char *nonce=ob_get_bytes_ptr(payload, crypto_box_NONCEBYTES);
|
2014-01-23 06:01:56 +00:00
|
|
|
if (!nonce){
|
2016-04-12 06:13:56 +00:00
|
|
|
WHYF("Expected %d bytes of nonce", crypto_box_NONCEBYTES);
|
2014-01-23 06:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 04:35:06 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
int cipher_len=ob_remaining(payload);
|
2016-04-12 06:13:56 +00:00
|
|
|
if (cipher_len < (int)crypto_box_MACBYTES){
|
|
|
|
WHYF("Expected at least %d bytes of cipher text", crypto_box_MACBYTES);
|
2014-01-23 06:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-12 06:13:56 +00:00
|
|
|
unsigned char *cipher_text=ob_get_bytes_ptr(payload, cipher_len);
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *plaintext = ob_new();
|
2016-04-12 06:13:56 +00:00
|
|
|
if (!ob_makespace(plaintext, cipher_len - crypto_box_MACBYTES)){
|
2014-01-23 06:01:56 +00:00
|
|
|
ob_free(plaintext);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-12 06:13:56 +00:00
|
|
|
ob_limitsize(plaintext, cipher_len - crypto_box_MACBYTES);
|
|
|
|
|
|
|
|
if (crypto_box_open_easy_afternm(ob_ptr(plaintext), cipher_text, cipher_len, nonce, k)) {
|
2014-01-23 06:01:56 +00:00
|
|
|
ob_free(plaintext);
|
2016-04-12 06:13:56 +00:00
|
|
|
WHYF("crypto_box_open_easy_afternm() failed (from %s, to %s, len %d)",
|
2014-01-23 06:01:56 +00:00
|
|
|
alloca_tohex_sid_t(header->source->sid), alloca_tohex_sid_t(header->destination->sid), cipher_len);
|
|
|
|
break;
|
2012-06-25 23:52:51 +00:00
|
|
|
}
|
2016-04-12 06:13:56 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
overlay_mdp_decode_header(header, plaintext);
|
|
|
|
ret=plaintext;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-13 22:45:29 +00:00
|
|
|
}
|
2014-01-23 06:01:56 +00:00
|
|
|
|
|
|
|
RETURN(ret);
|
2013-02-16 17:47:24 +00:00
|
|
|
OUT();
|
2012-06-25 23:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 07:52:18 +00:00
|
|
|
int overlay_saw_mdp_containing_frame(struct overlay_frame *f)
|
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));
|
2014-01-23 06:01:56 +00:00
|
|
|
struct internal_mdp_header header;
|
|
|
|
bzero(&header, sizeof header);
|
2012-09-07 02:04:00 +00:00
|
|
|
|
2014-02-05 04:56:56 +00:00
|
|
|
header.qos = mdp.out.queue = f->queue;
|
|
|
|
header.ttl = mdp.out.ttl = f->ttl;
|
2014-01-23 06:01:56 +00:00
|
|
|
header.source = f->source;
|
|
|
|
header.destination = f->destination;
|
|
|
|
header.receive_interface = f->interface;
|
2012-10-09 05:39:33 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
if (!(f->modifiers & OF_CRYPTO_CIPHERED))
|
|
|
|
header.crypt_flags |= MDP_FLAG_NO_CRYPT;
|
|
|
|
if (!(f->modifiers & OF_CRYPTO_SIGNED))
|
|
|
|
header.crypt_flags |= MDP_FLAG_NO_SIGN;
|
|
|
|
|
2012-06-25 23:52:51 +00:00
|
|
|
/* Get source and destination addresses */
|
2014-02-05 04:56:56 +00:00
|
|
|
mdp.out.dst.sid = (f->destination) ? f->destination->sid : SID_BROADCAST;
|
|
|
|
mdp.out.src.sid = f->source->sid;
|
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 */
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *mdp_payload = overlay_mdp_decrypt(&header, f->payload);
|
|
|
|
if (mdp_payload==NULL)
|
2012-09-01 02:47:22 +00:00
|
|
|
RETURN(-1);
|
2014-01-23 06:01:56 +00:00
|
|
|
|
2012-04-13 22:45:29 +00:00
|
|
|
/* and do something with it! */
|
2014-01-23 06:01:56 +00:00
|
|
|
int ret=overlay_saw_mdp_frame(&header, mdp_payload);
|
|
|
|
ob_free(mdp_payload);
|
|
|
|
RETURN(ret);
|
2013-02-16 17:47:24 +00:00
|
|
|
OUT();
|
2012-03-27 08:55:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 01:41:56 +00:00
|
|
|
void mdp_init_response(const struct internal_mdp_header *in, struct internal_mdp_header *out)
|
2012-04-15 20:36:43 +00:00
|
|
|
{
|
2014-02-03 01:41:56 +00:00
|
|
|
out->source = in->destination ? in->destination : my_subscriber;
|
|
|
|
out->source_port = in->destination_port;
|
|
|
|
out->destination = in->source;
|
|
|
|
out->destination_port = in->source_port;
|
|
|
|
out->ttl = 0;
|
|
|
|
out->qos = in->qos;
|
2012-04-15 20:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
static int send_packet_to_client(
|
|
|
|
struct internal_mdp_header *header,
|
|
|
|
struct overlay_buffer *payload,
|
|
|
|
int version,
|
|
|
|
struct socket_address *client){
|
|
|
|
|
|
|
|
switch(version){
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
bzero(&mdp, sizeof mdp);
|
|
|
|
ob_checkpoint(payload);
|
|
|
|
overlay_mdp_fill_legacy(header, payload, &mdp);
|
|
|
|
ob_rewind(payload);
|
|
|
|
|
|
|
|
ssize_t len = overlay_mdp_relevant_bytes(&mdp);
|
|
|
|
if (len < 0)
|
|
|
|
return WHY("unsupported MDP packet type");
|
|
|
|
DEBUGF(mdprequests, "Forwarding packet to client %s", alloca_socket_address(client));
|
|
|
|
ssize_t r = sendto(mdp_sock.poll.fd, &mdp, len, 0, &client->addr, client->addrlen);
|
|
|
|
if (r == -1){
|
|
|
|
WHYF_perror("sendto(fd=%d,len=%zu,addr=%s)", mdp_sock.poll.fd, (size_t)len, alloca_socket_address(client));
|
|
|
|
if (errno == ENOENT){
|
|
|
|
/* far-end of socket has died, so drop binding */
|
|
|
|
INFOF("Closing dead MDP client '%s'", alloca_socket_address(client));
|
|
|
|
mark_dead_client(client);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r != len)
|
|
|
|
WARNF("sendto() sent %zu bytes of MDP reply (%zu) to %s", (size_t)r, (size_t)len, alloca_socket_address(client));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
struct mdp_header client_header;
|
2016-05-09 06:51:46 +00:00
|
|
|
bzero(&client_header, sizeof(client_header));
|
2016-05-04 06:12:10 +00:00
|
|
|
client_header.local.sid=header->destination?header->destination->sid:SID_BROADCAST;
|
|
|
|
client_header.local.port=header->destination_port;
|
|
|
|
client_header.remote.sid=header->source->sid;
|
|
|
|
client_header.remote.port=header->source_port;
|
|
|
|
client_header.qos=header->qos;
|
|
|
|
client_header.ttl=header->ttl;
|
|
|
|
client_header.flags=header->crypt_flags;
|
|
|
|
|
|
|
|
DEBUGF(mdprequests, "Forwarding packet to client v2 %s", alloca_socket_address(client));
|
|
|
|
|
|
|
|
size_t len = ob_remaining(payload);
|
|
|
|
const uint8_t *ptr = ob_get_bytes_ptr(payload, len);
|
|
|
|
|
|
|
|
return mdp_send2(__WHENCE__, client, &client_header, ptr, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
static int overlay_saw_mdp_frame(
|
|
|
|
struct internal_mdp_header *header,
|
|
|
|
struct overlay_buffer *payload)
|
2012-03-16 22:30:51 +00:00
|
|
|
{
|
2014-05-05 05:04:34 +00:00
|
|
|
if (!allow_inbound_packet(header))
|
2016-05-04 06:12:10 +00:00
|
|
|
return 0;
|
2012-03-27 10:43:40 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
/* 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
|
|
|
|
more prudent path.
|
|
|
|
*/
|
2012-06-22 08:00:21 +00:00
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Received packet (MDP ports: src=%s*:%"PRImdp_port_t", dst=%"PRImdp_port_t")",
|
2014-01-23 06:01:56 +00:00
|
|
|
alloca_tohex_sid_t_trunc(header->source->sid, 14),
|
|
|
|
header->source_port, header->destination_port);
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
struct mdp_binding *b = mdp_bindings;
|
|
|
|
|
|
|
|
// first look for an exact subscriber match
|
|
|
|
while(b){
|
|
|
|
if (b->port==header->destination_port && b->subscriber &&
|
|
|
|
((!header->destination) || b->subscriber == header->destination)
|
|
|
|
){
|
|
|
|
/* match */
|
|
|
|
if (send_packet_to_client(header, payload, b->version, &b->client)==0
|
|
|
|
&& header->destination
|
|
|
|
&& (b->flags & MDP_FLAG_REUSE)==0){
|
|
|
|
goto end;
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
2012-09-14 02:19:01 +00:00
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
b=b->_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then look for ANY bindings
|
|
|
|
while(b){
|
|
|
|
if (b->port==header->destination_port && !b->subscriber){
|
|
|
|
/* match */
|
|
|
|
if (send_packet_to_client(header, payload, b->version, &b->client)==0 && (b->flags & MDP_FLAG_REUSE)==0)
|
|
|
|
goto end;
|
2012-03-28 00:58:04 +00:00
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
b=b->_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for a compile time defined internal binding
|
|
|
|
struct internal_binding *binding;
|
|
|
|
for (binding = SECTION_START(bindings); binding < SECTION_END(bindings); ++binding) {
|
|
|
|
if (binding->port == header->destination_port){
|
|
|
|
binding->function(header, payload);
|
|
|
|
goto end;
|
2016-03-07 05:18:23 +00:00
|
|
|
}
|
2012-03-27 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
end:
|
|
|
|
free_dead_clients();
|
|
|
|
return 0;
|
2012-03-16 22:30:51 +00:00
|
|
|
}
|
2012-03-17 02:32:09 +00:00
|
|
|
|
2014-01-31 00:08:52 +00:00
|
|
|
int overlay_mdp_dnalookup_reply(struct subscriber *dest, mdp_port_t dest_port,
|
2014-02-05 04:56:56 +00:00
|
|
|
struct subscriber *resolved_sid, const char *uri, const char *did, const char *name)
|
2012-07-20 08:47:43 +00:00
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_PORT_DNALOOKUP resolved_sid=%s uri=%s did=%s name=%s",
|
|
|
|
alloca_tohex_sid_t(resolved_sid->sid),
|
|
|
|
alloca_str_toprint(uri),
|
|
|
|
alloca_str_toprint(did),
|
|
|
|
alloca_str_toprint(name)
|
2013-11-25 02:22:43 +00:00
|
|
|
);
|
2014-02-05 04:56:56 +00:00
|
|
|
|
|
|
|
struct internal_mdp_header header;
|
|
|
|
bzero(&header, sizeof header);
|
|
|
|
header.qos = OQ_ORDINARY;
|
|
|
|
header.source = resolved_sid;
|
|
|
|
header.source_port = MDP_PORT_DNALOOKUP;
|
|
|
|
header.destination = dest;
|
|
|
|
header.destination_port = dest_port;
|
2014-01-31 00:08:52 +00:00
|
|
|
|
2012-07-20 08:47:43 +00:00
|
|
|
/* build reply as TOKEN|URI|DID|NAME|<NUL> */
|
2014-02-05 04:56:56 +00:00
|
|
|
char buff[256];
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf b = strbuf_local_buf(buff);
|
2014-02-05 04:56:56 +00:00
|
|
|
strbuf_tohex(b, SID_STRLEN, resolved_sid->sid.binary);
|
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");
|
2014-02-05 04:56:56 +00:00
|
|
|
struct overlay_buffer *payload = ob_static((unsigned char*)buff, sizeof buff);
|
|
|
|
ob_limitsize(payload, strlen(buff));
|
|
|
|
int ret = overlay_send_frame(&header, payload);
|
|
|
|
ob_free(payload);
|
|
|
|
return ret;
|
2012-07-20 08:47:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
static int overlay_mdp_check_binding(struct subscriber *subscriber, mdp_port_t port,
|
|
|
|
struct socket_address *client)
|
2012-03-27 09:15:39 +00:00
|
|
|
{
|
2013-02-18 04:01:01 +00:00
|
|
|
/* System generated frames can send anything they want */
|
2013-11-25 01:45:20 +00:00
|
|
|
if (!client)
|
2013-02-18 04:01:01 +00:00
|
|
|
return 0;
|
2012-08-06 07:14:36 +00:00
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
/* Check if this client has bound this sid/port */
|
|
|
|
struct mdp_binding *b = mdp_bindings;
|
|
|
|
while(b){
|
|
|
|
if (b->port == port
|
|
|
|
&& (!b->subscriber || b->subscriber == subscriber)
|
|
|
|
&& cmp_sockaddr(&b->client, client)==0)
|
|
|
|
return 0;
|
|
|
|
b=b->_next;
|
2012-08-06 07:14:36 +00:00
|
|
|
}
|
2012-03-27 09:15:39 +00:00
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
WARNF("No matching binding: addr=%s port=%"PRImdp_port_t,
|
2013-10-09 08:24:21 +00:00
|
|
|
alloca_tohex_sid_t(subscriber->sid),
|
2013-10-09 08:52:51 +00:00
|
|
|
port
|
2012-08-02 07:46:18 +00:00
|
|
|
);
|
2016-05-04 06:12:10 +00:00
|
|
|
return -1;
|
2012-03-27 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
void overlay_mdp_encode_ports(struct overlay_buffer *plaintext, mdp_port_t dst_port, mdp_port_t src_port)
|
2013-10-09 08:52:51 +00:00
|
|
|
{
|
|
|
|
mdp_port_t port = dst_port << 1;
|
|
|
|
if (dst_port == src_port)
|
2012-12-04 04:17:57 +00:00
|
|
|
port |= 1;
|
2013-11-25 06:13:32 +00:00
|
|
|
ob_append_packed_ui32(plaintext, port);
|
|
|
|
if (dst_port != src_port)
|
|
|
|
ob_append_packed_ui32(plaintext, src_port);
|
2012-12-04 04:17:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 04:54:00 +00:00
|
|
|
static int nonce_initialised=0;
|
|
|
|
static uint8_t nonce_buffer[128];
|
|
|
|
|
|
|
|
static int generate_nonce(uint8_t *nonce, size_t bytes)
|
|
|
|
{
|
|
|
|
if (bytes<1||bytes>128) return -1;
|
|
|
|
if (!nonce_initialised) {
|
2016-04-13 06:26:57 +00:00
|
|
|
randombytes_buf(nonce_buffer,128);
|
2014-08-25 04:54:00 +00:00
|
|
|
nonce_initialised=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment nonce
|
|
|
|
unsigned i;
|
|
|
|
for(i=0;i<128;i++){
|
|
|
|
uint8_t b=nonce_buffer[i]+1;
|
|
|
|
nonce_buffer[i]=b;
|
|
|
|
if (b) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcopy(nonce_buffer,nonce,bytes);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-15 00:44:39 +00:00
|
|
|
static struct overlay_buffer * encrypt_payload(
|
|
|
|
struct subscriber *source,
|
|
|
|
struct subscriber *dest,
|
2013-11-25 02:22:43 +00:00
|
|
|
const unsigned char *buffer,
|
2016-04-12 06:13:56 +00:00
|
|
|
size_t msg_len)
|
2013-11-25 02:22:43 +00:00
|
|
|
{
|
2013-08-15 00:44:39 +00:00
|
|
|
struct overlay_buffer *ret = ob_new();
|
2013-11-25 06:13:32 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
return NULL;
|
2013-08-15 00:44:39 +00:00
|
|
|
|
2016-04-12 06:13:56 +00:00
|
|
|
unsigned char *nonce = ob_append_space(ret, msg_len + crypto_box_NONCEBYTES + crypto_box_MACBYTES);
|
2013-08-15 00:44:39 +00:00
|
|
|
if (!nonce){
|
|
|
|
ob_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-12 06:13:56 +00:00
|
|
|
unsigned char *cipher_text = nonce + crypto_box_NONCEBYTES;
|
2013-08-15 00:44:39 +00:00
|
|
|
|
2016-04-12 06:13:56 +00:00
|
|
|
if (generate_nonce(nonce, crypto_box_NONCEBYTES)){
|
2013-08-15 00:44:39 +00:00
|
|
|
ob_free(ret);
|
|
|
|
WHY("generate_nonce() failed to generate nonce");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reserve the high bit of the nonce as a flag for transmitting a shorter nonce.
|
|
|
|
nonce[0]&=0x7f;
|
|
|
|
|
|
|
|
/* 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. */
|
2016-05-31 03:20:32 +00:00
|
|
|
unsigned char *k=keyring_get_nm_bytes(source->identity->box_sk, source->identity->box_pk, &dest->sid);
|
2013-08-15 00:44:39 +00:00
|
|
|
if (!k) {
|
|
|
|
ob_free(ret);
|
|
|
|
WHY("could not compute Curve25519(NxM)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Actually authcrypt the payload */
|
2016-04-12 06:13:56 +00:00
|
|
|
if (crypto_box_easy_afternm(cipher_text, buffer, msg_len, nonce, k)) {
|
2013-08-15 00:44:39 +00:00
|
|
|
ob_free(ret);
|
2016-04-12 06:13:56 +00:00
|
|
|
WHY("crypto_box_easy_afternm() failed");
|
2013-08-15 00:44:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt or sign the plaintext, then queue the frame for transmission.
|
2014-01-31 04:32:35 +00:00
|
|
|
// Note, the position of the payload MUST be at the start of the data, the limit MUST be used to specify the end
|
2014-06-26 07:17:23 +00:00
|
|
|
int _overlay_send_frame(struct __sourceloc whence, struct internal_mdp_header *header, struct overlay_buffer *payload)
|
2013-11-25 01:45:20 +00:00
|
|
|
{
|
2014-01-29 05:09:12 +00:00
|
|
|
if ((!header->destination) || header->destination->reachable == REACHABLE_SELF){
|
|
|
|
ob_checkpoint(payload);
|
|
|
|
overlay_saw_mdp_frame(header, payload);
|
|
|
|
ob_rewind(payload);
|
|
|
|
if (header->destination) {
|
|
|
|
/* Is local, and is not broadcast, so shouldn't get sent out on the wire. */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Local packet, not transmitting");
|
2014-01-29 05:09:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 06:19:24 +00:00
|
|
|
|
2014-05-05 05:04:34 +00:00
|
|
|
if (!allow_outbound_packet(header))
|
2014-04-29 06:01:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
if (header->ttl == 0)
|
|
|
|
header->ttl = PAYLOAD_TTL_DEFAULT;
|
|
|
|
else if (header->ttl > PAYLOAD_TTL_MAX)
|
2013-11-25 01:45:20 +00:00
|
|
|
return WHYF("Invalid TTL");
|
2013-08-15 06:19:24 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
if (header->qos == 0)
|
|
|
|
header->qos = OQ_ORDINARY;
|
2013-08-15 06:19:24 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
if (!header->source)
|
2013-11-25 01:45:20 +00:00
|
|
|
return WHYF("No source specified");
|
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Attempting to queue mdp packet from %s:%d to %s:%d",
|
|
|
|
alloca_tohex_sid_t(header->source->sid), header->source_port,
|
|
|
|
header->destination?alloca_tohex_sid_t(header->destination->sid):"broadcast", header->destination_port);
|
2013-11-28 06:23:12 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
/* Prepare the overlay frame for dispatch */
|
|
|
|
struct overlay_frame *frame = emalloc_zero(sizeof(struct overlay_frame));
|
|
|
|
if (!frame)
|
|
|
|
return -1;
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
frame->source = header->source;
|
|
|
|
frame->destination = header->destination;
|
|
|
|
frame->ttl = header->ttl;
|
|
|
|
frame->queue = header->qos;
|
2013-11-25 01:45:20 +00:00
|
|
|
frame->type = OF_TYPE_DATA;
|
2015-06-15 05:40:08 +00:00
|
|
|
frame->resend = header->resend;
|
2015-06-22 03:00:18 +00:00
|
|
|
frame->send_context = header->send_context;
|
|
|
|
frame->send_hook = header->send_hook;
|
2015-06-15 05:40:08 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
if (!(header->crypt_flags & MDP_FLAG_NO_CRYPT))
|
|
|
|
frame->modifiers |= OF_CRYPTO_CIPHERED;
|
|
|
|
if (!(header->crypt_flags & MDP_FLAG_NO_SIGN))
|
|
|
|
frame->modifiers |= OF_CRYPTO_SIGNED;
|
2013-11-25 01:45:20 +00:00
|
|
|
|
|
|
|
// copy the plain text message into a new buffer, with the wire encoded port numbers
|
|
|
|
struct overlay_buffer *plaintext=ob_new();
|
|
|
|
if (!plaintext){
|
|
|
|
op_free(frame);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
overlay_mdp_encode_ports(plaintext, header->destination_port, header->source_port);
|
|
|
|
if (payload && ob_remaining(payload)){
|
|
|
|
ob_append_bytes(plaintext, ob_current_ptr(payload), ob_remaining(payload));
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ob_overrun(plaintext)) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Frame overrun: position=%zu allocSize=%zu sizeLimit=%zu",
|
|
|
|
plaintext->position, plaintext->allocSize, plaintext->sizeLimit);
|
2013-11-25 01:45:20 +00:00
|
|
|
op_free(frame);
|
|
|
|
ob_free(plaintext);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Send frame %zu bytes", ob_position(plaintext));
|
|
|
|
if (IF_DEBUG(mdprequests) && IF_DEBUG(verbose))
|
|
|
|
dump("Frame plaintext", ob_ptr(plaintext), ob_position(plaintext));
|
2013-11-25 01:45:20 +00:00
|
|
|
|
2013-08-15 00:44:39 +00:00
|
|
|
/* Work out the disposition of the frame-> For now we are only worried
|
|
|
|
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). */
|
2014-01-31 01:45:56 +00:00
|
|
|
switch(header->crypt_flags) {
|
|
|
|
case 0:
|
2013-11-25 01:45:20 +00:00
|
|
|
if (!frame->destination){
|
|
|
|
ob_free(plaintext);
|
|
|
|
op_free(frame);
|
|
|
|
return WHY("Cannot encrypt to broadcast destinations");
|
|
|
|
}
|
|
|
|
|
2013-08-15 00:44:39 +00:00
|
|
|
/* crypted and signed (using CryptoBox authcryption primitive) */
|
|
|
|
frame->payload = encrypt_payload(frame->source, frame->destination, ob_ptr(plaintext), ob_position(plaintext));
|
|
|
|
if (!frame->payload){
|
|
|
|
ob_free(plaintext);
|
|
|
|
op_free(frame);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-25 02:22:43 +00:00
|
|
|
#if 0
|
2015-07-06 08:19:49 +00:00
|
|
|
if (IF_DEBUG(crypto))
|
2013-11-25 02:22:43 +00:00
|
|
|
dump("Frame signed ciphertext", ob_ptr(frame->payload), ob_position(frame->payload));
|
|
|
|
#endif
|
2013-08-15 00:44:39 +00:00
|
|
|
break;
|
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
case MDP_FLAG_NO_CRYPT:
|
2013-08-15 00:44:39 +00:00
|
|
|
// Lets just append some space into the existing payload buffer for the signature, without copying it.
|
|
|
|
frame->payload = plaintext;
|
2013-11-25 02:22:43 +00:00
|
|
|
if ( !ob_makespace(frame->payload, SIGNATURE_BYTES)
|
2016-04-19 05:45:53 +00:00
|
|
|
|| keyring_sign_message(frame->source->identity, ob_ptr(frame->payload), frame->payload->allocSize, &frame->payload->position) == -1
|
2013-11-25 06:13:32 +00:00
|
|
|
) {
|
2013-08-15 00:44:39 +00:00
|
|
|
op_free(frame);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-25 02:22:43 +00:00
|
|
|
#if 0
|
2015-07-06 08:19:49 +00:00
|
|
|
if (IF_DEBUG(crypto))
|
2013-11-25 02:22:43 +00:00
|
|
|
dump("Frame signed plaintext", ob_ptr(frame->payload), ob_position(frame->payload));
|
|
|
|
#endif
|
2013-08-15 00:44:39 +00:00
|
|
|
break;
|
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
case MDP_FLAG_NO_CRYPT|MDP_FLAG_NO_SIGN:
|
2013-08-15 00:44:39 +00:00
|
|
|
/* clear text and no signature */
|
|
|
|
frame->payload = plaintext;
|
|
|
|
break;
|
2013-11-25 01:45:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
ob_free(plaintext);
|
|
|
|
op_free(frame);
|
|
|
|
return WHY("Invalid encrypt / sign combination");
|
2013-08-15 00:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame->destination && frame->ttl>1)
|
|
|
|
overlay_broadcast_generate_address(&frame->broadcast_id);
|
|
|
|
|
2014-06-26 07:17:23 +00:00
|
|
|
if (_overlay_payload_enqueue(whence, frame)){
|
2013-08-15 00:44:39 +00:00
|
|
|
op_free(frame);
|
|
|
|
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()
|
|
|
|
*/
|
2014-02-05 04:56:56 +00:00
|
|
|
static int overlay_mdp_dispatch(overlay_mdp_frame *mdp, struct socket_address *client)
|
2012-03-28 00:58:04 +00:00
|
|
|
{
|
2012-06-25 23:52:51 +00:00
|
|
|
IN();
|
2015-07-06 08:19:49 +00:00
|
|
|
unsigned __d = IF_DEBUG(mdprequests) ? fd_depth() : 0;
|
|
|
|
DEBUGF(mdprequests, "[%u] src=%s*:%"PRImdp_port_t", dst=%s*:%"PRImdp_port_t", recv=%s",
|
2013-11-25 02:22:43 +00:00
|
|
|
__d,
|
|
|
|
alloca_tohex_sid_t_trunc(mdp->out.src.sid, 14), mdp->out.src.port,
|
|
|
|
alloca_tohex_sid_t_trunc(mdp->out.dst.sid, 14), mdp->out.dst.port,
|
2013-11-25 01:45:20 +00:00
|
|
|
client ? alloca_socket_address(client) : "NULL"
|
2013-11-25 02:22:43 +00:00
|
|
|
);
|
2012-09-14 06:20:04 +00:00
|
|
|
|
2013-07-11 02:50:12 +00:00
|
|
|
if (mdp->out.payload_length > sizeof(mdp->out.payload))
|
|
|
|
FATAL("Payload length is past the end of the buffer");
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
struct internal_mdp_header header;
|
|
|
|
bzero(&header, sizeof(header));
|
|
|
|
|
|
|
|
header.source_port = mdp->out.src.port;
|
|
|
|
header.destination_port = mdp->out.dst.port;
|
|
|
|
header.ttl = mdp->out.ttl;
|
|
|
|
header.qos = mdp->out.queue;
|
2012-09-14 06:20:04 +00:00
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
if (is_sid_t_any(mdp->out.src.sid)){
|
2012-09-14 06:20:04 +00:00
|
|
|
/* set source to ourselves */
|
2014-01-23 06:01:56 +00:00
|
|
|
header.source = my_subscriber;
|
|
|
|
mdp->out.src.sid = header.source->sid;
|
2013-10-09 08:24:21 +00:00
|
|
|
}else if (is_sid_t_broadcast(mdp->out.src.sid)){
|
2013-08-15 00:44:39 +00:00
|
|
|
/* Nope, I'm sorry but we simply can't send packets from
|
|
|
|
* broadcast addresses. */
|
2012-09-14 06:20:04 +00:00
|
|
|
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.
|
2014-01-23 06:01:56 +00:00
|
|
|
header.source = find_subscriber(mdp->out.src.sid.binary, SID_SIZE, 0);
|
|
|
|
if (!header.source){
|
2013-10-09 08:24:21 +00:00
|
|
|
RETURN(WHYF("Possible spoofing attempt, tried to send a packet from %s, which is an unknown SID", alloca_tohex_sid_t(mdp->out.src.sid)));
|
2012-09-14 06:20:04 +00:00
|
|
|
}
|
2014-01-23 06:01:56 +00:00
|
|
|
if (header.source->reachable!=REACHABLE_SELF){
|
2013-10-09 08:24:21 +00:00
|
|
|
RETURN(WHYF("Possible spoofing attempt, tried to send a packet from %s", alloca_tohex_sid_t(mdp->out.src.sid)));
|
2012-09-14 06:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
if (overlay_mdp_check_binding(header.source, header.source_port, client)){
|
2012-06-25 23:52:51 +00:00
|
|
|
RETURN(overlay_mdp_reply_error
|
2012-07-30 01:49:25 +00:00
|
|
|
(mdp_sock.poll.fd,
|
2013-11-25 01:45:20 +00:00
|
|
|
client,8,
|
2012-06-25 23:52:51 +00:00
|
|
|
"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
|
|
|
|
2013-08-15 00:44:39 +00:00
|
|
|
/* Work out if destination is broadcast or not */
|
2013-10-09 08:24:21 +00:00
|
|
|
if (is_sid_t_broadcast(mdp->out.dst.sid)){
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "[%u] Broadcast packet", __d);
|
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. */
|
2013-08-15 00:44:39 +00:00
|
|
|
if (!(mdp->packetTypeAndFlags&MDP_NOCRYPT))
|
2012-07-30 01:49:25 +00:00
|
|
|
RETURN(overlay_mdp_reply_error(mdp_sock.poll.fd,
|
2013-11-25 01:45:20 +00:00
|
|
|
client,5,
|
2012-08-27 00:34:59 +00:00
|
|
|
"Broadcast packets cannot be encrypted "));
|
|
|
|
}else{
|
2014-01-23 06:01:56 +00:00
|
|
|
header.destination = find_subscriber(mdp->out.dst.sid.binary, SID_SIZE, 1);
|
2013-08-15 00:44:39 +00:00
|
|
|
// should we reply with an error if the destination is not currently routable?
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-10-10 05:52:31 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
if (header.ttl > PAYLOAD_TTL_MAX) {
|
2013-11-25 01:45:20 +00:00
|
|
|
RETURN(overlay_mdp_reply_error(mdp_sock.poll.fd, client, 9, "TTL out of range"));
|
2013-04-23 06:32:39 +00:00
|
|
|
}
|
2012-03-28 00:58:04 +00:00
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "[%u] destination->sid=%s", __d, header.destination ? alloca_tohex_sid_t(header.destination->sid) : "NULL");
|
2014-01-23 06:01:56 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
if (mdp->packetTypeAndFlags&MDP_NOCRYPT)
|
|
|
|
header.crypt_flags |= MDP_FLAG_NO_CRYPT;
|
|
|
|
if (mdp->packetTypeAndFlags&MDP_NOSIGN)
|
|
|
|
header.crypt_flags |= MDP_FLAG_NO_SIGN;
|
2013-08-15 00:44:39 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *buff = ob_static(mdp->out.payload, mdp->out.payload_length);
|
|
|
|
ob_limitsize(buff, mdp->out.payload_length);
|
|
|
|
|
|
|
|
int ret=overlay_send_frame(&header, buff);
|
|
|
|
|
|
|
|
ob_free(buff);
|
|
|
|
RETURN(ret);
|
2013-02-16 17:47:24 +00:00
|
|
|
OUT();
|
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){
|
2014-02-05 04:56:56 +00:00
|
|
|
struct 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 &&
|
2013-10-09 08:24:21 +00:00
|
|
|
response->frame_sid_count < MDP_MAX_SID_REQUEST)
|
|
|
|
response->sids[response->frame_sid_count++] = subscriber->sid;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-15 04:06:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 05:11:07 +00:00
|
|
|
static int overlay_mdp_address_list(struct overlay_mdp_addrlist *request, struct overlay_mdp_addrlist *response)
|
2013-11-25 02:22:43 +00:00
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_GETADDRS first_sid=%u mode=%d", request->first_sid, request->mode);
|
2012-10-03 06:51:06 +00:00
|
|
|
|
|
|
|
/* Prepare reply packet */
|
|
|
|
response->mode = request->mode;
|
|
|
|
response->first_sid = request->first_sid;
|
|
|
|
response->frame_sid_count = 0;
|
|
|
|
|
|
|
|
/* Populate with SIDs */
|
|
|
|
enum_subscribers(NULL, search_subscribers, response);
|
|
|
|
|
|
|
|
response->last_sid = response->first_sid + response->frame_sid_count - 1;
|
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "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
|
|
|
|
);
|
2012-10-03 06:51:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:10:22 +00:00
|
|
|
struct routing_state{
|
2016-05-09 06:51:46 +00:00
|
|
|
struct mdp_header *header;
|
2013-11-25 01:45:20 +00:00
|
|
|
struct socket_address *client;
|
2012-11-26 04:10:22 +00:00
|
|
|
};
|
|
|
|
|
2016-05-09 06:51:46 +00:00
|
|
|
static void send_route(struct subscriber *subscriber, struct socket_address *client, struct mdp_header *header)
|
|
|
|
{
|
|
|
|
uint8_t payload[MDP_MTU];
|
|
|
|
struct overlay_buffer *b = ob_static(payload, sizeof payload);
|
|
|
|
ob_limitsize(b, sizeof payload);
|
|
|
|
ob_append_bytes(b, subscriber->sid.binary, SID_SIZE);
|
|
|
|
ob_append_byte(b, subscriber->reachable);
|
|
|
|
if (subscriber->reachable & REACHABLE){
|
|
|
|
ob_append_byte(b, subscriber->hop_count);
|
|
|
|
if (subscriber->hop_count>1){
|
|
|
|
ob_append_bytes(b, subscriber->next_hop->sid.binary, SID_SIZE);
|
|
|
|
if (subscriber->hop_count>2){
|
|
|
|
ob_append_bytes(b, subscriber->prior_hop->sid.binary, SID_SIZE);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
ob_append_byte(b, subscriber->destination->interface - overlay_interfaces);
|
|
|
|
ob_append_str(b, subscriber->destination->interface->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!ob_overrun(b));
|
|
|
|
mdp_reply2(__WHENCE__, client, header, 0, payload, ob_position(b));
|
|
|
|
ob_free(b);
|
|
|
|
}
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
static int routing_table(struct subscriber *subscriber, void *context)
|
|
|
|
{
|
2016-05-09 06:51:46 +00:00
|
|
|
if (subscriber->reachable != REACHABLE_NONE){
|
|
|
|
struct routing_state *state = (struct routing_state *)context;
|
|
|
|
send_route(subscriber, state->client, state->header);
|
|
|
|
}
|
2012-11-26 04:10:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-09 06:51:46 +00:00
|
|
|
static void send_route_changed(struct subscriber *subscriber, int UNUSED(prior_reachable)){
|
|
|
|
struct mdp_header header;
|
|
|
|
bzero(&header, sizeof(header));
|
|
|
|
header.local.sid = SID_INTERNAL;
|
|
|
|
header.local.port = MDP_ROUTE_TABLE;
|
|
|
|
header.remote.port = MDP_ROUTE_TABLE;
|
|
|
|
|
|
|
|
struct mdp_binding *b = mdp_bindings;
|
|
|
|
while(b){
|
|
|
|
if (b->port == MDP_ROUTE_TABLE && b->subscriber == internal){
|
|
|
|
send_route(subscriber, &b->client, &header);
|
|
|
|
}
|
|
|
|
b=b->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEFINE_TRIGGER(link_change, send_route_changed);
|
|
|
|
|
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)
|
|
|
|
{
|
2013-12-09 07:15:47 +00:00
|
|
|
struct socket_address addr;
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
addr.inet.sin_family=AF_INET;
|
|
|
|
addr.inet.sin_port=htons(PORT_DNA);
|
|
|
|
|
2012-12-05 04:43:47 +00:00
|
|
|
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){
|
2013-12-09 07:15:47 +00:00
|
|
|
addr.inet.sin_addr.s_addr=htonl(state->current);
|
|
|
|
if (addr.inet.sin_addr.s_addr != state->interface->address.inet.sin_addr.s_addr){
|
|
|
|
struct network_destination *destination = create_unicast_destination(&addr, state->interface);
|
2013-08-08 05:50:31 +00:00
|
|
|
if (!destination)
|
|
|
|
break;
|
|
|
|
int ret = overlay_send_probe(NULL, destination, OQ_ORDINARY);
|
|
|
|
release_destination_ref(destination);
|
|
|
|
if (ret)
|
2012-12-08 04:39:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-05 04:43:47 +00:00
|
|
|
state->current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->current <= state->last){
|
2014-06-04 06:40:36 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
RESCHEDULE(alarm, now+500, now+500, TIME_MS_NEVER_WILL);
|
2012-12-05 04:43:47 +00:00
|
|
|
}else{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUG(mdprequests, "Scan completed");
|
2012-12-05 04:43:47 +00:00
|
|
|
state->interface=NULL;
|
|
|
|
state->current=0;
|
|
|
|
state->last=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
static int mdp_process_identity_request(struct socket_address *client, struct mdp_header *header,
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *payload)
|
2013-10-09 05:04:41 +00:00
|
|
|
{
|
2014-01-23 06:01:56 +00:00
|
|
|
if (ob_remaining(payload)<sizeof(struct mdp_identity_request)){
|
2013-10-10 03:52:20 +00:00
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return WHY("Request too small");
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2014-01-23 06:01:56 +00:00
|
|
|
struct mdp_identity_request request;
|
|
|
|
ob_get_bytes(payload, (uint8_t *)&request, sizeof(request));
|
2013-10-09 05:04:41 +00:00
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
switch(request.action){
|
2013-10-10 03:52:20 +00:00
|
|
|
case ACTION_LOCK:
|
2014-01-23 06:01:56 +00:00
|
|
|
switch (request.type){
|
2013-10-10 03:52:20 +00:00
|
|
|
case TYPE_PIN:
|
|
|
|
{
|
2014-01-23 06:01:56 +00:00
|
|
|
while(1){
|
|
|
|
const char *pin = ob_get_str_ptr(payload);
|
|
|
|
if (!pin)
|
|
|
|
break;
|
2014-10-31 03:13:23 +00:00
|
|
|
keyring_iterator it;
|
|
|
|
keyring_iterator_start(keyring, &it);
|
|
|
|
keyring_next_identity(&it);
|
|
|
|
while(it.identity){
|
|
|
|
if (it.identity->subscriber != my_subscriber && strcmp(it.identity->PKRPin, pin) == 0)
|
|
|
|
keyring_release_identity(&it);
|
|
|
|
else
|
|
|
|
keyring_next_identity(&it);
|
2013-10-10 03:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TYPE_SID:
|
2014-01-23 06:01:56 +00:00
|
|
|
while(1){
|
|
|
|
const sid_t *sid=(const sid_t*)ob_get_bytes_ptr(payload,SID_SIZE);
|
|
|
|
if (sid==NULL)
|
|
|
|
break;
|
|
|
|
keyring_release_subscriber(keyring, sid);
|
2013-10-10 03:52:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return WHY("Unknown request type");
|
|
|
|
}
|
|
|
|
break;
|
2013-10-09 05:04:41 +00:00
|
|
|
case ACTION_UNLOCK:
|
|
|
|
{
|
2014-01-23 06:01:56 +00:00
|
|
|
if (request.type!=TYPE_PIN){
|
2013-10-10 03:52:20 +00:00
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return WHY("Unknown request type");
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2014-09-08 01:55:35 +00:00
|
|
|
unsigned unlock_count=0;
|
2014-01-23 06:01:56 +00:00
|
|
|
while(1){
|
|
|
|
const char *pin = ob_get_str_ptr(payload);
|
|
|
|
if (!pin)
|
|
|
|
break;
|
|
|
|
unlock_count += keyring_enter_pin(keyring, pin);
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2014-09-08 01:55:35 +00:00
|
|
|
if (unlock_count && directory_service)
|
|
|
|
directory_registration();
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-10-10 03:52:20 +00:00
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return WHY("Unknown request action");
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
|
|
|
mdp_reply_ok(client, header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-24 02:01:35 +00:00
|
|
|
// return one response per matching identity
|
2013-11-25 01:45:20 +00:00
|
|
|
static int mdp_search_identities(struct socket_address *client, struct mdp_header *header,
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *payload)
|
2013-10-09 05:04:41 +00:00
|
|
|
{
|
2014-10-31 03:13:23 +00:00
|
|
|
keyring_iterator it;
|
|
|
|
keyring_iterator_start(keyring, &it);
|
|
|
|
|
2013-10-24 02:01:35 +00:00
|
|
|
const char *tag=NULL;
|
|
|
|
const unsigned char *value=NULL;
|
|
|
|
size_t value_len=0;
|
2014-01-23 06:01:56 +00:00
|
|
|
size_t payload_len = ob_remaining(payload);
|
2013-10-24 02:01:35 +00:00
|
|
|
|
|
|
|
if (payload_len){
|
2014-01-23 06:01:56 +00:00
|
|
|
if (keyring_unpack_tag(ob_ptr(payload), payload_len, &tag, &value, &value_len)){
|
2013-10-24 02:01:35 +00:00
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
if (value_len){
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Looking for next %s tag & value", tag);
|
2014-10-31 03:13:23 +00:00
|
|
|
if (!keyring_find_public_tag_value(&it, tag, value, value_len))
|
2013-10-24 02:01:35 +00:00
|
|
|
break;
|
|
|
|
}else if(tag){
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Looking for next %s tag", tag);
|
2014-10-31 03:13:23 +00:00
|
|
|
if (!keyring_find_public_tag(&it, tag, NULL, NULL))
|
2013-10-24 02:01:35 +00:00
|
|
|
break;
|
|
|
|
}else{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Looking for next identity");
|
2014-10-31 03:13:23 +00:00
|
|
|
if (!keyring_next_identity(&it))
|
2013-10-24 02:01:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
unsigned char reply_payload[1200];
|
2013-11-25 01:45:20 +00:00
|
|
|
size_t ofs=0;
|
2013-10-09 05:04:41 +00:00
|
|
|
|
2014-10-31 03:13:23 +00:00
|
|
|
bcopy(it.identity->subscriber->sid.binary, &reply_payload[ofs], sizeof(it.identity->subscriber->sid));
|
|
|
|
ofs+=sizeof(it.identity->subscriber->sid);
|
2013-11-25 01:45:20 +00:00
|
|
|
|
2013-10-24 02:01:35 +00:00
|
|
|
// TODO return other details of this identity
|
2013-11-25 01:45:20 +00:00
|
|
|
|
2014-11-11 02:27:15 +00:00
|
|
|
mdp_reply2(__WHENCE__, client, header, 0, reply_payload, ofs);
|
2013-10-24 02:01:35 +00:00
|
|
|
}
|
|
|
|
mdp_reply_ok(client, header);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 05:11:07 +00:00
|
|
|
const char *external_name="ext"; // TODO?
|
|
|
|
|
|
|
|
int mdp_send_external_packet(struct overlay_interface *interface, struct socket_address *address, const uint8_t *payload, size_t len)
|
|
|
|
{
|
|
|
|
struct mdp_header header;
|
|
|
|
bzero(&header, sizeof header);
|
|
|
|
header.remote.port = MDP_INTERFACE;
|
|
|
|
uint8_t addrlen = address->addrlen;
|
|
|
|
|
|
|
|
struct iovec iov[]={
|
|
|
|
{
|
|
|
|
.iov_base = (void *)&header,
|
|
|
|
.iov_len = sizeof(struct mdp_header)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.iov_base = (void *)&addrlen,
|
|
|
|
.iov_len = sizeof addrlen
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.iov_base = (void *)&address->raw,
|
|
|
|
.iov_len = addrlen
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.iov_base = (void *)payload,
|
|
|
|
.iov_len = len
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct msghdr hdr={
|
|
|
|
.msg_name=&interface->address.addr,
|
|
|
|
.msg_namelen=interface->address.addrlen,
|
|
|
|
.msg_iov=iov,
|
|
|
|
.msg_iovlen=4,
|
|
|
|
};
|
|
|
|
|
|
|
|
int fd=-1;
|
|
|
|
switch(interface->address.addr.sa_family){
|
|
|
|
case AF_UNIX:
|
|
|
|
fd = mdp_sock2.poll.fd;
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
fd = mdp_sock2_inet.poll.fd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fd==-1)
|
|
|
|
return WHYF("Unhandled client family %d", interface->address.addr.sa_family);
|
|
|
|
|
|
|
|
if (sendmsg(fd, &hdr, 0)<0)
|
|
|
|
return WHY_perror("sendmsg");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mdp_interface_packet(struct socket_address *client, struct mdp_header *UNUSED(header),
|
|
|
|
struct overlay_buffer *payload){
|
|
|
|
int msg_type = ob_get(payload);
|
|
|
|
switch (msg_type){
|
|
|
|
case MDP_INTERFACE_UP:{
|
|
|
|
struct config_network_interface ifconfig;
|
|
|
|
cf_dfl_config_network_interface(&ifconfig);
|
|
|
|
|
|
|
|
struct cf_om_node *conf_node = NULL;
|
|
|
|
int result = cf_om_parse(external_name, (char*)ob_current_ptr(payload), ob_remaining(payload), &conf_node);
|
|
|
|
if (result == CFOK || result == CFEMPTY){
|
|
|
|
result = conf_node ? cf_opt_config_network_interface(&ifconfig, conf_node) : CFEMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == CFOK || result == CFEMPTY){
|
|
|
|
if (ifconfig.socket_type != SOCK_EXT){
|
|
|
|
// TODO log nice warning, pick right result code
|
|
|
|
result |= CFSUB(CFUNSUPPORTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == CFOK || result == CFEMPTY){
|
|
|
|
struct overlay_interface *interface=overlay_interface_find_name_addr(external_name, client);
|
|
|
|
if (!interface){
|
|
|
|
overlay_interface_init(external_name, client, NULL, NULL, &ifconfig);
|
|
|
|
}else{
|
|
|
|
if (overlay_interface_configure(interface, &ifconfig)==-1)
|
|
|
|
overlay_interface_close(interface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
case MDP_INTERFACE_DOWN:{
|
|
|
|
struct overlay_interface *interface=overlay_interface_find_name_addr(external_name, client);
|
|
|
|
if (interface)
|
|
|
|
overlay_interface_close(interface);
|
|
|
|
}break;
|
|
|
|
case MDP_INTERFACE_RECV:{
|
|
|
|
struct overlay_interface *interface=overlay_interface_find_name_addr(external_name, client);
|
|
|
|
if (interface){
|
|
|
|
struct socket_address addr;
|
|
|
|
addr.addrlen = ob_get(payload);
|
|
|
|
if (addr.addrlen > sizeof(addr))
|
|
|
|
break; // TODO errors
|
|
|
|
bcopy(ob_get_bytes_ptr(payload, addr.addrlen), addr.raw, addr.addrlen);
|
|
|
|
packetOkOverlay(interface, ob_current_ptr(payload), ob_remaining(payload), &addr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:26:57 +00:00
|
|
|
static mdp_port_t get_next_port(){
|
|
|
|
again:
|
|
|
|
|
|
|
|
if (next_port_binding > 32*1024)
|
2016-05-04 06:12:10 +00:00
|
|
|
// note, we're assuming that there are NO internal port bindings >=256
|
2016-04-13 06:26:57 +00:00
|
|
|
next_port_binding=256;
|
|
|
|
else
|
|
|
|
next_port_binding++;
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
// make sure there are *no* bindings for this port on any SID.
|
|
|
|
struct mdp_binding *b = mdp_bindings;
|
|
|
|
while(b){
|
|
|
|
if (b->port == next_port_binding)
|
2016-04-13 06:26:57 +00:00
|
|
|
goto again;
|
2016-05-04 06:12:10 +00:00
|
|
|
b = b->_next;
|
2016-04-13 06:26:57 +00:00
|
|
|
}
|
|
|
|
return next_port_binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mdp_process_packet(struct socket_address *client, struct mdp_header *header,
|
2014-01-23 06:01:56 +00:00
|
|
|
struct overlay_buffer *payload)
|
2013-10-09 05:04:41 +00:00
|
|
|
{
|
2014-01-23 06:01:56 +00:00
|
|
|
struct internal_mdp_header internal_header;
|
|
|
|
bzero(&internal_header, sizeof(internal_header));
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
if ((header->flags & MDP_FLAG_CLOSE) && header->local.port==0){
|
2016-05-04 06:12:10 +00:00
|
|
|
mark_dead_client(client);
|
|
|
|
free_dead_clients();
|
2013-11-25 01:45:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
// find local sid
|
2016-05-09 06:51:46 +00:00
|
|
|
int sid_type;
|
|
|
|
switch(sid_type=sid_get_special_type(&header->local.sid)){
|
|
|
|
case SID_TYPE_ANY:
|
|
|
|
// leaving the sid blank indicates that we should use our main identity
|
|
|
|
internal_header.source = my_subscriber;
|
|
|
|
header->local.sid = my_subscriber->sid;
|
|
|
|
break;
|
|
|
|
case SID_TYPE_INTERNAL:
|
|
|
|
internal_header.source = internal;
|
|
|
|
header->flags |= MDP_FLAG_REUSE;
|
|
|
|
case SID_TYPE_BROADCAST:
|
|
|
|
// leave source NULL to indicate listening on all local SID's
|
|
|
|
// note that attempting anything else will fail
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// find the matching sid from our keyring
|
|
|
|
internal_header.source = find_subscriber(header->local.sid.binary, sizeof(header->local.sid), 0);
|
|
|
|
if (!internal_header.source || internal_header.source->reachable != REACHABLE_SELF){
|
|
|
|
WHY("Subscriber is not local");
|
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
2016-05-09 06:51:46 +00:00
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
struct mdp_binding **pclient_binding=NULL;
|
|
|
|
struct mdp_binding *client_binding=NULL;
|
|
|
|
struct mdp_binding *conflicting_binding=NULL;
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
// assign the next available port number
|
|
|
|
if (header->local.port==0 && header->flags & MDP_FLAG_BIND){
|
2016-04-13 06:26:57 +00:00
|
|
|
header->local.port=get_next_port();
|
2016-05-04 06:12:10 +00:00
|
|
|
}else{
|
|
|
|
// find existing matching or conflicting bindings
|
|
|
|
struct mdp_binding **binding = &mdp_bindings;
|
|
|
|
while(*binding){
|
|
|
|
struct mdp_binding *b = (*binding);
|
|
|
|
if (b->port == header->local.port
|
|
|
|
&& b->subscriber == internal_header.source){
|
|
|
|
|
|
|
|
if (cmp_sockaddr(&b->client, client)==0){
|
|
|
|
client_binding = b;
|
|
|
|
pclient_binding = binding;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// any conflicting binding will do;
|
|
|
|
conflicting_binding = b;
|
|
|
|
}
|
|
|
|
binding = &b->_next;
|
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 06:01:56 +00:00
|
|
|
internal_header.source_port = header->local.port;
|
|
|
|
internal_header.destination_port = header->remote.port;
|
|
|
|
internal_header.ttl = header->ttl;
|
|
|
|
internal_header.qos = header->qos;
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
if (header->flags & MDP_FLAG_BIND){
|
2016-05-04 06:12:10 +00:00
|
|
|
if (conflicting_binding && (header->flags & MDP_FLAG_REUSE)==0 && (conflicting_binding->flags & MDP_FLAG_REUSE))
|
|
|
|
conflicting_binding = NULL;
|
|
|
|
|
|
|
|
if (conflicting_binding){
|
|
|
|
WHYF("Sorry %s, %s:%u is already bound by %s",
|
|
|
|
alloca_socket_address(client),
|
|
|
|
alloca_tohex_sid_t(header->local.sid),
|
|
|
|
header->local.port,
|
|
|
|
alloca_socket_address(&conflicting_binding->client));
|
2014-05-23 02:27:42 +00:00
|
|
|
mdp_reply_error(client, header);
|
2013-11-25 01:45:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
|
|
|
|
if (!client_binding){
|
|
|
|
DEBUGF(mdprequests, "Bind MDP %s:%d to %s",
|
|
|
|
alloca_tohex_sid_t(header->local.sid),
|
|
|
|
header->local.port,
|
|
|
|
alloca_socket_address(client));
|
|
|
|
client_binding = emalloc_zero(sizeof(struct mdp_binding));
|
|
|
|
if (!client_binding){
|
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// claim binding
|
|
|
|
client_binding->port = header->local.port;
|
|
|
|
client_binding->subscriber = internal_header.source;
|
|
|
|
bcopy(&client->addr, &client_binding->client.addr, client->addrlen);
|
|
|
|
client_binding->client.addrlen = client->addrlen;
|
|
|
|
client_binding->binding_time=gettime_ms();
|
|
|
|
client_binding->version=1;
|
|
|
|
|
|
|
|
client_binding->_next = mdp_bindings;
|
|
|
|
mdp_bindings = client_binding;
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2016-05-04 06:12:10 +00:00
|
|
|
// tell the client that they (still?) have this binding (with flags & MDP_FLAG_BIND still set)
|
2014-11-11 02:27:15 +00:00
|
|
|
mdp_reply2(__WHENCE__, client, header, MDP_FLAG_BIND, NULL, 0);
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_sid_t_any(header->remote.sid)){
|
|
|
|
// process local commands
|
|
|
|
switch(header->remote.port){
|
|
|
|
case MDP_LISTEN:
|
2016-05-04 06:12:10 +00:00
|
|
|
// double check that you have a binding
|
|
|
|
if (!client_binding){
|
|
|
|
WHYF("That port is not bound by you %s",
|
2013-12-20 00:43:00 +00:00
|
|
|
alloca_socket_address(client));
|
2014-05-23 02:27:42 +00:00
|
|
|
mdp_reply_error(client, header);
|
2013-12-20 00:43:00 +00:00
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
break;
|
|
|
|
case MDP_IDENTITY:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Processing MDP_IDENTITY from %s", alloca_socket_address(client));
|
2014-01-23 06:01:56 +00:00
|
|
|
mdp_process_identity_request(client, header, payload);
|
2013-11-25 01:45:20 +00:00
|
|
|
break;
|
|
|
|
// seach unlocked identities
|
|
|
|
case MDP_SEARCH_IDS:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Processing MDP_SEARCH_IDS from %s", alloca_socket_address(client));
|
2014-01-23 06:01:56 +00:00
|
|
|
mdp_search_identities(client, header, payload);
|
2013-11-25 01:45:20 +00:00
|
|
|
break;
|
2014-04-16 09:55:50 +00:00
|
|
|
case MDP_SYNC_CONFIG:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Processing MDP_SYNC_CONFIG from %s", alloca_socket_address(client));
|
2014-04-16 09:55:50 +00:00
|
|
|
server_config_reload(NULL);
|
|
|
|
mdp_reply_ok(client, header);
|
|
|
|
break;
|
2015-03-02 05:11:07 +00:00
|
|
|
case MDP_INTERFACE:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Processing MDP_INTERFACE from %s", alloca_socket_address(client));
|
2015-03-02 05:11:07 +00:00
|
|
|
mdp_interface_packet(client, header, payload);
|
|
|
|
break;
|
2016-05-09 06:51:46 +00:00
|
|
|
case MDP_ROUTE_TABLE:
|
|
|
|
DEBUGF(mdprequests, "Processing MDP_ROUTING_TABLE from %s", alloca_socket_address(client));
|
|
|
|
{
|
|
|
|
struct routing_state state={
|
|
|
|
.client = client,
|
|
|
|
.header = header
|
|
|
|
};
|
|
|
|
enum_subscribers(NULL, routing_table, &state);
|
|
|
|
mdp_reply_ok(client, header);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-25 01:45:20 +00:00
|
|
|
default:
|
|
|
|
WHYF("Unknown command port %d", header->remote.port);
|
2014-05-23 02:27:42 +00:00
|
|
|
mdp_reply_error(client, header);
|
2013-11-25 01:45:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
2016-05-09 06:51:46 +00:00
|
|
|
if (sid_type == SID_TYPE_INTERNAL || sid_type == SID_TYPE_BROADCAST){
|
|
|
|
WHYF("Can't send data packet from a special SID");
|
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
// double check that you have a binding
|
|
|
|
if (!client_binding){
|
|
|
|
WHYF("Can't send data packet, %s:%d is not bound to %s!",
|
2014-09-16 01:52:26 +00:00
|
|
|
alloca_tohex_sid_t(header->local.sid),
|
2016-05-04 06:12:10 +00:00
|
|
|
header->local.port,
|
|
|
|
alloca_socket_address(client));
|
2013-11-25 01:45:20 +00:00
|
|
|
mdp_reply_error(client, header);
|
2013-10-09 05:04:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
if (!is_sid_t_broadcast(header->remote.sid))
|
2014-01-23 06:01:56 +00:00
|
|
|
internal_header.destination = find_subscriber(header->remote.sid.binary, SID_SIZE, 1);
|
2013-10-09 05:04:41 +00:00
|
|
|
|
2014-01-31 01:45:56 +00:00
|
|
|
internal_header.crypt_flags = header->flags & (MDP_FLAG_NO_CRYPT|MDP_FLAG_NO_SIGN);
|
2013-10-09 05:04:41 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
// construct, encrypt, sign and queue the packet
|
|
|
|
if (overlay_send_frame(
|
2014-01-23 06:01:56 +00:00
|
|
|
&internal_header,
|
|
|
|
payload)){
|
2013-11-25 01:45:20 +00:00
|
|
|
mdp_reply_error(client, header);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove binding
|
2016-05-04 06:12:10 +00:00
|
|
|
if (client_binding
|
|
|
|
&& header->flags & MDP_FLAG_CLOSE){
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "Unbind MDP %s:%d from %s",
|
2016-05-04 06:12:10 +00:00
|
|
|
client_binding->subscriber?alloca_tohex_sid_t(client_binding->subscriber->sid):"All",
|
|
|
|
client_binding->port,
|
2015-07-06 08:19:49 +00:00
|
|
|
alloca_socket_address(client));
|
2016-05-04 06:12:10 +00:00
|
|
|
*pclient_binding = client_binding->_next;
|
|
|
|
free(client_binding);
|
2013-11-25 01:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 02:27:15 +00:00
|
|
|
static int mdp_send2(struct __sourceloc __whence, const struct socket_address *client, const struct mdp_header *header,
|
2013-11-25 01:45:20 +00:00
|
|
|
const uint8_t *payload, size_t payload_len)
|
|
|
|
{
|
|
|
|
struct iovec iov[]={
|
|
|
|
{
|
|
|
|
.iov_base = (void *)header,
|
|
|
|
.iov_len = sizeof(struct mdp_header)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.iov_base = (void *)payload,
|
|
|
|
.iov_len = payload_len
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct msghdr hdr={
|
2014-02-20 04:14:38 +00:00
|
|
|
.msg_name=(struct sockaddr*)&client->addr,
|
2013-11-25 01:45:20 +00:00
|
|
|
.msg_namelen=client->addrlen,
|
|
|
|
.msg_iov=iov,
|
|
|
|
.msg_iovlen=2,
|
|
|
|
};
|
|
|
|
|
2014-02-21 06:09:47 +00:00
|
|
|
int fd=-1;
|
|
|
|
switch(client->addr.sa_family){
|
|
|
|
case AF_UNIX:
|
|
|
|
fd = mdp_sock2.poll.fd;
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
fd = mdp_sock2_inet.poll.fd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fd==-1)
|
|
|
|
return WHYF("Unhandled client family %d", client->addr.sa_family);
|
|
|
|
|
2016-05-04 06:12:10 +00:00
|
|
|
if (sendmsg(fd, &hdr, 0)<0){
|
|
|
|
WHY_perror("sendmsg");
|
|
|
|
if (errno == ENOENT){
|
|
|
|
/* far-end of socket has died, so drop binding */
|
|
|
|
INFOF("Closing dead MDP client '%s'", alloca_socket_address(client));
|
|
|
|
mark_dead_client(client);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-21 06:09:47 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mdp_poll2(struct sched_ent *alarm)
|
|
|
|
{
|
|
|
|
if (alarm->poll.revents & POLLIN) {
|
2015-07-20 07:06:51 +00:00
|
|
|
uint8_t payload[1400];
|
2013-11-25 01:45:20 +00:00
|
|
|
struct mdp_header header;
|
|
|
|
struct socket_address client;
|
2014-06-05 05:26:19 +00:00
|
|
|
bzero(&client, sizeof client);
|
2013-11-25 01:45:20 +00:00
|
|
|
client.addrlen=sizeof(client.addr);
|
|
|
|
|
|
|
|
struct iovec iov[]={
|
|
|
|
{
|
|
|
|
.iov_base = (void *)&header,
|
2013-12-10 06:51:35 +00:00
|
|
|
.iov_len = sizeof header
|
2013-11-25 01:45:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.iov_base = (void *)payload,
|
2013-12-10 06:51:35 +00:00
|
|
|
.iov_len = sizeof payload
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct msghdr hdr={
|
|
|
|
.msg_name=&client.addr,
|
|
|
|
.msg_namelen=sizeof(client.store),
|
|
|
|
.msg_iov=iov,
|
|
|
|
.msg_iovlen=2,
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t len = recvmsg(alarm->poll.fd, &hdr, 0);
|
2013-12-10 06:51:35 +00:00
|
|
|
if (len == -1){
|
|
|
|
WHYF_perror("recvmsg(%d,%p,0)", alarm->poll.fd, &hdr);
|
2013-11-25 01:45:20 +00:00
|
|
|
return;
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2013-12-10 06:51:35 +00:00
|
|
|
if ((size_t)len < sizeof header) {
|
2013-12-11 04:05:14 +00:00
|
|
|
WHYF("Expected length %zu, got %zu from %s", sizeof header, (size_t)len, alloca_socket_address(&client));
|
2013-11-25 01:45:20 +00:00
|
|
|
return;
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
|
|
|
|
client.addrlen = hdr.msg_namelen;
|
2013-12-10 06:51:35 +00:00
|
|
|
size_t payload_len = (size_t)(len - sizeof header);
|
2014-01-23 06:01:56 +00:00
|
|
|
|
|
|
|
struct overlay_buffer *buff = ob_static(payload, payload_len);
|
|
|
|
ob_limitsize(buff, payload_len);
|
|
|
|
|
|
|
|
mdp_process_packet(&client, &header, buff);
|
|
|
|
|
|
|
|
ob_free(buff);
|
2013-10-09 05:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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];
|
2016-01-18 17:35:46 +00:00
|
|
|
int ttl = -1;
|
2013-11-25 01:45:20 +00:00
|
|
|
struct socket_address client;
|
2013-11-29 02:26:59 +00:00
|
|
|
client.addrlen = sizeof client.store;
|
2016-01-18 17:35:46 +00:00
|
|
|
ssize_t len = recv_message(alarm->poll.fd, &client, &ttl, buffer, sizeof(buffer));
|
2012-08-22 05:20:14 +00:00
|
|
|
|
2013-12-10 06:51:35 +00:00
|
|
|
if ((size_t)len > 0) {
|
2013-11-25 01:45:20 +00:00
|
|
|
if (client.addrlen <= sizeof(sa_family_t))
|
|
|
|
WHYF("got client.addrlen=%d too short -- ignoring frame len=%zu", (int)client.addrlen, (size_t)len);
|
2013-10-08 03:41:00 +00:00
|
|
|
else {
|
|
|
|
/* 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;
|
2012-08-22 05:20:14 +00:00
|
|
|
|
2013-10-08 03:41:00 +00:00
|
|
|
switch (mdp_type) {
|
|
|
|
case MDP_GOODBYE:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_GOODBYE from %s", alloca_socket_address(&client));
|
2016-05-04 06:12:10 +00:00
|
|
|
mark_dead_client(&client);
|
2012-08-22 05:20:14 +00:00
|
|
|
return;
|
2013-10-08 03:41:00 +00:00
|
|
|
|
|
|
|
case MDP_GETADDRS:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_GETADDRS from %s", alloca_socket_address(&client));
|
2013-10-08 03:41:00 +00:00
|
|
|
{
|
|
|
|
overlay_mdp_frame mdpreply;
|
|
|
|
bzero(&mdpreply, sizeof(overlay_mdp_frame));
|
|
|
|
mdpreply.packetTypeAndFlags = MDP_ADDRLIST;
|
|
|
|
if (!overlay_mdp_address_list(&mdp->addrlist, &mdpreply.addrlist))
|
|
|
|
/* Send back to caller */
|
|
|
|
overlay_mdp_reply(alarm->poll.fd,
|
2013-11-25 01:45:20 +00:00
|
|
|
&client,
|
2013-10-08 03:41:00 +00:00
|
|
|
&mdpreply);
|
2012-12-05 04:43:47 +00:00
|
|
|
|
2013-10-08 03:41:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MDP_TX: /* Send payload (and don't treat it as system privileged) */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_TX from %s", alloca_socket_address(&client));
|
2013-10-08 03:41:00 +00:00
|
|
|
|
|
|
|
// Dont allow mdp clients to send very high priority payloads
|
|
|
|
if (mdp->out.queue<=OQ_MESH_MANAGEMENT)
|
|
|
|
mdp->out.queue=OQ_ORDINARY;
|
2013-11-25 01:45:20 +00:00
|
|
|
overlay_mdp_dispatch(mdp, &client);
|
2013-10-08 03:41:00 +00:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MDP_BIND: /* Bind to port */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_BIND from %s", alloca_socket_address(&client));
|
2013-10-08 03:41:00 +00:00
|
|
|
{
|
|
|
|
struct subscriber *subscriber=NULL;
|
|
|
|
/* Make sure source address is either all zeros (listen on all), or a valid
|
|
|
|
local address */
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
if (!is_sid_t_any(mdp->bind.sid)){
|
|
|
|
subscriber = find_subscriber(mdp->bind.sid.binary, SID_SIZE, 0);
|
2013-10-08 03:41:00 +00:00
|
|
|
if ((!subscriber) || subscriber->reachable != REACHABLE_SELF){
|
2013-10-09 08:24:21 +00:00
|
|
|
WHYF("Invalid bind request for sid=%s", alloca_tohex_sid_t(mdp->bind.sid));
|
2013-10-08 03:41:00 +00:00
|
|
|
/* Source address is invalid */
|
2013-11-25 01:45:20 +00:00
|
|
|
overlay_mdp_reply_error(alarm->poll.fd, &client, 7,
|
2013-10-08 03:41:00 +00:00
|
|
|
"Bind address is not valid (must be a local MDP address, or all zeroes).");
|
|
|
|
return;
|
2012-12-06 00:12:34 +00:00
|
|
|
}
|
2013-10-08 03:41:00 +00:00
|
|
|
|
2012-12-05 04:43:47 +00:00
|
|
|
}
|
2013-12-09 07:52:18 +00:00
|
|
|
if (overlay_mdp_process_bind_request(subscriber, mdp->bind.port,
|
2013-11-25 01:45:20 +00:00
|
|
|
mdp->packetTypeAndFlags, &client))
|
|
|
|
overlay_mdp_reply_error(alarm->poll.fd, &client, 3, "Port already in use");
|
2013-10-08 03:41:00 +00:00
|
|
|
else
|
2013-11-25 01:45:20 +00:00
|
|
|
overlay_mdp_reply_ok(alarm->poll.fd, &client, "Port bound");
|
2013-10-08 03:41:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MDP_SCAN:
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(mdprequests, "MDP_SCAN from %s", alloca_socket_address(&client));
|
2013-10-08 03:41:00 +00:00
|
|
|
{
|
|
|
|
struct overlay_mdp_scan *scan = (struct overlay_mdp_scan *)&mdp->raw;
|
|
|
|
time_ms_t start=gettime_ms();
|
2012-12-05 04:43:47 +00:00
|
|
|
|
2013-10-08 03:41:00 +00:00
|
|
|
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;
|
2013-12-09 07:15:47 +00:00
|
|
|
if (interface->address.addr.sa_family!=AF_INET)
|
|
|
|
continue;
|
2013-10-08 03:41:00 +00:00
|
|
|
scans[i].interface = interface;
|
2013-12-09 07:15:47 +00:00
|
|
|
scans[i].current = ntohl(interface->address.inet.sin_addr.s_addr & ~interface->netmask.s_addr)+1;
|
|
|
|
scans[i].last = ntohl(interface->destination->address.inet.sin_addr.s_addr)-1;
|
2013-10-08 03:41:00 +00:00
|
|
|
if (scans[i].last - scans[i].current>0x10000){
|
2013-12-09 07:15:47 +00:00
|
|
|
INFOF("Skipping scan on interface %s as the address space is too large (%04x %04x)",
|
|
|
|
interface->name, scans[i].last, scans[i].current);
|
2013-10-08 03:41:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
scans[i].alarm.function=overlay_mdp_scan;
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(&scans[i].alarm, start, start, start+500);
|
2013-10-08 03:41:00 +00:00
|
|
|
start+=100;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
struct overlay_interface *interface = overlay_interface_find(scan->addr, 1);
|
|
|
|
if (!interface){
|
2013-11-25 01:45:20 +00:00
|
|
|
overlay_mdp_reply_error(alarm->poll.fd, &client, 1, "Unable to find matching interface");
|
2013-10-08 03:41:00 +00:00
|
|
|
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.function=overlay_mdp_scan;
|
2014-06-04 06:40:36 +00:00
|
|
|
RESCHEDULE(&scans[i].alarm, start, start, start+500);
|
2013-10-08 03:41:00 +00:00
|
|
|
}
|
2012-12-05 04:43:47 +00:00
|
|
|
}
|
2013-10-08 03:41:00 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
overlay_mdp_reply_ok(alarm->poll.fd, &client, "Scan initiated");
|
2012-12-05 04:43:47 +00:00
|
|
|
}
|
2013-10-08 03:41:00 +00:00
|
|
|
break;
|
2012-12-05 04:43:47 +00:00
|
|
|
|
2013-10-08 03:41:00 +00:00
|
|
|
default:
|
|
|
|
/* Client is not allowed to send any other frame type */
|
2013-11-25 01:45:20 +00:00
|
|
|
WARNF("Unsupported MDP frame type [%d] from %s", mdp_type, alloca_socket_address(&client));
|
|
|
|
overlay_mdp_reply_error(alarm->poll.fd, &client, 2, "Illegal request type. Clients may use only MDP_TX or MDP_BIND.");
|
2012-12-05 04:43:47 +00:00
|
|
|
}
|
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
|
|
|
}
|