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 */
97 * The interface that provided the packet for the current callback
100 struct netif *current_netif;
103 * Header of the input packet currently being processed.
105 const struct ip_hdr *current_header;
106 /** Source IP address of current_header */
107 ip_addr_t current_iphdr_src;
108 /** Destination IP address of current_header */
109 ip_addr_t current_iphdr_dest;
111 /** The IP header ID of the next outgoing IP packet */
115 * Finds the appropriate network interface for a given IP address. It
116 * searches the list of network interfaces linearly. A match is found
117 * if the masked IP address of the network interface equals the masked
118 * IP address given to the function.
120 * @param dest the destination IP address for which to find the route
121 * @return the netif on which to send to reach dest
124 ip_route(ip_addr_t *dest)
128 /* iterate through netifs */
129 for(netif = netif_list; netif != NULL; netif = netif->next) {
130 /* network mask matches? */
131 if (netif_is_up(netif)) {
132 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
133 /* return netif on which to forward IP packet */
138 if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
139 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
140 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
141 IP_STATS_INC(ip.rterr);
142 snmp_inc_ipoutnoroutes();
145 /* no matching netif found, use default netif */
146 return netif_default;
151 * Forwards an IP packet. It finds an appropriate route for the
152 * packet, decrements the TTL value of the packet, adjusts the
153 * checksum and outputs the packet on the appropriate interface.
155 * @param p the packet to forward (p->payload points to IP header)
156 * @param iphdr the IP header of the input packet
157 * @param inp the netif on which this packet was received
160 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
166 /* RFC3927 2.7: do not forward link-local addresses */
167 if (ip_addr_islinklocal(¤t_iphdr_dest)) {
168 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
169 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
170 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
174 /* Find network interface where to forward this IP packet to. */
175 netif = ip_route(¤t_iphdr_dest);
177 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
178 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
179 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
182 /* Do not forward packets onto the same network interface on which
185 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
190 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
191 /* send ICMP if TTL == 0 */
192 if (IPH_TTL(iphdr) == 0) {
193 snmp_inc_ipinhdrerrors();
195 /* Don't send ICMP messages in response to ICMP messages */
196 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
197 icmp_time_exceeded(p, ICMP_TE_TTL);
199 #endif /* LWIP_ICMP */
203 /* Incrementally update the IP checksum. */
204 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
205 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
207 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
210 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
211 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
212 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
215 IP_STATS_INC(ip.xmit);
216 snmp_inc_ipforwdatagrams();
218 PERF_STOP("ip_forward");
219 /* transmit pbuf on chosen interface */
220 netif->output(netif, p, ¤t_iphdr_dest);
223 snmp_inc_ipoutnoroutes();
225 #endif /* IP_FORWARD */
228 * This function is called by the network interface device driver when
229 * an IP packet is received. The function does the basic checks of the
230 * IP header such as packet size being at least larger than the header
231 * size etc. If the packet was not destined for us, the packet is
232 * forwarded (using ip_forward). The IP checksum is always checked.
234 * Finally, the packet is sent to the upper layer protocol input function.
236 * @param p the received IP packet (p->payload points to IP header)
237 * @param inp the netif on which this packet was received
238 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
239 * processed, but currently always returns ERR_OK)
242 ip_input(struct pbuf *p, struct netif *inp)
244 struct ip_hdr *iphdr;
248 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
250 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
252 IP_STATS_INC(ip.recv);
253 snmp_inc_ipinreceives();
255 /* identify the IP header */
256 iphdr = (struct ip_hdr *)p->payload;
257 if (IPH_V(iphdr) != 4) {
258 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
261 IP_STATS_INC(ip.err);
262 IP_STATS_INC(ip.drop);
263 snmp_inc_ipinhdrerrors();
267 /* obtain IP header length in number of 32-bit words */
268 iphdr_hlen = IPH_HL(iphdr);
269 /* calculate IP header length in bytes */
271 /* obtain ip length in bytes */
272 iphdr_len = ntohs(IPH_LEN(iphdr));
274 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
275 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
276 if (iphdr_hlen > p->len) {
277 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
278 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
279 iphdr_hlen, p->len));
281 if (iphdr_len > p->tot_len) {
282 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
283 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
284 iphdr_len, p->tot_len));
286 /* free (drop) packet pbufs */
288 IP_STATS_INC(ip.lenerr);
289 IP_STATS_INC(ip.drop);
290 snmp_inc_ipindiscards();
294 /* verify checksum */
295 #if CHECKSUM_CHECK_IP
296 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
298 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
299 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
302 IP_STATS_INC(ip.chkerr);
303 IP_STATS_INC(ip.drop);
304 snmp_inc_ipinhdrerrors();
309 /* Trim pbuf. This should have been done at the netif layer,
310 * but we'll do it anyway just to be sure that its done. */
311 pbuf_realloc(p, iphdr_len);
313 /* copy IP addresses to aligned ip_addr_t */
314 ip_addr_copy(current_iphdr_dest, iphdr->dest);
315 ip_addr_copy(current_iphdr_src, iphdr->src);
317 /* match packet against an interface, i.e. is this packet for us? */
319 if (ip_addr_ismulticast(¤t_iphdr_dest)) {
320 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ¤t_iphdr_dest))) {
326 #endif /* LWIP_IGMP */
328 /* start trying with inp. if that's not acceptable, start walking the
329 list of configured netifs.
330 'first' is used as a boolean to mark whether we started walking the list */
334 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",
335 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
336 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
337 ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
338 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
340 /* interface is up and configured? */
341 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
342 /* unicast to this interface address? */
343 if (ip_addr_cmp(¤t_iphdr_dest, &(netif->ip_addr)) ||
344 /* or broadcast on this interface network address? */
345 ip_addr_isbroadcast(¤t_iphdr_dest, netif)) {
346 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
347 netif->name[0], netif->name[1]));
348 /* break out of for loop */
352 /* connections to link-local addresses must persist after changing
353 the netif's address (RFC3927 ch. 1.9) */
354 if ((netif->autoip != NULL) &&
355 ip_addr_cmp(¤t_iphdr_dest, &(netif->autoip->llipaddr))) {
356 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
357 netif->name[0], netif->name[1]));
358 /* break out of for loop */
361 #endif /* LWIP_AUTOIP */
372 } while(netif != NULL);
375 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
376 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
377 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
378 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
380 * If you want to accept private broadcast communication while a netif is down,
381 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
383 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
386 /* remote port is DHCP server? */
387 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
388 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
389 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
390 ntohs(udphdr->dest)));
391 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
392 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
398 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
400 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
401 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
402 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
403 if (check_ip_src && !ip_addr_isany(¤t_iphdr_src))
404 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
405 { if ((ip_addr_isbroadcast(¤t_iphdr_src, inp)) ||
406 (ip_addr_ismulticast(¤t_iphdr_src))) {
407 /* packet source is not valid */
408 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
409 /* free (drop) packet pbufs */
411 IP_STATS_INC(ip.drop);
412 snmp_inc_ipinaddrerrors();
413 snmp_inc_ipindiscards();
418 /* packet not for us? */
420 /* packet not for us, route or discard */
421 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
423 /* non-broadcast packet? */
424 if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp)) {
425 /* try to forward IP packet on (other) interfaces */
426 ip_forward(p, iphdr, inp);
428 #endif /* IP_FORWARD */
430 snmp_inc_ipinaddrerrors();
431 snmp_inc_ipindiscards();
436 /* packet consists of multiple fragments? */
437 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
438 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
439 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",
440 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));
441 /* reassemble the packet*/
443 /* packet not fully reassembled yet? */
447 iphdr = (struct ip_hdr *)p->payload;
448 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
450 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
451 ntohs(IPH_OFFSET(iphdr))));
452 IP_STATS_INC(ip.opterr);
453 IP_STATS_INC(ip.drop);
454 /* unsupported protocol feature */
455 snmp_inc_ipinunknownprotos();
457 #endif /* IP_REASSEMBLY */
460 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
463 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
464 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
466 if (iphdr_hlen > IP_HLEN) {
467 #endif /* LWIP_IGMP */
468 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
470 IP_STATS_INC(ip.opterr);
471 IP_STATS_INC(ip.drop);
472 /* unsupported protocol feature */
473 snmp_inc_ipinunknownprotos();
476 #endif /* IP_OPTIONS_ALLOWED == 0 */
478 /* send to upper layers */
479 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
481 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
484 current_header = iphdr;
487 /* raw input did not eat the packet? */
488 if (raw_input(p, inp) == 0)
489 #endif /* LWIP_RAW */
492 switch (IPH_PROTO(iphdr)) {
496 case IP_PROTO_UDPLITE:
497 #endif /* LWIP_UDPLITE */
498 snmp_inc_ipindelivers();
501 #endif /* LWIP_UDP */
504 snmp_inc_ipindelivers();
507 #endif /* LWIP_TCP */
510 snmp_inc_ipindelivers();
513 #endif /* LWIP_ICMP */
516 igmp_input(p, inp, ¤t_iphdr_dest);
518 #endif /* LWIP_IGMP */
521 /* send ICMP destination protocol unreachable unless is was a broadcast */
522 if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp) &&
523 !ip_addr_ismulticast(¤t_iphdr_dest)) {
525 icmp_dest_unreach(p, ICMP_DUR_PROTO);
527 #endif /* LWIP_ICMP */
530 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
532 IP_STATS_INC(ip.proterr);
533 IP_STATS_INC(ip.drop);
534 snmp_inc_ipinunknownprotos();
538 current_netif = NULL;
539 current_header = NULL;
540 ip_addr_set_any(¤t_iphdr_src);
541 ip_addr_set_any(¤t_iphdr_dest);
547 * Sends an IP packet on a network interface. This function constructs
548 * the IP header and calculates the IP header checksum. If the source
549 * IP address is NULL, the IP address of the outgoing network
550 * interface is filled in as source address.
551 * If the destination IP address is IP_HDRINCL, p is assumed to already
552 * include an IP header and p->payload points to it instead of the data.
554 * @param p the packet to send (p->payload points to the data, e.g. next
555 protocol header; if dest == IP_HDRINCL, p already includes an IP
556 header and p->payload points to that IP header)
557 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
558 * IP address of the netif used to send is used as source address)
559 * @param dest the destination IP address to send the packet to
560 * @param ttl the TTL value to be set in the IP header
561 * @param tos the TOS value to be set in the IP header
562 * @param proto the PROTOCOL to be set in the IP header
563 * @param netif the netif on which to send this packet
564 * @return ERR_OK if the packet was sent OK
565 * ERR_BUF if p doesn't have enough space for IP/LINK headers
566 * returns errors returned by netif->output
568 * @note ip_id: RFC791 "some host may be able to simply use
569 * unique identifiers independent of destination"
572 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
574 u8_t proto, struct netif *netif)
577 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
581 * Same as ip_output_if() but with the possibility to include IP options:
583 * @ param ip_options pointer to the IP options, copied into the IP header
584 * @ param optlen length of ip_options
586 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
587 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
590 #endif /* IP_OPTIONS_SEND */
591 struct ip_hdr *iphdr;
593 #if CHECKSUM_GEN_IP_INLINE
595 #endif /* CHECKSUM_GEN_IP_INLINE */
597 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
598 gets altered as the packet is passed down the stack */
599 LWIP_ASSERT("p->ref == 1", p->ref == 1);
601 snmp_inc_ipoutrequests();
603 /* Should the IP header be generated or is it already included in p? */
604 if (dest != IP_HDRINCL) {
605 u16_t ip_hlen = IP_HLEN;
607 u16_t optlen_aligned = 0;
609 #if CHECKSUM_GEN_IP_INLINE
611 #endif /* CHECKSUM_GEN_IP_INLINE */
612 /* round up to a multiple of 4 */
613 optlen_aligned = ((optlen + 3) & ~3);
614 ip_hlen += optlen_aligned;
615 /* First write in the IP options */
616 if (pbuf_header(p, optlen_aligned)) {
617 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
618 IP_STATS_INC(ip.err);
619 snmp_inc_ipoutdiscards();
622 MEMCPY(p->payload, ip_options, optlen);
623 if (optlen < optlen_aligned) {
624 /* zero the remaining bytes */
625 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
627 #if CHECKSUM_GEN_IP_INLINE
628 for (i = 0; i < optlen_aligned/2; i++) {
629 chk_sum += ((u16_t*)p->payload)[i];
631 #endif /* CHECKSUM_GEN_IP_INLINE */
633 #endif /* IP_OPTIONS_SEND */
634 /* generate IP header */
635 if (pbuf_header(p, IP_HLEN)) {
636 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
638 IP_STATS_INC(ip.err);
639 snmp_inc_ipoutdiscards();
643 iphdr = (struct ip_hdr *)p->payload;
644 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
645 (p->len >= sizeof(struct ip_hdr)));
647 IPH_TTL_SET(iphdr, ttl);
648 IPH_PROTO_SET(iphdr, proto);
649 #if CHECKSUM_GEN_IP_INLINE
650 chk_sum += LWIP_MAKE_U16(proto, ttl);
651 #endif /* CHECKSUM_GEN_IP_INLINE */
653 /* dest cannot be NULL here */
654 ip_addr_copy(iphdr->dest, *dest);
655 #if CHECKSUM_GEN_IP_INLINE
656 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
657 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
658 #endif /* CHECKSUM_GEN_IP_INLINE */
660 IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
661 #if CHECKSUM_GEN_IP_INLINE
662 chk_sum += iphdr->_v_hl_tos;
663 #endif /* CHECKSUM_GEN_IP_INLINE */
664 IPH_LEN_SET(iphdr, htons(p->tot_len));
665 #if CHECKSUM_GEN_IP_INLINE
666 chk_sum += iphdr->_len;
667 #endif /* CHECKSUM_GEN_IP_INLINE */
668 IPH_OFFSET_SET(iphdr, 0);
669 IPH_ID_SET(iphdr, htons(ip_id));
670 #if CHECKSUM_GEN_IP_INLINE
671 chk_sum += iphdr->_id;
672 #endif /* CHECKSUM_GEN_IP_INLINE */
675 if (ip_addr_isany(src)) {
676 ip_addr_copy(iphdr->src, netif->ip_addr);
678 /* src cannot be NULL here */
679 ip_addr_copy(iphdr->src, *src);
682 #if CHECKSUM_GEN_IP_INLINE
683 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
684 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
685 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
686 chk_sum = (chk_sum >> 16) + chk_sum;
688 iphdr->_chksum = chk_sum; /* network order */
689 #else /* CHECKSUM_GEN_IP_INLINE */
690 IPH_CHKSUM_SET(iphdr, 0);
692 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
694 #endif /* CHECKSUM_GEN_IP_INLINE */
696 /* IP header already included in p */
697 iphdr = (struct ip_hdr *)p->payload;
698 ip_addr_copy(dest_addr, iphdr->dest);
702 IP_STATS_INC(ip.xmit);
704 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
708 if (ip_addr_cmp(dest, &netif->ip_addr)) {
709 /* Packet to self, enqueue it for loopback */
710 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
711 return netif_loop_output(netif, p, dest);
714 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
715 netif_loop_output(netif, p, dest);
717 #endif /* LWIP_IGMP */
718 #endif /* ENABLE_LOOPBACK */
720 /* don't fragment if interface has mtu set to 0 [loopif] */
721 if (netif->mtu && (p->tot_len > netif->mtu)) {
722 return ip_frag(p, netif, dest);
726 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
727 return netif->output(netif, p, dest);
731 * Simple interface to ip_output_if. It finds the outgoing network
732 * interface and calls upon ip_output_if to do the actual work.
734 * @param p the packet to send (p->payload points to the data, e.g. next
735 protocol header; if dest == IP_HDRINCL, p already includes an IP
736 header and p->payload points to that IP header)
737 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
738 * IP address of the netif used to send is used as source address)
739 * @param dest the destination IP address to send the packet to
740 * @param ttl the TTL value to be set in the IP header
741 * @param tos the TOS value to be set in the IP header
742 * @param proto the PROTOCOL to be set in the IP header
744 * @return ERR_RTE if no route is found
745 * see ip_output_if() for more return values
748 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
749 u8_t ttl, u8_t tos, u8_t proto)
753 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
754 gets altered as the packet is passed down the stack */
755 LWIP_ASSERT("p->ref == 1", p->ref == 1);
757 if ((netif = ip_route(dest)) == NULL) {
758 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
759 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
760 IP_STATS_INC(ip.rterr);
764 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
767 #if LWIP_NETIF_HWADDRHINT
768 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
769 * before calling ip_output_if.
771 * @param p the packet to send (p->payload points to the data, e.g. next
772 protocol header; if dest == IP_HDRINCL, p already includes an IP
773 header and p->payload points to that IP header)
774 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
775 * IP address of the netif used to send is used as source address)
776 * @param dest the destination IP address to send the packet to
777 * @param ttl the TTL value to be set in the IP header
778 * @param tos the TOS value to be set in the IP header
779 * @param proto the PROTOCOL to be set in the IP header
780 * @param addr_hint address hint pointer set to netif->addr_hint before
781 * calling ip_output_if()
783 * @return ERR_RTE if no route is found
784 * see ip_output_if() for more return values
787 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
788 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
793 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
794 gets altered as the packet is passed down the stack */
795 LWIP_ASSERT("p->ref == 1", p->ref == 1);
797 if ((netif = ip_route(dest)) == NULL) {
798 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
799 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
800 IP_STATS_INC(ip.rterr);
804 netif->addr_hint = addr_hint;
805 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
806 netif->addr_hint = NULL;
810 #endif /* LWIP_NETIF_HWADDRHINT*/
813 /* Print an IP header by using LWIP_DEBUGF
814 * @param p an IP packet, p->payload pointing to the IP header
817 ip_debug_print(struct pbuf *p)
819 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
822 payload = (u8_t *)iphdr + IP_HLEN;
824 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
825 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
826 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
830 ntohs(IPH_LEN(iphdr))));
831 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
832 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
833 ntohs(IPH_ID(iphdr)),
834 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
835 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
836 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
837 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
838 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
839 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
842 ntohs(IPH_CHKSUM(iphdr))));
843 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
844 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
845 ip4_addr1_16(&iphdr->src),
846 ip4_addr2_16(&iphdr->src),
847 ip4_addr3_16(&iphdr->src),
848 ip4_addr4_16(&iphdr->src)));
849 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
850 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
851 ip4_addr1_16(&iphdr->dest),
852 ip4_addr2_16(&iphdr->dest),
853 ip4_addr3_16(&iphdr->dest),
854 ip4_addr4_16(&iphdr->dest)));
855 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
857 #endif /* IP_DEBUG */