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