Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 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> |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 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 | |
Hangbin Liu | b81f884a | 2017-06-01 14:57:56 +0800 | [diff] [blame] | 21 | #ifdef CONFIG_XFRM_OFFLOAD |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 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); |
Xin Long | 06a0afc | 2020-04-10 17:06:31 +0800 | [diff] [blame] | 28 | if (xo->flags & XFRM_GSO_SEGMENT) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 29 | skb->transport_header -= x->props.header_len; |
Xin Long | 06a0afc | 2020-04-10 17:06:31 +0800 | [diff] [blame] | 30 | |
| 31 | pskb_pull(skb, skb_transport_offset(skb) + x->props.header_len); |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void __xfrm_mode_tunnel_prep(struct xfrm_state *x, struct sk_buff *skb, |
| 35 | unsigned int hsize) |
| 36 | |
| 37 | { |
| 38 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 39 | |
| 40 | if (xo->flags & XFRM_GSO_SEGMENT) |
| 41 | skb->transport_header = skb->network_header + hsize; |
| 42 | |
| 43 | skb_reset_mac_len(skb); |
| 44 | pskb_pull(skb, skb->mac_len + x->props.header_len); |
| 45 | } |
| 46 | |
Xin Long | 3084917 | 2020-03-26 17:02:31 +0800 | [diff] [blame] | 47 | static void __xfrm_mode_beet_prep(struct xfrm_state *x, struct sk_buff *skb, |
| 48 | unsigned int hsize) |
| 49 | { |
| 50 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 51 | int phlen = 0; |
| 52 | |
| 53 | if (xo->flags & XFRM_GSO_SEGMENT) |
| 54 | skb->transport_header = skb->network_header + hsize; |
| 55 | |
| 56 | skb_reset_mac_len(skb); |
| 57 | if (x->sel.family != AF_INET6) { |
| 58 | phlen = IPV4_BEET_PHMAXLEN; |
| 59 | if (x->outer_mode.family == AF_INET6) |
| 60 | phlen += sizeof(struct ipv6hdr) - sizeof(struct iphdr); |
| 61 | } |
| 62 | |
| 63 | pskb_pull(skb, skb->mac_len + hsize + (x->props.header_len - phlen)); |
| 64 | } |
| 65 | |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 66 | /* Adjust pointers into the packet when IPsec is done at layer2 */ |
| 67 | static void xfrm_outer_mode_prep(struct xfrm_state *x, struct sk_buff *skb) |
| 68 | { |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 69 | switch (x->outer_mode.encap) { |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 70 | case XFRM_MODE_TUNNEL: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 71 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 72 | return __xfrm_mode_tunnel_prep(x, skb, |
| 73 | sizeof(struct iphdr)); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 74 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 75 | return __xfrm_mode_tunnel_prep(x, skb, |
| 76 | sizeof(struct ipv6hdr)); |
| 77 | break; |
| 78 | case XFRM_MODE_TRANSPORT: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 79 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 80 | return __xfrm_transport_prep(x, skb, |
| 81 | sizeof(struct iphdr)); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 82 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 83 | return __xfrm_transport_prep(x, skb, |
| 84 | sizeof(struct ipv6hdr)); |
| 85 | break; |
Xin Long | 3084917 | 2020-03-26 17:02:31 +0800 | [diff] [blame] | 86 | case XFRM_MODE_BEET: |
| 87 | if (x->outer_mode.family == AF_INET) |
| 88 | return __xfrm_mode_beet_prep(x, skb, |
| 89 | sizeof(struct iphdr)); |
| 90 | if (x->outer_mode.family == AF_INET6) |
| 91 | return __xfrm_mode_beet_prep(x, skb, |
| 92 | sizeof(struct ipv6hdr)); |
| 93 | break; |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 94 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 95 | case XFRM_MODE_IN_TRIGGER: |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 100 | struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again) |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 101 | { |
| 102 | int err; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 103 | unsigned long flags; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 104 | struct xfrm_state *x; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 105 | struct softnet_data *sd; |
Xin Long | d1d17a3 | 2020-03-04 16:51:42 +0800 | [diff] [blame] | 106 | struct sk_buff *skb2, *nskb, *pskb = NULL; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 107 | netdev_features_t esp_features = features; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 108 | struct xfrm_offload *xo = xfrm_offload(skb); |
Jarod Wilson | 272c233 | 2020-06-19 10:31:52 -0400 | [diff] [blame] | 109 | struct net_device *dev = skb->dev; |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 110 | struct sec_path *sp; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 111 | |
Huy Nguyen | 94579ac | 2020-06-01 16:39:37 -0500 | [diff] [blame] | 112 | if (!xo || (xo->flags & XFRM_XMIT)) |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 113 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 114 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 115 | if (!(features & NETIF_F_HW_ESP)) |
| 116 | esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 117 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 118 | sp = skb_sec_path(skb); |
| 119 | x = sp->xvec[sp->len - 1]; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 120 | if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND) |
| 121 | return skb; |
| 122 | |
Jarod Wilson | bdfd2d1 | 2020-06-23 16:40:01 -0400 | [diff] [blame] | 123 | /* This skb was already validated on the upper/virtual dev */ |
| 124 | if ((x->xso.dev != dev) && (x->xso.real_dev == dev)) |
Jarod Wilson | 272c233 | 2020-06-19 10:31:52 -0400 | [diff] [blame] | 125 | return skb; |
| 126 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 127 | local_irq_save(flags); |
| 128 | sd = this_cpu_ptr(&softnet_data); |
| 129 | err = !skb_queue_empty(&sd->xfrm_backlog); |
| 130 | local_irq_restore(flags); |
| 131 | |
| 132 | if (err) { |
| 133 | *again = true; |
| 134 | return skb; |
| 135 | } |
| 136 | |
Jarod Wilson | 272c233 | 2020-06-19 10:31:52 -0400 | [diff] [blame] | 137 | if (skb_is_gso(skb) && unlikely(x->xso.dev != dev)) { |
| 138 | struct sk_buff *segs; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 139 | |
Jarod Wilson | 272c233 | 2020-06-19 10:31:52 -0400 | [diff] [blame] | 140 | /* Packet got rerouted, fixup features and segment it. */ |
| 141 | esp_features = esp_features & ~(NETIF_F_HW_ESP | NETIF_F_GSO_ESP); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 142 | |
Jarod Wilson | 272c233 | 2020-06-19 10:31:52 -0400 | [diff] [blame] | 143 | segs = skb_gso_segment(skb, esp_features); |
| 144 | if (IS_ERR(segs)) { |
| 145 | kfree_skb(skb); |
| 146 | atomic_long_inc(&dev->tx_dropped); |
| 147 | return NULL; |
| 148 | } else { |
| 149 | consume_skb(skb); |
| 150 | skb = segs; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | if (!skb->next) { |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 155 | esp_features |= skb->dev->gso_partial_features; |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 156 | xfrm_outer_mode_prep(x, skb); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 157 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 158 | xo->flags |= XFRM_DEV_RESUME; |
| 159 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 160 | err = x->type_offload->xmit(x, skb, esp_features); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 161 | if (err) { |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 162 | if (err == -EINPROGRESS) |
| 163 | return NULL; |
| 164 | |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 165 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 166 | kfree_skb(skb); |
| 167 | return NULL; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 171 | |
| 172 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 175 | skb_list_walk_safe(skb, skb2, nskb) { |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 176 | esp_features |= skb->dev->gso_partial_features; |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 177 | skb_mark_not_on_list(skb2); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 178 | |
| 179 | xo = xfrm_offload(skb2); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 180 | xo->flags |= XFRM_DEV_RESUME; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 181 | |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 182 | xfrm_outer_mode_prep(x, skb2); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 183 | |
| 184 | err = x->type_offload->xmit(x, skb2, esp_features); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 185 | if (!err) { |
| 186 | skb2->next = nskb; |
| 187 | } else if (err != -EINPROGRESS) { |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 188 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
| 189 | skb2->next = nskb; |
| 190 | kfree_skb_list(skb2); |
| 191 | return NULL; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 192 | } else { |
| 193 | if (skb == skb2) |
| 194 | skb = nskb; |
Xin Long | d1d17a3 | 2020-03-04 16:51:42 +0800 | [diff] [blame] | 195 | else |
| 196 | pskb->next = nskb; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 197 | |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 198 | continue; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 199 | } |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 200 | |
| 201 | skb_push(skb2, skb2->data - skb_mac_header(skb2)); |
Xin Long | d1d17a3 | 2020-03-04 16:51:42 +0800 | [diff] [blame] | 202 | pskb = skb2; |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 203 | } |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 204 | |
| 205 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 206 | } |
| 207 | EXPORT_SYMBOL_GPL(validate_xmit_xfrm); |
| 208 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 209 | int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, |
| 210 | struct xfrm_user_offload *xuo) |
| 211 | { |
| 212 | int err; |
| 213 | struct dst_entry *dst; |
| 214 | struct net_device *dev; |
| 215 | struct xfrm_state_offload *xso = &x->xso; |
| 216 | xfrm_address_t *saddr; |
| 217 | xfrm_address_t *daddr; |
| 218 | |
| 219 | if (!x->type_offload) |
Ilan Tayari | ffdb521 | 2017-08-01 12:49:08 +0300 | [diff] [blame] | 220 | return -EINVAL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 221 | |
Yossef Efraim | 50bd870 | 2018-01-14 11:39:10 +0200 | [diff] [blame] | 222 | /* We don't yet support UDP encapsulation and TFC padding. */ |
| 223 | if (x->encap || x->tfcpad) |
Yossef Efraim | 43024b9 | 2017-11-28 11:49:29 +0200 | [diff] [blame] | 224 | return -EINVAL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 225 | |
| 226 | dev = dev_get_by_index(net, xuo->ifindex); |
| 227 | if (!dev) { |
| 228 | if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) { |
| 229 | saddr = &x->props.saddr; |
| 230 | daddr = &x->id.daddr; |
| 231 | } else { |
| 232 | saddr = &x->id.daddr; |
| 233 | daddr = &x->props.saddr; |
| 234 | } |
| 235 | |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 236 | dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr, |
Steffen Klassert | 9b42c1f | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 237 | x->props.family, |
| 238 | xfrm_smark_get(0, x)); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 239 | if (IS_ERR(dst)) |
| 240 | return 0; |
| 241 | |
| 242 | dev = dst->dev; |
| 243 | |
| 244 | dev_hold(dev); |
| 245 | dst_release(dst); |
| 246 | } |
| 247 | |
| 248 | if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) { |
Steffen Klassert | 67a6338 | 2017-09-04 10:59:55 +0200 | [diff] [blame] | 249 | xso->dev = NULL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 250 | dev_put(dev); |
| 251 | return 0; |
| 252 | } |
| 253 | |
Yossef Efraim | 50bd870 | 2018-01-14 11:39:10 +0200 | [diff] [blame] | 254 | if (x->props.flags & XFRM_STATE_ESN && |
| 255 | !dev->xfrmdev_ops->xdo_dev_state_advance_esn) { |
| 256 | xso->dev = NULL; |
| 257 | dev_put(dev); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 261 | xso->dev = dev; |
Eric Dumazet | e1b539b | 2021-12-09 07:44:51 -0800 | [diff] [blame] | 262 | netdev_tracker_alloc(dev, &xso->dev_tracker, GFP_ATOMIC); |
Jarod Wilson | bdfd2d1 | 2020-06-23 16:40:01 -0400 | [diff] [blame] | 263 | xso->real_dev = dev; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 264 | xso->num_exthdrs = 1; |
| 265 | xso->flags = xuo->flags; |
| 266 | |
| 267 | err = dev->xfrmdev_ops->xdo_dev_state_add(x); |
| 268 | if (err) { |
Shannon Nelson | 4a13209 | 2018-08-22 14:38:10 -0700 | [diff] [blame] | 269 | xso->num_exthdrs = 0; |
| 270 | xso->flags = 0; |
Aviad Yehezkel | aa5dd6f | 2018-01-18 15:41:51 +0200 | [diff] [blame] | 271 | xso->dev = NULL; |
Ayush Sawal | dd72fad | 2021-06-22 09:25:31 +0530 | [diff] [blame] | 272 | xso->real_dev = NULL; |
Eric Dumazet | e1b539b | 2021-12-09 07:44:51 -0800 | [diff] [blame] | 273 | dev_put_track(dev, &xso->dev_tracker); |
Shannon Nelson | 4a13209 | 2018-08-22 14:38:10 -0700 | [diff] [blame] | 274 | |
| 275 | if (err != -EOPNOTSUPP) |
| 276 | return err; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | EXPORT_SYMBOL_GPL(xfrm_dev_state_add); |
| 282 | |
| 283 | bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x) |
| 284 | { |
| 285 | int mtu; |
| 286 | struct dst_entry *dst = skb_dst(skb); |
| 287 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 288 | struct net_device *dev = x->xso.dev; |
| 289 | |
| 290 | if (!x->type_offload || x->encap) |
| 291 | return false; |
| 292 | |
Shannon Nelson | fcb662d | 2018-06-26 14:19:10 -0700 | [diff] [blame] | 293 | if ((!dev || (dev == xfrm_dst_path(dst)->dev)) && |
Florian Westphal | c7b37c7 | 2019-06-24 22:04:48 +0200 | [diff] [blame] | 294 | (!xdst->child->xfrm)) { |
| 295 | mtu = xfrm_state_mtu(x, xdst->child_mtu_cached); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 296 | if (skb->len <= mtu) |
| 297 | goto ok; |
| 298 | |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 299 | if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 300 | goto ok; |
| 301 | } |
| 302 | |
| 303 | return false; |
| 304 | |
| 305 | ok: |
| 306 | if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok) |
| 307 | return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x); |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 312 | |
| 313 | void xfrm_dev_resume(struct sk_buff *skb) |
| 314 | { |
| 315 | struct net_device *dev = skb->dev; |
| 316 | int ret = NETDEV_TX_BUSY; |
| 317 | struct netdev_queue *txq; |
| 318 | struct softnet_data *sd; |
| 319 | unsigned long flags; |
| 320 | |
| 321 | rcu_read_lock(); |
Paolo Abeni | 4bd97d5 | 2019-03-20 11:02:04 +0100 | [diff] [blame] | 322 | txq = netdev_core_pick_tx(dev, skb, NULL); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 323 | |
| 324 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
| 325 | if (!netif_xmit_frozen_or_stopped(txq)) |
| 326 | skb = dev_hard_start_xmit(skb, dev, txq, &ret); |
| 327 | HARD_TX_UNLOCK(dev, txq); |
| 328 | |
| 329 | if (!dev_xmit_complete(ret)) { |
| 330 | local_irq_save(flags); |
| 331 | sd = this_cpu_ptr(&softnet_data); |
| 332 | skb_queue_tail(&sd->xfrm_backlog, skb); |
| 333 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 334 | local_irq_restore(flags); |
| 335 | } |
| 336 | rcu_read_unlock(); |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(xfrm_dev_resume); |
| 339 | |
| 340 | void xfrm_dev_backlog(struct softnet_data *sd) |
| 341 | { |
| 342 | struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog; |
| 343 | struct sk_buff_head list; |
| 344 | struct sk_buff *skb; |
| 345 | |
| 346 | if (skb_queue_empty(xfrm_backlog)) |
| 347 | return; |
| 348 | |
| 349 | __skb_queue_head_init(&list); |
| 350 | |
| 351 | spin_lock(&xfrm_backlog->lock); |
| 352 | skb_queue_splice_init(xfrm_backlog, &list); |
| 353 | spin_unlock(&xfrm_backlog->lock); |
| 354 | |
| 355 | while (!skb_queue_empty(&list)) { |
| 356 | skb = __skb_dequeue(&list); |
| 357 | xfrm_dev_resume(skb); |
| 358 | } |
| 359 | |
| 360 | } |
Hangbin Liu | b81f884a | 2017-06-01 14:57:56 +0800 | [diff] [blame] | 361 | #endif |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 362 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 363 | static int xfrm_api_check(struct net_device *dev) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 364 | { |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 365 | #ifdef CONFIG_XFRM_OFFLOAD |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 366 | if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) && |
| 367 | !(dev->features & NETIF_F_HW_ESP)) |
| 368 | return NOTIFY_BAD; |
| 369 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 370 | if ((dev->features & NETIF_F_HW_ESP) && |
| 371 | (!(dev->xfrmdev_ops && |
| 372 | dev->xfrmdev_ops->xdo_dev_state_add && |
| 373 | dev->xfrmdev_ops->xdo_dev_state_delete))) |
| 374 | return NOTIFY_BAD; |
| 375 | #else |
| 376 | if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM)) |
| 377 | return NOTIFY_BAD; |
| 378 | #endif |
| 379 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 380 | return NOTIFY_DONE; |
| 381 | } |
| 382 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 383 | static int xfrm_dev_register(struct net_device *dev) |
| 384 | { |
| 385 | return xfrm_api_check(dev); |
| 386 | } |
| 387 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 388 | static int xfrm_dev_feat_change(struct net_device *dev) |
| 389 | { |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 390 | return xfrm_api_check(dev); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int xfrm_dev_down(struct net_device *dev) |
| 394 | { |
Ilan Tayari | 2c1497b | 2017-05-08 10:30:18 +0300 | [diff] [blame] | 395 | if (dev->features & NETIF_F_HW_ESP) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 396 | xfrm_dev_state_flush(dev_net(dev), dev, true); |
| 397 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 398 | return NOTIFY_DONE; |
| 399 | } |
| 400 | |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 401 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 402 | { |
| 403 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 404 | |
| 405 | switch (event) { |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 406 | case NETDEV_REGISTER: |
| 407 | return xfrm_dev_register(dev); |
| 408 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 409 | case NETDEV_FEAT_CHANGE: |
| 410 | return xfrm_dev_feat_change(dev); |
| 411 | |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 412 | case NETDEV_DOWN: |
Raed Salem | 03891f8 | 2020-02-02 13:19:34 +0200 | [diff] [blame] | 413 | case NETDEV_UNREGISTER: |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 414 | return xfrm_dev_down(dev); |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 415 | } |
| 416 | return NOTIFY_DONE; |
| 417 | } |
| 418 | |
| 419 | static struct notifier_block xfrm_dev_notifier = { |
| 420 | .notifier_call = xfrm_dev_event, |
| 421 | }; |
| 422 | |
Kirill Tkhai | e9a441b | 2018-03-29 17:03:25 +0300 | [diff] [blame] | 423 | void __init xfrm_dev_init(void) |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 424 | { |
| 425 | register_netdevice_notifier(&xfrm_dev_notifier); |
| 426 | } |