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 
     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 class IPACM_ConntrackListener : public IPACM_Listener
     65 {
     66 
     67 private:
     68 	bool isCTReg;
     69 	bool isNatThreadStart;
     70 	bool WanUp;
     71 	NatApp *nat_inst;
     72 
     73 	int NatIfaceCnt;
     74 	int StaClntCnt;
     75 	NatIfaces *pNatIfaces;
     76 	uint32_t nat_iface_ipv4_addr[MAX_IFACE_ADDRESS];
     77 	uint32_t nonnat_iface_ipv4_addr[MAX_IFACE_ADDRESS];
     78 	uint32_t sta_clnt_ipv4_addr[MAX_STA_CLNT_IFACES];
     79 	IPACM_Config *pConfig;
     80 #ifdef CT_OPT
     81 	IPACM_LanToLan *p_lan2lan;
     82 #endif
     83 
     84 	void ProcessCTMessage(void *);
     85 	void ProcessTCPorUDPMsg(struct nf_conntrack *,
     86 	enum nf_conntrack_msg_type, u_int8_t);
     87 	void TriggerWANUp(void *);
     88 	void TriggerWANDown(uint32_t);
     89 	int  CreateNatThreads(void);
     90 	bool AddIface(nat_table_entry *, bool *);
     91 	void AddORDeleteNatEntry(const nat_entry_bundle *);
     92 	void PopulateTCPorUDPEntry(struct nf_conntrack *, uint32_t, nat_table_entry *);
     93 	void CheckSTAClient(const nat_table_entry *, bool *);
     94 	int CheckNatIface(ipacm_event_data_all *, bool *);
     95 	void HandleNonNatIPAddr(void *, bool);
     96 
     97 #ifdef CT_OPT
     98 	void ProcessCTV6Message(void *);
     99 	void HandleLan2Lan(struct nf_conntrack *,
    100 		enum nf_conntrack_msg_type, nat_table_entry* );
    101 #endif
    102 
    103 public:
    104 	char wan_ifname[IPA_IFACE_NAME_LEN];
    105 	uint32_t wan_ipaddr;
    106 	bool isStaMode;
    107 	IPACM_ConntrackListener();
    108 	void event_callback(ipa_cm_event_id, void *data);
    109 	inline bool isWanUp()
    110 	{
    111 		return WanUp;
    112 	}
    113 
    114 	void HandleNeighIpAddrAddEvt(ipacm_event_data_all *);
    115 	void HandleNeighIpAddrDelEvt(uint32_t);
    116 	void HandleSTAClientAddEvt(uint32_t);
    117 	void HandleSTAClientDelEvt(uint32_t);
    118 	int  CreateConnTrackThreads(void);
    119 };
    120 
    121 extern IPACM_ConntrackListener *CtList;
    122 
    123 #endif /* IPACM_CONNTRACK_LISTENER */
    124