net_ipv6_pref2mask.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /****************************************************************************
  2. * net/utils/net_ipv6_pref2mask.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/net/ip.h>
  25. #include "utils/utils.h"
  26. #ifdef CONFIG_NET_IPv6
  27. /****************************************************************************
  28. * Public Functions
  29. ****************************************************************************/
  30. /****************************************************************************
  31. * Name: net_ipv6_pref2mask
  32. *
  33. * Description:
  34. * Convert a IPv6 prefix length to a network mask. The prefix length
  35. * specifies the number of MS bits under mask (0-128)
  36. *
  37. * Input Parameters:
  38. * preflen - Determines the width of the netmask (in bits). Range 0-128
  39. * mask - The location to return the netmask.
  40. *
  41. * Returned Value:
  42. * None
  43. *
  44. ****************************************************************************/
  45. void net_ipv6_pref2mask(uint8_t preflen, net_ipv6addr_t mask)
  46. {
  47. unsigned int bit;
  48. unsigned int i;
  49. /* Set the network mask. preflen is the number of MS bits under the mask.
  50. *
  51. * Eg. preflen = 38
  52. * NETMASK: ffff ffff fc00 0000 0000 0000 0000 0000
  53. * bit: 1 1..1
  54. * 1 1..3 3..4 4..6 6..7 8..9 9..1 1..2
  55. * 0..5 6..1 2..7 8..3 4..9 0..5 6..1 2..7
  56. * preflen: 1 1..1
  57. * 1 1..3 3..4 4..6 6..8 8..9 9..1 1..2
  58. * 1..6 7..2 3..8 9..4 5..0 1..6 7..2 3..8
  59. */
  60. for (i = 0; i < 8; i++)
  61. {
  62. /* bit = {0, 16, 32, 48, 64, 80, 96, 112} */
  63. bit = i << 4;
  64. if (preflen > bit)
  65. {
  66. /* Eg. preflen = 38, bit = {0, 16, 32} */
  67. if (preflen >= (bit + 16))
  68. {
  69. /* Eg. preflen = 38, bit = {0, 16} */
  70. mask[i] = 0xffff;
  71. }
  72. else
  73. {
  74. /* Eg. preflen = 38, bit = {32}
  75. * preflen - bit = 6
  76. * mask = 0xffff << (16-6)
  77. * = 0xfc00
  78. */
  79. mask[i] = 0xffff << (16 - (preflen - bit));
  80. }
  81. }
  82. else
  83. {
  84. /* Eg. preflen=38, bit= {48, 64, 80, 96, 112} */
  85. mask[i] = 0x0000;
  86. }
  87. }
  88. }
  89. #endif /* CONFIG_NET_IPv6 */