2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <crypto/internal/geniv.h>
35 #include <crypto/aead.h>
36 #include <linux/inetdevice.h>
37 #include <linux/netdevice.h>
38 #include <linux/module.h>
41 #include "en_accel/ipsec.h"
42 #include "en_accel/ipsec_rxtx.h"
45 static struct mlx5e_ipsec_sa_entry *to_ipsec_sa_entry(struct xfrm_state *x)
47 struct mlx5e_ipsec_sa_entry *sa;
52 sa = (struct mlx5e_ipsec_sa_entry *)x->xso.offload_handle;
60 struct xfrm_state *mlx5e_ipsec_sadb_rx_lookup(struct mlx5e_ipsec *ipsec,
63 struct mlx5e_ipsec_sa_entry *sa_entry;
64 struct xfrm_state *ret = NULL;
67 hash_for_each_possible_rcu(ipsec->sadb_rx, sa_entry, hlist, handle)
68 if (sa_entry->handle == handle) {
78 static int mlx5e_ipsec_sadb_rx_add(struct mlx5e_ipsec_sa_entry *sa_entry)
80 struct mlx5e_ipsec *ipsec = sa_entry->ipsec;
84 ret = ida_simple_get(&ipsec->halloc, 1, 0, GFP_KERNEL);
88 spin_lock_irqsave(&ipsec->sadb_rx_lock, flags);
89 sa_entry->handle = ret;
90 hash_add_rcu(ipsec->sadb_rx, &sa_entry->hlist, sa_entry->handle);
91 spin_unlock_irqrestore(&ipsec->sadb_rx_lock, flags);
96 static void mlx5e_ipsec_sadb_rx_del(struct mlx5e_ipsec_sa_entry *sa_entry)
98 struct mlx5e_ipsec *ipsec = sa_entry->ipsec;
101 spin_lock_irqsave(&ipsec->sadb_rx_lock, flags);
102 hash_del_rcu(&sa_entry->hlist);
103 spin_unlock_irqrestore(&ipsec->sadb_rx_lock, flags);
106 static void mlx5e_ipsec_sadb_rx_free(struct mlx5e_ipsec_sa_entry *sa_entry)
108 struct mlx5e_ipsec *ipsec = sa_entry->ipsec;
110 /* xfrm already doing sync rcu between del and free callbacks */
112 ida_simple_remove(&ipsec->halloc, sa_entry->handle);
115 static bool mlx5e_ipsec_update_esn_state(struct mlx5e_ipsec_sa_entry *sa_entry)
117 struct xfrm_replay_state_esn *replay_esn;
122 if (!(sa_entry->x->props.flags & XFRM_STATE_ESN)) {
123 sa_entry->esn_state.trigger = 0;
127 replay_esn = sa_entry->x->replay_esn;
128 seq_bottom = replay_esn->seq - replay_esn->replay_window + 1;
129 overlap = sa_entry->esn_state.overlap;
131 sa_entry->esn_state.esn = xfrm_replay_seqhi(sa_entry->x,
133 esn = &sa_entry->esn_state.esn;
135 sa_entry->esn_state.trigger = 1;
136 if (unlikely(overlap && seq_bottom < MLX5E_IPSEC_ESN_SCOPE_MID)) {
138 sa_entry->esn_state.overlap = 0;
140 } else if (unlikely(!overlap &&
141 (seq_bottom >= MLX5E_IPSEC_ESN_SCOPE_MID))) {
142 sa_entry->esn_state.overlap = 1;
150 mlx5e_ipsec_build_accel_xfrm_attrs(struct mlx5e_ipsec_sa_entry *sa_entry,
151 struct mlx5_accel_esp_xfrm_attrs *attrs)
153 struct xfrm_state *x = sa_entry->x;
154 struct aes_gcm_keymat *aes_gcm = &attrs->keymat.aes_gcm;
155 struct aead_geniv_ctx *geniv_ctx;
156 struct crypto_aead *aead;
157 unsigned int crypto_data_len, key_len;
160 memset(attrs, 0, sizeof(*attrs));
163 crypto_data_len = (x->aead->alg_key_len + 7) / 8;
164 key_len = crypto_data_len - 4; /* 4 bytes salt at end */
166 memcpy(aes_gcm->aes_key, x->aead->alg_key, key_len);
167 aes_gcm->key_len = key_len * 8;
169 /* salt and seq_iv */
171 geniv_ctx = crypto_aead_ctx(aead);
172 ivsize = crypto_aead_ivsize(aead);
173 memcpy(&aes_gcm->seq_iv, &geniv_ctx->salt, ivsize);
174 memcpy(&aes_gcm->salt, x->aead->alg_key + key_len,
175 sizeof(aes_gcm->salt));
178 aes_gcm->icv_len = x->aead->alg_icv_len;
181 if (sa_entry->esn_state.trigger) {
182 attrs->flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
183 attrs->esn = sa_entry->esn_state.esn;
184 if (sa_entry->esn_state.overlap)
185 attrs->flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
189 attrs->sa_handle = sa_entry->handle;
192 attrs->keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
195 attrs->action = (!(x->xso.flags & XFRM_OFFLOAD_INBOUND)) ?
196 MLX5_ACCEL_ESP_ACTION_ENCRYPT :
197 MLX5_ACCEL_ESP_ACTION_DECRYPT;
199 attrs->flags |= (x->props.mode == XFRM_MODE_TRANSPORT) ?
200 MLX5_ACCEL_ESP_FLAGS_TRANSPORT :
201 MLX5_ACCEL_ESP_FLAGS_TUNNEL;
204 static inline int mlx5e_xfrm_validate_state(struct xfrm_state *x)
206 struct net_device *netdev = x->xso.dev;
207 struct mlx5e_priv *priv;
209 priv = netdev_priv(netdev);
211 if (x->props.aalgo != SADB_AALG_NONE) {
212 netdev_info(netdev, "Cannot offload authenticated xfrm states\n");
215 if (x->props.ealgo != SADB_X_EALG_AES_GCM_ICV16) {
216 netdev_info(netdev, "Only AES-GCM-ICV16 xfrm state may be offloaded\n");
219 if (x->props.calgo != SADB_X_CALG_NONE) {
220 netdev_info(netdev, "Cannot offload compressed xfrm states\n");
223 if (x->props.flags & XFRM_STATE_ESN &&
224 !(mlx5_accel_ipsec_device_caps(priv->mdev) &
225 MLX5_ACCEL_IPSEC_CAP_ESN)) {
226 netdev_info(netdev, "Cannot offload ESN xfrm states\n");
229 if (x->props.family != AF_INET &&
230 x->props.family != AF_INET6) {
231 netdev_info(netdev, "Only IPv4/6 xfrm states may be offloaded\n");
234 if (x->props.mode != XFRM_MODE_TRANSPORT &&
235 x->props.mode != XFRM_MODE_TUNNEL) {
236 dev_info(&netdev->dev, "Only transport and tunnel xfrm states may be offloaded\n");
239 if (x->id.proto != IPPROTO_ESP) {
240 netdev_info(netdev, "Only ESP xfrm state may be offloaded\n");
244 netdev_info(netdev, "Encapsulated xfrm state may not be offloaded\n");
248 netdev_info(netdev, "Cannot offload xfrm states without aead\n");
251 if (x->aead->alg_icv_len != 128) {
252 netdev_info(netdev, "Cannot offload xfrm states with AEAD ICV length other than 128bit\n");
255 if ((x->aead->alg_key_len != 128 + 32) &&
256 (x->aead->alg_key_len != 256 + 32)) {
257 netdev_info(netdev, "Cannot offload xfrm states with AEAD key length other than 128/256 bit\n");
261 netdev_info(netdev, "Cannot offload xfrm states with tfc padding\n");
265 netdev_info(netdev, "Cannot offload xfrm states without geniv\n");
268 if (strcmp(x->geniv, "seqiv")) {
269 netdev_info(netdev, "Cannot offload xfrm states with geniv other than seqiv\n");
272 if (x->props.family == AF_INET6 &&
273 !(mlx5_accel_ipsec_device_caps(priv->mdev) &
274 MLX5_ACCEL_IPSEC_CAP_IPV6)) {
275 netdev_info(netdev, "IPv6 xfrm state offload is not supported by this device\n");
281 static int mlx5e_xfrm_add_state(struct xfrm_state *x)
283 struct mlx5e_ipsec_sa_entry *sa_entry = NULL;
284 struct net_device *netdev = x->xso.dev;
285 struct mlx5_accel_esp_xfrm_attrs attrs;
286 struct mlx5e_priv *priv;
287 __be32 saddr[4] = {0}, daddr[4] = {0}, spi;
288 bool is_ipv6 = false;
291 priv = netdev_priv(netdev);
293 err = mlx5e_xfrm_validate_state(x);
297 sa_entry = kzalloc(sizeof(*sa_entry), GFP_KERNEL);
304 sa_entry->ipsec = priv->ipsec;
306 /* Add the SA to handle processed incoming packets before the add SA
307 * completion was received
309 if (x->xso.flags & XFRM_OFFLOAD_INBOUND) {
310 err = mlx5e_ipsec_sadb_rx_add(sa_entry);
312 netdev_info(netdev, "Failed adding to SADB_RX: %d\n", err);
316 sa_entry->set_iv_op = (x->props.flags & XFRM_STATE_ESN) ?
317 mlx5e_ipsec_set_iv_esn : mlx5e_ipsec_set_iv;
321 mlx5e_ipsec_update_esn_state(sa_entry);
324 mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &attrs);
326 mlx5_accel_esp_create_xfrm(priv->mdev, &attrs,
327 MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA);
328 if (IS_ERR(sa_entry->xfrm)) {
329 err = PTR_ERR(sa_entry->xfrm);
333 /* create hw context */
334 if (x->props.family == AF_INET) {
335 saddr[3] = x->props.saddr.a4;
336 daddr[3] = x->id.daddr.a4;
338 memcpy(saddr, x->props.saddr.a6, sizeof(saddr));
339 memcpy(daddr, x->id.daddr.a6, sizeof(daddr));
343 sa_entry->hw_context =
344 mlx5_accel_esp_create_hw_context(priv->mdev,
348 if (IS_ERR(sa_entry->hw_context)) {
349 err = PTR_ERR(sa_entry->hw_context);
353 x->xso.offload_handle = (unsigned long)sa_entry;
357 mlx5_accel_esp_destroy_xfrm(sa_entry->xfrm);
359 if (x->xso.flags & XFRM_OFFLOAD_INBOUND) {
360 mlx5e_ipsec_sadb_rx_del(sa_entry);
361 mlx5e_ipsec_sadb_rx_free(sa_entry);
369 static void mlx5e_xfrm_del_state(struct xfrm_state *x)
371 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
376 if (x->xso.flags & XFRM_OFFLOAD_INBOUND)
377 mlx5e_ipsec_sadb_rx_del(sa_entry);
380 static void mlx5e_xfrm_free_state(struct xfrm_state *x)
382 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
387 if (sa_entry->hw_context) {
388 flush_workqueue(sa_entry->ipsec->wq);
389 mlx5_accel_esp_free_hw_context(sa_entry->hw_context);
390 mlx5_accel_esp_destroy_xfrm(sa_entry->xfrm);
393 if (x->xso.flags & XFRM_OFFLOAD_INBOUND)
394 mlx5e_ipsec_sadb_rx_free(sa_entry);
399 int mlx5e_ipsec_init(struct mlx5e_priv *priv)
401 struct mlx5e_ipsec *ipsec = NULL;
403 if (!MLX5_IPSEC_DEV(priv->mdev)) {
404 netdev_dbg(priv->netdev, "Not an IPSec offload device\n");
408 ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
412 hash_init(ipsec->sadb_rx);
413 spin_lock_init(&ipsec->sadb_rx_lock);
414 ida_init(&ipsec->halloc);
415 ipsec->en_priv = priv;
416 ipsec->en_priv->ipsec = ipsec;
417 ipsec->no_trailer = !!(mlx5_accel_ipsec_device_caps(priv->mdev) &
418 MLX5_ACCEL_IPSEC_CAP_RX_NO_TRAILER);
419 ipsec->wq = alloc_ordered_workqueue("mlx5e_ipsec: %s", 0,
425 netdev_dbg(priv->netdev, "IPSec attached to netdevice\n");
429 void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
431 struct mlx5e_ipsec *ipsec = priv->ipsec;
436 drain_workqueue(ipsec->wq);
437 destroy_workqueue(ipsec->wq);
439 ida_destroy(&ipsec->halloc);
444 static bool mlx5e_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
446 if (x->props.family == AF_INET) {
447 /* Offload with IPv4 options is not supported yet */
448 if (ip_hdr(skb)->ihl > 5)
451 /* Offload with IPv6 extension headers is not support yet */
452 if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
459 struct mlx5e_ipsec_modify_state_work {
460 struct work_struct work;
461 struct mlx5_accel_esp_xfrm_attrs attrs;
462 struct mlx5e_ipsec_sa_entry *sa_entry;
465 static void _update_xfrm_state(struct work_struct *work)
468 struct mlx5e_ipsec_modify_state_work *modify_work =
469 container_of(work, struct mlx5e_ipsec_modify_state_work, work);
470 struct mlx5e_ipsec_sa_entry *sa_entry = modify_work->sa_entry;
472 ret = mlx5_accel_esp_modify_xfrm(sa_entry->xfrm,
473 &modify_work->attrs);
475 netdev_warn(sa_entry->ipsec->en_priv->netdev,
476 "Not an IPSec offload device\n");
481 static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x)
483 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
484 struct mlx5e_ipsec_modify_state_work *modify_work;
490 need_update = mlx5e_ipsec_update_esn_state(sa_entry);
494 modify_work = kzalloc(sizeof(*modify_work), GFP_ATOMIC);
498 mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &modify_work->attrs);
499 modify_work->sa_entry = sa_entry;
501 INIT_WORK(&modify_work->work, _update_xfrm_state);
502 WARN_ON(!queue_work(sa_entry->ipsec->wq, &modify_work->work));
505 static const struct xfrmdev_ops mlx5e_ipsec_xfrmdev_ops = {
506 .xdo_dev_state_add = mlx5e_xfrm_add_state,
507 .xdo_dev_state_delete = mlx5e_xfrm_del_state,
508 .xdo_dev_state_free = mlx5e_xfrm_free_state,
509 .xdo_dev_offload_ok = mlx5e_ipsec_offload_ok,
510 .xdo_dev_state_advance_esn = mlx5e_xfrm_advance_esn_state,
513 void mlx5e_ipsec_build_netdev(struct mlx5e_priv *priv)
515 struct mlx5_core_dev *mdev = priv->mdev;
516 struct net_device *netdev = priv->netdev;
521 if (!(mlx5_accel_ipsec_device_caps(mdev) & MLX5_ACCEL_IPSEC_CAP_ESP) ||
522 !MLX5_CAP_ETH(mdev, swp)) {
523 mlx5_core_dbg(mdev, "mlx5e: ESP and SWP offload not supported\n");
527 mlx5_core_info(mdev, "mlx5e: IPSec ESP acceleration enabled\n");
528 netdev->xfrmdev_ops = &mlx5e_ipsec_xfrmdev_ops;
529 netdev->features |= NETIF_F_HW_ESP;
530 netdev->hw_enc_features |= NETIF_F_HW_ESP;
532 if (!MLX5_CAP_ETH(mdev, swp_csum)) {
533 mlx5_core_dbg(mdev, "mlx5e: SWP checksum not supported\n");
537 netdev->features |= NETIF_F_HW_ESP_TX_CSUM;
538 netdev->hw_enc_features |= NETIF_F_HW_ESP_TX_CSUM;
540 if (!(mlx5_accel_ipsec_device_caps(mdev) & MLX5_ACCEL_IPSEC_CAP_LSO) ||
541 !MLX5_CAP_ETH(mdev, swp_lso)) {
542 mlx5_core_dbg(mdev, "mlx5e: ESP LSO not supported\n");
546 mlx5_core_dbg(mdev, "mlx5e: ESP GSO capability turned on\n");
547 netdev->features |= NETIF_F_GSO_ESP;
548 netdev->hw_features |= NETIF_F_GSO_ESP;
549 netdev->hw_enc_features |= NETIF_F_GSO_ESP;