Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/
udp.c
00001 /** 00002 * @file 00003 * User Datagram Protocol module 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 00040 /* udp.c 00041 * 00042 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828). 00043 * 00044 */ 00045 00046 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! 00047 */ 00048 00049 #include "lwip/opt.h" 00050 00051 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ 00052 00053 #include "lwip/udp.h" 00054 #include "lwip/def.h" 00055 #include "lwip/memp.h" 00056 #include "lwip/inet_chksum.h" 00057 #include "lwip/ip_addr.h" 00058 #include "lwip/netif.h" 00059 #include "lwip/icmp.h" 00060 #include "lwip/stats.h" 00061 #include "lwip/snmp.h" 00062 #include "arch/perf.h" 00063 #include "lwip/dhcp.h " 00064 00065 #include <string.h> 00066 00067 /* The list of UDP PCBs */ 00068 /* exported in udp.h (was static) */ 00069 struct udp_pcb *udp_pcbs; 00070 00071 /** 00072 * Process an incoming UDP datagram. 00073 * 00074 * Given an incoming UDP datagram (as a chain of pbufs) this function 00075 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs 00076 * recv function. If no pcb is found or the datagram is incorrect, the 00077 * pbuf is freed. 00078 * 00079 * @param p pbuf to be demultiplexed to a UDP PCB. 00080 * @param inp network interface on which the datagram was received. 00081 * 00082 */ 00083 void 00084 udp_input(struct pbuf *p, struct netif *inp) 00085 { 00086 struct udp_hdr *udphdr; 00087 struct udp_pcb *pcb, *prev; 00088 struct udp_pcb *uncon_pcb; 00089 struct ip_hdr *iphdr; 00090 u16_t src, dest; 00091 u8_t local_match; 00092 u8_t broadcast; 00093 00094 PERF_START; 00095 00096 UDP_STATS_INC(udp.recv); 00097 00098 iphdr = (struct ip_hdr *)p->payload; 00099 00100 /* Check minimum length (IP header + UDP header) 00101 * and move payload pointer to UDP header */ 00102 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) { 00103 /* drop short packets */ 00104 LWIP_DEBUGF(UDP_DEBUG, 00105 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); 00106 UDP_STATS_INC(udp.lenerr); 00107 UDP_STATS_INC(udp.drop); 00108 snmp_inc_udpinerrors(); 00109 pbuf_free(p); 00110 goto end; 00111 } 00112 00113 udphdr = (struct udp_hdr *)p->payload; 00114 00115 /* is broadcast packet ? */ 00116 broadcast = ip_addr_isbroadcast(¤t_iphdr_dest, inp); 00117 00118 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); 00119 00120 /* convert src and dest ports to host byte order */ 00121 src = ntohs(udphdr->src); 00122 dest = ntohs(udphdr->dest); 00123 00124 udp_debug_print(udphdr); 00125 00126 /* print the UDP source and destination */ 00127 LWIP_DEBUGF(UDP_DEBUG, 00128 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- " 00129 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", 00130 ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest), 00131 ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest), 00132 ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src), 00133 ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src))); 00134 00135 #if LWIP_DHCP 00136 pcb = NULL; 00137 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by 00138 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */ 00139 if (dest == DHCP_CLIENT_PORT) { 00140 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */ 00141 if (src == DHCP_SERVER_PORT) { 00142 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) { 00143 /* accept the packe if 00144 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY! 00145 - inp->dhcp->pcb->remote == ANY or iphdr->src */ 00146 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) || 00147 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), ¤t_iphdr_src))) { 00148 pcb = inp->dhcp->pcb; 00149 } 00150 } 00151 } 00152 } else 00153 #endif /* LWIP_DHCP */ 00154 { 00155 prev = NULL; 00156 local_match = 0; 00157 uncon_pcb = NULL; 00158 /* Iterate through the UDP pcb list for a matching pcb. 00159 * 'Perfect match' pcbs (connected to the remote port & ip address) are 00160 * preferred. If no perfect match is found, the first unconnected pcb that 00161 * matches the local port and ip address gets the datagram. */ 00162 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { 00163 local_match = 0; 00164 /* print the PCB local and remote address */ 00165 LWIP_DEBUGF(UDP_DEBUG, 00166 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- " 00167 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", 00168 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00169 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port, 00170 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), 00171 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port)); 00172 00173 /* compare PCB local addr+port to UDP destination addr+port */ 00174 if ((pcb->local_port == dest) && 00175 ((!broadcast && ip_addr_isany(&pcb->local_ip)) || 00176 ip_addr_cmp(&(pcb->local_ip), ¤t_iphdr_dest) || 00177 #if LWIP_IGMP 00178 ip_addr_ismulticast(¤t_iphdr_dest) || 00179 #endif /* LWIP_IGMP */ 00180 #if IP_SOF_BROADCAST_RECV 00181 (broadcast && (pcb->so_options & SOF_BROADCAST)))) { 00182 #else /* IP_SOF_BROADCAST_RECV */ 00183 (broadcast))) { 00184 #endif /* IP_SOF_BROADCAST_RECV */ 00185 local_match = 1; 00186 if ((uncon_pcb == NULL) && 00187 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) { 00188 /* the first unconnected matching PCB */ 00189 uncon_pcb = pcb; 00190 } 00191 } 00192 /* compare PCB remote addr+port to UDP source addr+port */ 00193 if ((local_match != 0) && 00194 (pcb->remote_port == src) && 00195 (ip_addr_isany(&pcb->remote_ip) || 00196 ip_addr_cmp(&(pcb->remote_ip), ¤t_iphdr_src))) { 00197 /* the first fully matching PCB */ 00198 if (prev != NULL) { 00199 /* move the pcb to the front of udp_pcbs so that is 00200 found faster next time */ 00201 prev->next = pcb->next; 00202 pcb->next = udp_pcbs; 00203 udp_pcbs = pcb; 00204 } else { 00205 UDP_STATS_INC(udp.cachehit); 00206 } 00207 break; 00208 } 00209 prev = pcb; 00210 } 00211 /* no fully matching pcb found? then look for an unconnected pcb */ 00212 if (pcb == NULL) { 00213 pcb = uncon_pcb; 00214 } 00215 } 00216 00217 /* Check checksum if this is a match or if it was directed at us. */ 00218 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, ¤t_iphdr_dest)) { 00219 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); 00220 #if LWIP_UDPLITE 00221 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) { 00222 /* Do the UDP Lite checksum */ 00223 #if CHECKSUM_CHECK_UDP 00224 u16_t chklen = ntohs(udphdr->len); 00225 if (chklen < sizeof(struct udp_hdr)) { 00226 if (chklen == 0) { 00227 /* For UDP-Lite, checksum length of 0 means checksum 00228 over the complete packet (See RFC 3828 chap. 3.1) */ 00229 chklen = p->tot_len; 00230 } else { 00231 /* At least the UDP-Lite header must be covered by the 00232 checksum! (Again, see RFC 3828 chap. 3.1) */ 00233 UDP_STATS_INC(udp.chkerr); 00234 UDP_STATS_INC(udp.drop); 00235 snmp_inc_udpinerrors(); 00236 pbuf_free(p); 00237 goto end; 00238 } 00239 } 00240 if (inet_chksum_pseudo_partial(p, ¤t_iphdr_src, ¤t_iphdr_dest, 00241 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) { 00242 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00243 ("udp_input: UDP Lite datagram discarded due to failing checksum\n")); 00244 UDP_STATS_INC(udp.chkerr); 00245 UDP_STATS_INC(udp.drop); 00246 snmp_inc_udpinerrors(); 00247 pbuf_free(p); 00248 goto end; 00249 } 00250 #endif /* CHECKSUM_CHECK_UDP */ 00251 } else 00252 #endif /* LWIP_UDPLITE */ 00253 { 00254 #if CHECKSUM_CHECK_UDP 00255 if (udphdr->chksum != 0) { 00256 if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(), 00257 IP_PROTO_UDP, p->tot_len) != 0) { 00258 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00259 ("udp_input: UDP datagram discarded due to failing checksum\n")); 00260 UDP_STATS_INC(udp.chkerr); 00261 UDP_STATS_INC(udp.drop); 00262 snmp_inc_udpinerrors(); 00263 pbuf_free(p); 00264 goto end; 00265 } 00266 } 00267 #endif /* CHECKSUM_CHECK_UDP */ 00268 } 00269 if(pbuf_header(p, -UDP_HLEN)) { 00270 /* Can we cope with this failing? Just assert for now */ 00271 LWIP_ASSERT("pbuf_header failed\n", 0); 00272 UDP_STATS_INC(udp.drop); 00273 snmp_inc_udpinerrors(); 00274 pbuf_free(p); 00275 goto end; 00276 } 00277 if (pcb != NULL) { 00278 snmp_inc_udpindatagrams(); 00279 #if SO_REUSE && SO_REUSE_RXTOALL 00280 if ((broadcast || ip_addr_ismulticast(¤t_iphdr_dest)) && 00281 ((pcb->so_options & SOF_REUSEADDR) != 0)) { 00282 /* pass broadcast- or multicast packets to all multicast pcbs 00283 if SOF_REUSEADDR is set on the first match */ 00284 struct udp_pcb *mpcb; 00285 u8_t p_header_changed = 0; 00286 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { 00287 if (mpcb != pcb) { 00288 /* compare PCB local addr+port to UDP destination addr+port */ 00289 if ((mpcb->local_port == dest) && 00290 ((!broadcast && ip_addr_isany(&mpcb->local_ip)) || 00291 ip_addr_cmp(&(mpcb->local_ip), ¤t_iphdr_dest) || 00292 #if LWIP_IGMP 00293 ip_addr_ismulticast(¤t_iphdr_dest) || 00294 #endif /* LWIP_IGMP */ 00295 #if IP_SOF_BROADCAST_RECV 00296 (broadcast && (mpcb->so_options & SOF_BROADCAST)))) { 00297 #else /* IP_SOF_BROADCAST_RECV */ 00298 (broadcast))) { 00299 #endif /* IP_SOF_BROADCAST_RECV */ 00300 /* pass a copy of the packet to all local matches */ 00301 if (mpcb->recv != NULL) { 00302 struct pbuf *q; 00303 /* for that, move payload to IP header again */ 00304 if (p_header_changed == 0) { 00305 pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00306 p_header_changed = 1; 00307 } 00308 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); 00309 if (q != NULL) { 00310 err_t err = pbuf_copy(q, p); 00311 if (err == ERR_OK) { 00312 /* move payload to UDP data */ 00313 pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00314 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); 00315 } 00316 } 00317 } 00318 } 00319 } 00320 } 00321 if (p_header_changed) { 00322 /* and move payload to UDP data again */ 00323 pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00324 } 00325 } 00326 #endif /* SO_REUSE && SO_REUSE_RXTOALL */ 00327 /* callback */ 00328 if (pcb->recv != NULL) { 00329 /* now the recv function is responsible for freeing p */ 00330 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); 00331 } else { 00332 /* no recv function registered? then we have to free the pbuf! */ 00333 pbuf_free(p); 00334 goto end; 00335 } 00336 } else { 00337 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); 00338 00339 #if LWIP_ICMP 00340 /* No match was found, send ICMP destination port unreachable unless 00341 destination address was broadcast/multicast. */ 00342 if (!broadcast && 00343 !ip_addr_ismulticast(¤t_iphdr_dest)) { 00344 /* move payload pointer back to ip header */ 00345 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN); 00346 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr)); 00347 icmp_dest_unreach(p, ICMP_DUR_PORT); 00348 } 00349 #endif /* LWIP_ICMP */ 00350 UDP_STATS_INC(udp.proterr); 00351 UDP_STATS_INC(udp.drop); 00352 snmp_inc_udpnoports(); 00353 pbuf_free(p); 00354 } 00355 } else { 00356 pbuf_free(p); 00357 } 00358 end: 00359 PERF_STOP("udp_input"); 00360 } 00361 00362 /** 00363 * Send data using UDP. 00364 * 00365 * @param pcb UDP PCB used to send the data. 00366 * @param p chain of pbuf's to be sent. 00367 * 00368 * The datagram will be sent to the current remote_ip & remote_port 00369 * stored in pcb. If the pcb is not bound to a port, it will 00370 * automatically be bound to a random port. 00371 * 00372 * @return lwIP error code. 00373 * - ERR_OK. Successful. No error occured. 00374 * - ERR_MEM. Out of memory. 00375 * - ERR_RTE. Could not find route to destination address. 00376 * - More errors could be returned by lower protocol layers. 00377 * 00378 * @see udp_disconnect() udp_sendto() 00379 */ 00380 err_t 00381 udp_send(struct udp_pcb *pcb, struct pbuf *p) 00382 { 00383 /* send to the packet using remote ip and port stored in the pcb */ 00384 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); 00385 } 00386 00387 #if LWIP_CHECKSUM_ON_COPY 00388 /** Same as udp_send() but with checksum 00389 */ 00390 err_t 00391 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, 00392 u8_t have_chksum, u16_t chksum) 00393 { 00394 /* send to the packet using remote ip and port stored in the pcb */ 00395 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, 00396 have_chksum, chksum); 00397 } 00398 #endif /* LWIP_CHECKSUM_ON_COPY */ 00399 00400 /** 00401 * Send data to a specified address using UDP. 00402 * 00403 * @param pcb UDP PCB used to send the data. 00404 * @param p chain of pbuf's to be sent. 00405 * @param dst_ip Destination IP address. 00406 * @param dst_port Destination UDP port. 00407 * 00408 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00409 * 00410 * If the PCB already has a remote address association, it will 00411 * be restored after the data is sent. 00412 * 00413 * @return lwIP error code (@see udp_send for possible error codes) 00414 * 00415 * @see udp_disconnect() udp_send() 00416 */ 00417 err_t 00418 udp_sendto(struct udp_pcb *pcb, struct pbuf *p, 00419 ip_addr_t *dst_ip, u16_t dst_port) 00420 { 00421 #if LWIP_CHECKSUM_ON_COPY 00422 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); 00423 } 00424 00425 /** Same as udp_sendto(), but with checksum */ 00426 err_t 00427 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, 00428 u16_t dst_port, u8_t have_chksum, u16_t chksum) 00429 { 00430 #endif /* LWIP_CHECKSUM_ON_COPY */ 00431 struct netif *netif; 00432 00433 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); 00434 00435 /* find the outgoing network interface for this packet */ 00436 #if LWIP_IGMP 00437 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip)); 00438 #else 00439 netif = ip_route(dst_ip); 00440 #endif /* LWIP_IGMP */ 00441 00442 /* no outgoing network interface could be found? */ 00443 if (netif == NULL) { 00444 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 00445 ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip))); 00446 UDP_STATS_INC(udp.rterr); 00447 return ERR_RTE; 00448 } 00449 #if LWIP_CHECKSUM_ON_COPY 00450 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); 00451 #else /* LWIP_CHECKSUM_ON_COPY */ 00452 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); 00453 #endif /* LWIP_CHECKSUM_ON_COPY */ 00454 } 00455 00456 /** 00457 * Send data to a specified address using UDP. 00458 * The netif used for sending can be specified. 00459 * 00460 * This function exists mainly for DHCP, to be able to send UDP packets 00461 * on a netif that is still down. 00462 * 00463 * @param pcb UDP PCB used to send the data. 00464 * @param p chain of pbuf's to be sent. 00465 * @param dst_ip Destination IP address. 00466 * @param dst_port Destination UDP port. 00467 * @param netif the netif used for sending. 00468 * 00469 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00470 * 00471 * @return lwIP error code (@see udp_send for possible error codes) 00472 * 00473 * @see udp_disconnect() udp_send() 00474 */ 00475 err_t 00476 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, 00477 ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) 00478 { 00479 #if LWIP_CHECKSUM_ON_COPY 00480 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); 00481 } 00482 00483 /** Same as udp_sendto_if(), but with checksum */ 00484 err_t 00485 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, 00486 u16_t dst_port, struct netif *netif, u8_t have_chksum, 00487 u16_t chksum) 00488 { 00489 #endif /* LWIP_CHECKSUM_ON_COPY */ 00490 struct udp_hdr *udphdr; 00491 ip_addr_t *src_ip; 00492 err_t err; 00493 struct pbuf *q; /* q will be sent down the stack */ 00494 00495 #if IP_SOF_BROADCAST 00496 /* broadcast filter? */ 00497 if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) { 00498 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00499 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); 00500 return ERR_VAL; 00501 } 00502 #endif /* IP_SOF_BROADCAST */ 00503 00504 /* if the PCB is not yet bound to a port, bind it here */ 00505 if (pcb->local_port == 0) { 00506 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); 00507 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 00508 if (err != ERR_OK) { 00509 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); 00510 return err; 00511 } 00512 } 00513 00514 /* not enough space to add an UDP header to first pbuf in given p chain? */ 00515 if (pbuf_header(p, UDP_HLEN)) { 00516 /* allocate header in a separate new pbuf */ 00517 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); 00518 /* new header pbuf could not be allocated? */ 00519 if (q == NULL) { 00520 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); 00521 return ERR_MEM; 00522 } 00523 if (p->tot_len != 0) { 00524 /* chain header q in front of given pbuf p (only if p contains data) */ 00525 pbuf_chain(q, p); 00526 } 00527 /* first pbuf q points to header pbuf */ 00528 LWIP_DEBUGF(UDP_DEBUG, 00529 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); 00530 } else { 00531 /* adding space for header within p succeeded */ 00532 /* first pbuf q equals given pbuf */ 00533 q = p; 00534 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); 00535 } 00536 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", 00537 (q->len >= sizeof(struct udp_hdr))); 00538 /* q now represents the packet to be sent */ 00539 udphdr = (struct udp_hdr *)q->payload; 00540 udphdr->src = htons(pcb->local_port); 00541 udphdr->dest = htons(dst_port); 00542 /* in UDP, 0 checksum means 'no checksum' */ 00543 udphdr->chksum = 0x0000; 00544 00545 /* Multicast Loop? */ 00546 #if LWIP_IGMP 00547 if (ip_addr_ismulticast(dst_ip) && ((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0)) { 00548 q->flags |= PBUF_FLAG_MCASTLOOP; 00549 } 00550 #endif /* LWIP_IGMP */ 00551 00552 00553 /* PCB local address is IP_ANY_ADDR? */ 00554 if (ip_addr_isany(&pcb->local_ip)) { 00555 /* use outgoing network interface IP address as source address */ 00556 src_ip = &(netif->ip_addr); 00557 } else { 00558 /* check if UDP PCB local IP address is correct 00559 * this could be an old address if netif->ip_addr has changed */ 00560 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) { 00561 /* local_ip doesn't match, drop the packet */ 00562 if (q != p) { 00563 /* free the header pbuf */ 00564 pbuf_free(q); 00565 q = NULL; 00566 /* p is still referenced by the caller, and will live on */ 00567 } 00568 return ERR_VAL; 00569 } 00570 /* use UDP PCB local IP address as source address */ 00571 src_ip = &(pcb->local_ip); 00572 } 00573 00574 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); 00575 00576 #if LWIP_UDPLITE 00577 /* UDP Lite protocol? */ 00578 if (pcb->flags & UDP_FLAGS_UDPLITE) { 00579 u16_t chklen, chklen_hdr; 00580 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); 00581 /* set UDP message length in UDP header */ 00582 chklen_hdr = chklen = pcb->chksum_len_tx; 00583 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { 00584 if (chklen != 0) { 00585 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); 00586 } 00587 /* For UDP-Lite, checksum length of 0 means checksum 00588 over the complete packet. (See RFC 3828 chap. 3.1) 00589 At least the UDP-Lite header must be covered by the 00590 checksum, therefore, if chksum_len has an illegal 00591 value, we generate the checksum over the complete 00592 packet to be safe. */ 00593 chklen_hdr = 0; 00594 chklen = q->tot_len; 00595 } 00596 udphdr->len = htons(chklen_hdr); 00597 /* calculate checksum */ 00598 #if CHECKSUM_GEN_UDP 00599 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, 00600 IP_PROTO_UDPLITE, q->tot_len, 00601 #if !LWIP_CHECKSUM_ON_COPY 00602 chklen); 00603 #else /* !LWIP_CHECKSUM_ON_COPY */ 00604 (have_chksum ? UDP_HLEN : chklen)); 00605 if (have_chksum) { 00606 u32_t acc; 00607 acc = udphdr->chksum + (u16_t)~(chksum); 00608 udphdr->chksum = FOLD_U32T(acc); 00609 } 00610 #endif /* !LWIP_CHECKSUM_ON_COPY */ 00611 00612 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00613 if (udphdr->chksum == 0x0000) { 00614 udphdr->chksum = 0xffff; 00615 } 00616 #endif /* CHECKSUM_GEN_UDP */ 00617 /* output to IP */ 00618 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n")); 00619 #if LWIP_NETIF_HWADDRHINT 00620 netif->addr_hint = &(pcb->addr_hint); 00621 #endif /* LWIP_NETIF_HWADDRHINT*/ 00622 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif); 00623 #if LWIP_NETIF_HWADDRHINT 00624 netif->addr_hint = NULL; 00625 #endif /* LWIP_NETIF_HWADDRHINT*/ 00626 } else 00627 #endif /* LWIP_UDPLITE */ 00628 { /* UDP */ 00629 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); 00630 udphdr->len = htons(q->tot_len); 00631 /* calculate checksum */ 00632 #if CHECKSUM_GEN_UDP 00633 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { 00634 u16_t udpchksum; 00635 #if LWIP_CHECKSUM_ON_COPY 00636 if (have_chksum) { 00637 u32_t acc; 00638 udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP, 00639 q->tot_len, UDP_HLEN); 00640 acc = udpchksum + (u16_t)~(chksum); 00641 udpchksum = FOLD_U32T(acc); 00642 } else 00643 #endif /* LWIP_CHECKSUM_ON_COPY */ 00644 { 00645 udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len); 00646 } 00647 00648 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00649 if (udpchksum == 0x0000) { 00650 udpchksum = 0xffff; 00651 } 00652 udphdr->chksum = udpchksum; 00653 } 00654 #endif /* CHECKSUM_GEN_UDP */ 00655 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); 00656 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n")); 00657 /* output to IP */ 00658 #if LWIP_NETIF_HWADDRHINT 00659 netif->addr_hint = &(pcb->addr_hint); 00660 #endif /* LWIP_NETIF_HWADDRHINT*/ 00661 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif); 00662 #if LWIP_NETIF_HWADDRHINT 00663 netif->addr_hint = NULL; 00664 #endif /* LWIP_NETIF_HWADDRHINT*/ 00665 } 00666 /* TODO: must this be increased even if error occured? */ 00667 snmp_inc_udpoutdatagrams(); 00668 00669 /* did we chain a separate header pbuf earlier? */ 00670 if (q != p) { 00671 /* free the header pbuf */ 00672 pbuf_free(q); 00673 q = NULL; 00674 /* p is still referenced by the caller, and will live on */ 00675 } 00676 00677 UDP_STATS_INC(udp.xmit); 00678 return err; 00679 } 00680 00681 /** 00682 * Bind an UDP PCB. 00683 * 00684 * @param pcb UDP PCB to be bound with a local address ipaddr and port. 00685 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to 00686 * bind to all local interfaces. 00687 * @param port local UDP port to bind with. Use 0 to automatically bind 00688 * to a random port between UDP_LOCAL_PORT_RANGE_START and 00689 * UDP_LOCAL_PORT_RANGE_END. 00690 * 00691 * ipaddr & port are expected to be in the same byte order as in the pcb. 00692 * 00693 * @return lwIP error code. 00694 * - ERR_OK. Successful. No error occured. 00695 * - ERR_USE. The specified ipaddr and port are already bound to by 00696 * another UDP PCB. 00697 * 00698 * @see udp_disconnect() 00699 */ 00700 err_t 00701 udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) 00702 { 00703 struct udp_pcb *ipcb; 00704 u8_t rebind; 00705 00706 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); 00707 ip_addr_debug_print(UDP_DEBUG, ipaddr); 00708 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); 00709 00710 rebind = 0; 00711 /* Check for double bind and rebind of the same pcb */ 00712 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00713 /* is this UDP PCB already on active list? */ 00714 if (pcb == ipcb) { 00715 /* pcb may occur at most once in active list */ 00716 LWIP_ASSERT("rebind == 0", rebind == 0); 00717 /* pcb already in list, just rebind */ 00718 rebind = 1; 00719 } 00720 00721 /* By default, we don't allow to bind to a port that any other udp 00722 PCB is alread bound to, unless *all* PCBs with that port have tha 00723 REUSEADDR flag set. */ 00724 #if SO_REUSE 00725 else if (((pcb->so_options & SOF_REUSEADDR) == 0) && 00726 ((ipcb->so_options & SOF_REUSEADDR) == 0)) { 00727 #else /* SO_REUSE */ 00728 /* port matches that of PCB in list and REUSEADDR not set -> reject */ 00729 else { 00730 #endif /* SO_REUSE */ 00731 if ((ipcb->local_port == port) && 00732 /* IP address matches, or one is IP_ADDR_ANY? */ 00733 (ip_addr_isany(&(ipcb->local_ip)) || 00734 ip_addr_isany(ipaddr) || 00735 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) { 00736 /* other PCB already binds to this local IP and port */ 00737 LWIP_DEBUGF(UDP_DEBUG, 00738 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); 00739 return ERR_USE; 00740 } 00741 } 00742 } 00743 00744 ip_addr_set(&pcb->local_ip, ipaddr); 00745 00746 /* no port specified? */ 00747 if (port == 0) { 00748 #ifndef UDP_LOCAL_PORT_RANGE_START 00749 /* From http://www.iana.org/assignments/port-numbers: 00750 "The Dynamic and/or Private Ports are those from 49152 through 65535" */ 00751 #define UDP_LOCAL_PORT_RANGE_START 0xc000 00752 #define UDP_LOCAL_PORT_RANGE_END 0xffff 00753 #endif 00754 port = UDP_LOCAL_PORT_RANGE_START; 00755 ipcb = udp_pcbs; 00756 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) { 00757 if (ipcb->local_port == port) { 00758 /* port is already used by another udp_pcb */ 00759 port++; 00760 /* restart scanning all udp pcbs */ 00761 ipcb = udp_pcbs; 00762 } else { 00763 /* go on with next udp pcb */ 00764 ipcb = ipcb->next; 00765 } 00766 } 00767 if (ipcb != NULL) { 00768 /* no more ports available in local range */ 00769 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); 00770 return ERR_USE; 00771 } 00772 } 00773 pcb->local_port = port; 00774 snmp_insert_udpidx_tree(pcb); 00775 /* pcb not active yet? */ 00776 if (rebind == 0) { 00777 /* place the PCB on the active list if not already there */ 00778 pcb->next = udp_pcbs; 00779 udp_pcbs = pcb; 00780 } 00781 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, 00782 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n", 00783 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00784 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), 00785 pcb->local_port)); 00786 return ERR_OK; 00787 } 00788 /** 00789 * Connect an UDP PCB. 00790 * 00791 * This will associate the UDP PCB with the remote address. 00792 * 00793 * @param pcb UDP PCB to be connected with remote address ipaddr and port. 00794 * @param ipaddr remote IP address to connect with. 00795 * @param port remote UDP port to connect with. 00796 * 00797 * @return lwIP error code 00798 * 00799 * ipaddr & port are expected to be in the same byte order as in the pcb. 00800 * 00801 * The udp pcb is bound to a random local port if not already bound. 00802 * 00803 * @see udp_disconnect() 00804 */ 00805 err_t 00806 udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) 00807 { 00808 struct udp_pcb *ipcb; 00809 00810 if (pcb->local_port == 0) { 00811 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 00812 if (err != ERR_OK) { 00813 return err; 00814 } 00815 } 00816 00817 ip_addr_set(&pcb->remote_ip, ipaddr); 00818 pcb->remote_port = port; 00819 pcb->flags |= UDP_FLAGS_CONNECTED; 00820 /** TODO: this functionality belongs in upper layers */ 00821 #ifdef LWIP_UDP_TODO 00822 /* Nail down local IP for netconn_addr()/getsockname() */ 00823 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) { 00824 struct netif *netif; 00825 00826 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) { 00827 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr)); 00828 UDP_STATS_INC(udp.rterr); 00829 return ERR_RTE; 00830 } 00831 /** TODO: this will bind the udp pcb locally, to the interface which 00832 is used to route output packets to the remote address. However, we 00833 might want to accept incoming packets on any interface! */ 00834 pcb->local_ip = netif->ip_addr; 00835 } else if (ip_addr_isany(&pcb->remote_ip)) { 00836 pcb->local_ip.addr = 0; 00837 } 00838 #endif 00839 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, 00840 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n", 00841 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00842 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), 00843 pcb->local_port)); 00844 00845 /* Insert UDP PCB into the list of active UDP PCBs. */ 00846 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00847 if (pcb == ipcb) { 00848 /* already on the list, just return */ 00849 return ERR_OK; 00850 } 00851 } 00852 /* PCB not yet on the list, add PCB now */ 00853 pcb->next = udp_pcbs; 00854 udp_pcbs = pcb; 00855 return ERR_OK; 00856 } 00857 00858 /** 00859 * Disconnect a UDP PCB 00860 * 00861 * @param pcb the udp pcb to disconnect. 00862 */ 00863 void 00864 udp_disconnect(struct udp_pcb *pcb) 00865 { 00866 /* reset remote address association */ 00867 ip_addr_set_any(&pcb->remote_ip); 00868 pcb->remote_port = 0; 00869 /* mark PCB as unconnected */ 00870 pcb->flags &= ~UDP_FLAGS_CONNECTED; 00871 } 00872 00873 /** 00874 * Set a receive callback for a UDP PCB 00875 * 00876 * This callback will be called when receiving a datagram for the pcb. 00877 * 00878 * @param pcb the pcb for wich to set the recv callback 00879 * @param recv function pointer of the callback function 00880 * @param recv_arg additional argument to pass to the callback function 00881 */ 00882 void 00883 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) 00884 { 00885 /* remember recv() callback and user data */ 00886 pcb->recv = recv; 00887 pcb->recv_arg = recv_arg; 00888 } 00889 00890 /** 00891 * Remove an UDP PCB. 00892 * 00893 * @param pcb UDP PCB to be removed. The PCB is removed from the list of 00894 * UDP PCB's and the data structure is freed from memory. 00895 * 00896 * @see udp_new() 00897 */ 00898 void 00899 udp_remove(struct udp_pcb *pcb) 00900 { 00901 struct udp_pcb *pcb2; 00902 00903 snmp_delete_udpidx_tree(pcb); 00904 /* pcb to be removed is first in list? */ 00905 if (udp_pcbs == pcb) { 00906 /* make list start at 2nd pcb */ 00907 udp_pcbs = udp_pcbs->next; 00908 /* pcb not 1st in list */ 00909 } else { 00910 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { 00911 /* find pcb in udp_pcbs list */ 00912 if (pcb2->next != NULL && pcb2->next == pcb) { 00913 /* remove pcb from list */ 00914 pcb2->next = pcb->next; 00915 } 00916 } 00917 } 00918 memp_free(MEMP_UDP_PCB, pcb); 00919 } 00920 00921 /** 00922 * Create a UDP PCB. 00923 * 00924 * @return The UDP PCB which was created. NULL if the PCB data structure 00925 * could not be allocated. 00926 * 00927 * @see udp_remove() 00928 */ 00929 struct udp_pcb * 00930 udp_new(void) 00931 { 00932 struct udp_pcb *pcb; 00933 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); 00934 /* could allocate UDP PCB? */ 00935 if (pcb != NULL) { 00936 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 00937 * which means checksum is generated over the whole datagram per default 00938 * (recommended as default by RFC 3828). */ 00939 /* initialize PCB to all zeroes */ 00940 memset(pcb, 0, sizeof(struct udp_pcb)); 00941 pcb->ttl = UDP_TTL; 00942 } 00943 return pcb; 00944 } 00945 00946 #if UDP_DEBUG 00947 /** 00948 * Print UDP header information for debug purposes. 00949 * 00950 * @param udphdr pointer to the udp header in memory. 00951 */ 00952 void 00953 udp_debug_print(struct udp_hdr *udphdr) 00954 { 00955 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); 00956 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00957 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", 00958 ntohs(udphdr->src), ntohs(udphdr->dest))); 00959 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00960 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", 00961 ntohs(udphdr->len), ntohs(udphdr->chksum))); 00962 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00963 } 00964 #endif /* UDP_DEBUG */ 00965 00966 #endif /* LWIP_UDP */
Generated on Tue Jul 12 2022 23:10:20 by 1.7.2