blob: aba3cd85f284f3e49add31fe65e3b791f2386fa1 [file] [log] [blame]
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/* -
3 * net/sched/act_ct.c Connection Tracking action
4 *
5 * Authors: Paul Blakey <paulb@mellanox.com>
6 * Yossi Kuperman <yossiku@mellanox.com>
7 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/rtnetlink.h>
15#include <linux/pkt_cls.h>
16#include <linux/ip.h>
17#include <linux/ipv6.h>
Paul Blakeyc34b9612020-03-03 15:07:49 +020018#include <linux/rhashtable.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030019#include <net/netlink.h>
20#include <net/pkt_sched.h>
21#include <net/pkt_cls.h>
22#include <net/act_api.h>
23#include <net/ip.h>
24#include <net/ipv6_frag.h>
25#include <uapi/linux/tc_act/tc_ct.h>
26#include <net/tc_act/tc_ct.h>
27
Paul Blakeyc34b9612020-03-03 15:07:49 +020028#include <net/netfilter/nf_flow_table.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030029#include <net/netfilter/nf_conntrack.h>
30#include <net/netfilter/nf_conntrack_core.h>
31#include <net/netfilter/nf_conntrack_zones.h>
32#include <net/netfilter/nf_conntrack_helper.h>
wenxubeb97d32020-04-21 07:55:43 +080033#include <net/netfilter/nf_conntrack_acct.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030034#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Jeremy Sowden40d102c2019-09-13 09:13:05 +010035#include <uapi/linux/netfilter/nf_nat.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030036
Paul Blakeyc34b9612020-03-03 15:07:49 +020037static struct workqueue_struct *act_ct_wq;
38static struct rhashtable zones_ht;
Eric Dumazet138470a2020-03-08 14:27:48 -070039static DEFINE_MUTEX(zones_mutex);
Paul Blakeyc34b9612020-03-03 15:07:49 +020040
41struct tcf_ct_flow_table {
42 struct rhash_head node; /* In zones tables */
43
44 struct rcu_work rwork;
45 struct nf_flowtable nf_ft;
Eric Dumazet138470a2020-03-08 14:27:48 -070046 refcount_t ref;
Paul Blakeyc34b9612020-03-03 15:07:49 +020047 u16 zone;
Paul Blakeyc34b9612020-03-03 15:07:49 +020048
49 bool dying;
50};
51
52static const struct rhashtable_params zones_params = {
53 .head_offset = offsetof(struct tcf_ct_flow_table, node),
54 .key_offset = offsetof(struct tcf_ct_flow_table, zone),
55 .key_len = sizeof_field(struct tcf_ct_flow_table, zone),
56 .automatic_shrinking = true,
57};
58
Paul Blakey9c26ba92020-03-12 12:23:06 +020059static struct flow_action_entry *
60tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
61{
62 int i = flow_action->num_entries++;
63
64 return &flow_action->entries[i];
65}
66
67static void tcf_ct_add_mangle_action(struct flow_action *action,
68 enum flow_action_mangle_base htype,
69 u32 offset,
70 u32 mask,
71 u32 val)
72{
73 struct flow_action_entry *entry;
74
75 entry = tcf_ct_flow_table_flow_action_get_next(action);
76 entry->id = FLOW_ACTION_MANGLE;
77 entry->mangle.htype = htype;
78 entry->mangle.mask = ~mask;
79 entry->mangle.offset = offset;
80 entry->mangle.val = val;
81}
82
83/* The following nat helper functions check if the inverted reverse tuple
84 * (target) is different then the current dir tuple - meaning nat for ports
85 * and/or ip is needed, and add the relevant mangle actions.
86 */
87static void
88tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
89 struct nf_conntrack_tuple target,
90 struct flow_action *action)
91{
92 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
93 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
94 offsetof(struct iphdr, saddr),
95 0xFFFFFFFF,
96 be32_to_cpu(target.src.u3.ip));
97 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
98 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
99 offsetof(struct iphdr, daddr),
100 0xFFFFFFFF,
101 be32_to_cpu(target.dst.u3.ip));
102}
103
104static void
105tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
106 union nf_inet_addr *addr,
107 u32 offset)
108{
109 int i;
110
111 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
112 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
113 i * sizeof(u32) + offset,
114 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
115}
116
117static void
118tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
119 struct nf_conntrack_tuple target,
120 struct flow_action *action)
121{
122 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
123 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
124 offsetof(struct ipv6hdr,
125 saddr));
126 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
127 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
128 offsetof(struct ipv6hdr,
129 daddr));
130}
131
132static void
133tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
134 struct nf_conntrack_tuple target,
135 struct flow_action *action)
136{
137 __be16 target_src = target.src.u.tcp.port;
138 __be16 target_dst = target.dst.u.tcp.port;
139
140 if (target_src != tuple->src.u.tcp.port)
141 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
142 offsetof(struct tcphdr, source),
143 0xFFFF, be16_to_cpu(target_src));
144 if (target_dst != tuple->dst.u.tcp.port)
145 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
146 offsetof(struct tcphdr, dest),
147 0xFFFF, be16_to_cpu(target_dst));
148}
149
150static void
151tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
152 struct nf_conntrack_tuple target,
153 struct flow_action *action)
154{
155 __be16 target_src = target.src.u.udp.port;
156 __be16 target_dst = target.dst.u.udp.port;
157
158 if (target_src != tuple->src.u.udp.port)
Roi Dayan47b5d2a2020-10-19 12:02:44 +0300159 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
Paul Blakey9c26ba92020-03-12 12:23:06 +0200160 offsetof(struct udphdr, source),
161 0xFFFF, be16_to_cpu(target_src));
162 if (target_dst != tuple->dst.u.udp.port)
Roi Dayan47b5d2a2020-10-19 12:02:44 +0300163 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
Paul Blakey9c26ba92020-03-12 12:23:06 +0200164 offsetof(struct udphdr, dest),
165 0xFFFF, be16_to_cpu(target_dst));
166}
167
168static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
169 enum ip_conntrack_dir dir,
170 struct flow_action *action)
171{
172 struct nf_conn_labels *ct_labels;
173 struct flow_action_entry *entry;
Paul Blakey30b0cf92020-03-12 12:23:07 +0200174 enum ip_conntrack_info ctinfo;
Paul Blakey9c26ba92020-03-12 12:23:06 +0200175 u32 *act_ct_labels;
176
177 entry = tcf_ct_flow_table_flow_action_get_next(action);
178 entry->id = FLOW_ACTION_CT_METADATA;
179#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
180 entry->ct_metadata.mark = ct->mark;
181#endif
Paul Blakey30b0cf92020-03-12 12:23:07 +0200182 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
183 IP_CT_ESTABLISHED_REPLY;
184 /* aligns with the CT reference on the SKB nf_ct_set */
185 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
Paul Blakey9c26ba92020-03-12 12:23:06 +0200186
187 act_ct_labels = entry->ct_metadata.labels;
188 ct_labels = nf_ct_labels_find(ct);
189 if (ct_labels)
190 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
191 else
192 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
193}
194
195static int tcf_ct_flow_table_add_action_nat(struct net *net,
196 struct nf_conn *ct,
197 enum ip_conntrack_dir dir,
198 struct flow_action *action)
199{
200 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
201 struct nf_conntrack_tuple target;
202
wenxu05aa69e2020-05-30 13:54:51 +0800203 if (!(ct->status & IPS_NAT_MASK))
204 return 0;
205
Paul Blakey9c26ba92020-03-12 12:23:06 +0200206 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
207
208 switch (tuple->src.l3num) {
209 case NFPROTO_IPV4:
210 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
211 action);
212 break;
213 case NFPROTO_IPV6:
214 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
215 action);
216 break;
217 default:
218 return -EOPNOTSUPP;
219 }
220
221 switch (nf_ct_protonum(ct)) {
222 case IPPROTO_TCP:
223 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
224 break;
225 case IPPROTO_UDP:
226 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
227 break;
228 default:
229 return -EOPNOTSUPP;
230 }
231
232 return 0;
233}
234
235static int tcf_ct_flow_table_fill_actions(struct net *net,
236 const struct flow_offload *flow,
237 enum flow_offload_tuple_dir tdir,
238 struct nf_flow_rule *flow_rule)
239{
240 struct flow_action *action = &flow_rule->rule->action;
241 int num_entries = action->num_entries;
242 struct nf_conn *ct = flow->ct;
243 enum ip_conntrack_dir dir;
244 int i, err;
245
246 switch (tdir) {
247 case FLOW_OFFLOAD_DIR_ORIGINAL:
248 dir = IP_CT_DIR_ORIGINAL;
249 break;
250 case FLOW_OFFLOAD_DIR_REPLY:
251 dir = IP_CT_DIR_REPLY;
252 break;
253 default:
254 return -EOPNOTSUPP;
255 }
256
257 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
258 if (err)
259 goto err_nat;
260
261 tcf_ct_flow_table_add_action_meta(ct, dir, action);
262 return 0;
263
264err_nat:
265 /* Clear filled actions */
266 for (i = num_entries; i < action->num_entries; i++)
267 memset(&action->entries[i], 0, sizeof(action->entries[i]));
268 action->num_entries = num_entries;
269
270 return err;
271}
272
Paul Blakeyc34b9612020-03-03 15:07:49 +0200273static struct nf_flowtable_type flowtable_ct = {
Paul Blakey9c26ba92020-03-12 12:23:06 +0200274 .action = tcf_ct_flow_table_fill_actions,
Paul Blakeyc34b9612020-03-03 15:07:49 +0200275 .owner = THIS_MODULE,
276};
277
278static int tcf_ct_flow_table_get(struct tcf_ct_params *params)
279{
280 struct tcf_ct_flow_table *ct_ft;
281 int err = -ENOMEM;
282
Eric Dumazet138470a2020-03-08 14:27:48 -0700283 mutex_lock(&zones_mutex);
Paul Blakeyc34b9612020-03-03 15:07:49 +0200284 ct_ft = rhashtable_lookup_fast(&zones_ht, &params->zone, zones_params);
Eric Dumazet138470a2020-03-08 14:27:48 -0700285 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
286 goto out_unlock;
Paul Blakeyc34b9612020-03-03 15:07:49 +0200287
Eric Dumazet138470a2020-03-08 14:27:48 -0700288 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
Paul Blakeyc34b9612020-03-03 15:07:49 +0200289 if (!ct_ft)
290 goto err_alloc;
Eric Dumazet138470a2020-03-08 14:27:48 -0700291 refcount_set(&ct_ft->ref, 1);
Paul Blakeyc34b9612020-03-03 15:07:49 +0200292
293 ct_ft->zone = params->zone;
294 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
295 if (err)
296 goto err_insert;
297
298 ct_ft->nf_ft.type = &flowtable_ct;
Paul Blakeyedd58612020-03-12 12:23:09 +0200299 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD;
Paul Blakeyc34b9612020-03-03 15:07:49 +0200300 err = nf_flow_table_init(&ct_ft->nf_ft);
301 if (err)
302 goto err_init;
303
304 __module_get(THIS_MODULE);
Eric Dumazet138470a2020-03-08 14:27:48 -0700305out_unlock:
Paul Blakeyc34b9612020-03-03 15:07:49 +0200306 params->ct_ft = ct_ft;
Paul Blakeyedd58612020-03-12 12:23:09 +0200307 params->nf_ft = &ct_ft->nf_ft;
Eric Dumazet138470a2020-03-08 14:27:48 -0700308 mutex_unlock(&zones_mutex);
Paul Blakeyc34b9612020-03-03 15:07:49 +0200309
310 return 0;
311
312err_init:
313 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
314err_insert:
315 kfree(ct_ft);
316err_alloc:
Eric Dumazet138470a2020-03-08 14:27:48 -0700317 mutex_unlock(&zones_mutex);
Paul Blakeyc34b9612020-03-03 15:07:49 +0200318 return err;
319}
320
321static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
322{
323 struct tcf_ct_flow_table *ct_ft;
324
325 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
326 rwork);
327 nf_flow_table_free(&ct_ft->nf_ft);
328 kfree(ct_ft);
329
330 module_put(THIS_MODULE);
331}
332
333static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
334{
335 struct tcf_ct_flow_table *ct_ft = params->ct_ft;
336
Eric Dumazet138470a2020-03-08 14:27:48 -0700337 if (refcount_dec_and_test(&params->ct_ft->ref)) {
Paul Blakeyc34b9612020-03-03 15:07:49 +0200338 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
339 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
340 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
341 }
Paul Blakeyc34b9612020-03-03 15:07:49 +0200342}
343
Paul Blakey64ff70b2020-03-03 15:07:50 +0200344static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
345 struct nf_conn *ct,
346 bool tcp)
347{
348 struct flow_offload *entry;
349 int err;
350
351 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
352 return;
353
354 entry = flow_offload_alloc(ct);
355 if (!entry) {
356 WARN_ON_ONCE(1);
357 goto err_alloc;
358 }
359
360 if (tcp) {
361 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
362 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
363 }
364
365 err = flow_offload_add(&ct_ft->nf_ft, entry);
366 if (err)
367 goto err_add;
368
369 return;
370
371err_add:
372 flow_offload_free(entry);
373err_alloc:
374 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
375}
376
377static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
378 struct nf_conn *ct,
379 enum ip_conntrack_info ctinfo)
380{
381 bool tcp = false;
382
383 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
384 return;
385
386 switch (nf_ct_protonum(ct)) {
387 case IPPROTO_TCP:
388 tcp = true;
389 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
390 return;
391 break;
392 case IPPROTO_UDP:
393 break;
394 default:
395 return;
396 }
397
398 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
399 ct->status & IPS_SEQ_ADJUST)
400 return;
401
402 tcf_ct_flow_table_add(ct_ft, ct, tcp);
403}
404
Paul Blakey46475bb2020-03-03 15:07:51 +0200405static bool
406tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
Paul Blakey07ac9d12020-03-04 13:49:38 +0200407 struct flow_offload_tuple *tuple,
408 struct tcphdr **tcph)
Paul Blakey46475bb2020-03-03 15:07:51 +0200409{
410 struct flow_ports *ports;
411 unsigned int thoff;
412 struct iphdr *iph;
413
Paul Blakey4cc5fde2020-03-04 13:49:39 +0200414 if (!pskb_network_may_pull(skb, sizeof(*iph)))
Paul Blakey46475bb2020-03-03 15:07:51 +0200415 return false;
416
417 iph = ip_hdr(skb);
418 thoff = iph->ihl * 4;
419
420 if (ip_is_fragment(iph) ||
421 unlikely(thoff != sizeof(struct iphdr)))
422 return false;
423
424 if (iph->protocol != IPPROTO_TCP &&
425 iph->protocol != IPPROTO_UDP)
426 return false;
427
428 if (iph->ttl <= 1)
429 return false;
430
Paul Blakey4cc5fde2020-03-04 13:49:39 +0200431 if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ?
432 thoff + sizeof(struct tcphdr) :
433 thoff + sizeof(*ports)))
Paul Blakey46475bb2020-03-03 15:07:51 +0200434 return false;
435
Paul Blakey07ac9d12020-03-04 13:49:38 +0200436 iph = ip_hdr(skb);
437 if (iph->protocol == IPPROTO_TCP)
438 *tcph = (void *)(skb_network_header(skb) + thoff);
Paul Blakey46475bb2020-03-03 15:07:51 +0200439
Paul Blakey07ac9d12020-03-04 13:49:38 +0200440 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
Paul Blakey46475bb2020-03-03 15:07:51 +0200441 tuple->src_v4.s_addr = iph->saddr;
442 tuple->dst_v4.s_addr = iph->daddr;
443 tuple->src_port = ports->source;
444 tuple->dst_port = ports->dest;
445 tuple->l3proto = AF_INET;
446 tuple->l4proto = iph->protocol;
447
448 return true;
449}
450
451static bool
452tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
Paul Blakey07ac9d12020-03-04 13:49:38 +0200453 struct flow_offload_tuple *tuple,
454 struct tcphdr **tcph)
Paul Blakey46475bb2020-03-03 15:07:51 +0200455{
456 struct flow_ports *ports;
457 struct ipv6hdr *ip6h;
458 unsigned int thoff;
459
Paul Blakey4cc5fde2020-03-04 13:49:39 +0200460 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
Paul Blakey46475bb2020-03-03 15:07:51 +0200461 return false;
462
463 ip6h = ipv6_hdr(skb);
464
465 if (ip6h->nexthdr != IPPROTO_TCP &&
466 ip6h->nexthdr != IPPROTO_UDP)
467 return false;
468
469 if (ip6h->hop_limit <= 1)
470 return false;
471
472 thoff = sizeof(*ip6h);
Paul Blakey4cc5fde2020-03-04 13:49:39 +0200473 if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ?
474 thoff + sizeof(struct tcphdr) :
475 thoff + sizeof(*ports)))
Paul Blakey46475bb2020-03-03 15:07:51 +0200476 return false;
477
Paul Blakey07ac9d12020-03-04 13:49:38 +0200478 ip6h = ipv6_hdr(skb);
479 if (ip6h->nexthdr == IPPROTO_TCP)
480 *tcph = (void *)(skb_network_header(skb) + thoff);
Paul Blakey46475bb2020-03-03 15:07:51 +0200481
Paul Blakey07ac9d12020-03-04 13:49:38 +0200482 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
Paul Blakey46475bb2020-03-03 15:07:51 +0200483 tuple->src_v6 = ip6h->saddr;
484 tuple->dst_v6 = ip6h->daddr;
485 tuple->src_port = ports->source;
486 tuple->dst_port = ports->dest;
487 tuple->l3proto = AF_INET6;
488 tuple->l4proto = ip6h->nexthdr;
489
490 return true;
491}
492
Paul Blakey46475bb2020-03-03 15:07:51 +0200493static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
494 struct sk_buff *skb,
495 u8 family)
496{
497 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
498 struct flow_offload_tuple_rhash *tuplehash;
499 struct flow_offload_tuple tuple = {};
500 enum ip_conntrack_info ctinfo;
Paul Blakey07ac9d12020-03-04 13:49:38 +0200501 struct tcphdr *tcph = NULL;
Paul Blakey46475bb2020-03-03 15:07:51 +0200502 struct flow_offload *flow;
503 struct nf_conn *ct;
Paul Blakey46475bb2020-03-03 15:07:51 +0200504 u8 dir;
505
506 /* Previously seen or loopback */
507 ct = nf_ct_get(skb, &ctinfo);
508 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
509 return false;
510
511 switch (family) {
512 case NFPROTO_IPV4:
Paul Blakey07ac9d12020-03-04 13:49:38 +0200513 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
Paul Blakey46475bb2020-03-03 15:07:51 +0200514 return false;
515 break;
516 case NFPROTO_IPV6:
Paul Blakey07ac9d12020-03-04 13:49:38 +0200517 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
Paul Blakey46475bb2020-03-03 15:07:51 +0200518 return false;
519 break;
520 default:
521 return false;
522 }
523
524 tuplehash = flow_offload_lookup(nf_ft, &tuple);
525 if (!tuplehash)
526 return false;
527
528 dir = tuplehash->tuple.dir;
529 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
530 ct = flow->ct;
531
Paul Blakey07ac9d12020-03-04 13:49:38 +0200532 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
533 flow_offload_teardown(flow);
534 return false;
535 }
536
Paul Blakey46475bb2020-03-03 15:07:51 +0200537 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
538 IP_CT_ESTABLISHED_REPLY;
539
Paul Blakey8b3646d2020-03-12 12:23:08 +0200540 flow_offload_refresh(nf_ft, flow);
Paul Blakey46475bb2020-03-03 15:07:51 +0200541 nf_conntrack_get(&ct->ct_general);
542 nf_ct_set(skb, ct, ctinfo);
wenxubeb97d32020-04-21 07:55:43 +0800543 nf_ct_acct_update(ct, dir, skb->len);
Paul Blakey46475bb2020-03-03 15:07:51 +0200544
545 return true;
546}
547
Paul Blakeyc34b9612020-03-03 15:07:49 +0200548static int tcf_ct_flow_tables_init(void)
549{
550 return rhashtable_init(&zones_ht, &zones_params);
551}
552
553static void tcf_ct_flow_tables_uninit(void)
554{
555 rhashtable_destroy(&zones_ht);
556}
557
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300558static struct tc_action_ops act_ct_ops;
559static unsigned int ct_net_id;
560
561struct tc_ct_action_net {
562 struct tc_action_net tn; /* Must be first */
563 bool labels;
564};
565
566/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
567static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
568 u16 zone_id, bool force)
569{
570 enum ip_conntrack_info ctinfo;
571 struct nf_conn *ct;
572
573 ct = nf_ct_get(skb, &ctinfo);
574 if (!ct)
575 return false;
576 if (!net_eq(net, read_pnet(&ct->ct_net)))
577 return false;
578 if (nf_ct_zone(ct)->id != zone_id)
579 return false;
580
581 /* Force conntrack entry direction. */
582 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
583 if (nf_ct_is_confirmed(ct))
584 nf_ct_kill(ct);
585
586 nf_conntrack_put(&ct->ct_general);
587 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
588
589 return false;
590 }
591
592 return true;
593}
594
595/* Trim the skb to the length specified by the IP/IPv6 header,
596 * removing any trailing lower-layer padding. This prepares the skb
597 * for higher-layer processing that assumes skb->len excludes padding
598 * (such as nf_ip_checksum). The caller needs to pull the skb to the
599 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
600 */
601static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
602{
603 unsigned int len;
604 int err;
605
606 switch (family) {
607 case NFPROTO_IPV4:
608 len = ntohs(ip_hdr(skb)->tot_len);
609 break;
610 case NFPROTO_IPV6:
611 len = sizeof(struct ipv6hdr)
612 + ntohs(ipv6_hdr(skb)->payload_len);
613 break;
614 default:
615 len = skb->len;
616 }
617
618 err = pskb_trim_rcsum(skb, len);
619
620 return err;
621}
622
623static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
624{
625 u8 family = NFPROTO_UNSPEC;
626
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +0200627 switch (skb_protocol(skb, true)) {
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300628 case htons(ETH_P_IP):
629 family = NFPROTO_IPV4;
630 break;
631 case htons(ETH_P_IPV6):
632 family = NFPROTO_IPV6;
633 break;
634 default:
635 break;
636 }
637
638 return family;
639}
640
641static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
642{
643 unsigned int len;
644
645 len = skb_network_offset(skb) + sizeof(struct iphdr);
646 if (unlikely(skb->len < len))
647 return -EINVAL;
648 if (unlikely(!pskb_may_pull(skb, len)))
649 return -ENOMEM;
650
651 *frag = ip_is_fragment(ip_hdr(skb));
652 return 0;
653}
654
655static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
656{
657 unsigned int flags = 0, len, payload_ofs = 0;
658 unsigned short frag_off;
659 int nexthdr;
660
661 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
662 if (unlikely(skb->len < len))
663 return -EINVAL;
664 if (unlikely(!pskb_may_pull(skb, len)))
665 return -ENOMEM;
666
667 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
668 if (unlikely(nexthdr < 0))
669 return -EPROTO;
670
671 *frag = flags & IP6_FH_F_FRAG;
672 return 0;
673}
674
675static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
wenxuae372cb2020-07-19 20:30:37 +0800676 u8 family, u16 zone, bool *defrag)
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300677{
678 enum ip_conntrack_info ctinfo;
wenxuae372cb2020-07-19 20:30:37 +0800679 struct qdisc_skb_cb cb;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300680 struct nf_conn *ct;
681 int err = 0;
682 bool frag;
683
684 /* Previously seen (loopback)? Ignore. */
685 ct = nf_ct_get(skb, &ctinfo);
686 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
687 return 0;
688
689 if (family == NFPROTO_IPV4)
690 err = tcf_ct_ipv4_is_fragment(skb, &frag);
691 else
692 err = tcf_ct_ipv6_is_fragment(skb, &frag);
693 if (err || !frag)
694 return err;
695
696 skb_get(skb);
wenxuae372cb2020-07-19 20:30:37 +0800697 cb = *qdisc_skb_cb(skb);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300698
699 if (family == NFPROTO_IPV4) {
700 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
701
702 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
703 local_bh_disable();
704 err = ip_defrag(net, skb, user);
705 local_bh_enable();
706 if (err && err != -EINPROGRESS)
Alaa Hleiheleda814b2020-08-19 18:24:10 +0300707 return err;
wenxuae372cb2020-07-19 20:30:37 +0800708
wenxu038ebb12020-07-31 10:45:01 +0800709 if (!err) {
wenxuae372cb2020-07-19 20:30:37 +0800710 *defrag = true;
wenxu038ebb12020-07-31 10:45:01 +0800711 cb.mru = IPCB(skb)->frag_max_size;
712 }
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300713 } else { /* NFPROTO_IPV6 */
714#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
715 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
716
717 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
718 err = nf_ct_frag6_gather(net, skb, user);
719 if (err && err != -EINPROGRESS)
720 goto out_free;
wenxuae372cb2020-07-19 20:30:37 +0800721
wenxu038ebb12020-07-31 10:45:01 +0800722 if (!err) {
wenxuae372cb2020-07-19 20:30:37 +0800723 *defrag = true;
wenxu038ebb12020-07-31 10:45:01 +0800724 cb.mru = IP6CB(skb)->frag_max_size;
725 }
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300726#else
727 err = -EOPNOTSUPP;
728 goto out_free;
729#endif
730 }
731
wenxuae372cb2020-07-19 20:30:37 +0800732 *qdisc_skb_cb(skb) = cb;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300733 skb_clear_hash(skb);
734 skb->ignore_df = 1;
735 return err;
736
737out_free:
738 kfree_skb(skb);
739 return err;
740}
741
742static void tcf_ct_params_free(struct rcu_head *head)
743{
744 struct tcf_ct_params *params = container_of(head,
745 struct tcf_ct_params, rcu);
746
Paul Blakeyc34b9612020-03-03 15:07:49 +0200747 tcf_ct_flow_table_put(params);
748
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300749 if (params->tmpl)
750 nf_conntrack_put(&params->tmpl->ct_general);
751 kfree(params);
752}
753
754#if IS_ENABLED(CONFIG_NF_NAT)
755/* Modelled after nf_nat_ipv[46]_fn().
756 * range is only used for new, uninitialized NAT state.
757 * Returns either NF_ACCEPT or NF_DROP.
758 */
759static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
760 enum ip_conntrack_info ctinfo,
761 const struct nf_nat_range2 *range,
762 enum nf_nat_manip_type maniptype)
763{
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +0200764 __be16 proto = skb_protocol(skb, true);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300765 int hooknum, err = NF_ACCEPT;
766
767 /* See HOOK2MANIP(). */
768 if (maniptype == NF_NAT_MANIP_SRC)
769 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
770 else
771 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
772
773 switch (ctinfo) {
774 case IP_CT_RELATED:
775 case IP_CT_RELATED_REPLY:
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +0200776 if (proto == htons(ETH_P_IP) &&
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300777 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
778 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
779 hooknum))
780 err = NF_DROP;
781 goto out;
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +0200782 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300783 __be16 frag_off;
784 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
785 int hdrlen = ipv6_skip_exthdr(skb,
786 sizeof(struct ipv6hdr),
787 &nexthdr, &frag_off);
788
789 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
790 if (!nf_nat_icmpv6_reply_translation(skb, ct,
791 ctinfo,
792 hooknum,
793 hdrlen))
794 err = NF_DROP;
795 goto out;
796 }
797 }
798 /* Non-ICMP, fall thru to initialize if needed. */
Gustavo A. R. Silva964201d2020-07-07 12:21:38 -0500799 fallthrough;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300800 case IP_CT_NEW:
801 /* Seen it before? This can happen for loopback, retrans,
802 * or local packets.
803 */
804 if (!nf_nat_initialized(ct, maniptype)) {
805 /* Initialize according to the NAT action. */
806 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
807 /* Action is set up to establish a new
808 * mapping.
809 */
810 ? nf_nat_setup_info(ct, range, maniptype)
811 : nf_nat_alloc_null_binding(ct, hooknum);
812 if (err != NF_ACCEPT)
813 goto out;
814 }
815 break;
816
817 case IP_CT_ESTABLISHED:
818 case IP_CT_ESTABLISHED_REPLY:
819 break;
820
821 default:
822 err = NF_DROP;
823 goto out;
824 }
825
826 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
827out:
828 return err;
829}
830#endif /* CONFIG_NF_NAT */
831
832static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
833{
834#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
835 u32 new_mark;
836
837 if (!mask)
838 return;
839
840 new_mark = mark | (ct->mark & ~(mask));
841 if (ct->mark != new_mark) {
842 ct->mark = new_mark;
843 if (nf_ct_is_confirmed(ct))
844 nf_conntrack_event_cache(IPCT_MARK, ct);
845 }
846#endif
847}
848
849static void tcf_ct_act_set_labels(struct nf_conn *ct,
850 u32 *labels,
851 u32 *labels_m)
852{
853#if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800854 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300855
856 if (!memchr_inv(labels_m, 0, labels_sz))
857 return;
858
859 nf_connlabels_replace(ct, labels, labels_m, 4);
860#endif
861}
862
863static int tcf_ct_act_nat(struct sk_buff *skb,
864 struct nf_conn *ct,
865 enum ip_conntrack_info ctinfo,
866 int ct_action,
867 struct nf_nat_range2 *range,
868 bool commit)
869{
870#if IS_ENABLED(CONFIG_NF_NAT)
Aaron Conole95219af2019-12-03 16:34:14 -0500871 int err;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300872 enum nf_nat_manip_type maniptype;
873
874 if (!(ct_action & TCA_CT_ACT_NAT))
875 return NF_ACCEPT;
876
877 /* Add NAT extension if not confirmed yet. */
878 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
879 return NF_DROP; /* Can't NAT. */
880
881 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
882 (ctinfo != IP_CT_RELATED || commit)) {
883 /* NAT an established or related connection like before. */
884 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
885 /* This is the REPLY direction for a connection
886 * for which NAT was applied in the forward
887 * direction. Do the reverse NAT.
888 */
889 maniptype = ct->status & IPS_SRC_NAT
890 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
891 else
892 maniptype = ct->status & IPS_SRC_NAT
893 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
894 } else if (ct_action & TCA_CT_ACT_NAT_SRC) {
895 maniptype = NF_NAT_MANIP_SRC;
896 } else if (ct_action & TCA_CT_ACT_NAT_DST) {
897 maniptype = NF_NAT_MANIP_DST;
898 } else {
899 return NF_ACCEPT;
900 }
901
Aaron Conole95219af2019-12-03 16:34:14 -0500902 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
903 if (err == NF_ACCEPT &&
904 ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
905 if (maniptype == NF_NAT_MANIP_SRC)
906 maniptype = NF_NAT_MANIP_DST;
907 else
908 maniptype = NF_NAT_MANIP_SRC;
909
910 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
911 }
912 return err;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300913#else
914 return NF_ACCEPT;
915#endif
916}
917
918static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
919 struct tcf_result *res)
920{
921 struct net *net = dev_net(skb->dev);
922 bool cached, commit, clear, force;
923 enum ip_conntrack_info ctinfo;
924 struct tcf_ct *c = to_ct(a);
925 struct nf_conn *tmpl = NULL;
926 struct nf_hook_state state;
927 int nh_ofs, err, retval;
928 struct tcf_ct_params *p;
Paul Blakey46475bb2020-03-03 15:07:51 +0200929 bool skip_add = false;
wenxuae372cb2020-07-19 20:30:37 +0800930 bool defrag = false;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300931 struct nf_conn *ct;
932 u8 family;
933
934 p = rcu_dereference_bh(c->params);
935
936 retval = READ_ONCE(c->tcf_action);
937 commit = p->ct_action & TCA_CT_ACT_COMMIT;
938 clear = p->ct_action & TCA_CT_ACT_CLEAR;
939 force = p->ct_action & TCA_CT_ACT_FORCE;
940 tmpl = p->tmpl;
941
wenxu8367b3a2020-07-04 15:42:47 +0800942 tcf_lastuse_update(&c->tcf_tm);
943
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300944 if (clear) {
945 ct = nf_ct_get(skb, &ctinfo);
946 if (ct) {
947 nf_conntrack_put(&ct->ct_general);
948 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
949 }
950
951 goto out;
952 }
953
954 family = tcf_ct_skb_nf_family(skb);
955 if (family == NFPROTO_UNSPEC)
956 goto drop;
957
958 /* The conntrack module expects to be working at L3.
959 * We also try to pull the IPv4/6 header to linear area
960 */
961 nh_ofs = skb_network_offset(skb);
962 skb_pull_rcsum(skb, nh_ofs);
wenxuae372cb2020-07-19 20:30:37 +0800963 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300964 if (err == -EINPROGRESS) {
965 retval = TC_ACT_STOLEN;
966 goto out;
967 }
968 if (err)
969 goto drop;
970
971 err = tcf_ct_skb_network_trim(skb, family);
972 if (err)
973 goto drop;
974
975 /* If we are recirculating packets to match on ct fields and
976 * committing with a separate ct action, then we don't need to
977 * actually run the packet through conntrack twice unless it's for a
978 * different zone.
979 */
980 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
981 if (!cached) {
Paul Blakey46475bb2020-03-03 15:07:51 +0200982 if (!commit && tcf_ct_flow_table_lookup(p, skb, family)) {
983 skip_add = true;
984 goto do_nat;
985 }
986
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300987 /* Associate skb with specified zone. */
988 if (tmpl) {
989 ct = nf_ct_get(skb, &ctinfo);
990 if (skb_nfct(skb))
991 nf_conntrack_put(skb_nfct(skb));
992 nf_conntrack_get(&tmpl->ct_general);
993 nf_ct_set(skb, tmpl, IP_CT_NEW);
994 }
995
996 state.hook = NF_INET_PRE_ROUTING;
997 state.net = net;
998 state.pf = family;
999 err = nf_conntrack_in(skb, &state);
1000 if (err != NF_ACCEPT)
1001 goto out_push;
1002 }
1003
Paul Blakey46475bb2020-03-03 15:07:51 +02001004do_nat:
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001005 ct = nf_ct_get(skb, &ctinfo);
1006 if (!ct)
1007 goto out_push;
1008 nf_ct_deliver_cached_events(ct);
1009
1010 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1011 if (err != NF_ACCEPT)
1012 goto drop;
1013
1014 if (commit) {
1015 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1016 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1017
1018 /* This will take care of sending queued events
1019 * even if the connection is already confirmed.
1020 */
1021 nf_conntrack_confirm(skb);
Paul Blakey46475bb2020-03-03 15:07:51 +02001022 } else if (!skip_add) {
1023 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001024 }
1025
1026out_push:
1027 skb_push_rcsum(skb, nh_ofs);
1028
1029out:
Vlad Buslov5e1ad952019-10-30 16:09:01 +02001030 tcf_action_update_bstats(&c->common, skb);
wenxuae372cb2020-07-19 20:30:37 +08001031 if (defrag)
1032 qdisc_skb_cb(skb)->pkt_len = skb->len;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001033 return retval;
1034
1035drop:
Vlad Buslov26b537a2019-10-30 16:09:02 +02001036 tcf_action_inc_drop_qstats(&c->common);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001037 return TC_ACT_SHOT;
1038}
1039
1040static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001041 [TCA_CT_ACTION] = { .type = NLA_U16 },
Johannes Berg81408602020-08-18 10:17:31 +02001042 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001043 [TCA_CT_ZONE] = { .type = NLA_U16 },
1044 [TCA_CT_MARK] = { .type = NLA_U32 },
1045 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1046 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1047 .len = 128 / BITS_PER_BYTE },
1048 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1049 .len = 128 / BITS_PER_BYTE },
1050 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1051 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
Johannes Berg81408602020-08-18 10:17:31 +02001052 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1053 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001054 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1055 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1056};
1057
1058static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1059 struct tc_ct *parm,
1060 struct nlattr **tb,
1061 struct netlink_ext_ack *extack)
1062{
1063 struct nf_nat_range2 *range;
1064
1065 if (!(p->ct_action & TCA_CT_ACT_NAT))
1066 return 0;
1067
1068 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1069 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1070 return -EOPNOTSUPP;
1071 }
1072
1073 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1074 return 0;
1075
1076 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1077 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1078 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1079 return -EOPNOTSUPP;
1080 }
1081
1082 range = &p->range;
1083 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1084 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1085
1086 p->ipv4_range = true;
1087 range->flags |= NF_NAT_RANGE_MAP_IPS;
1088 range->min_addr.ip =
1089 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1090
1091 range->max_addr.ip = max_attr ?
1092 nla_get_in_addr(max_attr) :
1093 range->min_addr.ip;
1094 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1095 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1096
1097 p->ipv4_range = false;
1098 range->flags |= NF_NAT_RANGE_MAP_IPS;
1099 range->min_addr.in6 =
1100 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1101
1102 range->max_addr.in6 = max_attr ?
1103 nla_get_in6_addr(max_attr) :
1104 range->min_addr.in6;
1105 }
1106
1107 if (tb[TCA_CT_NAT_PORT_MIN]) {
1108 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1109 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1110
1111 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1112 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1113 range->min_proto.all;
1114 }
1115
1116 return 0;
1117}
1118
1119static void tcf_ct_set_key_val(struct nlattr **tb,
1120 void *val, int val_type,
1121 void *mask, int mask_type,
1122 int len)
1123{
1124 if (!tb[val_type])
1125 return;
1126 nla_memcpy(val, tb[val_type], len);
1127
1128 if (!mask)
1129 return;
1130
1131 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1132 memset(mask, 0xff, len);
1133 else
1134 nla_memcpy(mask, tb[mask_type], len);
1135}
1136
1137static int tcf_ct_fill_params(struct net *net,
1138 struct tcf_ct_params *p,
1139 struct tc_ct *parm,
1140 struct nlattr **tb,
1141 struct netlink_ext_ack *extack)
1142{
1143 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1144 struct nf_conntrack_zone zone;
1145 struct nf_conn *tmpl;
1146 int err;
1147
1148 p->zone = NF_CT_DEFAULT_ZONE_ID;
1149
1150 tcf_ct_set_key_val(tb,
1151 &p->ct_action, TCA_CT_ACTION,
1152 NULL, TCA_CT_UNSPEC,
1153 sizeof(p->ct_action));
1154
1155 if (p->ct_action & TCA_CT_ACT_CLEAR)
1156 return 0;
1157
1158 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1159 if (err)
1160 return err;
1161
1162 if (tb[TCA_CT_MARK]) {
1163 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1164 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1165 return -EOPNOTSUPP;
1166 }
1167 tcf_ct_set_key_val(tb,
1168 &p->mark, TCA_CT_MARK,
1169 &p->mark_mask, TCA_CT_MARK_MASK,
1170 sizeof(p->mark));
1171 }
1172
1173 if (tb[TCA_CT_LABELS]) {
1174 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1175 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1176 return -EOPNOTSUPP;
1177 }
1178
1179 if (!tn->labels) {
1180 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1181 return -EOPNOTSUPP;
1182 }
1183 tcf_ct_set_key_val(tb,
1184 p->labels, TCA_CT_LABELS,
1185 p->labels_mask, TCA_CT_LABELS_MASK,
1186 sizeof(p->labels));
1187 }
1188
1189 if (tb[TCA_CT_ZONE]) {
1190 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1191 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1192 return -EOPNOTSUPP;
1193 }
1194
1195 tcf_ct_set_key_val(tb,
1196 &p->zone, TCA_CT_ZONE,
1197 NULL, TCA_CT_UNSPEC,
1198 sizeof(p->zone));
1199 }
1200
1201 if (p->zone == NF_CT_DEFAULT_ZONE_ID)
1202 return 0;
1203
1204 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1205 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1206 if (!tmpl) {
1207 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1208 return -ENOMEM;
1209 }
1210 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1211 nf_conntrack_get(&tmpl->ct_general);
1212 p->tmpl = tmpl;
1213
1214 return 0;
1215}
1216
1217static int tcf_ct_init(struct net *net, struct nlattr *nla,
1218 struct nlattr *est, struct tc_action **a,
1219 int replace, int bind, bool rtnl_held,
Vlad Buslovabbb0d32019-10-30 16:09:05 +02001220 struct tcf_proto *tp, u32 flags,
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001221 struct netlink_ext_ack *extack)
1222{
1223 struct tc_action_net *tn = net_generic(net, ct_net_id);
1224 struct tcf_ct_params *params = NULL;
1225 struct nlattr *tb[TCA_CT_MAX + 1];
1226 struct tcf_chain *goto_ch = NULL;
1227 struct tc_ct *parm;
1228 struct tcf_ct *c;
1229 int err, res = 0;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +00001230 u32 index;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001231
1232 if (!nla) {
1233 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1234 return -EINVAL;
1235 }
1236
1237 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1238 if (err < 0)
1239 return err;
1240
1241 if (!tb[TCA_CT_PARMS]) {
1242 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1243 return -EINVAL;
1244 }
1245 parm = nla_data(tb[TCA_CT_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +00001246 index = parm->index;
1247 err = tcf_idr_check_alloc(tn, &index, a, bind);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001248 if (err < 0)
1249 return err;
1250
1251 if (!err) {
Vlad Buslove3822672019-10-30 16:09:06 +02001252 err = tcf_idr_create_from_flags(tn, index, est, a,
1253 &act_ct_ops, bind, flags);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001254 if (err) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +00001255 tcf_idr_cleanup(tn, index);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001256 return err;
1257 }
1258 res = ACT_P_CREATED;
1259 } else {
1260 if (bind)
1261 return 0;
1262
1263 if (!replace) {
1264 tcf_idr_release(*a, bind);
1265 return -EEXIST;
1266 }
1267 }
1268 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1269 if (err < 0)
1270 goto cleanup;
1271
1272 c = to_ct(*a);
1273
1274 params = kzalloc(sizeof(*params), GFP_KERNEL);
1275 if (unlikely(!params)) {
1276 err = -ENOMEM;
1277 goto cleanup;
1278 }
1279
1280 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1281 if (err)
1282 goto cleanup;
1283
Paul Blakeyc34b9612020-03-03 15:07:49 +02001284 err = tcf_ct_flow_table_get(params);
1285 if (err)
1286 goto cleanup;
1287
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001288 spin_lock_bh(&c->tcf_lock);
1289 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Paul E. McKenney445d3742019-09-23 16:09:18 -07001290 params = rcu_replace_pointer(c->params, params,
1291 lockdep_is_held(&c->tcf_lock));
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001292 spin_unlock_bh(&c->tcf_lock);
1293
1294 if (goto_ch)
1295 tcf_chain_put_by_act(goto_ch);
1296 if (params)
Paul Blakeydd2af102020-03-18 12:50:33 +02001297 call_rcu(&params->rcu, tcf_ct_params_free);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001298
1299 return res;
1300
1301cleanup:
1302 if (goto_ch)
1303 tcf_chain_put_by_act(goto_ch);
1304 kfree(params);
1305 tcf_idr_release(*a, bind);
1306 return err;
1307}
1308
1309static void tcf_ct_cleanup(struct tc_action *a)
1310{
1311 struct tcf_ct_params *params;
1312 struct tcf_ct *c = to_ct(a);
1313
1314 params = rcu_dereference_protected(c->params, 1);
1315 if (params)
1316 call_rcu(&params->rcu, tcf_ct_params_free);
1317}
1318
1319static int tcf_ct_dump_key_val(struct sk_buff *skb,
1320 void *val, int val_type,
1321 void *mask, int mask_type,
1322 int len)
1323{
1324 int err;
1325
1326 if (mask && !memchr_inv(mask, 0, len))
1327 return 0;
1328
1329 err = nla_put(skb, val_type, len, val);
1330 if (err)
1331 return err;
1332
1333 if (mask_type != TCA_CT_UNSPEC) {
1334 err = nla_put(skb, mask_type, len, mask);
1335 if (err)
1336 return err;
1337 }
1338
1339 return 0;
1340}
1341
1342static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1343{
1344 struct nf_nat_range2 *range = &p->range;
1345
1346 if (!(p->ct_action & TCA_CT_ACT_NAT))
1347 return 0;
1348
1349 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1350 return 0;
1351
1352 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1353 if (p->ipv4_range) {
1354 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1355 range->min_addr.ip))
1356 return -1;
1357 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1358 range->max_addr.ip))
1359 return -1;
1360 } else {
1361 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1362 &range->min_addr.in6))
1363 return -1;
1364 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1365 &range->max_addr.in6))
1366 return -1;
1367 }
1368 }
1369
1370 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1371 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1372 range->min_proto.all))
1373 return -1;
1374 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1375 range->max_proto.all))
1376 return -1;
1377 }
1378
1379 return 0;
1380}
1381
1382static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1383 int bind, int ref)
1384{
1385 unsigned char *b = skb_tail_pointer(skb);
1386 struct tcf_ct *c = to_ct(a);
1387 struct tcf_ct_params *p;
1388
1389 struct tc_ct opt = {
1390 .index = c->tcf_index,
1391 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1392 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1393 };
1394 struct tcf_t t;
1395
1396 spin_lock_bh(&c->tcf_lock);
1397 p = rcu_dereference_protected(c->params,
1398 lockdep_is_held(&c->tcf_lock));
1399 opt.action = c->tcf_action;
1400
1401 if (tcf_ct_dump_key_val(skb,
1402 &p->ct_action, TCA_CT_ACTION,
1403 NULL, TCA_CT_UNSPEC,
1404 sizeof(p->ct_action)))
1405 goto nla_put_failure;
1406
1407 if (p->ct_action & TCA_CT_ACT_CLEAR)
1408 goto skip_dump;
1409
1410 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1411 tcf_ct_dump_key_val(skb,
1412 &p->mark, TCA_CT_MARK,
1413 &p->mark_mask, TCA_CT_MARK_MASK,
1414 sizeof(p->mark)))
1415 goto nla_put_failure;
1416
1417 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1418 tcf_ct_dump_key_val(skb,
1419 p->labels, TCA_CT_LABELS,
1420 p->labels_mask, TCA_CT_LABELS_MASK,
1421 sizeof(p->labels)))
1422 goto nla_put_failure;
1423
1424 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1425 tcf_ct_dump_key_val(skb,
1426 &p->zone, TCA_CT_ZONE,
1427 NULL, TCA_CT_UNSPEC,
1428 sizeof(p->zone)))
1429 goto nla_put_failure;
1430
1431 if (tcf_ct_dump_nat(skb, p))
1432 goto nla_put_failure;
1433
1434skip_dump:
1435 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1436 goto nla_put_failure;
1437
1438 tcf_tm_dump(&t, &c->tcf_tm);
1439 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1440 goto nla_put_failure;
1441 spin_unlock_bh(&c->tcf_lock);
1442
1443 return skb->len;
1444nla_put_failure:
1445 spin_unlock_bh(&c->tcf_lock);
1446 nlmsg_trim(skb, b);
1447 return -1;
1448}
1449
1450static int tcf_ct_walker(struct net *net, struct sk_buff *skb,
1451 struct netlink_callback *cb, int type,
1452 const struct tc_action_ops *ops,
1453 struct netlink_ext_ack *extack)
1454{
1455 struct tc_action_net *tn = net_generic(net, ct_net_id);
1456
1457 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1458}
1459
1460static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index)
1461{
1462 struct tc_action_net *tn = net_generic(net, ct_net_id);
1463
1464 return tcf_idr_search(tn, a, index);
1465}
1466
Po Liu4b61d3e2020-06-19 14:01:07 +08001467static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1468 u64 drops, u64 lastuse, bool hw)
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001469{
1470 struct tcf_ct *c = to_ct(a);
1471
Po Liu4b61d3e2020-06-19 14:01:07 +08001472 tcf_action_update_stats(a, bytes, packets, drops, hw);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001473 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1474}
1475
1476static struct tc_action_ops act_ct_ops = {
1477 .kind = "ct",
1478 .id = TCA_ID_CT,
1479 .owner = THIS_MODULE,
1480 .act = tcf_ct_act,
1481 .dump = tcf_ct_dump,
1482 .init = tcf_ct_init,
1483 .cleanup = tcf_ct_cleanup,
1484 .walk = tcf_ct_walker,
1485 .lookup = tcf_ct_search,
1486 .stats_update = tcf_stats_update,
1487 .size = sizeof(struct tcf_ct),
1488};
1489
1490static __net_init int ct_init_net(struct net *net)
1491{
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001492 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001493 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1494
1495 if (nf_connlabels_get(net, n_bits - 1)) {
1496 tn->labels = false;
1497 pr_err("act_ct: Failed to set connlabels length");
1498 } else {
1499 tn->labels = true;
1500 }
1501
Cong Wang981471b2019-08-25 10:01:32 -07001502 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001503}
1504
1505static void __net_exit ct_exit_net(struct list_head *net_list)
1506{
1507 struct net *net;
1508
1509 rtnl_lock();
1510 list_for_each_entry(net, net_list, exit_list) {
1511 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1512
1513 if (tn->labels)
1514 nf_connlabels_put(net);
1515 }
1516 rtnl_unlock();
1517
1518 tc_action_net_exit(net_list, ct_net_id);
1519}
1520
1521static struct pernet_operations ct_net_ops = {
1522 .init = ct_init_net,
1523 .exit_batch = ct_exit_net,
1524 .id = &ct_net_id,
1525 .size = sizeof(struct tc_ct_action_net),
1526};
1527
1528static int __init ct_init_module(void)
1529{
Paul Blakeyc34b9612020-03-03 15:07:49 +02001530 int err;
1531
1532 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1533 if (!act_ct_wq)
1534 return -ENOMEM;
1535
1536 err = tcf_ct_flow_tables_init();
1537 if (err)
1538 goto err_tbl_init;
1539
1540 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1541 if (err)
1542 goto err_register;
1543
1544 return 0;
1545
Paul Blakeyc34b9612020-03-03 15:07:49 +02001546err_register:
1547 tcf_ct_flow_tables_uninit();
liujian8c5c51f2020-07-30 16:14:28 +08001548err_tbl_init:
1549 destroy_workqueue(act_ct_wq);
Paul Blakeyc34b9612020-03-03 15:07:49 +02001550 return err;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001551}
1552
1553static void __exit ct_cleanup_module(void)
1554{
1555 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
Paul Blakeyc34b9612020-03-03 15:07:49 +02001556 tcf_ct_flow_tables_uninit();
1557 destroy_workqueue(act_ct_wq);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001558}
1559
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03001560module_init(ct_init_module);
1561module_exit(ct_cleanup_module);
1562MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1563MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1564MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1565MODULE_DESCRIPTION("Connection tracking action");
1566MODULE_LICENSE("GPL v2");