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