1 /* 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 25 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, 26 struct xfrm_user_offload *xuo) 27 { 28 int err; 29 struct dst_entry *dst; 30 struct net_device *dev; 31 struct xfrm_state_offload *xso = &x->xso; 32 xfrm_address_t *saddr; 33 xfrm_address_t *daddr; 34 35 if (!x->type_offload) 36 return 0; 37 38 /* We don't yet support UDP encapsulation, TFC padding and ESN. */ 39 if (x->encap || x->tfcpad || (x->props.flags & XFRM_STATE_ESN)) 40 return 0; 41 42 dev = dev_get_by_index(net, xuo->ifindex); 43 if (!dev) { 44 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) { 45 saddr = &x->props.saddr; 46 daddr = &x->id.daddr; 47 } else { 48 saddr = &x->id.daddr; 49 daddr = &x->props.saddr; 50 } 51 52 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr, x->props.family); 53 if (IS_ERR(dst)) 54 return 0; 55 56 dev = dst->dev; 57 58 dev_hold(dev); 59 dst_release(dst); 60 } 61 62 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) { 63 dev_put(dev); 64 return 0; 65 } 66 67 xso->dev = dev; 68 xso->num_exthdrs = 1; 69 xso->flags = xuo->flags; 70 71 err = dev->xfrmdev_ops->xdo_dev_state_add(x); 72 if (err) { 73 dev_put(dev); 74 return err; 75 } 76 77 return 0; 78 } 79 EXPORT_SYMBOL_GPL(xfrm_dev_state_add); 80 81 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x) 82 { 83 int mtu; 84 struct dst_entry *dst = skb_dst(skb); 85 struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 86 struct net_device *dev = x->xso.dev; 87 88 if (!x->type_offload || x->encap) 89 return false; 90 91 if ((x->xso.offload_handle && (dev == dst->path->dev)) && 92 !dst->child->xfrm && x->type->get_mtu) { 93 mtu = x->type->get_mtu(x, xdst->child_mtu_cached); 94 95 if (skb->len <= mtu) 96 goto ok; 97 98 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu)) 99 goto ok; 100 } 101 102 return false; 103 104 ok: 105 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok) 106 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x); 107 108 return true; 109 } 110 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok); 111 112 int xfrm_dev_register(struct net_device *dev) 113 { 114 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops) 115 return NOTIFY_BAD; 116 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) && 117 !(dev->features & NETIF_F_HW_ESP)) 118 return NOTIFY_BAD; 119 120 return NOTIFY_DONE; 121 } 122 123 static int xfrm_dev_unregister(struct net_device *dev) 124 { 125 return NOTIFY_DONE; 126 } 127 128 static int xfrm_dev_feat_change(struct net_device *dev) 129 { 130 if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops) 131 return NOTIFY_BAD; 132 else if (!(dev->features & NETIF_F_HW_ESP)) 133 dev->xfrmdev_ops = NULL; 134 135 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) && 136 !(dev->features & NETIF_F_HW_ESP)) 137 return NOTIFY_BAD; 138 139 return NOTIFY_DONE; 140 } 141 142 static int xfrm_dev_down(struct net_device *dev) 143 { 144 if (dev->hw_features & NETIF_F_HW_ESP) 145 xfrm_dev_state_flush(dev_net(dev), dev, true); 146 147 xfrm_garbage_collect(dev_net(dev)); 148 149 return NOTIFY_DONE; 150 } 151 152 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) 153 { 154 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 155 156 switch (event) { 157 case NETDEV_REGISTER: 158 return xfrm_dev_register(dev); 159 160 case NETDEV_UNREGISTER: 161 return xfrm_dev_unregister(dev); 162 163 case NETDEV_FEAT_CHANGE: 164 return xfrm_dev_feat_change(dev); 165 166 case NETDEV_DOWN: 167 return xfrm_dev_down(dev); 168 } 169 return NOTIFY_DONE; 170 } 171 172 static struct notifier_block xfrm_dev_notifier = { 173 .notifier_call = xfrm_dev_event, 174 }; 175 176 void __net_init xfrm_dev_init(void) 177 { 178 register_netdevice_notifier(&xfrm_dev_notifier); 179 } 180