@@ -186,6 +186,155 @@ static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
186186 tcf_ct_flow_table_add (ct_ft , ct , tcp );
187187}
188188
189+ static bool
190+ tcf_ct_flow_table_fill_tuple_ipv4 (struct sk_buff * skb ,
191+ struct flow_offload_tuple * tuple )
192+ {
193+ struct flow_ports * ports ;
194+ unsigned int thoff ;
195+ struct iphdr * iph ;
196+
197+ if (!pskb_may_pull (skb , sizeof (* iph )))
198+ return false;
199+
200+ iph = ip_hdr (skb );
201+ thoff = iph -> ihl * 4 ;
202+
203+ if (ip_is_fragment (iph ) ||
204+ unlikely (thoff != sizeof (struct iphdr )))
205+ return false;
206+
207+ if (iph -> protocol != IPPROTO_TCP &&
208+ iph -> protocol != IPPROTO_UDP )
209+ return false;
210+
211+ if (iph -> ttl <= 1 )
212+ return false;
213+
214+ if (!pskb_may_pull (skb , thoff + sizeof (* ports )))
215+ return false;
216+
217+ ports = (struct flow_ports * )(skb_network_header (skb ) + thoff );
218+
219+ tuple -> src_v4 .s_addr = iph -> saddr ;
220+ tuple -> dst_v4 .s_addr = iph -> daddr ;
221+ tuple -> src_port = ports -> source ;
222+ tuple -> dst_port = ports -> dest ;
223+ tuple -> l3proto = AF_INET ;
224+ tuple -> l4proto = iph -> protocol ;
225+
226+ return true;
227+ }
228+
229+ static bool
230+ tcf_ct_flow_table_fill_tuple_ipv6 (struct sk_buff * skb ,
231+ struct flow_offload_tuple * tuple )
232+ {
233+ struct flow_ports * ports ;
234+ struct ipv6hdr * ip6h ;
235+ unsigned int thoff ;
236+
237+ if (!pskb_may_pull (skb , sizeof (* ip6h )))
238+ return false;
239+
240+ ip6h = ipv6_hdr (skb );
241+
242+ if (ip6h -> nexthdr != IPPROTO_TCP &&
243+ ip6h -> nexthdr != IPPROTO_UDP )
244+ return false;
245+
246+ if (ip6h -> hop_limit <= 1 )
247+ return false;
248+
249+ thoff = sizeof (* ip6h );
250+ if (!pskb_may_pull (skb , thoff + sizeof (* ports )))
251+ return false;
252+
253+ ports = (struct flow_ports * )(skb_network_header (skb ) + thoff );
254+
255+ tuple -> src_v6 = ip6h -> saddr ;
256+ tuple -> dst_v6 = ip6h -> daddr ;
257+ tuple -> src_port = ports -> source ;
258+ tuple -> dst_port = ports -> dest ;
259+ tuple -> l3proto = AF_INET6 ;
260+ tuple -> l4proto = ip6h -> nexthdr ;
261+
262+ return true;
263+ }
264+
265+ static bool tcf_ct_flow_table_check_tcp (struct flow_offload * flow ,
266+ struct sk_buff * skb ,
267+ unsigned int thoff )
268+ {
269+ struct tcphdr * tcph ;
270+
271+ if (!pskb_may_pull (skb , thoff + sizeof (* tcph )))
272+ return false;
273+
274+ tcph = (void * )(skb_network_header (skb ) + thoff );
275+ if (unlikely (tcph -> fin || tcph -> rst )) {
276+ flow_offload_teardown (flow );
277+ return false;
278+ }
279+
280+ return true;
281+ }
282+
283+ static bool tcf_ct_flow_table_lookup (struct tcf_ct_params * p ,
284+ struct sk_buff * skb ,
285+ u8 family )
286+ {
287+ struct nf_flowtable * nf_ft = & p -> ct_ft -> nf_ft ;
288+ struct flow_offload_tuple_rhash * tuplehash ;
289+ struct flow_offload_tuple tuple = {};
290+ enum ip_conntrack_info ctinfo ;
291+ struct flow_offload * flow ;
292+ struct nf_conn * ct ;
293+ unsigned int thoff ;
294+ int ip_proto ;
295+ u8 dir ;
296+
297+ /* Previously seen or loopback */
298+ ct = nf_ct_get (skb , & ctinfo );
299+ if ((ct && !nf_ct_is_template (ct )) || ctinfo == IP_CT_UNTRACKED )
300+ return false;
301+
302+ switch (family ) {
303+ case NFPROTO_IPV4 :
304+ if (!tcf_ct_flow_table_fill_tuple_ipv4 (skb , & tuple ))
305+ return false;
306+ break ;
307+ case NFPROTO_IPV6 :
308+ if (!tcf_ct_flow_table_fill_tuple_ipv6 (skb , & tuple ))
309+ return false;
310+ break ;
311+ default :
312+ return false;
313+ }
314+
315+ tuplehash = flow_offload_lookup (nf_ft , & tuple );
316+ if (!tuplehash )
317+ return false;
318+
319+ dir = tuplehash -> tuple .dir ;
320+ flow = container_of (tuplehash , struct flow_offload , tuplehash [dir ]);
321+ ct = flow -> ct ;
322+
323+ ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
324+ IP_CT_ESTABLISHED_REPLY ;
325+
326+ thoff = ip_hdr (skb )-> ihl * 4 ;
327+ ip_proto = ip_hdr (skb )-> protocol ;
328+ if (ip_proto == IPPROTO_TCP &&
329+ !tcf_ct_flow_table_check_tcp (flow , skb , thoff ))
330+ return false;
331+
332+ nf_conntrack_get (& ct -> ct_general );
333+ nf_ct_set (skb , ct , ctinfo );
334+
335+ return true;
336+ }
337+
189338static int tcf_ct_flow_tables_init (void )
190339{
191340 return rhashtable_init (& zones_ht , & zones_params );
@@ -554,6 +703,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
554703 struct nf_hook_state state ;
555704 int nh_ofs , err , retval ;
556705 struct tcf_ct_params * p ;
706+ bool skip_add = false;
557707 struct nf_conn * ct ;
558708 u8 family ;
559709
@@ -603,6 +753,11 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
603753 */
604754 cached = tcf_ct_skb_nfct_cached (net , skb , p -> zone , force );
605755 if (!cached ) {
756+ if (!commit && tcf_ct_flow_table_lookup (p , skb , family )) {
757+ skip_add = true;
758+ goto do_nat ;
759+ }
760+
606761 /* Associate skb with specified zone. */
607762 if (tmpl ) {
608763 ct = nf_ct_get (skb , & ctinfo );
@@ -620,6 +775,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
620775 goto out_push ;
621776 }
622777
778+ do_nat :
623779 ct = nf_ct_get (skb , & ctinfo );
624780 if (!ct )
625781 goto out_push ;
@@ -637,10 +793,10 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
637793 * even if the connection is already confirmed.
638794 */
639795 nf_conntrack_confirm (skb );
796+ } else if (!skip_add ) {
797+ tcf_ct_flow_table_process_conn (p -> ct_ft , ct , ctinfo );
640798 }
641799
642- tcf_ct_flow_table_process_conn (p -> ct_ft , ct , ctinfo );
643-
644800out_push :
645801 skb_push_rcsum (skb , nh_ofs );
646802
0 commit comments