blob: d20654dec7c83ae23203c0c0b87e96f9cbd09857 [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 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
41static void nl_err(struct nlmsghdr *h);
Simon Kelley5aabfc72007-08-29 11:24:47 +010042static void nl_routechange(struct nlmsghdr *h);
Simon Kelleycdeda282006-03-16 20:16:06 +000043
Simon Kelley5aabfc72007-08-29 11:24:47 +010044void netlink_init(void)
Simon Kelley0a852542005-03-23 20:28:59 +000045{
46 struct sockaddr_nl addr;
Simon Kelley7622fc02009-06-04 20:32:05 +010047 socklen_t slen = sizeof(addr);
Simon Kelley0a852542005-03-23 20:28:59 +000048
49 addr.nl_family = AF_NETLINK;
50 addr.nl_pad = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010051 addr.nl_pid = 0; /* autobind */
Simon Kelleycdeda282006-03-16 20:16:06 +000052#ifdef HAVE_IPV6
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010053 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000054#else
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010055 addr.nl_groups = RTMGRP_IPV4_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000056#endif
Simon Kelley0a852542005-03-23 20:28:59 +000057
Simon Kelley309331f2006-04-22 15:05:01 +010058 /* May not be able to have permission to set multicast groups don't die in that case */
59 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
60 {
61 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
62 {
63 addr.nl_groups = 0;
64 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
65 daemon->netlinkfd = -1;
66 }
67 }
68
Simon Kelley7622fc02009-06-04 20:32:05 +010069 if (daemon->netlinkfd == -1 ||
70 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +010071 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
Simon Kelley7622fc02009-06-04 20:32:05 +010072
73 /* save pid assigned by bind() and retrieved by getsockname() */
74 netlink_pid = addr.nl_pid;
Simon Kelley5aabfc72007-08-29 11:24:47 +010075
Simon Kelley7622fc02009-06-04 20:32:05 +010076 iov.iov_len = 100;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010077 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley0a852542005-03-23 20:28:59 +000078}
79
Simon Kelley5aabfc72007-08-29 11:24:47 +010080static ssize_t netlink_recv(void)
Simon Kelleycdeda282006-03-16 20:16:06 +000081{
82 struct msghdr msg;
Simon Kelley7622fc02009-06-04 20:32:05 +010083 struct sockaddr_nl nladdr;
Simon Kelleycdeda282006-03-16 20:16:06 +000084 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +000085
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010086 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +000087 {
Simon Kelley7622fc02009-06-04 20:32:05 +010088 msg.msg_control = NULL;
89 msg.msg_controllen = 0;
90 msg.msg_name = &nladdr;
91 msg.msg_namelen = sizeof(nladdr);
92 msg.msg_iov = &iov;
93 msg.msg_iovlen = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010094 msg.msg_flags = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010095
Simon Kelley7622fc02009-06-04 20:32:05 +010096 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
97
98 /* make buffer big enough */
99 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100100 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100101 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
102 if ((size_t)rc == iov.iov_len)
103 {
104 if (expand_buf(&iov, rc + 100))
105 continue;
106 }
107 else
108 expand_buf(&iov, rc);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100109 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100110
Simon Kelley7622fc02009-06-04 20:32:05 +0100111 /* read it for real */
112 msg.msg_flags = 0;
113 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
114
115 /* Make sure this is from the kernel */
116 if (rc == -1 || nladdr.nl_pid == 0)
117 break;
118 }
119
120 /* discard stuff which is truncated at this point (expand_buf() may fail) */
121 if (msg.msg_flags & MSG_TRUNC)
122 {
123 rc = -1;
124 errno = ENOMEM;
125 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000126
127 return rc;
128}
129
Simon Kelley28866e92011-02-14 20:19:14 +0000130
Simon Kelleyc72daea2012-01-05 21:33:27 +0000131/* family = AF_UNSPEC finds ARP table entries.
132 family = AF_LOCAL finds MAC addresses. */
Simon Kelley28866e92011-02-14 20:19:14 +0000133int iface_enumerate(int family, void *parm, int (*callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000134{
135 struct sockaddr_nl addr;
136 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000137 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000138 static unsigned int seq = 0;
Simon Kelleye44ddca2012-02-18 17:08:50 +0000139 int callback_ok = 1;
Simon Kelley0a852542005-03-23 20:28:59 +0000140
141 struct {
142 struct nlmsghdr nlh;
143 struct rtgenmsg g;
144 } req;
145
Simon Kelley0a852542005-03-23 20:28:59 +0000146 addr.nl_family = AF_NETLINK;
147 addr.nl_pad = 0;
148 addr.nl_groups = 0;
149 addr.nl_pid = 0; /* address to kernel */
Simon Kelleyc72daea2012-01-05 21:33:27 +0000150
151 again:
152 if (family == AF_UNSPEC)
153 req.nlh.nlmsg_type = RTM_GETNEIGH;
154 else if (family == AF_LOCAL)
155 req.nlh.nlmsg_type = RTM_GETLINK;
156 else
157 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley0a852542005-03-23 20:28:59 +0000158
159 req.nlh.nlmsg_len = sizeof(req);
Simon Kelley7cebd202006-05-06 14:13:33 +0100160 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000161 req.nlh.nlmsg_pid = 0;
162 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100163 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000164
165 /* Don't block in recvfrom if send fails */
166 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
167 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
Simon Kelley0a852542005-03-23 20:28:59 +0000168
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100169 if (len == -1)
170 return 0;
171
172 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000173 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100174 if ((len = netlink_recv()) == -1)
Simon Kelley7622fc02009-06-04 20:32:05 +0100175 {
176 if (errno == ENOBUFS)
177 {
178 sleep(1);
179 goto again;
180 }
181 return 0;
182 }
183
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100184 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
Simon Kelley7622fc02009-06-04 20:32:05 +0100185 if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100186 nl_routechange(h); /* May be multicast arriving async */
Simon Kelley7622fc02009-06-04 20:32:05 +0100187 else if (h->nlmsg_type == NLMSG_ERROR)
188 nl_err(h);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100189 else if (h->nlmsg_type == NLMSG_DONE)
Simon Kelleye44ddca2012-02-18 17:08:50 +0000190 return callback_ok;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000191 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100192 {
193 struct ifaddrmsg *ifa = NLMSG_DATA(h);
194 struct rtattr *rta = IFA_RTA(ifa);
195 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
196
Simon Kelley28866e92011-02-14 20:19:14 +0000197 if (ifa->ifa_family == family)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100198 {
Simon Kelley28866e92011-02-14 20:19:14 +0000199 if (ifa->ifa_family == AF_INET)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100200 {
Simon Kelley28866e92011-02-14 20:19:14 +0000201 struct in_addr netmask, addr, broadcast;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100202
Simon Kelley28866e92011-02-14 20:19:14 +0000203 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
204 addr.s_addr = 0;
205 broadcast.s_addr = 0;
206
207 while (RTA_OK(rta, len1))
208 {
209 if (rta->rta_type == IFA_LOCAL)
210 addr = *((struct in_addr *)(rta+1));
211 else if (rta->rta_type == IFA_BROADCAST)
212 broadcast = *((struct in_addr *)(rta+1));
213
214 rta = RTA_NEXT(rta, len1);
215 }
216
Simon Kelleye44ddca2012-02-18 17:08:50 +0000217 if (addr.s_addr && callback_ok)
Simon Kelley28866e92011-02-14 20:19:14 +0000218 if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000219 callback_ok = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100220 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100221#ifdef HAVE_IPV6
Simon Kelley28866e92011-02-14 20:19:14 +0000222 else if (ifa->ifa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100223 {
Simon Kelley28866e92011-02-14 20:19:14 +0000224 struct in6_addr *addrp = NULL;
225 while (RTA_OK(rta, len1))
226 {
227 if (rta->rta_type == IFA_ADDRESS)
228 addrp = ((struct in6_addr *)(rta+1));
229
230 rta = RTA_NEXT(rta, len1);
231 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100232
Simon Kelleye44ddca2012-02-18 17:08:50 +0000233 if (addrp && callback_ok)
Simon Kelley52b92f42012-01-22 16:05:15 +0000234 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
235 (int)(ifa->ifa_index), (int)(ifa->ifa_flags & IFA_F_TENTATIVE), parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000236 callback_ok = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000237 }
238#endif
239 }
240 }
241 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
242 {
243 struct ndmsg *neigh = NLMSG_DATA(h);
244 struct rtattr *rta = NDA_RTA(neigh);
245 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
246 size_t maclen = 0;
247 char *inaddr = NULL, *mac = NULL;
248
249 while (RTA_OK(rta, len1))
250 {
251 if (rta->rta_type == NDA_DST)
252 inaddr = (char *)(rta+1);
253 else if (rta->rta_type == NDA_LLADDR)
254 {
255 maclen = rta->rta_len - sizeof(struct rtattr);
256 mac = (char *)(rta+1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100257 }
258
Simon Kelley28866e92011-02-14 20:19:14 +0000259 rta = RTA_NEXT(rta, len1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100260 }
Simon Kelley28866e92011-02-14 20:19:14 +0000261
Simon Kelleye44ddca2012-02-18 17:08:50 +0000262 if (inaddr && mac && callback_ok)
Simon Kelley28866e92011-02-14 20:19:14 +0000263 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000264 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000265 }
266#ifdef HAVE_DHCP6
267 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
268 {
269 struct ifinfomsg *link = NLMSG_DATA(h);
270 struct rtattr *rta = IFLA_RTA(link);
271 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
272 char *mac = NULL;
273 size_t maclen = 0;
274
275 while (RTA_OK(rta, len1))
276 {
277 if (rta->rta_type == IFLA_ADDRESS)
278 {
279 maclen = rta->rta_len - sizeof(struct rtattr);
280 mac = (char *)(rta+1);
281 }
282
283 rta = RTA_NEXT(rta, len1);
284 }
285
Simon Kelleye44ddca2012-02-18 17:08:50 +0000286 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000287 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
Simon Kelleye44ddca2012-02-18 17:08:50 +0000288 callback_ok = 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000289 }
290#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000291 }
Simon Kelley0a852542005-03-23 20:28:59 +0000292}
293
Simon Kelley5aabfc72007-08-29 11:24:47 +0100294void netlink_multicast(void)
Simon Kelleycdeda282006-03-16 20:16:06 +0000295{
296 ssize_t len;
297 struct nlmsghdr *h;
Simon Kelley7622fc02009-06-04 20:32:05 +0100298 int flags;
299
300 /* don't risk blocking reading netlink messages here. */
301 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
302 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
303 return;
Simon Kelleycdeda282006-03-16 20:16:06 +0000304
Simon Kelley5aabfc72007-08-29 11:24:47 +0100305 if ((len = netlink_recv()) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000306 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100307 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
308 if (h->nlmsg_type == NLMSG_ERROR)
309 nl_err(h);
310 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100311 nl_routechange(h);
Simon Kelleycdeda282006-03-16 20:16:06 +0000312 }
Simon Kelley7622fc02009-06-04 20:32:05 +0100313
314 /* restore non-blocking status */
315 fcntl(daemon->netlinkfd, F_SETFL, flags);
Simon Kelleycdeda282006-03-16 20:16:06 +0000316}
317
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100318static void nl_err(struct nlmsghdr *h)
319{
320 struct nlmsgerr *err = NLMSG_DATA(h);
Simon Kelley7622fc02009-06-04 20:32:05 +0100321
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100322 if (err->error != 0)
Simon Kelleyf2621c72007-04-29 19:47:21 +0100323 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100324}
325
326/* We arrange to receive netlink multicast messages whenever the network route is added.
327 If this happens and we still have a DNS packet in the buffer, we re-send it.
328 This helps on DoD links, where frequently the packet which triggers dialling is
329 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
Simon Kelley7622fc02009-06-04 20:32:05 +0100330 failing. Note that we only accept these messages from the kernel (pid == 0) */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100331static void nl_routechange(struct nlmsghdr *h)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100332{
Simon Kelley7622fc02009-06-04 20:32:05 +0100333 if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100334 {
335 struct rtmsg *rtm = NLMSG_DATA(h);
Simon Kelley3927da42008-07-20 15:10:39 +0100336 int fd;
337
338 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
339 return;
340
Simon Kelley9009d742008-11-14 20:04:27 +0000341 /* Force re-reading resolv file right now, for luck. */
Simon Kelleyc52e1892010-06-07 22:01:39 +0100342 daemon->last_resolv = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100343
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000344#ifdef HAVE_DHCP6
345 /* force RAs to sync new network and pick up new interfaces. */
Simon Kelley843c96b2012-02-27 17:42:38 +0000346 if (daemon->ra_contexts)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000347 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000348 schedule_subnet_map();
349 ra_start_unsolicted(dnsmasq_time(), NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000350 /* cause lease_update_file to run after we return, in case we were called from
351 iface_enumerate and can't re-enter it now */
Simon Kelley353ae4d2012-03-19 20:07:51 +0000352 send_alarm(0, 0);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000353 }
354#endif
355
Simon Kelley9009d742008-11-14 20:04:27 +0000356 if (daemon->srv_save)
357 {
358 if (daemon->srv_save->sfd)
359 fd = daemon->srv_save->sfd->fd;
360 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
361 fd = daemon->rfd_save->fd;
362 else
363 return;
364
365 while(sendto(fd, daemon->packet, daemon->packet_len, 0,
366 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
367 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100368 }
369}
Simon Kelley9009d742008-11-14 20:04:27 +0000370
Simon Kelley0a852542005-03-23 20:28:59 +0000371#endif
372
373