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