ixgbe_ipsec.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 Oracle and/or its affiliates. All rights reserved. */
  3. #include "ixgbe.h"
  4. #include <net/xfrm.h>
  5. #include <crypto/aead.h>
  6. /**
  7. * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
  8. * @hw: hw specific details
  9. * @idx: register index to write
  10. * @key: key byte array
  11. * @salt: salt bytes
  12. **/
  13. static void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
  14. u32 key[], u32 salt)
  15. {
  16. u32 reg;
  17. int i;
  18. for (i = 0; i < 4; i++)
  19. IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i),
  20. (__force u32)cpu_to_be32(key[3 - i]));
  21. IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, (__force u32)cpu_to_be32(salt));
  22. IXGBE_WRITE_FLUSH(hw);
  23. reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
  24. reg &= IXGBE_RXTXIDX_IPS_EN;
  25. reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
  26. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
  27. IXGBE_WRITE_FLUSH(hw);
  28. }
  29. /**
  30. * ixgbe_ipsec_set_rx_item - set an Rx table item
  31. * @hw: hw specific details
  32. * @idx: register index to write
  33. * @tbl: table selector
  34. *
  35. * Trigger the device to store into a particular Rx table the
  36. * data that has already been loaded into the input register
  37. **/
  38. static void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
  39. enum ixgbe_ipsec_tbl_sel tbl)
  40. {
  41. u32 reg;
  42. reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
  43. reg &= IXGBE_RXTXIDX_IPS_EN;
  44. reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
  45. idx << IXGBE_RXTXIDX_IDX_SHIFT |
  46. IXGBE_RXTXIDX_WRITE;
  47. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
  48. IXGBE_WRITE_FLUSH(hw);
  49. }
  50. /**
  51. * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
  52. * @hw: hw specific details
  53. * @idx: register index to write
  54. * @spi: security parameter index
  55. * @key: key byte array
  56. * @salt: salt bytes
  57. * @mode: rx decrypt control bits
  58. * @ip_idx: index into IP table for related IP address
  59. **/
  60. static void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
  61. u32 key[], u32 salt, u32 mode, u32 ip_idx)
  62. {
  63. int i;
  64. /* store the SPI (in bigendian) and IPidx */
  65. IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI,
  66. (__force u32)cpu_to_le32((__force u32)spi));
  67. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
  68. IXGBE_WRITE_FLUSH(hw);
  69. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
  70. /* store the key, salt, and mode */
  71. for (i = 0; i < 4; i++)
  72. IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i),
  73. (__force u32)cpu_to_be32(key[3 - i]));
  74. IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, (__force u32)cpu_to_be32(salt));
  75. IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
  76. IXGBE_WRITE_FLUSH(hw);
  77. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
  78. }
  79. /**
  80. * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
  81. * @hw: hw specific details
  82. * @idx: register index to write
  83. * @addr: IP address byte array
  84. **/
  85. static void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
  86. {
  87. int i;
  88. /* store the ip address */
  89. for (i = 0; i < 4; i++)
  90. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i),
  91. (__force u32)cpu_to_le32((__force u32)addr[i]));
  92. IXGBE_WRITE_FLUSH(hw);
  93. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
  94. }
  95. /**
  96. * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
  97. * @adapter: board private structure
  98. **/
  99. static void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
  100. {
  101. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  102. struct ixgbe_hw *hw = &adapter->hw;
  103. u32 buf[4] = {0, 0, 0, 0};
  104. u16 idx;
  105. /* disable Rx and Tx SA lookup */
  106. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
  107. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
  108. /* scrub the tables - split the loops for the max of the IP table */
  109. for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
  110. ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
  111. ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
  112. ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
  113. }
  114. for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
  115. ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
  116. ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
  117. }
  118. ipsec->num_rx_sa = 0;
  119. ipsec->num_tx_sa = 0;
  120. }
  121. /**
  122. * ixgbe_ipsec_stop_data
  123. * @adapter: board private structure
  124. **/
  125. static void ixgbe_ipsec_stop_data(struct ixgbe_adapter *adapter)
  126. {
  127. struct ixgbe_hw *hw = &adapter->hw;
  128. bool link = adapter->link_up;
  129. u32 t_rdy, r_rdy;
  130. u32 limit;
  131. u32 reg;
  132. /* halt data paths */
  133. reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
  134. reg |= IXGBE_SECTXCTRL_TX_DIS;
  135. IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
  136. reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  137. reg |= IXGBE_SECRXCTRL_RX_DIS;
  138. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
  139. /* If both Tx and Rx are ready there are no packets
  140. * that we need to flush so the loopback configuration
  141. * below is not necessary.
  142. */
  143. t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
  144. IXGBE_SECTXSTAT_SECTX_RDY;
  145. r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
  146. IXGBE_SECRXSTAT_SECRX_RDY;
  147. if (t_rdy && r_rdy)
  148. return;
  149. /* If the tx fifo doesn't have link, but still has data,
  150. * we can't clear the tx sec block. Set the MAC loopback
  151. * before block clear
  152. */
  153. if (!link) {
  154. reg = IXGBE_READ_REG(hw, IXGBE_MACC);
  155. reg |= IXGBE_MACC_FLU;
  156. IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
  157. reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
  158. reg |= IXGBE_HLREG0_LPBK;
  159. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
  160. IXGBE_WRITE_FLUSH(hw);
  161. mdelay(3);
  162. }
  163. /* wait for the paths to empty */
  164. limit = 20;
  165. do {
  166. mdelay(10);
  167. t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
  168. IXGBE_SECTXSTAT_SECTX_RDY;
  169. r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
  170. IXGBE_SECRXSTAT_SECRX_RDY;
  171. } while (!(t_rdy && r_rdy) && limit--);
  172. /* undo loopback if we played with it earlier */
  173. if (!link) {
  174. reg = IXGBE_READ_REG(hw, IXGBE_MACC);
  175. reg &= ~IXGBE_MACC_FLU;
  176. IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
  177. reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
  178. reg &= ~IXGBE_HLREG0_LPBK;
  179. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
  180. IXGBE_WRITE_FLUSH(hw);
  181. }
  182. }
  183. /**
  184. * ixgbe_ipsec_stop_engine
  185. * @adapter: board private structure
  186. **/
  187. static void ixgbe_ipsec_stop_engine(struct ixgbe_adapter *adapter)
  188. {
  189. struct ixgbe_hw *hw = &adapter->hw;
  190. u32 reg;
  191. ixgbe_ipsec_stop_data(adapter);
  192. /* disable Rx and Tx SA lookup */
  193. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
  194. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
  195. /* disable the Rx and Tx engines and full packet store-n-forward */
  196. reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
  197. reg |= IXGBE_SECTXCTRL_SECTX_DIS;
  198. reg &= ~IXGBE_SECTXCTRL_STORE_FORWARD;
  199. IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
  200. reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  201. reg |= IXGBE_SECRXCTRL_SECRX_DIS;
  202. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
  203. /* restore the "tx security buffer almost full threshold" to 0x250 */
  204. IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x250);
  205. /* Set minimum IFG between packets back to the default 0x1 */
  206. reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
  207. reg = (reg & 0xfffffff0) | 0x1;
  208. IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
  209. /* final set for normal (no ipsec offload) processing */
  210. IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_SECTX_DIS);
  211. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, IXGBE_SECRXCTRL_SECRX_DIS);
  212. IXGBE_WRITE_FLUSH(hw);
  213. }
  214. /**
  215. * ixgbe_ipsec_start_engine
  216. * @adapter: board private structure
  217. *
  218. * NOTE: this increases power consumption whether being used or not
  219. **/
  220. static void ixgbe_ipsec_start_engine(struct ixgbe_adapter *adapter)
  221. {
  222. struct ixgbe_hw *hw = &adapter->hw;
  223. u32 reg;
  224. ixgbe_ipsec_stop_data(adapter);
  225. /* Set minimum IFG between packets to 3 */
  226. reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
  227. reg = (reg & 0xfffffff0) | 0x3;
  228. IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
  229. /* Set "tx security buffer almost full threshold" to 0x15 so that the
  230. * almost full indication is generated only after buffer contains at
  231. * least an entire jumbo packet.
  232. */
  233. reg = IXGBE_READ_REG(hw, IXGBE_SECTXBUFFAF);
  234. reg = (reg & 0xfffffc00) | 0x15;
  235. IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, reg);
  236. /* restart the data paths by clearing the DISABLE bits */
  237. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
  238. IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_STORE_FORWARD);
  239. /* enable Rx and Tx SA lookup */
  240. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, IXGBE_RXTXIDX_IPS_EN);
  241. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, IXGBE_RXTXIDX_IPS_EN);
  242. IXGBE_WRITE_FLUSH(hw);
  243. }
  244. /**
  245. * ixgbe_ipsec_restore - restore the ipsec HW settings after a reset
  246. * @adapter: board private structure
  247. **/
  248. void ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
  249. {
  250. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  251. struct ixgbe_hw *hw = &adapter->hw;
  252. int i;
  253. if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
  254. return;
  255. /* clean up and restart the engine */
  256. ixgbe_ipsec_stop_engine(adapter);
  257. ixgbe_ipsec_clear_hw_tables(adapter);
  258. ixgbe_ipsec_start_engine(adapter);
  259. /* reload the IP addrs */
  260. for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
  261. struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
  262. if (ipsa->used)
  263. ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
  264. }
  265. /* reload the Rx and Tx keys */
  266. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  267. struct rx_sa *rsa = &ipsec->rx_tbl[i];
  268. struct tx_sa *tsa = &ipsec->tx_tbl[i];
  269. if (rsa->used)
  270. ixgbe_ipsec_set_rx_sa(hw, i, rsa->xs->id.spi,
  271. rsa->key, rsa->salt,
  272. rsa->mode, rsa->iptbl_ind);
  273. if (tsa->used)
  274. ixgbe_ipsec_set_tx_sa(hw, i, tsa->key, tsa->salt);
  275. }
  276. }
  277. /**
  278. * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
  279. * @ipsec: pointer to ipsec struct
  280. * @rxtable: true if we need to look in the Rx table
  281. *
  282. * Returns the first unused index in either the Rx or Tx SA table
  283. **/
  284. static int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
  285. {
  286. u32 i;
  287. if (rxtable) {
  288. if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
  289. return -ENOSPC;
  290. /* search rx sa table */
  291. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  292. if (!ipsec->rx_tbl[i].used)
  293. return i;
  294. }
  295. } else {
  296. if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
  297. return -ENOSPC;
  298. /* search tx sa table */
  299. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  300. if (!ipsec->tx_tbl[i].used)
  301. return i;
  302. }
  303. }
  304. return -ENOSPC;
  305. }
  306. /**
  307. * ixgbe_ipsec_find_rx_state - find the state that matches
  308. * @ipsec: pointer to ipsec struct
  309. * @daddr: inbound address to match
  310. * @proto: protocol to match
  311. * @spi: SPI to match
  312. * @ip4: true if using an ipv4 address
  313. *
  314. * Returns a pointer to the matching SA state information
  315. **/
  316. static struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
  317. __be32 *daddr, u8 proto,
  318. __be32 spi, bool ip4)
  319. {
  320. struct rx_sa *rsa;
  321. struct xfrm_state *ret = NULL;
  322. rcu_read_lock();
  323. hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
  324. (__force u32)spi) {
  325. if (spi == rsa->xs->id.spi &&
  326. ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
  327. (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
  328. sizeof(rsa->xs->id.daddr.a6)))) &&
  329. proto == rsa->xs->id.proto) {
  330. ret = rsa->xs;
  331. xfrm_state_hold(ret);
  332. break;
  333. }
  334. }
  335. rcu_read_unlock();
  336. return ret;
  337. }
  338. /**
  339. * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
  340. * @xs: pointer to xfrm_state struct
  341. * @mykey: pointer to key array to populate
  342. * @mysalt: pointer to salt value to populate
  343. *
  344. * This copies the protocol keys and salt to our own data tables. The
  345. * 82599 family only supports the one algorithm.
  346. **/
  347. static int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
  348. u32 *mykey, u32 *mysalt)
  349. {
  350. struct net_device *dev = xs->xso.dev;
  351. unsigned char *key_data;
  352. char *alg_name = NULL;
  353. const char aes_gcm_name[] = "rfc4106(gcm(aes))";
  354. int key_len;
  355. if (!xs->aead) {
  356. netdev_err(dev, "Unsupported IPsec algorithm\n");
  357. return -EINVAL;
  358. }
  359. if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
  360. netdev_err(dev, "IPsec offload requires %d bit authentication\n",
  361. IXGBE_IPSEC_AUTH_BITS);
  362. return -EINVAL;
  363. }
  364. key_data = &xs->aead->alg_key[0];
  365. key_len = xs->aead->alg_key_len;
  366. alg_name = xs->aead->alg_name;
  367. if (strcmp(alg_name, aes_gcm_name)) {
  368. netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
  369. aes_gcm_name);
  370. return -EINVAL;
  371. }
  372. /* The key bytes come down in a bigendian array of bytes, so
  373. * we don't need to do any byteswapping.
  374. * 160 accounts for 16 byte key and 4 byte salt
  375. */
  376. if (key_len == 160) {
  377. *mysalt = ((u32 *)key_data)[4];
  378. } else if (key_len != 128) {
  379. netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
  380. return -EINVAL;
  381. } else {
  382. netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
  383. *mysalt = 0;
  384. }
  385. memcpy(mykey, key_data, 16);
  386. return 0;
  387. }
  388. /**
  389. * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
  390. * @xs: pointer to transformer state struct
  391. **/
  392. static int ixgbe_ipsec_check_mgmt_ip(struct xfrm_state *xs)
  393. {
  394. struct net_device *dev = xs->xso.dev;
  395. struct ixgbe_adapter *adapter = netdev_priv(dev);
  396. struct ixgbe_hw *hw = &adapter->hw;
  397. u32 mfval, manc, reg;
  398. int num_filters = 4;
  399. bool manc_ipv4;
  400. u32 bmcipval;
  401. int i, j;
  402. #define MANC_EN_IPV4_FILTER BIT(24)
  403. #define MFVAL_IPV4_FILTER_SHIFT 16
  404. #define MFVAL_IPV6_FILTER_SHIFT 24
  405. #define MIPAF_ARR(_m, _n) (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
  406. #define IXGBE_BMCIP(_n) (0x5050 + ((_n) * 4))
  407. #define IXGBE_BMCIPVAL 0x5060
  408. #define BMCIP_V4 0x2
  409. #define BMCIP_V6 0x3
  410. #define BMCIP_MASK 0x3
  411. manc = IXGBE_READ_REG(hw, IXGBE_MANC);
  412. manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
  413. mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
  414. bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
  415. if (xs->props.family == AF_INET) {
  416. /* are there any IPv4 filters to check? */
  417. if (manc_ipv4) {
  418. /* the 4 ipv4 filters are all in MIPAF(3, i) */
  419. for (i = 0; i < num_filters; i++) {
  420. if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
  421. continue;
  422. reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
  423. if (reg == xs->id.daddr.a4)
  424. return 1;
  425. }
  426. }
  427. if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
  428. reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
  429. if (reg == xs->id.daddr.a4)
  430. return 1;
  431. }
  432. } else {
  433. /* if there are ipv4 filters, they are in the last ipv6 slot */
  434. if (manc_ipv4)
  435. num_filters = 3;
  436. for (i = 0; i < num_filters; i++) {
  437. if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
  438. continue;
  439. for (j = 0; j < 4; j++) {
  440. reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
  441. if (reg != xs->id.daddr.a6[j])
  442. break;
  443. }
  444. if (j == 4) /* did we match all 4 words? */
  445. return 1;
  446. }
  447. if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
  448. for (j = 0; j < 4; j++) {
  449. reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
  450. if (reg != xs->id.daddr.a6[j])
  451. break;
  452. }
  453. if (j == 4) /* did we match all 4 words? */
  454. return 1;
  455. }
  456. }
  457. return 0;
  458. }
  459. /**
  460. * ixgbe_ipsec_add_sa - program device with a security association
  461. * @xs: pointer to transformer state struct
  462. **/
  463. static int ixgbe_ipsec_add_sa(struct xfrm_state *xs)
  464. {
  465. struct net_device *dev = xs->xso.dev;
  466. struct ixgbe_adapter *adapter = netdev_priv(dev);
  467. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  468. struct ixgbe_hw *hw = &adapter->hw;
  469. int checked, match, first;
  470. u16 sa_idx;
  471. int ret;
  472. int i;
  473. if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
  474. netdev_err(dev, "Unsupported protocol 0x%04x for ipsec offload\n",
  475. xs->id.proto);
  476. return -EINVAL;
  477. }
  478. if (ixgbe_ipsec_check_mgmt_ip(xs)) {
  479. netdev_err(dev, "IPsec IP addr clash with mgmt filters\n");
  480. return -EINVAL;
  481. }
  482. if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
  483. struct rx_sa rsa;
  484. if (xs->calg) {
  485. netdev_err(dev, "Compression offload not supported\n");
  486. return -EINVAL;
  487. }
  488. /* find the first unused index */
  489. ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
  490. if (ret < 0) {
  491. netdev_err(dev, "No space for SA in Rx table!\n");
  492. return ret;
  493. }
  494. sa_idx = (u16)ret;
  495. memset(&rsa, 0, sizeof(rsa));
  496. rsa.used = true;
  497. rsa.xs = xs;
  498. if (rsa.xs->id.proto & IPPROTO_ESP)
  499. rsa.decrypt = xs->ealg || xs->aead;
  500. /* get the key and salt */
  501. ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
  502. if (ret) {
  503. netdev_err(dev, "Failed to get key data for Rx SA table\n");
  504. return ret;
  505. }
  506. /* get ip for rx sa table */
  507. if (xs->props.family == AF_INET6)
  508. memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
  509. else
  510. memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
  511. /* The HW does not have a 1:1 mapping from keys to IP addrs, so
  512. * check for a matching IP addr entry in the table. If the addr
  513. * already exists, use it; else find an unused slot and add the
  514. * addr. If one does not exist and there are no unused table
  515. * entries, fail the request.
  516. */
  517. /* Find an existing match or first not used, and stop looking
  518. * after we've checked all we know we have.
  519. */
  520. checked = 0;
  521. match = -1;
  522. first = -1;
  523. for (i = 0;
  524. i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
  525. (checked < ipsec->num_rx_sa || first < 0);
  526. i++) {
  527. if (ipsec->ip_tbl[i].used) {
  528. if (!memcmp(ipsec->ip_tbl[i].ipaddr,
  529. rsa.ipaddr, sizeof(rsa.ipaddr))) {
  530. match = i;
  531. break;
  532. }
  533. checked++;
  534. } else if (first < 0) {
  535. first = i; /* track the first empty seen */
  536. }
  537. }
  538. if (ipsec->num_rx_sa == 0)
  539. first = 0;
  540. if (match >= 0) {
  541. /* addrs are the same, we should use this one */
  542. rsa.iptbl_ind = match;
  543. ipsec->ip_tbl[match].ref_cnt++;
  544. } else if (first >= 0) {
  545. /* no matches, but here's an empty slot */
  546. rsa.iptbl_ind = first;
  547. memcpy(ipsec->ip_tbl[first].ipaddr,
  548. rsa.ipaddr, sizeof(rsa.ipaddr));
  549. ipsec->ip_tbl[first].ref_cnt = 1;
  550. ipsec->ip_tbl[first].used = true;
  551. ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
  552. } else {
  553. /* no match and no empty slot */
  554. netdev_err(dev, "No space for SA in Rx IP SA table\n");
  555. memset(&rsa, 0, sizeof(rsa));
  556. return -ENOSPC;
  557. }
  558. rsa.mode = IXGBE_RXMOD_VALID;
  559. if (rsa.xs->id.proto & IPPROTO_ESP)
  560. rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
  561. if (rsa.decrypt)
  562. rsa.mode |= IXGBE_RXMOD_DECRYPT;
  563. if (rsa.xs->props.family == AF_INET6)
  564. rsa.mode |= IXGBE_RXMOD_IPV6;
  565. /* the preparations worked, so save the info */
  566. memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
  567. ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
  568. rsa.salt, rsa.mode, rsa.iptbl_ind);
  569. xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
  570. ipsec->num_rx_sa++;
  571. /* hash the new entry for faster search in Rx path */
  572. hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
  573. (__force u32)rsa.xs->id.spi);
  574. } else {
  575. struct tx_sa tsa;
  576. /* find the first unused index */
  577. ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
  578. if (ret < 0) {
  579. netdev_err(dev, "No space for SA in Tx table\n");
  580. return ret;
  581. }
  582. sa_idx = (u16)ret;
  583. memset(&tsa, 0, sizeof(tsa));
  584. tsa.used = true;
  585. tsa.xs = xs;
  586. if (xs->id.proto & IPPROTO_ESP)
  587. tsa.encrypt = xs->ealg || xs->aead;
  588. ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
  589. if (ret) {
  590. netdev_err(dev, "Failed to get key data for Tx SA table\n");
  591. memset(&tsa, 0, sizeof(tsa));
  592. return ret;
  593. }
  594. /* the preparations worked, so save the info */
  595. memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
  596. ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
  597. xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
  598. ipsec->num_tx_sa++;
  599. }
  600. /* enable the engine if not already warmed up */
  601. if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
  602. ixgbe_ipsec_start_engine(adapter);
  603. adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
  604. }
  605. return 0;
  606. }
  607. /**
  608. * ixgbe_ipsec_del_sa - clear out this specific SA
  609. * @xs: pointer to transformer state struct
  610. **/
  611. static void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
  612. {
  613. struct net_device *dev = xs->xso.dev;
  614. struct ixgbe_adapter *adapter = netdev_priv(dev);
  615. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  616. struct ixgbe_hw *hw = &adapter->hw;
  617. u32 zerobuf[4] = {0, 0, 0, 0};
  618. u16 sa_idx;
  619. if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
  620. struct rx_sa *rsa;
  621. u8 ipi;
  622. sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
  623. rsa = &ipsec->rx_tbl[sa_idx];
  624. if (!rsa->used) {
  625. netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
  626. sa_idx, xs->xso.offload_handle);
  627. return;
  628. }
  629. ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
  630. hash_del_rcu(&rsa->hlist);
  631. /* if the IP table entry is referenced by only this SA,
  632. * i.e. ref_cnt is only 1, clear the IP table entry as well
  633. */
  634. ipi = rsa->iptbl_ind;
  635. if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
  636. ipsec->ip_tbl[ipi].ref_cnt--;
  637. if (!ipsec->ip_tbl[ipi].ref_cnt) {
  638. memset(&ipsec->ip_tbl[ipi], 0,
  639. sizeof(struct rx_ip_sa));
  640. ixgbe_ipsec_set_rx_ip(hw, ipi,
  641. (__force __be32 *)zerobuf);
  642. }
  643. }
  644. memset(rsa, 0, sizeof(struct rx_sa));
  645. ipsec->num_rx_sa--;
  646. } else {
  647. sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
  648. if (!ipsec->tx_tbl[sa_idx].used) {
  649. netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
  650. sa_idx, xs->xso.offload_handle);
  651. return;
  652. }
  653. ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
  654. memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
  655. ipsec->num_tx_sa--;
  656. }
  657. /* if there are no SAs left, stop the engine to save energy */
  658. if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
  659. adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
  660. ixgbe_ipsec_stop_engine(adapter);
  661. }
  662. }
  663. /**
  664. * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
  665. * @skb: current data packet
  666. * @xs: pointer to transformer state struct
  667. **/
  668. static bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
  669. {
  670. if (xs->props.family == AF_INET) {
  671. /* Offload with IPv4 options is not supported yet */
  672. if (ip_hdr(skb)->ihl != 5)
  673. return false;
  674. } else {
  675. /* Offload with IPv6 extension headers is not support yet */
  676. if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
  677. return false;
  678. }
  679. return true;
  680. }
  681. static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
  682. .xdo_dev_state_add = ixgbe_ipsec_add_sa,
  683. .xdo_dev_state_delete = ixgbe_ipsec_del_sa,
  684. .xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
  685. };
  686. /**
  687. * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
  688. * @tx_ring: outgoing context
  689. * @first: current data packet
  690. * @itd: ipsec Tx data for later use in building context descriptor
  691. **/
  692. int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
  693. struct ixgbe_tx_buffer *first,
  694. struct ixgbe_ipsec_tx_data *itd)
  695. {
  696. struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
  697. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  698. struct xfrm_state *xs;
  699. struct tx_sa *tsa;
  700. if (unlikely(!first->skb->sp->len)) {
  701. netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
  702. __func__, first->skb->sp->len);
  703. return 0;
  704. }
  705. xs = xfrm_input_state(first->skb);
  706. if (unlikely(!xs)) {
  707. netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
  708. __func__, xs);
  709. return 0;
  710. }
  711. itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
  712. if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
  713. netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
  714. __func__, itd->sa_idx, xs->xso.offload_handle);
  715. return 0;
  716. }
  717. tsa = &ipsec->tx_tbl[itd->sa_idx];
  718. if (unlikely(!tsa->used)) {
  719. netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
  720. __func__, itd->sa_idx);
  721. return 0;
  722. }
  723. first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
  724. if (xs->id.proto == IPPROTO_ESP) {
  725. itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
  726. IXGBE_ADVTXD_TUCMD_L4T_TCP;
  727. if (first->protocol == htons(ETH_P_IP))
  728. itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
  729. /* The actual trailer length is authlen (16 bytes) plus
  730. * 2 bytes for the proto and the padlen values, plus
  731. * padlen bytes of padding. This ends up not the same
  732. * as the static value found in xs->props.trailer_len (21).
  733. *
  734. * ... but if we're doing GSO, don't bother as the stack
  735. * doesn't add a trailer for those.
  736. */
  737. if (!skb_is_gso(first->skb)) {
  738. /* The "correct" way to get the auth length would be
  739. * to use
  740. * authlen = crypto_aead_authsize(xs->data);
  741. * but since we know we only have one size to worry
  742. * about * we can let the compiler use the constant
  743. * and save us a few CPU cycles.
  744. */
  745. const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
  746. struct sk_buff *skb = first->skb;
  747. u8 padlen;
  748. int ret;
  749. ret = skb_copy_bits(skb, skb->len - (authlen + 2),
  750. &padlen, 1);
  751. if (unlikely(ret))
  752. return 0;
  753. itd->trailer_len = authlen + 2 + padlen;
  754. }
  755. }
  756. if (tsa->encrypt)
  757. itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
  758. return 1;
  759. }
  760. /**
  761. * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
  762. * @rx_ring: receiving ring
  763. * @rx_desc: receive data descriptor
  764. * @skb: current data packet
  765. *
  766. * Determine if there was an ipsec encapsulation noticed, and if so set up
  767. * the resulting status for later in the receive stack.
  768. **/
  769. void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
  770. union ixgbe_adv_rx_desc *rx_desc,
  771. struct sk_buff *skb)
  772. {
  773. struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
  774. __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
  775. __le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
  776. IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
  777. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  778. struct xfrm_offload *xo = NULL;
  779. struct xfrm_state *xs = NULL;
  780. struct ipv6hdr *ip6 = NULL;
  781. struct iphdr *ip4 = NULL;
  782. void *daddr;
  783. __be32 spi;
  784. u8 *c_hdr;
  785. u8 proto;
  786. /* Find the ip and crypto headers in the data.
  787. * We can assume no vlan header in the way, b/c the
  788. * hw won't recognize the IPsec packet and anyway the
  789. * currently vlan device doesn't support xfrm offload.
  790. */
  791. if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
  792. ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
  793. daddr = &ip4->daddr;
  794. c_hdr = (u8 *)ip4 + ip4->ihl * 4;
  795. } else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
  796. ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  797. daddr = &ip6->daddr;
  798. c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
  799. } else {
  800. return;
  801. }
  802. switch (pkt_info & ipsec_pkt_types) {
  803. case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
  804. spi = ((struct ip_auth_hdr *)c_hdr)->spi;
  805. proto = IPPROTO_AH;
  806. break;
  807. case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
  808. spi = ((struct ip_esp_hdr *)c_hdr)->spi;
  809. proto = IPPROTO_ESP;
  810. break;
  811. default:
  812. return;
  813. }
  814. xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
  815. if (unlikely(!xs))
  816. return;
  817. skb->sp = secpath_dup(skb->sp);
  818. if (unlikely(!skb->sp))
  819. return;
  820. skb->sp->xvec[skb->sp->len++] = xs;
  821. skb->sp->olen++;
  822. xo = xfrm_offload(skb);
  823. xo->flags = CRYPTO_DONE;
  824. xo->status = CRYPTO_SUCCESS;
  825. adapter->rx_ipsec++;
  826. }
  827. /**
  828. * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
  829. * @adapter: board private structure
  830. **/
  831. void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
  832. {
  833. struct ixgbe_hw *hw = &adapter->hw;
  834. struct ixgbe_ipsec *ipsec;
  835. u32 t_dis, r_dis;
  836. size_t size;
  837. if (hw->mac.type == ixgbe_mac_82598EB)
  838. return;
  839. /* If there is no support for either Tx or Rx offload
  840. * we should not be advertising support for IPsec.
  841. */
  842. t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
  843. IXGBE_SECTXSTAT_SECTX_OFF_DIS;
  844. r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
  845. IXGBE_SECRXSTAT_SECRX_OFF_DIS;
  846. if (t_dis || r_dis)
  847. return;
  848. ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
  849. if (!ipsec)
  850. goto err1;
  851. hash_init(ipsec->rx_sa_list);
  852. size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
  853. ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
  854. if (!ipsec->rx_tbl)
  855. goto err2;
  856. size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
  857. ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
  858. if (!ipsec->tx_tbl)
  859. goto err2;
  860. size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
  861. ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
  862. if (!ipsec->ip_tbl)
  863. goto err2;
  864. ipsec->num_rx_sa = 0;
  865. ipsec->num_tx_sa = 0;
  866. adapter->ipsec = ipsec;
  867. ixgbe_ipsec_stop_engine(adapter);
  868. ixgbe_ipsec_clear_hw_tables(adapter);
  869. adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
  870. return;
  871. err2:
  872. kfree(ipsec->ip_tbl);
  873. kfree(ipsec->rx_tbl);
  874. kfree(ipsec->tx_tbl);
  875. kfree(ipsec);
  876. err1:
  877. netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
  878. }
  879. /**
  880. * ixgbe_stop_ipsec_offload - tear down the ipsec offload
  881. * @adapter: board private structure
  882. **/
  883. void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
  884. {
  885. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  886. adapter->ipsec = NULL;
  887. if (ipsec) {
  888. kfree(ipsec->ip_tbl);
  889. kfree(ipsec->rx_tbl);
  890. kfree(ipsec->tx_tbl);
  891. kfree(ipsec);
  892. }
  893. }