incubator-nuttx/net/icmp/icmp_pmtu.c

121 lines
3.6 KiB
C
Raw Normal View History

nuttx/net:Support to PMTUD RFC 1191 When a router is unable to forward a datagram because it exceeds the MTU of the next-hop network and its Don't Fragment bit is set, the router is required to return an ICMP Destination Unreachable message to the source of the datagram, with the Code indicating "fragmentation needed and DF set". To support the Path MTU Discovery technique specified in this memo, the router MUST include the MTU of that next-hop network in the low-order 16 bits of the ICMP header field that is labelled "unused" in the ICMP specification [7]. The high-order 16 bits remain unused, and MUST be set to zero. Thus, the message has the following format: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type = 3 | Code = 4 | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unused = 0 | Next-Hop MTU | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Internet Header + 64 bits of Original Datagram Data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ RFC 1185 Packet Too Big Message 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | MTU | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | As much of invoking packet | + as will fit without the ICMPv6 packet + | exceeding 576 octets | IPv6 Fields: Destination Address Copied from the Source Address field of the invoking packet. ICMPv6 Fields: Type 2 Code 0 MTU The Maximum Transmission Unit of the next-hop link. Description A Packet Too Big MUST be sent by a router in response to a packet that it cannot forward because the packet is larger than the MTU of the outgoing link. The information in this message is used as part of the Path MTU Discovery process [RFC-1191]. Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-05-11 19:32:30 +08:00
/****************************************************************************
* net/icmp/icmp_pmtu.c
*
* SPDX-License-Identifier: Apache-2.0
*
nuttx/net:Support to PMTUD RFC 1191 When a router is unable to forward a datagram because it exceeds the MTU of the next-hop network and its Don't Fragment bit is set, the router is required to return an ICMP Destination Unreachable message to the source of the datagram, with the Code indicating "fragmentation needed and DF set". To support the Path MTU Discovery technique specified in this memo, the router MUST include the MTU of that next-hop network in the low-order 16 bits of the ICMP header field that is labelled "unused" in the ICMP specification [7]. The high-order 16 bits remain unused, and MUST be set to zero. Thus, the message has the following format: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type = 3 | Code = 4 | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unused = 0 | Next-Hop MTU | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Internet Header + 64 bits of Original Datagram Data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ RFC 1185 Packet Too Big Message 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | MTU | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | As much of invoking packet | + as will fit without the ICMPv6 packet + | exceeding 576 octets | IPv6 Fields: Destination Address Copied from the Source Address field of the invoking packet. ICMPv6 Fields: Type 2 Code 0 MTU The Maximum Transmission Unit of the next-hop link. Description A Packet Too Big MUST be sent by a router in response to a packet that it cannot forward because the packet is larger than the MTU of the outgoing link. The information in this message is used as part of the Path MTU Discovery process [RFC-1191]. Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-05-11 19:32:30 +08:00
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/time.h>
#include <nuttx/clock.h>
#include "icmp/icmp.h"
#include "utils/utils.h"
/****************************************************************************
* Private Data
****************************************************************************/
static struct icmp_pmtu_entry
g_icmp_pmtu_entry[CONFIG_NET_ICMP_PMTU_ENTRIES];
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: icmpv4_find_pmtu_entry
*
* Description:
* Search for a ipv4 destination cache entry
*
* Parameters:
* destipaddr the IPv4 address of the destination
*
* Return:
* not null is success; null is failure
****************************************************************************/
FAR struct icmp_pmtu_entry *icmpv4_find_pmtu_entry(in_addr_t destipaddr)
{
clock_t now = clock_systime_ticks();
int i;
for (i = 0; i < CONFIG_NET_ICMP_PMTU_ENTRIES; i++)
{
if (g_icmp_pmtu_entry[i].pmtu == 0)
{
continue;
}
if (net_ipv4addr_cmp(destipaddr, g_icmp_pmtu_entry[i].daddr))
{
g_icmp_pmtu_entry[i].time = now;
return &g_icmp_pmtu_entry[i];
}
}
return NULL;
}
/****************************************************************************
* Name: icmpv4_add_pmtu_entry
*
* Description:
* Create a new ipv4 destination cache entry. If no unused entry is found,
* will recycle oldest entry
*
* Parameters:
* destipaddr the IPv4 address of the destination
* mtu MTU
*
* Return:
* void
****************************************************************************/
void icmpv4_add_pmtu_entry(in_addr_t destipaddr, int mtu)
{
clock_t now = clock_systime_ticks();
int j = 0;
int i;
for (i = 0; i < CONFIG_NET_ICMP_PMTU_ENTRIES; i++)
{
if ((g_icmp_pmtu_entry[i].pmtu == 0) ||
(sclock_t)(now - g_icmp_pmtu_entry[i].time) >=
SEC2TICK(CONFIG_NET_ICMP_PMTU_TIMEOUT * 60))
{
j = i;
break;
}
if ((sclock_t)(g_icmp_pmtu_entry[i].time -
g_icmp_pmtu_entry[j].time) < 0)
{
j = i;
}
}
net_ipv4addr_copy(g_icmp_pmtu_entry[j].daddr, destipaddr);
g_icmp_pmtu_entry[j].pmtu = mtu;
g_icmp_pmtu_entry[j].time = now;
}