2015-02-06 15:11:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/utils/net_ipv6_pref2mask.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-04-02 12:39:20 +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-06 15:11:09 +01:00
|
|
|
*
|
2021-04-02 12:39:20 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-06 15:11:09 +01:00
|
|
|
*
|
2021-04-02 12:39:20 +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-06 15:11:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
|
|
|
|
#include "utils/utils.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_ipv6_pref2mask
|
2015-02-06 15:11:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a IPv6 prefix length to a network mask. The prefix length
|
|
|
|
* specifies the number of MS bits under mask (0-128)
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2023-11-03 14:22:03 +01:00
|
|
|
* mask - The location to return the netmask.
|
2015-02-06 15:11:09 +01:00
|
|
|
* preflen - Determines the width of the netmask (in bits). Range 0-128
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-03 14:22:03 +01:00
|
|
|
void net_ipv6_pref2mask(net_ipv6addr_t mask, uint8_t preflen)
|
2015-02-06 15:11:09 +01:00
|
|
|
{
|
|
|
|
unsigned int bit;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Set the network mask. preflen is the number of MS bits under the mask.
|
|
|
|
*
|
|
|
|
* Eg. preflen = 38
|
|
|
|
* NETMASK: ffff ffff fc00 0000 0000 0000 0000 0000
|
|
|
|
* bit: 1 1..1
|
2017-06-28 21:32:19 +02:00
|
|
|
* 1 1..3 3..4 4..6 6..7 8..9 9..1 1..2
|
2015-02-06 15:11:09 +01:00
|
|
|
* 0..5 6..1 2..7 8..3 4..9 0..5 6..1 2..7
|
|
|
|
* preflen: 1 1..1
|
|
|
|
* 1 1..3 3..4 4..6 6..8 8..9 9..1 1..2
|
|
|
|
* 1..6 7..2 3..8 9..4 5..0 1..6 7..2 3..8
|
|
|
|
*/
|
|
|
|
|
2017-05-18 02:22:33 +02:00
|
|
|
for (i = 0; i < 8; i++)
|
2015-02-06 15:11:09 +01:00
|
|
|
{
|
|
|
|
/* bit = {0, 16, 32, 48, 64, 80, 96, 112} */
|
|
|
|
|
|
|
|
bit = i << 4;
|
|
|
|
|
|
|
|
if (preflen > bit)
|
|
|
|
{
|
|
|
|
/* Eg. preflen = 38, bit = {0, 16, 32} */
|
|
|
|
|
2017-05-18 02:22:33 +02:00
|
|
|
if (preflen >= (bit + 16))
|
2015-02-06 15:11:09 +01:00
|
|
|
{
|
|
|
|
/* Eg. preflen = 38, bit = {0, 16} */
|
|
|
|
|
|
|
|
mask[i] = 0xffff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Eg. preflen = 38, bit = {32}
|
2020-01-30 09:22:18 +01:00
|
|
|
* preflen - bit = 6
|
2017-05-18 02:22:33 +02:00
|
|
|
* mask = 0xffff << (16-6)
|
2015-02-06 15:11:09 +01:00
|
|
|
* = 0xfc00
|
|
|
|
*/
|
|
|
|
|
2023-10-24 11:09:01 +02:00
|
|
|
mask[i] = HTONS(0xffff << (16 - (preflen - bit)));
|
2015-02-06 15:11:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-18 02:22:33 +02:00
|
|
|
/* Eg. preflen=38, bit= {48, 64, 80, 96, 112} */
|
2015-02-06 15:11:09 +01:00
|
|
|
|
|
|
|
mask[i] = 0x0000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|