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