udp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /**
  2. * @file
  3. * User Datagram Protocol module
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. /* udp.c
  38. *
  39. * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).
  40. *
  41. */
  42. /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
  43. */
  44. #include "lwip/opt.h"
  45. #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
  46. #include "lwip/udp.h"
  47. #include "lwip/def.h"
  48. #include "lwip/memp.h"
  49. #include "lwip/inet_chksum.h"
  50. #include "lwip/ip_addr.h"
  51. #include "lwip/netif.h"
  52. #include "lwip/icmp.h"
  53. #include "lwip/stats.h"
  54. #include "lwip/snmp.h"
  55. #include "arch/perf.h"
  56. #include "lwip/dhcp.h"
  57. #include <string.h>
  58. #ifdef MEMLEAK_DEBUG
  59. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  60. #endif
  61. /* The list of UDP PCBs */
  62. /* exported in udp.h (was static) */
  63. struct udp_pcb *udp_pcbs;
  64. /**
  65. * Process an incoming UDP datagram.
  66. *
  67. * Given an incoming UDP datagram (as a chain of pbufs) this function
  68. * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
  69. * recv function. If no pcb is found or the datagram is incorrect, the
  70. * pbuf is freed.
  71. *
  72. * @param p pbuf to be demultiplexed to a UDP PCB.
  73. * @param inp network interface on which the datagram was received.
  74. *
  75. */
  76. void ICACHE_FLASH_ATTR
  77. udp_input(struct pbuf *p, struct netif *inp)
  78. {
  79. struct udp_hdr *udphdr;
  80. struct udp_pcb *pcb, *prev;
  81. struct udp_pcb *uncon_pcb;
  82. struct ip_hdr *iphdr;
  83. u16_t src, dest;
  84. u8_t local_match;
  85. u8_t broadcast;
  86. PERF_START;
  87. UDP_STATS_INC(udp.recv);
  88. iphdr = (struct ip_hdr *)p->payload;
  89. /* Check minimum length (IP header + UDP header)
  90. * and move payload pointer to UDP header */
  91. if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) {
  92. /* drop short packets */
  93. LWIP_DEBUGF(UDP_DEBUG,
  94. ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
  95. UDP_STATS_INC(udp.lenerr);
  96. UDP_STATS_INC(udp.drop);
  97. snmp_inc_udpinerrors();
  98. pbuf_free(p);
  99. goto end;
  100. }
  101. udphdr = (struct udp_hdr *)p->payload;
  102. /* is broadcast packet ? */
  103. broadcast = ip_addr_isbroadcast(&current_iphdr_dest, inp);
  104. LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
  105. /* convert src and dest ports to host byte order */
  106. src = ntohs(udphdr->src);
  107. dest = ntohs(udphdr->dest);
  108. udp_debug_print(udphdr);
  109. /* print the UDP source and destination */
  110. LWIP_DEBUGF(UDP_DEBUG,
  111. ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
  112. "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
  113. ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest),
  114. ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest),
  115. ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src),
  116. ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src)));
  117. #if LWIP_DHCP
  118. pcb = NULL;
  119. /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
  120. the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
  121. if (dest == DHCP_CLIENT_PORT) {
  122. /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
  123. if (src == DHCP_SERVER_PORT) {
  124. if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
  125. /* accept the packe if
  126. (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
  127. - inp->dhcp->pcb->remote == ANY or iphdr->src */
  128. if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
  129. ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &current_iphdr_src))) {
  130. pcb = inp->dhcp->pcb;
  131. }
  132. }
  133. }
  134. } else if (dest == DHCP_SERVER_PORT) {
  135. if (src == DHCP_CLIENT_PORT) {
  136. if ( inp->dhcps_pcb != NULL ) {
  137. if ((ip_addr_isany(&inp->dhcps_pcb->local_ip) ||
  138. ip_addr_cmp(&(inp->dhcps_pcb->local_ip), &current_iphdr_dest))) {
  139. pcb = inp->dhcps_pcb;
  140. }
  141. }
  142. }
  143. } else
  144. #endif /* LWIP_DHCP */
  145. {
  146. prev = NULL;
  147. local_match = 0;
  148. uncon_pcb = NULL;
  149. /* Iterate through the UDP pcb list for a matching pcb.
  150. * 'Perfect match' pcbs (connected to the remote port & ip address) are
  151. * preferred. If no perfect match is found, the first unconnected pcb that
  152. * matches the local port and ip address gets the datagram. */
  153. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
  154. local_match = 0;
  155. /* print the PCB local and remote address */
  156. LWIP_DEBUGF(UDP_DEBUG,
  157. ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
  158. "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
  159. ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
  160. ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port,
  161. ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
  162. ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port));
  163. /* compare PCB local addr+port to UDP destination addr+port */
  164. if ((pcb->local_port == dest) &&
  165. ((!broadcast && ip_addr_isany(&pcb->local_ip)) ||
  166. ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest) ||
  167. #if LWIP_IGMP
  168. ip_addr_ismulticast(&current_iphdr_dest) ||
  169. #endif /* LWIP_IGMP */
  170. #if IP_SOF_BROADCAST_RECV
  171. (broadcast && (pcb->so_options & SOF_BROADCAST)))) {
  172. #else /* IP_SOF_BROADCAST_RECV */
  173. (broadcast))) {
  174. #endif /* IP_SOF_BROADCAST_RECV */
  175. local_match = 1;
  176. if ((uncon_pcb == NULL) &&
  177. ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
  178. /* the first unconnected matching PCB */
  179. uncon_pcb = pcb;
  180. }
  181. }
  182. /* compare PCB remote addr+port to UDP source addr+port */
  183. if ((local_match != 0) &&
  184. (pcb->remote_port == src) &&
  185. (ip_addr_isany(&pcb->remote_ip) ||
  186. ip_addr_cmp(&(pcb->remote_ip), &current_iphdr_src))) {
  187. /* the first fully matching PCB */
  188. if (prev != NULL) {
  189. /* move the pcb to the front of udp_pcbs so that is
  190. found faster next time */
  191. prev->next = pcb->next;
  192. pcb->next = udp_pcbs;
  193. udp_pcbs = pcb;
  194. } else {
  195. UDP_STATS_INC(udp.cachehit);
  196. }
  197. break;
  198. }
  199. prev = pcb;
  200. }
  201. /* no fully matching pcb found? then look for an unconnected pcb */
  202. if (pcb == NULL) {
  203. pcb = uncon_pcb;
  204. }
  205. }
  206. /* Check checksum if this is a match or if it was directed at us. */
  207. if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &current_iphdr_dest)) {
  208. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
  209. #if LWIP_UDPLITE
  210. if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
  211. /* Do the UDP Lite checksum */
  212. #if CHECKSUM_CHECK_UDP
  213. u16_t chklen = ntohs(udphdr->len);
  214. if (chklen < sizeof(struct udp_hdr)) {
  215. if (chklen == 0) {
  216. /* For UDP-Lite, checksum length of 0 means checksum
  217. over the complete packet (See RFC 3828 chap. 3.1) */
  218. chklen = p->tot_len;
  219. } else {
  220. /* At least the UDP-Lite header must be covered by the
  221. checksum! (Again, see RFC 3828 chap. 3.1) */
  222. UDP_STATS_INC(udp.chkerr);
  223. UDP_STATS_INC(udp.drop);
  224. snmp_inc_udpinerrors();
  225. pbuf_free(p);
  226. goto end;
  227. }
  228. }
  229. if (inet_chksum_pseudo_partial(p, &current_iphdr_src, &current_iphdr_dest,
  230. IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
  231. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  232. ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
  233. UDP_STATS_INC(udp.chkerr);
  234. UDP_STATS_INC(udp.drop);
  235. snmp_inc_udpinerrors();
  236. pbuf_free(p);
  237. goto end;
  238. }
  239. #endif /* CHECKSUM_CHECK_UDP */
  240. } else
  241. #endif /* LWIP_UDPLITE */
  242. {
  243. #if CHECKSUM_CHECK_UDP
  244. if (udphdr->chksum != 0) {
  245. if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(),
  246. IP_PROTO_UDP, p->tot_len) != 0) {
  247. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  248. ("udp_input: UDP datagram discarded due to failing checksum\n"));
  249. UDP_STATS_INC(udp.chkerr);
  250. UDP_STATS_INC(udp.drop);
  251. snmp_inc_udpinerrors();
  252. pbuf_free(p);
  253. goto end;
  254. }
  255. }
  256. #endif /* CHECKSUM_CHECK_UDP */
  257. }
  258. if(pbuf_header(p, -UDP_HLEN)) {
  259. /* Can we cope with this failing? Just assert for now */
  260. LWIP_ASSERT("pbuf_header failed\n", 0);
  261. UDP_STATS_INC(udp.drop);
  262. snmp_inc_udpinerrors();
  263. pbuf_free(p);
  264. goto end;
  265. }
  266. if (pcb != NULL) {
  267. snmp_inc_udpindatagrams();
  268. #if SO_REUSE && SO_REUSE_RXTOALL
  269. if ((broadcast || ip_addr_ismulticast(&current_iphdr_dest)) &&
  270. ((pcb->so_options & SOF_REUSEADDR) != 0)) {
  271. /* pass broadcast- or multicast packets to all multicast pcbs
  272. if SOF_REUSEADDR is set on the first match */
  273. struct udp_pcb *mpcb;
  274. u8_t p_header_changed = 0;
  275. for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
  276. if (mpcb != pcb) {
  277. /* compare PCB local addr+port to UDP destination addr+port */
  278. if ((mpcb->local_port == dest) &&
  279. ((!broadcast && ip_addr_isany(&mpcb->local_ip)) ||
  280. ip_addr_cmp(&(mpcb->local_ip), &current_iphdr_dest) ||
  281. #if LWIP_IGMP
  282. ip_addr_ismulticast(&current_iphdr_dest) ||
  283. #endif /* LWIP_IGMP */
  284. #if IP_SOF_BROADCAST_RECV
  285. (broadcast && (mpcb->so_options & SOF_BROADCAST)))) {
  286. #else /* IP_SOF_BROADCAST_RECV */
  287. (broadcast))) {
  288. #endif /* IP_SOF_BROADCAST_RECV */
  289. /* pass a copy of the packet to all local matches */
  290. if (mpcb->recv != NULL) {
  291. struct pbuf *q;
  292. /* for that, move payload to IP header again */
  293. if (p_header_changed == 0) {
  294. pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
  295. p_header_changed = 1;
  296. }
  297. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  298. if (q != NULL) {
  299. err_t err = pbuf_copy(q, p);
  300. if (err == ERR_OK) {
  301. /* move payload to UDP data */
  302. pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
  303. mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. if (p_header_changed) {
  311. /* and move payload to UDP data again */
  312. pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
  313. }
  314. }
  315. #endif /* SO_REUSE && SO_REUSE_RXTOALL */
  316. /* callback */
  317. if (pcb->recv != NULL) {
  318. /* now the recv function is responsible for freeing p */
  319. pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
  320. } else {
  321. /* no recv function registered? then we have to free the pbuf! */
  322. pbuf_free(p);
  323. goto end;
  324. }
  325. } else {
  326. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
  327. #if LWIP_ICMP
  328. /* No match was found, send ICMP destination port unreachable unless
  329. destination address was broadcast/multicast. */
  330. if (!broadcast &&
  331. !ip_addr_ismulticast(&current_iphdr_dest)) {
  332. /* move payload pointer back to ip header */
  333. pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
  334. LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
  335. icmp_dest_unreach(p, ICMP_DUR_PORT);
  336. }
  337. #endif /* LWIP_ICMP */
  338. UDP_STATS_INC(udp.proterr);
  339. UDP_STATS_INC(udp.drop);
  340. snmp_inc_udpnoports();
  341. pbuf_free(p);
  342. }
  343. } else {
  344. pbuf_free(p);
  345. }
  346. end:
  347. PERF_STOP("udp_input");
  348. }
  349. /**
  350. * Send data using UDP.
  351. *
  352. * @param pcb UDP PCB used to send the data.
  353. * @param p chain of pbuf's to be sent.
  354. *
  355. * The datagram will be sent to the current remote_ip & remote_port
  356. * stored in pcb. If the pcb is not bound to a port, it will
  357. * automatically be bound to a random port.
  358. *
  359. * @return lwIP error code.
  360. * - ERR_OK. Successful. No error occured.
  361. * - ERR_MEM. Out of memory.
  362. * - ERR_RTE. Could not find route to destination address.
  363. * - More errors could be returned by lower protocol layers.
  364. *
  365. * @see udp_disconnect() udp_sendto()
  366. */
  367. err_t ICACHE_FLASH_ATTR
  368. udp_send(struct udp_pcb *pcb, struct pbuf *p)
  369. {
  370. /* send to the packet using remote ip and port stored in the pcb */
  371. return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
  372. }
  373. #if LWIP_CHECKSUM_ON_COPY
  374. /** Same as udp_send() but with checksum
  375. */
  376. err_t ICACHE_FLASH_ATTR
  377. udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
  378. u8_t have_chksum, u16_t chksum)
  379. {
  380. /* send to the packet using remote ip and port stored in the pcb */
  381. return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
  382. have_chksum, chksum);
  383. }
  384. #endif /* LWIP_CHECKSUM_ON_COPY */
  385. /**
  386. * Send data to a specified address using UDP.
  387. *
  388. * @param pcb UDP PCB used to send the data.
  389. * @param p chain of pbuf's to be sent.
  390. * @param dst_ip Destination IP address.
  391. * @param dst_port Destination UDP port.
  392. *
  393. * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
  394. *
  395. * If the PCB already has a remote address association, it will
  396. * be restored after the data is sent.
  397. *
  398. * @return lwIP error code (@see udp_send for possible error codes)
  399. *
  400. * @see udp_disconnect() udp_send()
  401. */
  402. err_t ICACHE_FLASH_ATTR
  403. udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
  404. ip_addr_t *dst_ip, u16_t dst_port)
  405. {
  406. #if LWIP_CHECKSUM_ON_COPY
  407. return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
  408. }
  409. /** Same as udp_sendto(), but with checksum */
  410. err_t ICACHE_FLASH_ATTR
  411. udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
  412. u16_t dst_port, u8_t have_chksum, u16_t chksum)
  413. {
  414. #endif /* LWIP_CHECKSUM_ON_COPY */
  415. struct netif *netif;
  416. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
  417. /* find the outgoing network interface for this packet */
  418. #if LWIP_IGMP
  419. netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
  420. #else
  421. netif = ip_route(dst_ip);
  422. #endif /* LWIP_IGMP */
  423. /* no outgoing network interface could be found? */
  424. if (netif == NULL) {
  425. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  426. ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip)));
  427. UDP_STATS_INC(udp.rterr);
  428. return ERR_RTE;
  429. }
  430. #if LWIP_CHECKSUM_ON_COPY
  431. return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
  432. #else /* LWIP_CHECKSUM_ON_COPY */
  433. return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
  434. #endif /* LWIP_CHECKSUM_ON_COPY */
  435. }
  436. /**
  437. * Send data to a specified address using UDP.
  438. * The netif used for sending can be specified.
  439. *
  440. * This function exists mainly for DHCP, to be able to send UDP packets
  441. * on a netif that is still down.
  442. *
  443. * @param pcb UDP PCB used to send the data.
  444. * @param p chain of pbuf's to be sent.
  445. * @param dst_ip Destination IP address.
  446. * @param dst_port Destination UDP port.
  447. * @param netif the netif used for sending.
  448. *
  449. * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
  450. *
  451. * @return lwIP error code (@see udp_send for possible error codes)
  452. *
  453. * @see udp_disconnect() udp_send()
  454. */
  455. err_t ICACHE_FLASH_ATTR
  456. udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
  457. ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
  458. {
  459. #if LWIP_CHECKSUM_ON_COPY
  460. return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
  461. }
  462. /** Same as udp_sendto_if(), but with checksum */
  463. err_t ICACHE_FLASH_ATTR
  464. udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
  465. u16_t dst_port, struct netif *netif, u8_t have_chksum,
  466. u16_t chksum)
  467. {
  468. #endif /* LWIP_CHECKSUM_ON_COPY */
  469. struct udp_hdr *udphdr;
  470. ip_addr_t *src_ip;
  471. err_t err;
  472. struct pbuf *q; /* q will be sent down the stack */
  473. #if IP_SOF_BROADCAST
  474. /* broadcast filter? */
  475. if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) {
  476. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  477. ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  478. return ERR_VAL;
  479. }
  480. #endif /* IP_SOF_BROADCAST */
  481. /* if the PCB is not yet bound to a port, bind it here */
  482. if (pcb->local_port == 0) {
  483. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
  484. err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  485. if (err != ERR_OK) {
  486. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
  487. return err;
  488. }
  489. }
  490. /* not enough space to add an UDP header to first pbuf in given p chain? */
  491. if (pbuf_header(p, UDP_HLEN)) {
  492. /* allocate header in a separate new pbuf */
  493. q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
  494. /* new header pbuf could not be allocated? */
  495. if (q == NULL) {
  496. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
  497. return ERR_MEM;
  498. }
  499. if (p->tot_len != 0) {
  500. /* chain header q in front of given pbuf p (only if p contains data) */
  501. pbuf_chain(q, p);
  502. }
  503. /* first pbuf q points to header pbuf */
  504. LWIP_DEBUGF(UDP_DEBUG,
  505. ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  506. } else {
  507. /* adding space for header within p succeeded */
  508. /* first pbuf q equals given pbuf */
  509. q = p;
  510. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
  511. }
  512. LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
  513. (q->len >= sizeof(struct udp_hdr)));
  514. /* q now represents the packet to be sent */
  515. udphdr = (struct udp_hdr *)q->payload;
  516. udphdr->src = htons(pcb->local_port);
  517. udphdr->dest = htons(dst_port);
  518. /* in UDP, 0 checksum means 'no checksum' */
  519. udphdr->chksum = 0x0000;
  520. /* Multicast Loop? */
  521. #if LWIP_IGMP
  522. if (ip_addr_ismulticast(dst_ip) && ((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0)) {
  523. q->flags |= PBUF_FLAG_MCASTLOOP;
  524. }
  525. #endif /* LWIP_IGMP */
  526. /* PCB local address is IP_ANY_ADDR? */
  527. if (ip_addr_isany(&pcb->local_ip)) {
  528. /* use outgoing network interface IP address as source address */
  529. src_ip = &(netif->ip_addr);
  530. } else {
  531. /* check if UDP PCB local IP address is correct
  532. * this could be an old address if netif->ip_addr has changed */
  533. if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
  534. /* local_ip doesn't match, drop the packet */
  535. if (q != p) {
  536. /* free the header pbuf */
  537. pbuf_free(q);
  538. q = NULL;
  539. /* p is still referenced by the caller, and will live on */
  540. }
  541. return ERR_VAL;
  542. }
  543. /* use UDP PCB local IP address as source address */
  544. src_ip = &(pcb->local_ip);
  545. }
  546. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
  547. #if LWIP_UDPLITE
  548. /* UDP Lite protocol? */
  549. if (pcb->flags & UDP_FLAGS_UDPLITE) {
  550. u16_t chklen, chklen_hdr;
  551. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
  552. /* set UDP message length in UDP header */
  553. chklen_hdr = chklen = pcb->chksum_len_tx;
  554. if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
  555. if (chklen != 0) {
  556. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
  557. }
  558. /* For UDP-Lite, checksum length of 0 means checksum
  559. over the complete packet. (See RFC 3828 chap. 3.1)
  560. At least the UDP-Lite header must be covered by the
  561. checksum, therefore, if chksum_len has an illegal
  562. value, we generate the checksum over the complete
  563. packet to be safe. */
  564. chklen_hdr = 0;
  565. chklen = q->tot_len;
  566. }
  567. udphdr->len = htons(chklen_hdr);
  568. /* calculate checksum */
  569. #if CHECKSUM_GEN_UDP
  570. udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
  571. IP_PROTO_UDPLITE, q->tot_len,
  572. #if !LWIP_CHECKSUM_ON_COPY
  573. chklen);
  574. #else /* !LWIP_CHECKSUM_ON_COPY */
  575. (have_chksum ? UDP_HLEN : chklen));
  576. if (have_chksum) {
  577. u32_t acc;
  578. acc = udphdr->chksum + (u16_t)~(chksum);
  579. udphdr->chksum = FOLD_U32T(acc);
  580. }
  581. #endif /* !LWIP_CHECKSUM_ON_COPY */
  582. /* chksum zero must become 0xffff, as zero means 'no checksum' */
  583. if (udphdr->chksum == 0x0000) {
  584. udphdr->chksum = 0xffff;
  585. }
  586. #endif /* CHECKSUM_GEN_UDP */
  587. /* output to IP */
  588. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
  589. #if LWIP_NETIF_HWADDRHINT
  590. netif->addr_hint = &(pcb->addr_hint);
  591. #endif /* LWIP_NETIF_HWADDRHINT*/
  592. err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
  593. #if LWIP_NETIF_HWADDRHINT
  594. netif->addr_hint = NULL;
  595. #endif /* LWIP_NETIF_HWADDRHINT*/
  596. } else
  597. #endif /* LWIP_UDPLITE */
  598. { /* UDP */
  599. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
  600. udphdr->len = htons(q->tot_len);
  601. /* calculate checksum */
  602. #if CHECKSUM_GEN_UDP
  603. if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
  604. u16_t udpchksum;
  605. #if LWIP_CHECKSUM_ON_COPY
  606. if (have_chksum) {
  607. u32_t acc;
  608. udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP,
  609. q->tot_len, UDP_HLEN);
  610. acc = udpchksum + (u16_t)~(chksum);
  611. udpchksum = FOLD_U32T(acc);
  612. } else
  613. #endif /* LWIP_CHECKSUM_ON_COPY */
  614. {
  615. udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
  616. }
  617. /* chksum zero must become 0xffff, as zero means 'no checksum' */
  618. if (udpchksum == 0x0000) {
  619. udpchksum = 0xffff;
  620. }
  621. udphdr->chksum = udpchksum;
  622. }
  623. #endif /* CHECKSUM_GEN_UDP */
  624. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
  625. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
  626. /* output to IP */
  627. #if LWIP_NETIF_HWADDRHINT
  628. netif->addr_hint = &(pcb->addr_hint);
  629. #endif /* LWIP_NETIF_HWADDRHINT*/
  630. err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
  631. #if LWIP_NETIF_HWADDRHINT
  632. netif->addr_hint = NULL;
  633. #endif /* LWIP_NETIF_HWADDRHINT*/
  634. }
  635. /* TODO: must this be increased even if error occured? */
  636. snmp_inc_udpoutdatagrams();
  637. /* did we chain a separate header pbuf earlier? */
  638. if (q != p) {
  639. /* free the header pbuf */
  640. pbuf_free(q);
  641. q = NULL;
  642. /* p is still referenced by the caller, and will live on */
  643. }
  644. UDP_STATS_INC(udp.xmit);
  645. return err;
  646. }
  647. /**
  648. * Bind an UDP PCB.
  649. *
  650. * @param pcb UDP PCB to be bound with a local address ipaddr and port.
  651. * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
  652. * bind to all local interfaces.
  653. * @param port local UDP port to bind with. Use 0 to automatically bind
  654. * to a random port between UDP_LOCAL_PORT_RANGE_START and
  655. * UDP_LOCAL_PORT_RANGE_END.
  656. *
  657. * ipaddr & port are expected to be in the same byte order as in the pcb.
  658. *
  659. * @return lwIP error code.
  660. * - ERR_OK. Successful. No error occured.
  661. * - ERR_USE. The specified ipaddr and port are already bound to by
  662. * another UDP PCB.
  663. *
  664. * @see udp_disconnect()
  665. */
  666. err_t ICACHE_FLASH_ATTR
  667. udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
  668. {
  669. struct udp_pcb *ipcb;
  670. u8_t rebind;
  671. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
  672. ip_addr_debug_print(UDP_DEBUG, ipaddr);
  673. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
  674. rebind = 0;
  675. /* Check for double bind and rebind of the same pcb */
  676. for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  677. /* is this UDP PCB already on active list? */
  678. if (pcb == ipcb) {
  679. /* pcb may occur at most once in active list */
  680. LWIP_ASSERT("rebind == 0", rebind == 0);
  681. /* pcb already in list, just rebind */
  682. rebind = 1;
  683. }
  684. /* By default, we don't allow to bind to a port that any other udp
  685. PCB is alread bound to, unless *all* PCBs with that port have tha
  686. REUSEADDR flag set. */
  687. #if SO_REUSE
  688. else if (((pcb->so_options & SOF_REUSEADDR) == 0) &&
  689. ((ipcb->so_options & SOF_REUSEADDR) == 0)) {
  690. #else /* SO_REUSE */
  691. /* port matches that of PCB in list and REUSEADDR not set -> reject */
  692. else {
  693. #endif /* SO_REUSE */
  694. if ((ipcb->local_port == port) &&
  695. /* IP address matches, or one is IP_ADDR_ANY? */
  696. (ip_addr_isany(&(ipcb->local_ip)) ||
  697. ip_addr_isany(ipaddr) ||
  698. ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
  699. /* other PCB already binds to this local IP and port */
  700. LWIP_DEBUGF(UDP_DEBUG,
  701. ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
  702. return ERR_USE;
  703. }
  704. }
  705. }
  706. ip_addr_set(&pcb->local_ip, ipaddr);
  707. /* no port specified? */
  708. if (port == 0) {
  709. port = UDP_LOCAL_PORT_RANGE_START;
  710. ipcb = udp_pcbs;
  711. while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
  712. if (ipcb->local_port == port) {
  713. /* port is already used by another udp_pcb */
  714. port++;
  715. /* restart scanning all udp pcbs */
  716. ipcb = udp_pcbs;
  717. } else {
  718. /* go on with next udp pcb */
  719. ipcb = ipcb->next;
  720. }
  721. }
  722. if (ipcb != NULL) {
  723. /* no more ports available in local range */
  724. LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
  725. return ERR_USE;
  726. }
  727. }
  728. pcb->local_port = port;
  729. snmp_insert_udpidx_tree(pcb);
  730. /* pcb not active yet? */
  731. if (rebind == 0) {
  732. /* place the PCB on the active list if not already there */
  733. pcb->next = udp_pcbs;
  734. udp_pcbs = pcb;
  735. }
  736. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
  737. ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
  738. ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
  739. ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
  740. pcb->local_port));
  741. return ERR_OK;
  742. }
  743. /**
  744. * Connect an UDP PCB.
  745. *
  746. * This will associate the UDP PCB with the remote address.
  747. *
  748. * @param pcb UDP PCB to be connected with remote address ipaddr and port.
  749. * @param ipaddr remote IP address to connect with.
  750. * @param port remote UDP port to connect with.
  751. *
  752. * @return lwIP error code
  753. *
  754. * ipaddr & port are expected to be in the same byte order as in the pcb.
  755. *
  756. * The udp pcb is bound to a random local port if not already bound.
  757. *
  758. * @see udp_disconnect()
  759. */
  760. err_t ICACHE_FLASH_ATTR
  761. udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
  762. {
  763. struct udp_pcb *ipcb;
  764. if (pcb->local_port == 0) {
  765. err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  766. if (err != ERR_OK) {
  767. return err;
  768. }
  769. }
  770. ip_addr_set(&pcb->remote_ip, ipaddr);
  771. pcb->remote_port = port;
  772. pcb->flags |= UDP_FLAGS_CONNECTED;
  773. /** TODO: this functionality belongs in upper layers */
  774. #ifdef LWIP_UDP_TODO
  775. /* Nail down local IP for netconn_addr()/getsockname() */
  776. if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
  777. struct netif *netif;
  778. if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
  779. LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
  780. UDP_STATS_INC(udp.rterr);
  781. return ERR_RTE;
  782. }
  783. /** TODO: this will bind the udp pcb locally, to the interface which
  784. is used to route output packets to the remote address. However, we
  785. might want to accept incoming packets on any interface! */
  786. pcb->local_ip = netif->ip_addr;
  787. } else if (ip_addr_isany(&pcb->remote_ip)) {
  788. pcb->local_ip.addr = 0;
  789. }
  790. #endif
  791. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
  792. ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
  793. ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
  794. ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
  795. pcb->local_port));
  796. /* Insert UDP PCB into the list of active UDP PCBs. */
  797. for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  798. if (pcb == ipcb) {
  799. /* already on the list, just return */
  800. return ERR_OK;
  801. }
  802. }
  803. /* PCB not yet on the list, add PCB now */
  804. pcb->next = udp_pcbs;
  805. udp_pcbs = pcb;
  806. return ERR_OK;
  807. }
  808. /**
  809. * Disconnect a UDP PCB
  810. *
  811. * @param pcb the udp pcb to disconnect.
  812. */
  813. void ICACHE_FLASH_ATTR
  814. udp_disconnect(struct udp_pcb *pcb)
  815. {
  816. /* reset remote address association */
  817. ip_addr_set_any(&pcb->remote_ip);
  818. pcb->remote_port = 0;
  819. /* mark PCB as unconnected */
  820. pcb->flags &= ~UDP_FLAGS_CONNECTED;
  821. }
  822. /**
  823. * Set a receive callback for a UDP PCB
  824. *
  825. * This callback will be called when receiving a datagram for the pcb.
  826. *
  827. * @param pcb the pcb for wich to set the recv callback
  828. * @param recv function pointer of the callback function
  829. * @param recv_arg additional argument to pass to the callback function
  830. */
  831. void ICACHE_FLASH_ATTR
  832. udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
  833. {
  834. /* remember recv() callback and user data */
  835. pcb->recv = recv;
  836. pcb->recv_arg = recv_arg;
  837. }
  838. /**
  839. * Remove an UDP PCB.
  840. *
  841. * @param pcb UDP PCB to be removed. The PCB is removed from the list of
  842. * UDP PCB's and the data structure is freed from memory.
  843. *
  844. * @see udp_new()
  845. */
  846. void ICACHE_FLASH_ATTR
  847. udp_remove(struct udp_pcb *pcb)
  848. {
  849. struct udp_pcb *pcb2;
  850. snmp_delete_udpidx_tree(pcb);
  851. /* pcb to be removed is first in list? */
  852. if (udp_pcbs == pcb) {
  853. /* make list start at 2nd pcb */
  854. udp_pcbs = udp_pcbs->next;
  855. /* pcb not 1st in list */
  856. } else {
  857. for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  858. /* find pcb in udp_pcbs list */
  859. if (pcb2->next != NULL && pcb2->next == pcb) {
  860. /* remove pcb from list */
  861. pcb2->next = pcb->next;
  862. }
  863. }
  864. }
  865. memp_free(MEMP_UDP_PCB, pcb);
  866. }
  867. /**
  868. * Create a UDP PCB.
  869. *
  870. * @return The UDP PCB which was created. NULL if the PCB data structure
  871. * could not be allocated.
  872. *
  873. * @see udp_remove()
  874. */
  875. struct udp_pcb * ICACHE_FLASH_ATTR
  876. udp_new(void)
  877. {
  878. struct udp_pcb *pcb;
  879. pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
  880. /* could allocate UDP PCB? */
  881. if (pcb != NULL) {
  882. /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
  883. * which means checksum is generated over the whole datagram per default
  884. * (recommended as default by RFC 3828). */
  885. /* initialize PCB to all zeroes */
  886. os_memset(pcb, 0, sizeof(struct udp_pcb));
  887. pcb->ttl = UDP_TTL;
  888. }
  889. return pcb;
  890. }
  891. #if UDP_DEBUG
  892. /**
  893. * Print UDP header information for debug purposes.
  894. *
  895. * @param udphdr pointer to the udp header in memory.
  896. */
  897. void ICACHE_FLASH_ATTR
  898. udp_debug_print(struct udp_hdr *udphdr)
  899. {
  900. LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
  901. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  902. LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
  903. ntohs(udphdr->src), ntohs(udphdr->dest)));
  904. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  905. LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
  906. ntohs(udphdr->len), ntohs(udphdr->chksum)));
  907. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  908. }
  909. #endif /* UDP_DEBUG */
  910. #endif /* LWIP_UDP */