blob: 7f1aae4335a8bfbd68edc62a186f643c00efa2fa [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;
139
140 struct {
141 struct nlmsghdr nlh;
142 struct rtgenmsg g;
143 } req;
144
Simon Kelley0a852542005-03-23 20:28:59 +0000145 addr.nl_family = AF_NETLINK;
146 addr.nl_pad = 0;
147 addr.nl_groups = 0;
148 addr.nl_pid = 0; /* address to kernel */
Simon Kelleyc72daea2012-01-05 21:33:27 +0000149
150 again:
151 if (family == AF_UNSPEC)
152 req.nlh.nlmsg_type = RTM_GETNEIGH;
153 else if (family == AF_LOCAL)
154 req.nlh.nlmsg_type = RTM_GETLINK;
155 else
156 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley0a852542005-03-23 20:28:59 +0000157
158 req.nlh.nlmsg_len = sizeof(req);
Simon Kelley7cebd202006-05-06 14:13:33 +0100159 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000160 req.nlh.nlmsg_pid = 0;
161 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100162 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000163
164 /* Don't block in recvfrom if send fails */
165 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
166 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
Simon Kelley0a852542005-03-23 20:28:59 +0000167
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100168 if (len == -1)
169 return 0;
170
171 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000172 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100173 if ((len = netlink_recv()) == -1)
Simon Kelley7622fc02009-06-04 20:32:05 +0100174 {
175 if (errno == ENOBUFS)
176 {
177 sleep(1);
178 goto again;
179 }
180 return 0;
181 }
182
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100183 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 +0100184 if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100185 nl_routechange(h); /* May be multicast arriving async */
Simon Kelley7622fc02009-06-04 20:32:05 +0100186 else if (h->nlmsg_type == NLMSG_ERROR)
187 nl_err(h);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100188 else if (h->nlmsg_type == NLMSG_DONE)
Simon Kelley28866e92011-02-14 20:19:14 +0000189 return 1;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000190 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100191 {
192 struct ifaddrmsg *ifa = NLMSG_DATA(h);
193 struct rtattr *rta = IFA_RTA(ifa);
194 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
195
Simon Kelley28866e92011-02-14 20:19:14 +0000196 if (ifa->ifa_family == family)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100197 {
Simon Kelley28866e92011-02-14 20:19:14 +0000198 if (ifa->ifa_family == AF_INET)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100199 {
Simon Kelley28866e92011-02-14 20:19:14 +0000200 struct in_addr netmask, addr, broadcast;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100201
Simon Kelley28866e92011-02-14 20:19:14 +0000202 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
203 addr.s_addr = 0;
204 broadcast.s_addr = 0;
205
206 while (RTA_OK(rta, len1))
207 {
208 if (rta->rta_type == IFA_LOCAL)
209 addr = *((struct in_addr *)(rta+1));
210 else if (rta->rta_type == IFA_BROADCAST)
211 broadcast = *((struct in_addr *)(rta+1));
212
213 rta = RTA_NEXT(rta, len1);
214 }
215
216 if (addr.s_addr)
217 if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
218 return 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100219 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100220#ifdef HAVE_IPV6
Simon Kelley28866e92011-02-14 20:19:14 +0000221 else if (ifa->ifa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100222 {
Simon Kelley28866e92011-02-14 20:19:14 +0000223 struct in6_addr *addrp = NULL;
224 while (RTA_OK(rta, len1))
225 {
226 if (rta->rta_type == IFA_ADDRESS)
227 addrp = ((struct in6_addr *)(rta+1));
228
229 rta = RTA_NEXT(rta, len1);
230 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100231
Simon Kelley28866e92011-02-14 20:19:14 +0000232 if (addrp)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000233 if (!((*callback)(addrp, ifa->ifa_prefixlen, ifa->ifa_index,
Simon Kelley74c95c22011-10-19 09:33:39 +0100234 ifa->ifa_index, ifa->ifa_flags & IFA_F_TENTATIVE, parm)))
Simon Kelley28866e92011-02-14 20:19:14 +0000235 return 0;
236 }
237#endif
238 }
239 }
240 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
241 {
242 struct ndmsg *neigh = NLMSG_DATA(h);
243 struct rtattr *rta = NDA_RTA(neigh);
244 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
245 size_t maclen = 0;
246 char *inaddr = NULL, *mac = NULL;
247
248 while (RTA_OK(rta, len1))
249 {
250 if (rta->rta_type == NDA_DST)
251 inaddr = (char *)(rta+1);
252 else if (rta->rta_type == NDA_LLADDR)
253 {
254 maclen = rta->rta_len - sizeof(struct rtattr);
255 mac = (char *)(rta+1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100256 }
257
Simon Kelley28866e92011-02-14 20:19:14 +0000258 rta = RTA_NEXT(rta, len1);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100259 }
Simon Kelley28866e92011-02-14 20:19:14 +0000260
261 if (inaddr && mac)
262 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
263 return 0;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000264 }
265#ifdef HAVE_DHCP6
266 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
267 {
268 struct ifinfomsg *link = NLMSG_DATA(h);
269 struct rtattr *rta = IFLA_RTA(link);
270 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
271 char *mac = NULL;
272 size_t maclen = 0;
273
274 while (RTA_OK(rta, len1))
275 {
276 if (rta->rta_type == IFLA_ADDRESS)
277 {
278 maclen = rta->rta_len - sizeof(struct rtattr);
279 mac = (char *)(rta+1);
280 }
281
282 rta = RTA_NEXT(rta, len1);
283 }
284
285 if (mac && !((*callback)(link->ifi_type, link->ifi_flags, mac, maclen, parm)))
286 return 0;
287 }
288#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000289 }
Simon Kelley0a852542005-03-23 20:28:59 +0000290}
291
Simon Kelley5aabfc72007-08-29 11:24:47 +0100292void netlink_multicast(void)
Simon Kelleycdeda282006-03-16 20:16:06 +0000293{
294 ssize_t len;
295 struct nlmsghdr *h;
Simon Kelley7622fc02009-06-04 20:32:05 +0100296 int flags;
297
298 /* don't risk blocking reading netlink messages here. */
299 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
300 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
301 return;
Simon Kelleycdeda282006-03-16 20:16:06 +0000302
Simon Kelley5aabfc72007-08-29 11:24:47 +0100303 if ((len = netlink_recv()) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000304 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100305 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
306 if (h->nlmsg_type == NLMSG_ERROR)
307 nl_err(h);
308 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100309 nl_routechange(h);
Simon Kelleycdeda282006-03-16 20:16:06 +0000310 }
Simon Kelley7622fc02009-06-04 20:32:05 +0100311
312 /* restore non-blocking status */
313 fcntl(daemon->netlinkfd, F_SETFL, flags);
Simon Kelleycdeda282006-03-16 20:16:06 +0000314}
315
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100316static void nl_err(struct nlmsghdr *h)
317{
318 struct nlmsgerr *err = NLMSG_DATA(h);
Simon Kelley7622fc02009-06-04 20:32:05 +0100319
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100320 if (err->error != 0)
Simon Kelleyf2621c72007-04-29 19:47:21 +0100321 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100322}
323
324/* We arrange to receive netlink multicast messages whenever the network route is added.
325 If this happens and we still have a DNS packet in the buffer, we re-send it.
326 This helps on DoD links, where frequently the packet which triggers dialling is
327 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
Simon Kelley7622fc02009-06-04 20:32:05 +0100328 failing. Note that we only accept these messages from the kernel (pid == 0) */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100329static void nl_routechange(struct nlmsghdr *h)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100330{
Simon Kelley7622fc02009-06-04 20:32:05 +0100331 if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100332 {
333 struct rtmsg *rtm = NLMSG_DATA(h);
Simon Kelley3927da42008-07-20 15:10:39 +0100334 int fd;
335
336 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
337 return;
338
Simon Kelley9009d742008-11-14 20:04:27 +0000339 /* Force re-reading resolv file right now, for luck. */
Simon Kelleyc52e1892010-06-07 22:01:39 +0100340 daemon->last_resolv = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100341
Simon Kelley9009d742008-11-14 20:04:27 +0000342 if (daemon->srv_save)
343 {
344 if (daemon->srv_save->sfd)
345 fd = daemon->srv_save->sfd->fd;
346 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
347 fd = daemon->rfd_save->fd;
348 else
349 return;
350
351 while(sendto(fd, daemon->packet, daemon->packet_len, 0,
352 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
353 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100354 }
355}
Simon Kelley9009d742008-11-14 20:04:27 +0000356
Simon Kelley0a852542005-03-23 20:28:59 +0000357#endif
358
359