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