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>
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
34 #include <libubox/utils.h>
35 #include "interface.h"
41 interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len)
43 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
44 static struct sockaddr_in a;
45 static struct msghdr m = {
46 .msg_name = (struct sockaddr *) &a,
47 .msg_namelen = sizeof(a),
48 .msg_control = cmsg_data,
49 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
51 struct in_pktinfo *pkti;
53 int fd = iface->fd.fd;
55 a.sin_family = AF_INET;
56 a.sin_port = htons(MCAST_PORT);
58 m.msg_iovlen = iov_len;
60 memset(cmsg_data, 0, sizeof(cmsg_data));
61 cmsg = CMSG_FIRSTHDR(&m);
62 cmsg->cmsg_len = m.msg_controllen;
63 cmsg->cmsg_level = IPPROTO_IP;
64 cmsg->cmsg_type = IP_PKTINFO;
66 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
67 pkti->ipi_ifindex = iface->ifindex;
69 a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
71 return sendmsg(fd, &m, 0);
75 interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len)
77 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
78 static struct sockaddr_in6 a;
79 static struct msghdr m = {
80 .msg_name = (struct sockaddr *) &a,
81 .msg_namelen = sizeof(a),
82 .msg_control = cmsg_data,
83 .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)),
85 struct in6_pktinfo *pkti;
87 int fd = iface->fd.fd;
89 a.sin6_family = AF_INET6;
90 a.sin6_port = htons(MCAST_PORT);
92 m.msg_iovlen = iov_len;
94 memset(cmsg_data, 0, sizeof(cmsg_data));
95 cmsg = CMSG_FIRSTHDR(&m);
96 cmsg->cmsg_len = m.msg_controllen;
97 cmsg->cmsg_level = IPPROTO_IPV6;
98 cmsg->cmsg_type = IPV6_PKTINFO;
100 pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
101 pkti->ipi6_ifindex = iface->ifindex;
103 inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
105 return sendmsg(fd, &m, 0);
109 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
112 return interface_send_packet6(iface, iov, iov_len);
114 return interface_send_packet4(iface, iov, iov_len);
117 static void interface_close(struct interface *iface)
119 if (iface->fd.fd < 0)
122 announce_free(iface);
123 uloop_fd_delete(&iface->fd);
128 static void interface_free(struct interface *iface)
130 interface_close(iface);
135 read_socket(struct uloop_fd *u, unsigned int events)
137 struct interface *iface = container_of(u, struct interface, fd);
138 static uint8_t buffer[8 * 1024];
142 interface_close(iface);
143 uloop_timeout_set(&iface->reconnect, 1000);
147 len = read(u->fd, buffer, sizeof(buffer));
150 fprintf(stderr, "read failed: %s\n", strerror(errno));
154 dns_handle_packet(iface, buffer, len);
158 interface_socket_setup4(struct interface *iface)
160 struct ip_mreqn mreq;
164 struct sockaddr_in sa = { 0 };
165 int fd = iface->fd.fd;
167 sa.sin_family = AF_INET;
168 sa.sin_port = htons(MCAST_PORT);
169 inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
171 memset(&mreq, 0, sizeof(mreq));
172 mreq.imr_address.s_addr = iface->v4_addr.s_addr;
173 mreq.imr_multiaddr = sa.sin_addr;
174 mreq.imr_ifindex = iface->ifindex;
176 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
177 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
179 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
180 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
182 /* Some network drivers have issues with dropping membership of
183 * mcast groups when the iface is down, but don't allow rejoining
184 * when it comes back up. This is an ugly workaround
185 * -- this was copied from avahi --
187 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
189 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
190 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
196 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
197 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
199 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
200 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
202 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
203 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
209 interface_socket_setup6(struct interface *iface)
211 struct ipv6_mreq mreq;
215 struct sockaddr_in6 sa = { 0 };
216 int fd = iface->fd.fd;
218 sa.sin6_family = AF_INET6;
219 sa.sin6_port = htons(MCAST_PORT);
220 inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
222 memset(&mreq, 0, sizeof(mreq));
223 mreq.ipv6mr_multiaddr = sa.sin6_addr;
224 mreq.ipv6mr_interface = iface->ifindex;
226 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
227 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
229 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
230 fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
232 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
233 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
235 setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
236 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
237 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
243 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
244 fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
246 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
247 fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
249 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
250 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
256 reconnect_socket(struct uloop_timeout *timeout)
258 struct interface *iface = container_of(timeout, struct interface, reconnect);
263 snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", iface->mcast_addr, iface->name);
264 type = USOCK_IPV6ONLY;
266 snprintf(mcast_addr, sizeof(mcast_addr), "%s", iface->mcast_addr);
267 type = USOCK_IPV4ONLY;
270 iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | type, mcast_addr, "5353");
271 if (iface->fd.fd < 0) {
272 fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
276 if (!iface->v6 && interface_socket_setup4(iface)) {
281 if (iface->v6 && interface_socket_setup6(iface)) {
286 uloop_fd_add(&iface->fd, ULOOP_READ);
287 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
288 announce_init(iface);
292 uloop_timeout_set(timeout, 1000);
296 static void interface_start(struct interface *iface)
298 iface->fd.cb = read_socket;
299 iface->reconnect.cb = reconnect_socket;
300 uloop_timeout_set(&iface->reconnect, 100);
304 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
305 struct vlist_node *node_old)
307 struct interface *iface;
310 iface = container_of(node_old, struct interface, node);
311 interface_free(iface);
315 iface = container_of(node_new, struct interface, node);
316 interface_start(iface);
321 get_iface_ipv4(struct interface *iface)
323 struct sockaddr_in *sin;
327 sock = socket(AF_INET, SOCK_DGRAM, 0);
331 memset(&ir, 0, sizeof(struct ifreq));
332 strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
334 ret = ioctl(sock, SIOCGIFADDR, &ir);
338 sin = (struct sockaddr_in *) &ir.ifr_addr;
339 memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
340 iface->mcast_addr = MCAST_ADDR;
347 get_iface_ipv6(struct interface *iface)
349 struct sockaddr_in6 addr = {AF_INET6, 0, iface->ifindex, IN6ADDR_ANY_INIT, 0};
350 socklen_t alen = sizeof(addr);
353 addr.sin6_addr.s6_addr[0] = 0xff;
354 addr.sin6_addr.s6_addr[1] = 0x02;
355 addr.sin6_addr.s6_addr[15] = 0x01;
357 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
358 connect(sock, (struct sockaddr*)&addr, sizeof(addr));
359 ret = getsockname(sock, (struct sockaddr*)&addr, &alen);
361 memcpy(&iface->v6_addr, &addr.sin6_addr, sizeof(iface->v6_addr));
362 iface->mcast_addr = MCAST_ADDR6;
369 static int _interface_add(const char *name, int v6)
371 struct interface *iface;
375 iface = calloc_a(sizeof(*iface),
376 &name_buf, strlen(name) + 1,
377 &id_buf, strlen(name) + 3);
379 sprintf(id_buf, "%d_%s", v6, name);
380 iface->name = strcpy(name_buf, name);
382 iface->ifindex = if_nametoindex(name);
385 if (iface->ifindex <= 0)
388 if (!v6 && get_iface_ipv4(iface))
391 if (v6 && get_iface_ipv6(iface))
394 vlist_add(&interfaces, &iface->node, iface->id);
402 int interface_add(const char *name)
404 int v4 = _interface_add(name, 0);
405 int v6 = _interface_add(name, 1);
409 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);