5fb9bad072edbec2bcb43c12d6c0a37a588a5a2a
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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
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.
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
20 #include <sys/utsname.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/types.h>
33 #include <libubox/usock.h>
34 #include <libubox/uloop.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
38 #include "interface.h"
45 interface_send_packet4(struct interface
*iface
, struct sockaddr_in
*to
, struct iovec
*iov
, int iov_len
)
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
)),
55 struct in_pktinfo
*pkti
;
57 int fd
= iface
->fd
.fd
;
59 a
.sin_family
= AF_INET
;
60 a
.sin_port
= htons(MCAST_PORT
);
62 m
.msg_iovlen
= iov_len
;
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
;
70 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
71 pkti
->ipi_ifindex
= iface
->ifindex
;
73 if (iface
->multicast
) {
74 a
.sin_addr
.s_addr
= inet_addr(MCAST_ADDR
);
76 fprintf(stderr
, "Ignoring IPv4 address for multicast interface\n");
78 a
.sin_addr
.s_addr
= to
->sin_addr
.s_addr
;
81 return sendmsg(fd
, &m
, 0);
85 interface_send_packet6(struct interface
*iface
, struct sockaddr_in6
*to
, struct iovec
*iov
, int iov_len
)
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
)),
95 struct in6_pktinfo
*pkti
;
97 int fd
= iface
->fd
.fd
;
99 a
.sin6_family
= AF_INET6
;
100 a
.sin6_port
= htons(MCAST_PORT
);
102 m
.msg_iovlen
= iov_len
;
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
;
110 pkti
= (struct in6_pktinfo
*) CMSG_DATA(cmsg
);
111 pkti
->ipi6_ifindex
= iface
->ifindex
;
113 if (iface
->multicast
) {
114 inet_pton(AF_INET6
, MCAST_ADDR6
, &a
.sin6_addr
);
116 fprintf(stderr
, "Ignoring IPv6 address for multicast interface\n");
118 a
.sin6_addr
= to
->sin6_addr
;
121 return sendmsg(fd
, &m
, 0);
125 interface_send_packet(struct interface
*iface
, struct sockaddr
*to
, struct iovec
*iov
, int iov_len
)
127 if (!iface
->multicast
&& !to
) {
128 fprintf(stderr
, "No IP address specified for unicast interface\n");
134 fprintf(stderr
, "TX ipv%d: %s\n", iface
->v6
* 2 + 4, iface
->name
);
135 fprintf(stderr
, " multicast: %d\n", iface
->multicast
);
139 return interface_send_packet6(iface
, (struct sockaddr_in6
*)to
, iov
, iov_len
);
141 return interface_send_packet4(iface
, (struct sockaddr_in
*)to
, iov
, iov_len
);
144 static void interface_close(struct interface
*iface
)
146 if (iface
->fd
.fd
< 0)
149 announce_free(iface
);
150 uloop_fd_delete(&iface
->fd
);
155 static void interface_free(struct interface
*iface
)
157 uloop_timeout_cancel(&iface
->reconnect
);
158 interface_close(iface
);
163 interface_valid_src(void *ip1
, void *mask
, void *ip2
, int len
)
173 for (i
= 0; i
< len
; i
++, i1
++, i2
++, m
++) {
174 if ((*i1
& *m
) != (*i2
& *m
))
182 read_socket4(struct uloop_fd
*u
, unsigned int events
)
184 struct interface
*iface
= container_of(u
, struct interface
, fd
);
185 static uint8_t buffer
[8 * 1024];
187 char cmsg
[CMSG_SPACE(sizeof(struct in_pktinfo
)) + CMSG_SPACE(sizeof(int)) + 1];
188 struct cmsghdr
*cmsgptr
;
191 struct sockaddr_in from
;
192 int flags
= 0, ifindex
= -1;
194 struct in_pktinfo
*inp
= NULL
;
197 interface_close(iface
);
198 uloop_timeout_set(&iface
->reconnect
, 1000);
202 iov
[0].iov_base
= buffer
;
203 iov
[0].iov_len
= sizeof(buffer
);
205 memset(&msg
, 0, sizeof(msg
));
206 msg
.msg_name
= (struct sockaddr
*) &from
;
207 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
210 msg
.msg_control
= &cmsg
;
211 msg
.msg_controllen
= sizeof(cmsg
);
213 len
= recvmsg(u
->fd
, &msg
, flags
);
215 perror("read failed");
218 for (cmsgptr
= CMSG_FIRSTHDR(&msg
); cmsgptr
!= NULL
; cmsgptr
= CMSG_NXTHDR(&msg
, cmsgptr
)) {
219 void *c
= CMSG_DATA(cmsgptr
);
221 switch (cmsgptr
->cmsg_type
) {
223 inp
= ((struct in_pktinfo
*) c
);
227 ttl
= (uint8_t) *((int *) c
);
231 fprintf(stderr
, "unknown cmsg %x\n", cmsgptr
->cmsg_type
);
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 fprintf(stderr
, " ttl %u\n", ttl
);
253 if (inp
->ipi_ifindex
!= iface
->ifindex
)
254 fprintf(stderr
, "invalid iface index %d != %d\n", ifindex
, iface
->ifindex
);
255 else if (!interface_valid_src((void *) &iface
->v4_addr
, (void *) &iface
->v4_netmask
, (void *) &from
.sin_addr
, 4))
256 dns_handle_packet(iface
, (struct sockaddr
*) &from
, ntohs(from
.sin_port
), buffer
, len
);
260 read_socket6(struct uloop_fd
*u
, unsigned int events
)
262 struct interface
*iface
= container_of(u
, struct interface
, fd
);
263 static uint8_t buffer
[8 * 1024];
265 char cmsg6
[CMSG_SPACE(sizeof(struct in6_pktinfo
)) + CMSG_SPACE(sizeof(int)) + 1];
266 struct cmsghdr
*cmsgptr
;
269 struct sockaddr_in6 from
;
270 int flags
= 0, ifindex
= -1;
272 struct in6_pktinfo
*inp
= NULL
;
275 interface_close(iface
);
276 uloop_timeout_set(&iface
->reconnect
, 1000);
280 iov
[0].iov_base
= buffer
;
281 iov
[0].iov_len
= sizeof(buffer
);
283 memset(&msg
, 0, sizeof(msg
));
284 msg
.msg_name
= (struct sockaddr
*) &from
;
285 msg
.msg_namelen
= sizeof(struct sockaddr_in6
);
288 msg
.msg_control
= &cmsg6
;
289 msg
.msg_controllen
= sizeof(cmsg6
);
291 len
= recvmsg(u
->fd
, &msg
, flags
);
293 perror("read failed");
296 for (cmsgptr
= CMSG_FIRSTHDR(&msg
); cmsgptr
!= NULL
; cmsgptr
= CMSG_NXTHDR(&msg
, cmsgptr
)) {
297 void *c
= CMSG_DATA(cmsgptr
);
299 switch (cmsgptr
->cmsg_type
) {
301 inp
= ((struct in6_pktinfo
*) c
);
305 ttl
= (uint8_t) *((int *) c
);
309 fprintf(stderr
, "unknown cmsg %x\n", cmsgptr
->cmsg_type
);
320 fprintf(stderr
, "RX ipv6: %s\n", iface
->name
);
321 fprintf(stderr
, " multicast: %d\n", iface
->multicast
);
322 inet_ntop(AF_INET6
, &from
.sin6_addr
, buf
, 256);
323 fprintf(stderr
, " src %s:%d\n", buf
, ntohs(from
.sin6_port
));
324 inet_ntop(AF_INET6
, &inp
->ipi6_addr
, buf
, 256);
325 fprintf(stderr
, " dst %s\n", buf
);
326 fprintf(stderr
, " ttl %u\n", ttl
);
329 if (inp
->ipi6_ifindex
!= iface
->ifindex
)
330 fprintf(stderr
, "invalid iface index %d != %d\n", ifindex
, iface
->ifindex
);
331 else if (!interface_valid_src((void *) &iface
->v6_addr
, (void *) &iface
->v6_netmask
, (void *) &from
.sin6_addr
, 16))
332 dns_handle_packet(iface
, (struct sockaddr
*) &from
, ntohs(from
.sin6_port
), buffer
, len
);
336 interface_mcast_setup4(struct interface
*iface
)
338 struct ip_mreqn mreq
;
341 struct sockaddr_in sa
= { 0 };
342 int fd
= iface
->fd
.fd
;
344 sa
.sin_family
= AF_INET
;
345 sa
.sin_port
= htons(MCAST_PORT
);
346 inet_pton(AF_INET
, MCAST_ADDR
, &sa
.sin_addr
);
348 memset(&mreq
, 0, sizeof(mreq
));
349 mreq
.imr_address
.s_addr
= iface
->v4_addr
.s_addr
;
350 mreq
.imr_multiaddr
= sa
.sin_addr
;
351 mreq
.imr_ifindex
= iface
->ifindex
;
353 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
)) < 0)
354 fprintf(stderr
, "ioctl failed: IP_MULTICAST_TTL\n");
356 /* Some network drivers have issues with dropping membership of
357 * mcast groups when the iface is down, but don't allow rejoining
358 * when it comes back up. This is an ugly workaround
359 * -- this was copied from avahi --
361 setsockopt(fd
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, &mreq
, sizeof(mreq
));
363 if (setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0) {
364 fprintf(stderr
, "failed to join multicast group: %m\n");
370 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &no
, sizeof(no
)) < 0)
371 fprintf(stderr
, "ioctl failed: IP_MULTICAST_LOOP\n");
377 interface_socket_setup6(struct interface
*iface
)
379 struct ipv6_mreq mreq
;
382 struct sockaddr_in6 sa
= { 0 };
383 int fd
= iface
->fd
.fd
;
385 sa
.sin6_family
= AF_INET6
;
386 sa
.sin6_port
= htons(MCAST_PORT
);
387 inet_pton(AF_INET6
, MCAST_ADDR6
, &sa
.sin6_addr
);
389 memset(&mreq
, 0, sizeof(mreq
));
390 mreq
.ipv6mr_multiaddr
= sa
.sin6_addr
;
391 mreq
.ipv6mr_interface
= iface
->ifindex
;
393 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &ttl
, sizeof(ttl
)) < 0)
394 fprintf(stderr
, "ioctl failed: IPV6_MULTICAST_HOPS\n");
396 setsockopt(fd
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
, &mreq
, sizeof(mreq
));
397 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0) {
398 fprintf(stderr
, "failed to join multicast group: %m\n");
404 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &no
, sizeof(no
)) < 0)
405 fprintf(stderr
, "ioctl failed: IPV6_MULTICAST_LOOP\n");
411 reconnect_socket4(struct uloop_timeout
*timeout
)
413 struct interface
*iface
= container_of(timeout
, struct interface
, reconnect
);
417 iface
->fd
.fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
| USOCK_IPV4ONLY
,
418 (iface
->multicast
) ? (iface
->mcast_addr
) : (iface
->v4_addrs
), "5353");
419 if (iface
->fd
.fd
< 0) {
420 fprintf(stderr
, "failed to add listener %s: %m\n", iface
->mcast_addr
);
424 if (setsockopt(iface
->fd
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
, iface
->name
, strlen(iface
->name
) < 0))
425 fprintf(stderr
, "ioctl failed: SO_BINDTODEVICE\n");
427 if (setsockopt(iface
->fd
.fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
)) < 0)
428 fprintf(stderr
, "ioctl failed: SO_REUSEADDR\n");
430 if (setsockopt(iface
->fd
.fd
, IPPROTO_IP
, IP_TTL
, &ttl
, sizeof(ttl
)) < 0)
431 fprintf(stderr
, "ioctl failed: IP_TTL\n");
433 if (setsockopt(iface
->fd
.fd
, IPPROTO_IP
, IP_RECVTTL
, &yes
, sizeof(yes
)) < 0)
434 fprintf(stderr
, "ioctl failed: IP_RECVTTL\n");
436 if (setsockopt(iface
->fd
.fd
, IPPROTO_IP
, IP_PKTINFO
, &yes
, sizeof(yes
)) < 0)
437 fprintf(stderr
, "ioctl failed: IP_PKTINFO\n");
439 if (iface
->multicast
&& interface_mcast_setup4(iface
)) {
444 uloop_fd_add(&iface
->fd
, ULOOP_READ
);
445 if (iface
->multicast
) {
446 dns_send_question(iface
, NULL
, C_DNS_SD
, TYPE_PTR
, 0);
447 announce_init(iface
);
453 uloop_timeout_set(timeout
, 1000);
457 reconnect_socket6(struct uloop_timeout
*timeout
)
459 struct interface
*iface
= container_of(timeout
, struct interface
, reconnect
);
460 char mcast_addr
[128];
464 snprintf(mcast_addr
, sizeof(mcast_addr
), "%s%%%s", (iface
->multicast
) ? (iface
->mcast_addr
) : (iface
->v6_addrs
), iface
->name
);
465 iface
->fd
.fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
| USOCK_IPV6ONLY
, mcast_addr
, "5353");
466 if (iface
->fd
.fd
< 0) {
467 fprintf(stderr
, "failed to add listener %s: %m\n", mcast_addr
);
471 if (setsockopt(iface
->fd
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
, iface
->name
, strlen(iface
->name
) < 0))
472 fprintf(stderr
, "ioctl failed: SO_BINDTODEVICE\n");
474 if (setsockopt(iface
->fd
.fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &ttl
, sizeof(ttl
)) < 0)
475 fprintf(stderr
, "ioctl failed: IPV6_UNICAST_HOPS\n");
477 if (setsockopt(iface
->fd
.fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &yes
, sizeof(yes
)) < 0)
478 fprintf(stderr
, "ioctl failed: IPV6_RECVPKTINFO\n");
480 if (setsockopt(iface
->fd
.fd
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &yes
, sizeof(yes
)) < 0)
481 fprintf(stderr
, "ioctl failed: IPV6_RECVHOPLIMIT\n");
483 if (setsockopt(iface
->fd
.fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
)) < 0)
484 fprintf(stderr
, "ioctl failed: SO_REUSEADDR\n");
486 if (iface
->multicast
&& interface_socket_setup6(iface
)) {
491 uloop_fd_add(&iface
->fd
, ULOOP_READ
);
493 if (iface
->multicast
) {
494 dns_send_question(iface
, NULL
, C_DNS_SD
, TYPE_PTR
, 0);
495 announce_init(iface
);
501 uloop_timeout_set(timeout
, 1000);
505 static void interface_start(struct interface
*iface
)
508 iface
->fd
.cb
= read_socket6
;
509 iface
->reconnect
.cb
= reconnect_socket6
;
511 iface
->fd
.cb
= read_socket4
;
512 iface
->reconnect
.cb
= reconnect_socket4
;
514 uloop_timeout_set(&iface
->reconnect
, 100);
518 iface_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
519 struct vlist_node
*node_old
)
521 struct interface
*iface
;
524 iface
= container_of(node_old
, struct interface
, node
);
525 cache_cleanup(iface
);
526 interface_free(iface
);
530 iface
= container_of(node_new
, struct interface
, node
);
531 interface_start(iface
);
535 static struct interface
* _interface_add(const char *name
, int multicast
, int v6
)
537 struct interface
*iface
;
541 iface
= calloc_a(sizeof(*iface
),
542 &name_buf
, strlen(name
) + 1,
543 &id_buf
, strlen(name
) + 5);
545 sprintf(id_buf
, "%d_%d_%s", multicast
, v6
, name
);
546 iface
->name
= strcpy(name_buf
, name
);
548 iface
->ifindex
= if_nametoindex(name
);
550 iface
->multicast
= multicast
;
553 iface
->mcast_addr
= MCAST_ADDR6
;
555 iface
->mcast_addr
= MCAST_ADDR
;
557 if (iface
->ifindex
<= 0)
560 vlist_add(&interfaces
, &iface
->node
, iface
->id
);
568 int interface_add(const char *name
)
570 struct interface
*v4
= NULL
, *v6
= NULL
, *unicast
;
571 struct ifaddrs
*ifap
, *ifa
;
575 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
576 if (strcmp(ifa
->ifa_name
, name
))
578 if (ifa
->ifa_addr
->sa_family
== AF_INET
&& !v4
) {
579 struct sockaddr_in
*sa
;
581 if (cfg_proto
&& (cfg_proto
!= 4))
584 unicast
= _interface_add(name
, 0, 0);
587 v4
= _interface_add(name
, 1, 0);
591 sa
= (struct sockaddr_in
*) ifa
->ifa_addr
;
592 memcpy(&v4
->v4_addr
, &sa
->sin_addr
, sizeof(v4
->v4_addr
));
593 memcpy(&unicast
->v4_addr
, &sa
->sin_addr
, sizeof(unicast
->v4_addr
));
595 inet_ntop(AF_INET
, &sa
->sin_addr
, v4
->v4_addrs
, sizeof(v4
->v4_addrs
));
596 inet_ntop(AF_INET
, &sa
->sin_addr
, unicast
->v4_addrs
, sizeof(unicast
->v4_addrs
));
598 sa
= (struct sockaddr_in
*) ifa
->ifa_netmask
;
599 memcpy(&unicast
->v4_netmask
, &sa
->sin_addr
, sizeof(unicast
->v4_netmask
));
600 memcpy(&v4
->v4_netmask
, &sa
->sin_addr
, sizeof(v4
->v4_netmask
));
606 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&& !v6
) {
607 uint8_t ll_prefix
[] = {0xfe, 0x80 };
608 struct sockaddr_in6
*sa6
;
610 if (cfg_proto
&& (cfg_proto
!= 6))
613 sa6
= (struct sockaddr_in6
*) ifa
->ifa_addr
;
614 if (memcmp(&sa6
->sin6_addr
, &ll_prefix
, 2))
617 unicast
= _interface_add(name
, 0, 1);
620 v6
= _interface_add(name
, 1, 1);
624 memcpy(&v6
->v6_addr
, &sa6
->sin6_addr
, sizeof(v6
->v6_addr
));
625 memcpy(&unicast
->v6_addr
, &sa6
->sin6_addr
, sizeof(unicast
->v6_addr
));
627 inet_ntop(AF_INET6
, &sa6
->sin6_addr
, v6
->v6_addrs
, sizeof(v6
->v6_addrs
));
628 inet_ntop(AF_INET6
, &sa6
->sin6_addr
, unicast
->v6_addrs
, sizeof(unicast
->v6_addrs
));
630 sa6
= (struct sockaddr_in6
*) ifa
->ifa_netmask
;
631 memcpy(&v6
->v6_netmask
, &sa6
->sin6_addr
, sizeof(v6
->v6_netmask
));
632 memcpy(&unicast
->v6_netmask
, &sa6
->sin6_addr
, sizeof(unicast
->v6_netmask
));
644 void interface_shutdown(void)
646 struct interface
*iface
;
648 vlist_for_each_element(&interfaces
, iface
, node
)
649 if (iface
->fd
.fd
> 0 && iface
->multicast
) {
650 dns_reply_a(iface
, NULL
, 0);
651 service_announce_services(iface
, NULL
, 0);
653 vlist_for_each_element(&interfaces
, iface
, node
)
654 interface_close(iface
);
658 interface_get(const char *name
, int v6
, int multicast
)
661 snprintf(id_buf
, sizeof(id_buf
), "%d_%d_%s", multicast
, v6
, name
);
662 struct interface
*iface
= vlist_find(&interfaces
, id_buf
, iface
, node
);
666 VLIST_TREE(interfaces
, avl_strcmp
, iface_update_cb
, false, false);