Update all header guards.
[tinc] / src / multicast_device.c
1 /*
2     device.c -- multicast socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2014 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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "device.h"
25 #include "net.h"
26 #include "logger.h"
27 #include "netutl.h"
28 #include "utils.h"
29 #include "route.h"
30 #include "xalloc.h"
31
32 static char *device_info;
33
34 static uint64_t device_total_in = 0;
35 static uint64_t device_total_out = 0;
36
37 static struct addrinfo *ai = NULL;
38 static mac_t ignore_src = {{0}};
39
40 static bool setup_device(void) {
41         char *host;
42         char *port;
43         char *space;
44         int ttl = 1;
45
46         device_info = "multicast socket";
47
48         get_config_string(lookup_config(config_tree, "Interface"), &iface);
49
50         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
51                 logger(LOG_ERR, "Device variable required for %s", device_info);
52                 return false;
53         }
54
55         host = xstrdup(device);
56         space = strchr(host, ' ');
57         if(!space) {
58                 logger(LOG_ERR, "Port number required for %s", device_info);
59                 free(host);
60                 return false;
61         }
62
63         *space++ = 0;
64         port = space;
65         space = strchr(port, ' ');
66
67         if(space) {
68                 *space++ = 0;
69                 ttl = atoi(space);
70         }
71
72         ai = str2addrinfo(host, port, SOCK_DGRAM);
73         if(!ai) {
74                 free(host);
75                 return false;
76         }
77
78         device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
79         if(device_fd < 0) {
80                 logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
81                 free(host);
82                 return false;
83         }
84
85 #ifdef FD_CLOEXEC
86         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
87 #endif
88
89         static const int one = 1;
90         setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
91
92         if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
93                 closesocket(device_fd);
94                 logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
95                 free(host);
96                 return false;
97         }
98
99         switch(ai->ai_family) {
100 #ifdef IP_ADD_MEMBERSHIP
101                 case AF_INET: {
102                         struct ip_mreq mreq;
103                         struct sockaddr_in in;
104                         memcpy(&in, ai->ai_addr, sizeof in);
105                         mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
106                         mreq.imr_interface.s_addr = htonl(INADDR_ANY);
107                         if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof mreq)) {
108                                 logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
109                                 closesocket(device_fd);
110                                 free(host);
111                                 return false;
112                         }
113 #ifdef IP_MULTICAST_LOOP
114                         setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof one);
115 #endif
116 #ifdef IP_MULTICAST_TTL
117                         setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof ttl);
118 #endif
119                 } break;
120 #endif
121
122 #ifdef IPV6_JOIN_GROUP
123                 case AF_INET6: {
124                         struct ipv6_mreq mreq;
125                         struct sockaddr_in6 in6;
126                         memcpy(&in6, ai->ai_addr, sizeof in6);
127                         memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof mreq.ipv6mr_multiaddr);
128                         mreq.ipv6mr_interface = in6.sin6_scope_id;
129                         if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof mreq)) {
130                                 logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
131                                 closesocket(device_fd);
132                                 free(host);
133                                 return false;
134                         }
135 #ifdef IPV6_MULTICAST_LOOP
136                         setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof one);
137 #endif
138 #ifdef IPV6_MULTICAST_HOPS
139                         setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof ttl);
140 #endif
141                 } break;
142 #endif
143         
144                 default:
145                         logger(LOG_ERR, "Multicast for address family %x unsupported", ai->ai_family);
146                         closesocket(device_fd);
147                         free(host);
148                         return false;
149         }
150
151         free(host);
152         logger(LOG_INFO, "%s is a %s", device, device_info);
153
154         return true;
155 }
156
157 static void close_device(void) {
158         close(device_fd);
159
160         free(device);
161         free(iface);
162
163         if(ai)
164                 freeaddrinfo(ai);
165 }
166
167 static bool read_packet(vpn_packet_t *packet) {
168         int lenin;
169
170         if((lenin = recv(device_fd, (void *)packet->data, MTU, 0)) <= 0) {
171                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
172                            device, strerror(errno));
173                 return false;
174         }
175
176         if(!memcmp(&ignore_src, packet->data + 6, sizeof ignore_src)) {
177                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignoring loopback packet of %d bytes from %s", lenin, device_info);
178                 packet->len = 0;
179                 return true;
180         }
181
182         packet->len = lenin;
183
184         device_total_in += packet->len;
185
186         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
187                            device_info);
188
189         return true;
190 }
191
192 static bool write_packet(vpn_packet_t *packet) {
193         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
194                            packet->len, device_info);
195
196         if(sendto(device_fd, (void *)packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
197                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
198                            strerror(errno));
199                 return false;
200         }
201
202         device_total_out += packet->len;
203
204         memcpy(&ignore_src, packet->data + 6, sizeof ignore_src);
205
206         return true;
207 }
208
209 static void dump_device_stats(void) {
210         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
211         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
212         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
213 }
214
215 const devops_t multicast_devops = {
216         .setup = setup_device,
217         .close = close_device,
218         .read = read_packet,
219         .write = write_packet,
220         .dump_stats = dump_device_stats,
221 };
222
223 #if 0
224
225 static bool not_supported(void) {
226         logger(LOG_ERR, "Raw socket device not supported on this platform");
227         return false;
228 }
229
230 const devops_t multicast_devops = {
231         .setup = not_supported,
232         .close = NULL,
233         .read = NULL,
234         .write = NULL,
235         .dump_stats = NULL,
236 };
237 #endif