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>
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.
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.
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.
20 $Id: device.c,v 1.1.2.2 2001/10/12 15:38:35 guus Exp $
23 #include <sys/sockio.h>
24 #include <sys/stropts.h>
25 #include <net/if_tun.h>
27 #define DEFAULT_DEVICE "/dev/tun"
34 int device_total_in = 0;
35 int device_total_out = 0;
37 int setup_device(void)
39 int ip_fd = -1, if_fd = -1;
44 if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
45 device_fname = DEFAULT_DEVICE;
48 if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
50 syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
57 while(*ptr && !isdigit((int)*ptr)) ptr++;
60 if( (ip_fd = open("/dev/ip", O_RDWR, 0)) < 0){
61 syslog(LOG_ERR, _("Could not open /dev/ip: %m"));
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"));
71 if( (if_fd = open(fname, O_RDWR, 0)) < 0){
72 syslog(LOG_ERR, _("Could not open %s twice: %m"), fname);
76 if(ioctl(if_fd, I_PUSH, "ip") < 0){
77 syslog(LOG_ERR, _("Can't push IP module: %m"));
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);
87 if(ioctl(ip_fd, I_LINK, if_fd) < 0){
88 syslog(LOG_ERR, _("Can't link TUN device to IP: %m"));
92 device_info = _("Solaris tun device");
94 /* Set default MAC address for ethertap devices */
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;
104 syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
109 int read_packet(vpn_packet_t *packet)
113 if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0)
115 syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
119 memcpy(vp->data, mymac.net.mac.address.x, 6);
120 memcpy(vp->data + 6, mymac.net.mac.address.x, 6);
124 packet->len = lenin + 14;
126 device_total_in += packet->len;
128 if(debug_lvl >= DEBUG_TRAFFIC)
130 syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), device_info, packet.len);
137 int write_packet(vpn_packet_t *packet)
140 if(debug_lvl >= DEBUG_TRAFFIC)
141 syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
142 packet->len, device_info);
144 if(write(device_fd, packet->data + 14, packet->len - 14) < 0)
146 syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, packet.len);
150 device_total_out += packet->len;