1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
5 * James Leu (jleu@mindspring.net).
6 * Copyright (C) 2001 by various other people who didn't put their name here.
12 #include <sys/types.h>
13 #include <sys/socket.h>
19 #include <um_malloc.h>
21 enum request_type
{ REQ_NEW_CONTROL
};
23 #define SWITCH_MAGIC 0xfeedface
28 enum request_type type
;
29 struct sockaddr_un sock
;
32 static struct sockaddr_un
*new_addr(void *name
, int len
)
34 struct sockaddr_un
*sun
;
36 sun
= uml_kmalloc(sizeof(struct sockaddr_un
), UM_GFP_KERNEL
);
38 printk(UM_KERN_ERR
"new_addr: allocation of sockaddr_un "
42 sun
->sun_family
= AF_UNIX
;
43 memcpy(sun
->sun_path
, name
, len
);
47 static int connect_to_switch(struct daemon_data
*pri
)
49 struct sockaddr_un
*ctl_addr
= pri
->ctl_addr
;
50 struct sockaddr_un
*local_addr
= pri
->local_addr
;
51 struct sockaddr_un
*sun
;
52 struct request_v3 req
;
55 pri
->control
= socket(AF_UNIX
, SOCK_STREAM
, 0);
56 if (pri
->control
< 0) {
58 printk(UM_KERN_ERR
"daemon_open : control socket failed, "
59 "errno = %d\n", -err
);
63 if (connect(pri
->control
, (struct sockaddr
*) ctl_addr
,
64 sizeof(*ctl_addr
)) < 0) {
66 printk(UM_KERN_ERR
"daemon_open : control connect failed, "
67 "errno = %d\n", -err
);
71 fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
74 printk(UM_KERN_ERR
"daemon_open : data socket failed, "
75 "errno = %d\n", -err
);
78 if (bind(fd
, (struct sockaddr
*) local_addr
, sizeof(*local_addr
)) < 0) {
80 printk(UM_KERN_ERR
"daemon_open : data bind failed, "
81 "errno = %d\n", -err
);
85 sun
= uml_kmalloc(sizeof(struct sockaddr_un
), UM_GFP_KERNEL
);
87 printk(UM_KERN_ERR
"new_addr: allocation of sockaddr_un "
93 req
.magic
= SWITCH_MAGIC
;
94 req
.version
= SWITCH_VERSION
;
95 req
.type
= REQ_NEW_CONTROL
;
96 req
.sock
= *local_addr
;
97 n
= write(pri
->control
, &req
, sizeof(req
));
98 if (n
!= sizeof(req
)) {
99 printk(UM_KERN_ERR
"daemon_open : control setup request "
100 "failed, err = %d\n", -errno
);
105 n
= read(pri
->control
, sun
, sizeof(*sun
));
106 if (n
!= sizeof(*sun
)) {
107 printk(UM_KERN_ERR
"daemon_open : read of data socket failed, "
108 "err = %d\n", -errno
);
113 pri
->data_addr
= sun
;
125 static int daemon_user_init(void *data
, void *dev
)
127 struct daemon_data
*pri
= data
;
135 if (!strcmp(pri
->sock_type
, "unix"))
136 pri
->ctl_addr
= new_addr(pri
->ctl_sock
,
137 strlen(pri
->ctl_sock
) + 1);
139 name
.pid
= os_getpid();
140 gettimeofday(&tv
, NULL
);
141 name
.usecs
= tv
.tv_usec
;
142 pri
->local_addr
= new_addr(&name
, sizeof(name
));
144 pri
->fd
= connect_to_switch(pri
);
146 kfree(pri
->local_addr
);
147 pri
->local_addr
= NULL
;
154 static int daemon_open(void *data
)
156 struct daemon_data
*pri
= data
;
160 static void daemon_remove(void *data
)
162 struct daemon_data
*pri
= data
;
169 kfree(pri
->data_addr
);
170 pri
->data_addr
= NULL
;
171 kfree(pri
->ctl_addr
);
172 pri
->ctl_addr
= NULL
;
173 kfree(pri
->local_addr
);
174 pri
->local_addr
= NULL
;
177 int daemon_user_write(int fd
, void *buf
, int len
, struct daemon_data
*pri
)
179 struct sockaddr_un
*data_addr
= pri
->data_addr
;
181 return net_sendto(fd
, buf
, len
, data_addr
, sizeof(*data_addr
));
184 const struct net_user_info daemon_user_info
= {
185 .init
= daemon_user_init
,
188 .remove
= daemon_remove
,
190 .delete_address
= NULL
,
191 .mtu
= ETH_MAX_PACKET
,
192 .max_packet
= ETH_MAX_PACKET
+ ETH_HEADER_OTHER
,