1 /* Copyright 2005 Renzo Davoli - VDE-2
2 * --pidfile/-p and cleanup management by Mattia Belletti (C) 2004.
3 * Licensed under the GPLv2
4 * Modified by Ludovico Gardenghi 2005
5 * -g option (group management) by Daniel P. Berrange
6 * dir permission patch by Alessio Caprari 2006
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
33 #include <vdecommon.h>
37 #include "sockutils.h"
40 static struct swmodule swmi
;
41 static struct mod_support modfun
;
42 static unsigned int ctl_type
;
43 static unsigned int wd_type
;
44 static unsigned int data_type
;
46 static char real_ctl_socket
[PATH_MAX
];
47 static char *ctl_socket
= real_ctl_socket
;
50 static int dirmode
= -1;
51 static gid_t grp_owner
= -1;
53 #define MODULENAME "unix prog"
55 #define DATA_BUF_SIZE 131072
56 #define SWITCH_MAGIC 0xfeedface
59 enum request_type
{ REQ_NEW_CONTROL
, REQ_NEW_PORT0
};
63 enum request_type type
;
66 unsigned char addr
[ETH_ALEN
];
67 struct sockaddr_un name
;
71 } __attribute__((packed
));
76 enum request_type type
;
77 struct sockaddr_un sock
;
79 } __attribute__((packed
));
86 static int send_datasock(int fd
, int ctl_fd
, void *packet
, int len
, void *data
, int port
)
89 struct sockaddr
*dst
=(struct sockaddr
*)data
;
91 n
= len
- sendto(fd
, packet
, len
, 0, dst
, sizeof(struct sockaddr_un
));
95 if(errno
!= EAGAIN
) printlog(LOG_WARNING
,"send_sockaddr port %d: %s",port
,strerror(errno
));
105 static void closeport(int fd
, int portno
)
111 static int newport(int fd
, int portno
, uid_t user
)
114 struct sockaddr_un sun
;
116 int sockbufsize
= DATA_BUF_SIZE
;
117 int optsize
= sizeof(sockbufsize
);
120 if((data_fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0)) < 0){
121 printlog(LOG_ERR
,"socket: %s",strerror(errno
));
124 if(fcntl(data_fd
, F_SETFL
, O_NONBLOCK
) < 0){
125 printlog(LOG_ERR
,"Setting O_NONBLOCK on data fd %s",strerror(errno
));
130 if(setsockopt(data_fd
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
, optsize
) < 0)
131 printlog(LOG_WARNING
, "Warning: setting send buffer size on data fd %d to %d failed, expect packet loss: %s",
132 data_fd
, sockbufsize
, strerror(errno
));
133 if(setsockopt(data_fd
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
, optsize
) < 0)
134 printlog(LOG_WARNING
, "Warning: setting send buffer size on data fd %d to %d failed, expect packet loss: %s",
135 data_fd
, sockbufsize
, strerror(errno
));
138 sun
.sun_family
= AF_UNIX
;
139 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s/%03d",ctl_socket
,portno
);
140 if ((unlink(sun
.sun_path
) < 0 && errno
!= ENOENT
) ||
141 bind(data_fd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
142 printlog(LOG_ERR
,"Binding to data socket %s",strerror(errno
));
143 close_ep(portno
-1,fd
);
147 chmod(sun
.sun_path
,mode
& 0700);
149 chmod(sun
.sun_path
,mode
);
150 if(chown(sun
.sun_path
,user
,grp_owner
) < 0) {
151 printlog(LOG_ERR
, "chown: %s", strerror(errno
));
152 close_ep(portno
-1,fd
);
156 add_fd(data_fd
,data_type
,portno
);
161 static void *memdup(void *src
,int size
)
163 void *dst
=malloc(size
);
165 memcpy(dst
,src
,size
);
169 #define GETFILEOWNER(PATH) ({\
171 (stat((PATH),&s)?-1:s.st_uid); \
174 static int new_port_v1_v3(int fd
, int type_port
,
175 struct sockaddr_un
*sun_out
)
178 enum request_type type
= type_port
& 0xff;
179 int port_request
=type_port
>> 8;
181 struct sockaddr_un sun_in
;
185 /* no break: falltrough */
186 case REQ_NEW_CONTROL
:
187 if (sun_out
->sun_path
[0] != 0) { //not for unnamed sockets
188 if (access(sun_out
->sun_path
,R_OK
| W_OK
) != 0) { //socket error
192 user
=GETFILEOWNER(sun_out
->sun_path
);
194 port
= setup_ep(port_request
, fd
, memdup(sun_out
,sizeof(struct sockaddr_un
)), user
, &modfun
);
199 sun_in
.sun_family
= AF_UNIX
;
200 snprintf(sun_in
.sun_path
,sizeof(sun_in
.sun_path
),"%s/%03d",ctl_socket
,port
);
201 n
= write(fd
, &sun_in
, sizeof(sun_in
));
202 if(n
!= sizeof(sun_in
)){
203 printlog(LOG_WARNING
,"Sending data socket name %s",strerror(errno
));
207 if (type
==REQ_NEW_PORT0
)
208 setmgmtperm(sun_in
.sun_path
);
212 printlog(LOG_WARNING
,"Bad request type : %d", type
);
218 static void handle_input(unsigned char type
,int fd
,int revents
,int *arg
)
220 if (type
== data_type
) {
221 struct bipacket packet
;
222 struct sockaddr sock
;
224 socklen_t socklen
= sizeof(sock
);
226 len
=recvfrom(fd
, &(packet
.p
), sizeof(struct packet
),0, &sock
, &socklen
);
228 if (errno
== EAGAIN
) return;
229 printlog(LOG_WARNING
,"Reading data: %s",strerror(errno
));
232 printlog(LOG_WARNING
,"EOF data port: %s",strerror(errno
));
233 else if(len
>= ETH_HEADER_SIZE
)
234 handle_in_packet(*arg
, &(packet
.p
), len
);
236 else if (type
== wd_type
) {
237 char reqbuf
[REQBUFLEN
+1];
238 union request
*req
=(union request
*)reqbuf
;
241 len
= read(fd
, reqbuf
, REQBUFLEN
);
244 printlog(LOG_WARNING
,"Reading request %s", strerror(errno
));
248 } else if (len
> 0) {
250 if(req
->v1
.magic
== SWITCH_MAGIC
){
252 if(req
->v3
.version
== 3) {
253 port
=new_port_v1_v3(fd
, req
->v3
.type
, &(req
->v3
.sock
));
256 setup_description(*arg
,fd
,strdup(req
->v3
.description
));
259 else if(req
->v3
.version
> 2 || req
->v3
.version
== 2) {
260 printlog(LOG_ERR
, "Request for a version %d port, which this "
261 "vde_switch doesn't support", req
->v3
.version
);
265 *arg
=port
=new_port_v1_v3(fd
, req
->v1
.type
, &(req
->v1
.u
.new_control
.name
));
266 setup_description(*arg
,fd
,strdup(req
->v1
.description
));
270 printlog(LOG_WARNING
,"V0 request not supported");
281 else /*if (type == ctl_type)*/ {
282 struct sockaddr addr
;
287 new = accept(fd
, &addr
, &len
);
289 printlog(LOG_WARNING
,"accept %s",strerror(errno
));
292 if(fcntl(new, F_SETFL
, O_NONBLOCK
) < 0){
293 printlog(LOG_WARNING
,"fcntl - setting O_NONBLOCK %s",strerror(errno
));
298 add_fd(new,wd_type
,-1);
302 static void cleanup(unsigned char type
,int fd
,int arg
)
304 struct sockaddr_un clun
;
308 if((test_fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0){
309 printlog(LOG_ERR
,"socket %s",strerror(errno
));
311 clun
.sun_family
=AF_UNIX
;
312 snprintf(clun
.sun_path
,sizeof(clun
.sun_path
),"%s/ctl",ctl_socket
);
313 if(connect(test_fd
, (struct sockaddr
*) &clun
, sizeof(clun
))){
315 if(unlink(clun
.sun_path
) < 0)
316 printlog(LOG_WARNING
,"Could not remove ctl socket '%s': %s", ctl_socket
, strerror(errno
));
317 else if(rmdir(ctl_socket
) < 0)
318 printlog(LOG_WARNING
,"Could not remove ctl dir '%s': %s", ctl_socket
, strerror(errno
));
320 else printlog(LOG_WARNING
,"Cleanup not removing files");
322 if (type
== data_type
&& arg
>=0) {
323 snprintf(clun
.sun_path
,sizeof(clun
.sun_path
),"%s/%03d",ctl_socket
,arg
);
324 unlink(clun
.sun_path
);
330 #define DIRMODEARG 0x100
332 static struct option long_options
[] = {
334 {"vdesock", 1, 0, 's'},
338 {"dirmode", 1, 0, DIRMODEARG
},
339 {"group", 1, 0, 'g'},
342 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
344 static void usage(void)
347 "(opts from datasock module)\n"
348 " -s, --sock SOCK control directory pathname\n"
349 " -s, --vdesock SOCK Same as --sock SOCK\n"
350 " -s, --unix SOCK Same as --sock SOCK\n"
351 " -m, --mode MODE Permissions for the control socket (octal)\n"
352 " --dirmode MODE Permissions for the sockets directory (octal)\n"
353 " -g, --group GROUP Group owner for comm sockets\n"
357 static int parseopt(int c
, char *optarg
)
363 /* This should return NULL as the path probably does not exist */
364 vde_realpath(optarg
, real_ctl_socket
);
365 ctl_socket
= real_ctl_socket
;
368 sscanf(optarg
,"%o",&mode
);
371 if (!(grp
= getgrnam(optarg
))) {
372 fprintf(stderr
, "No such group '%s'\n", optarg
);
375 grp_owner
=grp
->gr_gid
;
378 sscanf(optarg
, "%o", &dirmode
);
386 static void init(void)
389 struct sockaddr_un sun
;
392 /* Set up default modes */
393 if (mode
< 0 && dirmode
< 0)
396 mode
= 00600; /* -rw------- for the ctl socket */
397 dirmode
= 02700; /* -rwx--S--- for the directory */
399 else if (mode
>= 0 && dirmode
< 0)
401 /* If only mode (-m) has been specified, we guess the dirmode from it,
402 * adding the executable bit where needed */
404 # define ADDBIT(mode, conditionmask, add) ((mode & conditionmask) ? ((mode & conditionmask) | add) : (mode & conditionmask))
406 dirmode
= 02000 | /* Add also setgid */
407 ADDBIT(mode
, 0600, 0100) |
408 ADDBIT(mode
, 0060, 0010) |
409 ADDBIT(mode
, 0006, 0001);
411 else if (mode
< 0 && dirmode
>= 0)
413 /* If only dirmode (--dirmode) has been specified, we guess the ctl
414 * socket mode from it, turning off the executable bit everywhere */
415 mode
= dirmode
& 0666;
418 if((connect_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0){
419 printlog(LOG_ERR
,"Could not obtain a BSD socket: %s", strerror(errno
));
422 if(setsockopt(connect_fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &one
,
424 printlog(LOG_ERR
,"Could not set socket options on %d: %s", connect_fd
, strerror(errno
));
427 if(fcntl(connect_fd
, F_SETFL
, O_NONBLOCK
) < 0){
428 printlog(LOG_ERR
,"Could not set O_NONBLOCK on connection fd %d: %s", connect_fd
, strerror(errno
));
431 if (((mkdir(ctl_socket
, 0777) < 0) && (errno
!= EEXIST
))){
432 printlog(LOG_ERR
,"Could not create the VDE ctl directory '%s': %s", ctl_socket
, strerror(errno
));
435 if (chmod(ctl_socket
, dirmode
) < 0) {
436 printlog(LOG_ERR
,"Could not set the VDE ctl directory '%s' permissions: %s", ctl_socket
, strerror(errno
));
439 sun
.sun_family
= AF_UNIX
;
440 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s/ctl",ctl_socket
);
441 if(bind(connect_fd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
442 if((errno
== EADDRINUSE
) && still_used(&sun
)){
443 printlog(LOG_ERR
, "Could not bind to socket '%s/ctl': %s", ctl_socket
, strerror(errno
));
446 else if(bind(connect_fd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
447 printlog(LOG_ERR
, "Could not bind to socket '%s/ctl' (second attempt): %s", ctl_socket
, strerror(errno
));
451 chmod(sun
.sun_path
,mode
);
452 if(chown(sun
.sun_path
,-1,grp_owner
) < 0) {
453 printlog(LOG_ERR
, "Could not chown socket '%s': %s", sun
.sun_path
, strerror(errno
));
456 if(listen(connect_fd
, 15) < 0){
457 printlog(LOG_ERR
,"Could not listen on fd %d: %s", connect_fd
, strerror(errno
));
460 ctl_type
=add_type(&swmi
,0);
461 wd_type
=add_type(&swmi
,0);
462 data_type
=add_type(&swmi
,1);
463 add_fd(connect_fd
,ctl_type
,-1);
466 static int showinfo(FILE *fd
)
468 printoutc(fd
,"ctl dir %s",ctl_socket
);
469 printoutc(fd
,"std mode 0%03o",mode
);
473 static struct comlist cl
[]={
474 {"ds","============","DATA SOCKET MENU",NULL
,NOARG
},
475 {"ds/showinfo","","show ds info",showinfo
,NOARG
|WITHFILE
},
478 static void delep (int fd
, void* data
, void *descr
)
480 if (fd
>=0) remove_fd(fd
);
481 if (data
) free(data
);
482 if (descr
) free(descr
);
485 void start_datasock(void)
487 ctl_socket
= (geteuid()==0)?VDESTDSOCK
:VDETMPSOCK
;
488 modfun
.modname
=swmi
.swmname
=MODULENAME
;
489 swmi
.swmnopts
=Nlong_options
;
490 swmi
.swmopts
=long_options
;
492 swmi
.parseopt
=parseopt
;
494 swmi
.handle_input
=handle_input
;
495 swmi
.cleanup
=cleanup
;
496 modfun
.sender
=send_datasock
;
497 modfun
.newport
=newport
;
499 modfun
.delport
=closeport
;