|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Mellanox Technologies. All rights reserved. |
| 3 | + * |
| 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: |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or |
| 11 | + * without modification, are permitted provided that the following |
| 12 | + * conditions are met: |
| 13 | + * |
| 14 | + * - Redistributions of source code must retain the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer. |
| 17 | + * |
| 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. |
| 22 | + * |
| 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 |
| 30 | + * SOFTWARE. |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +#include <linux/netdevice.h> |
| 35 | +#include <net/ipv6.h> |
| 36 | +#include "en_accel/tls.h" |
| 37 | +#include "accel/tls.h" |
| 38 | + |
| 39 | +static void mlx5e_tls_set_ipv4_flow(void *flow, struct sock *sk) |
| 40 | +{ |
| 41 | + struct inet_sock *inet = inet_sk(sk); |
| 42 | + |
| 43 | + MLX5_SET(tls_flow, flow, ipv6, 0); |
| 44 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, dst_ipv4_dst_ipv6.ipv4_layout.ipv4), |
| 45 | + &inet->inet_daddr, MLX5_FLD_SZ_BYTES(ipv4_layout, ipv4)); |
| 46 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, src_ipv4_src_ipv6.ipv4_layout.ipv4), |
| 47 | + &inet->inet_rcv_saddr, MLX5_FLD_SZ_BYTES(ipv4_layout, ipv4)); |
| 48 | +} |
| 49 | + |
| 50 | +#if IS_ENABLED(CONFIG_IPV6) |
| 51 | +static void mlx5e_tls_set_ipv6_flow(void *flow, struct sock *sk) |
| 52 | +{ |
| 53 | + struct ipv6_pinfo *np = inet6_sk(sk); |
| 54 | + |
| 55 | + MLX5_SET(tls_flow, flow, ipv6, 1); |
| 56 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, dst_ipv4_dst_ipv6.ipv6_layout.ipv6), |
| 57 | + &sk->sk_v6_daddr, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6)); |
| 58 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, src_ipv4_src_ipv6.ipv6_layout.ipv6), |
| 59 | + &np->saddr, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6)); |
| 60 | +} |
| 61 | +#endif |
| 62 | + |
| 63 | +static void mlx5e_tls_set_flow_tcp_ports(void *flow, struct sock *sk) |
| 64 | +{ |
| 65 | + struct inet_sock *inet = inet_sk(sk); |
| 66 | + |
| 67 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, src_port), &inet->inet_sport, |
| 68 | + MLX5_FLD_SZ_BYTES(tls_flow, src_port)); |
| 69 | + memcpy(MLX5_ADDR_OF(tls_flow, flow, dst_port), &inet->inet_dport, |
| 70 | + MLX5_FLD_SZ_BYTES(tls_flow, dst_port)); |
| 71 | +} |
| 72 | + |
| 73 | +static int mlx5e_tls_set_flow(void *flow, struct sock *sk, u32 caps) |
| 74 | +{ |
| 75 | + switch (sk->sk_family) { |
| 76 | + case AF_INET: |
| 77 | + mlx5e_tls_set_ipv4_flow(flow, sk); |
| 78 | + break; |
| 79 | +#if IS_ENABLED(CONFIG_IPV6) |
| 80 | + case AF_INET6: |
| 81 | + if (!sk->sk_ipv6only && |
| 82 | + ipv6_addr_type(&sk->sk_v6_daddr) == IPV6_ADDR_MAPPED) { |
| 83 | + mlx5e_tls_set_ipv4_flow(flow, sk); |
| 84 | + break; |
| 85 | + } |
| 86 | + if (!(caps & MLX5_ACCEL_TLS_IPV6)) |
| 87 | + goto error_out; |
| 88 | + |
| 89 | + mlx5e_tls_set_ipv6_flow(flow, sk); |
| 90 | + break; |
| 91 | +#endif |
| 92 | + default: |
| 93 | + goto error_out; |
| 94 | + } |
| 95 | + |
| 96 | + mlx5e_tls_set_flow_tcp_ports(flow, sk); |
| 97 | + return 0; |
| 98 | +error_out: |
| 99 | + return -EINVAL; |
| 100 | +} |
| 101 | + |
| 102 | +static int mlx5e_tls_add(struct net_device *netdev, struct sock *sk, |
| 103 | + enum tls_offload_ctx_dir direction, |
| 104 | + struct tls_crypto_info *crypto_info, |
| 105 | + u32 start_offload_tcp_sn) |
| 106 | +{ |
| 107 | + struct mlx5e_priv *priv = netdev_priv(netdev); |
| 108 | + struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 109 | + struct mlx5_core_dev *mdev = priv->mdev; |
| 110 | + u32 caps = mlx5_accel_tls_device_caps(mdev); |
| 111 | + int ret = -ENOMEM; |
| 112 | + void *flow; |
| 113 | + |
| 114 | + if (direction != TLS_OFFLOAD_CTX_DIR_TX) |
| 115 | + return -EINVAL; |
| 116 | + |
| 117 | + flow = kzalloc(MLX5_ST_SZ_BYTES(tls_flow), GFP_KERNEL); |
| 118 | + if (!flow) |
| 119 | + return ret; |
| 120 | + |
| 121 | + ret = mlx5e_tls_set_flow(flow, sk, caps); |
| 122 | + if (ret) |
| 123 | + goto free_flow; |
| 124 | + |
| 125 | + if (direction == TLS_OFFLOAD_CTX_DIR_TX) { |
| 126 | + struct mlx5e_tls_offload_context *tx_ctx = |
| 127 | + mlx5e_get_tls_tx_context(tls_ctx); |
| 128 | + u32 swid; |
| 129 | + |
| 130 | + ret = mlx5_accel_tls_add_tx_flow(mdev, flow, crypto_info, |
| 131 | + start_offload_tcp_sn, &swid); |
| 132 | + if (ret < 0) |
| 133 | + goto free_flow; |
| 134 | + |
| 135 | + tx_ctx->swid = htonl(swid); |
| 136 | + tx_ctx->expected_seq = start_offload_tcp_sn; |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +free_flow: |
| 141 | + kfree(flow); |
| 142 | + return ret; |
| 143 | +} |
| 144 | + |
| 145 | +static void mlx5e_tls_del(struct net_device *netdev, |
| 146 | + struct tls_context *tls_ctx, |
| 147 | + enum tls_offload_ctx_dir direction) |
| 148 | +{ |
| 149 | + struct mlx5e_priv *priv = netdev_priv(netdev); |
| 150 | + |
| 151 | + if (direction == TLS_OFFLOAD_CTX_DIR_TX) { |
| 152 | + u32 swid = ntohl(mlx5e_get_tls_tx_context(tls_ctx)->swid); |
| 153 | + |
| 154 | + mlx5_accel_tls_del_tx_flow(priv->mdev, swid); |
| 155 | + } else { |
| 156 | + netdev_err(netdev, "unsupported direction %d\n", direction); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +static const struct tlsdev_ops mlx5e_tls_ops = { |
| 161 | + .tls_dev_add = mlx5e_tls_add, |
| 162 | + .tls_dev_del = mlx5e_tls_del, |
| 163 | +}; |
| 164 | + |
| 165 | +void mlx5e_tls_build_netdev(struct mlx5e_priv *priv) |
| 166 | +{ |
| 167 | + struct net_device *netdev = priv->netdev; |
| 168 | + |
| 169 | + if (!mlx5_accel_is_tls_device(priv->mdev)) |
| 170 | + return; |
| 171 | + |
| 172 | + netdev->tlsdev_ops = &mlx5e_tls_ops; |
| 173 | +} |
0 commit comments