053b4a5cfe71dba75ed4cd12b9c7793fd0365b1a
[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 <errno.h>
15 #include <sys/socket.h>
16 #include <linux/if.h>
17 #include <linux/if_tun.h>
18
19 #define VSYS_TUNTAP "/vsys/fd_tuntap.control"
20
21
22 int tun_alloc(int iftype, char *if_name);
23
24 /* Reads vif FD from "fd", writes interface name to vif_name, and returns vif FD.
25  * vif_name should be IFNAMSIZ chars long. */
26 static
27 int receive_vif_fd(int fd, char *vif_name)
28 {
29         struct msghdr msg;
30         struct iovec iov;
31         int rv;
32         size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
33         struct cmsghdr *cmsg;
34         unsigned char *data;
35
36     /* Use IOV to read interface name */
37         iov.iov_base = vif_name;
38         iov.iov_len = IFNAMSIZ;
39
40         msg.msg_name = 0;
41         msg.msg_namelen = 0;
42         msg.msg_iov = &iov;
43         msg.msg_iovlen = 1;
44         /* old BSD implementations should use msg_accrights instead of
45          * msg_control; the interface is different. */
46         msg.msg_control = ccmsg;
47         msg.msg_controllen = sizeof(ccmsg);
48
49         while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
50         if (rv == -1) {
51                 perror("recvmsg");
52                 return -1;
53         }
54         if(!rv) {
55                 /* EOF */
56                 return -1;
57         }
58
59         cmsg = CMSG_FIRSTHDR(&msg);
60         if (!cmsg->cmsg_type == SCM_RIGHTS) {
61                 fprintf(stderr, "got control message of unknown type %d\n",
62                         cmsg->cmsg_type);
63                 return -1;
64         }
65         data = CMSG_DATA(cmsg);
66         return *(int*)data;
67 }
68
69
70 int tun_alloc(int iftype, char *if_name)
71 {
72     int control_fd;
73     struct sockaddr_un addr;
74     int remotefd;
75
76     control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
77     if (control_fd == -1) {
78         perror("Could not create UNIX socket\n");
79         exit(-1);
80     }
81
82     memset(&addr, 0, sizeof(struct sockaddr_un));
83     /* Clear structure */
84     addr.sun_family = AF_UNIX;
85     strncpy(addr.sun_path, VSYS_TUNTAP,
86             sizeof(addr.sun_path) - 1);
87
88     if (connect(control_fd, (struct sockaddr *) &addr,
89                 sizeof(struct sockaddr_un)) == -1) {
90         perror("Could not connect to Vsys control socket");
91         exit(-1);
92     }
93
94     /* passing type param */
95     if (send(control_fd, &iftype, sizeof(iftype), 0) != sizeof(iftype)) {
96         perror("Could not send paramater to Vsys control socket");
97         exit(-1);
98     }
99
100     remotefd = receive_vif_fd(control_fd, if_name);
101
102     close(control_fd);
103
104     return remotefd;
105 }