3 * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
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.
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
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet_chksum.h"
47 #include "lwip/netif.h"
48 #include "lwip/icmp.h"
49 #include "lwip/igmp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/snmp.h"
54 #include "lwip/dhcp.h"
55 #include "lwip/autoip.h"
56 #include "lwip/stats.h"
57 #include "arch/perf.h"
61 /** Set this to 0 in the rare case of wanting to call an extra function to
62 * generate the IP checksum (in contrast to calculating it on-the-fly). */
63 #ifndef LWIP_INLINE_IP_CHKSUM
64 #define LWIP_INLINE_IP_CHKSUM 1
66 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
67 #define CHECKSUM_GEN_IP_INLINE 1
69 #define CHECKSUM_GEN_IP_INLINE 0
72 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
73 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
75 /** Some defines for DHCP to let link-layer-addressed packets through while the
77 * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT
78 * to return 1 if the port is accepted and 0 if the port is not accepted.
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(DHCP_CLIENT_PORT)) \
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(dst_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(DHCP_CLIENT_PORT))
90 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
93 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
94 #endif /* LWIP_DHCP */
96 /** Global data for both IPv4 and IPv6 */
97 struct ip_globals ip_data;
99 /** The IP header ID of the next outgoing IP packet */
103 * Finds the appropriate network interface for a given IP address. It
104 * searches the list of network interfaces linearly. A match is found
105 * if the masked IP address of the network interface equals the masked
106 * IP address given to the function.
108 * @param dest the destination IP address for which to find the route
109 * @return the netif on which to send to reach dest
112 ip_route(ip_addr_t *dest)
116 #ifdef LWIP_HOOK_IP4_ROUTE
117 netif = LWIP_HOOK_IP4_ROUTE(dest);
123 /* iterate through netifs */
124 for (netif = netif_list; netif != NULL; netif = netif->next) {
125 /* network mask matches? */
126 if (netif_is_up(netif)) {
127 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
128 /* return netif on which to forward IP packet */
133 if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
134 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
135 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
136 IP_STATS_INC(ip.rterr);
137 snmp_inc_ipoutnoroutes();
140 /* no matching netif found, use default netif */
141 return netif_default;
146 * Determine whether an IP address is in a reserved set of addresses
147 * that may not be forwarded, or whether datagrams to that destination
149 * @param p the packet to forward
150 * @param dest the destination IP address
151 * @return 1: can forward 0: discard
154 ip_canforward(struct pbuf *p)
156 u32_t addr = ip4_addr_get_u32(ip_current_dest_addr());
158 if (p->flags & PBUF_FLAG_LLBCAST) {
159 /* don't route link-layer broadcasts */
162 if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) {
163 /* don't route link-layer multicasts unless the destination address is an IP
167 if (IP_EXPERIMENTAL(addr)) {
170 if (IP_CLASSA(addr)) {
171 u32_t net = addr & IP_CLASSA_NET;
172 if ((net == 0) || (net == (IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
173 /* don't route loopback packets */
181 * Forwards an IP packet. It finds an appropriate route for the
182 * packet, decrements the TTL value of the packet, adjusts the
183 * checksum and outputs the packet on the appropriate interface.
185 * @param p the packet to forward (p->payload points to IP header)
186 * @param iphdr the IP header of the input packet
187 * @param inp the netif on which this packet was received
190 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
196 if (!ip_canforward(p)) {
200 /* RFC3927 2.7: do not forward link-local addresses */
201 if (ip_addr_islinklocal(ip_current_dest_addr())) {
202 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
203 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
204 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
208 /* Find network interface where to forward this IP packet to. */
209 netif = ip_route(ip_current_dest_addr());
211 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
212 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
213 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
216 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
217 /* Do not forward packets onto the same network interface on which
220 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
223 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
226 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
227 /* send ICMP if TTL == 0 */
228 if (IPH_TTL(iphdr) == 0) {
229 snmp_inc_ipinhdrerrors();
231 /* Don't send ICMP messages in response to ICMP messages */
232 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
233 icmp_time_exceeded(p, ICMP_TE_TTL);
235 #endif /* LWIP_ICMP */
239 /* Incrementally update the IP checksum. */
240 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
241 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
243 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
246 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
247 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
248 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
251 IP_STATS_INC(ip.xmit);
252 snmp_inc_ipforwdatagrams();
254 PERF_STOP("ip_forward");
255 /* transmit pbuf on chosen interface */
256 netif->output(netif, p, ip_current_dest_addr());
259 snmp_inc_ipoutnoroutes();
261 #endif /* IP_FORWARD */
264 * This function is called by the network interface device driver when
265 * an IP packet is received. The function does the basic checks of the
266 * IP header such as packet size being at least larger than the header
267 * size etc. If the packet was not destined for us, the packet is
268 * forwarded (using ip_forward). The IP checksum is always checked.
270 * Finally, the packet is sent to the upper layer protocol input function.
272 * @param p the received IP packet (p->payload points to IP header)
273 * @param inp the netif on which this packet was received
274 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
275 * processed, but currently always returns ERR_OK)
278 ip_input(struct pbuf *p, struct netif *inp)
280 struct ip_hdr *iphdr;
284 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
286 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
288 IP_STATS_INC(ip.recv);
289 snmp_inc_ipinreceives();
291 /* identify the IP header */
292 iphdr = (struct ip_hdr *)p->payload;
293 if (IPH_V(iphdr) != 4) {
294 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
297 IP_STATS_INC(ip.err);
298 IP_STATS_INC(ip.drop);
299 snmp_inc_ipinhdrerrors();
303 #ifdef LWIP_HOOK_IP4_INPUT
304 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
305 /* the packet has been eaten */
310 /* obtain IP header length in number of 32-bit words */
311 iphdr_hlen = IPH_HL(iphdr);
312 /* calculate IP header length in bytes */
314 /* obtain ip length in bytes */
315 iphdr_len = ntohs(IPH_LEN(iphdr));
317 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
318 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
319 if (iphdr_hlen > p->len) {
320 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
321 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
322 iphdr_hlen, p->len));
324 if (iphdr_len > p->tot_len) {
325 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
326 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
327 iphdr_len, p->tot_len));
329 /* free (drop) packet pbufs */
331 IP_STATS_INC(ip.lenerr);
332 IP_STATS_INC(ip.drop);
333 snmp_inc_ipindiscards();
337 /* verify checksum */
338 #if CHECKSUM_CHECK_IP
339 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
341 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
342 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
345 IP_STATS_INC(ip.chkerr);
346 IP_STATS_INC(ip.drop);
347 snmp_inc_ipinhdrerrors();
352 /* Trim pbuf. This should have been done at the netif layer,
353 * but we'll do it anyway just to be sure that its done. */
354 pbuf_realloc(p, iphdr_len);
356 /* copy IP addresses to aligned ip_addr_t */
357 ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_dest), iphdr->dest);
358 ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_src), iphdr->src);
360 /* match packet against an interface, i.e. is this packet for us? */
362 if (ip_addr_ismulticast(ip_current_dest_addr())) {
363 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip_current_dest_addr()))) {
369 #endif /* LWIP_IGMP */
371 /* start trying with inp. if that's not acceptable, start walking the
372 list of configured netifs.
373 'first' is used as a boolean to mark whether we started walking the list */
377 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",
378 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
379 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
380 ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
381 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
383 /* interface is up and configured? */
384 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
385 /* unicast to this interface address? */
386 if (ip_addr_cmp(ip_current_dest_addr(), &(netif->ip_addr)) ||
387 /* or broadcast on this interface network address? */
388 ip_addr_isbroadcast(ip_current_dest_addr(), netif)) {
389 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
390 netif->name[0], netif->name[1]));
391 /* break out of for loop */
395 /* connections to link-local addresses must persist after changing
396 the netif's address (RFC3927 ch. 1.9) */
397 if ((netif->autoip != NULL) &&
398 ip_addr_cmp(ip_current_dest_addr(), &(netif->autoip->llipaddr))) {
399 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
400 netif->name[0], netif->name[1]));
401 /* break out of for loop */
404 #endif /* LWIP_AUTOIP */
415 } while(netif != NULL);
418 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
419 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
420 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
421 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
423 * If you want to accept private broadcast communication while a netif is down,
424 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
426 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
429 /* remote port is DHCP server? */
430 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
431 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
432 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
433 ntohs(udphdr->dest)));
434 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
435 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
441 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
443 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
444 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
445 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
446 if (check_ip_src && !ip_addr_isany(ip_current_src_addr()))
447 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
448 { if ((ip_addr_isbroadcast(ip_current_src_addr(), inp)) ||
449 (ip_addr_ismulticast(ip_current_src_addr()))) {
450 /* packet source is not valid */
451 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
452 /* free (drop) packet pbufs */
454 IP_STATS_INC(ip.drop);
455 snmp_inc_ipinaddrerrors();
456 snmp_inc_ipindiscards();
461 /* packet not for us? */
463 /* packet not for us, route or discard */
464 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
466 /* non-broadcast packet? */
467 if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp)) {
468 /* try to forward IP packet on (other) interfaces */
469 ip_forward(p, iphdr, inp);
471 #endif /* IP_FORWARD */
473 snmp_inc_ipinaddrerrors();
474 snmp_inc_ipindiscards();
479 /* packet consists of multiple fragments? */
480 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
481 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
482 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 ip_reass()\n",
483 ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
484 /* reassemble the packet*/
486 /* packet not fully reassembled yet? */
490 iphdr = (struct ip_hdr *)p->payload;
491 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
493 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
494 ntohs(IPH_OFFSET(iphdr))));
495 IP_STATS_INC(ip.opterr);
496 IP_STATS_INC(ip.drop);
497 /* unsupported protocol feature */
498 snmp_inc_ipinunknownprotos();
500 #endif /* IP_REASSEMBLY */
503 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
506 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
507 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
509 if (iphdr_hlen > IP_HLEN) {
510 #endif /* LWIP_IGMP */
511 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
513 IP_STATS_INC(ip.opterr);
514 IP_STATS_INC(ip.drop);
515 /* unsupported protocol feature */
516 snmp_inc_ipinunknownprotos();
519 #endif /* IP_OPTIONS_ALLOWED == 0 */
521 /* send to upper layers */
522 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
524 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
526 ip_data.current_netif = inp;
527 ip_data.current_ip4_header = iphdr;
528 ip_data.current_ip_header_tot_len = IPH_HL(iphdr) * 4;
531 /* raw input did not eat the packet? */
532 if (raw_input(p, inp) == 0)
533 #endif /* LWIP_RAW */
535 pbuf_header(p, -iphdr_hlen); /* Move to payload, no check necessary. */
537 switch (IPH_PROTO(iphdr)) {
541 case IP_PROTO_UDPLITE:
542 #endif /* LWIP_UDPLITE */
543 snmp_inc_ipindelivers();
546 #endif /* LWIP_UDP */
549 snmp_inc_ipindelivers();
552 #endif /* LWIP_TCP */
555 snmp_inc_ipindelivers();
558 #endif /* LWIP_ICMP */
561 igmp_input(p, inp, ip_current_dest_addr());
563 #endif /* LWIP_IGMP */
566 /* send ICMP destination protocol unreachable unless is was a broadcast */
567 if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp) &&
568 !ip_addr_ismulticast(ip_current_dest_addr())) {
569 pbuf_header(p, iphdr_hlen); /* Move to ip header, no check necessary. */
571 icmp_dest_unreach(p, ICMP_DUR_PROTO);
573 #endif /* LWIP_ICMP */
576 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
578 IP_STATS_INC(ip.proterr);
579 IP_STATS_INC(ip.drop);
580 snmp_inc_ipinunknownprotos();
584 /* @todo: this is not really necessary... */
585 ip_data.current_netif = NULL;
586 ip_data.current_ip4_header = NULL;
587 ip_data.current_ip_header_tot_len = 0;
588 ip_addr_set_any(ip_current_src_addr());
589 ip_addr_set_any(ip_current_dest_addr());
595 * Sends an IP packet on a network interface. This function constructs
596 * the IP header and calculates the IP header checksum. If the source
597 * IP address is NULL, the IP address of the outgoing network
598 * interface is filled in as source address.
599 * If the destination IP address is IP_HDRINCL, p is assumed to already
600 * include an IP header and p->payload points to it instead of the data.
602 * @param p the packet to send (p->payload points to the data, e.g. next
603 protocol header; if dest == IP_HDRINCL, p already includes an IP
604 header and p->payload points to that IP header)
605 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
606 * IP address of the netif used to send is used as source address)
607 * @param dest the destination IP address to send the packet to
608 * @param ttl the TTL value to be set in the IP header
609 * @param tos the TOS value to be set in the IP header
610 * @param proto the PROTOCOL to be set in the IP header
611 * @param netif the netif on which to send this packet
612 * @return ERR_OK if the packet was sent OK
613 * ERR_BUF if p doesn't have enough space for IP/LINK headers
614 * returns errors returned by netif->output
616 * @note ip_id: RFC791 "some host may be able to simply use
617 * unique identifiers independent of destination"
620 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
622 u8_t proto, struct netif *netif)
625 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
629 * Same as ip_output_if() but with the possibility to include IP options:
631 * @ param ip_options pointer to the IP options, copied into the IP header
632 * @ param optlen length of ip_options
634 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
635 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
638 #endif /* IP_OPTIONS_SEND */
639 struct ip_hdr *iphdr;
641 #if CHECKSUM_GEN_IP_INLINE
643 #endif /* CHECKSUM_GEN_IP_INLINE */
645 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
646 gets altered as the packet is passed down the stack */
647 LWIP_ASSERT("p->ref == 1", p->ref == 1);
649 snmp_inc_ipoutrequests();
651 /* Should the IP header be generated or is it already included in p? */
652 if (dest != IP_HDRINCL) {
653 u16_t ip_hlen = IP_HLEN;
655 u16_t optlen_aligned = 0;
657 #if CHECKSUM_GEN_IP_INLINE
659 #endif /* CHECKSUM_GEN_IP_INLINE */
660 /* round up to a multiple of 4 */
661 optlen_aligned = ((optlen + 3) & ~3);
662 ip_hlen += optlen_aligned;
663 /* First write in the IP options */
664 if (pbuf_header(p, optlen_aligned)) {
665 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
666 IP_STATS_INC(ip.err);
667 snmp_inc_ipoutdiscards();
670 MEMCPY(p->payload, ip_options, optlen);
671 if (optlen < optlen_aligned) {
672 /* zero the remaining bytes */
673 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
675 #if CHECKSUM_GEN_IP_INLINE
676 for (i = 0; i < optlen_aligned/2; i++) {
677 chk_sum += ((u16_t*)p->payload)[i];
679 #endif /* CHECKSUM_GEN_IP_INLINE */
681 #endif /* IP_OPTIONS_SEND */
682 /* generate IP header */
683 if (pbuf_header(p, IP_HLEN)) {
684 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
686 IP_STATS_INC(ip.err);
687 snmp_inc_ipoutdiscards();
691 iphdr = (struct ip_hdr *)p->payload;
692 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
693 (p->len >= sizeof(struct ip_hdr)));
695 IPH_TTL_SET(iphdr, ttl);
696 IPH_PROTO_SET(iphdr, proto);
697 #if CHECKSUM_GEN_IP_INLINE
698 chk_sum += LWIP_MAKE_U16(proto, ttl);
699 #endif /* CHECKSUM_GEN_IP_INLINE */
701 /* dest cannot be NULL here */
702 ip_addr_copy(iphdr->dest, *dest);
703 #if CHECKSUM_GEN_IP_INLINE
704 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
705 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
706 #endif /* CHECKSUM_GEN_IP_INLINE */
708 IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
709 IPH_TOS_SET(iphdr, tos);
710 #if CHECKSUM_GEN_IP_INLINE
711 chk_sum += LWIP_MAKE_U16(tos, iphdr->_v_hl);
712 #endif /* CHECKSUM_GEN_IP_INLINE */
713 IPH_LEN_SET(iphdr, htons(p->tot_len));
714 #if CHECKSUM_GEN_IP_INLINE
715 chk_sum += iphdr->_len;
716 #endif /* CHECKSUM_GEN_IP_INLINE */
717 IPH_OFFSET_SET(iphdr, 0);
718 IPH_ID_SET(iphdr, htons(ip_id));
719 #if CHECKSUM_GEN_IP_INLINE
720 chk_sum += iphdr->_id;
721 #endif /* CHECKSUM_GEN_IP_INLINE */
724 if (ip_addr_isany(src)) {
725 ip_addr_copy(iphdr->src, netif->ip_addr);
727 /* src cannot be NULL here */
728 ip_addr_copy(iphdr->src, *src);
731 #if CHECKSUM_GEN_IP_INLINE
732 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
733 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
734 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
735 chk_sum = (chk_sum >> 16) + chk_sum;
737 iphdr->_chksum = chk_sum; /* network order */
738 #else /* CHECKSUM_GEN_IP_INLINE */
739 IPH_CHKSUM_SET(iphdr, 0);
741 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
743 #endif /* CHECKSUM_GEN_IP_INLINE */
745 /* IP header already included in p */
746 iphdr = (struct ip_hdr *)p->payload;
747 ip_addr_copy(dest_addr, iphdr->dest);
751 IP_STATS_INC(ip.xmit);
753 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
757 if (ip_addr_cmp(dest, &netif->ip_addr)) {
758 /* Packet to self, enqueue it for loopback */
759 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
760 return netif_loop_output(netif, p, dest);
763 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
764 netif_loop_output(netif, p, dest);
766 #endif /* LWIP_IGMP */
767 #endif /* ENABLE_LOOPBACK */
769 /* don't fragment if interface has mtu set to 0 [loopif] */
770 if (netif->mtu && (p->tot_len > netif->mtu)) {
771 return ip_frag(p, netif, dest);
775 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
776 return netif->output(netif, p, dest);
780 * Simple interface to ip_output_if. It finds the outgoing network
781 * interface and calls upon ip_output_if to do the actual work.
783 * @param p the packet to send (p->payload points to the data, e.g. next
784 protocol header; if dest == IP_HDRINCL, p already includes an IP
785 header and p->payload points to that IP header)
786 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
787 * IP address of the netif used to send is used as source address)
788 * @param dest the destination IP address to send the packet to
789 * @param ttl the TTL value to be set in the IP header
790 * @param tos the TOS value to be set in the IP header
791 * @param proto the PROTOCOL to be set in the IP header
793 * @return ERR_RTE if no route is found
794 * see ip_output_if() for more return values
797 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
798 u8_t ttl, u8_t tos, u8_t proto)
802 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
803 gets altered as the packet is passed down the stack */
804 LWIP_ASSERT("p->ref == 1", p->ref == 1);
806 if ((netif = ip_route(dest)) == NULL) {
807 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
808 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
809 IP_STATS_INC(ip.rterr);
813 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
816 #if LWIP_NETIF_HWADDRHINT
817 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
818 * before calling ip_output_if.
820 * @param p the packet to send (p->payload points to the data, e.g. next
821 protocol header; if dest == IP_HDRINCL, p already includes an IP
822 header and p->payload points to that IP header)
823 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
824 * IP address of the netif used to send is used as source address)
825 * @param dest the destination IP address to send the packet to
826 * @param ttl the TTL value to be set in the IP header
827 * @param tos the TOS value to be set in the IP header
828 * @param proto the PROTOCOL to be set in the IP header
829 * @param addr_hint address hint pointer set to netif->addr_hint before
830 * calling ip_output_if()
832 * @return ERR_RTE if no route is found
833 * see ip_output_if() for more return values
836 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
837 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
842 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
843 gets altered as the packet is passed down the stack */
844 LWIP_ASSERT("p->ref == 1", p->ref == 1);
846 if ((netif = ip_route(dest)) == NULL) {
847 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
848 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
849 IP_STATS_INC(ip.rterr);
853 NETIF_SET_HWADDRHINT(netif, addr_hint);
854 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
855 NETIF_SET_HWADDRHINT(netif, NULL);
859 #endif /* LWIP_NETIF_HWADDRHINT*/
862 /* Print an IP header by using LWIP_DEBUGF
863 * @param p an IP packet, p->payload pointing to the IP header
866 ip_debug_print(struct pbuf *p)
868 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
871 payload = (u8_t *)iphdr + IP_HLEN;
873 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
874 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
875 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
879 ntohs(IPH_LEN(iphdr))));
880 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
881 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
882 ntohs(IPH_ID(iphdr)),
883 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
884 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
885 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
886 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
887 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
888 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
891 ntohs(IPH_CHKSUM(iphdr))));
892 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
893 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
894 ip4_addr1_16(&iphdr->src),
895 ip4_addr2_16(&iphdr->src),
896 ip4_addr3_16(&iphdr->src),
897 ip4_addr4_16(&iphdr->src)));
898 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
899 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
900 ip4_addr1_16(&iphdr->dest),
901 ip4_addr2_16(&iphdr->dest),
902 ip4_addr3_16(&iphdr->dest),
903 ip4_addr4_16(&iphdr->dest)));
904 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
906 #endif /* IP_DEBUG */