ip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (C) 2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. * -
  36. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  37. *
  38. * Permission to use, copy, modify, and distribute this software for any
  39. * purpose with or without fee is hereby granted, provided that the above
  40. * copyright notice and this permission notice appear in all copies, and that
  41. * the name of Digital Equipment Corporation not be used in advertising or
  42. * publicity pertaining to distribution of the document or software without
  43. * specific, written prior permission.
  44. *
  45. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  46. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  47. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  48. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  49. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  50. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  51. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  52. * SOFTWARE.
  53. */
  54. /*
  55. * $Id: ip.c 4115 2012-04-12 21:06:13Z olereinhardt $
  56. */
  57. #include "eboot.h"
  58. #include "config.h"
  59. #include "arp.h"
  60. #include "ip.h"
  61. /*!
  62. * \addtogroup xgStack
  63. */
  64. /*@{*/
  65. /*!
  66. * \brief Calculate the IP checksum over a block of data.
  67. *
  68. * \param data Pointer to the data block.
  69. * \param size Size of the data block.
  70. *
  71. * \return The checksum in network byte order.
  72. */
  73. static u_short IpChkSum(const u_char *data, u_short size)
  74. {
  75. register u_long sum = 0;
  76. for (;;) {
  77. if (size < 2)
  78. break;
  79. sum += *((u_short *) data);
  80. data += 2;
  81. size -= 2;
  82. }
  83. if (size)
  84. sum += *(u_char *) data;
  85. while ((size = (u_short) (sum >> 16)) != 0)
  86. sum = (u_short) sum + size;
  87. return (u_short) sum ^ 0xFFFF;
  88. }
  89. /*!
  90. * \brief Receive an IP packet with the specified protocol type.
  91. *
  92. * This function calls EtherInput(). Any incoming Ethernet
  93. * frame, which is not of the specified type will be discarded.
  94. *
  95. * \param proto Protocol type.
  96. * \param tms Return with timeout after the specified
  97. * number of waiting loops. On a 14 Mhz ATmega
  98. * this value represents approximately the number
  99. * of milliseconds to wait.
  100. *
  101. * \return The number of bytes received, 0 on timeout or -1 in case of
  102. * a failure.
  103. */
  104. int IpInput(u_char proto, u_short tms)
  105. {
  106. int rc;
  107. IPHDR *ip = &rframe.ip_hdr;
  108. for (;;) {
  109. /*
  110. * Get the next IP packet.
  111. */
  112. if ((rc = EtherInput(ETHERTYPE_IP, tms)) <= 0)
  113. break;
  114. /*
  115. * Discard packets of different IP version.
  116. */
  117. if (ip->ip_v != IPVERSION)
  118. continue;
  119. /*
  120. * Discard fragmented packets.
  121. */
  122. if ((ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
  123. continue;
  124. /*
  125. * Discard packets with different protocols.
  126. */
  127. if (ip->ip_p != proto)
  128. continue;
  129. /*
  130. * Accept this packet, if it is addressed to us.
  131. */
  132. rc = htons(ip->ip_len) - (ip->ip_hl * 4);
  133. if(ip->ip_dst == INADDR_BROADCAST)
  134. break;
  135. if(confnet.cdn_ip_addr == 0)
  136. continue;
  137. if(ip->ip_dst == confnet.cdn_ip_addr)
  138. break;
  139. if((ip->ip_dst | confnet.cdn_ip_mask) == INADDR_BROADCAST)
  140. break;
  141. }
  142. return rc;
  143. }
  144. /*!
  145. * \brief Send an IP packet.
  146. *
  147. * This function fills the IP header of the global send frame and calls
  148. * EtherOutput(). Routing is not supported.
  149. *
  150. * \param dip Destination IP address in network byte order.
  151. * \param proto Ethernet protocol type.
  152. * \param len Number of data bytes to transmit.
  153. *
  154. * \return 0 on success or -1 to indicate an error.
  155. */
  156. int IpOutput(u_long dip, u_char proto, u_short len)
  157. {
  158. u_char dmac[6];
  159. register IPHDR *ip = &sframe.ip_hdr;
  160. /*
  161. * Get the Ethernet hardware address.
  162. */
  163. if (ArpRequest(dip, dmac))
  164. return -1;
  165. /*
  166. * Fill the IP header.
  167. */
  168. ip->ip_v = IPVERSION;
  169. ip->ip_hl = sizeof(IPHDR) >> 2;
  170. ip->ip_len = htons(sizeof(IPHDR) + len);
  171. ip->ip_ttl = 0x40;
  172. ip->ip_p = proto;
  173. ip->ip_dst = dip;
  174. ip->ip_src = confnet.cdn_ip_addr;
  175. ip->ip_id++;
  176. ip->ip_sum = 0;
  177. ip->ip_sum = IpChkSum((u_char *)ip, sizeof(IPHDR));
  178. return EtherOutput(dmac, ETHERTYPE_IP, sizeof(IPHDR) + len);
  179. }
  180. /*@}*/