blob: acf00104ef312b563be9f3aa9698fa2a9dbdfea7 [file] [log] [blame]
Steffen Klassert21f42cc2017-04-14 10:05:53 +02001/*
2 * xfrm_device.c - IPsec device offloading code.
3 *
4 * Copyright (c) 2015 secunet Security Networks AG
5 *
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/errno.h>
16#include <linux/module.h>
17#include <linux/netdevice.h>
18#include <linux/skbuff.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <net/dst.h>
22#include <net/xfrm.h>
23#include <linux/notifier.h>
24
Hangbin Liub81f884a2017-06-01 14:57:56 +080025#ifdef CONFIG_XFRM_OFFLOAD
Steffen Klassertf6e27112017-04-14 10:07:28 +020026int validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features)
27{
28 int err;
29 struct xfrm_state *x;
30 struct xfrm_offload *xo = xfrm_offload(skb);
31
32 if (skb_is_gso(skb))
33 return 0;
34
35 if (xo) {
36 x = skb->sp->xvec[skb->sp->len - 1];
37 if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
38 return 0;
39
40 x->outer_mode->xmit(x, skb);
41
42 err = x->type_offload->xmit(x, skb, features);
43 if (err) {
44 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
45 return err;
46 }
47
48 skb_push(skb, skb->data - skb_mac_header(skb));
49 }
50
51 return 0;
52}
53EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
54
Steffen Klassertd77e38e2017-04-14 10:06:10 +020055int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
56 struct xfrm_user_offload *xuo)
57{
58 int err;
59 struct dst_entry *dst;
60 struct net_device *dev;
61 struct xfrm_state_offload *xso = &x->xso;
62 xfrm_address_t *saddr;
63 xfrm_address_t *daddr;
64
65 if (!x->type_offload)
Ilan Tayariffdb5212017-08-01 12:49:08 +030066 return -EINVAL;
Steffen Klassertd77e38e2017-04-14 10:06:10 +020067
68 /* We don't yet support UDP encapsulation, TFC padding and ESN. */
69 if (x->encap || x->tfcpad || (x->props.flags & XFRM_STATE_ESN))
70 return 0;
71
72 dev = dev_get_by_index(net, xuo->ifindex);
73 if (!dev) {
74 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
75 saddr = &x->props.saddr;
76 daddr = &x->id.daddr;
77 } else {
78 saddr = &x->id.daddr;
79 daddr = &x->props.saddr;
80 }
81
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090082 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
83 x->props.family, x->props.output_mark);
Steffen Klassertd77e38e2017-04-14 10:06:10 +020084 if (IS_ERR(dst))
85 return 0;
86
87 dev = dst->dev;
88
89 dev_hold(dev);
90 dst_release(dst);
91 }
92
93 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
94 dev_put(dev);
95 return 0;
96 }
97
98 xso->dev = dev;
99 xso->num_exthdrs = 1;
100 xso->flags = xuo->flags;
101
102 err = dev->xfrmdev_ops->xdo_dev_state_add(x);
103 if (err) {
104 dev_put(dev);
105 return err;
106 }
107
108 return 0;
109}
110EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
111
112bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
113{
114 int mtu;
115 struct dst_entry *dst = skb_dst(skb);
116 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
117 struct net_device *dev = x->xso.dev;
118
119 if (!x->type_offload || x->encap)
120 return false;
121
122 if ((x->xso.offload_handle && (dev == dst->path->dev)) &&
123 !dst->child->xfrm && x->type->get_mtu) {
124 mtu = x->type->get_mtu(x, xdst->child_mtu_cached);
125
126 if (skb->len <= mtu)
127 goto ok;
128
129 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
130 goto ok;
131 }
132
133 return false;
134
135ok:
136 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
137 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
138
139 return true;
140}
141EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
Hangbin Liub81f884a2017-06-01 14:57:56 +0800142#endif
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200143
Wei Yongjun24d472e2017-05-18 15:51:38 +0000144static int xfrm_dev_register(struct net_device *dev)
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200145{
146 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
147 return NOTIFY_BAD;
148 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
149 !(dev->features & NETIF_F_HW_ESP))
150 return NOTIFY_BAD;
151
152 return NOTIFY_DONE;
153}
154
155static int xfrm_dev_unregister(struct net_device *dev)
156{
Florian Westphalec30d782017-07-17 13:57:27 +0200157 xfrm_policy_cache_flush();
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200158 return NOTIFY_DONE;
159}
160
161static int xfrm_dev_feat_change(struct net_device *dev)
162{
163 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
164 return NOTIFY_BAD;
165 else if (!(dev->features & NETIF_F_HW_ESP))
166 dev->xfrmdev_ops = NULL;
167
168 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
169 !(dev->features & NETIF_F_HW_ESP))
170 return NOTIFY_BAD;
171
172 return NOTIFY_DONE;
173}
174
175static int xfrm_dev_down(struct net_device *dev)
176{
Ilan Tayari2c1497b2017-05-08 10:30:18 +0300177 if (dev->features & NETIF_F_HW_ESP)
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200178 xfrm_dev_state_flush(dev_net(dev), dev, true);
179
Florian Westphalec30d782017-07-17 13:57:27 +0200180 xfrm_policy_cache_flush();
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200181 return NOTIFY_DONE;
182}
183
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200184static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
185{
186 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
187
188 switch (event) {
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200189 case NETDEV_REGISTER:
190 return xfrm_dev_register(dev);
191
192 case NETDEV_UNREGISTER:
193 return xfrm_dev_unregister(dev);
194
195 case NETDEV_FEAT_CHANGE:
196 return xfrm_dev_feat_change(dev);
197
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200198 case NETDEV_DOWN:
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200199 return xfrm_dev_down(dev);
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200200 }
201 return NOTIFY_DONE;
202}
203
204static struct notifier_block xfrm_dev_notifier = {
205 .notifier_call = xfrm_dev_event,
206};
207
208void __net_init xfrm_dev_init(void)
209{
210 register_netdevice_notifier(&xfrm_dev_notifier);
211}