Home | History | Annotate | Download | only in inc
      1 /*
      2 Copyright (c) 2013-2016, 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 }nat_table_entry;
     69 
     70 #define CHK_TBL_HDL()  if(nat_table_hdl == 0){ return -1; }
     71 
     72 class NatApp
     73 {
     74 private:
     75 
     76 	static NatApp *pInstance;
     77 
     78 	nat_table_entry *cache;
     79 	nat_table_entry temp[MAX_TEMP_ENTRIES];
     80 	uint32_t pub_ip_addr;
     81 	uint32_t pub_ip_addr_pre;
     82 	uint32_t nat_table_hdl;
     83 
     84 	int curCnt, max_entries;
     85 
     86 	ipacm_alg *pALGPorts;
     87 	uint16_t nALGPort;
     88 
     89 	uint32_t tcp_timeout;
     90 	uint32_t udp_timeout;
     91 
     92 	uint32_t PwrSaveIfs[IPA_MAX_NUM_WIFI_CLIENTS];
     93 
     94 	struct nf_conntrack *ct;
     95 	struct nfct_handle *ct_hdl;
     96 
     97 	NatApp();
     98 	int Init();
     99 
    100 	void UpdateCTUdpTs(nat_table_entry *, uint32_t);
    101 	bool ChkForDup(const nat_table_entry *);
    102 	bool isAlgPort(uint8_t, uint16_t);
    103 	void Reset();
    104 	bool isPwrSaveIf(uint32_t);
    105 
    106 public:
    107 	static NatApp* GetInstance();
    108 
    109 	int AddTable(uint32_t);
    110 	uint32_t GetTableHdl(uint32_t);
    111 	int DeleteTable(uint32_t);
    112 
    113 	int AddEntry(const nat_table_entry *);
    114 	int DeleteEntry(const nat_table_entry *);
    115 
    116 	void UpdateUDPTimeStamp();
    117 
    118 	int UpdatePwrSaveIf(uint32_t);
    119 	int ResetPwrSaveIf(uint32_t);
    120 	int DelEntriesOnClntDiscon(uint32_t);
    121 	int DelEntriesOnSTAClntDiscon(uint32_t);
    122 
    123 	void Read_TcpUdp_Timeout(void);
    124 
    125 	void AddTempEntry(const nat_table_entry *);
    126 	void CacheEntry(const nat_table_entry *);
    127 	void DeleteTempEntry(const nat_table_entry *);
    128 	void FlushTempEntries(uint32_t, bool, bool isDummy = false);
    129 };
    130 
    131 
    132 
    133 #endif /* IPACM_CONNTRACK_NATAPP_H */
    134