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.
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
19 #include <sys/utsname.h>
21 #include <linux/sockios.h>
22 #include <arpa/inet.h>
30 #include <libubox/usock.h>
31 #include <libubox/uloop.h>
32 #include <libubox/avl-cmp.h>
33 #include <libubox/utils.h>
34 #include "interface.h"
40 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
42 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
43 static struct sockaddr_in a;
44 static struct msghdr m = {
45 .msg_name = (struct sockaddr *) &a,
46 .msg_namelen = sizeof(a),
47 .msg_control = cmsg_data,
48 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
50 struct in_pktinfo *pkti;
52 int fd = iface->fd.fd;
54 a.sin_family = AF_INET;
55 a.sin_port = htons(MCAST_PORT);
57 m.msg_iovlen = iov_len;
59 memset(cmsg_data, 0, sizeof(cmsg_data));
60 cmsg = CMSG_FIRSTHDR(&m);
61 cmsg->cmsg_len = m.msg_controllen;
62 cmsg->cmsg_level = IPPROTO_IP;
63 cmsg->cmsg_type = IP_PKTINFO;
65 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
66 pkti->ipi_ifindex = iface->ifindex;
68 a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
70 return sendmsg(fd, &m, 0);
73 static void interface_close(struct interface *iface)
79 uloop_fd_delete(&iface->fd);
84 static void interface_free(struct interface *iface)
86 interface_close(iface);
91 read_socket(struct uloop_fd *u, unsigned int events)
93 struct interface *iface = container_of(u, struct interface, fd);
94 static uint8_t buffer[8 * 1024];
98 interface_close(iface);
99 uloop_timeout_set(&iface->reconnect, 1000);
103 len = read(u->fd, buffer, sizeof(buffer));
105 fprintf(stderr, "read failed: %s\n", strerror(errno));
109 dns_handle_packet(iface, buffer, len);
113 interface_socket_setup(struct interface *iface)
115 struct ip_mreqn mreq;
119 struct sockaddr_in sa = { 0 };
120 int fd = iface->fd.fd;
122 sa.sin_family = AF_INET;
123 sa.sin_port = htons(MCAST_PORT);
124 inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
126 memset(&mreq, 0, sizeof(mreq));
127 mreq.imr_address.s_addr = iface->v4_addr.s_addr;
128 mreq.imr_multiaddr = sa.sin_addr;
129 mreq.imr_ifindex = iface->ifindex;
131 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
132 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
134 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
135 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
137 /* Some network drivers have issues with dropping membership of
138 * mcast groups when the iface is down, but don't allow rejoining
139 * when it comes back up. This is an ugly workaround
140 * -- this was copied from avahi --
142 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
144 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
145 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
151 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
152 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
154 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
155 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
157 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
158 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
164 reconnect_socket(struct uloop_timeout *timeout)
166 struct interface *iface = container_of(timeout, struct interface, reconnect);
168 iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
169 if (iface->fd.fd < 0) {
170 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
174 if (interface_socket_setup(iface)) {
179 uloop_fd_add(&iface->fd, ULOOP_READ);
180 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
181 announce_init(iface);
185 uloop_timeout_set(timeout, 1000);
189 static void interface_start(struct interface *iface)
191 iface->fd.cb = read_socket;
192 iface->reconnect.cb = reconnect_socket;
193 uloop_timeout_set(&iface->reconnect, 100);
197 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
198 struct vlist_node *node_old)
200 struct interface *iface;
203 iface = container_of(node_old, struct interface, node);
204 interface_free(iface);
208 iface = container_of(node_new, struct interface, node);
209 interface_start(iface);
214 get_iface_ipv4(struct interface *iface)
216 struct sockaddr_in *sin;
220 sock = socket(AF_INET, SOCK_DGRAM, 0);
224 memset(&ir, 0, sizeof(struct ifreq));
225 strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
227 ret = ioctl(sock, SIOCGIFADDR, &ir);
231 sin = (struct sockaddr_in *) &ir.ifr_addr;
232 memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
239 int interface_add(const char *name)
241 struct interface *iface;
244 iface = calloc_a(sizeof(*iface),
245 &name_buf, strlen(name) + 1);
247 iface->name = strcpy(name_buf, name);
248 iface->ifindex = if_nametoindex(name);
251 if (iface->ifindex <= 0)
254 if (get_iface_ipv4(iface))
257 vlist_add(&interfaces, &iface->node, name);
265 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);