dhcp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 1983, 1993 by The Regents of the University of California
  3. * Copyright (C) 2001-2007 by egnite Software GmbH
  4. * Copyright (C) 2010 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. *
  36. * -
  37. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  38. *
  39. * Permission to use, copy, modify, and distribute this software for any
  40. * purpose with or without fee is hereby granted, provided that the above
  41. * copyright notice and this permission notice appear in all copies, and that
  42. * the name of Digital Equipment Corporation not be used in advertising or
  43. * publicity pertaining to distribution of the document or software without
  44. * specific, written prior permission.
  45. *
  46. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  47. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  48. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  49. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  50. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  51. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  52. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  53. * SOFTWARE.
  54. */
  55. /*
  56. * $Id: dhcp.c 4115 2012-04-12 21:06:13Z olereinhardt $
  57. */
  58. #include "dialog.h"
  59. #include "dhcp.h"
  60. #include "bootmon.h"
  61. BOOTPHDR test;
  62. /*!
  63. * \addtogroup xgDhcp
  64. */
  65. /*@{*/
  66. /*!
  67. * \brief Retrieve the specified DCHP option.
  68. *
  69. * \param opt Option to look for.
  70. * \param ptr Pointer to the buffer that receives the option value.
  71. * \param size Size of the buffer.
  72. *
  73. * \return Size of the retrieved option value or zero,
  74. * if the specified option couldn't be found.
  75. */
  76. unsigned char DhcpGetOption(unsigned char opt, void *ptr, unsigned char size)
  77. {
  78. register unsigned char *cp;
  79. unsigned char i;
  80. cp = rframe.eth.udp.u.bootp.bp_options;
  81. for (;;) {
  82. if (*cp == DHCPOPT_PAD) {
  83. cp++;
  84. continue;
  85. }
  86. if (*cp == DHCPOPT_END)
  87. break;
  88. if (*cp == opt) {
  89. if (*(cp + 1) < size)
  90. size = *(cp + 1);
  91. for (i = 0; i < size; i++)
  92. *(((unsigned char *) ptr) + i) = *(cp + i + 2);
  93. return *(cp + 1);
  94. }
  95. cp++;
  96. cp += *cp;
  97. cp++;
  98. }
  99. return 0;
  100. }
  101. /*!
  102. * \brief Add specified DHCP option.
  103. *
  104. * \param dst Pointer to the buffer that receives the option.
  105. * \param opt Type of option to set.
  106. * \param src Pointer to the buffer that contains the option value.
  107. * \param size Size of the option value.
  108. *
  109. * \return Pointer to the option buffer to receive the next option.
  110. */
  111. unsigned char *DhcpSetOption(unsigned char *dst, unsigned char opt, unsigned char *src, unsigned char size)
  112. {
  113. *dst++ = opt;
  114. *dst++ = size;
  115. while (size--)
  116. *dst++ = *src++;
  117. *dst = DHCPOPT_END;
  118. return dst;
  119. }
  120. /*!
  121. * \brief Send DHCP datagram and wait for a specified answer.
  122. *
  123. * \param slen Length of message to send.
  124. * \param xtype Expected response type.
  125. *
  126. * \return The number of data bytes received, 0 on timeout or -1 in
  127. * case of a failure.
  128. */
  129. int DhcpTransact(unsigned short slen, unsigned char xtype)
  130. {
  131. unsigned char type;
  132. unsigned char retry;
  133. int rlen = 0;
  134. for (retry = 0; retry < 3;) {
  135. /*
  136. * Send a message, if nothing has been received yet.
  137. */
  138. DEBUG("[RQ DHCP]");
  139. if (rlen == 0) {
  140. if (UdpOutput(INADDR_BROADCAST, DHCP_SERVERPORT, DHCP_CLIENTPORT, slen) < 0) {
  141. /* Transmit failure, must be a NIC problem. Give up. */
  142. return -1;
  143. }
  144. }
  145. /*
  146. * Do a retry on timouts or failures. A receive failures may be
  147. * caused by a hardware failure or a bad frame.
  148. */
  149. if ((rlen = UdpInput(DHCP_CLIENTPORT, 500)) <= 0) {
  150. retry++;
  151. continue;
  152. }
  153. /*
  154. * Check if the response contains the expected ID and
  155. * message type.
  156. */
  157. if (rframe.eth.udp.u.bootp.bp_xid == sframe.eth.udp.u.bootp.bp_xid && DhcpGetOption(DHCPOPT_MSGTYPE, &type, 1) == 1 && type == xtype) {
  158. DEBUG("[DHCP]");
  159. break;
  160. }
  161. }
  162. return rlen;
  163. }
  164. /*!
  165. * \brief Query any DHCP server on the local net.
  166. *
  167. * On success, this routine will fill some global
  168. * variables:
  169. *
  170. * - my_ip
  171. * - server_ip
  172. * - bootfile
  173. * - my_netmask
  174. *
  175. * \return 0 on success, -1 otherwise.
  176. */
  177. int DhcpQuery(void)
  178. {
  179. BOOTPHDR *bp;
  180. unsigned short slen;
  181. unsigned char i;
  182. unsigned long sid;
  183. register unsigned char *cp;
  184. /*
  185. * Nothing to do if we got a fixed IP address.
  186. */
  187. if (confnet.cdn_cip_addr) {
  188. DEBUG("DHCP skipped\n");
  189. confnet.cdn_ip_addr = confnet.cdn_cip_addr;
  190. return 0;
  191. }
  192. confnet.cdn_ip_addr = 0;
  193. /*
  194. * Setup bootp message.
  195. */
  196. bp = &sframe.eth.udp.u.bootp;
  197. bp->bp_op = 1;
  198. bp->bp_xid = random_id;
  199. bp->bp_htype = 1;
  200. bp->bp_hlen = sizeof(confnet.cdn_mac);
  201. memcpy_(bp->bp_chaddr, confnet.cdn_mac, 6);
  202. /*
  203. * Add DHCP option for discover message.
  204. */
  205. bp->bp_cookie = 0x63538263;
  206. i = DHCP_DISCOVER;
  207. DhcpSetOption(bp->bp_options, DHCPOPT_MSGTYPE, &i, 1);
  208. /*
  209. * Send DHCP discover and wait for any response.
  210. */
  211. slen = sizeof(BOOTPHDR) - sizeof(sframe.eth.udp.u.bootp.bp_options) + 4;
  212. if (DhcpTransact(slen, DHCP_OFFER) <= 0) {
  213. return -1;
  214. }
  215. /*
  216. * Get the server ID option.
  217. */
  218. DhcpGetOption(DHCPOPT_SID, &sid, 4);
  219. /*
  220. * Reuse the bootp structure and add DHCP options for request message.
  221. */
  222. DEBUGULONG(rframe.eth.udp.u.bootp.bp_yiaddr);
  223. i = DHCP_REQUEST;
  224. cp = DhcpSetOption(bp->bp_options, DHCPOPT_MSGTYPE, &i, 1);
  225. cp = DhcpSetOption(cp, DHCPOPT_REQUESTIP, (unsigned char *) &rframe.eth.udp.u.bootp.bp_yiaddr, 4);
  226. DhcpSetOption(cp, DHCPOPT_SID, (unsigned char *) &sid, 4);
  227. /*
  228. * Send DHCP request and wait for ACK.
  229. */
  230. slen = sizeof(BOOTPHDR) - sizeof(sframe.eth.udp.u.bootp.bp_options) + 16;
  231. if (DhcpTransact(slen, DHCP_ACK) <= 0) {
  232. return -1;
  233. }
  234. /*
  235. * Retrieve local IP, bootp server IP, bootfile name and netmask.
  236. */
  237. confnet.cdn_ip_addr = rframe.eth.udp.u.bootp.bp_yiaddr;
  238. if (confboot.cb_tftp_ip == 0) {
  239. confboot.cb_tftp_ip = rframe.eth.udp.u.bootp.bp_siaddr;
  240. }
  241. if (confboot.cb_image[0] == 0) {
  242. for (cp = (unsigned char *)rframe.eth.udp.u.bootp.bp_file, i = 0; *cp && i < sizeof(confboot.cb_image) - 1; cp++, i++) {
  243. confboot.cb_image[i] = *cp;
  244. }
  245. confboot.cb_image[i] = 0;
  246. }
  247. DhcpGetOption(DHCPOPT_NETMASK, &confnet.cdn_ip_mask, 4);
  248. return 0;
  249. }
  250. /*@}*/