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