dhcp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (C) 1983, 1993 by The Regents of the University of California
  3. * Copyright (C) 2001-2004 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 <string.h>
  59. #include "util.h"
  60. #include "config.h"
  61. #include "eboot.h"
  62. #include "ip.h"
  63. #include "debug.h"
  64. #include "dhcp.h"
  65. /*!
  66. * \addtogroup xgDhcp
  67. */
  68. /*@{*/
  69. /*!
  70. * \brief Retrive the specified DCHP option.
  71. *
  72. * \param opt Option to look for.
  73. * \param ptr Pointer to the buffer that receives the option value.
  74. * \param size Size of the buffer.
  75. *
  76. * \return Size of the retrieved option value or zero,
  77. * if the specified option couldn't be found.
  78. */
  79. u_char DhcpGetOption(u_char opt, void *ptr, u_char size)
  80. {
  81. register u_char *cp;
  82. u_char i;
  83. cp = rframe.u.bootp.bp_options;
  84. for (;;) {
  85. if (*cp == DHCPOPT_PAD) {
  86. cp++;
  87. continue;
  88. }
  89. if (*cp == DHCPOPT_END)
  90. break;
  91. if (*cp == opt) {
  92. if (*(cp + 1) < size)
  93. size = *(cp + 1);
  94. for (i = 0; i < size; i++)
  95. *(((u_char *) ptr) + i) = *(cp + i + 2);
  96. return *(cp + 1);
  97. }
  98. cp++;
  99. cp += *cp;
  100. cp++;
  101. }
  102. return 0;
  103. }
  104. u_char *DhcpSetOption(u_char *dst, u_char opt, u_char *src, u_char size)
  105. {
  106. *dst++ = opt;
  107. *dst++ = size;
  108. while(size--)
  109. *dst++ = *src++;
  110. *dst = DHCPOPT_END;
  111. return dst;
  112. }
  113. int DhcpTransact(int slen, u_char xtype)
  114. {
  115. u_char type;
  116. u_char retry;
  117. int rlen;
  118. for (rlen = retry = 0; rlen == 0 && retry < 3; retry++) {
  119. if (UdpOutput(INADDR_BROADCAST, DHCP_SERVERPORT, DHCP_CLIENTPORT, slen) < 0) {
  120. return -1;
  121. }
  122. if ((rlen = UdpInput(DHCP_CLIENTPORT, 20000)) < 0) {
  123. return -1;
  124. }
  125. if (rlen &&
  126. rframe.u.bootp.bp_xid == sframe.u.bootp.bp_xid && DhcpGetOption(DHCPOPT_MSGTYPE, &type, 1) == 1 && type == xtype) {
  127. DEBUG("[DHCP]");
  128. break;
  129. }
  130. rlen = 0;
  131. Delay(1000);
  132. }
  133. return rlen;
  134. }
  135. /*!
  136. * \brief Query any DHCP server on the local net.
  137. *
  138. * On success, this routine will fill some global
  139. * variables:
  140. *
  141. * - local_ip
  142. * - server_ip
  143. * - bootfile
  144. * - netmask
  145. * - broadcast
  146. * - gateway
  147. * - dns
  148. * - sid
  149. *
  150. * \return 0 on success, -1 otherwise.
  151. */
  152. int DhcpQuery(void)
  153. {
  154. BOOTPHDR *bp;
  155. u_short slen;
  156. u_char i;
  157. register u_char *cp;
  158. /*
  159. * Nothing to do if we got a fixed IP address.
  160. */
  161. if (confnet.cdn_cip_addr) {
  162. confnet.cdn_ip_addr = confnet.cdn_cip_addr;
  163. return 0;
  164. }
  165. confnet.cdn_ip_addr = 0;
  166. /*
  167. * Discovery loop.
  168. */
  169. bp = &sframe.u.bootp;
  170. bp->bp_op = 1;
  171. bp->bp_xid = confnet.cdn_mac[4];
  172. bp->bp_xid <<= 8;
  173. bp->bp_xid |= confnet.cdn_mac[5];
  174. bp->bp_flags = 0x0080;
  175. bp->bp_htype = 1;
  176. bp->bp_hlen = sizeof(confnet.cdn_mac);
  177. for (i = 0; i < 6; i++)
  178. bp->bp_chaddr[i] = confnet.cdn_mac[i];
  179. bp->bp_cookie = 0x63538263;
  180. i = DHCP_DISCOVER;
  181. cp = DhcpSetOption(bp->bp_options, DHCPOPT_MSGTYPE, &i, 1);
  182. slen = sizeof(BOOTPHDR) - sizeof(sframe.u.bootp.bp_options) + 4;
  183. if(DhcpTransact(slen, DHCP_OFFER) <= 0) {
  184. DEBUG("[NoOffer]");
  185. return -1;
  186. }
  187. DhcpGetOption(DHCPOPT_SID, &sid, 4);
  188. /*
  189. * Request loop.
  190. */
  191. DEBUGULONG(rframe.u.bootp.bp_yiaddr);
  192. i = DHCP_REQUEST;
  193. cp = DhcpSetOption(bp->bp_options, DHCPOPT_MSGTYPE, &i, 1);
  194. cp = DhcpSetOption(cp, DHCPOPT_REQUESTIP, (u_char *)&rframe.u.bootp.bp_yiaddr, 4);
  195. cp = DhcpSetOption(cp, DHCPOPT_SID, (u_char *)&sid, 4);
  196. slen = sizeof(BOOTPHDR) - sizeof(sframe.u.bootp.bp_options) + 16;
  197. if(DhcpTransact(slen, DHCP_ACK) <= 0) {
  198. DEBUG("[NoAck]");
  199. return -1;
  200. }
  201. confnet.cdn_ip_addr = rframe.u.bootp.bp_yiaddr;
  202. if (confboot.cb_tftp_ip == 0) {
  203. confboot.cb_tftp_ip = rframe.u.bootp.bp_siaddr;
  204. }
  205. DEBUGULONG(confboot.cb_tftp_ip);
  206. if (confboot.cb_image[0] == 0) {
  207. for (cp = (u_char *)rframe.u.bootp.bp_file, i = 0; *cp && i < sizeof(confboot.cb_image) - 1; cp++, i++)
  208. confboot.cb_image[i] = *cp;
  209. confboot.cb_image[i] = 0;
  210. }
  211. DEBUG((char *)confboot.cb_image);
  212. DhcpGetOption(DHCPOPT_NETMASK, &confnet.cdn_ip_mask, 4);
  213. return 0;
  214. }
  215. /*@}*/