1 /*
2 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef DBG
30 #define DBG false
31 #endif /* DBG */
32 #define LOG_TAG "IPAHALService/CtUpdateAmbassador"
33
34 /* External Includes */
35 #include <arpa/inet.h>
36 #include <cutils/log.h>
37
38 /* HIDL Includes */
39 #include <android/hardware/tetheroffload/control/1.0/ITetheringOffloadCallback.h>
40
41 /* Internal Includes */
42 #include "CtUpdateAmbassador.h"
43
44 /* Namespace pollution avoidance */
45 using ::android::hardware::tetheroffload::control::V1_0::ITetheringOffloadCallback;
46 using ::android::hardware::tetheroffload::control::V1_0::NetworkProtocol;
47 using HALIpAddrPortPair = ::android::hardware::tetheroffload::control::V1_0::IPv4AddrPortPair;
48 using HALNatTimeoutUpdate = ::android::hardware::tetheroffload::control::V1_0::NatTimeoutUpdate;
49
50 using IpaIpAddrPortPair = ::IOffloadManager::ConntrackTimeoutUpdater::IpAddrPortPair;
51 using IpaNatTimeoutUpdate = ::IOffloadManager::ConntrackTimeoutUpdater::NatTimeoutUpdate;
52 using IpaL4Protocol = ::IOffloadManager::ConntrackTimeoutUpdater::L4Protocol;
53
54
CtUpdateAmbassador(const::android::sp<ITetheringOffloadCallback> & cb)55 CtUpdateAmbassador::CtUpdateAmbassador(
56 const ::android::sp<ITetheringOffloadCallback>& cb) : mFramework(cb) {
57 } /* CtUpdateAmbassador */
58
updateTimeout(IpaNatTimeoutUpdate in)59 void CtUpdateAmbassador::updateTimeout(IpaNatTimeoutUpdate in) {
60 if (DBG) {
61 ALOGD("updateTimeout(src={%#010X, %#04X}, dest={%#010X, %#04X}, Proto=%d)",
62 in.src.ipAddr, in.src.port, in.dst.ipAddr, in.dst.port,
63 in.proto);
64 }
65 HALNatTimeoutUpdate out;
66 if (!translate(in, out)) {
67 /* Cannot log the input outside of DBG flag because it contains sensitive
68 * information. This will lead to a two step debug if the information
69 * cannot be gleaned from IPACM logs. The other option is to improve this
70 * with the use of our local log. That would likely still be hard to
71 * instruct testers to collect logs, because, assuming timeout updates
72 * are numerous, it will overrun the ring quickly. Therefore, the tester
73 * would have to know the exact moment as issue occurred. Or we make the
74 * ring massive. This would lead to a significant memory overhead.
75 * Because of this overhead, we would likely not want to check in a change
76 * with it and once we provide a debug build for increasing buffer size,
77 * why not just define the DBG flag?
78 */
79 ALOGE("Failed to translate timeout event :(");
80 } else {
81 mFramework->updateTimeout(out);
82 }
83 } /* updateTimeout */
84
translate(IpaNatTimeoutUpdate in,HALNatTimeoutUpdate & out)85 bool CtUpdateAmbassador::translate(IpaNatTimeoutUpdate in, HALNatTimeoutUpdate &out) {
86 return translate(in.src, out.src)
87 && translate(in.dst, out.dst)
88 && L4ToNetwork(in.proto, out.proto);
89 } /* translate */
90
translate(IpaIpAddrPortPair in,HALIpAddrPortPair & out)91 bool CtUpdateAmbassador::translate(IpaIpAddrPortPair in, HALIpAddrPortPair& out) {
92 char ipAddrStr[INET_ADDRSTRLEN];
93
94 if (inet_ntop(AF_INET, &(in.ipAddr), ipAddrStr, INET_ADDRSTRLEN) == nullptr) {
95 /* errno would be valid here with EAFNOSUPPORT or ENOSPC, neither should really
96 * be possible in our scenario though.
97 */
98 return false;
99 }
100
101 out.addr = ipAddrStr;
102 out.port = in.port;
103
104 return true;
105 } /* translate */
106
L4ToNetwork(IpaL4Protocol in,NetworkProtocol & out)107 bool CtUpdateAmbassador::L4ToNetwork(IpaL4Protocol in, NetworkProtocol &out) {
108 bool ret = false;
109 switch(in) {
110 case IpaL4Protocol::TCP:
111 ret = true;
112 out = NetworkProtocol::TCP;
113 break;
114 case IpaL4Protocol::UDP:
115 ret = true;
116 out = NetworkProtocol::UDP;
117 break;
118 default:
119 ret = false;
120 break;
121 }
122 return ret;
123 } /* L4ToNetwork */
124