/* Copyright (C) 2005 Henrik Nordstrom
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * The VDE connection setup (open_vde) is loosely based on a similar
 * functions in vdeq.c by Renzo Davoli
 */

#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>	/* the L2 protocols */
#include <sys/ioctl.h>
#include <fcntl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/un.h>
#include <errno.h>

#include <linux/if_tun.h>

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

#define SWITCH_MAGIC 0xfeedface
#define BUFSIZE 2048
#define ETH_ALEN 6

enum request_type {
    REQ_NEW_CONTROL
};

struct request_v3 {
    uint32_t magic;
    uint32_t version;
    enum request_type type;
    struct sockaddr_un sock;
};

static int
open_vde(char *name, int intno, int group)
{
    int pid = getpid();
    struct request_v3 req;
    int fdctl;
    int fddata;
    struct sockaddr_un sock;

    if ((fdctl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
	perror("socket");
	exit(1);
    }
    sock.sun_family = AF_UNIX;
    snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
    if (connect(fdctl, (struct sockaddr *) &sock, sizeof(sock))) {
	perror("connect");
	exit(1);
    }
    memset(&req, 0, sizeof(req));
    req.magic = SWITCH_MAGIC;
    req.version = 3;
    req.type = REQ_NEW_CONTROL + ((group > 0) ? ((geteuid() << 8) + group) << 8 : 0);
    req.sock.sun_family = AF_UNIX;
    sprintf(&req.sock.sun_path[1], "%5d-%2d", pid, intno);

    if ((fddata = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
	perror("socket");
	exit(1);
    }
    if (bind(fddata, (struct sockaddr *) &req.sock, sizeof(req.sock)) < 0) {
	perror("bind");
	exit(1);
    }
    if (send(fdctl, &req, sizeof(req), 0) < 0) {
	perror("send");
	exit(1);
    }
    if (recv(fdctl, &sock, sizeof(struct sockaddr_un), 0) < 0) {
	perror("recv");
	exit(1);
    }
    if (connect(fddata, (struct sockaddr *) &sock, sizeof(struct sockaddr_un)) < 0) {
	perror("connect");
	exit(1);
    }
    /* note: fdctl is intentionally leaked. Closed on exit by the OS. */
    return fddata;
}

static int
get_packet_socket(const char *ifname)
{
    int sock;
    struct ifreq ifr;
    struct packet_mreq mreq;
    struct sockaddr_ll sll;

    sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sock < 0) {
	perror("packet socket: ");
	exit(1);
    }
    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, ifname);
    ioctl(sock, SIOCGIFHWADDR, &ifr);
    if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
	fprintf(stderr, "ERROR: %s is not an ethernet device (%d)\n", ifname, (int) ifr.ifr_hwaddr.sa_family);
	exit(1);
    }
    ioctl(sock, SIOCGIFINDEX, &ifr);

    memset(&sll, 0, sizeof(sll));
    sll.sll_family = AF_PACKET;
    sll.sll_protocol = htons(ETH_P_ALL);
    sll.sll_ifindex = ifr.ifr_ifindex;
    bind(sock, (struct sockaddr *)&sll, sizeof(sll));

    memset(&mreq, 0, sizeof(mreq));
    mreq.mr_ifindex = ifr.ifr_ifindex;
    mreq.mr_type = PACKET_MR_PROMISC;
    setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

    return sock;
}

void
set_nonblocking(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, (fl | O_NONBLOCK));
}

char packet[65536];

static void
relay(fd_set * rfd, int fda, int fdb)
{
    int n;
    union {
	struct sockaddr_ll ll;
	struct sockaddr_un un;
    } sa;
    socklen_t sa_len = sizeof(sa);

    if (FD_ISSET(fda, rfd)) {
	n = recvfrom(fda, packet, sizeof(packet), 0, (struct sockaddr *)&sa, &sa_len);
	if (n > 0)
	    send(fdb, packet, n, 0);
	else if (n == 0)
	    exit(1);
	else if (errno != EINTR) {
	    perror("recv:");
	    exit(1);
	}
    }
}

int
main(int argc, char **argv)
{
    int packet_socket, vde_socket;
    fd_set rfd;
    int fds;

    if (argc != 3) {
	printf("Usage: %s interface vde_socket\n", argv[0]);
	exit(1);
    }
    packet_socket = get_packet_socket(argv[1]);
    vde_socket = open_vde(argv[2], 0, 0);
    set_nonblocking(vde_socket);
    set_nonblocking(packet_socket);

    while (recv(packet_socket, packet, sizeof(packet), 0) > 0);

    fds = packet_socket;
    if (vde_socket > fds)
	fds = vde_socket;
    fds += 1;
    FD_ZERO(&rfd);
    int i;
    for (;;) {
	int n;
	FD_SET(packet_socket, &rfd);
	FD_SET(vde_socket, &rfd);

	n = select(fds, &rfd, NULL, NULL, NULL);
	if (n > 0) {
	    relay(&rfd, packet_socket, vde_socket);
	    relay(&rfd, vde_socket, packet_socket);
	} else if (n < 0 && errno != EINTR) {
	    perror("select:");
	}
    }
}
