1 // SPDX-License-Identifier: GPL-2.0-only
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_conntrack_common.h>
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>
15 #include <net/netfilter/nf_conntrack_extend.h>
16 #include <net/netfilter/nf_flow_table.h>
17
18 struct nft_flow_offload {
19 struct nft_flowtable *flowtable;
20 };
21
nft_xmit_type(struct dst_entry * dst)22 static enum flow_offload_xmit_type nft_xmit_type(struct dst_entry *dst)
23 {
24 if (dst_xfrm(dst))
25 return FLOW_OFFLOAD_XMIT_XFRM;
26
27 return FLOW_OFFLOAD_XMIT_NEIGH;
28 }
29
nft_default_forward_path(struct nf_flow_route * route,struct dst_entry * dst_cache,enum ip_conntrack_dir dir)30 static void nft_default_forward_path(struct nf_flow_route *route,
31 struct dst_entry *dst_cache,
32 enum ip_conntrack_dir dir)
33 {
34 route->tuple[!dir].in.ifindex = dst_cache->dev->ifindex;
35 route->tuple[dir].dst = dst_cache;
36 route->tuple[dir].xmit_type = nft_xmit_type(dst_cache);
37 }
38
nft_is_valid_ether_device(const struct net_device * dev)39 static bool nft_is_valid_ether_device(const struct net_device *dev)
40 {
41 if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
42 dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
43 return false;
44
45 return true;
46 }
47
nft_dev_fill_forward_path(const struct nf_flow_route * route,const struct dst_entry * dst_cache,const struct nf_conn * ct,enum ip_conntrack_dir dir,u8 * ha,struct net_device_path_stack * stack)48 static int nft_dev_fill_forward_path(const struct nf_flow_route *route,
49 const struct dst_entry *dst_cache,
50 const struct nf_conn *ct,
51 enum ip_conntrack_dir dir, u8 *ha,
52 struct net_device_path_stack *stack)
53 {
54 const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
55 struct net_device *dev = dst_cache->dev;
56 struct neighbour *n;
57 u8 nud_state;
58
59 if (!nft_is_valid_ether_device(dev))
60 goto out;
61
62 n = dst_neigh_lookup(dst_cache, daddr);
63 if (!n)
64 return -1;
65
66 read_lock_bh(&n->lock);
67 nud_state = n->nud_state;
68 ether_addr_copy(ha, n->ha);
69 read_unlock_bh(&n->lock);
70 neigh_release(n);
71
72 if (!(nud_state & NUD_VALID))
73 return -1;
74
75 out:
76 return dev_fill_forward_path(dev, ha, stack);
77 }
78
79 struct nft_forward_info {
80 const struct net_device *indev;
81 const struct net_device *outdev;
82 const struct net_device *hw_outdev;
83 struct id {
84 __u16 id;
85 __be16 proto;
86 } encap[NF_FLOW_TABLE_ENCAP_MAX];
87 u8 num_encaps;
88 u8 ingress_vlans;
89 u8 h_source[ETH_ALEN];
90 u8 h_dest[ETH_ALEN];
91 enum flow_offload_xmit_type xmit_type;
92 };
93
nft_dev_path_info(const struct net_device_path_stack * stack,struct nft_forward_info * info,unsigned char * ha,struct nf_flowtable * flowtable)94 static void nft_dev_path_info(const struct net_device_path_stack *stack,
95 struct nft_forward_info *info,
96 unsigned char *ha, struct nf_flowtable *flowtable)
97 {
98 const struct net_device_path *path;
99 int i;
100
101 memcpy(info->h_dest, ha, ETH_ALEN);
102
103 for (i = 0; i < stack->num_paths; i++) {
104 path = &stack->path[i];
105 switch (path->type) {
106 case DEV_PATH_ETHERNET:
107 case DEV_PATH_DSA:
108 case DEV_PATH_VLAN:
109 case DEV_PATH_PPPOE:
110 info->indev = path->dev;
111 if (is_zero_ether_addr(info->h_source))
112 memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
113
114 if (path->type == DEV_PATH_ETHERNET)
115 break;
116 if (path->type == DEV_PATH_DSA) {
117 i = stack->num_paths;
118 break;
119 }
120
121 /* DEV_PATH_VLAN and DEV_PATH_PPPOE */
122 if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
123 info->indev = NULL;
124 break;
125 }
126 if (!info->outdev)
127 info->outdev = path->dev;
128 info->encap[info->num_encaps].id = path->encap.id;
129 info->encap[info->num_encaps].proto = path->encap.proto;
130 info->num_encaps++;
131 if (path->type == DEV_PATH_PPPOE)
132 memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
133 break;
134 case DEV_PATH_BRIDGE:
135 if (is_zero_ether_addr(info->h_source))
136 memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
137
138 switch (path->bridge.vlan_mode) {
139 case DEV_PATH_BR_VLAN_UNTAG_HW:
140 info->ingress_vlans |= BIT(info->num_encaps - 1);
141 break;
142 case DEV_PATH_BR_VLAN_TAG:
143 info->encap[info->num_encaps].id = path->bridge.vlan_id;
144 info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
145 info->num_encaps++;
146 break;
147 case DEV_PATH_BR_VLAN_UNTAG:
148 info->num_encaps--;
149 break;
150 case DEV_PATH_BR_VLAN_KEEP:
151 break;
152 }
153 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
154 break;
155 default:
156 info->indev = NULL;
157 break;
158 }
159 }
160 if (!info->outdev)
161 info->outdev = info->indev;
162
163 info->hw_outdev = info->indev;
164
165 if (nf_flowtable_hw_offload(flowtable) &&
166 nft_is_valid_ether_device(info->indev))
167 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
168 }
169
nft_flowtable_find_dev(const struct net_device * dev,struct nft_flowtable * ft)170 static bool nft_flowtable_find_dev(const struct net_device *dev,
171 struct nft_flowtable *ft)
172 {
173 struct nft_hook *hook;
174 bool found = false;
175
176 list_for_each_entry_rcu(hook, &ft->hook_list, list) {
177 if (hook->ops.dev != dev)
178 continue;
179
180 found = true;
181 break;
182 }
183
184 return found;
185 }
186
nft_dev_forward_path(struct nf_flow_route * route,const struct nf_conn * ct,enum ip_conntrack_dir dir,struct nft_flowtable * ft)187 static void nft_dev_forward_path(struct nf_flow_route *route,
188 const struct nf_conn *ct,
189 enum ip_conntrack_dir dir,
190 struct nft_flowtable *ft)
191 {
192 const struct dst_entry *dst = route->tuple[dir].dst;
193 struct net_device_path_stack stack;
194 struct nft_forward_info info = {};
195 unsigned char ha[ETH_ALEN];
196 int i;
197
198 if (nft_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
199 nft_dev_path_info(&stack, &info, ha, &ft->data);
200
201 if (!info.indev || !nft_flowtable_find_dev(info.indev, ft))
202 return;
203
204 route->tuple[!dir].in.ifindex = info.indev->ifindex;
205 for (i = 0; i < info.num_encaps; i++) {
206 route->tuple[!dir].in.encap[i].id = info.encap[i].id;
207 route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
208 }
209 route->tuple[!dir].in.num_encaps = info.num_encaps;
210 route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
211
212 if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
213 memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
214 memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
215 route->tuple[dir].out.ifindex = info.outdev->ifindex;
216 route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
217 route->tuple[dir].xmit_type = info.xmit_type;
218 }
219 }
220
nft_flow_route(const struct nft_pktinfo * pkt,const struct nf_conn * ct,struct nf_flow_route * route,enum ip_conntrack_dir dir,struct nft_flowtable * ft)221 static int nft_flow_route(const struct nft_pktinfo *pkt,
222 const struct nf_conn *ct,
223 struct nf_flow_route *route,
224 enum ip_conntrack_dir dir,
225 struct nft_flowtable *ft)
226 {
227 struct dst_entry *this_dst = skb_dst(pkt->skb);
228 struct dst_entry *other_dst = NULL;
229 struct flowi fl;
230
231 memset(&fl, 0, sizeof(fl));
232 switch (nft_pf(pkt)) {
233 case NFPROTO_IPV4:
234 fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
235 fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
236 break;
237 case NFPROTO_IPV6:
238 fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
239 fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
240 break;
241 }
242
243 if (!dst_hold_safe(this_dst))
244 return -ENOENT;
245
246 nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
247 if (!other_dst) {
248 dst_release(this_dst);
249 return -ENOENT;
250 }
251
252 nft_default_forward_path(route, this_dst, dir);
253 nft_default_forward_path(route, other_dst, !dir);
254
255 if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
256 route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
257 nft_dev_forward_path(route, ct, dir, ft);
258 nft_dev_forward_path(route, ct, !dir, ft);
259 }
260
261 return 0;
262 }
263
nft_flow_offload_skip(struct sk_buff * skb,int family)264 static bool nft_flow_offload_skip(struct sk_buff *skb, int family)
265 {
266 if (skb_sec_path(skb))
267 return true;
268
269 if (family == NFPROTO_IPV4) {
270 const struct ip_options *opt;
271
272 opt = &(IPCB(skb)->opt);
273
274 if (unlikely(opt->optlen))
275 return true;
276 }
277
278 return false;
279 }
280
nft_flow_offload_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)281 static void nft_flow_offload_eval(const struct nft_expr *expr,
282 struct nft_regs *regs,
283 const struct nft_pktinfo *pkt)
284 {
285 struct nft_flow_offload *priv = nft_expr_priv(expr);
286 struct nf_flowtable *flowtable = &priv->flowtable->data;
287 struct tcphdr _tcph, *tcph = NULL;
288 struct nf_flow_route route = {};
289 enum ip_conntrack_info ctinfo;
290 struct flow_offload *flow;
291 enum ip_conntrack_dir dir;
292 struct nf_conn *ct;
293 int ret;
294
295 if (nft_flow_offload_skip(pkt->skb, nft_pf(pkt)))
296 goto out;
297
298 ct = nf_ct_get(pkt->skb, &ctinfo);
299 if (!ct)
300 goto out;
301
302 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
303 case IPPROTO_TCP:
304 tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
305 sizeof(_tcph), &_tcph);
306 if (unlikely(!tcph || tcph->fin || tcph->rst ||
307 !nf_conntrack_tcp_established(ct)))
308 goto out;
309 break;
310 case IPPROTO_UDP:
311 break;
312 default:
313 goto out;
314 }
315
316 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
317 ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
318 goto out;
319
320 if (!nf_ct_is_confirmed(ct))
321 goto out;
322
323 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
324 goto out;
325
326 dir = CTINFO2DIR(ctinfo);
327 if (nft_flow_route(pkt, ct, &route, dir, priv->flowtable) < 0)
328 goto err_flow_route;
329
330 flow = flow_offload_alloc(ct);
331 if (!flow)
332 goto err_flow_alloc;
333
334 flow_offload_route_init(flow, &route);
335
336 if (tcph) {
337 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
338 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
339 }
340
341 ret = flow_offload_add(flowtable, flow);
342 if (ret < 0)
343 goto err_flow_add;
344
345 return;
346
347 err_flow_add:
348 flow_offload_free(flow);
349 err_flow_alloc:
350 dst_release(route.tuple[dir].dst);
351 dst_release(route.tuple[!dir].dst);
352 err_flow_route:
353 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
354 out:
355 regs->verdict.code = NFT_BREAK;
356 }
357
nft_flow_offload_validate(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nft_data ** data)358 static int nft_flow_offload_validate(const struct nft_ctx *ctx,
359 const struct nft_expr *expr,
360 const struct nft_data **data)
361 {
362 unsigned int hook_mask = (1 << NF_INET_FORWARD);
363
364 if (ctx->family != NFPROTO_IPV4 &&
365 ctx->family != NFPROTO_IPV6 &&
366 ctx->family != NFPROTO_INET)
367 return -EOPNOTSUPP;
368
369 return nft_chain_validate_hooks(ctx->chain, hook_mask);
370 }
371
372 static const struct nla_policy nft_flow_offload_policy[NFTA_FLOW_MAX + 1] = {
373 [NFTA_FLOW_TABLE_NAME] = { .type = NLA_STRING,
374 .len = NFT_NAME_MAXLEN - 1 },
375 };
376
nft_flow_offload_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])377 static int nft_flow_offload_init(const struct nft_ctx *ctx,
378 const struct nft_expr *expr,
379 const struct nlattr * const tb[])
380 {
381 struct nft_flow_offload *priv = nft_expr_priv(expr);
382 u8 genmask = nft_genmask_next(ctx->net);
383 struct nft_flowtable *flowtable;
384
385 if (!tb[NFTA_FLOW_TABLE_NAME])
386 return -EINVAL;
387
388 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
389 genmask);
390 if (IS_ERR(flowtable))
391 return PTR_ERR(flowtable);
392
393 if (!nft_use_inc(&flowtable->use))
394 return -EMFILE;
395
396 priv->flowtable = flowtable;
397
398 return nf_ct_netns_get(ctx->net, ctx->family);
399 }
400
nft_flow_offload_deactivate(const struct nft_ctx * ctx,const struct nft_expr * expr,enum nft_trans_phase phase)401 static void nft_flow_offload_deactivate(const struct nft_ctx *ctx,
402 const struct nft_expr *expr,
403 enum nft_trans_phase phase)
404 {
405 struct nft_flow_offload *priv = nft_expr_priv(expr);
406
407 nf_tables_deactivate_flowtable(ctx, priv->flowtable, phase);
408 }
409
nft_flow_offload_activate(const struct nft_ctx * ctx,const struct nft_expr * expr)410 static void nft_flow_offload_activate(const struct nft_ctx *ctx,
411 const struct nft_expr *expr)
412 {
413 struct nft_flow_offload *priv = nft_expr_priv(expr);
414
415 nft_use_inc_restore(&priv->flowtable->use);
416 }
417
nft_flow_offload_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)418 static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
419 const struct nft_expr *expr)
420 {
421 nf_ct_netns_put(ctx->net, ctx->family);
422 }
423
nft_flow_offload_dump(struct sk_buff * skb,const struct nft_expr * expr)424 static int nft_flow_offload_dump(struct sk_buff *skb, const struct nft_expr *expr)
425 {
426 struct nft_flow_offload *priv = nft_expr_priv(expr);
427
428 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
429 goto nla_put_failure;
430
431 return 0;
432
433 nla_put_failure:
434 return -1;
435 }
436
437 static struct nft_expr_type nft_flow_offload_type;
438 static const struct nft_expr_ops nft_flow_offload_ops = {
439 .type = &nft_flow_offload_type,
440 .size = NFT_EXPR_SIZE(sizeof(struct nft_flow_offload)),
441 .eval = nft_flow_offload_eval,
442 .init = nft_flow_offload_init,
443 .activate = nft_flow_offload_activate,
444 .deactivate = nft_flow_offload_deactivate,
445 .destroy = nft_flow_offload_destroy,
446 .validate = nft_flow_offload_validate,
447 .dump = nft_flow_offload_dump,
448 };
449
450 static struct nft_expr_type nft_flow_offload_type __read_mostly = {
451 .name = "flow_offload",
452 .ops = &nft_flow_offload_ops,
453 .policy = nft_flow_offload_policy,
454 .maxattr = NFTA_FLOW_MAX,
455 .owner = THIS_MODULE,
456 };
457
flow_offload_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)458 static int flow_offload_netdev_event(struct notifier_block *this,
459 unsigned long event, void *ptr)
460 {
461 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
462
463 if (event != NETDEV_DOWN)
464 return NOTIFY_DONE;
465
466 nf_flow_table_cleanup(dev);
467
468 return NOTIFY_DONE;
469 }
470
471 static struct notifier_block flow_offload_netdev_notifier = {
472 .notifier_call = flow_offload_netdev_event,
473 };
474
nft_flow_offload_module_init(void)475 static int __init nft_flow_offload_module_init(void)
476 {
477 int err;
478
479 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
480 if (err)
481 goto err;
482
483 err = nft_register_expr(&nft_flow_offload_type);
484 if (err < 0)
485 goto register_expr;
486
487 return 0;
488
489 register_expr:
490 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
491 err:
492 return err;
493 }
494
nft_flow_offload_module_exit(void)495 static void __exit nft_flow_offload_module_exit(void)
496 {
497 nft_unregister_expr(&nft_flow_offload_type);
498 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
499 }
500
501 module_init(nft_flow_offload_module_init);
502 module_exit(nft_flow_offload_module_exit);
503
504 MODULE_LICENSE("GPL");
505 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
506 MODULE_ALIAS_NFT_EXPR("flow_offload");
507 MODULE_DESCRIPTION("nftables hardware flow offload module");
508