treewide: Add SPDX license identifier for more missed files
[linux-2.6-block.git] / net / netfilter / nft_flow_offload.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
a3c90f7a
PNA
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/init.h>
5#include <linux/netlink.h>
6#include <linux/netfilter.h>
7#include <linux/workqueue.h>
8#include <linux/spinlock.h>
9#include <linux/netfilter/nf_tables.h>
10#include <net/ip.h> /* for ipv4 options. */
11#include <net/netfilter/nf_tables.h>
12#include <net/netfilter/nf_tables_core.h>
13#include <net/netfilter/nf_conntrack_core.h>
14#include <linux/netfilter/nf_conntrack_common.h>
15#include <net/netfilter/nf_flow_table.h>
2314e879 16#include <net/netfilter/nf_conntrack_helper.h>
a3c90f7a
PNA
17
18struct nft_flow_offload {
19 struct nft_flowtable *flowtable;
20};
21
22static int nft_flow_route(const struct nft_pktinfo *pkt,
23 const struct nf_conn *ct,
24 struct nf_flow_route *route,
25 enum ip_conntrack_dir dir)
26{
27 struct dst_entry *this_dst = skb_dst(pkt->skb);
28 struct dst_entry *other_dst = NULL;
29 struct flowi fl;
30
31 memset(&fl, 0, sizeof(fl));
32 switch (nft_pf(pkt)) {
33 case NFPROTO_IPV4:
a799aea0 34 fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
10f4e765 35 fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
a3c90f7a
PNA
36 break;
37 case NFPROTO_IPV6:
a799aea0 38 fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
10f4e765 39 fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
a3c90f7a
PNA
40 break;
41 }
42
43 nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
44 if (!other_dst)
45 return -ENOENT;
46
47 route->tuple[dir].dst = this_dst;
a3c90f7a 48 route->tuple[!dir].dst = other_dst;
a3c90f7a
PNA
49
50 return 0;
51}
52
53static bool nft_flow_offload_skip(struct sk_buff *skb)
54{
55 struct ip_options *opt = &(IPCB(skb)->opt);
56
57 if (unlikely(opt->optlen))
58 return true;
59 if (skb_sec_path(skb))
60 return true;
61
62 return false;
63}
64
65static void nft_flow_offload_eval(const struct nft_expr *expr,
66 struct nft_regs *regs,
67 const struct nft_pktinfo *pkt)
68{
69 struct nft_flow_offload *priv = nft_expr_priv(expr);
70 struct nf_flowtable *flowtable = &priv->flowtable->data;
2314e879 71 const struct nf_conn_help *help;
a3c90f7a
PNA
72 enum ip_conntrack_info ctinfo;
73 struct nf_flow_route route;
74 struct flow_offload *flow;
75 enum ip_conntrack_dir dir;
76 struct nf_conn *ct;
77 int ret;
78
79 if (nft_flow_offload_skip(pkt->skb))
80 goto out;
81
82 ct = nf_ct_get(pkt->skb, &ctinfo);
83 if (!ct)
84 goto out;
85
86 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
87 case IPPROTO_TCP:
88 case IPPROTO_UDP:
89 break;
90 default:
91 goto out;
92 }
93
2314e879
HY
94 help = nfct_help(ct);
95 if (help)
a3c90f7a
PNA
96 goto out;
97
270a8a29 98 if (!nf_ct_is_confirmed(ct))
a3c90f7a
PNA
99 goto out;
100
101 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
102 goto out;
103
104 dir = CTINFO2DIR(ctinfo);
105 if (nft_flow_route(pkt, ct, &route, dir) < 0)
106 goto err_flow_route;
107
108 flow = flow_offload_alloc(ct, &route);
109 if (!flow)
110 goto err_flow_alloc;
111
112 ret = flow_offload_add(flowtable, flow);
113 if (ret < 0)
114 goto err_flow_add;
115
26a302af 116 dst_release(route.tuple[!dir].dst);
a3c90f7a
PNA
117 return;
118
119err_flow_add:
120 flow_offload_free(flow);
121err_flow_alloc:
122 dst_release(route.tuple[!dir].dst);
123err_flow_route:
124 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
125out:
126 regs->verdict.code = NFT_BREAK;
127}
128
129static int nft_flow_offload_validate(const struct nft_ctx *ctx,
130 const struct nft_expr *expr,
131 const struct nft_data **data)
132{
133 unsigned int hook_mask = (1 << NF_INET_FORWARD);
134
135 return nft_chain_validate_hooks(ctx->chain, hook_mask);
136}
137
138static int nft_flow_offload_init(const struct nft_ctx *ctx,
139 const struct nft_expr *expr,
140 const struct nlattr * const tb[])
141{
142 struct nft_flow_offload *priv = nft_expr_priv(expr);
143 u8 genmask = nft_genmask_next(ctx->net);
144 struct nft_flowtable *flowtable;
145
146 if (!tb[NFTA_FLOW_TABLE_NAME])
147 return -EINVAL;
148
cac20fcd
PNA
149 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
150 genmask);
a3c90f7a
PNA
151 if (IS_ERR(flowtable))
152 return PTR_ERR(flowtable);
153
154 priv->flowtable = flowtable;
155 flowtable->use++;
156
36596dad 157 return nf_ct_netns_get(ctx->net, ctx->family);
a3c90f7a
PNA
158}
159
160static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
161 const struct nft_expr *expr)
162{
163 struct nft_flow_offload *priv = nft_expr_priv(expr);
164
165 priv->flowtable->use--;
36596dad 166 nf_ct_netns_put(ctx->net, ctx->family);
a3c90f7a
PNA
167}
168
169static int nft_flow_offload_dump(struct sk_buff *skb, const struct nft_expr *expr)
170{
171 struct nft_flow_offload *priv = nft_expr_priv(expr);
172
173 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
174 goto nla_put_failure;
175
176 return 0;
177
178nla_put_failure:
179 return -1;
180}
181
182static struct nft_expr_type nft_flow_offload_type;
183static const struct nft_expr_ops nft_flow_offload_ops = {
184 .type = &nft_flow_offload_type,
185 .size = NFT_EXPR_SIZE(sizeof(struct nft_flow_offload)),
186 .eval = nft_flow_offload_eval,
187 .init = nft_flow_offload_init,
188 .destroy = nft_flow_offload_destroy,
189 .validate = nft_flow_offload_validate,
190 .dump = nft_flow_offload_dump,
191};
192
193static struct nft_expr_type nft_flow_offload_type __read_mostly = {
194 .name = "flow_offload",
195 .ops = &nft_flow_offload_ops,
196 .maxattr = NFTA_FLOW_MAX,
197 .owner = THIS_MODULE,
198};
199
a3c90f7a
PNA
200static int flow_offload_netdev_event(struct notifier_block *this,
201 unsigned long event, void *ptr)
202{
203 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
204
205 if (event != NETDEV_DOWN)
206 return NOTIFY_DONE;
207
5f1be84a 208 nf_flow_table_cleanup(dev);
a3c90f7a
PNA
209
210 return NOTIFY_DONE;
211}
212
213static struct notifier_block flow_offload_netdev_notifier = {
214 .notifier_call = flow_offload_netdev_event,
215};
216
217static int __init nft_flow_offload_module_init(void)
218{
219 int err;
220
584eab29
TY
221 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
222 if (err)
223 goto err;
a3c90f7a
PNA
224
225 err = nft_register_expr(&nft_flow_offload_type);
226 if (err < 0)
227 goto register_expr;
228
229 return 0;
230
231register_expr:
232 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
584eab29 233err:
a3c90f7a
PNA
234 return err;
235}
236
237static void __exit nft_flow_offload_module_exit(void)
238{
a3c90f7a
PNA
239 nft_unregister_expr(&nft_flow_offload_type);
240 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
a3c90f7a
PNA
241}
242
243module_init(nft_flow_offload_module_init);
244module_exit(nft_flow_offload_module_exit);
245
246MODULE_LICENSE("GPL");
247MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
248MODULE_ALIAS_NFT_EXPR("flow_offload");