add unicast listener sockets
[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 break;
188 }
189 }
190
191 if (0) {
192 char buf[256];
193
194 inet_ntop(AF_INET, &from.sin_addr, buf, 256);
195 fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
196 inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
197 fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
198 inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
199 fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
200 }
201
202 if (inp->ipi_ifindex != iface->ifindex)
203 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
204 else if (ttl == 255)
205 dns_handle_packet(iface, buffer, len, 0);
206 }
207
208 static void
209 read_socket6(struct uloop_fd *u, unsigned int events)
210 {
211 struct interface *iface = container_of(u, struct interface, fd);
212 static uint8_t buffer[8 * 1024];
213 struct iovec iov[1];
214 char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 1];
215 struct cmsghdr *cmsgptr;
216 struct msghdr msg;
217 socklen_t len;
218 struct sockaddr_in6 from;
219 int flags = 0, ifindex = -1;
220
221 if (u->eof) {
222 interface_close(iface);
223 uloop_timeout_set(&iface->reconnect, 1000);
224 return;
225 }
226
227 iov[0].iov_base = buffer;
228 iov[0].iov_len = sizeof(buffer);
229
230 memset(&msg, 0, sizeof(msg));
231 msg.msg_name = (struct sockaddr *) &from;
232 msg.msg_namelen = (iface->v6) ? (sizeof(struct sockaddr_in6)) : (sizeof(struct sockaddr_in));
233 msg.msg_iov = iov;
234 msg.msg_iovlen = 1;
235 msg.msg_control = &cmsg6;
236 msg.msg_controllen = sizeof(cmsg6);
237
238 len = recvmsg(u->fd, &msg, flags);
239 if (len == -1) {
240 perror("read failed");
241 return;
242 }
243 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
244 void *c = CMSG_DATA(cmsgptr);
245
246 if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_PKTINFO)
247 ifindex = ((struct in_pktinfo *) c)->ipi_ifindex;
248 else if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO)
249 ifindex = ((struct in6_pktinfo *) c)->ipi6_ifindex;
250 }
251 if (ifindex != iface->ifindex)
252 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
253 else
254 dns_handle_packet(iface, buffer, len, 0);
255 }
256
257 static int
258 interface_mcast_setup4(struct interface *iface)
259 {
260 struct ip_mreqn mreq;
261 uint8_t ttl = 255;
262 int no = 0;
263 struct sockaddr_in sa = { 0 };
264 int fd = iface->fd.fd;
265
266 sa.sin_family = AF_INET;
267 sa.sin_port = htons(MCAST_PORT);
268 inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
269
270 memset(&mreq, 0, sizeof(mreq));
271 mreq.imr_address.s_addr = iface->v4_addr.s_addr;
272 mreq.imr_multiaddr = sa.sin_addr;
273 mreq.imr_ifindex = iface->ifindex;
274
275 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
276 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
277
278 /* Some network drivers have issues with dropping membership of
279 * mcast groups when the iface is down, but don't allow rejoining
280 * when it comes back up. This is an ugly workaround
281 * -- this was copied from avahi --
282 */
283 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
284
285 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
286 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
287 close(fd);
288 fd = -1;
289 return -1;
290 }
291
292 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
293 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
294
295 return 0;
296 }
297
298 static int
299 interface_socket_setup6(struct interface *iface)
300 {
301 struct ipv6_mreq mreq;
302 int ttl = 255;
303 int no = 0;
304 struct sockaddr_in6 sa = { 0 };
305 int fd = iface->fd.fd;
306
307 sa.sin6_family = AF_INET6;
308 sa.sin6_port = htons(MCAST_PORT);
309 inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
310
311 memset(&mreq, 0, sizeof(mreq));
312 mreq.ipv6mr_multiaddr = sa.sin6_addr;
313 mreq.ipv6mr_interface = iface->ifindex;
314
315 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
316 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
317
318 setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
319 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
320 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
321 close(fd);
322 fd = -1;
323 return -1;
324 }
325
326 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
327 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
328
329 return 0;
330 }
331
332 static void
333 reconnect_socket4(struct uloop_timeout *timeout)
334 {
335 struct interface *iface = container_of(timeout, struct interface, reconnect);
336 int yes = 1;
337
338 iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY,
339 (iface->multicast) ? (iface->mcast_addr) : (iface->v4_addrs), "5353");
340 if (iface->fd.fd < 0) {
341 fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno));
342 goto retry;
343 }
344
345 if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
346 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
347
348 if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
349 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
350
351 if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
352 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
353
354 if (iface->multicast && interface_mcast_setup4(iface)) {
355 iface->fd.fd = -1;
356 goto retry;
357 }
358
359 uloop_fd_add(&iface->fd, ULOOP_READ);
360 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
361 announce_init(iface);
362
363 return;
364
365 retry:
366 uloop_timeout_set(timeout, 1000);
367 }
368
369 static void
370 reconnect_socket6(struct uloop_timeout *timeout)
371 {
372 struct interface *iface = container_of(timeout, struct interface, reconnect);
373 char mcast_addr[128];
374 int ttl = 255;
375 int yes = 1;
376
377 snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", iface->mcast_addr, iface->name);
378 iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353");
379 if (iface->fd.fd < 0) {
380 fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
381 goto retry;
382 }
383
384 if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
385 fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
386
387 if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
388 fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
389
390 if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
391 fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
392
393 if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
394 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
395
396 if (iface->multicast && interface_socket_setup6(iface)) {
397 iface->fd.fd = -1;
398 goto retry;
399 }
400
401 uloop_fd_add(&iface->fd, ULOOP_READ);
402 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
403 announce_init(iface);
404 return;
405
406 retry:
407 uloop_timeout_set(timeout, 1000);
408 }
409
410
411 static void interface_start(struct interface *iface)
412 {
413 if (iface->v6) {
414 iface->fd.cb = read_socket6;
415 iface->reconnect.cb = reconnect_socket6;
416 } else {
417 iface->fd.cb = read_socket4;
418 iface->reconnect.cb = reconnect_socket4;
419 }
420 uloop_timeout_set(&iface->reconnect, 100);
421 }
422
423 static void
424 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
425 struct vlist_node *node_old)
426 {
427 struct interface *iface;
428
429 if (node_old) {
430 iface = container_of(node_old, struct interface, node);
431 interface_free(iface);
432 }
433
434 if (node_new) {
435 iface = container_of(node_new, struct interface, node);
436 interface_start(iface);
437 }
438 }
439
440 static struct interface* _interface_add(const char *name, int multicast, int v6)
441 {
442 struct interface *iface;
443 char *name_buf;
444 char *id_buf;
445
446 iface = calloc_a(sizeof(*iface),
447 &name_buf, strlen(name) + 1,
448 &id_buf, strlen(name) + 3);
449
450 sprintf(id_buf, "%d_%s", v6, name);
451 iface->name = strcpy(name_buf, name);
452 iface->id = id_buf;
453 iface->ifindex = if_nametoindex(name);
454 iface->fd.fd = -1;
455 iface->multicast = multicast;
456 iface->v6 = v6;
457 if (v6)
458 iface->mcast_addr = MCAST_ADDR6;
459 else
460 iface->mcast_addr = MCAST_ADDR;
461
462 if (iface->ifindex <= 0)
463 goto error;
464
465 vlist_add(&interfaces, &iface->node, iface->id);
466 return iface;
467
468 error:
469 free(iface);
470 return NULL;
471 }
472
473 int interface_add(const char *name)
474 {
475 struct interface *v4 = NULL, *v6 = NULL, *unicast;
476 struct ifaddrs *ifap, *ifa;
477
478 getifaddrs(&ifap);
479
480 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
481 if (strcmp(ifa->ifa_name, name))
482 continue;
483 if (ifa->ifa_addr->sa_family == AF_INET && !v4) {
484 struct sockaddr_in *sa;
485
486 if (cfg_proto && (cfg_proto != 4))
487 continue;
488
489 v4 = _interface_add(name, 1, 0);
490 if (!v4)
491 continue;
492
493 sa = (struct sockaddr_in *) ifa->ifa_addr;
494 memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
495 inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
496
497 unicast = _interface_add(name, 0, 0);
498 if (!unicast)
499 continue;
500
501 memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
502 inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
503 }
504
505 if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
506 uint8_t ll_prefix[] = {0xfe, 0x80 };
507 struct sockaddr_in6 *sa6;
508
509 if (cfg_proto && (cfg_proto != 6))
510 continue;
511
512 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
513 if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
514 continue;
515
516 v6 = _interface_add(name, 1, 1);
517 if (!v6)
518 continue;
519 memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
520 inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
521
522 unicast = _interface_add(name, 0, 1);
523 if (!unicast)
524 continue;
525
526 memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
527 inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
528 }
529 }
530
531 freeifaddrs(ifap);
532
533 return !v4 && !v6;
534 }
535
536 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);