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