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