ip4.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /**
  2. * @file
  3. * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
  4. *
  5. * @see ip_frag.c
  6. *
  7. */
  8. /*
  9. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the lwIP TCP/IP stack.
  35. *
  36. * Author: Adam Dunkels <adam@sics.se>
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_IPV4
  41. #include "lwip/ip.h"
  42. #include "lwip/def.h"
  43. #include "lwip/mem.h"
  44. #include "lwip/ip4_frag.h"
  45. #include "lwip/inet_chksum.h"
  46. #include "lwip/netif.h"
  47. #include "lwip/icmp.h"
  48. #include "lwip/igmp.h"
  49. #include "lwip/priv/raw_priv.h"
  50. #include "lwip/udp.h"
  51. #include "lwip/priv/tcp_priv.h"
  52. #include "lwip/autoip.h"
  53. #include "lwip/stats.h"
  54. #include "lwip/prot/iana.h"
  55. #include <string.h>
  56. #ifdef LWIP_HOOK_FILENAME
  57. #include LWIP_HOOK_FILENAME
  58. #endif
  59. /** Set this to 0 in the rare case of wanting to call an extra function to
  60. * generate the IP checksum (in contrast to calculating it on-the-fly). */
  61. #ifndef LWIP_INLINE_IP_CHKSUM
  62. #if LWIP_CHECKSUM_CTRL_PER_NETIF
  63. #define LWIP_INLINE_IP_CHKSUM 0
  64. #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
  65. #define LWIP_INLINE_IP_CHKSUM 1
  66. #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
  67. #endif
  68. #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
  69. #define CHECKSUM_GEN_IP_INLINE 1
  70. #else
  71. #define CHECKSUM_GEN_IP_INLINE 0
  72. #endif
  73. #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
  74. #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
  75. /** Some defines for DHCP to let link-layer-addressed packets through while the
  76. * netif is down.
  77. * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port)
  78. * to return 1 if the port is accepted and 0 if the port is not accepted.
  79. */
  80. #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
  81. /* accept DHCP client port and custom port */
  82. #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \
  83. || (LWIP_IP_ACCEPT_UDP_PORT(port)))
  84. #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
  85. /* accept custom port only */
  86. #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
  87. #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
  88. /* accept DHCP client port only */
  89. #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT))
  90. #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
  91. #else /* LWIP_DHCP */
  92. #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
  93. #endif /* LWIP_DHCP */
  94. /** The IP header ID of the next outgoing IP packet */
  95. static u16_t ip_id;
  96. #if LWIP_MULTICAST_TX_OPTIONS
  97. /** The default netif used for multicast */
  98. static struct netif *ip4_default_multicast_netif;
  99. /**
  100. * @ingroup ip4
  101. * Set a default netif for IPv4 multicast. */
  102. void
  103. ip4_set_default_multicast_netif(struct netif *default_multicast_netif)
  104. {
  105. ip4_default_multicast_netif = default_multicast_netif;
  106. }
  107. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  108. #ifdef LWIP_HOOK_IP4_ROUTE_SRC
  109. /**
  110. * Source based IPv4 routing must be fully implemented in
  111. * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters.
  112. */
  113. struct netif *
  114. ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest)
  115. {
  116. if (src != NULL) {
  117. /* when src==NULL, the hook is called from ip4_route(dest) */
  118. struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(src, dest);
  119. if (netif != NULL) {
  120. return netif;
  121. }
  122. }
  123. return ip4_route(dest);
  124. }
  125. #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
  126. /**
  127. * Finds the appropriate network interface for a given IP address. It
  128. * searches the list of network interfaces linearly. A match is found
  129. * if the masked IP address of the network interface equals the masked
  130. * IP address given to the function.
  131. *
  132. * @param dest the destination IP address for which to find the route
  133. * @return the netif on which to send to reach dest
  134. */
  135. struct netif *
  136. ip4_route(const ip4_addr_t *dest)
  137. {
  138. #if !LWIP_SINGLE_NETIF
  139. struct netif *netif;
  140. LWIP_ASSERT_CORE_LOCKED();
  141. #if LWIP_MULTICAST_TX_OPTIONS
  142. /* Use administratively selected interface for multicast by default */
  143. if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
  144. return ip4_default_multicast_netif;
  145. }
  146. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  147. /* bug #54569: in case LWIP_SINGLE_NETIF=1 and LWIP_DEBUGF() disabled, the following loop is optimized away */
  148. LWIP_UNUSED_ARG(dest);
  149. /* iterate through netifs */
  150. NETIF_FOREACH(netif) {
  151. /* is the netif up, does it have a link and a valid address? */
  152. if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  153. /* network mask matches? */
  154. if (ip4_addr_net_eq(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {
  155. /* return netif on which to forward IP packet */
  156. return netif;
  157. }
  158. /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
  159. if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_eq(dest, netif_ip4_gw(netif))) {
  160. /* return netif on which to forward IP packet */
  161. return netif;
  162. }
  163. }
  164. }
  165. #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
  166. /* loopif is disabled, loopback traffic is passed through any netif */
  167. if (ip4_addr_isloopback(dest)) {
  168. /* don't check for link on loopback traffic */
  169. if ((netif_default != NULL) && netif_is_up(netif_default)) {
  170. return netif_default;
  171. }
  172. /* default netif is not up, just use any netif for loopback traffic */
  173. NETIF_FOREACH(netif) {
  174. if (netif_is_up(netif)) {
  175. return netif;
  176. }
  177. }
  178. return NULL;
  179. }
  180. #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
  181. #ifdef LWIP_HOOK_IP4_ROUTE_SRC
  182. netif = LWIP_HOOK_IP4_ROUTE_SRC(NULL, dest);
  183. if (netif != NULL) {
  184. return netif;
  185. }
  186. #elif defined(LWIP_HOOK_IP4_ROUTE)
  187. netif = LWIP_HOOK_IP4_ROUTE(dest);
  188. if (netif != NULL) {
  189. return netif;
  190. }
  191. #endif
  192. #endif /* !LWIP_SINGLE_NETIF */
  193. if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||
  194. ip4_addr_isany_val(*netif_ip4_addr(netif_default)) || ip4_addr_isloopback(dest)) {
  195. /* No matching netif found and default netif is not usable.
  196. If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */
  197. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  198. ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
  199. IP_STATS_INC(ip.rterr);
  200. MIB2_STATS_INC(mib2.ipoutnoroutes);
  201. return NULL;
  202. }
  203. return netif_default;
  204. }
  205. #if IP_FORWARD
  206. /**
  207. * Determine whether an IP address is in a reserved set of addresses
  208. * that may not be forwarded, or whether datagrams to that destination
  209. * may be forwarded.
  210. * @param p the packet to forward
  211. * @return 1: can forward 0: discard
  212. */
  213. static int
  214. ip4_canforward(struct pbuf *p)
  215. {
  216. u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));
  217. #ifdef LWIP_HOOK_IP4_CANFORWARD
  218. int ret = LWIP_HOOK_IP4_CANFORWARD(p, addr);
  219. if (ret >= 0) {
  220. return ret;
  221. }
  222. #endif /* LWIP_HOOK_IP4_CANFORWARD */
  223. if (p->flags & PBUF_FLAG_LLBCAST) {
  224. /* don't route link-layer broadcasts */
  225. return 0;
  226. }
  227. if ((p->flags & PBUF_FLAG_LLMCAST) || IP_MULTICAST(addr)) {
  228. /* don't route link-layer multicasts (use LWIP_HOOK_IP4_CANFORWARD instead) */
  229. return 0;
  230. }
  231. if (IP_EXPERIMENTAL(addr)) {
  232. return 0;
  233. }
  234. if (IP_CLASSA(addr)) {
  235. u32_t net = addr & IP_CLASSA_NET;
  236. if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
  237. /* don't route loopback packets */
  238. return 0;
  239. }
  240. }
  241. return 1;
  242. }
  243. /**
  244. * Forwards an IP packet. It finds an appropriate route for the
  245. * packet, decrements the TTL value of the packet, adjusts the
  246. * checksum and outputs the packet on the appropriate interface.
  247. *
  248. * @param p the packet to forward (p->payload points to IP header)
  249. * @param iphdr the IP header of the input packet
  250. * @param inp the netif on which this packet was received
  251. */
  252. static void
  253. ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
  254. {
  255. struct netif *netif;
  256. PERF_START;
  257. LWIP_UNUSED_ARG(inp);
  258. if (!ip4_canforward(p)) {
  259. goto return_noroute;
  260. }
  261. /* RFC3927 2.7: do not forward link-local addresses */
  262. if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
  263. LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  264. ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
  265. ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
  266. goto return_noroute;
  267. }
  268. /* Find network interface where to forward this IP packet to. */
  269. netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr());
  270. if (netif == NULL) {
  271. LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
  272. ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
  273. ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
  274. /* @todo: send ICMP_DUR_NET? */
  275. goto return_noroute;
  276. }
  277. #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
  278. /* Do not forward packets onto the same network interface on which
  279. * they arrived. */
  280. if (netif == inp) {
  281. LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n"));
  282. goto return_noroute;
  283. }
  284. #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
  285. /* decrement TTL */
  286. IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
  287. /* send ICMP if TTL == 0 */
  288. if (IPH_TTL(iphdr) == 0) {
  289. MIB2_STATS_INC(mib2.ipinhdrerrors);
  290. #if LWIP_ICMP
  291. /* Don't send ICMP messages in response to ICMP messages */
  292. if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
  293. icmp_time_exceeded(p, ICMP_TE_TTL);
  294. }
  295. #endif /* LWIP_ICMP */
  296. return;
  297. }
  298. /* Incrementally update the IP checksum. */
  299. if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
  300. IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1));
  301. } else {
  302. IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100)));
  303. }
  304. /* Take care of setting checksums to 0 for checksum offload netifs */
  305. if (CHECKSUM_GEN_IP || NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP)) {
  306. IPH_CHKSUM_SET(iphdr, 0);
  307. }
  308. switch (IPH_PROTO(iphdr)) {
  309. #if LWIP_UDP
  310. #if LWIP_UDPLITE
  311. case IP_PROTO_UDPLITE:
  312. #endif
  313. case IP_PROTO_UDP:
  314. if (CHECKSUM_GEN_UDP || NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_UDP)) {
  315. ((struct udp_hdr *)((u8_t *)iphdr + IPH_HL_BYTES(iphdr)))->chksum = 0;
  316. }
  317. break;
  318. #endif
  319. #if LWIP_TCP
  320. case IP_PROTO_TCP:
  321. if (CHECKSUM_GEN_TCP || NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_TCP)) {
  322. ((struct tcp_hdr *)((u8_t *)iphdr + IPH_HL_BYTES(iphdr)))->chksum = 0;
  323. }
  324. break;
  325. #endif
  326. #if LWIP_ICMP
  327. case IP_PROTO_ICMP:
  328. if (CHECKSUM_GEN_ICMP || NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP)) {
  329. ((struct icmp_hdr *)((u8_t *)iphdr + IPH_HL_BYTES(iphdr)))->chksum = 0;
  330. }
  331. break;
  332. #endif
  333. default:
  334. /* there's really nothing to do here other than satisfying 'switch-default' */
  335. break;
  336. }
  337. LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  338. ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
  339. ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
  340. IP_STATS_INC(ip.fw);
  341. MIB2_STATS_INC(mib2.ipforwdatagrams);
  342. IP_STATS_INC(ip.xmit);
  343. PERF_STOP("ip4_forward");
  344. /* don't fragment if interface has mtu set to 0 [loopif] */
  345. if (netif->mtu && (p->tot_len > netif->mtu)) {
  346. if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
  347. #if IP_FRAG
  348. ip4_frag(p, netif, ip4_current_dest_addr());
  349. #else /* IP_FRAG */
  350. /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
  351. #endif /* IP_FRAG */
  352. } else {
  353. #if LWIP_ICMP
  354. /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
  355. icmp_dest_unreach(p, ICMP_DUR_FRAG);
  356. #endif /* LWIP_ICMP */
  357. }
  358. return;
  359. }
  360. /* transmit pbuf on chosen interface */
  361. netif->output(netif, p, ip4_current_dest_addr());
  362. return;
  363. return_noroute:
  364. MIB2_STATS_INC(mib2.ipoutnoroutes);
  365. }
  366. #endif /* IP_FORWARD */
  367. /** Return true if the current input packet should be accepted on this netif */
  368. static int
  369. ip4_input_accept(struct netif *netif)
  370. {
  371. LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n",
  372. ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
  373. ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
  374. ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
  375. ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
  376. /* interface is up and configured? */
  377. if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
  378. /* unicast to this interface address? */
  379. if (ip4_addr_eq(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
  380. /* or broadcast on this interface network address? */
  381. ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
  382. #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
  383. || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
  384. #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
  385. ) {
  386. LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",
  387. netif->name[0], netif->name[1]));
  388. /* accept on this netif */
  389. return 1;
  390. }
  391. #if LWIP_AUTOIP
  392. /* connections to link-local addresses must persist after changing
  393. the netif's address (RFC3927 ch. 1.9) */
  394. if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
  395. LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",
  396. netif->name[0], netif->name[1]));
  397. /* accept on this netif */
  398. return 1;
  399. }
  400. #endif /* LWIP_AUTOIP */
  401. }
  402. return 0;
  403. }
  404. /**
  405. * This function is called by the network interface device driver when
  406. * an IP packet is received. The function does the basic checks of the
  407. * IP header such as packet size being at least larger than the header
  408. * size etc. If the packet was not destined for us, the packet is
  409. * forwarded (using ip_forward). The IP checksum is always checked.
  410. *
  411. * Finally, the packet is sent to the upper layer protocol input function.
  412. *
  413. * @param p the received IP packet (p->payload points to IP header)
  414. * @param inp the netif on which this packet was received
  415. * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
  416. * processed, but currently always returns ERR_OK)
  417. */
  418. err_t
  419. ip4_input(struct pbuf *p, struct netif *inp)
  420. {
  421. const struct ip_hdr *iphdr;
  422. struct netif *netif;
  423. u16_t iphdr_hlen;
  424. u16_t iphdr_len;
  425. #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
  426. int check_ip_src = 1;
  427. #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
  428. #if LWIP_RAW
  429. raw_input_state_t raw_status;
  430. #endif /* LWIP_RAW */
  431. LWIP_ASSERT_CORE_LOCKED();
  432. IP_STATS_INC(ip.recv);
  433. MIB2_STATS_INC(mib2.ipinreceives);
  434. /* identify the IP header */
  435. iphdr = (struct ip_hdr *)p->payload;
  436. if (IPH_V(iphdr) != 4) {
  437. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
  438. ip4_debug_print(p);
  439. pbuf_free(p);
  440. IP_STATS_INC(ip.err);
  441. IP_STATS_INC(ip.drop);
  442. MIB2_STATS_INC(mib2.ipinhdrerrors);
  443. return ERR_OK;
  444. }
  445. #ifdef LWIP_HOOK_IP4_INPUT
  446. if (LWIP_HOOK_IP4_INPUT(p, inp)) {
  447. /* the packet has been eaten */
  448. return ERR_OK;
  449. }
  450. #endif
  451. /* obtain IP header length in bytes */
  452. iphdr_hlen = IPH_HL_BYTES(iphdr);
  453. /* obtain ip length in bytes */
  454. iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
  455. /* Trim pbuf. This is especially required for packets < 60 bytes. */
  456. if (iphdr_len < p->tot_len) {
  457. pbuf_realloc(p, iphdr_len);
  458. }
  459. /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
  460. if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
  461. if (iphdr_hlen < IP_HLEN) {
  462. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  463. ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
  464. }
  465. if (iphdr_hlen > p->len) {
  466. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  467. ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
  468. iphdr_hlen, p->len));
  469. }
  470. if (iphdr_len > p->tot_len) {
  471. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  472. ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
  473. iphdr_len, p->tot_len));
  474. }
  475. /* free (drop) packet pbufs */
  476. pbuf_free(p);
  477. IP_STATS_INC(ip.lenerr);
  478. IP_STATS_INC(ip.drop);
  479. MIB2_STATS_INC(mib2.ipindiscards);
  480. return ERR_OK;
  481. }
  482. /* verify checksum */
  483. #if CHECKSUM_CHECK_IP
  484. IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
  485. if (inet_chksum(iphdr, iphdr_hlen) != 0) {
  486. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  487. ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
  488. ip4_debug_print(p);
  489. pbuf_free(p);
  490. IP_STATS_INC(ip.chkerr);
  491. IP_STATS_INC(ip.drop);
  492. MIB2_STATS_INC(mib2.ipinhdrerrors);
  493. return ERR_OK;
  494. }
  495. }
  496. #endif
  497. /* copy IP addresses to aligned ip_addr_t */
  498. ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
  499. ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
  500. /* match packet against an interface, i.e. is this packet for us? */
  501. if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
  502. #if LWIP_IGMP
  503. if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
  504. /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
  505. ip4_addr_t allsystems;
  506. IP4_ADDR(&allsystems, 224, 0, 0, 1);
  507. if (ip4_addr_eq(ip4_current_dest_addr(), &allsystems) &&
  508. ip4_addr_isany(ip4_current_src_addr())) {
  509. check_ip_src = 0;
  510. }
  511. netif = inp;
  512. } else {
  513. netif = NULL;
  514. }
  515. #else /* LWIP_IGMP */
  516. if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
  517. netif = inp;
  518. } else {
  519. netif = NULL;
  520. }
  521. #endif /* LWIP_IGMP */
  522. } else {
  523. /* start trying with inp. if that's not acceptable, start walking the
  524. list of configured netifs. */
  525. if (ip4_input_accept(inp)) {
  526. netif = inp;
  527. } else {
  528. netif = NULL;
  529. #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
  530. /* Packets sent to the loopback address must not be accepted on an
  531. * interface that does not have the loopback address assigned to it,
  532. * unless a non-loopback interface is used for loopback traffic. */
  533. if (!ip4_addr_isloopback(ip4_current_dest_addr()))
  534. #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
  535. {
  536. #if !LWIP_SINGLE_NETIF
  537. NETIF_FOREACH(netif) {
  538. if (netif == inp) {
  539. /* we checked that before already */
  540. continue;
  541. }
  542. if (ip4_input_accept(netif)) {
  543. break;
  544. }
  545. }
  546. #endif /* !LWIP_SINGLE_NETIF */
  547. }
  548. }
  549. }
  550. #if IP_ACCEPT_LINK_LAYER_ADDRESSING
  551. /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
  552. * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
  553. * According to RFC 1542 section 3.1.1, referred by RFC 2131).
  554. *
  555. * If you want to accept private broadcast communication while a netif is down,
  556. * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
  557. *
  558. * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
  559. */
  560. if (netif == NULL) {
  561. /* remote port is DHCP server? */
  562. if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
  563. const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);
  564. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
  565. lwip_ntohs(udphdr->dest)));
  566. if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
  567. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
  568. netif = inp;
  569. check_ip_src = 0;
  570. }
  571. }
  572. }
  573. #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
  574. /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
  575. #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
  576. if (check_ip_src
  577. #if IP_ACCEPT_LINK_LAYER_ADDRESSING
  578. /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
  579. && !ip4_addr_isany_val(*ip4_current_src_addr())
  580. #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
  581. )
  582. #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
  583. {
  584. if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
  585. (ip4_addr_ismulticast(ip4_current_src_addr()))) {
  586. /* packet source is not valid */
  587. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
  588. /* free (drop) packet pbufs */
  589. pbuf_free(p);
  590. IP_STATS_INC(ip.drop);
  591. MIB2_STATS_INC(mib2.ipinaddrerrors);
  592. MIB2_STATS_INC(mib2.ipindiscards);
  593. return ERR_OK;
  594. }
  595. }
  596. /* packet not for us? */
  597. if (netif == NULL) {
  598. /* packet not for us, route or discard */
  599. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
  600. #if IP_FORWARD
  601. /* non-broadcast packet? */
  602. if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
  603. /* try to forward IP packet on (other) interfaces */
  604. ip4_forward(p, (struct ip_hdr *)p->payload, inp);
  605. } else
  606. #endif /* IP_FORWARD */
  607. {
  608. IP_STATS_INC(ip.drop);
  609. MIB2_STATS_INC(mib2.ipinaddrerrors);
  610. MIB2_STATS_INC(mib2.ipindiscards);
  611. }
  612. pbuf_free(p);
  613. return ERR_OK;
  614. }
  615. /* packet consists of multiple fragments? */
  616. if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
  617. #if IP_REASSEMBLY /* packet fragment reassembly code present? */
  618. LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip4_reass()\n",
  619. lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8)));
  620. /* reassemble the packet*/
  621. p = ip4_reass(p);
  622. /* packet not fully reassembled yet? */
  623. if (p == NULL) {
  624. return ERR_OK;
  625. }
  626. iphdr = (const struct ip_hdr *)p->payload;
  627. #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
  628. pbuf_free(p);
  629. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
  630. lwip_ntohs(IPH_OFFSET(iphdr))));
  631. IP_STATS_INC(ip.opterr);
  632. IP_STATS_INC(ip.drop);
  633. /* unsupported protocol feature */
  634. MIB2_STATS_INC(mib2.ipinunknownprotos);
  635. return ERR_OK;
  636. #endif /* IP_REASSEMBLY */
  637. }
  638. #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
  639. #if LWIP_IGMP
  640. /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
  641. if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
  642. #else
  643. if (iphdr_hlen > IP_HLEN) {
  644. #endif /* LWIP_IGMP */
  645. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
  646. pbuf_free(p);
  647. IP_STATS_INC(ip.opterr);
  648. IP_STATS_INC(ip.drop);
  649. /* unsupported protocol feature */
  650. MIB2_STATS_INC(mib2.ipinunknownprotos);
  651. return ERR_OK;
  652. }
  653. #endif /* IP_OPTIONS_ALLOWED == 0 */
  654. /* send to upper layers */
  655. LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
  656. ip4_debug_print(p);
  657. LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
  658. ip_data.current_netif = netif;
  659. ip_data.current_input_netif = inp;
  660. ip_data.current_ip4_header = iphdr;
  661. ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);
  662. #if LWIP_RAW
  663. /* raw input did not eat the packet? */
  664. raw_status = raw_input(p, inp);
  665. if (raw_status != RAW_INPUT_EATEN)
  666. #endif /* LWIP_RAW */
  667. {
  668. pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */
  669. switch (IPH_PROTO(iphdr)) {
  670. #if LWIP_UDP
  671. case IP_PROTO_UDP:
  672. #if LWIP_UDPLITE
  673. case IP_PROTO_UDPLITE:
  674. #endif /* LWIP_UDPLITE */
  675. MIB2_STATS_INC(mib2.ipindelivers);
  676. udp_input(p, inp);
  677. break;
  678. #endif /* LWIP_UDP */
  679. #if LWIP_TCP
  680. case IP_PROTO_TCP:
  681. MIB2_STATS_INC(mib2.ipindelivers);
  682. tcp_input(p, inp);
  683. break;
  684. #endif /* LWIP_TCP */
  685. #if LWIP_ICMP
  686. case IP_PROTO_ICMP:
  687. MIB2_STATS_INC(mib2.ipindelivers);
  688. icmp_input(p, inp);
  689. break;
  690. #endif /* LWIP_ICMP */
  691. #if LWIP_IGMP
  692. case IP_PROTO_IGMP:
  693. igmp_input(p, inp, ip4_current_dest_addr());
  694. break;
  695. #endif /* LWIP_IGMP */
  696. default:
  697. #if LWIP_RAW
  698. if (raw_status == RAW_INPUT_DELIVERED) {
  699. MIB2_STATS_INC(mib2.ipindelivers);
  700. } else
  701. #endif /* LWIP_RAW */
  702. {
  703. #if LWIP_ICMP
  704. /* send ICMP destination protocol unreachable unless is was a broadcast */
  705. if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
  706. !ip4_addr_ismulticast(ip4_current_dest_addr())) {
  707. pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */
  708. icmp_dest_unreach(p, ICMP_DUR_PROTO);
  709. }
  710. #endif /* LWIP_ICMP */
  711. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
  712. IP_STATS_INC(ip.proterr);
  713. IP_STATS_INC(ip.drop);
  714. MIB2_STATS_INC(mib2.ipinunknownprotos);
  715. }
  716. pbuf_free(p);
  717. break;
  718. }
  719. }
  720. /* @todo: this is not really necessary... */
  721. ip_data.current_netif = NULL;
  722. ip_data.current_input_netif = NULL;
  723. ip_data.current_ip4_header = NULL;
  724. ip_data.current_ip_header_tot_len = 0;
  725. ip4_addr_set_any(ip4_current_src_addr());
  726. ip4_addr_set_any(ip4_current_dest_addr());
  727. return ERR_OK;
  728. }
  729. /**
  730. * Sends an IP packet on a network interface. This function constructs
  731. * the IP header and calculates the IP header checksum. If the source
  732. * IP address is NULL, the IP address of the outgoing network
  733. * interface is filled in as source address.
  734. * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
  735. * include an IP header and p->payload points to it instead of the data.
  736. *
  737. * @param p the packet to send (p->payload points to the data, e.g. next
  738. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  739. IP header and p->payload points to that IP header)
  740. * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
  741. * IP address of the netif used to send is used as source address)
  742. * @param dest the destination IP address to send the packet to
  743. * @param ttl the TTL value to be set in the IP header
  744. * @param tos the TOS value to be set in the IP header
  745. * @param proto the PROTOCOL to be set in the IP header
  746. * @param netif the netif on which to send this packet
  747. * @return ERR_OK if the packet was sent OK
  748. * ERR_BUF if p doesn't have enough space for IP/LINK headers
  749. * returns errors returned by netif->output
  750. *
  751. * @note ip_id: RFC791 "some host may be able to simply use
  752. * unique identifiers independent of destination"
  753. */
  754. err_t
  755. ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  756. u8_t ttl, u8_t tos,
  757. u8_t proto, struct netif *netif)
  758. {
  759. #if IP_OPTIONS_SEND
  760. return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
  761. }
  762. /**
  763. * Same as ip_output_if() but with the possibility to include IP options:
  764. *
  765. * @ param ip_options pointer to the IP options, copied into the IP header
  766. * @ param optlen length of ip_options
  767. */
  768. err_t
  769. ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  770. u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
  771. u16_t optlen)
  772. {
  773. #endif /* IP_OPTIONS_SEND */
  774. const ip4_addr_t *src_used = src;
  775. if (dest != LWIP_IP_HDRINCL) {
  776. if (ip4_addr_isany(src)) {
  777. src_used = netif_ip4_addr(netif);
  778. }
  779. }
  780. #if IP_OPTIONS_SEND
  781. return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
  782. ip_options, optlen);
  783. #else /* IP_OPTIONS_SEND */
  784. return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
  785. #endif /* IP_OPTIONS_SEND */
  786. }
  787. /**
  788. * Same as ip_output_if() but 'src' address is not replaced by netif address
  789. * when it is 'any'.
  790. */
  791. err_t
  792. ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  793. u8_t ttl, u8_t tos,
  794. u8_t proto, struct netif *netif)
  795. {
  796. #if IP_OPTIONS_SEND
  797. return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
  798. }
  799. /**
  800. * Same as ip_output_if_opt() but 'src' address is not replaced by netif address
  801. * when it is 'any'.
  802. */
  803. err_t
  804. ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  805. u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
  806. u16_t optlen)
  807. {
  808. #endif /* IP_OPTIONS_SEND */
  809. struct ip_hdr *iphdr;
  810. ip4_addr_t dest_addr;
  811. #if CHECKSUM_GEN_IP_INLINE
  812. u32_t chk_sum = 0;
  813. #endif /* CHECKSUM_GEN_IP_INLINE */
  814. LWIP_ASSERT_CORE_LOCKED();
  815. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  816. MIB2_STATS_INC(mib2.ipoutrequests);
  817. /* Should the IP header be generated or is it already included in p? */
  818. if (dest != LWIP_IP_HDRINCL) {
  819. u16_t ip_hlen = IP_HLEN;
  820. #if IP_OPTIONS_SEND
  821. u16_t optlen_aligned = 0;
  822. if (optlen != 0) {
  823. #if CHECKSUM_GEN_IP_INLINE
  824. int i;
  825. #endif /* CHECKSUM_GEN_IP_INLINE */
  826. if (optlen > (IP_HLEN_MAX - IP_HLEN)) {
  827. /* optlen too long */
  828. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));
  829. IP_STATS_INC(ip.err);
  830. MIB2_STATS_INC(mib2.ipoutdiscards);
  831. return ERR_VAL;
  832. }
  833. /* round up to a multiple of 4 */
  834. optlen_aligned = (u16_t)((optlen + 3) & ~3);
  835. ip_hlen = (u16_t)(ip_hlen + optlen_aligned);
  836. /* First write in the IP options */
  837. if (pbuf_add_header(p, optlen_aligned)) {
  838. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
  839. IP_STATS_INC(ip.err);
  840. MIB2_STATS_INC(mib2.ipoutdiscards);
  841. return ERR_BUF;
  842. }
  843. MEMCPY(p->payload, ip_options, optlen);
  844. if (optlen < optlen_aligned) {
  845. /* zero the remaining bytes */
  846. memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));
  847. }
  848. #if CHECKSUM_GEN_IP_INLINE
  849. for (i = 0; i < optlen_aligned / 2; i++) {
  850. chk_sum += ((u16_t *)p->payload)[i];
  851. }
  852. #endif /* CHECKSUM_GEN_IP_INLINE */
  853. }
  854. #endif /* IP_OPTIONS_SEND */
  855. /* generate IP header */
  856. if (pbuf_add_header(p, IP_HLEN)) {
  857. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
  858. IP_STATS_INC(ip.err);
  859. MIB2_STATS_INC(mib2.ipoutdiscards);
  860. return ERR_BUF;
  861. }
  862. iphdr = (struct ip_hdr *)p->payload;
  863. LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
  864. (p->len >= sizeof(struct ip_hdr)));
  865. IPH_TTL_SET(iphdr, ttl);
  866. IPH_PROTO_SET(iphdr, proto);
  867. #if CHECKSUM_GEN_IP_INLINE
  868. chk_sum += PP_NTOHS(proto | (ttl << 8));
  869. #endif /* CHECKSUM_GEN_IP_INLINE */
  870. /* dest cannot be NULL here */
  871. ip4_addr_copy(iphdr->dest, *dest);
  872. #if CHECKSUM_GEN_IP_INLINE
  873. chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
  874. chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
  875. #endif /* CHECKSUM_GEN_IP_INLINE */
  876. IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
  877. IPH_TOS_SET(iphdr, tos);
  878. #if CHECKSUM_GEN_IP_INLINE
  879. chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
  880. #endif /* CHECKSUM_GEN_IP_INLINE */
  881. IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
  882. #if CHECKSUM_GEN_IP_INLINE
  883. chk_sum += iphdr->_len;
  884. #endif /* CHECKSUM_GEN_IP_INLINE */
  885. IPH_OFFSET_SET(iphdr, 0);
  886. IPH_ID_SET(iphdr, lwip_htons(ip_id));
  887. #if CHECKSUM_GEN_IP_INLINE
  888. chk_sum += iphdr->_id;
  889. #endif /* CHECKSUM_GEN_IP_INLINE */
  890. ++ip_id;
  891. if (src == NULL) {
  892. ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
  893. } else {
  894. /* src cannot be NULL here */
  895. ip4_addr_copy(iphdr->src, *src);
  896. }
  897. #if CHECKSUM_GEN_IP_INLINE
  898. chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
  899. chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
  900. chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
  901. chk_sum = (chk_sum >> 16) + chk_sum;
  902. chk_sum = ~chk_sum;
  903. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
  904. iphdr->_chksum = (u16_t)chk_sum; /* network order */
  905. }
  906. #if LWIP_CHECKSUM_CTRL_PER_NETIF
  907. else {
  908. IPH_CHKSUM_SET(iphdr, 0);
  909. }
  910. #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
  911. #else /* CHECKSUM_GEN_IP_INLINE */
  912. IPH_CHKSUM_SET(iphdr, 0);
  913. #if CHECKSUM_GEN_IP
  914. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
  915. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
  916. }
  917. #endif /* CHECKSUM_GEN_IP */
  918. #endif /* CHECKSUM_GEN_IP_INLINE */
  919. } else {
  920. /* IP header already included in p */
  921. if (p->len < IP_HLEN) {
  922. LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));
  923. IP_STATS_INC(ip.err);
  924. MIB2_STATS_INC(mib2.ipoutdiscards);
  925. return ERR_BUF;
  926. }
  927. iphdr = (struct ip_hdr *)p->payload;
  928. ip4_addr_copy(dest_addr, iphdr->dest);
  929. dest = &dest_addr;
  930. }
  931. IP_STATS_INC(ip.xmit);
  932. LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
  933. ip4_debug_print(p);
  934. #if ENABLE_LOOPBACK
  935. if (ip4_addr_eq(dest, netif_ip4_addr(netif))
  936. #if !LWIP_HAVE_LOOPIF
  937. || ip4_addr_isloopback(dest)
  938. #endif /* !LWIP_HAVE_LOOPIF */
  939. ) {
  940. /* Packet to self, enqueue it for loopback */
  941. LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()\n"));
  942. return netif_loop_output(netif, p);
  943. }
  944. #if LWIP_MULTICAST_TX_OPTIONS
  945. if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
  946. netif_loop_output(netif, p);
  947. }
  948. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  949. #endif /* ENABLE_LOOPBACK */
  950. #if IP_FRAG
  951. /* don't fragment if interface has mtu set to 0 [loopif] */
  952. if (netif->mtu && (p->tot_len > netif->mtu)) {
  953. return ip4_frag(p, netif, dest);
  954. }
  955. #endif /* IP_FRAG */
  956. LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
  957. return netif->output(netif, p, dest);
  958. }
  959. /**
  960. * Simple interface to ip_output_if. It finds the outgoing network
  961. * interface and calls upon ip_output_if to do the actual work.
  962. *
  963. * @param p the packet to send (p->payload points to the data, e.g. next
  964. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  965. IP header and p->payload points to that IP header)
  966. * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
  967. * IP address of the netif used to send is used as source address)
  968. * @param dest the destination IP address to send the packet to
  969. * @param ttl the TTL value to be set in the IP header
  970. * @param tos the TOS value to be set in the IP header
  971. * @param proto the PROTOCOL to be set in the IP header
  972. *
  973. * @return ERR_RTE if no route is found
  974. * see ip_output_if() for more return values
  975. */
  976. err_t
  977. ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  978. u8_t ttl, u8_t tos, u8_t proto)
  979. {
  980. struct netif *netif;
  981. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  982. if ((netif = ip4_route_src(src, dest)) == NULL) {
  983. LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  984. ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
  985. IP_STATS_INC(ip.rterr);
  986. return ERR_RTE;
  987. }
  988. return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
  989. }
  990. #if LWIP_NETIF_USE_HINTS
  991. /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
  992. * before calling ip_output_if.
  993. *
  994. * @param p the packet to send (p->payload points to the data, e.g. next
  995. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  996. IP header and p->payload points to that IP header)
  997. * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
  998. * IP address of the netif used to send is used as source address)
  999. * @param dest the destination IP address to send the packet to
  1000. * @param ttl the TTL value to be set in the IP header
  1001. * @param tos the TOS value to be set in the IP header
  1002. * @param proto the PROTOCOL to be set in the IP header
  1003. * @param netif_hint netif output hint pointer set to netif->hint before
  1004. * calling ip_output_if()
  1005. *
  1006. * @return ERR_RTE if no route is found
  1007. * see ip_output_if() for more return values
  1008. */
  1009. err_t
  1010. ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
  1011. u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
  1012. {
  1013. struct netif *netif;
  1014. err_t err;
  1015. LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
  1016. if ((netif = ip4_route_src(src, dest)) == NULL) {
  1017. LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  1018. ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
  1019. IP_STATS_INC(ip.rterr);
  1020. return ERR_RTE;
  1021. }
  1022. NETIF_SET_HINTS(netif, netif_hint);
  1023. err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
  1024. NETIF_RESET_HINTS(netif);
  1025. return err;
  1026. }
  1027. #endif /* LWIP_NETIF_USE_HINTS*/
  1028. #if IP_DEBUG
  1029. /* Print an IP header by using LWIP_DEBUGF
  1030. * @param p an IP packet, p->payload pointing to the IP header
  1031. */
  1032. void
  1033. ip4_debug_print(struct pbuf *p)
  1034. {
  1035. struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
  1036. LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
  1037. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1038. LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
  1039. (u16_t)IPH_V(iphdr),
  1040. (u16_t)IPH_HL(iphdr),
  1041. (u16_t)IPH_TOS(iphdr),
  1042. lwip_ntohs(IPH_LEN(iphdr))));
  1043. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1044. LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
  1045. lwip_ntohs(IPH_ID(iphdr)),
  1046. (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
  1047. (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
  1048. (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
  1049. (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
  1050. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1051. LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
  1052. (u16_t)IPH_TTL(iphdr),
  1053. (u16_t)IPH_PROTO(iphdr),
  1054. lwip_ntohs(IPH_CHKSUM(iphdr))));
  1055. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1056. LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
  1057. ip4_addr1_16_val(iphdr->src),
  1058. ip4_addr2_16_val(iphdr->src),
  1059. ip4_addr3_16_val(iphdr->src),
  1060. ip4_addr4_16_val(iphdr->src)));
  1061. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1062. LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
  1063. ip4_addr1_16_val(iphdr->dest),
  1064. ip4_addr2_16_val(iphdr->dest),
  1065. ip4_addr3_16_val(iphdr->dest),
  1066. ip4_addr4_16_val(iphdr->dest)));
  1067. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  1068. }
  1069. #endif /* IP_DEBUG */
  1070. #endif /* LWIP_IPV4 */