2015-02-05 16:43:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/icmpv6/icmpv6_radvertise.c
|
|
|
|
* Send an ICMPv6 Router Advertisement
|
|
|
|
*
|
2021-09-18 00:12:48 +02: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-02-05 16:43:07 +01:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-05 16:43:07 +01:00
|
|
|
*
|
2021-09-18 00:12:48 +02: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-02-05 16:43:07 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/netconfig.h>
|
|
|
|
#include <nuttx/net/netstats.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/icmpv6.h>
|
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
#include "netdev/netdev.h"
|
2017-08-07 19:50:50 +02:00
|
|
|
#include "inet/inet.h"
|
2015-02-05 16:43:07 +01:00
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6_ROUTER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-17 23:09:14 +01:00
|
|
|
#define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
#define ICMPv6ADVERTISE \
|
2022-01-17 23:09:14 +01:00
|
|
|
((FAR struct icmpv6_router_advertise_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-08 14:47:41 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_ROUTER_MANUAL
|
2015-02-05 16:43:07 +01:00
|
|
|
static const net_ipv6addr_t g_ipv6_prefix =
|
|
|
|
{
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_1),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_2),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_3),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_4),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_5),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_6),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_7),
|
|
|
|
HTONS(CONFIG_NET_ICMPv6_PREFIX_8)
|
|
|
|
};
|
2017-11-08 14:47:41 +01:00
|
|
|
#endif /* CONFIG_NET_ICMPv6_ROUTER_MANUAL */
|
2015-02-05 16:43:07 +01:00
|
|
|
|
2017-09-24 14:33:05 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv6addr_mask
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Copy an IPv6 address under a mask
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - Location to return the masked address
|
|
|
|
* src - The IPv6 address to mask
|
|
|
|
* maksk - The address mask
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-08 14:47:41 +01:00
|
|
|
#ifndef CONFIG_NET_ICMPv6_ROUTER_MANUAL
|
2017-09-24 14:33:05 +02:00
|
|
|
static inline void ipv6addr_mask(FAR uint16_t *dest, FAR const uint16_t *src,
|
|
|
|
FAR const uint16_t *mask)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
dest[i] = src[i] & mask[i];
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 14:47:41 +01:00
|
|
|
#endif /* !CONFIG_NET_ICMPv6_ROUTER_MANUAL */
|
2017-09-24 14:33:05 +02:00
|
|
|
|
2015-02-05 16:43:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_radvertise
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send an ICMPv6 Router Advertisement
|
|
|
|
*
|
2017-09-24 14:33:05 +02:00
|
|
|
* Input Parameters:
|
2015-02-05 16:43:07 +01:00
|
|
|
* dev - The device driver structure containing the outgoing ICMPv6 packet
|
|
|
|
* buffer
|
|
|
|
*
|
2017-09-24 14:33:05 +02:00
|
|
|
* Returned Value:
|
2015-02-05 16:43:07 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void icmpv6_radvertise(FAR struct net_driver_s *dev)
|
|
|
|
{
|
2017-07-07 03:37:01 +02:00
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
2017-07-06 20:00:59 +02:00
|
|
|
FAR struct icmpv6_router_advertise_s *adv;
|
2015-02-05 16:43:07 +01:00
|
|
|
FAR struct icmpv6_srclladdr_s *srcaddr;
|
|
|
|
FAR struct icmpv6_mtu_s *mtu;
|
|
|
|
FAR struct icmpv6_prefixinfo_s *prefix;
|
2017-07-07 14:51:52 +02:00
|
|
|
uint16_t lladdrsize;
|
2017-07-06 20:00:59 +02:00
|
|
|
uint16_t l3size;
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set up the IPv6 header */
|
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
ipv6->vtc = 0x60; /* Version/traffic class (MS) */
|
|
|
|
ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
|
|
|
|
ipv6->flow = 0; /* Flow label (LS) */
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Length excludes the IPv6 header */
|
|
|
|
|
2018-11-09 23:05:11 +01:00
|
|
|
lladdrsize = netdev_lladdrsize(dev);
|
2017-09-24 14:33:05 +02:00
|
|
|
l3size = sizeof(struct icmpv6_router_advertise_s) +
|
2017-07-07 14:51:52 +02:00
|
|
|
SIZEOF_ICMPV6_SRCLLADDR_S(lladdrsize) +
|
2017-07-06 20:00:59 +02:00
|
|
|
sizeof(struct icmpv6_mtu_s) +
|
2017-09-24 14:33:05 +02:00
|
|
|
sizeof(struct icmpv6_prefixinfo_s);
|
2017-07-06 20:00:59 +02:00
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
ipv6->len[0] = (l3size >> 8);
|
|
|
|
ipv6->len[1] = (l3size & 0xff);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
ipv6->proto = IP_PROTO_ICMP6; /* Next header */
|
|
|
|
ipv6->ttl = 255; /* Hop limit */
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Swap source for destination IP address, add our source IP address */
|
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
net_ipv6addr_copy(ipv6->destipaddr, g_ipv6_allnodes);
|
2017-09-24 14:33:05 +02:00
|
|
|
|
|
|
|
/* Source IP address must be set to link-local IP */
|
|
|
|
|
2018-11-09 22:28:58 +01:00
|
|
|
icmpv6_linkipaddr(dev, ipv6->srcipaddr);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set up the ICMPv6 Router Advertise response */
|
|
|
|
|
|
|
|
adv = ICMPv6ADVERTISE;
|
|
|
|
adv->type = ICMPV6_ROUTER_ADVERTISE; /* Message type */
|
|
|
|
adv->code = 0; /* Message qualifier */
|
|
|
|
adv->hoplimit = 64; /* Current hop limit */
|
|
|
|
adv->flags = ICMPv6_RADV_FLAG_M; /* Managed address flag. */
|
|
|
|
adv->lifetime = HTONS(1800); /* Router lifetime */
|
2017-09-24 14:33:05 +02:00
|
|
|
adv->reachable = 0; /* Reachable time */
|
|
|
|
adv->retrans = 0; /* Retransmission timer */
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set up the source address option */
|
|
|
|
|
2017-07-06 20:00:59 +02:00
|
|
|
srcaddr = (FAR struct icmpv6_srclladdr_s *)
|
2020-01-12 21:17:29 +01:00
|
|
|
((FAR uint8_t *)adv +
|
|
|
|
sizeof(struct icmpv6_router_advertise_s));
|
2015-02-05 16:43:07 +01:00
|
|
|
srcaddr->opttype = ICMPv6_OPT_SRCLLADDR;
|
2017-07-07 14:51:52 +02:00
|
|
|
srcaddr->optlen = ICMPv6_OPT_OCTECTS(lladdrsize);
|
|
|
|
memcpy(srcaddr->srclladdr, &dev->d_mac, lladdrsize);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set up the MTU option */
|
|
|
|
|
2017-07-06 20:00:59 +02:00
|
|
|
mtu = (FAR struct icmpv6_mtu_s *)
|
2020-01-11 16:40:32 +01:00
|
|
|
((FAR uint8_t *)srcaddr +
|
|
|
|
SIZEOF_ICMPV6_SRCLLADDR_S(lladdrsize));
|
2015-02-05 16:43:07 +01:00
|
|
|
mtu->opttype = ICMPv6_OPT_MTU;
|
|
|
|
mtu->optlen = 1;
|
|
|
|
mtu->reserved = 0;
|
2018-11-09 22:41:23 +01:00
|
|
|
mtu->mtu = HTONL(dev->d_pktsize - dev->d_llhdrlen);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set up the prefix option */
|
|
|
|
|
2017-09-24 14:33:05 +02:00
|
|
|
prefix = (FAR struct icmpv6_prefixinfo_s *)
|
|
|
|
((FAR uint8_t *)mtu + sizeof(struct icmpv6_mtu_s));
|
|
|
|
prefix->opttype = ICMPv6_OPT_PREFIX;
|
|
|
|
prefix->optlen = 4;
|
|
|
|
prefix->flags = ICMPv6_PRFX_FLAG_L | ICMPv6_PRFX_FLAG_A;
|
|
|
|
prefix->vlifetime = HTONL(2592000);
|
|
|
|
prefix->plifetime = HTONL(604800);
|
|
|
|
prefix->reserved[0] = 0;
|
|
|
|
prefix->reserved[1] = 0;
|
|
|
|
|
2017-11-08 14:47:41 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_ROUTER_MANUAL
|
|
|
|
/* Copy the configured prefex */
|
|
|
|
|
2017-11-08 14:50:36 +01:00
|
|
|
prefix->preflen = CONFIG_NET_ICMPv6_PREFLEN;
|
2017-11-08 14:47:41 +01:00
|
|
|
net_ipv6addr_copy(prefix->prefix, g_ipv6_prefix);
|
|
|
|
#else
|
2017-09-24 14:33:05 +02:00
|
|
|
/* Set the prefix and prefix length based on net driver IP and netmask */
|
|
|
|
|
2017-11-08 14:50:36 +01:00
|
|
|
prefix->preflen = net_ipv6_mask2pref(dev->d_ipv6netmask);
|
2017-09-24 14:33:05 +02:00
|
|
|
ipv6addr_mask(prefix->prefix, dev->d_ipv6addr, dev->d_ipv6netmask);
|
2017-11-08 14:47:41 +01:00
|
|
|
#endif /* CONFIG_NET_ICMPv6_ROUTER_MANUAL */
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Calculate the checksum over both the ICMP header and payload */
|
|
|
|
|
2017-07-07 03:37:01 +02:00
|
|
|
adv->chksum = 0;
|
2018-11-05 15:56:07 +01:00
|
|
|
adv->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
/* Set the size to the size of the IPv6 header and the payload size */
|
|
|
|
|
2017-09-24 14:33:05 +02:00
|
|
|
dev->d_len = IPv6_HDRLEN + l3size;
|
2015-02-05 16:43:07 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Outgoing ICMPv6 Router Advertise length: %d (%d)\n",
|
2017-07-07 03:37:01 +02:00
|
|
|
dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
|
2015-02-05 16:43:07 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.icmpv6.sent++;
|
|
|
|
g_netstats.ipv6.sent++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_ROUTER */
|