blob: f8e85e0bdace20fddc82ca805e6101bcc9c76a8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <stdint.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070010#include <unistd.h>
11#include <errno.h>
12#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <sys/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <sys/time.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070015#include <sys/un.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "daemon.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070017#include "net_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070019#include "um_malloc.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070020#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022enum request_type { REQ_NEW_CONTROL };
23
24#define SWITCH_MAGIC 0xfeedface
25
26struct request_v3 {
27 uint32_t magic;
28 uint32_t version;
29 enum request_type type;
30 struct sockaddr_un sock;
31};
32
33static struct sockaddr_un *new_addr(void *name, int len)
34{
35 struct sockaddr_un *sun;
36
Jeff Dike43f5b302008-05-12 14:01:52 -070037 sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070038 if (sun == NULL) {
39 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
40 "failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070041 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43 sun->sun_family = AF_UNIX;
44 memcpy(sun->sun_path, name, len);
Jeff Dike56bd1942007-05-06 14:51:02 -070045 return sun;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48static int connect_to_switch(struct daemon_data *pri)
49{
50 struct sockaddr_un *ctl_addr = pri->ctl_addr;
51 struct sockaddr_un *local_addr = pri->local_addr;
52 struct sockaddr_un *sun;
53 struct request_v3 req;
54 int fd, n, err;
55
56 pri->control = socket(AF_UNIX, SOCK_STREAM, 0);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070057 if (pri->control < 0) {
Paolo 'Blaisorblade' Giarrussode7b37c2007-03-07 20:41:14 -080058 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070059 printk(UM_KERN_ERR "daemon_open : control socket failed, "
60 "errno = %d\n", -err);
Paolo 'Blaisorblade' Giarrussode7b37c2007-03-07 20:41:14 -080061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070064 if (connect(pri->control, (struct sockaddr *) ctl_addr,
65 sizeof(*ctl_addr)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070067 printk(UM_KERN_ERR "daemon_open : control connect failed, "
68 "errno = %d\n", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 goto out;
70 }
71
72 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070073 if (fd < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070075 printk(UM_KERN_ERR "daemon_open : data socket failed, "
76 "errno = %d\n", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 goto out;
78 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070079 if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 err = -errno;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070081 printk(UM_KERN_ERR "daemon_open : data bind failed, "
82 "errno = %d\n", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 goto out_close;
84 }
85
Jeff Dike43f5b302008-05-12 14:01:52 -070086 sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070087 if (sun == NULL) {
88 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
89 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 err = -ENOMEM;
91 goto out_close;
92 }
93
94 req.magic = SWITCH_MAGIC;
95 req.version = SWITCH_VERSION;
96 req.type = REQ_NEW_CONTROL;
97 req.sock = *local_addr;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070098 n = write(pri->control, &req, sizeof(req));
99 if (n != sizeof(req)) {
100 printk(UM_KERN_ERR "daemon_open : control setup request "
101 "failed, err = %d\n", -errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 err = -ENOTCONN;
Paolo 'Blaisorblade' Giarrussoba260e22005-11-13 16:07:14 -0800103 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700106 n = read(pri->control, sun, sizeof(*sun));
107 if (n != sizeof(*sun)) {
108 printk(UM_KERN_ERR "daemon_open : read of data socket failed, "
109 "err = %d\n", -errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 err = -ENOTCONN;
Paolo 'Blaisorblade' Giarrussoba260e22005-11-13 16:07:14 -0800111 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 pri->data_addr = sun;
Jeff Dike56bd1942007-05-06 14:51:02 -0700115 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Paolo 'Blaisorblade' Giarrussoba260e22005-11-13 16:07:14 -0800117 out_free:
118 kfree(sun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 out_close:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700120 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 out:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700122 close(pri->control);
Jeff Dike56bd1942007-05-06 14:51:02 -0700123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Jeff Dikef34d9d22007-05-06 14:51:04 -0700126static int daemon_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct daemon_data *pri = data;
129 struct timeval tv;
130 struct {
131 char zero;
132 int pid;
133 int usecs;
134 } name;
135
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700136 if (!strcmp(pri->sock_type, "unix"))
137 pri->ctl_addr = new_addr(pri->ctl_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 strlen(pri->ctl_sock) + 1);
139 name.zero = 0;
140 name.pid = os_getpid();
141 gettimeofday(&tv, NULL);
142 name.usecs = tv.tv_usec;
143 pri->local_addr = new_addr(&name, sizeof(name));
144 pri->dev = dev;
145 pri->fd = connect_to_switch(pri);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700146 if (pri->fd < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 kfree(pri->local_addr);
148 pri->local_addr = NULL;
Jeff Dikef34d9d22007-05-06 14:51:04 -0700149 return pri->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Jeff Dikef34d9d22007-05-06 14:51:04 -0700151
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static int daemon_open(void *data)
156{
157 struct daemon_data *pri = data;
Jeff Dike56bd1942007-05-06 14:51:02 -0700158 return pri->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static void daemon_remove(void *data)
162{
163 struct daemon_data *pri = data;
164
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700165 close(pri->fd);
Paolo 'Blaisorblade' Giarrussoc42791b2006-01-18 17:42:53 -0800166 pri->fd = -1;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700167 close(pri->control);
Paolo 'Blaisorblade' Giarrussoc42791b2006-01-18 17:42:53 -0800168 pri->control = -1;
169
Jesper Juhl41f21482005-06-25 14:55:20 -0700170 kfree(pri->data_addr);
Paolo 'Blaisorblade' Giarrussoc42791b2006-01-18 17:42:53 -0800171 pri->data_addr = NULL;
Jesper Juhl41f21482005-06-25 14:55:20 -0700172 kfree(pri->ctl_addr);
Paolo 'Blaisorblade' Giarrussoc42791b2006-01-18 17:42:53 -0800173 pri->ctl_addr = NULL;
Jesper Juhl41f21482005-06-25 14:55:20 -0700174 kfree(pri->local_addr);
Paolo 'Blaisorblade' Giarrussoc42791b2006-01-18 17:42:53 -0800175 pri->local_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri)
179{
180 struct sockaddr_un *data_addr = pri->data_addr;
181
Jeff Dike56bd1942007-05-06 14:51:02 -0700182 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Jeff Dike5e7672e2006-09-27 01:50:33 -0700185const struct net_user_info daemon_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .init = daemon_user_init,
187 .open = daemon_open,
188 .close = NULL,
189 .remove = daemon_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .add_address = NULL,
191 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700192 .mtu = ETH_MAX_PACKET,
193 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};