2017-04-14 16:05:53 +08:00
|
|
|
/*
|
|
|
|
* xfrm_device.c - IPsec device offloading code.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 secunet Security Networks AG
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Steffen Klassert <steffen.klassert@secunet.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <net/dst.h>
|
|
|
|
#include <net/xfrm.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
|
2017-06-01 14:57:56 +08:00
|
|
|
#ifdef CONFIG_XFRM_OFFLOAD
|
2017-12-20 17:41:36 +08:00
|
|
|
struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
|
2017-04-14 16:07:28 +08:00
|
|
|
{
|
|
|
|
int err;
|
2017-12-20 17:41:36 +08:00
|
|
|
unsigned long flags;
|
2017-04-14 16:07:28 +08:00
|
|
|
struct xfrm_state *x;
|
2017-12-20 17:41:31 +08:00
|
|
|
struct sk_buff *skb2;
|
2017-12-20 17:41:36 +08:00
|
|
|
struct softnet_data *sd;
|
2017-12-20 17:41:31 +08:00
|
|
|
netdev_features_t esp_features = features;
|
2017-04-14 16:07:28 +08:00
|
|
|
struct xfrm_offload *xo = xfrm_offload(skb);
|
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
if (!xo)
|
|
|
|
return skb;
|
2017-04-14 16:07:28 +08:00
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
if (!(features & NETIF_F_HW_ESP))
|
|
|
|
esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
|
2017-04-14 16:07:28 +08:00
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
x = skb->sp->xvec[skb->sp->len - 1];
|
|
|
|
if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
|
|
|
|
return skb;
|
|
|
|
|
2017-12-20 17:41:36 +08:00
|
|
|
local_irq_save(flags);
|
|
|
|
sd = this_cpu_ptr(&softnet_data);
|
|
|
|
err = !skb_queue_empty(&sd->xfrm_backlog);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
*again = true;
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
if (skb_is_gso(skb)) {
|
|
|
|
struct net_device *dev = skb->dev;
|
|
|
|
|
2018-06-27 05:19:10 +08:00
|
|
|
if (unlikely(x->xso.dev != dev)) {
|
2017-12-20 17:41:31 +08:00
|
|
|
struct sk_buff *segs;
|
|
|
|
|
|
|
|
/* Packet got rerouted, fixup features and segment it. */
|
|
|
|
esp_features = esp_features & ~(NETIF_F_HW_ESP
|
|
|
|
| NETIF_F_GSO_ESP);
|
|
|
|
|
|
|
|
segs = skb_gso_segment(skb, esp_features);
|
|
|
|
if (IS_ERR(segs)) {
|
|
|
|
kfree_skb(skb);
|
2017-12-20 17:41:36 +08:00
|
|
|
atomic_long_inc(&dev->tx_dropped);
|
2017-12-20 17:41:31 +08:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
consume_skb(skb);
|
|
|
|
skb = segs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skb->next) {
|
2017-04-14 16:07:28 +08:00
|
|
|
x->outer_mode->xmit(x, skb);
|
|
|
|
|
2017-12-20 17:41:36 +08:00
|
|
|
xo->flags |= XFRM_DEV_RESUME;
|
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
err = x->type_offload->xmit(x, skb, esp_features);
|
2017-04-14 16:07:28 +08:00
|
|
|
if (err) {
|
2017-12-20 17:41:36 +08:00
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-14 16:07:28 +08:00
|
|
|
XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
|
2017-12-20 17:41:31 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
return NULL;
|
2017-04-14 16:07:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
skb_push(skb, skb->data - skb_mac_header(skb));
|
2017-12-20 17:41:31 +08:00
|
|
|
|
|
|
|
return skb;
|
2017-04-14 16:07:28 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 17:41:31 +08:00
|
|
|
skb2 = skb;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct sk_buff *nskb = skb2->next;
|
2018-07-30 11:42:53 +08:00
|
|
|
skb_mark_not_on_list(skb2);
|
2017-12-20 17:41:31 +08:00
|
|
|
|
|
|
|
xo = xfrm_offload(skb2);
|
2017-12-20 17:41:36 +08:00
|
|
|
xo->flags |= XFRM_DEV_RESUME;
|
2017-12-20 17:41:31 +08:00
|
|
|
|
|
|
|
x->outer_mode->xmit(x, skb2);
|
|
|
|
|
|
|
|
err = x->type_offload->xmit(x, skb2, esp_features);
|
2017-12-20 17:41:36 +08:00
|
|
|
if (!err) {
|
|
|
|
skb2->next = nskb;
|
|
|
|
} else if (err != -EINPROGRESS) {
|
2017-12-20 17:41:31 +08:00
|
|
|
XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
|
|
|
|
skb2->next = nskb;
|
|
|
|
kfree_skb_list(skb2);
|
|
|
|
return NULL;
|
2017-12-20 17:41:36 +08:00
|
|
|
} else {
|
|
|
|
if (skb == skb2)
|
|
|
|
skb = nskb;
|
|
|
|
|
|
|
|
if (!skb)
|
|
|
|
return NULL;
|
2017-12-20 17:41:31 +08:00
|
|
|
|
2017-12-20 17:41:36 +08:00
|
|
|
goto skip_push;
|
|
|
|
}
|
2017-12-20 17:41:31 +08:00
|
|
|
|
|
|
|
skb_push(skb2, skb2->data - skb_mac_header(skb2));
|
|
|
|
|
2017-12-20 17:41:36 +08:00
|
|
|
skip_push:
|
2017-12-20 17:41:31 +08:00
|
|
|
skb2 = nskb;
|
|
|
|
} while (skb2);
|
|
|
|
|
|
|
|
return skb;
|
2017-04-14 16:07:28 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
|
|
|
|
|
2017-04-14 16:06:10 +08:00
|
|
|
int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
|
|
|
|
struct xfrm_user_offload *xuo)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct dst_entry *dst;
|
|
|
|
struct net_device *dev;
|
|
|
|
struct xfrm_state_offload *xso = &x->xso;
|
|
|
|
xfrm_address_t *saddr;
|
|
|
|
xfrm_address_t *daddr;
|
|
|
|
|
|
|
|
if (!x->type_offload)
|
2017-08-01 17:49:08 +08:00
|
|
|
return -EINVAL;
|
2017-04-14 16:06:10 +08:00
|
|
|
|
2018-01-14 17:39:10 +08:00
|
|
|
/* We don't yet support UDP encapsulation and TFC padding. */
|
|
|
|
if (x->encap || x->tfcpad)
|
2017-11-28 17:49:29 +08:00
|
|
|
return -EINVAL;
|
2017-04-14 16:06:10 +08:00
|
|
|
|
|
|
|
dev = dev_get_by_index(net, xuo->ifindex);
|
|
|
|
if (!dev) {
|
|
|
|
if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
|
|
|
|
saddr = &x->props.saddr;
|
|
|
|
daddr = &x->id.daddr;
|
|
|
|
} else {
|
|
|
|
saddr = &x->id.daddr;
|
|
|
|
daddr = &x->props.saddr;
|
|
|
|
}
|
|
|
|
|
net: xfrm: support setting an output mark.
On systems that use mark-based routing it may be necessary for
routing lookups to use marks in order for packets to be routed
correctly. An example of such a system is Android, which uses
socket marks to route packets via different networks.
Currently, routing lookups in tunnel mode always use a mark of
zero, making routing incorrect on such systems.
This patch adds a new output_mark element to the xfrm state and
a corresponding XFRMA_OUTPUT_MARK netlink attribute. The output
mark differs from the existing xfrm mark in two ways:
1. The xfrm mark is used to match xfrm policies and states, while
the xfrm output mark is used to set the mark (and influence
the routing) of the packets emitted by those states.
2. The existing mark is constrained to be a subset of the bits of
the originating socket or transformed packet, but the output
mark is arbitrary and depends only on the state.
The use of a separate mark provides additional flexibility. For
example:
- A packet subject to two transforms (e.g., transport mode inside
tunnel mode) can have two different output marks applied to it,
one for the transport mode SA and one for the tunnel mode SA.
- On a system where socket marks determine routing, the packets
emitted by an IPsec tunnel can be routed based on a mark that
is determined by the tunnel, not by the marks of the
unencrypted packets.
- Support for setting the output marks can be introduced without
breaking any existing setups that employ both mark-based
routing and xfrm tunnel mode. Simply changing the code to use
the xfrm mark for routing output packets could xfrm mark could
change behaviour in a way that breaks these setups.
If the output mark is unspecified or set to zero, the mark is not
set or changed.
Tested: make allyesconfig; make -j64
Tested: https://android-review.googlesource.com/452776
Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2017-08-11 01:11:33 +08:00
|
|
|
dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
|
2018-06-12 18:44:26 +08:00
|
|
|
x->props.family,
|
|
|
|
xfrm_smark_get(0, x));
|
2017-04-14 16:06:10 +08:00
|
|
|
if (IS_ERR(dst))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dev = dst->dev;
|
|
|
|
|
|
|
|
dev_hold(dev);
|
|
|
|
dst_release(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
|
2017-09-04 16:59:55 +08:00
|
|
|
xso->dev = NULL;
|
2017-04-14 16:06:10 +08:00
|
|
|
dev_put(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:39:10 +08:00
|
|
|
if (x->props.flags & XFRM_STATE_ESN &&
|
|
|
|
!dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
|
|
|
|
xso->dev = NULL;
|
|
|
|
dev_put(dev);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:06:10 +08:00
|
|
|
xso->dev = dev;
|
|
|
|
xso->num_exthdrs = 1;
|
|
|
|
xso->flags = xuo->flags;
|
|
|
|
|
|
|
|
err = dev->xfrmdev_ops->xdo_dev_state_add(x);
|
|
|
|
if (err) {
|
2018-08-23 05:38:10 +08:00
|
|
|
xso->num_exthdrs = 0;
|
|
|
|
xso->flags = 0;
|
2018-01-18 21:41:51 +08:00
|
|
|
xso->dev = NULL;
|
2017-04-14 16:06:10 +08:00
|
|
|
dev_put(dev);
|
2018-08-23 05:38:10 +08:00
|
|
|
|
|
|
|
if (err != -EOPNOTSUPP)
|
|
|
|
return err;
|
2017-04-14 16:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
|
|
|
|
|
|
|
|
bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
|
|
|
|
{
|
|
|
|
int mtu;
|
|
|
|
struct dst_entry *dst = skb_dst(skb);
|
|
|
|
struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
|
|
|
|
struct net_device *dev = x->xso.dev;
|
|
|
|
|
|
|
|
if (!x->type_offload || x->encap)
|
|
|
|
return false;
|
|
|
|
|
2018-06-27 05:19:10 +08:00
|
|
|
if ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
|
|
|
|
(!xdst->child->xfrm && x->type->get_mtu)) {
|
2017-04-14 16:06:10 +08:00
|
|
|
mtu = x->type->get_mtu(x, xdst->child_mtu_cached);
|
|
|
|
|
|
|
|
if (skb->len <= mtu)
|
|
|
|
goto ok;
|
|
|
|
|
2018-03-01 14:13:37 +08:00
|
|
|
if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
|
2017-04-14 16:06:10 +08:00
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
|
|
|
|
return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
|
2017-12-20 17:41:36 +08:00
|
|
|
|
|
|
|
void xfrm_dev_resume(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct net_device *dev = skb->dev;
|
|
|
|
int ret = NETDEV_TX_BUSY;
|
|
|
|
struct netdev_queue *txq;
|
|
|
|
struct softnet_data *sd;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
txq = netdev_pick_tx(dev, skb, NULL);
|
|
|
|
|
|
|
|
HARD_TX_LOCK(dev, txq, smp_processor_id());
|
|
|
|
if (!netif_xmit_frozen_or_stopped(txq))
|
|
|
|
skb = dev_hard_start_xmit(skb, dev, txq, &ret);
|
|
|
|
HARD_TX_UNLOCK(dev, txq);
|
|
|
|
|
|
|
|
if (!dev_xmit_complete(ret)) {
|
|
|
|
local_irq_save(flags);
|
|
|
|
sd = this_cpu_ptr(&softnet_data);
|
|
|
|
skb_queue_tail(&sd->xfrm_backlog, skb);
|
|
|
|
raise_softirq_irqoff(NET_TX_SOFTIRQ);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_dev_resume);
|
|
|
|
|
|
|
|
void xfrm_dev_backlog(struct softnet_data *sd)
|
|
|
|
{
|
|
|
|
struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
|
|
|
|
struct sk_buff_head list;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
if (skb_queue_empty(xfrm_backlog))
|
|
|
|
return;
|
|
|
|
|
|
|
|
__skb_queue_head_init(&list);
|
|
|
|
|
|
|
|
spin_lock(&xfrm_backlog->lock);
|
|
|
|
skb_queue_splice_init(xfrm_backlog, &list);
|
|
|
|
spin_unlock(&xfrm_backlog->lock);
|
|
|
|
|
|
|
|
while (!skb_queue_empty(&list)) {
|
|
|
|
skb = __skb_dequeue(&list);
|
|
|
|
xfrm_dev_resume(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-06-01 14:57:56 +08:00
|
|
|
#endif
|
2017-04-14 16:06:10 +08:00
|
|
|
|
2017-12-20 07:35:48 +08:00
|
|
|
static int xfrm_api_check(struct net_device *dev)
|
2017-04-14 16:06:10 +08:00
|
|
|
{
|
2017-12-20 07:35:48 +08:00
|
|
|
#ifdef CONFIG_XFRM_OFFLOAD
|
2017-04-14 16:06:10 +08:00
|
|
|
if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
|
|
|
|
!(dev->features & NETIF_F_HW_ESP))
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
|
2017-12-20 07:35:48 +08:00
|
|
|
if ((dev->features & NETIF_F_HW_ESP) &&
|
|
|
|
(!(dev->xfrmdev_ops &&
|
|
|
|
dev->xfrmdev_ops->xdo_dev_state_add &&
|
|
|
|
dev->xfrmdev_ops->xdo_dev_state_delete)))
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
#else
|
|
|
|
if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
#endif
|
|
|
|
|
2017-04-14 16:06:10 +08:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:35:48 +08:00
|
|
|
static int xfrm_dev_register(struct net_device *dev)
|
|
|
|
{
|
|
|
|
return xfrm_api_check(dev);
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:06:10 +08:00
|
|
|
static int xfrm_dev_feat_change(struct net_device *dev)
|
|
|
|
{
|
2017-12-20 07:35:48 +08:00
|
|
|
return xfrm_api_check(dev);
|
2017-04-14 16:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int xfrm_dev_down(struct net_device *dev)
|
|
|
|
{
|
2017-05-08 15:30:18 +08:00
|
|
|
if (dev->features & NETIF_F_HW_ESP)
|
2017-04-14 16:06:10 +08:00
|
|
|
xfrm_dev_state_flush(dev_net(dev), dev, true);
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:05:53 +08:00
|
|
|
static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
|
|
|
|
|
|
|
switch (event) {
|
2017-04-14 16:06:10 +08:00
|
|
|
case NETDEV_REGISTER:
|
|
|
|
return xfrm_dev_register(dev);
|
|
|
|
|
|
|
|
case NETDEV_FEAT_CHANGE:
|
|
|
|
return xfrm_dev_feat_change(dev);
|
|
|
|
|
2017-04-14 16:05:53 +08:00
|
|
|
case NETDEV_DOWN:
|
2017-04-14 16:06:10 +08:00
|
|
|
return xfrm_dev_down(dev);
|
2017-04-14 16:05:53 +08:00
|
|
|
}
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block xfrm_dev_notifier = {
|
|
|
|
.notifier_call = xfrm_dev_event,
|
|
|
|
};
|
|
|
|
|
2018-03-29 22:03:25 +08:00
|
|
|
void __init xfrm_dev_init(void)
|
2017-04-14 16:05:53 +08:00
|
|
|
{
|
|
|
|
register_netdevice_notifier(&xfrm_dev_notifier);
|
|
|
|
}
|