PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/external/dnsmasq/src/dhcp.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C | 1002 lines | 743 code | 146 blank | 113 comment | 305 complexity | dd735fba78454f3efd93566851c12e7b MD5 | raw file
  1. /* dnsmasq is Copyright (c) 2000-2009 Simon Kelley
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 dated June, 1991, or
  5. (at your option) version 3 dated 29 June, 2007.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "dnsmasq.h"
  14. #ifdef HAVE_DHCP
  15. struct iface_param {
  16. struct in_addr relay, primary;
  17. struct dhcp_context *current;
  18. int ind;
  19. };
  20. static int complete_context(struct in_addr local, int if_index,
  21. struct in_addr netmask, struct in_addr broadcast, void *vparam);
  22. void dhcp_init(void)
  23. {
  24. int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  25. struct sockaddr_in saddr;
  26. int oneopt = 1;
  27. #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
  28. int mtu = IP_PMTUDISC_DONT;
  29. #endif
  30. if (fd == -1)
  31. die (_("cannot create DHCP socket: %s"), NULL, EC_BADNET);
  32. if (!fix_fd(fd) ||
  33. #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
  34. setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu)) == -1 ||
  35. #endif
  36. #if defined(HAVE_LINUX_NETWORK)
  37. setsockopt(fd, SOL_IP, IP_PKTINFO, &oneopt, sizeof(oneopt)) == -1 ||
  38. #else
  39. setsockopt(fd, IPPROTO_IP, IP_RECVIF, &oneopt, sizeof(oneopt)) == -1 ||
  40. #endif
  41. setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &oneopt, sizeof(oneopt)) == -1)
  42. die(_("failed to set options on DHCP socket: %s"), NULL, EC_BADNET);
  43. /* When bind-interfaces is set, there might be more than one dnmsasq
  44. instance binding port 67. That's OK if they serve different networks.
  45. Need to set REUSEADDR to make this posible, or REUSEPORT on *BSD. */
  46. if (daemon->options & OPT_NOWILD)
  47. {
  48. #ifdef SO_REUSEPORT
  49. int rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt));
  50. #else
  51. int rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt));
  52. #endif
  53. if (rc == -1)
  54. die(_("failed to set SO_REUSE{ADDR|PORT} on DHCP socket: %s"), NULL, EC_BADNET);
  55. }
  56. memset(&saddr, 0, sizeof(saddr));
  57. saddr.sin_family = AF_INET;
  58. saddr.sin_port = htons(daemon->dhcp_server_port);
  59. saddr.sin_addr.s_addr = INADDR_ANY;
  60. #ifdef HAVE_SOCKADDR_SA_LEN
  61. saddr.sin_len = sizeof(struct sockaddr_in);
  62. #endif
  63. if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in)))
  64. die(_("failed to bind DHCP server socket: %s"), NULL, EC_BADNET);
  65. daemon->dhcpfd = fd;
  66. #if defined(HAVE_BSD_NETWORK)
  67. /* When we're not using capabilities, we need to do this here before
  68. we drop root. Also, set buffer size small, to avoid wasting
  69. kernel buffers */
  70. if (daemon->options & OPT_NO_PING)
  71. daemon->dhcp_icmp_fd = -1;
  72. else if ((daemon->dhcp_icmp_fd = make_icmp_sock()) == -1 ||
  73. setsockopt(daemon->dhcp_icmp_fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) == -1 )
  74. die(_("cannot create ICMP raw socket: %s."), NULL, EC_BADNET);
  75. /* Make BPF raw send socket */
  76. init_bpf();
  77. #endif
  78. check_dhcp_hosts(1);
  79. daemon->dhcp_packet.iov_len = sizeof(struct dhcp_packet);
  80. daemon->dhcp_packet.iov_base = safe_malloc(daemon->dhcp_packet.iov_len);
  81. }
  82. void dhcp_packet(time_t now)
  83. {
  84. struct dhcp_packet *mess;
  85. struct dhcp_context *context;
  86. struct iname *tmp;
  87. struct ifreq ifr;
  88. struct msghdr msg;
  89. struct sockaddr_in dest;
  90. struct cmsghdr *cmptr;
  91. struct iovec iov;
  92. ssize_t sz;
  93. int iface_index = 0, unicast_dest = 0, is_inform = 0;
  94. struct in_addr iface_addr, *addrp = NULL;
  95. struct iface_param parm;
  96. union {
  97. struct cmsghdr align; /* this ensures alignment */
  98. #if defined(HAVE_LINUX_NETWORK)
  99. char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
  100. #elif defined(HAVE_SOLARIS_NETWORK)
  101. char control[CMSG_SPACE(sizeof(unsigned int))];
  102. #elif defined(HAVE_BSD_NETWORK)
  103. char control[CMSG_SPACE(sizeof(struct sockaddr_dl))];
  104. #endif
  105. } control_u;
  106. msg.msg_control = NULL;
  107. msg.msg_controllen = 0;
  108. msg.msg_name = NULL;
  109. msg.msg_namelen = 0;
  110. msg.msg_iov = &daemon->dhcp_packet;
  111. msg.msg_iovlen = 1;
  112. while (1)
  113. {
  114. msg.msg_flags = 0;
  115. while ((sz = recvmsg(daemon->dhcpfd, &msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
  116. if (sz == -1)
  117. return;
  118. if (!(msg.msg_flags & MSG_TRUNC))
  119. break;
  120. /* Very new Linux kernels return the actual size needed,
  121. older ones always return truncated size */
  122. if ((size_t)sz == daemon->dhcp_packet.iov_len)
  123. {
  124. if (!expand_buf(&daemon->dhcp_packet, sz + 100))
  125. return;
  126. }
  127. else
  128. {
  129. expand_buf(&daemon->dhcp_packet, sz);
  130. break;
  131. }
  132. }
  133. /* expand_buf may have moved buffer */
  134. mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
  135. msg.msg_controllen = sizeof(control_u);
  136. msg.msg_control = control_u.control;
  137. msg.msg_flags = 0;
  138. msg.msg_name = &dest;
  139. msg.msg_namelen = sizeof(dest);
  140. while ((sz = recvmsg(daemon->dhcpfd, &msg, 0)) == -1 && errno == EINTR);
  141. if ((msg.msg_flags & MSG_TRUNC) || sz < (ssize_t)(sizeof(*mess) - sizeof(mess->options)))
  142. return;
  143. #if defined (HAVE_LINUX_NETWORK)
  144. if (msg.msg_controllen >= sizeof(struct cmsghdr))
  145. for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
  146. if (cmptr->cmsg_level == SOL_IP && cmptr->cmsg_type == IP_PKTINFO)
  147. {
  148. iface_index = ((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_ifindex;
  149. if (((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_addr.s_addr != INADDR_BROADCAST)
  150. unicast_dest = 1;
  151. }
  152. #elif defined(HAVE_BSD_NETWORK)
  153. if (msg.msg_controllen >= sizeof(struct cmsghdr))
  154. for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
  155. if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
  156. iface_index = ((struct sockaddr_dl *)CMSG_DATA(cmptr))->sdl_index;
  157. #elif defined(HAVE_SOLARIS_NETWORK)
  158. if (msg.msg_controllen >= sizeof(struct cmsghdr))
  159. for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
  160. if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
  161. iface_index = *((unsigned int *)CMSG_DATA(cmptr));
  162. #endif
  163. if (!indextoname(daemon->dhcpfd, iface_index, ifr.ifr_name))
  164. return;
  165. #ifdef MSG_BCAST
  166. /* OpenBSD tells us when a packet was broadcast */
  167. if (!(msg.msg_flags & MSG_BCAST))
  168. unicast_dest = 1;
  169. #endif
  170. ifr.ifr_addr.sa_family = AF_INET;
  171. if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) != -1 )
  172. {
  173. addrp = &iface_addr;
  174. iface_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
  175. }
  176. if (!iface_check(AF_INET, (struct all_addr *)addrp, ifr.ifr_name, &iface_index))
  177. return;
  178. for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
  179. if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0))
  180. return;
  181. /* interface may have been changed by alias in iface_check */
  182. if (!addrp)
  183. {
  184. if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) == -1)
  185. {
  186. my_syslog(MS_DHCP | LOG_WARNING, _("DHCP packet received on %s which has no address"), ifr.ifr_name);
  187. return;
  188. }
  189. else
  190. iface_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
  191. }
  192. /* unlinked contexts are marked by context->current == context */
  193. for (context = daemon->dhcp; context; context = context->next)
  194. context->current = context;
  195. parm.relay = mess->giaddr;
  196. parm.primary = iface_addr;
  197. parm.current = NULL;
  198. parm.ind = iface_index;
  199. if (!iface_enumerate(&parm, complete_context, NULL))
  200. return;
  201. lease_prune(NULL, now); /* lose any expired leases */
  202. iov.iov_len = dhcp_reply(parm.current, ifr.ifr_name, iface_index, (size_t)sz,
  203. now, unicast_dest, &is_inform);
  204. lease_update_file(now);
  205. lease_update_dns();
  206. if (iov.iov_len == 0)
  207. return;
  208. msg.msg_name = &dest;
  209. msg.msg_namelen = sizeof(dest);
  210. msg.msg_control = NULL;
  211. msg.msg_controllen = 0;
  212. msg.msg_iov = &iov;
  213. iov.iov_base = daemon->dhcp_packet.iov_base;
  214. /* packet buffer may have moved */
  215. mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
  216. #ifdef HAVE_SOCKADDR_SA_LEN
  217. dest.sin_len = sizeof(struct sockaddr_in);
  218. #endif
  219. if (mess->giaddr.s_addr)
  220. {
  221. /* Send to BOOTP relay */
  222. dest.sin_port = htons(daemon->dhcp_server_port);
  223. dest.sin_addr = mess->giaddr;
  224. }
  225. else if (mess->ciaddr.s_addr)
  226. {
  227. /* If the client's idea of its own address tallys with
  228. the source address in the request packet, we believe the
  229. source port too, and send back to that. If we're replying
  230. to a DHCPINFORM, trust the source address always. */
  231. if ((!is_inform && dest.sin_addr.s_addr != mess->ciaddr.s_addr) ||
  232. dest.sin_port == 0 || dest.sin_addr.s_addr == 0)
  233. {
  234. dest.sin_port = htons(daemon->dhcp_client_port);
  235. dest.sin_addr = mess->ciaddr;
  236. }
  237. }
  238. #if defined(HAVE_LINUX_NETWORK)
  239. else if ((ntohs(mess->flags) & 0x8000) || mess->hlen == 0 ||
  240. mess->hlen > sizeof(ifr.ifr_addr.sa_data) || mess->htype == 0)
  241. {
  242. /* broadcast to 255.255.255.255 (or mac address invalid) */
  243. struct in_pktinfo *pkt;
  244. msg.msg_control = control_u.control;
  245. msg.msg_controllen = sizeof(control_u);
  246. cmptr = CMSG_FIRSTHDR(&msg);
  247. pkt = (struct in_pktinfo *)CMSG_DATA(cmptr);
  248. pkt->ipi_ifindex = iface_index;
  249. pkt->ipi_spec_dst.s_addr = 0;
  250. msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  251. cmptr->cmsg_level = SOL_IP;
  252. cmptr->cmsg_type = IP_PKTINFO;
  253. dest.sin_addr.s_addr = INADDR_BROADCAST;
  254. dest.sin_port = htons(daemon->dhcp_client_port);
  255. }
  256. else
  257. {
  258. /* unicast to unconfigured client. Inject mac address direct into ARP cache.
  259. struct sockaddr limits size to 14 bytes. */
  260. struct arpreq req;
  261. dest.sin_addr = mess->yiaddr;
  262. dest.sin_port = htons(daemon->dhcp_client_port);
  263. *((struct sockaddr_in *)&req.arp_pa) = dest;
  264. req.arp_ha.sa_family = mess->htype;
  265. memcpy(req.arp_ha.sa_data, mess->chaddr, mess->hlen);
  266. strncpy(req.arp_dev, ifr.ifr_name, 16);
  267. req.arp_flags = ATF_COM;
  268. ioctl(daemon->dhcpfd, SIOCSARP, &req);
  269. }
  270. #elif defined(HAVE_SOLARIS_NETWORK)
  271. else if ((ntohs(mess->flags) & 0x8000) || mess->hlen != ETHER_ADDR_LEN || mess->htype != ARPHRD_ETHER)
  272. {
  273. /* broadcast to 255.255.255.255 (or mac address invalid) */
  274. dest.sin_addr.s_addr = INADDR_BROADCAST;
  275. dest.sin_port = htons(daemon->dhcp_client_port);
  276. /* note that we don't specify the interface here: that's done by the
  277. IP_BOUND_IF sockopt lower down. */
  278. }
  279. else
  280. {
  281. /* unicast to unconfigured client. Inject mac address direct into ARP cache.
  282. Note that this only works for ethernet on solaris, because we use SIOCSARP
  283. and not SIOCSXARP, which would be perfect, except that it returns ENXIO
  284. mysteriously. Bah. Fall back to broadcast for other net types. */
  285. struct arpreq req;
  286. dest.sin_addr = mess->yiaddr;
  287. dest.sin_port = htons(daemon->dhcp_client_port);
  288. *((struct sockaddr_in *)&req.arp_pa) = dest;
  289. req.arp_ha.sa_family = AF_UNSPEC;
  290. memcpy(req.arp_ha.sa_data, mess->chaddr, mess->hlen);
  291. req.arp_flags = ATF_COM;
  292. ioctl(daemon->dhcpfd, SIOCSARP, &req);
  293. }
  294. #elif defined(HAVE_BSD_NETWORK)
  295. else
  296. {
  297. send_via_bpf(mess, iov.iov_len, iface_addr, &ifr);
  298. return;
  299. }
  300. #endif
  301. #ifdef HAVE_SOLARIS_NETWORK
  302. setsockopt(daemon->dhcpfd, IPPROTO_IP, IP_BOUND_IF, &iface_index, sizeof(iface_index));
  303. #endif
  304. while(sendmsg(daemon->dhcpfd, &msg, 0) == -1 && retry_send());
  305. }
  306. /* This is a complex routine: it gets called with each (address,netmask,broadcast) triple
  307. of each interface (and any relay address) and does the following things:
  308. 1) Discards stuff for interfaces other than the one on which a DHCP packet just arrived.
  309. 2) Fills in any netmask and broadcast addresses which have not been explicitly configured.
  310. 3) Fills in local (this host) and router (this host or relay) addresses.
  311. 4) Links contexts which are valid for hosts directly connected to the arrival interface on ->current.
  312. Note that the current chain may be superceded later for configured hosts or those coming via gateways. */
  313. static int complete_context(struct in_addr local, int if_index,
  314. struct in_addr netmask, struct in_addr broadcast, void *vparam)
  315. {
  316. struct dhcp_context *context;
  317. struct iface_param *param = vparam;
  318. for (context = daemon->dhcp; context; context = context->next)
  319. {
  320. if (!(context->flags & CONTEXT_NETMASK) &&
  321. (is_same_net(local, context->start, netmask) ||
  322. is_same_net(local, context->end, netmask)))
  323. {
  324. if (context->netmask.s_addr != netmask.s_addr &&
  325. !(is_same_net(local, context->start, netmask) &&
  326. is_same_net(local, context->end, netmask)))
  327. {
  328. strcpy(daemon->dhcp_buff, inet_ntoa(context->start));
  329. strcpy(daemon->dhcp_buff2, inet_ntoa(context->end));
  330. my_syslog(MS_DHCP | LOG_WARNING, _("DHCP range %s -- %s is not consistent with netmask %s"),
  331. daemon->dhcp_buff, daemon->dhcp_buff2, inet_ntoa(netmask));
  332. }
  333. context->netmask = netmask;
  334. }
  335. if (context->netmask.s_addr)
  336. {
  337. if (is_same_net(local, context->start, context->netmask) &&
  338. is_same_net(local, context->end, context->netmask))
  339. {
  340. /* link it onto the current chain if we've not seen it before */
  341. if (if_index == param->ind && context->current == context)
  342. {
  343. context->router = local;
  344. context->local = local;
  345. context->current = param->current;
  346. param->current = context;
  347. }
  348. if (!(context->flags & CONTEXT_BRDCAST))
  349. {
  350. if (is_same_net(broadcast, context->start, context->netmask))
  351. context->broadcast = broadcast;
  352. else
  353. context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr;
  354. }
  355. }
  356. else if (param->relay.s_addr && is_same_net(param->relay, context->start, context->netmask))
  357. {
  358. context->router = param->relay;
  359. context->local = param->primary;
  360. /* fill in missing broadcast addresses for relayed ranges */
  361. if (!(context->flags & CONTEXT_BRDCAST))
  362. context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr;
  363. }
  364. }
  365. }
  366. return 1;
  367. }
  368. struct dhcp_context *address_available(struct dhcp_context *context,
  369. struct in_addr taddr,
  370. struct dhcp_netid *netids)
  371. {
  372. /* Check is an address is OK for this network, check all
  373. possible ranges. Make sure that the address isn't in use
  374. by the server itself. */
  375. unsigned int start, end, addr = ntohl(taddr.s_addr);
  376. struct dhcp_context *tmp;
  377. for (tmp = context; tmp; tmp = tmp->current)
  378. if (taddr.s_addr == context->router.s_addr)
  379. return NULL;
  380. for (tmp = context; tmp; tmp = tmp->current)
  381. {
  382. start = ntohl(tmp->start.s_addr);
  383. end = ntohl(tmp->end.s_addr);
  384. if (!(tmp->flags & CONTEXT_STATIC) &&
  385. addr >= start &&
  386. addr <= end &&
  387. match_netid(tmp->filter, netids, 1))
  388. return tmp;
  389. }
  390. return NULL;
  391. }
  392. struct dhcp_context *narrow_context(struct dhcp_context *context,
  393. struct in_addr taddr,
  394. struct dhcp_netid *netids)
  395. {
  396. /* We start of with a set of possible contexts, all on the current physical interface.
  397. These are chained on ->current.
  398. Here we have an address, and return the actual context correponding to that
  399. address. Note that none may fit, if the address came a dhcp-host and is outside
  400. any dhcp-range. In that case we return a static range if possible, or failing that,
  401. any context on the correct subnet. (If there's more than one, this is a dodgy
  402. configuration: maybe there should be a warning.) */
  403. struct dhcp_context *tmp;
  404. if (!(tmp = address_available(context, taddr, netids)))
  405. {
  406. for (tmp = context; tmp; tmp = tmp->current)
  407. if (is_same_net(taddr, tmp->start, tmp->netmask) &&
  408. (tmp->flags & CONTEXT_STATIC))
  409. break;
  410. if (!tmp)
  411. for (tmp = context; tmp; tmp = tmp->current)
  412. if (is_same_net(taddr, tmp->start, tmp->netmask))
  413. break;
  414. }
  415. /* Only one context allowed now */
  416. if (tmp)
  417. tmp->current = NULL;
  418. return tmp;
  419. }
  420. struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr)
  421. {
  422. struct dhcp_config *config;
  423. for (config = configs; config; config = config->next)
  424. if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr)
  425. return config;
  426. return NULL;
  427. }
  428. /* Is every member of check matched by a member of pool?
  429. If tagnotneeded, untagged is OK */
  430. int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool, int tagnotneeded)
  431. {
  432. struct dhcp_netid *tmp1;
  433. if (!check && !tagnotneeded)
  434. return 0;
  435. for (; check; check = check->next)
  436. {
  437. if (check->net[0] != '#')
  438. {
  439. for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
  440. if (strcmp(check->net, tmp1->net) == 0)
  441. break;
  442. if (!tmp1)
  443. return 0;
  444. }
  445. else
  446. for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
  447. if (strcmp((check->net)+1, tmp1->net) == 0)
  448. return 0;
  449. }
  450. return 1;
  451. }
  452. int address_allocate(struct dhcp_context *context,
  453. struct in_addr *addrp, unsigned char *hwaddr, int hw_len,
  454. struct dhcp_netid *netids, time_t now)
  455. {
  456. /* Find a free address: exclude anything in use and anything allocated to
  457. a particular hwaddr/clientid/hostname in our configuration.
  458. Try to return from contexts which match netids first. */
  459. struct in_addr start, addr;
  460. struct dhcp_context *c, *d;
  461. int i, pass;
  462. unsigned int j;
  463. /* hash hwaddr */
  464. for (j = 0, i = 0; i < hw_len; i++)
  465. j += hwaddr[i] + (hwaddr[i] << 8) + (hwaddr[i] << 16);
  466. for (pass = 0; pass <= 1; pass++)
  467. for (c = context; c; c = c->current)
  468. if (c->flags & CONTEXT_STATIC)
  469. continue;
  470. else if (!match_netid(c->filter, netids, pass))
  471. continue;
  472. else
  473. {
  474. /* pick a seed based on hwaddr then iterate until we find a free address. */
  475. start.s_addr = addr.s_addr =
  476. htonl(ntohl(c->start.s_addr) +
  477. ((j + c->addr_epoch) % (1 + ntohl(c->end.s_addr) - ntohl(c->start.s_addr))));
  478. do {
  479. /* eliminate addresses in use by the server. */
  480. for (d = context; d; d = d->current)
  481. if (addr.s_addr == d->router.s_addr)
  482. break;
  483. /* Addresses which end in .255 and .0 are broken in Windows even when using
  484. supernetting. ie dhcp-range=192.168.0.1,192.168.1.254,255,255,254.0
  485. then 192.168.0.255 is a valid IP address, but not for Windows as it's
  486. in the class C range. See KB281579. We therefore don't allocate these
  487. addresses to avoid hard-to-diagnose problems. Thanks Bill. */
  488. if (!d &&
  489. !lease_find_by_addr(addr) &&
  490. !config_find_by_address(daemon->dhcp_conf, addr) &&
  491. (!IN_CLASSC(ntohl(addr.s_addr)) ||
  492. ((ntohl(addr.s_addr) & 0xff) != 0xff && ((ntohl(addr.s_addr) & 0xff) != 0x0))))
  493. {
  494. struct ping_result *r, *victim = NULL;
  495. int count, max = (int)(0.6 * (((float)PING_CACHE_TIME)/
  496. ((float)PING_WAIT)));
  497. *addrp = addr;
  498. if (daemon->options & OPT_NO_PING)
  499. return 1;
  500. /* check if we failed to ping addr sometime in the last
  501. PING_CACHE_TIME seconds. If so, assume the same situation still exists.
  502. This avoids problems when a stupid client bangs
  503. on us repeatedly. As a final check, if we did more
  504. than 60% of the possible ping checks in the last
  505. PING_CACHE_TIME, we are in high-load mode, so don't do any more. */
  506. for (count = 0, r = daemon->ping_results; r; r = r->next)
  507. if (difftime(now, r->time) > (float)PING_CACHE_TIME)
  508. victim = r; /* old record */
  509. else if (++count == max || r->addr.s_addr == addr.s_addr)
  510. return 1;
  511. if (icmp_ping(addr))
  512. /* address in use: perturb address selection so that we are
  513. less likely to try this address again. */
  514. c->addr_epoch++;
  515. else
  516. {
  517. /* at this point victim may hold an expired record */
  518. if (!victim)
  519. {
  520. if ((victim = whine_malloc(sizeof(struct ping_result))))
  521. {
  522. victim->next = daemon->ping_results;
  523. daemon->ping_results = victim;
  524. }
  525. }
  526. /* record that this address is OK for 30s
  527. without more ping checks */
  528. if (victim)
  529. {
  530. victim->addr = addr;
  531. victim->time = now;
  532. }
  533. return 1;
  534. }
  535. }
  536. addr.s_addr = htonl(ntohl(addr.s_addr) + 1);
  537. if (addr.s_addr == htonl(ntohl(c->end.s_addr) + 1))
  538. addr = c->start;
  539. } while (addr.s_addr != start.s_addr);
  540. }
  541. return 0;
  542. }
  543. static int is_addr_in_context(struct dhcp_context *context, struct dhcp_config *config)
  544. {
  545. if (!context) /* called via find_config() from lease_update_from_configs() */
  546. return 1;
  547. if (!(config->flags & CONFIG_ADDR))
  548. return 1;
  549. for (; context; context = context->current)
  550. if (is_same_net(config->addr, context->start, context->netmask))
  551. return 1;
  552. return 0;
  553. }
  554. int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type)
  555. {
  556. struct hwaddr_config *conf_addr;
  557. for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
  558. if (conf_addr->wildcard_mask == 0 &&
  559. conf_addr->hwaddr_len == len &&
  560. (conf_addr->hwaddr_type == type || conf_addr->hwaddr_type == 0) &&
  561. memcmp(conf_addr->hwaddr, hwaddr, len) == 0)
  562. return 1;
  563. return 0;
  564. }
  565. struct dhcp_config *find_config(struct dhcp_config *configs,
  566. struct dhcp_context *context,
  567. unsigned char *clid, int clid_len,
  568. unsigned char *hwaddr, int hw_len,
  569. int hw_type, char *hostname)
  570. {
  571. int count, new;
  572. struct dhcp_config *config, *candidate;
  573. struct hwaddr_config *conf_addr;
  574. if (clid)
  575. for (config = configs; config; config = config->next)
  576. if (config->flags & CONFIG_CLID)
  577. {
  578. if (config->clid_len == clid_len &&
  579. memcmp(config->clid, clid, clid_len) == 0 &&
  580. is_addr_in_context(context, config))
  581. return config;
  582. /* dhcpcd prefixes ASCII client IDs by zero which is wrong, but we try and
  583. cope with that here */
  584. if (*clid == 0 && config->clid_len == clid_len-1 &&
  585. memcmp(config->clid, clid+1, clid_len-1) == 0 &&
  586. is_addr_in_context(context, config))
  587. return config;
  588. }
  589. for (config = configs; config; config = config->next)
  590. if (config_has_mac(config, hwaddr, hw_len, hw_type) &&
  591. is_addr_in_context(context, config))
  592. return config;
  593. if (hostname && context)
  594. for (config = configs; config; config = config->next)
  595. if ((config->flags & CONFIG_NAME) &&
  596. hostname_isequal(config->hostname, hostname) &&
  597. is_addr_in_context(context, config))
  598. return config;
  599. /* use match with fewest wildcast octets */
  600. for (candidate = NULL, count = 0, config = configs; config; config = config->next)
  601. if (is_addr_in_context(context, config))
  602. for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
  603. if (conf_addr->wildcard_mask != 0 &&
  604. conf_addr->hwaddr_len == hw_len &&
  605. (conf_addr->hwaddr_type == hw_type || conf_addr->hwaddr_type == 0) &&
  606. (new = memcmp_masked(conf_addr->hwaddr, hwaddr, hw_len, conf_addr->wildcard_mask)) > count)
  607. {
  608. count = new;
  609. candidate = config;
  610. }
  611. return candidate;
  612. }
  613. void dhcp_read_ethers(void)
  614. {
  615. FILE *f = fopen(ETHERSFILE, "r");
  616. unsigned int flags;
  617. char *buff = daemon->namebuff;
  618. char *ip, *cp;
  619. struct in_addr addr;
  620. unsigned char hwaddr[ETHER_ADDR_LEN];
  621. struct dhcp_config **up, *tmp;
  622. struct dhcp_config *config;
  623. int count = 0, lineno = 0;
  624. addr.s_addr = 0; /* eliminate warning */
  625. if (!f)
  626. {
  627. my_syslog(MS_DHCP | LOG_ERR, _("failed to read %s: %s"), ETHERSFILE, strerror(errno));
  628. return;
  629. }
  630. /* This can be called again on SIGHUP, so remove entries created last time round. */
  631. for (up = &daemon->dhcp_conf, config = daemon->dhcp_conf; config; config = tmp)
  632. {
  633. tmp = config->next;
  634. if (config->flags & CONFIG_FROM_ETHERS)
  635. {
  636. *up = tmp;
  637. /* cannot have a clid */
  638. if (config->flags & CONFIG_NAME)
  639. free(config->hostname);
  640. free(config->hwaddr);
  641. free(config);
  642. }
  643. else
  644. up = &config->next;
  645. }
  646. while (fgets(buff, MAXDNAME, f))
  647. {
  648. char *host = NULL;
  649. lineno++;
  650. while (strlen(buff) > 0 && isspace((int)buff[strlen(buff)-1]))
  651. buff[strlen(buff)-1] = 0;
  652. if ((*buff == '#') || (*buff == '+') || (*buff == 0))
  653. continue;
  654. for (ip = buff; *ip && !isspace((int)*ip); ip++);
  655. for(; *ip && isspace((int)*ip); ip++)
  656. *ip = 0;
  657. if (!*ip || parse_hex(buff, hwaddr, ETHER_ADDR_LEN, NULL, NULL) != ETHER_ADDR_LEN)
  658. {
  659. my_syslog(MS_DHCP | LOG_ERR, _("bad line at %s line %d"), ETHERSFILE, lineno);
  660. continue;
  661. }
  662. /* check for name or dotted-quad */
  663. for (cp = ip; *cp; cp++)
  664. if (!(*cp == '.' || (*cp >='0' && *cp <= '9')))
  665. break;
  666. if (!*cp)
  667. {
  668. if ((addr.s_addr = inet_addr(ip)) == (in_addr_t)-1)
  669. {
  670. my_syslog(MS_DHCP | LOG_ERR, _("bad address at %s line %d"), ETHERSFILE, lineno);
  671. continue;
  672. }
  673. flags = CONFIG_ADDR;
  674. for (config = daemon->dhcp_conf; config; config = config->next)
  675. if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr)
  676. break;
  677. }
  678. else
  679. {
  680. int nomem;
  681. if (!(host = canonicalise(ip, &nomem)) || !legal_hostname(host))
  682. {
  683. if (!nomem)
  684. my_syslog(MS_DHCP | LOG_ERR, _("bad name at %s line %d"), ETHERSFILE, lineno);
  685. free(host);
  686. continue;
  687. }
  688. flags = CONFIG_NAME;
  689. for (config = daemon->dhcp_conf; config; config = config->next)
  690. if ((config->flags & CONFIG_NAME) && hostname_isequal(config->hostname, host))
  691. break;
  692. }
  693. if (config && (config->flags & CONFIG_FROM_ETHERS))
  694. {
  695. my_syslog(MS_DHCP | LOG_ERR, _("ignoring %s line %d, duplicate name or IP address"), ETHERSFILE, lineno);
  696. continue;
  697. }
  698. if (!config)
  699. {
  700. for (config = daemon->dhcp_conf; config; config = config->next)
  701. {
  702. struct hwaddr_config *conf_addr = config->hwaddr;
  703. if (conf_addr &&
  704. conf_addr->next == NULL &&
  705. conf_addr->wildcard_mask == 0 &&
  706. conf_addr->hwaddr_len == ETHER_ADDR_LEN &&
  707. (conf_addr->hwaddr_type == ARPHRD_ETHER || conf_addr->hwaddr_type == 0) &&
  708. memcmp(conf_addr->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0)
  709. break;
  710. }
  711. if (!config)
  712. {
  713. if (!(config = whine_malloc(sizeof(struct dhcp_config))))
  714. continue;
  715. config->flags = CONFIG_FROM_ETHERS;
  716. config->hwaddr = NULL;
  717. config->domain = NULL;
  718. config->next = daemon->dhcp_conf;
  719. daemon->dhcp_conf = config;
  720. }
  721. config->flags |= flags;
  722. if (flags & CONFIG_NAME)
  723. {
  724. config->hostname = host;
  725. host = NULL;
  726. }
  727. if (flags & CONFIG_ADDR)
  728. config->addr = addr;
  729. }
  730. config->flags |= CONFIG_NOCLID;
  731. if (!config->hwaddr)
  732. config->hwaddr = whine_malloc(sizeof(struct hwaddr_config));
  733. if (config->hwaddr)
  734. {
  735. memcpy(config->hwaddr->hwaddr, hwaddr, ETHER_ADDR_LEN);
  736. config->hwaddr->hwaddr_len = ETHER_ADDR_LEN;
  737. config->hwaddr->hwaddr_type = ARPHRD_ETHER;
  738. config->hwaddr->wildcard_mask = 0;
  739. config->hwaddr->next = NULL;
  740. }
  741. count++;
  742. free(host);
  743. }
  744. fclose(f);
  745. my_syslog(MS_DHCP | LOG_INFO, _("read %s - %d addresses"), ETHERSFILE, count);
  746. }
  747. void check_dhcp_hosts(int fatal)
  748. {
  749. /* If the same IP appears in more than one host config, then DISCOVER
  750. for one of the hosts will get the address, but REQUEST will be NAKed,
  751. since the address is reserved by the other one -> protocol loop.
  752. Also check that FQDNs match the domain we are using. */
  753. struct dhcp_config *configs, *cp;
  754. for (configs = daemon->dhcp_conf; configs; configs = configs->next)
  755. {
  756. char *domain;
  757. if ((configs->flags & DHOPT_BANK) || fatal)
  758. {
  759. for (cp = configs->next; cp; cp = cp->next)
  760. if ((configs->flags & cp->flags & CONFIG_ADDR) && configs->addr.s_addr == cp->addr.s_addr)
  761. {
  762. if (fatal)
  763. die(_("duplicate IP address %s in dhcp-config directive."),
  764. inet_ntoa(cp->addr), EC_BADCONF);
  765. else
  766. my_syslog(MS_DHCP | LOG_ERR, _("duplicate IP address %s in %s."),
  767. inet_ntoa(cp->addr), daemon->dhcp_hosts_file);
  768. configs->flags &= ~CONFIG_ADDR;
  769. }
  770. /* split off domain part */
  771. if ((configs->flags & CONFIG_NAME) && (domain = strip_hostname(configs->hostname)))
  772. configs->domain = domain;
  773. }
  774. }
  775. }
  776. void dhcp_update_configs(struct dhcp_config *configs)
  777. {
  778. /* Some people like to keep all static IP addresses in /etc/hosts.
  779. This goes through /etc/hosts and sets static addresses for any DHCP config
  780. records which don't have an address and whose name matches.
  781. We take care to maintain the invariant that any IP address can appear
  782. in at most one dhcp-host. Since /etc/hosts can be re-read by SIGHUP,
  783. restore the status-quo ante first. */
  784. struct dhcp_config *config;
  785. struct crec *crec;
  786. for (config = configs; config; config = config->next)
  787. if (config->flags & CONFIG_ADDR_HOSTS)
  788. config->flags &= ~(CONFIG_ADDR | CONFIG_ADDR_HOSTS);
  789. if (daemon->port != 0)
  790. for (config = configs; config; config = config->next)
  791. if (!(config->flags & CONFIG_ADDR) &&
  792. (config->flags & CONFIG_NAME) &&
  793. (crec = cache_find_by_name(NULL, config->hostname, 0, F_IPV4)) &&
  794. (crec->flags & F_HOSTS))
  795. {
  796. if (cache_find_by_name(crec, config->hostname, 0, F_IPV4))
  797. {
  798. /* use primary (first) address */
  799. while (crec && !(crec->flags & F_REVERSE))
  800. crec = cache_find_by_name(crec, config->hostname, 0, F_IPV4);
  801. if (!crec)
  802. continue; /* should be never */
  803. my_syslog(MS_DHCP | LOG_WARNING, _("%s has more than one address in hostsfile, using %s for DHCP"),
  804. config->hostname, inet_ntoa(crec->addr.addr.addr.addr4));
  805. }
  806. if (config_find_by_address(configs, crec->addr.addr.addr.addr4))
  807. my_syslog(MS_DHCP | LOG_WARNING, _("duplicate IP address %s (%s) in dhcp-config directive"),
  808. inet_ntoa(crec->addr.addr.addr.addr4), config->hostname);
  809. else
  810. {
  811. config->addr = crec->addr.addr.addr.addr4;
  812. config->flags |= CONFIG_ADDR | CONFIG_ADDR_HOSTS;
  813. }
  814. }
  815. }
  816. /* If we've not found a hostname any other way, try and see if there's one in /etc/hosts
  817. for this address. If it has a domain part, that must match the set domain and
  818. it gets stripped. The set of legal domain names is bigger than the set of legal hostnames
  819. so check here that the domain name is legal as a hostname. */
  820. char *host_from_dns(struct in_addr addr)
  821. {
  822. struct crec *lookup;
  823. char *hostname = NULL;
  824. char *d1, *d2;
  825. if (daemon->port == 0)
  826. return NULL; /* DNS disabled. */
  827. lookup = cache_find_by_addr(NULL, (struct all_addr *)&addr, 0, F_IPV4);
  828. if (lookup && (lookup->flags & F_HOSTS))
  829. {
  830. hostname = daemon->dhcp_buff;
  831. strncpy(hostname, cache_get_name(lookup), 256);
  832. hostname[255] = 0;
  833. d1 = strip_hostname(hostname);
  834. d2 = get_domain(addr);
  835. if (!legal_hostname(hostname) || (d1 && (!d2 || !hostname_isequal(d1, d2))))
  836. hostname = NULL;
  837. }
  838. return hostname;
  839. }
  840. /* return domain or NULL if none. */
  841. char *strip_hostname(char *hostname)
  842. {
  843. char *dot = strchr(hostname, '.');
  844. if (!dot)
  845. return NULL;
  846. *dot = 0; /* truncate */
  847. if (strlen(dot+1) != 0)
  848. return dot+1;
  849. return NULL;
  850. }
  851. #endif