tap-vsockd: Use Linux coding style (mostly)

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2016-09-02 12:52:35 +01:00
parent 6df7fae1a9
commit 1a289d04e5
5 changed files with 608 additions and 515 deletions

View File

@ -4,23 +4,25 @@
int parseguid(const char *s, GUID *g) int parseguid(const char *s, GUID *g)
{ {
int res; int res;
int p0, p1, p2, p3, p4, p5, p6, p7; int p0, p1, p2, p3, p4, p5, p6, p7;
res = sscanf(s, GUID_FMT, res = sscanf(s, GUID_FMT,
&g->Data1, &g->Data2, &g->Data3, &g->Data1, &g->Data2, &g->Data3,
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7); &p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
if (res != 11) if (res != 11)
return 1; return 1;
g->Data4[0] = p0;
g->Data4[1] = p1; g->Data4[0] = p0;
g->Data4[2] = p2; g->Data4[1] = p1;
g->Data4[3] = p3; g->Data4[2] = p2;
g->Data4[4] = p4; g->Data4[3] = p3;
g->Data4[5] = p5; g->Data4[4] = p4;
g->Data4[6] = p6; g->Data4[5] = p5;
g->Data4[7] = p7; g->Data4[6] = p6;
return 0; g->Data4[7] = p7;
return 0;
} }
DEFINE_GUID(HV_GUID_ZERO, DEFINE_GUID(HV_GUID_ZERO,
@ -28,7 +30,7 @@ DEFINE_GUID(HV_GUID_ZERO,
DEFINE_GUID(HV_GUID_BROADCAST, DEFINE_GUID(HV_GUID_BROADCAST,
0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); 0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
DEFINE_GUID(HV_GUID_WILDCARD, DEFINE_GUID(HV_GUID_WILDCARD,
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
DEFINE_GUID(HV_GUID_CHILDREN, DEFINE_GUID(HV_GUID_CHILDREN,
0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd); 0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd);

View File

@ -7,10 +7,10 @@
/* GUID handling */ /* GUID handling */
typedef struct _GUID { typedef struct _GUID {
uint32_t Data1; uint32_t Data1;
uint16_t Data2; uint16_t Data2;
uint16_t Data3; uint16_t Data3;
uint8_t Data4[8]; uint8_t Data4[8];
} GUID; } GUID;
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
@ -33,12 +33,11 @@ extern int parseguid(const char *s, GUID *g);
#define AF_HYPERV 43 #define AF_HYPERV 43
#define HV_PROTOCOL_RAW 1 #define HV_PROTOCOL_RAW 1
typedef struct _SOCKADDR_HV typedef struct _SOCKADDR_HV {
{ unsigned short Family;
unsigned short Family; unsigned short Reserved;
unsigned short Reserved; GUID VmId;
GUID VmId; GUID ServiceId;
GUID ServiceId;
} SOCKADDR_HV; } SOCKADDR_HV;
extern const GUID HV_GUID_ZERO; extern const GUID HV_GUID_ZERO;

View File

@ -10,162 +10,215 @@
#include "protocol.h" #include "protocol.h"
/* Version 0 of the protocol used this */ /* Version 0 of the protocol used this */
char expected_hello_old[5] = { 'V', 'M', 'N', 'E', 'T' }; char expected_hello_old[5] = {'V', 'M', 'N', 'E', 'T'};
/* Version 1 and later of the protocol used this */ /* Version 1 and later of the protocol used this */
char expected_hello[5] = { 'V', 'M', 'N', '3', 'T' }; char expected_hello[5] = {'V', 'M', 'N', '3', 'T'};
int really_read(int fd, uint8_t *buffer, size_t total){ int really_read(int fd, uint8_t *buffer, size_t total)
size_t remaining = total; {
ssize_t n;
while (remaining > 0){
n = read(fd, buffer, remaining);
if (n == 0){
syslog(LOG_CRIT, "EOF reading from socket: closing\n");
goto err;
}
if (n < 0){
syslog(LOG_CRIT, "Failure reading from socket: closing: %s", strerror(errno));
goto err;
}
remaining -= (size_t)n;
buffer = buffer + n;
}
return 0;
err:
/* On error: stop reading from the socket and trigger a clean shutdown */
shutdown(fd, SHUT_RD);
return -1;
}
int really_write(int fd, uint8_t *buffer, size_t total){
size_t remaining = total; size_t remaining = total;
ssize_t n; ssize_t n;
while (remaining > 0){
n = write(fd, buffer, remaining); while (remaining > 0) {
if (n == 0){ n = read(fd, buffer, remaining);
syslog(LOG_CRIT, "EOF writing to socket: closing"); if (n == 0) {
goto err; syslog(LOG_CRIT, "EOF reading from socket: closing\n");
} goto err;
if (n < 0){ }
syslog(LOG_CRIT, "Failure writing to socket: closing: %s", strerror(errno)); if (n < 0) {
goto err; syslog(LOG_CRIT,
} "Failure reading from socket: closing: %s",
remaining -= (size_t) n; strerror(errno));
buffer = buffer + n; goto err;
} }
return 0; remaining -= (size_t) n;
buffer = buffer + n;
}
return 0;
err: err:
/* On error: stop listening to the socket */ /*
shutdown(fd, SHUT_WR); * On error: stop reading from the socket and trigger a clean
return -1; * shutdown
*/
shutdown(fd, SHUT_RD);
return -1;
} }
struct init_message *create_init_message(){ int really_write(int fd, uint8_t *buffer, size_t total)
struct init_message *m = (struct init_message*) malloc(sizeof(struct init_message)); {
bzero(m, sizeof(struct init_message)); size_t remaining = total;
memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello)); ssize_t n;
m->version = CURRENT_VERSION;
memset(&m->commit[0], 0, sizeof(m->commit)); while (remaining > 0) {
return m; n = write(fd, buffer, remaining);
if (n == 0) {
syslog(LOG_CRIT, "EOF writing to socket: closing");
goto err;
}
if (n < 0) {
syslog(LOG_CRIT,
"Failure writing to socket: closing: %s",
strerror(errno));
goto err;
}
remaining -= (size_t) n;
buffer = buffer + n;
}
return 0;
err:
/* On error: stop listening to the socket */
shutdown(fd, SHUT_WR);
return -1;
} }
char *print_init_message(struct init_message *m) { struct init_message *create_init_message()
char tmp[41]; {
memcpy(&tmp[0], &m->commit[0], 40); struct init_message *m =
tmp[40] = '\000'; (struct init_message *)malloc(sizeof(struct init_message));
char *buffer = (char*) malloc(80);
int n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp); bzero(m, sizeof(struct init_message));
if (n < 0) { memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello));
perror("Failed to format init_message"); m->version = CURRENT_VERSION;
exit(1); memset(&m->commit[0], 0, sizeof(m->commit));
}
return buffer; return m;
} }
int read_init_message(int fd, struct init_message *ci) { char *print_init_message(struct init_message *m)
bzero(ci, sizeof(struct init_message)); {
if (really_read(fd, (uint8_t*) &ci->hello[0], sizeof(ci->hello)) == -1){ char tmp[41];
syslog(LOG_CRIT, "Failed to read hello from client");
return -1; memcpy(&tmp[0], &m->commit[0], 40);
} tmp[40] = '\000';
if (memcmp(&ci->hello[0], &expected_hello_old[0], sizeof(expected_hello_old)) == 0) { char *buffer = (char *)malloc(80);
ci->version = 0; int n;
return 0;
} n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp);
if (memcmp(&ci->hello[0], &expected_hello[0], sizeof(expected_hello)) != 0) { if (n < 0) {
syslog(LOG_CRIT, "Failed to read header magic from client"); perror("Failed to format init_message");
return -1; exit(1);
} }
if (really_read(fd, (uint8_t*) &ci->version, sizeof(ci->version)) == -1){ return buffer;
syslog(LOG_CRIT, "Failed to read header version from client");
return -1;
}
if (really_read(fd, (uint8_t*) &ci->commit[0], sizeof(ci->commit)) == -1){
syslog(LOG_CRIT, "Failed to read header hash from client");
return -1;
}
return 0;
} }
int write_init_message(int fd, struct init_message *ci) { int read_init_message(int fd, struct init_message *ci)
if (really_write(fd, (uint8_t*) &ci->hello[0], sizeof(ci->hello)) == -1){ {
syslog(LOG_CRIT, "Failed to write hello to client"); int res;
return -1;
} bzero(ci, sizeof(struct init_message));
if (ci->version > 0) {
if (really_write(fd, (uint8_t*) &ci->version, sizeof(ci->version)) == -1){ res = really_read(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
syslog(LOG_CRIT, "Failed to write version to client"); if (res == -1) {
return -1; syslog(LOG_CRIT, "Failed to read hello from client");
} return -1;
if (really_write(fd, (uint8_t*) &ci->commit[0], sizeof(ci->commit)) == -1){ }
syslog(LOG_CRIT, "Failed to write header hash to client");
return -1; res = memcmp(&ci->hello[0],
} &expected_hello_old[0], sizeof(expected_hello_old));
} if (res == 0) {
return 0; ci->version = 0;
return 0;
}
res = memcmp(&ci->hello[0],
&expected_hello[0], sizeof(expected_hello));
if (res != 0) {
syslog(LOG_CRIT, "Failed to read header magic from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->version, sizeof(ci->version));
if (res == -1) {
syslog(LOG_CRIT, "Failed to read header version from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->commit[0], sizeof(ci->commit));
if (res == -1) {
syslog(LOG_CRIT, "Failed to read header hash from client");
return -1;
}
return 0;
} }
int read_vif_info(int fd, struct vif_info *vif) { int write_init_message(int fd, struct init_message *ci)
uint8_t buffer[10]; {
if (really_read(fd, &buffer[0], sizeof(buffer)) == -1){ int res;
syslog(LOG_CRIT, "Failed to read vif info from client");
return -1; res = really_write(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
} if (res == -1) {
vif->mtu = (size_t) (buffer[0] | (buffer[1] << 8)); syslog(LOG_CRIT, "Failed to write hello to client");
vif->max_packet_size = (size_t) (buffer[2] | (buffer[3] << 8)); return -1;
memcpy(vif->mac, &buffer[4], 6); }
return 0; if (ci->version > 0) {
res = really_write(fd, (uint8_t *)&ci->version,
sizeof(ci->version));
if (res == -1) {
syslog(LOG_CRIT, "Failed to write version to client");
return -1;
}
res = really_write(fd, (uint8_t *)&ci->commit[0],
sizeof(ci->commit));
if (res == -1) {
syslog(LOG_CRIT,
"Failed to write header hash to client");
return -1;
}
}
return 0;
}
int read_vif_info(int fd, struct vif_info *vif)
{
uint8_t buffer[10];
if (really_read(fd, &buffer[0], sizeof(buffer)) == -1) {
syslog(LOG_CRIT, "Failed to read vif info from client");
return -1;
}
vif->mtu = (size_t)(buffer[0] | (buffer[1] << 8));
vif->max_packet_size = (size_t)(buffer[2] | (buffer[3] << 8));
memcpy(vif->mac, &buffer[4], 6);
return 0;
} }
int write_vif_info(int fd, struct vif_info *vif) { int write_vif_info(int fd, struct vif_info *vif)
uint8_t buffer[10]; {
buffer[0] = (uint8_t) ((vif->mtu >> 0) & 0xff); uint8_t buffer[10];
buffer[1] = (uint8_t) ((vif->mtu >> 8) & 0xff);
buffer[2] = (uint8_t) ((vif->max_packet_size >> 0) & 0xff); buffer[0] = (uint8_t) ((vif->mtu >> 0) & 0xff);
buffer[3] = (uint8_t) ((vif->max_packet_size >> 8) & 0xff); buffer[1] = (uint8_t) ((vif->mtu >> 8) & 0xff);
memcpy(&buffer[0] + 4, &(vif->mac)[0], 6); buffer[2] = (uint8_t) ((vif->max_packet_size >> 0) & 0xff);
if (really_write(fd, &buffer[0], sizeof(buffer)) == -1){ buffer[3] = (uint8_t) ((vif->max_packet_size >> 8) & 0xff);
syslog(LOG_CRIT, "Failed to write vif into to client"); memcpy(&buffer[0] + 4, &(vif->mac)[0], 6);
return -1;
} if (really_write(fd, &buffer[0], sizeof(buffer)) == -1) {
return 0; syslog(LOG_CRIT, "Failed to write vif into to client");
return -1;
}
return 0;
} }
int write_command(int fd, enum command *c) { int write_command(int fd, enum command *c)
uint8_t command = *c; {
if (really_write(fd, (uint8_t*) &command, sizeof(command)) == -1){ uint8_t command = *c;
syslog(LOG_CRIT, "Failed to write command to client");
return -1; if (really_write(fd, (uint8_t *)&command, sizeof(command)) == -1) {
} syslog(LOG_CRIT, "Failed to write command to client");
return 0; return -1;
}
return 0;
} }
int write_ethernet_args(int fd, struct ethernet_args *args){ int write_ethernet_args(int fd, struct ethernet_args *args)
if (really_write(fd, (uint8_t*) &args->uuid_string[0], 36) == -1){ {
syslog(LOG_CRIT, "Failed to write ethernet args to client"); if (really_write(fd, (uint8_t *)&args->uuid_string[0], 36) == -1) {
return -1; syslog(LOG_CRIT, "Failed to write ethernet args to client");
} return -1;
return 0; }
return 0;
} }

View File

@ -7,14 +7,17 @@
/* Client -> Server init_message */ /* Client -> Server init_message */
/* Server -> Client init_message */ /* Server -> Client init_message */
struct init_message { struct init_message {
char hello[5]; char hello[5];
uint8_t _padding[3]; uint8_t _padding[3];
uint32_t version; uint32_t version;
char commit[40]; /* git sha of the compiled commit */ char commit[40]; /* git sha of the compiled commit */
}; };
/* This should be bumped whenever we add something (like a feature or a bugfix) /*
and we wish the UI to be able to detect when to trigger a reinstall. */ * This should be bumped whenever we add something (like a feature or a
* bugfix) and we wish the UI to be able to detect when to trigger a
* reinstall.
*/
#define CURRENT_VERSION 13 #define CURRENT_VERSION 13
extern struct init_message *create_init_message(void); extern struct init_message *create_init_message(void);
@ -24,24 +27,24 @@ extern char *print_init_message(struct init_message *m);
/* Client -> Server command */ /* Client -> Server command */
enum command { enum command {
ethernet = 1, ethernet = 1,
}; };
extern int write_command(int fd, enum command *c); extern int write_command(int fd, enum command *c);
/* Client -> Server command arguments */ /* Client -> Server command arguments */
struct ethernet_args { struct ethernet_args {
char uuid_string[36]; char uuid_string[36];
}; };
extern int write_ethernet_args(int fd, struct ethernet_args *args); extern int write_ethernet_args(int fd, struct ethernet_args *args);
/* Server -> Client: details of a vif */ /* Server -> Client: details of a vif */
struct vif_info { struct vif_info {
uint8_t mac[6]; uint8_t mac[6];
short _padding; short _padding;
size_t max_packet_size; size_t max_packet_size;
size_t mtu; size_t mtu;
}; };
extern int read_vif_info(int fd, struct vif_info *vif); extern int read_vif_info(int fd, struct vif_info *vif);

View File

@ -1,6 +1,3 @@
/*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -25,421 +22,460 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include "hvsock.h" #include "hvsock.h"
#include "protocol.h" #include "protocol.h"
int daemon_flag = 0; int daemon_flag;
int listen_flag = 0; int listen_flag;
int connect_flag = 0; int connect_flag;
char *default_sid = "30D48B34-7D27-4B0B-AAAF-BBBED334DD59"; char *default_sid = "30D48B34-7D27-4B0B-AAAF-BBBED334DD59";
void fatal(const char *msg) void fatal(const char *msg)
{ {
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno)); syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
exit(1); exit(1);
} }
int alloc_tap(const char *dev) { int alloc_tap(const char *dev)
int fd; {
struct ifreq ifr; const char *clonedev = "/dev/net/tun";
const char *clonedev = "/dev/net/tun"; struct ifreq ifr;
if ((fd = open(clonedev, O_RDWR)) == -1) { int persist = 1;
fatal("Failed to open /dev/net/tun"); int fd;
}
memset(&ifr, 0, sizeof(ifr)); fd = open(clonedev, O_RDWR);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; if (fd == -1)
strncpy(ifr.ifr_name, dev, IFNAMSIZ); fatal("Failed to open /dev/net/tun");
if (ioctl(fd, TUNSETIFF, (void*) &ifr) < 0) {
fatal("TUNSETIFF failed"); memset(&ifr, 0, sizeof(ifr));
} ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
int persist = 1; strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(fd, TUNSETPERSIST, persist) < 0) { if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0)
fatal("TUNSETPERSIST failed"); fatal("TUNSETIFF failed");
}
syslog(LOG_INFO, "successfully created TAP device %s", dev); if (ioctl(fd, TUNSETPERSIST, persist) < 0)
return fd; fatal("TUNSETPERSIST failed");
syslog(LOG_INFO, "successfully created TAP device %s", dev);
return fd;
} }
void set_macaddr(const char *dev, uint8_t *mac) { void set_macaddr(const char *dev, uint8_t *mac)
int fd; {
struct ifreq ifq; struct ifreq ifq;
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0); fd = socket(PF_INET, SOCK_DGRAM, 0);
strcpy(ifq.ifr_name, dev); strcpy(ifq.ifr_name, dev);
memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6); memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6);
ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER; ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
if (ioctl(fd, SIOCSIFHWADDR, &ifq) == -1) { if (ioctl(fd, SIOCSIFHWADDR, &ifq) == -1)
fatal("SIOCSIFHWADDR failed"); fatal("SIOCSIFHWADDR failed");
}
close(fd); close(fd);
} }
/* Negotiate a vmnet connection, returns 0 on success and 1 on error. */ /* Negotiate a vmnet connection, returns 0 on success and 1 on error. */
int negotiate(int fd, struct vif_info *vif) int negotiate(int fd, struct vif_info *vif)
{ {
/* Negotiate with com.docker.slirp */ /* Negotiate with com.docker.slirp */
struct init_message *me = create_init_message(); struct init_message *me = create_init_message();
if (write_init_message(fd, me) == -1) { enum command command = ethernet;
goto err; struct ethernet_args args;
} struct init_message you;
struct init_message you; char *txt;
if (read_init_message(fd, &you) == -1) {
goto err; if (write_init_message(fd, me) == -1)
} goto err;
char *txt = print_init_message(&you);
syslog(LOG_INFO, "Server reports %s", txt); if (read_init_message(fd, &you) == -1)
free(txt); goto err;
enum command command = ethernet;
if (write_command(fd, &command) == -1) { txt = print_init_message(&you);
goto err; syslog(LOG_INFO, "Server reports %s", txt);
} free(txt);
struct ethernet_args args;
/* We don't need a uuid */ if (write_command(fd, &command) == -1)
memset(&args.uuid_string[0], 0, sizeof(args.uuid_string)); goto err;
if (write_ethernet_args(fd, &args) == -1) {
goto err; /* We don't need a uuid */
} memset(&args.uuid_string[0], 0, sizeof(args.uuid_string));
if (read_vif_info(fd, vif) == -1) { if (write_ethernet_args(fd, &args) == -1)
goto err; goto err;
}
return 0; if (read_vif_info(fd, vif) == -1)
goto err;
return 0;
err: err:
syslog(LOG_CRIT, "Failed to negotiate vmnet connection"); syslog(LOG_CRIT, "Failed to negotiate vmnet connection");
return 1; return 1;
} }
/* Argument passed to proxy threads */ /* Argument passed to proxy threads */
struct connection { struct connection {
int fd; /* Hyper-V socket with vmnet protocol */ int fd; /* Hyper-V socket with vmnet protocol */
int tapfd; /* TAP device with ethernet frames */ int tapfd; /* TAP device with ethernet frames */
struct vif_info vif; /* Contains VIF MAC, MTU etc, received from server */ struct vif_info vif; /* Contains MAC, MTU etc, received from server */
}; };
static void* vmnet_to_tap(void *arg) static void *vmnet_to_tap(void *arg)
{ {
int length, n; struct connection *connection = (struct connection *)arg;
struct connection *connection = (struct connection*) arg; uint8_t buffer[2048];
uint8_t header[2]; uint8_t header[2];
uint8_t buffer[2048]; int length, n;
for (;;) { for (;;) {
if (really_read(connection->fd, &header[0], 2) == -1){ if (really_read(connection->fd, &header[0], 2) == -1)
fatal("Failed to read a packet header from host"); fatal("Failed to read a packet header from host");
}
length = (header[0] & 0xff) | ((header[1] & 0xff) << 8); length = (header[0] & 0xff) | ((header[1] & 0xff) << 8);
if (length > sizeof(buffer)) { if (length > sizeof(buffer)) {
syslog(LOG_CRIT, "Received an over-large packet: %d > %ld", length, sizeof(buffer)); syslog(LOG_CRIT,
exit(1); "Received an over-large packet: %d > %ld",
} length, sizeof(buffer));
if (really_read(connection->fd, &buffer[0], length) == -1){ exit(1);
syslog(LOG_CRIT, "Failed to read packet contents from host"); }
exit(1);
} if (really_read(connection->fd, &buffer[0], length) == -1) {
n = write(connection->tapfd, &buffer[0], length); syslog(LOG_CRIT,
if (n != length) { "Failed to read packet contents from host");
syslog(LOG_CRIT, "Failed to write %d bytes to tap device (wrote %d)", length, n); exit(1);
exit(1); }
}
} n = write(connection->tapfd, &buffer[0], length);
if (n != length) {
syslog(LOG_CRIT,
"Failed to write %d bytes to tap device (wrote %d)", length, n);
exit(1);
}
}
} }
static void* tap_to_vmnet(void *arg) static void *tap_to_vmnet(void *arg)
{ {
int length; struct connection *connection = (struct connection *)arg;
struct connection *connection = (struct connection*) arg; uint8_t buffer[2048];
uint8_t header[2]; uint8_t header[2];
uint8_t buffer[2048]; int length;
for (;;) { for (;;) {
length = read(connection->tapfd, &buffer[0], sizeof(buffer)); length = read(connection->tapfd, &buffer[0], sizeof(buffer));
if (length == -1) { if (length == -1) {
if (errno == ENXIO) { if (errno == ENXIO)
fatal("tap device has gone down"); fatal("tap device has gone down");
}
syslog(LOG_WARNING, "ignoring error %d", errno); syslog(LOG_WARNING, "ignoring error %d", errno);
/* This is what mirage-net-unix does. Is it a good idea really? */ /*
continue; * This is what mirage-net-unix does. Is it a good
} * idea really?
header[0] = (length >> 0) & 0xff; */
header[1] = (length >> 8) & 0xff; continue;
if (really_write(connection->fd, &header[0], 2) == -1){ }
fatal("Failed to write packet header");
} header[0] = (length >> 0) & 0xff;
if (really_write(connection->fd, &buffer[0], length) == -1) { header[1] = (length >> 8) & 0xff;
fatal("Failed to write packet body"); if (really_write(connection->fd, &header[0], 2) == -1)
} fatal("Failed to write packet header");
}
return NULL; if (really_write(connection->fd, &buffer[0], length) == -1)
fatal("Failed to write packet body");
}
return NULL;
} }
/* Handle a connection by exchanging ethernet frames forever. /*
* Handle a connection by exchanging ethernet frames forever.
*/ */
static void handle(struct connection *connection) static void handle(struct connection *connection)
{ {
pthread_t v2t, t2v; pthread_t v2t, t2v;
if (pthread_create(&v2t, NULL, vmnet_to_tap, connection) != 0){ if (pthread_create(&v2t, NULL, vmnet_to_tap, connection) != 0)
fatal("Failed to create the vmnet_to_tap thread"); fatal("Failed to create the vmnet_to_tap thread");
}
if (pthread_create(&t2v, NULL, tap_to_vmnet, connection) != 0){ if (pthread_create(&t2v, NULL, tap_to_vmnet, connection) != 0)
fatal("Failed to create the tap_to_vmnet thread"); fatal("Failed to create the tap_to_vmnet thread");
}
if (pthread_join(v2t, NULL) != 0){ if (pthread_join(v2t, NULL) != 0)
fatal("Failed to join the vmnet_to_tap thread"); fatal("Failed to join the vmnet_to_tap thread");
}
if (pthread_join(t2v, NULL) != 0){ if (pthread_join(t2v, NULL) != 0)
fatal("Failed to join the tap_to_vmnet thread"); fatal("Failed to join the tap_to_vmnet thread");
}
} }
static int create_listening_socket(GUID serviceid) { static int create_listening_socket(GUID serviceid)
int lsock = -1; {
SOCKADDR_HV sa; SOCKADDR_HV sa;
int res; int lsock = -1;
int res;
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW); lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
if (lsock == -1) { if (lsock == -1)
fatal("socket()"); fatal("socket()");
}
sa.Family = AF_HYPERV; sa.Family = AF_HYPERV;
sa.Reserved = 0; sa.Reserved = 0;
sa.VmId = HV_GUID_WILDCARD; sa.VmId = HV_GUID_WILDCARD;
sa.ServiceId = serviceid; sa.ServiceId = serviceid;
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa)); res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == -1) { if (res == -1)
fatal("bind()"); fatal("bind()");
}
res = listen(lsock, SOMAXCONN); res = listen(lsock, SOMAXCONN);
if (res == -1) { if (res == -1)
fatal("listen()"); fatal("listen()");
}
return lsock; return lsock;
} }
static int connect_socket(GUID serviceid) { static int connect_socket(GUID serviceid)
int sock = -1; {
SOCKADDR_HV sa; SOCKADDR_HV sa;
int res; int sock = -1;
int res;
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW); sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
if (sock == -1) { if (sock == -1)
fatal("socket()"); fatal("socket()");
}
sa.Family = AF_HYPERV; sa.Family = AF_HYPERV;
sa.Reserved = 0; sa.Reserved = 0;
sa.VmId = HV_GUID_PARENT; sa.VmId = HV_GUID_PARENT;
sa.ServiceId = serviceid; sa.ServiceId = serviceid;
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa)); res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == -1) { if (res == -1)
fatal("connect()"); fatal("connect()");
}
return sock; return sock;
} }
static int accept_socket(int lsock) { static int accept_socket(int lsock)
int csock = -1; {
SOCKADDR_HV sac; SOCKADDR_HV sac;
socklen_t socklen = sizeof(sac); socklen_t socklen = sizeof(sac);
int csock = -1;
csock = accept(lsock, (struct sockaddr *)&sac, &socklen); csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
if (csock == -1) { if (csock == -1)
fatal("accept()"); fatal("accept()");
}
syslog(LOG_INFO, "Connect from: "GUID_FMT":"GUID_FMT"\n", syslog(LOG_INFO, "Connect from: " GUID_FMT ":" GUID_FMT "\n",
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId)); GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
return csock;
return csock;
} }
void write_pidfile(const char *pidfile) { void write_pidfile(const char *pidfile)
pid_t pid = getpid(); {
char * pid_s; pid_t pid = getpid();
FILE *file; char *pid_s;
int len; FILE *file;
int len;
if (asprintf(&pid_s, "%lld", (long long) pid) == -1) { if (asprintf(&pid_s, "%lld", (long long)pid) == -1)
fatal("Failed to allocate pidfile string"); fatal("Failed to allocate pidfile string");
}
len = strlen(pid_s);
file = fopen(pidfile, "w");
if (file == NULL) {
syslog(LOG_CRIT, "Failed to open pidfile %s", pidfile);
exit(1);
}
if (fwrite(pid_s, 1, len, file) != len) { len = strlen(pid_s);
fatal("Failed to write pid to pidfile"); file = fopen(pidfile, "w");
} if (file == NULL) {
fclose(file); syslog(LOG_CRIT, "Failed to open pidfile %s", pidfile);
free(pid_s); exit(1);
}
if (fwrite(pid_s, 1, len, file) != len)
fatal("Failed to write pid to pidfile");
fclose(file);
free(pid_s);
} }
void daemonize(const char *pidfile){ void daemonize(const char *pidfile)
pid_t pid = fork (); {
if (pid == -1) { pid_t pid;
fatal("Failed to fork()"); int null;
}
else if (pid != 0) pid = fork();
exit(0); if (pid == -1)
if (setsid () == -1) { fatal("Failed to fork()");
fatal("Failed to setsid()"); else if (pid != 0)
} exit(0);
if (chdir ("/") == -1) {
fatal("Failed to chdir()"); if (setsid() == -1)
} fatal("Failed to setsid()");
int null = open("/dev/null", O_RDWR);
dup2(null, STDIN_FILENO); if (chdir("/") == -1)
dup2(null, STDOUT_FILENO); fatal("Failed to chdir()");
dup2(null, STDERR_FILENO);
close(null); null = open("/dev/null", O_RDWR);
if (pidfile) write_pidfile(pidfile); dup2(null, STDIN_FILENO);
dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO);
close(null);
if (pidfile)
write_pidfile(pidfile);
} }
void usage(char *name) void usage(char *name)
{ {
printf("%s usage:\n", name); printf("%s usage:\n", name);
printf("\t[--daemon] [--tap <name>] [--serviceid <guid>] [--pid <file>]\n"); printf("\t[--daemon] [--tap <name>] [--serviceid <guid>] [--pid <file>]\n");
printf("\t[--listen | --connect]\n\n"); printf("\t[--listen | --connect]\n\n");
printf("where\n"); printf("where\n");
printf("\t--daemonize: run as a background daemon\n"); printf("\t--daemonize: run as a background daemon\n");
printf("\t--tap <name>: create a tap device with the given name\n"); printf("\t--tap <name>: create a tap device with the given name\n");
printf("\t (defaults to eth1)\n"); printf("\t (defaults to eth1)\n");
printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n"); printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n");
printf("\t (defaults to %s)\n", default_sid); printf("\t (defaults to %s)\n", default_sid);
printf("\t--pid <file>: write a pid to the given file\n"); printf("\t--pid <file>: write a pid to the given file\n");
printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n"); printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n");
printf("\t--connect: connect to the parent partition\n"); printf("\t--connect: connect to the parent partition\n");
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int res = 0; char *serviceid = default_sid;
GUID sid; struct connection connection;
int c; char *tap = "eth1";
/* Defaults to a testing GUID */ char *pidfile = NULL;
char *serviceid = default_sid; int lsocket = -1;
char *tap = "eth1"; int sock = -1;
char *pidfile = NULL; int res = 0;
int status;
pid_t child;
int tapfd;
GUID sid;
int c;
opterr = 0; int option_index;
while (1) { int log_flags = LOG_CONS | LOG_NDELAY;
static struct option long_options[] = { static struct option long_options[] = {
/* These options set a flag. */ /* These options set a flag. */
{"daemon", no_argument, &daemon_flag, 1}, {"daemon", no_argument, &daemon_flag, 1},
{"serviceid", required_argument, NULL, 's'}, {"serviceid", required_argument, NULL, 's'},
{"tap", required_argument, NULL, 't'}, {"tap", required_argument, NULL, 't'},
{"pidfile", required_argument, NULL, 'p'}, {"pidfile", required_argument, NULL, 'p'},
{"listen", no_argument, &listen_flag, 1}, {"listen", no_argument, &listen_flag, 1},
{"connect", no_argument, &connect_flag, 1}, {"connect", no_argument, &connect_flag, 1},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int option_index = 0;
c = getopt_long (argc, argv, "ds:t:p:", long_options, &option_index); opterr = 0;
if (c == -1) break; while (1) {
option_index = 0;
switch (c) { c = getopt_long(argc, argv, "ds:t:p:",
case 'd': long_options, &option_index);
daemon_flag = 1; if (c == -1)
break; break;
case 's':
serviceid = optarg;
break;
case 't':
tap = optarg;
break;
case 'p':
pidfile = optarg;
break;
case 0:
break;
default:
usage (argv[0]);
exit (1);
}
}
if ((listen_flag && connect_flag) || !(listen_flag || connect_flag)){
fprintf(stderr, "Please supply either the --listen or --connect flag, but not both.\n");
exit(1);
}
if (daemon_flag && !pidfile){
fprintf(stderr, "For daemon mode, please supply a --pidfile argument.\n");
exit(1);
}
res = parseguid(serviceid, &sid);
if (res) {
fprintf(stderr, "Failed to parse serviceid as GUID: %s\n", serviceid);
usage(argv[0]);
exit(1);
}
int log_flags = LOG_CONS | LOG_NDELAY; switch (c) {
if (!daemon_flag) { case 'd':
log_flags |= LOG_PERROR; daemon_flag = 1;
} break;
openlog(argv[0], log_flags, LOG_DAEMON); case 's':
serviceid = optarg;
break;
case 't':
tap = optarg;
break;
case 'p':
pidfile = optarg;
break;
case 0:
break;
default:
usage(argv[0]);
exit(1);
}
}
int tapfd = alloc_tap(tap); if ((listen_flag && connect_flag) || !(listen_flag || connect_flag)) {
fprintf(stderr, "Please supply either the --listen or --connect flag, but not both.\n");
exit(1);
}
struct connection connection; if (daemon_flag && !pidfile) {
connection.tapfd = tapfd; fprintf(stderr, "For daemon mode, please supply a --pidfile argument.\n");
exit(1);
}
int sock = -1; res = parseguid(serviceid, &sid);
int lsocket = -1; if (res) {
if (listen_flag) { fprintf(stderr, "Failed to parse serviceid as GUID: %s\n", serviceid);
syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap); usage(argv[0]);
lsocket = create_listening_socket(sid); exit(1);
} else { }
syslog(LOG_INFO, "starting in connect mode with serviceid=%s and tap=%s", serviceid, tap);
}
for (;;) { if (!daemon_flag)
if (sock != -1) { log_flags |= LOG_PERROR;
close(sock);
sock = -1;
}
if (listen_flag) {
sock = accept_socket(lsocket);
} else {
sock = connect_socket(sid);
}
connection.fd = sock; openlog(argv[0], log_flags, LOG_DAEMON);
if (negotiate(sock, &connection.vif) != 0) {
sleep(1);
continue;
}
syslog(LOG_INFO, "VMNET VIF has MAC %02x:%02x:%02x:%02x:%02x:%02x",
connection.vif.mac[0], connection.vif.mac[1], connection.vif.mac[2],
connection.vif.mac[3], connection.vif.mac[4], connection.vif.mac[5]
);
set_macaddr(tap, &connection.vif.mac[0]);
/* Daemonize after we've made our first reliable connection */ tapfd = alloc_tap(tap);
if (daemon_flag) { connection.tapfd = tapfd;
daemon_flag = 0;
daemonize(pidfile); if (listen_flag) {
} syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap);
/* Run the multithreaded part in a subprocess. On error the process will lsocket = create_listening_socket(sid);
exit() which tears down all the threads */ } else {
pid_t child = fork(); syslog(LOG_INFO, "starting in connect mode with serviceid=%s and tap=%s", serviceid, tap);
if (child == 0) { }
handle(&connection);
/* should never happen but just in case of a logic bug in handle */ for (;;) {
exit(1); if (sock != -1) {
} close(sock);
int status; sock = -1;
while (waitpid(child, &status, 0) == -1) { } }
}
if (listen_flag)
sock = accept_socket(lsocket);
else
sock = connect_socket(sid);
connection.fd = sock;
if (negotiate(sock, &connection.vif) != 0) {
sleep(1);
continue;
}
syslog(LOG_INFO, "VMNET VIF has MAC %02x:%02x:%02x:%02x:%02x:%02x",
connection.vif.mac[0], connection.vif.mac[1], connection.vif.mac[2],
connection.vif.mac[3], connection.vif.mac[4], connection.vif.mac[5]
);
set_macaddr(tap, &connection.vif.mac[0]);
/* Daemonize after we've made our first reliable connection */
if (daemon_flag) {
daemon_flag = 0;
daemonize(pidfile);
}
/*
* Run the multithreaded part in a subprocess. On error the
* process will exit() which tears down all the threads
*/
child = fork();
if (child == 0) {
handle(&connection);
/*
* should never happen but just in case of a logic
* bug in handle
*/
exit(1);
}
for (;;) {
if (waitpid(child, &status, 0) != -1)
break;
}
}
} }