1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2021, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker /**
30*cfb92d14SAndroid Build Coastguard Worker * @file
31*cfb92d14SAndroid Build Coastguard Worker * @brief
32*cfb92d14SAndroid Build Coastguard Worker * This file includes the implementation of OTBR firewall.
33*cfb92d14SAndroid Build Coastguard Worker */
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Worker #include "firewall.hpp"
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
38*cfb92d14SAndroid Build Coastguard Worker
39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/logging.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/netdata.h>
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
43*cfb92d14SAndroid Build Coastguard Worker #include "posix/platform/utils.hpp"
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker namespace ot {
46*cfb92d14SAndroid Build Coastguard Worker namespace Posix {
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Worker #if defined(__linux__) && OPENTHREAD_POSIX_CONFIG_FIREWALL_ENABLE
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Worker #if !OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || !OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
51*cfb92d14SAndroid Build Coastguard Worker #error Configurations 'OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE' and 'OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE' are required.
52*cfb92d14SAndroid Build Coastguard Worker #endif
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard Worker static const char kIpsetCommand[] = OPENTHREAD_POSIX_CONFIG_IPSET_BINARY;
55*cfb92d14SAndroid Build Coastguard Worker static const char kIngressDenySrcIpSet[] = "otbr-ingress-deny-src";
56*cfb92d14SAndroid Build Coastguard Worker static const char kIngressDenySrcSwapIpSet[] = "otbr-ingress-deny-src-swap";
57*cfb92d14SAndroid Build Coastguard Worker static const char kIngressAllowDstIpSet[] = "otbr-ingress-allow-dst";
58*cfb92d14SAndroid Build Coastguard Worker static const char kIngressAllowDstSwapIpSet[] = "otbr-ingress-allow-dst-swap";
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker class IpSetManager
61*cfb92d14SAndroid Build Coastguard Worker {
62*cfb92d14SAndroid Build Coastguard Worker public:
63*cfb92d14SAndroid Build Coastguard Worker otError FlushIpSet(const char *aName);
64*cfb92d14SAndroid Build Coastguard Worker otError AddToIpSet(const char *aSetName, const char *aAddress);
65*cfb92d14SAndroid Build Coastguard Worker otError SwapIpSets(const char *aSetName1, const char *aSetName2);
66*cfb92d14SAndroid Build Coastguard Worker };
67*cfb92d14SAndroid Build Coastguard Worker
FlushIpSet(const char * aName)68*cfb92d14SAndroid Build Coastguard Worker inline otError IpSetManager::FlushIpSet(const char *aName)
69*cfb92d14SAndroid Build Coastguard Worker {
70*cfb92d14SAndroid Build Coastguard Worker return ExecuteCommand("%s flush %s", kIpsetCommand, aName);
71*cfb92d14SAndroid Build Coastguard Worker }
72*cfb92d14SAndroid Build Coastguard Worker
AddToIpSet(const char * aSetName,const char * aAddress)73*cfb92d14SAndroid Build Coastguard Worker inline otError IpSetManager::AddToIpSet(const char *aSetName, const char *aAddress)
74*cfb92d14SAndroid Build Coastguard Worker {
75*cfb92d14SAndroid Build Coastguard Worker return ExecuteCommand("%s add %s %s -exist", kIpsetCommand, aSetName, aAddress);
76*cfb92d14SAndroid Build Coastguard Worker }
77*cfb92d14SAndroid Build Coastguard Worker
SwapIpSets(const char * aSetName1,const char * aSetName2)78*cfb92d14SAndroid Build Coastguard Worker inline otError IpSetManager::SwapIpSets(const char *aSetName1, const char *aSetName2)
79*cfb92d14SAndroid Build Coastguard Worker {
80*cfb92d14SAndroid Build Coastguard Worker return ExecuteCommand("%s swap %s %s", kIpsetCommand, aSetName1, aSetName2);
81*cfb92d14SAndroid Build Coastguard Worker }
82*cfb92d14SAndroid Build Coastguard Worker
UpdateIpSets(otInstance * aInstance)83*cfb92d14SAndroid Build Coastguard Worker void UpdateIpSets(otInstance *aInstance)
84*cfb92d14SAndroid Build Coastguard Worker {
85*cfb92d14SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
86*cfb92d14SAndroid Build Coastguard Worker otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
87*cfb92d14SAndroid Build Coastguard Worker otBorderRouterConfig config;
88*cfb92d14SAndroid Build Coastguard Worker otIp6Prefix prefix;
89*cfb92d14SAndroid Build Coastguard Worker char prefixBuf[OT_IP6_PREFIX_STRING_SIZE];
90*cfb92d14SAndroid Build Coastguard Worker IpSetManager ipSetManager;
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker // 1. Flush the '*-swap' ipsets
93*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.FlushIpSet(kIngressAllowDstSwapIpSet));
94*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.FlushIpSet(kIngressDenySrcSwapIpSet));
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Worker // 2. Update otbr-deny-src-swap
97*cfb92d14SAndroid Build Coastguard Worker while (otNetDataGetNextOnMeshPrefix(aInstance, &iterator, &config) == OT_ERROR_NONE)
98*cfb92d14SAndroid Build Coastguard Worker {
99*cfb92d14SAndroid Build Coastguard Worker if (config.mDp)
100*cfb92d14SAndroid Build Coastguard Worker {
101*cfb92d14SAndroid Build Coastguard Worker continue;
102*cfb92d14SAndroid Build Coastguard Worker }
103*cfb92d14SAndroid Build Coastguard Worker otIp6PrefixToString(&config.mPrefix, prefixBuf, sizeof(prefixBuf));
104*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.AddToIpSet(kIngressDenySrcSwapIpSet, prefixBuf));
105*cfb92d14SAndroid Build Coastguard Worker }
106*cfb92d14SAndroid Build Coastguard Worker memcpy(prefix.mPrefix.mFields.m8, otThreadGetMeshLocalPrefix(aInstance)->m8,
107*cfb92d14SAndroid Build Coastguard Worker sizeof(otThreadGetMeshLocalPrefix(aInstance)->m8));
108*cfb92d14SAndroid Build Coastguard Worker prefix.mLength = OT_IP6_PREFIX_BITSIZE;
109*cfb92d14SAndroid Build Coastguard Worker otIp6PrefixToString(&prefix, prefixBuf, sizeof(prefixBuf));
110*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.AddToIpSet(kIngressDenySrcSwapIpSet, prefixBuf));
111*cfb92d14SAndroid Build Coastguard Worker
112*cfb92d14SAndroid Build Coastguard Worker // 3. Update otbr-allow-dst-swap
113*cfb92d14SAndroid Build Coastguard Worker iterator = OT_NETWORK_DATA_ITERATOR_INIT;
114*cfb92d14SAndroid Build Coastguard Worker while (otNetDataGetNextOnMeshPrefix(aInstance, &iterator, &config) == OT_ERROR_NONE)
115*cfb92d14SAndroid Build Coastguard Worker {
116*cfb92d14SAndroid Build Coastguard Worker otIp6PrefixToString(&config.mPrefix, prefixBuf, sizeof(prefixBuf));
117*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.AddToIpSet(kIngressAllowDstSwapIpSet, prefixBuf));
118*cfb92d14SAndroid Build Coastguard Worker }
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker // 4. Swap ipsets to let them take effect
121*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.SwapIpSets(kIngressDenySrcSwapIpSet, kIngressDenySrcIpSet));
122*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ipSetManager.SwapIpSets(kIngressAllowDstSwapIpSet, kIngressAllowDstIpSet));
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker exit:
125*cfb92d14SAndroid Build Coastguard Worker if (error != OT_ERROR_NONE)
126*cfb92d14SAndroid Build Coastguard Worker {
127*cfb92d14SAndroid Build Coastguard Worker otLogWarnPlat("Firewall - failed to update ipsets: %s", otThreadErrorToString(error));
128*cfb92d14SAndroid Build Coastguard Worker }
129*cfb92d14SAndroid Build Coastguard Worker }
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker #endif // defined(__linux__) && OPENTHREAD_POSIX_CONFIG_FIREWALL_ENABLE
132*cfb92d14SAndroid Build Coastguard Worker
133*cfb92d14SAndroid Build Coastguard Worker } // namespace Posix
134*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
135