ipv6 cleanup
[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 #include <sys/types.h>
25
26 #include <ifaddrs.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <errno.h>
32
33 #include <libubox/usock.h>
34 #include <libubox/uloop.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include "interface.h"
38 #include "util.h"
39 #include "dns.h"
40 #include "announce.h"
41
42 static int
43 interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len)
44 {
45         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
46         static struct sockaddr_in a;
47         static struct msghdr m = {
48                 .msg_name = (struct sockaddr *) &a,
49                 .msg_namelen = sizeof(a),
50                 .msg_control = cmsg_data,
51                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
52         };
53         struct in_pktinfo *pkti;
54         struct cmsghdr *cmsg;
55         int fd = iface->fd.fd;
56
57         a.sin_family = AF_INET;
58         a.sin_port = htons(MCAST_PORT);
59         m.msg_iov = iov;
60         m.msg_iovlen = iov_len;
61
62         memset(cmsg_data, 0, sizeof(cmsg_data));
63         cmsg = CMSG_FIRSTHDR(&m);
64         cmsg->cmsg_len = m.msg_controllen;
65         cmsg->cmsg_level = IPPROTO_IP;
66         cmsg->cmsg_type = IP_PKTINFO;
67
68         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
69         pkti->ipi_ifindex = iface->ifindex;
70
71         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
72
73         return sendmsg(fd, &m, 0);
74 }
75
76 static int
77 interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len)
78 {
79         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
80         static struct sockaddr_in6 a;
81         static struct msghdr m = {
82                 .msg_name = (struct sockaddr *) &a,
83                 .msg_namelen = sizeof(a),
84                 .msg_control = cmsg_data,
85                 .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)),
86         };
87         struct in6_pktinfo *pkti;
88         struct cmsghdr *cmsg;
89         int fd = iface->fd.fd;
90
91         a.sin6_family = AF_INET6;
92         a.sin6_port = htons(MCAST_PORT);
93         m.msg_iov = iov;
94         m.msg_iovlen = iov_len;
95
96         memset(cmsg_data, 0, sizeof(cmsg_data));
97         cmsg = CMSG_FIRSTHDR(&m);
98         cmsg->cmsg_len = m.msg_controllen;
99         cmsg->cmsg_level = IPPROTO_IPV6;
100         cmsg->cmsg_type = IPV6_PKTINFO;
101
102         pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
103         pkti->ipi6_ifindex = iface->ifindex;
104
105         inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
106
107         return sendmsg(fd, &m, 0);
108 }
109
110 int
111 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
112 {
113         if (iface->v6)
114                 return interface_send_packet6(iface, iov, iov_len);
115
116         return interface_send_packet4(iface, iov, iov_len);
117 }
118
119 static void interface_close(struct interface *iface)
120 {
121         if (iface->fd.fd < 0)
122                 return;
123
124         announce_free(iface);
125         uloop_fd_delete(&iface->fd);
126         close(iface->fd.fd);
127         iface->fd.fd = -1;
128 }
129
130 static void interface_free(struct interface *iface)
131 {
132         interface_close(iface);
133         free(iface);
134 }
135
136 static void
137 read_socket4(struct uloop_fd *u, unsigned int events)
138 {
139         struct interface *iface = container_of(u, struct interface, fd);
140         static uint8_t buffer[8 * 1024];
141         struct iovec iov[1];
142         char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
143         struct cmsghdr *cmsgptr;
144         struct msghdr msg;
145         socklen_t len;
146         struct sockaddr_in from;
147         int flags = 0, ifindex = -1;
148         uint8_t ttl = 0;
149         struct in_pktinfo *inp = NULL;
150
151         if (u->eof) {
152                 interface_close(iface);
153                 uloop_timeout_set(&iface->reconnect, 1000);
154                 return;
155         }
156
157         iov[0].iov_base = buffer;
158         iov[0].iov_len = sizeof(buffer);
159
160         memset(&msg, 0, sizeof(msg));
161         msg.msg_name = (struct sockaddr *) &from;
162         msg.msg_namelen = sizeof(struct sockaddr_in);
163         msg.msg_iov = iov;
164         msg.msg_iovlen = 1;
165         msg.msg_control = &cmsg;
166         msg.msg_controllen = sizeof(cmsg);
167
168         len = recvmsg(u->fd, &msg, flags);
169         if (len == -1) {
170                 perror("read failed");
171                 return;
172         }
173         for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
174                 void *c = CMSG_DATA(cmsgptr);
175
176                 switch (cmsgptr->cmsg_type) {
177                 case IP_PKTINFO:
178                         inp = ((struct in_pktinfo *) c);
179                         break;
180
181                 case IP_TTL:
182                         ttl = (uint8_t) *((int *) c);
183                         break;
184
185                 default:
186                         fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
187                         return;
188                 }
189         }
190
191         if (ttl != 255)
192                 return;
193
194         if (debug > 1) {
195                 char buf[256];
196
197                 fprintf(stderr, "iface: %s\n", iface->name);
198                 fprintf(stderr, "  v6: %d\n", iface->v6);
199                 fprintf(stderr, "  multicast: %d\n", iface->multicast);
200                 inet_ntop(AF_INET, &from.sin_addr, buf, 256);
201                 fprintf(stderr, "  src %s:%d\n", buf, from.sin_port);
202                 inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
203                 fprintf(stderr, "  dst %s\n", buf);
204                 inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
205                 fprintf(stderr, "  real %s\n", buf);
206         }
207
208         if (inp->ipi_ifindex != iface->ifindex)
209                 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
210         else
211                 dns_handle_packet(iface, buffer, len, 0);
212 }
213
214 static void
215 read_socket6(struct uloop_fd *u, unsigned int events)
216 {
217         struct interface *iface = container_of(u, struct interface, fd);
218         static uint8_t buffer[8 * 1024];
219         struct iovec iov[1];
220         char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
221         struct cmsghdr *cmsgptr;
222         struct msghdr msg;
223         socklen_t len;
224         struct sockaddr_in6 from;
225         int flags = 0, ifindex = -1;
226         int ttl = 0;
227         struct in6_pktinfo *inp = NULL;
228
229         if (u->eof) {
230                 interface_close(iface);
231                 uloop_timeout_set(&iface->reconnect, 1000);
232                 return;
233         }
234
235         iov[0].iov_base = buffer;
236         iov[0].iov_len = sizeof(buffer);
237
238         memset(&msg, 0, sizeof(msg));
239         msg.msg_name = (struct sockaddr *) &from;
240         msg.msg_namelen = sizeof(struct sockaddr_in6);
241         msg.msg_iov = iov;
242         msg.msg_iovlen = 1;
243         msg.msg_control = &cmsg6;
244         msg.msg_controllen = sizeof(cmsg6);
245
246         len = recvmsg(u->fd, &msg, flags);
247         if (len == -1) {
248                 perror("read failed");
249                 return;
250         }
251         for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
252                 void *c = CMSG_DATA(cmsgptr);
253
254                 switch (cmsgptr->cmsg_type) {
255                 case IPV6_PKTINFO:
256                         inp = ((struct in6_pktinfo *) c);
257                         break;
258
259                 case IPV6_HOPLIMIT:
260                         ttl = (uint8_t) *((int *) c);
261                         break;
262
263                 default:
264                         fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
265                         return;
266                 }
267         }
268
269         if (ttl != 255)
270                 return;
271
272         if (debug > 1) {
273                 char buf[256];
274
275                 fprintf(stderr, "iface: %s\n", iface->name);
276                 fprintf(stderr, "  v6: %d\n", iface->v6);
277                 fprintf(stderr, "  multicast: %d\n", iface->multicast);
278                 inet_ntop(AF_INET6, &from.sin6_addr, buf, 256);
279                 fprintf(stderr, "  src %s:%d\n", buf, from.sin6_port);
280                 inet_ntop(AF_INET6, &inp->ipi6_addr, buf, 256);
281                 fprintf(stderr, "  dst %s\n", buf);
282         }
283
284         if (inp->ipi6_ifindex != iface->ifindex)
285                 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
286         else
287                 dns_handle_packet(iface, buffer, len, 0);
288 }
289
290 static int
291 interface_mcast_setup4(struct interface *iface)
292 {
293         struct ip_mreqn mreq;
294         uint8_t ttl = 255;
295         int no = 0;
296         struct sockaddr_in sa = { 0 };
297         int fd = iface->fd.fd;
298
299         sa.sin_family = AF_INET;
300         sa.sin_port = htons(MCAST_PORT);
301         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
302
303         memset(&mreq, 0, sizeof(mreq));
304         mreq.imr_address.s_addr = iface->v4_addr.s_addr;
305         mreq.imr_multiaddr = sa.sin_addr;
306         mreq.imr_ifindex = iface->ifindex;
307
308         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
309                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
310
311         /* Some network drivers have issues with dropping membership of
312          * mcast groups when the iface is down, but don't allow rejoining
313          * when it comes back up. This is an ugly workaround
314          * -- this was copied from avahi --
315          */
316         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
317
318         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
319                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
320                 close(fd);
321                 fd = -1;
322                 return -1;
323         }
324
325         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
326                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
327
328         return 0;
329 }
330
331 static int
332 interface_socket_setup6(struct interface *iface)
333 {
334         struct ipv6_mreq mreq;
335         int ttl = 255;
336         int no = 0;
337         struct sockaddr_in6 sa = { 0 };
338         int fd = iface->fd.fd;
339
340         sa.sin6_family = AF_INET6;
341         sa.sin6_port = htons(MCAST_PORT);
342         inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
343
344         memset(&mreq, 0, sizeof(mreq));
345         mreq.ipv6mr_multiaddr = sa.sin6_addr;
346         mreq.ipv6mr_interface = iface->ifindex;
347
348         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
349                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
350
351         setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
352         if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
353                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
354                 close(fd);
355                 fd = -1;
356                 return -1;
357         }
358
359         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
360                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
361
362         return 0;
363 }
364
365 static void
366 reconnect_socket4(struct uloop_timeout *timeout)
367 {
368         struct interface *iface = container_of(timeout, struct interface, reconnect);
369         int yes = 1;
370
371         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY,
372                 (iface->multicast) ? (iface->mcast_addr) : (iface->v4_addrs), "5353");
373         if (iface->fd.fd < 0) {
374                 fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno));
375                 goto retry;
376         }
377
378         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
379                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
380
381         if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
382                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
383
384         if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
385                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
386
387         if (iface->multicast && interface_mcast_setup4(iface)) {
388                 iface->fd.fd = -1;
389                 goto retry;
390         }
391
392         uloop_fd_add(&iface->fd, ULOOP_READ);
393         if (iface->multicast) {
394                 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 1);
395                 announce_init(iface);
396         }
397
398         return;
399
400 retry:
401         uloop_timeout_set(timeout, 1000);
402 }
403
404 static void
405 reconnect_socket6(struct uloop_timeout *timeout)
406 {
407         struct interface *iface = container_of(timeout, struct interface, reconnect);
408         char mcast_addr[128];
409         int ttl = 255;
410         int yes = 1;
411
412         snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", (iface->multicast) ? (iface->mcast_addr) : (iface->v6_addrs), iface->name);
413         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353");
414         if (iface->fd.fd < 0) {
415                 fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
416                 goto retry;
417         }
418
419         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
420                 fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
421
422         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
423                 fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
424
425         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
426                 fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
427
428         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
429                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
430
431         if (iface->multicast && interface_socket_setup6(iface)) {
432                 iface->fd.fd = -1;
433                 goto retry;
434         }
435
436         uloop_fd_add(&iface->fd, ULOOP_READ);
437
438         if (iface->multicast) {
439                 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 1);
440                 announce_init(iface);
441         }
442
443         return;
444
445 retry:
446         uloop_timeout_set(timeout, 1000);
447 }
448
449
450 static void interface_start(struct interface *iface)
451 {
452         if (iface->v6) {
453                 iface->fd.cb = read_socket6;
454                 iface->reconnect.cb = reconnect_socket6;
455         } else {
456                 iface->fd.cb = read_socket4;
457                 iface->reconnect.cb = reconnect_socket4;
458         }
459         uloop_timeout_set(&iface->reconnect, 100);
460 }
461
462 static void
463 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
464                 struct vlist_node *node_old)
465 {
466         struct interface *iface;
467
468         if (node_old) {
469                 iface = container_of(node_old, struct interface, node);
470                 interface_free(iface);
471         }
472
473         if (node_new) {
474                 iface = container_of(node_new, struct interface, node);
475                 interface_start(iface);
476         }
477 }
478
479 static struct interface* _interface_add(const char *name, int multicast, int v6)
480 {
481         struct interface *iface;
482         char *name_buf;
483         char *id_buf;
484
485         iface = calloc_a(sizeof(*iface),
486                 &name_buf, strlen(name) + 1,
487                 &id_buf, strlen(name) + 5);
488
489         sprintf(id_buf, "%d_%d_%s", multicast, v6, name);
490         iface->name = strcpy(name_buf, name);
491         iface->id = id_buf;
492         iface->ifindex = if_nametoindex(name);
493         iface->fd.fd = -1;
494         iface->multicast = multicast;
495         iface->v6 = v6;
496         if (v6)
497                 iface->mcast_addr = MCAST_ADDR6;
498         else
499                 iface->mcast_addr = MCAST_ADDR;
500
501         if (iface->ifindex <= 0)
502                 goto error;
503
504         vlist_add(&interfaces, &iface->node, iface->id);
505         return iface;
506
507 error:
508         free(iface);
509         return NULL;
510 }
511
512 int interface_add(const char *name)
513 {
514         struct interface *v4 = NULL, *v6 = NULL, *unicast;
515         struct ifaddrs *ifap, *ifa;
516
517         getifaddrs(&ifap);
518
519         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
520                 if (strcmp(ifa->ifa_name, name))
521                         continue;
522                 if (ifa->ifa_addr->sa_family == AF_INET && !v4) {
523                         struct sockaddr_in *sa;
524
525                         if (cfg_proto && (cfg_proto != 4))
526                                 continue;
527
528                         unicast = _interface_add(name, 0, 0);
529                         if (!unicast)
530                                 continue;
531                         sa = (struct sockaddr_in *) ifa->ifa_addr;
532                         memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
533                         inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
534
535                         v4 = _interface_add(name, 1, 0);
536                         if (!v4)
537                                 continue;
538                         memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
539                         inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
540
541                         v4->peer = unicast;
542                         unicast->peer = v4;
543                 }
544
545                 if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
546                         uint8_t ll_prefix[] = {0xfe, 0x80 };
547                         struct sockaddr_in6 *sa6;
548
549                         if (cfg_proto && (cfg_proto != 6))
550                                 continue;
551
552                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
553                         if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
554                                 continue;
555
556                         unicast = _interface_add(name, 0, 1);
557                         if (!unicast)
558                                 continue;
559                         memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
560                         inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
561
562                         v6 = _interface_add(name, 1, 1);
563                         if (!v6)
564                                 continue;
565                         memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
566                         inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
567
568                         v6->peer = unicast;
569                         unicast->peer = v6;
570                 }
571         }
572
573         freeifaddrs(ifap);
574
575         return !v4 && !v6;
576 }
577
578 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);