【lwip】07-链路层收发以太网数据帧源码分析( 二 )

  • 多播地址 。
    • 以太网要求多播MAC地址第一个bit(最先发出)为1 。
      • IPV4对应的:01:00:5E:00:00:00 —— 01:00:5E:7F:FF:FF
      • IPV6对应的:33:33:xx:xx:xx:xx
  • 广播地址 。
    • 以太网要求广播地址48bit全为1 。FF:FF:FF:FF:FF:FF
  • 7.5 以太网帧报文数据结构以太网首部数据结构:struct eth_hdr
    /** Ethernet header */struct eth_hdr {#if ETH_PAD_SIZEPACK_STRUCT_FLD_8(u8_t padding[ETH_PAD_SIZE]); /* 以太网帧前的填充字段,使后面数据字段地址和系统对齐 */#endifPACK_STRUCT_FLD_S(struct eth_addr dest); /* 目标MAC地址字段 */PACK_STRUCT_FLD_S(struct eth_addr src); /* 源MAC地址字段 */PACK_STRUCT_FIELD(u16_t type); /* 协议类型字段 */} PACK_STRUCT_STRUCT;7.6 发送以太网数据帧以太网链路层发包使用ethernet_output()函数 。
    主要内容:
    • 填充以太网帧各个字段,如有VLAN , 则VLAN也填充 。
    • 通过链路层发出:netif->linkoutput(netif, p);
    /** * @ingroup ethernet * Send an ethernet packet on the network using netif->linkoutput(). * The ethernet header is filled in before sending. * * @see LWIP_HOOK_VLAN_SET * * @param netif the lwIP network interface on which to send the packet * @param p the packet to send. pbuf layer must be @ref PBUF_LINK. * @param src the source MAC address to be copied into the ethernet header * @param dst the destination MAC address to be copied into the ethernet header * @param eth_type ethernet type (@ref lwip_ieee_eth_type) * @return ERR_OK if the packet was sent, any other err_t on failure */err_tethernet_output(struct netif * netif, struct pbuf * p,const struct eth_addr * src, const struct eth_addr * dst,u16_t eth_type) {struct eth_hdr *ethhdr;u16_t eth_type_be = lwip_htons(eth_type);#if ETHARP_SUPPORT_VLAN && (defined(LWIP_HOOK_VLAN_SET) || LWIP_VLAN_PCP)s32_t vlan_prio_vid;#ifdef LWIP_HOOK_VLAN_SETvlan_prio_vid = LWIP_HOOK_VLAN_SET(netif, p, src, dst, eth_type); /* 使用钩子函数来处理获取VLAN帧的TCI字段 */#elif LWIP_VLAN_PCPvlan_prio_vid = -1;if (netif->hints && (netif->hints->tci >= 0)) {vlan_prio_vid = (u16_t)netif->hints->tci; /* 直接从网卡中获取VLAN帧的TCI字段 */}#endifif (vlan_prio_vid >= 0) { /* 如果需要开启VLAN标签,就需要在以太网帧中组建VLAN字段 */struct eth_vlan_hdr *vlanhdr;LWIP_ASSERT("prio_vid must be <= 0xFFFF", vlan_prio_vid <= 0xFFFF);if (pbuf_add_header(p, SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) != 0) { /* pbuf的payload往前偏移,包含以太网首部(包含VLAN字段) */goto pbuf_header_failed;}vlanhdr = (struct eth_vlan_hdr *)(((u8_t *)p->payload) + SIZEOF_ETH_HDR); /* 这里注意偏移 。了解带VLAN标签的以太网帧报文就知道下面的操作 */vlanhdr->tpid= eth_type_be; /* 以太网的类型字段 */vlanhdr->prio_vid = lwip_htons((u16_t)vlan_prio_vid); /* VLAN标签的TCI字段 */eth_type_be = PP_HTONS(ETHTYPE_VLAN); /* 这里才是VLAN的TPID字段(代码实现的手法) */} else#endif /* ETHARP_SUPPORT_VLAN && (defined(LWIP_HOOK_VLAN_SET) || LWIP_VLAN_PCP) */{if (pbuf_add_header(p, SIZEOF_ETH_HDR) != 0) { /* pbuf的payload往前偏移 , 包含以太网首部 */goto pbuf_header_failed;}}LWIP_ASSERT_CORE_LOCKED(); /* tcpip内核上锁确认 */ethhdr = (struct eth_hdr *)p->payload; /* 指针赋值 */ethhdr->type = eth_type_be; /* 协议类型字段/如果开启了VLAN,这里才是VLAN的TPID字段 */SMEMCPY(&ethhdr->dest, dst, ETH_HWADDR_LEN); /* 目标MAC字段 */SMEMCPY(&ethhdr->src,src, ETH_HWADDR_LEN); /* 源MAC字段 */LWIP_ASSERT("netif->hwaddr_len must be 6 for ethernet_output!",(netif->hwaddr_len == ETH_HWADDR_LEN));LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,("ethernet_output: sending packet %p\n", (void *)p));/* 通过网卡发送以太网帧 */return netif->linkoutput(netif, p);pbuf_header_failed:LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,("ethernet_output: could not allocate room for header.\n"));LINK_STATS_INC(link.lenerr);return ERR_BUF;}7.7 接收以太网数据帧以太网链路层收包使用ethernet_input()函数 。
    该函数主要是根据以太网帧首部的类型字段,把包分发到不同的协议处理 。
    IP数据包丢到:ip_input()
    ARP数据包丢到:etharp_input()
    /** * @ingroup lwip_nosys * Process received ethernet frames. Using this function instead of directly * calling ip_input and passing ARP frames through etharp in ethernetif_input, * the ARP cache is protected from concurrent access.<br> * Don't call directly, pass to netif_add() and call netif->input(). * * @param p the received packet, p->payload pointing to the ethernet header * @param netif the network interface on which the packet was received * * @see LWIP_HOOK_UNKNOWN_ETH_PROTOCOL * @see ETHARP_SUPPORT_VLAN * @see LWIP_HOOK_VLAN_CHECK */err_tethernet_input(struct pbuf *p, struct netif *netif){struct eth_hdr *ethhdr;u16_t type;#if LWIP_ARP || ETHARP_SUPPORT_VLAN || LWIP_IPV6u16_t next_hdr_offset = SIZEOF_ETH_HDR;#endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */LWIP_ASSERT_CORE_LOCKED();if (p->len <= SIZEOF_ETH_HDR) {/* 只有一个以太网报头(或更少)的数据包,不处理 */ETHARP_STATS_INC(etharp.proterr);ETHARP_STATS_INC(etharp.drop);MIB2_STATS_NETIF_INC(netif, ifinerrors);goto free_and_return;}/* 找到以太网首部字段 */ethhdr = (struct eth_hdr *)p->payload;LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,("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",(unsigned char)ethhdr->dest.addr[0], (unsigned char)ethhdr->dest.addr[1], (unsigned char)ethhdr->dest.addr[2],(unsigned char)ethhdr->dest.addr[3], (unsigned char)ethhdr->dest.addr[4], (unsigned char)ethhdr->dest.addr[5],(unsigned char)ethhdr->src.addr[0],(unsigned char)ethhdr->src.addr[1],(unsigned char)ethhdr->src.addr[2],(unsigned char)ethhdr->src.addr[3],(unsigned char)ethhdr->src.addr[4],(unsigned char)ethhdr->src.addr[5],lwip_htons(ethhdr->type)));type = ethhdr->type;#if ETHARP_SUPPORT_VLANif (type == PP_HTONS(ETHTYPE_VLAN)) {struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr *)(((char *)ethhdr) + SIZEOF_ETH_HDR);next_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR; /* 找到下一个协议层的首部 。这里就是以太网帧首部长度 */if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {/* 只有ethernet/vlan报头(或更少)的数据包,不处理 */ETHARP_STATS_INC(etharp.proterr);ETHARP_STATS_INC(etharp.drop);MIB2_STATS_NETIF_INC(netif, ifinerrors);goto free_and_return;}#if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */#ifdef LWIP_HOOK_VLAN_CHECKif (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) { /* 优先使用VLAN钩子函数的过滤 */#elif defined(ETHARP_VLAN_CHECK_FN)if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) { /* ETHARP_VLAN_CHECK_FN函数的过滤 */#elif defined(ETHARP_VLAN_CHECK)if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) { /* 指定接收一个VLAN */#endif/* 静默忽略此报文:不是我们需要的VLAN */pbuf_free(p);return ERR_OK;}#endif /* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */type = vlan->tpid; /* 这个字段其实就是以太网首部的协议类型字段 */}#endif /* ETHARP_SUPPORT_VLAN */#if LWIP_ARP_FILTER_NETIFnetif = LWIP_ARP_FILTER_NETIF_FN(p, netif, lwip_htons(type)); /* 一个硬件映射多个IP 。找到对应的netif */#endif /* LWIP_ARP_FILTER_NETIF*/if (p->if_idx == NETIF_NO_INDEX) {/* pbuf标记对应netif标识 */p->if_idx = netif_get_index(netif);}if (ethhdr->dest.addr[0] & 1) { /* 目标MAC分析 , 首个bit为1,说明是非单播包 *//* 多播或者广播包 */if (ethhdr->dest.addr[0] == LL_IP4_MULTICAST_ADDR_0) { /* 0x01 */#if LWIP_IPV4if ((ethhdr->dest.addr[1] == LL_IP4_MULTICAST_ADDR_1) && /* 0x00 */(ethhdr->dest.addr[2] == LL_IP4_MULTICAST_ADDR_2)) { /* 0x5e *//* 01-00-5e-开头的为IPV4链路层组播包:将pbuf标记为链路层组播 */p->flags |= PBUF_FLAG_LLMCAST;}#endif /* LWIP_IPV4 */}#if LWIP_IPV6else if ((ethhdr->dest.addr[0] == LL_IP6_MULTICAST_ADDR_0) && /* 0x33 */(ethhdr->dest.addr[1] == LL_IP6_MULTICAST_ADDR_1)) { /* 0x33 *//* 33-33-开头的为IPV6链路层组播包:将pbuf标记为链路层组播 */p->flags |= PBUF_FLAG_LLMCAST;}#endif /* LWIP_IPV6 */else if (eth_addr_cmp(&ethhdr->dest, &ethbroadcast)) {/* FF:FF:FF:FF:FF:FF *//* 将pbuf标记为链路层广播 */p->flags |= PBUF_FLAG_LLBCAST;}}switch (type) { /* 以太网帧协议类型字段处理 */#if LWIP_IPV4 && LWIP_ARPcase PP_HTONS(ETHTYPE_IP): /* IPv4数据包 */if (!(netif->flags & NETIF_FLAG_ETHARP)) {/* 如果对应的netif不是以太网设备,那当前数据包不能流入这个netif */goto free_and_return;}/* 链路层处理完毕,pbuf数据区指向跳过以太网首部 , 指向以太网帧数据字段,然后递交给网络层处理 */if (pbuf_remove_header(p, next_hdr_offset)) {LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,("ethernet_input: IPv4 packet dropped, too short (%"U16_F"/%"U16_F")\n",p->tot_len, next_hdr_offset));LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));goto free_and_return;} else {/* 转交给ipv4模块处理 */ip4_input(p, netif);}break;case PP_HTONS(ETHTYPE_ARP): /* ARP数据包 */if (!(netif->flags & NETIF_FLAG_ETHARP)) {/* 如果对应的netif不是以太网设备,那当前数据包不能流入这个netif */goto free_and_return;}/* 链路层处理完毕,pbuf数据区指向跳过以太网首部 , 指向以太网帧数据字段 , 然后递交给网络层处理 */if (pbuf_remove_header(p, next_hdr_offset)) {LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,("ethernet_input: ARP response packet dropped, too short (%"U16_F"/%"U16_F")\n",p->tot_len, next_hdr_offset));LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));ETHARP_STATS_INC(etharp.lenerr);ETHARP_STATS_INC(etharp.drop);goto free_and_return;} else {/* 转交给ARP模块处理 */etharp_input(p, netif);}break;#endif /* LWIP_IPV4 && LWIP_ARP */#if PPPOE_SUPPORTcase PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP以太网上发现阶段的数据包 *//* 转交给PPP分析并处理发现阶段的数据包 */pppoe_disc_input(netif, p);break;case PP_HTONS(ETHTYPE_PPPOE): /* PPP以太网上会话阶段的数据包 *//* 转交给PPP分析并处理会话节点的数据包 */pppoe_data_input(netif, p);break;#endif /* PPPOE_SUPPORT */#if LWIP_IPV6case PP_HTONS(ETHTYPE_IPV6): /* IPv6数据包 *//* 链路层处理完毕 , pbuf数据区指向跳过以太网首部,指向以太网帧数据字段,然后递交给网络层处理 */if ((p->len < next_hdr_offset) || pbuf_remove_header(p, next_hdr_offset)) {LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,("ethernet_input: IPv6 packet dropped, too short (%"U16_F"/%"U16_F")\n",p->tot_len, next_hdr_offset));goto free_and_return;} else {/* 转交给IPV6模块处理 */ip6_input(p, netif);}break;#endif /* LWIP_IPV6 */default:#ifdef LWIP_HOOK_UNKNOWN_ETH_PROTOCOLif (LWIP_HOOK_UNKNOWN_ETH_PROTOCOL(p, netif) == ERR_OK) { /* 其它协议的以太网帧 , 可用钩子函数处理 */break; /* 是用户需要的以太网数据帧 */}#endifETHARP_STATS_INC(etharp.proterr);ETHARP_STATS_INC(etharp.drop);MIB2_STATS_NETIF_INC(netif, ifinunknownprotos);goto free_and_return; /* 本协议栈无法处理的以太网数据?。苯佣?*/}/* 以太网数据帧有效,已经转交处理了 */return ERR_OK;free_and_return:pbuf_free(p);return ERR_OK; /* 以太网数据帧无效,丢弃这个数据包 */}

    推荐阅读