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