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