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