dhcp.c 9.7 KB

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