Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_raw.c
00001 /** 00002 * @file 00003 * Implementation of raw protocol PCBs for low-level handling of 00004 * different types of protocols besides (or overriding) those 00005 * already available in lwIP.\n 00006 * See also @ref raw_raw 00007 * 00008 * @defgroup raw_raw RAW 00009 * @ingroup callbackstyle_api 00010 * Implementation of raw protocol PCBs for low-level handling of 00011 * different types of protocols besides (or overriding) those 00012 * already available in lwIP.\n 00013 * @see @ref api 00014 */ 00015 00016 /* 00017 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00018 * All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without modification, 00021 * are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, 00024 * this list of conditions and the following disclaimer. 00025 * 2. Redistributions in binary form must reproduce the above copyright notice, 00026 * this list of conditions and the following disclaimer in the documentation 00027 * and/or other materials provided with the distribution. 00028 * 3. The name of the author may not be used to endorse or promote products 00029 * derived from this software without specific prior written permission. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00032 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00033 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00034 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00035 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00036 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00037 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00038 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00039 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00040 * OF SUCH DAMAGE. 00041 * 00042 * This file is part of the lwIP TCP/IP stack. 00043 * 00044 * Author: Adam Dunkels <adam@sics.se> 00045 * 00046 */ 00047 00048 #include "lwip/opt.h" 00049 00050 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */ 00051 00052 #include "lwip/def.h" 00053 #include "lwip/memp.h" 00054 #include "lwip/ip_addr.h" 00055 #include "lwip/netif.h" 00056 #include "lwip/raw.h" 00057 #include "lwip/priv/raw_priv.h" 00058 #include "lwip/stats.h" 00059 #include "lwip/ip6.h" 00060 #include "lwip/ip6_addr.h" 00061 #include "lwip/inet_chksum.h" 00062 00063 #include <string.h> 00064 00065 /** The list of RAW PCBs */ 00066 static struct raw_pcb *raw_pcbs; 00067 00068 static u8_t 00069 raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast) 00070 { 00071 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */ 00072 00073 /* check if PCB is bound to specific netif */ 00074 if ((pcb->netif_idx != NETIF_NO_INDEX) && 00075 (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { 00076 return 0; 00077 } 00078 00079 #if LWIP_IPV4 && LWIP_IPV6 00080 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */ 00081 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { 00082 #if IP_SOF_BROADCAST_RECV 00083 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { 00084 return 0; 00085 } 00086 #endif /* IP_SOF_BROADCAST_RECV */ 00087 return 1; 00088 } 00089 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00090 00091 /* Only need to check PCB if incoming IP version matches PCB IP version */ 00092 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { 00093 #if LWIP_IPV4 00094 /* Special case: IPv4 broadcast: receive all broadcasts 00095 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */ 00096 if (broadcast != 0) { 00097 #if IP_SOF_BROADCAST_RECV 00098 if (ip_get_option(pcb, SOF_BROADCAST)) 00099 #endif /* IP_SOF_BROADCAST_RECV */ 00100 { 00101 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) { 00102 return 1; 00103 } 00104 } 00105 } else 00106 #endif /* LWIP_IPV4 */ 00107 /* Handle IPv4 and IPv6: catch all or exact match */ 00108 if (ip_addr_isany(&pcb->local_ip) || 00109 ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { 00110 return 1; 00111 } 00112 } 00113 00114 return 0; 00115 } 00116 00117 /** 00118 * Determine if in incoming IP packet is covered by a RAW PCB 00119 * and if so, pass it to a user-provided receive callback function. 00120 * 00121 * Given an incoming IP datagram (as a chain of pbufs) this function 00122 * finds a corresponding RAW PCB and calls the corresponding receive 00123 * callback function. 00124 * 00125 * @param p pbuf to be demultiplexed to a RAW PCB. 00126 * @param inp network interface on which the datagram was received. 00127 * @return - 1 if the packet has been eaten by a RAW PCB receive 00128 * callback function. The caller MAY NOT not reference the 00129 * packet any longer, and MAY NOT call pbuf_free(). 00130 * @return - 0 if packet is not eaten (pbuf is still referenced by the 00131 * caller). 00132 * 00133 */ 00134 raw_input_state_t 00135 raw_input(struct pbuf *p, struct netif *inp) 00136 { 00137 struct raw_pcb *pcb, *prev; 00138 s16_t proto; 00139 raw_input_state_t ret = RAW_INPUT_NONE; 00140 u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()); 00141 00142 LWIP_UNUSED_ARG(inp); 00143 00144 #if LWIP_IPV6 00145 #if LWIP_IPV4 00146 if (IP_HDR_GET_VERSION(p->payload) == 6) 00147 #endif /* LWIP_IPV4 */ 00148 { 00149 struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload; 00150 proto = IP6H_NEXTH(ip6hdr); 00151 } 00152 #if LWIP_IPV4 00153 else 00154 #endif /* LWIP_IPV4 */ 00155 #endif /* LWIP_IPV6 */ 00156 #if LWIP_IPV4 00157 { 00158 proto = IPH_PROTO((struct ip_hdr *)p->payload); 00159 } 00160 #endif /* LWIP_IPV4 */ 00161 00162 prev = NULL; 00163 pcb = raw_pcbs; 00164 /* loop through all raw pcbs until the packet is eaten by one */ 00165 /* this allows multiple pcbs to match against the packet by design */ 00166 while (pcb != NULL) { 00167 if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) && 00168 (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) || 00169 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) { 00170 /* receive callback function available? */ 00171 if (pcb->recv != NULL) { 00172 u8_t eaten; 00173 #ifndef LWIP_NOASSERT 00174 void *old_payload = p->payload; 00175 #endif 00176 ret = RAW_INPUT_DELIVERED; 00177 /* the receive callback function did not eat the packet? */ 00178 eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()); 00179 if (eaten != 0) { 00180 /* receive function ate the packet */ 00181 p = NULL; 00182 if (prev != NULL) { 00183 /* move the pcb to the front of raw_pcbs so that is 00184 found faster next time */ 00185 prev->next = pcb->next; 00186 pcb->next = raw_pcbs; 00187 raw_pcbs = pcb; 00188 } 00189 return RAW_INPUT_EATEN; 00190 } else { 00191 /* sanity-check that the receive callback did not alter the pbuf */ 00192 LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet", 00193 p->payload == old_payload); 00194 } 00195 } 00196 /* no receive callback function was set for this raw PCB */ 00197 } 00198 /* drop the packet */ 00199 prev = pcb; 00200 pcb = pcb->next; 00201 } 00202 return ret; 00203 } 00204 00205 /** 00206 * @ingroup raw_raw 00207 * Bind a RAW PCB. 00208 * 00209 * @param pcb RAW PCB to be bound with a local address ipaddr. 00210 * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to 00211 * bind to all local interfaces. 00212 * 00213 * @return lwIP error code. 00214 * - ERR_OK. Successful. No error occurred. 00215 * - ERR_USE. The specified IP address is already bound to by 00216 * another RAW PCB. 00217 * 00218 * @see raw_disconnect() 00219 */ 00220 err_t 00221 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr) 00222 { 00223 LWIP_ASSERT_CORE_LOCKED(); 00224 if ((pcb == NULL) || (ipaddr == NULL)) { 00225 return ERR_VAL; 00226 } 00227 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr); 00228 #if LWIP_IPV6 && LWIP_IPV6_SCOPES 00229 /* If the given IP address should have a zone but doesn't, assign one now. 00230 * This is legacy support: scope-aware callers should always provide properly 00231 * zoned source addresses. */ 00232 if (IP_IS_V6(&pcb->local_ip) && 00233 ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) { 00234 ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip)); 00235 } 00236 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ 00237 return ERR_OK; 00238 } 00239 00240 /** 00241 * @ingroup raw_raw 00242 * Bind an RAW PCB to a specific netif. 00243 * After calling this function, all packets received via this PCB 00244 * are guaranteed to have come in via the specified netif, and all 00245 * outgoing packets will go out via the specified netif. 00246 * 00247 * @param pcb RAW PCB to be bound with netif. 00248 * @param netif netif to bind to. Can be NULL. 00249 * 00250 * @see raw_disconnect() 00251 */ 00252 void 00253 raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif) 00254 { 00255 LWIP_ASSERT_CORE_LOCKED(); 00256 if (netif != NULL) { 00257 pcb->netif_idx = netif_get_index(netif); 00258 } else { 00259 pcb->netif_idx = NETIF_NO_INDEX; 00260 } 00261 } 00262 00263 /** 00264 * @ingroup raw_raw 00265 * Connect an RAW PCB. This function is required by upper layers 00266 * of lwip. Using the raw api you could use raw_sendto() instead 00267 * 00268 * This will associate the RAW PCB with the remote address. 00269 * 00270 * @param pcb RAW PCB to be connected with remote address ipaddr and port. 00271 * @param ipaddr remote IP address to connect with. 00272 * 00273 * @return lwIP error code 00274 * 00275 * @see raw_disconnect() and raw_sendto() 00276 */ 00277 err_t 00278 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr) 00279 { 00280 LWIP_ASSERT_CORE_LOCKED(); 00281 if ((pcb == NULL) || (ipaddr == NULL)) { 00282 return ERR_VAL; 00283 } 00284 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr); 00285 #if LWIP_IPV6 && LWIP_IPV6_SCOPES 00286 /* If the given IP address should have a zone but doesn't, assign one now, 00287 * using the bound address to make a more informed decision when possible. */ 00288 if (IP_IS_V6(&pcb->remote_ip) && 00289 ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) { 00290 ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip)); 00291 } 00292 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ 00293 raw_set_flags(pcb, RAW_FLAGS_CONNECTED); 00294 return ERR_OK; 00295 } 00296 00297 /** 00298 * @ingroup raw_raw 00299 * Disconnect a RAW PCB. 00300 * 00301 * @param pcb the raw pcb to disconnect. 00302 */ 00303 void 00304 raw_disconnect(struct raw_pcb *pcb) 00305 { 00306 LWIP_ASSERT_CORE_LOCKED(); 00307 /* reset remote address association */ 00308 #if LWIP_IPV4 && LWIP_IPV6 00309 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { 00310 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE); 00311 } else { 00312 #endif 00313 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip); 00314 #if LWIP_IPV4 && LWIP_IPV6 00315 } 00316 #endif 00317 pcb->netif_idx = NETIF_NO_INDEX; 00318 /* mark PCB as unconnected */ 00319 raw_clear_flags(pcb, RAW_FLAGS_CONNECTED); 00320 } 00321 00322 /** 00323 * @ingroup raw_raw 00324 * Set the callback function for received packets that match the 00325 * raw PCB's protocol and binding. 00326 * 00327 * The callback function MUST either 00328 * - eat the packet by calling pbuf_free() and returning non-zero. The 00329 * packet will not be passed to other raw PCBs or other protocol layers. 00330 * - not free the packet, and return zero. The packet will be matched 00331 * against further PCBs and/or forwarded to another protocol layers. 00332 */ 00333 void 00334 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg) 00335 { 00336 LWIP_ASSERT_CORE_LOCKED(); 00337 /* remember recv() callback and user data */ 00338 pcb->recv = recv; 00339 pcb->recv_arg = recv_arg; 00340 } 00341 00342 /** 00343 * @ingroup raw_raw 00344 * Send the raw IP packet to the given address. An IP header will be prepended 00345 * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that 00346 * case, the packet must include an IP header, which will then be sent as is. 00347 * 00348 * @param pcb the raw pcb which to send 00349 * @param p the IP payload to send 00350 * @param ipaddr the destination address of the IP packet 00351 * 00352 */ 00353 err_t 00354 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) 00355 { 00356 struct netif *netif; 00357 const ip_addr_t *src_ip; 00358 00359 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { 00360 return ERR_VAL; 00361 } 00362 00363 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n")); 00364 00365 if (pcb->netif_idx != NETIF_NO_INDEX) { 00366 netif = netif_get_by_index(pcb->netif_idx); 00367 } else { 00368 #if LWIP_MULTICAST_TX_OPTIONS 00369 netif = NULL; 00370 if (ip_addr_ismulticast(ipaddr)) { 00371 /* For multicast-destined packets, use the user-provided interface index to 00372 * determine the outgoing interface, if an interface index is set and a 00373 * matching netif can be found. Otherwise, fall back to regular routing. */ 00374 netif = netif_get_by_index(pcb->mcast_ifindex); 00375 } 00376 00377 if (netif == NULL) 00378 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00379 { 00380 netif = ip_route(&pcb->local_ip, ipaddr); 00381 } 00382 } 00383 00384 if (netif == NULL) { 00385 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to ")); 00386 ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr); 00387 return ERR_RTE; 00388 } 00389 00390 if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) { 00391 /* use outgoing network interface IP address as source address */ 00392 src_ip = ip_netif_get_local_ip(netif, ipaddr); 00393 #if LWIP_IPV6 00394 if (src_ip == NULL) { 00395 return ERR_RTE; 00396 } 00397 #endif /* LWIP_IPV6 */ 00398 } else { 00399 /* use RAW PCB local IP address as source address */ 00400 src_ip = &pcb->local_ip; 00401 } 00402 00403 return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip); 00404 } 00405 00406 /** 00407 * @ingroup raw_raw 00408 * Send the raw IP packet to the given address, using a particular outgoing 00409 * netif and source IP address. An IP header will be prepended to the packet, 00410 * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the 00411 * packet must include an IP header, which will then be sent as is. 00412 * 00413 * @param pcb RAW PCB used to send the data 00414 * @param p chain of pbufs to be sent 00415 * @param dst_ip destination IP address 00416 * @param netif the netif used for sending 00417 * @param src_ip source IP address 00418 */ 00419 err_t 00420 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, 00421 struct netif *netif, const ip_addr_t *src_ip) 00422 { 00423 err_t err; 00424 struct pbuf *q; /* q will be sent down the stack */ 00425 u16_t header_size; 00426 u8_t ttl; 00427 00428 LWIP_ASSERT_CORE_LOCKED(); 00429 00430 if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) || 00431 !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { 00432 return ERR_VAL; 00433 } 00434 00435 header_size = ( 00436 #if LWIP_IPV4 && LWIP_IPV6 00437 IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN); 00438 #elif LWIP_IPV4 00439 IP_HLEN); 00440 #else 00441 IP6_HLEN); 00442 #endif 00443 00444 /* Handle the HDRINCL option as an exception: none of the code below applies 00445 * to this case, and sending the packet needs to be done differently too. */ 00446 if (pcb->flags & RAW_FLAGS_HDRINCL) { 00447 /* A full header *must* be present in the first pbuf of the chain, as the 00448 * output routines may access its fields directly. */ 00449 if (p->len < header_size) { 00450 return ERR_VAL; 00451 } 00452 /* @todo multicast loop support, if at all desired for this scenario.. */ 00453 NETIF_SET_HINTS(netif, &pcb->netif_hints); 00454 err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif); 00455 NETIF_RESET_HINTS(netif); 00456 return err; 00457 } 00458 00459 /* packet too large to add an IP header without causing an overflow? */ 00460 if ((u16_t)(p->tot_len + header_size) < p->tot_len) { 00461 return ERR_MEM; 00462 } 00463 /* not enough space to add an IP header to first pbuf in given p chain? */ 00464 if (pbuf_add_header(p, header_size)) { 00465 /* allocate header in new pbuf */ 00466 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM); 00467 /* new header pbuf could not be allocated? */ 00468 if (q == NULL) { 00469 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n")); 00470 return ERR_MEM; 00471 } 00472 if (p->tot_len != 0) { 00473 /* chain header q in front of given pbuf p */ 00474 pbuf_chain(q, p); 00475 } 00476 /* { first pbuf q points to header pbuf } */ 00477 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); 00478 } else { 00479 /* first pbuf q equals given pbuf */ 00480 q = p; 00481 if (pbuf_remove_header(q, header_size)) { 00482 LWIP_ASSERT("Can't restore header we just removed!", 0); 00483 return ERR_MEM; 00484 } 00485 } 00486 00487 #if IP_SOF_BROADCAST 00488 if (IP_IS_V4(dst_ip)) { 00489 /* broadcast filter? */ 00490 if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) { 00491 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); 00492 /* free any temporary header pbuf allocated by pbuf_header() */ 00493 if (q != p) { 00494 pbuf_free(q); 00495 } 00496 return ERR_VAL; 00497 } 00498 } 00499 #endif /* IP_SOF_BROADCAST */ 00500 00501 /* Multicast Loop? */ 00502 #if LWIP_MULTICAST_TX_OPTIONS 00503 if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) { 00504 q->flags |= PBUF_FLAG_MCASTLOOP; 00505 } 00506 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00507 00508 #if LWIP_IPV6 00509 /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542, 00510 compute the checksum and update the checksum in the payload. */ 00511 if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) { 00512 u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip)); 00513 LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2)); 00514 SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t)); 00515 } 00516 #endif 00517 00518 /* Determine TTL to use */ 00519 #if LWIP_MULTICAST_TX_OPTIONS 00520 ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl); 00521 #else /* LWIP_MULTICAST_TX_OPTIONS */ 00522 ttl = pcb->ttl; 00523 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00524 00525 NETIF_SET_HINTS(netif, &pcb->netif_hints); 00526 err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif); 00527 NETIF_RESET_HINTS(netif); 00528 00529 /* did we chain a header earlier? */ 00530 if (q != p) { 00531 /* free the header */ 00532 pbuf_free(q); 00533 } 00534 return err; 00535 } 00536 00537 /** 00538 * @ingroup raw_raw 00539 * Send the raw IP packet to the address given by raw_connect() 00540 * 00541 * @param pcb the raw pcb which to send 00542 * @param p the IP payload to send 00543 * 00544 */ 00545 err_t 00546 raw_send(struct raw_pcb *pcb, struct pbuf *p) 00547 { 00548 return raw_sendto(pcb, p, &pcb->remote_ip); 00549 } 00550 00551 /** 00552 * @ingroup raw_raw 00553 * Remove an RAW PCB. 00554 * 00555 * @param pcb RAW PCB to be removed. The PCB is removed from the list of 00556 * RAW PCB's and the data structure is freed from memory. 00557 * 00558 * @see raw_new() 00559 */ 00560 void 00561 raw_remove(struct raw_pcb *pcb) 00562 { 00563 struct raw_pcb *pcb2; 00564 LWIP_ASSERT_CORE_LOCKED(); 00565 /* pcb to be removed is first in list? */ 00566 if (raw_pcbs == pcb) { 00567 /* make list start at 2nd pcb */ 00568 raw_pcbs = raw_pcbs->next; 00569 /* pcb not 1st in list */ 00570 } else { 00571 for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { 00572 /* find pcb in raw_pcbs list */ 00573 if (pcb2->next != NULL && pcb2->next == pcb) { 00574 /* remove pcb from list */ 00575 pcb2->next = pcb->next; 00576 break; 00577 } 00578 } 00579 } 00580 memp_free(MEMP_RAW_PCB, pcb); 00581 } 00582 00583 /** 00584 * @ingroup raw_raw 00585 * Create a RAW PCB. 00586 * 00587 * @return The RAW PCB which was created. NULL if the PCB data structure 00588 * could not be allocated. 00589 * 00590 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP) 00591 * 00592 * @see raw_remove() 00593 */ 00594 struct raw_pcb * 00595 raw_new(u8_t proto) 00596 { 00597 struct raw_pcb *pcb; 00598 00599 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n")); 00600 LWIP_ASSERT_CORE_LOCKED(); 00601 00602 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB); 00603 /* could allocate RAW PCB? */ 00604 if (pcb != NULL) { 00605 /* initialize PCB to all zeroes */ 00606 memset(pcb, 0, sizeof(struct raw_pcb)); 00607 pcb->protocol = proto; 00608 pcb->ttl = RAW_TTL; 00609 #if LWIP_MULTICAST_TX_OPTIONS 00610 raw_set_multicast_ttl(pcb, RAW_TTL); 00611 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00612 pcb->next = raw_pcbs; 00613 raw_pcbs = pcb; 00614 } 00615 return pcb; 00616 } 00617 00618 /** 00619 * @ingroup raw_raw 00620 * Create a RAW PCB for specific IP type. 00621 * 00622 * @return The RAW PCB which was created. NULL if the PCB data structure 00623 * could not be allocated. 00624 * 00625 * @param type IP address type, see @ref lwip_ip_addr_type definitions. 00626 * If you want to listen to IPv4 and IPv6 (dual-stack) packets, 00627 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE. 00628 * @param proto the protocol number (next header) of the IPv6 packet payload 00629 * (e.g. IP6_NEXTH_ICMP6) 00630 * 00631 * @see raw_remove() 00632 */ 00633 struct raw_pcb * 00634 raw_new_ip_type(u8_t type, u8_t proto) 00635 { 00636 struct raw_pcb *pcb; 00637 LWIP_ASSERT_CORE_LOCKED(); 00638 pcb = raw_new(proto); 00639 #if LWIP_IPV4 && LWIP_IPV6 00640 if (pcb != NULL) { 00641 IP_SET_TYPE_VAL(pcb->local_ip, type); 00642 IP_SET_TYPE_VAL(pcb->remote_ip, type); 00643 } 00644 #else /* LWIP_IPV4 && LWIP_IPV6 */ 00645 LWIP_UNUSED_ARG(type); 00646 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00647 return pcb; 00648 } 00649 00650 /** This function is called from netif.c when address is changed 00651 * 00652 * @param old_addr IP address of the netif before change 00653 * @param new_addr IP address of the netif after change 00654 */ 00655 void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr) 00656 { 00657 struct raw_pcb *rpcb; 00658 00659 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) { 00660 for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) { 00661 /* PCB bound to current local interface address? */ 00662 if (ip_addr_cmp(&rpcb->local_ip, old_addr)) { 00663 /* The PCB is bound to the old ipaddr and 00664 * is set to bound to the new one instead */ 00665 ip_addr_copy(rpcb->local_ip, *new_addr); 00666 } 00667 } 00668 } 00669 } 00670 00671 #endif /* LWIP_RAW */
Generated on Tue Jul 12 2022 13:54:29 by
