Dnsmasq: 2.61 update
[tomato.git] / release / src / router / dnsmasq / src / netlink.c
blobd20654dec7c83ae23203c0c0b87e96f9cbd09857
1 /* dnsmasq is Copyright (c) 2000-2012 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/>.
17 #include "dnsmasq.h"
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. */
26 #ifndef IFA_RTA
27 # define IFA_RTA(r) \
28 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
30 # include <linux/if_addr.h>
31 #endif
33 #ifndef NDA_RTA
34 # define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
35 #endif
38 static struct iovec iov;
39 static u32 netlink_pid;
41 static void nl_err(struct nlmsghdr *h);
42 static void nl_routechange(struct nlmsghdr *h);
44 void netlink_init(void)
46 struct sockaddr_nl addr;
47 socklen_t slen = sizeof(addr);
49 addr.nl_family = AF_NETLINK;
50 addr.nl_pad = 0;
51 addr.nl_pid = 0; /* autobind */
52 #ifdef HAVE_IPV6
53 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
54 #else
55 addr.nl_groups = RTMGRP_IPV4_ROUTE;
56 #endif
58 /* May not be able to have permission to set multicast groups don't die in that case */
59 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
61 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
63 addr.nl_groups = 0;
64 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
65 daemon->netlinkfd = -1;
69 if (daemon->netlinkfd == -1 ||
70 getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == 1)
71 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
73 /* save pid assigned by bind() and retrieved by getsockname() */
74 netlink_pid = addr.nl_pid;
76 iov.iov_len = 100;
77 iov.iov_base = safe_malloc(iov.iov_len);
80 static ssize_t netlink_recv(void)
82 struct msghdr msg;
83 struct sockaddr_nl nladdr;
84 ssize_t rc;
86 while (1)
88 msg.msg_control = NULL;
89 msg.msg_controllen = 0;
90 msg.msg_name = &nladdr;
91 msg.msg_namelen = sizeof(nladdr);
92 msg.msg_iov = &iov;
93 msg.msg_iovlen = 1;
94 msg.msg_flags = 0;
96 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
98 /* make buffer big enough */
99 if (rc != -1 && (msg.msg_flags & MSG_TRUNC))
101 /* Very new Linux kernels return the actual size needed, older ones always return truncated size */
102 if ((size_t)rc == iov.iov_len)
104 if (expand_buf(&iov, rc + 100))
105 continue;
107 else
108 expand_buf(&iov, rc);
111 /* read it for real */
112 msg.msg_flags = 0;
113 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
115 /* Make sure this is from the kernel */
116 if (rc == -1 || nladdr.nl_pid == 0)
117 break;
120 /* discard stuff which is truncated at this point (expand_buf() may fail) */
121 if (msg.msg_flags & MSG_TRUNC)
123 rc = -1;
124 errno = ENOMEM;
127 return rc;
131 /* family = AF_UNSPEC finds ARP table entries.
132 family = AF_LOCAL finds MAC addresses. */
133 int iface_enumerate(int family, void *parm, int (*callback)())
135 struct sockaddr_nl addr;
136 struct nlmsghdr *h;
137 ssize_t len;
138 static unsigned int seq = 0;
139 int callback_ok = 1;
141 struct {
142 struct nlmsghdr nlh;
143 struct rtgenmsg g;
144 } req;
146 addr.nl_family = AF_NETLINK;
147 addr.nl_pad = 0;
148 addr.nl_groups = 0;
149 addr.nl_pid = 0; /* address to kernel */
151 again:
152 if (family == AF_UNSPEC)
153 req.nlh.nlmsg_type = RTM_GETNEIGH;
154 else if (family == AF_LOCAL)
155 req.nlh.nlmsg_type = RTM_GETLINK;
156 else
157 req.nlh.nlmsg_type = RTM_GETADDR;
159 req.nlh.nlmsg_len = sizeof(req);
160 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
161 req.nlh.nlmsg_pid = 0;
162 req.nlh.nlmsg_seq = ++seq;
163 req.g.rtgen_family = family;
165 /* Don't block in recvfrom if send fails */
166 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
167 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
169 if (len == -1)
170 return 0;
172 while (1)
174 if ((len = netlink_recv()) == -1)
176 if (errno == ENOBUFS)
178 sleep(1);
179 goto again;
181 return 0;
184 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
185 if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
186 nl_routechange(h); /* May be multicast arriving async */
187 else if (h->nlmsg_type == NLMSG_ERROR)
188 nl_err(h);
189 else if (h->nlmsg_type == NLMSG_DONE)
190 return callback_ok;
191 else if (h->nlmsg_type == RTM_NEWADDR && family != AF_UNSPEC && family != AF_LOCAL)
193 struct ifaddrmsg *ifa = NLMSG_DATA(h);
194 struct rtattr *rta = IFA_RTA(ifa);
195 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
197 if (ifa->ifa_family == family)
199 if (ifa->ifa_family == AF_INET)
201 struct in_addr netmask, addr, broadcast;
203 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
204 addr.s_addr = 0;
205 broadcast.s_addr = 0;
207 while (RTA_OK(rta, len1))
209 if (rta->rta_type == IFA_LOCAL)
210 addr = *((struct in_addr *)(rta+1));
211 else if (rta->rta_type == IFA_BROADCAST)
212 broadcast = *((struct in_addr *)(rta+1));
214 rta = RTA_NEXT(rta, len1);
217 if (addr.s_addr && callback_ok)
218 if (!((*callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
219 callback_ok = 0;
221 #ifdef HAVE_IPV6
222 else if (ifa->ifa_family == AF_INET6)
224 struct in6_addr *addrp = NULL;
225 while (RTA_OK(rta, len1))
227 if (rta->rta_type == IFA_ADDRESS)
228 addrp = ((struct in6_addr *)(rta+1));
230 rta = RTA_NEXT(rta, len1);
233 if (addrp && callback_ok)
234 if (!((*callback)(addrp, (int)(ifa->ifa_prefixlen), (int)(ifa->ifa_scope),
235 (int)(ifa->ifa_index), (int)(ifa->ifa_flags & IFA_F_TENTATIVE), parm)))
236 callback_ok = 0;
238 #endif
241 else if (h->nlmsg_type == RTM_NEWNEIGH && family == AF_UNSPEC)
243 struct ndmsg *neigh = NLMSG_DATA(h);
244 struct rtattr *rta = NDA_RTA(neigh);
245 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*neigh));
246 size_t maclen = 0;
247 char *inaddr = NULL, *mac = NULL;
249 while (RTA_OK(rta, len1))
251 if (rta->rta_type == NDA_DST)
252 inaddr = (char *)(rta+1);
253 else if (rta->rta_type == NDA_LLADDR)
255 maclen = rta->rta_len - sizeof(struct rtattr);
256 mac = (char *)(rta+1);
259 rta = RTA_NEXT(rta, len1);
262 if (inaddr && mac && callback_ok)
263 if (!((*callback)(neigh->ndm_family, inaddr, mac, maclen, parm)))
264 callback_ok = 0;
266 #ifdef HAVE_DHCP6
267 else if (h->nlmsg_type == RTM_NEWLINK && family == AF_LOCAL)
269 struct ifinfomsg *link = NLMSG_DATA(h);
270 struct rtattr *rta = IFLA_RTA(link);
271 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*link));
272 char *mac = NULL;
273 size_t maclen = 0;
275 while (RTA_OK(rta, len1))
277 if (rta->rta_type == IFLA_ADDRESS)
279 maclen = rta->rta_len - sizeof(struct rtattr);
280 mac = (char *)(rta+1);
283 rta = RTA_NEXT(rta, len1);
286 if (mac && callback_ok && !((link->ifi_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) &&
287 !((*callback)((int)link->ifi_index, (unsigned int)link->ifi_type, mac, maclen, parm)))
288 callback_ok = 0;
290 #endif
294 void netlink_multicast(void)
296 ssize_t len;
297 struct nlmsghdr *h;
298 int flags;
300 /* don't risk blocking reading netlink messages here. */
301 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
302 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
303 return;
305 if ((len = netlink_recv()) != -1)
307 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
308 if (h->nlmsg_type == NLMSG_ERROR)
309 nl_err(h);
310 else
311 nl_routechange(h);
314 /* restore non-blocking status */
315 fcntl(daemon->netlinkfd, F_SETFL, flags);
318 static void nl_err(struct nlmsghdr *h)
320 struct nlmsgerr *err = NLMSG_DATA(h);
322 if (err->error != 0)
323 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
326 /* We arrange to receive netlink multicast messages whenever the network route is added.
327 If this happens and we still have a DNS packet in the buffer, we re-send it.
328 This helps on DoD links, where frequently the packet which triggers dialling is
329 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
330 failing. Note that we only accept these messages from the kernel (pid == 0) */
331 static void nl_routechange(struct nlmsghdr *h)
333 if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE)
335 struct rtmsg *rtm = NLMSG_DATA(h);
336 int fd;
338 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK)
339 return;
341 /* Force re-reading resolv file right now, for luck. */
342 daemon->last_resolv = 0;
344 #ifdef HAVE_DHCP6
345 /* force RAs to sync new network and pick up new interfaces. */
346 if (daemon->ra_contexts)
348 schedule_subnet_map();
349 ra_start_unsolicted(dnsmasq_time(), NULL);
350 /* cause lease_update_file to run after we return, in case we were called from
351 iface_enumerate and can't re-enter it now */
352 send_alarm(0, 0);
354 #endif
356 if (daemon->srv_save)
358 if (daemon->srv_save->sfd)
359 fd = daemon->srv_save->sfd->fd;
360 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
361 fd = daemon->rfd_save->fd;
362 else
363 return;
365 while(sendto(fd, daemon->packet, daemon->packet_len, 0,
366 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
371 #endif