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