Dirk-Willem van Gulik (NXP/mbed) / Mbed 2 deprecated Bonjour

Dependencies:   mbed

Committer:
dirkx
Date:
Wed Jul 21 19:25:56 2010 +0000
Revision:
0:355018f44c9f
Child:
2:816cbd922d3e

        

Who changed what in which revision?

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