tapif.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD
  7. */
  8. #include <string.h>
  9. #include "lwip/opt.h"
  10. #include "lwip/pbuf.h"
  11. #include "lwip/snmp.h"
  12. #include "lwip/ethip6.h"
  13. #include "netif/etharp.h"
  14. #include "esp_netif.h"
  15. #include "esp_netif_net_stack.h"
  16. #define IFNAME0 't'
  17. #define IFNAME1 'p'
  18. static err_t
  19. low_level_output(struct netif *netif, struct pbuf *p)
  20. {
  21. esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
  22. char buf[1518]; /* max packet size including VLAN excluding CRC */
  23. if (p->tot_len > sizeof(buf)) {
  24. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  25. LWIP_DEBUGF(NETIF_DEBUG, ("tapif: packet too large"));
  26. return ERR_IF;
  27. }
  28. /* initiate transfer(); */
  29. pbuf_copy_partial(p, buf, p->tot_len, 0);
  30. int ret = esp_netif_transmit(esp_netif, buf, p->tot_len);
  31. /* Check error */
  32. if (likely(ret == ESP_OK)) {
  33. return ERR_OK;
  34. }
  35. if (ret == ESP_ERR_NO_MEM) {
  36. return ERR_MEM;
  37. }
  38. return ERR_IF;
  39. }
  40. static void
  41. low_level_init(struct netif *netif)
  42. {
  43. /* Obtain MAC address from network interface. */
  44. netif->hwaddr[0] = 0x02;
  45. netif->hwaddr[1] = 0x12;
  46. netif->hwaddr[2] = 0x34;
  47. netif->hwaddr[3] = 0x56;
  48. netif->hwaddr[4] = 0x78;
  49. netif->hwaddr[5] = 0xab;
  50. netif->hwaddr_len = 6;
  51. /* device capabilities */
  52. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
  53. }
  54. err_t lwip_tapif_init(struct netif *netif)
  55. {
  56. LWIP_ASSERT("Tried to initialize tapif with NULL netif", netif != NULL);
  57. netif->name[0] = IFNAME0;
  58. netif->name[1] = IFNAME1;
  59. #if LWIP_IPV4
  60. netif->output = etharp_output;
  61. #endif /* LWIP_IPV4 */
  62. #if LWIP_IPV6
  63. netif->output_ip6 = ethip6_output;
  64. #endif /* LWIP_IPV6 */
  65. netif->linkoutput = low_level_output;
  66. netif->mtu = 1500;
  67. low_level_init(netif);
  68. netif_set_link_up(netif);
  69. return ERR_OK;
  70. }
  71. void lwip_tapif_input(void *h, void *buffer, size_t len, void *l2_buff)
  72. {
  73. struct netif *netif = h;
  74. struct pbuf *p;
  75. LWIP_ASSERT("running tapif input with NULL netif", netif != NULL);
  76. p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
  77. if (p == NULL) {
  78. return;
  79. }
  80. memcpy(p->payload, buffer, len);
  81. /* full packet send to tcpip_thread to process */
  82. if (unlikely(netif->input(p, netif) != ERR_OK)) {
  83. LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: IP input error\n"));
  84. pbuf_free(p);
  85. }
  86. }