whd_network.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Sample code used for reference only
  3. */
  4. /**
  5. * This function should be called when a packet is ready to be read
  6. * from the interface. It uses the function low_level_input() that
  7. * should handle the actual reception of bytes from the network
  8. * interface. Then the type of the received packet is determined and
  9. * the appropriate input function is called.
  10. *
  11. * @param buffer : the incoming ethernet packet
  12. */
  13. void host_network_process_ethernet_data(whd_interface_t ifp, /*@only@*/ whd_buffer_t buffer)
  14. {
  15. struct eth_hdr *ethernet_header;
  16. struct netif *tmp_netif;
  17. u8_t result;
  18. uint16_t ethertype;
  19. struct pbuf *lwip_buffer = (struct pbuf *)buffer;
  20. if (buffer == NULL)
  21. return;
  22. /* points to packet payload, which starts with an Ethernet header */
  23. ethernet_header = (struct eth_hdr *)lwip_buffer->payload;
  24. ethertype = htons(ethernet_header->type);
  25. /* Check if this is an 802.1Q VLAN tagged packet */
  26. if (ethertype == WHD_ETHERTYPE_8021Q)
  27. {
  28. /* Need to remove the 4 octet VLAN Tag, by moving src and dest addresses 4 octets to the right,
  29. * and then read the actual ethertype. The VLAN ID and priority fields are currently ignored. */
  30. uint8_t temp_buffer[12];
  31. memcpy(temp_buffer, lwip_buffer->payload, 12);
  32. memcpy( ( (uint8_t *)lwip_buffer->payload ) + 4, temp_buffer, 12 );
  33. lwip_buffer->payload = ( (uint8_t *)lwip_buffer->payload ) + 4;
  34. lwip_buffer->len = (u16_t)(lwip_buffer->len - 4);
  35. ethernet_header = (struct eth_hdr *)lwip_buffer->payload;
  36. ethertype = htons(ethernet_header->type);
  37. }
  38. switch (ethertype)
  39. {
  40. case WHD_ETHERTYPE_IPv4:
  41. /* Find the netif object matching the provided interface */
  42. for (tmp_netif = netif_list; (tmp_netif != NULL) && (tmp_netif->state != (void *)ifp);
  43. tmp_netif = tmp_netif->next)
  44. {
  45. }
  46. if (tmp_netif == NULL)
  47. {
  48. /* Received a packet for a network interface is not initialised Cannot do anything with packet - just drop it. */
  49. result = pbuf_free(lwip_buffer);
  50. LWIP_ASSERT("Failed to release packet buffer", (result != (u8_t)0) );
  51. lwip_buffer = NULL;
  52. return;
  53. }
  54. /* Send to packet to tcpip_thread to process */
  55. if (tcpip_input(lwip_buffer, tmp_netif) != ERR_OK)
  56. {
  57. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n") );
  58. /* Stop lint warning - packet has not been released in this case */ /*@-usereleased@*/
  59. result = pbuf_free(lwip_buffer);
  60. /*@+usereleased@*/
  61. LWIP_ASSERT("Failed to release packet buffer", (result != (u8_t)0) );
  62. lwip_buffer = NULL;
  63. }
  64. break;
  65. case WHD_ETHERTYPE_EAPOL:
  66. /* Process EAPOL packet */
  67. break;
  68. default:
  69. result = pbuf_free(lwip_buffer);
  70. LWIP_ASSERT("Failed to release packet buffer", (result != (u8_t)0) );
  71. lwip_buffer = NULL;
  72. break;
  73. }
  74. }
  75. whd_netif_funcs_t netif_ops =
  76. {
  77. .whd_network_process_ethernet_data = host_network_process_ethernet_data,
  78. };