ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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