3 * User Datagram Protocol module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
42 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).
46 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
51 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
55 #include "lwip/memp.h"
56 #include "lwip/inet_chksum.h"
57 #include "lwip/ip_addr.h"
59 #include "lwip/ip6_addr.h"
60 #include "lwip/inet_chksum.h"
61 #include "lwip/netif.h"
62 #include "lwip/icmp.h"
63 #include "lwip/icmp6.h"
64 #include "lwip/stats.h"
65 #include "lwip/snmp.h"
66 #include "lwip/dhcp.h"
71 static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__;
75 #ifndef UDP_LOCAL_PORT_RANGE_START
76 /* From http://www.iana.org/assignments/port-numbers:
77 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
78 #define UDP_LOCAL_PORT_RANGE_START 0xc000
79 #define UDP_LOCAL_PORT_RANGE_END 0xffff
80 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
83 /* last local UDP port */
84 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
86 /* The list of UDP PCBs */
87 /* exported in udp.h (was static) */
88 struct udp_pcb *udp_pcbs;
91 * Initialize this module.
96 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
97 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
98 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
102 * Allocate a new local UDP port.
104 * @return a new (free) local UDP port number
113 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
114 udp_port = UDP_LOCAL_PORT_RANGE_START;
116 /* Check all PCBs. */
117 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
118 if (pcb->local_port == udp_port) {
119 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
127 struct udp_pcb *ipcb = udp_pcbs;
128 while ((ipcb != NULL) && (udp_port != UDP_LOCAL_PORT_RANGE_END)) {
129 if (ipcb->local_port == udp_port) {
130 /* port is already used by another udp_pcb */
132 /* restart scanning all udp pcbs */
135 /* go on with next udp pcb */
146 /** Common code to see if the current input packet matches the pcb
147 * (current input packet is accessed via ip(4/6)_current_* macros)
149 * @param pcb pcb to check
150 * @param inp network interface on which the datagram was received (only used for IPv4)
151 * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4)
152 * @return 1 on match, 0 otherwise
155 udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
157 LWIP_UNUSED_ARG(inp); /* in IPv6 only case */
158 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
160 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
161 if(IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
162 #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
163 if((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
166 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */
170 /* Only need to check PCB if incoming IP version matches PCB IP version */
171 if(IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
172 LWIP_ASSERT("UDP PCB: inconsistent local/remote IP versions", IP_IS_V6_VAL(pcb->local_ip) == IP_IS_V6_VAL(pcb->remote_ip));
175 /* Special case: IPv4 broadcast: all or broadcasts in my subnet
176 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
178 #if IP_SOF_BROADCAST_RECV
179 if(ip_get_option(pcb, SOF_BROADCAST))
180 #endif /* IP_SOF_BROADCAST_RECV */
182 if(ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
183 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
184 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
189 #endif /* LWIP_IPV4 */
190 /* Handle IPv4 and IPv6: all, multicast or exact match */
191 if(ip_addr_isany(&pcb->local_ip) ||
193 (ip_current_is_v6() && ip6_addr_ismulticast(ip6_current_dest_addr())) ||
194 #endif /* LWIP_IPV6_MLD */
196 (!ip_current_is_v6() && ip4_addr_ismulticast(ip4_current_dest_addr())) ||
197 #endif /* LWIP_IGMP */
198 ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
207 * Process an incoming UDP datagram.
209 * Given an incoming UDP datagram (as a chain of pbufs) this function
210 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
211 * recv function. If no pcb is found or the datagram is incorrect, the
214 * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header)
215 * @param inp network interface on which the datagram was received.
219 udp_input(struct pbuf *p, struct netif *inp)
221 struct udp_hdr *udphdr;
222 struct udp_pcb *pcb, *prev;
223 struct udp_pcb *uncon_pcb;
228 LWIP_UNUSED_ARG(inp);
232 UDP_STATS_INC(udp.recv);
234 /* Check minimum length (UDP header) */
235 if (p->len < UDP_HLEN) {
236 /* drop short packets */
237 LWIP_DEBUGF(UDP_DEBUG,
238 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
239 UDP_STATS_INC(udp.lenerr);
240 UDP_STATS_INC(udp.drop);
241 MIB2_STATS_INC(mib2.udpinerrors);
246 udphdr = (struct udp_hdr *)p->payload;
248 /* is broadcast packet ? */
249 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
251 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
253 /* convert src and dest ports to host byte order */
254 src = ntohs(udphdr->src);
255 dest = ntohs(udphdr->dest);
257 udp_debug_print(udphdr);
259 /* print the UDP source and destination */
260 LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
261 ip_addr_debug_print(UDP_DEBUG, ip_current_dest_addr());
262 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", ntohs(udphdr->dest)));
263 ip_addr_debug_print(UDP_DEBUG, ip_current_src_addr());
264 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", ntohs(udphdr->src)));
269 /* Iterate through the UDP pcb list for a matching pcb.
270 * 'Perfect match' pcbs (connected to the remote port & ip address) are
271 * preferred. If no perfect match is found, the first unconnected pcb that
272 * matches the local port and ip address gets the datagram. */
273 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
274 /* print the PCB local and remote address */
275 LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
276 ip_addr_debug_print(UDP_DEBUG, &pcb->local_ip);
277 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
278 ip_addr_debug_print(UDP_DEBUG, &pcb->remote_ip);
279 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
281 /* compare PCB local addr+port to UDP destination addr+port */
282 if ((pcb->local_port == dest) &&
283 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
284 if ((uncon_pcb == NULL) &&
285 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
286 /* the first unconnected matching PCB */
290 /* compare PCB remote addr+port to UDP source addr+port */
291 if ((pcb->remote_port == src) &&
292 (ip_addr_isany_val(pcb->remote_ip) ||
293 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
294 /* the first fully matching PCB */
296 /* move the pcb to the front of udp_pcbs so that is
297 found faster next time */
298 prev->next = pcb->next;
299 pcb->next = udp_pcbs;
302 UDP_STATS_INC(udp.cachehit);
310 /* no fully matching pcb found? then look for an unconnected pcb */
315 /* Check checksum if this is a match or if it was directed at us. */
320 if (ip_current_is_v6()) {
321 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
323 #endif /* LWIP_IPV6 */
325 if (!ip_current_is_v6()) {
326 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
328 #endif /* LWIP_IPV4 */
332 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
333 #if CHECKSUM_CHECK_UDP
334 IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) {
336 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
337 /* Do the UDP Lite checksum */
338 u16_t chklen = ntohs(udphdr->len);
339 if (chklen < sizeof(struct udp_hdr)) {
341 /* For UDP-Lite, checksum length of 0 means checksum
342 over the complete packet (See RFC 3828 chap. 3.1) */
345 /* At least the UDP-Lite header must be covered by the
346 checksum! (Again, see RFC 3828 chap. 3.1) */
350 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
352 ip_current_src_addr(), ip_current_dest_addr()) != 0) {
356 #endif /* LWIP_UDPLITE */
358 if (udphdr->chksum != 0) {
359 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
360 ip_current_src_addr(),
361 ip_current_dest_addr()) != 0) {
367 #endif /* CHECKSUM_CHECK_UDP */
368 if (pbuf_header(p, -UDP_HLEN)) {
369 /* Can we cope with this failing? Just assert for now */
370 LWIP_ASSERT("pbuf_header failed\n", 0);
371 UDP_STATS_INC(udp.drop);
372 MIB2_STATS_INC(mib2.udpinerrors);
378 MIB2_STATS_INC(mib2.udpindatagrams);
379 #if SO_REUSE && SO_REUSE_RXTOALL
380 if (ip_get_option(pcb, SOF_REUSEADDR) &&
381 (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) {
382 /* pass broadcast- or multicast packets to all multicast pcbs
383 if SOF_REUSEADDR is set on the first match */
384 struct udp_pcb *mpcb;
385 u8_t p_header_changed = 0;
386 s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN);
387 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
389 /* compare PCB local addr+port to UDP destination addr+port */
390 if ((mpcb->local_port == dest) &&
391 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
392 /* pass a copy of the packet to all local matches */
393 if (mpcb->recv != NULL) {
395 /* for that, move payload to IP header again */
396 if (p_header_changed == 0) {
397 pbuf_header_force(p, hdrs_len);
398 p_header_changed = 1;
400 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
402 err_t err = pbuf_copy(q, p);
404 /* move payload to UDP data */
405 pbuf_header(q, -hdrs_len);
406 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
413 if (p_header_changed) {
414 /* and move payload to UDP data again */
415 pbuf_header(p, -hdrs_len);
418 #endif /* SO_REUSE && SO_REUSE_RXTOALL */
420 if (pcb->recv != NULL) {
421 /* now the recv function is responsible for freeing p */
422 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
424 /* no recv function registered? then we have to free the pbuf! */
429 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
431 #if LWIP_ICMP || LWIP_ICMP6
432 /* No match was found, send ICMP destination port unreachable unless
433 destination address was broadcast/multicast. */
434 if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) {
435 /* move payload pointer back to ip header */
436 pbuf_header_force(p, ip_current_header_tot_len() + UDP_HLEN);
437 icmp_port_unreach(ip_current_is_v6(), p);
439 #endif /* LWIP_ICMP || LWIP_ICMP6 */
440 UDP_STATS_INC(udp.proterr);
441 UDP_STATS_INC(udp.drop);
442 MIB2_STATS_INC(mib2.udpnoports);
449 PERF_STOP("udp_input");
451 #if CHECKSUM_CHECK_UDP
453 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
454 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
455 UDP_STATS_INC(udp.chkerr);
456 UDP_STATS_INC(udp.drop);
457 MIB2_STATS_INC(mib2.udpinerrors);
459 PERF_STOP("udp_input");
460 #endif /* CHECKSUM_CHECK_UDP */
464 * Send data using UDP.
466 * @param pcb UDP PCB used to send the data.
467 * @param p chain of pbuf's to be sent.
469 * The datagram will be sent to the current remote_ip & remote_port
470 * stored in pcb. If the pcb is not bound to a port, it will
471 * automatically be bound to a random port.
473 * @return lwIP error code.
474 * - ERR_OK. Successful. No error occurred.
475 * - ERR_MEM. Out of memory.
476 * - ERR_RTE. Could not find route to destination address.
477 * - ERR_VAL. No PCB or PCB is dual-stack
478 * - More errors could be returned by lower protocol layers.
480 * @see udp_disconnect() udp_sendto()
483 udp_send(struct udp_pcb *pcb, struct pbuf *p)
485 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
489 /* send to the packet using remote ip and port stored in the pcb */
490 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
493 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
494 /** Same as udp_send() but with checksum
497 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
498 u8_t have_chksum, u16_t chksum)
500 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
504 /* send to the packet using remote ip and port stored in the pcb */
505 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
506 have_chksum, chksum);
508 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
511 * Send data to a specified address using UDP.
513 * @param pcb UDP PCB used to send the data.
514 * @param p chain of pbuf's to be sent.
515 * @param dst_ip Destination IP address.
516 * @param dst_port Destination UDP port.
518 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
520 * If the PCB already has a remote address association, it will
521 * be restored after the data is sent.
523 * @return lwIP error code (@see udp_send for possible error codes)
525 * @see udp_disconnect() udp_send()
528 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
529 const ip_addr_t *dst_ip, u16_t dst_port)
531 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
532 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
535 /** Same as udp_sendto(), but with checksum */
537 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
538 u16_t dst_port, u8_t have_chksum, u16_t chksum)
540 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
542 const ip_addr_t *dst_ip_route = dst_ip;
544 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
548 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
550 #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS)
551 if (ip_addr_ismulticast(dst_ip_route)) {
553 if (IP_IS_V6(dst_ip)) {
554 /* For multicast, find a netif based on source address. */
555 dst_ip_route = &pcb->local_ip;
557 #endif /* LWIP_IPV6 */
559 #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
560 /* IPv4 does not use source-based routing by default, so we use an
561 administratively selected interface for multicast by default.
562 However, this can be overridden by setting an interface address
563 in pcb->multicast_ip that is used for routing. */
564 if (!ip_addr_isany_val(pcb->multicast_ip) &&
565 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
566 dst_ip_route = &pcb->multicast_ip;
568 #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
571 #endif /* LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) */
573 /* find the outgoing network interface for this packet */
574 netif = ip_route(&pcb->local_ip, dst_ip_route);
576 /* no outgoing network interface could be found? */
578 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
579 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip);
580 LWIP_DEBUGF(UDP_DEBUG, ("\n"));
581 UDP_STATS_INC(udp.rterr);
584 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
585 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
586 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
587 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
588 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
592 * Send data to a specified address using UDP.
593 * The netif used for sending can be specified.
595 * This function exists mainly for DHCP, to be able to send UDP packets
596 * on a netif that is still down.
598 * @param pcb UDP PCB used to send the data.
599 * @param p chain of pbuf's to be sent.
600 * @param dst_ip Destination IP address.
601 * @param dst_port Destination UDP port.
602 * @param netif the netif used for sending.
604 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
606 * @return lwIP error code (@see udp_send for possible error codes)
608 * @see udp_disconnect() udp_send()
611 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
612 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
614 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
615 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
618 /** Same as udp_sendto_if(), but with checksum */
620 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
621 u16_t dst_port, struct netif *netif, u8_t have_chksum,
624 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
625 const ip_addr_t *src_ip;
627 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
631 /* PCB local address is IP_ANY_ADDR? */
633 if (IP_IS_V6(dst_ip)) {
634 if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) {
635 src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip));
636 if (src_ip == NULL) {
637 /* No suitable source address was found. */
641 /* use UDP PCB local IPv6 address as source address, if still valid. */
642 if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
643 /* Address isn't valid anymore. */
646 src_ip = &pcb->local_ip;
649 #endif /* LWIP_IPV6 */
650 #if LWIP_IPV4 && LWIP_IPV6
652 #endif /* LWIP_IPV4 && LWIP_IPV6 */
654 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
655 /* use outgoing network interface IP address as source address */
656 src_ip = netif_ip_addr4(netif);
658 /* check if UDP PCB local IP address is correct
659 * this could be an old address if netif->ip_addr has changed */
660 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
661 /* local_ip doesn't match, drop the packet */
664 /* use UDP PCB local IP address as source address */
665 src_ip = &pcb->local_ip;
667 #endif /* LWIP_IPV4 */
668 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
669 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
670 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
671 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
672 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
675 /** Same as udp_sendto_if(), but with source address */
677 udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
678 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
680 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
681 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
684 /** Same as udp_sendto_if_src(), but with checksum */
686 udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
687 u16_t dst_port, struct netif *netif, u8_t have_chksum,
688 u16_t chksum, const ip_addr_t *src_ip)
690 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
691 struct udp_hdr *udphdr;
693 struct pbuf *q; /* q will be sent down the stack */
697 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
698 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
702 #if LWIP_IPV4 && IP_SOF_BROADCAST
703 /* broadcast filter? */
704 if (!ip_get_option(pcb, SOF_BROADCAST) &&
707 #endif /* LWIP_IPV6 */
708 ip_addr_isbroadcast(dst_ip, netif)) {
709 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
710 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
713 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
715 /* if the PCB is not yet bound to a port, bind it here */
716 if (pcb->local_port == 0) {
717 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
718 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
720 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
725 /* not enough space to add an UDP header to first pbuf in given p chain? */
726 if (pbuf_header(p, UDP_HLEN)) {
727 /* allocate header in a separate new pbuf */
728 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
729 /* new header pbuf could not be allocated? */
731 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
734 if (p->tot_len != 0) {
735 /* chain header q in front of given pbuf p (only if p contains data) */
738 /* first pbuf q points to header pbuf */
739 LWIP_DEBUGF(UDP_DEBUG,
740 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
742 /* adding space for header within p succeeded */
743 /* first pbuf q equals given pbuf */
745 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
747 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
748 (q->len >= sizeof(struct udp_hdr)));
749 /* q now represents the packet to be sent */
750 udphdr = (struct udp_hdr *)q->payload;
751 udphdr->src = htons(pcb->local_port);
752 udphdr->dest = htons(dst_port);
753 /* in UDP, 0 checksum means 'no checksum' */
754 udphdr->chksum = 0x0000;
756 /* Multicast Loop? */
757 #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD)
758 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
759 q->flags |= PBUF_FLAG_MCASTLOOP;
761 #endif /* (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
763 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
766 /* UDP Lite protocol? */
767 if (pcb->flags & UDP_FLAGS_UDPLITE) {
768 u16_t chklen, chklen_hdr;
769 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
770 /* set UDP message length in UDP header */
771 chklen_hdr = chklen = pcb->chksum_len_tx;
772 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
774 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
776 /* For UDP-Lite, checksum length of 0 means checksum
777 over the complete packet. (See RFC 3828 chap. 3.1)
778 At least the UDP-Lite header must be covered by the
779 checksum, therefore, if chksum_len has an illegal
780 value, we generate the checksum over the complete
781 packet to be safe. */
785 udphdr->len = htons(chklen_hdr);
786 /* calculate checksum */
788 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
789 #if LWIP_CHECKSUM_ON_COPY
793 #endif /* LWIP_CHECKSUM_ON_COPY */
794 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
795 q->tot_len, chklen, src_ip, dst_ip);
796 #if LWIP_CHECKSUM_ON_COPY
799 acc = udphdr->chksum + (u16_t)~(chksum);
800 udphdr->chksum = FOLD_U32T(acc);
802 #endif /* LWIP_CHECKSUM_ON_COPY */
804 /* chksum zero must become 0xffff, as zero means 'no checksum' */
805 if (udphdr->chksum == 0x0000) {
806 udphdr->chksum = 0xffff;
809 #endif /* CHECKSUM_GEN_UDP */
811 ip_proto = IP_PROTO_UDPLITE;
813 #endif /* LWIP_UDPLITE */
815 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
816 udphdr->len = htons(q->tot_len);
817 /* calculate checksum */
819 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
820 /* Checksum is mandatory over IPv6. */
821 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
823 #if LWIP_CHECKSUM_ON_COPY
826 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
827 q->tot_len, UDP_HLEN, src_ip, dst_ip);
828 acc = udpchksum + (u16_t)~(chksum);
829 udpchksum = FOLD_U32T(acc);
831 #endif /* LWIP_CHECKSUM_ON_COPY */
833 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
837 /* chksum zero must become 0xffff, as zero means 'no checksum' */
838 if (udpchksum == 0x0000) {
841 udphdr->chksum = udpchksum;
844 #endif /* CHECKSUM_GEN_UDP */
845 ip_proto = IP_PROTO_UDP;
848 /* Determine TTL to use */
849 #if LWIP_MULTICAST_TX_OPTIONS
850 ttl = (ip_addr_ismulticast(dst_ip) ? pcb->mcast_ttl : pcb->ttl);
851 #else /* LWIP_MULTICAST_TX_OPTIONS */
853 #endif /* LWIP_MULTICAST_TX_OPTIONS */
855 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
856 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
858 NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
859 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
860 NETIF_SET_HWADDRHINT(netif, NULL);
862 /* TODO: must this be increased even if error occurred? */
863 MIB2_STATS_INC(mib2.udpoutdatagrams);
865 /* did we chain a separate header pbuf earlier? */
867 /* free the header pbuf */
870 /* p is still referenced by the caller, and will live on */
873 UDP_STATS_INC(udp.xmit);
880 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
881 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
882 * bind to all local interfaces.
883 * @param port local UDP port to bind with. Use 0 to automatically bind
884 * to a random port between UDP_LOCAL_PORT_RANGE_START and
885 * UDP_LOCAL_PORT_RANGE_END.
887 * ipaddr & port are expected to be in the same byte order as in the pcb.
889 * @return lwIP error code.
890 * - ERR_OK. Successful. No error occurred.
891 * - ERR_USE. The specified ipaddr and port are already bound to by
894 * @see udp_disconnect()
897 udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
899 struct udp_pcb *ipcb;
903 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
904 if (ipaddr == NULL) {
905 ipaddr = IP_ADDR_ANY;
907 #endif /* LWIP_IPV4 */
909 /* still need to check for ipaddr == NULL in IPv6 only case */
910 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
914 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
915 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr);
916 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
919 /* Check for double bind and rebind of the same pcb */
920 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
921 /* is this UDP PCB already on active list? */
928 /* no port specified? */
930 port = udp_new_port();
932 /* no more ports available in local range */
933 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
937 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
939 /* By default, we don't allow to bind to a port that any other udp
940 PCB is already bound to, unless *all* PCBs with that port have tha
941 REUSEADDR flag set. */
943 if (!ip_get_option(pcb, SOF_REUSEADDR) ||
944 !ip_get_option(ipcb, SOF_REUSEADDR))
945 #endif /* SO_REUSE */
947 /* port matches that of PCB in list and REUSEADDR not set -> reject */
948 if ((ipcb->local_port == port) && (IP_IS_V6(ipaddr) == IP_IS_V6_VAL(ipcb->local_ip)) &&
949 /* IP address matches, or one is IP_ADDR_ANY? */
950 (ip_addr_isany(&ipcb->local_ip) ||
951 ip_addr_isany(ipaddr) ||
952 ip_addr_cmp(&ipcb->local_ip, ipaddr))) {
953 /* other PCB already binds to this local IP and port */
954 LWIP_DEBUGF(UDP_DEBUG,
955 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
963 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
965 pcb->local_port = port;
967 /* pcb not active yet? */
969 /* place the PCB on the active list if not already there */
970 pcb->next = udp_pcbs;
973 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
974 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip);
975 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
980 * Connect an UDP PCB.
982 * This will associate the UDP PCB with the remote address.
984 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
985 * @param ipaddr remote IP address to connect with.
986 * @param port remote UDP port to connect with.
988 * @return lwIP error code
990 * ipaddr & port are expected to be in the same byte order as in the pcb.
992 * The udp pcb is bound to a random local port if not already bound.
994 * @see udp_disconnect()
997 udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
999 struct udp_pcb *ipcb;
1001 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
1005 if (pcb->local_port == 0) {
1006 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1007 if (err != ERR_OK) {
1012 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
1013 pcb->remote_port = port;
1014 pcb->flags |= UDP_FLAGS_CONNECTED;
1016 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
1017 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
1019 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
1021 /* Insert UDP PCB into the list of active UDP PCBs. */
1022 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
1024 /* already on the list, just return */
1028 /* PCB not yet on the list, add PCB now */
1029 pcb->next = udp_pcbs;
1035 * Disconnect a UDP PCB
1037 * @param pcb the udp pcb to disconnect.
1040 udp_disconnect(struct udp_pcb *pcb)
1042 /* reset remote address association */
1043 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
1044 pcb->remote_port = 0;
1045 /* mark PCB as unconnected */
1046 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1050 * Set a receive callback for a UDP PCB
1052 * This callback will be called when receiving a datagram for the pcb.
1054 * @param pcb the pcb for which to set the recv callback
1055 * @param recv function pointer of the callback function
1056 * @param recv_arg additional argument to pass to the callback function
1059 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
1061 /* remember recv() callback and user data */
1063 pcb->recv_arg = recv_arg;
1067 * Remove an UDP PCB.
1069 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
1070 * UDP PCB's and the data structure is freed from memory.
1075 udp_remove(struct udp_pcb *pcb)
1077 struct udp_pcb *pcb2;
1079 mib2_udp_unbind(pcb);
1080 /* pcb to be removed is first in list? */
1081 if (udp_pcbs == pcb) {
1082 /* make list start at 2nd pcb */
1083 udp_pcbs = udp_pcbs->next;
1084 /* pcb not 1st in list */
1086 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1087 /* find pcb in udp_pcbs list */
1088 if (pcb2->next != NULL && pcb2->next == pcb) {
1089 /* remove pcb from list */
1090 pcb2->next = pcb->next;
1095 memp_free(MEMP_UDP_PCB, pcb);
1101 * @return The UDP PCB which was created. NULL if the PCB data structure
1102 * could not be allocated.
1109 struct udp_pcb *pcb;
1110 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
1111 /* could allocate UDP PCB? */
1113 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
1114 * which means checksum is generated over the whole datagram per default
1115 * (recommended as default by RFC 3828). */
1116 /* initialize PCB to all zeroes */
1117 memset(pcb, 0, sizeof(struct udp_pcb));
1119 #if LWIP_MULTICAST_TX_OPTIONS
1120 pcb->mcast_ttl = UDP_TTL;
1121 #endif /* LWIP_MULTICAST_TX_OPTIONS */
1127 * Create a UDP PCB for specific IP type.
1129 * @param type IP address type, see IPADDR_TYPE_XX definitions.
1130 * @return The UDP PCB which was created. NULL if the PCB data structure
1131 * could not be allocated.
1136 udp_new_ip_type(u8_t type)
1138 struct udp_pcb *pcb;
1140 #if LWIP_IPV4 && LWIP_IPV6
1142 IP_SET_TYPE_VAL(pcb->local_ip, type);
1143 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1146 LWIP_UNUSED_ARG(type);
1147 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1152 /** This function is called from netif.c when address is changed
1154 * @param old_addr IPv4 address of the netif before change
1155 * @param new_addr IPv4 address of the netif after change
1157 void udp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* new_addr)
1159 struct udp_pcb* upcb;
1161 if (!ip4_addr_isany(new_addr)) {
1162 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
1163 /* Is this an IPv4 pcb? */
1164 if (!IP_IS_V6_VAL(upcb->local_ip)) {
1165 /* PCB bound to current local interface address? */
1166 if (!ip4_addr_isany(ip_2_ip4(&upcb->local_ip)) &&
1167 ip4_addr_cmp(ip_2_ip4(&upcb->local_ip), old_addr)) {
1168 /* The PCB is bound to the old ipaddr and
1169 * is set to bound to the new one instead */
1170 ip_addr_copy_from_ip4(upcb->local_ip, *new_addr);
1176 #endif /* LWIP_IPV4 */
1180 * Print UDP header information for debug purposes.
1182 * @param udphdr pointer to the udp header in memory.
1185 udp_debug_print(struct udp_hdr *udphdr)
1187 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
1188 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1189 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1190 ntohs(udphdr->src), ntohs(udphdr->dest)));
1191 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1192 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
1193 ntohs(udphdr->len), ntohs(udphdr->chksum)));
1194 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1196 #endif /* UDP_DEBUG */
1198 #endif /* LWIP_UDP */