I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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