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 /* iterate through netifs */
117 for(netif = netif_list; netif != NULL; netif = netif->next) {
118 /* network mask matches? */
119 if (netif_is_up(netif)) {
120 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
121 /* return netif on which to forward IP packet */
126 if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
127 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
128 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
129 IP_STATS_INC(ip.rterr);
130 snmp_inc_ipoutnoroutes();
133 /* no matching netif found, use default netif */
134 return netif_default;
139 * Forwards an IP packet. It finds an appropriate route for the
140 * packet, decrements the TTL value of the packet, adjusts the
141 * checksum and outputs the packet on the appropriate interface.
143 * @param p the packet to forward (p->payload points to IP header)
144 * @param iphdr the IP header of the input packet
145 * @param inp the netif on which this packet was received
148 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
154 /* RFC3927 2.7: do not forward link-local addresses */
155 if (ip_addr_islinklocal(ip_current_dest_addr())) {
156 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
157 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
158 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
162 /* Find network interface where to forward this IP packet to. */
163 netif = ip_route(ip_current_dest_addr());
165 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
166 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
167 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
170 /* Do not forward packets onto the same network interface on which
173 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
178 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
179 /* send ICMP if TTL == 0 */
180 if (IPH_TTL(iphdr) == 0) {
181 snmp_inc_ipinhdrerrors();
183 /* Don't send ICMP messages in response to ICMP messages */
184 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
185 icmp_time_exceeded(p, ICMP_TE_TTL);
187 #endif /* LWIP_ICMP */
191 /* Incrementally update the IP checksum. */
192 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
193 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
195 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
198 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
199 ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()),
200 ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr())));
203 IP_STATS_INC(ip.xmit);
204 snmp_inc_ipforwdatagrams();
206 PERF_STOP("ip_forward");
207 /* transmit pbuf on chosen interface */
208 netif->output(netif, p, ip_current_dest_addr());
211 snmp_inc_ipoutnoroutes();
213 #endif /* IP_FORWARD */
216 * This function is called by the network interface device driver when
217 * an IP packet is received. The function does the basic checks of the
218 * IP header such as packet size being at least larger than the header
219 * size etc. If the packet was not destined for us, the packet is
220 * forwarded (using ip_forward). The IP checksum is always checked.
222 * Finally, the packet is sent to the upper layer protocol input function.
224 * @param p the received IP packet (p->payload points to IP header)
225 * @param inp the netif on which this packet was received
226 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
227 * processed, but currently always returns ERR_OK)
230 ip_input(struct pbuf *p, struct netif *inp)
232 struct ip_hdr *iphdr;
236 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
238 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
240 IP_STATS_INC(ip.recv);
241 snmp_inc_ipinreceives();
243 /* identify the IP header */
244 iphdr = (struct ip_hdr *)p->payload;
245 if (IPH_V(iphdr) != 4) {
246 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
249 IP_STATS_INC(ip.err);
250 IP_STATS_INC(ip.drop);
251 snmp_inc_ipinhdrerrors();
255 /* obtain IP header length in number of 32-bit words */
256 iphdr_hlen = IPH_HL(iphdr);
257 /* calculate IP header length in bytes */
259 /* obtain ip length in bytes */
260 iphdr_len = ntohs(IPH_LEN(iphdr));
262 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
263 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
264 if (iphdr_hlen > p->len) {
265 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
266 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
267 iphdr_hlen, p->len));
269 if (iphdr_len > p->tot_len) {
270 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
271 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
272 iphdr_len, p->tot_len));
274 /* free (drop) packet pbufs */
276 IP_STATS_INC(ip.lenerr);
277 IP_STATS_INC(ip.drop);
278 snmp_inc_ipindiscards();
282 /* verify checksum */
283 #if CHECKSUM_CHECK_IP
284 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
286 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
287 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
290 IP_STATS_INC(ip.chkerr);
291 IP_STATS_INC(ip.drop);
292 snmp_inc_ipinhdrerrors();
297 /* Trim pbuf. This should have been done at the netif layer,
298 * but we'll do it anyway just to be sure that its done. */
299 pbuf_realloc(p, iphdr_len);
301 /* copy IP addresses to aligned ip_addr_t */
302 ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_dest), iphdr->dest);
303 ip_addr_copy(*ipX_2_ip(&ip_data.current_iphdr_src), iphdr->src);
305 /* match packet against an interface, i.e. is this packet for us? */
307 if (ip_addr_ismulticast(ip_current_dest_addr())) {
308 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip_current_dest_addr()))) {
314 #endif /* LWIP_IGMP */
316 /* start trying with inp. if that's not acceptable, start walking the
317 list of configured netifs.
318 'first' is used as a boolean to mark whether we started walking the list */
322 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",
323 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
324 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
325 ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
326 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
328 /* interface is up and configured? */
329 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
330 /* unicast to this interface address? */
331 if (ip_addr_cmp(ip_current_dest_addr(), &(netif->ip_addr)) ||
332 /* or broadcast on this interface network address? */
333 ip_addr_isbroadcast(ip_current_dest_addr(), netif)) {
334 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
335 netif->name[0], netif->name[1]));
336 /* break out of for loop */
340 /* connections to link-local addresses must persist after changing
341 the netif's address (RFC3927 ch. 1.9) */
342 if ((netif->autoip != NULL) &&
343 ip_addr_cmp(ip_current_dest_addr(), &(netif->autoip->llipaddr))) {
344 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
345 netif->name[0], netif->name[1]));
346 /* break out of for loop */
349 #endif /* LWIP_AUTOIP */
360 } while(netif != NULL);
363 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
364 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
365 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
366 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
368 * If you want to accept private broadcast communication while a netif is down,
369 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
371 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
374 /* remote port is DHCP server? */
375 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
376 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
377 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
378 ntohs(udphdr->dest)));
379 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
380 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
386 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
388 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
389 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
390 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
391 if (check_ip_src && !ip_addr_isany(ip_current_src_addr()))
392 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
393 { if ((ip_addr_isbroadcast(ip_current_src_addr(), inp)) ||
394 (ip_addr_ismulticast(ip_current_src_addr()))) {
395 /* packet source is not valid */
396 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
397 /* free (drop) packet pbufs */
399 IP_STATS_INC(ip.drop);
400 snmp_inc_ipinaddrerrors();
401 snmp_inc_ipindiscards();
406 /* packet not for us? */
408 /* packet not for us, route or discard */
409 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
411 /* non-broadcast packet? */
412 if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp)) {
413 /* try to forward IP packet on (other) interfaces */
414 ip_forward(p, iphdr, inp);
416 #endif /* IP_FORWARD */
418 snmp_inc_ipinaddrerrors();
419 snmp_inc_ipindiscards();
424 /* packet consists of multiple fragments? */
425 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
426 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
427 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",
428 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));
429 /* reassemble the packet*/
431 /* packet not fully reassembled yet? */
435 iphdr = (struct ip_hdr *)p->payload;
436 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
438 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
439 ntohs(IPH_OFFSET(iphdr))));
440 IP_STATS_INC(ip.opterr);
441 IP_STATS_INC(ip.drop);
442 /* unsupported protocol feature */
443 snmp_inc_ipinunknownprotos();
445 #endif /* IP_REASSEMBLY */
448 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
451 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
452 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
454 if (iphdr_hlen > IP_HLEN) {
455 #endif /* LWIP_IGMP */
456 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
458 IP_STATS_INC(ip.opterr);
459 IP_STATS_INC(ip.drop);
460 /* unsupported protocol feature */
461 snmp_inc_ipinunknownprotos();
464 #endif /* IP_OPTIONS_ALLOWED == 0 */
466 /* send to upper layers */
467 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
469 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
471 ip_data.current_netif = inp;
472 ip_data.current_ip4_header = iphdr;
473 ip_data.current_ip_header_tot_len = IPH_HL(iphdr) * 4;
476 /* raw input did not eat the packet? */
477 if (raw_input(p, inp) == 0)
478 #endif /* LWIP_RAW */
480 pbuf_header(p, -iphdr_hlen); /* Move to payload, no check necessary. */
482 switch (IPH_PROTO(iphdr)) {
486 case IP_PROTO_UDPLITE:
487 #endif /* LWIP_UDPLITE */
488 snmp_inc_ipindelivers();
491 #endif /* LWIP_UDP */
494 snmp_inc_ipindelivers();
497 #endif /* LWIP_TCP */
500 snmp_inc_ipindelivers();
503 #endif /* LWIP_ICMP */
506 igmp_input(p, inp, ip_current_dest_addr());
508 #endif /* LWIP_IGMP */
511 /* send ICMP destination protocol unreachable unless is was a broadcast */
512 if (!ip_addr_isbroadcast(ip_current_dest_addr(), inp) &&
513 !ip_addr_ismulticast(ip_current_dest_addr())) {
514 pbuf_header(p, iphdr_hlen); /* Move to ip header, no check necessary. */
516 icmp_dest_unreach(p, ICMP_DUR_PROTO);
518 #endif /* LWIP_ICMP */
521 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
523 IP_STATS_INC(ip.proterr);
524 IP_STATS_INC(ip.drop);
525 snmp_inc_ipinunknownprotos();
529 /* @todo: this is not really necessary... */
530 ip_data.current_netif = NULL;
531 ip_data.current_ip4_header = NULL;
532 ip_data.current_ip_header_tot_len = 0;
533 ip_addr_set_any(ip_current_src_addr());
534 ip_addr_set_any(ip_current_dest_addr());
540 * Sends an IP packet on a network interface. This function constructs
541 * the IP header and calculates the IP header checksum. If the source
542 * IP address is NULL, the IP address of the outgoing network
543 * interface is filled in as source address.
544 * If the destination IP address is IP_HDRINCL, p is assumed to already
545 * include an IP header and p->payload points to it instead of the data.
547 * @param p the packet to send (p->payload points to the data, e.g. next
548 protocol header; if dest == IP_HDRINCL, p already includes an IP
549 header and p->payload points to that IP header)
550 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
551 * IP address of the netif used to send is used as source address)
552 * @param dest the destination IP address to send the packet to
553 * @param ttl the TTL value to be set in the IP header
554 * @param tos the TOS value to be set in the IP header
555 * @param proto the PROTOCOL to be set in the IP header
556 * @param netif the netif on which to send this packet
557 * @return ERR_OK if the packet was sent OK
558 * ERR_BUF if p doesn't have enough space for IP/LINK headers
559 * returns errors returned by netif->output
561 * @note ip_id: RFC791 "some host may be able to simply use
562 * unique identifiers independent of destination"
565 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
567 u8_t proto, struct netif *netif)
570 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
574 * Same as ip_output_if() but with the possibility to include IP options:
576 * @ param ip_options pointer to the IP options, copied into the IP header
577 * @ param optlen length of ip_options
579 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
580 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
583 #endif /* IP_OPTIONS_SEND */
584 struct ip_hdr *iphdr;
586 #if CHECKSUM_GEN_IP_INLINE
588 #endif /* CHECKSUM_GEN_IP_INLINE */
590 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
591 gets altered as the packet is passed down the stack */
592 LWIP_ASSERT("p->ref == 1", p->ref == 1);
594 snmp_inc_ipoutrequests();
596 /* Should the IP header be generated or is it already included in p? */
597 if (dest != IP_HDRINCL) {
598 u16_t ip_hlen = IP_HLEN;
600 u16_t optlen_aligned = 0;
602 #if CHECKSUM_GEN_IP_INLINE
604 #endif /* CHECKSUM_GEN_IP_INLINE */
605 /* round up to a multiple of 4 */
606 optlen_aligned = ((optlen + 3) & ~3);
607 ip_hlen += optlen_aligned;
608 /* First write in the IP options */
609 if (pbuf_header(p, optlen_aligned)) {
610 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
611 IP_STATS_INC(ip.err);
612 snmp_inc_ipoutdiscards();
615 MEMCPY(p->payload, ip_options, optlen);
616 if (optlen < optlen_aligned) {
617 /* zero the remaining bytes */
618 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
620 #if CHECKSUM_GEN_IP_INLINE
621 for (i = 0; i < optlen_aligned/2; i++) {
622 chk_sum += ((u16_t*)p->payload)[i];
624 #endif /* CHECKSUM_GEN_IP_INLINE */
626 #endif /* IP_OPTIONS_SEND */
627 /* generate IP header */
628 if (pbuf_header(p, IP_HLEN)) {
629 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
631 IP_STATS_INC(ip.err);
632 snmp_inc_ipoutdiscards();
636 iphdr = (struct ip_hdr *)p->payload;
637 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
638 (p->len >= sizeof(struct ip_hdr)));
640 IPH_TTL_SET(iphdr, ttl);
641 IPH_PROTO_SET(iphdr, proto);
642 #if CHECKSUM_GEN_IP_INLINE
643 chk_sum += LWIP_MAKE_U16(proto, ttl);
644 #endif /* CHECKSUM_GEN_IP_INLINE */
646 /* dest cannot be NULL here */
647 ip_addr_copy(iphdr->dest, *dest);
648 #if CHECKSUM_GEN_IP_INLINE
649 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
650 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
651 #endif /* CHECKSUM_GEN_IP_INLINE */
653 IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
654 #if CHECKSUM_GEN_IP_INLINE
655 chk_sum += iphdr->_v_hl_tos;
656 #endif /* CHECKSUM_GEN_IP_INLINE */
657 IPH_LEN_SET(iphdr, htons(p->tot_len));
658 #if CHECKSUM_GEN_IP_INLINE
659 chk_sum += iphdr->_len;
660 #endif /* CHECKSUM_GEN_IP_INLINE */
661 IPH_OFFSET_SET(iphdr, 0);
662 IPH_ID_SET(iphdr, htons(ip_id));
663 #if CHECKSUM_GEN_IP_INLINE
664 chk_sum += iphdr->_id;
665 #endif /* CHECKSUM_GEN_IP_INLINE */
668 if (ip_addr_isany(src)) {
669 ip_addr_copy(iphdr->src, netif->ip_addr);
671 /* src cannot be NULL here */
672 ip_addr_copy(iphdr->src, *src);
675 #if CHECKSUM_GEN_IP_INLINE
676 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
677 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
678 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
679 chk_sum = (chk_sum >> 16) + chk_sum;
681 iphdr->_chksum = chk_sum; /* network order */
682 #else /* CHECKSUM_GEN_IP_INLINE */
683 IPH_CHKSUM_SET(iphdr, 0);
685 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
687 #endif /* CHECKSUM_GEN_IP_INLINE */
689 /* IP header already included in p */
690 iphdr = (struct ip_hdr *)p->payload;
691 ip_addr_copy(dest_addr, iphdr->dest);
695 IP_STATS_INC(ip.xmit);
697 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
701 if (ip_addr_cmp(dest, &netif->ip_addr)) {
702 /* Packet to self, enqueue it for loopback */
703 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
704 return netif_loop_output(netif, p, dest);
707 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
708 netif_loop_output(netif, p, dest);
710 #endif /* LWIP_IGMP */
711 #endif /* ENABLE_LOOPBACK */
713 /* don't fragment if interface has mtu set to 0 [loopif] */
714 if (netif->mtu && (p->tot_len > netif->mtu)) {
715 return ip_frag(p, netif, dest);
719 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
720 return netif->output(netif, p, dest);
724 * Simple interface to ip_output_if. It finds the outgoing network
725 * interface and calls upon ip_output_if to do the actual work.
727 * @param p the packet to send (p->payload points to the data, e.g. next
728 protocol header; if dest == IP_HDRINCL, p already includes an IP
729 header and p->payload points to that IP header)
730 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
731 * IP address of the netif used to send is used as source address)
732 * @param dest the destination IP address to send the packet to
733 * @param ttl the TTL value to be set in the IP header
734 * @param tos the TOS value to be set in the IP header
735 * @param proto the PROTOCOL to be set in the IP header
737 * @return ERR_RTE if no route is found
738 * see ip_output_if() for more return values
741 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
742 u8_t ttl, u8_t tos, u8_t proto)
746 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
747 gets altered as the packet is passed down the stack */
748 LWIP_ASSERT("p->ref == 1", p->ref == 1);
750 if ((netif = ip_route(dest)) == NULL) {
751 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
752 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
753 IP_STATS_INC(ip.rterr);
757 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
760 #if LWIP_NETIF_HWADDRHINT
761 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
762 * before calling ip_output_if.
764 * @param p the packet to send (p->payload points to the data, e.g. next
765 protocol header; if dest == IP_HDRINCL, p already includes an IP
766 header and p->payload points to that IP header)
767 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
768 * IP address of the netif used to send is used as source address)
769 * @param dest the destination IP address to send the packet to
770 * @param ttl the TTL value to be set in the IP header
771 * @param tos the TOS value to be set in the IP header
772 * @param proto the PROTOCOL to be set in the IP header
773 * @param addr_hint address hint pointer set to netif->addr_hint before
774 * calling ip_output_if()
776 * @return ERR_RTE if no route is found
777 * see ip_output_if() for more return values
780 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
781 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
786 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
787 gets altered as the packet is passed down the stack */
788 LWIP_ASSERT("p->ref == 1", p->ref == 1);
790 if ((netif = ip_route(dest)) == NULL) {
791 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
792 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
793 IP_STATS_INC(ip.rterr);
797 NETIF_SET_HWADDRHINT(netif, addr_hint);
798 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
799 NETIF_SET_HWADDRHINT(netif, NULL);
803 #endif /* LWIP_NETIF_HWADDRHINT*/
806 /* Print an IP header by using LWIP_DEBUGF
807 * @param p an IP packet, p->payload pointing to the IP header
810 ip_debug_print(struct pbuf *p)
812 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
815 payload = (u8_t *)iphdr + IP_HLEN;
817 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
818 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
819 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
823 ntohs(IPH_LEN(iphdr))));
824 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
825 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
826 ntohs(IPH_ID(iphdr)),
827 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
828 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
829 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
830 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
831 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
832 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
835 ntohs(IPH_CHKSUM(iphdr))));
836 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
837 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
838 ip4_addr1_16(&iphdr->src),
839 ip4_addr2_16(&iphdr->src),
840 ip4_addr3_16(&iphdr->src),
841 ip4_addr4_16(&iphdr->src)));
842 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
843 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
844 ip4_addr1_16(&iphdr->dest),
845 ip4_addr2_16(&iphdr->dest),
846 ip4_addr3_16(&iphdr->dest),
847 ip4_addr4_16(&iphdr->dest)));
848 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
850 #endif /* IP_DEBUG */