3
0

serverpacket.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. 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) >= ntohl(server_config.start)
  111. && ntohl(req_align) <= ntohl(server_config.end)
  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) packet.yiaddr = find_address(1);
  125. }
  126. if (!packet.yiaddr) {
  127. bb_error_msg("no IP addresses to give - OFFER abandoned");
  128. return -1;
  129. }
  130. if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
  131. bb_error_msg("lease pool is full - OFFER abandoned");
  132. return -1;
  133. }
  134. lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
  135. if (lease_time) {
  136. memcpy(&lease_time_align, lease_time, 4);
  137. lease_time_align = ntohl(lease_time_align);
  138. if (lease_time_align > server_config.lease)
  139. lease_time_align = server_config.lease;
  140. }
  141. /* Make sure we aren't just using the lease time from the previous offer */
  142. if (lease_time_align < server_config.min_lease)
  143. lease_time_align = server_config.lease;
  144. /* ADDME: end of short circuit */
  145. } else {
  146. /* It is a static lease... use it */
  147. packet.yiaddr = static_lease_ip;
  148. }
  149. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  150. curr = server_config.options;
  151. while (curr) {
  152. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  153. add_option_string(packet.options, curr->data);
  154. curr = curr->next;
  155. }
  156. add_bootp_options(&packet);
  157. addr.s_addr = packet.yiaddr;
  158. bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
  159. return send_packet(&packet, 0);
  160. }
  161. int sendNAK(struct dhcpMessage *oldpacket)
  162. {
  163. struct dhcpMessage packet;
  164. init_packet(&packet, oldpacket, DHCPNAK);
  165. DEBUG("Sending NAK");
  166. return send_packet(&packet, 1);
  167. }
  168. int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
  169. {
  170. struct dhcpMessage packet;
  171. struct option_set *curr;
  172. uint8_t *lease_time;
  173. uint32_t lease_time_align = server_config.lease;
  174. struct in_addr addr;
  175. init_packet(&packet, oldpacket, DHCPACK);
  176. packet.yiaddr = yiaddr;
  177. if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
  178. memcpy(&lease_time_align, lease_time, 4);
  179. lease_time_align = ntohl(lease_time_align);
  180. if (lease_time_align > server_config.lease)
  181. lease_time_align = server_config.lease;
  182. else if (lease_time_align < server_config.min_lease)
  183. lease_time_align = server_config.lease;
  184. }
  185. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  186. curr = server_config.options;
  187. while (curr) {
  188. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  189. add_option_string(packet.options, curr->data);
  190. curr = curr->next;
  191. }
  192. add_bootp_options(&packet);
  193. addr.s_addr = packet.yiaddr;
  194. bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
  195. if (send_packet(&packet, 0) < 0)
  196. return -1;
  197. add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
  198. return 0;
  199. }
  200. int send_inform(struct dhcpMessage *oldpacket)
  201. {
  202. struct dhcpMessage packet;
  203. struct option_set *curr;
  204. init_packet(&packet, oldpacket, DHCPACK);
  205. curr = server_config.options;
  206. while (curr) {
  207. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  208. add_option_string(packet.options, curr->data);
  209. curr = curr->next;
  210. }
  211. add_bootp_options(&packet);
  212. return send_packet(&packet, 0);
  213. }