BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file |
||
3 | * User Datagram Protocol module\n |
||
4 | * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).\n |
||
5 | * See also @ref udp_raw |
||
6 | * |
||
7 | * @defgroup udp_raw UDP |
||
8 | * @ingroup callbackstyle_api |
||
9 | * User Datagram Protocol module\n |
||
10 | * @see @ref raw_api and @ref netconn |
||
11 | */ |
||
12 | |||
13 | /* |
||
14 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
||
15 | * All rights reserved. |
||
16 | * |
||
17 | * Redistribution and use in source and binary forms, with or without modification, |
||
18 | * are permitted provided that the following conditions are met: |
||
19 | * |
||
20 | * 1. Redistributions of source code must retain the above copyright notice, |
||
21 | * this list of conditions and the following disclaimer. |
||
22 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
23 | * this list of conditions and the following disclaimer in the documentation |
||
24 | * and/or other materials provided with the distribution. |
||
25 | * 3. The name of the author may not be used to endorse or promote products |
||
26 | * derived from this software without specific prior written permission. |
||
27 | * |
||
28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
||
29 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
||
31 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
32 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
||
33 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
34 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
35 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
36 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
||
37 | * OF SUCH DAMAGE. |
||
38 | * |
||
39 | * This file is part of the lwIP TCP/IP stack. |
||
40 | * |
||
41 | * Author: Adam Dunkels <adam@sics.se> |
||
42 | * |
||
43 | */ |
||
44 | |||
45 | /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! |
||
46 | */ |
||
47 | |||
48 | #include "lwip/opt.h" |
||
49 | |||
50 | #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ |
||
51 | |||
52 | #include "lwip/udp.h" |
||
53 | #include "lwip/def.h" |
||
54 | #include "lwip/memp.h" |
||
55 | #include "lwip/inet_chksum.h" |
||
56 | #include "lwip/ip_addr.h" |
||
57 | #include "lwip/ip6.h" |
||
58 | #include "lwip/ip6_addr.h" |
||
59 | #include "lwip/netif.h" |
||
60 | #include "lwip/icmp.h" |
||
61 | #include "lwip/icmp6.h" |
||
62 | #include "lwip/stats.h" |
||
63 | #include "lwip/snmp.h" |
||
64 | #include "lwip/dhcp.h" |
||
65 | |||
66 | #include <string.h> |
||
67 | |||
68 | #ifndef UDP_LOCAL_PORT_RANGE_START |
||
69 | /* From http://www.iana.org/assignments/port-numbers: |
||
70 | "The Dynamic and/or Private Ports are those from 49152 through 65535" */ |
||
71 | #define UDP_LOCAL_PORT_RANGE_START 0xc000 |
||
72 | #define UDP_LOCAL_PORT_RANGE_END 0xffff |
||
73 | #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & (u16_t)~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)) |
||
74 | #endif |
||
75 | |||
76 | /* last local UDP port */ |
||
77 | static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START; |
||
78 | |||
79 | /* The list of UDP PCBs */ |
||
80 | /* exported in udp.h (was static) */ |
||
81 | struct udp_pcb *udp_pcbs; |
||
82 | |||
83 | /** |
||
84 | * Initialize this module. |
||
85 | */ |
||
86 | void |
||
87 | udp_init(void) |
||
88 | { |
||
89 | #ifdef LWIP_RAND |
||
90 | udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND()); |
||
91 | #endif /* LWIP_RAND */ |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Allocate a new local UDP port. |
||
96 | * |
||
97 | * @return a new (free) local UDP port number |
||
98 | */ |
||
99 | static u16_t |
||
100 | udp_new_port(void) |
||
101 | { |
||
102 | u16_t n = 0; |
||
103 | struct udp_pcb *pcb; |
||
104 | |||
105 | again: |
||
106 | if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) { |
||
107 | udp_port = UDP_LOCAL_PORT_RANGE_START; |
||
108 | } |
||
109 | /* Check all PCBs. */ |
||
110 | for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { |
||
111 | if (pcb->local_port == udp_port) { |
||
112 | if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) { |
||
113 | return 0; |
||
114 | } |
||
115 | goto again; |
||
116 | } |
||
117 | } |
||
118 | return udp_port; |
||
119 | } |
||
120 | |||
121 | /** Common code to see if the current input packet matches the pcb |
||
122 | * (current input packet is accessed via ip(4/6)_current_* macros) |
||
123 | * |
||
124 | * @param pcb pcb to check |
||
125 | * @param inp network interface on which the datagram was received (only used for IPv4) |
||
126 | * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4) |
||
127 | * @return 1 on match, 0 otherwise |
||
128 | */ |
||
129 | static u8_t |
||
130 | udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast) |
||
131 | { |
||
132 | LWIP_UNUSED_ARG(inp); /* in IPv6 only case */ |
||
133 | LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */ |
||
134 | |||
135 | /* check if PCB is bound to specific netif */ |
||
136 | if ((pcb->netif_idx != NETIF_NO_INDEX) && |
||
137 | (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { |
||
138 | return 0; |
||
139 | } |
||
140 | |||
141 | /* Dual-stack: PCBs listening to any IP type also listen to any IP address */ |
||
142 | if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { |
||
143 | #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV |
||
144 | if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { |
||
145 | return 0; |
||
146 | } |
||
147 | #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */ |
||
148 | return 1; |
||
149 | } |
||
150 | |||
151 | /* Only need to check PCB if incoming IP version matches PCB IP version */ |
||
152 | if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { |
||
153 | #if LWIP_IPV4 |
||
154 | /* Special case: IPv4 broadcast: all or broadcasts in my subnet |
||
155 | * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */ |
||
156 | if (broadcast != 0) { |
||
157 | #if IP_SOF_BROADCAST_RECV |
||
158 | if (ip_get_option(pcb, SOF_BROADCAST)) |
||
159 | #endif /* IP_SOF_BROADCAST_RECV */ |
||
160 | { |
||
161 | if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || |
||
162 | ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) || |
||
163 | ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) { |
||
164 | return 1; |
||
165 | } |
||
166 | } |
||
167 | } else |
||
168 | #endif /* LWIP_IPV4 */ |
||
169 | /* Handle IPv4 and IPv6: all or exact match */ |
||
170 | if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { |
||
171 | return 1; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return 0; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Process an incoming UDP datagram. |
||
180 | * |
||
181 | * Given an incoming UDP datagram (as a chain of pbufs) this function |
||
182 | * finds a corresponding UDP PCB and hands over the pbuf to the pcbs |
||
183 | * recv function. If no pcb is found or the datagram is incorrect, the |
||
184 | * pbuf is freed. |
||
185 | * |
||
186 | * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header) |
||
187 | * @param inp network interface on which the datagram was received. |
||
188 | * |
||
189 | */ |
||
190 | void |
||
191 | udp_input(struct pbuf *p, struct netif *inp) |
||
192 | { |
||
193 | struct udp_hdr *udphdr; |
||
194 | struct udp_pcb *pcb, *prev; |
||
195 | struct udp_pcb *uncon_pcb; |
||
196 | u16_t src, dest; |
||
197 | u8_t broadcast; |
||
198 | u8_t for_us = 0; |
||
199 | |||
200 | LWIP_UNUSED_ARG(inp); |
||
201 | |||
202 | PERF_START; |
||
203 | |||
204 | UDP_STATS_INC(udp.recv); |
||
205 | |||
206 | /* Check minimum length (UDP header) */ |
||
207 | if (p->len < UDP_HLEN) { |
||
208 | /* drop short packets */ |
||
209 | LWIP_DEBUGF(UDP_DEBUG, |
||
210 | ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); |
||
211 | UDP_STATS_INC(udp.lenerr); |
||
212 | UDP_STATS_INC(udp.drop); |
||
213 | MIB2_STATS_INC(mib2.udpinerrors); |
||
214 | pbuf_free(p); |
||
215 | goto end; |
||
216 | } |
||
217 | |||
218 | udphdr = (struct udp_hdr *)p->payload; |
||
219 | |||
220 | /* is broadcast packet ? */ |
||
221 | broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()); |
||
222 | |||
223 | LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); |
||
224 | |||
225 | /* convert src and dest ports to host byte order */ |
||
226 | src = lwip_ntohs(udphdr->src); |
||
227 | dest = lwip_ntohs(udphdr->dest); |
||
228 | |||
229 | udp_debug_print(udphdr); |
||
230 | |||
231 | /* print the UDP source and destination */ |
||
232 | LWIP_DEBUGF(UDP_DEBUG, ("udp (")); |
||
233 | ip_addr_debug_print_val(UDP_DEBUG, *ip_current_dest_addr()); |
||
234 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest))); |
||
235 | ip_addr_debug_print_val(UDP_DEBUG, *ip_current_src_addr()); |
||
236 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src))); |
||
237 | |||
238 | pcb = NULL; |
||
239 | prev = NULL; |
||
240 | uncon_pcb = NULL; |
||
241 | /* Iterate through the UDP pcb list for a matching pcb. |
||
242 | * 'Perfect match' pcbs (connected to the remote port & ip address) are |
||
243 | * preferred. If no perfect match is found, the first unconnected pcb that |
||
244 | * matches the local port and ip address gets the datagram. */ |
||
245 | for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { |
||
246 | /* print the PCB local and remote address */ |
||
247 | LWIP_DEBUGF(UDP_DEBUG, ("pcb (")); |
||
248 | ip_addr_debug_print_val(UDP_DEBUG, pcb->local_ip); |
||
249 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port)); |
||
250 | ip_addr_debug_print_val(UDP_DEBUG, pcb->remote_ip); |
||
251 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port)); |
||
252 | |||
253 | /* compare PCB local addr+port to UDP destination addr+port */ |
||
254 | if ((pcb->local_port == dest) && |
||
255 | (udp_input_local_match(pcb, inp, broadcast) != 0)) { |
||
256 | if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) && |
||
257 | ((uncon_pcb == NULL) |
||
258 | #if SO_REUSE |
||
259 | /* prefer specific IPs over cath-all */ |
||
260 | || !ip_addr_isany(&pcb->local_ip) |
||
261 | #endif /* SO_REUSE */ |
||
262 | )) { |
||
263 | /* the first unconnected matching PCB */ |
||
264 | uncon_pcb = pcb; |
||
265 | } |
||
266 | |||
267 | /* compare PCB remote addr+port to UDP source addr+port */ |
||
268 | if ((pcb->remote_port == src) && |
||
269 | (ip_addr_isany_val(pcb->remote_ip) || |
||
270 | ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) { |
||
271 | /* the first fully matching PCB */ |
||
272 | if (prev != NULL) { |
||
273 | /* move the pcb to the front of udp_pcbs so that is |
||
274 | found faster next time */ |
||
275 | prev->next = pcb->next; |
||
276 | pcb->next = udp_pcbs; |
||
277 | udp_pcbs = pcb; |
||
278 | } else { |
||
279 | UDP_STATS_INC(udp.cachehit); |
||
280 | } |
||
281 | break; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | prev = pcb; |
||
286 | } |
||
287 | /* no fully matching pcb found? then look for an unconnected pcb */ |
||
288 | if (pcb == NULL) { |
||
289 | pcb = uncon_pcb; |
||
290 | } |
||
291 | |||
292 | /* Check checksum if this is a match or if it was directed at us. */ |
||
293 | if (pcb != NULL) { |
||
294 | for_us = 1; |
||
295 | } else { |
||
296 | #if LWIP_IPV6 |
||
297 | if (ip_current_is_v6()) { |
||
298 | for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0; |
||
299 | } |
||
300 | #endif /* LWIP_IPV6 */ |
||
301 | #if LWIP_IPV4 |
||
302 | if (!ip_current_is_v6()) { |
||
303 | for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr()); |
||
304 | } |
||
305 | #endif /* LWIP_IPV4 */ |
||
306 | } |
||
307 | |||
308 | if (for_us) { |
||
309 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); |
||
310 | #if CHECKSUM_CHECK_UDP |
||
311 | IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) { |
||
312 | #if LWIP_UDPLITE |
||
313 | if (ip_current_header_proto() == IP_PROTO_UDPLITE) { |
||
314 | /* Do the UDP Lite checksum */ |
||
315 | u16_t chklen = lwip_ntohs(udphdr->len); |
||
316 | if (chklen < sizeof(struct udp_hdr)) { |
||
317 | if (chklen == 0) { |
||
318 | /* For UDP-Lite, checksum length of 0 means checksum |
||
319 | over the complete packet (See RFC 3828 chap. 3.1) */ |
||
320 | chklen = p->tot_len; |
||
321 | } else { |
||
322 | /* At least the UDP-Lite header must be covered by the |
||
323 | checksum! (Again, see RFC 3828 chap. 3.1) */ |
||
324 | goto chkerr; |
||
325 | } |
||
326 | } |
||
327 | if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE, |
||
328 | p->tot_len, chklen, |
||
329 | ip_current_src_addr(), ip_current_dest_addr()) != 0) { |
||
330 | goto chkerr; |
||
331 | } |
||
332 | } else |
||
333 | #endif /* LWIP_UDPLITE */ |
||
334 | { |
||
335 | if (udphdr->chksum != 0) { |
||
336 | if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len, |
||
337 | ip_current_src_addr(), |
||
338 | ip_current_dest_addr()) != 0) { |
||
339 | goto chkerr; |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | #endif /* CHECKSUM_CHECK_UDP */ |
||
345 | if (pbuf_remove_header(p, UDP_HLEN)) { |
||
346 | /* Can we cope with this failing? Just assert for now */ |
||
347 | LWIP_ASSERT("pbuf_remove_header failed\n", 0); |
||
348 | UDP_STATS_INC(udp.drop); |
||
349 | MIB2_STATS_INC(mib2.udpinerrors); |
||
350 | pbuf_free(p); |
||
351 | goto end; |
||
352 | } |
||
353 | |||
354 | if (pcb != NULL) { |
||
355 | MIB2_STATS_INC(mib2.udpindatagrams); |
||
356 | #if SO_REUSE && SO_REUSE_RXTOALL |
||
357 | if (ip_get_option(pcb, SOF_REUSEADDR) && |
||
358 | (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) { |
||
359 | /* pass broadcast- or multicast packets to all multicast pcbs |
||
360 | if SOF_REUSEADDR is set on the first match */ |
||
361 | struct udp_pcb *mpcb; |
||
362 | for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { |
||
363 | if (mpcb != pcb) { |
||
364 | /* compare PCB local addr+port to UDP destination addr+port */ |
||
365 | if ((mpcb->local_port == dest) && |
||
366 | (udp_input_local_match(mpcb, inp, broadcast) != 0)) { |
||
367 | /* pass a copy of the packet to all local matches */ |
||
368 | if (mpcb->recv != NULL) { |
||
369 | struct pbuf *q; |
||
370 | q = pbuf_clone(PBUF_RAW, PBUF_POOL, p); |
||
371 | if (q != NULL) { |
||
372 | mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); |
||
373 | } |
||
374 | } |
||
375 | } |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 | #endif /* SO_REUSE && SO_REUSE_RXTOALL */ |
||
380 | /* callback */ |
||
381 | if (pcb->recv != NULL) { |
||
382 | /* now the recv function is responsible for freeing p */ |
||
383 | pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); |
||
384 | } else { |
||
385 | /* no recv function registered? then we have to free the pbuf! */ |
||
386 | pbuf_free(p); |
||
387 | goto end; |
||
388 | } |
||
389 | } else { |
||
390 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); |
||
391 | |||
392 | #if LWIP_ICMP || LWIP_ICMP6 |
||
393 | /* No match was found, send ICMP destination port unreachable unless |
||
394 | destination address was broadcast/multicast. */ |
||
395 | if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) { |
||
396 | /* move payload pointer back to ip header */ |
||
397 | pbuf_header_force(p, (s16_t)(ip_current_header_tot_len() + UDP_HLEN)); |
||
398 | icmp_port_unreach(ip_current_is_v6(), p); |
||
399 | } |
||
400 | #endif /* LWIP_ICMP || LWIP_ICMP6 */ |
||
401 | UDP_STATS_INC(udp.proterr); |
||
402 | UDP_STATS_INC(udp.drop); |
||
403 | MIB2_STATS_INC(mib2.udpnoports); |
||
404 | pbuf_free(p); |
||
405 | } |
||
406 | } else { |
||
407 | pbuf_free(p); |
||
408 | } |
||
409 | end: |
||
410 | PERF_STOP("udp_input"); |
||
411 | return; |
||
412 | #if CHECKSUM_CHECK_UDP |
||
413 | chkerr: |
||
414 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
415 | ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n")); |
||
416 | UDP_STATS_INC(udp.chkerr); |
||
417 | UDP_STATS_INC(udp.drop); |
||
418 | MIB2_STATS_INC(mib2.udpinerrors); |
||
419 | pbuf_free(p); |
||
420 | PERF_STOP("udp_input"); |
||
421 | #endif /* CHECKSUM_CHECK_UDP */ |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @ingroup udp_raw |
||
426 | * Send data using UDP. |
||
427 | * |
||
428 | * @param pcb UDP PCB used to send the data. |
||
429 | * @param p chain of pbuf's to be sent. |
||
430 | * |
||
431 | * The datagram will be sent to the current remote_ip & remote_port |
||
432 | * stored in pcb. If the pcb is not bound to a port, it will |
||
433 | * automatically be bound to a random port. |
||
434 | * |
||
435 | * @return lwIP error code. |
||
436 | * - ERR_OK. Successful. No error occurred. |
||
437 | * - ERR_MEM. Out of memory. |
||
438 | * - ERR_RTE. Could not find route to destination address. |
||
439 | * - ERR_VAL. No PCB or PCB is dual-stack |
||
440 | * - More errors could be returned by lower protocol layers. |
||
441 | * |
||
442 | * @see udp_disconnect() udp_sendto() |
||
443 | */ |
||
444 | err_t |
||
445 | udp_send(struct udp_pcb *pcb, struct pbuf *p) |
||
446 | { |
||
447 | if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { |
||
448 | return ERR_VAL; |
||
449 | } |
||
450 | |||
451 | /* send to the packet using remote ip and port stored in the pcb */ |
||
452 | return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); |
||
453 | } |
||
454 | |||
455 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
456 | /** @ingroup udp_raw |
||
457 | * Same as udp_send() but with checksum |
||
458 | */ |
||
459 | err_t |
||
460 | udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, |
||
461 | u8_t have_chksum, u16_t chksum) |
||
462 | { |
||
463 | if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { |
||
464 | return ERR_VAL; |
||
465 | } |
||
466 | |||
467 | /* send to the packet using remote ip and port stored in the pcb */ |
||
468 | return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, |
||
469 | have_chksum, chksum); |
||
470 | } |
||
471 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
472 | |||
473 | /** |
||
474 | * @ingroup udp_raw |
||
475 | * Send data to a specified address using UDP. |
||
476 | * |
||
477 | * @param pcb UDP PCB used to send the data. |
||
478 | * @param p chain of pbuf's to be sent. |
||
479 | * @param dst_ip Destination IP address. |
||
480 | * @param dst_port Destination UDP port. |
||
481 | * |
||
482 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
||
483 | * |
||
484 | * If the PCB already has a remote address association, it will |
||
485 | * be restored after the data is sent. |
||
486 | * |
||
487 | * @return lwIP error code (@see udp_send for possible error codes) |
||
488 | * |
||
489 | * @see udp_disconnect() udp_send() |
||
490 | */ |
||
491 | err_t |
||
492 | udp_sendto(struct udp_pcb *pcb, struct pbuf *p, |
||
493 | const ip_addr_t *dst_ip, u16_t dst_port) |
||
494 | { |
||
495 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
496 | return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); |
||
497 | } |
||
498 | |||
499 | /** @ingroup udp_raw |
||
500 | * Same as udp_sendto(), but with checksum */ |
||
501 | err_t |
||
502 | udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
||
503 | u16_t dst_port, u8_t have_chksum, u16_t chksum) |
||
504 | { |
||
505 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
506 | struct netif *netif; |
||
507 | |||
508 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
||
509 | return ERR_VAL; |
||
510 | } |
||
511 | |||
512 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); |
||
513 | |||
514 | if (pcb->netif_idx != NETIF_NO_INDEX) { |
||
515 | netif = netif_get_by_index(pcb->netif_idx); |
||
516 | } else { |
||
517 | #if LWIP_MULTICAST_TX_OPTIONS |
||
518 | netif = NULL; |
||
519 | if (ip_addr_ismulticast(dst_ip)) { |
||
520 | /* For IPv6, the interface to use for packets with a multicast destination |
||
521 | * is specified using an interface index. The same approach may be used for |
||
522 | * IPv4 as well, in which case it overrides the IPv4 multicast override |
||
523 | * address below. Here we have to look up the netif by going through the |
||
524 | * list, but by doing so we skip a route lookup. If the interface index has |
||
525 | * gone stale, we fall through and do the regular route lookup after all. */ |
||
526 | if (pcb->mcast_ifindex != NETIF_NO_INDEX) { |
||
527 | netif = netif_get_by_index(pcb->mcast_ifindex); |
||
528 | } |
||
529 | #if LWIP_IPV4 |
||
530 | else |
||
531 | #if LWIP_IPV6 |
||
532 | if (IP_IS_V4(dst_ip)) |
||
533 | #endif /* LWIP_IPV6 */ |
||
534 | { |
||
535 | /* IPv4 does not use source-based routing by default, so we use an |
||
536 | administratively selected interface for multicast by default. |
||
537 | However, this can be overridden by setting an interface address |
||
538 | in pcb->mcast_ip4 that is used for routing. If this routing lookup |
||
539 | fails, we try regular routing as though no override was set. */ |
||
540 | if (!ip4_addr_isany_val(pcb->mcast_ip4) && |
||
541 | !ip4_addr_cmp(&pcb->mcast_ip4, IP4_ADDR_BROADCAST)) { |
||
542 | netif = ip4_route_src(ip_2_ip4(&pcb->local_ip), &pcb->mcast_ip4); |
||
543 | } |
||
544 | } |
||
545 | #endif /* LWIP_IPV4 */ |
||
546 | } |
||
547 | |||
548 | if (netif == NULL) |
||
549 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
550 | { |
||
551 | /* find the outgoing network interface for this packet */ |
||
552 | netif = ip_route(&pcb->local_ip, dst_ip); |
||
553 | } |
||
554 | } |
||
555 | |||
556 | /* no outgoing network interface could be found? */ |
||
557 | if (netif == NULL) { |
||
558 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to ")); |
||
559 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip); |
||
560 | LWIP_DEBUGF(UDP_DEBUG, ("\n")); |
||
561 | UDP_STATS_INC(udp.rterr); |
||
562 | return ERR_RTE; |
||
563 | } |
||
564 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
565 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); |
||
566 | #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
567 | return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); |
||
568 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * @ingroup udp_raw |
||
573 | * Send data to a specified address using UDP. |
||
574 | * The netif used for sending can be specified. |
||
575 | * |
||
576 | * This function exists mainly for DHCP, to be able to send UDP packets |
||
577 | * on a netif that is still down. |
||
578 | * |
||
579 | * @param pcb UDP PCB used to send the data. |
||
580 | * @param p chain of pbuf's to be sent. |
||
581 | * @param dst_ip Destination IP address. |
||
582 | * @param dst_port Destination UDP port. |
||
583 | * @param netif the netif used for sending. |
||
584 | * |
||
585 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
||
586 | * |
||
587 | * @return lwIP error code (@see udp_send for possible error codes) |
||
588 | * |
||
589 | * @see udp_disconnect() udp_send() |
||
590 | */ |
||
591 | err_t |
||
592 | udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, |
||
593 | const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) |
||
594 | { |
||
595 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
596 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); |
||
597 | } |
||
598 | |||
599 | /** Same as udp_sendto_if(), but with checksum */ |
||
600 | err_t |
||
601 | udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
||
602 | u16_t dst_port, struct netif *netif, u8_t have_chksum, |
||
603 | u16_t chksum) |
||
604 | { |
||
605 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
606 | const ip_addr_t *src_ip; |
||
607 | |||
608 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
||
609 | return ERR_VAL; |
||
610 | } |
||
611 | |||
612 | /* PCB local address is IP_ANY_ADDR or multicast? */ |
||
613 | #if LWIP_IPV6 |
||
614 | if (IP_IS_V6(dst_ip)) { |
||
615 | if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip)) || |
||
616 | ip6_addr_ismulticast(ip_2_ip6(&pcb->local_ip))) { |
||
617 | src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip)); |
||
618 | if (src_ip == NULL) { |
||
619 | /* No suitable source address was found. */ |
||
620 | return ERR_RTE; |
||
621 | } |
||
622 | } else { |
||
623 | /* use UDP PCB local IPv6 address as source address, if still valid. */ |
||
624 | if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) { |
||
625 | /* Address isn't valid anymore. */ |
||
626 | return ERR_RTE; |
||
627 | } |
||
628 | src_ip = &pcb->local_ip; |
||
629 | } |
||
630 | } |
||
631 | #endif /* LWIP_IPV6 */ |
||
632 | #if LWIP_IPV4 && LWIP_IPV6 |
||
633 | else |
||
634 | #endif /* LWIP_IPV4 && LWIP_IPV6 */ |
||
635 | #if LWIP_IPV4 |
||
636 | if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || |
||
637 | ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) { |
||
638 | /* if the local_ip is any or multicast |
||
639 | * use the outgoing network interface IP address as source address */ |
||
640 | src_ip = netif_ip_addr4(netif); |
||
641 | } else { |
||
642 | /* check if UDP PCB local IP address is correct |
||
643 | * this could be an old address if netif->ip_addr has changed */ |
||
644 | if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) { |
||
645 | /* local_ip doesn't match, drop the packet */ |
||
646 | return ERR_RTE; |
||
647 | } |
||
648 | /* use UDP PCB local IP address as source address */ |
||
649 | src_ip = &pcb->local_ip; |
||
650 | } |
||
651 | #endif /* LWIP_IPV4 */ |
||
652 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
653 | return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip); |
||
654 | #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
655 | return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip); |
||
656 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
657 | } |
||
658 | |||
659 | /** @ingroup udp_raw |
||
660 | * Same as @ref udp_sendto_if, but with source address */ |
||
661 | err_t |
||
662 | udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, |
||
663 | const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip) |
||
664 | { |
||
665 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
||
666 | return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip); |
||
667 | } |
||
668 | |||
669 | /** Same as udp_sendto_if_src(), but with checksum */ |
||
670 | err_t |
||
671 | udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
||
672 | u16_t dst_port, struct netif *netif, u8_t have_chksum, |
||
673 | u16_t chksum, const ip_addr_t *src_ip) |
||
674 | { |
||
675 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
||
676 | struct udp_hdr *udphdr; |
||
677 | err_t err; |
||
678 | struct pbuf *q; /* q will be sent down the stack */ |
||
679 | u8_t ip_proto; |
||
680 | u8_t ttl; |
||
681 | |||
682 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || |
||
683 | !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
||
684 | return ERR_VAL; |
||
685 | } |
||
686 | |||
687 | #if LWIP_IPV4 && IP_SOF_BROADCAST |
||
688 | /* broadcast filter? */ |
||
689 | if (!ip_get_option(pcb, SOF_BROADCAST) && |
||
690 | #if LWIP_IPV6 |
||
691 | IP_IS_V4(dst_ip) && |
||
692 | #endif /* LWIP_IPV6 */ |
||
693 | ip_addr_isbroadcast(dst_ip, netif)) { |
||
694 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
695 | ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); |
||
696 | return ERR_VAL; |
||
697 | } |
||
698 | #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */ |
||
699 | |||
700 | /* if the PCB is not yet bound to a port, bind it here */ |
||
701 | if (pcb->local_port == 0) { |
||
702 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); |
||
703 | err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
||
704 | if (err != ERR_OK) { |
||
705 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); |
||
706 | return err; |
||
707 | } |
||
708 | } |
||
709 | |||
710 | /* packet too large to add a UDP header without causing an overflow? */ |
||
711 | if ((u16_t)(p->tot_len + UDP_HLEN) < p->tot_len) { |
||
712 | return ERR_MEM; |
||
713 | } |
||
714 | /* not enough space to add an UDP header to first pbuf in given p chain? */ |
||
715 | if (pbuf_add_header(p, UDP_HLEN)) { |
||
716 | /* allocate header in a separate new pbuf */ |
||
717 | q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); |
||
718 | /* new header pbuf could not be allocated? */ |
||
719 | if (q == NULL) { |
||
720 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); |
||
721 | return ERR_MEM; |
||
722 | } |
||
723 | if (p->tot_len != 0) { |
||
724 | /* chain header q in front of given pbuf p (only if p contains data) */ |
||
725 | pbuf_chain(q, p); |
||
726 | } |
||
727 | /* first pbuf q points to header pbuf */ |
||
728 | LWIP_DEBUGF(UDP_DEBUG, |
||
729 | ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); |
||
730 | } else { |
||
731 | /* adding space for header within p succeeded */ |
||
732 | /* first pbuf q equals given pbuf */ |
||
733 | q = p; |
||
734 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); |
||
735 | } |
||
736 | LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", |
||
737 | (q->len >= sizeof(struct udp_hdr))); |
||
738 | /* q now represents the packet to be sent */ |
||
739 | udphdr = (struct udp_hdr *)q->payload; |
||
740 | udphdr->src = lwip_htons(pcb->local_port); |
||
741 | udphdr->dest = lwip_htons(dst_port); |
||
742 | /* in UDP, 0 checksum means 'no checksum' */ |
||
743 | udphdr->chksum = 0x0000; |
||
744 | |||
745 | /* Multicast Loop? */ |
||
746 | #if LWIP_MULTICAST_TX_OPTIONS |
||
747 | if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) { |
||
748 | q->flags |= PBUF_FLAG_MCASTLOOP; |
||
749 | } |
||
750 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
751 | |||
752 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); |
||
753 | |||
754 | #if LWIP_UDPLITE |
||
755 | /* UDP Lite protocol? */ |
||
756 | if (pcb->flags & UDP_FLAGS_UDPLITE) { |
||
757 | u16_t chklen, chklen_hdr; |
||
758 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); |
||
759 | /* set UDP message length in UDP header */ |
||
760 | chklen_hdr = chklen = pcb->chksum_len_tx; |
||
761 | if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { |
||
762 | if (chklen != 0) { |
||
763 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); |
||
764 | } |
||
765 | /* For UDP-Lite, checksum length of 0 means checksum |
||
766 | over the complete packet. (See RFC 3828 chap. 3.1) |
||
767 | At least the UDP-Lite header must be covered by the |
||
768 | checksum, therefore, if chksum_len has an illegal |
||
769 | value, we generate the checksum over the complete |
||
770 | packet to be safe. */ |
||
771 | chklen_hdr = 0; |
||
772 | chklen = q->tot_len; |
||
773 | } |
||
774 | udphdr->len = lwip_htons(chklen_hdr); |
||
775 | /* calculate checksum */ |
||
776 | #if CHECKSUM_GEN_UDP |
||
777 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { |
||
778 | #if LWIP_CHECKSUM_ON_COPY |
||
779 | if (have_chksum) { |
||
780 | chklen = UDP_HLEN; |
||
781 | } |
||
782 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
||
783 | udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE, |
||
784 | q->tot_len, chklen, src_ip, dst_ip); |
||
785 | #if LWIP_CHECKSUM_ON_COPY |
||
786 | if (have_chksum) { |
||
787 | u32_t acc; |
||
788 | acc = udphdr->chksum + (u16_t)~(chksum); |
||
789 | udphdr->chksum = FOLD_U32T(acc); |
||
790 | } |
||
791 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
||
792 | |||
793 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
||
794 | if (udphdr->chksum == 0x0000) { |
||
795 | udphdr->chksum = 0xffff; |
||
796 | } |
||
797 | } |
||
798 | #endif /* CHECKSUM_GEN_UDP */ |
||
799 | |||
800 | ip_proto = IP_PROTO_UDPLITE; |
||
801 | } else |
||
802 | #endif /* LWIP_UDPLITE */ |
||
803 | { /* UDP */ |
||
804 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); |
||
805 | udphdr->len = lwip_htons(q->tot_len); |
||
806 | /* calculate checksum */ |
||
807 | #if CHECKSUM_GEN_UDP |
||
808 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { |
||
809 | /* Checksum is mandatory over IPv6. */ |
||
810 | if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { |
||
811 | u16_t udpchksum; |
||
812 | #if LWIP_CHECKSUM_ON_COPY |
||
813 | if (have_chksum) { |
||
814 | u32_t acc; |
||
815 | udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP, |
||
816 | q->tot_len, UDP_HLEN, src_ip, dst_ip); |
||
817 | acc = udpchksum + (u16_t)~(chksum); |
||
818 | udpchksum = FOLD_U32T(acc); |
||
819 | } else |
||
820 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
||
821 | { |
||
822 | udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len, |
||
823 | src_ip, dst_ip); |
||
824 | } |
||
825 | |||
826 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
||
827 | if (udpchksum == 0x0000) { |
||
828 | udpchksum = 0xffff; |
||
829 | } |
||
830 | udphdr->chksum = udpchksum; |
||
831 | } |
||
832 | } |
||
833 | #endif /* CHECKSUM_GEN_UDP */ |
||
834 | ip_proto = IP_PROTO_UDP; |
||
835 | } |
||
836 | |||
837 | /* Determine TTL to use */ |
||
838 | #if LWIP_MULTICAST_TX_OPTIONS |
||
839 | ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl); |
||
840 | #else /* LWIP_MULTICAST_TX_OPTIONS */ |
||
841 | ttl = pcb->ttl; |
||
842 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
843 | |||
844 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); |
||
845 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto)); |
||
846 | /* output to IP */ |
||
847 | NETIF_SET_HINTS(netif, &(pcb->netif_hints)); |
||
848 | err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif); |
||
849 | NETIF_RESET_HINTS(netif); |
||
850 | |||
851 | /* @todo: must this be increased even if error occurred? */ |
||
852 | MIB2_STATS_INC(mib2.udpoutdatagrams); |
||
853 | |||
854 | /* did we chain a separate header pbuf earlier? */ |
||
855 | if (q != p) { |
||
856 | /* free the header pbuf */ |
||
857 | pbuf_free(q); |
||
858 | q = NULL; |
||
859 | /* p is still referenced by the caller, and will live on */ |
||
860 | } |
||
861 | |||
862 | UDP_STATS_INC(udp.xmit); |
||
863 | return err; |
||
864 | } |
||
865 | |||
866 | /** |
||
867 | * @ingroup udp_raw |
||
868 | * Bind an UDP PCB. |
||
869 | * |
||
870 | * @param pcb UDP PCB to be bound with a local address ipaddr and port. |
||
871 | * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to |
||
872 | * bind to all local interfaces. |
||
873 | * @param port local UDP port to bind with. Use 0 to automatically bind |
||
874 | * to a random port between UDP_LOCAL_PORT_RANGE_START and |
||
875 | * UDP_LOCAL_PORT_RANGE_END. |
||
876 | * |
||
877 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
||
878 | * |
||
879 | * @return lwIP error code. |
||
880 | * - ERR_OK. Successful. No error occurred. |
||
881 | * - ERR_USE. The specified ipaddr and port are already bound to by |
||
882 | * another UDP PCB. |
||
883 | * |
||
884 | * @see udp_disconnect() |
||
885 | */ |
||
886 | err_t |
||
887 | udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) |
||
888 | { |
||
889 | struct udp_pcb *ipcb; |
||
890 | u8_t rebind; |
||
891 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES |
||
892 | ip_addr_t zoned_ipaddr; |
||
893 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ |
||
894 | |||
895 | #if LWIP_IPV4 |
||
896 | /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */ |
||
897 | if (ipaddr == NULL) { |
||
898 | ipaddr = IP4_ADDR_ANY; |
||
899 | } |
||
900 | #endif /* LWIP_IPV4 */ |
||
901 | |||
902 | /* still need to check for ipaddr == NULL in IPv6 only case */ |
||
903 | if ((pcb == NULL) || (ipaddr == NULL)) { |
||
904 | return ERR_VAL; |
||
905 | } |
||
906 | |||
907 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); |
||
908 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr); |
||
909 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); |
||
910 | |||
911 | rebind = 0; |
||
912 | /* Check for double bind and rebind of the same pcb */ |
||
913 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
||
914 | /* is this UDP PCB already on active list? */ |
||
915 | if (pcb == ipcb) { |
||
916 | rebind = 1; |
||
917 | break; |
||
918 | } |
||
919 | } |
||
920 | |||
921 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES |
||
922 | /* If the given IP address should have a zone but doesn't, assign one now. |
||
923 | * This is legacy support: scope-aware callers should always provide properly |
||
924 | * zoned source addresses. Do the zone selection before the address-in-use |
||
925 | * check below; as such we have to make a temporary copy of the address. */ |
||
926 | if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { |
||
927 | ip_addr_copy(zoned_ipaddr, *ipaddr); |
||
928 | ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr)); |
||
929 | ipaddr = &zoned_ipaddr; |
||
930 | } |
||
931 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ |
||
932 | |||
933 | /* no port specified? */ |
||
934 | if (port == 0) { |
||
935 | port = udp_new_port(); |
||
936 | if (port == 0) { |
||
937 | /* no more ports available in local range */ |
||
938 | LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); |
||
939 | return ERR_USE; |
||
940 | } |
||
941 | } else { |
||
942 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
||
943 | if (pcb != ipcb) { |
||
944 | /* By default, we don't allow to bind to a port that any other udp |
||
945 | PCB is already bound to, unless *all* PCBs with that port have tha |
||
946 | REUSEADDR flag set. */ |
||
947 | #if SO_REUSE |
||
948 | if (!ip_get_option(pcb, SOF_REUSEADDR) || |
||
949 | !ip_get_option(ipcb, SOF_REUSEADDR)) |
||
950 | #endif /* SO_REUSE */ |
||
951 | { |
||
952 | /* port matches that of PCB in list and REUSEADDR not set -> reject */ |
||
953 | if ((ipcb->local_port == port) && |
||
954 | /* IP address matches? */ |
||
955 | ip_addr_cmp(&ipcb->local_ip, ipaddr)) { |
||
956 | /* other PCB already binds to this local IP and port */ |
||
957 | LWIP_DEBUGF(UDP_DEBUG, |
||
958 | ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); |
||
959 | return ERR_USE; |
||
960 | } |
||
961 | } |
||
962 | } |
||
963 | } |
||
964 | } |
||
965 | |||
966 | ip_addr_set_ipaddr(&pcb->local_ip, ipaddr); |
||
967 | |||
968 | pcb->local_port = port; |
||
969 | mib2_udp_bind(pcb); |
||
970 | /* pcb not active yet? */ |
||
971 | if (rebind == 0) { |
||
972 | /* place the PCB on the active list if not already there */ |
||
973 | pcb->next = udp_pcbs; |
||
974 | udp_pcbs = pcb; |
||
975 | } |
||
976 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to ")); |
||
977 | ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, pcb->local_ip); |
||
978 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port)); |
||
979 | return ERR_OK; |
||
980 | } |
||
981 | |||
982 | /** |
||
983 | * @ingroup udp_raw |
||
984 | * Bind an UDP PCB to a specific netif. |
||
985 | * After calling this function, all packets received via this PCB |
||
986 | * are guaranteed to have come in via the specified netif, and all |
||
987 | * outgoing packets will go out via the specified netif. |
||
988 | * |
||
989 | * @param pcb UDP PCB to be bound. |
||
990 | * @param netif netif to bind udp pcb to. Can be NULL. |
||
991 | * |
||
992 | * @see udp_disconnect() |
||
993 | */ |
||
994 | void |
||
995 | udp_bind_netif(struct udp_pcb *pcb, const struct netif *netif) |
||
996 | { |
||
997 | if (netif != NULL) { |
||
998 | pcb->netif_idx = netif_get_index(netif); |
||
999 | } else { |
||
1000 | pcb->netif_idx = NETIF_NO_INDEX; |
||
1001 | } |
||
1002 | } |
||
1003 | |||
1004 | /** |
||
1005 | * @ingroup udp_raw |
||
1006 | * Connect an UDP PCB. |
||
1007 | * |
||
1008 | * This will associate the UDP PCB with the remote address. |
||
1009 | * |
||
1010 | * @param pcb UDP PCB to be connected with remote address ipaddr and port. |
||
1011 | * @param ipaddr remote IP address to connect with. |
||
1012 | * @param port remote UDP port to connect with. |
||
1013 | * |
||
1014 | * @return lwIP error code |
||
1015 | * |
||
1016 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
||
1017 | * |
||
1018 | * The udp pcb is bound to a random local port if not already bound. |
||
1019 | * |
||
1020 | * @see udp_disconnect() |
||
1021 | */ |
||
1022 | err_t |
||
1023 | udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) |
||
1024 | { |
||
1025 | struct udp_pcb *ipcb; |
||
1026 | |||
1027 | if ((pcb == NULL) || (ipaddr == NULL)) { |
||
1028 | return ERR_VAL; |
||
1029 | } |
||
1030 | |||
1031 | if (pcb->local_port == 0) { |
||
1032 | err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
||
1033 | if (err != ERR_OK) { |
||
1034 | return err; |
||
1035 | } |
||
1036 | } |
||
1037 | |||
1038 | ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr); |
||
1039 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES |
||
1040 | /* If the given IP address should have a zone but doesn't, assign one now, |
||
1041 | * using the bound address to make a more informed decision when possible. */ |
||
1042 | if (IP_IS_V6(&pcb->remote_ip) && |
||
1043 | ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) { |
||
1044 | ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip)); |
||
1045 | } |
||
1046 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ |
||
1047 | |||
1048 | pcb->remote_port = port; |
||
1049 | pcb->flags |= UDP_FLAGS_CONNECTED; |
||
1050 | |||
1051 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to ")); |
||
1052 | ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
1053 | pcb->remote_ip); |
||
1054 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port)); |
||
1055 | |||
1056 | /* Insert UDP PCB into the list of active UDP PCBs. */ |
||
1057 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
||
1058 | if (pcb == ipcb) { |
||
1059 | /* already on the list, just return */ |
||
1060 | return ERR_OK; |
||
1061 | } |
||
1062 | } |
||
1063 | /* PCB not yet on the list, add PCB now */ |
||
1064 | pcb->next = udp_pcbs; |
||
1065 | udp_pcbs = pcb; |
||
1066 | return ERR_OK; |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * @ingroup udp_raw |
||
1071 | * Disconnect a UDP PCB |
||
1072 | * |
||
1073 | * @param pcb the udp pcb to disconnect. |
||
1074 | */ |
||
1075 | void |
||
1076 | udp_disconnect(struct udp_pcb *pcb) |
||
1077 | { |
||
1078 | /* reset remote address association */ |
||
1079 | #if LWIP_IPV4 && LWIP_IPV6 |
||
1080 | if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { |
||
1081 | ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE); |
||
1082 | } else { |
||
1083 | #endif |
||
1084 | ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip); |
||
1085 | #if LWIP_IPV4 && LWIP_IPV6 |
||
1086 | } |
||
1087 | #endif |
||
1088 | pcb->remote_port = 0; |
||
1089 | pcb->netif_idx = NETIF_NO_INDEX; |
||
1090 | /* mark PCB as unconnected */ |
||
1091 | udp_clear_flags(pcb, UDP_FLAGS_CONNECTED); |
||
1092 | } |
||
1093 | |||
1094 | /** |
||
1095 | * @ingroup udp_raw |
||
1096 | * Set a receive callback for a UDP PCB |
||
1097 | * |
||
1098 | * This callback will be called when receiving a datagram for the pcb. |
||
1099 | * |
||
1100 | * @param pcb the pcb for which to set the recv callback |
||
1101 | * @param recv function pointer of the callback function |
||
1102 | * @param recv_arg additional argument to pass to the callback function |
||
1103 | */ |
||
1104 | void |
||
1105 | udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) |
||
1106 | { |
||
1107 | /* remember recv() callback and user data */ |
||
1108 | pcb->recv = recv; |
||
1109 | pcb->recv_arg = recv_arg; |
||
1110 | } |
||
1111 | |||
1112 | /** |
||
1113 | * @ingroup udp_raw |
||
1114 | * Remove an UDP PCB. |
||
1115 | * |
||
1116 | * @param pcb UDP PCB to be removed. The PCB is removed from the list of |
||
1117 | * UDP PCB's and the data structure is freed from memory. |
||
1118 | * |
||
1119 | * @see udp_new() |
||
1120 | */ |
||
1121 | void |
||
1122 | udp_remove(struct udp_pcb *pcb) |
||
1123 | { |
||
1124 | struct udp_pcb *pcb2; |
||
1125 | |||
1126 | mib2_udp_unbind(pcb); |
||
1127 | /* pcb to be removed is first in list? */ |
||
1128 | if (udp_pcbs == pcb) { |
||
1129 | /* make list start at 2nd pcb */ |
||
1130 | udp_pcbs = udp_pcbs->next; |
||
1131 | /* pcb not 1st in list */ |
||
1132 | } else { |
||
1133 | for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { |
||
1134 | /* find pcb in udp_pcbs list */ |
||
1135 | if (pcb2->next != NULL && pcb2->next == pcb) { |
||
1136 | /* remove pcb from list */ |
||
1137 | pcb2->next = pcb->next; |
||
1138 | break; |
||
1139 | } |
||
1140 | } |
||
1141 | } |
||
1142 | memp_free(MEMP_UDP_PCB, pcb); |
||
1143 | } |
||
1144 | |||
1145 | /** |
||
1146 | * @ingroup udp_raw |
||
1147 | * Create a UDP PCB. |
||
1148 | * |
||
1149 | * @return The UDP PCB which was created. NULL if the PCB data structure |
||
1150 | * could not be allocated. |
||
1151 | * |
||
1152 | * @see udp_remove() |
||
1153 | */ |
||
1154 | struct udp_pcb * |
||
1155 | udp_new(void) |
||
1156 | { |
||
1157 | struct udp_pcb *pcb; |
||
1158 | pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); |
||
1159 | /* could allocate UDP PCB? */ |
||
1160 | if (pcb != NULL) { |
||
1161 | /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 |
||
1162 | * which means checksum is generated over the whole datagram per default |
||
1163 | * (recommended as default by RFC 3828). */ |
||
1164 | /* initialize PCB to all zeroes */ |
||
1165 | memset(pcb, 0, sizeof(struct udp_pcb)); |
||
1166 | pcb->ttl = UDP_TTL; |
||
1167 | #if LWIP_MULTICAST_TX_OPTIONS |
||
1168 | udp_set_multicast_ttl(pcb, UDP_TTL); |
||
1169 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
1170 | } |
||
1171 | return pcb; |
||
1172 | } |
||
1173 | |||
1174 | /** |
||
1175 | * @ingroup udp_raw |
||
1176 | * Create a UDP PCB for specific IP type. |
||
1177 | * |
||
1178 | * @param type IP address type, see @ref lwip_ip_addr_type definitions. |
||
1179 | * If you want to listen to IPv4 and IPv6 (dual-stack) packets, |
||
1180 | * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE. |
||
1181 | * @return The UDP PCB which was created. NULL if the PCB data structure |
||
1182 | * could not be allocated. |
||
1183 | * |
||
1184 | * @see udp_remove() |
||
1185 | */ |
||
1186 | struct udp_pcb * |
||
1187 | udp_new_ip_type(u8_t type) |
||
1188 | { |
||
1189 | struct udp_pcb *pcb; |
||
1190 | pcb = udp_new(); |
||
1191 | #if LWIP_IPV4 && LWIP_IPV6 |
||
1192 | if (pcb != NULL) { |
||
1193 | IP_SET_TYPE_VAL(pcb->local_ip, type); |
||
1194 | IP_SET_TYPE_VAL(pcb->remote_ip, type); |
||
1195 | } |
||
1196 | #else |
||
1197 | LWIP_UNUSED_ARG(type); |
||
1198 | #endif /* LWIP_IPV4 && LWIP_IPV6 */ |
||
1199 | return pcb; |
||
1200 | } |
||
1201 | |||
1202 | /** This function is called from netif.c when address is changed |
||
1203 | * |
||
1204 | * @param old_addr IP address of the netif before change |
||
1205 | * @param new_addr IP address of the netif after change |
||
1206 | */ |
||
1207 | void udp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr) |
||
1208 | { |
||
1209 | struct udp_pcb *upcb; |
||
1210 | |||
1211 | if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) { |
||
1212 | for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) { |
||
1213 | /* PCB bound to current local interface address? */ |
||
1214 | if (ip_addr_cmp(&upcb->local_ip, old_addr)) { |
||
1215 | /* The PCB is bound to the old ipaddr and |
||
1216 | * is set to bound to the new one instead */ |
||
1217 | ip_addr_copy(upcb->local_ip, *new_addr); |
||
1218 | } |
||
1219 | } |
||
1220 | } |
||
1221 | } |
||
1222 | |||
1223 | #if UDP_DEBUG |
||
1224 | /** |
||
1225 | * Print UDP header information for debug purposes. |
||
1226 | * |
||
1227 | * @param udphdr pointer to the udp header in memory. |
||
1228 | */ |
||
1229 | void |
||
1230 | udp_debug_print(struct udp_hdr *udphdr) |
||
1231 | { |
||
1232 | LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); |
||
1233 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
||
1234 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", |
||
1235 | lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest))); |
||
1236 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
||
1237 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", |
||
1238 | lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum))); |
||
1239 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
||
1240 | } |
||
1241 | #endif /* UDP_DEBUG */ |
||
1242 | |||
1243 | #endif /* LWIP_UDP */ |