ipout.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2001-2007 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  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. * \file net/ipout.c
  38. * \brief IP output functions.
  39. *
  40. * \verbatim
  41. * $Id: ipout.c 5505 2014-01-01 11:15:16Z mifi $
  42. * \endverbatim
  43. */
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include <net/ether.h>
  47. #include <net/route.h>
  48. #include <netinet/if_ether.h>
  49. #include <netinet/if_ppp.h>
  50. #include <netinet/ipcsum.h>
  51. #include <netinet/ip.h>
  52. #include <netinet/in.h>
  53. /*!
  54. * \addtogroup xgIP
  55. */
  56. /*@{*/
  57. /*!
  58. * \brief Send IP datagram.
  59. *
  60. * Route an IP datagram to the proper interface.
  61. *
  62. * The function will not return until the data has been stored
  63. * in the network device hardware for transmission. If the
  64. * device is not ready for transmitting a new packet, the
  65. * calling thread will be suspended until the device becomes
  66. * ready again. If the hardware address of the target host needs
  67. * to be resolved the function will be suspended too.
  68. *
  69. * \param proto Protocol type.
  70. * \param dest Destination IP address. The function will determine
  71. * the proper network interface by checking the routing
  72. * table. It will also perform any necessary hardware
  73. * address resolution.
  74. * \param nb Network buffer structure containing the datagram.
  75. * This buffer will be released if the function returns
  76. * an error.
  77. *
  78. * \return 0 on success, -1 otherwise.
  79. *
  80. * \bug Broadcasts to multiple network devices will fail after the
  81. * first device returns an error.
  82. */
  83. int NutIpOutput(uint8_t proto, uint32_t dest, NETBUF * nb)
  84. {
  85. uint8_t ha[6];
  86. IPHDR_OPT *ip;
  87. NUTDEVICE *dev;
  88. IFNET *nif;
  89. uint32_t gate;
  90. if (proto != IPPROTO_IGMP) {
  91. if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(IPHDR))) == 0)
  92. return -1;
  93. } else {
  94. if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(IPHDR_OPT))) == 0)
  95. return -1;
  96. }
  97. /*
  98. * Set those items in the IP header, which are common for
  99. * all interfaces.
  100. */
  101. ip = nb->nb_nw.vp;
  102. ip->ip_v = 4;
  103. if (proto == IPPROTO_IGMP) {
  104. ip->ip_hl = sizeof(IPHDR_OPT) / 4;
  105. } else {
  106. ip->ip_hl = sizeof(IPHDR) / 4;
  107. }
  108. ip->ip_tos = 0;
  109. ip->ip_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
  110. ip->ip_off = 0;
  111. if (proto == IPPROTO_IGMP) {
  112. ip->ip_ttl = 1;
  113. } else {
  114. ip->ip_ttl = 0x40;
  115. }
  116. ip->ip_p = proto;
  117. ip->ip_dst = dest;
  118. if (proto == IPPROTO_IGMP) {
  119. /* Router Alert Option */
  120. ip->ip_option = htonl(0x94040000);
  121. }
  122. /*
  123. * Limited broadcasts are sent on all network interfaces.
  124. * See RFC 919.
  125. */
  126. if ((dest == 0xffffffff) || (IP_IS_MULTICAST(dest))) {
  127. if (dest == 0xffffffff) {
  128. /* Broadcast */
  129. memset(ha, 0xff, sizeof(ha));
  130. } else {
  131. /* Multicast */
  132. ha[0] = 0x01;
  133. ha[1] = 0x00;
  134. ha[2] = 0x5E;
  135. ha[3] = ((uint8_t *) & dest)[1] & 0x7f;
  136. ha[4] = ((uint8_t *) & dest)[2];
  137. ha[5] = ((uint8_t *) & dest)[3];
  138. }
  139. for (dev = nutDeviceList; dev; dev = dev->dev_next) {
  140. if (dev->dev_type == IFTYP_NET) {
  141. /*
  142. * Set remaining IP header items using a NETBUF clone and calculate
  143. * the checksum.
  144. */
  145. int rc = 0;
  146. NETBUF *nb_clone = NutNetBufClonePart(nb, NBAF_NETWORK);
  147. nif = dev->dev_icb;
  148. ip = nb_clone->nb_nw.vp;
  149. ip->ip_id = htons(nif->if_pkt_id);
  150. nif->if_pkt_id++;
  151. ip->ip_src = nif->if_local_ip;
  152. ip->ip_ttl = 1;
  153. ip->ip_sum = 0;
  154. ip->ip_sum = NutIpChkSum(0, nb_clone->nb_nw.vp, nb_clone->nb_nw.sz);
  155. if (nif->if_type == IFT_ETHER)
  156. rc = (*nif->if_output) (dev, ETHERTYPE_IP, ha, nb_clone);
  157. else if (nif->if_type == IFT_PPP)
  158. rc = (*nif->if_output) (dev, PPP_IP, 0, nb_clone);
  159. if (rc == 0) {
  160. NutNetBufFree(nb_clone);
  161. }
  162. }
  163. }
  164. return 0;
  165. }
  166. /*
  167. * Get destination's route. This will also return the proper
  168. * interface.
  169. */
  170. if ((dev = NutIpRouteQuery(dest, &gate)) == 0) {
  171. NutNetBufFree(nb);
  172. return -1;
  173. }
  174. /*
  175. * Set remaining IP header items and calculate the checksum.
  176. */
  177. nif = dev->dev_icb;
  178. ip->ip_id = htons(nif->if_pkt_id);
  179. nif->if_pkt_id++;
  180. ip->ip_src = nif->if_local_ip;
  181. ip->ip_sum = 0;
  182. ip->ip_sum = NutIpChkSum(0, nb->nb_nw.vp, nb->nb_nw.sz);
  183. /*
  184. * On Ethernet we query the MAC address of our next hop,
  185. * which might be the destination or the gateway to this
  186. * destination.
  187. */
  188. if (nif->if_type == IFT_ETHER) {
  189. /*
  190. * Detect directed broadcasts for the local network. In this
  191. * case don't send ARP queries, but send directly to MAC broadcast
  192. * address.
  193. */
  194. if ((gate == 0) && ((dest | nif->if_mask) == 0xffffffff)) {
  195. memset(ha, 0xff, sizeof(ha));
  196. } else if (NutArpCacheQuery(dev, gate ? gate : dest, ha)) {
  197. /* Note, that a failed ARP request is not considered a
  198. transmission error. It might be caused by a simple
  199. packet loss. */
  200. return 0;
  201. }
  202. return (*nif->if_output) (dev, ETHERTYPE_IP, ha, nb);
  203. } else if (nif->if_type == IFT_PPP)
  204. return (*nif->if_output) (dev, PPP_IP, 0, nb);
  205. NutNetBufFree(nb);
  206. return -1;
  207. }
  208. /*!
  209. * \brief Forward IP datagram.
  210. *
  211. * Forwards IP datagram to the proper destination if NET_IP_FORWARD has
  212. * been configured.
  213. *
  214. * \param nb Network buffer structure containing the datagram.
  215. * This buffer will be released if the function returns
  216. * an error.
  217. *
  218. * \return Always 0.
  219. */
  220. int NutIpForward(NETBUF *nb)
  221. {
  222. #ifdef NUT_IP_FORWARDING
  223. NUTDEVICE *dev;
  224. IFNET *nif;
  225. NETBUF *r_nb;
  226. IPHDR *ip;
  227. uint32_t dest;
  228. /* Check the time to live. */
  229. ip = nb->nb_nw.vp;
  230. if (ip->ip_ttl <= 1) {
  231. return 0;
  232. }
  233. /* Retrieve interface and gateway to the destination. */
  234. dev = NutIpRouteQuery(ip->ip_dst, &dest);
  235. if (dev == NULL) {
  236. return 0;
  237. }
  238. if (dest == 0) {
  239. dest = ip->ip_dst;
  240. }
  241. nif = dev->dev_icb;
  242. /* Allocate a new NETBUF and copy the payload to it. */
  243. r_nb = NutNetBufAlloc(NULL, NBAF_TRANSPORT, nb->nb_tp.sz);
  244. if (r_nb == NULL) {
  245. return 0;
  246. }
  247. memcpy(r_nb->nb_tp.vp, nb->nb_tp.vp, nb->nb_tp.sz);
  248. /* Copy the IP header to the new NETBUF. */
  249. r_nb = NutNetBufAlloc(r_nb, NBAF_NETWORK, nb->nb_nw.sz);
  250. if (r_nb == NULL) {
  251. return 0;
  252. }
  253. memcpy(r_nb->nb_nw.vp, nb->nb_nw.vp, nb->nb_nw.sz);
  254. ip = r_nb->nb_nw.vp;
  255. ip->ip_ttl--;
  256. ip->ip_sum = 0;
  257. ip->ip_sum = NutIpChkSum(0, ip, sizeof(IPHDR));
  258. /* Forward the new NETBUF to the interface. */
  259. if (nif->if_type == IFT_ETHER) {
  260. uint8_t ha[6];
  261. if (NutArpCacheQuery(dev, dest, ha)) {
  262. return 0;
  263. }
  264. if ((*nif->if_output) (dev, ETHERTYPE_IP, ha, r_nb)) {
  265. return 0;
  266. }
  267. }
  268. else if (nif->if_type == IFT_PPP) {
  269. if ((*nif->if_output) (dev, PPP_IP, 0, r_nb)) {
  270. return 0;
  271. }
  272. }
  273. NutNetBufFree(r_nb);
  274. #else
  275. (void)nb;
  276. #endif
  277. return 0;
  278. }
  279. /*@}*/