| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 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, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 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. |
| 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/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_DHCP |
| 20 |
|
| 21 |
struct iface_param { |
| 22 |
struct dhcp_context *current; |
| 23 |
int ind; |
| 24 |
}; |
| 25 |
|
| 26 |
struct match_param { |
| 27 |
int ind, matched; |
| 28 |
struct in_addr netmask, broadcast, addr; |
| 29 |
}; |
| 30 |
|
| 31 |
static int complete_context(struct in_addr local, int if_index, char *label, |
| 32 |
struct in_addr netmask, struct in_addr broadcast, void *vparam); |
| 33 |
static int check_listen_addrs(struct in_addr local, int if_index, char *label, |
| 34 |
struct in_addr netmask, struct in_addr broadcast, void *vparam); |
| 35 |
|
| 36 |
static int make_fd(int port) |
| 37 |
{ |
| 38 |
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 39 |
struct sockaddr_in saddr; |
| 40 |
int oneopt = 1; |
| 41 |
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
| 42 |
int mtu = IP_PMTUDISC_DONT; |
| 43 |
#endif |
| 44 |
#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6) |
| 45 |
int tos = IPTOS_CLASS_CS6; |
| 46 |
#endif |
| 47 |
|
| 48 |
if (fd == -1) |
| 49 |
die (_("cannot create DHCP socket: %s"), NULL, EC_BADNET); |
| 50 |
|
| 51 |
if (!fix_fd(fd) || |
| 52 |
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
| 53 |
setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu)) == -1 || |
| 54 |
#endif |
| 55 |
#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6) |
| 56 |
setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1 || |
| 57 |
#endif |
| 58 |
#if defined(HAVE_LINUX_NETWORK) |
| 59 |
setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &oneopt, sizeof(oneopt)) == -1 || |
| 60 |
#else |
| 61 |
setsockopt(fd, IPPROTO_IP, IP_RECVIF, &oneopt, sizeof(oneopt)) == -1 || |
| 62 |
#endif |
| 63 |
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &oneopt, sizeof(oneopt)) == -1) |
| 64 |
die(_("failed to set options on DHCP socket: %s"), NULL, EC_BADNET); |
| 65 |
|
| 66 |
/* When bind-interfaces is set, there might be more than one dnsmasq |
| 67 |
instance binding port 67. That's OK if they serve different networks. |
| 68 |
Need to set REUSEADDR|REUSEPORT to make this possible. |
| 69 |
Handle the case that REUSEPORT is defined, but the kernel doesn't |
| 70 |
support it. This handles the introduction of REUSEPORT on Linux. */ |
| 71 |
if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND)) |
| 72 |
{ |
| 73 |
int rc = 0; |
| 74 |
|
| 75 |
#ifdef SO_REUSEPORT |
| 76 |
if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt))) == -1 && |
| 77 |
errno == ENOPROTOOPT) |
| 78 |
rc = 0; |
| 79 |
#endif |
| 80 |
|
| 81 |
if (rc != -1) |
| 82 |
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt)); |
| 83 |
|
| 84 |
if (rc == -1) |
| 85 |
die(_("failed to set SO_REUSE{ADDR|PORT} on DHCP socket: %s"), NULL, EC_BADNET); |
| 86 |
} |
| 87 |
|
| 88 |
memset(&saddr, 0, sizeof(saddr)); |
| 89 |
saddr.sin_family = AF_INET; |
| 90 |
saddr.sin_port = htons(port); |
| 91 |
saddr.sin_addr.s_addr = INADDR_ANY; |
| 92 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 93 |
saddr.sin_len = sizeof(struct sockaddr_in); |
| 94 |
#endif |
| 95 |
|
| 96 |
if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in))) |
| 97 |
die(_("failed to bind DHCP server socket: %s"), NULL, EC_BADNET); |
| 98 |
|
| 99 |
return fd; |
| 100 |
} |
| 101 |
|
| 102 |
void dhcp_init(void) |
| 103 |
{ |
| 104 |
#if defined(HAVE_BSD_NETWORK) |
| 105 |
int oneopt = 1; |
| 106 |
#endif |
| 107 |
|
| 108 |
daemon->dhcpfd = make_fd(daemon->dhcp_server_port); |
| 109 |
if (daemon->enable_pxe) |
| 110 |
daemon->pxefd = make_fd(PXE_PORT); |
| 111 |
else |
| 112 |
daemon->pxefd = -1; |
| 113 |
|
| 114 |
#if defined(HAVE_BSD_NETWORK) |
| 115 |
/* When we're not using capabilities, we need to do this here before |
| 116 |
we drop root. Also, set buffer size small, to avoid wasting |
| 117 |
kernel buffers */ |
| 118 |
|
| 119 |
if (option_bool(OPT_NO_PING)) |
| 120 |
daemon->dhcp_icmp_fd = -1; |
| 121 |
else if ((daemon->dhcp_icmp_fd = make_icmp_sock()) == -1 || |
| 122 |
setsockopt(daemon->dhcp_icmp_fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) == -1 ) |
| 123 |
die(_("cannot create ICMP raw socket: %s."), NULL, EC_BADNET); |
| 124 |
|
| 125 |
/* Make BPF raw send socket */ |
| 126 |
init_bpf(); |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
void dhcp_packet(time_t now, int pxe_fd) |
| 131 |
{ |
| 132 |
int fd = pxe_fd ? daemon->pxefd : daemon->dhcpfd; |
| 133 |
struct dhcp_packet *mess; |
| 134 |
struct dhcp_context *context; |
| 135 |
struct dhcp_relay *relay; |
| 136 |
int is_relay_reply = 0, is_relay_use_source = 0; |
| 137 |
struct iname *tmp; |
| 138 |
struct ifreq ifr; |
| 139 |
struct msghdr msg; |
| 140 |
struct sockaddr_in dest; |
| 141 |
struct cmsghdr *cmptr; |
| 142 |
struct iovec iov; |
| 143 |
ssize_t sz; |
| 144 |
int iface_index = 0, unicast_dest = 0, is_inform = 0, loopback = 0; |
| 145 |
int rcvd_iface_index, relay_index; |
| 146 |
struct in_addr iface_addr; |
| 147 |
struct iface_param parm; |
| 148 |
time_t recvtime = now; |
| 149 |
#ifdef HAVE_LINUX_NETWORK |
| 150 |
struct arpreq arp_req; |
| 151 |
struct timeval tv; |
| 152 |
struct in_addr dst_addr; |
| 153 |
#endif |
| 154 |
|
| 155 |
union { |
| 156 |
struct cmsghdr align; /* this ensures alignment */ |
| 157 |
#if defined(HAVE_LINUX_NETWORK) |
| 158 |
char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 159 |
#elif defined(HAVE_SOLARIS_NETWORK) |
| 160 |
char control[CMSG_SPACE(sizeof(unsigned int))]; |
| 161 |
#elif defined(HAVE_BSD_NETWORK) |
| 162 |
char control[CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 163 |
#endif |
| 164 |
} control_u; |
| 165 |
struct dhcp_bridge *bridge, *alias; |
| 166 |
|
| 167 |
msg.msg_controllen = sizeof(control_u); |
| 168 |
msg.msg_control = control_u.control; |
| 169 |
msg.msg_name = &dest; |
| 170 |
msg.msg_namelen = sizeof(dest); |
| 171 |
msg.msg_iov = &daemon->dhcp_packet; |
| 172 |
msg.msg_iovlen = 1; |
| 173 |
|
| 174 |
if ((sz = recv_dhcp_packet(fd, &msg)) == -1 || |
| 175 |
(sz < (ssize_t)(sizeof(*mess) - sizeof(mess->options)))) |
| 176 |
return; |
| 177 |
|
| 178 |
#if defined (HAVE_LINUX_NETWORK) |
| 179 |
if (ioctl(fd, SIOCGSTAMP, &tv) == 0) |
| 180 |
recvtime = tv.tv_sec; |
| 181 |
|
| 182 |
dst_addr.s_addr = 0; |
| 183 |
|
| 184 |
if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 185 |
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 186 |
if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
| 187 |
{ |
| 188 |
union { |
| 189 |
unsigned char *c; |
| 190 |
struct in_pktinfo *p; |
| 191 |
} p; |
| 192 |
p.c = CMSG_DATA(cmptr); |
| 193 |
iface_index = p.p->ipi_ifindex; |
| 194 |
dst_addr = p.p->ipi_addr; |
| 195 |
if (dst_addr.s_addr != INADDR_BROADCAST) |
| 196 |
unicast_dest = 1; |
| 197 |
} |
| 198 |
|
| 199 |
#elif defined(HAVE_BSD_NETWORK) |
| 200 |
if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 201 |
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 202 |
if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
| 203 |
{ |
| 204 |
union { |
| 205 |
unsigned char *c; |
| 206 |
struct sockaddr_dl *s; |
| 207 |
} p; |
| 208 |
p.c = CMSG_DATA(cmptr); |
| 209 |
iface_index = p.s->sdl_index; |
| 210 |
} |
| 211 |
|
| 212 |
#elif defined(HAVE_SOLARIS_NETWORK) |
| 213 |
if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 214 |
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 215 |
if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
| 216 |
{ |
| 217 |
union { |
| 218 |
unsigned char *c; |
| 219 |
unsigned int *i; |
| 220 |
} p; |
| 221 |
p.c = CMSG_DATA(cmptr); |
| 222 |
iface_index = *(p.i); |
| 223 |
} |
| 224 |
#endif |
| 225 |
|
| 226 |
#ifdef HAVE_DUMPFILE |
| 227 |
union mysockaddr *sockp = NULL; |
| 228 |
|
| 229 |
# ifdef HAVE_LINUX_NETWORK |
| 230 |
union mysockaddr tosock; |
| 231 |
|
| 232 |
sockp = &tosock; |
| 233 |
tosock.in.sin_port = htons(daemon->dhcp_server_port); |
| 234 |
tosock.in.sin_addr = dst_addr; |
| 235 |
tosock.sa.sa_family = AF_INET; |
| 236 |
# ifdef HAVE_SOCKADDR_SA_LEN |
| 237 |
tosock.in.sin_len = sizeof(struct sockaddr_in); |
| 238 |
# endif |
| 239 |
# endif |
| 240 |
|
| 241 |
dump_packet_udp(DUMP_DHCP, (void *)daemon->dhcp_packet.iov_base, sz, (union mysockaddr *)&dest, sockp, -1); |
| 242 |
#endif |
| 243 |
|
| 244 |
if (!indextoname(daemon->dhcpfd, iface_index, ifr.ifr_name) || |
| 245 |
ioctl(daemon->dhcpfd, SIOCGIFFLAGS, &ifr) != 0) |
| 246 |
return; |
| 247 |
|
| 248 |
mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
| 249 |
|
| 250 |
/* Non-standard extension: |
| 251 |
If giaddr == 255.255.255.255 we reply to the source |
| 252 |
address in the request packet header. This makes |
| 253 |
stand-alone leasequery clients easier, as they |
| 254 |
can leave source address determination to the kernel. |
| 255 |
In this case, set a flag and clear giaddr here, |
| 256 |
to avoid massive relay confusion. */ |
| 257 |
if (mess->giaddr.s_addr == INADDR_BROADCAST) |
| 258 |
{ |
| 259 |
mess->giaddr.s_addr = 0; |
| 260 |
is_relay_use_source = 1; |
| 261 |
} |
| 262 |
|
| 263 |
loopback = !mess->giaddr.s_addr && (ifr.ifr_flags & IFF_LOOPBACK); |
| 264 |
|
| 265 |
#ifdef HAVE_LINUX_NETWORK |
| 266 |
/* ARP fiddling uses original interface even if we pretend to use a different one. */ |
| 267 |
safe_strncpy(arp_req.arp_dev, ifr.ifr_name, sizeof(arp_req.arp_dev)); |
| 268 |
#endif |
| 269 |
|
| 270 |
/* If the interface on which the DHCP request was received is an |
| 271 |
alias of some other interface (as specified by the |
| 272 |
--bridge-interface option), change ifr.ifr_name so that we look |
| 273 |
for DHCP contexts associated with the aliased interface instead |
| 274 |
of with the aliasing one. */ |
| 275 |
rcvd_iface_index = iface_index; |
| 276 |
for (bridge = daemon->bridges; bridge; bridge = bridge->next) |
| 277 |
{ |
| 278 |
for (alias = bridge->alias; alias; alias = alias->next) |
| 279 |
if (wildcard_matchn(alias->iface, ifr.ifr_name, IF_NAMESIZE)) |
| 280 |
{ |
| 281 |
if (!(iface_index = if_nametoindex(bridge->iface))) |
| 282 |
{ |
| 283 |
my_syslog(MS_DHCP | LOG_WARNING, |
| 284 |
_("unknown interface %s in bridge-interface"), |
| 285 |
bridge->iface); |
| 286 |
return; |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
safe_strncpy(ifr.ifr_name, bridge->iface, sizeof(ifr.ifr_name)); |
| 291 |
break; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (alias) |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
#ifdef MSG_BCAST |
| 300 |
/* OpenBSD tells us when a packet was broadcast */ |
| 301 |
if (!(msg.msg_flags & MSG_BCAST)) |
| 302 |
unicast_dest = 1; |
| 303 |
#endif |
| 304 |
|
| 305 |
if ((relay_index = relay_reply4((struct dhcp_packet *)daemon->dhcp_packet.iov_base, (size_t)sz, ifr.ifr_name))) |
| 306 |
{ |
| 307 |
/* Reply from server, using us as relay. */ |
| 308 |
rcvd_iface_index = relay_index; |
| 309 |
if (!indextoname(daemon->dhcpfd, rcvd_iface_index, ifr.ifr_name)) |
| 310 |
return; |
| 311 |
is_relay_reply = 1; |
| 312 |
iov.iov_len = sz; |
| 313 |
#ifdef HAVE_LINUX_NETWORK |
| 314 |
safe_strncpy(arp_req.arp_dev, ifr.ifr_name, sizeof(arp_req.arp_dev)); |
| 315 |
#endif |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
ifr.ifr_addr.sa_family = AF_INET; |
| 320 |
if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) != -1 ) |
| 321 |
iface_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
| 322 |
else |
| 323 |
{ |
| 324 |
if (iface_check(AF_INET, NULL, ifr.ifr_name, NULL)) |
| 325 |
my_syslog(MS_DHCP | LOG_WARNING, _("DHCP packet received on %s which has no address"), ifr.ifr_name); |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 330 |
if (tmp->name && (tmp->flags & INAME_4) && wildcard_match(tmp->name, ifr.ifr_name)) |
| 331 |
return; |
| 332 |
|
| 333 |
/* unlinked contexts are marked by context->current == context */ |
| 334 |
for (context = daemon->dhcp; context; context = context->next) |
| 335 |
context->current = context; |
| 336 |
|
| 337 |
for (relay = daemon->relay4; relay; relay = relay->next) |
| 338 |
relay->matchcount = 0; |
| 339 |
|
| 340 |
parm.current = NULL; |
| 341 |
parm.ind = iface_index; |
| 342 |
|
| 343 |
if (!iface_check(AF_INET, (union all_addr *)&iface_addr, ifr.ifr_name, NULL)) |
| 344 |
{ |
| 345 |
/* If we failed to match the primary address of the interface, see if we've got a --listen-address |
| 346 |
for a secondary */ |
| 347 |
struct match_param match; |
| 348 |
|
| 349 |
match.matched = 0; |
| 350 |
match.ind = iface_index; |
| 351 |
|
| 352 |
if (!daemon->if_addrs || |
| 353 |
!iface_enumerate(AF_INET, &match, (callback_t){.af_inet=check_listen_addrs}) || |
| 354 |
!match.matched) |
| 355 |
return; |
| 356 |
|
| 357 |
iface_addr = match.addr; |
| 358 |
/* make sure secondary address gets priority in case |
| 359 |
there is more than one address on the interface in the same subnet */ |
| 360 |
complete_context(match.addr, iface_index, NULL, match.netmask, match.broadcast, &parm); |
| 361 |
} |
| 362 |
|
| 363 |
if (!iface_enumerate(AF_INET, &parm, (callback_t){.af_inet=complete_context})) |
| 364 |
return; |
| 365 |
|
| 366 |
relay_upstream4(iface_addr, iface_index, mess, (size_t)sz, unicast_dest); |
| 367 |
|
| 368 |
/* May have configured relay, but not DHCP server */ |
| 369 |
if (!daemon->dhcp) |
| 370 |
return; |
| 371 |
|
| 372 |
lease_prune(NULL, now); /* lose any expired leases */ |
| 373 |
iov.iov_len = dhcp_reply(parm.current, ifr.ifr_name, iface_index, (size_t)sz, now, unicast_dest, |
| 374 |
loopback, &is_inform, pxe_fd, iface_addr, recvtime, |
| 375 |
is_relay_use_source ? dest.sin_addr : mess->giaddr); |
| 376 |
lease_update_file(now); |
| 377 |
lease_update_dns(0); |
| 378 |
|
| 379 |
if (iov.iov_len == 0) |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
msg.msg_name = &dest; |
| 384 |
msg.msg_namelen = sizeof(dest); |
| 385 |
msg.msg_control = NULL; |
| 386 |
msg.msg_controllen = 0; |
| 387 |
msg.msg_iov = &iov; |
| 388 |
iov.iov_base = daemon->dhcp_packet.iov_base; |
| 389 |
|
| 390 |
/* packet buffer may have moved */ |
| 391 |
mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
| 392 |
|
| 393 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 394 |
dest.sin_len = sizeof(struct sockaddr_in); |
| 395 |
#endif |
| 396 |
|
| 397 |
if (pxe_fd) |
| 398 |
{ |
| 399 |
if (mess->ciaddr.s_addr != 0) |
| 400 |
dest.sin_addr = mess->ciaddr; |
| 401 |
} |
| 402 |
else if ((is_relay_use_source || mess->giaddr.s_addr) && !is_relay_reply) |
| 403 |
{ |
| 404 |
/* Send to BOOTP relay. */ |
| 405 |
if (is_relay_use_source) |
| 406 |
/* restore as-received value */ |
| 407 |
mess->giaddr.s_addr = INADDR_BROADCAST; |
| 408 |
else |
| 409 |
{ |
| 410 |
dest.sin_addr = mess->giaddr; |
| 411 |
dest.sin_port = htons(daemon->dhcp_server_port); |
| 412 |
} |
| 413 |
} |
| 414 |
else if (mess->ciaddr.s_addr) |
| 415 |
{ |
| 416 |
/* If the client's idea of its own address tallys with |
| 417 |
the source address in the request packet, we believe the |
| 418 |
source port too, and send back to that. If we're replying |
| 419 |
to a DHCPINFORM, trust the source address always. */ |
| 420 |
if ((!is_inform && dest.sin_addr.s_addr != mess->ciaddr.s_addr) || |
| 421 |
dest.sin_port == 0 || dest.sin_addr.s_addr == 0 || is_relay_reply) |
| 422 |
{ |
| 423 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 424 |
dest.sin_addr = mess->ciaddr; |
| 425 |
} |
| 426 |
} |
| 427 |
#if defined(HAVE_LINUX_NETWORK) |
| 428 |
else |
| 429 |
{ |
| 430 |
/* fill cmsg for outbound interface (both broadcast & unicast) */ |
| 431 |
struct in_pktinfo *pkt; |
| 432 |
msg.msg_control = control_u.control; |
| 433 |
msg.msg_controllen = sizeof(control_u); |
| 434 |
|
| 435 |
/* alignment padding passed to the kernel should not be uninitialised. */ |
| 436 |
memset(&control_u, 0, sizeof(control_u)); |
| 437 |
|
| 438 |
cmptr = CMSG_FIRSTHDR(&msg); |
| 439 |
pkt = (struct in_pktinfo *)CMSG_DATA(cmptr); |
| 440 |
pkt->ipi_ifindex = rcvd_iface_index; |
| 441 |
pkt->ipi_spec_dst.s_addr = 0; |
| 442 |
msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); |
| 443 |
cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 444 |
cmptr->cmsg_level = IPPROTO_IP; |
| 445 |
cmptr->cmsg_type = IP_PKTINFO; |
| 446 |
|
| 447 |
if ((ntohs(mess->flags) & 0x8000) || mess->hlen == 0 || |
| 448 |
mess->hlen > sizeof(ifr.ifr_addr.sa_data) || mess->htype == 0) |
| 449 |
{ |
| 450 |
/* broadcast to 255.255.255.255 (or mac address invalid) */ |
| 451 |
dest.sin_addr.s_addr = INADDR_BROADCAST; |
| 452 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 453 |
} |
| 454 |
else |
| 455 |
{ |
| 456 |
/* unicast to unconfigured client. Inject mac address direct into ARP cache. |
| 457 |
struct sockaddr limits size to 14 bytes. */ |
| 458 |
dest.sin_addr = mess->yiaddr; |
| 459 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 460 |
memcpy(&arp_req.arp_pa, &dest, sizeof(struct sockaddr_in)); |
| 461 |
arp_req.arp_ha.sa_family = mess->htype; |
| 462 |
memcpy(arp_req.arp_ha.sa_data, mess->chaddr, mess->hlen); |
| 463 |
/* interface name already copied in */ |
| 464 |
arp_req.arp_flags = ATF_COM; |
| 465 |
if (ioctl(daemon->dhcpfd, SIOCSARP, &arp_req) == -1) |
| 466 |
my_syslog(MS_DHCP | LOG_ERR, _("ARP-cache injection failed: %s"), strerror(errno)); |
| 467 |
} |
| 468 |
} |
| 469 |
#elif defined(HAVE_SOLARIS_NETWORK) |
| 470 |
else if ((ntohs(mess->flags) & 0x8000) || mess->hlen != ETHER_ADDR_LEN || mess->htype != ARPHRD_ETHER) |
| 471 |
{ |
| 472 |
/* broadcast to 255.255.255.255 (or mac address invalid) */ |
| 473 |
dest.sin_addr.s_addr = INADDR_BROADCAST; |
| 474 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 475 |
/* note that we don't specify the interface here: that's done by the |
| 476 |
IP_BOUND_IF sockopt lower down. */ |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
/* unicast to unconfigured client. Inject mac address direct into ARP cache. |
| 481 |
Note that this only works for ethernet on solaris, because we use SIOCSARP |
| 482 |
and not SIOCSXARP, which would be perfect, except that it returns ENXIO |
| 483 |
mysteriously. Bah. Fall back to broadcast for other net types. */ |
| 484 |
struct arpreq req; |
| 485 |
dest.sin_addr = mess->yiaddr; |
| 486 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 487 |
*((struct sockaddr_in *)&req.arp_pa) = dest; |
| 488 |
req.arp_ha.sa_family = AF_UNSPEC; |
| 489 |
memcpy(req.arp_ha.sa_data, mess->chaddr, mess->hlen); |
| 490 |
req.arp_flags = ATF_COM; |
| 491 |
ioctl(daemon->dhcpfd, SIOCSARP, &req); |
| 492 |
} |
| 493 |
#elif defined(HAVE_BSD_NETWORK) |
| 494 |
else |
| 495 |
{ |
| 496 |
#ifdef HAVE_DUMPFILE |
| 497 |
if (ntohs(mess->flags) & 0x8000) |
| 498 |
dest.sin_addr.s_addr = INADDR_BROADCAST; |
| 499 |
else |
| 500 |
dest.sin_addr = mess->yiaddr; |
| 501 |
dest.sin_port = htons(daemon->dhcp_client_port); |
| 502 |
|
| 503 |
dump_packet_udp(DUMP_DHCP, (void *)iov.iov_base, iov.iov_len, NULL, |
| 504 |
(union mysockaddr *)&dest, fd); |
| 505 |
#endif |
| 506 |
|
| 507 |
send_via_bpf(mess, iov.iov_len, iface_addr, &ifr); |
| 508 |
return; |
| 509 |
} |
| 510 |
#endif |
| 511 |
|
| 512 |
#ifdef HAVE_SOLARIS_NETWORK |
| 513 |
setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &iface_index, sizeof(iface_index)); |
| 514 |
#endif |
| 515 |
|
| 516 |
#ifdef HAVE_DUMPFILE |
| 517 |
dump_packet_udp(DUMP_DHCP, (void *)iov.iov_base, iov.iov_len, NULL, |
| 518 |
(union mysockaddr *)&dest, fd); |
| 519 |
#endif |
| 520 |
|
| 521 |
while(retry_send(sendmsg(fd, &msg, 0))); |
| 522 |
|
| 523 |
/* This can fail when, eg, iptables DROPS destination 255.255.255.255 */ |
| 524 |
if (errno != 0) |
| 525 |
{ |
| 526 |
inet_ntop(AF_INET, &dest.sin_addr, daemon->addrbuff, ADDRSTRLEN); |
| 527 |
my_syslog(MS_DHCP | LOG_WARNING, _("Error sending DHCP packet to %s: %s"), |
| 528 |
daemon->addrbuff, strerror(errno)); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/* check against secondary interface addresses */ |
| 533 |
static int check_listen_addrs(struct in_addr local, int if_index, char *label, |
| 534 |
struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 535 |
{ |
| 536 |
struct match_param *param = vparam; |
| 537 |
struct iname *tmp; |
| 538 |
|
| 539 |
(void) label; |
| 540 |
|
| 541 |
if (if_index == param->ind) |
| 542 |
{ |
| 543 |
for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 544 |
if ( tmp->addr.sa.sa_family == AF_INET && |
| 545 |
tmp->addr.in.sin_addr.s_addr == local.s_addr) |
| 546 |
{ |
| 547 |
param->matched = 1; |
| 548 |
param->addr = local; |
| 549 |
param->netmask = netmask; |
| 550 |
param->broadcast = broadcast; |
| 551 |
break; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
return 1; |
| 556 |
} |
| 557 |
|
| 558 |
/* This is a complex routine: it gets called with each (address,netmask,broadcast) triple |
| 559 |
of each interface (and any relay address) and does the following things: |
| 560 |
|
| 561 |
1) Discards stuff for interfaces other than the one on which a DHCP packet just arrived. |
| 562 |
2) Fills in any netmask and broadcast addresses which have not been explicitly configured. |
| 563 |
3) Fills in local (this host) and router (this host or relay) addresses. |
| 564 |
4) Links contexts which are valid for hosts directly connected to the arrival interface on ->current. |
| 565 |
|
| 566 |
Note that the current chain may be superseded later for configured hosts or those coming via gateways. */ |
| 567 |
|
| 568 |
static void guess_range_netmask(struct in_addr addr, struct in_addr netmask) |
| 569 |
{ |
| 570 |
struct dhcp_context *context; |
| 571 |
|
| 572 |
for (context = daemon->dhcp; context; context = context->next) |
| 573 |
if (!(context->flags & CONTEXT_NETMASK) && |
| 574 |
(is_same_net(addr, context->start, netmask) || |
| 575 |
is_same_net(addr, context->end, netmask))) |
| 576 |
{ |
| 577 |
if (context->netmask.s_addr != netmask.s_addr && |
| 578 |
!(is_same_net(addr, context->start, netmask) && |
| 579 |
is_same_net(addr, context->end, netmask))) |
| 580 |
{ |
| 581 |
inet_ntop(AF_INET, &context->start, daemon->dhcp_buff, DHCP_BUFF_SZ); |
| 582 |
inet_ntop(AF_INET, &context->end, daemon->dhcp_buff2, DHCP_BUFF_SZ); |
| 583 |
inet_ntop(AF_INET, &netmask, daemon->addrbuff, ADDRSTRLEN); |
| 584 |
my_syslog(MS_DHCP | LOG_WARNING, _("DHCP range %s -- %s is not consistent with netmask %s"), |
| 585 |
daemon->dhcp_buff, daemon->dhcp_buff2, daemon->addrbuff); |
| 586 |
} |
| 587 |
context->netmask = netmask; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
static int complete_context(struct in_addr local, int if_index, char *label, |
| 592 |
struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 593 |
{ |
| 594 |
struct dhcp_context *context; |
| 595 |
struct dhcp_relay *relay; |
| 596 |
struct iface_param *param = vparam; |
| 597 |
struct shared_network *share; |
| 598 |
|
| 599 |
(void)label; |
| 600 |
|
| 601 |
for (share = daemon->shared_networks; share; share = share->next) |
| 602 |
{ |
| 603 |
|
| 604 |
#ifdef HAVE_DHCP6 |
| 605 |
if (share->shared_addr.s_addr == 0) |
| 606 |
continue; |
| 607 |
#endif |
| 608 |
|
| 609 |
if (share->if_index != 0) |
| 610 |
{ |
| 611 |
if (share->if_index != if_index) |
| 612 |
continue; |
| 613 |
} |
| 614 |
else |
| 615 |
{ |
| 616 |
if (share->match_addr.s_addr != local.s_addr) |
| 617 |
continue; |
| 618 |
} |
| 619 |
|
| 620 |
for (context = daemon->dhcp; context; context = context->next) |
| 621 |
{ |
| 622 |
if (context->netmask.s_addr != 0 && |
| 623 |
is_same_net(share->shared_addr, context->start, context->netmask) && |
| 624 |
is_same_net(share->shared_addr, context->end, context->netmask)) |
| 625 |
{ |
| 626 |
/* link it onto the current chain if we've not seen it before */ |
| 627 |
if (context->current == context) |
| 628 |
{ |
| 629 |
/* For a shared network, we have no way to guess what the default route should be. */ |
| 630 |
context->router.s_addr = 0; |
| 631 |
context->local = local; /* Use configured address for Server Identifier */ |
| 632 |
context->current = param->current; |
| 633 |
param->current = context; |
| 634 |
} |
| 635 |
|
| 636 |
if (!(context->flags & CONTEXT_BRDCAST)) |
| 637 |
context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
guess_range_netmask(local, netmask); |
| 643 |
|
| 644 |
for (context = daemon->dhcp; context; context = context->next) |
| 645 |
{ |
| 646 |
if (context->netmask.s_addr != 0 && |
| 647 |
is_same_net(local, context->start, context->netmask) && |
| 648 |
is_same_net(local, context->end, context->netmask)) |
| 649 |
{ |
| 650 |
/* link it onto the current chain if we've not seen it before */ |
| 651 |
if (if_index == param->ind && context->current == context) |
| 652 |
{ |
| 653 |
context->router = local; |
| 654 |
context->local = local; |
| 655 |
context->current = param->current; |
| 656 |
param->current = context; |
| 657 |
} |
| 658 |
|
| 659 |
if (!(context->flags & CONTEXT_BRDCAST)) |
| 660 |
{ |
| 661 |
if (is_same_net(broadcast, context->start, context->netmask)) |
| 662 |
context->broadcast = broadcast; |
| 663 |
else |
| 664 |
context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr; |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
for (relay = daemon->relay4; relay; relay = relay->next) |
| 670 |
if (!relay->split_mode && relay->local.addr4.s_addr == local.s_addr) |
| 671 |
{ |
| 672 |
if (if_index == param->ind) |
| 673 |
relay->iface_index = if_index; |
| 674 |
|
| 675 |
/* More than one interface with the relay address breaks things. */ |
| 676 |
if (relay->matchcount++ == 1 && !relay->warned) |
| 677 |
{ |
| 678 |
relay->warned = 1; |
| 679 |
inet_ntop(AF_INET, &local, daemon->addrbuff, ADDRSTRLEN); |
| 680 |
my_syslog(MS_DHCP | LOG_WARNING, _("DHCP relay address %s appears on more than one interface"), daemon->addrbuff); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
return 1; |
| 685 |
} |
| 686 |
|
| 687 |
struct dhcp_context *address_available(struct dhcp_context *context, |
| 688 |
struct in_addr taddr, |
| 689 |
struct dhcp_netid *netids) |
| 690 |
{ |
| 691 |
/* Check is an address is OK for this network, check all |
| 692 |
possible ranges. Make sure that the address isn't in use |
| 693 |
by the server itself. */ |
| 694 |
|
| 695 |
unsigned int start, end, addr = ntohl(taddr.s_addr); |
| 696 |
struct dhcp_context *tmp; |
| 697 |
|
| 698 |
for (tmp = context; tmp; tmp = tmp->current) |
| 699 |
if (taddr.s_addr == tmp->router.s_addr) |
| 700 |
return NULL; |
| 701 |
|
| 702 |
for (tmp = context; tmp; tmp = tmp->current) |
| 703 |
{ |
| 704 |
start = ntohl(tmp->start.s_addr); |
| 705 |
end = ntohl(tmp->end.s_addr); |
| 706 |
|
| 707 |
if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_PROXY)) && |
| 708 |
addr >= start && |
| 709 |
addr <= end && |
| 710 |
match_netid(tmp->filter, netids, 1)) |
| 711 |
return tmp; |
| 712 |
} |
| 713 |
|
| 714 |
return NULL; |
| 715 |
} |
| 716 |
|
| 717 |
struct dhcp_context *narrow_context(struct dhcp_context *context, |
| 718 |
struct in_addr taddr, |
| 719 |
struct dhcp_netid *netids) |
| 720 |
{ |
| 721 |
/* We start of with a set of possible contexts, all on the current physical interface. |
| 722 |
These are chained on ->current. |
| 723 |
Here we have an address, and return the actual context corresponding to that |
| 724 |
address. Note that none may fit, if the address came a dhcp-host and is outside |
| 725 |
any dhcp-range. In that case we return a static range if possible, or failing that, |
| 726 |
any context on the correct subnet. (If there's more than one, this is a dodgy |
| 727 |
configuration: maybe there should be a warning.) */ |
| 728 |
|
| 729 |
struct dhcp_context *tmp; |
| 730 |
|
| 731 |
if (!(tmp = address_available(context, taddr, netids))) |
| 732 |
{ |
| 733 |
for (tmp = context; tmp; tmp = tmp->current) |
| 734 |
if (match_netid(tmp->filter, netids, 1) && |
| 735 |
is_same_net(taddr, tmp->start, tmp->netmask) && |
| 736 |
(tmp->flags & CONTEXT_STATIC)) |
| 737 |
break; |
| 738 |
|
| 739 |
if (!tmp) |
| 740 |
for (tmp = context; tmp; tmp = tmp->current) |
| 741 |
if (match_netid(tmp->filter, netids, 1) && |
| 742 |
is_same_net(taddr, tmp->start, tmp->netmask) && |
| 743 |
!(tmp->flags & CONTEXT_PROXY)) |
| 744 |
break; |
| 745 |
} |
| 746 |
|
| 747 |
/* Only one context allowed now */ |
| 748 |
if (tmp) |
| 749 |
tmp->current = NULL; |
| 750 |
|
| 751 |
return tmp; |
| 752 |
} |
| 753 |
|
| 754 |
struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr) |
| 755 |
{ |
| 756 |
struct dhcp_config *config; |
| 757 |
|
| 758 |
for (config = configs; config; config = config->next) |
| 759 |
if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
| 760 |
return config; |
| 761 |
|
| 762 |
return NULL; |
| 763 |
} |
| 764 |
|
| 765 |
/* Check if and address is in use by sending ICMP ping. |
| 766 |
This wrapper handles a cache and load-limiting. |
| 767 |
Return is NULL is address in use, or a pointer to a cache entry |
| 768 |
recording that it isn't. */ |
| 769 |
struct ping_result *do_icmp_ping(time_t now, struct in_addr addr, unsigned int hash, int loopback) |
| 770 |
{ |
| 771 |
static struct ping_result dummy; |
| 772 |
struct ping_result *r, *victim = NULL; |
| 773 |
int count, max = (int)(0.6 * (((float)PING_CACHE_TIME)/ |
| 774 |
((float)PING_WAIT))); |
| 775 |
|
| 776 |
/* check if we failed to ping addr sometime in the last |
| 777 |
PING_CACHE_TIME seconds. If so, assume the same situation still exists. |
| 778 |
This avoids problems when a stupid client bangs |
| 779 |
on us repeatedly. As a final check, if we did more |
| 780 |
than 60% of the possible ping checks in the last |
| 781 |
PING_CACHE_TIME, we are in high-load mode, so don't do any more. */ |
| 782 |
for (count = 0, r = daemon->ping_results; r; r = r->next) |
| 783 |
if (difftime(now, r->time) > (float)PING_CACHE_TIME) |
| 784 |
victim = r; /* old record */ |
| 785 |
else |
| 786 |
{ |
| 787 |
count++; |
| 788 |
if (r->addr.s_addr == addr.s_addr) |
| 789 |
return r; |
| 790 |
} |
| 791 |
|
| 792 |
/* didn't find cached entry */ |
| 793 |
if ((count >= max) || option_bool(OPT_NO_PING) || loopback) |
| 794 |
{ |
| 795 |
/* overloaded, or configured not to check, loopback interface, return "not in use" */ |
| 796 |
dummy.hash = hash; |
| 797 |
return &dummy; |
| 798 |
} |
| 799 |
else if (icmp_ping(addr)) |
| 800 |
return NULL; /* address in use. */ |
| 801 |
else |
| 802 |
{ |
| 803 |
/* at this point victim may hold an expired record */ |
| 804 |
if (!victim) |
| 805 |
{ |
| 806 |
if ((victim = whine_malloc(sizeof(struct ping_result)))) |
| 807 |
{ |
| 808 |
victim->next = daemon->ping_results; |
| 809 |
daemon->ping_results = victim; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
/* record that this address is OK for 30s |
| 814 |
without more ping checks */ |
| 815 |
if (victim) |
| 816 |
{ |
| 817 |
victim->addr = addr; |
| 818 |
victim->time = now; |
| 819 |
victim->hash = hash; |
| 820 |
} |
| 821 |
return victim; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
int address_allocate(struct dhcp_context *context, |
| 826 |
struct in_addr *addrp, unsigned char *hwaddr, int hw_len, |
| 827 |
struct dhcp_netid *netids, time_t now, int loopback) |
| 828 |
{ |
| 829 |
/* Find a free address: exclude anything in use and anything allocated to |
| 830 |
a particular hwaddr/clientid/hostname in our configuration. |
| 831 |
Try to return from contexts which match netids first. */ |
| 832 |
|
| 833 |
struct in_addr start, addr; |
| 834 |
struct dhcp_context *c, *d; |
| 835 |
int i, pass; |
| 836 |
unsigned int j; |
| 837 |
|
| 838 |
/* hash hwaddr: use the SDBM hashing algorithm. Seems to give good |
| 839 |
dispersal even with similarly-valued "strings". */ |
| 840 |
for (j = 0, i = 0; i < hw_len; i++) |
| 841 |
j = hwaddr[i] + (j << 6) + (j << 16) - j; |
| 842 |
|
| 843 |
/* j == 0 is marker */ |
| 844 |
if (j == 0) |
| 845 |
j = 1; |
| 846 |
|
| 847 |
for (pass = 0; pass <= 1; pass++) |
| 848 |
for (c = context; c; c = c->current) |
| 849 |
if (c->flags & (CONTEXT_STATIC | CONTEXT_PROXY)) |
| 850 |
continue; |
| 851 |
else if (!match_netid(c->filter, netids, pass)) |
| 852 |
continue; |
| 853 |
else |
| 854 |
{ |
| 855 |
if (option_bool(OPT_CONSEC_ADDR)) |
| 856 |
/* seed is largest extant lease addr in this context */ |
| 857 |
start = lease_find_max_addr(c); |
| 858 |
else |
| 859 |
/* pick a seed based on hwaddr */ |
| 860 |
start.s_addr = htonl(ntohl(c->start.s_addr) + |
| 861 |
((j + c->addr_epoch) % (1 + ntohl(c->end.s_addr) - ntohl(c->start.s_addr)))); |
| 862 |
|
| 863 |
/* iterate until we find a free address. */ |
| 864 |
addr = start; |
| 865 |
|
| 866 |
do { |
| 867 |
/* eliminate addresses in use by the server. */ |
| 868 |
for (d = context; d; d = d->current) |
| 869 |
if (addr.s_addr == d->router.s_addr) |
| 870 |
break; |
| 871 |
|
| 872 |
/* Addresses which end in .255 and .0 are broken in Windows even when using |
| 873 |
supernetting. ie dhcp-range=192.168.0.1,192.168.1.254,255,255,254.0 |
| 874 |
then 192.168.0.255 is a valid IP address, but not for Windows as it's |
| 875 |
in the class C range. See KB281579. We therefore don't allocate these |
| 876 |
addresses to avoid hard-to-diagnose problems. Thanks Bill. */ |
| 877 |
if (!d && |
| 878 |
!lease_find_by_addr(addr) && |
| 879 |
!config_find_by_address(daemon->dhcp_conf, addr) && |
| 880 |
(!IN_CLASSC(ntohl(addr.s_addr)) || |
| 881 |
((ntohl(addr.s_addr) & 0xff) != 0xff && ((ntohl(addr.s_addr) & 0xff) != 0x0)))) |
| 882 |
{ |
| 883 |
/* in consec-ip mode, skip addresses equal to |
| 884 |
the number of addresses rejected by clients. This |
| 885 |
should avoid the same client being offered the same |
| 886 |
address after it has rjected it. */ |
| 887 |
if (option_bool(OPT_CONSEC_ADDR) && c->addr_epoch) |
| 888 |
c->addr_epoch--; |
| 889 |
else |
| 890 |
{ |
| 891 |
struct ping_result *r; |
| 892 |
|
| 893 |
if ((r = do_icmp_ping(now, addr, j, loopback))) |
| 894 |
{ |
| 895 |
/* consec-ip mode: we offered this address for another client |
| 896 |
(different hash) recently, don't offer it to this one. */ |
| 897 |
if (!option_bool(OPT_CONSEC_ADDR) || r->hash == j) |
| 898 |
{ |
| 899 |
*addrp = addr; |
| 900 |
return 1; |
| 901 |
} |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
/* address in use: perturb address selection so that we are |
| 906 |
less likely to try this address again. */ |
| 907 |
if (!option_bool(OPT_CONSEC_ADDR)) |
| 908 |
c->addr_epoch++; |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
addr.s_addr = htonl(ntohl(addr.s_addr) + 1); |
| 914 |
|
| 915 |
if (addr.s_addr == htonl(ntohl(c->end.s_addr) + 1)) |
| 916 |
addr = c->start; |
| 917 |
|
| 918 |
} while (addr.s_addr != start.s_addr); |
| 919 |
} |
| 920 |
|
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
void dhcp_read_ethers(void) |
| 925 |
{ |
| 926 |
FILE *f = fopen(ETHERSFILE, "r"); |
| 927 |
unsigned int flags; |
| 928 |
size_t linesz = 0; |
| 929 |
char *line = NULL; |
| 930 |
char *ip, *cp; |
| 931 |
struct in_addr addr; |
| 932 |
unsigned char hwaddr[ETHER_ADDR_LEN]; |
| 933 |
struct dhcp_config **up, *tmp; |
| 934 |
struct dhcp_config *config; |
| 935 |
int count = 0, lineno = 0; |
| 936 |
|
| 937 |
addr.s_addr = 0; /* eliminate warning */ |
| 938 |
|
| 939 |
if (!f) |
| 940 |
{ |
| 941 |
my_syslog(MS_DHCP | LOG_ERR, _("failed to read %s: %s"), ETHERSFILE, strerror(errno)); |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
/* This can be called again on SIGHUP, so remove entries created last time round. */ |
| 946 |
for (up = &daemon->dhcp_conf, config = daemon->dhcp_conf; config; config = tmp) |
| 947 |
{ |
| 948 |
tmp = config->next; |
| 949 |
if (config->flags & CONFIG_FROM_ETHERS) |
| 950 |
{ |
| 951 |
*up = tmp; |
| 952 |
/* cannot have a clid */ |
| 953 |
if (config->flags & CONFIG_NAME) |
| 954 |
free(config->hostname); |
| 955 |
free(config->hwaddr); |
| 956 |
free(config); |
| 957 |
} |
| 958 |
else |
| 959 |
up = &config->next; |
| 960 |
} |
| 961 |
|
| 962 |
while (get_line_alloc(f, &line, &linesz)) |
| 963 |
{ |
| 964 |
char *host = NULL; |
| 965 |
|
| 966 |
lineno++; |
| 967 |
|
| 968 |
while (strlen(line) > 0 && isspace((unsigned char)line[strlen(line)-1])) |
| 969 |
line[strlen(line)-1] = 0; |
| 970 |
|
| 971 |
if ((*line == '#') || (*line == '+') || (*line == 0)) |
| 972 |
continue; |
| 973 |
|
| 974 |
for (ip = line; *ip && !isspace((unsigned char)*ip); ip++); |
| 975 |
for(; *ip && isspace((unsigned char)*ip); ip++) |
| 976 |
*ip = 0; |
| 977 |
if (!*ip || parse_hex(line, hwaddr, ETHER_ADDR_LEN, NULL, NULL) != ETHER_ADDR_LEN) |
| 978 |
{ |
| 979 |
my_syslog(MS_DHCP | LOG_ERR, _("bad line at %s line %d"), ETHERSFILE, lineno); |
| 980 |
continue; |
| 981 |
} |
| 982 |
|
| 983 |
/* check for name or dotted-quad */ |
| 984 |
for (cp = ip; *cp; cp++) |
| 985 |
if (!(*cp == '.' || (*cp >='0' && *cp <= '9'))) |
| 986 |
break; |
| 987 |
|
| 988 |
if (!*cp) |
| 989 |
{ |
| 990 |
if (inet_pton(AF_INET, ip, &addr.s_addr) < 1) |
| 991 |
{ |
| 992 |
my_syslog(MS_DHCP | LOG_ERR, _("bad address at %s line %d"), ETHERSFILE, lineno); |
| 993 |
continue; |
| 994 |
} |
| 995 |
|
| 996 |
flags = CONFIG_ADDR; |
| 997 |
|
| 998 |
for (config = daemon->dhcp_conf; config; config = config->next) |
| 999 |
if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
else |
| 1003 |
{ |
| 1004 |
int nomem; |
| 1005 |
if (!(host = canonicalise(ip, &nomem)) || !legal_hostname(host)) |
| 1006 |
{ |
| 1007 |
if (!nomem) |
| 1008 |
my_syslog(MS_DHCP | LOG_ERR, _("bad name at %s line %d"), ETHERSFILE, lineno); |
| 1009 |
free(host); |
| 1010 |
continue; |
| 1011 |
} |
| 1012 |
|
| 1013 |
flags = CONFIG_NAME; |
| 1014 |
|
| 1015 |
for (config = daemon->dhcp_conf; config; config = config->next) |
| 1016 |
if ((config->flags & CONFIG_NAME) && hostname_isequal(config->hostname, host)) |
| 1017 |
break; |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (config && (config->flags & CONFIG_FROM_ETHERS)) |
| 1021 |
{ |
| 1022 |
my_syslog(MS_DHCP | LOG_ERR, _("ignoring %s line %d, duplicate name or IP address"), ETHERSFILE, lineno); |
| 1023 |
continue; |
| 1024 |
} |
| 1025 |
|
| 1026 |
if (!config) |
| 1027 |
{ |
| 1028 |
for (config = daemon->dhcp_conf; config; config = config->next) |
| 1029 |
{ |
| 1030 |
struct hwaddr_config *conf_addr = config->hwaddr; |
| 1031 |
if (conf_addr && |
| 1032 |
conf_addr->next == NULL && |
| 1033 |
conf_addr->wildcard_mask == 0 && |
| 1034 |
conf_addr->hwaddr_len == ETHER_ADDR_LEN && |
| 1035 |
(conf_addr->hwaddr_type == ARPHRD_ETHER || conf_addr->hwaddr_type == 0) && |
| 1036 |
memcmp(conf_addr->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0) |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
|
| 1040 |
if (!config) |
| 1041 |
{ |
| 1042 |
if (!(config = whine_malloc(sizeof(struct dhcp_config)))) |
| 1043 |
continue; |
| 1044 |
config->flags = CONFIG_FROM_ETHERS; |
| 1045 |
config->hwaddr = NULL; |
| 1046 |
config->domain = NULL; |
| 1047 |
config->netid = NULL; |
| 1048 |
config->next = daemon->dhcp_conf; |
| 1049 |
daemon->dhcp_conf = config; |
| 1050 |
} |
| 1051 |
|
| 1052 |
config->flags |= flags; |
| 1053 |
|
| 1054 |
if (flags & CONFIG_NAME) |
| 1055 |
{ |
| 1056 |
config->hostname = host; |
| 1057 |
host = NULL; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (flags & CONFIG_ADDR) |
| 1061 |
config->addr = addr; |
| 1062 |
} |
| 1063 |
|
| 1064 |
config->flags |= CONFIG_NOCLID; |
| 1065 |
if (!config->hwaddr) |
| 1066 |
config->hwaddr = whine_malloc(sizeof(struct hwaddr_config)); |
| 1067 |
if (config->hwaddr) |
| 1068 |
{ |
| 1069 |
memcpy(config->hwaddr->hwaddr, hwaddr, ETHER_ADDR_LEN); |
| 1070 |
config->hwaddr->hwaddr_len = ETHER_ADDR_LEN; |
| 1071 |
config->hwaddr->hwaddr_type = ARPHRD_ETHER; |
| 1072 |
config->hwaddr->wildcard_mask = 0; |
| 1073 |
config->hwaddr->next = NULL; |
| 1074 |
} |
| 1075 |
count++; |
| 1076 |
|
| 1077 |
free(host); |
| 1078 |
|
| 1079 |
} |
| 1080 |
|
| 1081 |
fclose(f); |
| 1082 |
|
| 1083 |
my_syslog(MS_DHCP | LOG_INFO, _("read %s - %d addresses"), ETHERSFILE, count); |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
/* If we've not found a hostname any other way, try and see if there's one in /etc/hosts |
| 1088 |
for this address. If it has a domain part, that must match the set domain and |
| 1089 |
it gets stripped. The set of legal domain names is bigger than the set of legal hostnames |
| 1090 |
so check here that the domain name is legal as a hostname. |
| 1091 |
NOTE: we're only allowed to overwrite daemon->dhcp_buff if we succeed. */ |
| 1092 |
char *host_from_dns(struct in_addr addr) |
| 1093 |
{ |
| 1094 |
struct crec *lookup; |
| 1095 |
|
| 1096 |
if (daemon->port == 0) |
| 1097 |
return NULL; /* DNS disabled. */ |
| 1098 |
|
| 1099 |
lookup = cache_find_by_addr(NULL, (union all_addr *)&addr, 0, F_IPV4); |
| 1100 |
|
| 1101 |
if (lookup && (lookup->flags & F_HOSTS)) |
| 1102 |
{ |
| 1103 |
char *dot, *hostname = cache_get_name(lookup); |
| 1104 |
dot = strchr(hostname, '.'); |
| 1105 |
|
| 1106 |
if (dot && strlen(dot+1) != 0) |
| 1107 |
{ |
| 1108 |
char *d2 = get_domain(addr); |
| 1109 |
if (!d2 || !hostname_isequal(dot+1, d2)) |
| 1110 |
return NULL; /* wrong domain */ |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (!legal_hostname(hostname)) |
| 1114 |
return NULL; |
| 1115 |
|
| 1116 |
safe_strncpy(daemon->dhcp_buff, hostname, 256); |
| 1117 |
strip_hostname(daemon->dhcp_buff); |
| 1118 |
|
| 1119 |
return daemon->dhcp_buff; |
| 1120 |
} |
| 1121 |
|
| 1122 |
return NULL; |
| 1123 |
} |
| 1124 |
|
| 1125 |
#endif |