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)
74 #define PPP_KEXT_PATH "/System/Library/Extensions/PPP.kext"
76 #define ERROR_BUFFER_SIZE 1024
78 char tunemu_error[ERROR_BUFFER_SIZE];
80 static int pcap_use_count = 0;
81 static pcap_t *pcap = NULL;
83 static int data_buffer_length = 0;
84 static char *data_buffer = NULL;
86 static void tun_error(char *format, ...)
90 vsnprintf(tunemu_error, ERROR_BUFFER_SIZE, format, vl);
94 static void tun_noerror()
99 static void closeall()
101 int fd = getdtablesize();
105 open("/dev/null", O_RDWR, 0);
110 static int ppp_load_kext()
115 tun_error("fork for ppp kext: %s", strerror(errno));
122 execle("/sbin/kextload", "kextload", PPP_KEXT_PATH, NULL, NULL);
127 while (waitpid(pid, &status, 0) < 0)
132 tun_error("waitpid for ppp kext: %s", strerror(errno));
136 if (WEXITSTATUS(status) != 0)
138 tun_error("could not load ppp kext \"%s\"", PPP_KEXT_PATH);
146 static int ppp_new_instance()
149 int ppp_sockfd = socket(PF_PPP, SOCK_RAW, PPPPROTO_CTL);
152 if (ppp_load_kext() < 0)
155 ppp_sockfd = socket(PF_PPP, SOCK_RAW, PPPPROTO_CTL);
158 tun_error("creating ppp socket: %s", strerror(errno));
163 // connect to ppp procotol
164 struct sockaddr_ppp pppaddr;
165 pppaddr.ppp_len = sizeof(struct sockaddr_ppp);
166 pppaddr.ppp_family = AF_PPP;
167 pppaddr.ppp_proto = PPPPROTO_CTL;
168 pppaddr.ppp_cookie = 0;
169 if (connect(ppp_sockfd, (struct sockaddr *)&pppaddr, sizeof(struct sockaddr_ppp)) < 0)
171 tun_error("connecting ppp socket: %s", strerror(errno));
180 static int ppp_new_unit(int *unit_number)
182 int fd = ppp_new_instance();
187 if (ioctl(fd, PPPIOCNEWUNIT, unit_number) < 0)
189 tun_error("creating ppp unit: %s", strerror(errno));
198 static int ppp_setup_unit(int unit_fd)
200 // send traffic to program
201 int flags = SC_LOOP_TRAFFIC;
202 if (ioctl(unit_fd, PPPIOCSFLAGS, &flags) < 0)
204 tun_error("setting ppp loopback mode: %s", strerror(errno));
210 npi.protocol = PPP_IP;
211 npi.mode = NPMODE_PASS;
212 if (ioctl(unit_fd, PPPIOCSNPMODE, &npi) < 0)
214 tun_error("starting ppp unit: %s", strerror(errno));
222 static int open_pcap()
230 char errbuf[PCAP_ERRBUF_SIZE];
231 pcap = pcap_open_live("lo0", BUFSIZ, 0, 1, errbuf);
236 tun_error("opening pcap: %s", errbuf);
244 static void close_pcap()
250 if (pcap_use_count == 0)
257 static void allocate_data_buffer(int size)
259 if (data_buffer_length < size)
262 data_buffer_length = size;
263 data_buffer = malloc(data_buffer_length);
267 static void make_device_name(tunemu_device device, int unit_number)
269 snprintf(device, sizeof(tunemu_device), "ppp%d", unit_number);
272 static int check_device_name(tunemu_device device)
274 if (strlen(device) < 4)
277 int unit_number = atoi(device + 3);
278 if (unit_number < 0 || unit_number > 999)
281 tunemu_device compare;
282 make_device_name(compare, unit_number);
284 if (strcmp(device, compare) != 0)
290 int tunemu_open(tunemu_device device)
292 int ppp_unit_number = -1;
295 if (check_device_name(device) < 0)
297 tun_error("invalid device name \"%s\"", device);
301 ppp_unit_number = atoi(device + 3);
304 int ppp_unit_fd = ppp_new_unit(&ppp_unit_number);
308 if (ppp_setup_unit(ppp_unit_fd) < 0)
320 make_device_name(device, ppp_unit_number);
325 int tunemu_close(int ppp_sockfd)
327 int ret = close(ppp_sockfd);
335 int tunemu_read(int ppp_sockfd, char *buffer, int length)
337 allocate_data_buffer(length + 2);
339 length = read(ppp_sockfd, data_buffer, length + 2);
342 tun_error("reading packet: %s", strerror(errno));
351 memcpy(buffer, data_buffer + 2, length);
356 int tunemu_write(int ppp_sockfd, char *buffer, int length)
358 allocate_data_buffer(length + 4);
360 data_buffer[0] = 0x02;
361 data_buffer[1] = 0x00;
362 data_buffer[2] = 0x00;
363 data_buffer[3] = 0x00;
365 memcpy(data_buffer + 4, buffer, length);
369 tun_error("pcap not open");
373 length = pcap_inject(pcap, data_buffer, length + 4);
376 tun_error("injecting packet: %s", pcap_geterr(pcap));