1 /* Copyright 2002 Jeff Dike
2 * Licensed under the GPL
14 #include <sys/ioctl.h>
15 #include <linux/if_tun.h>
17 /* TUNSETGROUP appeared in 2.6.23 */
19 #define TUNSETGROUP _IOW('T', 206, int)
22 static void Usage(char *name
)
24 fprintf(stderr
, "Create: %s [-b] [-u owner] [-g group] [-n] [-t device-name] "
25 "[-f tun-clone-device]\n", name
);
26 fprintf(stderr
, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
28 fprintf(stderr
, "The default tun clone device is /dev/net/tun - some systems"
29 " use\n/dev/misc/net/tun instead\n\n");
30 fprintf(stderr
, "-b will result in brief output (just the device name)\n");
31 fprintf(stderr
, "-n create a tun interface (not needed if the device name prefix is tun\n");
35 int main(int argc
, char **argv
)
42 int tap_fd
, opt
, delete = 0, brief
= 0;
45 char *tun
= "", *file
= "/dev/net/tun", *name
= argv
[0], *end
;
47 while((opt
= getopt(argc
, argv
, "bd:f:t:u:in")) > 0){
60 pw
= getpwnam(optarg
);
65 owner
= strtol(optarg
, &end
, 0);
67 fprintf(stderr
, "'%s' is neither a username nor a numeric uid.\n",
73 gr
= getgrnam(optarg
);
78 group
= strtol(optarg
, &end
, 0);
80 fprintf(stderr
, "'%s' is neither a groupname nor a numeric group.\n",
103 if((tap_fd
= open(file
, O_RDWR
)) < 0){
104 fprintf(stderr
, "Failed to open '%s' : ", file
);
109 memset(&ifr
, 0, sizeof(ifr
));
111 if (strncmp(tun
,"tun",3)==0) type
=IFF_TUN
;
112 ifr
.ifr_flags
= type
| IFF_NO_PI
;
113 strncpy(ifr
.ifr_name
, tun
, sizeof(ifr
.ifr_name
) - 1);
114 if(ioctl(tap_fd
, TUNSETIFF
, (void *) &ifr
) < 0){
120 if(ioctl(tap_fd
, TUNSETPERSIST
, 0) < 0){
121 perror("TUNSETPERSIST");
124 printf("Set '%s' nonpersistent\n", ifr
.ifr_name
);
127 /* emulate behaviour prior to TUNSETGROUP */
128 if(owner
== -1 && group
== -1) {
133 if(ioctl(tap_fd
, TUNSETOWNER
, owner
) < 0){
134 perror("TUNSETOWNER");
139 if(ioctl(tap_fd
, TUNSETGROUP
, group
) < 0){
140 perror("TUNSETGROUP");
145 if(ioctl(tap_fd
, TUNSETPERSIST
, 1) < 0){
146 perror("TUNSETPERSIST");
150 printf("%s\n", ifr
.ifr_name
);
152 printf("Set '%s' persistent and owned by", ifr
.ifr_name
);
154 printf(" uid %d", owner
);
156 printf(" gid %d", group
);