1 /*
2  *  Copyright (c) 2021, 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 IPv6 Neighbor Discovery Agent.
32  */
33 
34 #include "nd_agent.hpp"
35 
36 #if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
37 
38 #include "common/locator_getters.hpp"
39 #include "thread/lowpan.hpp"
40 #include "thread/mle_router.hpp"
41 #include "thread/network_data_leader.hpp"
42 #include "thread/thread_netif.hpp"
43 
44 namespace ot {
45 namespace NeighborDiscovery {
46 
UpdateService(void)47 void Agent::UpdateService(void)
48 {
49     Error                           error;
50     uint16_t                        rloc16 = Get<Mle::MleRouter>().GetRloc16();
51     NetworkData::Iterator           iterator;
52     NetworkData::OnMeshPrefixConfig config;
53     Lowpan::Context                 lowpanContext;
54 
55     if (IsAlocInUse())
56     {
57         uint8_t contextId = static_cast<uint8_t>(mAloc.GetAddress().GetIid().GetLocator() -
58                                                  Mle::kAloc16NeighborDiscoveryAgentStart + 1);
59         bool    found     = false;
60 
61         iterator = NetworkData::kIteratorInit;
62 
63         while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, rloc16, config) == kErrorNone)
64         {
65             if (!config.mNdDns)
66             {
67                 continue;
68             }
69 
70             error = Get<NetworkData::Leader>().GetContext(static_cast<const Ip6::Address &>(config.mPrefix.mPrefix),
71                                                           lowpanContext);
72 
73             if ((error != kErrorNone) || (lowpanContext.mContextId != contextId))
74             {
75                 continue;
76             }
77 
78             found = true;
79             break;
80         }
81 
82         if (!found)
83         {
84             Get<ThreadNetif>().RemoveUnicastAddress(mAloc);
85             FreeAloc();
86         }
87     }
88 
89     VerifyOrExit(!IsAlocInUse());
90 
91     iterator = NetworkData::kIteratorInit;
92 
93     while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, rloc16, config) == kErrorNone)
94     {
95         if (!config.mNdDns)
96         {
97             continue;
98         }
99 
100         error = Get<NetworkData::Leader>().GetContext(static_cast<const Ip6::Address &>(config.mPrefix.mPrefix),
101                                                       lowpanContext);
102 
103         if (error == kErrorNone)
104         {
105             uint16_t rloc = Mle::kAloc16NeighborDiscoveryAgentStart + lowpanContext.mContextId - 1;
106 
107             mAloc.InitAsThreadOrigin();
108             mAloc.GetAddress().SetToAnycastLocator(Get<Mle::MleRouter>().GetMeshLocalPrefix(), rloc);
109             Get<ThreadNetif>().AddUnicastAddress(mAloc);
110             ExitNow();
111         }
112     }
113 
114 exit:
115     return;
116 }
117 
ApplyMeshLocalPrefix(void)118 void Agent::ApplyMeshLocalPrefix(void)
119 {
120     VerifyOrExit(IsAlocInUse());
121 
122     Get<ThreadNetif>().RemoveUnicastAddress(mAloc);
123     mAloc.GetAddress().SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
124     Get<ThreadNetif>().AddUnicastAddress(mAloc);
125 
126 exit:
127     return;
128 }
129 
130 } // namespace NeighborDiscovery
131 } // namespace ot
132 
133 #endif // OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
134