]>
Commit | Line | Data |
---|---|---|
824af85b | 1 | /* dnsmasq is Copyright (c) 2000-2007 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 SK |
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/>. | |
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 | ||
5e9e0efb SK |
33 | static struct iovec iov; |
34 | ||
35 | static void nl_err(struct nlmsghdr *h); | |
5aabfc78 | 36 | static void nl_routechange(struct nlmsghdr *h); |
cdeda28f | 37 | |
5aabfc78 | 38 | void netlink_init(void) |
0a852541 SK |
39 | { |
40 | struct sockaddr_nl addr; | |
0a852541 SK |
41 | |
42 | addr.nl_family = AF_NETLINK; | |
43 | addr.nl_pad = 0; | |
5e9e0efb | 44 | addr.nl_pid = 0; /* autobind */ |
cdeda28f | 45 | #ifdef HAVE_IPV6 |
5e9e0efb | 46 | addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE; |
cdeda28f | 47 | #else |
5e9e0efb | 48 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
cdeda28f | 49 | #endif |
0a852541 | 50 | |
309331f5 SK |
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 | { | |
54 | if (bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
55 | { | |
56 | addr.nl_groups = 0; | |
57 | if (errno != EPERM || bind(daemon->netlinkfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
58 | daemon->netlinkfd = -1; | |
59 | } | |
60 | } | |
61 | ||
62 | if (daemon->netlinkfd == -1) | |
5aabfc78 SK |
63 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
64 | ||
5e9e0efb SK |
65 | iov.iov_len = 200; |
66 | iov.iov_base = safe_malloc(iov.iov_len); | |
0a852541 SK |
67 | } |
68 | ||
5aabfc78 | 69 | static ssize_t netlink_recv(void) |
cdeda28f SK |
70 | { |
71 | struct msghdr msg; | |
72 | ssize_t rc; | |
73 | ||
74 | msg.msg_control = NULL; | |
75 | msg.msg_controllen = 0; | |
cdeda28f SK |
76 | msg.msg_name = NULL; |
77 | msg.msg_namelen = 0; | |
5e9e0efb | 78 | msg.msg_iov = &iov; |
cdeda28f SK |
79 | msg.msg_iovlen = 1; |
80 | ||
5e9e0efb | 81 | while (1) |
cdeda28f | 82 | { |
5e9e0efb SK |
83 | msg.msg_flags = 0; |
84 | while ((rc = recvmsg(daemon->netlinkfd, &msg, MSG_PEEK)) == -1 && errno == EINTR); | |
85 | ||
86 | /* 2.2.x doesn't suport MSG_PEEK at all, returning EOPNOTSUPP, so we just grab a | |
87 | big buffer and pray in that case. */ | |
88 | if (rc == -1 && errno == EOPNOTSUPP) | |
89 | { | |
90 | if (!expand_buf(&iov, 2000)) | |
91 | return -1; | |
92 | break; | |
93 | } | |
94 | ||
95 | if (rc == -1 || !(msg.msg_flags & MSG_TRUNC)) | |
96 | break; | |
97 | ||
98 | if (!expand_buf(&iov, iov.iov_len + 100)) | |
cdeda28f | 99 | return -1; |
cdeda28f | 100 | } |
5e9e0efb SK |
101 | |
102 | /* finally, read it for real */ | |
103 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); | |
cdeda28f SK |
104 | |
105 | return rc; | |
106 | } | |
107 | ||
5aabfc78 | 108 | int iface_enumerate(void *parm, int (*ipv4_callback)(), int (*ipv6_callback)()) |
0a852541 SK |
109 | { |
110 | struct sockaddr_nl addr; | |
111 | struct nlmsghdr *h; | |
cdeda28f | 112 | ssize_t len; |
0a852541 | 113 | static unsigned int seq = 0; |
5e9e0efb | 114 | int family = AF_INET; |
0a852541 SK |
115 | |
116 | struct { | |
117 | struct nlmsghdr nlh; | |
118 | struct rtgenmsg g; | |
119 | } req; | |
120 | ||
0a852541 SK |
121 | addr.nl_family = AF_NETLINK; |
122 | addr.nl_pad = 0; | |
123 | addr.nl_groups = 0; | |
124 | addr.nl_pid = 0; /* address to kernel */ | |
125 | ||
5e9e0efb | 126 | again: |
0a852541 SK |
127 | req.nlh.nlmsg_len = sizeof(req); |
128 | req.nlh.nlmsg_type = RTM_GETADDR; | |
7cebd20f | 129 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
0a852541 SK |
130 | req.nlh.nlmsg_pid = 0; |
131 | req.nlh.nlmsg_seq = ++seq; | |
5e9e0efb | 132 | req.g.rtgen_family = family; |
0a852541 SK |
133 | |
134 | /* Don't block in recvfrom if send fails */ | |
135 | while((len = sendto(daemon->netlinkfd, (void *)&req, sizeof(req), 0, | |
136 | (struct sockaddr *)&addr, sizeof(addr))) == -1 && retry_send()); | |
5e9e0efb | 137 | |
0a852541 | 138 | if (len == -1) |
5e9e0efb SK |
139 | return 0; |
140 | ||
141 | while (1) | |
0a852541 | 142 | { |
5aabfc78 | 143 | if ((len = netlink_recv()) == -1) |
5e9e0efb SK |
144 | return 0; |
145 | ||
146 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) | |
147 | if (h->nlmsg_type == NLMSG_ERROR) | |
148 | nl_err(h); | |
149 | else if (h->nlmsg_seq != seq) | |
5aabfc78 | 150 | nl_routechange(h); /* May be multicast arriving async */ |
5e9e0efb SK |
151 | else if (h->nlmsg_type == NLMSG_DONE) |
152 | { | |
153 | #ifdef HAVE_IPV6 | |
154 | if (family == AF_INET && ipv6_callback) | |
155 | { | |
156 | family = AF_INET6; | |
157 | goto again; | |
158 | } | |
159 | #endif | |
160 | return 1; | |
161 | } | |
162 | else if (h->nlmsg_type == RTM_NEWADDR) | |
163 | { | |
164 | struct ifaddrmsg *ifa = NLMSG_DATA(h); | |
165 | struct rtattr *rta = IFA_RTA(ifa); | |
166 | unsigned int len1 = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); | |
167 | ||
168 | if (ifa->ifa_family == AF_INET) | |
169 | { | |
170 | struct in_addr netmask, addr, broadcast; | |
171 | ||
172 | netmask.s_addr = htonl(0xffffffff << (32 - ifa->ifa_prefixlen)); | |
173 | addr.s_addr = 0; | |
174 | broadcast.s_addr = 0; | |
175 | ||
176 | while (RTA_OK(rta, len1)) | |
177 | { | |
178 | if (rta->rta_type == IFA_LOCAL) | |
179 | addr = *((struct in_addr *)(rta+1)); | |
180 | else if (rta->rta_type == IFA_BROADCAST) | |
181 | broadcast = *((struct in_addr *)(rta+1)); | |
182 | ||
183 | rta = RTA_NEXT(rta, len1); | |
184 | } | |
185 | ||
186 | if (addr.s_addr && ipv4_callback) | |
5aabfc78 | 187 | if (!((*ipv4_callback)(addr, ifa->ifa_index, netmask, broadcast, parm))) |
5e9e0efb SK |
188 | return 0; |
189 | } | |
190 | #ifdef HAVE_IPV6 | |
191 | else if (ifa->ifa_family == AF_INET6) | |
192 | { | |
193 | struct in6_addr *addrp = NULL; | |
194 | while (RTA_OK(rta, len1)) | |
195 | { | |
196 | if (rta->rta_type == IFA_ADDRESS) | |
197 | addrp = ((struct in6_addr *)(rta+1)); | |
198 | ||
199 | rta = RTA_NEXT(rta, len1); | |
200 | } | |
201 | ||
202 | if (addrp && ipv6_callback) | |
5aabfc78 | 203 | if (!((*ipv6_callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm))) |
5e9e0efb SK |
204 | return 0; |
205 | } | |
206 | #endif | |
207 | } | |
0a852541 | 208 | } |
5e9e0efb | 209 | } |
0a852541 | 210 | |
5aabfc78 | 211 | void netlink_multicast(void) |
5e9e0efb SK |
212 | { |
213 | ssize_t len; | |
214 | struct nlmsghdr *h; | |
0a852541 | 215 | |
5aabfc78 | 216 | if ((len = netlink_recv()) != -1) |
0a852541 | 217 | { |
5e9e0efb SK |
218 | for (h = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(h, (size_t)len); h = NLMSG_NEXT(h, len)) |
219 | if (h->nlmsg_type == NLMSG_ERROR) | |
220 | nl_err(h); | |
221 | else | |
5aabfc78 | 222 | nl_routechange(h); |
0a852541 | 223 | } |
0a852541 SK |
224 | } |
225 | ||
5e9e0efb SK |
226 | static void nl_err(struct nlmsghdr *h) |
227 | { | |
228 | struct nlmsgerr *err = NLMSG_DATA(h); | |
229 | if (err->error != 0) | |
f2621c7f | 230 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
5e9e0efb | 231 | } |
cdeda28f | 232 | |
5e9e0efb | 233 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
cdeda28f SK |
234 | If this happens and we still have a DNS packet in the buffer, we re-send it. |
235 | This helps on DoD links, where frequently the packet which triggers dialling is | |
236 | a DNS query, which then gets lost. By re-sending, we can avoid the lookup | |
237 | failing. */ | |
5aabfc78 | 238 | static void nl_routechange(struct nlmsghdr *h) |
cdeda28f | 239 | { |
5e9e0efb | 240 | if (h->nlmsg_type == RTM_NEWROUTE && daemon->srv_save) |
cdeda28f | 241 | { |
5e9e0efb | 242 | struct rtmsg *rtm = NLMSG_DATA(h); |
3927da46 SK |
243 | int fd; |
244 | ||
245 | if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK) | |
246 | return; | |
247 | ||
248 | if (daemon->srv_save->sfd) | |
249 | fd = daemon->srv_save->sfd->fd; | |
250 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) | |
251 | fd = daemon->rfd_save->fd; | |
252 | else | |
253 | return; | |
254 | ||
255 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, | |
256 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); | |
cdeda28f SK |
257 | } |
258 | } | |
0a852541 SK |
259 | #endif |
260 | ||
261 |