]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/um/drivers/daemon_user.c
uml: network formatting
[mirror_ubuntu-artful-kernel.git] / arch / um / drivers / daemon_user.c
CommitLineData
1da177e4 1/*
cd1ae0e4
JD
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
1da177e4
LT
4 * James Leu (jleu@mindspring.net).
5 * Copyright (C) 2001 by various other people who didn't put their name here.
6 * Licensed under the GPL.
7 */
8
1da177e4 9#include <stdint.h>
cd1ae0e4
JD
10#include <unistd.h>
11#include <errno.h>
12#include <sys/types.h>
1da177e4 13#include <sys/socket.h>
1da177e4 14#include <sys/time.h>
cd1ae0e4 15#include <sys/un.h>
1da177e4 16#include "daemon.h"
cd1ae0e4 17#include "net_user.h"
1da177e4 18#include "os.h"
c13e5690 19#include "um_malloc.h"
cd1ae0e4 20#include "user.h"
1da177e4
LT
21
22#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
23
24enum request_type { REQ_NEW_CONTROL };
25
26#define SWITCH_MAGIC 0xfeedface
27
28struct request_v3 {
29 uint32_t magic;
30 uint32_t version;
31 enum request_type type;
32 struct sockaddr_un sock;
33};
34
35static struct sockaddr_un *new_addr(void *name, int len)
36{
37 struct sockaddr_un *sun;
38
e4c4bf99 39 sun = kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
cd1ae0e4
JD
40 if (sun == NULL) {
41 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
42 "failed\n");
56bd194b 43 return NULL;
1da177e4
LT
44 }
45 sun->sun_family = AF_UNIX;
46 memcpy(sun->sun_path, name, len);
56bd194b 47 return sun;
1da177e4
LT
48}
49
50static int connect_to_switch(struct daemon_data *pri)
51{
52 struct sockaddr_un *ctl_addr = pri->ctl_addr;
53 struct sockaddr_un *local_addr = pri->local_addr;
54 struct sockaddr_un *sun;
55 struct request_v3 req;
56 int fd, n, err;
57
58 pri->control = socket(AF_UNIX, SOCK_STREAM, 0);
cd1ae0e4 59 if (pri->control < 0) {
de7b37cd 60 err = -errno;
cd1ae0e4
JD
61 printk(UM_KERN_ERR "daemon_open : control socket failed, "
62 "errno = %d\n", -err);
de7b37cd 63 return err;
1da177e4
LT
64 }
65
cd1ae0e4
JD
66 if (connect(pri->control, (struct sockaddr *) ctl_addr,
67 sizeof(*ctl_addr)) < 0) {
1da177e4 68 err = -errno;
cd1ae0e4
JD
69 printk(UM_KERN_ERR "daemon_open : control connect failed, "
70 "errno = %d\n", -err);
1da177e4
LT
71 goto out;
72 }
73
74 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
cd1ae0e4 75 if (fd < 0) {
1da177e4 76 err = -errno;
cd1ae0e4
JD
77 printk(UM_KERN_ERR "daemon_open : data socket failed, "
78 "errno = %d\n", -err);
1da177e4
LT
79 goto out;
80 }
cd1ae0e4 81 if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) {
1da177e4 82 err = -errno;
cd1ae0e4
JD
83 printk(UM_KERN_ERR "daemon_open : data bind failed, "
84 "errno = %d\n", -err);
1da177e4
LT
85 goto out_close;
86 }
87
e4c4bf99 88 sun = kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
cd1ae0e4
JD
89 if (sun == NULL) {
90 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
91 "failed\n");
1da177e4
LT
92 err = -ENOMEM;
93 goto out_close;
94 }
95
96 req.magic = SWITCH_MAGIC;
97 req.version = SWITCH_VERSION;
98 req.type = REQ_NEW_CONTROL;
99 req.sock = *local_addr;
cd1ae0e4
JD
100 n = write(pri->control, &req, sizeof(req));
101 if (n != sizeof(req)) {
102 printk(UM_KERN_ERR "daemon_open : control setup request "
103 "failed, err = %d\n", -errno);
1da177e4 104 err = -ENOTCONN;
ba260e23 105 goto out_free;
1da177e4
LT
106 }
107
cd1ae0e4
JD
108 n = read(pri->control, sun, sizeof(*sun));
109 if (n != sizeof(*sun)) {
110 printk(UM_KERN_ERR "daemon_open : read of data socket failed, "
111 "err = %d\n", -errno);
1da177e4 112 err = -ENOTCONN;
ba260e23 113 goto out_free;
1da177e4
LT
114 }
115
116 pri->data_addr = sun;
56bd194b 117 return fd;
1da177e4 118
ba260e23
PBG
119 out_free:
120 kfree(sun);
1da177e4 121 out_close:
cd1ae0e4 122 close(fd);
1da177e4 123 out:
cd1ae0e4 124 close(pri->control);
56bd194b 125 return err;
1da177e4
LT
126}
127
f34d9d2d 128static int daemon_user_init(void *data, void *dev)
1da177e4
LT
129{
130 struct daemon_data *pri = data;
131 struct timeval tv;
132 struct {
133 char zero;
134 int pid;
135 int usecs;
136 } name;
137
cd1ae0e4
JD
138 if (!strcmp(pri->sock_type, "unix"))
139 pri->ctl_addr = new_addr(pri->ctl_sock,
1da177e4
LT
140 strlen(pri->ctl_sock) + 1);
141 name.zero = 0;
142 name.pid = os_getpid();
143 gettimeofday(&tv, NULL);
144 name.usecs = tv.tv_usec;
145 pri->local_addr = new_addr(&name, sizeof(name));
146 pri->dev = dev;
147 pri->fd = connect_to_switch(pri);
cd1ae0e4 148 if (pri->fd < 0) {
1da177e4
LT
149 kfree(pri->local_addr);
150 pri->local_addr = NULL;
f34d9d2d 151 return pri->fd;
1da177e4 152 }
f34d9d2d
JD
153
154 return 0;
1da177e4
LT
155}
156
157static int daemon_open(void *data)
158{
159 struct daemon_data *pri = data;
56bd194b 160 return pri->fd;
1da177e4
LT
161}
162
163static void daemon_remove(void *data)
164{
165 struct daemon_data *pri = data;
166
cd1ae0e4 167 close(pri->fd);
c42791b6 168 pri->fd = -1;
cd1ae0e4 169 close(pri->control);
c42791b6
PBG
170 pri->control = -1;
171
41f2148a 172 kfree(pri->data_addr);
c42791b6 173 pri->data_addr = NULL;
41f2148a 174 kfree(pri->ctl_addr);
c42791b6 175 pri->ctl_addr = NULL;
41f2148a 176 kfree(pri->local_addr);
c42791b6 177 pri->local_addr = NULL;
1da177e4
LT
178}
179
180int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri)
181{
182 struct sockaddr_un *data_addr = pri->data_addr;
183
56bd194b 184 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
1da177e4
LT
185}
186
187static int daemon_set_mtu(int mtu, void *data)
188{
56bd194b 189 return mtu;
1da177e4
LT
190}
191
5e7672ec 192const struct net_user_info daemon_user_info = {
1da177e4
LT
193 .init = daemon_user_init,
194 .open = daemon_open,
195 .close = NULL,
196 .remove = daemon_remove,
197 .set_mtu = daemon_set_mtu,
198 .add_address = NULL,
199 .delete_address = NULL,
200 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
201};