392fd3713e03fd33232404fe81e186ae43f60044
[oweals/tinc.git] / src / multicast_device.c
1 /*
2     device.c -- multicast socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2012 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                 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         if(!ai)
73                 return false;
74
75         device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
76         if(device_fd < 0) {
77                 logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
78                 return false;
79         }
80
81 #ifdef FD_CLOEXEC
82         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
83 #endif
84
85         static const int one = 1;
86         setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
87
88         if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
89                 closesocket(device_fd);
90                 logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
91                 return false;
92         }
93
94         switch(ai->ai_family) {
95                 case AF_INET: {
96                         struct ip_mreq mreq;
97                         struct sockaddr_in in;
98                         memcpy(&in, ai->ai_addr, sizeof in);
99                         mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
100                         mreq.imr_interface.s_addr = htonl(INADDR_ANY);
101                         if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof mreq)) {
102                                 logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
103                                 closesocket(device_fd);
104                                 return false;
105                         }
106                         setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &one, sizeof one);
107                         setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof ttl);
108                 } break;
109
110                 case AF_INET6: {
111                         struct ipv6_mreq mreq;
112                         struct sockaddr_in6 in6;
113                         memcpy(&in6, ai->ai_addr, sizeof in6);
114                         memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof mreq.ipv6mr_multiaddr);
115                         mreq.ipv6mr_interface = 0;
116                         if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof mreq)) {
117                                 logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
118                                 closesocket(device_fd);
119                                 return false;
120                         }
121                         setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &one, sizeof one);
122                         setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof ttl);
123                 } break;
124         
125                 default:
126                         logger(LOG_ERR, "Multicast for address family %hx unsupported", ai->ai_family);
127                         closesocket(device_fd);
128                         return false;
129         }
130
131         logger(LOG_INFO, "%s is a %s", device, device_info);
132
133         return true;
134 }
135
136 static void close_device(void) {
137         close(device_fd);
138
139         free(device);
140         free(iface);
141
142         if(ai)
143                 freeaddrinfo(ai);
144 }
145
146 static bool read_packet(vpn_packet_t *packet) {
147         int lenin;
148
149         if((lenin = recv(device_fd, packet->data, MTU, 0)) <= 0) {
150                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
151                            device, strerror(errno));
152                 return false;
153         }
154
155         if(!memcmp(&ignore_src, packet->data + 6, sizeof ignore_src)) {
156                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignoring loopback packet of %d bytes from %s", lenin, device_info);
157                 packet->len = 0;
158                 return true;
159         }
160
161         packet->len = lenin;
162
163         device_total_in += packet->len;
164
165         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
166                            device_info);
167
168         return true;
169 }
170
171 static bool write_packet(vpn_packet_t *packet) {
172         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
173                            packet->len, device_info);
174
175         if(sendto(device_fd, packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
176                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
177                            strerror(errno));
178                 return false;
179         }
180
181         device_total_out += packet->len;
182
183         memcpy(&ignore_src, packet->data + 6, sizeof ignore_src);
184
185         return true;
186 }
187
188 static void dump_device_stats(void) {
189         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
190         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
191         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
192 }
193
194 const devops_t multicast_devops = {
195         .setup = setup_device,
196         .close = close_device,
197         .read = read_packet,
198         .write = write_packet,
199         .dump_stats = dump_device_stats,
200 };
201
202 #if 0
203
204 static bool not_supported(void) {
205         logger(LOG_ERR, "Raw socket device not supported on this platform");
206         return false;
207 }
208
209 const devops_t multicast_devops = {
210         .setup = not_supported,
211         .close = NULL,
212         .read = NULL,
213         .write = NULL,
214         .dump_stats = NULL,
215 };
216 #endif