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>
16 struct nft_flow_offload {
17 struct nft_flowtable *flowtable;
20 static 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)
25 struct dst_entry *this_dst = skb_dst(pkt->skb);
26 struct dst_entry *other_dst = NULL;
29 memset(&fl, 0, sizeof(fl));
30 switch (nft_pf(pkt)) {
32 fl.u.ip4.daddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
35 fl.u.ip6.daddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
39 nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
43 route->tuple[dir].dst = this_dst;
44 route->tuple[dir].ifindex = nft_in(pkt)->ifindex;
45 route->tuple[!dir].dst = other_dst;
46 route->tuple[!dir].ifindex = nft_out(pkt)->ifindex;
51 static bool nft_flow_offload_skip(struct sk_buff *skb)
53 struct ip_options *opt = &(IPCB(skb)->opt);
55 if (unlikely(opt->optlen))
57 if (skb_sec_path(skb))
63 static void nft_flow_offload_eval(const struct nft_expr *expr,
64 struct nft_regs *regs,
65 const struct nft_pktinfo *pkt)
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;
76 if (nft_flow_offload_skip(pkt->skb))
79 ct = nf_ct_get(pkt->skb, &ctinfo);
83 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
91 if (test_bit(IPS_HELPER_BIT, &ct->status))
94 if (ctinfo == IP_CT_NEW ||
95 ctinfo == IP_CT_RELATED)
98 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
101 dir = CTINFO2DIR(ctinfo);
102 if (nft_flow_route(pkt, ct, &route, dir) < 0)
105 flow = flow_offload_alloc(ct, &route);
109 ret = flow_offload_add(flowtable, flow);
116 flow_offload_free(flow);
118 dst_release(route.tuple[!dir].dst);
120 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
122 regs->verdict.code = NFT_BREAK;
125 static int nft_flow_offload_validate(const struct nft_ctx *ctx,
126 const struct nft_expr *expr,
127 const struct nft_data **data)
129 unsigned int hook_mask = (1 << NF_INET_FORWARD);
131 return nft_chain_validate_hooks(ctx->chain, hook_mask);
134 static int nft_flow_offload_init(const struct nft_ctx *ctx,
135 const struct nft_expr *expr,
136 const struct nlattr * const tb[])
138 struct nft_flow_offload *priv = nft_expr_priv(expr);
139 u8 genmask = nft_genmask_next(ctx->net);
140 struct nft_flowtable *flowtable;
142 if (!tb[NFTA_FLOW_TABLE_NAME])
145 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
147 if (IS_ERR(flowtable))
148 return PTR_ERR(flowtable);
150 priv->flowtable = flowtable;
153 return nf_ct_netns_get(ctx->net, ctx->family);
156 static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
157 const struct nft_expr *expr)
159 struct nft_flow_offload *priv = nft_expr_priv(expr);
161 priv->flowtable->use--;
162 nf_ct_netns_put(ctx->net, ctx->family);
165 static int nft_flow_offload_dump(struct sk_buff *skb, const struct nft_expr *expr)
167 struct nft_flow_offload *priv = nft_expr_priv(expr);
169 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
170 goto nla_put_failure;
178 static struct nft_expr_type nft_flow_offload_type;
179 static 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,
189 static 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,
196 static int flow_offload_netdev_event(struct notifier_block *this,
197 unsigned long event, void *ptr)
199 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
201 if (event != NETDEV_DOWN)
204 nf_flow_table_cleanup(dev);
209 static struct notifier_block flow_offload_netdev_notifier = {
210 .notifier_call = flow_offload_netdev_event,
213 static int __init nft_flow_offload_module_init(void)
217 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
221 err = nft_register_expr(&nft_flow_offload_type);
228 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
233 static void __exit nft_flow_offload_module_exit(void)
235 nft_unregister_expr(&nft_flow_offload_type);
236 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
239 module_init(nft_flow_offload_module_init);
240 module_exit(nft_flow_offload_module_exit);
242 MODULE_LICENSE("GPL");
244 MODULE_ALIAS_NFT_EXPR("flow_offload");