]>
Commit | Line | Data |
---|---|---|
aff33962 | 1 | /* dnsmasq is Copyright (c) 2000-2015 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 | |
a0358e5d | 41 | static void 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 | 52 | if (option_bool(OPT_CLEVERBIND)) |
1f776932 | 53 | addr.nl_groups |= RTMGRP_IPV4_IFADDR; |
54dd393f SK |
54 | #ifdef HAVE_IPV6 |
55 | addr.nl_groups |= RTMGRP_IPV6_ROUTE; | |
1f776932 SK |
56 | if (option_bool(OPT_CLEVERBIND)) |
57 | addr.nl_groups |= RTMGRP_IPV6_IFADDR; | |
58 | #endif | |
59 | #ifdef HAVE_DHCP6 | |
60 | if (daemon->doing_ra || daemon->doing_dhcp6) | |
54dd393f | 61 | addr.nl_groups |= RTMGRP_IPV6_IFADDR; |
cdeda28f | 62 | #endif |
0a852541 | 63 | |
309331f5 SK |
64 | /* May not be able to have permission to set multicast groups don't die in that case */ |
65 | if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1) | |
66 | { | |
67 | if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
68 | { | |
69 | addr.nl_groups = 0; | |
70 | if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
71 | daemon->netlinkfd = -1; | |
72 | } | |
73 | } | |
74 | ||
7622fc06 SK |
75 | if (daemon->netlinkfd == -1 || |
76 | getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1) | |
5aabfc78 | 77 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
7622fc06 SK |
78 | |
79 | /* save pid assigned by bind() and retrieved by getsockname() */ | |
80 | netlink_pid = addr.nl_pid; | |
5aabfc78 | 81 | |
7622fc06 | 82 | iov.iov_len = 100; |
5e9e0efb | 83 | iov.iov_base = safe_malloc(iov.iov_len); |
0a852541 SK |
84 | } |
85 | ||
5aabfc78 | 86 | static ssize_t netlink_recv(void) |
cdeda28f SK |
87 | { |
88 | struct msghdr msg; | |
7622fc06 | 89 | struct sockaddr_nl nladdr; |
cdeda28f SK |
90 | ssize_t rc; |
91 | ||
5e9e0efb | 92 | while (1) |
cdeda28f | 93 | { |
7622fc06 SK |
94 | msg.msg_control = NULL; |
95 | msg.msg_controllen = 0; | |
96 | msg.msg_name = &nladdr; | |
97 | msg.msg_namelen = sizeof(nladdr); | |
98 | msg.msg_iov = &iov; | |
99 | msg.msg_iovlen = 1; | |
5e9e0efb | 100 | msg.msg_flags = 0; |
5e9e0efb | 101 | |
7622fc06 SK |
102 | while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR); |
103 | ||
104 | /* make buffer big enough */ | |
105 | if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) | |
5e9e0efb | 106 | { |
7622fc06 SK |
107 | /* Very new Linux kernels return the actual size needed, older ones always return truncated size */ |
108 | if ((size_t)rc == iov.iov_len) | |
109 | { | |
110 | if (expand_buf(&iov, rc + 100)) | |
111 | continue; | |
112 | } | |
113 | else | |
114 | expand_buf(&iov, rc); | |
5e9e0efb | 115 | } |
7622fc06 SK |
116 | |
117 | /* read it for real */ | |
118 | msg.msg_flags = 0; | |
119 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); | |
5e9e0efb | 120 | |
7622fc06 SK |
121 | /* Make sure this is from the kernel */ |
122 | if (rc == -1 || nladdr.nl_pid == 0) | |
123 | break; | |
124 | } | |
125 | ||
126 | /* discard stuff which is truncated at this point (expand_buf() may fail) */ | |
127 | if (msg.msg_flags & MSG_TRUNC) | |
128 | { | |
129 | rc = -1; | |
130 | errno = ENOMEM; | |
cdeda28f | 131 | } |
cdeda28f SK |
132 | |
133 | return rc; | |
134 | } | |
135 | ||
28866e95 | 136 | |
c72daea8 SK |
137 | /* family = AF_UNSPEC finds ARP table entries. |
138 | family = AF_LOCAL finds MAC addresses. */ | |
28866e95 | 139 | int iface_enumerate(int family, void *parm, int (*callback)()) |
0a852541 SK |
140 | { |
141 | struct sockaddr_nl addr; | |
142 | struct nlmsghdr *h; | |
cdeda28f | 143 | ssize_t len; |
0a852541 | 144 | static unsigned int seq = 0; |
a0358e5d | 145 | int callback_ok = 1; |
0a852541 SK |
146 | |
147 | struct { | |
148 | struct nlmsghdr nlh; | |
149 | struct rtgenmsg g; | |
150 | } req; | |
151 | ||
0a852541 SK |
152 | addr.nl_family = AF_NETLINK; |
153 | addr.nl_pad = 0; | |
154 | addr.nl_groups = 0; | |
155 | addr.nl_pid = 0; /* address to kernel */ | |
c72daea8 SK |
156 | |
157 | again: | |
158 | if (family == AF_UNSPEC) | |
159 | req.nlh.nlmsg_type = RTM_GETNEIGH; | |
160 | else if (family == AF_LOCAL) | |
161 | req.nlh.nlmsg_type = RTM_GETLINK; | |
162 | else | |
163 | req.nlh.nlmsg_type = RTM_GETADDR; | |
0a852541 SK |
164 | |
165 | req.nlh.nlmsg_len = sizeof(req); | |
7cebd20f | 166 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
0a852541 SK |
167 | req.nlh.nlmsg_pid = 0; |
168 | req.nlh.nlmsg_seq = ++seq; | |
5e9e0efb | 169 | req.g.rtgen_family = family; |
0a852541 SK |
170 | |
171 | /* Don't block in recvfrom if send fails */ | |
ff841ebf SK |
172 | while(retry_send(sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, |
173 | (struct sockaddr *)&addr, sizeof(addr)))); | |
174 | ||
175 | if (errno != 0) | |
5e9e0efb SK |
176 | return 0; |
177 | ||
178 | while (1) | |
0a852541 | 179 | { |
5aabfc78 | 180 | if ((len = netlink_recv()) == -1) |
7622fc06 SK |
181 | { |
182 | if (errno == ENOBUFS) | |
183 | { | |
184 | sleep(1); | |
185 | goto again; | |
186 | } | |
187 | return 0; | |
188 | } | |
189 | ||
5e9e0efb | 190 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
54dd393f SK |
191 | if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid || h->nlmsg_type == NLMSG_ERROR) |
192 | { | |
193 | /* May be multicast arriving async */ | |
a0358e5d | 194 | nl_async(h); |
54dd393f | 195 | } |
5e9e0efb | 196 | else if (h->nlmsg_type == NLMSG_DONE) |
a0358e5d | 197 | return callback_ok; |
c72daea8 | 198 | else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL) |
5e9e0efb SK |
199 | { |
200 | struct ifaddrmsg *ifa = NLMSG_DATA(h); | |
201 | struct rtattr *rta = IFA_RTA(ifa); | |
202 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); | |
203 | ||
28866e95 | 204 | if (ifa->ifa_family == family) |
5e9e0efb | 205 | { |
28866e95 | 206 | if (ifa->ifa_family == AF_INET) |
5e9e0efb | 207 | { |
28866e95 | 208 | struct in_addr netmask, addr, broadcast; |
3f2873d4 SK |
209 | char *label = NULL; |
210 | ||
10cfc0dd RG |
211 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - ifa->ifa_prefixlen)); |
212 | ||
28866e95 SK |
213 | addr.s_addr = 0; |
214 | broadcast.s_addr = 0; | |
5e9e0efb | 215 | |
28866e95 SK |
216 | while (RTA_OK(rta, len1)) |
217 | { | |
218 | if (rta->rta_type == IFA_LOCAL) | |
219 | addr = *((struct in_addr *)(rta+1)); | |
220 | else if (rta->rta_type == IFA_BROADCAST) | |
221 | broadcast = *((struct in_addr *)(rta+1)); | |
3f2873d4 SK |
222 | else if (rta->rta_type == IFA_LABEL) |
223 | label = RTA_DATA(rta); | |
28866e95 SK |
224 | |
225 | rta = RTA_NEXT(rta, len1); | |
226 | } | |
227 | ||
e44ddcac | 228 | if (addr.s_addr && callback_ok) |
3f2873d4 | 229 | if (!((*callback)(addr, ifa->ifa_index, label, netmask, broadcast, parm))) |
e44ddcac | 230 | callback_ok = 0; |
5e9e0efb | 231 | } |
5e9e0efb | 232 | #ifdef HAVE_IPV6 |
28866e95 | 233 | else if (ifa->ifa_family == AF_INET6) |
5e9e0efb | 234 | { |
28866e95 | 235 | struct in6_addr *addrp = NULL; |
1f776932 | 236 | u32 valid = 0, preferred = 0; |
bad7b875 SK |
237 | int flags = 0; |
238 | ||
28866e95 SK |
239 | while (RTA_OK(rta, len1)) |
240 | { | |
241 | if (rta->rta_type == IFA_ADDRESS) | |
242 | addrp = ((struct in6_addr *)(rta+1)); | |
1f776932 SK |
243 | else if (rta->rta_type == IFA_CACHEINFO) |
244 | { | |
245 | struct ifa_cacheinfo *ifc = (struct ifa_cacheinfo *)(rta+1); | |
246 | preferred = ifc->ifa_prefered; | |
247 | valid = ifc->ifa_valid; | |
248 | } | |
28866e95 SK |
249 | rta = RTA_NEXT(rta, len1); |
250 | } | |
5e9e0efb | 251 | |
bad7b875 SK |
252 | if (ifa->ifa_flags & IFA_F_TENTATIVE) |
253 | flags |= IFACE_TENTATIVE; | |
254 | ||
255 | if (ifa->ifa_flags & IFA_F_DEPRECATED) | |
256 | flags |= IFACE_DEPRECATED; | |
257 | ||
57ab36e7 JG |
258 | if (!(ifa->ifa_flags & IFA_F_TEMPORARY)) |
259 | flags |= IFACE_PERMANENT; | |
260 | ||
e44ddcac | 261 | if (addrp && callback_ok) |
52b92f4d | 262 | if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope), |
bad7b875 | 263 | (int)(ifa->ifa_index), flags, |
1f776932 | 264 | (int) preferred, (int)valid, parm))) |
e44ddcac | 265 | callback_ok = 0; |
5e9e0efb | 266 | } |
5e9e0efb | 267 | #endif |
28866e95 | 268 | } |
5e9e0efb | 269 | } |
28866e95 SK |
270 | else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC) |
271 | { | |
272 | struct ndmsg *neigh = NLMSG_DATA(h); | |
273 | struct rtattr *rta = NDA_RTA(neigh); | |
274 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh)); | |
275 | size_t maclen = 0; | |
276 | char *inaddr = NULL, *mac = NULL; | |
277 | ||
278 | while (RTA_OK(rta, len1)) | |
279 | { | |
280 | if (rta->rta_type == NDA_DST) | |
281 | inaddr = (char *)(rta+1); | |
282 | else if (rta->rta_type == NDA_LLADDR) | |
283 | { | |
284 | maclen = rta->rta_len - sizeof(struct rtattr); | |
285 | mac = (char *)(rta+1); | |
286 | } | |
287 | ||
288 | rta = RTA_NEXT(rta, len1); | |
289 | } | |
290 | ||
e44ddcac | 291 | if (inaddr && mac && callback_ok) |
28866e95 | 292 | if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm))) |
e44ddcac | 293 | callback_ok = 0; |
c72daea8 SK |
294 | } |
295 | #ifdef HAVE_DHCP6 | |
296 | else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL) | |
297 | { | |
298 | struct ifinfomsg *link = NLMSG_DATA(h); | |
299 | struct rtattr *rta = IFLA_RTA(link); | |
300 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link)); | |
301 | char *mac = NULL; | |
302 | size_t maclen = 0; | |
303 | ||
304 | while (RTA_OK(rta, len1)) | |
305 | { | |
306 | if (rta->rta_type == IFLA_ADDRESS) | |
307 | { | |
308 | maclen = rta->rta_len - sizeof(struct rtattr); | |
309 | mac = (char *)(rta+1); | |
310 | } | |
311 | ||
312 | rta = RTA_NEXT(rta, len1); | |
313 | } | |
314 | ||
e44ddcac | 315 | if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) && |
c5ad4e79 | 316 | !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm))) |
e44ddcac | 317 | callback_ok = 0; |
c72daea8 SK |
318 | } |
319 | #endif | |
0a852541 | 320 | } |
5e9e0efb | 321 | } |
0a852541 | 322 | |
a0358e5d | 323 | void netlink_multicast(void) |
5e9e0efb SK |
324 | { |
325 | ssize_t len; | |
326 | struct nlmsghdr *h; | |
a0358e5d | 327 | int flags; |
7622fc06 SK |
328 | |
329 | /* don't risk blocking reading netlink messages here. */ | |
330 | if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 || | |
331 | fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1) | |
332 | return; | |
0a852541 | 333 | |
5aabfc78 | 334 | if ((len = netlink_recv()) != -1) |
54dd393f | 335 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
a0358e5d | 336 | nl_async(h); |
54dd393f SK |
337 | |
338 | /* restore non-blocking status */ | |
339 | fcntl(daemon->netlinkfd, F_SETFL, flags); | |
0a852541 SK |
340 | } |
341 | ||
a0358e5d | 342 | static void nl_async(struct nlmsghdr *h) |
5e9e0efb | 343 | { |
54dd393f SK |
344 | if (h->nlmsg_type == NLMSG_ERROR) |
345 | { | |
346 | struct nlmsgerr *err = NLMSG_DATA(h); | |
dfb23b3f SK |
347 | if (err->error != 0) |
348 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); | |
54dd393f SK |
349 | } |
350 | else if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) | |
351 | { | |
e17b4b38 SK |
352 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
353 | If this happens and we still have a DNS packet in the buffer, we re-send it. | |
354 | This helps on DoD links, where frequently the packet which triggers dialling is | |
355 | a DNS query, which then gets lost. By re-sending, we can avoid the lookup | |
356 | failing. */ | |
357 | struct rtmsg *rtm = NLMSG_DATA(h); | |
358 | ||
359 | if (rtm->rtm_type == RTN_UNICAST && rtm->rtm_scope == RT_SCOPE_LINK) | |
47a95169 | 360 | queue_event(EVENT_NEWROUTE); |
54dd393f | 361 | } |
1f776932 | 362 | else if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) |
47a95169 | 363 | queue_event(EVENT_NEWADDR); |
54dd393f | 364 | } |
0a852541 SK |
365 | #endif |
366 | ||
367 |