cache: add support for scanning on multiple interfaces
[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/avl-cmp.h>
31 #include <libubox/utils.h>
32 #include "interface.h"
33 #include "util.h"
34 #include "dns.h"
35
36 struct interface *cur_iface = NULL;
37
38 int
39 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
40 {
41         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
42         static struct sockaddr_in a = {
43                 .sin_family = AF_INET,
44                 .sin_port = htons(MCAST_PORT),
45         };
46         static struct msghdr m = {
47                 .msg_name = (struct sockaddr *) &a,
48                 .msg_namelen = sizeof(a),
49                 .msg_control = cmsg_data,
50                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
51         };
52         struct in_pktinfo *pkti;
53         struct cmsghdr *cmsg;
54         int fd = iface->fd.fd;
55
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_free(struct interface *iface)
74 {
75         if (cur_iface == iface)
76                 cur_iface = NULL;
77
78         if (iface->fd.fd >= 0) {
79                 uloop_fd_delete(&iface->fd);
80                 close(iface->fd.fd);
81         }
82         free(iface);
83 }
84
85 static void interface_start(struct interface *iface)
86 {
87         cur_iface = iface;
88 }
89
90 static void
91 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
92                 struct vlist_node *node_old)
93 {
94         struct interface *iface;
95
96         if (node_old) {
97                 iface = container_of(node_old, struct interface, node);
98                 interface_free(iface);
99         }
100
101         if (node_new) {
102                 iface = container_of(node_new, struct interface, node);
103                 interface_start(iface);
104         }
105 }
106
107 int interface_socket_setup(struct interface *iface)
108 {
109         struct ip_mreqn mreq;
110         uint8_t ttl = 255;
111         int yes = 1;
112         int no = 0;
113         struct sockaddr_in sa = { 0 };
114         struct in_addr in;
115         int fd = iface->fd.fd;
116
117         inet_aton(iface->ip, &in);
118
119         sa.sin_family = AF_INET;
120         sa.sin_port = htons(MCAST_PORT);
121         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
122
123         memset(&mreq, 0, sizeof(mreq));
124         mreq.imr_address.s_addr = in.s_addr;
125         mreq.imr_multiaddr = sa.sin_addr;
126         mreq.imr_ifindex = iface->ifindex;
127
128         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
129                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
130
131         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
132                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
133
134         /* Some network drivers have issues with dropping membership of
135          * mcast groups when the iface is down, but don't allow rejoining
136          * when it comes back up. This is an ugly workaround
137          * -- this was copied from avahi --
138          */
139         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
140
141         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
142                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
143                 close(fd);
144                 fd = -1;
145                 return -1;
146         }
147
148         if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
149                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
150
151         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
152                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
153
154         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
155                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
156
157         return 0;
158 }
159
160 static const char*
161 get_iface_ipv4(const char *ifname)
162 {
163         static char buffer[INET_ADDRSTRLEN];
164         struct ifreq ir;
165         const char *ret;
166         int sock;
167
168         sock = socket(AF_INET, SOCK_DGRAM, 0);
169         if (sock < 0)
170                 return NULL;
171
172         memset(&ir, 0, sizeof(struct ifreq));
173         strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
174
175         if (ioctl(sock, SIOCGIFADDR, &ir) < 0)
176                 return NULL;
177
178         ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer));
179         close(sock);
180
181         return ret;
182 }
183
184 int interface_add(const char *name)
185 {
186         struct interface *iface;
187         const char *ip_str;
188         char *name_buf, *ip_buf;
189
190         ip_str = get_iface_ipv4(name);
191         if (!ip_str)
192                 return -1;
193
194         iface = calloc_a(sizeof(*iface),
195                 &name_buf, strlen(name) + 1,
196                 &ip_buf, strlen(ip_str) + 1);
197
198         iface->name = strcpy(name_buf, name);
199         iface->ip = strcpy(ip_buf, ip_str);
200         iface->ifindex = if_nametoindex(name);
201         iface->fd.fd = -1;
202
203         if (iface->ifindex <= 0)
204                 goto error;
205
206         vlist_add(&interfaces, &iface->node, name);
207         return 0;
208
209 error:
210         free(iface);
211         return -1;
212 }
213
214 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);