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