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/aead.h>
38 #include "en_accel/ipsec_rxtx.h"
39 #include "en_accel/ipsec.h"
40 #include "accel/accel.h"
44 MLX5E_IPSEC_RX_SYNDROME_DECRYPTED = 0x11,
45 MLX5E_IPSEC_RX_SYNDROME_AUTH_FAILED = 0x12,
46 MLX5E_IPSEC_RX_SYNDROME_BAD_PROTO = 0x17,
49 struct mlx5e_ipsec_rx_metadata {
50 unsigned char nexthdr;
55 MLX5E_IPSEC_TX_SYNDROME_OFFLOAD = 0x8,
56 MLX5E_IPSEC_TX_SYNDROME_OFFLOAD_WITH_LSO_TCP = 0x9,
59 struct mlx5e_ipsec_tx_metadata {
60 __be16 mss_inv; /* 1/MSS in 16bit fixed point, only for LSO */
61 __be16 seq; /* LSBs of the first TCP seq, only for LSO */
62 u8 esp_next_proto; /* Next protocol of ESP */
65 struct mlx5e_ipsec_metadata {
66 unsigned char syndrome;
69 /* from FPGA to host, on successful decrypt */
70 struct mlx5e_ipsec_rx_metadata rx;
71 /* from host to FPGA */
72 struct mlx5e_ipsec_tx_metadata tx;
74 /* packet type ID field */
78 #define MAX_LSO_MSS 2048
80 /* Pre-calculated (Q0.16) fixed-point inverse 1/x function */
81 static __be16 mlx5e_ipsec_inverse_table[MAX_LSO_MSS];
83 static inline __be16 mlx5e_ipsec_mss_inv(struct sk_buff *skb)
85 return mlx5e_ipsec_inverse_table[skb_shinfo(skb)->gso_size];
88 static struct mlx5e_ipsec_metadata *mlx5e_ipsec_add_metadata(struct sk_buff *skb)
90 struct mlx5e_ipsec_metadata *mdata;
93 if (unlikely(skb_cow_head(skb, sizeof(*mdata))))
94 return ERR_PTR(-ENOMEM);
96 eth = (struct ethhdr *)skb_push(skb, sizeof(*mdata));
97 skb->mac_header -= sizeof(*mdata);
98 mdata = (struct mlx5e_ipsec_metadata *)(eth + 1);
100 memmove(skb->data, skb->data + sizeof(*mdata),
103 eth->h_proto = cpu_to_be16(MLX5E_METADATA_ETHER_TYPE);
105 memset(mdata->content.raw, 0, sizeof(mdata->content.raw));
109 static int mlx5e_ipsec_remove_trailer(struct sk_buff *skb, struct xfrm_state *x)
111 unsigned int alen = crypto_aead_authsize(x->data);
112 struct ipv6hdr *ipv6hdr = ipv6_hdr(skb);
113 struct iphdr *ipv4hdr = ip_hdr(skb);
114 unsigned int trailer_len;
118 ret = skb_copy_bits(skb, skb->len - alen - 2, &plen, 1);
122 trailer_len = alen + plen + 2;
124 pskb_trim(skb, skb->len - trailer_len);
125 if (skb->protocol == htons(ETH_P_IP)) {
126 ipv4hdr->tot_len = htons(ntohs(ipv4hdr->tot_len) - trailer_len);
127 ip_send_check(ipv4hdr);
129 ipv6hdr->payload_len = htons(ntohs(ipv6hdr->payload_len) -
135 static void mlx5e_ipsec_set_swp(struct sk_buff *skb,
136 struct mlx5_wqe_eth_seg *eseg, u8 mode,
137 struct xfrm_offload *xo)
139 struct mlx5e_swp_spec swp_spec = {};
142 * SWP: OutL3 InL3 InL4
143 * Pkt: MAC IP ESP IP L4
150 swp_spec.l3_proto = skb->protocol;
151 swp_spec.is_tun = mode == XFRM_MODE_TUNNEL;
152 if (swp_spec.is_tun) {
153 if (xo->proto == IPPROTO_IPV6) {
154 swp_spec.tun_l3_proto = htons(ETH_P_IPV6);
155 swp_spec.tun_l4_proto = inner_ipv6_hdr(skb)->nexthdr;
157 swp_spec.tun_l3_proto = htons(ETH_P_IP);
158 swp_spec.tun_l4_proto = inner_ip_hdr(skb)->protocol;
161 swp_spec.tun_l3_proto = skb->protocol;
162 swp_spec.tun_l4_proto = xo->proto;
165 mlx5e_set_eseg_swp(skb, eseg, &swp_spec);
168 void mlx5e_ipsec_set_iv_esn(struct sk_buff *skb, struct xfrm_state *x,
169 struct xfrm_offload *xo)
171 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
172 __u32 oseq = replay_esn->oseq;
177 if (unlikely(skb_is_gso(skb) && oseq < MLX5E_IPSEC_ESN_SCOPE_MID &&
178 MLX5E_IPSEC_ESN_SCOPE_MID < (oseq - skb_shinfo(skb)->gso_segs))) {
179 seq_hi = xo->seq.hi - 1;
184 /* Place the SN in the IV field */
185 seqno = cpu_to_be64(xo->seq.low + ((u64)seq_hi << 32));
186 iv_offset = skb_transport_offset(skb) + sizeof(struct ip_esp_hdr);
187 skb_store_bits(skb, iv_offset, &seqno, 8);
190 void mlx5e_ipsec_set_iv(struct sk_buff *skb, struct xfrm_state *x,
191 struct xfrm_offload *xo)
196 /* Place the SN in the IV field */
197 seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
198 iv_offset = skb_transport_offset(skb) + sizeof(struct ip_esp_hdr);
199 skb_store_bits(skb, iv_offset, &seqno, 8);
202 static void mlx5e_ipsec_set_metadata(struct sk_buff *skb,
203 struct mlx5e_ipsec_metadata *mdata,
204 struct xfrm_offload *xo)
206 struct ip_esp_hdr *esph;
209 if (skb_is_gso(skb)) {
210 /* Add LSO metadata indication */
211 esph = ip_esp_hdr(skb);
212 tcph = inner_tcp_hdr(skb);
213 netdev_dbg(skb->dev, " Offloading GSO packet outer L3 %u; L4 %u; Inner L3 %u; L4 %u\n",
215 skb->transport_header,
216 skb->inner_network_header,
217 skb->inner_transport_header);
218 netdev_dbg(skb->dev, " Offloading GSO packet of len %u; mss %u; TCP sp %u dp %u seq 0x%x ESP seq 0x%x\n",
219 skb->len, skb_shinfo(skb)->gso_size,
220 ntohs(tcph->source), ntohs(tcph->dest),
221 ntohl(tcph->seq), ntohl(esph->seq_no));
222 mdata->syndrome = MLX5E_IPSEC_TX_SYNDROME_OFFLOAD_WITH_LSO_TCP;
223 mdata->content.tx.mss_inv = mlx5e_ipsec_mss_inv(skb);
224 mdata->content.tx.seq = htons(ntohl(tcph->seq) & 0xFFFF);
226 mdata->syndrome = MLX5E_IPSEC_TX_SYNDROME_OFFLOAD;
228 mdata->content.tx.esp_next_proto = xo->proto;
230 netdev_dbg(skb->dev, " TX metadata syndrome %u proto %u mss_inv %04x seq %04x\n",
231 mdata->syndrome, mdata->content.tx.esp_next_proto,
232 ntohs(mdata->content.tx.mss_inv),
233 ntohs(mdata->content.tx.seq));
236 bool mlx5e_ipsec_handle_tx_skb(struct mlx5e_priv *priv,
237 struct mlx5_wqe_eth_seg *eseg,
240 struct xfrm_offload *xo = xfrm_offload(skb);
241 struct mlx5e_ipsec_metadata *mdata;
242 struct mlx5e_ipsec_sa_entry *sa_entry;
243 struct xfrm_state *x;
249 sp = skb_sec_path(skb);
250 if (unlikely(sp->len != 1)) {
251 atomic64_inc(&priv->ipsec->sw_stats.ipsec_tx_drop_bundle);
255 x = xfrm_input_state(skb);
257 atomic64_inc(&priv->ipsec->sw_stats.ipsec_tx_drop_no_state);
261 if (unlikely(!x->xso.offload_handle ||
262 (skb->protocol != htons(ETH_P_IP) &&
263 skb->protocol != htons(ETH_P_IPV6)))) {
264 atomic64_inc(&priv->ipsec->sw_stats.ipsec_tx_drop_not_ip);
268 if (!skb_is_gso(skb))
269 if (unlikely(mlx5e_ipsec_remove_trailer(skb, x))) {
270 atomic64_inc(&priv->ipsec->sw_stats.ipsec_tx_drop_trailer);
273 mdata = mlx5e_ipsec_add_metadata(skb);
275 atomic64_inc(&priv->ipsec->sw_stats.ipsec_tx_drop_metadata);
278 mlx5e_ipsec_set_swp(skb, eseg, x->props.mode, xo);
279 sa_entry = (struct mlx5e_ipsec_sa_entry *)x->xso.offload_handle;
280 sa_entry->set_iv_op(skb, x, xo);
281 mlx5e_ipsec_set_metadata(skb, mdata, xo);
290 static inline struct xfrm_state *
291 mlx5e_ipsec_build_sp(struct net_device *netdev, struct sk_buff *skb,
292 struct mlx5e_ipsec_metadata *mdata)
294 struct mlx5e_priv *priv = netdev_priv(netdev);
295 struct xfrm_offload *xo;
296 struct xfrm_state *xs;
300 sp = secpath_set(skb);
302 atomic64_inc(&priv->ipsec->sw_stats.ipsec_rx_drop_sp_alloc);
306 sa_handle = be32_to_cpu(mdata->content.rx.sa_handle);
307 xs = mlx5e_ipsec_sadb_rx_lookup(priv->ipsec, sa_handle);
309 atomic64_inc(&priv->ipsec->sw_stats.ipsec_rx_drop_sadb_miss);
313 sp = skb_sec_path(skb);
314 sp->xvec[sp->len++] = xs;
317 xo = xfrm_offload(skb);
318 xo->flags = CRYPTO_DONE;
319 switch (mdata->syndrome) {
320 case MLX5E_IPSEC_RX_SYNDROME_DECRYPTED:
321 xo->status = CRYPTO_SUCCESS;
322 if (likely(priv->ipsec->no_trailer)) {
323 xo->flags |= XFRM_ESP_NO_TRAILER;
324 xo->proto = mdata->content.rx.nexthdr;
327 case MLX5E_IPSEC_RX_SYNDROME_AUTH_FAILED:
328 xo->status = CRYPTO_TUNNEL_ESP_AUTH_FAILED;
330 case MLX5E_IPSEC_RX_SYNDROME_BAD_PROTO:
331 xo->status = CRYPTO_INVALID_PROTOCOL;
334 atomic64_inc(&priv->ipsec->sw_stats.ipsec_rx_drop_syndrome);
340 struct sk_buff *mlx5e_ipsec_handle_rx_skb(struct net_device *netdev,
341 struct sk_buff *skb, u32 *cqe_bcnt)
343 struct mlx5e_ipsec_metadata *mdata;
344 struct xfrm_state *xs;
346 if (!is_metadata_hdr_valid(skb))
349 /* Use the metadata */
350 mdata = (struct mlx5e_ipsec_metadata *)(skb->data + ETH_HLEN);
351 xs = mlx5e_ipsec_build_sp(netdev, skb, mdata);
357 remove_metadata_hdr(skb);
358 *cqe_bcnt -= MLX5E_METADATA_ETHER_LEN;
363 bool mlx5e_ipsec_feature_check(struct sk_buff *skb, struct net_device *netdev,
364 netdev_features_t features)
366 struct sec_path *sp = skb_sec_path(skb);
367 struct xfrm_state *x;
371 if (x && x->xso.offload_handle)
377 void mlx5e_ipsec_build_inverse_table(void)
382 /* Calculate 1/x inverse table for use in GSO data path.
383 * Using this table, we provide the IPSec accelerator with the value of
384 * 1/gso_size so that it can infer the position of each segment inside
385 * the GSO, and increment the ESP sequence number, and generate the IV.
386 * The HW needs this value in Q0.16 fixed-point number format
388 mlx5e_ipsec_inverse_table[1] = htons(0xFFFF);
389 for (mss = 2; mss < MAX_LSO_MSS; mss++) {
390 mss_inv = div_u64(1ULL << 32, mss) >> 16;
391 mlx5e_ipsec_inverse_table[mss] = htons(mss_inv);