]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nft_flow_offload.c
Merge tag 'nfs-for-5.4-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[mirror_ubuntu-jammy-kernel.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>
40d102cd 9#include <linux/netfilter/nf_conntrack_common.h>
a3c90f7a
PNA
10#include <linux/netfilter/nf_tables.h>
11#include <net/ip.h> /* for ipv4 options. */
12#include <net/netfilter/nf_tables.h>
13#include <net/netfilter/nf_tables_core.h>
14#include <net/netfilter/nf_conntrack_core.h>
40d102cd 15#include <net/netfilter/nf_conntrack_extend.h>
a3c90f7a
PNA
16#include <net/netfilter/nf_flow_table.h>
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
69aeb538 53static bool nft_flow_offload_skip(struct sk_buff *skb, int family)
a3c90f7a 54{
a3c90f7a
PNA
55 if (skb_sec_path(skb))
56 return true;
57
69aeb538
FW
58 if (family == NFPROTO_IPV4) {
59 const struct ip_options *opt;
60
61 opt = &(IPCB(skb)->opt);
62
63 if (unlikely(opt->optlen))
64 return true;
65 }
66
a3c90f7a
PNA
67 return false;
68}
69
70static void nft_flow_offload_eval(const struct nft_expr *expr,
71 struct nft_regs *regs,
72 const struct nft_pktinfo *pkt)
73{
74 struct nft_flow_offload *priv = nft_expr_priv(expr);
75 struct nf_flowtable *flowtable = &priv->flowtable->data;
dfe42be1 76 struct tcphdr _tcph, *tcph = NULL;
a3c90f7a
PNA
77 enum ip_conntrack_info ctinfo;
78 struct nf_flow_route route;
79 struct flow_offload *flow;
80 enum ip_conntrack_dir dir;
81 struct nf_conn *ct;
82 int ret;
83
69aeb538 84 if (nft_flow_offload_skip(pkt->skb, nft_pf(pkt)))
a3c90f7a
PNA
85 goto out;
86
87 ct = nf_ct_get(pkt->skb, &ctinfo);
88 if (!ct)
89 goto out;
90
91 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
92 case IPPROTO_TCP:
dfe42be1
PNA
93 tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff,
94 sizeof(_tcph), &_tcph);
95 if (unlikely(!tcph || tcph->fin || tcph->rst))
96 goto out;
8437a620 97 break;
a3c90f7a
PNA
98 case IPPROTO_UDP:
99 break;
100 default:
101 goto out;
102 }
103
91a9048f
FW
104 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
105 ct->status & IPS_SEQ_ADJUST)
a3c90f7a
PNA
106 goto out;
107
270a8a29 108 if (!nf_ct_is_confirmed(ct))
a3c90f7a
PNA
109 goto out;
110
111 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
112 goto out;
113
114 dir = CTINFO2DIR(ctinfo);
115 if (nft_flow_route(pkt, ct, &route, dir) < 0)
116 goto err_flow_route;
117
118 flow = flow_offload_alloc(ct, &route);
119 if (!flow)
120 goto err_flow_alloc;
121
dfe42be1 122 if (tcph) {
8437a620
FW
123 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
124 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
125 }
126
a3c90f7a
PNA
127 ret = flow_offload_add(flowtable, flow);
128 if (ret < 0)
129 goto err_flow_add;
130
26a302af 131 dst_release(route.tuple[!dir].dst);
a3c90f7a
PNA
132 return;
133
134err_flow_add:
135 flow_offload_free(flow);
136err_flow_alloc:
137 dst_release(route.tuple[!dir].dst);
138err_flow_route:
139 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
140out:
141 regs->verdict.code = NFT_BREAK;
142}
143
144static int nft_flow_offload_validate(const struct nft_ctx *ctx,
145 const struct nft_expr *expr,
146 const struct nft_data **data)
147{
148 unsigned int hook_mask = (1 << NF_INET_FORWARD);
149
150 return nft_chain_validate_hooks(ctx->chain, hook_mask);
151}
152
14c41586
PNA
153static const struct nla_policy nft_flow_offload_policy[NFTA_FLOW_MAX + 1] = {
154 [NFTA_FLOW_TABLE_NAME] = { .type = NLA_STRING,
155 .len = NFT_NAME_MAXLEN - 1 },
156};
157
a3c90f7a
PNA
158static int nft_flow_offload_init(const struct nft_ctx *ctx,
159 const struct nft_expr *expr,
160 const struct nlattr * const tb[])
161{
162 struct nft_flow_offload *priv = nft_expr_priv(expr);
163 u8 genmask = nft_genmask_next(ctx->net);
164 struct nft_flowtable *flowtable;
165
166 if (!tb[NFTA_FLOW_TABLE_NAME])
167 return -EINVAL;
168
cac20fcd
PNA
169 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
170 genmask);
a3c90f7a
PNA
171 if (IS_ERR(flowtable))
172 return PTR_ERR(flowtable);
173
174 priv->flowtable = flowtable;
175 flowtable->use++;
176
36596dad 177 return nf_ct_netns_get(ctx->net, ctx->family);
a3c90f7a
PNA
178}
179
180static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
181 const struct nft_expr *expr)
182{
183 struct nft_flow_offload *priv = nft_expr_priv(expr);
184
185 priv->flowtable->use--;
36596dad 186 nf_ct_netns_put(ctx->net, ctx->family);
a3c90f7a
PNA
187}
188
189static int nft_flow_offload_dump(struct sk_buff *skb, const struct nft_expr *expr)
190{
191 struct nft_flow_offload *priv = nft_expr_priv(expr);
192
193 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
194 goto nla_put_failure;
195
196 return 0;
197
198nla_put_failure:
199 return -1;
200}
201
202static struct nft_expr_type nft_flow_offload_type;
203static const struct nft_expr_ops nft_flow_offload_ops = {
204 .type = &nft_flow_offload_type,
205 .size = NFT_EXPR_SIZE(sizeof(struct nft_flow_offload)),
206 .eval = nft_flow_offload_eval,
207 .init = nft_flow_offload_init,
208 .destroy = nft_flow_offload_destroy,
209 .validate = nft_flow_offload_validate,
210 .dump = nft_flow_offload_dump,
211};
212
213static struct nft_expr_type nft_flow_offload_type __read_mostly = {
214 .name = "flow_offload",
215 .ops = &nft_flow_offload_ops,
14c41586 216 .policy = nft_flow_offload_policy,
a3c90f7a
PNA
217 .maxattr = NFTA_FLOW_MAX,
218 .owner = THIS_MODULE,
219};
220
a3c90f7a
PNA
221static int flow_offload_netdev_event(struct notifier_block *this,
222 unsigned long event, void *ptr)
223{
224 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
225
226 if (event != NETDEV_DOWN)
227 return NOTIFY_DONE;
228
5f1be84a 229 nf_flow_table_cleanup(dev);
a3c90f7a
PNA
230
231 return NOTIFY_DONE;
232}
233
234static struct notifier_block flow_offload_netdev_notifier = {
235 .notifier_call = flow_offload_netdev_event,
236};
237
238static int __init nft_flow_offload_module_init(void)
239{
240 int err;
241
584eab29
TY
242 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
243 if (err)
244 goto err;
a3c90f7a
PNA
245
246 err = nft_register_expr(&nft_flow_offload_type);
247 if (err < 0)
248 goto register_expr;
249
250 return 0;
251
252register_expr:
253 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
584eab29 254err:
a3c90f7a
PNA
255 return err;
256}
257
258static void __exit nft_flow_offload_module_exit(void)
259{
a3c90f7a
PNA
260 nft_unregister_expr(&nft_flow_offload_type);
261 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
a3c90f7a
PNA
262}
263
264module_init(nft_flow_offload_module_init);
265module_exit(nft_flow_offload_module_exit);
266
267MODULE_LICENSE("GPL");
268MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
269MODULE_ALIAS_NFT_EXPR("flow_offload");