9 #include <sys/utsname.h>
10 #include <sys/socket.h>
14 #ifdef HAVE_LINUX_IF_H
15 #include <linux/if_tun.h>
16 #define LINUX_TUN_SUPPORTED
20 #ifdef HAVE_NET_ROUTE_H
21 #include <net/route.h>
24 #if defined(HAVE_STROPTS_H) && defined(HAVE_SYS_SOCKIO_H) && \
25 defined(HAVE_NET_IF_TUN_H)
26 #define HAVE_TUN_STREAMS
29 #ifdef HAVE_TUN_STREAMS
31 #include <sys/sockio.h>
32 #include <net/if_tun.h>
35 #define TUN_FLAVOUR_GUESS 0
36 #define TUN_FLAVOUR_BSD 1
37 #define TUN_FLAVOUR_LINUX 2
38 #define TUN_FLAVOUR_STREAMS 3
40 static struct flagstr flavours
[]={
41 {"guess", TUN_FLAVOUR_GUESS
},
42 {"bsd", TUN_FLAVOUR_BSD
},
43 {"BSD", TUN_FLAVOUR_BSD
},
44 {"linux", TUN_FLAVOUR_LINUX
},
45 {"streams", TUN_FLAVOUR_STREAMS
},
46 {"STREAMS", TUN_FLAVOUR_STREAMS
},
50 #define TUN_CONFIG_GUESS 0
51 #define TUN_CONFIG_IOCTL 1
52 #define TUN_CONFIG_BSD 2
53 #define TUN_CONFIG_LINUX 3
54 #define TUN_CONFIG_SOLARIS25 4
56 static struct flagstr config_types
[]={
57 {"guess", TUN_CONFIG_GUESS
},
58 {"ioctl", TUN_CONFIG_IOCTL
},
59 {"bsd", TUN_CONFIG_BSD
},
60 {"BSD", TUN_CONFIG_BSD
},
61 {"linux", TUN_CONFIG_LINUX
},
62 {"solaris-2.5", TUN_CONFIG_SOLARIS25
},
66 /* Connection to the kernel through the universal TUN/TAP driver */
71 cstring_t device_path
;
73 string_t interface_name
;
74 cstring_t ifconfig_path
;
75 uint32_t ifconfig_type
;
79 bool_t search_for_if
; /* Applies to tun-BSD only */
80 struct buffer_if
*buff
; /* We receive packets into here
81 and send them to the netlink code. */
82 netlink_deliver_fn
*netlink_to_tunnel
;
83 uint32_t local_address
; /* host interface address */
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
,st
->nl
.max_start_pad
);
119 l
=read(st
->fd
,st
->buff
->start
,st
->buff
->len
-st
->nl
.max_start_pad
);
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
)
138 BUF_ASSERT_USED(buf
);
139 /* No error checking, because we'd just throw the packet away
140 anyway if it didn't work. */
141 write(st
->fd
,buf
->start
,buf
->size
);
145 static bool_t
tun_set_route(void *sst
, struct netlink_client
*routes
)
148 string_t network
, mask
, secnetaddr
;
149 struct subnet_list
*nets
;
153 if (routes
->up
== routes
->kup
) return False
;
154 if (st
->route_type
==TUN_CONFIG_IOCTL
) {
155 if (st
->tun_flavour
==TUN_FLAVOUR_STREAMS
) {
156 fd
=open(st
->ip_path
,O_RDWR
);
158 fatal_perror("tun_set_route: can't open %s",st
->ip_path
);
161 fd
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
163 fatal_perror("tun_set_route: socket()");
167 nets
=routes
->subnets
;
168 secnetaddr
=ipaddr_to_string(st
->nl
.secnet_address
);
169 for (i
=0; i
<nets
->entries
; i
++) {
170 network
=ipaddr_to_string(nets
->list
[i
].prefix
);
171 mask
=ipaddr_to_string(nets
->list
[i
].mask
);
172 Message(M_INFO
,"%s: %s route %s/%d %s kernel routing table\n",
173 st
->nl
.name
,routes
->up?
"adding":"deleting",network
,
174 nets
->list
[i
].len
,routes
->up?
"to":"from");
175 switch (st
->route_type
) {
176 case TUN_CONFIG_LINUX
:
177 sys_cmd(st
->route_path
,"route",routes
->up?
"add":"del",
178 "-net",network
,"netmask",mask
,
179 "gw",secnetaddr
,(char *)0);
182 sys_cmd(st
->route_path
,"route",routes
->up?
"add":"del",
183 "-net",network
,secnetaddr
,mask
,(char *)0);
185 case TUN_CONFIG_SOLARIS25
:
186 sys_cmd(st
->route_path
,"route",routes
->up?
"add":"del",
187 network
,secnetaddr
,(char *)0);
189 case TUN_CONFIG_IOCTL
:
191 /* darwin rtentry has a different format, use /sbin/route instead */
192 #if HAVE_NET_ROUTE_H && ! __APPLE__
194 struct sockaddr_in
*sa
;
197 memset(&rt
,0,sizeof(rt
));
198 sa
=(struct sockaddr_in
*)&rt
.rt_dst
;
199 sa
->sin_family
=AF_INET
;
200 sa
->sin_addr
.s_addr
=htonl(nets
->list
[i
].prefix
);
201 sa
=(struct sockaddr_in
*)&rt
.rt_genmask
;
202 sa
->sin_family
=AF_INET
;
203 sa
->sin_addr
.s_addr
=htonl(nets
->list
[i
].mask
);
204 sa
=(struct sockaddr_in
*)&rt
.rt_gateway
;
205 sa
->sin_family
=AF_INET
;
206 sa
->sin_addr
.s_addr
=htonl(st
->nl
.secnet_address
);
207 rt
.rt_flags
=RTF_UP
|RTF_GATEWAY
;
208 action
=routes
->up?SIOCADDRT
:SIOCDELRT
;
209 if (ioctl(fd
,action
,&rt
)<0) {
210 fatal_perror("tun_set_route: ioctl()");
213 fatal("tun_set_route: ioctl method not supported");
218 fatal("tun_set_route: unsupported route command type");
221 free(network
); free(mask
);
224 if (st
->route_type
==TUN_CONFIG_IOCTL
) {
227 routes
->kup
=routes
->up
;
231 static void tun_phase_hook(void *sst
, uint32_t newphase
)
234 string_t hostaddr
,secnetaddr
;
236 struct netlink_client
*r
;
238 if (st
->tun_flavour
==TUN_FLAVOUR_BSD
) {
239 if (st
->search_for_if
) {
243 dname
=safe_malloc(strlen(st
->device_path
)+4,"tun_old_apply");
244 st
->interface_name
=safe_malloc(8,"tun_phase_hook");
246 for (i
=0; i
<255; i
++) {
247 sprintf(dname
,"%s%d",st
->device_path
,i
);
248 if ((st
->fd
=open(dname
,O_RDWR
))>0) {
249 sprintf(st
->interface_name
,"tun%d",i
);
250 Message(M_INFO
,"%s: allocated network interface %s "
251 "through %s\n",st
->nl
.name
,st
->interface_name
,
257 fatal("%s: unable to open any TUN device (%s...)",
258 st
->nl
.name
,st
->device_path
);
261 st
->fd
=open(st
->device_path
,O_RDWR
);
263 fatal_perror("%s: unable to open TUN device file %s",
264 st
->nl
.name
,st
->device_path
);
267 } else if (st
->tun_flavour
==TUN_FLAVOUR_LINUX
) {
268 #ifdef LINUX_TUN_SUPPORTED
271 /* New TUN interface: open the device, then do ioctl TUNSETIFF
272 to set or find out the network interface name. */
273 st
->fd
=open(st
->device_path
,O_RDWR
);
275 fatal_perror("%s: can't open device file %s",st
->nl
.name
,
278 memset(&ifr
,0,sizeof(ifr
));
279 ifr
.ifr_flags
= IFF_TUN
| IFF_NO_PI
; /* Just send/receive IP packets,
281 if (st
->interface_name
)
282 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
283 if (ioctl(st
->fd
,TUNSETIFF
,&ifr
)<0) {
284 fatal_perror("%s: ioctl(TUNSETIFF)",st
->nl
.name
);
286 if (!st
->interface_name
) {
287 st
->interface_name
=safe_malloc(strlen(ifr
.ifr_name
)+1,"tun_apply");
288 strcpy(st
->interface_name
,ifr
.ifr_name
);
289 Message(M_INFO
,"%s: allocated network interface %s\n",st
->nl
.name
,
293 fatal("tun_phase_hook: TUN_FLAVOUR_LINUX unexpected");
294 #endif /* LINUX_TUN_SUPPORTED */
295 } else if (st
->tun_flavour
==TUN_FLAVOUR_STREAMS
) {
296 #ifdef HAVE_TUN_STREAMS
297 int tun_fd
, if_fd
, ppa
=-1, ip_fd
;
299 if ((ip_fd
=open(st
->ip_path
, O_RDWR
)) < 0) {
300 fatal_perror("%s: can't open %s",st
->nl
.name
,st
->ip_path
);
302 if ((tun_fd
=open(st
->device_path
,O_RDWR
)) < 0) {
303 fatal_perror("%s: can't open %s",st
->nl
.name
,st
->device_path
);
305 if ((ppa
=ioctl(tun_fd
,TUNNEWPPA
,ppa
)) < 0) {
306 fatal_perror("%s: can't assign new interface");
308 if ((if_fd
=open(st
->device_path
,O_RDWR
)) < 0) {
309 fatal_perror("%s: can't open %s (2)",st
->nl
.name
,st
->device_path
);
311 if (ioctl(if_fd
,I_PUSH
,"ip") < 0) {
312 fatal_perror("%s: can't push IP module",st
->nl
.name
);
314 if (ioctl(if_fd
,IF_UNITSEL
,(char *)&ppa
) < 0) {
315 fatal_perror("%s: can't set ppa %d",st
->nl
.name
,ppa
);
317 if (ioctl(ip_fd
, I_LINK
, if_fd
) < 0) {
318 fatal_perror("%s: can't link TUN device to IP",st
->nl
.name
);
320 st
->interface_name
=safe_malloc(10,"tun_apply");
321 sprintf(st
->interface_name
,"tun%d",ppa
);
324 fatal("tun_phase_hook: TUN_FLAVOUR_STREAMS unexpected");
325 #endif /* HAVE_TUN_STREAMS */
327 fatal("tun_phase_hook: unknown flavour of TUN");
329 /* All the networks we'll be using have been registered. Invoke ifconfig
330 to set the TUN device's address, and route to add routes to all
333 hostaddr
=ipaddr_to_string(st
->local_address
);
334 secnetaddr
=ipaddr_to_string(st
->nl
.secnet_address
);
335 snprintf(mtu
,sizeof(mtu
),"%d",st
->nl
.mtu
);
338 switch (st
->ifconfig_type
) {
339 case TUN_CONFIG_LINUX
:
340 sys_cmd(st
->ifconfig_path
,"ifconfig",st
->interface_name
,
341 hostaddr
,"netmask","255.255.255.255","-broadcast",
343 "pointopoint",secnetaddr
,"mtu",mtu
,"up",(char *)0);
346 sys_cmd(st
->ifconfig_path
,"ifconfig",st
->interface_name
,
347 hostaddr
,"netmask","255.255.255.255",
348 secnetaddr
,"mtu",mtu
,"up",(char *)0);
350 case TUN_CONFIG_SOLARIS25
:
351 sys_cmd(st
->ifconfig_path
,"ifconfig",st
->interface_name
,
352 hostaddr
,secnetaddr
,"mtu",mtu
,"up",(char *)0);
354 case TUN_CONFIG_IOCTL
:
355 #if HAVE_NET_IF_H && ! __APPLE__
359 struct sockaddr_in
*sa
;
360 fd
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
362 /* Interface address */
363 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
364 sa
=(struct sockaddr_in
*)&ifr
.ifr_addr
;
365 memset(sa
,0,sizeof(*sa
));
366 sa
->sin_family
=AF_INET
;
367 sa
->sin_addr
.s_addr
=htonl(st
->local_address
);
368 if (ioctl(fd
,SIOCSIFADDR
, &ifr
)!=0) {
369 fatal_perror("tun_apply: SIOCSIFADDR");
371 #ifdef SIOCSIFNETMASK
373 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
374 sa
=(struct sockaddr_in
*)&ifr
.ifr_netmask
;
375 memset(sa
,0,sizeof(*sa
));
376 sa
->sin_family
=AF_INET
;
377 sa
->sin_addr
.s_addr
=htonl(0xffffffff);
378 if (ioctl(fd
,SIOCSIFNETMASK
, &ifr
)!=0) {
379 fatal_perror("tun_apply: SIOCSIFNETMASK");
382 /* Destination address (point-to-point) */
383 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
384 sa
=(struct sockaddr_in
*)&ifr
.ifr_dstaddr
;
385 memset(sa
,0,sizeof(*sa
));
386 sa
->sin_family
=AF_INET
;
387 sa
->sin_addr
.s_addr
=htonl(st
->nl
.secnet_address
);
388 if (ioctl(fd
,SIOCSIFDSTADDR
, &ifr
)!=0) {
389 fatal_perror("tun_apply: SIOCSIFDSTADDR");
392 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
393 ifr
.ifr_mtu
=st
->nl
.mtu
;
394 if (ioctl(fd
,SIOCSIFMTU
, &ifr
)!=0) {
395 fatal_perror("tun_apply: SIOCSIFMTU");
398 strncpy(ifr
.ifr_name
,st
->interface_name
,IFNAMSIZ
);
399 ifr
.ifr_flags
=IFF_UP
|IFF_POINTOPOINT
|IFF_RUNNING
|IFF_NOARP
;
400 if (ioctl(fd
,SIOCSIFFLAGS
, &ifr
)!=0) {
401 fatal_perror("tun_apply: SIOCSIFFLAGS");
407 fatal("tun_apply: ifconfig by ioctl() not supported");
408 #endif /* HAVE_NET_IF_H */
411 fatal("tun_apply: unsupported ifconfig method");
415 for (r
=st
->nl
.clients
; r
; r
=r
->next
) {
419 /* Register for poll() */
420 register_for_poll(st
, tun_beforepoll
, tun_afterpoll
, 1, st
->nl
.name
);
423 static list_t
*tun_create(closure_t
*self
, struct cloc loc
, dict_t
*context
,
424 list_t
*args
,uint32_t default_flavour
)
429 string_t flavour
,type
;
431 st
=safe_malloc(sizeof(*st
),"tun_apply");
433 /* First parameter must be a dict */
434 item
=list_elem(args
,0);
435 if (!item
|| item
->type
!=t_dict
)
436 cfgfatal(loc
,"tun","parameter must be a dictionary\n");
438 dict
=item
->data
.dict
;
440 st
->netlink_to_tunnel
=
441 netlink_init(&st
->nl
,st
,loc
,dict
,
442 "netlink-tun",tun_set_route
,tun_deliver_to_kernel
);
444 flavour
=dict_read_string(dict
,"flavour",False
,"tun-netlink",loc
);
446 st
->tun_flavour
=string_to_word(flavour
,loc
,flavours
,"tun-flavour");
448 st
->tun_flavour
=default_flavour
;
450 st
->device_path
=dict_read_string(dict
,"device",False
,"tun-netlink",loc
);
451 st
->ip_path
=dict_read_string(dict
,"ip-path",False
,"tun-netlink",loc
);
452 st
->interface_name
=dict_read_string(dict
,"interface",False
,
454 st
->search_for_if
=dict_read_bool(dict
,"interface-search",False
,
455 "tun-netlink",loc
,st
->device_path
==NULL
);
457 type
=dict_read_string(dict
,"ifconfig-type",False
,"tun-netlink",loc
);
458 if (type
) st
->ifconfig_type
=string_to_word(type
,loc
,config_types
,
460 else st
->ifconfig_type
=TUN_CONFIG_GUESS
;
461 st
->ifconfig_path
=dict_read_string(dict
,"ifconfig-path",False
,
464 type
=dict_read_string(dict
,"route-type",False
,"tun-netlink",loc
);
465 if (type
) st
->route_type
=string_to_word(type
,loc
,config_types
,
467 else st
->route_type
=TUN_CONFIG_GUESS
;
468 st
->route_path
=dict_read_string(dict
,"route-path",False
,"tun-netlink",loc
);
470 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"tun-netlink",loc
);
471 st
->local_address
=string_item_to_ipaddr(
472 dict_find_item(dict
,"local-address", True
, "netlink", loc
),"netlink");
474 if (st
->tun_flavour
==TUN_FLAVOUR_GUESS
) {
475 /* If we haven't been told what type of TUN we're using, take
476 a guess based on the system details. */
479 fatal_perror("tun_create: uname");
481 if (strcmp(u
.sysname
,"Linux")==0) {
482 if (u
.release
[0]=='2' && u
.release
[1]=='.' && u
.release
[3]=='.') {
483 if (u
.release
[2]=='2') st
->tun_flavour
=TUN_FLAVOUR_BSD
;
484 else if (u
.release
[2]=='4') st
->tun_flavour
=TUN_FLAVOUR_LINUX
;
486 } else if (strcmp(u
.sysname
,"SunOS")==0) {
487 st
->tun_flavour
=TUN_FLAVOUR_STREAMS
;
488 } else if (strcmp(u
.sysname
,"FreeBSD")==0
489 || strcmp(u
.sysname
,"Darwin")==0) {
490 st
->tun_flavour
=TUN_FLAVOUR_BSD
;
493 if (st
->tun_flavour
==TUN_FLAVOUR_GUESS
) {
494 cfgfatal(loc
,"tun","cannot guess which type of TUN is in use; "
495 "specify the flavour explicitly\n");
498 if (st
->ifconfig_type
==TUN_CONFIG_GUESS
) {
499 switch (st
->tun_flavour
) {
500 case TUN_FLAVOUR_LINUX
:
501 st
->ifconfig_type
=TUN_CONFIG_IOCTL
;
503 case TUN_FLAVOUR_BSD
:
505 /* XXX on Linux we still want TUN_CONFIG_IOCTL. Perhaps we can
506 use this on BSD too. */
507 st
->ifconfig_type
=TUN_CONFIG_IOCTL
;
509 st
->ifconfig_type
=TUN_CONFIG_BSD
;
512 case TUN_FLAVOUR_STREAMS
:
513 st
->ifconfig_type
=TUN_CONFIG_BSD
;
517 if (st
->route_type
==TUN_CONFIG_GUESS
)
518 st
->route_type
=st
->ifconfig_type
;
520 if (st
->ifconfig_type
==TUN_CONFIG_GUESS
) {
521 cfgfatal(loc
,"tun","cannot guess which ifconfig method to use\n");
523 if (st
->route_type
==TUN_CONFIG_GUESS
) {
524 cfgfatal(loc
,"tun","cannot guess which route method to use\n");
527 if (st
->ifconfig_type
==TUN_CONFIG_IOCTL
&& st
->ifconfig_path
) {
528 cfgfatal(loc
,"tun","ifconfig-type \"ioctl\" is incompatible with "
531 if (st
->route_type
==TUN_CONFIG_IOCTL
&& st
->route_path
) {
532 cfgfatal(loc
,"tun","route-type \"ioctl\" is incompatible with "
536 Message(M_DEBUG_CONFIG
,"%s: tun flavour %s\n",st
->nl
.name
,
537 tun_flavour_str(st
->tun_flavour
));
538 switch (st
->tun_flavour
) {
539 case TUN_FLAVOUR_BSD
:
540 if (!st
->device_path
) st
->device_path
="/dev/tun";
542 case TUN_FLAVOUR_LINUX
:
543 if (!st
->device_path
) st
->device_path
="/dev/net/tun";
545 case TUN_FLAVOUR_STREAMS
:
546 if (!st
->device_path
) st
->device_path
="/dev/tun";
547 if (st
->interface_name
) cfgfatal(loc
,"tun","interface name cannot "
548 "be specified with STREAMS TUN\n");
552 if (!st
->ip_path
) st
->ip_path
="/dev/ip";
553 if (!st
->ifconfig_path
) st
->ifconfig_path
="ifconfig";
554 if (!st
->route_path
) st
->route_path
="route";
556 #ifndef HAVE_TUN_STREAMS
557 if (st
->tun_flavour
==TUN_FLAVOUR_STREAMS
) {
558 cfgfatal(loc
,"tun","TUN flavour STREAMS unsupported in this build "
562 #ifndef LINUX_TUN_SUPPORTED
563 if (st
->tun_flavour
==TUN_FLAVOUR_LINUX
) {
564 cfgfatal(loc
,"tun","TUN flavour LINUX unsupported in this build "
569 /* Old TUN interface: the network interface name depends on which
570 /dev/tunX file we open. If 'interface-search' is set to true, treat
571 'device' as the prefix and try numbers from 0--255. If it's set
572 to false, treat 'device' as the whole name, and require than an
573 appropriate interface name be specified. */
574 if (st
->tun_flavour
==TUN_FLAVOUR_BSD
) {
575 if (st
->search_for_if
&& st
->interface_name
) {
576 cfgfatal(loc
,"tun","you may not specify an interface name "
577 "in interface-search mode\n");
579 if (!st
->search_for_if
&& !st
->interface_name
) {
580 cfgfatal(loc
,"tun","you must specify an interface name "
581 "when you explicitly specify a TUN device file\n");
585 add_hook(PHASE_GETRESOURCES
,tun_phase_hook
,st
);
587 return new_closure(&st
->nl
.cl
);
590 static list_t
*tun_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
593 return tun_create(self
,loc
,context
,args
,TUN_FLAVOUR_GUESS
);
596 static list_t
*tun_bsd_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
599 Message(M_WARNING
,"(%s,%d): obsolete use of tun-old; replace with tun "
600 "and specify flavour \"bsd\".\n",loc
.file
,loc
.line
);
601 return tun_create(self
,loc
,context
,args
,TUN_FLAVOUR_BSD
);
604 void tun_module(dict_t
*dict
)
606 add_closure(dict
,"tun",tun_apply
);
607 add_closure(dict
,"tun-old",tun_bsd_apply
);