1 /*
2  *  Copyright (c) 2019, 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 the MAC radio links.
32  */
33 
34 #include "mac_links.hpp"
35 
36 #include "common/code_utils.hpp"
37 #include "common/instance.hpp"
38 #include "common/locator_getters.hpp"
39 
40 namespace ot {
41 namespace Mac {
42 
43 //---------------------------------------------------------------------------------------------------------------------
44 // TxFrames
45 
TxFrames(Instance & aInstance)46 TxFrames::TxFrames(Instance &aInstance)
47     : InstanceLocator(aInstance)
48 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
49     , mTxFrame802154(aInstance.Get<SubMac>().GetTransmitFrame())
50 #endif
51 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
52     , mTxFrameTrel(aInstance.Get<Trel::Link>().GetTransmitFrame())
53 #endif
54 {
55 }
56 
57 #if OPENTHREAD_CONFIG_MULTI_RADIO
58 
GetTxFrame(RadioType aRadioType)59 TxFrame &TxFrames::GetTxFrame(RadioType aRadioType)
60 {
61     TxFrame *frame = nullptr;
62 
63     switch (aRadioType)
64     {
65 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
66     case kRadioTypeIeee802154:
67         frame = &mTxFrame802154;
68         break;
69 #endif
70 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
71     case kRadioTypeTrel:
72         frame = &mTxFrameTrel;
73         break;
74 #endif
75     }
76 
77     mSelectedRadioTypes.Add(aRadioType);
78 
79     return *frame;
80 }
81 
GetTxFrame(RadioTypes aRadioTypes)82 TxFrame &TxFrames::GetTxFrame(RadioTypes aRadioTypes)
83 {
84     // Return the TxFrame among all set of `aRadioTypes` with the smallest MTU.
85     // Note that this is `TxFrame` to be sent out in parallel over multiple radio
86     // radio links in `aRadioTypes, so we need to make sure that it fits in the
87     // most restricted radio link (with smallest MTU).
88 
89     TxFrame *frame = nullptr;
90 
91 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
92     if (aRadioTypes.Contains(kRadioTypeIeee802154))
93     {
94         frame = &mTxFrame802154;
95     }
96 #endif
97 
98 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
99     if (aRadioTypes.Contains(kRadioTypeTrel) && ((frame == nullptr) || (frame->GetMtu() > mTxFrameTrel.GetMtu())))
100     {
101         frame = &mTxFrameTrel;
102     }
103 #endif
104 
105     mSelectedRadioTypes.Add(aRadioTypes);
106 
107     return *frame;
108 }
109 
GetBroadcastTxFrame(void)110 TxFrame &TxFrames::GetBroadcastTxFrame(void)
111 {
112     RadioTypes allRadios;
113 
114     allRadios.AddAll();
115     return GetTxFrame(allRadios);
116 }
117 
118 #endif // #if OPENTHREAD_CONFIG_MULTI_RADIO
119 
120 //---------------------------------------------------------------------------------------------------------------------
121 // Links
122 
Links(Instance & aInstance)123 Links::Links(Instance &aInstance)
124     : InstanceLocator(aInstance)
125     , mSubMac(aInstance)
126 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
127     , mTrel(aInstance)
128 #endif
129     , mTxFrames(aInstance)
130 #if !OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
131     , mShortAddress(kShortAddrInvalid)
132 #endif
133 {
134 #if !OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
135     mExtAddress.Clear();
136 #endif
137 }
138 
139 #if OPENTHREAD_CONFIG_MULTI_RADIO
140 
Send(TxFrame & aFrame,RadioTypes aRadioTypes)141 void Links::Send(TxFrame &aFrame, RadioTypes aRadioTypes)
142 {
143 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
144     if (aRadioTypes.Contains(kRadioTypeIeee802154) && mTxFrames.mTxFrame802154.IsEmpty())
145     {
146         mTxFrames.mTxFrame802154.CopyFrom(aFrame);
147     }
148 #endif
149 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
150     if (aRadioTypes.Contains(kRadioTypeTrel) && mTxFrames.mTxFrameTrel.IsEmpty())
151     {
152         mTxFrames.mTxFrameTrel.CopyFrom(aFrame);
153     }
154 #endif
155 
156 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
157     if (aRadioTypes.Contains(kRadioTypeIeee802154))
158     {
159         Error error = mSubMac.Send();
160 
161         OT_ASSERT(error == kErrorNone);
162         OT_UNUSED_VARIABLE(error);
163     }
164 #endif
165 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
166     if (aRadioTypes.Contains(kRadioTypeTrel))
167     {
168         mTrel.Send();
169     }
170 #endif
171 }
172 
173 #endif // #if OPENTHREAD_CONFIG_MULTI_RADIO
174 
GetCurrentMacKey(const Frame & aFrame) const175 const Key *Links::GetCurrentMacKey(const Frame &aFrame) const
176 {
177     // Gets the security MAC key (for Key Mode 1) based on radio link type of `aFrame`.
178 
179     const Key *key = nullptr;
180 #if OPENTHREAD_CONFIG_MULTI_RADIO
181     RadioType radioType = aFrame.GetRadioType();
182 #endif
183 
184 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
185 #if OPENTHREAD_CONFIG_MULTI_RADIO
186     if (radioType == kRadioTypeIeee802154)
187 #endif
188     {
189         ExitNow(key = &Get<SubMac>().GetCurrentMacKey());
190     }
191 #endif
192 
193 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
194 #if OPENTHREAD_CONFIG_MULTI_RADIO
195     if (radioType == kRadioTypeTrel)
196 #endif
197     {
198         ExitNow(key = &Get<KeyManager>().GetCurrentTrelMacKey());
199     }
200 #endif
201 
202     OT_UNUSED_VARIABLE(aFrame);
203 
204 exit:
205     return key;
206 }
207 
GetTemporaryMacKey(const Frame & aFrame,uint32_t aKeySequence) const208 const Key *Links::GetTemporaryMacKey(const Frame &aFrame, uint32_t aKeySequence) const
209 {
210     // Gets the security MAC key (for Key Mode 1) based on radio link
211     // type of `aFrame` and given Key Sequence.
212 
213     const Key *key = nullptr;
214 #if OPENTHREAD_CONFIG_MULTI_RADIO
215     RadioType radioType = aFrame.GetRadioType();
216 #endif
217 
218 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
219 #if OPENTHREAD_CONFIG_MULTI_RADIO
220     if (radioType == kRadioTypeIeee802154)
221 #endif
222     {
223         if (aKeySequence == Get<KeyManager>().GetCurrentKeySequence() - 1)
224         {
225             ExitNow(key = &Get<SubMac>().GetPreviousMacKey());
226         }
227         else if (aKeySequence == Get<KeyManager>().GetCurrentKeySequence() + 1)
228         {
229             ExitNow(key = &Get<SubMac>().GetNextMacKey());
230         }
231         else
232         {
233             OT_ASSERT(false);
234         }
235     }
236 #endif
237 
238 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
239 #if OPENTHREAD_CONFIG_MULTI_RADIO
240     if (radioType == kRadioTypeTrel)
241 #endif
242     {
243         ExitNow(key = &Get<KeyManager>().GetTemporaryTrelMacKey(aKeySequence));
244     }
245 #endif
246 
247     OT_UNUSED_VARIABLE(aFrame);
248 
249 exit:
250     return key;
251 }
252 
253 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
SetMacFrameCounter(TxFrame & aFrame)254 void Links::SetMacFrameCounter(TxFrame &aFrame)
255 {
256 #if OPENTHREAD_CONFIG_MULTI_RADIO
257     RadioType radioType = aFrame.GetRadioType();
258 #endif
259 
260 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
261 #if OPENTHREAD_CONFIG_MULTI_RADIO
262     if (radioType == kRadioTypeTrel)
263 #endif
264     {
265         aFrame.SetFrameCounter(Get<KeyManager>().GetTrelMacFrameCounter());
266         Get<KeyManager>().IncrementTrelMacFrameCounter();
267         ExitNow();
268     }
269 #endif
270 
271 exit:
272     return;
273 }
274 #endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
275 
276 } // namespace Mac
277 } // namespace ot
278