Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2011 Simon Kelley |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [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 | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [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 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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 | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 19 | #if defined(HAVE_BSD_NETWORK) || defined(HAVE_SOLARIS_NETWORK) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 20 | |
| 21 | static struct iovec ifconf = { |
| 22 | .iov_base = NULL, |
| 23 | .iov_len = 0 |
| 24 | }; |
| 25 | |
| 26 | static struct iovec ifreq = { |
| 27 | .iov_base = NULL, |
| 28 | .iov_len = 0 |
| 29 | }; |
| 30 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 31 | #if defined(HAVE_BSD_NETWORK) && !defined(__APPLE__) |
| 32 | |
| 33 | #include <sys/sysctl.h> |
| 34 | #include <net/route.h> |
| 35 | #include <net/if_dl.h> |
| 36 | #include <netinet/if_ether.h> |
| 37 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame^] | 38 | #ifndef SA_SIZE |
| 39 | #define SA_SIZE(sa) \ |
| 40 | ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \ |
| 41 | sizeof(long) : \ |
| 42 | 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) ) |
| 43 | #endif |
| 44 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 45 | int arp_enumerate(void *parm, int (*callback)()) |
| 46 | { |
| 47 | int mib[6]; |
| 48 | size_t needed; |
| 49 | char *next; |
| 50 | struct rt_msghdr *rtm; |
| 51 | struct sockaddr_inarp *sin2; |
| 52 | struct sockaddr_dl *sdl; |
| 53 | int rc; |
| 54 | |
| 55 | mib[0] = CTL_NET; |
| 56 | mib[1] = PF_ROUTE; |
| 57 | mib[2] = 0; |
| 58 | mib[3] = AF_INET; |
| 59 | mib[4] = NET_RT_FLAGS; |
| 60 | #ifdef RTF_LLINFO |
| 61 | mib[5] = RTF_LLINFO; |
| 62 | #else |
| 63 | mib[5] = 0; |
| 64 | #endif |
| 65 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1 || needed == 0) |
| 66 | return 0; |
| 67 | |
| 68 | while (1) |
| 69 | { |
| 70 | if (!expand_buf(&ifconf, needed)) |
| 71 | return 0; |
| 72 | if ((rc = sysctl(mib, 6, ifconf.iov_base, &needed, NULL, 0)) == 0 || |
| 73 | errno != ENOMEM) |
| 74 | break; |
| 75 | needed += needed / 8; |
| 76 | } |
| 77 | if (rc == -1) |
| 78 | return 0; |
| 79 | |
| 80 | for (next = ifconf.iov_base ; next < (char *)ifconf.iov_base + needed; next += rtm->rtm_msglen) |
| 81 | { |
| 82 | rtm = (struct rt_msghdr *)next; |
| 83 | sin2 = (struct sockaddr_inarp *)(rtm + 1); |
| 84 | sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); |
| 85 | if (!(*callback)(AF_INET, &sin2->sin_addr, LLADDR(sdl), sdl->sdl_alen, parm)) |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | |
| 95 | int iface_enumerate(int family, void *parm, int (*callback)()) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 96 | { |
| 97 | char *ptr; |
| 98 | struct ifreq *ifr; |
| 99 | struct ifconf ifc; |
| 100 | int fd, errsav, ret = 0; |
| 101 | int lastlen = 0; |
| 102 | size_t len = 0; |
| 103 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 104 | if (family == AF_UNSPEC) |
| 105 | #if defined(HAVE_BSD_NETWORK) && !defined(__APPLE__) |
| 106 | return arp_enumerate(parm, callback); |
| 107 | #else |
| 108 | return 0; /* need code for Solaris and MacOS*/ |
| 109 | #endif |
| 110 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 111 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
| 112 | return 0; |
| 113 | |
| 114 | while(1) |
| 115 | { |
| 116 | len += 10*sizeof(struct ifreq); |
| 117 | |
| 118 | if (!expand_buf(&ifconf, len)) |
| 119 | goto err; |
| 120 | |
| 121 | ifc.ifc_len = len; |
| 122 | ifc.ifc_buf = ifconf.iov_base; |
| 123 | |
| 124 | if (ioctl(fd, SIOCGIFCONF, &ifc) == -1) |
| 125 | { |
| 126 | if (errno != EINVAL || lastlen != 0) |
| 127 | goto err; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | if (ifc.ifc_len == lastlen) |
| 132 | break; /* got a big enough buffer now */ |
| 133 | lastlen = ifc.ifc_len; |
| 134 | } |
| 135 | } |
| 136 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 137 | for (ptr = ifc.ifc_buf; ptr < (char *)(ifc.ifc_buf + ifc.ifc_len); ptr += len) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 138 | { |
| 139 | /* subsequent entries may not be aligned, so copy into |
| 140 | an aligned buffer to avoid nasty complaints about |
| 141 | unaligned accesses. */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 142 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 143 | len = sizeof(struct ifreq); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 144 | |
| 145 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 146 | ifr = (struct ifreq *)ptr; |
| 147 | if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_ifru)) |
| 148 | len = ifr->ifr_addr.sa_len + offsetof(struct ifreq, ifr_ifru); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 149 | #endif |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 150 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 151 | if (!expand_buf(&ifreq, len)) |
| 152 | goto err; |
| 153 | |
| 154 | ifr = (struct ifreq *)ifreq.iov_base; |
| 155 | memcpy(ifr, ptr, len); |
| 156 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 157 | if (ifr->ifr_addr.sa_family == family) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 158 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 159 | if (family == AF_INET) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 160 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 161 | struct in_addr addr, netmask, broadcast; |
| 162 | broadcast.s_addr = 0; |
| 163 | addr = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr; |
| 164 | if (ioctl(fd, SIOCGIFNETMASK, ifr) == -1) |
| 165 | continue; |
| 166 | netmask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr; |
| 167 | if (ioctl(fd, SIOCGIFBRDADDR, ifr) != -1) |
| 168 | broadcast = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr; |
| 169 | if (!((*callback)(addr, |
| 170 | (int)if_nametoindex(ifr->ifr_name), |
| 171 | netmask, broadcast, |
| 172 | parm))) |
| 173 | goto err; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 174 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 175 | #ifdef HAVE_IPV6 |
| 176 | else if (family == AF_INET6) |
| 177 | { |
| 178 | struct in6_addr *addr = &((struct sockaddr_in6 *)&ifr->ifr_addr)->sin6_addr; |
| 179 | /* voodoo to clear interface field in address */ |
| 180 | if (!option_bool(OPT_NOWILD) && IN6_IS_ADDR_LINKLOCAL(addr)) |
| 181 | { |
| 182 | addr->s6_addr[2] = 0; |
| 183 | addr->s6_addr[3] = 0; |
| 184 | } |
| 185 | if (!((*callback)(addr, |
| 186 | (int)((struct sockaddr_in6 *)&ifr->ifr_addr)->sin6_scope_id, |
| 187 | (int)if_nametoindex(ifr->ifr_name), |
| 188 | parm))) |
| 189 | goto err; |
| 190 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 191 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 192 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | ret = 1; |
| 196 | |
| 197 | err: |
| 198 | errsav = errno; |
| 199 | close(fd); |
| 200 | errno = errsav; |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 207 | #if defined(HAVE_BSD_NETWORK) && defined(HAVE_DHCP) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 208 | #include <net/bpf.h> |
| 209 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 210 | void init_bpf(void) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 211 | { |
| 212 | int i = 0; |
| 213 | |
| 214 | while (1) |
| 215 | { |
| 216 | /* useful size which happens to be sufficient */ |
| 217 | if (expand_buf(&ifreq, sizeof(struct ifreq))) |
| 218 | { |
| 219 | sprintf(ifreq.iov_base, "/dev/bpf%d", i++); |
| 220 | if ((daemon->dhcp_raw_fd = open(ifreq.iov_base, O_RDWR, 0)) != -1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 221 | return; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 222 | } |
| 223 | if (errno != EBUSY) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 224 | die(_("cannot create DHCP BPF socket: %s"), NULL, EC_BADNET); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 228 | void send_via_bpf(struct dhcp_packet *mess, size_t len, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 229 | struct in_addr iface_addr, struct ifreq *ifr) |
| 230 | { |
| 231 | /* Hairy stuff, packet either has to go to the |
| 232 | net broadcast or the destination can't reply to ARP yet, |
| 233 | but we do know the physical address. |
| 234 | Build the packet by steam, and send directly, bypassing |
| 235 | the kernel IP stack */ |
| 236 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 237 | struct ether_header ether; |
| 238 | struct ip ip; |
| 239 | struct udphdr { |
| 240 | u16 uh_sport; /* source port */ |
| 241 | u16 uh_dport; /* destination port */ |
| 242 | u16 uh_ulen; /* udp length */ |
| 243 | u16 uh_sum; /* udp checksum */ |
| 244 | } udp; |
| 245 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 246 | u32 i, sum; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 247 | struct iovec iov[4]; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 248 | |
| 249 | /* Only know how to do ethernet on *BSD */ |
| 250 | if (mess->htype != ARPHRD_ETHER || mess->hlen != ETHER_ADDR_LEN) |
| 251 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 252 | my_syslog(MS_DHCP | LOG_WARNING, _("DHCP request for unsupported hardware type (%d) received on %s"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 253 | mess->htype, ifr->ifr_name); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
| 257 | ifr->ifr_addr.sa_family = AF_LINK; |
| 258 | if (ioctl(daemon->dhcpfd, SIOCGIFADDR, ifr) < 0) |
| 259 | return; |
| 260 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 261 | memcpy(ether.ether_shost, LLADDR((struct sockaddr_dl *)&ifr->ifr_addr), ETHER_ADDR_LEN); |
| 262 | ether.ether_type = htons(ETHERTYPE_IP); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 263 | |
| 264 | if (ntohs(mess->flags) & 0x8000) |
| 265 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 266 | memset(ether.ether_dhost, 255, ETHER_ADDR_LEN); |
| 267 | ip.ip_dst.s_addr = INADDR_BROADCAST; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 268 | } |
| 269 | else |
| 270 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 271 | memcpy(ether.ether_dhost, mess->chaddr, ETHER_ADDR_LEN); |
| 272 | ip.ip_dst.s_addr = mess->yiaddr.s_addr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 275 | ip.ip_p = IPPROTO_UDP; |
| 276 | ip.ip_src.s_addr = iface_addr.s_addr; |
| 277 | ip.ip_len = htons(sizeof(struct ip) + |
| 278 | sizeof(struct udphdr) + |
| 279 | len) ; |
| 280 | ip.ip_hl = sizeof(struct ip) / 4; |
| 281 | ip.ip_v = IPVERSION; |
| 282 | ip.ip_tos = 0; |
| 283 | ip.ip_id = htons(0); |
| 284 | ip.ip_off = htons(0x4000); /* don't fragment */ |
| 285 | ip.ip_ttl = IPDEFTTL; |
| 286 | ip.ip_sum = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 287 | for (sum = 0, i = 0; i < sizeof(struct ip) / 2; i++) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 288 | sum += ((u16 *)&ip)[i]; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 289 | while (sum>>16) |
| 290 | sum = (sum & 0xffff) + (sum >> 16); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 291 | ip.ip_sum = (sum == 0xffff) ? sum : ~sum; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 292 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 293 | udp.uh_sport = htons(daemon->dhcp_server_port); |
| 294 | udp.uh_dport = htons(daemon->dhcp_client_port); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 295 | if (len & 1) |
| 296 | ((char *)mess)[len] = 0; /* for checksum, in case length is odd. */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 297 | udp.uh_sum = 0; |
| 298 | udp.uh_ulen = sum = htons(sizeof(struct udphdr) + len); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 299 | sum += htons(IPPROTO_UDP); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 300 | sum += ip.ip_src.s_addr & 0xffff; |
| 301 | sum += (ip.ip_src.s_addr >> 16) & 0xffff; |
| 302 | sum += ip.ip_dst.s_addr & 0xffff; |
| 303 | sum += (ip.ip_dst.s_addr >> 16) & 0xffff; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 304 | for (i = 0; i < sizeof(struct udphdr)/2; i++) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 305 | sum += ((u16 *)&udp)[i]; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 306 | for (i = 0; i < (len + 1) / 2; i++) |
| 307 | sum += ((u16 *)mess)[i]; |
| 308 | while (sum>>16) |
| 309 | sum = (sum & 0xffff) + (sum >> 16); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 310 | udp.uh_sum = (sum == 0xffff) ? sum : ~sum; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 311 | |
| 312 | ioctl(daemon->dhcp_raw_fd, BIOCSETIF, ifr); |
| 313 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 314 | iov[0].iov_base = ðer; |
| 315 | iov[0].iov_len = sizeof(ether); |
| 316 | iov[1].iov_base = &ip; |
| 317 | iov[1].iov_len = sizeof(ip); |
| 318 | iov[2].iov_base = &udp; |
| 319 | iov[2].iov_len = sizeof(udp); |
| 320 | iov[3].iov_base = mess; |
| 321 | iov[3].iov_len = len; |
| 322 | |
| 323 | while (writev(daemon->dhcp_raw_fd, iov, 4) == -1 && retry_send()); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 326 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 327 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 328 | |