serverpacket.c 7.7 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 "dhcpc.h"
  24. #include "dhcpd.h"
  25. #include "options.h"
  26. /* send a packet to giaddr using the kernel ip stack */
  27. static int send_packet_to_relay(struct dhcpMessage *payload)
  28. {
  29. DEBUG("Forwarding packet to relay");
  30. return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
  31. payload->giaddr, SERVER_PORT);
  32. }
  33. /* send a packet to a specific arp address and ip address by creating our own ip packet */
  34. static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
  35. {
  36. const uint8_t *chaddr;
  37. uint32_t ciaddr;
  38. if (force_broadcast) {
  39. DEBUG("broadcasting packet to client (NAK)");
  40. ciaddr = INADDR_BROADCAST;
  41. chaddr = MAC_BCAST_ADDR;
  42. } else if (payload->ciaddr) {
  43. DEBUG("unicasting packet to client ciaddr");
  44. ciaddr = payload->ciaddr;
  45. chaddr = payload->chaddr;
  46. } else if (payload->flags & htons(BROADCAST_FLAG)) {
  47. DEBUG("broadcasting packet to client (requested)");
  48. ciaddr = INADDR_BROADCAST;
  49. chaddr = MAC_BCAST_ADDR;
  50. } else {
  51. DEBUG("unicasting packet to client yiaddr");
  52. ciaddr = payload->yiaddr;
  53. chaddr = payload->chaddr;
  54. }
  55. return udhcp_send_raw_packet(payload,
  56. /*src*/ server_config.server, SERVER_PORT,
  57. /*dst*/ ciaddr, CLIENT_PORT, chaddr,
  58. server_config.ifindex);
  59. }
  60. /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
  61. static int send_packet(struct dhcpMessage *payload, int force_broadcast)
  62. {
  63. if (payload->giaddr)
  64. return send_packet_to_relay(payload);
  65. return send_packet_to_client(payload, force_broadcast);
  66. }
  67. static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
  68. {
  69. udhcp_init_header(packet, type);
  70. packet->xid = oldpacket->xid;
  71. memcpy(packet->chaddr, oldpacket->chaddr, 16);
  72. packet->flags = oldpacket->flags;
  73. packet->giaddr = oldpacket->giaddr;
  74. packet->ciaddr = oldpacket->ciaddr;
  75. add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
  76. }
  77. /* add in the bootp options */
  78. static void add_bootp_options(struct dhcpMessage *packet)
  79. {
  80. packet->siaddr = server_config.siaddr;
  81. if (server_config.sname)
  82. strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
  83. if (server_config.boot_file)
  84. strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
  85. }
  86. /* send a DHCP OFFER to a DHCP DISCOVER */
  87. int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
  88. {
  89. struct dhcpMessage packet;
  90. struct dhcpOfferedAddr *lease = NULL;
  91. uint32_t req_align, lease_time_align = server_config.lease;
  92. uint8_t *req, *lease_time;
  93. struct option_set *curr;
  94. struct in_addr addr;
  95. uint32_t static_lease_ip;
  96. init_packet(&packet, oldpacket, DHCPOFFER);
  97. static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
  98. /* ADDME: if static, short circuit */
  99. if (!static_lease_ip) {
  100. /* the client is in our lease/offered table */
  101. lease = find_lease_by_chaddr(oldpacket->chaddr);
  102. if (lease) {
  103. if (!lease_expired(lease))
  104. lease_time_align = lease->expires - time(0);
  105. packet.yiaddr = lease->yiaddr;
  106. /* Or the client has a requested ip */
  107. } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))
  108. /* Don't look here (ugly hackish thing to do) */
  109. && memcpy(&req_align, req, 4)
  110. /* and the ip is in the lease range */
  111. && ntohl(req_align) >= server_config.start_ip
  112. && ntohl(req_align) <= server_config.end_ip
  113. && !static_lease_ip /* Check that its not a static lease */
  114. /* and is not already taken/offered */
  115. && (!(lease = find_lease_by_yiaddr(req_align))
  116. /* or its taken, but expired */ /* ADDME: or maybe in here */
  117. || lease_expired(lease))
  118. ) {
  119. packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
  120. /* otherwise, find a free IP */
  121. } else {
  122. /* Is it a static lease? (No, because find_address skips static lease) */
  123. packet.yiaddr = find_address(0);
  124. /* try for an expired lease */
  125. if (!packet.yiaddr)
  126. packet.yiaddr = find_address(1);
  127. }
  128. if (!packet.yiaddr) {
  129. bb_error_msg("no IP addresses to give - OFFER abandoned");
  130. return -1;
  131. }
  132. if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
  133. bb_error_msg("lease pool is full - OFFER abandoned");
  134. return -1;
  135. }
  136. lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
  137. if (lease_time) {
  138. memcpy(&lease_time_align, lease_time, 4);
  139. lease_time_align = ntohl(lease_time_align);
  140. if (lease_time_align > server_config.lease)
  141. lease_time_align = server_config.lease;
  142. }
  143. /* Make sure we aren't just using the lease time from the previous offer */
  144. if (lease_time_align < server_config.min_lease)
  145. lease_time_align = server_config.lease;
  146. /* ADDME: end of short circuit */
  147. } else {
  148. /* It is a static lease... use it */
  149. packet.yiaddr = static_lease_ip;
  150. }
  151. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  152. curr = server_config.options;
  153. while (curr) {
  154. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  155. add_option_string(packet.options, curr->data);
  156. curr = curr->next;
  157. }
  158. add_bootp_options(&packet);
  159. addr.s_addr = packet.yiaddr;
  160. bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
  161. return send_packet(&packet, 0);
  162. }
  163. int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket)
  164. {
  165. struct dhcpMessage packet;
  166. init_packet(&packet, oldpacket, DHCPNAK);
  167. DEBUG("Sending NAK");
  168. return send_packet(&packet, 1);
  169. }
  170. int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
  171. {
  172. struct dhcpMessage packet;
  173. struct option_set *curr;
  174. uint8_t *lease_time;
  175. uint32_t lease_time_align = server_config.lease;
  176. struct in_addr addr;
  177. init_packet(&packet, oldpacket, DHCPACK);
  178. packet.yiaddr = yiaddr;
  179. lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
  180. if (lease_time) {
  181. memcpy(&lease_time_align, lease_time, 4);
  182. lease_time_align = ntohl(lease_time_align);
  183. if (lease_time_align > server_config.lease)
  184. lease_time_align = server_config.lease;
  185. else if (lease_time_align < server_config.min_lease)
  186. lease_time_align = server_config.lease;
  187. }
  188. add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
  189. curr = server_config.options;
  190. while (curr) {
  191. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  192. add_option_string(packet.options, curr->data);
  193. curr = curr->next;
  194. }
  195. add_bootp_options(&packet);
  196. addr.s_addr = packet.yiaddr;
  197. bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
  198. if (send_packet(&packet, 0) < 0)
  199. return -1;
  200. add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
  201. if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
  202. /* rewrite the file with leases at every new acceptance */
  203. write_leases();
  204. }
  205. return 0;
  206. }
  207. int FAST_FUNC send_inform(struct dhcpMessage *oldpacket)
  208. {
  209. struct dhcpMessage packet;
  210. struct option_set *curr;
  211. init_packet(&packet, oldpacket, DHCPACK);
  212. curr = server_config.options;
  213. while (curr) {
  214. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  215. add_option_string(packet.options, curr->data);
  216. curr = curr->next;
  217. }
  218. add_bootp_options(&packet);
  219. return send_packet(&packet, 0);
  220. }