1 /* Copyright 2002 Jeff Dike
2 * Licensed under the GPL
13 #include <sys/ioctl.h>
14 #include <linux/if_tun.h>
18 #include <vdecommon.h>
20 static void Usage(char *name
)
22 fprintf(stderr
, "Create: %s [-b] [-u owner] [-t device-name] "
23 "[-f tun-clone-device]\n", name
);
24 fprintf(stderr
, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
26 fprintf(stderr
, "The default tun clone device is /dev/net/tun - some systems"
27 " use\n/dev/misc/net/tun instead\n\n");
28 fprintf(stderr
, "-b will result in brief output (just the device name)\n");
32 int main(int argc
, char **argv
)
36 long owner
= geteuid();
37 int tap_fd
, opt
, delete = 0, brief
= 0;
38 char *tun
= "", *file
= "/dev/net/tun", *name
= argv
[0], *end
;
40 while((opt
= getopt(argc
, argv
, "bd:f:t:u:")) > 0){
53 pw
= getpwnam(optarg
);
58 owner
= strtol(optarg
, &end
, 0);
60 fprintf(stderr
, "'%s' is neither a username nor a numeric uid.\n",
80 if((tap_fd
= open(file
, O_RDWR
)) < 0){
81 fprintf(stderr
, "Failed to open '%s' : ", file
);
86 memset(&ifr
, 0, sizeof(ifr
));
88 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
89 strncpy(ifr
.ifr_name
, tun
, sizeof(ifr
.ifr_name
) - 1);
90 if(ioctl(tap_fd
, TUNSETIFF
, (void *) &ifr
) < 0){
96 if(ioctl(tap_fd
, TUNSETPERSIST
, 0) < 0){
97 perror("TUNSETPERSIST");
100 printf("Set '%s' nonpersistent\n", ifr
.ifr_name
);
103 if(ioctl(tap_fd
, TUNSETPERSIST
, 1) < 0){
104 perror("TUNSETPERSIST");
107 if(ioctl(tap_fd
, TUNSETOWNER
, owner
) < 0){
108 perror("TUNSETPERSIST");
112 printf("%s\n", ifr
.ifr_name
);
113 else printf("Set '%s' persistent and owned by uid %ld\n", ifr
.ifr_name
,