blob: b744ac92158bbb2a7b0177ac11a93834dddf7cce [file] [log] [blame]
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001 /* dnsmasq is Copyright (c) 2000-2006 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
5 the Free Software Foundation; version 2 dated June, 1991.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11*/
12
Simon Kelley0a852542005-03-23 20:28:59 +000013#include "dnsmasq.h"
14
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010015#ifdef HAVE_LINUX_NETWORK
Simon Kelley0a852542005-03-23 20:28:59 +000016
Simon Kelley91dccd02005-03-31 17:48:32 +010017#include <linux/types.h>
Simon Kelley0a852542005-03-23 20:28:59 +000018#include <linux/netlink.h>
19#include <linux/rtnetlink.h>
20
Simon Kelley832af0b2007-01-21 20:01:28 +000021/* linux 2.6.19 buggers up the headers, patch it up here. */
22#ifndef IFA_RTA
23# define IFA_RTA(r) \
24 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
25
26# include <linux/if_addr.h>
27#endif
28
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010029static struct iovec iov;
30
31static void nl_err(struct nlmsghdr *h);
32static void nl_routechange(struct daemon *daemon, struct nlmsghdr *h);
Simon Kelleycdeda282006-03-16 20:16:06 +000033
34void netlink_init(struct daemon *daemon)
Simon Kelley0a852542005-03-23 20:28:59 +000035{
36 struct sockaddr_nl addr;
Simon Kelley0a852542005-03-23 20:28:59 +000037
38 addr.nl_family = AF_NETLINK;
39 addr.nl_pad = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010040 addr.nl_pid = 0; /* autobind */
Simon Kelleycdeda282006-03-16 20:16:06 +000041#ifdef HAVE_IPV6
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010042 addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000043#else
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010044 addr.nl_groups = RTMGRP_IPV4_ROUTE;
Simon Kelleycdeda282006-03-16 20:16:06 +000045#endif
Simon Kelley0a852542005-03-23 20:28:59 +000046
Simon Kelley309331f2006-04-22 15:05:01 +010047 /* May not be able to have permission to set multicast groups don't die in that case */
48 if ((daemon->netlinkfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1)
49 {
50 if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
51 {
52 addr.nl_groups = 0;
53 if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
54 daemon->netlinkfd = -1;
55 }
56 }
57
58 if (daemon->netlinkfd == -1)
Simon Kelley7cebd202006-05-06 14:13:33 +010059 die(_("cannot create netlink socket: %s"), NULL);
60 else
61 {
62 int flags = fcntl(daemon->netlinkfd, F_GETFD);
63 if (flags != -1)
64 fcntl(daemon->netlinkfd, F_SETFD, flags | FD_CLOEXEC);
65 }
66
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010067 iov.iov_len = 200;
68 iov.iov_base = safe_malloc(iov.iov_len);
Simon Kelley0a852542005-03-23 20:28:59 +000069}
70
Simon Kelleycdeda282006-03-16 20:16:06 +000071static ssize_t netlink_recv(struct daemon *daemon)
72{
73 struct msghdr msg;
74 ssize_t rc;
Simon Kelley0a852542005-03-23 20:28:59 +000075
Simon Kelleycdeda282006-03-16 20:16:06 +000076 msg.msg_control = NULL;
77 msg.msg_controllen = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +000078 msg.msg_name = NULL;
79 msg.msg_namelen = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010080 msg.msg_iov = &iov;
Simon Kelleycdeda282006-03-16 20:16:06 +000081 msg.msg_iovlen = 1;
82
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010083 while (1)
Simon Kelleycdeda282006-03-16 20:16:06 +000084 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010085 msg.msg_flags = 0;
86 while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK)) == -1 && errno == EINTR);
87
88 /* 2.2.x doesn't suport MSG_PEEK at all, returning EOPNOTSUPP, so we just grab a
89 big buffer and pray in that case. */
90 if (rc == -1 && errno == EOPNOTSUPP)
91 {
92 if (!expand_buf(&iov, 2000))
93 return -1;
94 break;
95 }
96
97 if (rc == -1 || !(msg.msg_flags & MSG_TRUNC))
98 break;
99
100 if (!expand_buf(&iov, iov.iov_len + 100))
Simon Kelleycdeda282006-03-16 20:16:06 +0000101 return -1;
Simon Kelleycdeda282006-03-16 20:16:06 +0000102 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100103
104 /* finally, read it for real */
105 while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR);
Simon Kelleycdeda282006-03-16 20:16:06 +0000106
107 return rc;
108}
109
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100110int iface_enumerate(struct daemon *daemon, void *parm, int (*ipv4_callback)(), int (*ipv6_callback)())
Simon Kelley0a852542005-03-23 20:28:59 +0000111{
112 struct sockaddr_nl addr;
113 struct nlmsghdr *h;
Simon Kelleycdeda282006-03-16 20:16:06 +0000114 ssize_t len;
Simon Kelley0a852542005-03-23 20:28:59 +0000115 static unsigned int seq = 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100116 int family = AF_INET;
Simon Kelley0a852542005-03-23 20:28:59 +0000117
118 struct {
119 struct nlmsghdr nlh;
120 struct rtgenmsg g;
121 } req;
122
Simon Kelley0a852542005-03-23 20:28:59 +0000123 addr.nl_family = AF_NETLINK;
124 addr.nl_pad = 0;
125 addr.nl_groups = 0;
126 addr.nl_pid = 0; /* address to kernel */
127
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100128 again:
Simon Kelley0a852542005-03-23 20:28:59 +0000129 req.nlh.nlmsg_len = sizeof(req);
130 req.nlh.nlmsg_type = RTM_GETADDR;
Simon Kelley7cebd202006-05-06 14:13:33 +0100131 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK;
Simon Kelley0a852542005-03-23 20:28:59 +0000132 req.nlh.nlmsg_pid = 0;
133 req.nlh.nlmsg_seq = ++seq;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100134 req.g.rtgen_family = family;
Simon Kelley0a852542005-03-23 20:28:59 +0000135
136 /* Don't block in recvfrom if send fails */
137 while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0,
138 (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send());
Simon Kelley0a852542005-03-23 20:28:59 +0000139
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100140 if (len == -1)
141 return 0;
142
143 while (1)
Simon Kelley0a852542005-03-23 20:28:59 +0000144 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100145 if ((len = netlink_recv(daemon)) == -1)
Simon Kelley0a852542005-03-23 20:28:59 +0000146 return 0;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100147
148 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
149 if (h->nlmsg_type == NLMSG_ERROR)
150 nl_err(h);
151 else if (h->nlmsg_seq != seq)
152 nl_routechange(daemon, h); /* May be multicast arriving async */
153 else if (h->nlmsg_type == NLMSG_DONE)
154 {
155#ifdef HAVE_IPV6
156 if (family == AF_INET && ipv6_callback)
157 {
158 family = AF_INET6;
159 goto again;
160 }
161#endif
162 return 1;
163 }
164 else if (h->nlmsg_type == RTM_NEWADDR)
165 {
166 struct ifaddrmsg *ifa = NLMSG_DATA(h);
167 struct rtattr *rta = IFA_RTA(ifa);
168 unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
169
170 if (ifa->ifa_family == AF_INET)
171 {
172 struct in_addr netmask, addr, broadcast;
173
174 netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen));
175 addr.s_addr = 0;
176 broadcast.s_addr = 0;
177
178 while (RTA_OK(rta, len1))
179 {
180 if (rta->rta_type == IFA_LOCAL)
181 addr = *((struct in_addr *)(rta+1));
182 else if (rta->rta_type == IFA_BROADCAST)
183 broadcast = *((struct in_addr *)(rta+1));
184
185 rta = RTA_NEXT(rta, len1);
186 }
187
188 if (addr.s_addr && ipv4_callback)
189 if (!((*ipv4_callback)(daemon, addr, ifa->ifa_index, netmask, broadcast, parm)))
190 return 0;
191 }
192#ifdef HAVE_IPV6
193 else if (ifa->ifa_family == AF_INET6)
194 {
195 struct in6_addr *addrp = NULL;
196 while (RTA_OK(rta, len1))
197 {
198 if (rta->rta_type == IFA_ADDRESS)
199 addrp = ((struct in6_addr *)(rta+1));
200
201 rta = RTA_NEXT(rta, len1);
202 }
203
204 if (addrp && ipv6_callback)
205 if (!((*ipv6_callback)(daemon, addrp, ifa->ifa_index, ifa->ifa_index, parm)))
206 return 0;
207 }
208#endif
209 }
Simon Kelley0a852542005-03-23 20:28:59 +0000210 }
Simon Kelley0a852542005-03-23 20:28:59 +0000211}
212
Simon Kelleycdeda282006-03-16 20:16:06 +0000213void netlink_multicast(struct daemon *daemon)
214{
215 ssize_t len;
216 struct nlmsghdr *h;
217
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100218 if ((len = netlink_recv(daemon)) != -1)
Simon Kelleycdeda282006-03-16 20:16:06 +0000219 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100220 for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len))
221 if (h->nlmsg_type == NLMSG_ERROR)
222 nl_err(h);
223 else
224 nl_routechange(daemon, h);
Simon Kelleycdeda282006-03-16 20:16:06 +0000225 }
226}
227
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100228static void nl_err(struct nlmsghdr *h)
229{
230 struct nlmsgerr *err = NLMSG_DATA(h);
231 if (err->error != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100232 syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error)));
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100233}
234
235/* We arrange to receive netlink multicast messages whenever the network route is added.
236 If this happens and we still have a DNS packet in the buffer, we re-send it.
237 This helps on DoD links, where frequently the packet which triggers dialling is
238 a DNS query, which then gets lost. By re-sending, we can avoid the lookup
239 failing. */
240static void nl_routechange(struct daemon *daemon, struct nlmsghdr *h)
241{
242 if (h->nlmsg_type == RTM_NEWROUTE && daemon->srv_save)
243 {
244 struct rtmsg *rtm = NLMSG_DATA(h);
245 if (rtm->rtm_type == RTN_UNICAST &&
246 rtm->rtm_scope == RT_SCOPE_LINK)
247 while(sendto(daemon->srv_save->sfd->fd, daemon->packet, daemon->packet_len, 0,
248 &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send());
249 }
250}
Simon Kelley0a852542005-03-23 20:28:59 +0000251#endif
252
253