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 "arch/perf.h"
67 #include "lwip/dhcp.h"
71 /* The list of UDP PCBs */
72 /* exported in udp.h (was static) */
73 struct udp_pcb *udp_pcbs;
76 * Process an incoming UDP datagram.
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
83 * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header)
84 * @param inp network interface on which the datagram was received.
88 udp_input(struct pbuf *p, struct netif *inp)
90 struct udp_hdr *udphdr;
91 struct udp_pcb *pcb, *prev;
92 struct udp_pcb *uncon_pcb;
100 UDP_STATS_INC(udp.recv);
102 /* Check minimum length (UDP header) */
103 if (p->len < UDP_HLEN) {
104 /* drop short packets */
105 LWIP_DEBUGF(UDP_DEBUG,
106 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
107 UDP_STATS_INC(udp.lenerr);
108 UDP_STATS_INC(udp.drop);
109 snmp_inc_udpinerrors();
114 udphdr = (struct udp_hdr *)p->payload;
116 /* is broadcast packet ? */
118 broadcast = !ip_current_is_v6() && ip_addr_isbroadcast(ip_current_dest_addr(), inp);
119 #else /* LWIP_IPV6 */
120 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), inp);
121 #endif /* LWIP_IPV6 */
123 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
125 /* convert src and dest ports to host byte order */
126 src = ntohs(udphdr->src);
127 dest = ntohs(udphdr->dest);
129 udp_debug_print(udphdr);
131 /* print the UDP source and destination */
132 LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
133 ipX_addr_debug_print(ip_current_is_v6(), UDP_DEBUG, ipX_current_dest_addr());
134 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", ntohs(udphdr->dest)));
135 ipX_addr_debug_print(ip_current_is_v6(), UDP_DEBUG, ipX_current_src_addr());
136 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", ntohs(udphdr->src)));
140 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
141 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
142 if (dest == DHCP_CLIENT_PORT) {
143 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
144 if (src == DHCP_SERVER_PORT) {
145 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
146 /* accept the packe if
147 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
148 - inp->dhcp->pcb->remote == ANY or iphdr->src
149 (no need to check for IPv6 since the dhcp struct always uses IPv4) */
150 if (ipX_addr_isany(0, &inp->dhcp->pcb->remote_ip) ||
151 ip_addr_cmp(ipX_2_ip(&(inp->dhcp->pcb->remote_ip)), ip_current_src_addr())) {
152 pcb = inp->dhcp->pcb;
157 #endif /* LWIP_DHCP */
162 /* Iterate through the UDP pcb list for a matching pcb.
163 * 'Perfect match' pcbs (connected to the remote port & ip address) are
164 * preferred. If no perfect match is found, the first unconnected pcb that
165 * matches the local port and ip address gets the datagram. */
166 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
168 /* print the PCB local and remote address */
169 LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
170 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG, &pcb->local_ip);
171 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
172 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG, &pcb->remote_ip);
173 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
175 /* compare PCB local addr+port to UDP destination addr+port */
176 if ((pcb->local_port == dest) &&
178 ((PCB_ISIPV6(pcb) && (ip_current_is_v6()) &&
179 (ip6_addr_isany(ipX_2_ip6(&pcb->local_ip)) ||
181 ip6_addr_ismulticast(ip6_current_dest_addr()) ||
182 #endif /* LWIP_IPV6_MLD */
183 ip6_addr_cmp(ipX_2_ip6(&pcb->local_ip), ip6_current_dest_addr()))) ||
185 (ip_current_header() != NULL) &&
186 #else /* LWIP_IPV6 */
188 #endif /* LWIP_IPV6 */
189 ((!broadcast && ipX_addr_isany(0, &pcb->local_ip)) ||
190 ip_addr_cmp(ipX_2_ip(&pcb->local_ip), ip_current_dest_addr()) ||
192 ip_addr_ismulticast(ip_current_dest_addr()) ||
193 #endif /* LWIP_IGMP */
194 #if IP_SOF_BROADCAST_RECV
195 (broadcast && (pcb->so_options & SOF_BROADCAST)))))) {
196 #else /* IP_SOF_BROADCAST_RECV */
198 #endif /* IP_SOF_BROADCAST_RECV */
200 if ((uncon_pcb == NULL) &&
201 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
202 /* the first unconnected matching PCB */
206 /* compare PCB remote addr+port to UDP source addr+port */
207 if ((local_match != 0) &&
208 (pcb->remote_port == src) && IP_PCB_IPVER_INPUT_MATCH(pcb) &&
209 (ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->remote_ip) ||
210 ipX_addr_cmp(PCB_ISIPV6(pcb), &pcb->remote_ip, ipX_current_src_addr()))) {
211 /* the first fully matching PCB */
213 /* move the pcb to the front of udp_pcbs so that is
214 found faster next time */
215 prev->next = pcb->next;
216 pcb->next = udp_pcbs;
219 UDP_STATS_INC(udp.cachehit);
225 /* no fully matching pcb found? then look for an unconnected pcb */
231 /* Check checksum if this is a match or if it was directed at us. */
236 if (ip_current_is_v6()) {
237 for_us = netif_matches_ip6_addr(inp, ip6_current_dest_addr());
239 #endif /* LWIP_IPV6 */
241 for_us = ip_addr_cmp(&inp->ip_addr, ip_current_dest_addr());
245 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
246 #if CHECKSUM_CHECK_UDP
248 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
249 /* Do the UDP Lite checksum */
250 u16_t chklen = ntohs(udphdr->len);
251 if (chklen < sizeof(struct udp_hdr)) {
253 /* For UDP-Lite, checksum length of 0 means checksum
254 over the complete packet (See RFC 3828 chap. 3.1) */
257 /* At least the UDP-Lite header must be covered by the
258 checksum! (Again, see RFC 3828 chap. 3.1) */
262 if (ipX_chksum_pseudo_partial(ip_current_is_v6(), p, IP_PROTO_UDPLITE,
264 ipX_current_src_addr(), ipX_current_dest_addr()) != 0) {
268 #endif /* LWIP_UDPLITE */
270 if (udphdr->chksum != 0) {
271 if (ipX_chksum_pseudo(ip_current_is_v6(), p, IP_PROTO_UDP, p->tot_len,
272 ipX_current_src_addr(),
273 ipX_current_dest_addr()) != 0) {
278 #endif /* CHECKSUM_CHECK_UDP */
279 if(pbuf_header(p, -UDP_HLEN)) {
280 /* Can we cope with this failing? Just assert for now */
281 LWIP_ASSERT("pbuf_header failed\n", 0);
282 UDP_STATS_INC(udp.drop);
283 snmp_inc_udpinerrors();
288 snmp_inc_udpindatagrams();
289 #if SO_REUSE && SO_REUSE_RXTOALL
292 ip6_addr_ismulticast(ip6_current_dest_addr()) ||
293 #endif /* LWIP_IPV6 */
294 ip_addr_ismulticast(ip_current_dest_addr())) &&
295 ((pcb->so_options & SOF_REUSEADDR) != 0)) {
296 /* pass broadcast- or multicast packets to all multicast pcbs
297 if SOF_REUSEADDR is set on the first match */
298 struct udp_pcb *mpcb;
299 u8_t p_header_changed = 0;
300 s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN);
301 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
303 /* compare PCB local addr+port to UDP destination addr+port */
304 if ((mpcb->local_port == dest) &&
306 ((PCB_ISIPV6(mpcb) &&
307 (ip6_addr_ismulticast(ip6_current_dest_addr()) ||
308 ip6_addr_cmp(ipX_2_ip6(&mpcb->local_ip), ip6_current_dest_addr()))) ||
309 (!PCB_ISIPV6(mpcb) &&
310 #else /* LWIP_IPV6 */
312 #endif /* LWIP_IPV6 */
313 ((!broadcast && ipX_addr_isany(0, &mpcb->local_ip)) ||
314 ip_addr_cmp(ipX_2_ip(&mpcb->local_ip), ip_current_dest_addr()) ||
316 ip_addr_ismulticast(ip_current_dest_addr()) ||
317 #endif /* LWIP_IGMP */
318 #if IP_SOF_BROADCAST_RECV
319 (broadcast && (mpcb->so_options & SOF_BROADCAST)))))) {
320 #else /* IP_SOF_BROADCAST_RECV */
322 #endif /* IP_SOF_BROADCAST_RECV */
323 /* pass a copy of the packet to all local matches */
324 if (mpcb->recv.ip4 != NULL) {
326 /* for that, move payload to IP header again */
327 if (p_header_changed == 0) {
328 pbuf_header(p, hdrs_len);
329 p_header_changed = 1;
331 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
333 err_t err = pbuf_copy(q, p);
335 /* move payload to UDP data */
336 pbuf_header(q, -hdrs_len);
338 if (PCB_ISIPV6(mpcb)) {
339 mpcb->recv.ip6(mpcb->recv_arg, mpcb, q, ip6_current_src_addr(), src);
342 #endif /* LWIP_IPV6 */
344 mpcb->recv.ip4(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
352 if (p_header_changed) {
353 /* and move payload to UDP data again */
354 pbuf_header(p, -hdrs_len);
357 #endif /* SO_REUSE && SO_REUSE_RXTOALL */
359 if (pcb->recv.ip4 != NULL) {
360 /* now the recv function is responsible for freeing p */
362 if (PCB_ISIPV6(pcb)) {
363 pcb->recv.ip6(pcb->recv_arg, pcb, p, ip6_current_src_addr(), src);
366 #endif /* LWIP_IPV6 */
368 pcb->recv.ip4(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
371 /* no recv function registered? then we have to free the pbuf! */
376 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
378 #if LWIP_ICMP || LWIP_ICMP6
379 /* No match was found, send ICMP destination port unreachable unless
380 destination address was broadcast/multicast. */
383 !ip6_addr_ismulticast(ip6_current_dest_addr()) &&
384 #endif /* LWIP_IPV6 */
385 !ip_addr_ismulticast(ip_current_dest_addr())) {
386 /* move payload pointer back to ip header */
387 pbuf_header(p, ip_current_header_tot_len() + UDP_HLEN);
388 icmp_port_unreach(ip_current_is_v6(), p);
390 #endif /* LWIP_ICMP || LWIP_ICMP6 */
391 UDP_STATS_INC(udp.proterr);
392 UDP_STATS_INC(udp.drop);
393 snmp_inc_udpnoports();
400 PERF_STOP("udp_input");
403 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
404 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
405 UDP_STATS_INC(udp.chkerr);
406 UDP_STATS_INC(udp.drop);
407 snmp_inc_udpinerrors();
409 PERF_STOP("udp_input");
413 * Send data using UDP.
415 * @param pcb UDP PCB used to send the data.
416 * @param p chain of pbuf's to be sent.
418 * The datagram will be sent to the current remote_ip & remote_port
419 * stored in pcb. If the pcb is not bound to a port, it will
420 * automatically be bound to a random port.
422 * @return lwIP error code.
423 * - ERR_OK. Successful. No error occured.
424 * - ERR_MEM. Out of memory.
425 * - ERR_RTE. Could not find route to destination address.
426 * - More errors could be returned by lower protocol layers.
428 * @see udp_disconnect() udp_sendto()
431 udp_send(struct udp_pcb *pcb, struct pbuf *p)
433 /* send to the packet using remote ip and port stored in the pcb */
434 return udp_sendto(pcb, p, ipX_2_ip(&pcb->remote_ip), pcb->remote_port);
437 #if LWIP_CHECKSUM_ON_COPY
438 /** Same as udp_send() but with checksum
441 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
442 u8_t have_chksum, u16_t chksum)
444 /* send to the packet using remote ip and port stored in the pcb */
445 return udp_sendto_chksum(pcb, p, ipX_2_ip(&pcb->remote_ip), pcb->remote_port,
446 have_chksum, chksum);
448 #endif /* LWIP_CHECKSUM_ON_COPY */
451 * Send data to a specified address using UDP.
453 * @param pcb UDP PCB used to send the data.
454 * @param p chain of pbuf's to be sent.
455 * @param dst_ip Destination IP address.
456 * @param dst_port Destination UDP port.
458 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
460 * If the PCB already has a remote address association, it will
461 * be restored after the data is sent.
463 * @return lwIP error code (@see udp_send for possible error codes)
465 * @see udp_disconnect() udp_send()
468 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
469 ip_addr_t *dst_ip, u16_t dst_port)
471 #if LWIP_CHECKSUM_ON_COPY
472 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
475 /** Same as udp_sendto(), but with checksum */
477 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
478 u16_t dst_port, u8_t have_chksum, u16_t chksum)
480 #endif /* LWIP_CHECKSUM_ON_COPY */
482 ipX_addr_t *dst_ip_route = ip_2_ipX(dst_ip);
484 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
486 #if LWIP_IPV6 || LWIP_IGMP
487 if (ipX_addr_ismulticast(PCB_ISIPV6(pcb), dst_ip_route)) {
488 /* For multicast, find a netif based on source address. */
490 if (PCB_ISIPV6(pcb)) {
491 dst_ip_route = &pcb->local_ip;
493 #endif /* LWIP_IPV6 */
496 dst_ip_route = ip_2_ipX(&pcb->multicast_ip);
497 #endif /* LWIP_IGMP */
500 #endif /* LWIP_IPV6 || LWIP_IGMP */
502 /* find the outgoing network interface for this packet */
503 netif = ipX_route(PCB_ISIPV6(pcb), &pcb->local_ip, dst_ip_route);
505 /* no outgoing network interface could be found? */
507 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
508 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ip_2_ipX(dst_ip));
509 LWIP_DEBUGF(UDP_DEBUG, ("\n"));
510 UDP_STATS_INC(udp.rterr);
513 #if LWIP_CHECKSUM_ON_COPY
514 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
515 #else /* LWIP_CHECKSUM_ON_COPY */
516 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
517 #endif /* LWIP_CHECKSUM_ON_COPY */
521 * Send data to a specified address using UDP.
522 * The netif used for sending can be specified.
524 * This function exists mainly for DHCP, to be able to send UDP packets
525 * on a netif that is still down.
527 * @param pcb UDP PCB used to send the data.
528 * @param p chain of pbuf's to be sent.
529 * @param dst_ip Destination IP address.
530 * @param dst_port Destination UDP port.
531 * @param netif the netif used for sending.
533 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
535 * @return lwIP error code (@see udp_send for possible error codes)
537 * @see udp_disconnect() udp_send()
540 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
541 ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
543 #if LWIP_CHECKSUM_ON_COPY
544 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
547 /** Same as udp_sendto_if(), but with checksum */
549 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
550 u16_t dst_port, struct netif *netif, u8_t have_chksum,
553 #endif /* LWIP_CHECKSUM_ON_COPY */
554 struct udp_hdr *udphdr;
557 struct pbuf *q; /* q will be sent down the stack */
561 /* broadcast filter? */
562 if ( ((pcb->so_options & SOF_BROADCAST) == 0) &&
565 #endif /* LWIP_IPV6 */
566 ip_addr_isbroadcast(dst_ip, netif) ) {
567 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
568 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
571 #endif /* IP_SOF_BROADCAST */
573 /* if the PCB is not yet bound to a port, bind it here */
574 if (pcb->local_port == 0) {
575 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
576 err = udp_bind(pcb, ipX_2_ip(&pcb->local_ip), pcb->local_port);
578 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
583 /* not enough space to add an UDP header to first pbuf in given p chain? */
584 if (pbuf_header(p, UDP_HLEN)) {
585 /* allocate header in a separate new pbuf */
586 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
587 /* new header pbuf could not be allocated? */
589 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
592 if (p->tot_len != 0) {
593 /* chain header q in front of given pbuf p (only if p contains data) */
596 /* first pbuf q points to header pbuf */
597 LWIP_DEBUGF(UDP_DEBUG,
598 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
600 /* adding space for header within p succeeded */
601 /* first pbuf q equals given pbuf */
603 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
605 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
606 (q->len >= sizeof(struct udp_hdr)));
607 /* q now represents the packet to be sent */
608 udphdr = (struct udp_hdr *)q->payload;
609 udphdr->src = htons(pcb->local_port);
610 udphdr->dest = htons(dst_port);
611 /* in UDP, 0 checksum means 'no checksum' */
612 udphdr->chksum = 0x0000;
614 /* Multicast Loop? */
616 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) &&
621 ip6_addr_ismulticast(ip_2_ip6(dst_ip))) ||
622 #endif /* LWIP_IPV6_MLD */
624 #else /* LWIP_IPV6 */
626 #endif /* LWIP_IPV6 */
627 ip_addr_ismulticast(dst_ip)))) {
628 q->flags |= PBUF_FLAG_MCASTLOOP;
630 #endif /* LWIP_IGMP */
633 /* PCB local address is IP_ANY_ADDR? */
635 if (PCB_ISIPV6(pcb)) {
636 if (ip6_addr_isany(ipX_2_ip6(&pcb->local_ip))) {
637 src_ip = ip6_2_ip(ip6_select_source_address(netif, ip_2_ip6(dst_ip)));
638 if (src_ip == NULL) {
639 /* No suitable source address was found. */
641 /* free the header pbuf */
643 /* p is still referenced by the caller, and will live on */
648 /* use UDP PCB local IPv6 address as source address, if still valid. */
649 if (netif_matches_ip6_addr(netif, ipX_2_ip6(&pcb->local_ip)) < 0) {
650 /* Address isn't valid anymore. */
652 /* free the header pbuf */
654 /* p is still referenced by the caller, and will live on */
658 src_ip = ipX_2_ip(&pcb->local_ip);
662 #endif /* LWIP_IPV6 */
663 if (ip_addr_isany(ipX_2_ip(&pcb->local_ip))) {
664 /* use outgoing network interface IP address as source address */
665 src_ip = &(netif->ip_addr);
667 /* check if UDP PCB local IP address is correct
668 * this could be an old address if netif->ip_addr has changed */
669 if (!ip_addr_cmp(ipX_2_ip(&(pcb->local_ip)), &(netif->ip_addr))) {
670 /* local_ip doesn't match, drop the packet */
672 /* free the header pbuf */
675 /* p is still referenced by the caller, and will live on */
679 /* use UDP PCB local IP address as source address */
680 src_ip = ipX_2_ip(&(pcb->local_ip));
683 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
686 /* UDP Lite protocol? */
687 if (pcb->flags & UDP_FLAGS_UDPLITE) {
688 u16_t chklen, chklen_hdr;
689 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
690 /* set UDP message length in UDP header */
691 chklen_hdr = chklen = pcb->chksum_len_tx;
692 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
694 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
696 /* For UDP-Lite, checksum length of 0 means checksum
697 over the complete packet. (See RFC 3828 chap. 3.1)
698 At least the UDP-Lite header must be covered by the
699 checksum, therefore, if chksum_len has an illegal
700 value, we generate the checksum over the complete
701 packet to be safe. */
705 udphdr->len = htons(chklen_hdr);
706 /* calculate checksum */
708 #if LWIP_CHECKSUM_ON_COPY
712 #endif /* LWIP_CHECKSUM_ON_COPY */
713 udphdr->chksum = ipX_chksum_pseudo_partial(PCB_ISIPV6(pcb), q, IP_PROTO_UDPLITE,
714 q->tot_len, chklen, ip_2_ipX(src_ip), ip_2_ipX(dst_ip));
715 #if LWIP_CHECKSUM_ON_COPY
718 acc = udphdr->chksum + (u16_t)~(chksum);
719 udphdr->chksum = FOLD_U32T(acc);
721 #endif /* LWIP_CHECKSUM_ON_COPY */
723 /* chksum zero must become 0xffff, as zero means 'no checksum' */
724 if (udphdr->chksum == 0x0000) {
725 udphdr->chksum = 0xffff;
727 #endif /* CHECKSUM_GEN_UDP */
729 ip_proto = IP_PROTO_UDPLITE;
731 #endif /* LWIP_UDPLITE */
733 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
734 udphdr->len = htons(q->tot_len);
735 /* calculate checksum */
737 /* Checksum is mandatory over IPv6. */
738 if (PCB_ISIPV6(pcb) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
740 #if LWIP_CHECKSUM_ON_COPY
743 udpchksum = ipX_chksum_pseudo_partial(PCB_ISIPV6(pcb), q, IP_PROTO_UDP,
744 q->tot_len, UDP_HLEN, ip_2_ipX(src_ip), ip_2_ipX(dst_ip));
745 acc = udpchksum + (u16_t)~(chksum);
746 udpchksum = FOLD_U32T(acc);
748 #endif /* LWIP_CHECKSUM_ON_COPY */
750 udpchksum = ipX_chksum_pseudo(PCB_ISIPV6(pcb), q, IP_PROTO_UDP, q->tot_len,
751 ip_2_ipX(src_ip), ip_2_ipX(dst_ip));
754 /* chksum zero must become 0xffff, as zero means 'no checksum' */
755 if (udpchksum == 0x0000) {
758 udphdr->chksum = udpchksum;
760 #endif /* CHECKSUM_GEN_UDP */
761 ip_proto = IP_PROTO_UDP;
764 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
765 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
767 NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
768 err = ipX_output_if(PCB_ISIPV6(pcb), q, src_ip, dst_ip, pcb->ttl, pcb->tos, ip_proto, netif);
769 NETIF_SET_HWADDRHINT(netif, NULL);
771 /* TODO: must this be increased even if error occured? */
772 snmp_inc_udpoutdatagrams();
774 /* did we chain a separate header pbuf earlier? */
776 /* free the header pbuf */
779 /* p is still referenced by the caller, and will live on */
782 UDP_STATS_INC(udp.xmit);
789 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
790 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
791 * bind to all local interfaces.
792 * @param port local UDP port to bind with. Use 0 to automatically bind
793 * to a random port between UDP_LOCAL_PORT_RANGE_START and
794 * UDP_LOCAL_PORT_RANGE_END.
796 * ipaddr & port are expected to be in the same byte order as in the pcb.
798 * @return lwIP error code.
799 * - ERR_OK. Successful. No error occured.
800 * - ERR_USE. The specified ipaddr and port are already bound to by
803 * @see udp_disconnect()
806 udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
808 struct udp_pcb *ipcb;
811 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
812 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG | LWIP_DBG_TRACE, ip_2_ipX(ipaddr));
813 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
816 /* Check for double bind and rebind of the same pcb */
817 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
818 /* is this UDP PCB already on active list? */
820 /* pcb may occur at most once in active list */
821 LWIP_ASSERT("rebind == 0", rebind == 0);
822 /* pcb already in list, just rebind */
826 /* By default, we don't allow to bind to a port that any other udp
827 PCB is alread bound to, unless *all* PCBs with that port have tha
828 REUSEADDR flag set. */
830 else if (((pcb->so_options & SOF_REUSEADDR) == 0) &&
831 ((ipcb->so_options & SOF_REUSEADDR) == 0)) {
833 /* port matches that of PCB in list and REUSEADDR not set -> reject */
835 #endif /* SO_REUSE */
836 if ((ipcb->local_port == port) && IP_PCB_IPVER_EQ(pcb, ipcb) &&
837 /* IP address matches, or one is IP_ADDR_ANY? */
838 (ipX_addr_isany(PCB_ISIPV6(ipcb), &(ipcb->local_ip)) ||
839 ipX_addr_isany(PCB_ISIPV6(ipcb), ip_2_ipX(ipaddr)) ||
840 ipX_addr_cmp(PCB_ISIPV6(ipcb), &(ipcb->local_ip), ip_2_ipX(ipaddr)))) {
841 /* other PCB already binds to this local IP and port */
842 LWIP_DEBUGF(UDP_DEBUG,
843 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
849 ipX_addr_set_ipaddr(PCB_ISIPV6(pcb), &pcb->local_ip, ipaddr);
851 /* no port specified? */
853 #ifndef UDP_LOCAL_PORT_RANGE_START
854 /* From http://www.iana.org/assignments/port-numbers:
855 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
856 #define UDP_LOCAL_PORT_RANGE_START 0xc000
857 #define UDP_LOCAL_PORT_RANGE_END 0xffff
859 port = UDP_LOCAL_PORT_RANGE_START;
861 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
862 if (ipcb->local_port == port) {
863 /* port is already used by another udp_pcb */
865 /* restart scanning all udp pcbs */
868 /* go on with next udp pcb */
873 /* no more ports available in local range */
874 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
878 pcb->local_port = port;
879 snmp_insert_udpidx_tree(pcb);
880 /* pcb not active yet? */
882 /* place the PCB on the active list if not already there */
883 pcb->next = udp_pcbs;
886 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
887 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip);
888 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
893 * Connect an UDP PCB.
895 * This will associate the UDP PCB with the remote address.
897 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
898 * @param ipaddr remote IP address to connect with.
899 * @param port remote UDP port to connect with.
901 * @return lwIP error code
903 * ipaddr & port are expected to be in the same byte order as in the pcb.
905 * The udp pcb is bound to a random local port if not already bound.
907 * @see udp_disconnect()
910 udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
912 struct udp_pcb *ipcb;
914 if (pcb->local_port == 0) {
915 err_t err = udp_bind(pcb, ipX_2_ip(&pcb->local_ip), pcb->local_port);
921 ipX_addr_set_ipaddr(PCB_ISIPV6(pcb), &pcb->remote_ip, ipaddr);
922 pcb->remote_port = port;
923 pcb->flags |= UDP_FLAGS_CONNECTED;
924 /** TODO: this functionality belongs in upper layers */
927 if (!PCB_ISIPV6(pcb))
928 #endif /* LWIP_IPV6 */
930 /* Nail down local IP for netconn_addr()/getsockname() */
931 if (ip_addr_isany(ipX_2_ip(&pcb->local_ip)) && !ip_addr_isany(ipX_2_ip(&pcb->remote_ip))) {
934 if ((netif = ip_route(ipX_2_ip(&pcb->remote_ip))) == NULL) {
935 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n",
936 ip4_addr_get_u32(ipX_2_ip(&pcb->remote_ip))));
937 UDP_STATS_INC(udp.rterr);
940 /** TODO: this will bind the udp pcb locally, to the interface which
941 is used to route output packets to the remote address. However, we
942 might want to accept incoming packets on any interface! */
943 ipX_addr_copy(0, pcb->local_ip, netif->ip_addr);
944 } else if (ip_addr_isany(ipX_2_ip(&pcb->remote_ip))) {
945 ipX_addr_set_any(0, &pcb->local_ip);
949 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
950 ipX_addr_debug_print(PCB_ISIPV6(pcb), UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
952 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
954 /* Insert UDP PCB into the list of active UDP PCBs. */
955 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
957 /* already on the list, just return */
961 /* PCB not yet on the list, add PCB now */
962 pcb->next = udp_pcbs;
968 * Disconnect a UDP PCB
970 * @param pcb the udp pcb to disconnect.
973 udp_disconnect(struct udp_pcb *pcb)
975 /* reset remote address association */
976 ipX_addr_set_any(PCB_ISIPV6(pcb), &pcb->remote_ip);
977 pcb->remote_port = 0;
978 /* mark PCB as unconnected */
979 pcb->flags &= ~UDP_FLAGS_CONNECTED;
983 * Set a receive callback for a UDP PCB
985 * This callback will be called when receiving a datagram for the pcb.
987 * @param pcb the pcb for wich to set the recv callback
988 * @param recv function pointer of the callback function
989 * @param recv_arg additional argument to pass to the callback function
992 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
994 /* remember recv() callback and user data */
995 pcb->recv.ip4 = recv;
996 pcb->recv_arg = recv_arg;
1000 * Remove an UDP PCB.
1002 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
1003 * UDP PCB's and the data structure is freed from memory.
1008 udp_remove(struct udp_pcb *pcb)
1010 struct udp_pcb *pcb2;
1012 snmp_delete_udpidx_tree(pcb);
1013 /* pcb to be removed is first in list? */
1014 if (udp_pcbs == pcb) {
1015 /* make list start at 2nd pcb */
1016 udp_pcbs = udp_pcbs->next;
1017 /* pcb not 1st in list */
1019 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1020 /* find pcb in udp_pcbs list */
1021 if (pcb2->next != NULL && pcb2->next == pcb) {
1022 /* remove pcb from list */
1023 pcb2->next = pcb->next;
1027 memp_free(MEMP_UDP_PCB, pcb);
1033 * @return The UDP PCB which was created. NULL if the PCB data structure
1034 * could not be allocated.
1041 struct udp_pcb *pcb;
1042 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
1043 /* could allocate UDP PCB? */
1045 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
1046 * which means checksum is generated over the whole datagram per default
1047 * (recommended as default by RFC 3828). */
1048 /* initialize PCB to all zeroes */
1049 memset(pcb, 0, sizeof(struct udp_pcb));
1057 * Create a UDP PCB for IPv6.
1059 * @return The UDP PCB which was created. NULL if the PCB data structure
1060 * could not be allocated.
1067 struct udp_pcb *pcb;
1072 #endif /* LWIP_IPV6 */
1076 * Print UDP header information for debug purposes.
1078 * @param udphdr pointer to the udp header in memory.
1081 udp_debug_print(struct udp_hdr *udphdr)
1083 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
1084 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1085 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1086 ntohs(udphdr->src), ntohs(udphdr->dest)));
1087 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1088 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
1089 ntohs(udphdr->len), ntohs(udphdr->chksum)));
1090 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1092 #endif /* UDP_DEBUG */
1094 #endif /* LWIP_UDP */