10 #include <sys/utsname.h>
11 #include <sys/socket.h>
15 #ifdef HAVE_LINUX_IF_H
16 #include <linux/if_tun.h>
17 #define LINUX_TUN_SUPPORTED
21 #ifdef HAVE_NET_ROUTE_H
22 #include <net/route.h>
25 #if defined(HAVE_STROPTS_H) && defined(HAVE_SYS_SOCKIO_H) && \
26 defined(HAVE_NET_IF_TUN_H)
27 #define HAVE_TUN_STREAMS
30 #ifdef HAVE_TUN_STREAMS
32 #include <sys/sockio.h>
33 #include <net/if_tun.h>
36 #define TUN_FLAVOUR_GUESS 0
37 #define TUN_FLAVOUR_BSD 1
38 #define TUN_FLAVOUR_LINUX 2
39 #define TUN_FLAVOUR_STREAMS 3
41 static struct flagstr flavours[]={
42 {"guess", TUN_FLAVOUR_GUESS},
43 {"bsd", TUN_FLAVOUR_BSD},
44 {"BSD", TUN_FLAVOUR_BSD},
45 {"linux", TUN_FLAVOUR_LINUX},
46 {"streams", TUN_FLAVOUR_STREAMS},
47 {"STREAMS", TUN_FLAVOUR_STREAMS},
51 #define TUN_CONFIG_GUESS 0
52 #define TUN_CONFIG_IOCTL 1
53 #define TUN_CONFIG_BSD 2
54 #define TUN_CONFIG_LINUX 3
55 #define TUN_CONFIG_SOLARIS25 4
57 static struct flagstr config_types[]={
58 {"guess", TUN_CONFIG_GUESS},
59 {"ioctl", TUN_CONFIG_IOCTL},
60 {"bsd", TUN_CONFIG_BSD},
61 {"BSD", TUN_CONFIG_BSD},
62 {"linux", TUN_CONFIG_LINUX},
63 {"solaris-2.5", TUN_CONFIG_SOLARIS25},
67 /* Connection to the kernel through the universal TUN/TAP driver */
72 cstring_t device_path;
74 string_t interface_name;
75 cstring_t ifconfig_path;
76 uint32_t ifconfig_type;
80 bool_t search_for_if; /* Applies to tun-BSD only */
81 struct buffer_if *buff; /* We receive packets into here
82 and send them to the netlink code. */
83 netlink_deliver_fn *netlink_to_tunnel;
86 static cstring_t tun_flavour_str(uint32_t flavour)
89 case TUN_FLAVOUR_GUESS: return "guess";
90 case TUN_FLAVOUR_BSD: return "BSD";
91 case TUN_FLAVOUR_LINUX: return "linux";
92 case TUN_FLAVOUR_STREAMS: return "STREAMS";
93 default: return "unknown";
97 static int tun_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
103 fds[0].events=POLLIN;
107 static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds)
113 if (fds[0].revents&POLLERR) {
114 printf("tun_afterpoll: hup!\n");
116 if (fds[0].revents&POLLIN) {
117 BUF_ALLOC(st->buff,"tun_afterpoll");
118 buffer_init(st->buff,calculate_max_start_pad());
119 l=read(st->fd, st->buff->start, buf_remaining_space(st->buff));
121 fatal_perror("tun_afterpoll: read()");
124 fatal("tun_afterpoll: read()=0; device gone away?");
128 st->netlink_to_tunnel(&st->nl,st->buff);
129 BUF_ASSERT_FREE(st->buff);
134 static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf)
139 BUF_ASSERT_USED(buf);
141 /* Log errors, so we can tell what's going on, but only once a
142 minute, so we don't flood the logs. Short writes count as
144 rc = write(st->fd,buf->start,buf->size);
145 if(rc != buf->size) {
146 static struct timeval last_report;
147 if(tv_now_global.tv_sec >= last_report.tv_sec + 60) {
150 "failed to deliver packet to tun device: %s\n",
154 "truncated packet delivered to tun device\n");
155 last_report = tv_now_global;
161 static bool_t tun_set_route(void *sst, struct netlink_client *routes)
164 string_t network, mask, secnetaddr;
165 struct subnet_list *nets;
170 if (routes->options & OPT_SOFTROUTE)
173 up = routes->link_quality > LINK_QUALITY_UNUSED;
175 if (up == routes->kup) return False;
176 if (st->route_type==TUN_CONFIG_IOCTL) {
177 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
178 fd=open(st->ip_path,O_RDWR);
180 fatal_perror("tun_set_route: can't open %s",st->ip_path);
183 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
185 fatal_perror("tun_set_route: socket()");
189 nets=routes->subnets;
190 secnetaddr=ipaddr_to_string(st->nl.secnet_address);
191 for (i=0; i<nets->entries; i++) {
192 network=ipaddr_to_string(nets->list[i].prefix);
193 mask=ipaddr_to_string(nets->list[i].mask);
194 Message(M_INFO,"%s: %s route %s/%d %s kernel routing table\n",
195 st->nl.name,up?"adding":"deleting",network,
196 nets->list[i].len,up?"to":"from");
197 switch (st->route_type) {
198 case TUN_CONFIG_LINUX:
199 sys_cmd(st->route_path,"route",up?"add":"del",
200 "-net",network,"netmask",mask,
201 "gw",secnetaddr,(char *)0);
204 sys_cmd(st->route_path,"route",up?"add":"del",
205 "-net",network,secnetaddr,mask,(char *)0);
207 case TUN_CONFIG_SOLARIS25:
208 sys_cmd(st->route_path,"route",up?"add":"del",
209 network,secnetaddr,(char *)0);
211 case TUN_CONFIG_IOCTL:
213 /* darwin rtentry has a different format, use /sbin/route instead */
214 #if HAVE_NET_ROUTE_H && ! __APPLE__
216 struct sockaddr_in *sa;
220 sa=(struct sockaddr_in *)&rt.rt_dst;
221 sa->sin_family=AF_INET;
222 sa->sin_addr.s_addr=htonl(nets->list[i].prefix);
223 sa=(struct sockaddr_in *)&rt.rt_genmask;
224 sa->sin_family=AF_INET;
225 sa->sin_addr.s_addr=htonl(nets->list[i].mask);
226 sa=(struct sockaddr_in *)&rt.rt_gateway;
227 sa->sin_family=AF_INET;
228 sa->sin_addr.s_addr=htonl(st->nl.secnet_address);
229 rt.rt_flags=RTF_UP|RTF_GATEWAY;
230 action=up?SIOCADDRT:SIOCDELRT;
231 if (ioctl(fd,action,&rt)<0) {
232 fatal_perror("tun_set_route: ioctl()");
235 fatal("tun_set_route: ioctl method not supported");
240 fatal("tun_set_route: unsupported route command type");
251 static void tun_phase_hook(void *sst, uint32_t newphase)
254 string_t hostaddr,secnetaddr;
256 struct netlink_client *r;
258 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
259 if (st->search_for_if) {
263 dname=safe_malloc(strlen(st->device_path)+4,"tun_old_apply");
264 st->interface_name=safe_malloc(8,"tun_phase_hook");
266 for (i=0; i<255; i++) {
267 sprintf(dname,"%s%d",st->device_path,i);
268 if ((st->fd=open(dname,O_RDWR))>0) {
269 sprintf(st->interface_name,"tun%d",i);
270 Message(M_INFO,"%s: allocated network interface %s "
271 "through %s\n",st->nl.name,st->interface_name,
277 fatal("%s: unable to open any TUN device (%s...)",
278 st->nl.name,st->device_path);
281 st->fd=open(st->device_path,O_RDWR);
283 fatal_perror("%s: unable to open TUN device file %s",
284 st->nl.name,st->device_path);
287 } else if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
288 #ifdef LINUX_TUN_SUPPORTED
291 /* New TUN interface: open the device, then do ioctl TUNSETIFF
292 to set or find out the network interface name. */
293 st->fd=open(st->device_path,O_RDWR);
295 fatal_perror("%s: can't open device file %s",st->nl.name,
299 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets,
301 if (st->interface_name)
302 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
303 if (ioctl(st->fd,TUNSETIFF,&ifr)<0) {
304 fatal_perror("%s: ioctl(TUNSETIFF)",st->nl.name);
306 if (!st->interface_name) {
307 st->interface_name=safe_malloc(strlen(ifr.ifr_name)+1,"tun_apply");
308 strcpy(st->interface_name,ifr.ifr_name);
309 Message(M_INFO,"%s: allocated network interface %s\n",st->nl.name,
313 fatal("tun_phase_hook: TUN_FLAVOUR_LINUX unexpected");
314 #endif /* LINUX_TUN_SUPPORTED */
315 } else if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
316 #ifdef HAVE_TUN_STREAMS
317 int tun_fd, if_fd, ppa=-1, ip_fd;
319 if ((ip_fd=open(st->ip_path, O_RDWR)) < 0) {
320 fatal_perror("%s: can't open %s",st->nl.name,st->ip_path);
322 if ((tun_fd=open(st->device_path,O_RDWR)) < 0) {
323 fatal_perror("%s: can't open %s",st->nl.name,st->device_path);
325 if ((ppa=ioctl(tun_fd,TUNNEWPPA,ppa)) < 0) {
326 fatal_perror("%s: can't assign new interface");
328 if ((if_fd=open(st->device_path,O_RDWR)) < 0) {
329 fatal_perror("%s: can't open %s (2)",st->nl.name,st->device_path);
331 if (ioctl(if_fd,I_PUSH,"ip") < 0) {
332 fatal_perror("%s: can't push IP module",st->nl.name);
334 if (ioctl(if_fd,IF_UNITSEL,(char *)&ppa) < 0) {
335 fatal_perror("%s: can't set ppa %d",st->nl.name,ppa);
337 if (ioctl(ip_fd, I_LINK, if_fd) < 0) {
338 fatal_perror("%s: can't link TUN device to IP",st->nl.name);
340 st->interface_name=safe_malloc(10,"tun_apply");
341 sprintf(st->interface_name,"tun%d",ppa);
346 fatal("tun_phase_hook: TUN_FLAVOUR_STREAMS unexpected");
347 #endif /* HAVE_TUN_STREAMS */
349 fatal("tun_phase_hook: unknown flavour of TUN");
351 /* All the networks we'll be using have been registered. Invoke ifconfig
352 to set the TUN device's address, and route to add routes to all
357 hostaddr=ipaddr_to_string(st->nl.local_address);
358 secnetaddr=ipaddr_to_string(st->nl.secnet_address);
359 snprintf(mtu,sizeof(mtu),"%d",st->nl.mtu);
362 switch (st->ifconfig_type) {
363 case TUN_CONFIG_LINUX:
364 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
365 hostaddr,"netmask","255.255.255.255","-broadcast",
367 "pointopoint",secnetaddr,"mtu",mtu,"up",(char *)0);
370 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
371 hostaddr,"netmask","255.255.255.255",
372 secnetaddr,"mtu",mtu,"up",(char *)0);
374 case TUN_CONFIG_SOLARIS25:
375 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
376 hostaddr,secnetaddr,"mtu",mtu,"up",(char *)0);
378 case TUN_CONFIG_IOCTL:
379 #if HAVE_NET_IF_H && ! __APPLE__
383 struct sockaddr_in *sa;
384 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
386 /* Interface address */
387 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
388 sa=(struct sockaddr_in *)&ifr.ifr_addr;
390 sa->sin_family=AF_INET;
391 sa->sin_addr.s_addr=htonl(st->nl.local_address);
392 if (ioctl(fd,SIOCSIFADDR, &ifr)!=0) {
393 fatal_perror("tun_apply: SIOCSIFADDR");
395 #ifdef SIOCSIFNETMASK
397 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
398 sa=(struct sockaddr_in *)&ifr.ifr_netmask;
400 sa->sin_family=AF_INET;
401 sa->sin_addr.s_addr=htonl(0xffffffff);
402 if (ioctl(fd,SIOCSIFNETMASK, &ifr)!=0) {
403 fatal_perror("tun_apply: SIOCSIFNETMASK");
406 /* Destination address (point-to-point) */
407 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
408 sa=(struct sockaddr_in *)&ifr.ifr_dstaddr;
410 sa->sin_family=AF_INET;
411 sa->sin_addr.s_addr=htonl(st->nl.secnet_address);
412 if (ioctl(fd,SIOCSIFDSTADDR, &ifr)!=0) {
413 fatal_perror("tun_apply: SIOCSIFDSTADDR");
416 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
417 ifr.ifr_mtu=st->nl.mtu;
418 if (ioctl(fd,SIOCSIFMTU, &ifr)!=0) {
419 fatal_perror("tun_apply: SIOCSIFMTU");
422 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
423 ifr.ifr_flags=IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP;
424 if (ioctl(fd,SIOCSIFFLAGS, &ifr)!=0) {
425 fatal_perror("tun_apply: SIOCSIFFLAGS");
431 fatal("tun_apply: ifconfig by ioctl() not supported");
432 #endif /* HAVE_NET_IF_H */
435 fatal("tun_apply: unsupported ifconfig method");
439 for (r=st->nl.clients; r; r=r->next) {
443 /* Register for poll() */
444 register_for_poll(st, tun_beforepoll, tun_afterpoll, 1, st->nl.name);
447 static list_t *tun_create(closure_t *self, struct cloc loc, dict_t *context,
448 list_t *args,uint32_t default_flavour)
453 string_t flavour,type;
455 st=safe_malloc(sizeof(*st),"tun_apply");
457 /* First parameter must be a dict */
458 item=list_elem(args,0);
459 if (!item || item->type!=t_dict)
460 cfgfatal(loc,"tun","parameter must be a dictionary\n");
462 dict=item->data.dict;
464 st->netlink_to_tunnel=
465 netlink_init(&st->nl,st,loc,dict,
466 "netlink-tun",tun_set_route,tun_deliver_to_kernel);
468 flavour=dict_read_string(dict,"flavour",False,"tun-netlink",loc);
470 st->tun_flavour=string_to_word(flavour,loc,flavours,"tun-flavour");
472 st->tun_flavour=default_flavour;
474 st->device_path=dict_read_string(dict,"device",False,"tun-netlink",loc);
475 st->ip_path=dict_read_string(dict,"ip-path",False,"tun-netlink",loc);
476 st->interface_name=dict_read_string(dict,"interface",False,
478 st->search_for_if=dict_read_bool(dict,"interface-search",False,
479 "tun-netlink",loc,st->device_path==NULL);
481 type=dict_read_string(dict,"ifconfig-type",False,"tun-netlink",loc);
482 if (type) st->ifconfig_type=string_to_word(type,loc,config_types,
484 else st->ifconfig_type=TUN_CONFIG_GUESS;
485 st->ifconfig_path=dict_read_string(dict,"ifconfig-path",False,
488 type=dict_read_string(dict,"route-type",False,"tun-netlink",loc);
489 if (type) st->route_type=string_to_word(type,loc,config_types,
491 else st->route_type=TUN_CONFIG_GUESS;
492 st->route_path=dict_read_string(dict,"route-path",False,"tun-netlink",loc);
494 st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"tun-netlink",loc);
496 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
497 /* If we haven't been told what type of TUN we're using, take
498 a guess based on the system details. */
501 fatal_perror("tun_create: uname");
503 if (strcmp(u.sysname,"Linux")==0) {
504 st->tun_flavour=TUN_FLAVOUR_LINUX;
505 } else if (strcmp(u.sysname,"SunOS")==0) {
506 st->tun_flavour=TUN_FLAVOUR_STREAMS;
507 } else if (strcmp(u.sysname,"FreeBSD")==0
508 || strcmp(u.sysname,"Darwin")==0) {
509 st->tun_flavour=TUN_FLAVOUR_BSD;
512 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
513 cfgfatal(loc,"tun","cannot guess which type of TUN is in use; "
514 "specify the flavour explicitly\n");
517 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
518 switch (st->tun_flavour) {
519 case TUN_FLAVOUR_LINUX:
520 st->ifconfig_type=TUN_CONFIG_IOCTL;
522 case TUN_FLAVOUR_BSD:
524 /* XXX on Linux we still want TUN_CONFIG_IOCTL. Perhaps we can
525 use this on BSD too. */
526 st->ifconfig_type=TUN_CONFIG_IOCTL;
528 st->ifconfig_type=TUN_CONFIG_BSD;
531 case TUN_FLAVOUR_STREAMS:
532 st->ifconfig_type=TUN_CONFIG_BSD;
536 if (st->route_type==TUN_CONFIG_GUESS)
537 st->route_type=st->ifconfig_type;
539 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
540 cfgfatal(loc,"tun","cannot guess which ifconfig method to use\n");
542 if (st->route_type==TUN_CONFIG_GUESS) {
543 cfgfatal(loc,"tun","cannot guess which route method to use\n");
546 if (st->ifconfig_type==TUN_CONFIG_IOCTL && st->ifconfig_path) {
547 cfgfatal(loc,"tun","ifconfig-type \"ioctl\" is incompatible with "
550 if (st->route_type==TUN_CONFIG_IOCTL && st->route_path) {
551 cfgfatal(loc,"tun","route-type \"ioctl\" is incompatible with "
555 Message(M_DEBUG_CONFIG,"%s: tun flavour %s\n",st->nl.name,
556 tun_flavour_str(st->tun_flavour));
557 switch (st->tun_flavour) {
558 case TUN_FLAVOUR_BSD:
559 if (!st->device_path) st->device_path="/dev/tun";
561 case TUN_FLAVOUR_LINUX:
562 if (!st->device_path) st->device_path="/dev/net/tun";
564 case TUN_FLAVOUR_STREAMS:
565 if (!st->device_path) st->device_path="/dev/tun";
566 if (st->interface_name) cfgfatal(loc,"tun","interface name cannot "
567 "be specified with STREAMS TUN\n");
571 if (!st->ip_path) st->ip_path="/dev/ip";
572 if (!st->ifconfig_path) st->ifconfig_path="ifconfig";
573 if (!st->route_path) st->route_path="route";
575 #ifndef HAVE_TUN_STREAMS
576 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
577 cfgfatal(loc,"tun","TUN flavour STREAMS unsupported in this build "
581 #ifndef LINUX_TUN_SUPPORTED
582 if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
583 cfgfatal(loc,"tun","TUN flavour LINUX unsupported in this build "
588 /* Old TUN interface: the network interface name depends on which
589 /dev/tunX file we open. If 'interface-search' is set to true, treat
590 'device' as the prefix and try numbers from 0--255. If it's set
591 to false, treat 'device' as the whole name, and require than an
592 appropriate interface name be specified. */
593 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
594 if (st->search_for_if && st->interface_name) {
595 cfgfatal(loc,"tun","you may not specify an interface name "
596 "in interface-search mode\n");
598 if (!st->search_for_if && !st->interface_name) {
599 cfgfatal(loc,"tun","you must specify an interface name "
600 "when you explicitly specify a TUN device file\n");
604 add_hook(PHASE_GETRESOURCES,tun_phase_hook,st);
606 return new_closure(&st->nl.cl);
609 static list_t *tun_apply(closure_t *self, struct cloc loc, dict_t *context,
612 return tun_create(self,loc,context,args,TUN_FLAVOUR_GUESS);
615 static list_t *tun_bsd_apply(closure_t *self, struct cloc loc, dict_t *context,
618 Message(M_WARNING,"(%s,%d): obsolete use of tun-old; replace with tun "
619 "and specify flavour \"bsd\".\n",loc.file,loc.line);
620 return tun_create(self,loc,context,args,TUN_FLAVOUR_BSD);
623 void tun_module(dict_t *dict)
625 add_closure(dict,"tun",tun_apply);
626 add_closure(dict,"tun-old",tun_bsd_apply);