Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2007 Simon Kelley |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 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 |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 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/>. |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 19 | #ifdef HAVE_LINUX_NETWORK |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 20 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 21 | #include <linux/types.h> |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 22 | #include <linux/netlink.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 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 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 33 | static struct iovec iov; |
| 34 | |
| 35 | static void nl_err(struct nlmsghdr *h); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 36 | static void nl_routechange(struct nlmsghdr *h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 37 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 38 | void netlink_init(void) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 39 | { |
| 40 | struct sockaddr_nl addr; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 41 | |
| 42 | addr.nl_family = AF_NETLINK; |
| 43 | addr.nl_pad = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 44 | addr.nl_pid = 0; /* autobind */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 45 | #ifdef HAVE_IPV6 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 46 | addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 47 | #else |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 48 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 49 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 50 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 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) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 63 | die(_("cannot create netlink socket: %s"), NULL, EC_MISC); |
| 64 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 65 | iov.iov_len = 200; |
| 66 | iov.iov_base = safe_malloc(iov.iov_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 69 | static ssize_t netlink_recv(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 70 | { |
| 71 | struct msghdr msg; |
| 72 | ssize_t rc; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 73 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 74 | msg.msg_control = NULL; |
| 75 | msg.msg_controllen = 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 76 | msg.msg_name = NULL; |
| 77 | msg.msg_namelen = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 78 | msg.msg_iov = &iov; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 79 | msg.msg_iovlen = 1; |
| 80 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 81 | while (1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 82 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 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)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 99 | return -1; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 100 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 101 | |
| 102 | /* finally, read it for real */ |
| 103 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 104 | |
| 105 | return rc; |
| 106 | } |
| 107 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 108 | int iface_enumerate(void *parm, int (*ipv4_callback)(), int (*ipv6_callback)()) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 109 | { |
| 110 | struct sockaddr_nl addr; |
| 111 | struct nlmsghdr *h; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 112 | ssize_t len; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 113 | static unsigned int seq = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 114 | int family = AF_INET; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 115 | |
| 116 | struct { |
| 117 | struct nlmsghdr nlh; |
| 118 | struct rtgenmsg g; |
| 119 | } req; |
| 120 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 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 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 126 | again: |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 127 | req.nlh.nlmsg_len = sizeof(req); |
| 128 | req.nlh.nlmsg_type = RTM_GETADDR; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 129 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST | NLM_F_ACK; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 130 | req.nlh.nlmsg_pid = 0; |
| 131 | req.nlh.nlmsg_seq = ++seq; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 132 | req.g.rtgen_family = family; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 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()); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 137 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 138 | if (len == -1) |
| 139 | return 0; |
| 140 | |
| 141 | while (1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 142 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 143 | if ((len = netlink_recv()) == -1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 144 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 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) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 150 | nl_routechange(h); /* May be multicast arriving async */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 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) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 187 | if (!((*ipv4_callback)(addr, ifa->ifa_index, netmask, broadcast, parm))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 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) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 203 | if (!((*ipv6_callback)(addrp, ifa->ifa_index, ifa->ifa_index, parm))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | #endif |
| 207 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 208 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 211 | void netlink_multicast(void) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 212 | { |
| 213 | ssize_t len; |
| 214 | struct nlmsghdr *h; |
| 215 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 216 | if ((len = netlink_recv()) != -1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 217 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 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 |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 222 | nl_routechange(h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 226 | static void nl_err(struct nlmsghdr *h) |
| 227 | { |
| 228 | struct nlmsgerr *err = NLMSG_DATA(h); |
| 229 | if (err->error != 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 230 | my_syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* We arrange to receive netlink multicast messages whenever the network route is added. |
| 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. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 238 | static void nl_routechange(struct nlmsghdr *h) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 239 | { |
| 240 | if (h->nlmsg_type == RTM_NEWROUTE && daemon->srv_save) |
| 241 | { |
| 242 | struct rtmsg *rtm = NLMSG_DATA(h); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame^] | 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()); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 259 | #endif |
| 260 | |
| 261 | |