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/as_core_type.hpp"
39 #include "common/locator_getters.hpp"
40 #include "thread/lowpan.hpp"
41 #include "thread/mle_router.hpp"
42 #include "thread/network_data_leader.hpp"
43 #include "thread/thread_netif.hpp"
44
45 namespace ot {
46 namespace NeighborDiscovery {
47
UpdateService(void)48 void Agent::UpdateService(void)
49 {
50 Error error;
51 uint16_t rloc16 = Get<Mle::MleRouter>().GetRloc16();
52 NetworkData::Iterator iterator;
53 NetworkData::OnMeshPrefixConfig config;
54 Lowpan::Context lowpanContext;
55
56 if (IsAlocInUse())
57 {
58 uint8_t contextId = static_cast<uint8_t>(mAloc.GetAddress().GetIid().GetLocator() -
59 Mle::kAloc16NeighborDiscoveryAgentStart + 1);
60 bool found = false;
61
62 iterator = NetworkData::kIteratorInit;
63
64 while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, rloc16, config) == kErrorNone)
65 {
66 if (!config.mNdDns)
67 {
68 continue;
69 }
70
71 error = Get<NetworkData::Leader>().GetContext(AsCoreType(&config.mPrefix.mPrefix), 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(AsCoreType(&config.mPrefix.mPrefix), lowpanContext);
101
102 if (error == kErrorNone)
103 {
104 uint16_t aloc16 = Mle::kAloc16NeighborDiscoveryAgentStart + lowpanContext.mContextId - 1;
105
106 mAloc.InitAsThreadOrigin();
107 mAloc.GetAddress().SetToAnycastLocator(Get<Mle::MleRouter>().GetMeshLocalPrefix(), aloc16);
108 mAloc.mMeshLocal = true;
109 Get<ThreadNetif>().AddUnicastAddress(mAloc);
110 ExitNow();
111 }
112 }
113
114 exit:
115 return;
116 }
117
118 } // namespace NeighborDiscovery
119 } // namespace ot
120
121 #endif // OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
122