]>
Commit | Line | Data |
---|---|---|
59546085 | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
0a852541 SK |
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 | |
824af85b SK |
5 | the Free Software Foundation; version 2 dated June, 1991, or |
6 | (at your option) version 3 dated 29 June, 2007. | |
7 | ||
0a852541 SK |
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. | |
824af85b | 12 | |
73a08a24 SK |
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/>. | |
0a852541 SK |
15 | */ |
16 | ||
0a852541 SK |
17 | #include "dnsmasq.h" |
18 | ||
5e9e0efb | 19 | #ifdef HAVE_LINUX_NETWORK |
0a852541 | 20 | |
91dccd09 | 21 | #include <linux/types.h> |
0a852541 SK |
22 | #include <linux/netlink.h> |
23 | #include <linux/rtnetlink.h> | |
24 | ||
832af0ba SK |
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 | ||
28866e95 SK |
33 | #ifndef NDA_RTA |
34 | # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) | |
35 | #endif | |
36 | ||
c72daea8 | 37 | |
5e9e0efb | 38 | static struct iovec iov; |
7622fc06 | 39 | static u32 netlink_pid; |
5e9e0efb | 40 | |
54dd393f | 41 | static int nl_async(struct nlmsghdr *h); |
cdeda28f | 42 | |
5aabfc78 | 43 | void netlink_init(void) |
0a852541 SK |
44 | { |
45 | struct sockaddr_nl addr; | |
7622fc06 | 46 | socklen_t slen = sizeof(addr); |
0a852541 SK |
47 | |
48 | addr.nl_family = AF_NETLINK; | |
49 | addr.nl_pad = 0; | |
5e9e0efb | 50 | addr.nl_pid = 0; /* autobind */ |
5e9e0efb | 51 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
54dd393f SK |
52 | if (option_bool(OPT_CLEVERBIND)) |
53 | addr.nl_groups |= RTMGRP_IPV4_IFADDR; | |
54 | #ifdef HAVE_IPV6 | |
55 | addr.nl_groups |= RTMGRP_IPV6_ROUTE; | |
56 | if (daemon->ra_contexts || option_bool(OPT_CLEVERBIND)) | |
57 | addr.nl_groups |= RTMGRP_IPV6_IFADDR; | |
cdeda28f | 58 | #endif |
0a852541 | 59 | |
309331f5 SK |
60 | /* May not be able to have permission to set multicast groups don't die in that case */ |
61 | if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1) | |
62 | { | |
63 | if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
64 | { | |
65 | addr.nl_groups = 0; | |
66 | if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
67 | daemon->netlinkfd = -1; | |
68 | } | |
69 | } | |
70 | ||
7622fc06 SK |
71 | if (daemon->netlinkfd == -1 || |
72 | getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1) | |
5aabfc78 | 73 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
7622fc06 SK |
74 | |
75 | /* save pid assigned by bind() and retrieved by getsockname() */ | |
76 | netlink_pid = addr.nl_pid; | |
5aabfc78 | 77 | |
7622fc06 | 78 | iov.iov_len = 100; |
5e9e0efb | 79 | iov.iov_base = safe_malloc(iov.iov_len); |
0a852541 SK |
80 | } |
81 | ||
5aabfc78 | 82 | static ssize_t netlink_recv(void) |
cdeda28f SK |
83 | { |
84 | struct msghdr msg; | |
7622fc06 | 85 | struct sockaddr_nl nladdr; |
cdeda28f SK |
86 | ssize_t rc; |
87 | ||
5e9e0efb | 88 | while (1) |
cdeda28f | 89 | { |
7622fc06 SK |
90 | msg.msg_control = NULL; |
91 | msg.msg_controllen = 0; | |
92 | msg.msg_name = &nladdr; | |
93 | msg.msg_namelen = sizeof(nladdr); | |
94 | msg.msg_iov = &iov; | |
95 | msg.msg_iovlen = 1; | |
5e9e0efb | 96 | msg.msg_flags = 0; |
5e9e0efb | 97 | |
7622fc06 SK |
98 | while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR); |
99 | ||
100 | /* make buffer big enough */ | |
101 | if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) | |
5e9e0efb | 102 | { |
7622fc06 SK |
103 | /* Very new Linux kernels return the actual size needed, older ones always return truncated size */ |
104 | if ((size_t)rc == iov.iov_len) | |
105 | { | |
106 | if (expand_buf(&iov, rc + 100)) | |
107 | continue; | |
108 | } | |
109 | else | |
110 | expand_buf(&iov, rc); | |
5e9e0efb | 111 | } |
7622fc06 SK |
112 | |
113 | /* read it for real */ | |
114 | msg.msg_flags = 0; | |
115 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); | |
5e9e0efb | 116 | |
7622fc06 SK |
117 | /* Make sure this is from the kernel */ |
118 | if (rc == -1 || nladdr.nl_pid == 0) | |
119 | break; | |
120 | } | |
121 | ||
122 | /* discard stuff which is truncated at this point (expand_buf() may fail) */ | |
123 | if (msg.msg_flags & MSG_TRUNC) | |
124 | { | |
125 | rc = -1; | |
126 | errno = ENOMEM; | |
cdeda28f | 127 | } |
cdeda28f SK |
128 | |
129 | return rc; | |
130 | } | |
131 | ||
28866e95 | 132 | |
c72daea8 SK |
133 | /* family = AF_UNSPEC finds ARP table entries. |
134 | family = AF_LOCAL finds MAC addresses. */ | |
28866e95 | 135 | int iface_enumerate(int family, void *parm, int (*callback)()) |
0a852541 SK |
136 | { |
137 | struct sockaddr_nl addr; | |
138 | struct nlmsghdr *h; | |
cdeda28f | 139 | ssize_t len; |
0a852541 | 140 | static unsigned int seq = 0; |
54dd393f | 141 | int callback_ok = 1, newaddr = 0; |
0a852541 SK |
142 | |
143 | struct { | |
144 | struct nlmsghdr nlh; | |
145 | struct rtgenmsg g; | |
146 | } req; | |
147 | ||
0a852541 SK |
148 | addr.nl_family = AF_NETLINK; |
149 | addr.nl_pad = 0; | |
150 | addr.nl_groups = 0; | |
151 | addr.nl_pid = 0; /* address to kernel */ | |
c72daea8 SK |
152 | |
153 | again: | |
154 | if (family == AF_UNSPEC) | |
155 | req.nlh.nlmsg_type = RTM_GETNEIGH; | |
156 | else if (family == AF_LOCAL) | |
157 | req.nlh.nlmsg_type = RTM_GETLINK; | |
158 | else | |
159 | req.nlh.nlmsg_type = RTM_GETADDR; | |
0a852541 SK |
160 | |
161 | req.nlh.nlmsg_len = sizeof(req); | |
7cebd20f | 162 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
0a852541 SK |
163 | req.nlh.nlmsg_pid = 0; |
164 | req.nlh.nlmsg_seq = ++seq; | |
5e9e0efb | 165 | req.g.rtgen_family = family; |
0a852541 SK |
166 | |
167 | /* Don't block in recvfrom if send fails */ | |
168 | while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, | |
169 | (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send()); | |
5e9e0efb | 170 | |
0a852541 | 171 | if (len == -1) |
5e9e0efb SK |
172 | return 0; |
173 | ||
174 | while (1) | |
0a852541 | 175 | { |
5aabfc78 | 176 | if ((len = netlink_recv()) == -1) |
7622fc06 SK |
177 | { |
178 | if (errno == ENOBUFS) | |
179 | { | |
180 | sleep(1); | |
181 | goto again; | |
182 | } | |
183 | return 0; | |
184 | } | |
185 | ||
5e9e0efb | 186 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
54dd393f SK |
187 | if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR) |
188 | { | |
189 | /* May be multicast arriving async */ | |
af576b56 | 190 | if (nl_async(h) && option_bool(OPT_CLEVERBIND)) |
54dd393f SK |
191 | newaddr = 1; |
192 | } | |
5e9e0efb | 193 | else if (h->nlmsg_type == NLMSG_DONE) |
54dd393f SK |
194 | { |
195 | /* handle async new interface address arrivals, these have to be done | |
196 | after we complete as we're not re-entrant */ | |
197 | if (newaddr) | |
198 | { | |
199 | enumerate_interfaces(); | |
200 | create_bound_listeners(0); | |
201 | } | |
202 | ||
203 | return callback_ok; | |
204 | } | |
c72daea8 | 205 | else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL) |
5e9e0efb SK |
206 | { |
207 | struct ifaddrmsg *ifa = NLMSG_DATA(h); | |
208 | struct rtattr *rta = IFA_RTA(ifa); | |
209 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); | |
210 | ||
28866e95 | 211 | if (ifa->ifa_family == family) |
5e9e0efb | 212 | { |
28866e95 | 213 | if (ifa->ifa_family == AF_INET) |
5e9e0efb | 214 | { |
28866e95 SK |
215 | struct in_addr netmask, addr, broadcast; |
216 | ||
217 | netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen)); | |
218 | addr.s_addr = 0; | |
219 | broadcast.s_addr = 0; | |
5e9e0efb | 220 | |
28866e95 SK |
221 | while (RTA_OK(rta, len1)) |
222 | { | |
223 | if (rta->rta_type == IFA_LOCAL) | |
224 | addr = *((struct in_addr *)(rta+1)); | |
225 | else if (rta->rta_type == IFA_BROADCAST) | |
226 | broadcast = *((struct in_addr *)(rta+1)); | |
227 | ||
228 | rta = RTA_NEXT(rta, len1); | |
229 | } | |
230 | ||
e44ddcac | 231 | if (addr.s_addr && callback_ok) |
28866e95 | 232 | if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm))) |
e44ddcac | 233 | callback_ok = 0; |
5e9e0efb | 234 | } |
5e9e0efb | 235 | #ifdef HAVE_IPV6 |
28866e95 | 236 | else if (ifa->ifa_family == AF_INET6) |
5e9e0efb | 237 | { |
28866e95 SK |
238 | struct in6_addr *addrp = NULL; |
239 | while (RTA_OK(rta, len1)) | |
240 | { | |
241 | if (rta->rta_type == IFA_ADDRESS) | |
242 | addrp = ((struct in6_addr *)(rta+1)); | |
243 | ||
244 | rta = RTA_NEXT(rta, len1); | |
245 | } | |
5e9e0efb | 246 | |
e44ddcac | 247 | if (addrp && callback_ok) |
52b92f4d SK |
248 | if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope), |
249 | (int)(ifa->ifa_index), (int)(ifa->ifa_flags & IFA_F_TENTATIVE), parm))) | |
e44ddcac | 250 | callback_ok = 0; |
5e9e0efb | 251 | } |
5e9e0efb | 252 | #endif |
28866e95 | 253 | } |
5e9e0efb | 254 | } |
28866e95 SK |
255 | else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
256 | { | |
257 | struct ndmsg *neigh = NLMSG_DATA(h); | |
258 | struct rtattr *rta = NDA_RTA(neigh); | |
259 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); | |
260 | size_t maclen = 0; | |
261 | char *inaddr = NULL, *mac = NULL; | |
262 | ||
263 | while (RTA_OK(rta, len1)) | |
264 | { | |
265 | if (rta->rta_type == NDA_DST) | |
266 | inaddr = (char *)(rta+1); | |
267 | else if (rta->rta_type == NDA_LLADDR) | |
268 | { | |
269 | maclen = rta->rta_len - sizeof(struct rtattr); | |
270 | mac = (char *)(rta+1); | |
271 | } | |
272 | ||
273 | rta = RTA_NEXT(rta, len1); | |
274 | } | |
275 | ||
e44ddcac | 276 | if (inaddr && mac && callback_ok) |
28866e95 | 277 | if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm))) |
e44ddcac | 278 | callback_ok = 0; |
c72daea8 SK |
279 | } |
280 | #ifdef HAVE_DHCP6 | |
281 | else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) | |
282 | { | |
283 | struct ifinfomsg *link = NLMSG_DATA(h); | |
284 | struct rtattr *rta = IFLA_RTA(link); | |
285 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); | |
286 | char *mac = NULL; | |
287 | size_t maclen = 0; | |
288 | ||
289 | while (RTA_OK(rta, len1)) | |
290 | { | |
291 | if (rta->rta_type == IFLA_ADDRESS) | |
292 | { | |
293 | maclen = rta->rta_len - sizeof(struct rtattr); | |
294 | mac = (char *)(rta+1); | |
295 | } | |
296 | ||
297 | rta = RTA_NEXT(rta, len1); | |
298 | } | |
299 | ||
e44ddcac | 300 | if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) && |
c5ad4e79 | 301 | !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm))) |
e44ddcac | 302 | callback_ok = 0; |
c72daea8 SK |
303 | } |
304 | #endif | |
0a852541 | 305 | } |
5e9e0efb | 306 | } |
0a852541 | 307 | |
5aabfc78 | 308 | void netlink_multicast(void) |
5e9e0efb SK |
309 | { |
310 | ssize_t len; | |
311 | struct nlmsghdr *h; | |
54dd393f | 312 | int flags, newaddr = 0; |
7622fc06 SK |
313 | |
314 | /* don't risk blocking reading netlink messages here. */ | |
315 | if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 || | |
316 | fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1) | |
317 | return; | |
0a852541 | 318 | |
5aabfc78 | 319 | if ((len = netlink_recv()) != -1) |
54dd393f | 320 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
af576b56 | 321 | if (nl_async(h) && option_bool(OPT_CLEVERBIND)) |
54dd393f SK |
322 | newaddr = 1; |
323 | ||
324 | /* restore non-blocking status */ | |
325 | fcntl(daemon->netlinkfd, F_SETFL, flags); | |
326 | ||
327 | if (newaddr) | |
0a852541 | 328 | { |
54dd393f SK |
329 | enumerate_interfaces(); |
330 | create_bound_listeners(0); | |
0a852541 | 331 | } |
0a852541 SK |
332 | } |
333 | ||
54dd393f | 334 | static int nl_async(struct nlmsghdr *h) |
5e9e0efb | 335 | { |
54dd393f SK |
336 | if (h->nlmsg_type == NLMSG_ERROR) |
337 | { | |
338 | struct nlmsgerr *err = NLMSG_DATA(h); | |
dfb23b3f SK |
339 | if (err->error != 0) |
340 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); | |
54dd393f SK |
341 | return 0; |
342 | } | |
343 | else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) | |
344 | { | |
e17b4b38 SK |
345 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
346 | If this happens and we still have a DNS packet in the buffer, we re-send it. | |
347 | This helps on DoD links, where frequently the packet which triggers dialling is | |
348 | a DNS query, which then gets lost. By re-sending, we can avoid the lookup | |
349 | failing. */ | |
350 | struct rtmsg *rtm = NLMSG_DATA(h); | |
351 | ||
352 | if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK) | |
353 | { | |
354 | /* Force re-reading resolv file right now, for luck. */ | |
355 | daemon->last_resolv = 0; | |
356 | ||
357 | if (daemon->srv_save) | |
358 | { | |
359 | int fd; | |
360 | ||
361 | if (daemon->srv_save->sfd) | |
362 | fd = daemon->srv_save->sfd->fd; | |
363 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) | |
364 | fd = daemon->rfd_save->fd; | |
365 | else | |
366 | return 0; | |
367 | ||
368 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, | |
369 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); | |
370 | } | |
371 | } | |
54dd393f SK |
372 | return 0; |
373 | } | |
e17b4b38 | 374 | #ifdef HAVE_DHCP6 |
54dd393f | 375 | else if (h->nlmsg_type == RTM_NEWADDR) |
e17b4b38 SK |
376 | { |
377 | /* force RAs to sync new network and pick up new interfaces. */ | |
378 | if (daemon->ra_contexts) | |
54dd393f | 379 | { |
e17b4b38 SK |
380 | schedule_subnet_map(); |
381 | ra_start_unsolicted(dnsmasq_time(), NULL); | |
382 | /* cause lease_update_file to run after we return, in case we were called from | |
383 | iface_enumerate and can't re-enter it now */ | |
384 | send_alarm(0, 0); | |
54dd393f | 385 | } |
e17b4b38 SK |
386 | return 1; /* clever bind mode - rescan */ |
387 | } | |
54dd393f | 388 | #endif |
e17b4b38 | 389 | |
54dd393f SK |
390 | return 0; |
391 | } | |
392 | ||
0a852541 SK |
393 | #endif |
394 | ||
395 |