1 |
/* |
2 |
device.c -- raw socket |
3 |
Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>, |
4 |
2002-2004 Guus Sliepen <guus@tinc-vpn.org> |
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 1405 2004-11-08 22:11:33Z guus $ |
21 |
*/ |
22 |
|
23 |
#include <netpacket/packet.h> |
24 |
#include <netinet/ether.h> |
25 |
|
26 |
int device_fd = -1; |
27 |
char *device; |
28 |
char *iface; |
29 |
char ifrname[IFNAMSIZ]; |
30 |
char *device_info; |
31 |
|
32 |
static int device_total_in = 0; |
33 |
static int device_total_out = 0; |
34 |
|
35 |
bool setup_device(void) |
36 |
{ |
37 |
struct ifreq ifr; |
38 |
struct sockaddr_ll sa; |
39 |
|
40 |
cp(); |
41 |
|
42 |
if(!get_config_string |
43 |
(lookup_config(config_tree, "Interface"), &iface)) |
44 |
iface = "eth0"; |
45 |
|
46 |
if(!get_config_string(lookup_config(config_tree, "Device"), &device)) |
47 |
device = iface; |
48 |
|
49 |
device_info = _("raw socket"); |
50 |
|
51 |
if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { |
52 |
logger(LOG_ERR, _("Could not open %s: %s"), device_info, |
53 |
strerror(errno)); |
54 |
return false; |
55 |
} |
56 |
|
57 |
memset(&ifr, 0, sizeof(ifr)); |
58 |
strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); |
59 |
if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) { |
60 |
close(device_fd); |
61 |
logger(LOG_ERR, _("Can't find interface %s: %s"), iface, |
62 |
strerror(errno)); |
63 |
return false; |
64 |
} |
65 |
|
66 |
memset(&sa, '0', sizeof(sa)); |
67 |
sa.sll_family = AF_PACKET; |
68 |
sa.sll_protocol = htons(ETH_P_ALL); |
69 |
sa.sll_ifindex = ifr.ifr_ifindex; |
70 |
|
71 |
if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) { |
72 |
logger(LOG_ERR, _("Could not bind %s to %s: %s"), device, iface, strerror(errno)); |
73 |
return false; |
74 |
} |
75 |
|
76 |
logger(LOG_INFO, _("%s is a %s"), device, device_info); |
77 |
|
78 |
return true; |
79 |
} |
80 |
|
81 |
void close_device(void) |
82 |
{ |
83 |
cp(); |
84 |
|
85 |
close(device_fd); |
86 |
} |
87 |
|
88 |
bool read_packet(vpn_packet_t *packet) |
89 |
{ |
90 |
int lenin; |
91 |
|
92 |
cp(); |
93 |
|
94 |
if((lenin = read(device_fd, packet->data, MTU)) <= 0) { |
95 |
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, |
96 |
device, strerror(errno)); |
97 |
return false; |
98 |
} |
99 |
|
100 |
packet->len = lenin; |
101 |
|
102 |
device_total_in += packet->len; |
103 |
|
104 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, |
105 |
device_info); |
106 |
|
107 |
return true; |
108 |
} |
109 |
|
110 |
bool write_packet(vpn_packet_t *packet) |
111 |
{ |
112 |
cp(); |
113 |
|
114 |
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"), |
115 |
packet->len, device_info); |
116 |
|
117 |
if(write(device_fd, packet->data, packet->len) < 0) { |
118 |
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, |
119 |
strerror(errno)); |
120 |
return false; |
121 |
} |
122 |
|
123 |
device_total_out += packet->len; |
124 |
|
125 |
return true; |
126 |
} |
127 |
|
128 |
void dump_device_stats(void) |
129 |
{ |
130 |
cp(); |
131 |
|
132 |
logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device); |
133 |
logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in); |
134 |
logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out); |
135 |
} |