vlan_hooks.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "lwip/netif.h"
  7. #include "lwip/pbuf.h"
  8. #include "lwip/prot/ethernet.h"
  9. /**
  10. * @brief Returns the custom data stored in the supplied `netif` struct representing its vlan tag
  11. *
  12. * @param netif The lwIP network interface on which to send the packet
  13. * @param p The packet to send. pbuf layer must be @ref PBUF_LINK.
  14. * @param src The source MAC address to be copied into the ethernet header
  15. * @param dst The destination MAC address to be copied into the ethernet header
  16. * @param eth_type Ethernet type (@ref lwip_ieee_eth_type)
  17. *
  18. * @return Returns the vlan tag of the interface or -1.
  19. */
  20. static inline int lwip_vlan_set(struct netif *netif, struct pbuf *p,
  21. const struct eth_addr *src, const struct eth_addr *dst,
  22. u16_t eth_type)
  23. {
  24. u16_t vlan_id = *((uint16_t *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MAX + 1));
  25. if (0xFFF == vlan_id) {
  26. return -1;
  27. } else {
  28. return vlan_id;
  29. }
  30. }
  31. /**
  32. * @brief Checks if the vlan tag in the frame matches the vlan tag of the interface.
  33. *
  34. * @param netif: struct netif on which the packet has been received
  35. * @param eth_hdr: struct eth_hdr of the packet
  36. * @param vlan_hdr: struct eth_vlan_hdr of the packet
  37. *
  38. * @return Returns true if the vlan tag in the frame matches the vlan tag of the interface, else returns false.
  39. */
  40. static inline bool lwip_vlan_check(struct netif *netif, struct eth_hdr *eth_hdr, struct eth_vlan_hdr *vlan_hdr)
  41. {
  42. u16_t vlan_id = *((uint16_t *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MAX + 1));
  43. if (vlan_id == VLAN_ID(vlan_hdr)) {
  44. return true;
  45. }
  46. return false;
  47. }