1 /* 2 Copyright (c) 2013, 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_FILTER_H 31 #define IPACM_CONNTRACK_FILTER_H 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <arpa/inet.h> 37 #include <netinet/in.h> 38 #include <errno.h> 39 40 #include "IPACM_ConntrackClient.h" 41 #include "IPACM_CmdQueue.h" 42 #include "IPACM_Conntrack_NATApp.h" 43 #include "IPACM_EvtDispatcher.h" 44 #include "IPACM_Defs.h" 45 46 #ifndef IPACM_DEBUG 47 #define IPACM_DEBUG 48 #endif 49 50 extern "C" 51 { 52 #include <libnetfilter_conntrack/libnetfilter_conntrack.h> 53 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h> 54 #include <sys/inotify.h> 55 } 56 57 using namespace std; 58 59 #define UDP_TIMEOUT_UPDATE 20 60 #define BROADCAST_IPV4_ADDR 0xFFFFFFFF 61 62 class IPACM_ConntrackClient 63 { 64 65 private: 66 static IPACM_ConntrackClient *pInstance; 67 68 struct nfct_handle *tcp_hdl; 69 struct nfct_handle *udp_hdl; 70 struct nfct_filter *tcp_filter; 71 struct nfct_filter *udp_filter; 72 static int IPA_Conntrack_Filters_Ignore_Local_Addrs(struct nfct_filter *filter); 73 static int IPA_Conntrack_Filters_Ignore_Bridge_Addrs(struct nfct_filter *filter); 74 static int IPA_Conntrack_Filters_Ignore_Local_Iface(struct nfct_filter *, ipacm_event_iface_up *); 75 IPACM_ConntrackClient(); 76 77 public: 78 static int IPAConntrackEventCB(enum nf_conntrack_msg_type type, 79 struct nf_conntrack *ct, 80 void *data); 81 82 static int IPA_Conntrack_UDP_Filter_Init(void); 83 static int IPA_Conntrack_TCP_Filter_Init(void); 84 static void* TCPRegisterWithConnTrack(void *); 85 static void* UDPRegisterWithConnTrack(void *); 86 static void* UDPConnTimeoutUpdate(void *); 87 88 static void UpdateUDPFilters(void *, bool); 89 static void UpdateTCPFilters(void *, bool); 90 static void Read_TcpUdp_Timeout(char *in, int len); 91 92 static IPACM_ConntrackClient* GetInstance(); 93 94 static void UNRegisterWithConnTrack(void); 95 int fd_tcp; 96 int fd_udp; 97 98 unsigned int subscrips_tcp; 99 unsigned int subscrips_udp; 100 101 #ifdef IPACM_DEBUG 102 #define iptodot(X,Y) \ 103 IPACMLOG(" %s(0x%x): %d.%d.%d.%d\n", X, Y, ((Y>>24) & 0xFF), ((Y>>16) & 0xFF), ((Y>>8) & 0xFF), (Y & 0xFF)); 104 #endif 105 106 #define log_nat(A,B,C,D,E,F) \ 107 IPACMDBG_H("protocol %d Private IP: %d.%d.%d.%d\t Target IP: %d.%d.%d.%d\t private port: %d public port: %d %s",A,((B>>24) & 0xFF), ((B>>16) & 0xFF), ((B>>8) & 0xFF), (B & 0xFF), ((C>>24) & 0xFF), ((C>>16) & 0xFF),((C>>8) & 0xFF),(C & 0xFF),D,E,F); 108 109 }; 110 111 #endif /* IPACM_CONNTRACK_FILTER_H */ 112