Correctly check if subnet owner exists.
[tinc] / src / solaris / device.c
1 /*
2     device.c -- Interaction with Solaris tun device
3     Copyright (C) 2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2001 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: device.c,v 1.1.2.2 2001/10/12 15:38:35 guus Exp $
21 */
22
23 #include <sys/sockio.h>
24 #include <sys/stropts.h>
25 #include <net/if_tun.h>
26
27 #define DEFAULT_DEVICE "/dev/tun"
28
29 int device_fd = -1;
30 int device_type;
31 char *device_fname;
32 char *device_info;
33
34 int device_total_in = 0;
35 int device_total_out = 0;
36
37 int setup_device(void)
38 {
39   int ip_fd = -1, if_fd = -1;
40   int ppa;
41   char *ptr;
42
43 cp
44   if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
45     device_fname = DEFAULT_DEVICE;
46
47 cp
48   if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
49     {
50       syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
51       return -1;
52     }
53 cp
54   ppa = 0;
55
56   ptr = fname;
57   while(*ptr && !isdigit((int)*ptr)) ptr++;
58   ppa = atoi(ptr);
59
60   if( (ip_fd = open("/dev/ip", O_RDWR, 0)) < 0){
61      syslog(LOG_ERR, _("Could not open /dev/ip: %m"));
62      return -1;
63   }
64
65   /* Assign a new PPA and get its unit number. */
66   if( (ppa = ioctl(fd, TUNNEWPPA, ppa)) < 0){
67      syslog(LOG_ERR, _("Can't assign new interface: %m"));
68      return -1;
69   }
70
71   if( (if_fd = open(fname, O_RDWR, 0)) < 0){
72      syslog(LOG_ERR, _("Could not open %s twice: %m"), fname);
73      return -1;
74   }
75
76   if(ioctl(if_fd, I_PUSH, "ip") < 0){
77      syslog(LOG_ERR, _("Can't push IP module: %m"));
78      return -1;
79   }
80
81   /* Assign ppa according to the unit number returned by tun device */
82   if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0){
83      syslog(LOG_ERR, _("Can't set PPA %d: %m"), ppa);
84      return -1;
85   }
86
87   if(ioctl(ip_fd, I_LINK, if_fd) < 0){
88      syslog(LOG_ERR, _("Can't link TUN device to IP: %m"));
89      return -1;
90   }
91
92   device_info = _("Solaris tun device");
93
94   /* Set default MAC address for ethertap devices */
95
96   mymac.type = SUBNET_MAC;
97   mymac.net.mac.address.x[0] = 0xfe;
98   mymac.net.mac.address.x[1] = 0xfd;
99   mymac.net.mac.address.x[2] = 0x00;
100   mymac.net.mac.address.x[3] = 0x00;
101   mymac.net.mac.address.x[4] = 0x00;
102   mymac.net.mac.address.x[5] = 0x00;
103
104   syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
105 cp
106   return 0;
107 }
108
109 int read_packet(vpn_packet_t *packet)
110 {
111   int lenin;
112 cp
113   if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0)
114     {
115       syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
116       return -1;
117     }
118
119   memcpy(vp->data, mymac.net.mac.address.x, 6);
120   memcpy(vp->data + 6, mymac.net.mac.address.x, 6);
121   vp->data[12] = 0x08;
122   vp->data[13] = 0x00;
123
124   packet->len = lenin + 14;
125
126   device_total_in += packet->len;
127
128   if(debug_lvl >= DEBUG_TRAFFIC)
129     {
130       syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), device_info, packet.len);
131     }
132
133   return 0;
134 cp
135 }
136
137 int write_packet(vpn_packet_t *packet)
138 {
139 cp
140   if(debug_lvl >= DEBUG_TRAFFIC)
141     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
142            packet->len, device_info);
143
144   if(write(device_fd, packet->data + 14, packet->len - 14) < 0)
145     {
146       syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, packet.len);
147       return -1;
148     }
149
150   device_total_out += packet->len;
151 cp
152 }