1/* dnsmasq is Copyright (c) 2000-2009 Simon Kelley
2
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
5   the Free Software Foundation; version 2 dated June, 1991, or
6   (at your option) version 3 dated 29 June, 2007.
7
8   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.
12
13   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/>.
15*/
16
17#include "dnsmasq.h"
18
19#ifdef HAVE_LINUX_NETWORK
20
21#include <linux/types.h>
22#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24
25/* 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
33static struct iovec iov;
34static u32 netlink_pid;
35
36static void nl_err(struct nlmsghdr *h);
37static void nl_routechange(struct nlmsghdr *h);
38
39void netlink_init(void)
40{
41  struct sockaddr_nl addr;
42  socklen_t slen = sizeof(addr);
43
44  addr.nl_family = AF_NETLINK;
45  addr.nl_pad = 0;
46  addr.nl_pid = 0; /* autobind */
47#ifdef HAVE_IPV6
48  addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
49#else
50  addr.nl_groups = RTMGRP_IPV4_ROUTE;
51#endif
52
53  /* May not be able to have permission to set multicast groups don't die in that case */
54  if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
55    {
56      if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
57	{
58	  addr.nl_groups = 0;
59	  if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
60	    daemon->netlinkfd = -1;
61	}
62    }
63
64  if (daemon->netlinkfd == -1 ||
65      getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1)
66    die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
67
68  /* save pid assigned by bind() and retrieved by getsockname() */
69  netlink_pid = addr.nl_pid;
70
71  iov.iov_len = 100;
72  iov.iov_base = safe_malloc(iov.iov_len);
73}
74
75static ssize_t netlink_recv(void)
76{
77  struct msghdr msg;
78  struct sockaddr_nl nladdr;
79  ssize_t rc;
80
81  while (1)
82    {
83      msg.msg_control = NULL;
84      msg.msg_controllen = 0;
85      msg.msg_name = &nladdr;
86      msg.msg_namelen = sizeof(nladdr);
87      msg.msg_iov = &iov;
88      msg.msg_iovlen = 1;
89      msg.msg_flags = 0;
90
91      while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
92
93      /* make buffer big enough */
94      if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
95	{
96	  /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
97	  if ((size_t)rc == iov.iov_len)
98	    {
99	      if (expand_buf(&iov, rc + 100))
100		continue;
101	    }
102	  else
103	    expand_buf(&iov, rc);
104	}
105
106      /* read it for real */
107      msg.msg_flags = 0;
108      while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
109
110      /* Make sure this is from the kernel */
111      if (rc == -1 || nladdr.nl_pid == 0)
112	break;
113    }
114
115  /* discard stuff which is truncated at this point (expand_buf() may fail) */
116  if (msg.msg_flags & MSG_TRUNC)
117    {
118      rc = -1;
119      errno = ENOMEM;
120    }
121
122  return rc;
123}
124
125int iface_enumerate(void *parm, int (*ipv4_callback)(), int (*ipv6_callback)())
126{
127  struct sockaddr_nl addr;
128  struct nlmsghdr *h;
129  ssize_t len;
130  static unsigned int seq = 0;
131  int family = AF_INET;
132
133  struct {
134    struct nlmsghdr nlh;
135    struct rtgenmsg g;
136  } req;
137
138  addr.nl_family = AF_NETLINK;
139  addr.nl_pad = 0;
140  addr.nl_groups = 0;
141  addr.nl_pid = 0; /* address to kernel */
142
143 again:
144  req.nlh.nlmsg_len = sizeof(req);
145  req.nlh.nlmsg_type = RTM_GETADDR;
146  req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
147  req.nlh.nlmsg_pid = 0;
148  req.nlh.nlmsg_seq = ++seq;
149  req.g.rtgen_family = family;
150
151  /* Don't block in recvfrom if send fails */
152  while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
153		      (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
154
155  if (len == -1)
156    return 0;
157
158  while (1)
159    {
160      if ((len = netlink_recv()) == -1)
161	{
162	  if (errno == ENOBUFS)
163	    {
164	      sleep(1);
165	      goto again;
166	    }
167	  return 0;
168	}
169
170      for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
171	if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
172	  nl_routechange(h); /* May be multicast arriving async */
173	else if (h->nlmsg_type == NLMSG_ERROR)
174	  nl_err(h);
175	else if (h->nlmsg_type == NLMSG_DONE)
176	  {
177#ifdef HAVE_IPV6
178	    if (family == AF_INET && ipv6_callback)
179	      {
180		family = AF_INET6;
181		goto again;
182	      }
183#endif
184	    return 1;
185	  }
186	else if (h->nlmsg_type == RTM_NEWADDR)
187	  {
188	    struct ifaddrmsg *ifa = NLMSG_DATA(h);
189	    struct rtattr *rta = IFA_RTA(ifa);
190	    unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
191
192	    if (ifa->ifa_family == AF_INET)
193	      {
194		struct in_addr netmask, addr, broadcast;
195
196		netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
197		addr.s_addr = 0;
198		broadcast.s_addr = 0;
199
200		while (RTA_OK(rta, len1))
201		  {
202		    if (rta->rta_type == IFA_LOCAL)
203		      addr = *((struct in_addr *)(rta+1));
204		    else if (rta->rta_type == IFA_BROADCAST)
205		      broadcast = *((struct in_addr *)(rta+1));
206
207		    rta = RTA_NEXT(rta, len1);
208		  }
209
210		if (addr.s_addr && ipv4_callback)
211		  if (!((*ipv4_callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
212		    return 0;
213	      }
214#ifdef HAVE_IPV6
215	    else if (ifa->ifa_family == AF_INET6)
216	      {
217		struct in6_addr *addrp = NULL;
218		while (RTA_OK(rta, len1))
219		  {
220		    if (rta->rta_type == IFA_ADDRESS)
221		      addrp = ((struct in6_addr *)(rta+1));
222
223		    rta = RTA_NEXT(rta, len1);
224		  }
225
226		if (addrp && ipv6_callback)
227		  if (!((*ipv6_callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm)))
228		    return 0;
229	      }
230#endif
231	  }
232    }
233}
234
235
236void netlink_multicast(void)
237{
238  ssize_t len;
239  struct nlmsghdr *h;
240  int flags;
241
242  /* don't risk blocking reading netlink messages here. */
243  if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
244      fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
245    return;
246
247  if ((len = netlink_recv()) != -1)
248    {
249      for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
250	if (h->nlmsg_type == NLMSG_ERROR)
251	  nl_err(h);
252	else
253	  nl_routechange(h);
254    }
255
256  /* restore non-blocking status */
257  fcntl(daemon->netlinkfd, F_SETFL, flags);
258}
259
260static void nl_err(struct nlmsghdr *h)
261{
262  struct nlmsgerr *err = NLMSG_DATA(h);
263
264  if (err->error != 0)
265    my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
266}
267
268/* We arrange to receive netlink multicast messages whenever the network route is added.
269   If this happens and we still have a DNS packet in the buffer, we re-send it.
270   This helps on DoD links, where frequently the packet which triggers dialling is
271   a DNS query, which then gets lost. By re-sending, we can avoid the lookup
272   failing. Note that we only accept these messages from the kernel (pid == 0) */
273static void nl_routechange(struct nlmsghdr *h)
274{
275  if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
276    {
277      struct rtmsg *rtm = NLMSG_DATA(h);
278      int fd;
279
280      if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
281	return;
282
283      /* Force re-reading resolv file right now, for luck. */
284      daemon->last_resolv = 0;
285
286      if (daemon->srv_save)
287	{
288	  if (daemon->srv_save->sfd)
289	    fd = daemon->srv_save->sfd->fd;
290	  else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
291	    fd = daemon->rfd_save->fd;
292	  else
293	    return;
294
295	  while(sendto(fd, daemon->packet, daemon->packet_len, 0,
296		       &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
297	}
298    }
299}
300
301#endif
302
303
304