serverpacket.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* vi: set sw=4 ts=4: */
  2. /* serverpacket.c
  3. *
  4. * Construct and send DHCP server packets
  5. *
  6. * Russ Dill <Russ.Dill@asu.edu> July 2001
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "common.h"
  23. #include "dhcpd.h"
  24. #include "options.h"
  25. /* send a packet to giaddr using the kernel ip stack */
  26. static int send_packet_to_relay(struct dhcpMessage *payload)
  27. {
  28. DEBUG("Forwarding packet to relay");
  29. return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
  30. payload->giaddr, SERVER_PORT);
  31. }
  32. /* send a packet to a specific arp address and ip address by creating our own ip packet */
  33. static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
  34. {
  35. const uint8_t *chaddr;
  36. uint32_t ciaddr;
  37. if (force_broadcast) {
  38. DEBUG("broadcasting packet to client (NAK)");
  39. ciaddr = INADDR_BROADCAST;
  40. chaddr = MAC_BCAST_ADDR;
  41. } else if (payload->ciaddr) {
  42. DEBUG("unicasting packet to client ciaddr");
  43. ciaddr = payload->ciaddr;
  44. chaddr = payload->chaddr;
  45. } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
  46. DEBUG("broadcasting packet to client (requested)");
  47. ciaddr = INADDR_BROADCAST;
  48. chaddr = MAC_BCAST_ADDR;
  49. } else {
  50. DEBUG("unicasting packet to client yiaddr");
  51. ciaddr = payload->yiaddr;
  52. chaddr = payload->chaddr;
  53. }
  54. return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,
  55. ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
  56. }
  57. /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
  58. static int send_packet(struct dhcpMessage *payload, int force_broadcast)
  59. {
  60. int ret;
  61. if (payload->giaddr)
  62. ret = send_packet_to_relay(payload);
  63. else ret = send_packet_to_client(payload, force_broadcast);
  64. return ret;
  65. }
  66. static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
  67. {
  68. udhcp_init_header(packet, type);
  69. packet->xid = oldpacket->xid;
  70. memcpy(packet->chaddr, oldpacket->chaddr, 16);
  71. packet->flags = oldpacket->flags;
  72. packet->giaddr = oldpacket->giaddr;
  73. packet->ciaddr = oldpacket->ciaddr;
  74. add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
  75. }
  76. /* add in the bootp options */
  77. static void add_bootp_options(struct dhcpMessage *packet)
  78. {
  79. packet->siaddr = server_config.siaddr;
  80. if (server_config.sname)
  81. strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
  82. if (server_config.boot_file)
  83. strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
  84. }
  85. /* send a DHCP OFFER to a DHCP DISCOVER */
  86. int sendOffer(struct dhcpMessage *oldpacket)
  87. {
  88. struct dhcpMessage packet;
  89. struct dhcpOfferedAddr *lease = NULL;
  90. uint32_t req_align, lease_time_align = server_config.lease;
  91. uint8_t *req, *lease_time;
  92. struct option_set *curr;
  93. struct in_addr addr;
  94. uint32_t static_lease_ip;
  95. init_packet(&packet, oldpacket, DHCPOFFER);
  96. static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
  97. /* ADDME: if static, short circuit */
  98. if (!static_lease_ip) {
  99. /* the client is in our lease/offered table */
  100. lease = find_lease_by_chaddr(oldpacket->chaddr);
  101. if (lease) {
  102. if (!lease_expired(lease))
  103. lease_time_align = lease->expires - time(0);
  104. packet.yiaddr = lease->yiaddr;
  105. /* Or the client has a requested ip */
  106. } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))
  107. /* Don't look here (ugly hackish thing to do) */
  108. && memcpy(&req_align, req, 4)
  109. /* and the ip is in the lease range */
  110. && ntohl(req_align) >= server_config.start_ip
  111. && ntohl(req_align) <= server_config.end_ip
  112. && !static_lease_ip /* Check that its not a static lease */
  113. /* and is not already taken/offered */
  114. && (!(lease = find_lease_by_yiaddr(req_align))
  115. /* or its taken, but expired */ /* ADDME: or maybe in here */
  116. || lease_expired(lease))
  117. ) {
  118. packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
  119. /* otherwise, find a free IP */
  120. } else {
  121. /* Is it a static lease? (No, because find_address skips static lease) */
  122. packet.yiaddr = find_address(0);
  123. /* try for an expired lease */
  124. if (!packet.yiaddr)
  125. packet.yiaddr = find_address(1);
  126. }
  127. if (!packet.yiaddr) {
  128. bb_error_msg("no IP addresses to give - OFFER abandoned");
  129. return -1;
  130. }
  131. if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
  132. bb_error_msg("lease pool is full - OFFER abandoned");
  133. return -1;
  134. }
  135. lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
  136. if (lease_time) {
  137. memcpy(&lease_time_align, lease_time, 4);
  138. lease_time_align = ntohl(lease_time_align);
  139. if (lease_time_align > server_config.lease)
  140. lease_time_align = server_config.lease;
  141. }
  142. /* Make sure we aren't just using the lease time from the previous offer */
  143. if (lease_time_align < server_config.min_lease)
  144. lease_time_align = server_config.lease;
  145. /* ADDME: end of short circuit */
  146. } else {
  147. /* It is a static lease... use it */
  148. packet.yiaddr = static_lease_ip;
  149. }
  150. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  151. curr = server_config.options;
  152. while (curr) {
  153. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  154. add_option_string(packet.options, curr->data);
  155. curr = curr->next;
  156. }
  157. add_bootp_options(&packet);
  158. addr.s_addr = packet.yiaddr;
  159. bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
  160. return send_packet(&packet, 0);
  161. }
  162. int sendNAK(struct dhcpMessage *oldpacket)
  163. {
  164. struct dhcpMessage packet;
  165. init_packet(&packet, oldpacket, DHCPNAK);
  166. DEBUG("Sending NAK");
  167. return send_packet(&packet, 1);
  168. }
  169. int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
  170. {
  171. struct dhcpMessage packet;
  172. struct option_set *curr;
  173. uint8_t *lease_time;
  174. uint32_t lease_time_align = server_config.lease;
  175. struct in_addr addr;
  176. init_packet(&packet, oldpacket, DHCPACK);
  177. packet.yiaddr = yiaddr;
  178. lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
  179. if (lease_time) {
  180. memcpy(&lease_time_align, lease_time, 4);
  181. lease_time_align = ntohl(lease_time_align);
  182. if (lease_time_align > server_config.lease)
  183. lease_time_align = server_config.lease;
  184. else if (lease_time_align < server_config.min_lease)
  185. lease_time_align = server_config.lease;
  186. }
  187. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  188. curr = server_config.options;
  189. while (curr) {
  190. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  191. add_option_string(packet.options, curr->data);
  192. curr = curr->next;
  193. }
  194. add_bootp_options(&packet);
  195. addr.s_addr = packet.yiaddr;
  196. bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
  197. if (send_packet(&packet, 0) < 0)
  198. return -1;
  199. add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
  200. if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
  201. /* rewrite the file with leases at every new acceptance */
  202. write_leases();
  203. }
  204. return 0;
  205. }
  206. int send_inform(struct dhcpMessage *oldpacket)
  207. {
  208. struct dhcpMessage packet;
  209. struct option_set *curr;
  210. init_packet(&packet, oldpacket, DHCPACK);
  211. curr = server_config.options;
  212. while (curr) {
  213. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  214. add_option_string(packet.options, curr->data);
  215. curr = curr->next;
  216. }
  217. add_bootp_options(&packet);
  218. return send_packet(&packet, 0);
  219. }