@@ -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,74 +92,105 @@ 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+
165+ {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-m" , "mark" ,
166+ "--mark" , xmark_str , "-j" , "RETURN" , NULL },
167+
123168 /*
124169 send to nfqueue
125170 */
126171 {"iptables" , "-w" , "-t" , "mangle" , "-A" , "FAKEHTTP_R" , "-p" , "tcp" ,
127- "--tcp-flags" , "ACK ,FIN,RST" , "ACK " , "-j" , "NFQUEUE" ,
172+ "--tcp-flags" , "SYN ,FIN,RST" , "SYN " , "-j" , "NFQUEUE" ,
128173 "--queue-bypass" , "--queue-num" , nfqnum_str , NULL }};
129174
130175 char * ipt_opt_cmds [][32 ] = {
131176 /*
132- exclude packets from connections with more than 32 packets
133- */
134- {"iptables" , "-w" , "-t" , "mangle" , "-I" , "FAKEHTTP_R" , "-m" ,
135- "connbytes" , "!" , "--connbytes" , "0:32" , "--connbytes-dir" , "both" ,
136- "--connbytes-mode" , "packets" , "-j" , "RETURN" , NULL },
137-
138- /*
139- exclude big packets
177+ Also enqueue some of the early ACK packets to ensure the packet
178+ order. This rule is optional. We do not verify its execution
179+ result.
140180 */
141- {"iptables" , "-w" , "-t" , "mangle" , "-I" , "FAKEHTTP_R" , "-m" , "length" ,
142- "!" , "--length" , "0:120" , "-j" , "RETURN" , NULL }};
181+ {"iptables" , "-w" ,
182+ "-t" , "mangle" ,
183+ "-A" , "FAKEHTTP_R" ,
184+ "-p" , "tcp" ,
185+ "--tcp-flags" , "SYN,ACK,FIN,RST" ,
186+ "ACK" , "-m" ,
187+ "connbytes" , "--connbytes" ,
188+ "2:4" , "--connbytes-dir" ,
189+ "both" , "--connbytes-mode" ,
190+ "packets" , "-j" ,
191+ "NFQUEUE" , "--queue-bypass" ,
192+ "--queue-num" , nfqnum_str ,
193+ NULL }};
143194
144195 ipt_cmds_cnt = sizeof (ipt_cmds ) / sizeof (* ipt_cmds );
145196 ipt_opt_cmds_cnt = sizeof (ipt_opt_cmds ) / sizeof (* ipt_opt_cmds );
@@ -187,26 +238,21 @@ void fh_ipt4_cleanup(void)
187238 char * ipt_cmds [][32 ] = {
188239 {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_R" , NULL },
189240
190- {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP " , NULL },
241+ {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_S " , NULL },
191242
192- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "PREROUTING" , "-j" ,
193- "FAKEHTTP" , NULL },
194-
195- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "INPUT" , "-j" , "FAKEHTTP" ,
196- NULL },
197-
198- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "FORWARD" , "-j" , "FAKEHTTP" ,
199- NULL },
243+ {"iptables" , "-w" , "-t" , "mangle" , "-F" , "FAKEHTTP_D" , NULL },
200244
201- {"iptables" , "-w" , "-t" , "mangle" , "-D" , "OUTPUT " , "-j" , "FAKEHTTP " ,
202- NULL },
245+ {"iptables" , "-w" , "-t" , "mangle" , "-D" , "PREROUTING " , "-j" ,
246+ "FAKEHTTP_S" , NULL },
203247
204248 {"iptables" , "-w" , "-t" , "mangle" , "-D" , "POSTROUTING" , "-j" ,
205- "FAKEHTTP " , NULL },
249+ "FAKEHTTP_D " , NULL },
206250
207251 {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_R" , NULL },
208252
209- {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP" , NULL }};
253+ {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_S" , NULL },
254+
255+ {"iptables" , "-w" , "-t" , "mangle" , "-X" , "FAKEHTTP_D" , NULL }};
210256
211257 cnt = sizeof (ipt_cmds ) / sizeof (* ipt_cmds );
212258 for (i = 0 ; i < cnt ; i ++ ) {
0 commit comments