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;
121 if (!(sa_entry->x->props.flags & XFRM_STATE_ESN)) {
122 sa_entry->esn_state.trigger = 0;
126 replay_esn = sa_entry->x->replay_esn;
127 seq_bottom = replay_esn->seq - replay_esn->replay_window + 1;
128 overlap = sa_entry->esn_state.overlap;
130 sa_entry->esn_state.esn = xfrm_replay_seqhi(sa_entry->x,
133 sa_entry->esn_state.trigger = 1;
134 if (unlikely(overlap && seq_bottom < MLX5E_IPSEC_ESN_SCOPE_MID)) {
135 sa_entry->esn_state.overlap = 0;
137 } else if (unlikely(!overlap &&
138 (seq_bottom >= MLX5E_IPSEC_ESN_SCOPE_MID))) {
139 sa_entry->esn_state.overlap = 1;
147 mlx5e_ipsec_build_accel_xfrm_attrs(struct mlx5e_ipsec_sa_entry *sa_entry,
148 struct mlx5_accel_esp_xfrm_attrs *attrs)
150 struct xfrm_state *x = sa_entry->x;
151 struct aes_gcm_keymat *aes_gcm = &attrs->keymat.aes_gcm;
152 struct aead_geniv_ctx *geniv_ctx;
153 struct crypto_aead *aead;
154 unsigned int crypto_data_len, key_len;
157 memset(attrs, 0, sizeof(*attrs));
160 crypto_data_len = (x->aead->alg_key_len + 7) / 8;
161 key_len = crypto_data_len - 4; /* 4 bytes salt at end */
163 memcpy(aes_gcm->aes_key, x->aead->alg_key, key_len);
164 aes_gcm->key_len = key_len * 8;
166 /* salt and seq_iv */
168 geniv_ctx = crypto_aead_ctx(aead);
169 ivsize = crypto_aead_ivsize(aead);
170 memcpy(&aes_gcm->seq_iv, &geniv_ctx->salt, ivsize);
171 memcpy(&aes_gcm->salt, x->aead->alg_key + key_len,
172 sizeof(aes_gcm->salt));
175 aes_gcm->icv_len = x->aead->alg_icv_len;
178 if (sa_entry->esn_state.trigger) {
179 attrs->flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
180 attrs->esn = sa_entry->esn_state.esn;
181 if (sa_entry->esn_state.overlap)
182 attrs->flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
186 attrs->sa_handle = sa_entry->handle;
189 attrs->keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
192 attrs->action = (!(x->xso.flags & XFRM_OFFLOAD_INBOUND)) ?
193 MLX5_ACCEL_ESP_ACTION_ENCRYPT :
194 MLX5_ACCEL_ESP_ACTION_DECRYPT;
196 attrs->flags |= (x->props.mode == XFRM_MODE_TRANSPORT) ?
197 MLX5_ACCEL_ESP_FLAGS_TRANSPORT :
198 MLX5_ACCEL_ESP_FLAGS_TUNNEL;
201 static inline int mlx5e_xfrm_validate_state(struct xfrm_state *x)
203 struct net_device *netdev = x->xso.dev;
204 struct mlx5e_priv *priv;
206 priv = netdev_priv(netdev);
208 if (x->props.aalgo != SADB_AALG_NONE) {
209 netdev_info(netdev, "Cannot offload authenticated xfrm states\n");
212 if (x->props.ealgo != SADB_X_EALG_AES_GCM_ICV16) {
213 netdev_info(netdev, "Only AES-GCM-ICV16 xfrm state may be offloaded\n");
216 if (x->props.calgo != SADB_X_CALG_NONE) {
217 netdev_info(netdev, "Cannot offload compressed xfrm states\n");
220 if (x->props.flags & XFRM_STATE_ESN &&
221 !(mlx5_accel_ipsec_device_caps(priv->mdev) &
222 MLX5_ACCEL_IPSEC_CAP_ESN)) {
223 netdev_info(netdev, "Cannot offload ESN xfrm states\n");
226 if (x->props.family != AF_INET &&
227 x->props.family != AF_INET6) {
228 netdev_info(netdev, "Only IPv4/6 xfrm states may be offloaded\n");
231 if (x->props.mode != XFRM_MODE_TRANSPORT &&
232 x->props.mode != XFRM_MODE_TUNNEL) {
233 dev_info(&netdev->dev, "Only transport and tunnel xfrm states may be offloaded\n");
236 if (x->id.proto != IPPROTO_ESP) {
237 netdev_info(netdev, "Only ESP xfrm state may be offloaded\n");
241 netdev_info(netdev, "Encapsulated xfrm state may not be offloaded\n");
245 netdev_info(netdev, "Cannot offload xfrm states without aead\n");
248 if (x->aead->alg_icv_len != 128) {
249 netdev_info(netdev, "Cannot offload xfrm states with AEAD ICV length other than 128bit\n");
252 if ((x->aead->alg_key_len != 128 + 32) &&
253 (x->aead->alg_key_len != 256 + 32)) {
254 netdev_info(netdev, "Cannot offload xfrm states with AEAD key length other than 128/256 bit\n");
258 netdev_info(netdev, "Cannot offload xfrm states with tfc padding\n");
262 netdev_info(netdev, "Cannot offload xfrm states without geniv\n");
265 if (strcmp(x->geniv, "seqiv")) {
266 netdev_info(netdev, "Cannot offload xfrm states with geniv other than seqiv\n");
269 if (x->props.family == AF_INET6 &&
270 !(mlx5_accel_ipsec_device_caps(priv->mdev) &
271 MLX5_ACCEL_IPSEC_CAP_IPV6)) {
272 netdev_info(netdev, "IPv6 xfrm state offload is not supported by this device\n");
278 static int mlx5e_xfrm_add_state(struct xfrm_state *x)
280 struct mlx5e_ipsec_sa_entry *sa_entry = NULL;
281 struct net_device *netdev = x->xso.dev;
282 struct mlx5_accel_esp_xfrm_attrs attrs;
283 struct mlx5e_priv *priv;
284 __be32 saddr[4] = {0}, daddr[4] = {0}, spi;
285 bool is_ipv6 = false;
288 priv = netdev_priv(netdev);
290 err = mlx5e_xfrm_validate_state(x);
294 sa_entry = kzalloc(sizeof(*sa_entry), GFP_KERNEL);
301 sa_entry->ipsec = priv->ipsec;
303 /* Add the SA to handle processed incoming packets before the add SA
304 * completion was received
306 if (x->xso.flags & XFRM_OFFLOAD_INBOUND) {
307 err = mlx5e_ipsec_sadb_rx_add(sa_entry);
309 netdev_info(netdev, "Failed adding to SADB_RX: %d\n", err);
313 sa_entry->set_iv_op = (x->props.flags & XFRM_STATE_ESN) ?
314 mlx5e_ipsec_set_iv_esn : mlx5e_ipsec_set_iv;
318 mlx5e_ipsec_update_esn_state(sa_entry);
321 mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &attrs);
323 mlx5_accel_esp_create_xfrm(priv->mdev, &attrs,
324 MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA);
325 if (IS_ERR(sa_entry->xfrm)) {
326 err = PTR_ERR(sa_entry->xfrm);
330 /* create hw context */
331 if (x->props.family == AF_INET) {
332 saddr[3] = x->props.saddr.a4;
333 daddr[3] = x->id.daddr.a4;
335 memcpy(saddr, x->props.saddr.a6, sizeof(saddr));
336 memcpy(daddr, x->id.daddr.a6, sizeof(daddr));
340 sa_entry->hw_context =
341 mlx5_accel_esp_create_hw_context(priv->mdev,
345 if (IS_ERR(sa_entry->hw_context)) {
346 err = PTR_ERR(sa_entry->hw_context);
350 x->xso.offload_handle = (unsigned long)sa_entry;
354 mlx5_accel_esp_destroy_xfrm(sa_entry->xfrm);
356 if (x->xso.flags & XFRM_OFFLOAD_INBOUND) {
357 mlx5e_ipsec_sadb_rx_del(sa_entry);
358 mlx5e_ipsec_sadb_rx_free(sa_entry);
366 static void mlx5e_xfrm_del_state(struct xfrm_state *x)
368 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
373 if (x->xso.flags & XFRM_OFFLOAD_INBOUND)
374 mlx5e_ipsec_sadb_rx_del(sa_entry);
377 static void mlx5e_xfrm_free_state(struct xfrm_state *x)
379 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
384 if (sa_entry->hw_context) {
385 flush_workqueue(sa_entry->ipsec->wq);
386 mlx5_accel_esp_free_hw_context(sa_entry->hw_context);
387 mlx5_accel_esp_destroy_xfrm(sa_entry->xfrm);
390 if (x->xso.flags & XFRM_OFFLOAD_INBOUND)
391 mlx5e_ipsec_sadb_rx_free(sa_entry);
396 int mlx5e_ipsec_init(struct mlx5e_priv *priv)
398 struct mlx5e_ipsec *ipsec = NULL;
400 if (!MLX5_IPSEC_DEV(priv->mdev)) {
401 netdev_dbg(priv->netdev, "Not an IPSec offload device\n");
405 ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
409 hash_init(ipsec->sadb_rx);
410 spin_lock_init(&ipsec->sadb_rx_lock);
411 ida_init(&ipsec->halloc);
412 ipsec->en_priv = priv;
413 ipsec->en_priv->ipsec = ipsec;
414 ipsec->no_trailer = !!(mlx5_accel_ipsec_device_caps(priv->mdev) &
415 MLX5_ACCEL_IPSEC_CAP_RX_NO_TRAILER);
416 ipsec->wq = alloc_ordered_workqueue("mlx5e_ipsec: %s", 0,
422 netdev_dbg(priv->netdev, "IPSec attached to netdevice\n");
426 void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
428 struct mlx5e_ipsec *ipsec = priv->ipsec;
433 drain_workqueue(ipsec->wq);
434 destroy_workqueue(ipsec->wq);
436 ida_destroy(&ipsec->halloc);
441 static bool mlx5e_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
443 if (x->props.family == AF_INET) {
444 /* Offload with IPv4 options is not supported yet */
445 if (ip_hdr(skb)->ihl > 5)
448 /* Offload with IPv6 extension headers is not support yet */
449 if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
456 struct mlx5e_ipsec_modify_state_work {
457 struct work_struct work;
458 struct mlx5_accel_esp_xfrm_attrs attrs;
459 struct mlx5e_ipsec_sa_entry *sa_entry;
462 static void _update_xfrm_state(struct work_struct *work)
465 struct mlx5e_ipsec_modify_state_work *modify_work =
466 container_of(work, struct mlx5e_ipsec_modify_state_work, work);
467 struct mlx5e_ipsec_sa_entry *sa_entry = modify_work->sa_entry;
469 ret = mlx5_accel_esp_modify_xfrm(sa_entry->xfrm,
470 &modify_work->attrs);
472 netdev_warn(sa_entry->ipsec->en_priv->netdev,
473 "Not an IPSec offload device\n");
478 static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x)
480 struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
481 struct mlx5e_ipsec_modify_state_work *modify_work;
487 need_update = mlx5e_ipsec_update_esn_state(sa_entry);
491 modify_work = kzalloc(sizeof(*modify_work), GFP_ATOMIC);
495 mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &modify_work->attrs);
496 modify_work->sa_entry = sa_entry;
498 INIT_WORK(&modify_work->work, _update_xfrm_state);
499 WARN_ON(!queue_work(sa_entry->ipsec->wq, &modify_work->work));
502 static const struct xfrmdev_ops mlx5e_ipsec_xfrmdev_ops = {
503 .xdo_dev_state_add = mlx5e_xfrm_add_state,
504 .xdo_dev_state_delete = mlx5e_xfrm_del_state,
505 .xdo_dev_state_free = mlx5e_xfrm_free_state,
506 .xdo_dev_offload_ok = mlx5e_ipsec_offload_ok,
507 .xdo_dev_state_advance_esn = mlx5e_xfrm_advance_esn_state,
510 void mlx5e_ipsec_build_netdev(struct mlx5e_priv *priv)
512 struct mlx5_core_dev *mdev = priv->mdev;
513 struct net_device *netdev = priv->netdev;
515 if (!(mlx5_accel_ipsec_device_caps(mdev) & MLX5_ACCEL_IPSEC_CAP_ESP) ||
516 !MLX5_CAP_ETH(mdev, swp)) {
517 mlx5_core_dbg(mdev, "mlx5e: ESP and SWP offload not supported\n");
521 mlx5_core_info(mdev, "mlx5e: IPSec ESP acceleration enabled\n");
522 netdev->xfrmdev_ops = &mlx5e_ipsec_xfrmdev_ops;
523 netdev->features |= NETIF_F_HW_ESP;
524 netdev->hw_enc_features |= NETIF_F_HW_ESP;
526 if (!MLX5_CAP_ETH(mdev, swp_csum)) {
527 mlx5_core_dbg(mdev, "mlx5e: SWP checksum not supported\n");
531 netdev->features |= NETIF_F_HW_ESP_TX_CSUM;
532 netdev->hw_enc_features |= NETIF_F_HW_ESP_TX_CSUM;
534 if (!(mlx5_accel_ipsec_device_caps(mdev) & MLX5_ACCEL_IPSEC_CAP_LSO) ||
535 !MLX5_CAP_ETH(mdev, swp_lso)) {
536 mlx5_core_dbg(mdev, "mlx5e: ESP LSO not supported\n");
540 mlx5_core_dbg(mdev, "mlx5e: ESP GSO capability turned on\n");
541 netdev->features |= NETIF_F_GSO_ESP;
542 netdev->hw_features |= NETIF_F_GSO_ESP;
543 netdev->hw_enc_features |= NETIF_F_GSO_ESP;