ixgbe_ipsec.c 28 KB

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