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