|
15 | 15 | #include <linux/pkt_cls.h>
|
16 | 16 | #include <linux/ip.h>
|
17 | 17 | #include <linux/ipv6.h>
|
| 18 | +#include <linux/rhashtable.h> |
18 | 19 | #include <net/netlink.h>
|
19 | 20 | #include <net/pkt_sched.h>
|
20 | 21 | #include <net/pkt_cls.h>
|
|
24 | 25 | #include <uapi/linux/tc_act/tc_ct.h>
|
25 | 26 | #include <net/tc_act/tc_ct.h>
|
26 | 27 |
|
| 28 | +#include <net/netfilter/nf_flow_table.h> |
27 | 29 | #include <net/netfilter/nf_conntrack.h>
|
28 | 30 | #include <net/netfilter/nf_conntrack_core.h>
|
29 | 31 | #include <net/netfilter/nf_conntrack_zones.h>
|
30 | 32 | #include <net/netfilter/nf_conntrack_helper.h>
|
31 | 33 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
|
32 | 34 | #include <uapi/linux/netfilter/nf_nat.h>
|
33 | 35 |
|
| 36 | +static struct workqueue_struct *act_ct_wq; |
| 37 | +static struct rhashtable zones_ht; |
| 38 | +static DEFINE_SPINLOCK(zones_lock); |
| 39 | + |
| 40 | +struct tcf_ct_flow_table { |
| 41 | + struct rhash_head node; /* In zones tables */ |
| 42 | + |
| 43 | + struct rcu_work rwork; |
| 44 | + struct nf_flowtable nf_ft; |
| 45 | + u16 zone; |
| 46 | + u32 ref; |
| 47 | + |
| 48 | + bool dying; |
| 49 | +}; |
| 50 | + |
| 51 | +static const struct rhashtable_params zones_params = { |
| 52 | + .head_offset = offsetof(struct tcf_ct_flow_table, node), |
| 53 | + .key_offset = offsetof(struct tcf_ct_flow_table, zone), |
| 54 | + .key_len = sizeof_field(struct tcf_ct_flow_table, zone), |
| 55 | + .automatic_shrinking = true, |
| 56 | +}; |
| 57 | + |
| 58 | +static struct nf_flowtable_type flowtable_ct = { |
| 59 | + .owner = THIS_MODULE, |
| 60 | +}; |
| 61 | + |
| 62 | +static int tcf_ct_flow_table_get(struct tcf_ct_params *params) |
| 63 | +{ |
| 64 | + struct tcf_ct_flow_table *ct_ft; |
| 65 | + int err = -ENOMEM; |
| 66 | + |
| 67 | + spin_lock_bh(&zones_lock); |
| 68 | + ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); |
| 69 | + if (ct_ft) |
| 70 | + goto take_ref; |
| 71 | + |
| 72 | + ct_ft = kzalloc(sizeof(*ct_ft), GFP_ATOMIC); |
| 73 | + if (!ct_ft) |
| 74 | + goto err_alloc; |
| 75 | + |
| 76 | + ct_ft->zone = params->zone; |
| 77 | + err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); |
| 78 | + if (err) |
| 79 | + goto err_insert; |
| 80 | + |
| 81 | + ct_ft->nf_ft.type = &flowtable_ct; |
| 82 | + err = nf_flow_table_init(&ct_ft->nf_ft); |
| 83 | + if (err) |
| 84 | + goto err_init; |
| 85 | + |
| 86 | + __module_get(THIS_MODULE); |
| 87 | +take_ref: |
| 88 | + params->ct_ft = ct_ft; |
| 89 | + ct_ft->ref++; |
| 90 | + spin_unlock_bh(&zones_lock); |
| 91 | + |
| 92 | + return 0; |
| 93 | + |
| 94 | +err_init: |
| 95 | + rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); |
| 96 | +err_insert: |
| 97 | + kfree(ct_ft); |
| 98 | +err_alloc: |
| 99 | + spin_unlock_bh(&zones_lock); |
| 100 | + return err; |
| 101 | +} |
| 102 | + |
| 103 | +static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) |
| 104 | +{ |
| 105 | + struct tcf_ct_flow_table *ct_ft; |
| 106 | + |
| 107 | + ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, |
| 108 | + rwork); |
| 109 | + nf_flow_table_free(&ct_ft->nf_ft); |
| 110 | + kfree(ct_ft); |
| 111 | + |
| 112 | + module_put(THIS_MODULE); |
| 113 | +} |
| 114 | + |
| 115 | +static void tcf_ct_flow_table_put(struct tcf_ct_params *params) |
| 116 | +{ |
| 117 | + struct tcf_ct_flow_table *ct_ft = params->ct_ft; |
| 118 | + |
| 119 | + spin_lock_bh(&zones_lock); |
| 120 | + if (--params->ct_ft->ref == 0) { |
| 121 | + rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); |
| 122 | + INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); |
| 123 | + queue_rcu_work(act_ct_wq, &ct_ft->rwork); |
| 124 | + } |
| 125 | + spin_unlock_bh(&zones_lock); |
| 126 | +} |
| 127 | + |
| 128 | +static int tcf_ct_flow_tables_init(void) |
| 129 | +{ |
| 130 | + return rhashtable_init(&zones_ht, &zones_params); |
| 131 | +} |
| 132 | + |
| 133 | +static void tcf_ct_flow_tables_uninit(void) |
| 134 | +{ |
| 135 | + rhashtable_destroy(&zones_ht); |
| 136 | +} |
| 137 | + |
34 | 138 | static struct tc_action_ops act_ct_ops;
|
35 | 139 | static unsigned int ct_net_id;
|
36 | 140 |
|
@@ -207,6 +311,8 @@ static void tcf_ct_params_free(struct rcu_head *head)
|
207 | 311 | struct tcf_ct_params *params = container_of(head,
|
208 | 312 | struct tcf_ct_params, rcu);
|
209 | 313 |
|
| 314 | + tcf_ct_flow_table_put(params); |
| 315 | + |
210 | 316 | if (params->tmpl)
|
211 | 317 | nf_conntrack_put(¶ms->tmpl->ct_general);
|
212 | 318 | kfree(params);
|
@@ -730,6 +836,10 @@ static int tcf_ct_init(struct net *net, struct nlattr *nla,
|
730 | 836 | if (err)
|
731 | 837 | goto cleanup;
|
732 | 838 |
|
| 839 | + err = tcf_ct_flow_table_get(params); |
| 840 | + if (err) |
| 841 | + goto cleanup; |
| 842 | + |
733 | 843 | spin_lock_bh(&c->tcf_lock);
|
734 | 844 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
|
735 | 845 | params = rcu_replace_pointer(c->params, params,
|
@@ -974,12 +1084,34 @@ static struct pernet_operations ct_net_ops = {
|
974 | 1084 |
|
975 | 1085 | static int __init ct_init_module(void)
|
976 | 1086 | {
|
977 |
| - return tcf_register_action(&act_ct_ops, &ct_net_ops); |
| 1087 | + int err; |
| 1088 | + |
| 1089 | + act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); |
| 1090 | + if (!act_ct_wq) |
| 1091 | + return -ENOMEM; |
| 1092 | + |
| 1093 | + err = tcf_ct_flow_tables_init(); |
| 1094 | + if (err) |
| 1095 | + goto err_tbl_init; |
| 1096 | + |
| 1097 | + err = tcf_register_action(&act_ct_ops, &ct_net_ops); |
| 1098 | + if (err) |
| 1099 | + goto err_register; |
| 1100 | + |
| 1101 | + return 0; |
| 1102 | + |
| 1103 | +err_tbl_init: |
| 1104 | + destroy_workqueue(act_ct_wq); |
| 1105 | +err_register: |
| 1106 | + tcf_ct_flow_tables_uninit(); |
| 1107 | + return err; |
978 | 1108 | }
|
979 | 1109 |
|
980 | 1110 | static void __exit ct_cleanup_module(void)
|
981 | 1111 | {
|
982 | 1112 | tcf_unregister_action(&act_ct_ops, &ct_net_ops);
|
| 1113 | + tcf_ct_flow_tables_uninit(); |
| 1114 | + destroy_workqueue(act_ct_wq); |
983 | 1115 | }
|
984 | 1116 |
|
985 | 1117 | module_init(ct_init_module);
|
|
0 commit comments