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