Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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