2003-11-14 13:17:23 +00:00
|
|
|
/**
|
|
|
|
* @file
|
2004-05-05 13:27:27 +00:00
|
|
|
* Implementation of raw protocol PCBs for low-level handling of
|
|
|
|
* different types of protocols besides (or overriding) those
|
2016-07-25 06:20:39 +00:00
|
|
|
* already available in lwIP.\n
|
2016-07-26 11:53:59 +00:00
|
|
|
* See also @ref raw_raw
|
2016-08-07 08:05:34 +00:00
|
|
|
*
|
|
|
|
* @defgroup raw_raw RAW
|
2016-08-07 18:13:27 +00:00
|
|
|
* @ingroup callbackstyle_api
|
2016-08-07 08:05:34 +00:00
|
|
|
* Implementation of raw protocol PCBs for low-level handling of
|
|
|
|
* different types of protocols besides (or overriding) those
|
|
|
|
* already available in lwIP.\n
|
|
|
|
* @see @ref raw_api
|
2003-11-14 13:17:23 +00:00
|
|
|
*/
|
2007-08-09 22:21:44 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2003-11-14 13:17:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwip/opt.h"
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/memp.h"
|
|
|
|
#include "lwip/ip_addr.h"
|
2004-03-12 00:10:07 +00:00
|
|
|
#include "lwip/netif.h"
|
2003-11-14 13:17:23 +00:00
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip/stats.h"
|
2011-05-17 19:35:14 +00:00
|
|
|
#include "lwip/ip6.h"
|
|
|
|
#include "lwip/ip6_addr.h"
|
2012-12-20 02:21:07 +00:00
|
|
|
#include "lwip/inet_chksum.h"
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#include <string.h>
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2004-05-05 13:27:27 +00:00
|
|
|
/** The list of RAW PCBs */
|
2007-10-09 19:59:56 +00:00
|
|
|
static struct raw_pcb *raw_pcbs;
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2016-03-03 21:47:27 +00:00
|
|
|
static u8_t
|
|
|
|
raw_input_match(struct raw_pcb *pcb, u8_t broadcast)
|
|
|
|
{
|
|
|
|
LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
|
|
|
|
|
2016-06-27 18:56:21 +00:00
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
2016-03-03 21:47:27 +00:00
|
|
|
/* Dual-stack: PCBs listening to any IP type also listen to any IP address */
|
2016-07-05 05:36:51 +00:00
|
|
|
if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
|
2016-06-27 18:57:22 +00:00
|
|
|
#if IP_SOF_BROADCAST_RECV
|
2016-07-05 05:36:51 +00:00
|
|
|
if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
|
2016-03-03 21:47:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-27 18:57:22 +00:00
|
|
|
#endif /* IP_SOF_BROADCAST_RECV */
|
2016-03-03 21:47:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-27 18:56:21 +00:00
|
|
|
#endif /* LWIP_IPV4 && LWIP_IPV6 */
|
2016-08-19 07:40:55 +00:00
|
|
|
|
2016-03-03 21:47:27 +00:00
|
|
|
/* Only need to check PCB if incoming IP version matches PCB IP version */
|
2016-07-05 05:36:51 +00:00
|
|
|
if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
|
2016-03-03 21:47:27 +00:00
|
|
|
#if LWIP_IPV4
|
|
|
|
/* Special case: IPv4 broadcast: receive all broadcasts
|
|
|
|
* Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
|
2016-07-05 05:36:51 +00:00
|
|
|
if (broadcast != 0) {
|
2016-03-03 21:47:27 +00:00
|
|
|
#if IP_SOF_BROADCAST_RECV
|
2016-07-05 05:36:51 +00:00
|
|
|
if (ip_get_option(pcb, SOF_BROADCAST))
|
2016-03-03 21:47:27 +00:00
|
|
|
#endif /* IP_SOF_BROADCAST_RECV */
|
|
|
|
{
|
2016-07-05 05:36:51 +00:00
|
|
|
if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
|
2016-03-03 21:47:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* LWIP_IPV4 */
|
|
|
|
/* Handle IPv4 and IPv6: catch all or exact match */
|
2016-07-05 05:36:51 +00:00
|
|
|
if (ip_addr_isany(&pcb->local_ip) ||
|
2016-03-03 21:47:27 +00:00
|
|
|
ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 07:40:55 +00:00
|
|
|
|
2016-03-03 21:47:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
/**
|
2004-05-05 13:27:27 +00:00
|
|
|
* Determine if in incoming IP packet is covered by a RAW PCB
|
|
|
|
* and if so, pass it to a user-provided receive callback function.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
* Given an incoming IP datagram (as a chain of pbufs) this function
|
2004-05-05 13:27:27 +00:00
|
|
|
* finds a corresponding RAW PCB and calls the corresponding receive
|
|
|
|
* callback function.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
2007-08-09 22:21:44 +00:00
|
|
|
* @param p pbuf to be demultiplexed to a RAW PCB.
|
|
|
|
* @param inp network interface on which the datagram was received.
|
|
|
|
* @return - 1 if the packet has been eaten by a RAW PCB receive
|
2004-05-05 13:27:27 +00:00
|
|
|
* callback function. The caller MAY NOT not reference the
|
|
|
|
* packet any longer, and MAY NOT call pbuf_free().
|
|
|
|
* @return - 0 if packet is not eaten (pbuf is still referenced by the
|
|
|
|
* caller).
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2004-05-05 13:27:27 +00:00
|
|
|
u8_t
|
2003-11-14 13:17:23 +00:00
|
|
|
raw_input(struct pbuf *p, struct netif *inp)
|
|
|
|
{
|
2007-11-30 09:44:18 +00:00
|
|
|
struct raw_pcb *pcb, *prev;
|
2005-11-25 12:03:38 +00:00
|
|
|
s16_t proto;
|
2004-05-05 13:27:27 +00:00
|
|
|
u8_t eaten = 0;
|
2016-03-03 21:47:27 +00:00
|
|
|
u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-04-24 08:35:19 +00:00
|
|
|
LWIP_UNUSED_ARG(inp);
|
|
|
|
|
2011-05-17 19:35:14 +00:00
|
|
|
#if LWIP_IPV6
|
2015-04-09 20:21:15 +00:00
|
|
|
#if LWIP_IPV4
|
|
|
|
if (IP_HDR_GET_VERSION(p->payload) == 6)
|
|
|
|
#endif /* LWIP_IPV4 */
|
|
|
|
{
|
2014-02-27 21:18:45 +00:00
|
|
|
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
|
2011-05-17 19:35:14 +00:00
|
|
|
proto = IP6H_NEXTH(ip6hdr);
|
|
|
|
}
|
2015-04-09 20:21:15 +00:00
|
|
|
#if LWIP_IPV4
|
2011-05-17 19:35:14 +00:00
|
|
|
else
|
2015-04-09 20:21:15 +00:00
|
|
|
#endif /* LWIP_IPV4 */
|
2011-05-17 19:35:14 +00:00
|
|
|
#endif /* LWIP_IPV6 */
|
2015-04-09 20:21:15 +00:00
|
|
|
#if LWIP_IPV4
|
2011-05-17 19:35:14 +00:00
|
|
|
{
|
2015-04-09 20:21:15 +00:00
|
|
|
proto = IPH_PROTO((struct ip_hdr *)p->payload);
|
2011-05-17 19:35:14 +00:00
|
|
|
}
|
2015-04-09 20:21:15 +00:00
|
|
|
#endif /* LWIP_IPV4 */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-11-30 09:44:18 +00:00
|
|
|
prev = NULL;
|
2004-05-05 13:27:27 +00:00
|
|
|
pcb = raw_pcbs;
|
|
|
|
/* loop through all raw pcbs until the packet is eaten by one */
|
|
|
|
/* this allows multiple pcbs to match against the packet by design */
|
|
|
|
while ((eaten == 0) && (pcb != NULL)) {
|
2016-03-03 21:47:27 +00:00
|
|
|
if ((pcb->protocol == proto) && raw_input_match(pcb, broadcast)) {
|
|
|
|
/* receive callback function available? */
|
|
|
|
if (pcb->recv != NULL) {
|
2013-01-11 20:59:26 +00:00
|
|
|
#ifndef LWIP_NOASSERT
|
2016-03-03 21:47:27 +00:00
|
|
|
void* old_payload = p->payload;
|
2013-01-11 20:59:26 +00:00
|
|
|
#endif
|
2016-03-03 21:47:27 +00:00
|
|
|
/* the receive callback function did not eat the packet? */
|
|
|
|
eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
|
|
|
|
if (eaten != 0) {
|
|
|
|
/* receive function ate the packet */
|
|
|
|
p = NULL;
|
|
|
|
eaten = 1;
|
|
|
|
if (prev != NULL) {
|
|
|
|
/* move the pcb to the front of raw_pcbs so that is
|
|
|
|
found faster next time */
|
|
|
|
prev->next = pcb->next;
|
|
|
|
pcb->next = raw_pcbs;
|
|
|
|
raw_pcbs = pcb;
|
2007-11-30 09:44:18 +00:00
|
|
|
}
|
2016-03-03 21:47:27 +00:00
|
|
|
} else {
|
|
|
|
/* sanity-check that the receive callback did not alter the pbuf */
|
|
|
|
LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
|
|
|
|
p->payload == old_payload);
|
2004-05-05 13:27:27 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
2016-03-03 21:47:27 +00:00
|
|
|
/* no receive callback function was set for this raw PCB */
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
2016-03-03 21:47:27 +00:00
|
|
|
/* drop the packet */
|
2007-11-30 09:44:18 +00:00
|
|
|
prev = pcb;
|
2004-05-05 13:27:27 +00:00
|
|
|
pcb = pcb->next;
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
2004-05-05 13:27:27 +00:00
|
|
|
return eaten;
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Bind a RAW PCB.
|
|
|
|
*
|
|
|
|
* @param pcb RAW PCB to be bound with a local address ipaddr.
|
|
|
|
* @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
|
|
|
|
* bind to all local interfaces.
|
|
|
|
*
|
|
|
|
* @return lwIP error code.
|
2014-09-18 19:15:13 +00:00
|
|
|
* - ERR_OK. Successful. No error occurred.
|
2004-05-05 13:27:27 +00:00
|
|
|
* - ERR_USE. The specified IP address is already bound to by
|
2003-11-14 13:17:23 +00:00
|
|
|
* another RAW PCB.
|
|
|
|
*
|
|
|
|
* @see raw_disconnect()
|
|
|
|
*/
|
|
|
|
err_t
|
2015-01-24 00:25:18 +00:00
|
|
|
raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
|
2003-11-14 13:17:23 +00:00
|
|
|
{
|
2016-02-24 21:33:05 +00:00
|
|
|
if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
|
2015-04-09 20:21:15 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
|
2003-11-14 13:17:23 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Connect an RAW PCB. This function is required by upper layers
|
2004-05-05 13:27:27 +00:00
|
|
|
* of lwip. Using the raw api you could use raw_sendto() instead
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
* This will associate the RAW PCB with the remote address.
|
|
|
|
*
|
|
|
|
* @param pcb RAW PCB to be connected with remote address ipaddr and port.
|
|
|
|
* @param ipaddr remote IP address to connect with.
|
|
|
|
*
|
|
|
|
* @return lwIP error code
|
|
|
|
*
|
2004-05-05 13:27:27 +00:00
|
|
|
* @see raw_disconnect() and raw_sendto()
|
2003-11-14 13:17:23 +00:00
|
|
|
*/
|
|
|
|
err_t
|
2015-01-24 00:25:18 +00:00
|
|
|
raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
|
2003-11-14 13:17:23 +00:00
|
|
|
{
|
2016-02-24 21:33:05 +00:00
|
|
|
if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
|
2015-04-09 20:21:15 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
|
2003-11-14 13:17:23 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2004-05-05 13:27:27 +00:00
|
|
|
* Set the callback function for received packets that match the
|
2015-10-06 19:08:28 +00:00
|
|
|
* raw PCB's protocol and binding.
|
|
|
|
*
|
2004-05-05 13:27:27 +00:00
|
|
|
* The callback function MUST either
|
|
|
|
* - eat the packet by calling pbuf_free() and returning non-zero. The
|
|
|
|
* packet will not be passed to other raw PCBs or other protocol layers.
|
|
|
|
* - not free the packet, and return zero. The packet will be matched
|
|
|
|
* against further PCBs and/or forwarded to another protocol layers.
|
2015-10-06 19:08:28 +00:00
|
|
|
*
|
2004-05-05 13:27:27 +00:00
|
|
|
* @return non-zero if the packet was free()d, zero if the packet remains
|
|
|
|
* available for others.
|
2003-11-14 13:17:23 +00:00
|
|
|
*/
|
|
|
|
void
|
2010-01-14 20:02:15 +00:00
|
|
|
raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
|
2003-11-14 13:17:23 +00:00
|
|
|
{
|
|
|
|
/* remember recv() callback and user data */
|
2015-04-09 20:21:15 +00:00
|
|
|
pcb->recv = recv;
|
2003-11-14 13:17:23 +00:00
|
|
|
pcb->recv_arg = recv_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Send the raw IP packet to the given address. Note that actually you cannot
|
2004-05-05 13:27:27 +00:00
|
|
|
* modify the IP headers (this is inconsistent with the receive callback where
|
|
|
|
* you actually get the IP headers), you can only specify the IP payload here.
|
|
|
|
* It requires some more changes in lwIP. (there will be a raw_send() function
|
|
|
|
* then.)
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
* @param pcb the raw pcb which to send
|
2016-08-24 06:45:37 +00:00
|
|
|
* @param p the IP payload to send
|
2004-05-05 13:27:27 +00:00
|
|
|
* @param ipaddr the destination address of the IP packet
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
err_t
|
2015-01-24 00:25:18 +00:00
|
|
|
raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
|
2003-11-14 13:17:23 +00:00
|
|
|
{
|
|
|
|
err_t err;
|
|
|
|
struct netif *netif;
|
2015-09-23 11:19:56 +00:00
|
|
|
const ip_addr_t *src_ip;
|
2003-11-14 13:17:23 +00:00
|
|
|
struct pbuf *q; /* q will be sent down the stack */
|
2011-05-25 17:16:35 +00:00
|
|
|
s16_t header_size;
|
2015-04-09 20:21:15 +00:00
|
|
|
const ip_addr_t *dst_ip = ipaddr;
|
|
|
|
|
|
|
|
if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
|
|
|
|
return ERR_VAL;
|
|
|
|
}
|
2011-05-25 17:16:35 +00:00
|
|
|
|
2009-11-22 15:14:46 +00:00
|
|
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
|
2011-05-25 17:16:35 +00:00
|
|
|
|
2016-01-18 14:46:35 +00:00
|
|
|
header_size = (
|
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
2016-02-20 19:40:04 +00:00
|
|
|
IP_IS_V6(ipaddr) ? IP6_HLEN : IP_HLEN);
|
2016-01-18 14:46:35 +00:00
|
|
|
#elif LWIP_IPV4
|
|
|
|
IP_HLEN);
|
|
|
|
#else
|
|
|
|
IP6_HLEN);
|
|
|
|
#endif
|
2011-05-25 17:16:35 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
/* not enough space to add an IP header to first pbuf in given p chain? */
|
2011-05-25 17:16:35 +00:00
|
|
|
if (pbuf_header(p, header_size)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
/* allocate header in new pbuf */
|
|
|
|
q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
|
|
|
|
/* new header pbuf could not be allocated? */
|
|
|
|
if (q == NULL) {
|
2009-11-22 15:14:46 +00:00
|
|
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
|
2003-11-14 13:17:23 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2010-08-03 08:38:59 +00:00
|
|
|
if (p->tot_len != 0) {
|
|
|
|
/* chain header q in front of given pbuf p */
|
|
|
|
pbuf_chain(q, p);
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
/* { first pbuf q points to header pbuf } */
|
2004-05-05 13:27:27 +00:00
|
|
|
LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
|
2015-10-06 19:57:40 +00:00
|
|
|
} else {
|
2003-11-14 13:17:23 +00:00
|
|
|
/* first pbuf q equals given pbuf */
|
|
|
|
q = p;
|
2015-10-06 19:57:40 +00:00
|
|
|
if (pbuf_header(q, -header_size)) {
|
2007-03-21 12:55:00 +00:00
|
|
|
LWIP_ASSERT("Can't restore header we just removed!", 0);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
2009-04-29 12:42:43 +00:00
|
|
|
|
2016-02-22 12:38:40 +00:00
|
|
|
netif = ip_route(&pcb->local_ip, dst_ip);
|
2011-05-25 17:16:35 +00:00
|
|
|
if (netif == NULL) {
|
|
|
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
|
2015-04-09 20:21:15 +00:00
|
|
|
ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, dst_ip);
|
2004-05-05 13:27:27 +00:00
|
|
|
/* free any temporary header pbuf allocated by pbuf_header() */
|
2003-11-14 13:17:23 +00:00
|
|
|
if (q != p) {
|
|
|
|
pbuf_free(q);
|
|
|
|
}
|
|
|
|
return ERR_RTE;
|
|
|
|
}
|
|
|
|
|
2009-04-29 12:42:43 +00:00
|
|
|
#if IP_SOF_BROADCAST
|
2016-04-06 20:31:31 +00:00
|
|
|
if (IP_IS_V4(ipaddr))
|
2011-05-25 17:16:35 +00:00
|
|
|
{
|
2011-05-17 19:35:14 +00:00
|
|
|
/* broadcast filter? */
|
2011-12-17 21:12:01 +00:00
|
|
|
if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
|
2011-05-17 19:35:14 +00:00
|
|
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
|
|
|
|
/* free any temporary header pbuf allocated by pbuf_header() */
|
|
|
|
if (q != p) {
|
|
|
|
pbuf_free(q);
|
|
|
|
}
|
|
|
|
return ERR_VAL;
|
2009-04-29 12:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* IP_SOF_BROADCAST */
|
|
|
|
|
2015-04-09 20:21:15 +00:00
|
|
|
if (ip_addr_isany(&pcb->local_ip)) {
|
2011-05-26 15:46:44 +00:00
|
|
|
/* use outgoing network interface IP address as source address */
|
2016-02-22 12:38:40 +00:00
|
|
|
src_ip = ip_netif_get_local_ip(netif, dst_ip);
|
2011-05-17 19:35:14 +00:00
|
|
|
#if LWIP_IPV6
|
2011-05-26 15:46:44 +00:00
|
|
|
if (src_ip == NULL) {
|
|
|
|
if (q != p) {
|
|
|
|
pbuf_free(q);
|
2011-05-17 19:35:14 +00:00
|
|
|
}
|
2011-05-26 15:46:44 +00:00
|
|
|
return ERR_RTE;
|
2011-05-17 19:35:14 +00:00
|
|
|
}
|
|
|
|
#endif /* LWIP_IPV6 */
|
2011-05-26 15:46:44 +00:00
|
|
|
} else {
|
|
|
|
/* use RAW PCB local IP address as source address */
|
|
|
|
src_ip = &pcb->local_ip;
|
2011-05-17 19:35:14 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2012-12-20 02:21:07 +00:00
|
|
|
#if LWIP_IPV6
|
2014-02-20 19:09:33 +00:00
|
|
|
/* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
|
|
|
|
compute the checksum and update the checksum in the payload. */
|
2016-02-20 19:40:04 +00:00
|
|
|
if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
|
2015-09-24 12:38:52 +00:00
|
|
|
u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
|
2014-02-21 07:41:44 +00:00
|
|
|
LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
|
|
|
|
SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
|
2012-12-20 02:21:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-26 15:46:44 +00:00
|
|
|
NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
|
2016-02-22 12:38:40 +00:00
|
|
|
err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, pcb->protocol, netif);
|
2011-05-25 17:16:35 +00:00
|
|
|
NETIF_SET_HWADDRHINT(netif, NULL);
|
2011-05-26 15:46:44 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
/* did we chain a header earlier? */
|
|
|
|
if (q != p) {
|
|
|
|
/* free the header */
|
|
|
|
pbuf_free(q);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Send the raw IP packet to the address given by raw_connect()
|
|
|
|
*
|
|
|
|
* @param pcb the raw pcb which to send
|
2016-08-24 06:45:37 +00:00
|
|
|
* @param p the IP payload to send
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
raw_send(struct raw_pcb *pcb, struct pbuf *p)
|
|
|
|
{
|
2015-04-09 20:21:15 +00:00
|
|
|
return raw_sendto(pcb, p, &pcb->remote_ip);
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Remove an RAW PCB.
|
|
|
|
*
|
|
|
|
* @param pcb RAW PCB to be removed. The PCB is removed from the list of
|
|
|
|
* RAW PCB's and the data structure is freed from memory.
|
|
|
|
*
|
|
|
|
* @see raw_new()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
raw_remove(struct raw_pcb *pcb)
|
|
|
|
{
|
|
|
|
struct raw_pcb *pcb2;
|
|
|
|
/* pcb to be removed is first in list? */
|
|
|
|
if (raw_pcbs == pcb) {
|
|
|
|
/* make list start at 2nd pcb */
|
|
|
|
raw_pcbs = raw_pcbs->next;
|
2004-05-05 13:27:27 +00:00
|
|
|
/* pcb not 1st in list */
|
2007-06-07 21:29:13 +00:00
|
|
|
} else {
|
2015-10-07 08:03:12 +00:00
|
|
|
for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
|
2007-06-07 21:29:13 +00:00
|
|
|
/* find pcb in raw_pcbs list */
|
|
|
|
if (pcb2->next != NULL && pcb2->next == pcb) {
|
|
|
|
/* remove pcb from list */
|
|
|
|
pcb2->next = pcb->next;
|
2015-11-18 09:10:33 +00:00
|
|
|
break;
|
2007-06-07 21:29:13 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
memp_free(MEMP_RAW_PCB, pcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
2003-11-14 13:17:23 +00:00
|
|
|
* Create a RAW PCB.
|
|
|
|
*
|
|
|
|
* @return The RAW PCB which was created. NULL if the PCB data structure
|
|
|
|
* could not be allocated.
|
|
|
|
*
|
|
|
|
* @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
|
|
|
|
*
|
|
|
|
* @see raw_remove()
|
|
|
|
*/
|
|
|
|
struct raw_pcb *
|
2010-01-18 08:11:57 +00:00
|
|
|
raw_new(u8_t proto)
|
|
|
|
{
|
2003-11-14 13:17:23 +00:00
|
|
|
struct raw_pcb *pcb;
|
|
|
|
|
2009-11-22 15:14:46 +00:00
|
|
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
|
2003-11-14 13:17:23 +00:00
|
|
|
/* could allocate RAW PCB? */
|
|
|
|
if (pcb != NULL) {
|
|
|
|
/* initialize PCB to all zeroes */
|
|
|
|
memset(pcb, 0, sizeof(struct raw_pcb));
|
|
|
|
pcb->protocol = proto;
|
|
|
|
pcb->ttl = RAW_TTL;
|
|
|
|
pcb->next = raw_pcbs;
|
|
|
|
raw_pcbs = pcb;
|
|
|
|
}
|
|
|
|
return pcb;
|
|
|
|
}
|
|
|
|
|
2011-05-17 19:35:14 +00:00
|
|
|
/**
|
2016-07-26 11:53:59 +00:00
|
|
|
* @ingroup raw_raw
|
|
|
|
* Create a RAW PCB for specific IP type.
|
2011-05-17 19:35:14 +00:00
|
|
|
*
|
|
|
|
* @return The RAW PCB which was created. NULL if the PCB data structure
|
|
|
|
* could not be allocated.
|
|
|
|
*
|
2016-09-27 07:46:51 +00:00
|
|
|
* @param type IP address type, see @ref lwip_ip_addr_type definitions.
|
2016-08-26 13:58:18 +00:00
|
|
|
* If you want to listen to IPv4 and IPv6 (dual-stack) packets,
|
|
|
|
* supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
|
2011-05-17 19:35:14 +00:00
|
|
|
* @param proto the protocol number (next header) of the IPv6 packet payload
|
|
|
|
* (e.g. IP6_NEXTH_ICMP6)
|
|
|
|
*
|
|
|
|
* @see raw_remove()
|
|
|
|
*/
|
|
|
|
struct raw_pcb *
|
2016-03-03 21:47:27 +00:00
|
|
|
raw_new_ip_type(u8_t type, u8_t proto)
|
2011-05-17 19:35:14 +00:00
|
|
|
{
|
|
|
|
struct raw_pcb *pcb;
|
|
|
|
pcb = raw_new(proto);
|
2016-03-03 21:47:27 +00:00
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
2016-07-05 05:36:51 +00:00
|
|
|
if (pcb != NULL) {
|
2016-03-03 21:47:27 +00:00
|
|
|
IP_SET_TYPE_VAL(pcb->local_ip, type);
|
|
|
|
IP_SET_TYPE_VAL(pcb->remote_ip, type);
|
2016-03-02 22:44:51 +00:00
|
|
|
}
|
2016-03-03 21:55:42 +00:00
|
|
|
#else /* LWIP_IPV4 && LWIP_IPV6 */
|
|
|
|
LWIP_UNUSED_ARG(type);
|
2016-03-03 21:47:27 +00:00
|
|
|
#endif /* LWIP_IPV4 && LWIP_IPV6 */
|
2011-05-17 19:35:14 +00:00
|
|
|
return pcb;
|
|
|
|
}
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#endif /* LWIP_RAW */
|