2 * tunemu - Tun device emulation for Darwin
3 * Copyright (C) 2009 Friedrich Schöller <friedrich.schoeller@gmail.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
24 #include <sys/ioctl.h>
37 #define PPPPROTO_CTL 1
42 #define SC_LOOP_TRAFFIC 0x00000200
44 #define PPPIOCNEWUNIT _IOWR('t', 62, int)
45 #define PPPIOCSFLAGS _IOW('t', 89, int)
46 #define PPPIOCSNPMODE _IOW('t', 75, struct npioctl)
47 #define PPPIOCATTCHAN _IOW('t', 56, int)
48 #define PPPIOCGCHAN _IOR('t', 55, int)
49 #define PPPIOCCONNECT _IOW('t', 58, int)
50 #define PPPIOCGUNIT _IOR('t', 86, int)
71 #define PPP_KEXT_PATH "/System/Library/Extensions/PPP.kext"
73 #define ERROR_BUFFER_SIZE 1024
75 char tunemu_error[ERROR_BUFFER_SIZE];
77 static int pcap_use_count = 0;
78 static pcap_t *pcap = NULL;
80 static int data_buffer_length = 0;
81 static char *data_buffer = NULL;
83 static void tun_error(char *format, ...) {
86 vsnprintf(tunemu_error, sizeof(tunemu_error), format, vl);
90 static void tun_noerror() {
94 static void closeall() {
95 int fd = getdtablesize();
101 open("/dev/null", O_RDWR, 0);
106 static int ppp_load_kext() {
110 tun_error("fork for ppp kext: %s", strerror(errno));
116 execle("/sbin/kextload", "kextload", PPP_KEXT_PATH, NULL, NULL);
122 while(waitpid(pid, &status, 0) < 0) {
127 tun_error("waitpid for ppp kext: %s", strerror(errno));
131 if(WEXITSTATUS(status) != 0) {
132 tun_error("could not load ppp kext \"%s\"", PPP_KEXT_PATH);
140 static int ppp_new_instance() {
142 int ppp_sockfd = socket(PF_PPP, SOCK_RAW, PPPPROTO_CTL);
145 if(ppp_load_kext() < 0) {
149 ppp_sockfd = socket(PF_PPP, SOCK_RAW, PPPPROTO_CTL);
152 tun_error("creating ppp socket: %s", strerror(errno));
157 // connect to ppp procotol
158 struct sockaddr_ppp pppaddr;
159 pppaddr.ppp_len = sizeof(struct sockaddr_ppp);
160 pppaddr.ppp_family = AF_PPP;
161 pppaddr.ppp_proto = PPPPROTO_CTL;
162 pppaddr.ppp_cookie = 0;
164 if(connect(ppp_sockfd, (struct sockaddr *)&pppaddr, sizeof(struct sockaddr_ppp)) < 0) {
165 tun_error("connecting ppp socket: %s", strerror(errno));
174 static int ppp_new_unit(int *unit_number) {
175 int fd = ppp_new_instance();
182 if(ioctl(fd, PPPIOCNEWUNIT, unit_number) < 0) {
183 tun_error("creating ppp unit: %s", strerror(errno));
192 static int ppp_setup_unit(int unit_fd) {
193 // send traffic to program
194 int flags = SC_LOOP_TRAFFIC;
196 if(ioctl(unit_fd, PPPIOCSFLAGS, &flags) < 0) {
197 tun_error("setting ppp loopback mode: %s", strerror(errno));
203 npi.protocol = PPP_IP;
204 npi.mode = NPMODE_PASS;
206 if(ioctl(unit_fd, PPPIOCSNPMODE, &npi) < 0) {
207 tun_error("starting ppp unit: %s", strerror(errno));
215 static int open_pcap() {
221 char errbuf[PCAP_ERRBUF_SIZE];
222 pcap = pcap_open_live("lo0", BUFSIZ, 0, 1, errbuf);
226 tun_error("opening pcap: %s", errbuf);
234 static void close_pcap() {
241 if(pcap_use_count == 0) {
247 static void allocate_data_buffer(int size) {
248 if(data_buffer_length < size) {
250 data_buffer_length = size;
251 data_buffer = malloc(data_buffer_length);
255 static void make_device_name(tunemu_device device, int unit_number) {
256 snprintf(device, sizeof(tunemu_device), "ppp%d", unit_number);
259 static int check_device_name(tunemu_device device) {
260 if(strlen(device) < 4) {
264 int unit_number = atoi(device + 3);
266 if(unit_number < 0 || unit_number > 999) {
270 tunemu_device compare;
271 make_device_name(compare, unit_number);
273 if(strcmp(device, compare) != 0) {
280 int tunemu_open(tunemu_device device) {
281 int ppp_unit_number = -1;
284 if(check_device_name(device) < 0) {
285 tun_error("invalid device name \"%s\"", device);
289 ppp_unit_number = atoi(device + 3);
292 int ppp_unit_fd = ppp_new_unit(&ppp_unit_number);
294 if(ppp_unit_fd < 0) {
298 if(ppp_setup_unit(ppp_unit_fd) < 0) {
303 if(open_pcap() < 0) {
308 make_device_name(device, ppp_unit_number);
313 int tunemu_close(int ppp_sockfd) {
314 int ret = close(ppp_sockfd);
323 int tunemu_read(int ppp_sockfd, char *buffer, int length) {
324 allocate_data_buffer(length + 2);
326 length = read(ppp_sockfd, data_buffer, length + 2);
329 tun_error("reading packet: %s", strerror(errno));
341 memcpy(buffer, data_buffer + 2, length);
346 int tunemu_write(int ppp_sockfd, char *buffer, int length) {
347 allocate_data_buffer(length + 4);
349 data_buffer[0] = 0x02;
350 data_buffer[1] = 0x00;
351 data_buffer[2] = 0x00;
352 data_buffer[3] = 0x00;
354 memcpy(data_buffer + 4, buffer, length);
357 tun_error("pcap not open");
361 length = pcap_inject(pcap, data_buffer, length + 4);
364 tun_error("injecting packet: %s", pcap_geterr(pcap));