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