Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / tunalloc.c
1 /* Slice-side code to allocate tuntap interface in root slice
2  * Based on bmsocket.c
3  *  Thom Haddow - 08/10/09
4  *
5  * Call tun_alloc() with IFFTUN or IFFTAP as an argument to get back fd to
6  * new tuntap interface. Interface name can be acquired via TUNGETIFF ioctl.
7  */
8
9 #include <config.h>
10 #include <sys/un.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/socket.h>
17 #include <linux/if.h>
18 #include <linux/if_tun.h>
19
20 #define VSYS_TUNTAP "/vsys/fd_tuntap.control"
21
22
23 int tun_alloc(int iftype, char *if_name);
24
25 /* Reads vif FD from "fd", writes interface name to vif_name, and returns vif FD.
26  * vif_name should be IFNAMSIZ chars long. */
27 static
28 int receive_vif_fd(int fd, char *vif_name)
29 {
30         struct msghdr msg;
31         struct iovec iov;
32         int rv;
33         size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
34         struct cmsghdr *cmsg;
35         unsigned char *data;
36
37     /* Use IOV to read interface name */
38         iov.iov_base = vif_name;
39         iov.iov_len = IFNAMSIZ;
40
41         msg.msg_name = 0;
42         msg.msg_namelen = 0;
43         msg.msg_iov = &iov;
44         msg.msg_iovlen = 1;
45         /* old BSD implementations should use msg_accrights instead of
46          * msg_control; the interface is different. */
47         msg.msg_control = ccmsg;
48         msg.msg_controllen = sizeof(ccmsg);
49
50         while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
51         if (rv == -1) {
52                 perror("recvmsg");
53                 return -1;
54         }
55         if(!rv) {
56                 /* EOF */
57                 return -1;
58         }
59
60         cmsg = CMSG_FIRSTHDR(&msg);
61         if (!cmsg->cmsg_type == SCM_RIGHTS) {
62                 fprintf(stderr, "got control message of unknown type %d\n",
63                         cmsg->cmsg_type);
64                 return -1;
65         }
66         data = CMSG_DATA(cmsg);
67         return *(int*)data;
68 }
69
70
71 int tun_alloc(int iftype, char *if_name)
72 {
73     int control_fd;
74     struct sockaddr_un addr;
75     int remotefd;
76
77     control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
78     if (control_fd == -1) {
79         perror("Could not create UNIX socket\n");
80         exit(-1);
81     }
82
83     memset(&addr, 0, sizeof(struct sockaddr_un));
84     /* Clear structure */
85     addr.sun_family = AF_UNIX;
86     strncpy(addr.sun_path, VSYS_TUNTAP,
87             sizeof(addr.sun_path) - 1);
88
89     if (connect(control_fd, (struct sockaddr *) &addr,
90                 sizeof(struct sockaddr_un)) == -1) {
91         perror("Could not connect to Vsys control socket");
92         exit(-1);
93     }
94
95     /* passing type param */
96     if (send(control_fd, &iftype, sizeof(iftype), 0) != sizeof(iftype)) {
97         perror("Could not send paramater to Vsys control socket");
98         exit(-1);
99     }
100
101     remotefd = receive_vif_fd(control_fd, if_name);
102
103     close(control_fd);
104
105     return remotefd;
106 }