Linux 4.13.16
[linux/fpc-iii.git] / net / xfrm / xfrm_device.c
blob5f7e8bfa0c2dca6d811616aa7002b768d1bb51ae
1 /*
2 * xfrm_device.c - IPsec device offloading code.
4 * Copyright (c) 2015 secunet Security Networks AG
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
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.
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>
25 #ifdef CONFIG_XFRM_OFFLOAD
26 int validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features)
28 int err;
29 struct xfrm_state *x;
30 struct xfrm_offload *xo = xfrm_offload(skb);
32 if (skb_is_gso(skb))
33 return 0;
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;
40 x->outer_mode->xmit(x, skb);
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;
48 skb_push(skb, skb->data - skb_mac_header(skb));
51 return 0;
53 EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
55 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
56 struct xfrm_user_offload *xuo)
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;
65 if (!x->type_offload)
66 return 0;
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;
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;
82 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr, x->props.family);
83 if (IS_ERR(dst))
84 return 0;
86 dev = dst->dev;
88 dev_hold(dev);
89 dst_release(dst);
92 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
93 dev_put(dev);
94 return 0;
97 xso->dev = dev;
98 xso->num_exthdrs = 1;
99 xso->flags = xuo->flags;
101 err = dev->xfrmdev_ops->xdo_dev_state_add(x);
102 if (err) {
103 dev_put(dev);
104 return err;
107 return 0;
109 EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
111 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
113 int mtu;
114 struct dst_entry *dst = skb_dst(skb);
115 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
116 struct net_device *dev = x->xso.dev;
118 if (!x->type_offload || x->encap)
119 return false;
121 if ((x->xso.offload_handle && (dev == dst->path->dev)) &&
122 !dst->child->xfrm && x->type->get_mtu) {
123 mtu = x->type->get_mtu(x, xdst->child_mtu_cached);
125 if (skb->len <= mtu)
126 goto ok;
128 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
129 goto ok;
132 return false;
135 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
136 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
138 return true;
140 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
141 #endif
143 static int xfrm_dev_register(struct net_device *dev)
145 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
146 return NOTIFY_BAD;
147 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
148 !(dev->features & NETIF_F_HW_ESP))
149 return NOTIFY_BAD;
151 return NOTIFY_DONE;
154 static int xfrm_dev_unregister(struct net_device *dev)
156 return NOTIFY_DONE;
159 static int xfrm_dev_feat_change(struct net_device *dev)
161 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
162 return NOTIFY_BAD;
163 else if (!(dev->features & NETIF_F_HW_ESP))
164 dev->xfrmdev_ops = NULL;
166 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
167 !(dev->features & NETIF_F_HW_ESP))
168 return NOTIFY_BAD;
170 return NOTIFY_DONE;
173 static int xfrm_dev_down(struct net_device *dev)
175 if (dev->features & NETIF_F_HW_ESP)
176 xfrm_dev_state_flush(dev_net(dev), dev, true);
178 xfrm_garbage_collect(dev_net(dev));
180 return NOTIFY_DONE;
183 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
185 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
187 switch (event) {
188 case NETDEV_REGISTER:
189 return xfrm_dev_register(dev);
191 case NETDEV_UNREGISTER:
192 return xfrm_dev_unregister(dev);
194 case NETDEV_FEAT_CHANGE:
195 return xfrm_dev_feat_change(dev);
197 case NETDEV_DOWN:
198 return xfrm_dev_down(dev);
200 return NOTIFY_DONE;
203 static struct notifier_block xfrm_dev_notifier = {
204 .notifier_call = xfrm_dev_event,
207 void __net_init xfrm_dev_init(void)
209 register_netdevice_notifier(&xfrm_dev_notifier);