1 /* 2 * Copyright (c) 2016, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for IPv6 datagram filtering. 32 */ 33 34 #ifndef IP6_FILTER_HPP_ 35 #define IP6_FILTER_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include "common/array.hpp" 40 #include "common/locator.hpp" 41 #include "common/message.hpp" 42 #include "common/non_copyable.hpp" 43 44 namespace ot { 45 namespace Ip6 { 46 47 /** 48 * @addtogroup core-ipv6 49 * 50 * @brief 51 * This module includes definitions for IPv6 datagram filtering. 52 * 53 * @{ 54 */ 55 56 /** 57 * Implements an IPv6 datagram filter. 58 */ 59 class Filter : public InstanceLocator, private NonCopyable 60 { 61 public: 62 /** 63 * Initializes the Filter object. 64 * 65 * @param[in] aInstance A reference to the OpenThread instance. 66 */ Filter(Instance & aInstance)67 explicit Filter(Instance &aInstance) 68 : InstanceLocator(aInstance) 69 { 70 } 71 72 /** 73 * Indicates whether or not the IPv6 datagram passes the filter. 74 * 75 * @param[in] aMessage The IPv6 datagram to process. 76 * 77 * @retval TRUE Accept the IPv6 datagram. 78 * @retval FALSE Reject the IPv6 datagram. 79 */ 80 bool Accept(Message &aMessage) const; 81 82 /** 83 * Adds a port to the allowed unsecured port list. 84 * 85 * @param[in] aPort The port value. 86 * 87 * @retval kErrorNone The port was successfully added to the allowed unsecure port list. 88 * @retval kErrorInvalidArgs The port is invalid (value 0 is reserved for internal use). 89 * @retval kErrorNoBufs The unsecure port list is full. 90 */ AddUnsecurePort(uint16_t aPort)91 Error AddUnsecurePort(uint16_t aPort) { return UpdateUnsecurePorts(kAdd, aPort); } 92 93 /** 94 * Removes a port from the allowed unsecure port list. 95 * 96 * @param[in] aPort The port value. 97 * 98 * @retval kErrorNone The port was successfully removed from the allowed unsecure port list. 99 * @retval kErrorInvalidArgs The port is invalid (value 0 is reserved for internal use). 100 * @retval kErrorNotFound The port was not found in the unsecure port list. 101 */ RemoveUnsecurePort(uint16_t aPort)102 Error RemoveUnsecurePort(uint16_t aPort) { return UpdateUnsecurePorts(kRemove, aPort); } 103 104 /** 105 * Checks whether a port is in the unsecure port list. 106 * 107 * @param[in] aPort The port value. 108 * 109 * @returns Whether the given port is in the unsecure port list. 110 */ IsUnsecurePort(uint16_t aPort)111 bool IsUnsecurePort(uint16_t aPort) { return mUnsecurePorts.Contains(aPort); } 112 113 /** 114 * Removes all ports from the allowed unsecure port list. 115 */ RemoveAllUnsecurePorts(void)116 void RemoveAllUnsecurePorts(void) { mUnsecurePorts.Clear(); } 117 118 /** 119 * Returns a pointer to the unsecure port list. 120 * 121 * @note Port value 0 is used to indicate an invalid entry. 122 * 123 * @param[out] aNumEntries The number of entries in the list. 124 * 125 * @returns A pointer to the unsecure port list. 126 */ GetUnsecurePorts(uint8_t & aNumEntries) const127 const uint16_t *GetUnsecurePorts(uint8_t &aNumEntries) const 128 { 129 aNumEntries = mUnsecurePorts.GetLength(); 130 131 return &mUnsecurePorts[0]; 132 } 133 134 private: 135 static constexpr uint16_t kMaxUnsecurePorts = 2; 136 137 enum Action : uint8_t 138 { 139 kAdd, 140 kRemove, 141 }; 142 143 Error UpdateUnsecurePorts(Action aAction, uint16_t aPort); 144 145 Array<uint16_t, kMaxUnsecurePorts> mUnsecurePorts; 146 }; 147 148 } // namespace Ip6 149 } // namespace ot 150 151 #endif // IP6_FILTER_HPP_ 152