1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * xfrm_device.c - IPsec device offloading code. 4 * 5 * Copyright (c) 2015 secunet Security Networks AG 6 * 7 * Author: 8 * Steffen Klassert <steffen.klassert@secunet.com> 9 */ 10 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> 20 21 #ifdef CONFIG_XFRM_OFFLOAD 22 static void __xfrm_transport_prep(struct xfrm_state *x, struct sk_buff *skb, 23 unsigned int hsize) 24 { 25 struct xfrm_offload *xo = xfrm_offload(skb); 26 27 skb_reset_mac_len(skb); 28 pskb_pull(skb, skb->mac_len + hsize + x->props.header_len); 29 30 if (xo->flags & XFRM_GSO_SEGMENT) { 31 skb_reset_transport_header(skb); 32 skb->transport_header -= x->props.header_len; 33 } 34 } 35 36 static void __xfrm_mode_tunnel_prep(struct xfrm_state *x, struct sk_buff *skb, 37 unsigned int hsize) 38 39 { 40 struct xfrm_offload *xo = xfrm_offload(skb); 41 42 if (xo->flags & XFRM_GSO_SEGMENT) 43 skb->transport_header = skb->network_header + hsize; 44 45 skb_reset_mac_len(skb); 46 pskb_pull(skb, skb->mac_len + x->props.header_len); 47 } 48 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) 51 { 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; 73 } 74 } 75 76 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again) 77 { 78 int err; 79 unsigned long flags; 80 struct xfrm_state *x; 81 struct sk_buff *skb2; 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; 86 87 if (!xo) 88 return skb; 89 90 if (!(features & NETIF_F_HW_ESP)) 91 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); 92 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; 97 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); 102 103 if (err) { 104 *again = true; 105 return skb; 106 } 107 108 if (skb_is_gso(skb)) { 109 struct net_device *dev = skb->dev; 110 111 if (unlikely(x->xso.dev != dev)) { 112 struct sk_buff *segs; 113 114 /* Packet got rerouted, fixup features and segment it. */ 115 esp_features = esp_features & ~(NETIF_F_HW_ESP 116 | NETIF_F_GSO_ESP); 117 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; 126 } 127 } 128 } 129 130 if (!skb->next) { 131 esp_features |= skb->dev->gso_partial_features; 132 xfrm_outer_mode_prep(x, skb); 133 134 xo->flags |= XFRM_DEV_RESUME; 135 136 err = x->type_offload->xmit(x, skb, esp_features); 137 if (err) { 138 if (err == -EINPROGRESS) 139 return NULL; 140 141 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); 142 kfree_skb(skb); 143 return NULL; 144 } 145 146 skb_push(skb, skb->data - skb_mac_header(skb)); 147 148 return skb; 149 } 150 151 skb2 = skb; 152 153 do { 154 struct sk_buff *nskb = skb2->next; 155 156 esp_features |= skb->dev->gso_partial_features; 157 skb_mark_not_on_list(skb2); 158 159 xo = xfrm_offload(skb2); 160 xo->flags |= XFRM_DEV_RESUME; 161 162 xfrm_outer_mode_prep(x, skb2); 163 164 err = x->type_offload->xmit(x, skb2, esp_features); 165 if (!err) { 166 skb2->next = nskb; 167 } else if (err != -EINPROGRESS) { 168 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); 169 skb2->next = nskb; 170 kfree_skb_list(skb2); 171 return NULL; 172 } else { 173 if (skb == skb2) 174 skb = nskb; 175 176 if (!skb) 177 return NULL; 178 179 goto skip_push; 180 } 181 182 skb_push(skb2, skb2->data - skb_mac_header(skb2)); 183 184 skip_push: 185 skb2 = nskb; 186 } while (skb2); 187 188 return skb; 189 } 190 EXPORT_SYMBOL_GPL(validate_xmit_xfrm); 191 192 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, 193 struct xfrm_user_offload *xuo) 194 { 195 int err; 196 struct dst_entry *dst; 197 struct net_device *dev; 198 struct xfrm_state_offload *xso = &x->xso; 199 xfrm_address_t *saddr; 200 xfrm_address_t *daddr; 201 202 if (!x->type_offload) 203 return -EINVAL; 204 205 /* We don't yet support UDP encapsulation and TFC padding. */ 206 if (x->encap || x->tfcpad) 207 return -EINVAL; 208 209 dev = dev_get_by_index(net, xuo->ifindex); 210 if (!dev) { 211 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) { 212 saddr = &x->props.saddr; 213 daddr = &x->id.daddr; 214 } else { 215 saddr = &x->id.daddr; 216 daddr = &x->props.saddr; 217 } 218 219 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr, 220 x->props.family, 221 xfrm_smark_get(0, x)); 222 if (IS_ERR(dst)) 223 return 0; 224 225 dev = dst->dev; 226 227 dev_hold(dev); 228 dst_release(dst); 229 } 230 231 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) { 232 xso->dev = NULL; 233 dev_put(dev); 234 return 0; 235 } 236 237 if (x->props.flags & XFRM_STATE_ESN && 238 !dev->xfrmdev_ops->xdo_dev_state_advance_esn) { 239 xso->dev = NULL; 240 dev_put(dev); 241 return -EINVAL; 242 } 243 244 xso->dev = dev; 245 xso->num_exthdrs = 1; 246 xso->flags = xuo->flags; 247 248 err = dev->xfrmdev_ops->xdo_dev_state_add(x); 249 if (err) { 250 xso->num_exthdrs = 0; 251 xso->flags = 0; 252 xso->dev = NULL; 253 dev_put(dev); 254 255 if (err != -EOPNOTSUPP) 256 return err; 257 } 258 259 return 0; 260 } 261 EXPORT_SYMBOL_GPL(xfrm_dev_state_add); 262 263 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x) 264 { 265 int mtu; 266 struct dst_entry *dst = skb_dst(skb); 267 struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 268 struct net_device *dev = x->xso.dev; 269 270 if (!x->type_offload || x->encap) 271 return false; 272 273 if ((!dev || (dev == xfrm_dst_path(dst)->dev)) && 274 (!xdst->child->xfrm && x->type->get_mtu)) { 275 mtu = x->type->get_mtu(x, xdst->child_mtu_cached); 276 277 if (skb->len <= mtu) 278 goto ok; 279 280 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) 281 goto ok; 282 } 283 284 return false; 285 286 ok: 287 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok) 288 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x); 289 290 return true; 291 } 292 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok); 293 294 void xfrm_dev_resume(struct sk_buff *skb) 295 { 296 struct net_device *dev = skb->dev; 297 int ret = NETDEV_TX_BUSY; 298 struct netdev_queue *txq; 299 struct softnet_data *sd; 300 unsigned long flags; 301 302 rcu_read_lock(); 303 txq = netdev_core_pick_tx(dev, skb, NULL); 304 305 HARD_TX_LOCK(dev, txq, smp_processor_id()); 306 if (!netif_xmit_frozen_or_stopped(txq)) 307 skb = dev_hard_start_xmit(skb, dev, txq, &ret); 308 HARD_TX_UNLOCK(dev, txq); 309 310 if (!dev_xmit_complete(ret)) { 311 local_irq_save(flags); 312 sd = this_cpu_ptr(&softnet_data); 313 skb_queue_tail(&sd->xfrm_backlog, skb); 314 raise_softirq_irqoff(NET_TX_SOFTIRQ); 315 local_irq_restore(flags); 316 } 317 rcu_read_unlock(); 318 } 319 EXPORT_SYMBOL_GPL(xfrm_dev_resume); 320 321 void xfrm_dev_backlog(struct softnet_data *sd) 322 { 323 struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog; 324 struct sk_buff_head list; 325 struct sk_buff *skb; 326 327 if (skb_queue_empty(xfrm_backlog)) 328 return; 329 330 __skb_queue_head_init(&list); 331 332 spin_lock(&xfrm_backlog->lock); 333 skb_queue_splice_init(xfrm_backlog, &list); 334 spin_unlock(&xfrm_backlog->lock); 335 336 while (!skb_queue_empty(&list)) { 337 skb = __skb_dequeue(&list); 338 xfrm_dev_resume(skb); 339 } 340 341 } 342 #endif 343 344 static int xfrm_api_check(struct net_device *dev) 345 { 346 #ifdef CONFIG_XFRM_OFFLOAD 347 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) && 348 !(dev->features & NETIF_F_HW_ESP)) 349 return NOTIFY_BAD; 350 351 if ((dev->features & NETIF_F_HW_ESP) && 352 (!(dev->xfrmdev_ops && 353 dev->xfrmdev_ops->xdo_dev_state_add && 354 dev->xfrmdev_ops->xdo_dev_state_delete))) 355 return NOTIFY_BAD; 356 #else 357 if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM)) 358 return NOTIFY_BAD; 359 #endif 360 361 return NOTIFY_DONE; 362 } 363 364 static int xfrm_dev_register(struct net_device *dev) 365 { 366 return xfrm_api_check(dev); 367 } 368 369 static int xfrm_dev_feat_change(struct net_device *dev) 370 { 371 return xfrm_api_check(dev); 372 } 373 374 static int xfrm_dev_down(struct net_device *dev) 375 { 376 if (dev->features & NETIF_F_HW_ESP) 377 xfrm_dev_state_flush(dev_net(dev), dev, true); 378 379 return NOTIFY_DONE; 380 } 381 382 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) 383 { 384 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 385 386 switch (event) { 387 case NETDEV_REGISTER: 388 return xfrm_dev_register(dev); 389 390 case NETDEV_FEAT_CHANGE: 391 return xfrm_dev_feat_change(dev); 392 393 case NETDEV_DOWN: 394 return xfrm_dev_down(dev); 395 } 396 return NOTIFY_DONE; 397 } 398 399 static struct notifier_block xfrm_dev_notifier = { 400 .notifier_call = xfrm_dev_event, 401 }; 402 403 void __init xfrm_dev_init(void) 404 { 405 register_netdevice_notifier(&xfrm_dev_notifier); 406 } 407