Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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