daemon_user.c 4.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
J
Jeff Dike 已提交
2 3
 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
L
Linus Torvalds 已提交
4 5 6 7 8 9
 * James Leu (jleu@mindspring.net).
 * Copyright (C) 2001 by various other people who didn't put their name here.
 * Licensed under the GPL.
 */

#include <stdint.h>
J
Jeff Dike 已提交
10 11 12
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
L
Linus Torvalds 已提交
13 14
#include <sys/socket.h>
#include <sys/time.h>
J
Jeff Dike 已提交
15
#include <sys/un.h>
L
Linus Torvalds 已提交
16
#include "daemon.h"
17 18 19
#include <net_user.h>
#include <os.h>
#include <um_malloc.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

enum request_type { REQ_NEW_CONTROL };

#define SWITCH_MAGIC 0xfeedface

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

static struct sockaddr_un *new_addr(void *name, int len)
{
	struct sockaddr_un *sun;

J
Jeff Dike 已提交
36
	sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
J
Jeff Dike 已提交
37 38 39
	if (sun == NULL) {
		printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
		       "failed\n");
J
Jeff Dike 已提交
40
		return NULL;
L
Linus Torvalds 已提交
41 42 43
	}
	sun->sun_family = AF_UNIX;
	memcpy(sun->sun_path, name, len);
J
Jeff Dike 已提交
44
	return sun;
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55
}

static int connect_to_switch(struct daemon_data *pri)
{
	struct sockaddr_un *ctl_addr = pri->ctl_addr;
	struct sockaddr_un *local_addr = pri->local_addr;
	struct sockaddr_un *sun;
	struct request_v3 req;
	int fd, n, err;

	pri->control = socket(AF_UNIX, SOCK_STREAM, 0);
J
Jeff Dike 已提交
56
	if (pri->control < 0) {
57
		err = -errno;
J
Jeff Dike 已提交
58 59
		printk(UM_KERN_ERR "daemon_open : control socket failed, "
		       "errno = %d\n", -err);
60
		return err;
L
Linus Torvalds 已提交
61 62
	}

J
Jeff Dike 已提交
63 64
	if (connect(pri->control, (struct sockaddr *) ctl_addr,
		   sizeof(*ctl_addr)) < 0) {
L
Linus Torvalds 已提交
65
		err = -errno;
J
Jeff Dike 已提交
66 67
		printk(UM_KERN_ERR "daemon_open : control connect failed, "
		       "errno = %d\n", -err);
L
Linus Torvalds 已提交
68 69 70 71
		goto out;
	}

	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
J
Jeff Dike 已提交
72
	if (fd < 0) {
L
Linus Torvalds 已提交
73
		err = -errno;
J
Jeff Dike 已提交
74 75
		printk(UM_KERN_ERR "daemon_open : data socket failed, "
		       "errno = %d\n", -err);
L
Linus Torvalds 已提交
76 77
		goto out;
	}
J
Jeff Dike 已提交
78
	if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) {
L
Linus Torvalds 已提交
79
		err = -errno;
J
Jeff Dike 已提交
80 81
		printk(UM_KERN_ERR "daemon_open : data bind failed, "
		       "errno = %d\n", -err);
L
Linus Torvalds 已提交
82 83 84
		goto out_close;
	}

J
Jeff Dike 已提交
85
	sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
J
Jeff Dike 已提交
86 87 88
	if (sun == NULL) {
		printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
		       "failed\n");
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96
		err = -ENOMEM;
		goto out_close;
	}

	req.magic = SWITCH_MAGIC;
	req.version = SWITCH_VERSION;
	req.type = REQ_NEW_CONTROL;
	req.sock = *local_addr;
J
Jeff Dike 已提交
97 98 99 100
	n = write(pri->control, &req, sizeof(req));
	if (n != sizeof(req)) {
		printk(UM_KERN_ERR "daemon_open : control setup request "
		       "failed, err = %d\n", -errno);
L
Linus Torvalds 已提交
101
		err = -ENOTCONN;
102
		goto out_free;
L
Linus Torvalds 已提交
103 104
	}

J
Jeff Dike 已提交
105 106 107 108
	n = read(pri->control, sun, sizeof(*sun));
	if (n != sizeof(*sun)) {
		printk(UM_KERN_ERR "daemon_open : read of data socket failed, "
		       "err = %d\n", -errno);
L
Linus Torvalds 已提交
109
		err = -ENOTCONN;
110
		goto out_free;
L
Linus Torvalds 已提交
111 112 113
	}

	pri->data_addr = sun;
J
Jeff Dike 已提交
114
	return fd;
L
Linus Torvalds 已提交
115

116 117
 out_free:
	kfree(sun);
L
Linus Torvalds 已提交
118
 out_close:
J
Jeff Dike 已提交
119
	close(fd);
L
Linus Torvalds 已提交
120
 out:
J
Jeff Dike 已提交
121
	close(pri->control);
J
Jeff Dike 已提交
122
	return err;
L
Linus Torvalds 已提交
123 124
}

125
static int daemon_user_init(void *data, void *dev)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134
{
	struct daemon_data *pri = data;
	struct timeval tv;
	struct {
		char zero;
		int pid;
		int usecs;
	} name;

J
Jeff Dike 已提交
135 136
	if (!strcmp(pri->sock_type, "unix"))
		pri->ctl_addr = new_addr(pri->ctl_sock,
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144
					 strlen(pri->ctl_sock) + 1);
	name.zero = 0;
	name.pid = os_getpid();
	gettimeofday(&tv, NULL);
	name.usecs = tv.tv_usec;
	pri->local_addr = new_addr(&name, sizeof(name));
	pri->dev = dev;
	pri->fd = connect_to_switch(pri);
J
Jeff Dike 已提交
145
	if (pri->fd < 0) {
L
Linus Torvalds 已提交
146 147
		kfree(pri->local_addr);
		pri->local_addr = NULL;
148
		return pri->fd;
L
Linus Torvalds 已提交
149
	}
150 151

	return 0;
L
Linus Torvalds 已提交
152 153 154 155 156
}

static int daemon_open(void *data)
{
	struct daemon_data *pri = data;
J
Jeff Dike 已提交
157
	return pri->fd;
L
Linus Torvalds 已提交
158 159 160 161 162 163
}

static void daemon_remove(void *data)
{
	struct daemon_data *pri = data;

J
Jeff Dike 已提交
164
	close(pri->fd);
165
	pri->fd = -1;
J
Jeff Dike 已提交
166
	close(pri->control);
167 168
	pri->control = -1;

J
Jesper Juhl 已提交
169
	kfree(pri->data_addr);
170
	pri->data_addr = NULL;
J
Jesper Juhl 已提交
171
	kfree(pri->ctl_addr);
172
	pri->ctl_addr = NULL;
J
Jesper Juhl 已提交
173
	kfree(pri->local_addr);
174
	pri->local_addr = NULL;
L
Linus Torvalds 已提交
175 176 177 178 179 180
}

int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri)
{
	struct sockaddr_un *data_addr = pri->data_addr;

J
Jeff Dike 已提交
181
	return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
L
Linus Torvalds 已提交
182 183
}

J
Jeff Dike 已提交
184
const struct net_user_info daemon_user_info = {
L
Linus Torvalds 已提交
185 186 187 188 189 190
	.init		= daemon_user_init,
	.open		= daemon_open,
	.close	 	= NULL,
	.remove	 	= daemon_remove,
	.add_address	= NULL,
	.delete_address = NULL,
J
Jeff Dike 已提交
191 192
	.mtu		= ETH_MAX_PACKET,
	.max_packet	= ETH_MAX_PACKET + ETH_HEADER_OTHER,
L
Linus Torvalds 已提交
193
};