ip.c 7.2 KB

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