1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Oracle and/or its affiliates. All rights reserved. */
3
4 #include "ixgbevf.h"
5 #include <net/xfrm.h>
6 #include <crypto/aead.h>
7
8 #define IXGBE_IPSEC_KEY_BITS 160
9 static const char aes_gcm_name[] = "rfc4106(gcm(aes))";
10
11 /**
12 * ixgbevf_ipsec_set_pf_sa - ask the PF to set up an SA
13 * @adapter: board private structure
14 * @xs: xfrm info to be sent to the PF
15 *
16 * Returns: positive offload handle from the PF, or negative error code
17 **/
ixgbevf_ipsec_set_pf_sa(struct ixgbevf_adapter * adapter,struct xfrm_state * xs)18 static int ixgbevf_ipsec_set_pf_sa(struct ixgbevf_adapter *adapter,
19 struct xfrm_state *xs)
20 {
21 u32 msgbuf[IXGBE_VFMAILBOX_SIZE] = { 0 };
22 struct ixgbe_hw *hw = &adapter->hw;
23 struct sa_mbx_msg *sam;
24 int ret;
25
26 /* send the important bits to the PF */
27 sam = (struct sa_mbx_msg *)(&msgbuf[1]);
28 sam->dir = xs->xso.dir;
29 sam->spi = xs->id.spi;
30 sam->proto = xs->id.proto;
31 sam->family = xs->props.family;
32
33 if (xs->props.family == AF_INET6)
34 memcpy(sam->addr, &xs->id.daddr.a6, sizeof(xs->id.daddr.a6));
35 else
36 memcpy(sam->addr, &xs->id.daddr.a4, sizeof(xs->id.daddr.a4));
37 memcpy(sam->key, xs->aead->alg_key, sizeof(sam->key));
38
39 msgbuf[0] = IXGBE_VF_IPSEC_ADD;
40
41 spin_lock_bh(&adapter->mbx_lock);
42
43 ret = ixgbevf_write_mbx(hw, msgbuf, IXGBE_VFMAILBOX_SIZE);
44 if (ret)
45 goto out;
46
47 ret = ixgbevf_poll_mbx(hw, msgbuf, 2);
48 if (ret)
49 goto out;
50
51 ret = (int)msgbuf[1];
52 if (msgbuf[0] & IXGBE_VT_MSGTYPE_FAILURE && ret >= 0)
53 ret = -1;
54
55 out:
56 spin_unlock_bh(&adapter->mbx_lock);
57
58 return ret;
59 }
60
61 /**
62 * ixgbevf_ipsec_del_pf_sa - ask the PF to delete an SA
63 * @adapter: board private structure
64 * @pfsa: sa index returned from PF when created, -1 for all
65 *
66 * Returns: 0 on success, or negative error code
67 **/
ixgbevf_ipsec_del_pf_sa(struct ixgbevf_adapter * adapter,int pfsa)68 static int ixgbevf_ipsec_del_pf_sa(struct ixgbevf_adapter *adapter, int pfsa)
69 {
70 struct ixgbe_hw *hw = &adapter->hw;
71 u32 msgbuf[2];
72 int err;
73
74 memset(msgbuf, 0, sizeof(msgbuf));
75 msgbuf[0] = IXGBE_VF_IPSEC_DEL;
76 msgbuf[1] = (u32)pfsa;
77
78 spin_lock_bh(&adapter->mbx_lock);
79
80 err = ixgbevf_write_mbx(hw, msgbuf, 2);
81 if (err)
82 goto out;
83
84 err = ixgbevf_poll_mbx(hw, msgbuf, 2);
85 if (err)
86 goto out;
87
88 out:
89 spin_unlock_bh(&adapter->mbx_lock);
90 return err;
91 }
92
93 /**
94 * ixgbevf_ipsec_restore - restore the IPsec HW settings after a reset
95 * @adapter: board private structure
96 *
97 * Reload the HW tables from the SW tables after they've been bashed
98 * by a chip reset. While we're here, make sure any stale VF data is
99 * removed, since we go through reset when num_vfs changes.
100 **/
ixgbevf_ipsec_restore(struct ixgbevf_adapter * adapter)101 void ixgbevf_ipsec_restore(struct ixgbevf_adapter *adapter)
102 {
103 struct ixgbevf_ipsec *ipsec = adapter->ipsec;
104 struct net_device *netdev = adapter->netdev;
105 int i;
106
107 if (!(adapter->netdev->features & NETIF_F_HW_ESP))
108 return;
109
110 /* reload the Rx and Tx keys */
111 for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
112 struct rx_sa *r = &ipsec->rx_tbl[i];
113 struct tx_sa *t = &ipsec->tx_tbl[i];
114 int ret;
115
116 if (r->used) {
117 ret = ixgbevf_ipsec_set_pf_sa(adapter, r->xs);
118 if (ret < 0)
119 netdev_err(netdev, "reload rx_tbl[%d] failed = %d\n",
120 i, ret);
121 }
122
123 if (t->used) {
124 ret = ixgbevf_ipsec_set_pf_sa(adapter, t->xs);
125 if (ret < 0)
126 netdev_err(netdev, "reload tx_tbl[%d] failed = %d\n",
127 i, ret);
128 }
129 }
130 }
131
132 /**
133 * ixgbevf_ipsec_find_empty_idx - find the first unused security parameter index
134 * @ipsec: pointer to IPsec struct
135 * @rxtable: true if we need to look in the Rx table
136 *
137 * Returns the first unused index in either the Rx or Tx SA table
138 **/
139 static
ixgbevf_ipsec_find_empty_idx(struct ixgbevf_ipsec * ipsec,bool rxtable)140 int ixgbevf_ipsec_find_empty_idx(struct ixgbevf_ipsec *ipsec, bool rxtable)
141 {
142 u32 i;
143
144 if (rxtable) {
145 if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
146 return -ENOSPC;
147
148 /* search rx sa table */
149 for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
150 if (!ipsec->rx_tbl[i].used)
151 return i;
152 }
153 } else {
154 if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
155 return -ENOSPC;
156
157 /* search tx sa table */
158 for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
159 if (!ipsec->tx_tbl[i].used)
160 return i;
161 }
162 }
163
164 return -ENOSPC;
165 }
166
167 /**
168 * ixgbevf_ipsec_find_rx_state - find the state that matches
169 * @ipsec: pointer to IPsec struct
170 * @daddr: inbound address to match
171 * @proto: protocol to match
172 * @spi: SPI to match
173 * @ip4: true if using an IPv4 address
174 *
175 * Returns a pointer to the matching SA state information
176 **/
177 static
ixgbevf_ipsec_find_rx_state(struct ixgbevf_ipsec * ipsec,__be32 * daddr,u8 proto,__be32 spi,bool ip4)178 struct xfrm_state *ixgbevf_ipsec_find_rx_state(struct ixgbevf_ipsec *ipsec,
179 __be32 *daddr, u8 proto,
180 __be32 spi, bool ip4)
181 {
182 struct xfrm_state *ret = NULL;
183 struct rx_sa *rsa;
184
185 rcu_read_lock();
186 hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
187 (__force u32)spi) {
188 if (spi == rsa->xs->id.spi &&
189 ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
190 (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
191 sizeof(rsa->xs->id.daddr.a6)))) &&
192 proto == rsa->xs->id.proto) {
193 ret = rsa->xs;
194 xfrm_state_hold(ret);
195 break;
196 }
197 }
198 rcu_read_unlock();
199 return ret;
200 }
201
202 /**
203 * ixgbevf_ipsec_parse_proto_keys - find the key and salt based on the protocol
204 * @dev: pointer to net device to program
205 * @xs: pointer to xfrm_state struct
206 * @mykey: pointer to key array to populate
207 * @mysalt: pointer to salt value to populate
208 *
209 * This copies the protocol keys and salt to our own data tables. The
210 * 82599 family only supports the one algorithm.
211 **/
ixgbevf_ipsec_parse_proto_keys(struct net_device * dev,struct xfrm_state * xs,u32 * mykey,u32 * mysalt)212 static int ixgbevf_ipsec_parse_proto_keys(struct net_device *dev,
213 struct xfrm_state *xs,
214 u32 *mykey, u32 *mysalt)
215 {
216 unsigned char *key_data;
217 char *alg_name = NULL;
218 int key_len;
219
220 if (!xs->aead) {
221 netdev_err(dev, "Unsupported IPsec algorithm\n");
222 return -EINVAL;
223 }
224
225 if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
226 netdev_err(dev, "IPsec offload requires %d bit authentication\n",
227 IXGBE_IPSEC_AUTH_BITS);
228 return -EINVAL;
229 }
230
231 key_data = &xs->aead->alg_key[0];
232 key_len = xs->aead->alg_key_len;
233 alg_name = xs->aead->alg_name;
234
235 if (strcmp(alg_name, aes_gcm_name)) {
236 netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
237 aes_gcm_name);
238 return -EINVAL;
239 }
240
241 /* The key bytes come down in a big endian array of bytes, so
242 * we don't need to do any byte swapping.
243 * 160 accounts for 16 byte key and 4 byte salt
244 */
245 if (key_len > IXGBE_IPSEC_KEY_BITS) {
246 *mysalt = ((u32 *)key_data)[4];
247 } else if (key_len == IXGBE_IPSEC_KEY_BITS) {
248 *mysalt = 0;
249 } else {
250 netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
251 return -EINVAL;
252 }
253 memcpy(mykey, key_data, 16);
254
255 return 0;
256 }
257
258 /**
259 * ixgbevf_ipsec_add_sa - program device with a security association
260 * @dev: pointer to net device to program
261 * @xs: pointer to transformer state struct
262 * @extack: extack point to fill failure reason
263 **/
ixgbevf_ipsec_add_sa(struct net_device * dev,struct xfrm_state * xs,struct netlink_ext_ack * extack)264 static int ixgbevf_ipsec_add_sa(struct net_device *dev,
265 struct xfrm_state *xs,
266 struct netlink_ext_ack *extack)
267 {
268 struct ixgbevf_adapter *adapter;
269 struct ixgbevf_ipsec *ipsec;
270 u16 sa_idx;
271 int ret;
272
273 adapter = netdev_priv(dev);
274 ipsec = adapter->ipsec;
275
276 if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
277 NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol for IPsec offload");
278 return -EINVAL;
279 }
280
281 if (xs->props.mode != XFRM_MODE_TRANSPORT) {
282 NL_SET_ERR_MSG_MOD(extack, "Unsupported mode for ipsec offload");
283 return -EINVAL;
284 }
285
286 if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
287 NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
288 return -EINVAL;
289 }
290
291 if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
292 struct rx_sa rsa;
293
294 if (xs->calg) {
295 NL_SET_ERR_MSG_MOD(extack, "Compression offload not supported");
296 return -EINVAL;
297 }
298
299 /* find the first unused index */
300 ret = ixgbevf_ipsec_find_empty_idx(ipsec, true);
301 if (ret < 0) {
302 NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx table!");
303 return ret;
304 }
305 sa_idx = (u16)ret;
306
307 memset(&rsa, 0, sizeof(rsa));
308 rsa.used = true;
309 rsa.xs = xs;
310
311 if (rsa.xs->id.proto & IPPROTO_ESP)
312 rsa.decrypt = xs->ealg || xs->aead;
313
314 /* get the key and salt */
315 ret = ixgbevf_ipsec_parse_proto_keys(dev, xs, rsa.key,
316 &rsa.salt);
317 if (ret) {
318 NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Rx SA table");
319 return ret;
320 }
321
322 /* get ip for rx sa table */
323 if (xs->props.family == AF_INET6)
324 memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
325 else
326 memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
327
328 rsa.mode = IXGBE_RXMOD_VALID;
329 if (rsa.xs->id.proto & IPPROTO_ESP)
330 rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
331 if (rsa.decrypt)
332 rsa.mode |= IXGBE_RXMOD_DECRYPT;
333 if (rsa.xs->props.family == AF_INET6)
334 rsa.mode |= IXGBE_RXMOD_IPV6;
335
336 ret = ixgbevf_ipsec_set_pf_sa(adapter, xs);
337 if (ret < 0)
338 return ret;
339 rsa.pfsa = ret;
340
341 /* the preparations worked, so save the info */
342 memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
343
344 xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
345
346 ipsec->num_rx_sa++;
347
348 /* hash the new entry for faster search in Rx path */
349 hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
350 (__force u32)rsa.xs->id.spi);
351 } else {
352 struct tx_sa tsa;
353
354 /* find the first unused index */
355 ret = ixgbevf_ipsec_find_empty_idx(ipsec, false);
356 if (ret < 0) {
357 NL_SET_ERR_MSG_MOD(extack, "No space for SA in Tx table");
358 return ret;
359 }
360 sa_idx = (u16)ret;
361
362 memset(&tsa, 0, sizeof(tsa));
363 tsa.used = true;
364 tsa.xs = xs;
365
366 if (xs->id.proto & IPPROTO_ESP)
367 tsa.encrypt = xs->ealg || xs->aead;
368
369 ret = ixgbevf_ipsec_parse_proto_keys(dev, xs, tsa.key,
370 &tsa.salt);
371 if (ret) {
372 NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Tx SA table");
373 memset(&tsa, 0, sizeof(tsa));
374 return ret;
375 }
376
377 ret = ixgbevf_ipsec_set_pf_sa(adapter, xs);
378 if (ret < 0)
379 return ret;
380 tsa.pfsa = ret;
381
382 /* the preparations worked, so save the info */
383 memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
384
385 xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
386
387 ipsec->num_tx_sa++;
388 }
389
390 return 0;
391 }
392
393 /**
394 * ixgbevf_ipsec_del_sa - clear out this specific SA
395 * @dev: pointer to net device to program
396 * @xs: pointer to transformer state struct
397 **/
ixgbevf_ipsec_del_sa(struct net_device * dev,struct xfrm_state * xs)398 static void ixgbevf_ipsec_del_sa(struct net_device *dev,
399 struct xfrm_state *xs)
400 {
401 struct ixgbevf_adapter *adapter;
402 struct ixgbevf_ipsec *ipsec;
403 u16 sa_idx;
404
405 adapter = netdev_priv(dev);
406 ipsec = adapter->ipsec;
407
408 if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
409 sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
410
411 if (!ipsec->rx_tbl[sa_idx].used) {
412 netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
413 sa_idx, xs->xso.offload_handle);
414 return;
415 }
416
417 ixgbevf_ipsec_del_pf_sa(adapter, ipsec->rx_tbl[sa_idx].pfsa);
418 hash_del_rcu(&ipsec->rx_tbl[sa_idx].hlist);
419 memset(&ipsec->rx_tbl[sa_idx], 0, sizeof(struct rx_sa));
420 ipsec->num_rx_sa--;
421 } else {
422 sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
423
424 if (!ipsec->tx_tbl[sa_idx].used) {
425 netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
426 sa_idx, xs->xso.offload_handle);
427 return;
428 }
429
430 ixgbevf_ipsec_del_pf_sa(adapter, ipsec->tx_tbl[sa_idx].pfsa);
431 memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
432 ipsec->num_tx_sa--;
433 }
434 }
435
436 static const struct xfrmdev_ops ixgbevf_xfrmdev_ops = {
437 .xdo_dev_state_add = ixgbevf_ipsec_add_sa,
438 .xdo_dev_state_delete = ixgbevf_ipsec_del_sa,
439 };
440
441 /**
442 * ixgbevf_ipsec_tx - setup Tx flags for IPsec offload
443 * @tx_ring: outgoing context
444 * @first: current data packet
445 * @itd: ipsec Tx data for later use in building context descriptor
446 **/
ixgbevf_ipsec_tx(struct ixgbevf_ring * tx_ring,struct ixgbevf_tx_buffer * first,struct ixgbevf_ipsec_tx_data * itd)447 int ixgbevf_ipsec_tx(struct ixgbevf_ring *tx_ring,
448 struct ixgbevf_tx_buffer *first,
449 struct ixgbevf_ipsec_tx_data *itd)
450 {
451 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
452 struct ixgbevf_ipsec *ipsec = adapter->ipsec;
453 struct xfrm_state *xs;
454 struct sec_path *sp;
455 struct tx_sa *tsa;
456 u16 sa_idx;
457
458 sp = skb_sec_path(first->skb);
459 if (unlikely(!sp->len)) {
460 netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
461 __func__, sp->len);
462 return 0;
463 }
464
465 xs = xfrm_input_state(first->skb);
466 if (unlikely(!xs)) {
467 netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
468 __func__, xs);
469 return 0;
470 }
471
472 sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
473 if (unlikely(sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
474 netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
475 __func__, sa_idx, xs->xso.offload_handle);
476 return 0;
477 }
478
479 tsa = &ipsec->tx_tbl[sa_idx];
480 if (unlikely(!tsa->used)) {
481 netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
482 __func__, sa_idx);
483 return 0;
484 }
485
486 itd->pfsa = tsa->pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
487
488 first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CSUM;
489
490 if (xs->id.proto == IPPROTO_ESP) {
491 itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
492 IXGBE_ADVTXD_TUCMD_L4T_TCP;
493 if (first->protocol == htons(ETH_P_IP))
494 itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
495
496 /* The actual trailer length is authlen (16 bytes) plus
497 * 2 bytes for the proto and the padlen values, plus
498 * padlen bytes of padding. This ends up not the same
499 * as the static value found in xs->props.trailer_len (21).
500 *
501 * ... but if we're doing GSO, don't bother as the stack
502 * doesn't add a trailer for those.
503 */
504 if (!skb_is_gso(first->skb)) {
505 /* The "correct" way to get the auth length would be
506 * to use
507 * authlen = crypto_aead_authsize(xs->data);
508 * but since we know we only have one size to worry
509 * about * we can let the compiler use the constant
510 * and save us a few CPU cycles.
511 */
512 const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
513 struct sk_buff *skb = first->skb;
514 u8 padlen;
515 int ret;
516
517 ret = skb_copy_bits(skb, skb->len - (authlen + 2),
518 &padlen, 1);
519 if (unlikely(ret))
520 return 0;
521 itd->trailer_len = authlen + 2 + padlen;
522 }
523 }
524 if (tsa->encrypt)
525 itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
526
527 return 1;
528 }
529
530 /**
531 * ixgbevf_ipsec_rx - decode IPsec bits from Rx descriptor
532 * @rx_ring: receiving ring
533 * @rx_desc: receive data descriptor
534 * @skb: current data packet
535 *
536 * Determine if there was an IPsec encapsulation noticed, and if so set up
537 * the resulting status for later in the receive stack.
538 **/
ixgbevf_ipsec_rx(struct ixgbevf_ring * rx_ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)539 void ixgbevf_ipsec_rx(struct ixgbevf_ring *rx_ring,
540 union ixgbe_adv_rx_desc *rx_desc,
541 struct sk_buff *skb)
542 {
543 struct ixgbevf_adapter *adapter = netdev_priv(rx_ring->netdev);
544 __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
545 __le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
546 IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
547 struct ixgbevf_ipsec *ipsec = adapter->ipsec;
548 struct xfrm_offload *xo = NULL;
549 struct xfrm_state *xs = NULL;
550 struct ipv6hdr *ip6 = NULL;
551 struct iphdr *ip4 = NULL;
552 struct sec_path *sp;
553 void *daddr;
554 __be32 spi;
555 u8 *c_hdr;
556 u8 proto;
557
558 /* Find the IP and crypto headers in the data.
559 * We can assume no VLAN header in the way, b/c the
560 * hw won't recognize the IPsec packet and anyway the
561 * currently VLAN device doesn't support xfrm offload.
562 */
563 if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
564 ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
565 daddr = &ip4->daddr;
566 c_hdr = (u8 *)ip4 + ip4->ihl * 4;
567 } else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
568 ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
569 daddr = &ip6->daddr;
570 c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
571 } else {
572 return;
573 }
574
575 switch (pkt_info & ipsec_pkt_types) {
576 case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
577 spi = ((struct ip_auth_hdr *)c_hdr)->spi;
578 proto = IPPROTO_AH;
579 break;
580 case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
581 spi = ((struct ip_esp_hdr *)c_hdr)->spi;
582 proto = IPPROTO_ESP;
583 break;
584 default:
585 return;
586 }
587
588 xs = ixgbevf_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
589 if (unlikely(!xs))
590 return;
591
592 sp = secpath_set(skb);
593 if (unlikely(!sp))
594 return;
595
596 sp->xvec[sp->len++] = xs;
597 sp->olen++;
598 xo = xfrm_offload(skb);
599 xo->flags = CRYPTO_DONE;
600 xo->status = CRYPTO_SUCCESS;
601
602 adapter->rx_ipsec++;
603 }
604
605 /**
606 * ixgbevf_init_ipsec_offload - initialize registers for IPsec operation
607 * @adapter: board private structure
608 **/
ixgbevf_init_ipsec_offload(struct ixgbevf_adapter * adapter)609 void ixgbevf_init_ipsec_offload(struct ixgbevf_adapter *adapter)
610 {
611 struct ixgbevf_ipsec *ipsec;
612 size_t size;
613
614 switch (adapter->hw.api_version) {
615 case ixgbe_mbox_api_14:
616 break;
617 default:
618 return;
619 }
620
621 ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
622 if (!ipsec)
623 goto err1;
624 hash_init(ipsec->rx_sa_list);
625
626 size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
627 ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
628 if (!ipsec->rx_tbl)
629 goto err2;
630
631 size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
632 ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
633 if (!ipsec->tx_tbl)
634 goto err2;
635
636 ipsec->num_rx_sa = 0;
637 ipsec->num_tx_sa = 0;
638
639 adapter->ipsec = ipsec;
640
641 adapter->netdev->xfrmdev_ops = &ixgbevf_xfrmdev_ops;
642
643 #define IXGBEVF_ESP_FEATURES (NETIF_F_HW_ESP | \
644 NETIF_F_HW_ESP_TX_CSUM | \
645 NETIF_F_GSO_ESP)
646
647 adapter->netdev->features |= IXGBEVF_ESP_FEATURES;
648 adapter->netdev->hw_enc_features |= IXGBEVF_ESP_FEATURES;
649
650 return;
651
652 err2:
653 kfree(ipsec->rx_tbl);
654 kfree(ipsec->tx_tbl);
655 kfree(ipsec);
656 err1:
657 netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
658 }
659
660 /**
661 * ixgbevf_stop_ipsec_offload - tear down the IPsec offload
662 * @adapter: board private structure
663 **/
ixgbevf_stop_ipsec_offload(struct ixgbevf_adapter * adapter)664 void ixgbevf_stop_ipsec_offload(struct ixgbevf_adapter *adapter)
665 {
666 struct ixgbevf_ipsec *ipsec = adapter->ipsec;
667
668 adapter->ipsec = NULL;
669 if (ipsec) {
670 kfree(ipsec->rx_tbl);
671 kfree(ipsec->tx_tbl);
672 kfree(ipsec);
673 }
674 }
675