2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
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.
12 #include <sys/types.h>
13 #include <sys/socket.h>
19 #include "um_malloc.h"
22 enum request_type { REQ_NEW_CONTROL };
24 #define SWITCH_MAGIC 0xfeedface
29 enum request_type type;
30 struct sockaddr_un sock;
33 static struct sockaddr_un *new_addr(void *name, int len)
35 struct sockaddr_un *sun;
37 sun = kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
39 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
43 sun->sun_family = AF_UNIX;
44 memcpy(sun->sun_path, name, len);
48 static int connect_to_switch(struct daemon_data *pri)
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;
56 pri->control = socket(AF_UNIX, SOCK_STREAM, 0);
57 if (pri->control < 0) {
59 printk(UM_KERN_ERR "daemon_open : control socket failed, "
60 "errno = %d\n", -err);
64 if (connect(pri->control, (struct sockaddr *) ctl_addr,
65 sizeof(*ctl_addr)) < 0) {
67 printk(UM_KERN_ERR "daemon_open : control connect failed, "
68 "errno = %d\n", -err);
72 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
75 printk(UM_KERN_ERR "daemon_open : data socket failed, "
76 "errno = %d\n", -err);
79 if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) {
81 printk(UM_KERN_ERR "daemon_open : data bind failed, "
82 "errno = %d\n", -err);
86 sun = kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
88 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un "
94 req.magic = SWITCH_MAGIC;
95 req.version = SWITCH_VERSION;
96 req.type = REQ_NEW_CONTROL;
97 req.sock = *local_addr;
98 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);
106 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);
114 pri->data_addr = sun;
126 static int daemon_user_init(void *data, void *dev)
128 struct daemon_data *pri = data;
136 if (!strcmp(pri->sock_type, "unix"))
137 pri->ctl_addr = new_addr(pri->ctl_sock,
138 strlen(pri->ctl_sock) + 1);
140 name.pid = os_getpid();
141 gettimeofday(&tv, NULL);
142 name.usecs = tv.tv_usec;
143 pri->local_addr = new_addr(&name, sizeof(name));
145 pri->fd = connect_to_switch(pri);
147 kfree(pri->local_addr);
148 pri->local_addr = NULL;
155 static int daemon_open(void *data)
157 struct daemon_data *pri = data;
161 static void daemon_remove(void *data)
163 struct daemon_data *pri = data;
170 kfree(pri->data_addr);
171 pri->data_addr = NULL;
172 kfree(pri->ctl_addr);
173 pri->ctl_addr = NULL;
174 kfree(pri->local_addr);
175 pri->local_addr = NULL;
178 int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri)
180 struct sockaddr_un *data_addr = pri->data_addr;
182 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
185 const struct net_user_info daemon_user_info = {
186 .init = daemon_user_init,
189 .remove = daemon_remove,
191 .delete_address = NULL,
192 .mtu = ETH_MAX_PACKET,
193 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,