The Pedigree Project 0.1
udp.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
32/*
33 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without modification,
37 * are permitted provided that the following conditions are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
50 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
52 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
56 * OF SUCH DAMAGE.
57 *
58 * This file is part of the lwIP TCP/IP stack.
59 *
60 * Author: Adam Dunkels <adam@sics.se>
61 *
62 */
63
64/* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
65 */
66
67#include "lwip/opt.h"
68
69#if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
70
71#include "lwip/udp.h"
72#include "lwip/def.h"
73#include "lwip/memp.h"
74#include "lwip/inet_chksum.h"
75#include "lwip/ip_addr.h"
76#include "lwip/ip6.h"
77#include "lwip/ip6_addr.h"
78#include "lwip/netif.h"
79#include "lwip/icmp.h"
80#include "lwip/icmp6.h"
81#include "lwip/stats.h"
82#include "lwip/snmp.h"
83#include "lwip/dhcp.h"
84
85#include <string.h>
86
87#ifndef UDP_LOCAL_PORT_RANGE_START
88/* From http://www.iana.org/assignments/port-numbers:
89 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
90#define UDP_LOCAL_PORT_RANGE_START 0xc000
91#define UDP_LOCAL_PORT_RANGE_END 0xffff
92#define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
93#endif
94
95/* last local UDP port */
96static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
97
98/* The list of UDP PCBs */
99/* exported in udp.h (was static) */
100struct udp_pcb *udp_pcbs;
101
105void
106udp_init(void)
107{
108#if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
109 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
110#endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
111}
112
118static u16_t
119udp_new_port(void)
120{
121 u16_t n = 0;
122 struct udp_pcb *pcb;
123
124again:
125 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
126 udp_port = UDP_LOCAL_PORT_RANGE_START;
127 }
128 /* Check all PCBs. */
129 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
130 if (pcb->local_port == udp_port) {
131 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
132 return 0;
133 }
134 goto again;
135 }
136 }
137 return udp_port;
138}
139
148static u8_t
149udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
150{
151 LWIP_UNUSED_ARG(inp); /* in IPv6 only case */
152 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
153
154 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
155 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
156#if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
157 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
158 return 0;
159 }
160#endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */
161 return 1;
162 }
163
164 /* Only need to check PCB if incoming IP version matches PCB IP version */
165 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
166#if LWIP_IPV4
167 /* Special case: IPv4 broadcast: all or broadcasts in my subnet
168 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
169 if (broadcast != 0) {
170#if IP_SOF_BROADCAST_RECV
171 if (ip_get_option(pcb, SOF_BROADCAST))
172#endif /* IP_SOF_BROADCAST_RECV */
173 {
174 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
175 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
176 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
177 return 1;
178 }
179 }
180 } else
181#endif /* LWIP_IPV4 */
182 /* Handle IPv4 and IPv6: all or exact match */
183 if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
184 return 1;
185 }
186 }
187
188 return 0;
189}
190
203void
204udp_input(struct pbuf *p, struct netif *inp)
205{
206 struct udp_hdr *udphdr;
207 struct udp_pcb *pcb, *prev;
208 struct udp_pcb *uncon_pcb;
209 u16_t src, dest;
210 u8_t broadcast;
211 u8_t for_us = 0;
212
213 LWIP_UNUSED_ARG(inp);
214
215 PERF_START;
216
217 UDP_STATS_INC(udp.recv);
218
219 /* Check minimum length (UDP header) */
220 if (p->len < UDP_HLEN) {
221 /* drop short packets */
223 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
224 UDP_STATS_INC(udp.lenerr);
225 UDP_STATS_INC(udp.drop);
226 MIB2_STATS_INC(mib2.udpinerrors);
227 pbuf_free(p);
228 goto end;
229 }
230
231 udphdr = (struct udp_hdr *)p->payload;
232
233 /* is broadcast packet ? */
234 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
235
236 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
237
238 /* convert src and dest ports to host byte order */
239 src = lwip_ntohs(udphdr->src);
240 dest = lwip_ntohs(udphdr->dest);
241
242 udp_debug_print(udphdr);
243
244 /* print the UDP source and destination */
245 LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
246 ip_addr_debug_print(UDP_DEBUG, ip_current_dest_addr());
247 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest)));
248 ip_addr_debug_print(UDP_DEBUG, ip_current_src_addr());
249 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src)));
250
251 pcb = NULL;
252 prev = NULL;
253 uncon_pcb = NULL;
254 /* Iterate through the UDP pcb list for a matching pcb.
255 * 'Perfect match' pcbs (connected to the remote port & ip address) are
256 * preferred. If no perfect match is found, the first unconnected pcb that
257 * matches the local port and ip address gets the datagram. */
258 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
259 /* print the PCB local and remote address */
260 LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
261 ip_addr_debug_print(UDP_DEBUG, &pcb->local_ip);
262 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
263 ip_addr_debug_print(UDP_DEBUG, &pcb->remote_ip);
264 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
265
266 /* compare PCB local addr+port to UDP destination addr+port */
267 if ((pcb->local_port == dest) &&
268 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
269 if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
270 ((uncon_pcb == NULL)
271#if SO_REUSE
272 /* prefer specific IPs over cath-all */
273 || !ip_addr_isany(&pcb->local_ip)
274#endif /* SO_REUSE */
275 )) {
276 /* the first unconnected matching PCB */
277 uncon_pcb = pcb;
278 }
279
280 /* compare PCB remote addr+port to UDP source addr+port */
281 if ((pcb->remote_port == src) &&
282 (ip_addr_isany_val(pcb->remote_ip) ||
283 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
284 /* the first fully matching PCB */
285 if (prev != NULL) {
286 /* move the pcb to the front of udp_pcbs so that is
287 found faster next time */
288 prev->next = pcb->next;
289 pcb->next = udp_pcbs;
290 udp_pcbs = pcb;
291 } else {
292 UDP_STATS_INC(udp.cachehit);
293 }
294 break;
295 }
296 }
297
298 prev = pcb;
299 }
300 /* no fully matching pcb found? then look for an unconnected pcb */
301 if (pcb == NULL) {
302 pcb = uncon_pcb;
303 }
304
305 /* Check checksum if this is a match or if it was directed at us. */
306 if (pcb != NULL) {
307 for_us = 1;
308 } else {
309#if LWIP_IPV6
310 if (ip_current_is_v6()) {
311 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
312 }
313#endif /* LWIP_IPV6 */
314#if LWIP_IPV4
315 if (!ip_current_is_v6()) {
316 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
317 }
318#endif /* LWIP_IPV4 */
319 }
320
321 if (for_us) {
322 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
323#if CHECKSUM_CHECK_UDP
324 IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) {
325#if LWIP_UDPLITE
326 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
327 /* Do the UDP Lite checksum */
328 u16_t chklen = lwip_ntohs(udphdr->len);
329 if (chklen < sizeof(struct udp_hdr)) {
330 if (chklen == 0) {
331 /* For UDP-Lite, checksum length of 0 means checksum
332 over the complete packet (See RFC 3828 chap. 3.1) */
333 chklen = p->tot_len;
334 } else {
335 /* At least the UDP-Lite header must be covered by the
336 checksum! (Again, see RFC 3828 chap. 3.1) */
337 goto chkerr;
338 }
339 }
340 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
341 p->tot_len, chklen,
343 goto chkerr;
344 }
345 } else
346#endif /* LWIP_UDPLITE */
347 {
348 if (udphdr->chksum != 0) {
349 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
351 ip_current_dest_addr()) != 0) {
352 goto chkerr;
353 }
354 }
355 }
356 }
357#endif /* CHECKSUM_CHECK_UDP */
358 if (pbuf_header(p, -UDP_HLEN)) {
359 /* Can we cope with this failing? Just assert for now */
360 LWIP_ASSERT("pbuf_header failed\n", 0);
361 UDP_STATS_INC(udp.drop);
362 MIB2_STATS_INC(mib2.udpinerrors);
363 pbuf_free(p);
364 goto end;
365 }
366
367 if (pcb != NULL) {
368 MIB2_STATS_INC(mib2.udpindatagrams);
369#if SO_REUSE && SO_REUSE_RXTOALL
370 if (ip_get_option(pcb, SOF_REUSEADDR) &&
371 (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) {
372 /* pass broadcast- or multicast packets to all multicast pcbs
373 if SOF_REUSEADDR is set on the first match */
374 struct udp_pcb *mpcb;
375 u8_t p_header_changed = 0;
376 s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN);
377 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
378 if (mpcb != pcb) {
379 /* compare PCB local addr+port to UDP destination addr+port */
380 if ((mpcb->local_port == dest) &&
381 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
382 /* pass a copy of the packet to all local matches */
383 if (mpcb->recv != NULL) {
384 struct pbuf *q;
385 /* for that, move payload to IP header again */
386 if (p_header_changed == 0) {
387 pbuf_header_force(p, hdrs_len);
388 p_header_changed = 1;
389 }
391 if (q != NULL) {
392 err_t err = pbuf_copy(q, p);
393 if (err == ERR_OK) {
394 /* move payload to UDP data */
395 pbuf_header(q, -hdrs_len);
396 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
397 }
398 }
399 }
400 }
401 }
402 }
403 if (p_header_changed) {
404 /* and move payload to UDP data again */
405 pbuf_header(p, -hdrs_len);
406 }
407 }
408#endif /* SO_REUSE && SO_REUSE_RXTOALL */
409 /* callback */
410 if (pcb->recv != NULL) {
411 /* now the recv function is responsible for freeing p */
412 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
413 } else {
414 /* no recv function registered? then we have to free the pbuf! */
415 pbuf_free(p);
416 goto end;
417 }
418 } else {
419 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
420
421#if LWIP_ICMP || LWIP_ICMP6
422 /* No match was found, send ICMP destination port unreachable unless
423 destination address was broadcast/multicast. */
424 if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) {
425 /* move payload pointer back to ip header */
426 pbuf_header_force(p, (s16_t)(ip_current_header_tot_len() + UDP_HLEN));
427 icmp_port_unreach(ip_current_is_v6(), p);
428 }
429#endif /* LWIP_ICMP || LWIP_ICMP6 */
430 UDP_STATS_INC(udp.proterr);
431 UDP_STATS_INC(udp.drop);
432 MIB2_STATS_INC(mib2.udpnoports);
433 pbuf_free(p);
434 }
435 } else {
436 pbuf_free(p);
437 }
438end:
439 PERF_STOP("udp_input");
440 return;
441#if CHECKSUM_CHECK_UDP
442chkerr:
444 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
445 UDP_STATS_INC(udp.chkerr);
446 UDP_STATS_INC(udp.drop);
447 MIB2_STATS_INC(mib2.udpinerrors);
448 pbuf_free(p);
449 PERF_STOP("udp_input");
450#endif /* CHECKSUM_CHECK_UDP */
451}
452
473err_t
474udp_send(struct udp_pcb *pcb, struct pbuf *p)
475{
476 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
477 return ERR_VAL;
478 }
479
480 /* send to the packet using remote ip and port stored in the pcb */
481 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
482}
483
484#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
488err_t
489udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
490 u8_t have_chksum, u16_t chksum)
491{
492 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
493 return ERR_VAL;
494 }
495
496 /* send to the packet using remote ip and port stored in the pcb */
497 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
498 have_chksum, chksum);
499}
500#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
501
520err_t
521udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
522 const ip_addr_t *dst_ip, u16_t dst_port)
523{
524#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
525 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
526}
527
530err_t
531udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
532 u16_t dst_port, u8_t have_chksum, u16_t chksum)
533{
534#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
535 struct netif *netif;
536 const ip_addr_t *dst_ip_route = dst_ip;
537
538 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
539 return ERR_VAL;
540 }
541
542 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
543
544#if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS)
545 if (ip_addr_ismulticast(dst_ip_route)) {
546#if LWIP_IPV6
547 if (IP_IS_V6(dst_ip)) {
548 /* For multicast, find a netif based on source address. */
549 dst_ip_route = &pcb->local_ip;
550 } else
551#endif /* LWIP_IPV6 */
552 {
553#if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
554 /* IPv4 does not use source-based routing by default, so we use an
555 administratively selected interface for multicast by default.
556 However, this can be overridden by setting an interface address
557 in pcb->multicast_ip that is used for routing. */
558 if (!ip_addr_isany_val(pcb->multicast_ip) &&
559 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
560 dst_ip_route = &pcb->multicast_ip;
561 }
562#endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
563 }
564 }
565#endif /* LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) */
566
567 /* find the outgoing network interface for this packet */
568 if(IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
569 /* Don't call ip_route() with IP_ANY_TYPE */
570 netif = ip_route(IP46_ADDR_ANY(IP_GET_TYPE(dst_ip_route)), dst_ip_route);
571 } else {
572 netif = ip_route(&pcb->local_ip, dst_ip_route);
573 }
574
575 /* no outgoing network interface could be found? */
576 if (netif == NULL) {
577 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
578 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip);
579 LWIP_DEBUGF(UDP_DEBUG, ("\n"));
580 UDP_STATS_INC(udp.rterr);
581 return ERR_RTE;
582 }
583#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
584 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
585#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
586 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
587#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
588}
589
610err_t
611udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
612 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
613{
614#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
615 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
616}
617
619err_t
620udp_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,
622 u16_t chksum)
623{
624#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
625 const ip_addr_t *src_ip;
626
627 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
628 return ERR_VAL;
629 }
630
631 /* PCB local address is IP_ANY_ADDR? */
632#if LWIP_IPV6
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. */
638 return ERR_RTE;
639 }
640 } else {
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. */
644 return ERR_RTE;
645 }
646 src_ip = &pcb->local_ip;
647 }
648 }
649#endif /* LWIP_IPV6 */
650#if LWIP_IPV4 && LWIP_IPV6
651 else
652#endif /* LWIP_IPV4 && LWIP_IPV6 */
653#if LWIP_IPV4
654 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
655 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
656 /* if the local_ip is any or multicast
657 * use the outgoing network interface IP address as source address */
658 src_ip = netif_ip_addr4(netif);
659 } else {
660 /* check if UDP PCB local IP address is correct
661 * this could be an old address if netif->ip_addr has changed */
662 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
663 /* local_ip doesn't match, drop the packet */
664 return ERR_RTE;
665 }
666 /* use UDP PCB local IP address as source address */
667 src_ip = &pcb->local_ip;
668 }
669#endif /* LWIP_IPV4 */
670#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
671 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
672#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
673 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
674#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
675}
676
679err_t
680udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
681 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
682{
683#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
684 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
685}
686
688err_t
689udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
690 u16_t dst_port, struct netif *netif, u8_t have_chksum,
691 u16_t chksum, const ip_addr_t *src_ip)
692{
693#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
694 struct udp_hdr *udphdr;
695 err_t err;
696 struct pbuf *q; /* q will be sent down the stack */
697 u8_t ip_proto;
698 u8_t ttl;
699
700 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
701 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
702 return ERR_VAL;
703 }
704
705#if LWIP_IPV4 && IP_SOF_BROADCAST
706 /* broadcast filter? */
707 if (!ip_get_option(pcb, SOF_BROADCAST) &&
708#if LWIP_IPV6
709 IP_IS_V4(dst_ip) &&
710#endif /* LWIP_IPV6 */
711 ip_addr_isbroadcast(dst_ip, netif)) {
713 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
714 return ERR_VAL;
715 }
716#endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
717
718 /* if the PCB is not yet bound to a port, bind it here */
719 if (pcb->local_port == 0) {
720 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
721 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
722 if (err != ERR_OK) {
723 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
724 return err;
725 }
726 }
727
728 /* not enough space to add an UDP header to first pbuf in given p chain? */
729 if (pbuf_header(p, UDP_HLEN)) {
730 /* allocate header in a separate new pbuf */
731 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
732 /* new header pbuf could not be allocated? */
733 if (q == NULL) {
734 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
735 return ERR_MEM;
736 }
737 if (p->tot_len != 0) {
738 /* chain header q in front of given pbuf p (only if p contains data) */
739 pbuf_chain(q, p);
740 }
741 /* first pbuf q points to header pbuf */
743 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
744 } else {
745 /* adding space for header within p succeeded */
746 /* first pbuf q equals given pbuf */
747 q = p;
748 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
749 }
750 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
751 (q->len >= sizeof(struct udp_hdr)));
752 /* q now represents the packet to be sent */
753 udphdr = (struct udp_hdr *)q->payload;
754 udphdr->src = lwip_htons(pcb->local_port);
755 udphdr->dest = lwip_htons(dst_port);
756 /* in UDP, 0 checksum means 'no checksum' */
757 udphdr->chksum = 0x0000;
758
759 /* Multicast Loop? */
760#if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD)
761 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
763 }
764#endif /* (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
765
766 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
767
768#if LWIP_UDPLITE
769 /* UDP Lite protocol? */
770 if (pcb->flags & UDP_FLAGS_UDPLITE) {
771 u16_t chklen, chklen_hdr;
772 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
773 /* set UDP message length in UDP header */
774 chklen_hdr = chklen = pcb->chksum_len_tx;
775 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
776 if (chklen != 0) {
777 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
778 }
779 /* For UDP-Lite, checksum length of 0 means checksum
780 over the complete packet. (See RFC 3828 chap. 3.1)
781 At least the UDP-Lite header must be covered by the
782 checksum, therefore, if chksum_len has an illegal
783 value, we generate the checksum over the complete
784 packet to be safe. */
785 chklen_hdr = 0;
786 chklen = q->tot_len;
787 }
788 udphdr->len = lwip_htons(chklen_hdr);
789 /* calculate checksum */
790#if CHECKSUM_GEN_UDP
791 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
792#if LWIP_CHECKSUM_ON_COPY
793 if (have_chksum) {
794 chklen = UDP_HLEN;
795 }
796#endif /* LWIP_CHECKSUM_ON_COPY */
797 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
798 q->tot_len, chklen, src_ip, dst_ip);
799#if LWIP_CHECKSUM_ON_COPY
800 if (have_chksum) {
801 u32_t acc;
802 acc = udphdr->chksum + (u16_t)~(chksum);
803 udphdr->chksum = FOLD_U32T(acc);
804 }
805#endif /* LWIP_CHECKSUM_ON_COPY */
806
807 /* chksum zero must become 0xffff, as zero means 'no checksum' */
808 if (udphdr->chksum == 0x0000) {
809 udphdr->chksum = 0xffff;
810 }
811 }
812#endif /* CHECKSUM_GEN_UDP */
813
814 ip_proto = IP_PROTO_UDPLITE;
815 } else
816#endif /* LWIP_UDPLITE */
817 { /* UDP */
818 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
819 udphdr->len = lwip_htons(q->tot_len);
820 /* calculate checksum */
821#if CHECKSUM_GEN_UDP
822 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
823 /* Checksum is mandatory over IPv6. */
824 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
825 u16_t udpchksum;
826#if LWIP_CHECKSUM_ON_COPY
827 if (have_chksum) {
828 u32_t acc;
829 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
830 q->tot_len, UDP_HLEN, src_ip, dst_ip);
831 acc = udpchksum + (u16_t)~(chksum);
832 udpchksum = FOLD_U32T(acc);
833 } else
834#endif /* LWIP_CHECKSUM_ON_COPY */
835 {
836 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
837 src_ip, dst_ip);
838 }
839
840 /* chksum zero must become 0xffff, as zero means 'no checksum' */
841 if (udpchksum == 0x0000) {
842 udpchksum = 0xffff;
843 }
844 udphdr->chksum = udpchksum;
845 }
846 }
847#endif /* CHECKSUM_GEN_UDP */
848 ip_proto = IP_PROTO_UDP;
849 }
850
851 /* Determine TTL to use */
852#if LWIP_MULTICAST_TX_OPTIONS
853 ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
854#else /* LWIP_MULTICAST_TX_OPTIONS */
855 ttl = pcb->ttl;
856#endif /* LWIP_MULTICAST_TX_OPTIONS */
857
858 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
859 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
860 /* output to IP */
861 NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
862 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
863 NETIF_SET_HWADDRHINT(netif, NULL);
864
865 /* @todo: must this be increased even if error occurred? */
866 MIB2_STATS_INC(mib2.udpoutdatagrams);
867
868 /* did we chain a separate header pbuf earlier? */
869 if (q != p) {
870 /* free the header pbuf */
871 pbuf_free(q);
872 q = NULL;
873 /* p is still referenced by the caller, and will live on */
874 }
875
876 UDP_STATS_INC(udp.xmit);
877 return err;
878}
879
900err_t
901udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
902{
903 struct udp_pcb *ipcb;
904 u8_t rebind;
905
906#if LWIP_IPV4
907 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
908 if (ipaddr == NULL) {
909 ipaddr = IP4_ADDR_ANY;
910 }
911#endif /* LWIP_IPV4 */
912
913 /* still need to check for ipaddr == NULL in IPv6 only case */
914 if ((pcb == NULL) || (ipaddr == NULL)) {
915 return ERR_VAL;
916 }
917
918 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
919 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr);
920 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
921
922 rebind = 0;
923 /* Check for double bind and rebind of the same pcb */
924 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
925 /* is this UDP PCB already on active list? */
926 if (pcb == ipcb) {
927 rebind = 1;
928 break;
929 }
930 }
931
932 /* no port specified? */
933 if (port == 0) {
934 port = udp_new_port();
935 if (port == 0) {
936 /* no more ports available in local range */
937 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
938 return ERR_USE;
939 }
940 } else {
941 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
942 if (pcb != ipcb) {
943 /* By default, we don't allow to bind to a port that any other udp
944 PCB is already bound to, unless *all* PCBs with that port have tha
945 REUSEADDR flag set. */
946#if SO_REUSE
947 if (!ip_get_option(pcb, SOF_REUSEADDR) ||
948 !ip_get_option(ipcb, SOF_REUSEADDR))
949#endif /* SO_REUSE */
950 {
951 /* port matches that of PCB in list and REUSEADDR not set -> reject */
952 if ((ipcb->local_port == port) &&
953 /* IP address matches? */
954 ip_addr_cmp(&ipcb->local_ip, ipaddr)) {
955 /* other PCB already binds to this local IP and port */
957 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
958 return ERR_USE;
959 }
960 }
961 }
962 }
963 }
964
965 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
966
967 pcb->local_port = port;
968 mib2_udp_bind(pcb);
969 /* pcb not active yet? */
970 if (rebind == 0) {
971 /* place the PCB on the active list if not already there */
972 pcb->next = udp_pcbs;
973 udp_pcbs = pcb;
974 }
975 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
976 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip);
977 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
978 return ERR_OK;
979}
980
999err_t
1000udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
1001{
1002 struct udp_pcb *ipcb;
1003
1004 if ((pcb == NULL) || (ipaddr == NULL)) {
1005 return ERR_VAL;
1006 }
1007
1008 if (pcb->local_port == 0) {
1009 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1010 if (err != ERR_OK) {
1011 return err;
1012 }
1013 }
1014
1015 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
1016 pcb->remote_port = port;
1017 pcb->flags |= UDP_FLAGS_CONNECTED;
1018
1019 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
1020 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
1021 &pcb->remote_ip);
1022 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
1023
1024 /* Insert UDP PCB into the list of active UDP PCBs. */
1025 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
1026 if (pcb == ipcb) {
1027 /* already on the list, just return */
1028 return ERR_OK;
1029 }
1030 }
1031 /* PCB not yet on the list, add PCB now */
1032 pcb->next = udp_pcbs;
1033 udp_pcbs = pcb;
1034 return ERR_OK;
1035}
1036
1043void
1044udp_disconnect(struct udp_pcb *pcb)
1045{
1046 /* reset remote address association */
1047#if LWIP_IPV4 && LWIP_IPV6
1048 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
1049 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
1050 } else {
1051#endif
1052 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
1053#if LWIP_IPV4 && LWIP_IPV6
1054 }
1055#endif
1056 pcb->remote_port = 0;
1057 /* mark PCB as unconnected */
1058 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1059}
1060
1071void
1072udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
1073{
1074 /* remember recv() callback and user data */
1075 pcb->recv = recv;
1076 pcb->recv_arg = recv_arg;
1077}
1078
1088void
1089udp_remove(struct udp_pcb *pcb)
1090{
1091 struct udp_pcb *pcb2;
1092
1093 mib2_udp_unbind(pcb);
1094 /* pcb to be removed is first in list? */
1095 if (udp_pcbs == pcb) {
1096 /* make list start at 2nd pcb */
1097 udp_pcbs = udp_pcbs->next;
1098 /* pcb not 1st in list */
1099 } else {
1100 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1101 /* find pcb in udp_pcbs list */
1102 if (pcb2->next != NULL && pcb2->next == pcb) {
1103 /* remove pcb from list */
1104 pcb2->next = pcb->next;
1105 break;
1106 }
1107 }
1108 }
1109 memp_free(MEMP_UDP_PCB, pcb);
1110}
1111
1121struct udp_pcb *
1122udp_new(void)
1123{
1124 struct udp_pcb *pcb;
1125 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
1126 /* could allocate UDP PCB? */
1127 if (pcb != NULL) {
1128 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
1129 * which means checksum is generated over the whole datagram per default
1130 * (recommended as default by RFC 3828). */
1131 /* initialize PCB to all zeroes */
1132 memset(pcb, 0, sizeof(struct udp_pcb));
1133 pcb->ttl = UDP_TTL;
1134#if LWIP_MULTICAST_TX_OPTIONS
1135 udp_set_multicast_ttl(pcb, UDP_TTL);
1136#endif /* LWIP_MULTICAST_TX_OPTIONS */
1137 }
1138 return pcb;
1139}
1140
1153struct udp_pcb *
1154udp_new_ip_type(u8_t type)
1155{
1156 struct udp_pcb *pcb;
1157 pcb = udp_new();
1158#if LWIP_IPV4 && LWIP_IPV6
1159 if (pcb != NULL) {
1160 IP_SET_TYPE_VAL(pcb->local_ip, type);
1161 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1162 }
1163#else
1164 LWIP_UNUSED_ARG(type);
1165#endif /* LWIP_IPV4 && LWIP_IPV6 */
1166 return pcb;
1167}
1168
1174void udp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
1175{
1176 struct udp_pcb* upcb;
1177
1178 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
1179 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
1180 /* PCB bound to current local interface address? */
1181 if (ip_addr_cmp(&upcb->local_ip, old_addr)) {
1182 /* The PCB is bound to the old ipaddr and
1183 * is set to bound to the new one instead */
1184 ip_addr_copy(upcb->local_ip, *new_addr);
1185 }
1186 }
1187 }
1188}
1189
1190#if UDP_DEBUG
1196void
1197udp_debug_print(struct udp_hdr *udphdr)
1198{
1199 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
1200 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1201 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1202 lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest)));
1203 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1204 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
1205 lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum)));
1206 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1207}
1208#endif /* UDP_DEBUG */
1209
1210#endif /* LWIP_UDP */
#define LWIP_DEBUGF(debug, message)
Definition debug.h:183
#define LWIP_UNUSED_ARG(x)
Definition arch.h:327
#define LWIP_DBG_LEVEL_SERIOUS
Definition debug.h:76
#define LWIP_DBG_STATE
Definition debug.h:104
#define LWIP_DBG_TRACE
Definition debug.h:102
s8_t err_t
Definition err.h:76
@ ERR_USE
Definition err.h:98
@ ERR_RTE
Definition err.h:90
@ ERR_OK
Definition err.h:82
@ ERR_VAL
Definition err.h:94
@ ERR_MEM
Definition err.h:84
#define CHECKSUM_CHECK_UDP
Definition opt.h:2123
#define UDP_DEBUG
Definition opt.h:2826
#define UDP_TTL
Definition opt.h:1124
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition pbuf.c:901
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition pbuf.c:267
u8_t pbuf_free(struct pbuf *p)
Definition pbuf.c:734
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition pbuf.c:967
@ PBUF_RAM
Definition pbuf.h:127
@ PBUF_RAW
Definition pbuf.h:113
@ PBUF_IP
Definition pbuf.h:99
#define FOLD_U32T(u)
Definition inet_chksum.h:71
#define ip_get_option(pcb, opt)
Definition ip.h:234
#define ip_current_header_tot_len()
Definition ip.h:158
#define ip_current_src_addr()
Definition ip.h:160
#define ip_current_dest_addr()
Definition ip.h:162
#define ip_current_netif()
Definition ip.h:152
void * memp_malloc(memp_t type)
Definition memp.c:404
void memp_free(memp_t type, void *mem)
Definition memp.c:488
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition pbuf.c:684
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
Definition pbuf.c:694
#define PBUF_FLAG_MCASTLOOP
Definition pbuf.h:152
Definition netif.h:244
Definition pbuf.h:161
u16_t tot_len
Definition pbuf.h:175
u16_t len
Definition pbuf.h:178
void * payload
Definition pbuf.h:166
u8_t flags
Definition pbuf.h:184