treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / net / xfrm / xfrm_device.c
blob50f567a88f450f702b957628fbfbed3835a896a3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * xfrm_device.c - IPsec device offloading code.
5 * Copyright (c) 2015 secunet Security Networks AG
7 * Author:
8 * Steffen Klassert <steffen.klassert@secunet.com>
9 */
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <net/dst.h>
18 #include <net/xfrm.h>
19 #include <linux/notifier.h>
21 #ifdef CONFIG_XFRM_OFFLOAD
22 static void __xfrm_transport_prep(struct xfrm_state *x, struct sk_buff *skb,
23 unsigned int hsize)
25 struct xfrm_offload *xo = xfrm_offload(skb);
27 skb_reset_mac_len(skb);
28 pskb_pull(skb, skb->mac_len + hsize + x->props.header_len);
30 if (xo->flags & XFRM_GSO_SEGMENT) {
31 skb_reset_transport_header(skb);
32 skb->transport_header -= x->props.header_len;
36 static void __xfrm_mode_tunnel_prep(struct xfrm_state *x, struct sk_buff *skb,
37 unsigned int hsize)
40 struct xfrm_offload *xo = xfrm_offload(skb);
42 if (xo->flags & XFRM_GSO_SEGMENT)
43 skb->transport_header = skb->network_header + hsize;
45 skb_reset_mac_len(skb);
46 pskb_pull(skb, skb->mac_len + x->props.header_len);
49 /* Adjust pointers into the packet when IPsec is done at layer2 */
50 static void xfrm_outer_mode_prep(struct xfrm_state *x, struct sk_buff *skb)
52 switch (x->outer_mode.encap) {
53 case XFRM_MODE_TUNNEL:
54 if (x->outer_mode.family == AF_INET)
55 return __xfrm_mode_tunnel_prep(x, skb,
56 sizeof(struct iphdr));
57 if (x->outer_mode.family == AF_INET6)
58 return __xfrm_mode_tunnel_prep(x, skb,
59 sizeof(struct ipv6hdr));
60 break;
61 case XFRM_MODE_TRANSPORT:
62 if (x->outer_mode.family == AF_INET)
63 return __xfrm_transport_prep(x, skb,
64 sizeof(struct iphdr));
65 if (x->outer_mode.family == AF_INET6)
66 return __xfrm_transport_prep(x, skb,
67 sizeof(struct ipv6hdr));
68 break;
69 case XFRM_MODE_ROUTEOPTIMIZATION:
70 case XFRM_MODE_IN_TRIGGER:
71 case XFRM_MODE_BEET:
72 break;
76 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
78 int err;
79 unsigned long flags;
80 struct xfrm_state *x;
81 struct sk_buff *skb2, *nskb;
82 struct softnet_data *sd;
83 netdev_features_t esp_features = features;
84 struct xfrm_offload *xo = xfrm_offload(skb);
85 struct sec_path *sp;
87 if (!xo)
88 return skb;
90 if (!(features & NETIF_F_HW_ESP))
91 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
93 sp = skb_sec_path(skb);
94 x = sp->xvec[sp->len - 1];
95 if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
96 return skb;
98 local_irq_save(flags);
99 sd = this_cpu_ptr(&softnet_data);
100 err = !skb_queue_empty(&sd->xfrm_backlog);
101 local_irq_restore(flags);
103 if (err) {
104 *again = true;
105 return skb;
108 if (skb_is_gso(skb)) {
109 struct net_device *dev = skb->dev;
111 if (unlikely(x->xso.dev != dev)) {
112 struct sk_buff *segs;
114 /* Packet got rerouted, fixup features and segment it. */
115 esp_features = esp_features & ~(NETIF_F_HW_ESP
116 | NETIF_F_GSO_ESP);
118 segs = skb_gso_segment(skb, esp_features);
119 if (IS_ERR(segs)) {
120 kfree_skb(skb);
121 atomic_long_inc(&dev->tx_dropped);
122 return NULL;
123 } else {
124 consume_skb(skb);
125 skb = segs;
130 if (!skb->next) {
131 esp_features |= skb->dev->gso_partial_features;
132 xfrm_outer_mode_prep(x, skb);
134 xo->flags |= XFRM_DEV_RESUME;
136 err = x->type_offload->xmit(x, skb, esp_features);
137 if (err) {
138 if (err == -EINPROGRESS)
139 return NULL;
141 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
142 kfree_skb(skb);
143 return NULL;
146 skb_push(skb, skb->data - skb_mac_header(skb));
148 return skb;
151 skb_list_walk_safe(skb, skb2, nskb) {
152 esp_features |= skb->dev->gso_partial_features;
153 skb_mark_not_on_list(skb2);
155 xo = xfrm_offload(skb2);
156 xo->flags |= XFRM_DEV_RESUME;
158 xfrm_outer_mode_prep(x, skb2);
160 err = x->type_offload->xmit(x, skb2, esp_features);
161 if (!err) {
162 skb2->next = nskb;
163 } else if (err != -EINPROGRESS) {
164 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
165 skb2->next = nskb;
166 kfree_skb_list(skb2);
167 return NULL;
168 } else {
169 if (skb == skb2)
170 skb = nskb;
172 if (!skb)
173 return NULL;
175 continue;
178 skb_push(skb2, skb2->data - skb_mac_header(skb2));
181 return skb;
183 EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
185 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
186 struct xfrm_user_offload *xuo)
188 int err;
189 struct dst_entry *dst;
190 struct net_device *dev;
191 struct xfrm_state_offload *xso = &x->xso;
192 xfrm_address_t *saddr;
193 xfrm_address_t *daddr;
195 if (!x->type_offload)
196 return -EINVAL;
198 /* We don't yet support UDP encapsulation and TFC padding. */
199 if (x->encap || x->tfcpad)
200 return -EINVAL;
202 dev = dev_get_by_index(net, xuo->ifindex);
203 if (!dev) {
204 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
205 saddr = &x->props.saddr;
206 daddr = &x->id.daddr;
207 } else {
208 saddr = &x->id.daddr;
209 daddr = &x->props.saddr;
212 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
213 x->props.family,
214 xfrm_smark_get(0, x));
215 if (IS_ERR(dst))
216 return 0;
218 dev = dst->dev;
220 dev_hold(dev);
221 dst_release(dst);
224 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
225 xso->dev = NULL;
226 dev_put(dev);
227 return 0;
230 if (x->props.flags & XFRM_STATE_ESN &&
231 !dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
232 xso->dev = NULL;
233 dev_put(dev);
234 return -EINVAL;
237 xso->dev = dev;
238 xso->num_exthdrs = 1;
239 xso->flags = xuo->flags;
241 err = dev->xfrmdev_ops->xdo_dev_state_add(x);
242 if (err) {
243 xso->num_exthdrs = 0;
244 xso->flags = 0;
245 xso->dev = NULL;
246 dev_put(dev);
248 if (err != -EOPNOTSUPP)
249 return err;
252 return 0;
254 EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
256 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
258 int mtu;
259 struct dst_entry *dst = skb_dst(skb);
260 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
261 struct net_device *dev = x->xso.dev;
263 if (!x->type_offload || x->encap)
264 return false;
266 if ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
267 (!xdst->child->xfrm)) {
268 mtu = xfrm_state_mtu(x, xdst->child_mtu_cached);
269 if (skb->len <= mtu)
270 goto ok;
272 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
273 goto ok;
276 return false;
279 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
280 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
282 return true;
284 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
286 void xfrm_dev_resume(struct sk_buff *skb)
288 struct net_device *dev = skb->dev;
289 int ret = NETDEV_TX_BUSY;
290 struct netdev_queue *txq;
291 struct softnet_data *sd;
292 unsigned long flags;
294 rcu_read_lock();
295 txq = netdev_core_pick_tx(dev, skb, NULL);
297 HARD_TX_LOCK(dev, txq, smp_processor_id());
298 if (!netif_xmit_frozen_or_stopped(txq))
299 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
300 HARD_TX_UNLOCK(dev, txq);
302 if (!dev_xmit_complete(ret)) {
303 local_irq_save(flags);
304 sd = this_cpu_ptr(&softnet_data);
305 skb_queue_tail(&sd->xfrm_backlog, skb);
306 raise_softirq_irqoff(NET_TX_SOFTIRQ);
307 local_irq_restore(flags);
309 rcu_read_unlock();
311 EXPORT_SYMBOL_GPL(xfrm_dev_resume);
313 void xfrm_dev_backlog(struct softnet_data *sd)
315 struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
316 struct sk_buff_head list;
317 struct sk_buff *skb;
319 if (skb_queue_empty(xfrm_backlog))
320 return;
322 __skb_queue_head_init(&list);
324 spin_lock(&xfrm_backlog->lock);
325 skb_queue_splice_init(xfrm_backlog, &list);
326 spin_unlock(&xfrm_backlog->lock);
328 while (!skb_queue_empty(&list)) {
329 skb = __skb_dequeue(&list);
330 xfrm_dev_resume(skb);
334 #endif
336 static int xfrm_api_check(struct net_device *dev)
338 #ifdef CONFIG_XFRM_OFFLOAD
339 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
340 !(dev->features & NETIF_F_HW_ESP))
341 return NOTIFY_BAD;
343 if ((dev->features & NETIF_F_HW_ESP) &&
344 (!(dev->xfrmdev_ops &&
345 dev->xfrmdev_ops->xdo_dev_state_add &&
346 dev->xfrmdev_ops->xdo_dev_state_delete)))
347 return NOTIFY_BAD;
348 #else
349 if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
350 return NOTIFY_BAD;
351 #endif
353 return NOTIFY_DONE;
356 static int xfrm_dev_register(struct net_device *dev)
358 return xfrm_api_check(dev);
361 static int xfrm_dev_feat_change(struct net_device *dev)
363 return xfrm_api_check(dev);
366 static int xfrm_dev_down(struct net_device *dev)
368 if (dev->features & NETIF_F_HW_ESP)
369 xfrm_dev_state_flush(dev_net(dev), dev, true);
371 return NOTIFY_DONE;
374 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
376 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
378 switch (event) {
379 case NETDEV_REGISTER:
380 return xfrm_dev_register(dev);
382 case NETDEV_FEAT_CHANGE:
383 return xfrm_dev_feat_change(dev);
385 case NETDEV_DOWN:
386 return xfrm_dev_down(dev);
388 return NOTIFY_DONE;
391 static struct notifier_block xfrm_dev_notifier = {
392 .notifier_call = xfrm_dev_event,
395 void __init xfrm_dev_init(void)
397 register_netdevice_notifier(&xfrm_dev_notifier);