changelog, Makefile.in: finalise 0.3.4
[secnet] / tun.c
CommitLineData
9d3a4132
SE
1#include "secnet.h"
2#include "util.h"
3#include "netlink.h"
4#include <stdio.h>
5#include <string.h>
090dbeef 6#include <errno.h>
9d3a4132
SE
7#include <unistd.h>
8#include <fcntl.h>
9#include <sys/ioctl.h>
ff05a229
SE
10#include <sys/utsname.h>
11#include <sys/socket.h>
9d3a4132 12
ff05a229
SE
13#ifdef HAVE_NET_IF_H
14#include <net/if.h>
9d3a4132 15#ifdef HAVE_LINUX_IF_H
9d3a4132 16#include <linux/if_tun.h>
ff05a229 17#define LINUX_TUN_SUPPORTED
9d3a4132 18#endif
ff05a229
SE
19#endif
20
21#ifdef HAVE_NET_ROUTE_H
22#include <net/route.h>
23#endif
24
25#if defined(HAVE_STROPTS_H) && defined(HAVE_SYS_SOCKIO_H) && \
26defined(HAVE_NET_IF_TUN_H)
27#define HAVE_TUN_STREAMS
28#endif
29
30#ifdef HAVE_TUN_STREAMS
31#include <stropts.h>
32#include <sys/sockio.h>
33#include <net/if_tun.h>
34#endif
35
36#define TUN_FLAVOUR_GUESS 0
37#define TUN_FLAVOUR_BSD 1
38#define TUN_FLAVOUR_LINUX 2
39#define TUN_FLAVOUR_STREAMS 3
40
41static 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},
48 {NULL, 0}
49};
9d3a4132 50
ff05a229
SE
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
56
57static 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},
64 {NULL, 0}
65};
9d3a4132
SE
66
67/* Connection to the kernel through the universal TUN/TAP driver */
68
69struct tun {
70 struct netlink nl;
71 int fd;
fe5e9cc4
SE
72 cstring_t device_path;
73 cstring_t ip_path;
9d3a4132 74 string_t interface_name;
fe5e9cc4 75 cstring_t ifconfig_path;
ff05a229 76 uint32_t ifconfig_type;
fe5e9cc4 77 cstring_t route_path;
ff05a229
SE
78 uint32_t route_type;
79 uint32_t tun_flavour;
80 bool_t search_for_if; /* Applies to tun-BSD only */
9d3a4132
SE
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;
84};
85
fe5e9cc4 86static cstring_t tun_flavour_str(uint32_t flavour)
ff05a229
SE
87{
88 switch (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";
94 }
95}
96
9d3a4132 97static int tun_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
90a39563 98 int *timeout_io)
9d3a4132
SE
99{
100 struct tun *st=sst;
101 *nfds_io=1;
102 fds[0].fd=st->fd;
fe5e9cc4 103 fds[0].events=POLLIN;
9d3a4132
SE
104 return 0;
105}
106
90a39563 107static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds)
9d3a4132
SE
108{
109 struct tun *st=sst;
110 int l;
111
7138d0c5 112 if (nfds==0) return;
9d3a4132
SE
113 if (fds[0].revents&POLLERR) {
114 printf("tun_afterpoll: hup!\n");
115 }
116 if (fds[0].revents&POLLIN) {
117 BUF_ALLOC(st->buff,"tun_afterpoll");
3abd18e8 118 buffer_init(st->buff,calculate_max_start_pad());
92795040 119 l=read(st->fd, st->buff->start, buf_remaining_space(st->buff));
9d3a4132
SE
120 if (l<0) {
121 fatal_perror("tun_afterpoll: read()");
122 }
123 if (l==0) {
4f5e39ec 124 fatal("tun_afterpoll: read()=0; device gone away?");
9d3a4132
SE
125 }
126 if (l>0) {
127 st->buff->size=l;
469fd1d9 128 st->netlink_to_tunnel(&st->nl,st->buff);
9d3a4132
SE
129 BUF_ASSERT_FREE(st->buff);
130 }
131 }
132}
133
469fd1d9 134static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf)
9d3a4132
SE
135{
136 struct tun *st=sst;
090dbeef 137 ssize_t rc;
9d3a4132
SE
138
139 BUF_ASSERT_USED(buf);
090dbeef
RK
140
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
143 errors. */
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) {
148 if(rc < 0)
149 Message(M_WARNING,
150 "failed to deliver packet to tun device: %s\n",
151 strerror(errno));
152 else
153 Message(M_WARNING,
154 "truncated packet delivered to tun device\n");
155 last_report = tv_now_global;
156 }
157 }
9d3a4132
SE
158 BUF_FREE(buf);
159}
160
d3fe100d 161static bool_t tun_set_route(void *sst, struct netlink_client *routes)
9d3a4132
SE
162{
163 struct tun *st=sst;
164 string_t network, mask, secnetaddr;
d3fe100d 165 struct subnet_list *nets;
1caa23ff 166 int32_t i;
ff05a229 167 int fd=-1;
efacf9e0 168 bool_t up;
ff05a229 169
efacf9e0
ST
170 if (routes->options & OPT_SOFTROUTE)
171 up = routes->up;
172 else
173 up = routes->link_quality > LINK_QUALITY_UNUSED;
174
175 if (up == routes->kup) return False;
ff05a229
SE
176 if (st->route_type==TUN_CONFIG_IOCTL) {
177 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
178 fd=open(st->ip_path,O_RDWR);
179 if (fd<0) {
180 fatal_perror("tun_set_route: can't open %s",st->ip_path);
181 }
182 } else {
183 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
184 if (fd<0) {
185 fatal_perror("tun_set_route: socket()");
186 }
187 }
188 }
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",
efacf9e0
ST
195 st->nl.name,up?"adding":"deleting",network,
196 nets->list[i].len,up?"to":"from");
ff05a229
SE
197 switch (st->route_type) {
198 case TUN_CONFIG_LINUX:
efacf9e0 199 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
200 "-net",network,"netmask",mask,
201 "gw",secnetaddr,(char *)0);
202 break;
203 case TUN_CONFIG_BSD:
efacf9e0 204 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
205 "-net",network,secnetaddr,mask,(char *)0);
206 break;
207 case TUN_CONFIG_SOLARIS25:
efacf9e0 208 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
209 network,secnetaddr,(char *)0);
210 break;
211 case TUN_CONFIG_IOCTL:
212 {
ea7ec970
SE
213 /* darwin rtentry has a different format, use /sbin/route instead */
214#if HAVE_NET_ROUTE_H && ! __APPLE__
ff05a229
SE
215 struct rtentry rt;
216 struct sockaddr_in *sa;
217 int action;
218
076bb54e 219 FILLZERO(rt);
ff05a229
SE
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;
efacf9e0 230 action=up?SIOCADDRT:SIOCDELRT;
ff05a229
SE
231 if (ioctl(fd,action,&rt)<0) {
232 fatal_perror("tun_set_route: ioctl()");
233 }
234#else
4f5e39ec 235 fatal("tun_set_route: ioctl method not supported");
ff05a229 236#endif
d3fe100d 237 }
ff05a229
SE
238 break;
239 default:
4f5e39ec 240 fatal("tun_set_route: unsupported route command type");
ff05a229
SE
241 break;
242 }
ff05a229 243 }
ff05a229
SE
244 if (st->route_type==TUN_CONFIG_IOCTL) {
245 close(fd);
9d3a4132 246 }
efacf9e0 247 routes->kup=up;
ff05a229 248 return True;
9d3a4132
SE
249}
250
251static void tun_phase_hook(void *sst, uint32_t newphase)
252{
253 struct tun *st=sst;
254 string_t hostaddr,secnetaddr;
7c006408 255 char mtu[6];
d3fe100d 256 struct netlink_client *r;
9d3a4132 257
ff05a229 258 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
9d3a4132
SE
259 if (st->search_for_if) {
260 string_t dname;
261 int i;
262
9d3a4132
SE
263 dname=safe_malloc(strlen(st->device_path)+4,"tun_old_apply");
264 st->interface_name=safe_malloc(8,"tun_phase_hook");
265
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,
272 dname);
273 break;
274 }
275 }
276 if (st->fd==-1) {
4f5e39ec 277 fatal("%s: unable to open any TUN device (%s...)",
9d3a4132
SE
278 st->nl.name,st->device_path);
279 }
280 } else {
281 st->fd=open(st->device_path,O_RDWR);
282 if (st->fd==-1) {
283 fatal_perror("%s: unable to open TUN device file %s",
284 st->nl.name,st->device_path);
285 }
286 }
ff05a229
SE
287 } else if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
288#ifdef LINUX_TUN_SUPPORTED
9d3a4132
SE
289 struct ifreq ifr;
290
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);
294 if (st->fd==-1) {
295 fatal_perror("%s: can't open device file %s",st->nl.name,
296 st->device_path);
297 }
076bb54e 298 FILLZERO(ifr);
9d3a4132
SE
299 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets,
300 no extra headers */
301 if (st->interface_name)
302 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
9d3a4132
SE
303 if (ioctl(st->fd,TUNSETIFF,&ifr)<0) {
304 fatal_perror("%s: ioctl(TUNSETIFF)",st->nl.name);
305 }
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,
310 st->interface_name);
311 }
312#else
4f5e39ec 313 fatal("tun_phase_hook: TUN_FLAVOUR_LINUX unexpected");
ff05a229
SE
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;
318
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);
321 }
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);
324 }
325 if ((ppa=ioctl(tun_fd,TUNNEWPPA,ppa)) < 0) {
326 fatal_perror("%s: can't assign new interface");
327 }
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);
330 }
331 if (ioctl(if_fd,I_PUSH,"ip") < 0) {
332 fatal_perror("%s: can't push IP module",st->nl.name);
333 }
334 if (ioctl(if_fd,IF_UNITSEL,(char *)&ppa) < 0) {
335 fatal_perror("%s: can't set ppa %d",st->nl.name,ppa);
336 }
337 if (ioctl(ip_fd, I_LINK, if_fd) < 0) {
338 fatal_perror("%s: can't link TUN device to IP",st->nl.name);
339 }
340 st->interface_name=safe_malloc(10,"tun_apply");
341 sprintf(st->interface_name,"tun%d",ppa);
342 st->fd=tun_fd;
343#else
4f5e39ec 344 fatal("tun_phase_hook: TUN_FLAVOUR_STREAMS unexpected");
ff05a229
SE
345#endif /* HAVE_TUN_STREAMS */
346 } else {
4f5e39ec 347 fatal("tun_phase_hook: unknown flavour of TUN");
9d3a4132
SE
348 }
349 /* All the networks we'll be using have been registered. Invoke ifconfig
350 to set the TUN device's address, and route to add routes to all
351 our networks. */
352
091433c6 353 hostaddr=ipaddr_to_string(st->nl.local_address);
9d3a4132 354 secnetaddr=ipaddr_to_string(st->nl.secnet_address);
c1d2109a 355 snprintf(mtu,sizeof(mtu),"%d",st->nl.mtu);
9d3a4132
SE
356 mtu[5]=0;
357
fe5e9cc4 358 switch (st->ifconfig_type) {
ff05a229
SE
359 case TUN_CONFIG_LINUX:
360 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
361 hostaddr,"netmask","255.255.255.255","-broadcast",
362 "-multicast",
363 "pointopoint",secnetaddr,"mtu",mtu,"up",(char *)0);
364 break;
365 case TUN_CONFIG_BSD:
366 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
367 hostaddr,"netmask","255.255.255.255",
368 secnetaddr,"mtu",mtu,"up",(char *)0);
369 break;
370 case TUN_CONFIG_SOLARIS25:
371 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
372 hostaddr,secnetaddr,"mtu",mtu,"up",(char *)0);
373 break;
374 case TUN_CONFIG_IOCTL:
ea7ec970 375#if HAVE_NET_IF_H && ! __APPLE__
ff05a229
SE
376 {
377 int fd;
378 struct ifreq ifr;
379 struct sockaddr_in *sa;
380 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
381
382 /* Interface address */
383 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
384 sa=(struct sockaddr_in *)&ifr.ifr_addr;
076bb54e 385 FILLZERO(*sa);
ff05a229 386 sa->sin_family=AF_INET;
091433c6 387 sa->sin_addr.s_addr=htonl(st->nl.local_address);
ff05a229
SE
388 if (ioctl(fd,SIOCSIFADDR, &ifr)!=0) {
389 fatal_perror("tun_apply: SIOCSIFADDR");
390 }
391#ifdef SIOCSIFNETMASK
392 /* Netmask */
393 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
394 sa=(struct sockaddr_in *)&ifr.ifr_netmask;
076bb54e 395 FILLZERO(*sa);
ff05a229
SE
396 sa->sin_family=AF_INET;
397 sa->sin_addr.s_addr=htonl(0xffffffff);
398 if (ioctl(fd,SIOCSIFNETMASK, &ifr)!=0) {
399 fatal_perror("tun_apply: SIOCSIFNETMASK");
400 }
401#endif
402 /* Destination address (point-to-point) */
403 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
404 sa=(struct sockaddr_in *)&ifr.ifr_dstaddr;
076bb54e 405 FILLZERO(*sa);
ff05a229
SE
406 sa->sin_family=AF_INET;
407 sa->sin_addr.s_addr=htonl(st->nl.secnet_address);
408 if (ioctl(fd,SIOCSIFDSTADDR, &ifr)!=0) {
409 fatal_perror("tun_apply: SIOCSIFDSTADDR");
410 }
411 /* MTU */
412 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
413 ifr.ifr_mtu=st->nl.mtu;
414 if (ioctl(fd,SIOCSIFMTU, &ifr)!=0) {
415 fatal_perror("tun_apply: SIOCSIFMTU");
416 }
417 /* Flags */
418 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
419 ifr.ifr_flags=IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP;
420 if (ioctl(fd,SIOCSIFFLAGS, &ifr)!=0) {
421 fatal_perror("tun_apply: SIOCSIFFLAGS");
422 }
9d3a4132 423
ff05a229
SE
424 close(fd);
425 }
426#else
4f5e39ec 427 fatal("tun_apply: ifconfig by ioctl() not supported");
ff05a229
SE
428#endif /* HAVE_NET_IF_H */
429 break;
430 default:
4f5e39ec 431 fatal("tun_apply: unsupported ifconfig method");
ff05a229
SE
432 break;
433 }
434
d3fe100d
SE
435 for (r=st->nl.clients; r; r=r->next) {
436 tun_set_route(st,r);
9d3a4132
SE
437 }
438
439 /* Register for poll() */
440 register_for_poll(st, tun_beforepoll, tun_afterpoll, 1, st->nl.name);
441}
442
ff05a229
SE
443static list_t *tun_create(closure_t *self, struct cloc loc, dict_t *context,
444 list_t *args,uint32_t default_flavour)
9d3a4132
SE
445{
446 struct tun *st;
447 item_t *item;
448 dict_t *dict;
ff05a229 449 string_t flavour,type;
9d3a4132
SE
450
451 st=safe_malloc(sizeof(*st),"tun_apply");
452
453 /* First parameter must be a dict */
454 item=list_elem(args,0);
455 if (!item || item->type!=t_dict)
456 cfgfatal(loc,"tun","parameter must be a dictionary\n");
457
458 dict=item->data.dict;
459
460 st->netlink_to_tunnel=
461 netlink_init(&st->nl,st,loc,dict,
462 "netlink-tun",tun_set_route,tun_deliver_to_kernel);
463
ff05a229
SE
464 flavour=dict_read_string(dict,"flavour",False,"tun-netlink",loc);
465 if (flavour)
466 st->tun_flavour=string_to_word(flavour,loc,flavours,"tun-flavour");
467 else
468 st->tun_flavour=default_flavour;
469
9d3a4132 470 st->device_path=dict_read_string(dict,"device",False,"tun-netlink",loc);
ff05a229 471 st->ip_path=dict_read_string(dict,"ip-path",False,"tun-netlink",loc);
9d3a4132
SE
472 st->interface_name=dict_read_string(dict,"interface",False,
473 "tun-netlink",loc);
ff05a229
SE
474 st->search_for_if=dict_read_bool(dict,"interface-search",False,
475 "tun-netlink",loc,st->device_path==NULL);
476
477 type=dict_read_string(dict,"ifconfig-type",False,"tun-netlink",loc);
478 if (type) st->ifconfig_type=string_to_word(type,loc,config_types,
479 "ifconfig-type");
480 else st->ifconfig_type=TUN_CONFIG_GUESS;
481 st->ifconfig_path=dict_read_string(dict,"ifconfig-path",False,
482 "tun-netlink",loc);
483
484 type=dict_read_string(dict,"route-type",False,"tun-netlink",loc);
485 if (type) st->route_type=string_to_word(type,loc,config_types,
486 "route-type");
487 else st->route_type=TUN_CONFIG_GUESS;
488 st->route_path=dict_read_string(dict,"route-path",False,"tun-netlink",loc);
9d3a4132 489
9d3a4132
SE
490 st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"tun-netlink",loc);
491
ff05a229
SE
492 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
493 /* If we haven't been told what type of TUN we're using, take
494 a guess based on the system details. */
495 struct utsname u;
496 if (uname(&u)<0) {
497 fatal_perror("tun_create: uname");
498 }
499 if (strcmp(u.sysname,"Linux")==0) {
621c2861 500 st->tun_flavour=TUN_FLAVOUR_LINUX;
ff05a229
SE
501 } else if (strcmp(u.sysname,"SunOS")==0) {
502 st->tun_flavour=TUN_FLAVOUR_STREAMS;
ea7ec970
SE
503 } else if (strcmp(u.sysname,"FreeBSD")==0
504 || strcmp(u.sysname,"Darwin")==0) {
ff05a229
SE
505 st->tun_flavour=TUN_FLAVOUR_BSD;
506 }
507 }
508 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
509 cfgfatal(loc,"tun","cannot guess which type of TUN is in use; "
510 "specify the flavour explicitly\n");
511 }
9d3a4132 512
ff05a229
SE
513 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
514 switch (st->tun_flavour) {
515 case TUN_FLAVOUR_LINUX:
516 st->ifconfig_type=TUN_CONFIG_IOCTL;
517 break;
518 case TUN_FLAVOUR_BSD:
ea7ec970 519#if __linux__
4f5e39ec
SE
520 /* XXX on Linux we still want TUN_CONFIG_IOCTL. Perhaps we can
521 use this on BSD too. */
522 st->ifconfig_type=TUN_CONFIG_IOCTL;
ea7ec970
SE
523#else
524 st->ifconfig_type=TUN_CONFIG_BSD;
525#endif
ff05a229
SE
526 break;
527 case TUN_FLAVOUR_STREAMS:
528 st->ifconfig_type=TUN_CONFIG_BSD;
529 break;
530 }
531 }
532 if (st->route_type==TUN_CONFIG_GUESS)
533 st->route_type=st->ifconfig_type;
9d3a4132 534
ff05a229
SE
535 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
536 cfgfatal(loc,"tun","cannot guess which ifconfig method to use\n");
537 }
538 if (st->route_type==TUN_CONFIG_GUESS) {
539 cfgfatal(loc,"tun","cannot guess which route method to use\n");
540 }
9d3a4132 541
ff05a229
SE
542 if (st->ifconfig_type==TUN_CONFIG_IOCTL && st->ifconfig_path) {
543 cfgfatal(loc,"tun","ifconfig-type \"ioctl\" is incompatible with "
544 "ifconfig-path\n");
545 }
546 if (st->route_type==TUN_CONFIG_IOCTL && st->route_path) {
547 cfgfatal(loc,"tun","route-type \"ioctl\" is incompatible with "
548 "route-path\n");
549 }
9d3a4132 550
ff05a229
SE
551 Message(M_DEBUG_CONFIG,"%s: tun flavour %s\n",st->nl.name,
552 tun_flavour_str(st->tun_flavour));
553 switch (st->tun_flavour) {
554 case TUN_FLAVOUR_BSD:
555 if (!st->device_path) st->device_path="/dev/tun";
556 break;
557 case TUN_FLAVOUR_LINUX:
558 if (!st->device_path) st->device_path="/dev/net/tun";
559 break;
560 case TUN_FLAVOUR_STREAMS:
561 if (!st->device_path) st->device_path="/dev/tun";
562 if (st->interface_name) cfgfatal(loc,"tun","interface name cannot "
563 "be specified with STREAMS TUN\n");
564 break;
565 }
9d3a4132 566
ff05a229 567 if (!st->ip_path) st->ip_path="/dev/ip";
9d3a4132
SE
568 if (!st->ifconfig_path) st->ifconfig_path="ifconfig";
569 if (!st->route_path) st->route_path="route";
ff05a229
SE
570
571#ifndef HAVE_TUN_STREAMS
572 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
573 cfgfatal(loc,"tun","TUN flavour STREAMS unsupported in this build "
574 "of secnet\n");
575 }
576#endif
577#ifndef LINUX_TUN_SUPPORTED
578 if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
579 cfgfatal(loc,"tun","TUN flavour LINUX unsupported in this build "
580 "of secnet\n");
581 }
582#endif
9d3a4132
SE
583
584 /* Old TUN interface: the network interface name depends on which
585 /dev/tunX file we open. If 'interface-search' is set to true, treat
586 'device' as the prefix and try numbers from 0--255. If it's set
587 to false, treat 'device' as the whole name, and require than an
588 appropriate interface name be specified. */
ff05a229
SE
589 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
590 if (st->search_for_if && st->interface_name) {
591 cfgfatal(loc,"tun","you may not specify an interface name "
592 "in interface-search mode\n");
593 }
594 if (!st->search_for_if && !st->interface_name) {
595 cfgfatal(loc,"tun","you must specify an interface name "
596 "when you explicitly specify a TUN device file\n");
597 }
9d3a4132
SE
598 }
599
9d3a4132
SE
600 add_hook(PHASE_GETRESOURCES,tun_phase_hook,st);
601
602 return new_closure(&st->nl.cl);
603}
604
ff05a229
SE
605static list_t *tun_apply(closure_t *self, struct cloc loc, dict_t *context,
606 list_t *args)
607{
608 return tun_create(self,loc,context,args,TUN_FLAVOUR_GUESS);
609}
610
611static list_t *tun_bsd_apply(closure_t *self, struct cloc loc, dict_t *context,
612 list_t *args)
613{
614 Message(M_WARNING,"(%s,%d): obsolete use of tun-old; replace with tun "
615 "and specify flavour \"bsd\".\n",loc.file,loc.line);
616 return tun_create(self,loc,context,args,TUN_FLAVOUR_BSD);
617}
618
9d3a4132
SE
619void tun_module(dict_t *dict)
620{
9d3a4132 621 add_closure(dict,"tun",tun_apply);
ff05a229 622 add_closure(dict,"tun-old",tun_bsd_apply);
9d3a4132 623}