1 /* Slice-side code to allocate tuntap interface in root slice
3 * Thom Haddow - 08/10/09
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.
16 #include <sys/socket.h>
18 #include <linux/if_tun.h>
20 #define VSYS_TUNTAP "/vsys/fd_tuntap.control"
23 int tun_alloc(int iftype, char *if_name);
25 /* Reads vif FD from "fd", writes interface name to vif_name, and returns vif FD.
26 * vif_name should be IFNAMSIZ chars long. */
28 int receive_vif_fd(int fd, char *vif_name)
33 size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
37 /* Use IOV to read interface name */
38 iov.iov_base = vif_name;
39 iov.iov_len = IFNAMSIZ;
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);
50 while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
60 cmsg = CMSG_FIRSTHDR(&msg);
61 if (!cmsg->cmsg_type == SCM_RIGHTS) {
62 fprintf(stderr, "got control message of unknown type %d\n",
66 data = CMSG_DATA(cmsg);
71 int tun_alloc(int iftype, char *if_name)
74 struct sockaddr_un addr;
77 control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
78 if (control_fd == -1) {
79 perror("Could not create UNIX socket\n");
83 memset(&addr, 0, sizeof(struct sockaddr_un));
85 addr.sun_family = AF_UNIX;
86 strncpy(addr.sun_path, VSYS_TUNTAP,
87 sizeof(addr.sun_path) - 1);
89 if (connect(control_fd, (struct sockaddr *) &addr,
90 sizeof(struct sockaddr_un)) == -1) {
91 perror("Could not connect to Vsys control socket");
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");
101 remotefd = receive_vif_fd(control_fd, if_name);