ip.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2001-2007 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) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  37. *
  38. * This file is distributed in the hope that it will be useful, but WITHOUT
  39. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  40. * FITNESS FOR A PARTICULAR PURPOSE.
  41. *
  42. * You can redistribute this file and/or modify it under the terms of the GNU
  43. * General Public License (GPL) as published by the Free Software Foundation;
  44. * either version 2 of the License, or (at your discretion) any later version.
  45. * See the accompanying file "copying-gpl.txt" for more details.
  46. *
  47. * As a special exception to the GPL, permission is granted for additional
  48. * uses of the text contained in this file. See the accompanying file
  49. * "copying-liquorice.txt" for details.
  50. *
  51. * -
  52. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  53. *
  54. * Permission to use, copy, modify, and distribute this software for any
  55. * purpose with or without fee is hereby granted, provided that the above
  56. * copyright notice and this permission notice appear in all copies, and that
  57. * the name of Digital Equipment Corporation not be used in advertising or
  58. * publicity pertaining to distribution of the document or software without
  59. * specific, written prior permission.
  60. *
  61. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  62. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  63. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  64. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  65. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  66. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  67. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  68. * SOFTWARE.
  69. */
  70. /*
  71. * $Id: ip.c 4115 2012-04-12 21:06:13Z olereinhardt $
  72. */
  73. #include "dialog.h"
  74. #include "arp.h"
  75. #include "ip.h"
  76. #include "bootmon.h"
  77. /*!
  78. * \addtogroup xgStack
  79. */
  80. /*@{*/
  81. /*!
  82. * \brief Calculate the IP checksum over a block of data.
  83. *
  84. * \param data Pointer to the data block.
  85. * \param size Size of the data block.
  86. *
  87. * \return The checksum in network byte order.
  88. */
  89. unsigned short IpChkSum(const unsigned char *data, unsigned short size)
  90. {
  91. register unsigned long sum = 0;
  92. for (;;) {
  93. if (size < 2)
  94. break;
  95. sum += *data++;
  96. sum += *data++ << 8;
  97. size -= 2;
  98. }
  99. if (size)
  100. sum += *data;
  101. while ((size = (unsigned short) (sum >> 16)) != 0)
  102. sum = (unsigned short) sum + size;
  103. return (unsigned short) sum ^ 0xFFFF;
  104. }
  105. /*!
  106. * \brief Receive an IP packet with the specified protocol type.
  107. *
  108. * This function calls EtherInput(). Any incoming Ethernet
  109. * frame, which is not of the specified type will be discarded.
  110. *
  111. * \param proto Protocol type.
  112. * \param tms Return with timeout after the specified
  113. * number of waiting loops. On a 14 Mhz ATmega
  114. * this value represents approximately the number
  115. * of milliseconds to wait.
  116. *
  117. * \return The number of bytes received, 0 on timeout or -1 in case of
  118. * a failure.
  119. */
  120. int IpInput(unsigned char proto, unsigned int tms)
  121. {
  122. int rc;
  123. int unexp = 10;
  124. IPHDR *ip = &rframe.eth.udp.ip_hdr;
  125. for (;;) {
  126. /*
  127. * Limit the number of unexpected packets. Otherwise we may get
  128. * trapped in an endless loop here, if someone is continuously
  129. * sending us frames we don't want.
  130. */
  131. if (unexp-- == 0) {
  132. rc = 0;
  133. break;
  134. }
  135. /*
  136. * Get the next IP packet.
  137. */
  138. DEBUG("[IP-POLL]");
  139. if ((rc = EtherInput(ETHERTYPE_IP, tms)) <= 0) {
  140. DEBUG("[NoInp]");
  141. break;
  142. }
  143. /*
  144. * Discard packets of different IP version and
  145. * packets with different protocols.
  146. */
  147. if (ip->ip_v == IPVERSION && ip->ip_p == proto) {
  148. /*
  149. * Accept this packet, if it is addressed to us.
  150. */
  151. rc = htons(ip->ip_len) - (ip->ip_hl * 4);
  152. if (ip->ip_dst == INADDR_BROADCAST || confnet.cdn_ip_addr == 0)
  153. break;
  154. if (confnet.cdn_ip_addr) {
  155. if (ip->ip_dst == confnet.cdn_ip_addr) {
  156. break;
  157. }
  158. if ((ip->ip_dst | confnet.cdn_ip_mask) == INADDR_BROADCAST) {
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. return rc;
  165. }
  166. /*!
  167. * \brief Send an IP packet.
  168. *
  169. * This function fills the IP header of the global send frame and calls
  170. * EtherOutput(). Routing is not supported.
  171. *
  172. * \param dip Destination IP address in network byte order.
  173. * \param proto IP protocol type.
  174. * \param len Number of data bytes to transmit.
  175. *
  176. * \return 0 on success or -1 to indicate an error.
  177. */
  178. int IpOutput(unsigned long dip, int proto, unsigned int len)
  179. {
  180. unsigned char dmac[6];
  181. IPHDR *ip;
  182. /* Get the Ethernet hardware address of the destination. */
  183. if (ArpRequest(dip, dmac)) {
  184. return -1;
  185. }
  186. /* Fill the IP header. */
  187. len += sizeof(IPHDR);
  188. ip = &sframe.eth.udp.ip_hdr;
  189. ip->ip_v = IPVERSION;
  190. ip->ip_hl = sizeof(IPHDR) >> 2;
  191. ip->ip_len = htons((unsigned short)len);
  192. ip->ip_ttl = 0x40;
  193. ip->ip_p = (unsigned char)proto;
  194. ip->ip_dst = dip;
  195. ip->ip_src = confnet.cdn_ip_addr;
  196. ip->ip_id++;
  197. ip->ip_sum = 0;
  198. ip->ip_sum = IpChkSum((unsigned char *) ip, sizeof(IPHDR));
  199. /* Send Ethernet frame. */
  200. return EtherOutput(dmac, ETHERTYPE_IP, len);
  201. }
  202. /*@}*/