Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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