712d3c520ca810e2bbb2f7982c4b451051e9a01f
[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 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/utsname.h>
20 #include <net/if.h>
21 #include <linux/sockios.h>
22 #include <arpa/inet.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29
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"
35 #include "util.h"
36 #include "dns.h"
37 #include "announce.h"
38
39 int
40 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
41 {
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)),
49         };
50         struct in_pktinfo *pkti;
51         struct cmsghdr *cmsg;
52         int fd = iface->fd.fd;
53
54         a.sin_family = AF_INET;
55         a.sin_port = htons(MCAST_PORT);
56         m.msg_iov = iov;
57         m.msg_iovlen = iov_len;
58
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;
64
65         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
66         pkti->ipi_ifindex = iface->ifindex;
67
68         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
69
70         return sendmsg(fd, &m, 0);
71 }
72
73 static void interface_close(struct interface *iface)
74 {
75         if (iface->fd.fd < 0)
76                 return;
77
78         announce_free(iface);
79         uloop_fd_delete(&iface->fd);
80         close(iface->fd.fd);
81         iface->fd.fd = -1;
82 }
83
84 static void interface_free(struct interface *iface)
85 {
86         interface_close(iface);
87         free(iface);
88 }
89
90 static void
91 read_socket(struct uloop_fd *u, unsigned int events)
92 {
93         struct interface *iface = container_of(u, struct interface, fd);
94         static uint8_t buffer[8 * 1024];
95         int len;
96
97         if (u->eof) {
98                 interface_close(iface);
99                 uloop_timeout_set(&iface->reconnect, 1000);
100                 return;
101         }
102
103         len = read(u->fd, buffer, sizeof(buffer));
104         if (len < 1) {
105                 fprintf(stderr, "read failed: %s\n", strerror(errno));
106                 return;
107         }
108
109         dns_handle_packet(iface, buffer, len);
110 }
111
112 static int
113 interface_socket_setup(struct interface *iface)
114 {
115         struct ip_mreqn mreq;
116         uint8_t ttl = 255;
117         int yes = 1;
118         int no = 0;
119         struct sockaddr_in sa = { 0 };
120         int fd = iface->fd.fd;
121
122         sa.sin_family = AF_INET;
123         sa.sin_port = htons(MCAST_PORT);
124         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
125
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;
130
131         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
132                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
133
134         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
135                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
136
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 --
141          */
142         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
143
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));
146                 close(fd);
147                 fd = -1;
148                 return -1;
149         }
150
151         if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
152                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
153
154         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
155                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
156
157         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
158                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
159
160         return 0;
161 }
162
163 static void
164 reconnect_socket(struct uloop_timeout *timeout)
165 {
166         struct interface *iface = container_of(timeout, struct interface, reconnect);
167
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));
171                 goto retry;
172         }
173
174         if (interface_socket_setup(iface)) {
175                 iface->fd.fd = -1;
176                 goto retry;
177         }
178
179         uloop_fd_add(&iface->fd, ULOOP_READ);
180         dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
181         announce_init(iface);
182         return;
183
184 retry:
185         uloop_timeout_set(timeout, 1000);
186 }
187
188
189 static void interface_start(struct interface *iface)
190 {
191         iface->fd.cb = read_socket;
192         iface->reconnect.cb = reconnect_socket;
193         uloop_timeout_set(&iface->reconnect, 100);
194 }
195
196 static void
197 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
198                 struct vlist_node *node_old)
199 {
200         struct interface *iface;
201
202         if (node_old) {
203                 iface = container_of(node_old, struct interface, node);
204                 interface_free(iface);
205         }
206
207         if (node_new) {
208                 iface = container_of(node_new, struct interface, node);
209                 interface_start(iface);
210         }
211 }
212
213 static int
214 get_iface_ipv4(struct interface *iface)
215 {
216         struct sockaddr_in *sin;
217         struct ifreq ir;
218         int sock, ret = -1;
219
220         sock = socket(AF_INET, SOCK_DGRAM, 0);
221         if (sock < 0)
222                 return -1;
223
224         memset(&ir, 0, sizeof(struct ifreq));
225         strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
226
227         ret = ioctl(sock, SIOCGIFADDR, &ir);
228         if (ret < 0)
229                 goto out;
230
231         sin = (struct sockaddr_in *) &ir.ifr_addr;
232         memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
233
234 out:
235         close(sock);
236         return ret;
237 }
238
239 int interface_add(const char *name)
240 {
241         struct interface *iface;
242         char *name_buf;
243
244         iface = calloc_a(sizeof(*iface),
245                 &name_buf, strlen(name) + 1);
246
247         iface->name = strcpy(name_buf, name);
248         iface->ifindex = if_nametoindex(name);
249         iface->fd.fd = -1;
250
251         if (iface->ifindex <= 0)
252                 goto error;
253
254         if (get_iface_ipv4(iface))
255                 goto error;
256
257         vlist_add(&interfaces, &iface->node, name);
258         return 0;
259
260 error:
261         free(iface);
262         return -1;
263 }
264
265 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);