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