⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip.c

📁 最新的lwip 1.3.0版本在ucos平台上的移植
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * @file * This is the IPv4 layer implementation for incoming and outgoing IP traffic. *  * @see ip_frag.c * *//* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, *    this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, *    this list of conditions and the following disclaimer in the documentation *    and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * */#include "lwip/opt.h"#include "lwip/ip.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/ip_frag.h"#include "lwip/inet.h"#include "lwip/inet_chksum.h"#include "lwip/netif.h"#include "lwip/icmp.h"#include "lwip/igmp.h"#include "lwip/raw.h"#include "lwip/udp.h"#include "lwip/tcp.h"#include "lwip/snmp.h"#include "lwip/dhcp.h"#include "lwip/stats.h"#include "arch/perf.h"/** * Finds the appropriate network interface for a given IP address. It * searches the list of network interfaces linearly. A match is found * if the masked IP address of the network interface equals the masked * IP address given to the function. * * @param dest the destination IP address for which to find the route * @return the netif on which to send to reach dest */struct netif *ip_route(struct ip_addr *dest){  struct netif *netif;  /* iterate through netifs */  for(netif = netif_list; netif != NULL; netif = netif->next) {    /* network mask matches? */    if (netif_is_up(netif)) {      if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {        /* return netif on which to forward IP packet */        return netif;      }    }  }  if ((netif_default == NULL) || (!netif_is_up(netif_default))) {    LWIP_DEBUGF(IP_DEBUG | 2, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));    IP_STATS_INC(ip.rterr);    snmp_inc_ipoutnoroutes();    return NULL;  }  /* no matching netif found, use default netif */  return netif_default;}#if IP_FORWARD/** * Forwards an IP packet. It finds an appropriate route for the * packet, decrements the TTL value of the packet, adjusts the * checksum and outputs the packet on the appropriate interface. * * @param p the packet to forward (p->payload points to IP header) * @param iphdr the IP header of the input packet * @param inp the netif on which this packet was received * @return the netif on which the packet was sent (NULL if it wasn't sent) */static struct netif *ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp){  struct netif *netif;  PERF_START;  /* Find network interface where to forward this IP packet to. */  netif = ip_route((struct ip_addr *)&(iphdr->dest));  if (netif == NULL) {    LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",                      iphdr->dest.addr));    snmp_inc_ipoutnoroutes();    return (struct netif *)NULL;  }  /* Do not forward packets onto the same network interface on which   * they arrived. */  if (netif == inp) {    LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));    snmp_inc_ipoutnoroutes();    return (struct netif *)NULL;  }  /* decrement TTL */  IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);  /* send ICMP if TTL == 0 */  if (IPH_TTL(iphdr) == 0) {    snmp_inc_ipinhdrerrors();#if LWIP_ICMP    /* Don't send ICMP messages in response to ICMP messages */    if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {      icmp_time_exceeded(p, ICMP_TE_TTL);    }#endif /* LWIP_ICMP */    return (struct netif *)NULL;  }  /* Incrementally update the IP checksum. */  if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);  } else {    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));  }  LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",                    iphdr->dest.addr));  IP_STATS_INC(ip.fw);  IP_STATS_INC(ip.xmit);  snmp_inc_ipforwdatagrams();  PERF_STOP("ip_forward");  /* transmit pbuf on chosen interface */  netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));  return netif;}#endif /* IP_FORWARD *//** * This function is called by the network interface device driver when * an IP packet is received. The function does the basic checks of the * IP header such as packet size being at least larger than the header * size etc. If the packet was not destined for us, the packet is * forwarded (using ip_forward). The IP checksum is always checked. * * Finally, the packet is sent to the upper layer protocol input function. *  * @param p the received IP packet (p->payload points to IP header) * @param inp the netif on which this packet was received * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't *         processed, but currently always returns ERR_OK) */err_tip_input(struct pbuf *p, struct netif *inp){  struct ip_hdr *iphdr;  struct netif *netif;  u16_t iphdr_hlen;  u16_t iphdr_len;#if LWIP_DHCP  int check_ip_src=1;#endif /* LWIP_DHCP */  IP_STATS_INC(ip.recv);  snmp_inc_ipinreceives();  /* identify the IP header */  iphdr = p->payload;  if (IPH_V(iphdr) != 4) {    LWIP_DEBUGF(IP_DEBUG | 1, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));    ip_debug_print(p);    pbuf_free(p);    IP_STATS_INC(ip.err);    IP_STATS_INC(ip.drop);    snmp_inc_ipinhdrerrors();    return ERR_OK;  }  /* obtain IP header length in number of 32-bit words */  iphdr_hlen = IPH_HL(iphdr);  /* calculate IP header length in bytes */  iphdr_hlen *= 4;  /* obtain ip length in bytes */  iphdr_len = ntohs(IPH_LEN(iphdr));  /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */  if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {    if (iphdr_hlen > p->len)    LWIP_DEBUGF(IP_DEBUG | 2, ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",                               iphdr_hlen, p->len));    if (iphdr_len > p->tot_len)    LWIP_DEBUGF(IP_DEBUG | 2, ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), "                               "IP packet dropped.\n",                               iphdr_len, p->tot_len));    /* free (drop) packet pbufs */    pbuf_free(p);    IP_STATS_INC(ip.lenerr);    IP_STATS_INC(ip.drop);    snmp_inc_ipindiscards();    return ERR_OK;  }  /* verify checksum */#if CHECKSUM_CHECK_IP  if (inet_chksum(iphdr, iphdr_hlen) != 0) {    LWIP_DEBUGF(IP_DEBUG | 2, ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));    ip_debug_print(p);    pbuf_free(p);    IP_STATS_INC(ip.chkerr);    IP_STATS_INC(ip.drop);    snmp_inc_ipinhdrerrors();    return ERR_OK;  }#endif  /* Trim pbuf. This should have been done at the netif layer,   * but we'll do it anyway just to be sure that its done. */  pbuf_realloc(p, iphdr_len);  /* match packet against an interface, i.e. is this packet for us? */#if LWIP_IGMP  if (ip_addr_ismulticast(&(iphdr->dest))) {    if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {      netif = inp;    } else {      netif = NULL;    }  } else#endif /* LWIP_IGMP */  {    /* start trying with inp. if that's not acceptable, start walking the       list of configured netifs.       'first' is used as a boolean to mark whether we started walking the list */    int first = 1;    netif = inp;    do {      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",          iphdr->dest.addr, netif->ip_addr.addr,          iphdr->dest.addr & netif->netmask.addr,          netif->ip_addr.addr & netif->netmask.addr,          iphdr->dest.addr & ~(netif->netmask.addr)));      /* interface is up and configured? */      if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {        /* unicast to this interface address? */        if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||            /* or broadcast on this interface network address? */            ip_addr_isbroadcast(&(iphdr->dest), netif)) {          LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",              netif->name[0], netif->name[1]));          /* break out of for loop */          break;        }      }      if (first) {        first = 0;        netif = netif_list;      } else {        netif = netif->next;      }      if (netif == inp) {        netif = netif->next;      }    } while(netif != NULL);  }#if LWIP_DHCP  /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed   * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.   * According to RFC 1542 section 3.1.1, referred by RFC 2131).   */  if (netif == NULL) {    /* remote port is DHCP server? */    if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | 1, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",        ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));      if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | 1, ("ip_input: DHCP packet accepted.\n"));        netif = inp;        check_ip_src = 0;      }    }  }#endif /* LWIP_DHCP */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -