mdns_networking_lwip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * MDNS Server Networking
  8. *
  9. */
  10. #include <string.h>
  11. #include "esp_log.h"
  12. #include "lwip/ip_addr.h"
  13. #include "lwip/pbuf.h"
  14. #include "lwip/igmp.h"
  15. #include "lwip/udp.h"
  16. #include "lwip/mld6.h"
  17. #include "lwip/priv/tcpip_priv.h"
  18. #include "esp_system.h"
  19. #include "esp_event.h"
  20. #include "mdns_networking.h"
  21. #include "esp_netif_net_stack.h"
  22. /*
  23. * MDNS Server Networking
  24. *
  25. */
  26. enum interface_protocol {
  27. PROTO_IPV4 = 1 << MDNS_IP_PROTOCOL_V4,
  28. PROTO_IPV6 = 1 << MDNS_IP_PROTOCOL_V6
  29. };
  30. typedef struct interfaces {
  31. bool ready;
  32. int proto;
  33. } interfaces_t;
  34. static interfaces_t s_interfaces[MDNS_MAX_INTERFACES];
  35. static struct udp_pcb *_pcb_main = NULL;
  36. static const char *TAG = "mdns_networking";
  37. static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport);
  38. /**
  39. * @brief Low level UDP PCB Initialize
  40. */
  41. static esp_err_t _udp_pcb_main_init(void)
  42. {
  43. if (_pcb_main) {
  44. return ESP_OK;
  45. }
  46. _pcb_main = udp_new();
  47. if (!_pcb_main) {
  48. return ESP_ERR_NO_MEM;
  49. }
  50. if (udp_bind(_pcb_main, IP_ANY_TYPE, MDNS_SERVICE_PORT) != 0) {
  51. udp_remove(_pcb_main);
  52. _pcb_main = NULL;
  53. return ESP_ERR_INVALID_STATE;
  54. }
  55. _pcb_main->mcast_ttl = 255;
  56. _pcb_main->remote_port = MDNS_SERVICE_PORT;
  57. ip_addr_copy(_pcb_main->remote_ip, *(IP_ANY_TYPE));
  58. udp_recv(_pcb_main, &_udp_recv, NULL);
  59. return ESP_OK;
  60. }
  61. /**
  62. * @brief Low level UDP PCB Free
  63. */
  64. static void _udp_pcb_main_deinit(void)
  65. {
  66. if (_pcb_main) {
  67. udp_recv(_pcb_main, NULL, NULL);
  68. udp_disconnect(_pcb_main);
  69. udp_remove(_pcb_main);
  70. _pcb_main = NULL;
  71. }
  72. }
  73. /**
  74. * @brief Low level UDP Multicast membership control
  75. */
  76. static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protocol, bool join)
  77. {
  78. struct netif *netif = NULL;
  79. esp_netif_t *tcpip_if = _mdns_get_esp_netif(if_inx);
  80. if (!esp_netif_is_netif_up(tcpip_if)) {
  81. // Network interface went down before event propagated, skipping IGMP config
  82. return ESP_ERR_INVALID_STATE;
  83. }
  84. netif = esp_netif_get_netif_impl(tcpip_if);
  85. assert(netif);
  86. #if LWIP_IPV4
  87. if (ip_protocol == MDNS_IP_PROTOCOL_V4) {
  88. ip4_addr_t multicast_addr;
  89. IP4_ADDR(&multicast_addr, 224, 0, 0, 251);
  90. if (join) {
  91. if (igmp_joingroup_netif(netif, &multicast_addr)) {
  92. return ESP_ERR_INVALID_STATE;
  93. }
  94. } else {
  95. if (igmp_leavegroup_netif(netif, &multicast_addr)) {
  96. return ESP_ERR_INVALID_STATE;
  97. }
  98. }
  99. }
  100. #endif // LWIP_IPV4
  101. #if LWIP_IPV6
  102. if (ip_protocol == MDNS_IP_PROTOCOL_V6) {
  103. ip_addr_t multicast_addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000);
  104. if (join) {
  105. if (mld6_joingroup_netif(netif, ip_2_ip6(&multicast_addr))) {
  106. return ESP_ERR_INVALID_STATE;
  107. }
  108. } else {
  109. if (mld6_leavegroup_netif(netif, ip_2_ip6(&multicast_addr))) {
  110. return ESP_ERR_INVALID_STATE;
  111. }
  112. }
  113. }
  114. #endif // LWIP_IPV6
  115. return ESP_OK;
  116. }
  117. /**
  118. * @brief the receive callback of the raw udp api. Packets are received here
  119. *
  120. */
  121. static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport)
  122. {
  123. uint8_t i;
  124. while (pb != NULL) {
  125. struct pbuf *this_pb = pb;
  126. pb = pb->next;
  127. this_pb->next = NULL;
  128. mdns_rx_packet_t *packet = (mdns_rx_packet_t *)malloc(sizeof(mdns_rx_packet_t));
  129. if (!packet) {
  130. HOOK_MALLOC_FAILED;
  131. //missed packet - no memory
  132. pbuf_free(this_pb);
  133. continue;
  134. }
  135. packet->tcpip_if = MDNS_MAX_INTERFACES;
  136. packet->pb = this_pb;
  137. packet->src_port = rport;
  138. #if LWIP_IPV4 && LWIP_IPV6
  139. packet->src.type = raddr->type;
  140. memcpy(&packet->src.u_addr, &raddr->u_addr, sizeof(raddr->u_addr));
  141. #elif LWIP_IPV4
  142. packet->src.type = IPADDR_TYPE_V4;
  143. packet->src.u_addr.ip4.addr = raddr->addr;
  144. #elif LWIP_IPV6
  145. packet->src.type = IPADDR_TYPE_V6;
  146. memcpy(&packet->src.u_addr.ip6, raddr, sizeof(ip_addr_t));
  147. #endif
  148. packet->dest.type = packet->src.type;
  149. #if LWIP_IPV4
  150. if (packet->src.type == IPADDR_TYPE_V4) {
  151. packet->ip_protocol = MDNS_IP_PROTOCOL_V4;
  152. struct ip_hdr *iphdr = (struct ip_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP_HLEN);
  153. packet->dest.u_addr.ip4.addr = iphdr->dest.addr;
  154. packet->multicast = ip4_addr_ismulticast(&(packet->dest.u_addr.ip4));
  155. }
  156. #endif // LWIP_IPV4
  157. #if LWIP_IPV6
  158. if (packet->src.type == IPADDR_TYPE_V6) {
  159. packet->ip_protocol = MDNS_IP_PROTOCOL_V6;
  160. struct ip6_hdr *ip6hdr = (struct ip6_hdr *)(((uint8_t *)(packet->pb->payload)) - UDP_HLEN - IP6_HLEN);
  161. memcpy(&packet->dest.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
  162. packet->multicast = ip6_addr_ismulticast(&(packet->dest.u_addr.ip6));
  163. }
  164. #endif // LWIP_IPV6
  165. //lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
  166. struct netif *netif = NULL;
  167. bool found = false;
  168. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  169. netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
  170. if (s_interfaces[i].proto && netif && netif == ip_current_input_netif ()) {
  171. #if LWIP_IPV4
  172. if (packet->src.type == IPADDR_TYPE_V4) {
  173. if ((packet->src.u_addr.ip4.addr & ip_2_ip4(&netif->netmask)->addr) != (ip_2_ip4(&netif->ip_addr)->addr & ip_2_ip4(&netif->netmask)->addr)) {
  174. //packet source is not in the same subnet
  175. break;
  176. }
  177. }
  178. #endif // LWIP_IPV4
  179. packet->tcpip_if = i;
  180. found = true;
  181. break;
  182. }
  183. }
  184. if (!found || _mdns_send_rx_action(packet) != ESP_OK) {
  185. pbuf_free(this_pb);
  186. free(packet);
  187. }
  188. }
  189. }
  190. bool mdns_is_netif_ready(mdns_if_t netif, mdns_ip_protocol_t ip_proto)
  191. {
  192. return s_interfaces[netif].ready &&
  193. s_interfaces[netif].proto & (ip_proto == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6);
  194. }
  195. /**
  196. * @brief Check if any of the interfaces is up
  197. */
  198. static bool _udp_pcb_is_in_use(void)
  199. {
  200. int i, p;
  201. for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
  202. for (p = 0; p < MDNS_IP_PROTOCOL_MAX; p++) {
  203. if (mdns_is_netif_ready(i, p)) {
  204. return true;
  205. }
  206. }
  207. }
  208. return false;
  209. }
  210. /**
  211. * @brief Stop PCB Main code
  212. */
  213. static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  214. {
  215. s_interfaces[tcpip_if].proto &= ~(ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6);
  216. if (s_interfaces[tcpip_if].proto == 0) {
  217. s_interfaces[tcpip_if].ready = false;
  218. _udp_join_group(tcpip_if, ip_protocol, false);
  219. if (!_udp_pcb_is_in_use()) {
  220. _udp_pcb_main_deinit();
  221. }
  222. }
  223. }
  224. /**
  225. * @brief Start PCB Main code
  226. */
  227. static esp_err_t _udp_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  228. {
  229. if (mdns_is_netif_ready(tcpip_if, ip_protocol)) {
  230. return ESP_ERR_INVALID_STATE;
  231. }
  232. esp_err_t err = _udp_join_group(tcpip_if, ip_protocol, true);
  233. if (err) {
  234. return err;
  235. }
  236. err = _udp_pcb_main_init();
  237. if (err) {
  238. return err;
  239. }
  240. s_interfaces[tcpip_if].proto |= (ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6);
  241. s_interfaces[tcpip_if].ready = true;
  242. return ESP_OK;
  243. }
  244. typedef struct {
  245. struct tcpip_api_call_data call;
  246. mdns_if_t tcpip_if;
  247. mdns_ip_protocol_t ip_protocol;
  248. struct pbuf *pbt;
  249. const ip_addr_t *ip;
  250. uint16_t port;
  251. esp_err_t err;
  252. } mdns_api_call_t;
  253. /**
  254. * @brief Start PCB from LwIP thread
  255. */
  256. static err_t _mdns_pcb_init_api(struct tcpip_api_call_data *api_call_msg)
  257. {
  258. mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
  259. msg->err = _udp_pcb_init(msg->tcpip_if, msg->ip_protocol) == ESP_OK ? ERR_OK : ERR_IF;
  260. return msg->err;
  261. }
  262. /**
  263. * @brief Stop PCB from LwIP thread
  264. */
  265. static err_t _mdns_pcb_deinit_api(struct tcpip_api_call_data *api_call_msg)
  266. {
  267. mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
  268. _udp_pcb_deinit(msg->tcpip_if, msg->ip_protocol);
  269. msg->err = ESP_OK;
  270. return ESP_OK;
  271. }
  272. /*
  273. * Non-static functions below are
  274. * - _mdns prefixed
  275. * - commented in mdns_networking.h header
  276. */
  277. esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  278. {
  279. mdns_api_call_t msg = {
  280. .tcpip_if = tcpip_if,
  281. .ip_protocol = ip_protocol
  282. };
  283. tcpip_api_call(_mdns_pcb_init_api, &msg.call);
  284. return msg.err;
  285. }
  286. esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
  287. {
  288. mdns_api_call_t msg = {
  289. .tcpip_if = tcpip_if,
  290. .ip_protocol = ip_protocol
  291. };
  292. tcpip_api_call(_mdns_pcb_deinit_api, &msg.call);
  293. return msg.err;
  294. }
  295. static err_t _mdns_udp_pcb_write_api(struct tcpip_api_call_data *api_call_msg)
  296. {
  297. void *nif = NULL;
  298. mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
  299. nif = esp_netif_get_netif_impl(_mdns_get_esp_netif(msg->tcpip_if));
  300. if (!nif || !mdns_is_netif_ready(msg->tcpip_if, msg->ip_protocol) || _pcb_main == NULL) {
  301. pbuf_free(msg->pbt);
  302. msg->err = ERR_IF;
  303. return ERR_IF;
  304. }
  305. esp_err_t err = udp_sendto_if (_pcb_main, msg->pbt, msg->ip, msg->port, (struct netif *)nif);
  306. pbuf_free(msg->pbt);
  307. msg->err = err;
  308. return err;
  309. }
  310. size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len)
  311. {
  312. struct pbuf *pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
  313. if (pbt == NULL) {
  314. return 0;
  315. }
  316. memcpy((uint8_t *)pbt->payload, data, len);
  317. ip_addr_t ip_add_copy;
  318. #if LWIP_IPV6 && LWIP_IPV4
  319. ip_add_copy.type = ip->type;
  320. memcpy(&(ip_add_copy.u_addr), &(ip->u_addr), sizeof(ip_add_copy.u_addr));
  321. #elif LWIP_IPV4
  322. ip_add_copy.addr = ip->u_addr.ip4.addr;
  323. #elif LWIP_IPV6
  324. #if LWIP_IPV6_SCOPES
  325. ip_add_copy.zone = ip->u_addr.ip6.zone;
  326. #endif // LWIP_IPV6_SCOPES
  327. memcpy(ip_add_copy.addr, ip->u_addr.ip6.addr, sizeof(ip_add_copy.addr));
  328. #endif
  329. mdns_api_call_t msg = {
  330. .tcpip_if = tcpip_if,
  331. .ip_protocol = ip_protocol,
  332. .pbt = pbt,
  333. .ip = &ip_add_copy,
  334. .port = port
  335. };
  336. tcpip_api_call(_mdns_udp_pcb_write_api, &msg.call);
  337. if (msg.err) {
  338. return 0;
  339. }
  340. return len;
  341. }
  342. void *_mdns_get_packet_data(mdns_rx_packet_t *packet)
  343. {
  344. return packet->pb->payload;
  345. }
  346. size_t _mdns_get_packet_len(mdns_rx_packet_t *packet)
  347. {
  348. return packet->pb->len;
  349. }
  350. void _mdns_packet_free(mdns_rx_packet_t *packet)
  351. {
  352. pbuf_free(packet->pb);
  353. free(packet);
  354. }