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 30 #ifndef IPACM_CONNTRACK_LISTENER 31 #define IPACM_CONNTRACK_LISTENER 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <fcntl.h> 37 38 #include <arpa/inet.h> 39 #include <netinet/in.h> 40 #include <errno.h> 41 42 #include "IPACM_CmdQueue.h" 43 #include "IPACM_Conntrack_NATApp.h" 44 #include "IPACM_Listener.h" 45 #ifdef CT_OPT 46 #include "IPACM_LanToLan.h" 47 #endif 48 49 #define MAX_IFACE_ADDRESS 50 50 #define MAX_STA_CLNT_IFACES 10 51 #define STA_CLNT_SUBNET_MASK 0xFFFFFF00 52 53 using namespace std; 54 55 typedef struct _nat_entry_bundle 56 { 57 struct nf_conntrack *ct; 58 enum nf_conntrack_msg_type type; 59 nat_table_entry *rule; 60 bool isTempEntry; 61 62 }nat_entry_bundle; 63 64 typedef struct _ct_entry 65 { 66 struct nf_conntrack *ct; 67 u_int8_t protocol; 68 enum nf_conntrack_msg_type type; 69 }ct_entry; 70 71 class IPACM_ConntrackListener : public IPACM_Listener 72 { 73 74 private: 75 bool isCTReg; 76 bool isNatThreadStart; 77 bool WanUp; 78 bool isProcessCTDone; 79 NatApp *nat_inst; 80 81 int NatIfaceCnt; 82 int StaClntCnt; 83 NatIfaces *pNatIfaces; 84 uint32_t nat_iface_ipv4_addr[MAX_IFACE_ADDRESS]; 85 uint32_t nonnat_iface_ipv4_addr[MAX_IFACE_ADDRESS]; 86 uint32_t sta_clnt_ipv4_addr[MAX_STA_CLNT_IFACES]; 87 IPACM_Config *pConfig; 88 ct_entry *ct_entries; 89 ct_entry ct_cache[MAX_CONNTRACK_ENTRIES]; 90 #ifdef CT_OPT 91 IPACM_LanToLan *p_lan2lan; 92 #endif 93 94 void ProcessCTMessage(void *); 95 bool ProcessTCPorUDPMsg(struct nf_conntrack *, 96 enum nf_conntrack_msg_type, u_int8_t); 97 void TriggerWANUp(void *); 98 void TriggerWANDown(uint32_t); 99 int CreateNatThreads(void); 100 bool AddIface(nat_table_entry *, bool *); 101 void AddORDeleteNatEntry(const nat_entry_bundle *); 102 void PopulateTCPorUDPEntry(struct nf_conntrack *, uint32_t, nat_table_entry *); 103 void CheckSTAClient(const nat_table_entry *, bool *); 104 int CheckNatIface(ipacm_event_data_all *, bool *); 105 void HandleNonNatIPAddr(void *, bool); 106 107 #ifdef CT_OPT 108 void ProcessCTV6Message(void *); 109 void HandleLan2Lan(struct nf_conntrack *, 110 enum nf_conntrack_msg_type, nat_table_entry* ); 111 #endif 112 113 public: 114 char wan_ifname[IPA_IFACE_NAME_LEN]; 115 uint32_t wan_ipaddr; 116 ipacm_wan_iface_type backhaul_mode; 117 bool isReadCTDone; 118 IPACM_ConntrackListener(); 119 void event_callback(ipa_cm_event_id, void *data); isWanUp()120 inline bool isWanUp() 121 { 122 return WanUp; 123 } 124 125 void HandleNeighIpAddrAddEvt(ipacm_event_data_all *); 126 void HandleNeighIpAddrDelEvt(uint32_t); 127 void HandleSTAClientAddEvt(uint32_t); 128 void HandleSTAClientDelEvt(uint32_t); 129 int CreateConnTrackThreads(void); 130 void readConntrack(int fd); 131 void processConntrack(void); 132 void CacheORDeleteConntrack(struct nf_conntrack *ct, 133 enum nf_conntrack_msg_type type, u_int8_t protocol); 134 void processCacheConntrack(void); 135 }; 136 137 extern IPACM_ConntrackListener *CtList; 138 139 #endif /* IPACM_CONNTRACK_LISTENER */ 140