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.
11 #include <sys/socket.h>
16 #include "kern_util.h"
17 #include "user_util.h"
20 #include "um_malloc.h"
22 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
24 enum request_type
{ REQ_NEW_CONTROL
};
26 #define SWITCH_MAGIC 0xfeedface
31 enum request_type type
;
32 struct sockaddr_un sock
;
35 static struct sockaddr_un
*new_addr(void *name
, int len
)
37 struct sockaddr_un
*sun
;
39 sun
= um_kmalloc(sizeof(struct sockaddr_un
));
41 printk("new_addr: allocation of sockaddr_un failed\n");
44 sun
->sun_family
= AF_UNIX
;
45 memcpy(sun
->sun_path
, name
, len
);
49 static int connect_to_switch(struct daemon_data
*pri
)
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
;
57 pri
->control
= socket(AF_UNIX
, SOCK_STREAM
, 0);
60 printk("daemon_open : control socket failed, errno = %d\n",
65 if(connect(pri
->control
, (struct sockaddr
*) ctl_addr
,
66 sizeof(*ctl_addr
)) < 0){
68 printk("daemon_open : control connect failed, errno = %d\n",
73 fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
76 printk("daemon_open : data socket failed, errno = %d\n",
80 if(bind(fd
, (struct sockaddr
*) local_addr
, sizeof(*local_addr
)) < 0){
82 printk("daemon_open : data bind failed, errno = %d\n",
87 sun
= um_kmalloc(sizeof(struct sockaddr_un
));
89 printk("new_addr: allocation of sockaddr_un failed\n");
94 req
.magic
= SWITCH_MAGIC
;
95 req
.version
= SWITCH_VERSION
;
96 req
.type
= REQ_NEW_CONTROL
;
97 req
.sock
= *local_addr
;
98 n
= os_write_file(pri
->control
, &req
, sizeof(req
));
100 printk("daemon_open : control setup request failed, err = %d\n",
106 n
= os_read_file(pri
->control
, sun
, sizeof(*sun
));
107 if(n
!= sizeof(*sun
)){
108 printk("daemon_open : read of data socket failed, err = %d\n",
114 pri
->data_addr
= sun
;
122 os_close_file(pri
->control
);
126 static void 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
;
152 static int daemon_open(void *data
)
154 struct daemon_data
*pri
= data
;
158 static void daemon_remove(void *data
)
160 struct daemon_data
*pri
= data
;
162 os_close_file(pri
->fd
);
164 os_close_file(pri
->control
);
167 kfree(pri
->data_addr
);
168 pri
->data_addr
= NULL
;
169 kfree(pri
->ctl_addr
);
170 pri
->ctl_addr
= NULL
;
171 kfree(pri
->local_addr
);
172 pri
->local_addr
= NULL
;
175 int daemon_user_write(int fd
, void *buf
, int len
, struct daemon_data
*pri
)
177 struct sockaddr_un
*data_addr
= pri
->data_addr
;
179 return(net_sendto(fd
, buf
, len
, data_addr
, sizeof(*data_addr
)));
182 static int daemon_set_mtu(int mtu
, void *data
)
187 const struct net_user_info daemon_user_info
= {
188 .init
= daemon_user_init
,
191 .remove
= daemon_remove
,
192 .set_mtu
= daemon_set_mtu
,
194 .delete_address
= NULL
,
195 .max_packet
= MAX_PACKET
- ETH_HEADER_OTHER
199 * Overrides for Emacs so that we follow Linus's tabbing style.
200 * Emacs will notice this stuff at the end of the file and automatically
201 * adjust the settings for this buffer only. This must remain at the end
203 * ---------------------------------------------------------------------------
205 * c-file-style: "linux"