ixgbe_ipsec.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. #define IXGBE_IPSEC_KEY_BITS 160
  7. static const char aes_gcm_name[] = "rfc4106(gcm(aes))";
  8. static void ixgbe_ipsec_del_sa(struct xfrm_state *xs);
  9. /**
  10. * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
  11. * @hw: hw specific details
  12. * @idx: register index to write
  13. * @key: key byte array
  14. * @salt: salt bytes
  15. **/
  16. static void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
  17. u32 key[], u32 salt)
  18. {
  19. u32 reg;
  20. int i;
  21. for (i = 0; i < 4; i++)
  22. IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i),
  23. (__force u32)cpu_to_be32(key[3 - i]));
  24. IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, (__force u32)cpu_to_be32(salt));
  25. IXGBE_WRITE_FLUSH(hw);
  26. reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
  27. reg &= IXGBE_RXTXIDX_IPS_EN;
  28. reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
  29. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
  30. IXGBE_WRITE_FLUSH(hw);
  31. }
  32. /**
  33. * ixgbe_ipsec_set_rx_item - set an Rx table item
  34. * @hw: hw specific details
  35. * @idx: register index to write
  36. * @tbl: table selector
  37. *
  38. * Trigger the device to store into a particular Rx table the
  39. * data that has already been loaded into the input register
  40. **/
  41. static void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
  42. enum ixgbe_ipsec_tbl_sel tbl)
  43. {
  44. u32 reg;
  45. reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
  46. reg &= IXGBE_RXTXIDX_IPS_EN;
  47. reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
  48. idx << IXGBE_RXTXIDX_IDX_SHIFT |
  49. IXGBE_RXTXIDX_WRITE;
  50. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
  51. IXGBE_WRITE_FLUSH(hw);
  52. }
  53. /**
  54. * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
  55. * @hw: hw specific details
  56. * @idx: register index to write
  57. * @spi: security parameter index
  58. * @key: key byte array
  59. * @salt: salt bytes
  60. * @mode: rx decrypt control bits
  61. * @ip_idx: index into IP table for related IP address
  62. **/
  63. static void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
  64. u32 key[], u32 salt, u32 mode, u32 ip_idx)
  65. {
  66. int i;
  67. /* store the SPI (in bigendian) and IPidx */
  68. IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI,
  69. (__force u32)cpu_to_le32((__force u32)spi));
  70. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
  71. IXGBE_WRITE_FLUSH(hw);
  72. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
  73. /* store the key, salt, and mode */
  74. for (i = 0; i < 4; i++)
  75. IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i),
  76. (__force u32)cpu_to_be32(key[3 - i]));
  77. IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, (__force u32)cpu_to_be32(salt));
  78. IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
  79. IXGBE_WRITE_FLUSH(hw);
  80. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
  81. }
  82. /**
  83. * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
  84. * @hw: hw specific details
  85. * @idx: register index to write
  86. * @addr: IP address byte array
  87. **/
  88. static void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
  89. {
  90. int i;
  91. /* store the ip address */
  92. for (i = 0; i < 4; i++)
  93. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i),
  94. (__force u32)cpu_to_le32((__force u32)addr[i]));
  95. IXGBE_WRITE_FLUSH(hw);
  96. ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
  97. }
  98. /**
  99. * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
  100. * @adapter: board private structure
  101. **/
  102. static void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
  103. {
  104. struct ixgbe_hw *hw = &adapter->hw;
  105. u32 buf[4] = {0, 0, 0, 0};
  106. u16 idx;
  107. /* disable Rx and Tx SA lookup */
  108. IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
  109. IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
  110. /* scrub the tables - split the loops for the max of the IP table */
  111. for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
  112. ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
  113. ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
  114. ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
  115. }
  116. for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
  117. ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
  118. ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
  119. }
  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. * Reload the HW tables from the SW tables after they've been bashed
  249. * by a chip reset.
  250. *
  251. * Any VF entries are removed from the SW and HW tables since either
  252. * (a) the VF also gets reset on PF reset and will ask again for the
  253. * offloads, or (b) the VF has been removed by a change in the num_vfs.
  254. **/
  255. void ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
  256. {
  257. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  258. struct ixgbe_hw *hw = &adapter->hw;
  259. int i;
  260. if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
  261. return;
  262. /* clean up and restart the engine */
  263. ixgbe_ipsec_stop_engine(adapter);
  264. ixgbe_ipsec_clear_hw_tables(adapter);
  265. ixgbe_ipsec_start_engine(adapter);
  266. /* reload the Rx and Tx keys */
  267. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  268. struct rx_sa *r = &ipsec->rx_tbl[i];
  269. struct tx_sa *t = &ipsec->tx_tbl[i];
  270. if (r->used) {
  271. if (r->mode & IXGBE_RXTXMOD_VF)
  272. ixgbe_ipsec_del_sa(r->xs);
  273. else
  274. ixgbe_ipsec_set_rx_sa(hw, i, r->xs->id.spi,
  275. r->key, r->salt,
  276. r->mode, r->iptbl_ind);
  277. }
  278. if (t->used) {
  279. if (t->mode & IXGBE_RXTXMOD_VF)
  280. ixgbe_ipsec_del_sa(t->xs);
  281. else
  282. ixgbe_ipsec_set_tx_sa(hw, i, t->key, t->salt);
  283. }
  284. }
  285. /* reload the IP addrs */
  286. for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
  287. struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
  288. if (ipsa->used)
  289. ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
  290. }
  291. }
  292. /**
  293. * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
  294. * @ipsec: pointer to ipsec struct
  295. * @rxtable: true if we need to look in the Rx table
  296. *
  297. * Returns the first unused index in either the Rx or Tx SA table
  298. **/
  299. static int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
  300. {
  301. u32 i;
  302. if (rxtable) {
  303. if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
  304. return -ENOSPC;
  305. /* search rx sa table */
  306. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  307. if (!ipsec->rx_tbl[i].used)
  308. return i;
  309. }
  310. } else {
  311. if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
  312. return -ENOSPC;
  313. /* search tx sa table */
  314. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
  315. if (!ipsec->tx_tbl[i].used)
  316. return i;
  317. }
  318. }
  319. return -ENOSPC;
  320. }
  321. /**
  322. * ixgbe_ipsec_find_rx_state - find the state that matches
  323. * @ipsec: pointer to ipsec struct
  324. * @daddr: inbound address to match
  325. * @proto: protocol to match
  326. * @spi: SPI to match
  327. * @ip4: true if using an ipv4 address
  328. *
  329. * Returns a pointer to the matching SA state information
  330. **/
  331. static struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
  332. __be32 *daddr, u8 proto,
  333. __be32 spi, bool ip4)
  334. {
  335. struct rx_sa *rsa;
  336. struct xfrm_state *ret = NULL;
  337. rcu_read_lock();
  338. hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
  339. (__force u32)spi) {
  340. if (rsa->mode & IXGBE_RXTXMOD_VF)
  341. continue;
  342. if (spi == rsa->xs->id.spi &&
  343. ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
  344. (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
  345. sizeof(rsa->xs->id.daddr.a6)))) &&
  346. proto == rsa->xs->id.proto) {
  347. ret = rsa->xs;
  348. xfrm_state_hold(ret);
  349. break;
  350. }
  351. }
  352. rcu_read_unlock();
  353. return ret;
  354. }
  355. /**
  356. * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
  357. * @xs: pointer to xfrm_state struct
  358. * @mykey: pointer to key array to populate
  359. * @mysalt: pointer to salt value to populate
  360. *
  361. * This copies the protocol keys and salt to our own data tables. The
  362. * 82599 family only supports the one algorithm.
  363. **/
  364. static int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
  365. u32 *mykey, u32 *mysalt)
  366. {
  367. struct net_device *dev = xs->xso.dev;
  368. unsigned char *key_data;
  369. char *alg_name = NULL;
  370. int key_len;
  371. if (!xs->aead) {
  372. netdev_err(dev, "Unsupported IPsec algorithm\n");
  373. return -EINVAL;
  374. }
  375. if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
  376. netdev_err(dev, "IPsec offload requires %d bit authentication\n",
  377. IXGBE_IPSEC_AUTH_BITS);
  378. return -EINVAL;
  379. }
  380. key_data = &xs->aead->alg_key[0];
  381. key_len = xs->aead->alg_key_len;
  382. alg_name = xs->aead->alg_name;
  383. if (strcmp(alg_name, aes_gcm_name)) {
  384. netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
  385. aes_gcm_name);
  386. return -EINVAL;
  387. }
  388. /* The key bytes come down in a bigendian array of bytes, so
  389. * we don't need to do any byteswapping.
  390. * 160 accounts for 16 byte key and 4 byte salt
  391. */
  392. if (key_len == IXGBE_IPSEC_KEY_BITS) {
  393. *mysalt = ((u32 *)key_data)[4];
  394. } else if (key_len != (IXGBE_IPSEC_KEY_BITS - (sizeof(*mysalt) * 8))) {
  395. netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
  396. return -EINVAL;
  397. } else {
  398. netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
  399. *mysalt = 0;
  400. }
  401. memcpy(mykey, key_data, 16);
  402. return 0;
  403. }
  404. /**
  405. * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
  406. * @xs: pointer to transformer state struct
  407. **/
  408. static int ixgbe_ipsec_check_mgmt_ip(struct xfrm_state *xs)
  409. {
  410. struct net_device *dev = xs->xso.dev;
  411. struct ixgbe_adapter *adapter = netdev_priv(dev);
  412. struct ixgbe_hw *hw = &adapter->hw;
  413. u32 mfval, manc, reg;
  414. int num_filters = 4;
  415. bool manc_ipv4;
  416. u32 bmcipval;
  417. int i, j;
  418. #define MANC_EN_IPV4_FILTER BIT(24)
  419. #define MFVAL_IPV4_FILTER_SHIFT 16
  420. #define MFVAL_IPV6_FILTER_SHIFT 24
  421. #define MIPAF_ARR(_m, _n) (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
  422. #define IXGBE_BMCIP(_n) (0x5050 + ((_n) * 4))
  423. #define IXGBE_BMCIPVAL 0x5060
  424. #define BMCIP_V4 0x2
  425. #define BMCIP_V6 0x3
  426. #define BMCIP_MASK 0x3
  427. manc = IXGBE_READ_REG(hw, IXGBE_MANC);
  428. manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
  429. mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
  430. bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
  431. if (xs->props.family == AF_INET) {
  432. /* are there any IPv4 filters to check? */
  433. if (manc_ipv4) {
  434. /* the 4 ipv4 filters are all in MIPAF(3, i) */
  435. for (i = 0; i < num_filters; i++) {
  436. if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
  437. continue;
  438. reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
  439. if (reg == xs->id.daddr.a4)
  440. return 1;
  441. }
  442. }
  443. if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
  444. reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
  445. if (reg == xs->id.daddr.a4)
  446. return 1;
  447. }
  448. } else {
  449. /* if there are ipv4 filters, they are in the last ipv6 slot */
  450. if (manc_ipv4)
  451. num_filters = 3;
  452. for (i = 0; i < num_filters; i++) {
  453. if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
  454. continue;
  455. for (j = 0; j < 4; j++) {
  456. reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
  457. if (reg != xs->id.daddr.a6[j])
  458. break;
  459. }
  460. if (j == 4) /* did we match all 4 words? */
  461. return 1;
  462. }
  463. if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
  464. for (j = 0; j < 4; j++) {
  465. reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
  466. if (reg != xs->id.daddr.a6[j])
  467. break;
  468. }
  469. if (j == 4) /* did we match all 4 words? */
  470. return 1;
  471. }
  472. }
  473. return 0;
  474. }
  475. /**
  476. * ixgbe_ipsec_add_sa - program device with a security association
  477. * @xs: pointer to transformer state struct
  478. **/
  479. static int ixgbe_ipsec_add_sa(struct xfrm_state *xs)
  480. {
  481. struct net_device *dev = xs->xso.dev;
  482. struct ixgbe_adapter *adapter = netdev_priv(dev);
  483. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  484. struct ixgbe_hw *hw = &adapter->hw;
  485. int checked, match, first;
  486. u16 sa_idx;
  487. int ret;
  488. int i;
  489. if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
  490. netdev_err(dev, "Unsupported protocol 0x%04x for ipsec offload\n",
  491. xs->id.proto);
  492. return -EINVAL;
  493. }
  494. if (ixgbe_ipsec_check_mgmt_ip(xs)) {
  495. netdev_err(dev, "IPsec IP addr clash with mgmt filters\n");
  496. return -EINVAL;
  497. }
  498. if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
  499. struct rx_sa rsa;
  500. if (xs->calg) {
  501. netdev_err(dev, "Compression offload not supported\n");
  502. return -EINVAL;
  503. }
  504. /* find the first unused index */
  505. ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
  506. if (ret < 0) {
  507. netdev_err(dev, "No space for SA in Rx table!\n");
  508. return ret;
  509. }
  510. sa_idx = (u16)ret;
  511. memset(&rsa, 0, sizeof(rsa));
  512. rsa.used = true;
  513. rsa.xs = xs;
  514. if (rsa.xs->id.proto & IPPROTO_ESP)
  515. rsa.decrypt = xs->ealg || xs->aead;
  516. /* get the key and salt */
  517. ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
  518. if (ret) {
  519. netdev_err(dev, "Failed to get key data for Rx SA table\n");
  520. return ret;
  521. }
  522. /* get ip for rx sa table */
  523. if (xs->props.family == AF_INET6)
  524. memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
  525. else
  526. memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
  527. /* The HW does not have a 1:1 mapping from keys to IP addrs, so
  528. * check for a matching IP addr entry in the table. If the addr
  529. * already exists, use it; else find an unused slot and add the
  530. * addr. If one does not exist and there are no unused table
  531. * entries, fail the request.
  532. */
  533. /* Find an existing match or first not used, and stop looking
  534. * after we've checked all we know we have.
  535. */
  536. checked = 0;
  537. match = -1;
  538. first = -1;
  539. for (i = 0;
  540. i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
  541. (checked < ipsec->num_rx_sa || first < 0);
  542. i++) {
  543. if (ipsec->ip_tbl[i].used) {
  544. if (!memcmp(ipsec->ip_tbl[i].ipaddr,
  545. rsa.ipaddr, sizeof(rsa.ipaddr))) {
  546. match = i;
  547. break;
  548. }
  549. checked++;
  550. } else if (first < 0) {
  551. first = i; /* track the first empty seen */
  552. }
  553. }
  554. if (ipsec->num_rx_sa == 0)
  555. first = 0;
  556. if (match >= 0) {
  557. /* addrs are the same, we should use this one */
  558. rsa.iptbl_ind = match;
  559. ipsec->ip_tbl[match].ref_cnt++;
  560. } else if (first >= 0) {
  561. /* no matches, but here's an empty slot */
  562. rsa.iptbl_ind = first;
  563. memcpy(ipsec->ip_tbl[first].ipaddr,
  564. rsa.ipaddr, sizeof(rsa.ipaddr));
  565. ipsec->ip_tbl[first].ref_cnt = 1;
  566. ipsec->ip_tbl[first].used = true;
  567. ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
  568. } else {
  569. /* no match and no empty slot */
  570. netdev_err(dev, "No space for SA in Rx IP SA table\n");
  571. memset(&rsa, 0, sizeof(rsa));
  572. return -ENOSPC;
  573. }
  574. rsa.mode = IXGBE_RXMOD_VALID;
  575. if (rsa.xs->id.proto & IPPROTO_ESP)
  576. rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
  577. if (rsa.decrypt)
  578. rsa.mode |= IXGBE_RXMOD_DECRYPT;
  579. if (rsa.xs->props.family == AF_INET6)
  580. rsa.mode |= IXGBE_RXMOD_IPV6;
  581. /* the preparations worked, so save the info */
  582. memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
  583. ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
  584. rsa.salt, rsa.mode, rsa.iptbl_ind);
  585. xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
  586. ipsec->num_rx_sa++;
  587. /* hash the new entry for faster search in Rx path */
  588. hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
  589. (__force u32)rsa.xs->id.spi);
  590. } else {
  591. struct tx_sa tsa;
  592. /* find the first unused index */
  593. ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
  594. if (ret < 0) {
  595. netdev_err(dev, "No space for SA in Tx table\n");
  596. return ret;
  597. }
  598. sa_idx = (u16)ret;
  599. memset(&tsa, 0, sizeof(tsa));
  600. tsa.used = true;
  601. tsa.xs = xs;
  602. if (xs->id.proto & IPPROTO_ESP)
  603. tsa.encrypt = xs->ealg || xs->aead;
  604. ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
  605. if (ret) {
  606. netdev_err(dev, "Failed to get key data for Tx SA table\n");
  607. memset(&tsa, 0, sizeof(tsa));
  608. return ret;
  609. }
  610. /* the preparations worked, so save the info */
  611. memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
  612. ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
  613. xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
  614. ipsec->num_tx_sa++;
  615. }
  616. /* enable the engine if not already warmed up */
  617. if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
  618. ixgbe_ipsec_start_engine(adapter);
  619. adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
  620. }
  621. return 0;
  622. }
  623. /**
  624. * ixgbe_ipsec_del_sa - clear out this specific SA
  625. * @xs: pointer to transformer state struct
  626. **/
  627. static void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
  628. {
  629. struct net_device *dev = xs->xso.dev;
  630. struct ixgbe_adapter *adapter = netdev_priv(dev);
  631. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  632. struct ixgbe_hw *hw = &adapter->hw;
  633. u32 zerobuf[4] = {0, 0, 0, 0};
  634. u16 sa_idx;
  635. if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
  636. struct rx_sa *rsa;
  637. u8 ipi;
  638. sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
  639. rsa = &ipsec->rx_tbl[sa_idx];
  640. if (!rsa->used) {
  641. netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
  642. sa_idx, xs->xso.offload_handle);
  643. return;
  644. }
  645. ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
  646. hash_del_rcu(&rsa->hlist);
  647. /* if the IP table entry is referenced by only this SA,
  648. * i.e. ref_cnt is only 1, clear the IP table entry as well
  649. */
  650. ipi = rsa->iptbl_ind;
  651. if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
  652. ipsec->ip_tbl[ipi].ref_cnt--;
  653. if (!ipsec->ip_tbl[ipi].ref_cnt) {
  654. memset(&ipsec->ip_tbl[ipi], 0,
  655. sizeof(struct rx_ip_sa));
  656. ixgbe_ipsec_set_rx_ip(hw, ipi,
  657. (__force __be32 *)zerobuf);
  658. }
  659. }
  660. memset(rsa, 0, sizeof(struct rx_sa));
  661. ipsec->num_rx_sa--;
  662. } else {
  663. sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
  664. if (!ipsec->tx_tbl[sa_idx].used) {
  665. netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
  666. sa_idx, xs->xso.offload_handle);
  667. return;
  668. }
  669. ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
  670. memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
  671. ipsec->num_tx_sa--;
  672. }
  673. /* if there are no SAs left, stop the engine to save energy */
  674. if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
  675. adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
  676. ixgbe_ipsec_stop_engine(adapter);
  677. }
  678. }
  679. /**
  680. * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
  681. * @skb: current data packet
  682. * @xs: pointer to transformer state struct
  683. **/
  684. static bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
  685. {
  686. if (xs->props.family == AF_INET) {
  687. /* Offload with IPv4 options is not supported yet */
  688. if (ip_hdr(skb)->ihl != 5)
  689. return false;
  690. } else {
  691. /* Offload with IPv6 extension headers is not support yet */
  692. if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
  693. return false;
  694. }
  695. return true;
  696. }
  697. static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
  698. .xdo_dev_state_add = ixgbe_ipsec_add_sa,
  699. .xdo_dev_state_delete = ixgbe_ipsec_del_sa,
  700. .xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
  701. };
  702. /**
  703. * ixgbe_ipsec_vf_clear - clear the tables of data for a VF
  704. * @adapter: board private structure
  705. * @vf: VF id to be removed
  706. **/
  707. void ixgbe_ipsec_vf_clear(struct ixgbe_adapter *adapter, u32 vf)
  708. {
  709. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  710. int i;
  711. /* search rx sa table */
  712. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_rx_sa; i++) {
  713. if (!ipsec->rx_tbl[i].used)
  714. continue;
  715. if (ipsec->rx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
  716. ipsec->rx_tbl[i].vf == vf)
  717. ixgbe_ipsec_del_sa(ipsec->rx_tbl[i].xs);
  718. }
  719. /* search tx sa table */
  720. for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_tx_sa; i++) {
  721. if (!ipsec->tx_tbl[i].used)
  722. continue;
  723. if (ipsec->tx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
  724. ipsec->tx_tbl[i].vf == vf)
  725. ixgbe_ipsec_del_sa(ipsec->tx_tbl[i].xs);
  726. }
  727. }
  728. /**
  729. * ixgbe_ipsec_vf_add_sa - translate VF request to SA add
  730. * @adapter: board private structure
  731. * @msgbuf: The message buffer
  732. * @vf: the VF index
  733. *
  734. * Make up a new xs and algorithm info from the data sent by the VF.
  735. * We only need to sketch in just enough to set up the HW offload.
  736. * Put the resulting offload_handle into the return message to the VF.
  737. *
  738. * Returns 0 or error value
  739. **/
  740. int ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
  741. {
  742. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  743. struct xfrm_algo_desc *algo;
  744. struct sa_mbx_msg *sam;
  745. struct xfrm_state *xs;
  746. size_t aead_len;
  747. u16 sa_idx;
  748. u32 pfsa;
  749. int err;
  750. sam = (struct sa_mbx_msg *)(&msgbuf[1]);
  751. if (!adapter->vfinfo[vf].trusted) {
  752. e_warn(drv, "VF %d attempted to add an IPsec SA\n", vf);
  753. err = -EACCES;
  754. goto err_out;
  755. }
  756. /* Tx IPsec offload doesn't seem to work on this
  757. * device, so block these requests for now.
  758. */
  759. if (!(sam->flags & XFRM_OFFLOAD_INBOUND)) {
  760. err = -ENXIO;
  761. goto err_out;
  762. }
  763. xs = kzalloc(sizeof(*xs), GFP_KERNEL);
  764. if (unlikely(!xs)) {
  765. err = -ENOMEM;
  766. goto err_out;
  767. }
  768. xs->xso.flags = sam->flags;
  769. xs->id.spi = sam->spi;
  770. xs->id.proto = sam->proto;
  771. xs->props.family = sam->family;
  772. if (xs->props.family == AF_INET6)
  773. memcpy(&xs->id.daddr.a6, sam->addr, sizeof(xs->id.daddr.a6));
  774. else
  775. memcpy(&xs->id.daddr.a4, sam->addr, sizeof(xs->id.daddr.a4));
  776. xs->xso.dev = adapter->netdev;
  777. algo = xfrm_aead_get_byname(aes_gcm_name, IXGBE_IPSEC_AUTH_BITS, 1);
  778. if (unlikely(!algo)) {
  779. err = -ENOENT;
  780. goto err_xs;
  781. }
  782. aead_len = sizeof(*xs->aead) + IXGBE_IPSEC_KEY_BITS / 8;
  783. xs->aead = kzalloc(aead_len, GFP_KERNEL);
  784. if (unlikely(!xs->aead)) {
  785. err = -ENOMEM;
  786. goto err_xs;
  787. }
  788. xs->props.ealgo = algo->desc.sadb_alg_id;
  789. xs->geniv = algo->uinfo.aead.geniv;
  790. xs->aead->alg_icv_len = IXGBE_IPSEC_AUTH_BITS;
  791. xs->aead->alg_key_len = IXGBE_IPSEC_KEY_BITS;
  792. memcpy(xs->aead->alg_key, sam->key, sizeof(sam->key));
  793. memcpy(xs->aead->alg_name, aes_gcm_name, sizeof(aes_gcm_name));
  794. /* set up the HW offload */
  795. err = ixgbe_ipsec_add_sa(xs);
  796. if (err)
  797. goto err_aead;
  798. pfsa = xs->xso.offload_handle;
  799. if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
  800. sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
  801. ipsec->rx_tbl[sa_idx].vf = vf;
  802. ipsec->rx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
  803. } else {
  804. sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
  805. ipsec->tx_tbl[sa_idx].vf = vf;
  806. ipsec->tx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
  807. }
  808. msgbuf[1] = xs->xso.offload_handle;
  809. return 0;
  810. err_aead:
  811. memset(xs->aead, 0, sizeof(*xs->aead));
  812. kfree(xs->aead);
  813. err_xs:
  814. memset(xs, 0, sizeof(*xs));
  815. kfree(xs);
  816. err_out:
  817. msgbuf[1] = err;
  818. return err;
  819. }
  820. /**
  821. * ixgbe_ipsec_vf_del_sa - translate VF request to SA delete
  822. * @adapter: board private structure
  823. * @msgbuf: The message buffer
  824. * @vf: the VF index
  825. *
  826. * Given the offload_handle sent by the VF, look for the related SA table
  827. * entry and use its xs field to call for a delete of the SA.
  828. *
  829. * Note: We silently ignore requests to delete entries that are already
  830. * set to unused because when a VF is set to "DOWN", the PF first
  831. * gets a reset and clears all the VF's entries; then the VF's
  832. * XFRM stack sends individual deletes for each entry, which the
  833. * reset already removed. In the future it might be good to try to
  834. * optimize this so not so many unnecessary delete messages are sent.
  835. *
  836. * Returns 0 or error value
  837. **/
  838. int ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
  839. {
  840. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  841. struct xfrm_state *xs;
  842. u32 pfsa = msgbuf[1];
  843. u16 sa_idx;
  844. if (!adapter->vfinfo[vf].trusted) {
  845. e_err(drv, "vf %d attempted to delete an SA\n", vf);
  846. return -EPERM;
  847. }
  848. if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
  849. struct rx_sa *rsa;
  850. sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
  851. if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
  852. e_err(drv, "vf %d SA index %d out of range\n",
  853. vf, sa_idx);
  854. return -EINVAL;
  855. }
  856. rsa = &ipsec->rx_tbl[sa_idx];
  857. if (!rsa->used)
  858. return 0;
  859. if (!(rsa->mode & IXGBE_RXTXMOD_VF) ||
  860. rsa->vf != vf) {
  861. e_err(drv, "vf %d bad Rx SA index %d\n", vf, sa_idx);
  862. return -ENOENT;
  863. }
  864. xs = ipsec->rx_tbl[sa_idx].xs;
  865. } else {
  866. struct tx_sa *tsa;
  867. sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
  868. if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
  869. e_err(drv, "vf %d SA index %d out of range\n",
  870. vf, sa_idx);
  871. return -EINVAL;
  872. }
  873. tsa = &ipsec->tx_tbl[sa_idx];
  874. if (!tsa->used)
  875. return 0;
  876. if (!(tsa->mode & IXGBE_RXTXMOD_VF) ||
  877. tsa->vf != vf) {
  878. e_err(drv, "vf %d bad Tx SA index %d\n", vf, sa_idx);
  879. return -ENOENT;
  880. }
  881. xs = ipsec->tx_tbl[sa_idx].xs;
  882. }
  883. ixgbe_ipsec_del_sa(xs);
  884. /* remove the xs that was made-up in the add request */
  885. memset(xs, 0, sizeof(*xs));
  886. kfree(xs);
  887. return 0;
  888. }
  889. /**
  890. * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
  891. * @tx_ring: outgoing context
  892. * @first: current data packet
  893. * @itd: ipsec Tx data for later use in building context descriptor
  894. **/
  895. int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
  896. struct ixgbe_tx_buffer *first,
  897. struct ixgbe_ipsec_tx_data *itd)
  898. {
  899. struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
  900. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  901. struct xfrm_state *xs;
  902. struct tx_sa *tsa;
  903. if (unlikely(!first->skb->sp->len)) {
  904. netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
  905. __func__, first->skb->sp->len);
  906. return 0;
  907. }
  908. xs = xfrm_input_state(first->skb);
  909. if (unlikely(!xs)) {
  910. netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
  911. __func__, xs);
  912. return 0;
  913. }
  914. itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
  915. if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
  916. netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
  917. __func__, itd->sa_idx, xs->xso.offload_handle);
  918. return 0;
  919. }
  920. tsa = &ipsec->tx_tbl[itd->sa_idx];
  921. if (unlikely(!tsa->used)) {
  922. netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
  923. __func__, itd->sa_idx);
  924. return 0;
  925. }
  926. first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
  927. if (xs->id.proto == IPPROTO_ESP) {
  928. itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
  929. IXGBE_ADVTXD_TUCMD_L4T_TCP;
  930. if (first->protocol == htons(ETH_P_IP))
  931. itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
  932. /* The actual trailer length is authlen (16 bytes) plus
  933. * 2 bytes for the proto and the padlen values, plus
  934. * padlen bytes of padding. This ends up not the same
  935. * as the static value found in xs->props.trailer_len (21).
  936. *
  937. * ... but if we're doing GSO, don't bother as the stack
  938. * doesn't add a trailer for those.
  939. */
  940. if (!skb_is_gso(first->skb)) {
  941. /* The "correct" way to get the auth length would be
  942. * to use
  943. * authlen = crypto_aead_authsize(xs->data);
  944. * but since we know we only have one size to worry
  945. * about * we can let the compiler use the constant
  946. * and save us a few CPU cycles.
  947. */
  948. const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
  949. struct sk_buff *skb = first->skb;
  950. u8 padlen;
  951. int ret;
  952. ret = skb_copy_bits(skb, skb->len - (authlen + 2),
  953. &padlen, 1);
  954. if (unlikely(ret))
  955. return 0;
  956. itd->trailer_len = authlen + 2 + padlen;
  957. }
  958. }
  959. if (tsa->encrypt)
  960. itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
  961. return 1;
  962. }
  963. /**
  964. * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
  965. * @rx_ring: receiving ring
  966. * @rx_desc: receive data descriptor
  967. * @skb: current data packet
  968. *
  969. * Determine if there was an ipsec encapsulation noticed, and if so set up
  970. * the resulting status for later in the receive stack.
  971. **/
  972. void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
  973. union ixgbe_adv_rx_desc *rx_desc,
  974. struct sk_buff *skb)
  975. {
  976. struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
  977. __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
  978. __le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
  979. IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
  980. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  981. struct xfrm_offload *xo = NULL;
  982. struct xfrm_state *xs = NULL;
  983. struct ipv6hdr *ip6 = NULL;
  984. struct iphdr *ip4 = NULL;
  985. void *daddr;
  986. __be32 spi;
  987. u8 *c_hdr;
  988. u8 proto;
  989. /* Find the ip and crypto headers in the data.
  990. * We can assume no vlan header in the way, b/c the
  991. * hw won't recognize the IPsec packet and anyway the
  992. * currently vlan device doesn't support xfrm offload.
  993. */
  994. if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
  995. ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
  996. daddr = &ip4->daddr;
  997. c_hdr = (u8 *)ip4 + ip4->ihl * 4;
  998. } else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
  999. ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  1000. daddr = &ip6->daddr;
  1001. c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
  1002. } else {
  1003. return;
  1004. }
  1005. switch (pkt_info & ipsec_pkt_types) {
  1006. case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
  1007. spi = ((struct ip_auth_hdr *)c_hdr)->spi;
  1008. proto = IPPROTO_AH;
  1009. break;
  1010. case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
  1011. spi = ((struct ip_esp_hdr *)c_hdr)->spi;
  1012. proto = IPPROTO_ESP;
  1013. break;
  1014. default:
  1015. return;
  1016. }
  1017. xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
  1018. if (unlikely(!xs))
  1019. return;
  1020. skb->sp = secpath_dup(skb->sp);
  1021. if (unlikely(!skb->sp))
  1022. return;
  1023. skb->sp->xvec[skb->sp->len++] = xs;
  1024. skb->sp->olen++;
  1025. xo = xfrm_offload(skb);
  1026. xo->flags = CRYPTO_DONE;
  1027. xo->status = CRYPTO_SUCCESS;
  1028. adapter->rx_ipsec++;
  1029. }
  1030. /**
  1031. * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
  1032. * @adapter: board private structure
  1033. **/
  1034. void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
  1035. {
  1036. struct ixgbe_hw *hw = &adapter->hw;
  1037. struct ixgbe_ipsec *ipsec;
  1038. u32 t_dis, r_dis;
  1039. size_t size;
  1040. if (hw->mac.type == ixgbe_mac_82598EB)
  1041. return;
  1042. /* If there is no support for either Tx or Rx offload
  1043. * we should not be advertising support for IPsec.
  1044. */
  1045. t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
  1046. IXGBE_SECTXSTAT_SECTX_OFF_DIS;
  1047. r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
  1048. IXGBE_SECRXSTAT_SECRX_OFF_DIS;
  1049. if (t_dis || r_dis)
  1050. return;
  1051. ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
  1052. if (!ipsec)
  1053. goto err1;
  1054. hash_init(ipsec->rx_sa_list);
  1055. size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
  1056. ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
  1057. if (!ipsec->rx_tbl)
  1058. goto err2;
  1059. size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
  1060. ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
  1061. if (!ipsec->tx_tbl)
  1062. goto err2;
  1063. size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
  1064. ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
  1065. if (!ipsec->ip_tbl)
  1066. goto err2;
  1067. ipsec->num_rx_sa = 0;
  1068. ipsec->num_tx_sa = 0;
  1069. adapter->ipsec = ipsec;
  1070. ixgbe_ipsec_stop_engine(adapter);
  1071. ixgbe_ipsec_clear_hw_tables(adapter);
  1072. adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
  1073. return;
  1074. err2:
  1075. kfree(ipsec->ip_tbl);
  1076. kfree(ipsec->rx_tbl);
  1077. kfree(ipsec->tx_tbl);
  1078. kfree(ipsec);
  1079. err1:
  1080. netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
  1081. }
  1082. /**
  1083. * ixgbe_stop_ipsec_offload - tear down the ipsec offload
  1084. * @adapter: board private structure
  1085. **/
  1086. void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
  1087. {
  1088. struct ixgbe_ipsec *ipsec = adapter->ipsec;
  1089. adapter->ipsec = NULL;
  1090. if (ipsec) {
  1091. kfree(ipsec->ip_tbl);
  1092. kfree(ipsec->rx_tbl);
  1093. kfree(ipsec->tx_tbl);
  1094. kfree(ipsec);
  1095. }
  1096. }