• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 implements common methods for manipulating MLE TLVs.
32  */
33 
34 #include "tlvs.hpp"
35 
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38 #include "common/message.hpp"
39 
40 namespace ot {
41 
GetSize(void) const42 uint32_t Tlv::GetSize(void) const
43 {
44     return IsExtended() ? sizeof(ExtendedTlv) + As<ExtendedTlv>(this)->GetLength() : sizeof(Tlv) + GetLength();
45 }
46 
GetValue(void)47 uint8_t *Tlv::GetValue(void)
48 {
49     return reinterpret_cast<uint8_t *>(this) + (IsExtended() ? sizeof(ExtendedTlv) : sizeof(Tlv));
50 }
51 
GetValue(void) const52 const uint8_t *Tlv::GetValue(void) const
53 {
54     return reinterpret_cast<const uint8_t *>(this) + (IsExtended() ? sizeof(ExtendedTlv) : sizeof(Tlv));
55 }
56 
AppendTo(Message & aMessage) const57 Error Tlv::AppendTo(Message &aMessage) const
58 {
59     return aMessage.AppendBytes(this, static_cast<uint16_t>(GetSize()));
60 }
61 
FindTlv(const Message & aMessage,uint8_t aType,uint16_t aMaxSize,Tlv & aTlv)62 Error Tlv::FindTlv(const Message &aMessage, uint8_t aType, uint16_t aMaxSize, Tlv &aTlv)
63 {
64     Error    error;
65     uint16_t offset;
66     uint16_t size;
67 
68     SuccessOrExit(error = Find(aMessage, aType, &offset, &size, nullptr));
69 
70     if (aMaxSize > size)
71     {
72         aMaxSize = size;
73     }
74 
75     aMessage.ReadBytes(offset, &aTlv, aMaxSize);
76 
77 exit:
78     return error;
79 }
80 
FindTlvOffset(const Message & aMessage,uint8_t aType,uint16_t & aOffset)81 Error Tlv::FindTlvOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset)
82 {
83     return Find(aMessage, aType, &aOffset, nullptr, nullptr);
84 }
85 
FindTlvValueOffset(const Message & aMessage,uint8_t aType,uint16_t & aValueOffset,uint16_t & aLength)86 Error Tlv::FindTlvValueOffset(const Message &aMessage, uint8_t aType, uint16_t &aValueOffset, uint16_t &aLength)
87 {
88     Error    error;
89     uint16_t offset;
90     uint16_t size;
91     bool     isExtendedTlv;
92 
93     SuccessOrExit(error = Find(aMessage, aType, &offset, &size, &isExtendedTlv));
94 
95     if (!isExtendedTlv)
96     {
97         aValueOffset = offset + sizeof(Tlv);
98         aLength      = size - sizeof(Tlv);
99     }
100     else
101     {
102         aValueOffset = offset + sizeof(ExtendedTlv);
103         aLength      = size - sizeof(ExtendedTlv);
104     }
105 
106 exit:
107     return error;
108 }
109 
Find(const Message & aMessage,uint8_t aType,uint16_t * aOffset,uint16_t * aSize,bool * aIsExtendedTlv)110 Error Tlv::Find(const Message &aMessage, uint8_t aType, uint16_t *aOffset, uint16_t *aSize, bool *aIsExtendedTlv)
111 {
112     // This static method searches within a `aMessage` for a TLV type
113     // `aType` and outputs the TLV offset, size, and whether or not it
114     // is an Extended TLV.
115     //
116     // A `nullptr` pointer can be used for output parameters `aOffset`,
117     // `aSize`, or `aIsExtendedTlv` if the parameter is not required.
118     //
119     // Returns `kErrorNone` when found, otherwise `kErrorNotFound`.
120 
121     Error    error        = kErrorNotFound;
122     uint16_t offset       = aMessage.GetOffset();
123     uint16_t remainingLen = aMessage.GetLength();
124     Tlv      tlv;
125     uint32_t size;
126 
127     VerifyOrExit(offset <= remainingLen);
128     remainingLen -= offset;
129 
130     while (true)
131     {
132         SuccessOrExit(aMessage.Read(offset, tlv));
133 
134         if (tlv.mLength != kExtendedLength)
135         {
136             size = tlv.GetSize();
137         }
138         else
139         {
140             ExtendedTlv extTlv;
141 
142             SuccessOrExit(aMessage.Read(offset, extTlv));
143 
144             VerifyOrExit(extTlv.GetLength() <= (remainingLen - sizeof(ExtendedTlv)));
145             size = extTlv.GetSize();
146         }
147 
148         VerifyOrExit(size <= remainingLen);
149 
150         if (tlv.GetType() == aType)
151         {
152             if (aOffset != nullptr)
153             {
154                 *aOffset = offset;
155             }
156 
157             if (aSize != nullptr)
158             {
159                 *aSize = static_cast<uint16_t>(size);
160             }
161 
162             if (aIsExtendedTlv != nullptr)
163             {
164                 *aIsExtendedTlv = (tlv.mLength == kExtendedLength);
165             }
166 
167             error = kErrorNone;
168             ExitNow();
169         }
170 
171         offset += size;
172         remainingLen -= size;
173     }
174 
175 exit:
176     return error;
177 }
178 
ReadUintTlv(const Message & aMessage,uint16_t aOffset,UintType & aValue)179 template <typename UintType> Error Tlv::ReadUintTlv(const Message &aMessage, uint16_t aOffset, UintType &aValue)
180 {
181     Error error;
182 
183     SuccessOrExit(error = ReadTlv(aMessage, aOffset, &aValue, sizeof(aValue)));
184     aValue = Encoding::BigEndian::HostSwap<UintType>(aValue);
185 
186 exit:
187     return error;
188 }
189 
190 // Explicit instantiations of `ReadUintTlv<>()`
191 template Error Tlv::ReadUintTlv<uint8_t>(const Message &aMessage, uint16_t aOffset, uint8_t &aValue);
192 template Error Tlv::ReadUintTlv<uint16_t>(const Message &aMessage, uint16_t aOffset, uint16_t &aValue);
193 template Error Tlv::ReadUintTlv<uint32_t>(const Message &aMessage, uint16_t aOffset, uint32_t &aValue);
194 
ReadTlv(const Message & aMessage,uint16_t aOffset,void * aValue,uint8_t aMinLength)195 Error Tlv::ReadTlv(const Message &aMessage, uint16_t aOffset, void *aValue, uint8_t aMinLength)
196 {
197     Error error = kErrorNone;
198     Tlv   tlv;
199 
200     SuccessOrExit(error = aMessage.Read(aOffset, tlv));
201     VerifyOrExit(!tlv.IsExtended() && (tlv.GetLength() >= aMinLength), error = kErrorParse);
202     VerifyOrExit(tlv.GetSize() + aOffset <= aMessage.GetLength(), error = kErrorParse);
203 
204     aMessage.ReadBytes(aOffset + sizeof(Tlv), aValue, aMinLength);
205 
206 exit:
207     return error;
208 }
209 
FindUintTlv(const Message & aMessage,uint8_t aType,UintType & aValue)210 template <typename UintType> Error Tlv::FindUintTlv(const Message &aMessage, uint8_t aType, UintType &aValue)
211 {
212     Error    error = kErrorNone;
213     uint16_t offset;
214 
215     SuccessOrExit(error = FindTlvOffset(aMessage, aType, offset));
216     error = ReadUintTlv<UintType>(aMessage, offset, aValue);
217 
218 exit:
219     return error;
220 }
221 
222 // Explicit instantiations of `FindUintTlv<>()`
223 template Error Tlv::FindUintTlv<uint8_t>(const Message &aMessage, uint8_t aType, uint8_t &aValue);
224 template Error Tlv::FindUintTlv<uint16_t>(const Message &aMessage, uint8_t aType, uint16_t &aValue);
225 template Error Tlv::FindUintTlv<uint32_t>(const Message &aMessage, uint8_t aType, uint32_t &aValue);
226 
FindTlv(const Message & aMessage,uint8_t aType,void * aValue,uint8_t aLength)227 Error Tlv::FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint8_t aLength)
228 {
229     Error    error;
230     uint16_t offset;
231     uint16_t length;
232 
233     SuccessOrExit(error = FindTlvValueOffset(aMessage, aType, offset, length));
234     VerifyOrExit(length >= aLength, error = kErrorParse);
235     aMessage.ReadBytes(offset, aValue, aLength);
236 
237 exit:
238     return error;
239 }
240 
AppendUintTlv(Message & aMessage,uint8_t aType,UintType aValue)241 template <typename UintType> Error Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, UintType aValue)
242 {
243     UintType value = Encoding::BigEndian::HostSwap<UintType>(aValue);
244 
245     return AppendTlv(aMessage, aType, &value, sizeof(UintType));
246 }
247 
248 // Explicit instantiations of `AppendUintTlv<>()`
249 template Error Tlv::AppendUintTlv<uint8_t>(Message &aMessage, uint8_t aType, uint8_t aValue);
250 template Error Tlv::AppendUintTlv<uint16_t>(Message &aMessage, uint8_t aType, uint16_t aValue);
251 template Error Tlv::AppendUintTlv<uint32_t>(Message &aMessage, uint8_t aType, uint32_t aValue);
252 
AppendTlv(Message & aMessage,uint8_t aType,const void * aValue,uint8_t aLength)253 Error Tlv::AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength)
254 {
255     Error error = kErrorNone;
256     Tlv   tlv;
257 
258     OT_ASSERT(aLength <= Tlv::kBaseTlvMaxLength);
259 
260     tlv.SetType(aType);
261     tlv.SetLength(aLength);
262     SuccessOrExit(error = aMessage.Append(tlv));
263 
264     VerifyOrExit(aLength > 0);
265     error = aMessage.AppendBytes(aValue, aLength);
266 
267 exit:
268     return error;
269 }
270 
271 } // namespace ot
272