- if (st->tun_old) {
- if (st->search_for_if) {
- string_t dname;
- int i;
-
- /* ASSERT st->interface_name */
- dname=safe_malloc(strlen(st->device_path)+4,"tun_old_apply");
- st->interface_name=safe_malloc(8,"tun_phase_hook");
-
- for (i=0; i<255; i++) {
- sprintf(dname,"%s%d",st->device_path,i);
- if ((st->fd=open(dname,O_RDWR))>0) {
- sprintf(st->interface_name,"tun%d",i);
- Message(M_INFO,"%s: allocated network interface %s "
- "through %s\n",st->nl.name,st->interface_name,
- dname);
- break;
- }
- }
- if (st->fd==-1) {
- fatal("%s: unable to open any TUN device (%s...)\n",
- st->nl.name,st->device_path);
- }
- } else {
- st->fd=open(st->device_path,O_RDWR);
- if (st->fd==-1) {
- fatal_perror("%s: unable to open TUN device file %s",
- st->nl.name,st->device_path);
- }
- }
- } else {
-#ifdef HAVE_LINUX_IF_H
- struct ifreq ifr;
-
- /* New TUN interface: open the device, then do ioctl TUNSETIFF
- to set or find out the network interface name. */
- st->fd=open(st->device_path,O_RDWR);
- if (st->fd==-1) {
- fatal_perror("%s: can't open device file %s",st->nl.name,
- st->device_path);
- }
- memset(&ifr,0,sizeof(ifr));
- ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets,
- no extra headers */
- if (st->interface_name)
- strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ);
- Message(M_INFO,"%s: about to ioctl(TUNSETIFF)...\n",st->nl.name);
- if (ioctl(st->fd,TUNSETIFF,&ifr)<0) {
- fatal_perror("%s: ioctl(TUNSETIFF)",st->nl.name);
- }
- if (!st->interface_name) {
- st->interface_name=safe_malloc(strlen(ifr.ifr_name)+1,"tun_apply");
- strcpy(st->interface_name,ifr.ifr_name);
- Message(M_INFO,"%s: allocated network interface %s\n",st->nl.name,
- st->interface_name);
- }
-#else
- fatal("netlink.c:tun_phase_hook:!tun_old unexpected\n");
-#endif /* HAVE_LINUX_IF_H */
- }
- /* All the networks we'll be using have been registered. Invoke ifconfig
- to set the TUN device's address, and route to add routes to all
- our networks. */
-
- hostaddr=ipaddr_to_string(st->nl.local_address);
- secnetaddr=ipaddr_to_string(st->nl.secnet_address);
- snprintf(mtu,6,"%d",st->nl.mtu);
- mtu[5]=0;
-
- sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name,
- hostaddr,"netmask","255.255.255.255","-broadcast",
- "pointopoint",secnetaddr,"mtu",mtu,"up",(char *)0);
-
- for (c=st->nl.clients; c; c=c->next) {
- for (i=0; i<c->networks->entries; i++) {
- network=ipaddr_to_string(c->networks->list[i].prefix);
- mask=ipaddr_to_string(c->networks->list[i].mask);
- sys_cmd(st->route_path,"route","add","-net",network,
- "netmask",mask,"gw",secnetaddr,(char *)0);
- }
- }
-
- /* Register for poll() */
- register_for_poll(st, tun_beforepoll, tun_afterpoll, 1, st->nl.name);
-}
-
-#ifdef HAVE_LINUX_IF_H
-static list_t *tun_apply(closure_t *self, struct cloc loc, dict_t *context,
- list_t *args)
-{
- struct tun *st;
- item_t *item;