source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/lwip-2.1.2/src/core/ipv4/ip4.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 40.4 KB
Line 
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 */
107static u16_t ip_id;
108
109#if LWIP_MULTICAST_TX_OPTIONS
110/** The default netif used for multicast */
111static struct netif *ip4_default_multicast_netif;
112
113/**
114 * @ingroup ip4
115 * Set a default netif for IPv4 multicast. */
116void
117ip4_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 */
128struct netif *
129ip4_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 */
151struct netif *
152ip4_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_netcmp(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_cmp(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, looopback 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 */
238static int
239ip4_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 */
280static void
281ip4_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 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
340 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
341 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
342
343 IP_STATS_INC(ip.fw);
344 MIB2_STATS_INC(mib2.ipforwdatagrams);
345 IP_STATS_INC(ip.xmit);
346
347 PERF_STOP("ip4_forward");
348 /* don't fragment if interface has mtu set to 0 [loopif] */
349 if (netif->mtu && (p->tot_len > netif->mtu)) {
350 if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
351#if IP_FRAG
352 ip4_frag(p, netif, ip4_current_dest_addr());
353#else /* IP_FRAG */
354 /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
355#endif /* IP_FRAG */
356 } else {
357#if LWIP_ICMP
358 /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
359 icmp_dest_unreach(p, ICMP_DUR_FRAG);
360#endif /* LWIP_ICMP */
361 }
362 return;
363 }
364 /* transmit pbuf on chosen interface */
365 netif->output(netif, p, ip4_current_dest_addr());
366 return;
367return_noroute:
368 MIB2_STATS_INC(mib2.ipoutnoroutes);
369}
370#endif /* IP_FORWARD */
371
372/** Return true if the current input packet should be accepted on this netif */
373static int
374ip4_input_accept(struct netif *netif)
375{
376 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",
377 ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
378 ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
379 ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
380 ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
381
382 /* interface is up and configured? */
383 if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
384 /* unicast to this interface address? */
385 if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
386 /* or broadcast on this interface network address? */
387 ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
388#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
389 || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
390#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
391 ) {
392 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",
393 netif->name[0], netif->name[1]));
394 /* accept on this netif */
395 return 1;
396 }
397#if LWIP_AUTOIP
398 /* connections to link-local addresses must persist after changing
399 the netif's address (RFC3927 ch. 1.9) */
400 if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
401 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",
402 netif->name[0], netif->name[1]));
403 /* accept on this netif */
404 return 1;
405 }
406#endif /* LWIP_AUTOIP */
407 }
408 return 0;
409}
410
411/**
412 * This function is called by the network interface device driver when
413 * an IP packet is received. The function does the basic checks of the
414 * IP header such as packet size being at least larger than the header
415 * size etc. If the packet was not destined for us, the packet is
416 * forwarded (using ip_forward). The IP checksum is always checked.
417 *
418 * Finally, the packet is sent to the upper layer protocol input function.
419 *
420 * @param p the received IP packet (p->payload points to IP header)
421 * @param inp the netif on which this packet was received
422 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
423 * processed, but currently always returns ERR_OK)
424 */
425err_t
426ip4_input(struct pbuf *p, struct netif *inp)
427{
428 const struct ip_hdr *iphdr;
429 struct netif *netif;
430 u16_t iphdr_hlen;
431 u16_t iphdr_len;
432#if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
433 int check_ip_src = 1;
434#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
435#if LWIP_RAW
436 raw_input_state_t raw_status;
437#endif /* LWIP_RAW */
438#if ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_DHCP
439 struct etharp_hdr *hdr = (struct etharp_hdr *)(p->payload - SIZEOF_ETH_HDR);
440#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
441
442 LWIP_ASSERT_CORE_LOCKED();
443
444 IP_STATS_INC(ip.recv);
445 MIB2_STATS_INC(mib2.ipinreceives);
446
447 /* identify the IP header */
448 iphdr = (struct ip_hdr *)p->payload;
449 if (IPH_V(iphdr) != 4) {
450 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
451 ip4_debug_print(p);
452 pbuf_free(p);
453 IP_STATS_INC(ip.err);
454 IP_STATS_INC(ip.drop);
455 MIB2_STATS_INC(mib2.ipinhdrerrors);
456 return ERR_OK;
457 }
458
459#ifdef LWIP_HOOK_IP4_INPUT
460 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
461 /* the packet has been eaten */
462 return ERR_OK;
463 }
464#endif
465
466 /* obtain IP header length in bytes */
467 iphdr_hlen = IPH_HL_BYTES(iphdr);
468 /* obtain ip length in bytes */
469 iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
470
471 /* Trim pbuf. This is especially required for packets < 60 bytes. */
472 if (iphdr_len < p->tot_len) {
473 pbuf_realloc(p, iphdr_len);
474 }
475
476 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
477 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
478 if (iphdr_hlen < IP_HLEN) {
479 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
480 ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
481 }
482 if (iphdr_hlen > p->len) {
483 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
484 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
485 iphdr_hlen, p->len));
486 }
487 if (iphdr_len > p->tot_len) {
488 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
489 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
490 iphdr_len, p->tot_len));
491 }
492 /* free (drop) packet pbufs */
493 pbuf_free(p);
494 IP_STATS_INC(ip.lenerr);
495 IP_STATS_INC(ip.drop);
496 MIB2_STATS_INC(mib2.ipindiscards);
497 return ERR_OK;
498 }
499
500 /* verify checksum */
501#if CHECKSUM_CHECK_IP
502 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
503 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
504
505 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
506 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
507 ip4_debug_print(p);
508 pbuf_free(p);
509 IP_STATS_INC(ip.chkerr);
510 IP_STATS_INC(ip.drop);
511 MIB2_STATS_INC(mib2.ipinhdrerrors);
512 return ERR_OK;
513 }
514 }
515#endif
516
517 /* copy IP addresses to aligned ip_addr_t */
518 ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
519 ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
520
521 /* match packet against an interface, i.e. is this packet for us? */
522 if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
523#if LWIP_IGMP
524 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
525 /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
526 ip4_addr_t allsystems;
527 IP4_ADDR(&allsystems, 224, 0, 0, 1);
528 if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
529 ip4_addr_isany(ip4_current_src_addr())) {
530 check_ip_src = 0;
531 }
532 netif = inp;
533 } else {
534 netif = NULL;
535 }
536#else /* LWIP_IGMP */
537 if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
538 netif = inp;
539 } else {
540 netif = NULL;
541 }
542#endif /* LWIP_IGMP */
543 } else {
544 /* start trying with inp. if that's not acceptable, start walking the
545 list of configured netifs. */
546 if (ip4_input_accept(inp)) {
547 netif = inp;
548 } else {
549 netif = NULL;
550#if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
551 /* Packets sent to the loopback address must not be accepted on an
552 * interface that does not have the loopback address assigned to it,
553 * unless a non-loopback interface is used for loopback traffic. */
554 if (!ip4_addr_isloopback(ip4_current_dest_addr()))
555#endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
556 {
557#if !LWIP_SINGLE_NETIF
558 NETIF_FOREACH(netif) {
559 if (netif == inp) {
560 /* we checked that before already */
561 continue;
562 }
563 if (ip4_input_accept(netif)) {
564 break;
565 }
566 }
567#endif /* !LWIP_SINGLE_NETIF */
568 }
569 }
570 }
571
572#if IP_ACCEPT_LINK_LAYER_ADDRESSING
573 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
574 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
575 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
576 *
577 * If you want to accept private broadcast communication while a netif is down,
578 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
579 *
580 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
581 */
582 if (netif == NULL) {
583 /* remote port is DHCP server? */
584 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
585 const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen);
586 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
587 lwip_ntohs(udphdr->dest)));
588 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
589 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
590 netif = inp;
591 check_ip_src = 0;
592 }
593 }
594 }
595#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
596
597 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
598#if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
599 if (check_ip_src
600#if IP_ACCEPT_LINK_LAYER_ADDRESSING
601 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
602 && !ip4_addr_isany_val(*ip4_current_src_addr())
603#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
604 )
605#endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
606 {
607 if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
608 (ip4_addr_ismulticast(ip4_current_src_addr()))) {
609 /* packet source is not valid */
610 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
611 /* free (drop) packet pbufs */
612 pbuf_free(p);
613 IP_STATS_INC(ip.drop);
614 MIB2_STATS_INC(mib2.ipinaddrerrors);
615 MIB2_STATS_INC(mib2.ipindiscards);
616 return ERR_OK;
617 }
618 }
619
620 /* packet not for us? */
621 if (netif == NULL) {
622 /* packet not for us, route or discard */
623 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
624#if IP_FORWARD
625 /* non-broadcast packet? */
626 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
627 /* try to forward IP packet on (other) interfaces */
628 ip4_forward(p, (struct ip_hdr *)p->payload, inp);
629 } else
630#endif /* IP_FORWARD */
631 {
632 IP_STATS_INC(ip.drop);
633 MIB2_STATS_INC(mib2.ipinaddrerrors);
634 MIB2_STATS_INC(mib2.ipindiscards);
635 }
636 pbuf_free(p);
637 return ERR_OK;
638 }
639 /* packet consists of multiple fragments? */
640 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
641#if IP_REASSEMBLY /* packet fragment reassembly code present? */
642 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",
643 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)));
644 /* reassemble the packet*/
645 p = ip4_reass(p);
646 /* packet not fully reassembled yet? */
647 if (p == NULL) {
648 return ERR_OK;
649 }
650 iphdr = (const struct ip_hdr *)p->payload;
651#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
652 pbuf_free(p);
653 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
654 lwip_ntohs(IPH_OFFSET(iphdr))));
655 IP_STATS_INC(ip.opterr);
656 IP_STATS_INC(ip.drop);
657 /* unsupported protocol feature */
658 MIB2_STATS_INC(mib2.ipinunknownprotos);
659 return ERR_OK;
660#endif /* IP_REASSEMBLY */
661 }
662
663#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
664
665#if LWIP_IGMP
666 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
667 if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
668#else
669 if (iphdr_hlen > IP_HLEN) {
670#endif /* LWIP_IGMP */
671 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
672 pbuf_free(p);
673 IP_STATS_INC(ip.opterr);
674 IP_STATS_INC(ip.drop);
675 /* unsupported protocol feature */
676 MIB2_STATS_INC(mib2.ipinunknownprotos);
677 return ERR_OK;
678 }
679#endif /* IP_OPTIONS_ALLOWED == 0 */
680
681 /* send to upper layers */
682 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
683 ip4_debug_print(p);
684 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
685
686 ip_data.current_netif = netif;
687 ip_data.current_input_netif = inp;
688 ip_data.current_ip4_header = iphdr;
689 ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr);
690
691#if LWIP_RAW
692 /* raw input did not eat the packet? */
693 raw_status = raw_input(p, inp);
694 if (raw_status != RAW_INPUT_EATEN)
695#endif /* LWIP_RAW */
696 {
697 pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */
698
699 switch (IPH_PROTO(iphdr)) {
700#if LWIP_UDP
701 case IP_PROTO_UDP:
702#if LWIP_UDPLITE
703 case IP_PROTO_UDPLITE:
704#endif /* LWIP_UDPLITE */
705 MIB2_STATS_INC(mib2.ipindelivers);
706 udp_input(p, inp);
707 break;
708#endif /* LWIP_UDP */
709#if LWIP_TCP
710 case IP_PROTO_TCP:
711 MIB2_STATS_INC(mib2.ipindelivers);
712 tcp_input(p, inp);
713 break;
714#endif /* LWIP_TCP */
715#if LWIP_ICMP
716 case IP_PROTO_ICMP:
717 MIB2_STATS_INC(mib2.ipindelivers);
718#if ETHARP_SUPPORT_STATIC_ENTRIES && LWIP_DHCP
719 /* FOR PING ERROR WHEN NO ARP PAKCET */
720 etharp_add_static_entry(ip4_current_src_addr(), &(hdr->shwaddr));
721#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
722 icmp_input(p, inp);
723 break;
724#endif /* LWIP_ICMP */
725#if LWIP_IGMP
726 case IP_PROTO_IGMP:
727 igmp_input(p, inp, ip4_current_dest_addr());
728 break;
729#endif /* LWIP_IGMP */
730 default:
731#if LWIP_RAW
732 if (raw_status == RAW_INPUT_DELIVERED) {
733 MIB2_STATS_INC(mib2.ipindelivers);
734 } else
735#endif /* LWIP_RAW */
736 {
737#if LWIP_ICMP
738 /* send ICMP destination protocol unreachable unless is was a broadcast */
739 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
740 !ip4_addr_ismulticast(ip4_current_dest_addr())) {
741 pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */
742 icmp_dest_unreach(p, ICMP_DUR_PROTO);
743 }
744#endif /* LWIP_ICMP */
745
746 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
747
748 IP_STATS_INC(ip.proterr);
749 IP_STATS_INC(ip.drop);
750 MIB2_STATS_INC(mib2.ipinunknownprotos);
751 }
752 pbuf_free(p);
753 break;
754 }
755 }
756
757 /* @todo: this is not really necessary... */
758 ip_data.current_netif = NULL;
759 ip_data.current_input_netif = NULL;
760 ip_data.current_ip4_header = NULL;
761 ip_data.current_ip_header_tot_len = 0;
762 ip4_addr_set_any(ip4_current_src_addr());
763 ip4_addr_set_any(ip4_current_dest_addr());
764
765 return ERR_OK;
766}
767
768/**
769 * Sends an IP packet on a network interface. This function constructs
770 * the IP header and calculates the IP header checksum. If the source
771 * IP address is NULL, the IP address of the outgoing network
772 * interface is filled in as source address.
773 * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
774 * include an IP header and p->payload points to it instead of the data.
775 *
776 * @param p the packet to send (p->payload points to the data, e.g. next
777 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
778 IP header and p->payload points to that IP header)
779 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
780 * IP address of the netif used to send is used as source address)
781 * @param dest the destination IP address to send the packet to
782 * @param ttl the TTL value to be set in the IP header
783 * @param tos the TOS value to be set in the IP header
784 * @param proto the PROTOCOL to be set in the IP header
785 * @param netif the netif on which to send this packet
786 * @return ERR_OK if the packet was sent OK
787 * ERR_BUF if p doesn't have enough space for IP/LINK headers
788 * returns errors returned by netif->output
789 *
790 * @note ip_id: RFC791 "some host may be able to simply use
791 * unique identifiers independent of destination"
792 */
793err_t
794ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
795 u8_t ttl, u8_t tos,
796 u8_t proto, struct netif *netif)
797{
798#if IP_OPTIONS_SEND
799 return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
800}
801
802/**
803 * Same as ip_output_if() but with the possibility to include IP options:
804 *
805 * @ param ip_options pointer to the IP options, copied into the IP header
806 * @ param optlen length of ip_options
807 */
808err_t
809ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
810 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
811 u16_t optlen)
812{
813#endif /* IP_OPTIONS_SEND */
814 const ip4_addr_t *src_used = src;
815 if (dest != LWIP_IP_HDRINCL) {
816 if (ip4_addr_isany(src)) {
817 src_used = netif_ip4_addr(netif);
818 }
819 }
820
821#if IP_OPTIONS_SEND
822 return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
823 ip_options, optlen);
824#else /* IP_OPTIONS_SEND */
825 return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
826#endif /* IP_OPTIONS_SEND */
827}
828
829/**
830 * Same as ip_output_if() but 'src' address is not replaced by netif address
831 * when it is 'any'.
832 */
833err_t
834ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
835 u8_t ttl, u8_t tos,
836 u8_t proto, struct netif *netif)
837{
838#if IP_OPTIONS_SEND
839 return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
840}
841
842/**
843 * Same as ip_output_if_opt() but 'src' address is not replaced by netif address
844 * when it is 'any'.
845 */
846err_t
847ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
848 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
849 u16_t optlen)
850{
851#endif /* IP_OPTIONS_SEND */
852 struct ip_hdr *iphdr;
853 ip4_addr_t dest_addr;
854#if CHECKSUM_GEN_IP_INLINE
855 u32_t chk_sum = 0;
856#endif /* CHECKSUM_GEN_IP_INLINE */
857
858 LWIP_ASSERT_CORE_LOCKED();
859 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
860
861 MIB2_STATS_INC(mib2.ipoutrequests);
862
863 /* Should the IP header be generated or is it already included in p? */
864 if (dest != LWIP_IP_HDRINCL) {
865 u16_t ip_hlen = IP_HLEN;
866#if IP_OPTIONS_SEND
867 u16_t optlen_aligned = 0;
868 if (optlen != 0) {
869#if CHECKSUM_GEN_IP_INLINE
870 int i;
871#endif /* CHECKSUM_GEN_IP_INLINE */
872 if (optlen > (IP_HLEN_MAX - IP_HLEN)) {
873 /* optlen too long */
874 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n"));
875 IP_STATS_INC(ip.err);
876 MIB2_STATS_INC(mib2.ipoutdiscards);
877 return ERR_VAL;
878 }
879 /* round up to a multiple of 4 */
880 optlen_aligned = (u16_t)((optlen + 3) & ~3);
881 ip_hlen = (u16_t)(ip_hlen + optlen_aligned);
882 /* First write in the IP options */
883 if (pbuf_add_header(p, optlen_aligned)) {
884 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
885 IP_STATS_INC(ip.err);
886 MIB2_STATS_INC(mib2.ipoutdiscards);
887 return ERR_BUF;
888 }
889 MEMCPY(p->payload, ip_options, optlen);
890 if (optlen < optlen_aligned) {
891 /* zero the remaining bytes */
892 memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen));
893 }
894#if CHECKSUM_GEN_IP_INLINE
895 for (i = 0; i < optlen_aligned / 2; i++) {
896 chk_sum += ((u16_t *)p->payload)[i];
897 }
898#endif /* CHECKSUM_GEN_IP_INLINE */
899 }
900#endif /* IP_OPTIONS_SEND */
901 /* generate IP header */
902 if (pbuf_add_header(p, IP_HLEN)) {
903 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
904
905 IP_STATS_INC(ip.err);
906 MIB2_STATS_INC(mib2.ipoutdiscards);
907 return ERR_BUF;
908 }
909
910 iphdr = (struct ip_hdr *)p->payload;
911 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
912 (p->len >= sizeof(struct ip_hdr)));
913
914 IPH_TTL_SET(iphdr, ttl);
915 IPH_PROTO_SET(iphdr, proto);
916#if CHECKSUM_GEN_IP_INLINE
917 chk_sum += PP_NTOHS(proto | (ttl << 8));
918#endif /* CHECKSUM_GEN_IP_INLINE */
919
920 /* dest cannot be NULL here */
921 ip4_addr_copy(iphdr->dest, *dest);
922#if CHECKSUM_GEN_IP_INLINE
923 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
924 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
925#endif /* CHECKSUM_GEN_IP_INLINE */
926
927 IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
928 IPH_TOS_SET(iphdr, tos);
929#if CHECKSUM_GEN_IP_INLINE
930 chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
931#endif /* CHECKSUM_GEN_IP_INLINE */
932 IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
933#if CHECKSUM_GEN_IP_INLINE
934 chk_sum += iphdr->_len;
935#endif /* CHECKSUM_GEN_IP_INLINE */
936 IPH_OFFSET_SET(iphdr, 0);
937 IPH_ID_SET(iphdr, lwip_htons(ip_id));
938#if CHECKSUM_GEN_IP_INLINE
939 chk_sum += iphdr->_id;
940#endif /* CHECKSUM_GEN_IP_INLINE */
941 ++ip_id;
942
943 if (src == NULL) {
944 ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
945 } else {
946 /* src cannot be NULL here */
947 ip4_addr_copy(iphdr->src, *src);
948 }
949
950#if CHECKSUM_GEN_IP_INLINE
951 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
952 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
953 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
954 chk_sum = (chk_sum >> 16) + chk_sum;
955 chk_sum = ~chk_sum;
956 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
957 iphdr->_chksum = (u16_t)chk_sum; /* network order */
958 }
959#if LWIP_CHECKSUM_CTRL_PER_NETIF
960 else {
961 IPH_CHKSUM_SET(iphdr, 0);
962 }
963#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
964#else /* CHECKSUM_GEN_IP_INLINE */
965 IPH_CHKSUM_SET(iphdr, 0);
966#if CHECKSUM_GEN_IP
967 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
968 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
969 }
970#endif /* CHECKSUM_GEN_IP */
971#endif /* CHECKSUM_GEN_IP_INLINE */
972 } else {
973 /* IP header already included in p */
974 if (p->len < IP_HLEN) {
975 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n"));
976 IP_STATS_INC(ip.err);
977 MIB2_STATS_INC(mib2.ipoutdiscards);
978 return ERR_BUF;
979 }
980 iphdr = (struct ip_hdr *)p->payload;
981 ip4_addr_copy(dest_addr, iphdr->dest);
982 dest = &dest_addr;
983 }
984
985 IP_STATS_INC(ip.xmit);
986
987 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
988 ip4_debug_print(p);
989
990#if ENABLE_LOOPBACK
991 if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
992#if !LWIP_HAVE_LOOPIF
993 || ip4_addr_isloopback(dest)
994#endif /* !LWIP_HAVE_LOOPIF */
995 ) {
996 /* Packet to self, enqueue it for loopback */
997 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
998 return netif_loop_output(netif, p);
999 }
1000#if LWIP_MULTICAST_TX_OPTIONS
1001 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
1002 netif_loop_output(netif, p);
1003 }
1004#endif /* LWIP_MULTICAST_TX_OPTIONS */
1005#endif /* ENABLE_LOOPBACK */
1006#if IP_FRAG
1007 /* don't fragment if interface has mtu set to 0 [loopif] */
1008 if (netif->mtu && (p->tot_len > netif->mtu)) {
1009 return ip4_frag(p, netif, dest);
1010 }
1011#endif /* IP_FRAG */
1012
1013 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
1014 return netif->output(netif, p, dest);
1015}
1016
1017/**
1018 * Simple interface to ip_output_if. It finds the outgoing network
1019 * interface and calls upon ip_output_if to do the actual work.
1020 *
1021 * @param p the packet to send (p->payload points to the data, e.g. next
1022 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1023 IP header and p->payload points to that IP header)
1024 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1025 * IP address of the netif used to send is used as source address)
1026 * @param dest the destination IP address to send the packet to
1027 * @param ttl the TTL value to be set in the IP header
1028 * @param tos the TOS value to be set in the IP header
1029 * @param proto the PROTOCOL to be set in the IP header
1030 *
1031 * @return ERR_RTE if no route is found
1032 * see ip_output_if() for more return values
1033 */
1034err_t
1035ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1036 u8_t ttl, u8_t tos, u8_t proto)
1037{
1038 struct netif *netif;
1039
1040 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1041
1042 if ((netif = ip4_route_src(src, dest)) == NULL) {
1043 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1044 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1045 IP_STATS_INC(ip.rterr);
1046 return ERR_RTE;
1047 }
1048
1049 return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1050}
1051
1052#if LWIP_NETIF_USE_HINTS
1053/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
1054 * before calling ip_output_if.
1055 *
1056 * @param p the packet to send (p->payload points to the data, e.g. next
1057 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
1058 IP header and p->payload points to that IP header)
1059 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
1060 * IP address of the netif used to send is used as source address)
1061 * @param dest the destination IP address to send the packet to
1062 * @param ttl the TTL value to be set in the IP header
1063 * @param tos the TOS value to be set in the IP header
1064 * @param proto the PROTOCOL to be set in the IP header
1065 * @param netif_hint netif output hint pointer set to netif->hint before
1066 * calling ip_output_if()
1067 *
1068 * @return ERR_RTE if no route is found
1069 * see ip_output_if() for more return values
1070 */
1071err_t
1072ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
1073 u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
1074{
1075 struct netif *netif;
1076 err_t err;
1077
1078 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
1079
1080 if ((netif = ip4_route_src(src, dest)) == NULL) {
1081 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1082 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1083 IP_STATS_INC(ip.rterr);
1084 return ERR_RTE;
1085 }
1086
1087 NETIF_SET_HINTS(netif, netif_hint);
1088 err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1089 NETIF_RESET_HINTS(netif);
1090
1091 return err;
1092}
1093#endif /* LWIP_NETIF_USE_HINTS*/
1094
1095#if IP_DEBUG
1096/* Print an IP header by using LWIP_DEBUGF
1097 * @param p an IP packet, p->payload pointing to the IP header
1098 */
1099void
1100ip4_debug_print(struct pbuf *p)
1101{
1102 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
1103
1104 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
1105 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1106 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
1107 (u16_t)IPH_V(iphdr),
1108 (u16_t)IPH_HL(iphdr),
1109 (u16_t)IPH_TOS(iphdr),
1110 lwip_ntohs(IPH_LEN(iphdr))));
1111 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1112 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
1113 lwip_ntohs(IPH_ID(iphdr)),
1114 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
1115 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
1116 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
1117 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
1118 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1119 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
1120 (u16_t)IPH_TTL(iphdr),
1121 (u16_t)IPH_PROTO(iphdr),
1122 lwip_ntohs(IPH_CHKSUM(iphdr))));
1123 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1124 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
1125 ip4_addr1_16_val(iphdr->src),
1126 ip4_addr2_16_val(iphdr->src),
1127 ip4_addr3_16_val(iphdr->src),
1128 ip4_addr4_16_val(iphdr->src)));
1129 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1130 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
1131 ip4_addr1_16_val(iphdr->dest),
1132 ip4_addr2_16_val(iphdr->dest),
1133 ip4_addr3_16_val(iphdr->dest),
1134 ip4_addr4_16_val(iphdr->dest)));
1135 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
1136}
1137#endif /* IP_DEBUG */
1138
1139#endif /* LWIP_IPV4 */
Note: See TracBrowser for help on using the repository browser.