Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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