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 "instance/instance.hpp"
39 
40 namespace ot {
41 namespace NeighborDiscovery {
42 
UpdateService(void)43 void Agent::UpdateService(void)
44 {
45     Error                           error;
46     uint16_t                        rloc16 = Get<Mle::MleRouter>().GetRloc16();
47     NetworkData::Iterator           iterator;
48     NetworkData::OnMeshPrefixConfig config;
49     Lowpan::Context                 lowpanContext;
50 
51     if (IsAlocInUse())
52     {
53         uint8_t contextId = static_cast<uint8_t>(mAloc.GetAddress().GetIid().GetLocator() -
54                                                  Mle::kAloc16NeighborDiscoveryAgentStart + 1);
55         bool    found     = false;
56 
57         iterator = NetworkData::kIteratorInit;
58 
59         while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, rloc16, config) == kErrorNone)
60         {
61             if (!config.mNdDns)
62             {
63                 continue;
64             }
65 
66             error = Get<NetworkData::Leader>().GetContext(AsCoreType(&config.mPrefix.mPrefix), lowpanContext);
67 
68             if ((error != kErrorNone) || (lowpanContext.mContextId != contextId))
69             {
70                 continue;
71             }
72 
73             found = true;
74             break;
75         }
76 
77         if (!found)
78         {
79             Get<ThreadNetif>().RemoveUnicastAddress(mAloc);
80             FreeAloc();
81         }
82     }
83 
84     VerifyOrExit(!IsAlocInUse());
85 
86     iterator = NetworkData::kIteratorInit;
87 
88     while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, rloc16, config) == kErrorNone)
89     {
90         if (!config.mNdDns)
91         {
92             continue;
93         }
94 
95         error = Get<NetworkData::Leader>().GetContext(AsCoreType(&config.mPrefix.mPrefix), lowpanContext);
96 
97         if (error == kErrorNone)
98         {
99             uint16_t aloc16 = Mle::kAloc16NeighborDiscoveryAgentStart + lowpanContext.mContextId - 1;
100 
101             mAloc.InitAsThreadOrigin();
102             mAloc.GetAddress().SetToAnycastLocator(Get<Mle::MleRouter>().GetMeshLocalPrefix(), aloc16);
103             mAloc.mMeshLocal = true;
104             Get<ThreadNetif>().AddUnicastAddress(mAloc);
105             ExitNow();
106         }
107     }
108 
109 exit:
110     return;
111 }
112 
113 } // namespace NeighborDiscovery
114 } // namespace ot
115 
116 #endif // OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
117