blob: 05153f56cb686f1d91e33ea1f2f97d56b18bfbff [file] [log] [blame]
Simon Kelleyd1ced3a2018-01-01 22:18:03 +00001/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley
Simon Kelley0a852542005-03-23 20:28:59 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley0a852542005-03-23 20:28:59 +00008 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
Simon Kelley0a852542005-03-23 20:28:59 +000015*/
16
Simon Kelley0a852542005-03-23 20:28:59 +000017#include "dnsmasq.h"
18
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010019#ifdef HAVE_LINUX_NETWORK
Simon Kelley0a852542005-03-23 20:28:59 +000020
Simon Kelley91dccd02005-03-31 17:48:32 +010021#include <linux/types.h>
Simon Kelley0a852542005-03-23 20:28:59 +000022#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24
Simon Kelley832af0b2007-01-21 20:01:28 +000025/* linux 2.6.19 buggers up the headers, patch it up here. */
26#ifndef IFA_RTA
27# define IFA_RTA(r) \
28 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
29
30# include <linux/if_addr.h>
31#endif
32
Simon Kelley28866e92011-02-14 20:19:14 +000033#ifndef NDA_RTA
34# define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
35#endif
36
Simon Kelleyc72daea2012-01-05 21:33:27 +000037
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010038static struct iovec iov;
Simon Kelley7622fc02009-06-04 20:32:05 +010039static u32 netlink_pid;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010040
Simon Kelleya0358e52014-06-07 13:38:48 +010041static void nl_async(struct nlmsghdr *h);
Simon Kelleycdeda282006-03-16 20:16:06 +000042
Simon Kelley5aabfc72007-08-29 11:24:47 +010043void netlink_init(void)
Simon Kelley0a852542005-03-23 20:28:59 +000044{
45 struct sockaddr_nl addr;
Simon Kelley7622fc02009-06-04 20:32:05 +010046 socklen_t slen = sizeof(addr);
Simon Kelley0a852542005-03-23 20:28:59 +000047
48 addr.nl_family = AF_NETLINK;
49 addr.nl_pad = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010050 addr.nl_pid = 0; /* autobind */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010051 addr.nl_groups = RTMGRP_IPV4_ROUTE;
Simon Kelley54dd3932012-06-20 11:23:38 +010052 if (option_bool(OPT_CLEVERBIND))
Simon Kelley1f776932012-12-16 19:46:08 +000053 addr.nl_groups |= RTMGRP_IPV4_IFADDR;
Simon Kelley54dd3932012-06-20 11:23:38 +010054#ifdef HAVE_IPV6
55 addr.nl_groups |= RTMGRP_IPV6_ROUTE;
Simon Kelley1f776932012-12-16 19:46:08 +000056 if (option_bool(OPT_CLEVERBIND))
57 addr.nl_groups |= RTMGRP_IPV6_IFADDR;
58#endif
59#ifdef HAVE_DHCP6
60 if (daemon->doing_ra || daemon->doing_dhcp6)
Simon Kelley54dd3932012-06-20 11:23:38 +010061 addr.nl_groups |= RTMGRP_IPV6_IFADDR;
Simon Kelleycdeda282006-03-16 20:16:06 +000062#endif
Simon Kelley0a852542005-03-23 20:28:59 +000063
Simon Kelley309331f2006-04-22 15:05:01 +010064 /* May not be able to have permission to set multicast groups don't die in that case */
65 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
66 {
67 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
68 {
69 addr.nl_groups = 0;
70 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
71 daemon->netlinkfd = -1;
72 }
73 }
74
Simon Kelley7622fc02009-06-04 20:32:05 +010075 if (daemon->netlinkfd == -1 ||
Reiter Wolfgang5eb9dde2017-01-08 17:39:06 +000076 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +010077 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
Simon Kelley7622fc02009-06-04 20:32:05 +010078
79 /* save pid assigned by bind() and retrieved by getsockname() */
80 netlink_pid = addr.nl_pid;
Simon Kelley5aabfc72007-08-29 11:24:47 +010081
Simon Kelley7622fc02009-06-04 20:32:05 +010082 iov.iov_len = 100;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010083 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley0a852542005-03-23 20:28:59 +000084}
85
Simon Kelley5aabfc72007-08-29 11:24:47 +010086static ssize_t netlink_recv(void)
Simon Kelleycdeda282006-03-16 20:16:06 +000087{
88 struct msghdr msg;
Simon Kelley7622fc02009-06-04 20:32:05 +010089 struct sockaddr_nl nladdr;
Simon Kelleycdeda282006-03-16 20:16:06 +000090 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +000091
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010092 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +000093 {
Simon Kelley7622fc02009-06-04 20:32:05 +010094 msg.msg_control = NULL;
95 msg.msg_controllen = 0;
96 msg.msg_name = &nladdr;
97 msg.msg_namelen = sizeof(nladdr);
98 msg.msg_iov = &iov;
99 msg.msg_iovlen = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100100 msg.msg_flags = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100101
Simon Kelley7622fc02009-06-04 20:32:05 +0100102 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
103
104 /* make buffer big enough */
105 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100106 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100107 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
108 if ((size_t)rc == iov.iov_len)
109 {
110 if (expand_buf(&iov, rc + 100))
111 continue;
112 }
113 else
114 expand_buf(&iov, rc);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100115 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100116
Simon Kelley7622fc02009-06-04 20:32:05 +0100117 /* read it for real */
118 msg.msg_flags = 0;
119 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
120
121 /* Make sure this is from the kernel */
122 if (rc == -1 || nladdr.nl_pid == 0)
123 break;
124 }
125
126 /* discard stuff which is truncated at this point (expand_buf() may fail) */
127 if (msg.msg_flags & MSG_TRUNC)
128 {
129 rc = -1;
130 errno = ENOMEM;
131 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000132
133 return rc;
134}
135
Simon Kelley28866e92011-02-14 20:19:14 +0000136
Simon Kelleyc72daea2012-01-05 21:33:27 +0000137/* family = AF_UNSPEC finds ARP table entries.
138 family = AF_LOCAL finds MAC addresses. */
Simon Kelley28866e92011-02-14 20:19:14 +0000139int iface_enumerate(int family, void *parm, int (*callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000140{
141 struct sockaddr_nl addr;
142 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000143 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000144 static unsigned int seq = 0;
Simon Kelleya0358e52014-06-07 13:38:48 +0100145 int callback_ok = 1;
Simon Kelley0a852542005-03-23 20:28:59 +0000146
147 struct {
148 struct nlmsghdr nlh;
149 struct rtgenmsg g;
150 } req;
151
Simon Kelley0a852542005-03-23 20:28:59 +0000152 addr.nl_family = AF_NETLINK;
153 addr.nl_pad = 0;
154 addr.nl_groups = 0;
155 addr.nl_pid = 0; /* address to kernel */
Simon Kelleyc72daea2012-01-05 21:33:27 +0000156
157 again:
158 if (family == AF_UNSPEC)
159 req.nlh.nlmsg_type = RTM_GETNEIGH;
160 else if (family == AF_LOCAL)
161 req.nlh.nlmsg_type = RTM_GETLINK;
162 else
163 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley0a852542005-03-23 20:28:59 +0000164
165 req.nlh.nlmsg_len = sizeof(req);
Simon Kelley7cebd202006-05-06 14:13:33 +0100166 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000167 req.nlh.nlmsg_pid = 0;
168 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100169 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000170
171 /* Don't block in recvfrom if send fails */
Simon Kelleyff841eb2015-03-11 21:36:30 +0000172 while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
173 (struct sockaddr *)&addr, sizeof(addr))));
174
175 if (errno != 0)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100176 return 0;
177
178 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000179 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100180 if ((len = netlink_recv()) == -1)
Simon Kelley7622fc02009-06-04 20:32:05 +0100181 {
182 if (errno == ENOBUFS)
183 {
184 sleep(1);
185 goto again;
186 }
187 return 0;
188 }
189
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100190 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
Ivan Kokshaysky1d076672016-07-11 18:36:05 +0100191 if (h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR)
Simon Kelley54dd3932012-06-20 11:23:38 +0100192 {
193 /* May be multicast arriving async */
Simon Kelleya0358e52014-06-07 13:38:48 +0100194 nl_async(h);
Simon Kelley54dd3932012-06-20 11:23:38 +0100195 }
Ivan Kokshaysky1d076672016-07-11 18:36:05 +0100196 else if (h->nlmsg_seq != seq)
197 {
198 /* May be part of incomplete response to previous request after
199 ENOBUFS. Drop it. */
200 continue;
201 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100202 else if (h->nlmsg_type == NLMSG_DONE)
Simon Kelleya0358e52014-06-07 13:38:48 +0100203 return callback_ok;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000204 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100205 {
206 struct ifaddrmsg *ifa = NLMSG_DATA(h);
207 struct rtattr *rta = IFA_RTA(ifa);
208 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
209
Simon Kelley28866e92011-02-14 20:19:14 +0000210 if (ifa->ifa_family == family)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100211 {
Simon Kelley28866e92011-02-14 20:19:14 +0000212 if (ifa->ifa_family == AF_INET)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100213 {
Simon Kelley28866e92011-02-14 20:19:14 +0000214 struct in_addr netmask, addr, broadcast;
Simon Kelley3f2873d2013-05-14 11:28:47 +0100215 char *label = NULL;
216
Richard Genoud10cfc0d2014-09-17 21:17:39 +0100217 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen));
218
Simon Kelley28866e92011-02-14 20:19:14 +0000219 addr.s_addr = 0;
220 broadcast.s_addr = 0;
221
222 while (RTA_OK(rta, len1))
223 {
224 if (rta->rta_type == IFA_LOCAL)
225 addr = *((struct in_addr *)(rta+1));
226 else if (rta->rta_type == IFA_BROADCAST)
227 broadcast = *((struct in_addr *)(rta+1));
Simon Kelley3f2873d2013-05-14 11:28:47 +0100228 else if (rta->rta_type == IFA_LABEL)
229 label = RTA_DATA(rta);
Simon Kelley28866e92011-02-14 20:19:14 +0000230
231 rta = RTA_NEXT(rta, len1);
232 }
233
Simon Kelleye44ddca2012-02-18 17:08:50 +0000234 if (addr.s_addr && callback_ok)
Simon Kelley3f2873d2013-05-14 11:28:47 +0100235 if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000236 callback_ok = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100237 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100238#ifdef HAVE_IPV6
Simon Kelley28866e92011-02-14 20:19:14 +0000239 else if (ifa->ifa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100240 {
Simon Kelley28866e92011-02-14 20:19:14 +0000241 struct in6_addr *addrp = NULL;
Simon Kelley1f776932012-12-16 19:46:08 +0000242 u32 valid = 0, preferred = 0;
Simon Kelleybad7b872012-12-20 22:00:39 +0000243 int flags = 0;
244
Simon Kelley28866e92011-02-14 20:19:14 +0000245 while (RTA_OK(rta, len1))
246 {
247 if (rta->rta_type == IFA_ADDRESS)
248 addrp = ((struct in6_addr *)(rta+1));
Simon Kelley1f776932012-12-16 19:46:08 +0000249 else if (rta->rta_type == IFA_CACHEINFO)
250 {
251 struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1);
252 preferred = ifc->ifa_prefered;
253 valid = ifc->ifa_valid;
254 }
Simon Kelley28866e92011-02-14 20:19:14 +0000255 rta = RTA_NEXT(rta, len1);
256 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100257
Simon Kelleybad7b872012-12-20 22:00:39 +0000258 if (ifa->ifa_flags & IFA_F_TENTATIVE)
259 flags |= IFACE_TENTATIVE;
260
261 if (ifa->ifa_flags & IFA_F_DEPRECATED)
262 flags |= IFACE_DEPRECATED;
263
Jonas Gorski57ab36e2014-01-22 11:34:16 +0000264 if (!(ifa->ifa_flags & IFA_F_TEMPORARY))
265 flags |= IFACE_PERMANENT;
266
Simon Kelleye44ddca2012-02-18 17:08:50 +0000267 if (addrp && callback_ok)
Simon Kelley52b92f42012-01-22 16:05:15 +0000268 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
Simon Kelleybad7b872012-12-20 22:00:39 +0000269 (int)(ifa->ifa_index), flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000270 (int) preferred, (int)valid, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000271 callback_ok = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000272 }
273#endif
274 }
275 }
276 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
277 {
278 struct ndmsg *neigh = NLMSG_DATA(h);
279 struct rtattr *rta = NDA_RTA(neigh);
280 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
281 size_t maclen = 0;
282 char *inaddr = NULL, *mac = NULL;
283
284 while (RTA_OK(rta, len1))
285 {
286 if (rta->rta_type == NDA_DST)
287 inaddr = (char *)(rta+1);
288 else if (rta->rta_type == NDA_LLADDR)
289 {
290 maclen = rta->rta_len - sizeof(struct rtattr);
291 mac = (char *)(rta+1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100292 }
293
Simon Kelley28866e92011-02-14 20:19:14 +0000294 rta = RTA_NEXT(rta, len1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100295 }
Simon Kelley28866e92011-02-14 20:19:14 +0000296
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000297 if (!(neigh->ndm_state & (NUD_NOARP | NUD_INCOMPLETE | NUD_FAILED)) &&
298 inaddr && mac && callback_ok)
Simon Kelley28866e92011-02-14 20:19:14 +0000299 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000300 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000301 }
302#ifdef HAVE_DHCP6
303 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
304 {
305 struct ifinfomsg *link = NLMSG_DATA(h);
306 struct rtattr *rta = IFLA_RTA(link);
307 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
308 char *mac = NULL;
309 size_t maclen = 0;
310
311 while (RTA_OK(rta, len1))
312 {
313 if (rta->rta_type == IFLA_ADDRESS)
314 {
315 maclen = rta->rta_len - sizeof(struct rtattr);
316 mac = (char *)(rta+1);
317 }
318
319 rta = RTA_NEXT(rta, len1);
320 }
321
Simon Kelleye44ddca2012-02-18 17:08:50 +0000322 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000323 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000324 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000325 }
326#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000327 }
Simon Kelley0a852542005-03-23 20:28:59 +0000328}
329
Simon Kelleya0358e52014-06-07 13:38:48 +0100330void netlink_multicast(void)
Simon Kelleycdeda282006-03-16 20:16:06 +0000331{
332 ssize_t len;
333 struct nlmsghdr *h;
Simon Kelleya0358e52014-06-07 13:38:48 +0100334 int flags;
Simon Kelley7622fc02009-06-04 20:32:05 +0100335
336 /* don't risk blocking reading netlink messages here. */
337 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
338 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
339 return;
Simon Kelleycdeda282006-03-16 20:16:06 +0000340
Simon Kelley5aabfc72007-08-29 11:24:47 +0100341 if ((len = netlink_recv()) != -1)
Simon Kelley54dd3932012-06-20 11:23:38 +0100342 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
Simon Kelleya0358e52014-06-07 13:38:48 +0100343 nl_async(h);
Simon Kelley7622fc02009-06-04 20:32:05 +0100344
Simon Kelley54dd3932012-06-20 11:23:38 +0100345 /* restore non-blocking status */
346 fcntl(daemon->netlinkfd, F_SETFL, flags);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100347}
348
Simon Kelleya0358e52014-06-07 13:38:48 +0100349static void nl_async(struct nlmsghdr *h)
Simon Kelley54dd3932012-06-20 11:23:38 +0100350{
351 if (h->nlmsg_type == NLMSG_ERROR)
352 {
353 struct nlmsgerr *err = NLMSG_DATA(h);
Simon Kelleydfb23b32012-09-18 21:44:47 +0100354 if (err->error != 0)
355 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley54dd3932012-06-20 11:23:38 +0100356 }
357 else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
358 {
Simon Kelleye17b4b32012-06-28 21:44:30 +0100359 /* We arrange to receive netlink multicast messages whenever the network route is added.
360 If this happens and we still have a DNS packet in the buffer, we re-send it.
361 This helps on DoD links, where frequently the packet which triggers dialling is
362 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
363 failing. */
364 struct rtmsg *rtm = NLMSG_DATA(h);
365
366 if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK)
Simon Kelley47a95162014-07-08 22:22:02 +0100367 queue_event(EVENT_NEWROUTE);
Simon Kelley54dd3932012-06-20 11:23:38 +0100368 }
Simon Kelley1f776932012-12-16 19:46:08 +0000369 else if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
Simon Kelley47a95162014-07-08 22:22:02 +0100370 queue_event(EVENT_NEWADDR);
Simon Kelley54dd3932012-06-20 11:23:38 +0100371}
Simon Kelley0a852542005-03-23 20:28:59 +0000372#endif
373
374