1 /** 2 * @file 3 * This is the IPv4 layer implementation for incoming and outgoing IP traffic. 4 * 5 * @see ip_frag.c 6 * 7 */ 8 9 /* 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without modification, 14 * are permitted provided that the following conditions are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 3. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 * OF SUCH DAMAGE. 34 * 35 * This file is part of the lwIP TCP/IP stack. 36 * 37 * Author: Adam Dunkels <adam@sics.se> 38 * 39 */ 40 41 #include "lwip/opt.h" 42 43 #if LWIP_IPV4 44 45 #include "lwip/ip.h" 46 #include "lwip/def.h" 47 #include "lwip/mem.h" 48 #include "lwip/ip4_frag.h" 49 #include "lwip/inet_chksum.h" 50 #include "lwip/netif.h" 51 #include "lwip/icmp.h" 52 #include "lwip/igmp.h" 53 #include "lwip/raw.h" 54 #include "lwip/udp.h" 55 #include "lwip/priv/tcp_priv.h" 56 #include "lwip/autoip.h" 57 #include "lwip/stats.h" 58 #include "lwip/prot/dhcp.h" 59 60 #include <string.h> 61 62 #ifdef LWIP_HOOK_FILENAME 63 #include LWIP_HOOK_FILENAME 64 #endif 65 66 /** Set this to 0 in the rare case of wanting to call an extra function to 67 * generate the IP checksum (in contrast to calculating it on-the-fly). */ 68 #ifndef LWIP_INLINE_IP_CHKSUM 69 #if LWIP_CHECKSUM_CTRL_PER_NETIF 70 #define LWIP_INLINE_IP_CHKSUM 0 71 #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */ 72 #define LWIP_INLINE_IP_CHKSUM 1 73 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ 74 #endif 75 76 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP 77 #define CHECKSUM_GEN_IP_INLINE 1 78 #else 79 #define CHECKSUM_GEN_IP_INLINE 0 80 #endif 81 82 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT) 83 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1 84 85 /** Some defines for DHCP to let link-layer-addressed packets through while the 86 * netif is down. 87 * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port) 88 * to return 1 if the port is accepted and 0 if the port is not accepted. 89 */ 90 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) 91 /* accept DHCP client port and custom port */ 92 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \ 93 || (LWIP_IP_ACCEPT_UDP_PORT(port))) 94 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ 95 /* accept custom port only */ 96 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port)) 97 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ 98 /* accept DHCP client port only */ 99 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT)) 100 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ 101 102 #else /* LWIP_DHCP */ 103 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0 104 #endif /* LWIP_DHCP */ 105 106 /** The IP header ID of the next outgoing IP packet */ 107 static u16_t ip_id; 108 109 #if LWIP_MULTICAST_TX_OPTIONS 110 /** The default netif used for multicast */ 111 static struct netif* ip4_default_multicast_netif; 112 113 /** 114 * @ingroup ip4 115 * Set a default netif for IPv4 multicast. */ 116 void 117 ip4_set_default_multicast_netif(struct netif* default_multicast_netif) 118 { 119 ip4_default_multicast_netif = default_multicast_netif; 120 } 121 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 122 123 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 124 /** 125 * Source based IPv4 routing must be fully implemented in 126 * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides he parameters. 127 */ 128 struct netif * 129 ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src) 130 { 131 if (src != NULL) { 132 /* when src==NULL, the hook is called from ip4_route(dest) */ 133 struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, src); 134 if (netif != NULL) { 135 return netif; 136 } 137 } 138 return ip4_route(dest); 139 } 140 #endif /* LWIP_HOOK_IP4_ROUTE_SRC */ 141 142 /** 143 * Finds the appropriate network interface for a given IP address. It 144 * searches the list of network interfaces linearly. A match is found 145 * if the masked IP address of the network interface equals the masked 146 * IP address given to the function. 147 * 148 * @param dest the destination IP address for which to find the route 149 * @return the netif on which to send to reach dest 150 */ 151 struct netif * 152 __weak 153 ip4_route(const ip4_addr_t *dest) 154 { 155 #if !LWIP_SINGLE_NETIF 156 struct netif *netif; 157 158 #if LWIP_MULTICAST_TX_OPTIONS 159 /* Use administratively selected interface for multicast by default */ 160 if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) { 161 return ip4_default_multicast_netif; 162 } 163 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 164 165 /* iterate through netifs */ 166 for (netif = netif_list; netif != NULL; netif = netif->next) { 167 /* is the netif up, does it have a link and a valid address? */ 168 if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { 169 /* network mask matches? */ 170 if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) { 171 /* return netif on which to forward IP packet */ 172 return netif; 173 } 174 /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */ 175 if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) { 176 /* return netif on which to forward IP packet */ 177 return netif; 178 } 179 } 180 } 181 182 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF 183 /* loopif is disabled, looopback traffic is passed through any netif */ 184 if (ip4_addr_isloopback(dest)) { 185 /* don't check for link on loopback traffic */ 186 if (netif_default != NULL && netif_is_up(netif_default)) { 187 return netif_default; 188 } 189 /* default netif is not up, just use any netif for loopback traffic */ 190 for (netif = netif_list; netif != NULL; netif = netif->next) { 191 if (netif_is_up(netif)) { 192 return netif; 193 } 194 } 195 return NULL; 196 } 197 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */ 198 199 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 200 netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, NULL); 201 if (netif != NULL) { 202 return netif; 203 } 204 #elif defined(LWIP_HOOK_IP4_ROUTE) 205 netif = LWIP_HOOK_IP4_ROUTE(dest); 206 if (netif != NULL) { 207 return netif; 208 } 209 #endif 210 #endif /* !LWIP_SINGLE_NETIF */ 211 212 if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) || 213 ip4_addr_isany_val(*netif_ip4_addr(netif_default))) { 214 /* No matching netif found and default netif is not usable. 215 If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */ 216 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 217 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); 218 IP_STATS_INC(ip.rterr); 219 MIB2_STATS_INC(mib2.ipoutnoroutes); 220 return NULL; 221 } 222 223 return netif_default; 224 } 225 226 #if IP_FORWARD 227 /** 228 * Determine whether an IP address is in a reserved set of addresses 229 * that may not be forwarded, or whether datagrams to that destination 230 * may be forwarded. 231 * @param p the packet to forward 232 * @return 1: can forward 0: discard 233 */ 234 static int 235 ip4_canforward(struct pbuf *p) 236 { 237 u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr())); 238 239 if (p->flags & PBUF_FLAG_LLBCAST) { 240 /* don't route link-layer broadcasts */ 241 return 0; 242 } 243 if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) { 244 /* don't route link-layer multicasts unless the destination address is an IP 245 multicast address */ 246 return 0; 247 } 248 if (IP_EXPERIMENTAL(addr)) { 249 return 0; 250 } 251 if (IP_CLASSA(addr)) { 252 u32_t net = addr & IP_CLASSA_NET; 253 if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) { 254 /* don't route loopback packets */ 255 return 0; 256 } 257 } 258 return 1; 259 } 260 261 /** 262 * Forwards an IP packet. It finds an appropriate route for the 263 * packet, decrements the TTL value of the packet, adjusts the 264 * checksum and outputs the packet on the appropriate interface. 265 * 266 * @param p the packet to forward (p->payload points to IP header) 267 * @param iphdr the IP header of the input packet 268 * @param inp the netif on which this packet was received 269 */ 270 static void 271 ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) 272 { 273 struct netif *netif; 274 275 #if defined(__minix) 276 /* MINIX 3 only: forward packets only when enabled through configuration. */ 277 if (!lwip_ip4_forward) { 278 return; 279 } 280 #endif /* defined(__minix) */ 281 282 PERF_START; 283 LWIP_UNUSED_ARG(inp); 284 285 if (!ip4_canforward(p)) { 286 goto return_noroute; 287 } 288 289 /* RFC3927 2.7: do not forward link-local addresses */ 290 if (ip4_addr_islinklocal(ip4_current_dest_addr())) { 291 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 292 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), 293 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); 294 goto return_noroute; 295 } 296 297 /* Find network interface where to forward this IP packet to. */ 298 netif = ip4_route_src(ip4_current_dest_addr(), ip4_current_src_addr()); 299 if (netif == NULL) { 300 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", 301 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), 302 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); 303 /* @todo: send ICMP_DUR_NET? */ 304 goto return_noroute; 305 } 306 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF 307 /* Do not forward packets onto the same network interface on which 308 * they arrived. */ 309 if (netif == inp) { 310 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n")); 311 goto return_noroute; 312 } 313 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */ 314 315 /* decrement TTL */ 316 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1); 317 /* send ICMP if TTL == 0 */ 318 if (IPH_TTL(iphdr) == 0) { 319 MIB2_STATS_INC(mib2.ipinhdrerrors); 320 #if LWIP_ICMP 321 /* Don't send ICMP messages in response to ICMP messages */ 322 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) { 323 icmp_time_exceeded(p, ICMP_TE_TTL); 324 } 325 #endif /* LWIP_ICMP */ 326 return; 327 } 328 329 /* Incrementally update the IP checksum. */ 330 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) { 331 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1); 332 } else { 333 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100)); 334 } 335 336 LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 337 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), 338 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); 339 340 IP_STATS_INC(ip.fw); 341 MIB2_STATS_INC(mib2.ipforwdatagrams); 342 IP_STATS_INC(ip.xmit); 343 344 PERF_STOP("ip4_forward"); 345 /* don't fragment if interface has mtu set to 0 [loopif] */ 346 if (netif->mtu && (p->tot_len > netif->mtu)) { 347 if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) { 348 #if IP_FRAG 349 ip4_frag(p, netif, ip4_current_dest_addr()); 350 #else /* IP_FRAG */ 351 /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */ 352 #endif /* IP_FRAG */ 353 } else { 354 #if LWIP_ICMP 355 /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */ 356 icmp_dest_unreach(p, ICMP_DUR_FRAG); 357 #endif /* LWIP_ICMP */ 358 } 359 return; 360 } 361 /* transmit pbuf on chosen interface */ 362 netif->output(netif, p, ip4_current_dest_addr()); 363 return; 364 return_noroute: 365 MIB2_STATS_INC(mib2.ipoutnoroutes); 366 } 367 #endif /* IP_FORWARD */ 368 369 /** Return true if the current input packet should be accepted on this netif */ 370 static int 371 ip4_input_accept(struct netif *netif) 372 { 373 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", 374 ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)), 375 ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)), 376 ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)), 377 ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif)))); 378 379 /* interface is up and configured? */ 380 if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) { 381 /* unicast to this interface address? */ 382 if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) || 383 /* or broadcast on this interface network address? */ 384 ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) 385 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF 386 || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK)) 387 #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */ 388 ) { 389 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n", 390 netif->name[0], netif->name[1])); 391 /* accept on this netif */ 392 return 1; 393 } 394 #if LWIP_AUTOIP 395 /* connections to link-local addresses must persist after changing 396 the netif's address (RFC3927 ch. 1.9) */ 397 if (autoip_accept_packet(netif, ip4_current_dest_addr())) { 398 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n", 399 netif->name[0], netif->name[1])); 400 /* accept on this netif */ 401 return 1; 402 } 403 #endif /* LWIP_AUTOIP */ 404 } 405 return 0; 406 } 407 408 /** 409 * This function is called by the network interface device driver when 410 * an IP packet is received. The function does the basic checks of the 411 * IP header such as packet size being at least larger than the header 412 * size etc. If the packet was not destined for us, the packet is 413 * forwarded (using ip_forward). The IP checksum is always checked. 414 * 415 * Finally, the packet is sent to the upper layer protocol input function. 416 * 417 * @param p the received IP packet (p->payload points to IP header) 418 * @param inp the netif on which this packet was received 419 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't 420 * processed, but currently always returns ERR_OK) 421 */ 422 err_t 423 ip4_input(struct pbuf *p, struct netif *inp) 424 { 425 struct ip_hdr *iphdr; 426 struct netif *netif; 427 u16_t iphdr_hlen; 428 u16_t iphdr_len; 429 #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP 430 int check_ip_src = 1; 431 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */ 432 433 IP_STATS_INC(ip.recv); 434 MIB2_STATS_INC(mib2.ipinreceives); 435 436 /* identify the IP header */ 437 iphdr = (struct ip_hdr *)p->payload; 438 if (IPH_V(iphdr) != 4) { 439 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr))); 440 ip4_debug_print(p); 441 pbuf_free(p); 442 IP_STATS_INC(ip.err); 443 IP_STATS_INC(ip.drop); 444 MIB2_STATS_INC(mib2.ipinhdrerrors); 445 return ERR_OK; 446 } 447 448 #ifdef LWIP_HOOK_IP4_INPUT 449 if (LWIP_HOOK_IP4_INPUT(p, inp)) { 450 /* the packet has been eaten */ 451 return ERR_OK; 452 } 453 #endif 454 455 /* obtain IP header length in number of 32-bit words */ 456 iphdr_hlen = IPH_HL(iphdr); 457 /* calculate IP header length in bytes */ 458 iphdr_hlen *= 4; 459 /* obtain ip length in bytes */ 460 iphdr_len = lwip_ntohs(IPH_LEN(iphdr)); 461 462 /* Trim pbuf. This is especially required for packets < 60 bytes. */ 463 if (iphdr_len < p->tot_len) { 464 pbuf_realloc(p, iphdr_len); 465 } 466 467 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */ 468 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) { 469 if (iphdr_hlen < IP_HLEN) { 470 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 471 ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen)); 472 } 473 if (iphdr_hlen > p->len) { 474 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 475 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n", 476 iphdr_hlen, p->len)); 477 } 478 if (iphdr_len > p->tot_len) { 479 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 480 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n", 481 iphdr_len, p->tot_len)); 482 } 483 /* free (drop) packet pbufs */ 484 pbuf_free(p); 485 IP_STATS_INC(ip.lenerr); 486 IP_STATS_INC(ip.drop); 487 MIB2_STATS_INC(mib2.ipindiscards); 488 return ERR_OK; 489 } 490 491 /* verify checksum */ 492 #if CHECKSUM_CHECK_IP 493 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) { 494 if (inet_chksum(iphdr, iphdr_hlen) != 0) { 495 496 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 497 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen))); 498 ip4_debug_print(p); 499 pbuf_free(p); 500 IP_STATS_INC(ip.chkerr); 501 IP_STATS_INC(ip.drop); 502 MIB2_STATS_INC(mib2.ipinhdrerrors); 503 return ERR_OK; 504 } 505 } 506 #endif 507 508 /* copy IP addresses to aligned ip_addr_t */ 509 ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest); 510 ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src); 511 512 /* match packet against an interface, i.e. is this packet for us? */ 513 if (ip4_addr_ismulticast(ip4_current_dest_addr())) { 514 #if LWIP_IGMP 515 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) { 516 /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */ 517 ip4_addr_t allsystems; 518 IP4_ADDR(&allsystems, 224, 0, 0, 1); 519 if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) && 520 ip4_addr_isany(ip4_current_src_addr())) { 521 check_ip_src = 0; 522 } 523 netif = inp; 524 } else { 525 netif = NULL; 526 } 527 #else /* LWIP_IGMP */ 528 if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) { 529 netif = inp; 530 } else { 531 netif = NULL; 532 } 533 #endif /* LWIP_IGMP */ 534 } else { 535 /* start trying with inp. if that's not acceptable, start walking the 536 list of configured netifs. */ 537 if (ip4_input_accept(inp)) { 538 netif = inp; 539 } else { 540 netif = NULL; 541 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF 542 /* Packets sent to the loopback address must not be accepted on an 543 * interface that does not have the loopback address assigned to it, 544 * unless a non-loopback interface is used for loopback traffic. */ 545 if (!ip4_addr_isloopback(ip4_current_dest_addr())) 546 #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */ 547 { 548 #if !LWIP_SINGLE_NETIF 549 NETIF_FOREACH(netif) { 550 if (netif == inp) { 551 /* we checked that before already */ 552 continue; 553 } 554 if (ip4_input_accept(netif)) { 555 break; 556 } 557 } 558 #endif /* !LWIP_SINGLE_NETIF */ 559 } 560 } 561 } 562 563 #if IP_ACCEPT_LINK_LAYER_ADDRESSING 564 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed 565 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP. 566 * According to RFC 1542 section 3.1.1, referred by RFC 2131). 567 * 568 * If you want to accept private broadcast communication while a netif is down, 569 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.: 570 * 571 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345)) 572 */ 573 if (netif == NULL) { 574 /* remote port is DHCP server? */ 575 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { 576 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen); 577 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n", 578 lwip_ntohs(udphdr->dest))); 579 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) { 580 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n")); 581 netif = inp; 582 check_ip_src = 0; 583 } 584 } 585 } 586 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ 587 588 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */ 589 #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING 590 if (check_ip_src 591 #if IP_ACCEPT_LINK_LAYER_ADDRESSING 592 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */ 593 && !ip4_addr_isany_val(*ip4_current_src_addr()) 594 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ 595 ) 596 #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */ 597 { 598 if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) || 599 (ip4_addr_ismulticast(ip4_current_src_addr()))) { 600 /* packet source is not valid */ 601 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n")); 602 /* free (drop) packet pbufs */ 603 pbuf_free(p); 604 IP_STATS_INC(ip.drop); 605 MIB2_STATS_INC(mib2.ipinaddrerrors); 606 MIB2_STATS_INC(mib2.ipindiscards); 607 return ERR_OK; 608 } 609 } 610 611 /* packet not for us? */ 612 if (netif == NULL) { 613 /* packet not for us, route or discard */ 614 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n")); 615 #if IP_FORWARD 616 /* non-broadcast packet? */ 617 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) { 618 /* try to forward IP packet on (other) interfaces */ 619 ip4_forward(p, iphdr, inp); 620 } else 621 #endif /* IP_FORWARD */ 622 { 623 IP_STATS_INC(ip.drop); 624 MIB2_STATS_INC(mib2.ipinaddrerrors); 625 MIB2_STATS_INC(mib2.ipindiscards); 626 } 627 pbuf_free(p); 628 return ERR_OK; 629 } 630 /* packet consists of multiple fragments? */ 631 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) { 632 #if IP_REASSEMBLY /* packet fragment reassembly code present? */ 633 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 ip4_reass()\n", 634 lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8))); 635 /* reassemble the packet*/ 636 p = ip4_reass(p); 637 /* packet not fully reassembled yet? */ 638 if (p == NULL) { 639 return ERR_OK; 640 } 641 iphdr = (struct ip_hdr *)p->payload; 642 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */ 643 pbuf_free(p); 644 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n", 645 lwip_ntohs(IPH_OFFSET(iphdr)))); 646 IP_STATS_INC(ip.opterr); 647 IP_STATS_INC(ip.drop); 648 /* unsupported protocol feature */ 649 MIB2_STATS_INC(mib2.ipinunknownprotos); 650 return ERR_OK; 651 #endif /* IP_REASSEMBLY */ 652 } 653 654 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */ 655 656 #if LWIP_IGMP 657 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */ 658 if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) { 659 #else 660 if (iphdr_hlen > IP_HLEN) { 661 #endif /* LWIP_IGMP */ 662 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n")); 663 pbuf_free(p); 664 IP_STATS_INC(ip.opterr); 665 IP_STATS_INC(ip.drop); 666 /* unsupported protocol feature */ 667 MIB2_STATS_INC(mib2.ipinunknownprotos); 668 return ERR_OK; 669 } 670 #endif /* IP_OPTIONS_ALLOWED == 0 */ 671 672 /* send to upper layers */ 673 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n")); 674 ip4_debug_print(p); 675 LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len)); 676 677 ip_data.current_netif = netif; 678 ip_data.current_input_netif = inp; 679 ip_data.current_ip4_header = iphdr; 680 ip_data.current_ip_header_tot_len = IPH_HL(iphdr) * 4; 681 682 #if LWIP_RAW 683 /* raw input did not eat the packet? */ 684 if (raw_input(p, inp) == 0) 685 #endif /* LWIP_RAW */ 686 { 687 pbuf_header(p, -(s16_t)iphdr_hlen); /* Move to payload, no check necessary. */ 688 689 switch (IPH_PROTO(iphdr)) { 690 #if LWIP_UDP 691 case IP_PROTO_UDP: 692 #if LWIP_UDPLITE 693 case IP_PROTO_UDPLITE: 694 #endif /* LWIP_UDPLITE */ 695 MIB2_STATS_INC(mib2.ipindelivers); 696 udp_input(p, inp); 697 break; 698 #endif /* LWIP_UDP */ 699 #if LWIP_TCP 700 case IP_PROTO_TCP: 701 MIB2_STATS_INC(mib2.ipindelivers); 702 tcp_input(p, inp); 703 break; 704 #endif /* LWIP_TCP */ 705 #if LWIP_ICMP 706 case IP_PROTO_ICMP: 707 MIB2_STATS_INC(mib2.ipindelivers); 708 icmp_input(p, inp); 709 break; 710 #endif /* LWIP_ICMP */ 711 #if LWIP_IGMP 712 case IP_PROTO_IGMP: 713 igmp_input(p, inp, ip4_current_dest_addr()); 714 break; 715 #endif /* LWIP_IGMP */ 716 default: 717 #if LWIP_ICMP 718 /* send ICMP destination protocol unreachable unless is was a broadcast */ 719 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) && 720 !ip4_addr_ismulticast(ip4_current_dest_addr())) { 721 pbuf_header_force(p, iphdr_hlen); /* Move to ip header, no check necessary. */ 722 p->payload = iphdr; 723 icmp_dest_unreach(p, ICMP_DUR_PROTO); 724 } 725 #endif /* LWIP_ICMP */ 726 pbuf_free(p); 727 728 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr))); 729 730 IP_STATS_INC(ip.proterr); 731 IP_STATS_INC(ip.drop); 732 MIB2_STATS_INC(mib2.ipinunknownprotos); 733 } 734 } 735 736 /* @todo: this is not really necessary... */ 737 ip_data.current_netif = NULL; 738 ip_data.current_input_netif = NULL; 739 ip_data.current_ip4_header = NULL; 740 ip_data.current_ip_header_tot_len = 0; 741 ip4_addr_set_any(ip4_current_src_addr()); 742 ip4_addr_set_any(ip4_current_dest_addr()); 743 744 return ERR_OK; 745 } 746 747 /** 748 * Sends an IP packet on a network interface. This function constructs 749 * the IP header and calculates the IP header checksum. If the source 750 * IP address is NULL, the IP address of the outgoing network 751 * interface is filled in as source address. 752 * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already 753 * include an IP header and p->payload points to it instead of the data. 754 * 755 * @param p the packet to send (p->payload points to the data, e.g. next 756 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an 757 IP header and p->payload points to that IP header) 758 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the 759 * IP address of the netif used to send is used as source address) 760 * @param dest the destination IP address to send the packet to 761 * @param ttl the TTL value to be set in the IP header 762 * @param tos the TOS value to be set in the IP header 763 * @param proto the PROTOCOL to be set in the IP header 764 * @param netif the netif on which to send this packet 765 * @return ERR_OK if the packet was sent OK 766 * ERR_BUF if p doesn't have enough space for IP/LINK headers 767 * returns errors returned by netif->output 768 * 769 * @note ip_id: RFC791 "some host may be able to simply use 770 * unique identifiers independent of destination" 771 */ 772 err_t 773 ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 774 u8_t ttl, u8_t tos, 775 u8_t proto, struct netif *netif) 776 { 777 #if IP_OPTIONS_SEND 778 return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0); 779 } 780 781 /** 782 * Same as ip_output_if() but with the possibility to include IP options: 783 * 784 * @ param ip_options pointer to the IP options, copied into the IP header 785 * @ param optlen length of ip_options 786 */ 787 err_t 788 ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 789 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, 790 u16_t optlen) 791 { 792 #endif /* IP_OPTIONS_SEND */ 793 const ip4_addr_t *src_used = src; 794 if (dest != LWIP_IP_HDRINCL) { 795 if (ip4_addr_isany(src)) { 796 src_used = netif_ip4_addr(netif); 797 } 798 } 799 800 #if IP_OPTIONS_SEND 801 return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif, 802 ip_options, optlen); 803 #else /* IP_OPTIONS_SEND */ 804 return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif); 805 #endif /* IP_OPTIONS_SEND */ 806 } 807 808 /** 809 * Same as ip_output_if() but 'src' address is not replaced by netif address 810 * when it is 'any'. 811 */ 812 err_t 813 ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 814 u8_t ttl, u8_t tos, 815 u8_t proto, struct netif *netif) 816 { 817 #if IP_OPTIONS_SEND 818 return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0); 819 } 820 821 /** 822 * Same as ip_output_if_opt() but 'src' address is not replaced by netif address 823 * when it is 'any'. 824 */ 825 err_t 826 ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 827 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, 828 u16_t optlen) 829 { 830 #endif /* IP_OPTIONS_SEND */ 831 struct ip_hdr *iphdr; 832 ip4_addr_t dest_addr; 833 #if CHECKSUM_GEN_IP_INLINE 834 u32_t chk_sum = 0; 835 #endif /* CHECKSUM_GEN_IP_INLINE */ 836 837 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); 838 839 MIB2_STATS_INC(mib2.ipoutrequests); 840 841 /* Should the IP header be generated or is it already included in p? */ 842 if (dest != LWIP_IP_HDRINCL) { 843 u16_t ip_hlen = IP_HLEN; 844 #if IP_OPTIONS_SEND 845 u16_t optlen_aligned = 0; 846 if (optlen != 0) { 847 #if CHECKSUM_GEN_IP_INLINE 848 int i; 849 #endif /* CHECKSUM_GEN_IP_INLINE */ 850 /* round up to a multiple of 4 */ 851 optlen_aligned = ((optlen + 3) & ~3); 852 ip_hlen += optlen_aligned; 853 /* First write in the IP options */ 854 if (pbuf_header(p, optlen_aligned)) { 855 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n")); 856 IP_STATS_INC(ip.err); 857 MIB2_STATS_INC(mib2.ipoutdiscards); 858 return ERR_BUF; 859 } 860 MEMCPY(p->payload, ip_options, optlen); 861 if (optlen < optlen_aligned) { 862 /* zero the remaining bytes */ 863 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen); 864 } 865 #if CHECKSUM_GEN_IP_INLINE 866 for (i = 0; i < optlen_aligned/2; i++) { 867 chk_sum += ((u16_t*)p->payload)[i]; 868 } 869 #endif /* CHECKSUM_GEN_IP_INLINE */ 870 } 871 #endif /* IP_OPTIONS_SEND */ 872 /* generate IP header */ 873 if (pbuf_header(p, IP_HLEN)) { 874 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n")); 875 876 IP_STATS_INC(ip.err); 877 MIB2_STATS_INC(mib2.ipoutdiscards); 878 return ERR_BUF; 879 } 880 881 iphdr = (struct ip_hdr *)p->payload; 882 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr", 883 (p->len >= sizeof(struct ip_hdr))); 884 885 IPH_TTL_SET(iphdr, ttl); 886 IPH_PROTO_SET(iphdr, proto); 887 #if CHECKSUM_GEN_IP_INLINE 888 chk_sum += PP_NTOHS(proto | (ttl << 8)); 889 #endif /* CHECKSUM_GEN_IP_INLINE */ 890 891 /* dest cannot be NULL here */ 892 ip4_addr_copy(iphdr->dest, *dest); 893 #if CHECKSUM_GEN_IP_INLINE 894 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF; 895 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16; 896 #endif /* CHECKSUM_GEN_IP_INLINE */ 897 898 IPH_VHL_SET(iphdr, 4, ip_hlen / 4); 899 IPH_TOS_SET(iphdr, tos); 900 #if CHECKSUM_GEN_IP_INLINE 901 chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8)); 902 #endif /* CHECKSUM_GEN_IP_INLINE */ 903 IPH_LEN_SET(iphdr, lwip_htons(p->tot_len)); 904 #if CHECKSUM_GEN_IP_INLINE 905 chk_sum += iphdr->_len; 906 #endif /* CHECKSUM_GEN_IP_INLINE */ 907 IPH_OFFSET_SET(iphdr, 0); 908 IPH_ID_SET(iphdr, lwip_htons(ip_id)); 909 #if CHECKSUM_GEN_IP_INLINE 910 chk_sum += iphdr->_id; 911 #endif /* CHECKSUM_GEN_IP_INLINE */ 912 ++ip_id; 913 914 if (src == NULL) { 915 ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4); 916 } else { 917 /* src cannot be NULL here */ 918 ip4_addr_copy(iphdr->src, *src); 919 } 920 921 #if CHECKSUM_GEN_IP_INLINE 922 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF; 923 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16; 924 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF); 925 chk_sum = (chk_sum >> 16) + chk_sum; 926 chk_sum = ~chk_sum; 927 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { 928 iphdr->_chksum = (u16_t)chk_sum; /* network order */ 929 } 930 #if LWIP_CHECKSUM_CTRL_PER_NETIF 931 else { 932 IPH_CHKSUM_SET(iphdr, 0); 933 } 934 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/ 935 #else /* CHECKSUM_GEN_IP_INLINE */ 936 IPH_CHKSUM_SET(iphdr, 0); 937 #if CHECKSUM_GEN_IP 938 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { 939 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); 940 } 941 #endif /* CHECKSUM_GEN_IP */ 942 #endif /* CHECKSUM_GEN_IP_INLINE */ 943 } else { 944 /* IP header already included in p */ 945 iphdr = (struct ip_hdr *)p->payload; 946 ip4_addr_copy(dest_addr, iphdr->dest); 947 dest = &dest_addr; 948 } 949 950 IP_STATS_INC(ip.xmit); 951 952 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num)); 953 ip4_debug_print(p); 954 955 #if ENABLE_LOOPBACK 956 if (ip4_addr_cmp(dest, netif_ip4_addr(netif)) 957 #if !LWIP_HAVE_LOOPIF 958 || ip4_addr_isloopback(dest) 959 #endif /* !LWIP_HAVE_LOOPIF */ 960 ) { 961 /* Packet to self, enqueue it for loopback */ 962 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()")); 963 return netif_loop_output(netif, p); 964 } 965 #if LWIP_MULTICAST_TX_OPTIONS 966 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) { 967 netif_loop_output(netif, p); 968 } 969 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 970 #endif /* ENABLE_LOOPBACK */ 971 #if IP_FRAG 972 /* don't fragment if interface has mtu set to 0 [loopif] */ 973 if (netif->mtu && (p->tot_len > netif->mtu)) { 974 return ip4_frag(p, netif, dest); 975 } 976 #endif /* IP_FRAG */ 977 978 LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n")); 979 return netif->output(netif, p, dest); 980 } 981 982 /** 983 * Simple interface to ip_output_if. It finds the outgoing network 984 * interface and calls upon ip_output_if to do the actual work. 985 * 986 * @param p the packet to send (p->payload points to the data, e.g. next 987 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an 988 IP header and p->payload points to that IP header) 989 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the 990 * IP address of the netif used to send is used as source address) 991 * @param dest the destination IP address to send the packet to 992 * @param ttl the TTL value to be set in the IP header 993 * @param tos the TOS value to be set in the IP header 994 * @param proto the PROTOCOL to be set in the IP header 995 * 996 * @return ERR_RTE if no route is found 997 * see ip_output_if() for more return values 998 */ 999 err_t 1000 ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 1001 u8_t ttl, u8_t tos, u8_t proto) 1002 { 1003 struct netif *netif; 1004 1005 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); 1006 1007 if ((netif = ip4_route_src(dest, src)) == NULL) { 1008 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 1009 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); 1010 IP_STATS_INC(ip.rterr); 1011 return ERR_RTE; 1012 } 1013 1014 return ip4_output_if(p, src, dest, ttl, tos, proto, netif); 1015 } 1016 1017 #if LWIP_NETIF_HWADDRHINT 1018 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint 1019 * before calling ip_output_if. 1020 * 1021 * @param p the packet to send (p->payload points to the data, e.g. next 1022 protocol header; if dest == LWIP_IP_HDRINCL, p already includes an 1023 IP header and p->payload points to that IP header) 1024 * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the 1025 * IP address of the netif used to send is used as source address) 1026 * @param dest the destination IP address to send the packet to 1027 * @param ttl the TTL value to be set in the IP header 1028 * @param tos the TOS value to be set in the IP header 1029 * @param proto the PROTOCOL to be set in the IP header 1030 * @param addr_hint address hint pointer set to netif->addr_hint before 1031 * calling ip_output_if() 1032 * 1033 * @return ERR_RTE if no route is found 1034 * see ip_output_if() for more return values 1035 */ 1036 err_t 1037 ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, 1038 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint) 1039 { 1040 struct netif *netif; 1041 err_t err; 1042 1043 LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); 1044 1045 if ((netif = ip4_route_src(dest, src)) == NULL) { 1046 LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 1047 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); 1048 IP_STATS_INC(ip.rterr); 1049 return ERR_RTE; 1050 } 1051 1052 NETIF_SET_HWADDRHINT(netif, addr_hint); 1053 err = ip4_output_if(p, src, dest, ttl, tos, proto, netif); 1054 NETIF_SET_HWADDRHINT(netif, NULL); 1055 1056 return err; 1057 } 1058 #endif /* LWIP_NETIF_HWADDRHINT*/ 1059 1060 #if IP_DEBUG 1061 /* Print an IP header by using LWIP_DEBUGF 1062 * @param p an IP packet, p->payload pointing to the IP header 1063 */ 1064 void 1065 ip4_debug_print(struct pbuf *p) 1066 { 1067 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; 1068 1069 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n")); 1070 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1071 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n", 1072 (u16_t)IPH_V(iphdr), 1073 (u16_t)IPH_HL(iphdr), 1074 (u16_t)IPH_TOS(iphdr), 1075 lwip_ntohs(IPH_LEN(iphdr)))); 1076 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1077 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n", 1078 lwip_ntohs(IPH_ID(iphdr)), 1079 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1), 1080 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1), 1081 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1), 1082 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK))); 1083 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1084 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n", 1085 (u16_t)IPH_TTL(iphdr), 1086 (u16_t)IPH_PROTO(iphdr), 1087 lwip_ntohs(IPH_CHKSUM(iphdr)))); 1088 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1089 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n", 1090 ip4_addr1_16(&iphdr->src), 1091 ip4_addr2_16(&iphdr->src), 1092 ip4_addr3_16(&iphdr->src), 1093 ip4_addr4_16(&iphdr->src))); 1094 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1095 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n", 1096 ip4_addr1_16(&iphdr->dest), 1097 ip4_addr2_16(&iphdr->dest), 1098 ip4_addr3_16(&iphdr->dest), 1099 ip4_addr4_16(&iphdr->dest))); 1100 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); 1101 } 1102 #endif /* IP_DEBUG */ 1103 1104 #endif /* LWIP_IPV4 */ 1105