add the rfc
[project/mdnsd.git] / interface.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _GNU_SOURCE
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/utsname.h>
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
34 #include <libubox/utils.h>
35 #include "interface.h"
36 #include "util.h"
37 #include "dns.h"
38 #include "announce.h"
39
40 static int
41 interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len)
42 {
43         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
44         static struct sockaddr_in a;
45         static struct msghdr m = {
46                 .msg_name = (struct sockaddr *) &a,
47                 .msg_namelen = sizeof(a),
48                 .msg_control = cmsg_data,
49                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
50         };
51         struct in_pktinfo *pkti;
52         struct cmsghdr *cmsg;
53         int fd = iface->fd.fd;
54
55         a.sin_family = AF_INET;
56         a.sin_port = htons(MCAST_PORT);
57         m.msg_iov = iov;
58         m.msg_iovlen = iov_len;
59
60         memset(cmsg_data, 0, sizeof(cmsg_data));
61         cmsg = CMSG_FIRSTHDR(&m);
62         cmsg->cmsg_len = m.msg_controllen;
63         cmsg->cmsg_level = IPPROTO_IP;
64         cmsg->cmsg_type = IP_PKTINFO;
65
66         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
67         pkti->ipi_ifindex = iface->ifindex;
68
69         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
70
71         return sendmsg(fd, &m, 0);
72 }
73
74 static int
75 interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len)
76 {
77         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
78         static struct sockaddr_in6 a;
79         static struct msghdr m = {
80                 .msg_name = (struct sockaddr *) &a,
81                 .msg_namelen = sizeof(a),
82                 .msg_control = cmsg_data,
83                 .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)),
84         };
85         struct in6_pktinfo *pkti;
86         struct cmsghdr *cmsg;
87         int fd = iface->fd.fd;
88
89         a.sin6_family = AF_INET6;
90         a.sin6_port = htons(MCAST_PORT);
91         m.msg_iov = iov;
92         m.msg_iovlen = iov_len;
93
94         memset(cmsg_data, 0, sizeof(cmsg_data));
95         cmsg = CMSG_FIRSTHDR(&m);
96         cmsg->cmsg_len = m.msg_controllen;
97         cmsg->cmsg_level = IPPROTO_IPV6;
98         cmsg->cmsg_type = IPV6_PKTINFO;
99
100         pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
101         pkti->ipi6_ifindex = iface->ifindex;
102
103         inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
104
105         return sendmsg(fd, &m, 0);
106 }
107
108 int
109 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
110 {
111         if (iface->v6)
112                 return interface_send_packet6(iface, iov, iov_len);
113
114         return interface_send_packet4(iface, iov, iov_len);
115 }
116
117 static void interface_close(struct interface *iface)
118 {
119         if (iface->fd.fd < 0)
120                 return;
121
122         announce_free(iface);
123         uloop_fd_delete(&iface->fd);
124         close(iface->fd.fd);
125         iface->fd.fd = -1;
126 }
127
128 static void interface_free(struct interface *iface)
129 {
130         interface_close(iface);
131         free(iface);
132 }
133
134 static void
135 read_socket(struct uloop_fd *u, unsigned int events)
136 {
137         struct interface *iface = container_of(u, struct interface, fd);
138         static uint8_t buffer[8 * 1024];
139         int len;
140
141         if (u->eof) {
142                 interface_close(iface);
143                 uloop_timeout_set(&iface->reconnect, 1000);
144                 return;
145         }
146
147         len = read(u->fd, buffer, sizeof(buffer));
148         if (len < 1) {
149                 if (errno != EAGAIN)
150                         fprintf(stderr, "read failed: %s\n", strerror(errno));
151                 return;
152         }
153
154         dns_handle_packet(iface, buffer, len);
155 }
156
157 static int
158 interface_socket_setup4(struct interface *iface)
159 {
160         struct ip_mreqn mreq;
161         uint8_t ttl = 255;
162         int yes = 1;
163         int no = 0;
164         struct sockaddr_in sa = { 0 };
165         int fd = iface->fd.fd;
166
167         sa.sin_family = AF_INET;
168         sa.sin_port = htons(MCAST_PORT);
169         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
170
171         memset(&mreq, 0, sizeof(mreq));
172         mreq.imr_address.s_addr = iface->v4_addr.s_addr;
173         mreq.imr_multiaddr = sa.sin_addr;
174         mreq.imr_ifindex = iface->ifindex;
175
176         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
177                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
178
179         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
180                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
181
182         /* Some network drivers have issues with dropping membership of
183          * mcast groups when the iface is down, but don't allow rejoining
184          * when it comes back up. This is an ugly workaround
185          * -- this was copied from avahi --
186          */
187         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
188
189         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
190                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
191                 close(fd);
192                 fd = -1;
193                 return -1;
194         }
195
196         if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
197                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
198
199         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
200                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
201
202         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
203                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
204
205         return 0;
206 }
207
208 static int
209 interface_socket_setup6(struct interface *iface)
210 {
211         struct ipv6_mreq mreq;
212         int ttl = 255;
213         int yes = 1;
214         int no = 0;
215         struct sockaddr_in6 sa = { 0 };
216         int fd = iface->fd.fd;
217
218         sa.sin6_family = AF_INET6;
219         sa.sin6_port = htons(MCAST_PORT);
220         inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
221
222         memset(&mreq, 0, sizeof(mreq));
223         mreq.ipv6mr_multiaddr = sa.sin6_addr;
224         mreq.ipv6mr_interface = iface->ifindex;
225
226         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
227                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
228
229         if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
230                 fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
231
232         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
233                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
234
235         setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
236         if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
237                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
238                 close(fd);
239                 fd = -1;
240                 return -1;
241         }
242
243         if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
244                 fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
245
246         if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
247                 fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
248
249         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
250                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
251
252         return 0;
253 }
254
255 static void
256 reconnect_socket(struct uloop_timeout *timeout)
257 {
258         struct interface *iface = container_of(timeout, struct interface, reconnect);
259         char mcast_addr[16];
260         int type = 0;
261
262         if (iface->v6) {
263                 snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", iface->mcast_addr, iface->name);
264                 type = USOCK_IPV6ONLY;
265         } else {
266                 snprintf(mcast_addr, sizeof(mcast_addr), "%s", iface->mcast_addr);
267                 type = USOCK_IPV4ONLY;
268         }
269
270         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | type, mcast_addr, "5353");
271         if (iface->fd.fd < 0) {
272                 fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
273                 goto retry;
274         }
275
276         if (!iface->v6 && interface_socket_setup4(iface)) {
277                 iface->fd.fd = -1;
278                 goto retry;
279         }
280
281         if (iface->v6 && interface_socket_setup6(iface)) {
282                 iface->fd.fd = -1;
283                 goto retry;
284         }
285
286         uloop_fd_add(&iface->fd, ULOOP_READ);
287         dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
288         announce_init(iface);
289         return;
290
291 retry:
292         uloop_timeout_set(timeout, 1000);
293 }
294
295
296 static void interface_start(struct interface *iface)
297 {
298         iface->fd.cb = read_socket;
299         iface->reconnect.cb = reconnect_socket;
300         uloop_timeout_set(&iface->reconnect, 100);
301 }
302
303 static void
304 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
305                 struct vlist_node *node_old)
306 {
307         struct interface *iface;
308
309         if (node_old) {
310                 iface = container_of(node_old, struct interface, node);
311                 interface_free(iface);
312         }
313
314         if (node_new) {
315                 iface = container_of(node_new, struct interface, node);
316                 interface_start(iface);
317         }
318 }
319
320 static int
321 get_iface_ipv4(struct interface *iface)
322 {
323         struct sockaddr_in *sin;
324         struct ifreq ir;
325         int sock, ret = -1;
326
327         sock = socket(AF_INET, SOCK_DGRAM, 0);
328         if (sock < 0)
329                 return -1;
330
331         memset(&ir, 0, sizeof(struct ifreq));
332         strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
333
334         ret = ioctl(sock, SIOCGIFADDR, &ir);
335         if (ret < 0)
336                 goto out;
337
338         sin = (struct sockaddr_in *) &ir.ifr_addr;
339         memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
340         iface->mcast_addr = MCAST_ADDR;
341 out:
342         close(sock);
343         return ret;
344 }
345
346 static int
347 get_iface_ipv6(struct interface *iface)
348 {
349         struct sockaddr_in6 addr = {AF_INET6, 0, iface->ifindex, IN6ADDR_ANY_INIT, 0};
350         socklen_t alen = sizeof(addr);
351         int sock, ret = -1;
352
353         addr.sin6_addr.s6_addr[0] = 0xff;
354         addr.sin6_addr.s6_addr[1] = 0x02;
355         addr.sin6_addr.s6_addr[15] = 0x01;
356
357         sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
358         connect(sock, (struct sockaddr*)&addr, sizeof(addr));
359         ret = getsockname(sock, (struct sockaddr*)&addr, &alen);
360         if (!ret) {
361                 memcpy(&iface->v6_addr, &addr.sin6_addr, sizeof(iface->v6_addr));
362                 iface->mcast_addr = MCAST_ADDR6;
363                 iface->v6 = 1;
364         }
365         close(sock);
366         return ret;
367 }
368
369 static int _interface_add(const char *name, int v6)
370 {
371         struct interface *iface;
372         char *name_buf;
373         char *id_buf;
374
375         iface = calloc_a(sizeof(*iface),
376                 &name_buf, strlen(name) + 1,
377                 &id_buf, strlen(name) + 3);
378
379         sprintf(id_buf, "%d_%s", v6, name);
380         iface->name = strcpy(name_buf, name);
381         iface->id = id_buf;
382         iface->ifindex = if_nametoindex(name);
383         iface->fd.fd = -1;
384
385         if (iface->ifindex <= 0)
386                 goto error;
387
388         if (!v6 && get_iface_ipv4(iface))
389                 goto error;
390
391         if (v6 && get_iface_ipv6(iface))
392                 goto error;
393
394         vlist_add(&interfaces, &iface->node, iface->id);
395         return 0;
396
397 error:
398         free(iface);
399         return -1;
400 }
401
402 int interface_add(const char *name)
403 {
404         int v4 = _interface_add(name, 0);
405         int v6 = _interface_add(name, 1);
406         return v4 && v6;
407 }
408
409 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);