blob: d205f557dedf6a13bd484d14eacd1345d6906381 [file] [log] [blame]
Simon Kelley824af852008-02-12 20:43:05 +00001/* dnsmasq is Copyright (c) 2000-2007 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
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/>.
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 Kelley5e9e0ef2006-04-17 14:24:29 +010033static struct iovec iov;
34
35static void nl_err(struct nlmsghdr *h);
Simon Kelley5aabfc72007-08-29 11:24:47 +010036static void nl_routechange(struct nlmsghdr *h);
Simon Kelleycdeda282006-03-16 20:16:06 +000037
Simon Kelley5aabfc72007-08-29 11:24:47 +010038void netlink_init(void)
Simon Kelley0a852542005-03-23 20:28:59 +000039{
40 struct sockaddr_nl addr;
Simon Kelley0a852542005-03-23 20:28:59 +000041
42 addr.nl_family = AF_NETLINK;
43 addr.nl_pad = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010044 addr.nl_pid = 0; /* autobind */
Simon Kelleycdeda282006-03-16 20:16:06 +000045#ifdef HAVE_IPV6
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010046 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000047#else
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010048 addr.nl_groups = RTMGRP_IPV4_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000049#endif
Simon Kelley0a852542005-03-23 20:28:59 +000050
Simon Kelley309331f2006-04-22 15:05:01 +010051 /* May not be able to have permission to set multicast groups don't die in that case */
52 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
53 {
54 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
55 {
56 addr.nl_groups = 0;
57 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
58 daemon->netlinkfd = -1;
59 }
60 }
61
62 if (daemon->netlinkfd == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +010063 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
64
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010065 iov.iov_len = 200;
66 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley0a852542005-03-23 20:28:59 +000067}
68
Simon Kelley5aabfc72007-08-29 11:24:47 +010069static ssize_t netlink_recv(void)
Simon Kelleycdeda282006-03-16 20:16:06 +000070{
71 struct msghdr msg;
72 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +000073
Simon Kelleycdeda282006-03-16 20:16:06 +000074 msg.msg_control = NULL;
75 msg.msg_controllen = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +000076 msg.msg_name = NULL;
77 msg.msg_namelen = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010078 msg.msg_iov = &iov;
Simon Kelleycdeda282006-03-16 20:16:06 +000079 msg.msg_iovlen = 1;
80
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010081 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +000082 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010083 msg.msg_flags = 0;
84 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK)) == -1 && errno == EINTR);
85
86 /* 2.2.x doesn't suport MSG_PEEK at all, returning EOPNOTSUPP, so we just grab a
87 big buffer and pray in that case. */
88 if (rc == -1 && errno == EOPNOTSUPP)
89 {
90 if (!expand_buf(&iov, 2000))
91 return -1;
92 break;
93 }
94
95 if (rc == -1 || !(msg.msg_flags & MSG_TRUNC))
96 break;
97
98 if (!expand_buf(&iov, iov.iov_len + 100))
Simon Kelleycdeda282006-03-16 20:16:06 +000099 return -1;
Simon Kelleycdeda282006-03-16 20:16:06 +0000100 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100101
102 /* finally, read it for real */
103 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
Simon Kelleycdeda282006-03-16 20:16:06 +0000104
105 return rc;
106}
107
Simon Kelley5aabfc72007-08-29 11:24:47 +0100108int iface_enumerate(void *parm, int (*ipv4_callback)(), int (*ipv6_callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000109{
110 struct sockaddr_nl addr;
111 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000112 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000113 static unsigned int seq = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100114 int family = AF_INET;
Simon Kelley0a852542005-03-23 20:28:59 +0000115
116 struct {
117 struct nlmsghdr nlh;
118 struct rtgenmsg g;
119 } req;
120
Simon Kelley0a852542005-03-23 20:28:59 +0000121 addr.nl_family = AF_NETLINK;
122 addr.nl_pad = 0;
123 addr.nl_groups = 0;
124 addr.nl_pid = 0; /* address to kernel */
125
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100126 again:
Simon Kelley0a852542005-03-23 20:28:59 +0000127 req.nlh.nlmsg_len = sizeof(req);
128 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley7cebd202006-05-06 14:13:33 +0100129 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000130 req.nlh.nlmsg_pid = 0;
131 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100132 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000133
134 /* Don't block in recvfrom if send fails */
135 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
136 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
Simon Kelley0a852542005-03-23 20:28:59 +0000137
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100138 if (len == -1)
139 return 0;
140
141 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000142 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100143 if ((len = netlink_recv()) == -1)
Simon Kelley0a852542005-03-23 20:28:59 +0000144 return 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100145
146 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
147 if (h->nlmsg_type == NLMSG_ERROR)
148 nl_err(h);
149 else if (h->nlmsg_seq != seq)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100150 nl_routechange(h); /* May be multicast arriving async */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100151 else if (h->nlmsg_type == NLMSG_DONE)
152 {
153#ifdef HAVE_IPV6
154 if (family == AF_INET && ipv6_callback)
155 {
156 family = AF_INET6;
157 goto again;
158 }
159#endif
160 return 1;
161 }
162 else if (h->nlmsg_type == RTM_NEWADDR)
163 {
164 struct ifaddrmsg *ifa = NLMSG_DATA(h);
165 struct rtattr *rta = IFA_RTA(ifa);
166 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
167
168 if (ifa->ifa_family == AF_INET)
169 {
170 struct in_addr netmask, addr, broadcast;
171
172 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
173 addr.s_addr = 0;
174 broadcast.s_addr = 0;
175
176 while (RTA_OK(rta, len1))
177 {
178 if (rta->rta_type == IFA_LOCAL)
179 addr = *((struct in_addr *)(rta+1));
180 else if (rta->rta_type == IFA_BROADCAST)
181 broadcast = *((struct in_addr *)(rta+1));
182
183 rta = RTA_NEXT(rta, len1);
184 }
185
186 if (addr.s_addr && ipv4_callback)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100187 if (!((*ipv4_callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100188 return 0;
189 }
190#ifdef HAVE_IPV6
191 else if (ifa->ifa_family == AF_INET6)
192 {
193 struct in6_addr *addrp = NULL;
194 while (RTA_OK(rta, len1))
195 {
196 if (rta->rta_type == IFA_ADDRESS)
197 addrp = ((struct in6_addr *)(rta+1));
198
199 rta = RTA_NEXT(rta, len1);
200 }
201
202 if (addrp && ipv6_callback)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100203 if (!((*ipv6_callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm)))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100204 return 0;
205 }
206#endif
207 }
Simon Kelley0a852542005-03-23 20:28:59 +0000208 }
Simon Kelley0a852542005-03-23 20:28:59 +0000209}
210
Simon Kelley5aabfc72007-08-29 11:24:47 +0100211void netlink_multicast(void)
Simon Kelleycdeda282006-03-16 20:16:06 +0000212{
213 ssize_t len;
214 struct nlmsghdr *h;
215
Simon Kelley5aabfc72007-08-29 11:24:47 +0100216 if ((len = netlink_recv()) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000217 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100218 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
219 if (h->nlmsg_type == NLMSG_ERROR)
220 nl_err(h);
221 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100222 nl_routechange(h);
Simon Kelleycdeda282006-03-16 20:16:06 +0000223 }
224}
225
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100226static void nl_err(struct nlmsghdr *h)
227{
228 struct nlmsgerr *err = NLMSG_DATA(h);
229 if (err->error != 0)
Simon Kelleyf2621c72007-04-29 19:47:21 +0100230 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100231}
232
233/* We arrange to receive netlink multicast messages whenever the network route is added.
234 If this happens and we still have a DNS packet in the buffer, we re-send it.
235 This helps on DoD links, where frequently the packet which triggers dialling is
236 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
237 failing. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100238static void nl_routechange(struct nlmsghdr *h)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100239{
240 if (h->nlmsg_type == RTM_NEWROUTE && daemon->srv_save)
241 {
242 struct rtmsg *rtm = NLMSG_DATA(h);
Simon Kelley3927da42008-07-20 15:10:39 +0100243 int fd;
244
245 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
246 return;
247
248 if (daemon->srv_save->sfd)
249 fd = daemon->srv_save->sfd->fd;
250 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
251 fd = daemon->rfd_save->fd;
252 else
253 return;
254
255 while(sendto(fd, daemon->packet, daemon->packet_len, 0,
256 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100257 }
258}
Simon Kelley0a852542005-03-23 20:28:59 +0000259#endif
260
261