summaryrefslogtreecommitdiff
path: root/gssm/src/mktun/mktun.c
blob: e22d496ee17d22e73918a93fe0d1abbf33d3ec9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if_tun.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <arpa/inet.h>


static struct option long_options[] = {
	{ "delete", no_argument, 0, 'd'},
	{ "help", no_argument, 0, 'h'},
	{ 0, 0, 0, 0}
};


void usage(char *prog) {

	fprintf(stderr, "note: you must be root (except perhaps to delete "
	   "the interface)\n");
	fprintf(stderr, "usage: %s [--help | -h] | [--delete | -d] "
	   "<interface_name>\n", basename(prog));
	exit(-1);
}


int main(int argc, char **argv) {

	struct ifreq ifr, ifw;
	char if_name[IFNAMSIZ], *chan_name = "gsm";
	int option_index = 0, c, delete_if = 0, persist = 1, fd, sd;

	while((c = getopt_long(argc, argv, "dh?", long_options, &option_index))
	   != -1) {
		switch(c) {
			case 'd':
				delete_if = 1;
				break;
			case 'h':
			case '?':
			default:
				usage(argv[0]);
				break;
		}
	}

	if(optind >= argc)
		usage(argv[0]);

	chan_name = argv[optind];

	if(!delete_if && getuid() && geteuid())
		usage(argv[0]);

	// construct TUN interface
	if((fd = open("/dev/net/tun", O_RDWR)) == -1) {
		perror("open");
		return -1;
	}

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", chan_name);
	if(ioctl(fd, TUNSETIFF, (void *)&ifr) == -1) {
		perror("TUNSETIFF");
		close(fd);
		return -1;
	}

	// save actual name
	memcpy(if_name, ifr.ifr_name, IFNAMSIZ);

	if(delete_if)
		persist = 0;

	if(ioctl(fd, TUNSETPERSIST, (void *)persist) == -1) {
		perror("TUNSETPERSIST");
		close(fd);
		return -1;
	}

	if(delete_if) {
		close(fd);
		return 0;
	}

	// set interface up
	if((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		close(fd);
		return -1;
	}

	// get current flags
	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, if_name, IFNAMSIZ - 1);
	if(ioctl(sd, SIOCGIFFLAGS, &ifr) == -1) {
		perror("SIOCGIFFLAGS");
		close(sd);
		close(fd);
		return -1;
	}

	// set up
	memset(&ifw, 0, sizeof(ifw));
	strncpy(ifw.ifr_name, if_name, IFNAMSIZ - 1);
	ifw.ifr_flags = ifr.ifr_flags | IFF_UP | IFF_RUNNING;
	if(ioctl(sd, SIOCSIFFLAGS, &ifw) == -1) {
		perror("SIOCSIFFLAGS");
		close(sd);
		close(fd);
		return -1;
	}

	close(sd);
	close(fd);

	return 0;
}
personal git repositories of Harald Welte. Your mileage may vary