PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

sys/lib/libsa/ip.c

http://www.minix3.org/
C | 190 lines | 114 code | 20 blank | 56 comment | 35 complexity | c0f6cabd9d3a1ee2d1991b04371b0839 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* $NetBSD: ip.c,v 1.2 2011/05/13 23:35:09 nakayama Exp $ */
  2. /*
  3. * Copyright (c) 1992 Regents of the University of California.
  4. * Copyright (c) 2010 Zoltan Arnold NAGY
  5. * All rights reserved.
  6. *
  7. * This software was developed by the Computer Systems Engineering group
  8. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  9. * contributed to Berkeley.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. All advertising materials mentioning features or use of this software
  20. * must display the following acknowledgement:
  21. * This product includes software developed by the University of
  22. * California, Lawrence Berkeley Laboratory and its contributors.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. */
  40. #include <sys/param.h>
  41. #include <sys/socket.h>
  42. #include <net/if.h>
  43. #include <net/if_ether.h>
  44. #include <netinet/in.h>
  45. #include <netinet/in_systm.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/ip_var.h>
  48. #include <netinet/udp.h>
  49. #include <netinet/udp_var.h>
  50. #ifdef _STANDALONE
  51. #include <lib/libkern/libkern.h>
  52. #else
  53. #include <string.h>
  54. #endif
  55. #include "stand.h"
  56. #include "net.h"
  57. /*
  58. * sends an IP packet, if it's alredy constructed
  59. */
  60. static ssize_t
  61. _sendip(struct iodesc * d, struct ip * ip, size_t len)
  62. {
  63. u_char *ea;
  64. if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
  65. netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) {
  66. ea = arpwhohas(d, ip->ip_dst);
  67. } else {
  68. ea = arpwhohas(d, gateip);
  69. }
  70. return sendether(d, ip, len, ea, ETHERTYPE_IP);
  71. }
  72. /*
  73. * fills out the IP header
  74. * Caller must leave room for ethernet, ip and udp headers in front!
  75. */
  76. ssize_t
  77. sendip(struct iodesc * d, void *pkt, size_t len, u_int8_t proto)
  78. {
  79. ssize_t cc;
  80. struct ip *ip;
  81. ip = (struct ip *) pkt - 1;
  82. len += sizeof(*ip);
  83. (void) memset(ip, 0, sizeof(*ip));
  84. ip->ip_v = IPVERSION;
  85. ip->ip_hl = sizeof(*ip) >> 2;
  86. ip->ip_len = htons(len);
  87. ip->ip_p = proto;
  88. ip->ip_ttl = IPDEFTTL;
  89. ip->ip_src = d->myip;
  90. ip->ip_dst = d->destip;
  91. ip->ip_sum = ip_cksum(ip, sizeof(*ip));
  92. cc = _sendip(d, ip, len);
  93. if (cc == -1)
  94. return -1;
  95. if ((size_t) cc != len)
  96. panic("sendip: bad write (%zd != %zu)", cc, len);
  97. return (cc - (sizeof(*ip)));
  98. }
  99. /*
  100. * reads an IP packet
  101. * WARNING: the old version stripped the IP options, if there were
  102. * any. Because we have absolutely no idea if the upper layer needs
  103. * these or not, it's best to leave them there.
  104. *
  105. * The size returned is the size indicated in the header.
  106. */
  107. ssize_t
  108. readip(struct iodesc * d, void *pkt, size_t len, time_t tleft, u_int8_t proto)
  109. {
  110. ssize_t n;
  111. size_t hlen;
  112. struct ip *ip;
  113. u_int16_t etype;
  114. ip = (struct ip *) pkt - 1;
  115. n = readether(d, ip, len + sizeof(*ip), tleft, &etype);
  116. if (n == -1 || (size_t) n < sizeof(*ip))
  117. return -1;
  118. if (etype == ETHERTYPE_ARP) {
  119. struct arphdr *ah = (void *) ip;
  120. if (ah->ar_op == htons(ARPOP_REQUEST)) {
  121. /* Send ARP reply */
  122. arp_reply(d, ah);
  123. }
  124. return -1;
  125. }
  126. if (etype != ETHERTYPE_IP) {
  127. #ifdef NET_DEBUG
  128. if (debug)
  129. printf("readip: not IP. ether_type=%x\n", etype);
  130. #endif
  131. return -1;
  132. }
  133. /* Check ip header */
  134. if (ip->ip_v != IPVERSION ||
  135. ip->ip_p != proto) { /* half char */
  136. #ifdef NET_DEBUG
  137. if (debug) {
  138. printf("readip: wrong IP version or wrong proto "
  139. "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
  140. }
  141. #endif
  142. return -1;
  143. }
  144. hlen = ip->ip_hl << 2;
  145. if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) {
  146. #ifdef NET_DEBUG
  147. if (debug)
  148. printf("readip: short hdr or bad cksum.\n");
  149. #endif
  150. return -1;
  151. }
  152. if (n < ntohs(ip->ip_len)) {
  153. #ifdef NET_DEBUG
  154. if (debug)
  155. printf("readip: bad length %d < %d.\n",
  156. (int) n, ntohs(ip->ip_len));
  157. #endif
  158. return -1;
  159. }
  160. if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
  161. #ifdef NET_DEBUG
  162. if (debug) {
  163. printf("readip: bad saddr %s != ", inet_ntoa(d->myip));
  164. printf("%s\n", inet_ntoa(ip->ip_dst));
  165. }
  166. #endif
  167. return -1;
  168. }
  169. return (ntohs(ip->ip_len) - 20);
  170. }