1 /* dnsmasq is Copyright (c) 2000-2009 Simon Kelley
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
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
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.
12
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/>.
15 */
16
17 #include "dnsmasq.h"
18
19 #ifdef HAVE_LINUX_NETWORK
20
21 #include <linux/netlink.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/types.h>
24
25 /* linux 2.6.19 buggers up the headers, patch it up here. */
26 #ifndef IFA_RTA
27 #define IFA_RTA(r) ((struct rtattr*) (((char*) (r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
28
29 #include <linux/if_addr.h>
30 #endif
31
32 static struct iovec iov;
33 static u32 netlink_pid;
34
35 static void nl_err(struct nlmsghdr* h);
36 static void nl_routechange(struct nlmsghdr* h);
37
netlink_init(void)38 void netlink_init(void) {
39 struct sockaddr_nl addr;
40 socklen_t slen = sizeof(addr);
41
42 addr.nl_family = AF_NETLINK;
43 addr.nl_pad = 0;
44 addr.nl_pid = 0; /* autobind */
45 #ifdef HAVE_IPV6
46 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
47 #else
48 addr.nl_groups = RTMGRP_IPV4_ROUTE;
49 #endif
50
51 /* 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 if (bind(daemon->netlinkfd, (struct sockaddr*) &addr, sizeof(addr)) == -1) {
54 addr.nl_groups = 0;
55 if (errno != EPERM ||
56 bind(daemon->netlinkfd, (struct sockaddr*) &addr, sizeof(addr)) == -1)
57 daemon->netlinkfd = -1;
58 }
59 }
60
61 if (daemon->netlinkfd == -1 ||
62 getsockname(daemon->netlinkfd, (struct sockaddr*) &addr, &slen) == 1)
63 die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
64
65 /* save pid assigned by bind() and retrieved by getsockname() */
66 netlink_pid = addr.nl_pid;
67
68 iov.iov_len = 100;
69 iov.iov_base = safe_malloc(iov.iov_len);
70 }
71
netlink_recv(void)72 static ssize_t netlink_recv(void) {
73 struct msghdr msg;
74 struct sockaddr_nl nladdr;
75 ssize_t rc;
76
77 while (1) {
78 msg.msg_control = NULL;
79 msg.msg_controllen = 0;
80 msg.msg_name = &nladdr;
81 msg.msg_namelen = sizeof(nladdr);
82 msg.msg_iov = &iov;
83 msg.msg_iovlen = 1;
84 msg.msg_flags = 0;
85
86 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR)
87 ;
88
89 /* make buffer big enough */
90 if (rc != -1 && (msg.msg_flags & MSG_TRUNC)) {
91 /* Very new Linux kernels return the actual size needed, older ones always return
92 * truncated size */
93 if ((size_t) rc == iov.iov_len) {
94 if (expand_buf(&iov, rc + 100)) continue;
95 } else
96 expand_buf(&iov, rc);
97 }
98
99 /* read it for real */
100 msg.msg_flags = 0;
101 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR)
102 ;
103
104 /* Make sure this is from the kernel */
105 if (rc == -1 || nladdr.nl_pid == 0) break;
106 }
107
108 /* discard stuff which is truncated at this point (expand_buf() may fail) */
109 if (msg.msg_flags & MSG_TRUNC) {
110 rc = -1;
111 errno = ENOMEM;
112 }
113
114 return rc;
115 }
116
iface_enumerate(void * parm,int (* ipv4_callback)(),int (* ipv6_callback)())117 int iface_enumerate(void* parm, int (*ipv4_callback)(), int (*ipv6_callback)()) {
118 struct sockaddr_nl addr;
119 struct nlmsghdr* h;
120 ssize_t len;
121 static unsigned int seq = 0;
122 int family = AF_INET;
123
124 struct {
125 struct nlmsghdr nlh;
126 struct rtgenmsg g;
127 } req;
128
129 addr.nl_family = AF_NETLINK;
130 addr.nl_pad = 0;
131 addr.nl_groups = 0;
132 addr.nl_pid = 0; /* address to kernel */
133
134 again:
135 req.nlh.nlmsg_len = sizeof(req);
136 req.nlh.nlmsg_type = RTM_GETADDR;
137 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
138 req.nlh.nlmsg_pid = 0;
139 req.nlh.nlmsg_seq = ++seq;
140 req.g.rtgen_family = family;
141
142 /* Don't block in recvfrom if send fails */
143 while ((len = sendto(daemon->netlinkfd, (void*) &req, sizeof(req), 0, (struct sockaddr*) &addr,
144 sizeof(addr))) == -1 &&
145 retry_send())
146 ;
147
148 if (len == -1) return 0;
149
150 while (1) {
151 if ((len = netlink_recv()) == -1) {
152 if (errno == ENOBUFS) {
153 sleep(1);
154 goto again;
155 }
156 return 0;
157 }
158
159 for (h = (struct nlmsghdr*) iov.iov_base; NLMSG_OK(h, (size_t) len); h = NLMSG_NEXT(h, len))
160 if (h->nlmsg_seq != seq || h->nlmsg_pid != netlink_pid)
161 nl_routechange(h); /* May be multicast arriving async */
162 else if (h->nlmsg_type == NLMSG_ERROR)
163 nl_err(h);
164 else if (h->nlmsg_type == NLMSG_DONE) {
165 #ifdef HAVE_IPV6
166 if (family == AF_INET && ipv6_callback) {
167 family = AF_INET6;
168 goto again;
169 }
170 #endif
171 return 1;
172 } else if (h->nlmsg_type == RTM_NEWADDR) {
173 struct ifaddrmsg* ifa = NLMSG_DATA(h);
174 struct rtattr* rta = IFA_RTA(ifa);
175 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
176
177 if (ifa->ifa_family == AF_INET) {
178 struct in_addr netmask, addr, broadcast;
179
180 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
181 addr.s_addr = 0;
182 broadcast.s_addr = 0;
183
184 while (RTA_OK(rta, len1)) {
185 if (rta->rta_type == IFA_LOCAL)
186 addr = *((struct in_addr*) (rta + 1));
187 else if (rta->rta_type == IFA_BROADCAST)
188 broadcast = *((struct in_addr*) (rta + 1));
189
190 rta = RTA_NEXT(rta, len1);
191 }
192
193 if (addr.s_addr && ipv4_callback)
194 if (!((*ipv4_callback)(addr, ifa->ifa_index, netmask, broadcast, parm)))
195 return 0;
196 }
197 #ifdef HAVE_IPV6
198 else if (ifa->ifa_family == AF_INET6) {
199 struct in6_addr* addrp = NULL;
200 while (RTA_OK(rta, len1)) {
201 if (rta->rta_type == IFA_ADDRESS) addrp = ((struct in6_addr*) (rta + 1));
202
203 rta = RTA_NEXT(rta, len1);
204 }
205
206 if (addrp && ipv6_callback)
207 if (!((*ipv6_callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm)))
208 return 0;
209 }
210 #endif
211 }
212 }
213 }
214
netlink_multicast(void)215 void netlink_multicast(void) {
216 ssize_t len;
217 struct nlmsghdr* h;
218 int flags;
219
220 /* don't risk blocking reading netlink messages here. */
221 if ((flags = fcntl(daemon->netlinkfd, F_GETFL)) == -1 ||
222 fcntl(daemon->netlinkfd, F_SETFL, flags | O_NONBLOCK) == -1)
223 return;
224
225 if ((len = netlink_recv()) != -1) {
226 for (h = (struct nlmsghdr*) iov.iov_base; NLMSG_OK(h, (size_t) len); h = NLMSG_NEXT(h, len))
227 if (h->nlmsg_type == NLMSG_ERROR)
228 nl_err(h);
229 else
230 nl_routechange(h);
231 }
232
233 /* restore non-blocking status */
234 fcntl(daemon->netlinkfd, F_SETFL, flags);
235 }
236
nl_err(struct nlmsghdr * h)237 static void nl_err(struct nlmsghdr* h) {
238 struct nlmsgerr* err = NLMSG_DATA(h);
239
240 if (err->error != 0)
241 my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
242 }
243
244 /* We arrange to receive netlink multicast messages whenever the network route is added.
245 If this happens and we still have a DNS packet in the buffer, we re-send it.
246 This helps on DoD links, where frequently the packet which triggers dialling is
247 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
248 failing. Note that we only accept these messages from the kernel (pid == 0) */
nl_routechange(struct nlmsghdr * h)249 static void nl_routechange(struct nlmsghdr* h) {
250 if (h->nlmsg_pid == 0 && h->nlmsg_type == RTM_NEWROUTE) {
251 struct rtmsg* rtm = NLMSG_DATA(h);
252 int fd;
253
254 if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK) return;
255
256 /* Force re-reading resolv file right now, for luck. */
257 daemon->last_resolv = 0;
258
259 if (daemon->srv_save) {
260 if (daemon->srv_save->sfd)
261 fd = daemon->srv_save->sfd->fd;
262 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
263 fd = daemon->rfd_save->fd;
264 else
265 return;
266
267 while (sendto(fd, daemon->packet, daemon->packet_len, 0, &daemon->srv_save->addr.sa,
268 sa_len(&daemon->srv_save->addr)) == -1 &&
269 retry_send())
270 ;
271 }
272 }
273 }
274
275 #endif
276