ixgbe_ipsec.c 34 KB

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