@@ -33,15 +33,29 @@ static int ipt4_iface_setup(void)
3333 char iface_str [IFNAMSIZ ];
3434 size_t i ;
3535 int res ;
36- char * ipt_alliface_cmd [] = {"iptables" , "-w" , "-t" , "mangle" , "-A" ,
37- "FAKEHTTP" , "-j" , "FAKEHTTP_R" , NULL };
36+ char * ipt_alliface_src_cmd [] = {"iptables" , "-w" , "-t" ,
37+ "mangle" , "-A" , "FAKEHTTP_S" ,
38+ "-j" , "FAKEHTTP_R" , NULL };
3839
39- char * ipt_iface_cmd [] = {"iptables" , "-w" , "-t" , "mangle" ,
40- "-A" , "FAKEHTTP" , "-i" , iface_str ,
41- "-j" , "FAKEHTTP_R" , NULL };
40+ char * ipt_alliface_dst_cmd [] = {"iptables" , "-w" , "-t" ,
41+ "mangle" , "-A" , "FAKEHTTP_D" ,
42+ "-j" , "FAKEHTTP_R" , NULL };
43+
44+ char * ipt_iface_src_cmd [] = {"iptables" , "-w" , "-t" , "mangle" ,
45+ "-A" , "FAKEHTTP_S" , "-i" , iface_str ,
46+ "-j" , "FAKEHTTP_R" , NULL };
47+
48+ char * ipt_iface_dst_cmd [] = {"iptables" , "-w" , "-t" , "mangle" ,
49+ "-A" , "FAKEHTTP_D" , "-o" , iface_str ,
50+ "-j" , "FAKEHTTP_R" , NULL };
4251
4352 if (g_ctx .alliface ) {
44- res = fh_execute_command (ipt_alliface_cmd , 0 , NULL );
53+ res = fh_execute_command (ipt_alliface_src_cmd , 0 , NULL );
54+ if (res < 0 ) {
55+ E (T (fh_execute_command ));
56+ return -1 ;
57+ }
58+ res = fh_execute_command (ipt_alliface_dst_cmd , 0 , NULL );
4559 if (res < 0 ) {
4660 E (T (fh_execute_command ));
4761 return -1 ;
@@ -56,7 +70,13 @@ static int ipt4_iface_setup(void)
5670 return -1 ;
5771 }
5872
59- res = fh_execute_command (ipt_iface_cmd , 0 , NULL );
73+ res = fh_execute_command (ipt_iface_src_cmd , 0 , NULL );
74+ if (res < 0 ) {
75+ E (T (fh_execute_command ));
76+ return -1 ;
77+ }
78+
79+ res = fh_execute_command (ipt_iface_dst_cmd , 0 , NULL );
6080 if (res < 0 ) {
6181 E (T (fh_execute_command ));
6282 return -1 ;
@@ -72,59 +92,91 @@ int fh_ipt4_setup(void)
7292 size_t i , ipt_cmds_cnt , ipt_opt_cmds_cnt ;
7393 int res ;
7494 char * ipt_cmds [][32 ] = {
75- {"iptables" , "-w" , "-t" , "mangle" , "-N" , "FAKEHTTP" , NULL },
95+ {"iptables" , "-w" , "-t" , "mangle" , "-N" , "FAKEHTTP_S" , NULL },
96+
97+ {"iptables" , "-w" , "-t" , "mangle" , "-N" , "FAKEHTTP_D" , NULL },
7698
7799 {"iptables" , "-w" , "-t" , "mangle" , "-I" , "PREROUTING" , "-j" ,
78- "FAKEHTTP" , NULL },
100+ "FAKEHTTP_S" , NULL },
101+
102+ {"iptables" , "-w" , "-t" , "mangle" , "-I" , "POSTROUTING" , "-j" ,
103+ "FAKEHTTP_D" , NULL },
79104
80105 {"iptables" , "-w" , "-t" , "mangle" , "-N" , "FAKEHTTP_R" , NULL },
81106
82107 /*
83- exclude marked packets
108+ exclude local IPs (from source)
84109 */
85- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" , "mark" ,
86- "--mark" , xmark_str , "-j" , "CONNMARK" , "--set-xmark" , xmark_str ,
87- NULL },
110+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
111+ "0.0.0.0/8" , "-j" , "RETURN" , NULL },
88112
89- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" ,
90- "connmark" , "--mark" , xmark_str , "-j" , "MARK" , "--set-xmark" ,
91- xmark_str , NULL },
113+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
114+ "10.0.0.0/8" , "-j" , "RETURN" , NULL },
92115
93- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" , "mark" ,
94- "--mark" , xmark_str , "-j" , "RETURN" , NULL },
116+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
117+ "100.64.0.0/10" , "-j" , "RETURN" , NULL },
118+
119+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
120+ "127.0.0.0/8" , "-j" , "RETURN" , NULL },
121+
122+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
123+ "169.254.0.0/16" , "-j" , "RETURN" , NULL },
124+
125+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
126+ "172.16.0.0/12" , "-j" , "RETURN" , NULL },
127+
128+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
129+ "192.168.0.0/16" , "-j" , "RETURN" , NULL },
130+
131+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_S" , "-s" ,
132+ "224.0.0.0/3" , "-j" , "RETURN" , NULL },
95133
96134 /*
97- exclude local IPs
135+ exclude local IPs (to destination)
98136 */
99- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
137+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
100138 "0.0.0.0/8" , "-j" , "RETURN" , NULL },
101139
102- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
140+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
103141 "10.0.0.0/8" , "-j" , "RETURN" , NULL },
104142
105- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
143+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
106144 "100.64.0.0/10" , "-j" , "RETURN" , NULL },
107145
108- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
146+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
109147 "127.0.0.0/8" , "-j" , "RETURN" , NULL },
110148
111- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
149+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
112150 "169.254.0.0/16" , "-j" , "RETURN" , NULL },
113151
114- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
152+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
115153 "172.16.0.0/12" , "-j" , "RETURN" , NULL },
116154
117- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
155+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
118156 "192.168.0.0/16" , "-j" , "RETURN" , NULL },
119157
120- {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R " , "-s " ,
158+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_D " , "-d " ,
121159 "224.0.0.0/3" , "-j" , "RETURN" , NULL },
122160
161+ /*
162+ exclude marked packets
163+ */
164+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" , "mark" ,
165+ "--mark" , xmark_str , "-j" , "CONNMARK" , "--set-xmark" , xmark_str ,
166+ NULL },
167+
168+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" ,
169+ "connmark" , "--mark" , xmark_str , "-j" , "MARK" , "--set-xmark" ,
170+ xmark_str , NULL },
171+
172+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" , "mark" ,
173+ "--mark" , xmark_str , "-j" , "RETURN" , NULL },
174+
123175 /*
124176 send to nfqueue
125177 */
126178 {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-p" , "tcp" ,
127- "--tcp-flags" , "ACK ,FIN,RST" , "ACK " , "-j" , "NFQUEUE" ,
179+ "--tcp-flags" , "SYN ,FIN,RST" , "SYN " , "-j" , "NFQUEUE" ,
128180 "--queue-bypass" , "--queue-num" , nfqnum_str , NULL }};
129181
130182 char * ipt_opt_cmds [][32 ] = {
@@ -187,26 +239,21 @@ void fh_ipt4_cleanup(void)
187239 char * ipt_cmds [][32 ] = {
188240 {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_R" , NULL },
189241
190- {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP" , NULL },
191-
192- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "PREROUTING" , "-j" ,
193- "FAKEHTTP" , NULL },
194-
195- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "INPUT" , "-j" , "FAKEHTTP" ,
196- NULL },
242+ {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_S" , NULL },
197243
198- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "FORWARD" , "-j" , "FAKEHTTP" ,
199- NULL },
244+ {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_D" , NULL },
200245
201- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "OUTPUT " , "-j" , "FAKEHTTP " ,
202- NULL },
246+ {"iptables" , "-w" , "-t" , "mangle" , "-D" , "PREROUTING " , "-j" ,
247+ "FAKEHTTP_S" , NULL },
203248
204249 {"iptables" , "-w" , "-t" , "mangle" , "-D" , "POSTROUTING" , "-j" ,
205- "FAKEHTTP " , NULL },
250+ "FAKEHTTP_D " , NULL },
206251
207252 {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_R" , NULL },
208253
209- {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP" , NULL }};
254+ {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_S" , NULL },
255+
256+ {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_D" , NULL }};
210257
211258 cnt = sizeof (ipt_cmds ) / sizeof (* ipt_cmds );
212259 for (i = 0 ; i < cnt ; i ++ ) {
0 commit comments