Fix all -Wall -W compiler warnings.
[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
58         if(!space) {
59                 logger(LOG_ERR, "Port number required for %s", device_info);
60                 free(host);
61                 return false;
62         }
63
64         *space++ = 0;
65         port = space;
66         space = strchr(port, ' ');
67
68         if(space) {
69                 *space++ = 0;
70                 ttl = atoi(space);
71         }
72
73         ai = str2addrinfo(host, port, SOCK_DGRAM);
74
75         if(!ai) {
76                 free(host);
77                 return false;
78         }
79
80         device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
81
82         if(device_fd < 0) {
83                 logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
84                 free(host);
85                 return false;
86         }
87
88 #ifdef FD_CLOEXEC
89         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
90 #endif
91
92         static const int one = 1;
93         setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
94
95         if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
96                 closesocket(device_fd);
97                 logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
98                 free(host);
99                 return false;
100         }
101
102         switch(ai->ai_family) {
103 #ifdef IP_ADD_MEMBERSHIP
104
105         case AF_INET: {
106                 struct ip_mreq mreq;
107                 struct sockaddr_in in;
108                 memcpy(&in, ai->ai_addr, sizeof(in));
109                 mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
110                 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
111
112                 if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq))) {
113                         logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
114                         closesocket(device_fd);
115                         free(host);
116                         return false;
117                 }
118
119 #ifdef IP_MULTICAST_LOOP
120                 setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof(one));
121 #endif
122 #ifdef IP_MULTICAST_TTL
123                 setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));
124 #endif
125         }
126         break;
127 #endif
128
129 #ifdef IPV6_JOIN_GROUP
130
131         case AF_INET6: {
132                 struct ipv6_mreq mreq;
133                 struct sockaddr_in6 in6;
134                 memcpy(&in6, ai->ai_addr, sizeof(in6));
135                 memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof(mreq.ipv6mr_multiaddr));
136                 mreq.ipv6mr_interface = in6.sin6_scope_id;
137
138                 if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof(mreq))) {
139                         logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
140                         closesocket(device_fd);
141                         free(host);
142                         return false;
143                 }
144
145 #ifdef IPV6_MULTICAST_LOOP
146                 setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof(one));
147 #endif
148 #ifdef IPV6_MULTICAST_HOPS
149                 setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof(ttl));
150 #endif
151         }
152         break;
153 #endif
154
155         default:
156                 logger(LOG_ERR, "Multicast for address family %x unsupported", ai->ai_family);
157                 closesocket(device_fd);
158                 free(host);
159                 return false;
160         }
161
162         free(host);
163         logger(LOG_INFO, "%s is a %s", device, device_info);
164
165         return true;
166 }
167
168 static void close_device(void) {
169         close(device_fd);
170
171         free(device);
172         free(iface);
173
174         if(ai) {
175                 freeaddrinfo(ai);
176         }
177 }
178
179 static bool read_packet(vpn_packet_t *packet) {
180         int lenin;
181
182         if((lenin = recv(device_fd, (void *)packet->data, MTU, 0)) <= 0) {
183                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
184                        device, strerror(errno));
185                 return false;
186         }
187
188         if(!memcmp(&ignore_src, packet->data + 6, sizeof(ignore_src))) {
189                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignoring loopback packet of %d bytes from %s", lenin, device_info);
190                 packet->len = 0;
191                 return true;
192         }
193
194         packet->len = lenin;
195
196         device_total_in += packet->len;
197
198         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
199                                 device_info);
200
201         return true;
202 }
203
204 static bool write_packet(vpn_packet_t *packet) {
205         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
206                                 packet->len, device_info);
207
208         if(sendto(device_fd, (void *)packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
209                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
210                        strerror(errno));
211                 return false;
212         }
213
214         device_total_out += packet->len;
215
216         memcpy(&ignore_src, packet->data + 6, sizeof(ignore_src));
217
218         return true;
219 }
220
221 static void dump_device_stats(void) {
222         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
223         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
224         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
225 }
226
227 const devops_t multicast_devops = {
228         .setup = setup_device,
229         .close = close_device,
230         .read = read_packet,
231         .write = write_packet,
232         .dump_stats = dump_device_stats,
233 };
234
235 #if 0
236
237 static bool not_supported(void) {
238         logger(LOG_ERR, "Raw socket device not supported on this platform");
239         return false;
240 }
241
242 const devops_t multicast_devops = {
243         .setup = not_supported,
244         .close = NULL,
245         .read = NULL,
246         .write = NULL,
247         .dump_stats = NULL,
248 };
249 #endif