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