mirror of
https://git.savannah.nongnu.org/git/lwip.git
synced 2026-09-26 10:43:50 +08:00
Combined IPv4 and IPv6 code where possible, added defines to access IPv4/IPv6 in non-IP code so that the code is more readable.
This commit is contained in:
@@ -48,13 +48,13 @@
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/ip6.h"
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip6_chksum.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/icmp6.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define ETHTYPE_IPV6 0x86dd
|
||||
#define ETHTYPE_IPV6 0x86DD
|
||||
|
||||
/** The ethernet address */
|
||||
#ifdef PACK_STRUCT_USE_INCLUDES
|
||||
@@ -89,7 +89,30 @@ PACK_STRUCT_END
|
||||
|
||||
#define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
|
||||
|
||||
static err_t ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst);
|
||||
/**
|
||||
* Send an IPv6 packet on the network using netif->linkoutput
|
||||
* The ethernet header is filled in before sending.
|
||||
*
|
||||
* @params netif the lwIP network interface on which to send the packet
|
||||
* @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
|
||||
* @params src the source MAC address to be copied into the ethernet header
|
||||
* @params dst the destination MAC address to be copied into the ethernet header
|
||||
* @return ERR_OK if the packet was sent, any other err_t on failure
|
||||
*/
|
||||
static err_t
|
||||
ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
|
||||
{
|
||||
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
|
||||
|
||||
LWIP_ASSERT("netif->hwaddr_len must be 6 for ethip6!",
|
||||
(netif->hwaddr_len == 6));
|
||||
SMEMCPY(ðhdr->dest, dst, 6);
|
||||
SMEMCPY(ðhdr->src, src, 6);
|
||||
ethhdr->type = PP_HTONS(ETHTYPE_IPV6);
|
||||
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("ethip6_send: sending packet %p\n", (void *)p));
|
||||
/* send the packet */
|
||||
return netif->linkoutput(netif, p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
|
||||
@@ -169,29 +192,4 @@ ethip6_output(struct netif *netif, struct pbuf *q, ip6_addr_t *ip6addr)
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an IPv6 packet on the network using netif->linkoutput
|
||||
* The ethernet header is filled in before sending.
|
||||
*
|
||||
* @params netif the lwIP network interface on which to send the packet
|
||||
* @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
|
||||
* @params src the source MAC address to be copied into the ethernet header
|
||||
* @params dst the destination MAC address to be copied into the ethernet header
|
||||
* @return ERR_OK if the packet was sent, any other err_t on failure
|
||||
*/
|
||||
static err_t
|
||||
ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
|
||||
{
|
||||
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
|
||||
|
||||
LWIP_ASSERT("netif->hwaddr_len must be 6 for ethip6!",
|
||||
(netif->hwaddr_len == 6));
|
||||
SMEMCPY(ðhdr->dest, dst, 6);
|
||||
SMEMCPY(ðhdr->src, src, 6);
|
||||
ethhdr->type = PP_HTONS(ETHTYPE_IPV6);
|
||||
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("ethip6_send: sending packet %p\n", (void *)p));
|
||||
/* send the packet */
|
||||
return netif->linkoutput(netif, p);
|
||||
}
|
||||
|
||||
#endif /* LWIP_IPV6 && LWIP_ETHERNET */
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
#include "lwip/icmp6.h"
|
||||
#include "lwip/ip6.h"
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip6_chksum.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/nd6.h"
|
||||
@@ -63,7 +63,7 @@
|
||||
#endif
|
||||
|
||||
/* Forward declarations */
|
||||
static void icmp6_send_response(struct pbuf *p, u8_t type, u8_t code, u32_t data);
|
||||
static void icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type);
|
||||
|
||||
|
||||
/**
|
||||
@@ -96,8 +96,8 @@ icmp6_input(struct pbuf *p, struct netif *inp)
|
||||
icmp6hdr = (struct icmp6_hdr *)p->payload;
|
||||
|
||||
#if LWIP_ICMP6_CHECKSUM_CHECK
|
||||
if (ip6_chksum_pseudo(p, ip6_current_src_addr(), ip6_current_dest_addr(),
|
||||
IP6_NEXTH_ICMP6, p->tot_len) != 0) {
|
||||
if (ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->tot_len, ip6_current_src_addr(),
|
||||
ip6_current_dest_addr()) != 0) {
|
||||
/* Checksum failed */
|
||||
pbuf_free(p);
|
||||
ICMP6_STATS_INC(icmp6.chkerr);
|
||||
@@ -171,8 +171,7 @@ icmp6_input(struct pbuf *p, struct netif *inp)
|
||||
((struct icmp6_echo_hdr *)(r->payload))->type = ICMP6_TYPE_EREP;
|
||||
((struct icmp6_echo_hdr *)(r->payload))->chksum = 0;
|
||||
((struct icmp6_echo_hdr *)(r->payload))->chksum = ip6_chksum_pseudo(r,
|
||||
reply_src, ip6_current_src_addr(),
|
||||
IP6_NEXTH_ICMP6, r->tot_len);
|
||||
IP6_NEXTH_ICMP6, r->tot_len, reply_src, ip6_current_src_addr());
|
||||
|
||||
/* Send reply. */
|
||||
ICMP6_STATS_INC(icmp6.xmit);
|
||||
@@ -201,7 +200,7 @@ icmp6_input(struct pbuf *p, struct netif *inp)
|
||||
void
|
||||
icmp6_dest_unreach(struct pbuf *p, enum icmp6_dur_code c)
|
||||
{
|
||||
icmp6_send_response(p, ICMP6_TYPE_DUR, c, 0);
|
||||
icmp6_send_response(p, c, 0, ICMP6_TYPE_DUR);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +213,7 @@ icmp6_dest_unreach(struct pbuf *p, enum icmp6_dur_code c)
|
||||
void
|
||||
icmp6_packet_too_big(struct pbuf *p, u32_t mtu)
|
||||
{
|
||||
icmp6_send_response(p, ICMP6_TYPE_PTB, 0, mtu);
|
||||
icmp6_send_response(p, 0, mtu, ICMP6_TYPE_PTB);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,7 +226,7 @@ icmp6_packet_too_big(struct pbuf *p, u32_t mtu)
|
||||
void
|
||||
icmp6_time_exceeded(struct pbuf *p, enum icmp6_te_code c)
|
||||
{
|
||||
icmp6_send_response(p, ICMP6_TYPE_TE, c, 0);
|
||||
icmp6_send_response(p, c, 0, ICMP6_TYPE_TE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,7 +240,7 @@ icmp6_time_exceeded(struct pbuf *p, enum icmp6_te_code c)
|
||||
void
|
||||
icmp6_param_problem(struct pbuf *p, enum icmp6_pp_code c, u32_t pointer)
|
||||
{
|
||||
icmp6_send_response(p, ICMP6_TYPE_PP, c, pointer);
|
||||
icmp6_send_response(p, c, pointer, ICMP6_TYPE_PP);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,12 +248,12 @@ icmp6_param_problem(struct pbuf *p, enum icmp6_pp_code c, u32_t pointer)
|
||||
*
|
||||
* @param p the input packet for which the response should be sent,
|
||||
* p->payload pointing to the IPv6 header
|
||||
* @param type Type of the ICMPv6 header
|
||||
* @param code Code of the ICMPv6 header
|
||||
* @param data Additional 32-bit parameter in the ICMPv6 header
|
||||
* @param type Type of the ICMPv6 header
|
||||
*/
|
||||
static void
|
||||
icmp6_send_response(struct pbuf *p, u8_t type, u8_t code, u32_t data)
|
||||
icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type)
|
||||
{
|
||||
struct pbuf *q;
|
||||
struct icmp6_hdr *icmp6hdr;
|
||||
@@ -281,7 +280,7 @@ icmp6_send_response(struct pbuf *p, u8_t type, u8_t code, u32_t data)
|
||||
IP6_HLEN + LWIP_ICMP6_DATASIZE);
|
||||
|
||||
/* Select an address to use as source. */
|
||||
reply_src = ip6_select_source_address(current_netif, ip6_current_src_addr());
|
||||
reply_src = ip6_select_source_address(ip_current_netif(), ip6_current_src_addr());
|
||||
if (reply_src == NULL) {
|
||||
/* drop */
|
||||
pbuf_free(q);
|
||||
@@ -291,13 +290,12 @@ icmp6_send_response(struct pbuf *p, u8_t type, u8_t code, u32_t data)
|
||||
|
||||
/* calculate checksum */
|
||||
icmp6hdr->chksum = 0;
|
||||
icmp6hdr->chksum = ip6_chksum_pseudo(q, reply_src, ip6_current_src_addr(),
|
||||
IP6_NEXTH_ICMP6, q->tot_len);
|
||||
icmp6hdr->chksum = ip6_chksum_pseudo(q, IP6_NEXTH_ICMP6, q->tot_len,
|
||||
reply_src, ip6_current_src_addr());
|
||||
|
||||
ICMP6_STATS_INC(icmp6.xmit);
|
||||
ip6_output(q, reply_src, ip6_current_src_addr(), LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6);
|
||||
pbuf_free(q);
|
||||
}
|
||||
|
||||
|
||||
#endif /* LWIP_ICMP6 && LWIP_IPV6 */
|
||||
|
||||
@@ -61,17 +61,6 @@
|
||||
#include "lwip/stats.h"
|
||||
|
||||
|
||||
/** Header of the input IPv6 packet currently being processed. */
|
||||
const struct ip6_hdr *current_ip6_header;
|
||||
/** Total header length of current_ip6_header (i.e. after this, the UDP/TCP header starts) */
|
||||
u16_t current_ip6_header_tot_len;
|
||||
/** Source IPv6 address of current_header */
|
||||
ip6_addr_t current_ip6hdr_src;
|
||||
/** Destination IPv6 address of current_header */
|
||||
ip6_addr_t current_ip6hdr_dest;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Finds the appropriate network interface for a given IPv6 address. It tries to select
|
||||
* a netif following a sequence of heuristics:
|
||||
@@ -380,11 +369,11 @@ ip6_input(struct pbuf *p, struct netif *inp)
|
||||
pbuf_realloc(p, IP6_HLEN + IP6H_PLEN(ip6hdr));
|
||||
|
||||
/* copy IP addresses to aligned ip6_addr_t */
|
||||
ip6_addr_copy(current_ip6hdr_dest, ip6hdr->dest);
|
||||
ip6_addr_copy(current_ip6hdr_src, ip6hdr->src);
|
||||
ip6_addr_copy(ip_data.current_iphdr_dest.ip6, ip6hdr->dest);
|
||||
ip6_addr_copy(ip_data.current_iphdr_src.ip6, ip6hdr->src);
|
||||
|
||||
/* current header pointer. */
|
||||
current_ip6_header = ip6hdr;
|
||||
ip_data.current_ip6_header = ip6hdr;
|
||||
|
||||
/* match packet against an interface, i.e. is this packet for us? */
|
||||
if (ip6_addr_ismulticast(ip6_current_dest_addr())) {
|
||||
@@ -468,13 +457,13 @@ netif_found:
|
||||
}
|
||||
|
||||
/* current netif pointer. */
|
||||
current_netif = inp;
|
||||
ip_data.current_netif = inp;
|
||||
|
||||
/* Save next header type. */
|
||||
nexth = IP6H_NEXTH(ip6hdr);
|
||||
|
||||
/* Init header length. */
|
||||
hlen = current_ip6_header_tot_len = IP6_HLEN;
|
||||
hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
|
||||
|
||||
/* Move to payload. */
|
||||
pbuf_header(p, -IP6_HLEN);
|
||||
@@ -490,7 +479,7 @@ netif_found:
|
||||
|
||||
/* Get the header length. */
|
||||
hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
|
||||
current_ip6_header_tot_len += hlen;
|
||||
ip_data.current_ip_header_tot_len += hlen;
|
||||
|
||||
/* Skip over this header. */
|
||||
if (hlen > p->len) {
|
||||
@@ -513,7 +502,7 @@ netif_found:
|
||||
|
||||
/* Get the header length. */
|
||||
hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
|
||||
current_ip6_header_tot_len += hlen;
|
||||
ip_data.current_ip_header_tot_len += hlen;
|
||||
|
||||
/* Skip over this header. */
|
||||
if (hlen > p->len) {
|
||||
@@ -536,7 +525,7 @@ netif_found:
|
||||
|
||||
/* Get the header length. */
|
||||
hlen = 8 * (1 + *((u8_t *)p->payload) + 1);
|
||||
current_ip6_header_tot_len += hlen;
|
||||
ip_data.current_ip_header_tot_len += hlen;
|
||||
|
||||
/* Skip over this header. */
|
||||
if (hlen > p->len) {
|
||||
@@ -565,7 +554,7 @@ netif_found:
|
||||
|
||||
/* Fragment Header length. */
|
||||
hlen = 8;
|
||||
current_ip6_header_tot_len += hlen;
|
||||
ip_data.current_ip_header_tot_len += hlen;
|
||||
|
||||
/* Make sure this header fits in current pbuf. */
|
||||
if (hlen > p->len) {
|
||||
@@ -601,7 +590,7 @@ netif_found:
|
||||
* Update all our variables and pointers and continue. */
|
||||
ip6hdr = (struct ip6_hdr *)p->payload;
|
||||
nexth = IP6H_NEXTH(ip6hdr);
|
||||
hlen = current_ip6_header_tot_len = IP6_HLEN;
|
||||
hlen = ip_data.current_ip_header_tot_len = IP6_HLEN;
|
||||
pbuf_header(p, -IP6_HLEN);
|
||||
|
||||
#else /* LWIP_IPV6_REASS */
|
||||
@@ -623,7 +612,7 @@ netif_found:
|
||||
options_done:
|
||||
|
||||
/* p points to IPv6 header again. */
|
||||
pbuf_header(p, current_ip6_header_tot_len);
|
||||
pbuf_header(p, ip_data.current_ip_header_tot_len);
|
||||
|
||||
/* send to upper layers */
|
||||
LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n"));
|
||||
@@ -645,21 +634,21 @@ options_done:
|
||||
case IP6_NEXTH_UDPLITE:
|
||||
#endif /* LWIP_UDPLITE */
|
||||
/* Point to payload. */
|
||||
pbuf_header(p, -current_ip6_header_tot_len);
|
||||
pbuf_header(p, -ip_data.current_ip_header_tot_len);
|
||||
udp_input(p, inp);
|
||||
break;
|
||||
#endif /* LWIP_UDP */
|
||||
#if LWIP_TCP
|
||||
case IP6_NEXTH_TCP:
|
||||
/* Point to payload. */
|
||||
pbuf_header(p, -current_ip6_header_tot_len);
|
||||
pbuf_header(p, -ip_data.current_ip_header_tot_len);
|
||||
tcp_input(p, inp);
|
||||
break;
|
||||
#endif /* LWIP_TCP */
|
||||
#if LWIP_ICMP6
|
||||
case IP6_NEXTH_ICMP6:
|
||||
/* Point to payload. */
|
||||
pbuf_header(p, -current_ip6_header_tot_len);
|
||||
pbuf_header(p, -ip_data.current_ip_header_tot_len);
|
||||
icmp6_input(p, inp);
|
||||
break;
|
||||
#endif /* LWIP_ICMP */
|
||||
@@ -668,7 +657,7 @@ options_done:
|
||||
/* send ICMP parameter problem unless it was a multicast or ICMPv6 */
|
||||
if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) &&
|
||||
(IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) {
|
||||
icmp6_param_problem(p, ICMP6_PP_HEADER, current_ip6_header_tot_len - hlen);
|
||||
icmp6_param_problem(p, ICMP6_PP_HEADER, ip_data.current_ip_header_tot_len - hlen);
|
||||
}
|
||||
#endif /* LWIP_ICMP */
|
||||
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", IP6H_NEXTH(ip6hdr)));
|
||||
@@ -680,11 +669,11 @@ options_done:
|
||||
}
|
||||
|
||||
ip6_input_cleanup:
|
||||
current_netif = NULL;
|
||||
current_ip6_header = NULL;
|
||||
current_ip6_header_tot_len = 0;
|
||||
ip6_addr_set_any(¤t_ip6hdr_src);
|
||||
ip6_addr_set_any(¤t_ip6hdr_dest);
|
||||
ip_data.current_netif = NULL;
|
||||
ip_data.current_ip6_header = NULL;
|
||||
ip_data.current_ip_header_tot_len = 0;
|
||||
ip6_addr_set_any(&ip_data.current_iphdr_src.ip6);
|
||||
ip6_addr_set_any(&ip_data.current_iphdr_dest.ip6);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -727,7 +716,7 @@ ip6_output_if(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
|
||||
LWIP_ASSERT("p->ref == 1", p->ref == 1);
|
||||
|
||||
/* Should the IPv6 header be generated or is it already included in p? */
|
||||
if (dest != IP6_HDRINCL) {
|
||||
if (dest != IP_HDRINCL) {
|
||||
/* generate IPv6 header */
|
||||
if (pbuf_header(p, IP6_HLEN)) {
|
||||
LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_output: not enough room for IPv6 header in pbuf\n"));
|
||||
@@ -883,9 +872,9 @@ ip6_output_hinted(struct pbuf *p, ip6_addr_t *src, ip6_addr_t *dest,
|
||||
return ERR_RTE;
|
||||
}
|
||||
|
||||
netif->addr_hint = addr_hint;
|
||||
NETIF_SET_HWADDRHINT(netif, addr_hint);
|
||||
err = ip6_output_if(p, src, dest, hl, tc, nexth, netif);
|
||||
netif->addr_hint = NULL;
|
||||
NETIF_SET_HWADDRHINT(netif, NULL);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* IPv6 Checksum helper functions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2010 Inico Technologies Ltd.
|
||||
* 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: Ivan Delamer <delamer@inicotech.com>
|
||||
*
|
||||
*
|
||||
* Please coordinate changes and requests with Ivan Delamer
|
||||
* <delamer@inicotech.com>
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
|
||||
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip6_chksum.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/def.h"
|
||||
|
||||
#ifndef LWIP_CHKSUM
|
||||
# define LWIP_CHKSUM lwip_standard_chksum
|
||||
extern u16_t lwip_standard_chksum(void *dataptr, u16_t len);
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the checksum with IPv6 pseudo header used by TCP and UDP for a pbuf chain.
|
||||
* IPv6 addresses are expected to be in network byte order.
|
||||
*
|
||||
* @param p chain of pbufs over that a checksum should be calculated (ip data part)
|
||||
* @param src source ipv6 address (used for checksum of pseudo header)
|
||||
* @param dst destination ipv6 address (used for checksum of pseudo header)
|
||||
* @param proto ipv6 protocol/next header (used for checksum of pseudo header)
|
||||
* @param proto_len length of the ipv6 payload (used for checksum of pseudo header)
|
||||
* @return checksum (as u16_t) to be saved directly in the protocol header
|
||||
*/
|
||||
u16_t
|
||||
ip6_chksum_pseudo(struct pbuf *p,
|
||||
ip6_addr_t *src, ip6_addr_t *dest,
|
||||
u8_t proto, u16_t proto_len)
|
||||
{
|
||||
u32_t acc;
|
||||
u32_t addr;
|
||||
struct pbuf *q;
|
||||
u8_t swapped;
|
||||
|
||||
acc = 0;
|
||||
swapped = 0;
|
||||
/* iterate through all pbuf in chain */
|
||||
for(q = p; q != NULL; q = q->next) {
|
||||
acc += LWIP_CHKSUM(q->payload, q->len);
|
||||
/* fold the upper bit down */
|
||||
acc = FOLD_U32T(acc);
|
||||
if (q->len % 2 != 0) {
|
||||
swapped = 1 - swapped;
|
||||
acc = SWAP_BYTES_IN_WORD(acc);
|
||||
}
|
||||
}
|
||||
if (swapped) {
|
||||
acc = SWAP_BYTES_IN_WORD(acc);
|
||||
}
|
||||
|
||||
for (swapped = 0; swapped < 4; swapped++) {
|
||||
addr = src->addr[swapped];
|
||||
acc += (addr & 0xffffUL);
|
||||
acc += ((addr >> 16) & 0xffffUL);
|
||||
addr = dest->addr[swapped];
|
||||
acc += (addr & 0xffffUL);
|
||||
acc += ((addr >> 16) & 0xffffUL);
|
||||
}
|
||||
acc += (u32_t)htons((u16_t)proto);
|
||||
acc += (u32_t)htons(proto_len);
|
||||
|
||||
/* Fold 32-bit sum to 16 bits
|
||||
calling this twice is propably faster than if statements... */
|
||||
acc = FOLD_U32T(acc);
|
||||
acc = FOLD_U32T(acc);
|
||||
LWIP_DEBUGF(INET_DEBUG, ("ip6_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
|
||||
return (u16_t)~(acc & 0xffffUL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the checksum with IPv6 pseudo header used by TCP and UDP for a pbuf chain.
|
||||
* IPv6 addresses are expected to be in network byte order. Will only compute for a
|
||||
* portion of the payload.
|
||||
*
|
||||
* @param p chain of pbufs over that a checksum should be calculated (ip data part)
|
||||
* @param src source ipv6 address (used for checksum of pseudo header)
|
||||
* @param dst destination ipv6 address (used for checksum of pseudo header)
|
||||
* @param proto ipv6 protocol/next header (used for checksum of pseudo header)
|
||||
* @param proto_len length of the ipv6 payload (used for checksum of pseudo header)
|
||||
* @param chksum_len number of payload bytes used to compute chksum
|
||||
* @return checksum (as u16_t) to be saved directly in the protocol header
|
||||
*/
|
||||
u16_t
|
||||
ip6_chksum_pseudo_partial(struct pbuf *p,
|
||||
ip6_addr_t *src, ip6_addr_t *dest,
|
||||
u8_t proto, u16_t proto_len, u16_t chksum_len)
|
||||
{
|
||||
u32_t acc;
|
||||
u32_t addr;
|
||||
struct pbuf *q;
|
||||
u8_t swapped;
|
||||
u16_t chklen;
|
||||
|
||||
acc = 0;
|
||||
swapped = 0;
|
||||
/* iterate through all pbuf in chain */
|
||||
for(q = p; (q != NULL) && (chksum_len > 0); q = q->next) {
|
||||
chklen = q->len;
|
||||
if (chklen > chksum_len) {
|
||||
chklen = chksum_len;
|
||||
}
|
||||
acc += LWIP_CHKSUM(q->payload, chklen);
|
||||
chksum_len -= chklen;
|
||||
acc = FOLD_U32T(acc);
|
||||
if (q->len % 2 != 0) {
|
||||
swapped = 1 - swapped;
|
||||
acc = SWAP_BYTES_IN_WORD(acc);
|
||||
}
|
||||
}
|
||||
if (swapped) {
|
||||
acc = SWAP_BYTES_IN_WORD(acc);
|
||||
}
|
||||
|
||||
for (swapped = 0; swapped < 4; swapped++) {
|
||||
addr = src->addr[swapped];
|
||||
acc += (addr & 0xffffUL);
|
||||
acc += ((addr >> 16) & 0xffffUL);
|
||||
addr = dest->addr[swapped];
|
||||
acc += (addr & 0xffffUL);
|
||||
acc += ((addr >> 16) & 0xffffUL);
|
||||
}
|
||||
acc += (u32_t)htons((u16_t)proto);
|
||||
acc += (u32_t)htons(proto_len);
|
||||
|
||||
/* Fold 32-bit sum to 16 bits
|
||||
calling this twice is propably faster than if statements... */
|
||||
acc = FOLD_U32T(acc);
|
||||
acc = FOLD_U32T(acc);
|
||||
LWIP_DEBUGF(INET_DEBUG, ("ip6_chksum_pseudo_partial(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
|
||||
return (u16_t)~(acc & 0xffffUL);
|
||||
}
|
||||
|
||||
#endif /* LWIP_IPV6 */
|
||||
@@ -50,7 +50,7 @@
|
||||
#include "lwip/icmp6.h"
|
||||
#include "lwip/ip6.h"
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip6_chksum.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/memp.h"
|
||||
@@ -560,8 +560,8 @@ mld6_send(struct mld_group *group, u8_t type)
|
||||
mld_hdr->reserved = 0;
|
||||
ip6_addr_set(&(mld_hdr->multicast_address), &(group->group_address));
|
||||
|
||||
mld_hdr->chksum = ip6_chksum_pseudo(p, src_addr, &(group->group_address),
|
||||
IP6_NEXTH_ICMP6, p->len);
|
||||
mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len,
|
||||
src_addr, &(group->group_address));
|
||||
|
||||
/* Add hop-by-hop headers options: router alert with MLD value. */
|
||||
ip6_options_add_hbh_ra(p, IP6_NEXTH_ICMP6, IP6_ROUTER_ALERT_VALUE_MLD);
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
#include "lwip/memp.h"
|
||||
#include "lwip/ip6.h"
|
||||
#include "lwip/ip6_addr.h"
|
||||
#include "lwip/ip6_chksum.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/icmp6.h"
|
||||
#include "lwip/mld6.h"
|
||||
@@ -827,8 +827,8 @@ nd6_send_ns(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
|
||||
target_addr = &multicast_address;
|
||||
}
|
||||
|
||||
ns_hdr->chksum = ip6_chksum_pseudo(p, src_addr, target_addr,
|
||||
IP6_NEXTH_ICMP6, p->len);
|
||||
ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
|
||||
target_addr);
|
||||
|
||||
/* Send the packet out. */
|
||||
ND6_STATS_INC(nd6.xmit);
|
||||
@@ -897,8 +897,8 @@ nd6_send_na(struct netif * netif, ip6_addr_t * target_addr, u8_t flags)
|
||||
dest_addr = ip6_current_src_addr();
|
||||
}
|
||||
|
||||
na_hdr->chksum = ip6_chksum_pseudo(p, src_addr, dest_addr,
|
||||
IP6_NEXTH_ICMP6, p->len);
|
||||
na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
|
||||
dest_addr);
|
||||
|
||||
/* Send the packet out. */
|
||||
ND6_STATS_INC(nd6.xmit);
|
||||
@@ -964,8 +964,8 @@ nd6_send_rs(struct netif * netif)
|
||||
SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len);
|
||||
}
|
||||
|
||||
rs_hdr->chksum = ip6_chksum_pseudo(p, src_addr, &multicast_address,
|
||||
IP6_NEXTH_ICMP6, p->len);
|
||||
rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr,
|
||||
&multicast_address);
|
||||
|
||||
/* Send the packet out. */
|
||||
ND6_STATS_INC(nd6.xmit);
|
||||
@@ -1407,12 +1407,11 @@ nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
|
||||
s8_t i;
|
||||
|
||||
#if LWIP_NETIF_HWADDRHINT
|
||||
/* TODO should addr_hint point to nd6_cached_neighbor_index instead? */
|
||||
if (netif->addr_hint != NULL) {
|
||||
/* per-pcb cached entry was given */
|
||||
i = *(netif->addr_hint);
|
||||
if ((i >= 0) && (i < LWIP_ND6_NUM_DESTINATIONS)) {
|
||||
nd6_cached_destination_index = i;
|
||||
u8_t addr_hint = *(netif->addr_hint);
|
||||
if (addr_hint < LWIP_ND6_NUM_DESTINATIONS) {
|
||||
nd6_cached_destination_index = addr_hint;
|
||||
}
|
||||
}
|
||||
#endif /* LWIP_NETIF_HWADDRHINT */
|
||||
@@ -1425,7 +1424,7 @@ nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
|
||||
} else {
|
||||
/* Search destination cache. */
|
||||
i = nd6_find_destination_cache_entry(ip6addr);
|
||||
if (i>= 0) {
|
||||
if (i >= 0) {
|
||||
/* found destination entry. make it our new cached index. */
|
||||
nd6_cached_destination_index = i;
|
||||
}
|
||||
@@ -1465,7 +1464,6 @@ nd6_get_next_hop_entry(ip6_addr_t * ip6addr, struct netif * netif)
|
||||
}
|
||||
|
||||
#if LWIP_NETIF_HWADDRHINT
|
||||
/* TODO should addr_hint point to nd6_cached_neighbor_index instead? */
|
||||
if (netif->addr_hint != NULL) {
|
||||
/* per-pcb cached entry was given */
|
||||
*(netif->addr_hint) = nd6_cached_destination_index;
|
||||
|
||||
Reference in New Issue
Block a user