Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_ethernet.c Source File

lwip_ethernet.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Ethernet common functions
00004  * 
00005  * @defgroup ethernet Ethernet
00006  * @ingroup callbackstyle_api
00007  */
00008 
00009 /*
00010  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
00011  * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
00012  * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without modification,
00016  * are permitted provided that the following conditions are met:
00017  *
00018  * 1. Redistributions of source code must retain the above copyright notice,
00019  *    this list of conditions and the following disclaimer.
00020  * 2. Redistributions in binary form must reproduce the above copyright notice,
00021  *    this list of conditions and the following disclaimer in the documentation
00022  *    and/or other materials provided with the distribution.
00023  * 3. The name of the author may not be used to endorse or promote products
00024  *    derived from this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00027  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00028  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00029  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00031  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00034  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00035  * OF SUCH DAMAGE.
00036  *
00037  * This file is part of the lwIP TCP/IP stack.
00038  *
00039  */
00040 
00041 #include "lwip/opt.h"
00042 
00043 #if LWIP_ARP || LWIP_ETHERNET
00044 
00045 #include "netif/lwip_ethernet.h"
00046 #include "lwip/def.h"
00047 #include "lwip/stats.h"
00048 #include "lwip/etharp.h"
00049 #include "lwip/ip.h"
00050 #include "lwip/snmp.h"
00051 
00052 #include <string.h>
00053 
00054 #include "netif/ppp/ppp_opts.h"
00055 #if PPPOE_SUPPORT
00056 #include "netif/ppp/pppoe.h"
00057 #endif /* PPPOE_SUPPORT */
00058 
00059 #ifdef LWIP_HOOK_FILENAME
00060 #include LWIP_HOOK_FILENAME
00061 #endif
00062 
00063 const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
00064 const struct eth_addr ethzero = {{0,0,0,0,0,0}};
00065 
00066 /**
00067  * @ingroup lwip_nosys
00068  * Process received ethernet frames. Using this function instead of directly
00069  * calling ip_input and passing ARP frames through etharp in ethernetif_input,
00070  * the ARP cache is protected from concurrent access.\n
00071  * Don't call directly, pass to netif_add() and call netif->input().
00072  *
00073  * @param p the received packet, p->payload pointing to the ethernet header
00074  * @param netif the network interface on which the packet was received
00075  * 
00076  * @see LWIP_HOOK_UNKNOWN_ETH_PROTOCOL
00077  * @see ETHARP_SUPPORT_VLAN
00078  * @see LWIP_HOOK_VLAN_CHECK
00079  */
00080 err_t
00081 ethernet_input(struct pbuf *p, struct netif *netif)
00082 {
00083   struct eth_hdr* ethhdr;
00084   u16_t type;
00085 #if LWIP_ARP || ETHARP_SUPPORT_VLAN || LWIP_IPV6
00086   s16_t ip_hdr_offset = SIZEOF_ETH_HDR;
00087 #endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */
00088 
00089   if (p->len <= SIZEOF_ETH_HDR) {
00090     /* a packet with only an ethernet header (or less) is not valid for us */
00091     ETHARP_STATS_INC(etharp.proterr);
00092     ETHARP_STATS_INC(etharp.drop);
00093     MIB2_STATS_NETIF_INC(netif, ifinerrors);
00094     goto free_and_return;
00095   }
00096 
00097   /* points to packet payload, which starts with an Ethernet header */
00098   ethhdr = (struct eth_hdr *)p->payload;
00099   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
00100     ("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n",
00101      (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
00102      (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
00103      (unsigned)ethhdr->src.addr[0],  (unsigned)ethhdr->src.addr[1],  (unsigned)ethhdr->src.addr[2],
00104      (unsigned)ethhdr->src.addr[3],  (unsigned)ethhdr->src.addr[4],  (unsigned)ethhdr->src.addr[5],
00105      lwip_htons(ethhdr->type)));
00106 
00107   type = ethhdr->type;
00108 #if ETHARP_SUPPORT_VLAN
00109   if (type == PP_HTONS(ETHTYPE_VLAN)) {
00110     struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
00111     if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {
00112       /* a packet with only an ethernet/vlan header (or less) is not valid for us */
00113       ETHARP_STATS_INC(etharp.proterr);
00114       ETHARP_STATS_INC(etharp.drop);
00115       MIB2_STATS_NETIF_INC(netif, ifinerrors);
00116       goto free_and_return;
00117     }
00118 #if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */
00119 #ifdef LWIP_HOOK_VLAN_CHECK
00120     if (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) {
00121 #elif defined(ETHARP_VLAN_CHECK_FN)
00122     if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) {
00123 #elif defined(ETHARP_VLAN_CHECK)
00124     if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
00125 #endif
00126       /* silently ignore this packet: not for our VLAN */
00127       pbuf_free(p);
00128       return ERR_OK;
00129     }
00130 #endif /* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */
00131     type = vlan->tpid;
00132     ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
00133   }
00134 #endif /* ETHARP_SUPPORT_VLAN */
00135 
00136 #if LWIP_ARP_FILTER_NETIF
00137   netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, lwip_htons(type));
00138 #endif /* LWIP_ARP_FILTER_NETIF*/
00139 
00140   if (ethhdr->dest.addr[0] & 1) {
00141     /* this might be a multicast or broadcast packet */
00142     if (ethhdr->dest.addr[0] == LL_IP4_MULTICAST_ADDR_0) {
00143 #if LWIP_IPV4
00144       if ((ethhdr->dest.addr[1] == LL_IP4_MULTICAST_ADDR_1) &&
00145           (ethhdr->dest.addr[2] == LL_IP4_MULTICAST_ADDR_2)) {
00146         /* mark the pbuf as link-layer multicast */
00147         p->flags |= PBUF_FLAG_LLMCAST;
00148       }
00149 #endif /* LWIP_IPV4 */
00150     }
00151 #if LWIP_IPV6
00152     else if ((ethhdr->dest.addr[0] == LL_IP6_MULTICAST_ADDR_0) &&
00153              (ethhdr->dest.addr[1] == LL_IP6_MULTICAST_ADDR_1)) {
00154         /* mark the pbuf as link-layer multicast */
00155         p->flags |= PBUF_FLAG_LLMCAST;
00156     }
00157 #endif /* LWIP_IPV6 */
00158     else if (eth_addr_cmp(&ethhdr->dest, &ethbroadcast)) {
00159       /* mark the pbuf as link-layer broadcast */
00160       p->flags |= PBUF_FLAG_LLBCAST;
00161     }
00162   }
00163 
00164   switch (type) {
00165 #if LWIP_IPV4 && LWIP_ARP
00166     /* IP packet? */
00167     case PP_HTONS(ETHTYPE_IP):
00168       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
00169         goto free_and_return;
00170       }
00171       /* skip Ethernet header */
00172       if ((p->len < ip_hdr_offset) || pbuf_header(p, (s16_t)-ip_hdr_offset)) {
00173         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
00174           ("ethernet_input: IPv4 packet dropped, too short (%"S16_F"/%"S16_F")\n",
00175           p->tot_len, ip_hdr_offset));
00176         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));
00177         goto free_and_return;
00178       } else {
00179         /* pass to IP layer */
00180         ip4_input(p, netif);
00181       }
00182       break;
00183 
00184     case PP_HTONS(ETHTYPE_ARP):
00185       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
00186         goto free_and_return;
00187       }
00188       /* skip Ethernet header */
00189       if ((p->len < ip_hdr_offset) || pbuf_header(p, (s16_t)-ip_hdr_offset)) {
00190         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
00191           ("ethernet_input: ARP response packet dropped, too short (%"S16_F"/%"S16_F")\n",
00192           p->tot_len, ip_hdr_offset));
00193         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));
00194         ETHARP_STATS_INC(etharp.lenerr);
00195         ETHARP_STATS_INC(etharp.drop);
00196         goto free_and_return;
00197       } else {
00198         /* pass p to ARP module */
00199         etharp_input(p, netif);
00200       }
00201       break;
00202 #endif /* LWIP_IPV4 && LWIP_ARP */
00203 #if PPPOE_SUPPORT
00204     case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
00205       pppoe_disc_input(netif, p);
00206       break;
00207 
00208     case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
00209       pppoe_data_input(netif, p);
00210       break;
00211 #endif /* PPPOE_SUPPORT */
00212 
00213 #if LWIP_IPV6
00214     case PP_HTONS(ETHTYPE_IPV6): /* IPv6 */
00215       /* skip Ethernet header */
00216       if ((p->len < ip_hdr_offset) || pbuf_header(p, (s16_t)-ip_hdr_offset)) {
00217         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
00218           ("ethernet_input: IPv6 packet dropped, too short (%"S16_F"/%"S16_F")\n",
00219           p->tot_len, ip_hdr_offset));
00220         goto free_and_return;
00221       } else {
00222         /* pass to IPv6 layer */
00223         ip6_input(p, netif);
00224       }
00225       break;
00226 #endif /* LWIP_IPV6 */
00227 
00228     default:
00229 #ifdef LWIP_HOOK_UNKNOWN_ETH_PROTOCOL
00230       if(LWIP_HOOK_UNKNOWN_ETH_PROTOCOL(p, netif) == ERR_OK) {
00231         break;
00232       }
00233 #endif
00234       ETHARP_STATS_INC(etharp.proterr);
00235       ETHARP_STATS_INC(etharp.drop);
00236       MIB2_STATS_NETIF_INC(netif, ifinunknownprotos);
00237       goto free_and_return;
00238   }
00239 
00240   /* This means the pbuf is freed or consumed,
00241      so the caller doesn't have to free it again */
00242   return ERR_OK;
00243 
00244 free_and_return:
00245   pbuf_free(p);
00246   return ERR_OK;
00247 }
00248 
00249 /**
00250  * @ingroup ethernet
00251  * Send an ethernet packet on the network using netif->linkoutput().
00252  * The ethernet header is filled in before sending.
00253  *
00254  * @see LWIP_HOOK_VLAN_SET
00255  *
00256  * @param netif the lwIP network interface on which to send the packet
00257  * @param p the packet to send. pbuf layer must be @ref PBUF_LINK.
00258  * @param src the source MAC address to be copied into the ethernet header
00259  * @param dst the destination MAC address to be copied into the ethernet header
00260  * @param eth_type ethernet type (@ref eth_type)
00261  * @return ERR_OK if the packet was sent, any other err_t on failure
00262  */
00263 err_t
00264 ethernet_output(struct netif* netif, struct pbuf* p,
00265                 const struct eth_addr* src, const struct eth_addr* dst,
00266                 u16_t eth_type)
00267 {
00268   struct eth_hdr* ethhdr;
00269   u16_t eth_type_be = lwip_htons(eth_type);
00270 
00271 #if ETHARP_SUPPORT_VLAN && defined(LWIP_HOOK_VLAN_SET)
00272   s32_t vlan_prio_vid = LWIP_HOOK_VLAN_SET(netif, p, src, dst, eth_type);
00273   if (vlan_prio_vid >= 0) {
00274     struct eth_vlan_hdr* vlanhdr;
00275 
00276     LWIP_ASSERT("prio_vid must be <= 0xFFFF", vlan_prio_vid <= 0xFFFF);
00277 
00278     if (pbuf_header(p, SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) != 0) {
00279       goto pbuf_header_failed;
00280     }
00281     vlanhdr = (struct eth_vlan_hdr*)(((u8_t*)p->payload) + SIZEOF_ETH_HDR);
00282     vlanhdr->tpid     = eth_type_be;
00283     vlanhdr->prio_vid = lwip_htons((u16_t)vlan_prio_vid);
00284 
00285     eth_type_be = PP_HTONS(ETHTYPE_VLAN);
00286   } else
00287 #endif /* ETHARP_SUPPORT_VLAN && defined(LWIP_HOOK_VLAN_SET) */
00288   {
00289     if (pbuf_header(p, SIZEOF_ETH_HDR) != 0) {
00290       goto pbuf_header_failed;
00291     }
00292   }
00293 
00294   ethhdr = (struct eth_hdr*)p->payload;
00295   ethhdr->type = eth_type_be;
00296   ETHADDR32_COPY(&ethhdr->dest, dst);
00297   ETHADDR16_COPY(&ethhdr->src,  src);
00298 
00299   LWIP_ASSERT("netif->hwaddr_len must be 6 for ethernet_output!",
00300     (netif->hwaddr_len == ETH_HWADDR_LEN));
00301   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
00302     ("ethernet_output: sending packet %p\n", (void *)p));
00303 
00304   /* send the packet */
00305   return netif->linkoutput(netif, p);
00306 
00307 pbuf_header_failed:
00308   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
00309     ("ethernet_output: could not allocate room for header.\n"));
00310   LINK_STATS_INC(link.lenerr);
00311   return ERR_BUF;
00312 }
00313 
00314 #endif /* LWIP_ARP || LWIP_ETHERNET */