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); |
| 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 | { |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 52 | switch (x->outer_mode.encap) { |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 53 | case XFRM_MODE_TUNNEL: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 54 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 55 | return __xfrm_mode_tunnel_prep(x, skb, |
| 56 | sizeof(struct iphdr)); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 57 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 58 | return __xfrm_mode_tunnel_prep(x, skb, |
| 59 | sizeof(struct ipv6hdr)); |
| 60 | break; |
| 61 | case XFRM_MODE_TRANSPORT: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 62 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 63 | return __xfrm_transport_prep(x, skb, |
| 64 | sizeof(struct iphdr)); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 65 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 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 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | int err; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 79 | unsigned long flags; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 80 | struct xfrm_state *x; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 81 | struct sk_buff *skb2; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 82 | struct softnet_data *sd; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 83 | netdev_features_t esp_features = features; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 84 | struct xfrm_offload *xo = xfrm_offload(skb); |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 85 | struct sec_path *sp; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 86 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 87 | if (!xo) |
| 88 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 89 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 90 | if (!(features & NETIF_F_HW_ESP)) |
| 91 | esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 92 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 93 | sp = skb_sec_path(skb); |
| 94 | x = sp->xvec[sp->len - 1]; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 95 | if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND) |
| 96 | return skb; |
| 97 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 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 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 108 | if (skb_is_gso(skb)) { |
| 109 | struct net_device *dev = skb->dev; |
| 110 | |
Shannon Nelson | fcb662d | 2018-06-26 14:19:10 -0700 | [diff] [blame] | 111 | if (unlikely(x->xso.dev != dev)) { |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 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)) { |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 120 | kfree_skb(skb); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 121 | atomic_long_inc(&dev->tx_dropped); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 122 | return NULL; |
| 123 | } else { |
| 124 | consume_skb(skb); |
| 125 | skb = segs; |
| 126 | } |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!skb->next) { |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 131 | esp_features |= skb->dev->gso_partial_features; |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 132 | xfrm_outer_mode_prep(x, skb); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 133 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 134 | xo->flags |= XFRM_DEV_RESUME; |
| 135 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 136 | err = x->type_offload->xmit(x, skb, esp_features); |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 137 | if (err) { |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 138 | if (err == -EINPROGRESS) |
| 139 | return NULL; |
| 140 | |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 141 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 142 | kfree_skb(skb); |
| 143 | return NULL; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 147 | |
| 148 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 151 | skb2 = skb; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 152 | |
| 153 | do { |
| 154 | struct sk_buff *nskb = skb2->next; |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 155 | |
| 156 | esp_features |= skb->dev->gso_partial_features; |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 157 | skb_mark_not_on_list(skb2); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 158 | |
| 159 | xo = xfrm_offload(skb2); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 160 | xo->flags |= XFRM_DEV_RESUME; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 161 | |
Florian Westphal | 303c5fa | 2019-03-29 21:16:26 +0100 | [diff] [blame] | 162 | xfrm_outer_mode_prep(x, skb2); |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 163 | |
| 164 | err = x->type_offload->xmit(x, skb2, esp_features); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 165 | if (!err) { |
| 166 | skb2->next = nskb; |
| 167 | } else if (err != -EINPROGRESS) { |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 168 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
| 169 | skb2->next = nskb; |
| 170 | kfree_skb_list(skb2); |
| 171 | return NULL; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 172 | } else { |
| 173 | if (skb == skb2) |
| 174 | skb = nskb; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 175 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 176 | if (!skb) |
| 177 | return NULL; |
| 178 | |
| 179 | goto skip_push; |
| 180 | } |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 181 | |
| 182 | skb_push(skb2, skb2->data - skb_mac_header(skb2)); |
| 183 | |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 184 | skip_push: |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 185 | skb2 = nskb; |
| 186 | } while (skb2); |
| 187 | |
| 188 | return skb; |
Steffen Klassert | f6e2711 | 2017-04-14 10:07:28 +0200 | [diff] [blame] | 189 | } |
| 190 | EXPORT_SYMBOL_GPL(validate_xmit_xfrm); |
| 191 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 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) |
Ilan Tayari | ffdb521 | 2017-08-01 12:49:08 +0300 | [diff] [blame] | 203 | return -EINVAL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 204 | |
Yossef Efraim | 50bd870 | 2018-01-14 11:39:10 +0200 | [diff] [blame] | 205 | /* We don't yet support UDP encapsulation and TFC padding. */ |
| 206 | if (x->encap || x->tfcpad) |
Yossef Efraim | 43024b9 | 2017-11-28 11:49:29 +0200 | [diff] [blame] | 207 | return -EINVAL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 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 | |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 219 | dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr, |
Steffen Klassert | 9b42c1f | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 220 | x->props.family, |
| 221 | xfrm_smark_get(0, x)); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 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) { |
Steffen Klassert | 67a6338 | 2017-09-04 10:59:55 +0200 | [diff] [blame] | 232 | xso->dev = NULL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 233 | dev_put(dev); |
| 234 | return 0; |
| 235 | } |
| 236 | |
Yossef Efraim | 50bd870 | 2018-01-14 11:39:10 +0200 | [diff] [blame] | 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 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 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) { |
Shannon Nelson | 4a13209 | 2018-08-22 14:38:10 -0700 | [diff] [blame] | 250 | xso->num_exthdrs = 0; |
| 251 | xso->flags = 0; |
Aviad Yehezkel | aa5dd6f | 2018-01-18 15:41:51 +0200 | [diff] [blame] | 252 | xso->dev = NULL; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 253 | dev_put(dev); |
Shannon Nelson | 4a13209 | 2018-08-22 14:38:10 -0700 | [diff] [blame] | 254 | |
| 255 | if (err != -EOPNOTSUPP) |
| 256 | return err; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 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 | |
Shannon Nelson | fcb662d | 2018-06-26 14:19:10 -0700 | [diff] [blame] | 273 | if ((!dev || (dev == xfrm_dst_path(dst)->dev)) && |
Florian Westphal | c7b37c7 | 2019-06-24 22:04:48 +0200 | [diff] [blame] | 274 | (!xdst->child->xfrm)) { |
| 275 | mtu = xfrm_state_mtu(x, xdst->child_mtu_cached); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 276 | if (skb->len <= mtu) |
| 277 | goto ok; |
| 278 | |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 279 | if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 280 | goto ok; |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | |
| 285 | ok: |
| 286 | if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok) |
| 287 | return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x); |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 292 | |
| 293 | void xfrm_dev_resume(struct sk_buff *skb) |
| 294 | { |
| 295 | struct net_device *dev = skb->dev; |
| 296 | int ret = NETDEV_TX_BUSY; |
| 297 | struct netdev_queue *txq; |
| 298 | struct softnet_data *sd; |
| 299 | unsigned long flags; |
| 300 | |
| 301 | rcu_read_lock(); |
Paolo Abeni | 4bd97d5 | 2019-03-20 11:02:04 +0100 | [diff] [blame] | 302 | txq = netdev_core_pick_tx(dev, skb, NULL); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 303 | |
| 304 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
| 305 | if (!netif_xmit_frozen_or_stopped(txq)) |
| 306 | skb = dev_hard_start_xmit(skb, dev, txq, &ret); |
| 307 | HARD_TX_UNLOCK(dev, txq); |
| 308 | |
| 309 | if (!dev_xmit_complete(ret)) { |
| 310 | local_irq_save(flags); |
| 311 | sd = this_cpu_ptr(&softnet_data); |
| 312 | skb_queue_tail(&sd->xfrm_backlog, skb); |
| 313 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 314 | local_irq_restore(flags); |
| 315 | } |
| 316 | rcu_read_unlock(); |
| 317 | } |
| 318 | EXPORT_SYMBOL_GPL(xfrm_dev_resume); |
| 319 | |
| 320 | void xfrm_dev_backlog(struct softnet_data *sd) |
| 321 | { |
| 322 | struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog; |
| 323 | struct sk_buff_head list; |
| 324 | struct sk_buff *skb; |
| 325 | |
| 326 | if (skb_queue_empty(xfrm_backlog)) |
| 327 | return; |
| 328 | |
| 329 | __skb_queue_head_init(&list); |
| 330 | |
| 331 | spin_lock(&xfrm_backlog->lock); |
| 332 | skb_queue_splice_init(xfrm_backlog, &list); |
| 333 | spin_unlock(&xfrm_backlog->lock); |
| 334 | |
| 335 | while (!skb_queue_empty(&list)) { |
| 336 | skb = __skb_dequeue(&list); |
| 337 | xfrm_dev_resume(skb); |
| 338 | } |
| 339 | |
| 340 | } |
Hangbin Liu | b81f884a | 2017-06-01 14:57:56 +0800 | [diff] [blame] | 341 | #endif |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 342 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 343 | static int xfrm_api_check(struct net_device *dev) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 344 | { |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 345 | #ifdef CONFIG_XFRM_OFFLOAD |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 346 | if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) && |
| 347 | !(dev->features & NETIF_F_HW_ESP)) |
| 348 | return NOTIFY_BAD; |
| 349 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 350 | if ((dev->features & NETIF_F_HW_ESP) && |
| 351 | (!(dev->xfrmdev_ops && |
| 352 | dev->xfrmdev_ops->xdo_dev_state_add && |
| 353 | dev->xfrmdev_ops->xdo_dev_state_delete))) |
| 354 | return NOTIFY_BAD; |
| 355 | #else |
| 356 | if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM)) |
| 357 | return NOTIFY_BAD; |
| 358 | #endif |
| 359 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 360 | return NOTIFY_DONE; |
| 361 | } |
| 362 | |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 363 | static int xfrm_dev_register(struct net_device *dev) |
| 364 | { |
| 365 | return xfrm_api_check(dev); |
| 366 | } |
| 367 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 368 | static int xfrm_dev_feat_change(struct net_device *dev) |
| 369 | { |
Shannon Nelson | 92a2320 | 2017-12-19 15:35:48 -0800 | [diff] [blame] | 370 | return xfrm_api_check(dev); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int xfrm_dev_down(struct net_device *dev) |
| 374 | { |
Ilan Tayari | 2c1497b | 2017-05-08 10:30:18 +0300 | [diff] [blame] | 375 | if (dev->features & NETIF_F_HW_ESP) |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 376 | xfrm_dev_state_flush(dev_net(dev), dev, true); |
| 377 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 378 | return NOTIFY_DONE; |
| 379 | } |
| 380 | |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 381 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 382 | { |
| 383 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 384 | |
| 385 | switch (event) { |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 386 | case NETDEV_REGISTER: |
| 387 | return xfrm_dev_register(dev); |
| 388 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 389 | case NETDEV_FEAT_CHANGE: |
| 390 | return xfrm_dev_feat_change(dev); |
| 391 | |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 392 | case NETDEV_DOWN: |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 393 | return xfrm_dev_down(dev); |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 394 | } |
| 395 | return NOTIFY_DONE; |
| 396 | } |
| 397 | |
| 398 | static struct notifier_block xfrm_dev_notifier = { |
| 399 | .notifier_call = xfrm_dev_event, |
| 400 | }; |
| 401 | |
Kirill Tkhai | e9a441b | 2018-03-29 17:03:25 +0300 | [diff] [blame] | 402 | void __init xfrm_dev_init(void) |
Steffen Klassert | 21f42cc | 2017-04-14 10:05:53 +0200 | [diff] [blame] | 403 | { |
| 404 | register_netdevice_notifier(&xfrm_dev_notifier); |
| 405 | } |