Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Jul 24 20:59:52 2010 +0000
Revision:
2:816cbd922d3e
Parent:
0:355018f44c9f

        

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 2:816cbd922d3e 298 if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))
dirkx 0:355018f44c9f 299 /* or broadcast on this interface network address? */
dirkx 2:816cbd922d3e 300 || ip_addr_isbroadcast(&(iphdr->dest), netif)
dirkx 2:816cbd922d3e 301 #if 0
dirkx 0:355018f44c9f 302 /* or is this to our multicast interface? DIRKX */
dirkx 2:816cbd922d3e 303 || ip_addr_ismulticast(&(iphdr->dest))
dirkx 2:816cbd922d3e 304 #endif
dirkx 0:355018f44c9f 305 ) {
dirkx 0:355018f44c9f 306 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
dirkx 0:355018f44c9f 307 netif->name[0], netif->name[1]));
dirkx 0:355018f44c9f 308 /* break out of for loop */
dirkx 0:355018f44c9f 309 break;
dirkx 0:355018f44c9f 310 }
dirkx 0:355018f44c9f 311 #if LWIP_AUTOIP
dirkx 0:355018f44c9f 312 /* connections to link-local addresses must persist after changing
dirkx 0:355018f44c9f 313 the netif's address (RFC3927 ch. 1.9) */
dirkx 0:355018f44c9f 314 if ((netif->autoip != NULL) &&
dirkx 0:355018f44c9f 315 ip_addr_cmp(&(iphdr->dest), &(netif->autoip->llipaddr))) {
dirkx 0:355018f44c9f 316 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
dirkx 0:355018f44c9f 317 netif->name[0], netif->name[1]));
dirkx 0:355018f44c9f 318 /* break out of for loop */
dirkx 0:355018f44c9f 319 break;
dirkx 0:355018f44c9f 320 }
dirkx 0:355018f44c9f 321 #endif /* LWIP_AUTOIP */
dirkx 0:355018f44c9f 322 }
dirkx 0:355018f44c9f 323 if (first) {
dirkx 0:355018f44c9f 324 first = 0;
dirkx 0:355018f44c9f 325 netif = netif_list;
dirkx 0:355018f44c9f 326 } else {
dirkx 0:355018f44c9f 327 netif = netif->next;
dirkx 0:355018f44c9f 328 }
dirkx 0:355018f44c9f 329 if (netif == inp) {
dirkx 0:355018f44c9f 330 netif = netif->next;
dirkx 0:355018f44c9f 331 }
dirkx 0:355018f44c9f 332 } while(netif != NULL);
dirkx 0:355018f44c9f 333 }
dirkx 0:355018f44c9f 334
dirkx 0:355018f44c9f 335 #if LWIP_DHCP
dirkx 0:355018f44c9f 336 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
dirkx 0:355018f44c9f 337 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
dirkx 0:355018f44c9f 338 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
dirkx 0:355018f44c9f 339 */
dirkx 0:355018f44c9f 340 if (netif == NULL) {
dirkx 0:355018f44c9f 341 /* remote port is DHCP server? */
dirkx 0:355018f44c9f 342 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
dirkx 0:355018f44c9f 343 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
dirkx 0:355018f44c9f 344 ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
dirkx 0:355018f44c9f 345 if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {
dirkx 0:355018f44c9f 346 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
dirkx 0:355018f44c9f 347 netif = inp;
dirkx 0:355018f44c9f 348 check_ip_src = 0;
dirkx 0:355018f44c9f 349 }
dirkx 0:355018f44c9f 350 }
dirkx 0:355018f44c9f 351 }
dirkx 0:355018f44c9f 352 #endif /* LWIP_DHCP */
dirkx 0:355018f44c9f 353
dirkx 0:355018f44c9f 354 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
dirkx 0:355018f44c9f 355 #if LWIP_DHCP
dirkx 0:355018f44c9f 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:355018f44c9f 357 if (check_ip_src && !ip_addr_isany(&iphdr->src))
dirkx 0:355018f44c9f 358 #endif /* LWIP_DHCP */
dirkx 0:355018f44c9f 359 { if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
dirkx 0:355018f44c9f 360 (ip_addr_ismulticast(&(iphdr->src)))) {
dirkx 0:355018f44c9f 361 /* packet source is not valid */
dirkx 0:355018f44c9f 362 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
dirkx 0:355018f44c9f 363 /* free (drop) packet pbufs */
dirkx 0:355018f44c9f 364 pbuf_free(p);
dirkx 0:355018f44c9f 365 IP_STATS_INC(ip.drop);
dirkx 0:355018f44c9f 366 snmp_inc_ipinaddrerrors();
dirkx 0:355018f44c9f 367 snmp_inc_ipindiscards();
dirkx 0:355018f44c9f 368 return ERR_OK;
dirkx 0:355018f44c9f 369 }
dirkx 0:355018f44c9f 370 }
dirkx 0:355018f44c9f 371
dirkx 0:355018f44c9f 372 /* packet not for us? */
dirkx 0:355018f44c9f 373 if (netif == NULL) {
dirkx 0:355018f44c9f 374 /* packet not for us, route or discard */
dirkx 0:355018f44c9f 375 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
dirkx 0:355018f44c9f 376 #if IP_FORWARD
dirkx 0:355018f44c9f 377 /* non-broadcast packet? */
dirkx 0:355018f44c9f 378 if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
dirkx 0:355018f44c9f 379 /* try to forward IP packet on (other) interfaces */
dirkx 0:355018f44c9f 380 ip_forward(p, iphdr, inp);
dirkx 0:355018f44c9f 381 } else
dirkx 0:355018f44c9f 382 #endif /* IP_FORWARD */
dirkx 0:355018f44c9f 383 {
dirkx 0:355018f44c9f 384 snmp_inc_ipinaddrerrors();
dirkx 0:355018f44c9f 385 snmp_inc_ipindiscards();
dirkx 0:355018f44c9f 386 }
dirkx 0:355018f44c9f 387 pbuf_free(p);
dirkx 0:355018f44c9f 388 return ERR_OK;
dirkx 0:355018f44c9f 389 }
dirkx 0:355018f44c9f 390 /* packet consists of multiple fragments? */
dirkx 0:355018f44c9f 391 if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
dirkx 0:355018f44c9f 392 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
dirkx 0:355018f44c9f 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:355018f44c9f 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:355018f44c9f 395 /* reassemble the packet*/
dirkx 0:355018f44c9f 396 p = ip_reass(p);
dirkx 0:355018f44c9f 397 /* packet not fully reassembled yet? */
dirkx 0:355018f44c9f 398 if (p == NULL) {
dirkx 0:355018f44c9f 399 return ERR_OK;
dirkx 0:355018f44c9f 400 }
dirkx 0:355018f44c9f 401 iphdr = (struct ip_hdr *)p->payload;
dirkx 0:355018f44c9f 402 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
dirkx 0:355018f44c9f 403 pbuf_free(p);
dirkx 0:355018f44c9f 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:355018f44c9f 405 ntohs(IPH_OFFSET(iphdr))));
dirkx 0:355018f44c9f 406 IP_STATS_INC(ip.opterr);
dirkx 0:355018f44c9f 407 IP_STATS_INC(ip.drop);
dirkx 0:355018f44c9f 408 /* unsupported protocol feature */
dirkx 0:355018f44c9f 409 snmp_inc_ipinunknownprotos();
dirkx 0:355018f44c9f 410 return ERR_OK;
dirkx 0:355018f44c9f 411 #endif /* IP_REASSEMBLY */
dirkx 0:355018f44c9f 412 }
dirkx 0:355018f44c9f 413
dirkx 0:355018f44c9f 414 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
dirkx 0:355018f44c9f 415
dirkx 0:355018f44c9f 416 #if LWIP_IGMP
dirkx 0:355018f44c9f 417 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
dirkx 0:355018f44c9f 418 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
dirkx 0:355018f44c9f 419 #else
dirkx 0:355018f44c9f 420 if (iphdr_hlen > IP_HLEN) {
dirkx 0:355018f44c9f 421 #endif /* LWIP_IGMP */
dirkx 0:355018f44c9f 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:355018f44c9f 423 pbuf_free(p);
dirkx 0:355018f44c9f 424 IP_STATS_INC(ip.opterr);
dirkx 0:355018f44c9f 425 IP_STATS_INC(ip.drop);
dirkx 0:355018f44c9f 426 /* unsupported protocol feature */
dirkx 0:355018f44c9f 427 snmp_inc_ipinunknownprotos();
dirkx 0:355018f44c9f 428 return ERR_OK;
dirkx 0:355018f44c9f 429 }
dirkx 0:355018f44c9f 430 #endif /* IP_OPTIONS_ALLOWED == 0 */
dirkx 0:355018f44c9f 431
dirkx 0:355018f44c9f 432 /* send to upper layers */
dirkx 0:355018f44c9f 433 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
dirkx 0:355018f44c9f 434 ip_debug_print(p);
dirkx 0:355018f44c9f 435 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
dirkx 0:355018f44c9f 436
dirkx 0:355018f44c9f 437 current_netif = inp;
dirkx 0:355018f44c9f 438 current_header = iphdr;
dirkx 0:355018f44c9f 439
dirkx 0:355018f44c9f 440 #if LWIP_RAW
dirkx 0:355018f44c9f 441 /* raw input did not eat the packet? */
dirkx 0:355018f44c9f 442 if (raw_input(p, inp) == 0)
dirkx 0:355018f44c9f 443 #endif /* LWIP_RAW */
dirkx 0:355018f44c9f 444 {
dirkx 0:355018f44c9f 445
dirkx 0:355018f44c9f 446 switch (IPH_PROTO(iphdr)) {
dirkx 0:355018f44c9f 447 #if LWIP_UDP
dirkx 0:355018f44c9f 448 case IP_PROTO_UDP:
dirkx 0:355018f44c9f 449 #if LWIP_UDPLITE
dirkx 0:355018f44c9f 450 case IP_PROTO_UDPLITE:
dirkx 0:355018f44c9f 451 #endif /* LWIP_UDPLITE */
dirkx 0:355018f44c9f 452 snmp_inc_ipindelivers();
dirkx 0:355018f44c9f 453 udp_input(p, inp);
dirkx 0:355018f44c9f 454 break;
dirkx 0:355018f44c9f 455 #endif /* LWIP_UDP */
dirkx 0:355018f44c9f 456 #if LWIP_TCP
dirkx 0:355018f44c9f 457 case IP_PROTO_TCP:
dirkx 0:355018f44c9f 458 snmp_inc_ipindelivers();
dirkx 0:355018f44c9f 459 tcp_input(p, inp);
dirkx 0:355018f44c9f 460 break;
dirkx 0:355018f44c9f 461 #endif /* LWIP_TCP */
dirkx 0:355018f44c9f 462 #if LWIP_ICMP
dirkx 0:355018f44c9f 463 case IP_PROTO_ICMP:
dirkx 0:355018f44c9f 464 snmp_inc_ipindelivers();
dirkx 0:355018f44c9f 465 icmp_input(p, inp);
dirkx 0:355018f44c9f 466 break;
dirkx 0:355018f44c9f 467 #endif /* LWIP_ICMP */
dirkx 0:355018f44c9f 468 #if LWIP_IGMP
dirkx 0:355018f44c9f 469 case IP_PROTO_IGMP:
dirkx 0:355018f44c9f 470 igmp_input(p,inp,&(iphdr->dest));
dirkx 0:355018f44c9f 471 break;
dirkx 0:355018f44c9f 472 #endif /* LWIP_IGMP */
dirkx 0:355018f44c9f 473 default:
dirkx 0:355018f44c9f 474 #if LWIP_ICMP
dirkx 0:355018f44c9f 475 /* send ICMP destination protocol unreachable unless is was a broadcast */
dirkx 0:355018f44c9f 476 if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
dirkx 0:355018f44c9f 477 !ip_addr_ismulticast(&(iphdr->dest))) {
dirkx 0:355018f44c9f 478 p->payload = iphdr;
dirkx 0:355018f44c9f 479 icmp_dest_unreach(p, ICMP_DUR_PROTO);
dirkx 0:355018f44c9f 480 }
dirkx 0:355018f44c9f 481 #endif /* LWIP_ICMP */
dirkx 0:355018f44c9f 482 pbuf_free(p);
dirkx 0:355018f44c9f 483
dirkx 0:355018f44c9f 484 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
dirkx 0:355018f44c9f 485
dirkx 0:355018f44c9f 486 IP_STATS_INC(ip.proterr);
dirkx 0:355018f44c9f 487 IP_STATS_INC(ip.drop);
dirkx 0:355018f44c9f 488 snmp_inc_ipinunknownprotos();
dirkx 0:355018f44c9f 489 }
dirkx 0:355018f44c9f 490 }
dirkx 0:355018f44c9f 491
dirkx 0:355018f44c9f 492 current_netif = NULL;
dirkx 0:355018f44c9f 493 current_header = NULL;
dirkx 0:355018f44c9f 494
dirkx 0:355018f44c9f 495 return ERR_OK;
dirkx 0:355018f44c9f 496 }
dirkx 0:355018f44c9f 497
dirkx 0:355018f44c9f 498 /**
dirkx 0:355018f44c9f 499 * Sends an IP packet on a network interface. This function constructs
dirkx 0:355018f44c9f 500 * the IP header and calculates the IP header checksum. If the source
dirkx 0:355018f44c9f 501 * IP address is NULL, the IP address of the outgoing network
dirkx 0:355018f44c9f 502 * interface is filled in as source address.
dirkx 0:355018f44c9f 503 * If the destination IP address is IP_HDRINCL, p is assumed to already
dirkx 0:355018f44c9f 504 * include an IP header and p->payload points to it instead of the data.
dirkx 0:355018f44c9f 505 *
dirkx 0:355018f44c9f 506 * @param p the packet to send (p->payload points to the data, e.g. next
dirkx 0:355018f44c9f 507 protocol header; if dest == IP_HDRINCL, p already includes an IP
dirkx 0:355018f44c9f 508 header and p->payload points to that IP header)
dirkx 0:355018f44c9f 509 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
dirkx 0:355018f44c9f 510 * IP address of the netif used to send is used as source address)
dirkx 0:355018f44c9f 511 * @param dest the destination IP address to send the packet to
dirkx 0:355018f44c9f 512 * @param ttl the TTL value to be set in the IP header
dirkx 0:355018f44c9f 513 * @param tos the TOS value to be set in the IP header
dirkx 0:355018f44c9f 514 * @param proto the PROTOCOL to be set in the IP header
dirkx 0:355018f44c9f 515 * @param netif the netif on which to send this packet
dirkx 0:355018f44c9f 516 * @return ERR_OK if the packet was sent OK
dirkx 0:355018f44c9f 517 * ERR_BUF if p doesn't have enough space for IP/LINK headers
dirkx 0:355018f44c9f 518 * returns errors returned by netif->output
dirkx 0:355018f44c9f 519 *
dirkx 0:355018f44c9f 520 * @note ip_id: RFC791 "some host may be able to simply use
dirkx 0:355018f44c9f 521 * unique identifiers independent of destination"
dirkx 0:355018f44c9f 522 */
dirkx 0:355018f44c9f 523 err_t
dirkx 0:355018f44c9f 524 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
dirkx 0:355018f44c9f 525 u8_t ttl, u8_t tos,
dirkx 0:355018f44c9f 526 u8_t proto, struct netif *netif)
dirkx 0:355018f44c9f 527 {
dirkx 0:355018f44c9f 528 #if IP_OPTIONS_SEND
dirkx 0:355018f44c9f 529 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
dirkx 0:355018f44c9f 530 }
dirkx 0:355018f44c9f 531
dirkx 0:355018f44c9f 532 /**
dirkx 0:355018f44c9f 533 * Same as ip_output_if() but with the possibility to include IP options:
dirkx 0:355018f44c9f 534 *
dirkx 0:355018f44c9f 535 * @ param ip_options pointer to the IP options, copied into the IP header
dirkx 0:355018f44c9f 536 * @ param optlen length of ip_options
dirkx 0:355018f44c9f 537 */
dirkx 0:355018f44c9f 538 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
dirkx 0:355018f44c9f 539 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
dirkx 0:355018f44c9f 540 u16_t optlen)
dirkx 0:355018f44c9f 541 {
dirkx 0:355018f44c9f 542 #endif /* IP_OPTIONS_SEND */
dirkx 0:355018f44c9f 543 struct ip_hdr *iphdr;
dirkx 0:355018f44c9f 544 static u16_t ip_id = 0;
dirkx 0:355018f44c9f 545
dirkx 0:355018f44c9f 546 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
dirkx 0:355018f44c9f 547 gets altered as the packet is passed down the stack */
dirkx 0:355018f44c9f 548 LWIP_ASSERT("p->ref == 1", p->ref == 1);
dirkx 0:355018f44c9f 549
dirkx 0:355018f44c9f 550 snmp_inc_ipoutrequests();
dirkx 0:355018f44c9f 551
dirkx 0:355018f44c9f 552 /* Should the IP header be generated or is it already included in p? */
dirkx 0:355018f44c9f 553 if (dest != IP_HDRINCL) {
dirkx 0:355018f44c9f 554 u16_t ip_hlen = IP_HLEN;
dirkx 0:355018f44c9f 555 #if IP_OPTIONS_SEND
dirkx 0:355018f44c9f 556 u16_t optlen_aligned = 0;
dirkx 0:355018f44c9f 557 if (optlen != 0) {
dirkx 0:355018f44c9f 558 /* round up to a multiple of 4 */
dirkx 0:355018f44c9f 559 optlen_aligned = ((optlen + 3) & ~3);
dirkx 0:355018f44c9f 560 ip_hlen += optlen_aligned;
dirkx 0:355018f44c9f 561 /* First write in the IP options */
dirkx 0:355018f44c9f 562 if (pbuf_header(p, optlen_aligned)) {
dirkx 0:355018f44c9f 563 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
dirkx 0:355018f44c9f 564 IP_STATS_INC(ip.err);
dirkx 0:355018f44c9f 565 snmp_inc_ipoutdiscards();
dirkx 0:355018f44c9f 566 return ERR_BUF;
dirkx 0:355018f44c9f 567 }
dirkx 0:355018f44c9f 568 MEMCPY(p->payload, ip_options, optlen);
dirkx 0:355018f44c9f 569 if (optlen < optlen_aligned) {
dirkx 0:355018f44c9f 570 /* zero the remaining bytes */
dirkx 0:355018f44c9f 571 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
dirkx 0:355018f44c9f 572 }
dirkx 0:355018f44c9f 573 }
dirkx 0:355018f44c9f 574 #endif /* IP_OPTIONS_SEND */
dirkx 0:355018f44c9f 575 /* generate IP header */
dirkx 0:355018f44c9f 576 if (pbuf_header(p, IP_HLEN)) {
dirkx 0:355018f44c9f 577 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
dirkx 0:355018f44c9f 578
dirkx 0:355018f44c9f 579 IP_STATS_INC(ip.err);
dirkx 0:355018f44c9f 580 snmp_inc_ipoutdiscards();
dirkx 0:355018f44c9f 581 return ERR_BUF;
dirkx 0:355018f44c9f 582 }
dirkx 0:355018f44c9f 583
dirkx 0:355018f44c9f 584 iphdr = (struct ip_hdr *)p->payload;
dirkx 0:355018f44c9f 585 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
dirkx 0:355018f44c9f 586 (p->len >= sizeof(struct ip_hdr)));
dirkx 0:355018f44c9f 587
dirkx 0:355018f44c9f 588 IPH_TTL_SET(iphdr, ttl);
dirkx 0:355018f44c9f 589 IPH_PROTO_SET(iphdr, proto);
dirkx 0:355018f44c9f 590
dirkx 0:355018f44c9f 591 /* dest cannot be NULL here */
dirkx 0:355018f44c9f 592 ip_addr_copy(iphdr->dest, *dest);
dirkx 0:355018f44c9f 593
dirkx 0:355018f44c9f 594 IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
dirkx 0:355018f44c9f 595 IPH_LEN_SET(iphdr, htons(p->tot_len));
dirkx 0:355018f44c9f 596 IPH_OFFSET_SET(iphdr, 0);
dirkx 0:355018f44c9f 597 IPH_ID_SET(iphdr, htons(ip_id));
dirkx 0:355018f44c9f 598 ++ip_id;
dirkx 0:355018f44c9f 599
dirkx 0:355018f44c9f 600 if (ip_addr_isany(src)) {
dirkx 0:355018f44c9f 601 ip_addr_copy(iphdr->src, netif->ip_addr);
dirkx 0:355018f44c9f 602 } else {
dirkx 0:355018f44c9f 603 /* src cannot be NULL here */
dirkx 0:355018f44c9f 604 ip_addr_copy(iphdr->src, *src);
dirkx 0:355018f44c9f 605 }
dirkx 0:355018f44c9f 606
dirkx 0:355018f44c9f 607 IPH_CHKSUM_SET(iphdr, 0);
dirkx 0:355018f44c9f 608 #if CHECKSUM_GEN_IP
dirkx 0:355018f44c9f 609 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
dirkx 0:355018f44c9f 610 #endif
dirkx 0:355018f44c9f 611 } else {
dirkx 0:355018f44c9f 612 /* IP header already included in p */
dirkx 0:355018f44c9f 613 iphdr = (struct ip_hdr *)p->payload;
dirkx 0:355018f44c9f 614 dest = &(iphdr->dest);
dirkx 0:355018f44c9f 615 }
dirkx 0:355018f44c9f 616
dirkx 0:355018f44c9f 617 IP_STATS_INC(ip.xmit);
dirkx 0:355018f44c9f 618
dirkx 0:355018f44c9f 619 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
dirkx 0:355018f44c9f 620 ip_debug_print(p);
dirkx 0:355018f44c9f 621
dirkx 0:355018f44c9f 622 #if ENABLE_LOOPBACK
dirkx 0:355018f44c9f 623 if (ip_addr_cmp(dest, &netif->ip_addr)) {
dirkx 0:355018f44c9f 624 /* Packet to self, enqueue it for loopback */
dirkx 0:355018f44c9f 625 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
dirkx 0:355018f44c9f 626 return netif_loop_output(netif, p, dest);
dirkx 0:355018f44c9f 627 }
dirkx 0:355018f44c9f 628 #endif /* ENABLE_LOOPBACK */
dirkx 0:355018f44c9f 629 #if IP_FRAG
dirkx 0:355018f44c9f 630 /* don't fragment if interface has mtu set to 0 [loopif] */
dirkx 0:355018f44c9f 631 if (netif->mtu && (p->tot_len > netif->mtu)) {
dirkx 0:355018f44c9f 632 return ip_frag(p,netif,dest);
dirkx 0:355018f44c9f 633 }
dirkx 0:355018f44c9f 634 #endif
dirkx 0:355018f44c9f 635
dirkx 0:355018f44c9f 636 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
dirkx 0:355018f44c9f 637 return netif->output(netif, p, dest);
dirkx 0:355018f44c9f 638 }
dirkx 0:355018f44c9f 639
dirkx 0:355018f44c9f 640 /**
dirkx 0:355018f44c9f 641 * Simple interface to ip_output_if. It finds the outgoing network
dirkx 0:355018f44c9f 642 * interface and calls upon ip_output_if to do the actual work.
dirkx 0:355018f44c9f 643 *
dirkx 0:355018f44c9f 644 * @param p the packet to send (p->payload points to the data, e.g. next
dirkx 0:355018f44c9f 645 protocol header; if dest == IP_HDRINCL, p already includes an IP
dirkx 0:355018f44c9f 646 header and p->payload points to that IP header)
dirkx 0:355018f44c9f 647 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
dirkx 0:355018f44c9f 648 * IP address of the netif used to send is used as source address)
dirkx 0:355018f44c9f 649 * @param dest the destination IP address to send the packet to
dirkx 0:355018f44c9f 650 * @param ttl the TTL value to be set in the IP header
dirkx 0:355018f44c9f 651 * @param tos the TOS value to be set in the IP header
dirkx 0:355018f44c9f 652 * @param proto the PROTOCOL to be set in the IP header
dirkx 0:355018f44c9f 653 *
dirkx 0:355018f44c9f 654 * @return ERR_RTE if no route is found
dirkx 0:355018f44c9f 655 * see ip_output_if() for more return values
dirkx 0:355018f44c9f 656 */
dirkx 0:355018f44c9f 657 err_t
dirkx 0:355018f44c9f 658 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
dirkx 0:355018f44c9f 659 u8_t ttl, u8_t tos, u8_t proto)
dirkx 0:355018f44c9f 660 {
dirkx 0:355018f44c9f 661 struct netif *netif;
dirkx 0:355018f44c9f 662
dirkx 0:355018f44c9f 663 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
dirkx 0:355018f44c9f 664 gets altered as the packet is passed down the stack */
dirkx 0:355018f44c9f 665 LWIP_ASSERT("p->ref == 1", p->ref == 1);
dirkx 0:355018f44c9f 666
dirkx 0:355018f44c9f 667 if ((netif = ip_route(dest)) == NULL) {
dirkx 0:355018f44c9f 668 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
dirkx 0:355018f44c9f 669 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
dirkx 0:355018f44c9f 670 IP_STATS_INC(ip.rterr);
dirkx 0:355018f44c9f 671 return ERR_RTE;
dirkx 0:355018f44c9f 672 }
dirkx 0:355018f44c9f 673
dirkx 0:355018f44c9f 674 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
dirkx 0:355018f44c9f 675 }
dirkx 0:355018f44c9f 676
dirkx 0:355018f44c9f 677 #if LWIP_NETIF_HWADDRHINT
dirkx 0:355018f44c9f 678 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
dirkx 0:355018f44c9f 679 * before calling ip_output_if.
dirkx 0:355018f44c9f 680 *
dirkx 0:355018f44c9f 681 * @param p the packet to send (p->payload points to the data, e.g. next
dirkx 0:355018f44c9f 682 protocol header; if dest == IP_HDRINCL, p already includes an IP
dirkx 0:355018f44c9f 683 header and p->payload points to that IP header)
dirkx 0:355018f44c9f 684 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
dirkx 0:355018f44c9f 685 * IP address of the netif used to send is used as source address)
dirkx 0:355018f44c9f 686 * @param dest the destination IP address to send the packet to
dirkx 0:355018f44c9f 687 * @param ttl the TTL value to be set in the IP header
dirkx 0:355018f44c9f 688 * @param tos the TOS value to be set in the IP header
dirkx 0:355018f44c9f 689 * @param proto the PROTOCOL to be set in the IP header
dirkx 0:355018f44c9f 690 * @param addr_hint address hint pointer set to netif->addr_hint before
dirkx 0:355018f44c9f 691 * calling ip_output_if()
dirkx 0:355018f44c9f 692 *
dirkx 0:355018f44c9f 693 * @return ERR_RTE if no route is found
dirkx 0:355018f44c9f 694 * see ip_output_if() for more return values
dirkx 0:355018f44c9f 695 */
dirkx 0:355018f44c9f 696 err_t
dirkx 0:355018f44c9f 697 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
dirkx 0:355018f44c9f 698 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
dirkx 0:355018f44c9f 699 {
dirkx 0:355018f44c9f 700 struct netif *netif;
dirkx 0:355018f44c9f 701 err_t err;
dirkx 0:355018f44c9f 702
dirkx 0:355018f44c9f 703 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
dirkx 0:355018f44c9f 704 gets altered as the packet is passed down the stack */
dirkx 0:355018f44c9f 705 LWIP_ASSERT("p->ref == 1", p->ref == 1);
dirkx 0:355018f44c9f 706
dirkx 0:355018f44c9f 707 if ((netif = ip_route(dest)) == NULL) {
dirkx 0:355018f44c9f 708 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
dirkx 0:355018f44c9f 709 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
dirkx 0:355018f44c9f 710 IP_STATS_INC(ip.rterr);
dirkx 0:355018f44c9f 711 return ERR_RTE;
dirkx 0:355018f44c9f 712 }
dirkx 0:355018f44c9f 713
dirkx 0:355018f44c9f 714 netif->addr_hint = addr_hint;
dirkx 0:355018f44c9f 715 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
dirkx 0:355018f44c9f 716 netif->addr_hint = NULL;
dirkx 0:355018f44c9f 717
dirkx 0:355018f44c9f 718 return err;
dirkx 0:355018f44c9f 719 }
dirkx 0:355018f44c9f 720 #endif /* LWIP_NETIF_HWADDRHINT*/
dirkx 0:355018f44c9f 721
dirkx 0:355018f44c9f 722 #if IP_DEBUG
dirkx 0:355018f44c9f 723 /* Print an IP header by using LWIP_DEBUGF
dirkx 0:355018f44c9f 724 * @param p an IP packet, p->payload pointing to the IP header
dirkx 0:355018f44c9f 725 */
dirkx 0:355018f44c9f 726 void
dirkx 0:355018f44c9f 727 ip_debug_print(struct pbuf *p)
dirkx 0:355018f44c9f 728 {
dirkx 0:355018f44c9f 729 struct ip_hdr *iphdr = (struct ip_hdr *) p->payload;
dirkx 0:355018f44c9f 730 u8_t *payload;
dirkx 0:355018f44c9f 731
dirkx 0:355018f44c9f 732 payload = (u8_t *)iphdr + IP_HLEN;
dirkx 0:355018f44c9f 733
dirkx 0:355018f44c9f 734 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
dirkx 0:355018f44c9f 735 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 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:355018f44c9f 737 IPH_V(iphdr),
dirkx 0:355018f44c9f 738 IPH_HL(iphdr),
dirkx 0:355018f44c9f 739 IPH_TOS(iphdr),
dirkx 0:355018f44c9f 740 ntohs(IPH_LEN(iphdr))));
dirkx 0:355018f44c9f 741 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 742 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
dirkx 0:355018f44c9f 743 ntohs(IPH_ID(iphdr)),
dirkx 0:355018f44c9f 744 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
dirkx 0:355018f44c9f 745 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
dirkx 0:355018f44c9f 746 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
dirkx 0:355018f44c9f 747 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
dirkx 0:355018f44c9f 748 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 749 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
dirkx 0:355018f44c9f 750 IPH_TTL(iphdr),
dirkx 0:355018f44c9f 751 IPH_PROTO(iphdr),
dirkx 0:355018f44c9f 752 ntohs(IPH_CHKSUM(iphdr))));
dirkx 0:355018f44c9f 753 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 754 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
dirkx 0:355018f44c9f 755 ip4_addr1_16(&iphdr->src),
dirkx 0:355018f44c9f 756 ip4_addr2_16(&iphdr->src),
dirkx 0:355018f44c9f 757 ip4_addr3_16(&iphdr->src),
dirkx 0:355018f44c9f 758 ip4_addr4_16(&iphdr->src)));
dirkx 0:355018f44c9f 759 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 760 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
dirkx 0:355018f44c9f 761 ip4_addr1_16(&iphdr->dest),
dirkx 0:355018f44c9f 762 ip4_addr2_16(&iphdr->dest),
dirkx 0:355018f44c9f 763 ip4_addr3_16(&iphdr->dest),
dirkx 0:355018f44c9f 764 ip4_addr4_16(&iphdr->dest)));
dirkx 0:355018f44c9f 765 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
dirkx 0:355018f44c9f 766 }
dirkx 0:355018f44c9f 767 #endif /* IP_DEBUG */