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