Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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