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