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