Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_ethernet.c
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/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 #ifdef LWIP_HOOK_FILENAME 00055 #include LWIP_HOOK_FILENAME 00056 #endif 00057 00058 const struct eth_addr ethbroadcast = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; 00059 const struct eth_addr ethzero = {{0, 0, 0, 0, 0, 0}}; 00060 00061 /** 00062 * @ingroup lwip_nosys 00063 * Process received ethernet frames. Using this function instead of directly 00064 * calling ip_input and passing ARP frames through etharp in ethernetif_input, 00065 * the ARP cache is protected from concurrent access.\n 00066 * Don't call directly, pass to netif_add() and call netif->input(). 00067 * 00068 * @param p the received packet, p->payload pointing to the ethernet header 00069 * @param netif the network interface on which the packet was received 00070 * 00071 * @see LWIP_HOOK_UNKNOWN_ETH_PROTOCOL 00072 * @see ETHARP_SUPPORT_VLAN 00073 * @see LWIP_HOOK_VLAN_CHECK 00074 */ 00075 err_t 00076 ethernet_input(struct pbuf *p, struct netif *netif) 00077 { 00078 struct eth_hdr *ethhdr; 00079 u16_t type; 00080 #if LWIP_ARP || ETHARP_SUPPORT_VLAN || LWIP_IPV6 00081 u16_t next_hdr_offset = SIZEOF_ETH_HDR; 00082 #endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */ 00083 00084 LWIP_ASSERT_CORE_LOCKED(); 00085 00086 if (p->len <= SIZEOF_ETH_HDR) { 00087 /* a packet with only an ethernet header (or less) is not valid for us */ 00088 ETHARP_STATS_INC(etharp.proterr); 00089 ETHARP_STATS_INC(etharp.drop); 00090 MIB2_STATS_NETIF_INC(netif, ifinerrors); 00091 goto free_and_return; 00092 } 00093 00094 if (p->if_idx == NETIF_NO_INDEX) { 00095 p->if_idx = netif_get_index(netif); 00096 } 00097 00098 /* points to packet payload, which starts with an Ethernet header */ 00099 ethhdr = (struct eth_hdr *)p->payload; 00100 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, 00101 ("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", 00102 (unsigned char)ethhdr->dest.addr[0], (unsigned char)ethhdr->dest.addr[1], (unsigned char)ethhdr->dest.addr[2], 00103 (unsigned char)ethhdr->dest.addr[3], (unsigned char)ethhdr->dest.addr[4], (unsigned char)ethhdr->dest.addr[5], 00104 (unsigned char)ethhdr->src.addr[0], (unsigned char)ethhdr->src.addr[1], (unsigned char)ethhdr->src.addr[2], 00105 (unsigned char)ethhdr->src.addr[3], (unsigned char)ethhdr->src.addr[4], (unsigned char)ethhdr->src.addr[5], 00106 lwip_htons(ethhdr->type))); 00107 00108 type = ethhdr->type; 00109 #if ETHARP_SUPPORT_VLAN 00110 if (type == PP_HTONS(ETHTYPE_VLAN)) { 00111 struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr *)(((char *)ethhdr) + SIZEOF_ETH_HDR); 00112 next_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR; 00113 if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) { 00114 /* a packet with only an ethernet/vlan header (or less) is not valid for us */ 00115 ETHARP_STATS_INC(etharp.proterr); 00116 ETHARP_STATS_INC(etharp.drop); 00117 MIB2_STATS_NETIF_INC(netif, ifinerrors); 00118 goto free_and_return; 00119 } 00120 #if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */ 00121 #ifdef LWIP_HOOK_VLAN_CHECK 00122 if (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) { 00123 #elif defined(ETHARP_VLAN_CHECK_FN) 00124 if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) { 00125 #elif defined(ETHARP_VLAN_CHECK) 00126 if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) { 00127 #endif 00128 /* silently ignore this packet: not for our VLAN */ 00129 pbuf_free(p); 00130 return ERR_OK; 00131 } 00132 #endif /* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */ 00133 type = vlan->tpid; 00134 } 00135 #endif /* ETHARP_SUPPORT_VLAN */ 00136 00137 #if LWIP_ARP_FILTER_NETIF 00138 netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, lwip_htons(type)); 00139 #endif /* LWIP_ARP_FILTER_NETIF*/ 00140 00141 if (ethhdr->dest.addr[0] & 1) { 00142 /* this might be a multicast or broadcast packet */ 00143 if (ethhdr->dest.addr[0] == LL_IP4_MULTICAST_ADDR_0) { 00144 #if LWIP_IPV4 00145 if ((ethhdr->dest.addr[1] == LL_IP4_MULTICAST_ADDR_1) && 00146 (ethhdr->dest.addr[2] == LL_IP4_MULTICAST_ADDR_2)) { 00147 /* mark the pbuf as link-layer multicast */ 00148 p->flags |= PBUF_FLAG_LLMCAST; 00149 } 00150 #endif /* LWIP_IPV4 */ 00151 } 00152 #if LWIP_IPV6 00153 else if ((ethhdr->dest.addr[0] == LL_IP6_MULTICAST_ADDR_0) && 00154 (ethhdr->dest.addr[1] == LL_IP6_MULTICAST_ADDR_1)) { 00155 /* mark the pbuf as link-layer multicast */ 00156 p->flags |= PBUF_FLAG_LLMCAST; 00157 } 00158 #endif /* LWIP_IPV6 */ 00159 else if (eth_addr_cmp(ðhdr->dest, ðbroadcast)) { 00160 /* mark the pbuf as link-layer broadcast */ 00161 p->flags |= PBUF_FLAG_LLBCAST; 00162 } 00163 } 00164 00165 switch (type) { 00166 #if LWIP_IPV4 && LWIP_ARP 00167 /* IP packet? */ 00168 case PP_HTONS(ETHTYPE_IP): 00169 if (!(netif->flags & NETIF_FLAG_ETHARP)) { 00170 goto free_and_return; 00171 } 00172 /* skip Ethernet header (min. size checked above) */ 00173 if (pbuf_remove_header(p, next_hdr_offset)) { 00174 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, 00175 ("ethernet_input: IPv4 packet dropped, too short (%"U16_F"/%"U16_F")\n", 00176 p->tot_len, next_hdr_offset)); 00177 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet")); 00178 goto free_and_return; 00179 } else { 00180 /* pass to IP layer */ 00181 ip4_input(p, netif); 00182 } 00183 break; 00184 00185 case PP_HTONS(ETHTYPE_ARP): 00186 if (!(netif->flags & NETIF_FLAG_ETHARP)) { 00187 goto free_and_return; 00188 } 00189 /* skip Ethernet header (min. size checked above) */ 00190 if (pbuf_remove_header(p, next_hdr_offset)) { 00191 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, 00192 ("ethernet_input: ARP response packet dropped, too short (%"U16_F"/%"U16_F")\n", 00193 p->tot_len, next_hdr_offset)); 00194 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet")); 00195 ETHARP_STATS_INC(etharp.lenerr); 00196 ETHARP_STATS_INC(etharp.drop); 00197 goto free_and_return; 00198 } else { 00199 /* pass p to ARP module */ 00200 etharp_input(p, netif); 00201 } 00202 break; 00203 #endif /* LWIP_IPV4 && LWIP_ARP */ 00204 #if PPPOE_SUPPORT 00205 case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */ 00206 pppoe_disc_input(netif, p); 00207 break; 00208 00209 case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */ 00210 pppoe_data_input(netif, p); 00211 break; 00212 #endif /* PPPOE_SUPPORT */ 00213 00214 #if LWIP_IPV6 00215 case PP_HTONS(ETHTYPE_IPV6): /* IPv6 */ 00216 /* skip Ethernet header */ 00217 if ((p->len < next_hdr_offset) || pbuf_remove_header(p, next_hdr_offset)) { 00218 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, 00219 ("ethernet_input: IPv6 packet dropped, too short (%"U16_F"/%"U16_F")\n", 00220 p->tot_len, next_hdr_offset)); 00221 goto free_and_return; 00222 } else { 00223 /* pass to IPv6 layer */ 00224 ip6_input(p, netif); 00225 } 00226 break; 00227 #endif /* LWIP_IPV6 */ 00228 00229 default: 00230 #ifdef LWIP_HOOK_UNKNOWN_ETH_PROTOCOL 00231 if (LWIP_HOOK_UNKNOWN_ETH_PROTOCOL(p, netif) == ERR_OK) { 00232 break; 00233 } 00234 #endif 00235 ETHARP_STATS_INC(etharp.proterr); 00236 ETHARP_STATS_INC(etharp.drop); 00237 MIB2_STATS_NETIF_INC(netif, ifinunknownprotos); 00238 goto free_and_return; 00239 } 00240 00241 /* This means the pbuf is freed or consumed, 00242 so the caller doesn't have to free it again */ 00243 return ERR_OK; 00244 00245 free_and_return: 00246 pbuf_free(p); 00247 return ERR_OK; 00248 } 00249 00250 /** 00251 * @ingroup ethernet 00252 * Send an ethernet packet on the network using netif->linkoutput(). 00253 * The ethernet header is filled in before sending. 00254 * 00255 * @see LWIP_HOOK_VLAN_SET 00256 * 00257 * @param netif the lwIP network interface on which to send the packet 00258 * @param p the packet to send. pbuf layer must be @ref PBUF_LINK. 00259 * @param src the source MAC address to be copied into the ethernet header 00260 * @param dst the destination MAC address to be copied into the ethernet header 00261 * @param eth_type ethernet type (@ref lwip_ieee_eth_type) 00262 * @return ERR_OK if the packet was sent, any other err_t on failure 00263 */ 00264 err_t 00265 ethernet_output(struct netif * netif, struct pbuf * p, 00266 const struct eth_addr * src, const struct eth_addr * dst, 00267 u16_t eth_type) { 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_add_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_add_header(p, SIZEOF_ETH_HDR) != 0) { 00290 goto pbuf_header_failed; 00291 } 00292 } 00293 00294 LWIP_ASSERT_CORE_LOCKED(); 00295 00296 ethhdr = (struct eth_hdr *)p->payload; 00297 ethhdr->type = eth_type_be; 00298 SMEMCPY(ðhdr->dest, dst, ETH_HWADDR_LEN); 00299 SMEMCPY(ðhdr->src, src, ETH_HWADDR_LEN); 00300 00301 LWIP_ASSERT("netif->hwaddr_len must be 6 for ethernet_output!", 00302 (netif->hwaddr_len == ETH_HWADDR_LEN)); 00303 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, 00304 ("ethernet_output: sending packet %p\n", (void *)p)); 00305 00306 /* send the packet */ 00307 return netif->linkoutput(netif, p); 00308 00309 pbuf_header_failed: 00310 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, 00311 ("ethernet_output: could not allocate room for header.\n")); 00312 LINK_STATS_INC(link.lenerr); 00313 return ERR_BUF; 00314 } 00315 00316 #endif /* LWIP_ARP || LWIP_ETHERNET */
Generated on Tue Jul 12 2022 13:54:28 by
1.7.2