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