chiark / gitweb /
Static buffers: Replace references to bufs[b]
[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;
ee697dd9 101 BEFOREPOLL_WANT_FDS(1);
9d3a4132 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 120 if (l<0) {
ba703386 121 if (errno==EINTR || iswouldblock(errno)) return;
9d3a4132
SE
122 fatal_perror("tun_afterpoll: read()");
123 }
124 if (l==0) {
4f5e39ec 125 fatal("tun_afterpoll: read()=0; device gone away?");
9d3a4132
SE
126 }
127 if (l>0) {
128 st->buff->size=l;
469fd1d9 129 st->netlink_to_tunnel(&st->nl,st->buff);
9d3a4132
SE
130 BUF_ASSERT_FREE(st->buff);
131 }
132 }
133}
134
469fd1d9 135static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf)
9d3a4132
SE
136{
137 struct tun *st=sst;
090dbeef 138 ssize_t rc;
9d3a4132
SE
139
140 BUF_ASSERT_USED(buf);
090dbeef
RK
141
142 /* Log errors, so we can tell what's going on, but only once a
143 minute, so we don't flood the logs. Short writes count as
144 errors. */
145 rc = write(st->fd,buf->start,buf->size);
146 if(rc != buf->size) {
147 static struct timeval last_report;
148 if(tv_now_global.tv_sec >= last_report.tv_sec + 60) {
149 if(rc < 0)
150 Message(M_WARNING,
151 "failed to deliver packet to tun device: %s\n",
152 strerror(errno));
153 else
154 Message(M_WARNING,
155 "truncated packet delivered to tun device\n");
156 last_report = tv_now_global;
157 }
158 }
9d3a4132
SE
159 BUF_FREE(buf);
160}
161
d3fe100d 162static bool_t tun_set_route(void *sst, struct netlink_client *routes)
9d3a4132
SE
163{
164 struct tun *st=sst;
165 string_t network, mask, secnetaddr;
d3fe100d 166 struct subnet_list *nets;
1caa23ff 167 int32_t i;
ff05a229 168 int fd=-1;
efacf9e0 169 bool_t up;
ff05a229 170
efacf9e0
ST
171 if (routes->options & OPT_SOFTROUTE)
172 up = routes->up;
173 else
174 up = routes->link_quality > LINK_QUALITY_UNUSED;
175
176 if (up == routes->kup) return False;
ff05a229
SE
177 if (st->route_type==TUN_CONFIG_IOCTL) {
178 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
179 fd=open(st->ip_path,O_RDWR);
180 if (fd<0) {
181 fatal_perror("tun_set_route: can't open %s",st->ip_path);
182 }
183 } else {
184 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
185 if (fd<0) {
186 fatal_perror("tun_set_route: socket()");
187 }
188 }
189 }
190 nets=routes->subnets;
191 secnetaddr=ipaddr_to_string(st->nl.secnet_address);
192 for (i=0; i<nets->entries; i++) {
193 network=ipaddr_to_string(nets->list[i].prefix);
194 mask=ipaddr_to_string(nets->list[i].mask);
195 Message(M_INFO,"%s: %s route %s/%d %s kernel routing table\n",
efacf9e0
ST
196 st->nl.name,up?"adding":"deleting",network,
197 nets->list[i].len,up?"to":"from");
ff05a229
SE
198 switch (st->route_type) {
199 case TUN_CONFIG_LINUX:
efacf9e0 200 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
201 "-net",network,"netmask",mask,
202 "gw",secnetaddr,(char *)0);
203 break;
204 case TUN_CONFIG_BSD:
efacf9e0 205 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
206 "-net",network,secnetaddr,mask,(char *)0);
207 break;
208 case TUN_CONFIG_SOLARIS25:
efacf9e0 209 sys_cmd(st->route_path,"route",up?"add":"del",
ff05a229
SE
210 network,secnetaddr,(char *)0);
211 break;
212 case TUN_CONFIG_IOCTL:
213 {
ea7ec970
SE
214 /* darwin rtentry has a different format, use /sbin/route instead */
215#if HAVE_NET_ROUTE_H && ! __APPLE__
ff05a229
SE
216 struct rtentry rt;
217 struct sockaddr_in *sa;
218 int action;
219
076bb54e 220 FILLZERO(rt);
ff05a229
SE
221 sa=(struct sockaddr_in *)&rt.rt_dst;
222 sa->sin_family=AF_INET;
223 sa->sin_addr.s_addr=htonl(nets->list[i].prefix);
224 sa=(struct sockaddr_in *)&rt.rt_genmask;
225 sa->sin_family=AF_INET;
226 sa->sin_addr.s_addr=htonl(nets->list[i].mask);
227 sa=(struct sockaddr_in *)&rt.rt_gateway;
228 sa->sin_family=AF_INET;
229 sa->sin_addr.s_addr=htonl(st->nl.secnet_address);
230 rt.rt_flags=RTF_UP|RTF_GATEWAY;
efacf9e0 231 action=up?SIOCADDRT:SIOCDELRT;
ff05a229
SE
232 if (ioctl(fd,action,&rt)<0) {
233 fatal_perror("tun_set_route: ioctl()");
234 }
235#else
4f5e39ec 236 fatal("tun_set_route: ioctl method not supported");
ff05a229 237#endif
d3fe100d 238 }
ff05a229
SE
239 break;
240 default:
4f5e39ec 241 fatal("tun_set_route: unsupported route command type");
ff05a229
SE
242 break;
243 }
ff05a229 244 }
56fd04e4 245 if (fd >= 0) {
ff05a229 246 close(fd);
9d3a4132 247 }
efacf9e0 248 routes->kup=up;
ff05a229 249 return True;
9d3a4132
SE
250}
251
252static void tun_phase_hook(void *sst, uint32_t newphase)
253{
254 struct tun *st=sst;
255 string_t hostaddr,secnetaddr;
7c006408 256 char mtu[6];
d3fe100d 257 struct netlink_client *r;
9d3a4132 258
ff05a229 259 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
9d3a4132
SE
260 if (st->search_for_if) {
261 string_t dname;
262 int i;
263
9d3a4132
SE
264 dname=safe_malloc(strlen(st->device_path)+4,"tun_old_apply");
265 st->interface_name=safe_malloc(8,"tun_phase_hook");
266
267 for (i=0; i<255; i++) {
268 sprintf(dname,"%s%d",st->device_path,i);
269 if ((st->fd=open(dname,O_RDWR))>0) {
270 sprintf(st->interface_name,"tun%d",i);
271 Message(M_INFO,"%s: allocated network interface %s "
272 "through %s\n",st->nl.name,st->interface_name,
273 dname);
274 break;
275 }
276 }
277 if (st->fd==-1) {
4f5e39ec 278 fatal("%s: unable to open any TUN device (%s...)",
9d3a4132
SE
279 st->nl.name,st->device_path);
280 }
281 } else {
282 st->fd=open(st->device_path,O_RDWR);
283 if (st->fd==-1) {
284 fatal_perror("%s: unable to open TUN device file %s",
285 st->nl.name,st->device_path);
286 }
287 }
ff05a229
SE
288 } else if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
289#ifdef LINUX_TUN_SUPPORTED
9d3a4132
SE
290 struct ifreq ifr;
291
292 /* New TUN interface: open the device, then do ioctl TUNSETIFF
293 to set or find out the network interface name. */
294 st->fd=open(st->device_path,O_RDWR);
295 if (st->fd==-1) {
296 fatal_perror("%s: can't open device file %s",st->nl.name,
297 st->device_path);
298 }
076bb54e 299 FILLZERO(ifr);
9d3a4132
SE
300 ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets,
301 no extra headers */
302 if (st->interface_name)
303 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
9d3a4132
SE
304 if (ioctl(st->fd,TUNSETIFF,&ifr)<0) {
305 fatal_perror("%s: ioctl(TUNSETIFF)",st->nl.name);
306 }
307 if (!st->interface_name) {
308 st->interface_name=safe_malloc(strlen(ifr.ifr_name)+1,"tun_apply");
309 strcpy(st->interface_name,ifr.ifr_name);
310 Message(M_INFO,"%s: allocated network interface %s\n",st->nl.name,
311 st->interface_name);
312 }
313#else
4f5e39ec 314 fatal("tun_phase_hook: TUN_FLAVOUR_LINUX unexpected");
ff05a229
SE
315#endif /* LINUX_TUN_SUPPORTED */
316 } else if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
317#ifdef HAVE_TUN_STREAMS
318 int tun_fd, if_fd, ppa=-1, ip_fd;
319
320 if ((ip_fd=open(st->ip_path, O_RDWR)) < 0) {
321 fatal_perror("%s: can't open %s",st->nl.name,st->ip_path);
322 }
323 if ((tun_fd=open(st->device_path,O_RDWR)) < 0) {
324 fatal_perror("%s: can't open %s",st->nl.name,st->device_path);
325 }
326 if ((ppa=ioctl(tun_fd,TUNNEWPPA,ppa)) < 0) {
327 fatal_perror("%s: can't assign new interface");
328 }
329 if ((if_fd=open(st->device_path,O_RDWR)) < 0) {
330 fatal_perror("%s: can't open %s (2)",st->nl.name,st->device_path);
331 }
332 if (ioctl(if_fd,I_PUSH,"ip") < 0) {
333 fatal_perror("%s: can't push IP module",st->nl.name);
334 }
335 if (ioctl(if_fd,IF_UNITSEL,(char *)&ppa) < 0) {
336 fatal_perror("%s: can't set ppa %d",st->nl.name,ppa);
337 }
338 if (ioctl(ip_fd, I_LINK, if_fd) < 0) {
339 fatal_perror("%s: can't link TUN device to IP",st->nl.name);
340 }
341 st->interface_name=safe_malloc(10,"tun_apply");
342 sprintf(st->interface_name,"tun%d",ppa);
343 st->fd=tun_fd;
4fb0f88d
IJ
344 setcloexec(if_ifd);
345 setcloexec(ip_ifd);
ff05a229 346#else
4f5e39ec 347 fatal("tun_phase_hook: TUN_FLAVOUR_STREAMS unexpected");
ff05a229
SE
348#endif /* HAVE_TUN_STREAMS */
349 } else {
4f5e39ec 350 fatal("tun_phase_hook: unknown flavour of TUN");
9d3a4132
SE
351 }
352 /* All the networks we'll be using have been registered. Invoke ifconfig
353 to set the TUN device's address, and route to add routes to all
354 our networks. */
355
4fb0f88d 356 setcloexec(st->fd);
ba703386 357 setnonblock(st->fd);
4fb0f88d 358
091433c6 359 hostaddr=ipaddr_to_string(st->nl.local_address);
9d3a4132 360 secnetaddr=ipaddr_to_string(st->nl.secnet_address);
c1d2109a 361 snprintf(mtu,sizeof(mtu),"%d",st->nl.mtu);
9d3a4132
SE
362 mtu[5]=0;
363
fe5e9cc4 364 switch (st->ifconfig_type) {
ff05a229
SE
365 case TUN_CONFIG_LINUX:
366 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
367 hostaddr,"netmask","255.255.255.255","-broadcast",
368 "-multicast",
369 "pointopoint",secnetaddr,"mtu",mtu,"up",(char *)0);
370 break;
371 case TUN_CONFIG_BSD:
372 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
373 hostaddr,"netmask","255.255.255.255",
374 secnetaddr,"mtu",mtu,"up",(char *)0);
375 break;
376 case TUN_CONFIG_SOLARIS25:
377 sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
378 hostaddr,secnetaddr,"mtu",mtu,"up",(char *)0);
379 break;
380 case TUN_CONFIG_IOCTL:
ea7ec970 381#if HAVE_NET_IF_H && ! __APPLE__
ff05a229
SE
382 {
383 int fd;
384 struct ifreq ifr;
385 struct sockaddr_in *sa;
386 fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
387
388 /* Interface address */
389 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
390 sa=(struct sockaddr_in *)&ifr.ifr_addr;
076bb54e 391 FILLZERO(*sa);
ff05a229 392 sa->sin_family=AF_INET;
091433c6 393 sa->sin_addr.s_addr=htonl(st->nl.local_address);
ff05a229
SE
394 if (ioctl(fd,SIOCSIFADDR, &ifr)!=0) {
395 fatal_perror("tun_apply: SIOCSIFADDR");
396 }
397#ifdef SIOCSIFNETMASK
398 /* Netmask */
399 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
400 sa=(struct sockaddr_in *)&ifr.ifr_netmask;
076bb54e 401 FILLZERO(*sa);
ff05a229
SE
402 sa->sin_family=AF_INET;
403 sa->sin_addr.s_addr=htonl(0xffffffff);
404 if (ioctl(fd,SIOCSIFNETMASK, &ifr)!=0) {
405 fatal_perror("tun_apply: SIOCSIFNETMASK");
406 }
407#endif
408 /* Destination address (point-to-point) */
409 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
410 sa=(struct sockaddr_in *)&ifr.ifr_dstaddr;
076bb54e 411 FILLZERO(*sa);
ff05a229
SE
412 sa->sin_family=AF_INET;
413 sa->sin_addr.s_addr=htonl(st->nl.secnet_address);
414 if (ioctl(fd,SIOCSIFDSTADDR, &ifr)!=0) {
415 fatal_perror("tun_apply: SIOCSIFDSTADDR");
416 }
417 /* MTU */
418 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
419 ifr.ifr_mtu=st->nl.mtu;
420 if (ioctl(fd,SIOCSIFMTU, &ifr)!=0) {
421 fatal_perror("tun_apply: SIOCSIFMTU");
422 }
423 /* Flags */
424 strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
425 ifr.ifr_flags=IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP;
426 if (ioctl(fd,SIOCSIFFLAGS, &ifr)!=0) {
427 fatal_perror("tun_apply: SIOCSIFFLAGS");
428 }
9d3a4132 429
ff05a229
SE
430 close(fd);
431 }
432#else
4f5e39ec 433 fatal("tun_apply: ifconfig by ioctl() not supported");
ff05a229
SE
434#endif /* HAVE_NET_IF_H */
435 break;
436 default:
4f5e39ec 437 fatal("tun_apply: unsupported ifconfig method");
ff05a229
SE
438 break;
439 }
440
d3fe100d
SE
441 for (r=st->nl.clients; r; r=r->next) {
442 tun_set_route(st,r);
9d3a4132
SE
443 }
444
32654a31
IJ
445 add_hook(PHASE_CHILDPERSIST,childpersist_closefd_hook,&st->fd);
446
9d3a4132 447 /* Register for poll() */
32fc582f 448 register_for_poll(st, tun_beforepoll, tun_afterpoll, st->nl.name);
9d3a4132
SE
449}
450
ff05a229
SE
451static list_t *tun_create(closure_t *self, struct cloc loc, dict_t *context,
452 list_t *args,uint32_t default_flavour)
9d3a4132
SE
453{
454 struct tun *st;
455 item_t *item;
456 dict_t *dict;
ff05a229 457 string_t flavour,type;
9d3a4132 458
b7886fd4 459 NEW(st);
9d3a4132
SE
460
461 /* First parameter must be a dict */
462 item=list_elem(args,0);
463 if (!item || item->type!=t_dict)
464 cfgfatal(loc,"tun","parameter must be a dictionary\n");
465
466 dict=item->data.dict;
467
468 st->netlink_to_tunnel=
469 netlink_init(&st->nl,st,loc,dict,
470 "netlink-tun",tun_set_route,tun_deliver_to_kernel);
471
ff05a229
SE
472 flavour=dict_read_string(dict,"flavour",False,"tun-netlink",loc);
473 if (flavour)
474 st->tun_flavour=string_to_word(flavour,loc,flavours,"tun-flavour");
475 else
476 st->tun_flavour=default_flavour;
477
9d3a4132 478 st->device_path=dict_read_string(dict,"device",False,"tun-netlink",loc);
ff05a229 479 st->ip_path=dict_read_string(dict,"ip-path",False,"tun-netlink",loc);
9d3a4132
SE
480 st->interface_name=dict_read_string(dict,"interface",False,
481 "tun-netlink",loc);
ff05a229
SE
482 st->search_for_if=dict_read_bool(dict,"interface-search",False,
483 "tun-netlink",loc,st->device_path==NULL);
484
485 type=dict_read_string(dict,"ifconfig-type",False,"tun-netlink",loc);
486 if (type) st->ifconfig_type=string_to_word(type,loc,config_types,
487 "ifconfig-type");
488 else st->ifconfig_type=TUN_CONFIG_GUESS;
489 st->ifconfig_path=dict_read_string(dict,"ifconfig-path",False,
490 "tun-netlink",loc);
491
492 type=dict_read_string(dict,"route-type",False,"tun-netlink",loc);
493 if (type) st->route_type=string_to_word(type,loc,config_types,
494 "route-type");
495 else st->route_type=TUN_CONFIG_GUESS;
496 st->route_path=dict_read_string(dict,"route-path",False,"tun-netlink",loc);
9d3a4132 497
9d3a4132
SE
498 st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"tun-netlink",loc);
499
ff05a229
SE
500 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
501 /* If we haven't been told what type of TUN we're using, take
502 a guess based on the system details. */
503 struct utsname u;
504 if (uname(&u)<0) {
505 fatal_perror("tun_create: uname");
506 }
507 if (strcmp(u.sysname,"Linux")==0) {
621c2861 508 st->tun_flavour=TUN_FLAVOUR_LINUX;
ff05a229
SE
509 } else if (strcmp(u.sysname,"SunOS")==0) {
510 st->tun_flavour=TUN_FLAVOUR_STREAMS;
ea7ec970
SE
511 } else if (strcmp(u.sysname,"FreeBSD")==0
512 || strcmp(u.sysname,"Darwin")==0) {
ff05a229
SE
513 st->tun_flavour=TUN_FLAVOUR_BSD;
514 }
515 }
516 if (st->tun_flavour==TUN_FLAVOUR_GUESS) {
517 cfgfatal(loc,"tun","cannot guess which type of TUN is in use; "
518 "specify the flavour explicitly\n");
519 }
9d3a4132 520
ff05a229
SE
521 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
522 switch (st->tun_flavour) {
523 case TUN_FLAVOUR_LINUX:
524 st->ifconfig_type=TUN_CONFIG_IOCTL;
525 break;
526 case TUN_FLAVOUR_BSD:
ea7ec970 527#if __linux__
4f5e39ec
SE
528 /* XXX on Linux we still want TUN_CONFIG_IOCTL. Perhaps we can
529 use this on BSD too. */
530 st->ifconfig_type=TUN_CONFIG_IOCTL;
ea7ec970
SE
531#else
532 st->ifconfig_type=TUN_CONFIG_BSD;
533#endif
ff05a229
SE
534 break;
535 case TUN_FLAVOUR_STREAMS:
536 st->ifconfig_type=TUN_CONFIG_BSD;
537 break;
538 }
539 }
540 if (st->route_type==TUN_CONFIG_GUESS)
541 st->route_type=st->ifconfig_type;
9d3a4132 542
ff05a229
SE
543 if (st->ifconfig_type==TUN_CONFIG_GUESS) {
544 cfgfatal(loc,"tun","cannot guess which ifconfig method to use\n");
545 }
546 if (st->route_type==TUN_CONFIG_GUESS) {
547 cfgfatal(loc,"tun","cannot guess which route method to use\n");
548 }
9d3a4132 549
ff05a229
SE
550 if (st->ifconfig_type==TUN_CONFIG_IOCTL && st->ifconfig_path) {
551 cfgfatal(loc,"tun","ifconfig-type \"ioctl\" is incompatible with "
552 "ifconfig-path\n");
553 }
554 if (st->route_type==TUN_CONFIG_IOCTL && st->route_path) {
555 cfgfatal(loc,"tun","route-type \"ioctl\" is incompatible with "
556 "route-path\n");
557 }
9d3a4132 558
ff05a229
SE
559 Message(M_DEBUG_CONFIG,"%s: tun flavour %s\n",st->nl.name,
560 tun_flavour_str(st->tun_flavour));
561 switch (st->tun_flavour) {
562 case TUN_FLAVOUR_BSD:
563 if (!st->device_path) st->device_path="/dev/tun";
564 break;
565 case TUN_FLAVOUR_LINUX:
566 if (!st->device_path) st->device_path="/dev/net/tun";
567 break;
568 case TUN_FLAVOUR_STREAMS:
569 if (!st->device_path) st->device_path="/dev/tun";
570 if (st->interface_name) cfgfatal(loc,"tun","interface name cannot "
571 "be specified with STREAMS TUN\n");
572 break;
573 }
9d3a4132 574
ff05a229 575 if (!st->ip_path) st->ip_path="/dev/ip";
9d3a4132
SE
576 if (!st->ifconfig_path) st->ifconfig_path="ifconfig";
577 if (!st->route_path) st->route_path="route";
ff05a229
SE
578
579#ifndef HAVE_TUN_STREAMS
580 if (st->tun_flavour==TUN_FLAVOUR_STREAMS) {
581 cfgfatal(loc,"tun","TUN flavour STREAMS unsupported in this build "
582 "of secnet\n");
583 }
584#endif
585#ifndef LINUX_TUN_SUPPORTED
586 if (st->tun_flavour==TUN_FLAVOUR_LINUX) {
587 cfgfatal(loc,"tun","TUN flavour LINUX unsupported in this build "
588 "of secnet\n");
589 }
590#endif
9d3a4132
SE
591
592 /* Old TUN interface: the network interface name depends on which
593 /dev/tunX file we open. If 'interface-search' is set to true, treat
594 'device' as the prefix and try numbers from 0--255. If it's set
595 to false, treat 'device' as the whole name, and require than an
596 appropriate interface name be specified. */
ff05a229
SE
597 if (st->tun_flavour==TUN_FLAVOUR_BSD) {
598 if (st->search_for_if && st->interface_name) {
599 cfgfatal(loc,"tun","you may not specify an interface name "
600 "in interface-search mode\n");
601 }
602 if (!st->search_for_if && !st->interface_name) {
603 cfgfatal(loc,"tun","you must specify an interface name "
604 "when you explicitly specify a TUN device file\n");
605 }
9d3a4132
SE
606 }
607
9d3a4132
SE
608 add_hook(PHASE_GETRESOURCES,tun_phase_hook,st);
609
610 return new_closure(&st->nl.cl);
611}
612
ff05a229
SE
613static list_t *tun_apply(closure_t *self, struct cloc loc, dict_t *context,
614 list_t *args)
615{
616 return tun_create(self,loc,context,args,TUN_FLAVOUR_GUESS);
617}
618
619static list_t *tun_bsd_apply(closure_t *self, struct cloc loc, dict_t *context,
620 list_t *args)
621{
622 Message(M_WARNING,"(%s,%d): obsolete use of tun-old; replace with tun "
623 "and specify flavour \"bsd\".\n",loc.file,loc.line);
624 return tun_create(self,loc,context,args,TUN_FLAVOUR_BSD);
625}
626
9d3a4132
SE
627void tun_module(dict_t *dict)
628{
9d3a4132 629 add_closure(dict,"tun",tun_apply);
ff05a229 630 add_closure(dict,"tun-old",tun_bsd_apply);
9d3a4132 631}