1 /* dnsmasq is Copyright (c) 2000-2011 Simon Kelley
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.
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.
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/>.
19 #ifdef HAVE_LINUX_NETWORK
21 #include <linux/types.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
25 /* linux 2.6.19 buggers up the headers, patch it up here. */
28 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
30 # include <linux/if_addr.h>
34 # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
37 static struct iovec iov;
38 static u32 netlink_pid;
40 static void nl_err(struct nlmsghdr *h);
41 static void nl_routechange(struct nlmsghdr *h);
43 void netlink_init(void)
45 struct sockaddr_nl addr;
46 socklen_t slen = sizeof(addr);
48 addr.nl_family = AF_NETLINK;
50 addr.nl_pid = 0; /* autobind */
52 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
54 addr.nl_groups = RTMGRP_IPV4_ROUTE;
57 /* May not be able to have permission to set multicast groups don't die in that case */
58 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
60 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
63 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
64 daemon->netlinkfd = -1;
68 if (daemon->netlinkfd == -1 ||
69 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1)
70 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
72 /* save pid assigned by bind() and retrieved by getsockname() */
73 netlink_pid = addr.nl_pid;
76 iov.iov_base = safe_malloc(iov.iov_len);
79 static ssize_t netlink_recv(void)
82 struct sockaddr_nl nladdr;
87 msg.msg_control = NULL;
88 msg.msg_controllen = 0;
89 msg.msg_name = &nladdr;
90 msg.msg_namelen = sizeof(nladdr);
95 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
97 /* make buffer big enough */
98 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
100 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
101 if ((size_t)rc == iov.iov_len)
103 if (expand_buf(&iov, rc + 100))
107 expand_buf(&iov, rc);
110 /* read it for real */
112 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
114 /* Make sure this is from the kernel */
115 if (rc == -1 || nladdr.nl_pid == 0)
119 /* discard stuff which is truncated at this point (expand_buf() may fail) */
120 if (msg.msg_flags & MSG_TRUNC)
130 /* family = AF_UNSPEC finds ARP table entries. */
131 int iface_enumerate(int family, void *parm, int (*callback)())
133 struct sockaddr_nl addr;
136 static unsigned int seq = 0;
143 addr.nl_family = AF_NETLINK;
146 addr.nl_pid = 0; /* address to kernel */
149 req.nlh.nlmsg_len = sizeof(req);
150 req.nlh.nlmsg_type = family == AF_UNSPEC ? RTM_GETNEIGH : RTM_GETADDR;
151 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
152 req.nlh.nlmsg_pid = 0;
153 req.nlh.nlmsg_seq = ++seq;
154 req.g.rtgen_family = family;
156 /* Don't block in recvfrom if send fails */
157 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
158 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
165 if ((len = netlink_recv()) == -1)
167 if (errno == ENOBUFS)
175 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
176 if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
177 nl_routechange(h); /* May be multicast arriving async */
178 else if (h->nlmsg_type == NLMSG_ERROR)
180 else if (h->nlmsg_type == NLMSG_DONE)
182 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC)
184 struct ifaddrmsg *ifa = NLMSG_DATA(h);
185 struct rtattr *rta = IFA_RTA(ifa);
186 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
188 if (ifa->ifa_family == family)
190 if (ifa->ifa_family == AF_INET)
192 struct in_addr netmask, addr, broadcast;
194 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
196 broadcast.s_addr = 0;
198 while (RTA_OK(rta, len1))
200 if (rta->rta_type == IFA_LOCAL)
201 addr = *((struct in_addr *)(rta+1));
202 else if (rta->rta_type == IFA_BROADCAST)
203 broadcast = *((struct in_addr *)(rta+1));
205 rta = RTA_NEXT(rta, len1);
209 if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
213 else if (ifa->ifa_family == AF_INET6)
215 struct in6_addr *addrp = NULL;
216 while (RTA_OK(rta, len1))
218 if (rta->rta_type == IFA_ADDRESS)
219 addrp = ((struct in6_addr *)(rta+1));
221 rta = RTA_NEXT(rta, len1);
225 if (!((*callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm)))
231 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
233 struct ndmsg *neigh = NLMSG_DATA(h);
234 struct rtattr *rta = NDA_RTA(neigh);
235 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
237 char *inaddr = NULL, *mac = NULL;
239 while (RTA_OK(rta, len1))
241 if (rta->rta_type == NDA_DST)
242 inaddr = (char *)(rta+1);
243 else if (rta->rta_type == NDA_LLADDR)
245 maclen = rta->rta_len - sizeof(struct rtattr);
246 mac = (char *)(rta+1);
249 rta = RTA_NEXT(rta, len1);
253 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
259 void netlink_multicast(void)
265 /* don't risk blocking reading netlink messages here. */
266 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
267 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
270 if ((len = netlink_recv()) != -1)
272 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
273 if (h->nlmsg_type == NLMSG_ERROR)
279 /* restore non-blocking status */
280 fcntl(daemon->netlinkfd, F_SETFL, flags);
283 static void nl_err(struct nlmsghdr *h)
285 struct nlmsgerr *err = NLMSG_DATA(h);
288 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
291 /* We arrange to receive netlink multicast messages whenever the network route is added.
292 If this happens and we still have a DNS packet in the buffer, we re-send it.
293 This helps on DoD links, where frequently the packet which triggers dialling is
294 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
295 failing. Note that we only accept these messages from the kernel (pid == 0) */
296 static void nl_routechange(struct nlmsghdr *h)
298 if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
300 struct rtmsg *rtm = NLMSG_DATA(h);
303 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
306 /* Force re-reading resolv file right now, for luck. */
307 daemon->last_resolv = 0;
309 if (daemon->srv_save)
311 if (daemon->srv_save->sfd)
312 fd = daemon->srv_save->sfd->fd;
313 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
314 fd = daemon->rfd_save->fd;
318 while(sendto(fd, daemon->packet, daemon->packet_len, 0,
319 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());