2015-01-20 21:37:02 +01:00
|
|
|
/****************************************************************************
|
2017-03-31 19:15:16 +02:00
|
|
|
* net/neighbor/neighbor_ethernet_out.c
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* 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.
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-01-20 21:37:02 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
#include <nuttx/net/ethernet.h>
|
2018-06-23 20:53:27 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2015-01-20 21:37:02 +01:00
|
|
|
#include <nuttx/net/netdev.h>
|
2019-11-08 18:15:16 +01:00
|
|
|
#include <nuttx/net/neighbor.h>
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2022-12-03 16:03:29 +01:00
|
|
|
#include "devif/devif.h"
|
2015-01-20 21:37:02 +01:00
|
|
|
#include "route/route.h"
|
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
#include "neighbor/neighbor.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
/* Support for MLD multicast addresses.
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
|
|
|
* Well-known ethernet multicast address:
|
|
|
|
*
|
|
|
|
* ADDRESS TYPE USAGE
|
2018-11-09 21:21:49 +01:00
|
|
|
* 01-00-0c-cc-cc-cc 0x0802 CDP (Cisco Discovery Protocol), VTP (Virtual
|
|
|
|
* Trunking Protocol)
|
2015-01-20 21:37:02 +01:00
|
|
|
* 01-00-0c-cc-cc-cd 0x0802 Cisco Shared Spanning Tree Protocol Address
|
|
|
|
* 01-80-c2-00-00-00 0x0802 Spanning Tree Protocol (for bridges) IEEE 802.1D
|
|
|
|
* 01-80-c2-00-00-02 0x0809 Ethernet OAM Protocol IEEE 802.3ah
|
|
|
|
* 01-00-5e-xx-xx-xx 0x0800 IPv4 IGMP Multicast Address
|
|
|
|
* 33-33-00-00-00-00 0x86DD IPv6 Neighbor Discovery
|
|
|
|
* 33-33-xx-xx-xx-xx 0x86DD IPv6 Multicast Address (RFC3307)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-09 21:21:49 +01:00
|
|
|
* Name: neighbor_ethernet_out
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function should be called before sending out an IPv6 packet. The
|
|
|
|
* function checks the destination IPv6 address of the IPv6 packet to see
|
|
|
|
* what Ethernet MAC address that should be used as a destination MAC
|
|
|
|
* address on the Ethernet.
|
|
|
|
*
|
|
|
|
* If the destination IPv6 address is in the local network (determined
|
|
|
|
* by logical ANDing of netmask and our IPv6 address), the function
|
|
|
|
* checks the Neighbor Table to see if an entry for the destination IPv6
|
|
|
|
* address is found. If so, an Ethernet header is pre-pended at the
|
|
|
|
* beginning of the packet and the function returns.
|
|
|
|
*
|
|
|
|
* If no Neighbor Table entry is found for the destination IPv6 address,
|
2016-11-29 23:12:39 +01:00
|
|
|
* the packet in the d_buf is replaced by an ICMPv6 Neighbor Solicit
|
2017-06-28 21:32:19 +02:00
|
|
|
* request packet for the IPv6 address. The IPv6 packet is dropped and
|
2015-01-20 21:37:02 +01:00
|
|
|
* it is assumed that the higher level protocols (e.g., TCP) eventually
|
|
|
|
* will retransmit the dropped packet.
|
|
|
|
*
|
|
|
|
* Upon return in either the case, a packet to be sent is present in the
|
2016-11-29 23:12:39 +01:00
|
|
|
* d_buf buffer and the d_len field holds the length of the Ethernet
|
2015-01-20 21:37:02 +01:00
|
|
|
* frame that should be transmitted.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-09 21:21:49 +01:00
|
|
|
void neighbor_ethernet_out(FAR struct net_driver_s *dev)
|
2015-01-20 21:37:02 +01:00
|
|
|
{
|
|
|
|
FAR struct eth_hdr_s *eth = ETHBUF;
|
|
|
|
FAR struct ipv6_hdr_s *ip = IPv6BUF;
|
2018-11-09 21:14:15 +01:00
|
|
|
struct neighbor_addr_s laddr;
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2015-01-21 17:29:18 +01:00
|
|
|
/* Skip sending Neighbor Solicitations when the frame to be transmitted was
|
2018-11-09 22:41:23 +01:00
|
|
|
* written into a packet socket.
|
2015-01-20 21:37:02 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-20 22:59:52 +01:00
|
|
|
if (IFF_IS_NOARP(dev->d_flags))
|
2015-01-20 21:37:02 +01:00
|
|
|
{
|
|
|
|
/* Clear the indication and let the packet continue on its way. */
|
|
|
|
|
2015-01-21 17:29:18 +01:00
|
|
|
IFF_CLR_NOARP(dev->d_flags);
|
2015-01-20 21:37:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-21 17:29:18 +01:00
|
|
|
/* Find the destination IPv6 address in the Neighbor Table and construct
|
|
|
|
* the Ethernet header. If the destination IPv6 address isn't on the local
|
|
|
|
* network, we use the default router's IPv6 address instead.
|
2015-01-20 21:37:02 +01:00
|
|
|
*
|
2015-01-21 17:29:18 +01:00
|
|
|
* If no Neighbor Table entry is found, we overwrite the original IPv6
|
|
|
|
* packet with an Neighbor Solicitation Request for the IPv6 address.
|
2015-01-20 21:37:02 +01:00
|
|
|
*/
|
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
/* First check if destination isn't IPv6 multicast address. */
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
if (!net_is_addr_mcast(ip->destipaddr))
|
2015-01-20 21:37:02 +01:00
|
|
|
{
|
2018-11-09 22:41:23 +01:00
|
|
|
net_ipv6addr_t ipaddr;
|
2015-01-20 21:37:02 +01:00
|
|
|
|
|
|
|
/* Check if the destination address is on the local network. */
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (!NETDEV_V6ADDR_ONLINK(dev, ip->destipaddr))
|
2015-01-20 21:37:02 +01:00
|
|
|
{
|
|
|
|
/* Destination address is not on the local network */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
/* We have a routing table.. find the correct router to use in
|
|
|
|
* this case (or, as a fall-back, use the device's default router
|
|
|
|
* address). We will use the router IPv6 address instead of the
|
|
|
|
* destination address when determining the MAC address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
netdev_ipv6_router(dev, ip->destipaddr, ipaddr);
|
|
|
|
#else
|
|
|
|
/* Use the device's default router IPv6 address instead of the
|
|
|
|
* destination address when determining the MAC address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_ipv6addr_copy(ipaddr, dev->d_ipv6draddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Else, we use the destination IPv6 address. */
|
|
|
|
|
|
|
|
net_ipv6addr_copy(ipaddr, ip->destipaddr);
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:08:32 +01:00
|
|
|
/* Check if we already have this destination address in the
|
|
|
|
* Neighbor Table.
|
|
|
|
*/
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
if (neighbor_lookup(ipaddr, &laddr) < 0)
|
2015-01-20 21:37:02 +01:00
|
|
|
{
|
2020-11-24 16:08:32 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("IPv6 Neighbor solicitation for IPv6\n");
|
2015-01-20 21:37:02 +01:00
|
|
|
|
|
|
|
/* The destination address was not in our Neighbor Table, so we
|
2020-11-24 16:08:32 +01:00
|
|
|
* overwrite the IPv6 packet with an ICMPv6 Neighbor Solicitation
|
2015-01-20 21:37:02 +01:00
|
|
|
* message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
icmpv6_solicit(dev, ipaddr);
|
2020-11-24 16:08:32 +01:00
|
|
|
#else
|
|
|
|
/* What to do here? We need the laddr, but no way to get it. */
|
|
|
|
|
|
|
|
nerr("ERROR: IPv6 needs link layer address for ethernet.\n");
|
|
|
|
DEBUGPANIC();
|
|
|
|
return;
|
|
|
|
#endif
|
2015-01-20 21:37:02 +01:00
|
|
|
}
|
2018-11-09 22:41:23 +01:00
|
|
|
}
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
/* Build an Ethernet header. */
|
2015-01-20 21:37:02 +01:00
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
if (net_is_addr_mcast(ip->destipaddr))
|
|
|
|
{
|
|
|
|
eth->dest[0] = eth->dest[1] = 0x33;
|
|
|
|
memcpy(ð->dest[2], &ip->destipaddr[6], 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-24 16:08:32 +01:00
|
|
|
memcpy(eth->dest, laddr.u.na_ethernet.ether_addr_octet,
|
|
|
|
ETHER_ADDR_LEN);
|
2015-01-20 21:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish populating the Ethernet header */
|
|
|
|
|
2017-04-22 19:10:30 +02:00
|
|
|
memcpy(eth->src, dev->d_mac.ether.ether_addr_octet, ETHER_ADDR_LEN);
|
2015-01-20 21:37:02 +01:00
|
|
|
eth->type = HTONS(ETHTYPE_IP6);
|
2015-01-21 17:29:18 +01:00
|
|
|
|
|
|
|
/* Add the size of the layer layer header to the total size of the
|
|
|
|
* outgoing packet.
|
|
|
|
*/
|
|
|
|
|
2015-05-08 22:19:18 +02:00
|
|
|
dev->d_len += netdev_ipv6_hdrlen(dev);
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Outgoing IPv6 Packet length: %d (%d)\n",
|
2015-01-21 17:29:18 +01:00
|
|
|
dev->d_len, (ip->len[0] << 8) | ip->len[1]);
|
2015-01-20 21:37:02 +01:00
|
|
|
}
|