Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2006 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 |
| 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 Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 13 | #include "dnsmasq.h" |
| 14 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 15 | #ifdef HAVE_LINUX_NETWORK |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 16 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 17 | #include <linux/types.h> |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 18 | #include <linux/netlink.h> |
| 19 | #include <linux/rtnetlink.h> |
| 20 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 21 | /* 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 Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 29 | static struct iovec iov; |
| 30 | |
| 31 | static void nl_err(struct nlmsghdr *h); |
| 32 | static void nl_routechange(struct daemon *daemon, struct nlmsghdr *h); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 33 | |
| 34 | void netlink_init(struct daemon *daemon) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 35 | { |
| 36 | struct sockaddr_nl addr; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 37 | |
| 38 | addr.nl_family = AF_NETLINK; |
| 39 | addr.nl_pad = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 40 | addr.nl_pid = 0; /* autobind */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 41 | #ifdef HAVE_IPV6 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 42 | addr.nl_groups = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 43 | #else |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 44 | addr.nl_groups = RTMGRP_IPV4_ROUTE; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 45 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 46 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 47 | /* 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 Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 59 | 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 Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 67 | iov.iov_len = 200; |
| 68 | iov.iov_base = safe_malloc(iov.iov_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 71 | static ssize_t netlink_recv(struct daemon *daemon) |
| 72 | { |
| 73 | struct msghdr msg; |
| 74 | ssize_t rc; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 75 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 76 | msg.msg_control = NULL; |
| 77 | msg.msg_controllen = 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 78 | msg.msg_name = NULL; |
| 79 | msg.msg_namelen = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 80 | msg.msg_iov = &iov; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 81 | msg.msg_iovlen = 1; |
| 82 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 83 | while (1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 84 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 85 | 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 Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 101 | return -1; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 102 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 103 | |
| 104 | /* finally, read it for real */ |
| 105 | while ((rc = recvmsg(daemon->netlinkfd, &msg, 0)) == -1 && errno == EINTR); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 106 | |
| 107 | return rc; |
| 108 | } |
| 109 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 110 | int iface_enumerate(struct daemon *daemon, void *parm, int (*ipv4_callback)(), int (*ipv6_callback)()) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 111 | { |
| 112 | struct sockaddr_nl addr; |
| 113 | struct nlmsghdr *h; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 114 | ssize_t len; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 115 | static unsigned int seq = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | int family = AF_INET; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 117 | |
| 118 | struct { |
| 119 | struct nlmsghdr nlh; |
| 120 | struct rtgenmsg g; |
| 121 | } req; |
| 122 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 123 | 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 Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 128 | again: |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 129 | req.nlh.nlmsg_len = sizeof(req); |
| 130 | req.nlh.nlmsg_type = RTM_GETADDR; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 131 | 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] | 132 | req.nlh.nlmsg_pid = 0; |
| 133 | req.nlh.nlmsg_seq = ++seq; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 134 | req.g.rtgen_family = family; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 135 | |
| 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 Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 139 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 140 | if (len == -1) |
| 141 | return 0; |
| 142 | |
| 143 | while (1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 144 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 145 | if ((len = netlink_recv(daemon)) == -1) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 146 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 147 | |
| 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 Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 210 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 213 | void netlink_multicast(struct daemon *daemon) |
| 214 | { |
| 215 | ssize_t len; |
| 216 | struct nlmsghdr *h; |
| 217 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 218 | if ((len = netlink_recv(daemon)) != -1) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 219 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 220 | 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 Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 228 | static void nl_err(struct nlmsghdr *h) |
| 229 | { |
| 230 | struct nlmsgerr *err = NLMSG_DATA(h); |
| 231 | if (err->error != 0) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 232 | syslog(LOG_ERR, _("netlink returns error: %s"), strerror(-(err->error))); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 233 | } |
| 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. */ |
| 240 | static 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 Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 251 | #endif |
| 252 | |
| 253 | |