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>
18 struct nft_flow_offload {
19 struct nft_flowtable *flowtable;
22 static enum flow_offload_xmit_type nft_xmit_type(struct dst_entry *dst)
25 return FLOW_OFFLOAD_XMIT_XFRM;
27 return FLOW_OFFLOAD_XMIT_NEIGH;
30 static void nft_default_forward_path(struct nf_flow_route *route,
31 struct dst_entry *dst_cache,
32 enum ip_conntrack_dir dir)
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);
39 static bool nft_is_valid_ether_device(const struct net_device *dev)
41 if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
42 dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
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)
54 const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
55 struct net_device *dev = dst_cache->dev;
59 if (!nft_is_valid_ether_device(dev))
62 n = dst_neigh_lookup(dst_cache, daddr);
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);
72 if (!(nud_state & NUD_VALID))
76 return dev_fill_forward_path(dev, ha, stack);
79 struct nft_forward_info {
80 const struct net_device *indev;
81 const struct net_device *outdev;
82 const struct net_device *hw_outdev;
86 } encap[NF_FLOW_TABLE_ENCAP_MAX];
89 u8 h_source[ETH_ALEN];
91 enum flow_offload_xmit_type xmit_type;
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)
98 const struct net_device_path *path;
101 memcpy(info->h_dest, ha, ETH_ALEN);
103 for (i = 0; i < stack->num_paths; i++) {
104 path = &stack->path[i];
105 switch (path->type) {
106 case DEV_PATH_ETHERNET:
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);
114 if (path->type == DEV_PATH_ETHERNET)
116 if (path->type == DEV_PATH_DSA) {
117 i = stack->num_paths;
121 /* DEV_PATH_VLAN and DEV_PATH_PPPOE */
122 if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
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;
131 if (path->type == DEV_PATH_PPPOE)
132 memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
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);
138 switch (path->bridge.vlan_mode) {
139 case DEV_PATH_BR_VLAN_UNTAG_HW:
140 info->ingress_vlans |= BIT(info->num_encaps - 1);
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;
147 case DEV_PATH_BR_VLAN_UNTAG:
150 case DEV_PATH_BR_VLAN_KEEP:
153 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
161 info->outdev = info->indev;
163 info->hw_outdev = info->indev;
165 if (nf_flowtable_hw_offload(flowtable) &&
166 nft_is_valid_ether_device(info->indev))
167 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
170 static bool nft_flowtable_find_dev(const struct net_device *dev,
171 struct nft_flowtable *ft)
173 struct nft_hook *hook;
176 list_for_each_entry_rcu(hook, &ft->hook_list, list) {
177 if (hook->ops.dev != dev)
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)
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];
198 if (nft_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
199 nft_dev_path_info(&stack, &info, ha, &ft->data);
201 if (!info.indev || !nft_flowtable_find_dev(info.indev, ft))
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;
209 route->tuple[!dir].in.num_encaps = info.num_encaps;
210 route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
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;
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)
227 struct dst_entry *this_dst = skb_dst(pkt->skb);
228 struct dst_entry *other_dst = NULL;
231 memset(&fl, 0, sizeof(fl));
232 switch (nft_pf(pkt)) {
234 fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
235 fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip;
236 fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
237 fl.u.ip4.flowi4_iif = this_dst->dev->ifindex;
238 fl.u.ip4.flowi4_tos = RT_TOS(ip_hdr(pkt->skb)->tos);
239 fl.u.ip4.flowi4_mark = pkt->skb->mark;
240 fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
243 fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
244 fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.src.u3.in6;
245 fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
246 fl.u.ip6.flowi6_iif = this_dst->dev->ifindex;
247 fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
248 fl.u.ip6.flowi6_mark = pkt->skb->mark;
249 fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
253 nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
257 nft_default_forward_path(route, this_dst, dir);
258 nft_default_forward_path(route, other_dst, !dir);
260 if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
261 route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
262 nft_dev_forward_path(route, ct, dir, ft);
263 nft_dev_forward_path(route, ct, !dir, ft);
269 static bool nft_flow_offload_skip(struct sk_buff *skb, int family)
271 if (skb_sec_path(skb))
274 if (family == NFPROTO_IPV4) {
275 const struct ip_options *opt;
277 opt = &(IPCB(skb)->opt);
279 if (unlikely(opt->optlen))
286 static void nft_flow_offload_eval(const struct nft_expr *expr,
287 struct nft_regs *regs,
288 const struct nft_pktinfo *pkt)
290 struct nft_flow_offload *priv = nft_expr_priv(expr);
291 struct nf_flowtable *flowtable = &priv->flowtable->data;
292 struct tcphdr _tcph, *tcph = NULL;
293 struct nf_flow_route route = {};
294 enum ip_conntrack_info ctinfo;
295 struct flow_offload *flow;
296 enum ip_conntrack_dir dir;
300 if (nft_flow_offload_skip(pkt->skb, nft_pf(pkt)))
303 ct = nf_ct_get(pkt->skb, &ctinfo);
307 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
309 tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
310 sizeof(_tcph), &_tcph);
311 if (unlikely(!tcph || tcph->fin || tcph->rst ||
312 !nf_conntrack_tcp_established(ct)))
317 #ifdef CONFIG_NF_CT_PROTO_GRE
319 struct nf_conntrack_tuple *tuple;
321 if (ct->status & IPS_NAT_MASK)
323 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
324 /* No support for GRE v1 */
325 if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
334 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
335 ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
338 if (!nf_ct_is_confirmed(ct))
341 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
344 dir = CTINFO2DIR(ctinfo);
345 if (nft_flow_route(pkt, ct, &route, dir, priv->flowtable) < 0)
348 flow = flow_offload_alloc(ct);
352 if (flow_offload_route_init(flow, &route) < 0)
356 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
357 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
360 ret = flow_offload_add(flowtable, flow);
364 dst_release(route.tuple[!dir].dst);
368 flow_offload_free(flow);
370 dst_release(route.tuple[!dir].dst);
372 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
374 regs->verdict.code = NFT_BREAK;
377 static int nft_flow_offload_validate(const struct nft_ctx *ctx,
378 const struct nft_expr *expr,
379 const struct nft_data **data)
381 unsigned int hook_mask = (1 << NF_INET_FORWARD);
383 return nft_chain_validate_hooks(ctx->chain, hook_mask);
386 static const struct nla_policy nft_flow_offload_policy[NFTA_FLOW_MAX + 1] = {
387 [NFTA_FLOW_TABLE_NAME] = { .type = NLA_STRING,
388 .len = NFT_NAME_MAXLEN - 1 },
391 static int nft_flow_offload_init(const struct nft_ctx *ctx,
392 const struct nft_expr *expr,
393 const struct nlattr * const tb[])
395 struct nft_flow_offload *priv = nft_expr_priv(expr);
396 u8 genmask = nft_genmask_next(ctx->net);
397 struct nft_flowtable *flowtable;
399 if (!tb[NFTA_FLOW_TABLE_NAME])
402 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
404 if (IS_ERR(flowtable))
405 return PTR_ERR(flowtable);
407 priv->flowtable = flowtable;
410 return nf_ct_netns_get(ctx->net, ctx->family);
413 static void nft_flow_offload_deactivate(const struct nft_ctx *ctx,
414 const struct nft_expr *expr,
415 enum nft_trans_phase phase)
417 struct nft_flow_offload *priv = nft_expr_priv(expr);
419 nf_tables_deactivate_flowtable(ctx, priv->flowtable, phase);
422 static void nft_flow_offload_activate(const struct nft_ctx *ctx,
423 const struct nft_expr *expr)
425 struct nft_flow_offload *priv = nft_expr_priv(expr);
427 priv->flowtable->use++;
430 static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
431 const struct nft_expr *expr)
433 nf_ct_netns_put(ctx->net, ctx->family);
436 static int nft_flow_offload_dump(struct sk_buff *skb,
437 const struct nft_expr *expr, bool reset)
439 struct nft_flow_offload *priv = nft_expr_priv(expr);
441 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
442 goto nla_put_failure;
450 static struct nft_expr_type nft_flow_offload_type;
451 static const struct nft_expr_ops nft_flow_offload_ops = {
452 .type = &nft_flow_offload_type,
453 .size = NFT_EXPR_SIZE(sizeof(struct nft_flow_offload)),
454 .eval = nft_flow_offload_eval,
455 .init = nft_flow_offload_init,
456 .activate = nft_flow_offload_activate,
457 .deactivate = nft_flow_offload_deactivate,
458 .destroy = nft_flow_offload_destroy,
459 .validate = nft_flow_offload_validate,
460 .dump = nft_flow_offload_dump,
461 .reduce = NFT_REDUCE_READONLY,
464 static struct nft_expr_type nft_flow_offload_type __read_mostly = {
465 .name = "flow_offload",
466 .ops = &nft_flow_offload_ops,
467 .policy = nft_flow_offload_policy,
468 .maxattr = NFTA_FLOW_MAX,
469 .owner = THIS_MODULE,
472 static int flow_offload_netdev_event(struct notifier_block *this,
473 unsigned long event, void *ptr)
475 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
477 if (event != NETDEV_DOWN)
480 nf_flow_table_cleanup(dev);
485 static struct notifier_block flow_offload_netdev_notifier = {
486 .notifier_call = flow_offload_netdev_event,
489 static int __init nft_flow_offload_module_init(void)
493 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
497 err = nft_register_expr(&nft_flow_offload_type);
504 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
509 static void __exit nft_flow_offload_module_exit(void)
511 nft_unregister_expr(&nft_flow_offload_type);
512 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
515 module_init(nft_flow_offload_module_init);
516 module_exit(nft_flow_offload_module_exit);
518 MODULE_LICENSE("GPL");
520 MODULE_ALIAS_NFT_EXPR("flow_offload");
521 MODULE_DESCRIPTION("nftables hardware flow offload module");