1 /*
2 Copyright (c) 2013-2019, 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 IPACM_CONNTRACK_NATAPP_H
30 #define IPACM_CONNTRACK_NATAPP_H
31 
32 #include <string.h>  /* for stderror */
33 #include <stdlib.h>
34 #include <cstdio>  /* for perror */
35 
36 #include "IPACM_Config.h"
37 #include "IPACM_Xml.h"
38 
39 extern "C"
40 {
41 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
42 #include <ipa_nat_drv.h>
43 }
44 
45 #define MAX_TEMP_ENTRIES 25
46 
47 #define IPACM_TCP_FULL_FILE_NAME  "/proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established"
48 #define IPACM_UDP_FULL_FILE_NAME   "/proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout_stream"
49 
50 typedef struct _nat_table_entry
51 {
52 	uint32_t private_ip;
53 	uint16_t private_port;
54 
55 	uint32_t target_ip;
56 	uint16_t target_port;
57 
58 	uint32_t public_ip;
59 	uint16_t public_port;
60 
61 	u_int8_t  protocol;
62 	uint32_t timestamp;
63 
64 	bool dst_nat;
65 	bool enabled;
66 	uint32_t rule_hdl;
67 
68 	/* used for pcie-modem */
69 	uint32_t rule_id;
70 }nat_table_entry;
71 
72 #define CHK_TBL_HDL()  if(nat_table_hdl == 0){ return -1; }
73 
74 class NatApp
75 {
76 private:
77 
78 	static NatApp *pInstance;
79 
80 	nat_table_entry *cache;
81 	nat_table_entry temp[MAX_TEMP_ENTRIES];
82 	uint32_t pub_ip_addr;
83 	uint32_t pub_ip_addr_pre;
84 	uint32_t nat_table_hdl;
85 	/* used for pcie-modem */
86 	uint8_t pub_mux_id;
87 
88 	int curCnt, max_entries;
89 
90 	ipacm_alg *pALGPorts;
91 	uint16_t nALGPort;
92 
93 	uint32_t tcp_timeout;
94 	uint32_t udp_timeout;
95 
96 	uint32_t PwrSaveIfs[IPA_MAX_NUM_WIFI_CLIENTS];
97 
98 	struct nf_conntrack *ct;
99 	struct nfct_handle *ct_hdl;
100 
101 	int m_fd_ipa;
102 
103 	NatApp();
104 	~NatApp();
105 	int Init();
106 
107 	void UpdateCTUdpTs(nat_table_entry *, uint32_t);
108 	bool ChkForDup(const nat_table_entry *);
109 	bool isAlgPort(uint8_t, uint16_t);
110 	void Reset();
111 	bool isPwrSaveIf(uint32_t);
112 	uint32_t GenerateMetdata(uint8_t mux_id);
113 
114 public:
115 	static NatApp* GetInstance();
116 
117 	int AddTable(uint32_t, uint8_t mux_id);
118 	uint32_t GetTableHdl(uint32_t);
119 	int DeleteTable(uint32_t);
120 
121 	int AddEntry(const nat_table_entry *);
122 	int DeleteEntry(const nat_table_entry *);
123 
124 	int AddConnection(const nat_table_entry *);
125 	int DelConnection(const uint32_t);
126 
127 	void UpdateUDPTimeStamp();
128 
129 	int UpdatePwrSaveIf(uint32_t);
130 	int ResetPwrSaveIf(uint32_t);
131 	int DelEntriesOnClntDiscon(uint32_t);
132 	int DelEntriesOnSTAClntDiscon(uint32_t);
133 
134 	void Read_TcpUdp_Timeout(void);
135 
136 	void AddTempEntry(const nat_table_entry *);
137 	void CacheEntry(const nat_table_entry *);
138 	void DeleteTempEntry(const nat_table_entry *);
139 	void FlushTempEntries(uint32_t, bool, bool isDummy = false);
140 };
141 
142 
143 
144 #endif /* IPACM_CONNTRACK_NATAPP_H */
145