![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
HTTPClient using static IP
NetServices/lwip/core/ipv4/ip.c@0:d8f2f7d5f31b, 2011-05-30 (annotated)
- Committer:
- mr_q
- Date:
- Mon May 30 11:53:37 2011 +0000
- Revision:
- 0:d8f2f7d5f31b
v0.01 Draft
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mr_q | 0:d8f2f7d5f31b | 1 | /** |
mr_q | 0:d8f2f7d5f31b | 2 | * @file |
mr_q | 0:d8f2f7d5f31b | 3 | * This is the IPv4 layer implementation for incoming and outgoing IP traffic. |
mr_q | 0:d8f2f7d5f31b | 4 | * |
mr_q | 0:d8f2f7d5f31b | 5 | * @see ip_frag.c |
mr_q | 0:d8f2f7d5f31b | 6 | * |
mr_q | 0:d8f2f7d5f31b | 7 | */ |
mr_q | 0:d8f2f7d5f31b | 8 | |
mr_q | 0:d8f2f7d5f31b | 9 | /* |
mr_q | 0:d8f2f7d5f31b | 10 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
mr_q | 0:d8f2f7d5f31b | 11 | * All rights reserved. |
mr_q | 0:d8f2f7d5f31b | 12 | * |
mr_q | 0:d8f2f7d5f31b | 13 | * Redistribution and use in source and binary forms, with or without modification, |
mr_q | 0:d8f2f7d5f31b | 14 | * are permitted provided that the following conditions are met: |
mr_q | 0:d8f2f7d5f31b | 15 | * |
mr_q | 0:d8f2f7d5f31b | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
mr_q | 0:d8f2f7d5f31b | 17 | * this list of conditions and the following disclaimer. |
mr_q | 0:d8f2f7d5f31b | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mr_q | 0:d8f2f7d5f31b | 19 | * this list of conditions and the following disclaimer in the documentation |
mr_q | 0:d8f2f7d5f31b | 20 | * and/or other materials provided with the distribution. |
mr_q | 0:d8f2f7d5f31b | 21 | * 3. The name of the author may not be used to endorse or promote products |
mr_q | 0:d8f2f7d5f31b | 22 | * derived from this software without specific prior written permission. |
mr_q | 0:d8f2f7d5f31b | 23 | * |
mr_q | 0:d8f2f7d5f31b | 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
mr_q | 0:d8f2f7d5f31b | 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
mr_q | 0:d8f2f7d5f31b | 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
mr_q | 0:d8f2f7d5f31b | 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
mr_q | 0:d8f2f7d5f31b | 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
mr_q | 0:d8f2f7d5f31b | 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mr_q | 0:d8f2f7d5f31b | 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mr_q | 0:d8f2f7d5f31b | 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
mr_q | 0:d8f2f7d5f31b | 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
mr_q | 0:d8f2f7d5f31b | 33 | * OF SUCH DAMAGE. |
mr_q | 0:d8f2f7d5f31b | 34 | * |
mr_q | 0:d8f2f7d5f31b | 35 | * This file is part of the lwIP TCP/IP stack. |
mr_q | 0:d8f2f7d5f31b | 36 | * |
mr_q | 0:d8f2f7d5f31b | 37 | * Author: Adam Dunkels <adam@sics.se> |
mr_q | 0:d8f2f7d5f31b | 38 | * |
mr_q | 0:d8f2f7d5f31b | 39 | */ |
mr_q | 0:d8f2f7d5f31b | 40 | |
mr_q | 0:d8f2f7d5f31b | 41 | #include "lwip/opt.h" |
mr_q | 0:d8f2f7d5f31b | 42 | #include "lwip/ip.h" |
mr_q | 0:d8f2f7d5f31b | 43 | #include "lwip/def.h" |
mr_q | 0:d8f2f7d5f31b | 44 | #include "lwip/mem.h" |
mr_q | 0:d8f2f7d5f31b | 45 | #include "lwip/ip_frag.h" |
mr_q | 0:d8f2f7d5f31b | 46 | #include "lwip/inet_chksum.h" |
mr_q | 0:d8f2f7d5f31b | 47 | #include "lwip/netif.h" |
mr_q | 0:d8f2f7d5f31b | 48 | #include "lwip/icmp.h" |
mr_q | 0:d8f2f7d5f31b | 49 | #include "lwip/igmp.h" |
mr_q | 0:d8f2f7d5f31b | 50 | #include "lwip/raw.h" |
mr_q | 0:d8f2f7d5f31b | 51 | #include "lwip/udp.h" |
mr_q | 0:d8f2f7d5f31b | 52 | #include "lwip/tcp_impl.h" |
mr_q | 0:d8f2f7d5f31b | 53 | #include "lwip/snmp.h" |
mr_q | 0:d8f2f7d5f31b | 54 | #include "lwip/dhcp.h" |
mr_q | 0:d8f2f7d5f31b | 55 | #include "lwip/autoip.h" |
mr_q | 0:d8f2f7d5f31b | 56 | #include "lwip/stats.h" |
mr_q | 0:d8f2f7d5f31b | 57 | #include "arch/perf.h" |
mr_q | 0:d8f2f7d5f31b | 58 | |
mr_q | 0:d8f2f7d5f31b | 59 | #include <string.h> |
mr_q | 0:d8f2f7d5f31b | 60 | |
mr_q | 0:d8f2f7d5f31b | 61 | /** Set this to 0 in the rare case of wanting to call an extra function to |
mr_q | 0:d8f2f7d5f31b | 62 | * generate the IP checksum (in contrast to calculating it on-the-fly). */ |
mr_q | 0:d8f2f7d5f31b | 63 | #ifndef LWIP_INLINE_IP_CHKSUM |
mr_q | 0:d8f2f7d5f31b | 64 | #define LWIP_INLINE_IP_CHKSUM 1 |
mr_q | 0:d8f2f7d5f31b | 65 | #endif |
mr_q | 0:d8f2f7d5f31b | 66 | #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP |
mr_q | 0:d8f2f7d5f31b | 67 | #define CHECKSUM_GEN_IP_INLINE 1 |
mr_q | 0:d8f2f7d5f31b | 68 | #else |
mr_q | 0:d8f2f7d5f31b | 69 | #define CHECKSUM_GEN_IP_INLINE 0 |
mr_q | 0:d8f2f7d5f31b | 70 | #endif |
mr_q | 0:d8f2f7d5f31b | 71 | |
mr_q | 0:d8f2f7d5f31b | 72 | #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT) |
mr_q | 0:d8f2f7d5f31b | 73 | #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1 |
mr_q | 0:d8f2f7d5f31b | 74 | |
mr_q | 0:d8f2f7d5f31b | 75 | /** Some defines for DHCP to let link-layer-addressed packets through while the |
mr_q | 0:d8f2f7d5f31b | 76 | * netif is down. |
mr_q | 0:d8f2f7d5f31b | 77 | * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT |
mr_q | 0:d8f2f7d5f31b | 78 | * to return 1 if the port is accepted and 0 if the port is not accepted. |
mr_q | 0:d8f2f7d5f31b | 79 | */ |
mr_q | 0:d8f2f7d5f31b | 80 | #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) |
mr_q | 0:d8f2f7d5f31b | 81 | /* accept DHCP client port and custom port */ |
mr_q | 0:d8f2f7d5f31b | 82 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \ |
mr_q | 0:d8f2f7d5f31b | 83 | || (LWIP_IP_ACCEPT_UDP_PORT(port))) |
mr_q | 0:d8f2f7d5f31b | 84 | #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
mr_q | 0:d8f2f7d5f31b | 85 | /* accept custom port only */ |
mr_q | 0:d8f2f7d5f31b | 86 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(dst_port)) |
mr_q | 0:d8f2f7d5f31b | 87 | #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
mr_q | 0:d8f2f7d5f31b | 88 | /* accept DHCP client port only */ |
mr_q | 0:d8f2f7d5f31b | 89 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT)) |
mr_q | 0:d8f2f7d5f31b | 90 | #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
mr_q | 0:d8f2f7d5f31b | 91 | |
mr_q | 0:d8f2f7d5f31b | 92 | #else /* LWIP_DHCP */ |
mr_q | 0:d8f2f7d5f31b | 93 | #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0 |
mr_q | 0:d8f2f7d5f31b | 94 | #endif /* LWIP_DHCP */ |
mr_q | 0:d8f2f7d5f31b | 95 | |
mr_q | 0:d8f2f7d5f31b | 96 | /** |
mr_q | 0:d8f2f7d5f31b | 97 | * The interface that provided the packet for the current callback |
mr_q | 0:d8f2f7d5f31b | 98 | * invocation. |
mr_q | 0:d8f2f7d5f31b | 99 | */ |
mr_q | 0:d8f2f7d5f31b | 100 | struct netif *current_netif; |
mr_q | 0:d8f2f7d5f31b | 101 | |
mr_q | 0:d8f2f7d5f31b | 102 | /** |
mr_q | 0:d8f2f7d5f31b | 103 | * Header of the input packet currently being processed. |
mr_q | 0:d8f2f7d5f31b | 104 | */ |
mr_q | 0:d8f2f7d5f31b | 105 | const struct ip_hdr *current_header; |
mr_q | 0:d8f2f7d5f31b | 106 | /** Source IP address of current_header */ |
mr_q | 0:d8f2f7d5f31b | 107 | ip_addr_t current_iphdr_src; |
mr_q | 0:d8f2f7d5f31b | 108 | /** Destination IP address of current_header */ |
mr_q | 0:d8f2f7d5f31b | 109 | ip_addr_t current_iphdr_dest; |
mr_q | 0:d8f2f7d5f31b | 110 | |
mr_q | 0:d8f2f7d5f31b | 111 | /** The IP header ID of the next outgoing IP packet */ |
mr_q | 0:d8f2f7d5f31b | 112 | static u16_t ip_id; |
mr_q | 0:d8f2f7d5f31b | 113 | |
mr_q | 0:d8f2f7d5f31b | 114 | /** |
mr_q | 0:d8f2f7d5f31b | 115 | * Finds the appropriate network interface for a given IP address. It |
mr_q | 0:d8f2f7d5f31b | 116 | * searches the list of network interfaces linearly. A match is found |
mr_q | 0:d8f2f7d5f31b | 117 | * if the masked IP address of the network interface equals the masked |
mr_q | 0:d8f2f7d5f31b | 118 | * IP address given to the function. |
mr_q | 0:d8f2f7d5f31b | 119 | * |
mr_q | 0:d8f2f7d5f31b | 120 | * @param dest the destination IP address for which to find the route |
mr_q | 0:d8f2f7d5f31b | 121 | * @return the netif on which to send to reach dest |
mr_q | 0:d8f2f7d5f31b | 122 | */ |
mr_q | 0:d8f2f7d5f31b | 123 | struct netif * |
mr_q | 0:d8f2f7d5f31b | 124 | ip_route(ip_addr_t *dest) |
mr_q | 0:d8f2f7d5f31b | 125 | { |
mr_q | 0:d8f2f7d5f31b | 126 | struct netif *netif; |
mr_q | 0:d8f2f7d5f31b | 127 | |
mr_q | 0:d8f2f7d5f31b | 128 | /* iterate through netifs */ |
mr_q | 0:d8f2f7d5f31b | 129 | for(netif = netif_list; netif != NULL; netif = netif->next) { |
mr_q | 0:d8f2f7d5f31b | 130 | /* network mask matches? */ |
mr_q | 0:d8f2f7d5f31b | 131 | if (netif_is_up(netif)) { |
mr_q | 0:d8f2f7d5f31b | 132 | if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) { |
mr_q | 0:d8f2f7d5f31b | 133 | /* return netif on which to forward IP packet */ |
mr_q | 0:d8f2f7d5f31b | 134 | return netif; |
mr_q | 0:d8f2f7d5f31b | 135 | } |
mr_q | 0:d8f2f7d5f31b | 136 | } |
mr_q | 0:d8f2f7d5f31b | 137 | } |
mr_q | 0:d8f2f7d5f31b | 138 | if ((netif_default == NULL) || (!netif_is_up(netif_default))) { |
mr_q | 0:d8f2f7d5f31b | 139 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 140 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
mr_q | 0:d8f2f7d5f31b | 141 | IP_STATS_INC(ip.rterr); |
mr_q | 0:d8f2f7d5f31b | 142 | snmp_inc_ipoutnoroutes(); |
mr_q | 0:d8f2f7d5f31b | 143 | return NULL; |
mr_q | 0:d8f2f7d5f31b | 144 | } |
mr_q | 0:d8f2f7d5f31b | 145 | /* no matching netif found, use default netif */ |
mr_q | 0:d8f2f7d5f31b | 146 | return netif_default; |
mr_q | 0:d8f2f7d5f31b | 147 | } |
mr_q | 0:d8f2f7d5f31b | 148 | |
mr_q | 0:d8f2f7d5f31b | 149 | #if IP_FORWARD |
mr_q | 0:d8f2f7d5f31b | 150 | /** |
mr_q | 0:d8f2f7d5f31b | 151 | * Forwards an IP packet. It finds an appropriate route for the |
mr_q | 0:d8f2f7d5f31b | 152 | * packet, decrements the TTL value of the packet, adjusts the |
mr_q | 0:d8f2f7d5f31b | 153 | * checksum and outputs the packet on the appropriate interface. |
mr_q | 0:d8f2f7d5f31b | 154 | * |
mr_q | 0:d8f2f7d5f31b | 155 | * @param p the packet to forward (p->payload points to IP header) |
mr_q | 0:d8f2f7d5f31b | 156 | * @param iphdr the IP header of the input packet |
mr_q | 0:d8f2f7d5f31b | 157 | * @param inp the netif on which this packet was received |
mr_q | 0:d8f2f7d5f31b | 158 | */ |
mr_q | 0:d8f2f7d5f31b | 159 | static void |
mr_q | 0:d8f2f7d5f31b | 160 | ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) |
mr_q | 0:d8f2f7d5f31b | 161 | { |
mr_q | 0:d8f2f7d5f31b | 162 | struct netif *netif; |
mr_q | 0:d8f2f7d5f31b | 163 | |
mr_q | 0:d8f2f7d5f31b | 164 | PERF_START; |
mr_q | 0:d8f2f7d5f31b | 165 | |
mr_q | 0:d8f2f7d5f31b | 166 | /* RFC3927 2.7: do not forward link-local addresses */ |
mr_q | 0:d8f2f7d5f31b | 167 | if (ip_addr_islinklocal(¤t_iphdr_dest)) { |
mr_q | 0:d8f2f7d5f31b | 168 | LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 169 | ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest), |
mr_q | 0:d8f2f7d5f31b | 170 | ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest))); |
mr_q | 0:d8f2f7d5f31b | 171 | goto return_noroute; |
mr_q | 0:d8f2f7d5f31b | 172 | } |
mr_q | 0:d8f2f7d5f31b | 173 | |
mr_q | 0:d8f2f7d5f31b | 174 | /* Find network interface where to forward this IP packet to. */ |
mr_q | 0:d8f2f7d5f31b | 175 | netif = ip_route(¤t_iphdr_dest); |
mr_q | 0:d8f2f7d5f31b | 176 | if (netif == NULL) { |
mr_q | 0:d8f2f7d5f31b | 177 | LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", |
mr_q | 0:d8f2f7d5f31b | 178 | ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest), |
mr_q | 0:d8f2f7d5f31b | 179 | ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest))); |
mr_q | 0:d8f2f7d5f31b | 180 | goto return_noroute; |
mr_q | 0:d8f2f7d5f31b | 181 | } |
mr_q | 0:d8f2f7d5f31b | 182 | /* Do not forward packets onto the same network interface on which |
mr_q | 0:d8f2f7d5f31b | 183 | * they arrived. */ |
mr_q | 0:d8f2f7d5f31b | 184 | if (netif == inp) { |
mr_q | 0:d8f2f7d5f31b | 185 | LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n")); |
mr_q | 0:d8f2f7d5f31b | 186 | goto return_noroute; |
mr_q | 0:d8f2f7d5f31b | 187 | } |
mr_q | 0:d8f2f7d5f31b | 188 | |
mr_q | 0:d8f2f7d5f31b | 189 | /* decrement TTL */ |
mr_q | 0:d8f2f7d5f31b | 190 | IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1); |
mr_q | 0:d8f2f7d5f31b | 191 | /* send ICMP if TTL == 0 */ |
mr_q | 0:d8f2f7d5f31b | 192 | if (IPH_TTL(iphdr) == 0) { |
mr_q | 0:d8f2f7d5f31b | 193 | snmp_inc_ipinhdrerrors(); |
mr_q | 0:d8f2f7d5f31b | 194 | #if LWIP_ICMP |
mr_q | 0:d8f2f7d5f31b | 195 | /* Don't send ICMP messages in response to ICMP messages */ |
mr_q | 0:d8f2f7d5f31b | 196 | if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) { |
mr_q | 0:d8f2f7d5f31b | 197 | icmp_time_exceeded(p, ICMP_TE_TTL); |
mr_q | 0:d8f2f7d5f31b | 198 | } |
mr_q | 0:d8f2f7d5f31b | 199 | #endif /* LWIP_ICMP */ |
mr_q | 0:d8f2f7d5f31b | 200 | return; |
mr_q | 0:d8f2f7d5f31b | 201 | } |
mr_q | 0:d8f2f7d5f31b | 202 | |
mr_q | 0:d8f2f7d5f31b | 203 | /* Incrementally update the IP checksum. */ |
mr_q | 0:d8f2f7d5f31b | 204 | if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffff - 0x100)) { |
mr_q | 0:d8f2f7d5f31b | 205 | IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1); |
mr_q | 0:d8f2f7d5f31b | 206 | } else { |
mr_q | 0:d8f2f7d5f31b | 207 | IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100)); |
mr_q | 0:d8f2f7d5f31b | 208 | } |
mr_q | 0:d8f2f7d5f31b | 209 | |
mr_q | 0:d8f2f7d5f31b | 210 | LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 211 | ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest), |
mr_q | 0:d8f2f7d5f31b | 212 | ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest))); |
mr_q | 0:d8f2f7d5f31b | 213 | |
mr_q | 0:d8f2f7d5f31b | 214 | IP_STATS_INC(ip.fw); |
mr_q | 0:d8f2f7d5f31b | 215 | IP_STATS_INC(ip.xmit); |
mr_q | 0:d8f2f7d5f31b | 216 | snmp_inc_ipforwdatagrams(); |
mr_q | 0:d8f2f7d5f31b | 217 | |
mr_q | 0:d8f2f7d5f31b | 218 | PERF_STOP("ip_forward"); |
mr_q | 0:d8f2f7d5f31b | 219 | /* transmit pbuf on chosen interface */ |
mr_q | 0:d8f2f7d5f31b | 220 | netif->output(netif, p, ¤t_iphdr_dest); |
mr_q | 0:d8f2f7d5f31b | 221 | return; |
mr_q | 0:d8f2f7d5f31b | 222 | return_noroute: |
mr_q | 0:d8f2f7d5f31b | 223 | snmp_inc_ipoutnoroutes(); |
mr_q | 0:d8f2f7d5f31b | 224 | } |
mr_q | 0:d8f2f7d5f31b | 225 | #endif /* IP_FORWARD */ |
mr_q | 0:d8f2f7d5f31b | 226 | |
mr_q | 0:d8f2f7d5f31b | 227 | /** |
mr_q | 0:d8f2f7d5f31b | 228 | * This function is called by the network interface device driver when |
mr_q | 0:d8f2f7d5f31b | 229 | * an IP packet is received. The function does the basic checks of the |
mr_q | 0:d8f2f7d5f31b | 230 | * IP header such as packet size being at least larger than the header |
mr_q | 0:d8f2f7d5f31b | 231 | * size etc. If the packet was not destined for us, the packet is |
mr_q | 0:d8f2f7d5f31b | 232 | * forwarded (using ip_forward). The IP checksum is always checked. |
mr_q | 0:d8f2f7d5f31b | 233 | * |
mr_q | 0:d8f2f7d5f31b | 234 | * Finally, the packet is sent to the upper layer protocol input function. |
mr_q | 0:d8f2f7d5f31b | 235 | * |
mr_q | 0:d8f2f7d5f31b | 236 | * @param p the received IP packet (p->payload points to IP header) |
mr_q | 0:d8f2f7d5f31b | 237 | * @param inp the netif on which this packet was received |
mr_q | 0:d8f2f7d5f31b | 238 | * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't |
mr_q | 0:d8f2f7d5f31b | 239 | * processed, but currently always returns ERR_OK) |
mr_q | 0:d8f2f7d5f31b | 240 | */ |
mr_q | 0:d8f2f7d5f31b | 241 | err_t |
mr_q | 0:d8f2f7d5f31b | 242 | ip_input(struct pbuf *p, struct netif *inp) |
mr_q | 0:d8f2f7d5f31b | 243 | { |
mr_q | 0:d8f2f7d5f31b | 244 | struct ip_hdr *iphdr; |
mr_q | 0:d8f2f7d5f31b | 245 | struct netif *netif; |
mr_q | 0:d8f2f7d5f31b | 246 | u16_t iphdr_hlen; |
mr_q | 0:d8f2f7d5f31b | 247 | u16_t iphdr_len; |
mr_q | 0:d8f2f7d5f31b | 248 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING |
mr_q | 0:d8f2f7d5f31b | 249 | int check_ip_src=1; |
mr_q | 0:d8f2f7d5f31b | 250 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
mr_q | 0:d8f2f7d5f31b | 251 | |
mr_q | 0:d8f2f7d5f31b | 252 | IP_STATS_INC(ip.recv); |
mr_q | 0:d8f2f7d5f31b | 253 | snmp_inc_ipinreceives(); |
mr_q | 0:d8f2f7d5f31b | 254 | |
mr_q | 0:d8f2f7d5f31b | 255 | /* identify the IP header */ |
mr_q | 0:d8f2f7d5f31b | 256 | iphdr = (struct ip_hdr *)p->payload; |
mr_q | 0:d8f2f7d5f31b | 257 | if (IPH_V(iphdr) != 4) { |
mr_q | 0:d8f2f7d5f31b | 258 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr))); |
mr_q | 0:d8f2f7d5f31b | 259 | ip_debug_print(p); |
mr_q | 0:d8f2f7d5f31b | 260 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 261 | IP_STATS_INC(ip.err); |
mr_q | 0:d8f2f7d5f31b | 262 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 263 | snmp_inc_ipinhdrerrors(); |
mr_q | 0:d8f2f7d5f31b | 264 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 265 | } |
mr_q | 0:d8f2f7d5f31b | 266 | |
mr_q | 0:d8f2f7d5f31b | 267 | /* obtain IP header length in number of 32-bit words */ |
mr_q | 0:d8f2f7d5f31b | 268 | iphdr_hlen = IPH_HL(iphdr); |
mr_q | 0:d8f2f7d5f31b | 269 | /* calculate IP header length in bytes */ |
mr_q | 0:d8f2f7d5f31b | 270 | iphdr_hlen *= 4; |
mr_q | 0:d8f2f7d5f31b | 271 | /* obtain ip length in bytes */ |
mr_q | 0:d8f2f7d5f31b | 272 | iphdr_len = ntohs(IPH_LEN(iphdr)); |
mr_q | 0:d8f2f7d5f31b | 273 | |
mr_q | 0:d8f2f7d5f31b | 274 | /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */ |
mr_q | 0:d8f2f7d5f31b | 275 | if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) { |
mr_q | 0:d8f2f7d5f31b | 276 | if (iphdr_hlen > p->len) { |
mr_q | 0:d8f2f7d5f31b | 277 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
mr_q | 0:d8f2f7d5f31b | 278 | ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n", |
mr_q | 0:d8f2f7d5f31b | 279 | iphdr_hlen, p->len)); |
mr_q | 0:d8f2f7d5f31b | 280 | } |
mr_q | 0:d8f2f7d5f31b | 281 | if (iphdr_len > p->tot_len) { |
mr_q | 0:d8f2f7d5f31b | 282 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
mr_q | 0:d8f2f7d5f31b | 283 | ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n", |
mr_q | 0:d8f2f7d5f31b | 284 | iphdr_len, p->tot_len)); |
mr_q | 0:d8f2f7d5f31b | 285 | } |
mr_q | 0:d8f2f7d5f31b | 286 | /* free (drop) packet pbufs */ |
mr_q | 0:d8f2f7d5f31b | 287 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 288 | IP_STATS_INC(ip.lenerr); |
mr_q | 0:d8f2f7d5f31b | 289 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 290 | snmp_inc_ipindiscards(); |
mr_q | 0:d8f2f7d5f31b | 291 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 292 | } |
mr_q | 0:d8f2f7d5f31b | 293 | |
mr_q | 0:d8f2f7d5f31b | 294 | /* verify checksum */ |
mr_q | 0:d8f2f7d5f31b | 295 | #if CHECKSUM_CHECK_IP |
mr_q | 0:d8f2f7d5f31b | 296 | if (inet_chksum(iphdr, iphdr_hlen) != 0) { |
mr_q | 0:d8f2f7d5f31b | 297 | |
mr_q | 0:d8f2f7d5f31b | 298 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
mr_q | 0:d8f2f7d5f31b | 299 | ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen))); |
mr_q | 0:d8f2f7d5f31b | 300 | ip_debug_print(p); |
mr_q | 0:d8f2f7d5f31b | 301 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 302 | IP_STATS_INC(ip.chkerr); |
mr_q | 0:d8f2f7d5f31b | 303 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 304 | snmp_inc_ipinhdrerrors(); |
mr_q | 0:d8f2f7d5f31b | 305 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 306 | } |
mr_q | 0:d8f2f7d5f31b | 307 | #endif |
mr_q | 0:d8f2f7d5f31b | 308 | |
mr_q | 0:d8f2f7d5f31b | 309 | /* Trim pbuf. This should have been done at the netif layer, |
mr_q | 0:d8f2f7d5f31b | 310 | * but we'll do it anyway just to be sure that its done. */ |
mr_q | 0:d8f2f7d5f31b | 311 | pbuf_realloc(p, iphdr_len); |
mr_q | 0:d8f2f7d5f31b | 312 | |
mr_q | 0:d8f2f7d5f31b | 313 | /* copy IP addresses to aligned ip_addr_t */ |
mr_q | 0:d8f2f7d5f31b | 314 | ip_addr_copy(current_iphdr_dest, iphdr->dest); |
mr_q | 0:d8f2f7d5f31b | 315 | ip_addr_copy(current_iphdr_src, iphdr->src); |
mr_q | 0:d8f2f7d5f31b | 316 | |
mr_q | 0:d8f2f7d5f31b | 317 | /* match packet against an interface, i.e. is this packet for us? */ |
mr_q | 0:d8f2f7d5f31b | 318 | #if LWIP_IGMP |
mr_q | 0:d8f2f7d5f31b | 319 | if (ip_addr_ismulticast(¤t_iphdr_dest)) { |
mr_q | 0:d8f2f7d5f31b | 320 | if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ¤t_iphdr_dest))) { |
mr_q | 0:d8f2f7d5f31b | 321 | netif = inp; |
mr_q | 0:d8f2f7d5f31b | 322 | } else { |
mr_q | 0:d8f2f7d5f31b | 323 | netif = NULL; |
mr_q | 0:d8f2f7d5f31b | 324 | } |
mr_q | 0:d8f2f7d5f31b | 325 | } else |
mr_q | 0:d8f2f7d5f31b | 326 | #endif /* LWIP_IGMP */ |
mr_q | 0:d8f2f7d5f31b | 327 | { |
mr_q | 0:d8f2f7d5f31b | 328 | /* start trying with inp. if that's not acceptable, start walking the |
mr_q | 0:d8f2f7d5f31b | 329 | list of configured netifs. |
mr_q | 0:d8f2f7d5f31b | 330 | 'first' is used as a boolean to mark whether we started walking the list */ |
mr_q | 0:d8f2f7d5f31b | 331 | int first = 1; |
mr_q | 0:d8f2f7d5f31b | 332 | netif = inp; |
mr_q | 0:d8f2f7d5f31b | 333 | do { |
mr_q | 0:d8f2f7d5f31b | 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", |
mr_q | 0:d8f2f7d5f31b | 335 | ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr), |
mr_q | 0:d8f2f7d5f31b | 336 | ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask), |
mr_q | 0:d8f2f7d5f31b | 337 | ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask), |
mr_q | 0:d8f2f7d5f31b | 338 | ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask))); |
mr_q | 0:d8f2f7d5f31b | 339 | |
mr_q | 0:d8f2f7d5f31b | 340 | /* interface is up and configured? */ |
mr_q | 0:d8f2f7d5f31b | 341 | if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) { |
mr_q | 0:d8f2f7d5f31b | 342 | /* unicast to this interface address? */ |
mr_q | 0:d8f2f7d5f31b | 343 | if (ip_addr_cmp(¤t_iphdr_dest, &(netif->ip_addr)) || |
mr_q | 0:d8f2f7d5f31b | 344 | /* or broadcast on this interface network address? */ |
mr_q | 0:d8f2f7d5f31b | 345 | ip_addr_isbroadcast(¤t_iphdr_dest, netif)) { |
mr_q | 0:d8f2f7d5f31b | 346 | LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n", |
mr_q | 0:d8f2f7d5f31b | 347 | netif->name[0], netif->name[1])); |
mr_q | 0:d8f2f7d5f31b | 348 | /* break out of for loop */ |
mr_q | 0:d8f2f7d5f31b | 349 | break; |
mr_q | 0:d8f2f7d5f31b | 350 | } |
mr_q | 0:d8f2f7d5f31b | 351 | #if LWIP_AUTOIP |
mr_q | 0:d8f2f7d5f31b | 352 | /* connections to link-local addresses must persist after changing |
mr_q | 0:d8f2f7d5f31b | 353 | the netif's address (RFC3927 ch. 1.9) */ |
mr_q | 0:d8f2f7d5f31b | 354 | if ((netif->autoip != NULL) && |
mr_q | 0:d8f2f7d5f31b | 355 | ip_addr_cmp(¤t_iphdr_dest, &(netif->autoip->llipaddr))) { |
mr_q | 0:d8f2f7d5f31b | 356 | LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n", |
mr_q | 0:d8f2f7d5f31b | 357 | netif->name[0], netif->name[1])); |
mr_q | 0:d8f2f7d5f31b | 358 | /* break out of for loop */ |
mr_q | 0:d8f2f7d5f31b | 359 | break; |
mr_q | 0:d8f2f7d5f31b | 360 | } |
mr_q | 0:d8f2f7d5f31b | 361 | #endif /* LWIP_AUTOIP */ |
mr_q | 0:d8f2f7d5f31b | 362 | } |
mr_q | 0:d8f2f7d5f31b | 363 | if (first) { |
mr_q | 0:d8f2f7d5f31b | 364 | first = 0; |
mr_q | 0:d8f2f7d5f31b | 365 | netif = netif_list; |
mr_q | 0:d8f2f7d5f31b | 366 | } else { |
mr_q | 0:d8f2f7d5f31b | 367 | netif = netif->next; |
mr_q | 0:d8f2f7d5f31b | 368 | } |
mr_q | 0:d8f2f7d5f31b | 369 | if (netif == inp) { |
mr_q | 0:d8f2f7d5f31b | 370 | netif = netif->next; |
mr_q | 0:d8f2f7d5f31b | 371 | } |
mr_q | 0:d8f2f7d5f31b | 372 | } while(netif != NULL); |
mr_q | 0:d8f2f7d5f31b | 373 | } |
mr_q | 0:d8f2f7d5f31b | 374 | |
mr_q | 0:d8f2f7d5f31b | 375 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING |
mr_q | 0:d8f2f7d5f31b | 376 | /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed |
mr_q | 0:d8f2f7d5f31b | 377 | * using link layer addressing (such as Ethernet MAC) so we must not filter on IP. |
mr_q | 0:d8f2f7d5f31b | 378 | * According to RFC 1542 section 3.1.1, referred by RFC 2131). |
mr_q | 0:d8f2f7d5f31b | 379 | * |
mr_q | 0:d8f2f7d5f31b | 380 | * If you want to accept private broadcast communication while a netif is down, |
mr_q | 0:d8f2f7d5f31b | 381 | * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.: |
mr_q | 0:d8f2f7d5f31b | 382 | * |
mr_q | 0:d8f2f7d5f31b | 383 | * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345)) |
mr_q | 0:d8f2f7d5f31b | 384 | */ |
mr_q | 0:d8f2f7d5f31b | 385 | if (netif == NULL) { |
mr_q | 0:d8f2f7d5f31b | 386 | /* remote port is DHCP server? */ |
mr_q | 0:d8f2f7d5f31b | 387 | if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { |
mr_q | 0:d8f2f7d5f31b | 388 | struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen); |
mr_q | 0:d8f2f7d5f31b | 389 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 390 | ntohs(udphdr->dest))); |
mr_q | 0:d8f2f7d5f31b | 391 | if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) { |
mr_q | 0:d8f2f7d5f31b | 392 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n")); |
mr_q | 0:d8f2f7d5f31b | 393 | netif = inp; |
mr_q | 0:d8f2f7d5f31b | 394 | check_ip_src = 0; |
mr_q | 0:d8f2f7d5f31b | 395 | } |
mr_q | 0:d8f2f7d5f31b | 396 | } |
mr_q | 0:d8f2f7d5f31b | 397 | } |
mr_q | 0:d8f2f7d5f31b | 398 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
mr_q | 0:d8f2f7d5f31b | 399 | |
mr_q | 0:d8f2f7d5f31b | 400 | /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */ |
mr_q | 0:d8f2f7d5f31b | 401 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING |
mr_q | 0:d8f2f7d5f31b | 402 | /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */ |
mr_q | 0:d8f2f7d5f31b | 403 | if (check_ip_src && !ip_addr_isany(¤t_iphdr_src)) |
mr_q | 0:d8f2f7d5f31b | 404 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
mr_q | 0:d8f2f7d5f31b | 405 | { if ((ip_addr_isbroadcast(¤t_iphdr_src, inp)) || |
mr_q | 0:d8f2f7d5f31b | 406 | (ip_addr_ismulticast(¤t_iphdr_src))) { |
mr_q | 0:d8f2f7d5f31b | 407 | /* packet source is not valid */ |
mr_q | 0:d8f2f7d5f31b | 408 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n")); |
mr_q | 0:d8f2f7d5f31b | 409 | /* free (drop) packet pbufs */ |
mr_q | 0:d8f2f7d5f31b | 410 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 411 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 412 | snmp_inc_ipinaddrerrors(); |
mr_q | 0:d8f2f7d5f31b | 413 | snmp_inc_ipindiscards(); |
mr_q | 0:d8f2f7d5f31b | 414 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 415 | } |
mr_q | 0:d8f2f7d5f31b | 416 | } |
mr_q | 0:d8f2f7d5f31b | 417 | |
mr_q | 0:d8f2f7d5f31b | 418 | /* packet not for us? */ |
mr_q | 0:d8f2f7d5f31b | 419 | if (netif == NULL) { |
mr_q | 0:d8f2f7d5f31b | 420 | /* packet not for us, route or discard */ |
mr_q | 0:d8f2f7d5f31b | 421 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n")); |
mr_q | 0:d8f2f7d5f31b | 422 | #if IP_FORWARD |
mr_q | 0:d8f2f7d5f31b | 423 | /* non-broadcast packet? */ |
mr_q | 0:d8f2f7d5f31b | 424 | if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp)) { |
mr_q | 0:d8f2f7d5f31b | 425 | /* try to forward IP packet on (other) interfaces */ |
mr_q | 0:d8f2f7d5f31b | 426 | ip_forward(p, iphdr, inp); |
mr_q | 0:d8f2f7d5f31b | 427 | } else |
mr_q | 0:d8f2f7d5f31b | 428 | #endif /* IP_FORWARD */ |
mr_q | 0:d8f2f7d5f31b | 429 | { |
mr_q | 0:d8f2f7d5f31b | 430 | snmp_inc_ipinaddrerrors(); |
mr_q | 0:d8f2f7d5f31b | 431 | snmp_inc_ipindiscards(); |
mr_q | 0:d8f2f7d5f31b | 432 | } |
mr_q | 0:d8f2f7d5f31b | 433 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 434 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 435 | } |
mr_q | 0:d8f2f7d5f31b | 436 | /* packet consists of multiple fragments? */ |
mr_q | 0:d8f2f7d5f31b | 437 | if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) { |
mr_q | 0:d8f2f7d5f31b | 438 | #if IP_REASSEMBLY /* packet fragment reassembly code present? */ |
mr_q | 0:d8f2f7d5f31b | 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", |
mr_q | 0:d8f2f7d5f31b | 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)); |
mr_q | 0:d8f2f7d5f31b | 441 | /* reassemble the packet*/ |
mr_q | 0:d8f2f7d5f31b | 442 | p = ip_reass(p); |
mr_q | 0:d8f2f7d5f31b | 443 | /* packet not fully reassembled yet? */ |
mr_q | 0:d8f2f7d5f31b | 444 | if (p == NULL) { |
mr_q | 0:d8f2f7d5f31b | 445 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 446 | } |
mr_q | 0:d8f2f7d5f31b | 447 | iphdr = (struct ip_hdr *)p->payload; |
mr_q | 0:d8f2f7d5f31b | 448 | #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */ |
mr_q | 0:d8f2f7d5f31b | 449 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 450 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n", |
mr_q | 0:d8f2f7d5f31b | 451 | ntohs(IPH_OFFSET(iphdr)))); |
mr_q | 0:d8f2f7d5f31b | 452 | IP_STATS_INC(ip.opterr); |
mr_q | 0:d8f2f7d5f31b | 453 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 454 | /* unsupported protocol feature */ |
mr_q | 0:d8f2f7d5f31b | 455 | snmp_inc_ipinunknownprotos(); |
mr_q | 0:d8f2f7d5f31b | 456 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 457 | #endif /* IP_REASSEMBLY */ |
mr_q | 0:d8f2f7d5f31b | 458 | } |
mr_q | 0:d8f2f7d5f31b | 459 | |
mr_q | 0:d8f2f7d5f31b | 460 | #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */ |
mr_q | 0:d8f2f7d5f31b | 461 | |
mr_q | 0:d8f2f7d5f31b | 462 | #if LWIP_IGMP |
mr_q | 0:d8f2f7d5f31b | 463 | /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */ |
mr_q | 0:d8f2f7d5f31b | 464 | if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) { |
mr_q | 0:d8f2f7d5f31b | 465 | #else |
mr_q | 0:d8f2f7d5f31b | 466 | if (iphdr_hlen > IP_HLEN) { |
mr_q | 0:d8f2f7d5f31b | 467 | #endif /* LWIP_IGMP */ |
mr_q | 0:d8f2f7d5f31b | 468 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n")); |
mr_q | 0:d8f2f7d5f31b | 469 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 470 | IP_STATS_INC(ip.opterr); |
mr_q | 0:d8f2f7d5f31b | 471 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 472 | /* unsupported protocol feature */ |
mr_q | 0:d8f2f7d5f31b | 473 | snmp_inc_ipinunknownprotos(); |
mr_q | 0:d8f2f7d5f31b | 474 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 475 | } |
mr_q | 0:d8f2f7d5f31b | 476 | #endif /* IP_OPTIONS_ALLOWED == 0 */ |
mr_q | 0:d8f2f7d5f31b | 477 | |
mr_q | 0:d8f2f7d5f31b | 478 | /* send to upper layers */ |
mr_q | 0:d8f2f7d5f31b | 479 | LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n")); |
mr_q | 0:d8f2f7d5f31b | 480 | ip_debug_print(p); |
mr_q | 0:d8f2f7d5f31b | 481 | LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len)); |
mr_q | 0:d8f2f7d5f31b | 482 | |
mr_q | 0:d8f2f7d5f31b | 483 | current_netif = inp; |
mr_q | 0:d8f2f7d5f31b | 484 | current_header = iphdr; |
mr_q | 0:d8f2f7d5f31b | 485 | |
mr_q | 0:d8f2f7d5f31b | 486 | #if LWIP_RAW |
mr_q | 0:d8f2f7d5f31b | 487 | /* raw input did not eat the packet? */ |
mr_q | 0:d8f2f7d5f31b | 488 | if (raw_input(p, inp) == 0) |
mr_q | 0:d8f2f7d5f31b | 489 | #endif /* LWIP_RAW */ |
mr_q | 0:d8f2f7d5f31b | 490 | { |
mr_q | 0:d8f2f7d5f31b | 491 | |
mr_q | 0:d8f2f7d5f31b | 492 | switch (IPH_PROTO(iphdr)) { |
mr_q | 0:d8f2f7d5f31b | 493 | #if LWIP_UDP |
mr_q | 0:d8f2f7d5f31b | 494 | case IP_PROTO_UDP: |
mr_q | 0:d8f2f7d5f31b | 495 | #if LWIP_UDPLITE |
mr_q | 0:d8f2f7d5f31b | 496 | case IP_PROTO_UDPLITE: |
mr_q | 0:d8f2f7d5f31b | 497 | #endif /* LWIP_UDPLITE */ |
mr_q | 0:d8f2f7d5f31b | 498 | snmp_inc_ipindelivers(); |
mr_q | 0:d8f2f7d5f31b | 499 | udp_input(p, inp); |
mr_q | 0:d8f2f7d5f31b | 500 | break; |
mr_q | 0:d8f2f7d5f31b | 501 | #endif /* LWIP_UDP */ |
mr_q | 0:d8f2f7d5f31b | 502 | #if LWIP_TCP |
mr_q | 0:d8f2f7d5f31b | 503 | case IP_PROTO_TCP: |
mr_q | 0:d8f2f7d5f31b | 504 | snmp_inc_ipindelivers(); |
mr_q | 0:d8f2f7d5f31b | 505 | tcp_input(p, inp); |
mr_q | 0:d8f2f7d5f31b | 506 | break; |
mr_q | 0:d8f2f7d5f31b | 507 | #endif /* LWIP_TCP */ |
mr_q | 0:d8f2f7d5f31b | 508 | #if LWIP_ICMP |
mr_q | 0:d8f2f7d5f31b | 509 | case IP_PROTO_ICMP: |
mr_q | 0:d8f2f7d5f31b | 510 | snmp_inc_ipindelivers(); |
mr_q | 0:d8f2f7d5f31b | 511 | icmp_input(p, inp); |
mr_q | 0:d8f2f7d5f31b | 512 | break; |
mr_q | 0:d8f2f7d5f31b | 513 | #endif /* LWIP_ICMP */ |
mr_q | 0:d8f2f7d5f31b | 514 | #if LWIP_IGMP |
mr_q | 0:d8f2f7d5f31b | 515 | case IP_PROTO_IGMP: |
mr_q | 0:d8f2f7d5f31b | 516 | igmp_input(p, inp, ¤t_iphdr_dest); |
mr_q | 0:d8f2f7d5f31b | 517 | break; |
mr_q | 0:d8f2f7d5f31b | 518 | #endif /* LWIP_IGMP */ |
mr_q | 0:d8f2f7d5f31b | 519 | default: |
mr_q | 0:d8f2f7d5f31b | 520 | #if LWIP_ICMP |
mr_q | 0:d8f2f7d5f31b | 521 | /* send ICMP destination protocol unreachable unless is was a broadcast */ |
mr_q | 0:d8f2f7d5f31b | 522 | if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp) && |
mr_q | 0:d8f2f7d5f31b | 523 | !ip_addr_ismulticast(¤t_iphdr_dest)) { |
mr_q | 0:d8f2f7d5f31b | 524 | p->payload = iphdr; |
mr_q | 0:d8f2f7d5f31b | 525 | icmp_dest_unreach(p, ICMP_DUR_PROTO); |
mr_q | 0:d8f2f7d5f31b | 526 | } |
mr_q | 0:d8f2f7d5f31b | 527 | #endif /* LWIP_ICMP */ |
mr_q | 0:d8f2f7d5f31b | 528 | pbuf_free(p); |
mr_q | 0:d8f2f7d5f31b | 529 | |
mr_q | 0:d8f2f7d5f31b | 530 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr))); |
mr_q | 0:d8f2f7d5f31b | 531 | |
mr_q | 0:d8f2f7d5f31b | 532 | IP_STATS_INC(ip.proterr); |
mr_q | 0:d8f2f7d5f31b | 533 | IP_STATS_INC(ip.drop); |
mr_q | 0:d8f2f7d5f31b | 534 | snmp_inc_ipinunknownprotos(); |
mr_q | 0:d8f2f7d5f31b | 535 | } |
mr_q | 0:d8f2f7d5f31b | 536 | } |
mr_q | 0:d8f2f7d5f31b | 537 | |
mr_q | 0:d8f2f7d5f31b | 538 | current_netif = NULL; |
mr_q | 0:d8f2f7d5f31b | 539 | current_header = NULL; |
mr_q | 0:d8f2f7d5f31b | 540 | ip_addr_set_any(¤t_iphdr_src); |
mr_q | 0:d8f2f7d5f31b | 541 | ip_addr_set_any(¤t_iphdr_dest); |
mr_q | 0:d8f2f7d5f31b | 542 | |
mr_q | 0:d8f2f7d5f31b | 543 | return ERR_OK; |
mr_q | 0:d8f2f7d5f31b | 544 | } |
mr_q | 0:d8f2f7d5f31b | 545 | |
mr_q | 0:d8f2f7d5f31b | 546 | /** |
mr_q | 0:d8f2f7d5f31b | 547 | * Sends an IP packet on a network interface. This function constructs |
mr_q | 0:d8f2f7d5f31b | 548 | * the IP header and calculates the IP header checksum. If the source |
mr_q | 0:d8f2f7d5f31b | 549 | * IP address is NULL, the IP address of the outgoing network |
mr_q | 0:d8f2f7d5f31b | 550 | * interface is filled in as source address. |
mr_q | 0:d8f2f7d5f31b | 551 | * If the destination IP address is IP_HDRINCL, p is assumed to already |
mr_q | 0:d8f2f7d5f31b | 552 | * include an IP header and p->payload points to it instead of the data. |
mr_q | 0:d8f2f7d5f31b | 553 | * |
mr_q | 0:d8f2f7d5f31b | 554 | * @param p the packet to send (p->payload points to the data, e.g. next |
mr_q | 0:d8f2f7d5f31b | 555 | protocol header; if dest == IP_HDRINCL, p already includes an IP |
mr_q | 0:d8f2f7d5f31b | 556 | header and p->payload points to that IP header) |
mr_q | 0:d8f2f7d5f31b | 557 | * @param src the source IP address to send from (if src == IP_ADDR_ANY, the |
mr_q | 0:d8f2f7d5f31b | 558 | * IP address of the netif used to send is used as source address) |
mr_q | 0:d8f2f7d5f31b | 559 | * @param dest the destination IP address to send the packet to |
mr_q | 0:d8f2f7d5f31b | 560 | * @param ttl the TTL value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 561 | * @param tos the TOS value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 562 | * @param proto the PROTOCOL to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 563 | * @param netif the netif on which to send this packet |
mr_q | 0:d8f2f7d5f31b | 564 | * @return ERR_OK if the packet was sent OK |
mr_q | 0:d8f2f7d5f31b | 565 | * ERR_BUF if p doesn't have enough space for IP/LINK headers |
mr_q | 0:d8f2f7d5f31b | 566 | * returns errors returned by netif->output |
mr_q | 0:d8f2f7d5f31b | 567 | * |
mr_q | 0:d8f2f7d5f31b | 568 | * @note ip_id: RFC791 "some host may be able to simply use |
mr_q | 0:d8f2f7d5f31b | 569 | * unique identifiers independent of destination" |
mr_q | 0:d8f2f7d5f31b | 570 | */ |
mr_q | 0:d8f2f7d5f31b | 571 | err_t |
mr_q | 0:d8f2f7d5f31b | 572 | ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, |
mr_q | 0:d8f2f7d5f31b | 573 | u8_t ttl, u8_t tos, |
mr_q | 0:d8f2f7d5f31b | 574 | u8_t proto, struct netif *netif) |
mr_q | 0:d8f2f7d5f31b | 575 | { |
mr_q | 0:d8f2f7d5f31b | 576 | #if IP_OPTIONS_SEND |
mr_q | 0:d8f2f7d5f31b | 577 | return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0); |
mr_q | 0:d8f2f7d5f31b | 578 | } |
mr_q | 0:d8f2f7d5f31b | 579 | |
mr_q | 0:d8f2f7d5f31b | 580 | /** |
mr_q | 0:d8f2f7d5f31b | 581 | * Same as ip_output_if() but with the possibility to include IP options: |
mr_q | 0:d8f2f7d5f31b | 582 | * |
mr_q | 0:d8f2f7d5f31b | 583 | * @ param ip_options pointer to the IP options, copied into the IP header |
mr_q | 0:d8f2f7d5f31b | 584 | * @ param optlen length of ip_options |
mr_q | 0:d8f2f7d5f31b | 585 | */ |
mr_q | 0:d8f2f7d5f31b | 586 | err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, |
mr_q | 0:d8f2f7d5f31b | 587 | u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, |
mr_q | 0:d8f2f7d5f31b | 588 | u16_t optlen) |
mr_q | 0:d8f2f7d5f31b | 589 | { |
mr_q | 0:d8f2f7d5f31b | 590 | #endif /* IP_OPTIONS_SEND */ |
mr_q | 0:d8f2f7d5f31b | 591 | struct ip_hdr *iphdr; |
mr_q | 0:d8f2f7d5f31b | 592 | ip_addr_t dest_addr; |
mr_q | 0:d8f2f7d5f31b | 593 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 594 | u32_t chk_sum = 0; |
mr_q | 0:d8f2f7d5f31b | 595 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 596 | |
mr_q | 0:d8f2f7d5f31b | 597 | /* pbufs passed to IP must have a ref-count of 1 as their payload pointer |
mr_q | 0:d8f2f7d5f31b | 598 | gets altered as the packet is passed down the stack */ |
mr_q | 0:d8f2f7d5f31b | 599 | LWIP_ASSERT("p->ref == 1", p->ref == 1); |
mr_q | 0:d8f2f7d5f31b | 600 | |
mr_q | 0:d8f2f7d5f31b | 601 | snmp_inc_ipoutrequests(); |
mr_q | 0:d8f2f7d5f31b | 602 | |
mr_q | 0:d8f2f7d5f31b | 603 | /* Should the IP header be generated or is it already included in p? */ |
mr_q | 0:d8f2f7d5f31b | 604 | if (dest != IP_HDRINCL) { |
mr_q | 0:d8f2f7d5f31b | 605 | u16_t ip_hlen = IP_HLEN; |
mr_q | 0:d8f2f7d5f31b | 606 | #if IP_OPTIONS_SEND |
mr_q | 0:d8f2f7d5f31b | 607 | u16_t optlen_aligned = 0; |
mr_q | 0:d8f2f7d5f31b | 608 | if (optlen != 0) { |
mr_q | 0:d8f2f7d5f31b | 609 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 610 | int i; |
mr_q | 0:d8f2f7d5f31b | 611 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 612 | /* round up to a multiple of 4 */ |
mr_q | 0:d8f2f7d5f31b | 613 | optlen_aligned = ((optlen + 3) & ~3); |
mr_q | 0:d8f2f7d5f31b | 614 | ip_hlen += optlen_aligned; |
mr_q | 0:d8f2f7d5f31b | 615 | /* First write in the IP options */ |
mr_q | 0:d8f2f7d5f31b | 616 | if (pbuf_header(p, optlen_aligned)) { |
mr_q | 0:d8f2f7d5f31b | 617 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n")); |
mr_q | 0:d8f2f7d5f31b | 618 | IP_STATS_INC(ip.err); |
mr_q | 0:d8f2f7d5f31b | 619 | snmp_inc_ipoutdiscards(); |
mr_q | 0:d8f2f7d5f31b | 620 | return ERR_BUF; |
mr_q | 0:d8f2f7d5f31b | 621 | } |
mr_q | 0:d8f2f7d5f31b | 622 | MEMCPY(p->payload, ip_options, optlen); |
mr_q | 0:d8f2f7d5f31b | 623 | if (optlen < optlen_aligned) { |
mr_q | 0:d8f2f7d5f31b | 624 | /* zero the remaining bytes */ |
mr_q | 0:d8f2f7d5f31b | 625 | memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen); |
mr_q | 0:d8f2f7d5f31b | 626 | } |
mr_q | 0:d8f2f7d5f31b | 627 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 628 | for (i = 0; i < optlen_aligned; i += sizeof(u16_t)) { |
mr_q | 0:d8f2f7d5f31b | 629 | chk_sum += ((u16_t*)p->payload)[i]; |
mr_q | 0:d8f2f7d5f31b | 630 | } |
mr_q | 0:d8f2f7d5f31b | 631 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 632 | } |
mr_q | 0:d8f2f7d5f31b | 633 | #endif /* IP_OPTIONS_SEND */ |
mr_q | 0:d8f2f7d5f31b | 634 | /* generate IP header */ |
mr_q | 0:d8f2f7d5f31b | 635 | if (pbuf_header(p, IP_HLEN)) { |
mr_q | 0:d8f2f7d5f31b | 636 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n")); |
mr_q | 0:d8f2f7d5f31b | 637 | |
mr_q | 0:d8f2f7d5f31b | 638 | IP_STATS_INC(ip.err); |
mr_q | 0:d8f2f7d5f31b | 639 | snmp_inc_ipoutdiscards(); |
mr_q | 0:d8f2f7d5f31b | 640 | return ERR_BUF; |
mr_q | 0:d8f2f7d5f31b | 641 | } |
mr_q | 0:d8f2f7d5f31b | 642 | |
mr_q | 0:d8f2f7d5f31b | 643 | iphdr = (struct ip_hdr *)p->payload; |
mr_q | 0:d8f2f7d5f31b | 644 | LWIP_ASSERT("check that first pbuf can hold struct ip_hdr", |
mr_q | 0:d8f2f7d5f31b | 645 | (p->len >= sizeof(struct ip_hdr))); |
mr_q | 0:d8f2f7d5f31b | 646 | |
mr_q | 0:d8f2f7d5f31b | 647 | IPH_TTL_SET(iphdr, ttl); |
mr_q | 0:d8f2f7d5f31b | 648 | IPH_PROTO_SET(iphdr, proto); |
mr_q | 0:d8f2f7d5f31b | 649 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 650 | chk_sum += LWIP_MAKE_U16(proto, ttl); |
mr_q | 0:d8f2f7d5f31b | 651 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 652 | |
mr_q | 0:d8f2f7d5f31b | 653 | /* dest cannot be NULL here */ |
mr_q | 0:d8f2f7d5f31b | 654 | ip_addr_copy(iphdr->dest, *dest); |
mr_q | 0:d8f2f7d5f31b | 655 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 656 | chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF; |
mr_q | 0:d8f2f7d5f31b | 657 | chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16; |
mr_q | 0:d8f2f7d5f31b | 658 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 659 | |
mr_q | 0:d8f2f7d5f31b | 660 | IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos); |
mr_q | 0:d8f2f7d5f31b | 661 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 662 | chk_sum += iphdr->_v_hl_tos; |
mr_q | 0:d8f2f7d5f31b | 663 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 664 | IPH_LEN_SET(iphdr, htons(p->tot_len)); |
mr_q | 0:d8f2f7d5f31b | 665 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 666 | chk_sum += iphdr->_len; |
mr_q | 0:d8f2f7d5f31b | 667 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 668 | IPH_OFFSET_SET(iphdr, 0); |
mr_q | 0:d8f2f7d5f31b | 669 | IPH_ID_SET(iphdr, htons(ip_id)); |
mr_q | 0:d8f2f7d5f31b | 670 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 671 | chk_sum += iphdr->_id; |
mr_q | 0:d8f2f7d5f31b | 672 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 673 | ++ip_id; |
mr_q | 0:d8f2f7d5f31b | 674 | |
mr_q | 0:d8f2f7d5f31b | 675 | if (ip_addr_isany(src)) { |
mr_q | 0:d8f2f7d5f31b | 676 | ip_addr_copy(iphdr->src, netif->ip_addr); |
mr_q | 0:d8f2f7d5f31b | 677 | } else { |
mr_q | 0:d8f2f7d5f31b | 678 | /* src cannot be NULL here */ |
mr_q | 0:d8f2f7d5f31b | 679 | ip_addr_copy(iphdr->src, *src); |
mr_q | 0:d8f2f7d5f31b | 680 | } |
mr_q | 0:d8f2f7d5f31b | 681 | |
mr_q | 0:d8f2f7d5f31b | 682 | #if CHECKSUM_GEN_IP_INLINE |
mr_q | 0:d8f2f7d5f31b | 683 | chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF; |
mr_q | 0:d8f2f7d5f31b | 684 | chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16; |
mr_q | 0:d8f2f7d5f31b | 685 | chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF); |
mr_q | 0:d8f2f7d5f31b | 686 | chk_sum = (chk_sum >> 16) + chk_sum; |
mr_q | 0:d8f2f7d5f31b | 687 | chk_sum = ~chk_sum; |
mr_q | 0:d8f2f7d5f31b | 688 | iphdr->_chksum = chk_sum; /* network order */ |
mr_q | 0:d8f2f7d5f31b | 689 | #else /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 690 | IPH_CHKSUM_SET(iphdr, 0); |
mr_q | 0:d8f2f7d5f31b | 691 | #if CHECKSUM_GEN_IP |
mr_q | 0:d8f2f7d5f31b | 692 | IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); |
mr_q | 0:d8f2f7d5f31b | 693 | #endif |
mr_q | 0:d8f2f7d5f31b | 694 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
mr_q | 0:d8f2f7d5f31b | 695 | } else { |
mr_q | 0:d8f2f7d5f31b | 696 | /* IP header already included in p */ |
mr_q | 0:d8f2f7d5f31b | 697 | iphdr = (struct ip_hdr *)p->payload; |
mr_q | 0:d8f2f7d5f31b | 698 | ip_addr_copy(dest_addr, iphdr->dest); |
mr_q | 0:d8f2f7d5f31b | 699 | dest = &dest_addr; |
mr_q | 0:d8f2f7d5f31b | 700 | } |
mr_q | 0:d8f2f7d5f31b | 701 | |
mr_q | 0:d8f2f7d5f31b | 702 | IP_STATS_INC(ip.xmit); |
mr_q | 0:d8f2f7d5f31b | 703 | |
mr_q | 0:d8f2f7d5f31b | 704 | LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num)); |
mr_q | 0:d8f2f7d5f31b | 705 | ip_debug_print(p); |
mr_q | 0:d8f2f7d5f31b | 706 | |
mr_q | 0:d8f2f7d5f31b | 707 | #if ENABLE_LOOPBACK |
mr_q | 0:d8f2f7d5f31b | 708 | if (ip_addr_cmp(dest, &netif->ip_addr)) { |
mr_q | 0:d8f2f7d5f31b | 709 | /* Packet to self, enqueue it for loopback */ |
mr_q | 0:d8f2f7d5f31b | 710 | LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()")); |
mr_q | 0:d8f2f7d5f31b | 711 | return netif_loop_output(netif, p, dest); |
mr_q | 0:d8f2f7d5f31b | 712 | } |
mr_q | 0:d8f2f7d5f31b | 713 | #if LWIP_IGMP |
mr_q | 0:d8f2f7d5f31b | 714 | if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) { |
mr_q | 0:d8f2f7d5f31b | 715 | netif_loop_output(netif, p, dest); |
mr_q | 0:d8f2f7d5f31b | 716 | } |
mr_q | 0:d8f2f7d5f31b | 717 | #endif /* LWIP_IGMP */ |
mr_q | 0:d8f2f7d5f31b | 718 | #endif /* ENABLE_LOOPBACK */ |
mr_q | 0:d8f2f7d5f31b | 719 | #if IP_FRAG |
mr_q | 0:d8f2f7d5f31b | 720 | /* don't fragment if interface has mtu set to 0 [loopif] */ |
mr_q | 0:d8f2f7d5f31b | 721 | if (netif->mtu && (p->tot_len > netif->mtu)) { |
mr_q | 0:d8f2f7d5f31b | 722 | return ip_frag(p, netif, dest); |
mr_q | 0:d8f2f7d5f31b | 723 | } |
mr_q | 0:d8f2f7d5f31b | 724 | #endif /* IP_FRAG */ |
mr_q | 0:d8f2f7d5f31b | 725 | |
mr_q | 0:d8f2f7d5f31b | 726 | LWIP_DEBUGF(IP_DEBUG, ("netif->output()")); |
mr_q | 0:d8f2f7d5f31b | 727 | return netif->output(netif, p, dest); |
mr_q | 0:d8f2f7d5f31b | 728 | } |
mr_q | 0:d8f2f7d5f31b | 729 | |
mr_q | 0:d8f2f7d5f31b | 730 | /** |
mr_q | 0:d8f2f7d5f31b | 731 | * Simple interface to ip_output_if. It finds the outgoing network |
mr_q | 0:d8f2f7d5f31b | 732 | * interface and calls upon ip_output_if to do the actual work. |
mr_q | 0:d8f2f7d5f31b | 733 | * |
mr_q | 0:d8f2f7d5f31b | 734 | * @param p the packet to send (p->payload points to the data, e.g. next |
mr_q | 0:d8f2f7d5f31b | 735 | protocol header; if dest == IP_HDRINCL, p already includes an IP |
mr_q | 0:d8f2f7d5f31b | 736 | header and p->payload points to that IP header) |
mr_q | 0:d8f2f7d5f31b | 737 | * @param src the source IP address to send from (if src == IP_ADDR_ANY, the |
mr_q | 0:d8f2f7d5f31b | 738 | * IP address of the netif used to send is used as source address) |
mr_q | 0:d8f2f7d5f31b | 739 | * @param dest the destination IP address to send the packet to |
mr_q | 0:d8f2f7d5f31b | 740 | * @param ttl the TTL value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 741 | * @param tos the TOS value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 742 | * @param proto the PROTOCOL to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 743 | * |
mr_q | 0:d8f2f7d5f31b | 744 | * @return ERR_RTE if no route is found |
mr_q | 0:d8f2f7d5f31b | 745 | * see ip_output_if() for more return values |
mr_q | 0:d8f2f7d5f31b | 746 | */ |
mr_q | 0:d8f2f7d5f31b | 747 | err_t |
mr_q | 0:d8f2f7d5f31b | 748 | ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, |
mr_q | 0:d8f2f7d5f31b | 749 | u8_t ttl, u8_t tos, u8_t proto) |
mr_q | 0:d8f2f7d5f31b | 750 | { |
mr_q | 0:d8f2f7d5f31b | 751 | struct netif *netif; |
mr_q | 0:d8f2f7d5f31b | 752 | |
mr_q | 0:d8f2f7d5f31b | 753 | /* pbufs passed to IP must have a ref-count of 1 as their payload pointer |
mr_q | 0:d8f2f7d5f31b | 754 | gets altered as the packet is passed down the stack */ |
mr_q | 0:d8f2f7d5f31b | 755 | LWIP_ASSERT("p->ref == 1", p->ref == 1); |
mr_q | 0:d8f2f7d5f31b | 756 | |
mr_q | 0:d8f2f7d5f31b | 757 | if ((netif = ip_route(dest)) == NULL) { |
mr_q | 0:d8f2f7d5f31b | 758 | LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 759 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
mr_q | 0:d8f2f7d5f31b | 760 | IP_STATS_INC(ip.rterr); |
mr_q | 0:d8f2f7d5f31b | 761 | return ERR_RTE; |
mr_q | 0:d8f2f7d5f31b | 762 | } |
mr_q | 0:d8f2f7d5f31b | 763 | |
mr_q | 0:d8f2f7d5f31b | 764 | return ip_output_if(p, src, dest, ttl, tos, proto, netif); |
mr_q | 0:d8f2f7d5f31b | 765 | } |
mr_q | 0:d8f2f7d5f31b | 766 | |
mr_q | 0:d8f2f7d5f31b | 767 | #if LWIP_NETIF_HWADDRHINT |
mr_q | 0:d8f2f7d5f31b | 768 | /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint |
mr_q | 0:d8f2f7d5f31b | 769 | * before calling ip_output_if. |
mr_q | 0:d8f2f7d5f31b | 770 | * |
mr_q | 0:d8f2f7d5f31b | 771 | * @param p the packet to send (p->payload points to the data, e.g. next |
mr_q | 0:d8f2f7d5f31b | 772 | protocol header; if dest == IP_HDRINCL, p already includes an IP |
mr_q | 0:d8f2f7d5f31b | 773 | header and p->payload points to that IP header) |
mr_q | 0:d8f2f7d5f31b | 774 | * @param src the source IP address to send from (if src == IP_ADDR_ANY, the |
mr_q | 0:d8f2f7d5f31b | 775 | * IP address of the netif used to send is used as source address) |
mr_q | 0:d8f2f7d5f31b | 776 | * @param dest the destination IP address to send the packet to |
mr_q | 0:d8f2f7d5f31b | 777 | * @param ttl the TTL value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 778 | * @param tos the TOS value to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 779 | * @param proto the PROTOCOL to be set in the IP header |
mr_q | 0:d8f2f7d5f31b | 780 | * @param addr_hint address hint pointer set to netif->addr_hint before |
mr_q | 0:d8f2f7d5f31b | 781 | * calling ip_output_if() |
mr_q | 0:d8f2f7d5f31b | 782 | * |
mr_q | 0:d8f2f7d5f31b | 783 | * @return ERR_RTE if no route is found |
mr_q | 0:d8f2f7d5f31b | 784 | * see ip_output_if() for more return values |
mr_q | 0:d8f2f7d5f31b | 785 | */ |
mr_q | 0:d8f2f7d5f31b | 786 | err_t |
mr_q | 0:d8f2f7d5f31b | 787 | ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, |
mr_q | 0:d8f2f7d5f31b | 788 | u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint) |
mr_q | 0:d8f2f7d5f31b | 789 | { |
mr_q | 0:d8f2f7d5f31b | 790 | struct netif *netif; |
mr_q | 0:d8f2f7d5f31b | 791 | err_t err; |
mr_q | 0:d8f2f7d5f31b | 792 | |
mr_q | 0:d8f2f7d5f31b | 793 | /* pbufs passed to IP must have a ref-count of 1 as their payload pointer |
mr_q | 0:d8f2f7d5f31b | 794 | gets altered as the packet is passed down the stack */ |
mr_q | 0:d8f2f7d5f31b | 795 | LWIP_ASSERT("p->ref == 1", p->ref == 1); |
mr_q | 0:d8f2f7d5f31b | 796 | |
mr_q | 0:d8f2f7d5f31b | 797 | if ((netif = ip_route(dest)) == NULL) { |
mr_q | 0:d8f2f7d5f31b | 798 | LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
mr_q | 0:d8f2f7d5f31b | 799 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
mr_q | 0:d8f2f7d5f31b | 800 | IP_STATS_INC(ip.rterr); |
mr_q | 0:d8f2f7d5f31b | 801 | return ERR_RTE; |
mr_q | 0:d8f2f7d5f31b | 802 | } |
mr_q | 0:d8f2f7d5f31b | 803 | |
mr_q | 0:d8f2f7d5f31b | 804 | netif->addr_hint = addr_hint; |
mr_q | 0:d8f2f7d5f31b | 805 | err = ip_output_if(p, src, dest, ttl, tos, proto, netif); |
mr_q | 0:d8f2f7d5f31b | 806 | netif->addr_hint = NULL; |
mr_q | 0:d8f2f7d5f31b | 807 | |
mr_q | 0:d8f2f7d5f31b | 808 | return err; |
mr_q | 0:d8f2f7d5f31b | 809 | } |
mr_q | 0:d8f2f7d5f31b | 810 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
mr_q | 0:d8f2f7d5f31b | 811 | |
mr_q | 0:d8f2f7d5f31b | 812 | #if IP_DEBUG |
mr_q | 0:d8f2f7d5f31b | 813 | /* Print an IP header by using LWIP_DEBUGF |
mr_q | 0:d8f2f7d5f31b | 814 | * @param p an IP packet, p->payload pointing to the IP header |
mr_q | 0:d8f2f7d5f31b | 815 | */ |
mr_q | 0:d8f2f7d5f31b | 816 | void |
mr_q | 0:d8f2f7d5f31b | 817 | ip_debug_print(struct pbuf *p) |
mr_q | 0:d8f2f7d5f31b | 818 | { |
mr_q | 0:d8f2f7d5f31b | 819 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; |
mr_q | 0:d8f2f7d5f31b | 820 | u8_t *payload; |
mr_q | 0:d8f2f7d5f31b | 821 | |
mr_q | 0:d8f2f7d5f31b | 822 | payload = (u8_t *)iphdr + IP_HLEN; |
mr_q | 0:d8f2f7d5f31b | 823 | |
mr_q | 0:d8f2f7d5f31b | 824 | LWIP_DEBUGF(IP_DEBUG, ("IP header:\n")); |
mr_q | 0:d8f2f7d5f31b | 825 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 826 | LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n", |
mr_q | 0:d8f2f7d5f31b | 827 | IPH_V(iphdr), |
mr_q | 0:d8f2f7d5f31b | 828 | IPH_HL(iphdr), |
mr_q | 0:d8f2f7d5f31b | 829 | IPH_TOS(iphdr), |
mr_q | 0:d8f2f7d5f31b | 830 | ntohs(IPH_LEN(iphdr)))); |
mr_q | 0:d8f2f7d5f31b | 831 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 832 | LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n", |
mr_q | 0:d8f2f7d5f31b | 833 | ntohs(IPH_ID(iphdr)), |
mr_q | 0:d8f2f7d5f31b | 834 | ntohs(IPH_OFFSET(iphdr)) >> 15 & 1, |
mr_q | 0:d8f2f7d5f31b | 835 | ntohs(IPH_OFFSET(iphdr)) >> 14 & 1, |
mr_q | 0:d8f2f7d5f31b | 836 | ntohs(IPH_OFFSET(iphdr)) >> 13 & 1, |
mr_q | 0:d8f2f7d5f31b | 837 | ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)); |
mr_q | 0:d8f2f7d5f31b | 838 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 839 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n", |
mr_q | 0:d8f2f7d5f31b | 840 | IPH_TTL(iphdr), |
mr_q | 0:d8f2f7d5f31b | 841 | IPH_PROTO(iphdr), |
mr_q | 0:d8f2f7d5f31b | 842 | ntohs(IPH_CHKSUM(iphdr)))); |
mr_q | 0:d8f2f7d5f31b | 843 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 844 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n", |
mr_q | 0:d8f2f7d5f31b | 845 | ip4_addr1_16(&iphdr->src), |
mr_q | 0:d8f2f7d5f31b | 846 | ip4_addr2_16(&iphdr->src), |
mr_q | 0:d8f2f7d5f31b | 847 | ip4_addr3_16(&iphdr->src), |
mr_q | 0:d8f2f7d5f31b | 848 | ip4_addr4_16(&iphdr->src))); |
mr_q | 0:d8f2f7d5f31b | 849 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 850 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n", |
mr_q | 0:d8f2f7d5f31b | 851 | ip4_addr1_16(&iphdr->dest), |
mr_q | 0:d8f2f7d5f31b | 852 | ip4_addr2_16(&iphdr->dest), |
mr_q | 0:d8f2f7d5f31b | 853 | ip4_addr3_16(&iphdr->dest), |
mr_q | 0:d8f2f7d5f31b | 854 | ip4_addr4_16(&iphdr->dest))); |
mr_q | 0:d8f2f7d5f31b | 855 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
mr_q | 0:d8f2f7d5f31b | 856 | } |
mr_q | 0:d8f2f7d5f31b | 857 | #endif /* IP_DEBUG */ |