chtls_cm.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. /*
  2. * Copyright (c) 2018 Chelsio Communications, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Written by: Atul Gupta (atul.gupta@chelsio.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/timer.h>
  15. #include <linux/notifier.h>
  16. #include <linux/inetdevice.h>
  17. #include <linux/ip.h>
  18. #include <linux/tcp.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/if_vlan.h>
  23. #include <net/tcp.h>
  24. #include <net/dst.h>
  25. #include "chtls.h"
  26. #include "chtls_cm.h"
  27. /*
  28. * State transitions and actions for close. Note that if we are in SYN_SENT
  29. * we remain in that state as we cannot control a connection while it's in
  30. * SYN_SENT; such connections are allowed to establish and are then aborted.
  31. */
  32. static unsigned char new_state[16] = {
  33. /* current state: new state: action: */
  34. /* (Invalid) */ TCP_CLOSE,
  35. /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  36. /* TCP_SYN_SENT */ TCP_SYN_SENT,
  37. /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  38. /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1,
  39. /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2,
  40. /* TCP_TIME_WAIT */ TCP_CLOSE,
  41. /* TCP_CLOSE */ TCP_CLOSE,
  42. /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN,
  43. /* TCP_LAST_ACK */ TCP_LAST_ACK,
  44. /* TCP_LISTEN */ TCP_CLOSE,
  45. /* TCP_CLOSING */ TCP_CLOSING,
  46. };
  47. static struct chtls_sock *chtls_sock_create(struct chtls_dev *cdev)
  48. {
  49. struct chtls_sock *csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
  50. if (!csk)
  51. return NULL;
  52. csk->txdata_skb_cache = alloc_skb(TXDATA_SKB_LEN, GFP_ATOMIC);
  53. if (!csk->txdata_skb_cache) {
  54. kfree(csk);
  55. return NULL;
  56. }
  57. kref_init(&csk->kref);
  58. csk->cdev = cdev;
  59. skb_queue_head_init(&csk->txq);
  60. csk->wr_skb_head = NULL;
  61. csk->wr_skb_tail = NULL;
  62. csk->mss = MAX_MSS;
  63. csk->tlshws.ofld = 1;
  64. csk->tlshws.txkey = -1;
  65. csk->tlshws.rxkey = -1;
  66. csk->tlshws.mfs = TLS_MFS;
  67. skb_queue_head_init(&csk->tlshws.sk_recv_queue);
  68. return csk;
  69. }
  70. static void chtls_sock_release(struct kref *ref)
  71. {
  72. struct chtls_sock *csk =
  73. container_of(ref, struct chtls_sock, kref);
  74. kfree(csk);
  75. }
  76. static struct net_device *chtls_ipv4_netdev(struct chtls_dev *cdev,
  77. struct sock *sk)
  78. {
  79. struct net_device *ndev = cdev->ports[0];
  80. if (likely(!inet_sk(sk)->inet_rcv_saddr))
  81. return ndev;
  82. ndev = ip_dev_find(&init_net, inet_sk(sk)->inet_rcv_saddr);
  83. if (!ndev)
  84. return NULL;
  85. if (is_vlan_dev(ndev))
  86. return vlan_dev_real_dev(ndev);
  87. return ndev;
  88. }
  89. static void assign_rxopt(struct sock *sk, unsigned int opt)
  90. {
  91. const struct chtls_dev *cdev;
  92. struct chtls_sock *csk;
  93. struct tcp_sock *tp;
  94. csk = rcu_dereference_sk_user_data(sk);
  95. tp = tcp_sk(sk);
  96. cdev = csk->cdev;
  97. tp->tcp_header_len = sizeof(struct tcphdr);
  98. tp->rx_opt.mss_clamp = cdev->mtus[TCPOPT_MSS_G(opt)] - 40;
  99. tp->mss_cache = tp->rx_opt.mss_clamp;
  100. tp->rx_opt.tstamp_ok = TCPOPT_TSTAMP_G(opt);
  101. tp->rx_opt.snd_wscale = TCPOPT_SACK_G(opt);
  102. tp->rx_opt.wscale_ok = TCPOPT_WSCALE_OK_G(opt);
  103. SND_WSCALE(tp) = TCPOPT_SND_WSCALE_G(opt);
  104. if (!tp->rx_opt.wscale_ok)
  105. tp->rx_opt.rcv_wscale = 0;
  106. if (tp->rx_opt.tstamp_ok) {
  107. tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED;
  108. tp->rx_opt.mss_clamp -= TCPOLEN_TSTAMP_ALIGNED;
  109. } else if (csk->opt2 & TSTAMPS_EN_F) {
  110. csk->opt2 &= ~TSTAMPS_EN_F;
  111. csk->mtu_idx = TCPOPT_MSS_G(opt);
  112. }
  113. }
  114. static void chtls_purge_receive_queue(struct sock *sk)
  115. {
  116. struct sk_buff *skb;
  117. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  118. skb_dst_set(skb, (void *)NULL);
  119. kfree_skb(skb);
  120. }
  121. }
  122. static void chtls_purge_write_queue(struct sock *sk)
  123. {
  124. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  125. struct sk_buff *skb;
  126. while ((skb = __skb_dequeue(&csk->txq))) {
  127. sk->sk_wmem_queued -= skb->truesize;
  128. __kfree_skb(skb);
  129. }
  130. }
  131. static void chtls_purge_recv_queue(struct sock *sk)
  132. {
  133. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  134. struct chtls_hws *tlsk = &csk->tlshws;
  135. struct sk_buff *skb;
  136. while ((skb = __skb_dequeue(&tlsk->sk_recv_queue)) != NULL) {
  137. skb_dst_set(skb, NULL);
  138. kfree_skb(skb);
  139. }
  140. }
  141. static void abort_arp_failure(void *handle, struct sk_buff *skb)
  142. {
  143. struct cpl_abort_req *req = cplhdr(skb);
  144. struct chtls_dev *cdev;
  145. cdev = (struct chtls_dev *)handle;
  146. req->cmd = CPL_ABORT_NO_RST;
  147. cxgb4_ofld_send(cdev->lldi->ports[0], skb);
  148. }
  149. static struct sk_buff *alloc_ctrl_skb(struct sk_buff *skb, int len)
  150. {
  151. if (likely(skb && !skb_shared(skb) && !skb_cloned(skb))) {
  152. __skb_trim(skb, 0);
  153. refcount_inc(&skb->users);
  154. } else {
  155. skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
  156. }
  157. return skb;
  158. }
  159. static void chtls_send_abort(struct sock *sk, int mode, struct sk_buff *skb)
  160. {
  161. struct cpl_abort_req *req;
  162. struct chtls_sock *csk;
  163. struct tcp_sock *tp;
  164. csk = rcu_dereference_sk_user_data(sk);
  165. tp = tcp_sk(sk);
  166. if (!skb)
  167. skb = alloc_ctrl_skb(csk->txdata_skb_cache, sizeof(*req));
  168. req = (struct cpl_abort_req *)skb_put(skb, sizeof(*req));
  169. INIT_TP_WR_CPL(req, CPL_ABORT_REQ, csk->tid);
  170. skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
  171. req->rsvd0 = htonl(tp->snd_nxt);
  172. req->rsvd1 = !csk_flag_nochk(csk, CSK_TX_DATA_SENT);
  173. req->cmd = mode;
  174. t4_set_arp_err_handler(skb, csk->cdev, abort_arp_failure);
  175. send_or_defer(sk, tp, skb, mode == CPL_ABORT_SEND_RST);
  176. }
  177. static void chtls_send_reset(struct sock *sk, int mode, struct sk_buff *skb)
  178. {
  179. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  180. if (unlikely(csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) ||
  181. !csk->cdev)) {
  182. if (sk->sk_state == TCP_SYN_RECV)
  183. csk_set_flag(csk, CSK_RST_ABORTED);
  184. goto out;
  185. }
  186. if (!csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
  187. struct tcp_sock *tp = tcp_sk(sk);
  188. if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
  189. WARN_ONCE(1, "send tx flowc error");
  190. csk_set_flag(csk, CSK_TX_DATA_SENT);
  191. }
  192. csk_set_flag(csk, CSK_ABORT_RPL_PENDING);
  193. chtls_purge_write_queue(sk);
  194. csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
  195. if (sk->sk_state != TCP_SYN_RECV)
  196. chtls_send_abort(sk, mode, skb);
  197. else
  198. goto out;
  199. return;
  200. out:
  201. if (skb)
  202. kfree_skb(skb);
  203. }
  204. static void release_tcp_port(struct sock *sk)
  205. {
  206. if (inet_csk(sk)->icsk_bind_hash)
  207. inet_put_port(sk);
  208. }
  209. static void tcp_uncork(struct sock *sk)
  210. {
  211. struct tcp_sock *tp = tcp_sk(sk);
  212. if (tp->nonagle & TCP_NAGLE_CORK) {
  213. tp->nonagle &= ~TCP_NAGLE_CORK;
  214. chtls_tcp_push(sk, 0);
  215. }
  216. }
  217. static void chtls_close_conn(struct sock *sk)
  218. {
  219. struct cpl_close_con_req *req;
  220. struct chtls_sock *csk;
  221. struct sk_buff *skb;
  222. unsigned int tid;
  223. unsigned int len;
  224. len = roundup(sizeof(struct cpl_close_con_req), 16);
  225. csk = rcu_dereference_sk_user_data(sk);
  226. tid = csk->tid;
  227. skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
  228. req = (struct cpl_close_con_req *)__skb_put(skb, len);
  229. memset(req, 0, len);
  230. req->wr.wr_hi = htonl(FW_WR_OP_V(FW_TP_WR) |
  231. FW_WR_IMMDLEN_V(sizeof(*req) -
  232. sizeof(req->wr)));
  233. req->wr.wr_mid = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)) |
  234. FW_WR_FLOWID_V(tid));
  235. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
  236. tcp_uncork(sk);
  237. skb_entail(sk, skb, ULPCB_FLAG_NO_HDR | ULPCB_FLAG_NO_APPEND);
  238. if (sk->sk_state != TCP_SYN_SENT)
  239. chtls_push_frames(csk, 1);
  240. }
  241. /*
  242. * Perform a state transition during close and return the actions indicated
  243. * for the transition. Do not make this function inline, the main reason
  244. * it exists at all is to avoid multiple inlining of tcp_set_state.
  245. */
  246. static int make_close_transition(struct sock *sk)
  247. {
  248. int next = (int)new_state[sk->sk_state];
  249. tcp_set_state(sk, next & TCP_STATE_MASK);
  250. return next & TCP_ACTION_FIN;
  251. }
  252. void chtls_close(struct sock *sk, long timeout)
  253. {
  254. int data_lost, prev_state;
  255. struct chtls_sock *csk;
  256. csk = rcu_dereference_sk_user_data(sk);
  257. lock_sock(sk);
  258. sk->sk_shutdown |= SHUTDOWN_MASK;
  259. data_lost = skb_queue_len(&sk->sk_receive_queue);
  260. data_lost |= skb_queue_len(&csk->tlshws.sk_recv_queue);
  261. chtls_purge_recv_queue(sk);
  262. chtls_purge_receive_queue(sk);
  263. if (sk->sk_state == TCP_CLOSE) {
  264. goto wait;
  265. } else if (data_lost || sk->sk_state == TCP_SYN_SENT) {
  266. chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
  267. release_tcp_port(sk);
  268. goto unlock;
  269. } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  270. sk->sk_prot->disconnect(sk, 0);
  271. } else if (make_close_transition(sk)) {
  272. chtls_close_conn(sk);
  273. }
  274. wait:
  275. if (timeout)
  276. sk_stream_wait_close(sk, timeout);
  277. unlock:
  278. prev_state = sk->sk_state;
  279. sock_hold(sk);
  280. sock_orphan(sk);
  281. release_sock(sk);
  282. local_bh_disable();
  283. bh_lock_sock(sk);
  284. if (prev_state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
  285. goto out;
  286. if (sk->sk_state == TCP_FIN_WAIT2 && tcp_sk(sk)->linger2 < 0 &&
  287. !csk_flag(sk, CSK_ABORT_SHUTDOWN)) {
  288. struct sk_buff *skb;
  289. skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
  290. if (skb)
  291. chtls_send_reset(sk, CPL_ABORT_SEND_RST, skb);
  292. }
  293. if (sk->sk_state == TCP_CLOSE)
  294. inet_csk_destroy_sock(sk);
  295. out:
  296. bh_unlock_sock(sk);
  297. local_bh_enable();
  298. sock_put(sk);
  299. }
  300. /*
  301. * Wait until a socket enters on of the given states.
  302. */
  303. static int wait_for_states(struct sock *sk, unsigned int states)
  304. {
  305. DECLARE_WAITQUEUE(wait, current);
  306. struct socket_wq _sk_wq;
  307. long current_timeo;
  308. int err = 0;
  309. current_timeo = 200;
  310. /*
  311. * We want this to work even when there's no associated struct socket.
  312. * In that case we provide a temporary wait_queue_head_t.
  313. */
  314. if (!sk->sk_wq) {
  315. init_waitqueue_head(&_sk_wq.wait);
  316. _sk_wq.fasync_list = NULL;
  317. init_rcu_head_on_stack(&_sk_wq.rcu);
  318. RCU_INIT_POINTER(sk->sk_wq, &_sk_wq);
  319. }
  320. add_wait_queue(sk_sleep(sk), &wait);
  321. while (!sk_in_state(sk, states)) {
  322. if (!current_timeo) {
  323. err = -EBUSY;
  324. break;
  325. }
  326. if (signal_pending(current)) {
  327. err = sock_intr_errno(current_timeo);
  328. break;
  329. }
  330. set_current_state(TASK_UNINTERRUPTIBLE);
  331. release_sock(sk);
  332. if (!sk_in_state(sk, states))
  333. current_timeo = schedule_timeout(current_timeo);
  334. __set_current_state(TASK_RUNNING);
  335. lock_sock(sk);
  336. }
  337. remove_wait_queue(sk_sleep(sk), &wait);
  338. if (rcu_dereference(sk->sk_wq) == &_sk_wq)
  339. sk->sk_wq = NULL;
  340. return err;
  341. }
  342. int chtls_disconnect(struct sock *sk, int flags)
  343. {
  344. struct chtls_sock *csk;
  345. struct tcp_sock *tp;
  346. int err;
  347. tp = tcp_sk(sk);
  348. csk = rcu_dereference_sk_user_data(sk);
  349. chtls_purge_recv_queue(sk);
  350. chtls_purge_receive_queue(sk);
  351. chtls_purge_write_queue(sk);
  352. if (sk->sk_state != TCP_CLOSE) {
  353. sk->sk_err = ECONNRESET;
  354. chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
  355. err = wait_for_states(sk, TCPF_CLOSE);
  356. if (err)
  357. return err;
  358. }
  359. chtls_purge_recv_queue(sk);
  360. chtls_purge_receive_queue(sk);
  361. tp->max_window = 0xFFFF << (tp->rx_opt.snd_wscale);
  362. return tcp_disconnect(sk, flags);
  363. }
  364. #define SHUTDOWN_ELIGIBLE_STATE (TCPF_ESTABLISHED | \
  365. TCPF_SYN_RECV | TCPF_CLOSE_WAIT)
  366. void chtls_shutdown(struct sock *sk, int how)
  367. {
  368. if ((how & SEND_SHUTDOWN) &&
  369. sk_in_state(sk, SHUTDOWN_ELIGIBLE_STATE) &&
  370. make_close_transition(sk))
  371. chtls_close_conn(sk);
  372. }
  373. void chtls_destroy_sock(struct sock *sk)
  374. {
  375. struct chtls_sock *csk;
  376. csk = rcu_dereference_sk_user_data(sk);
  377. chtls_purge_recv_queue(sk);
  378. csk->ulp_mode = ULP_MODE_NONE;
  379. chtls_purge_write_queue(sk);
  380. free_tls_keyid(sk);
  381. kref_put(&csk->kref, chtls_sock_release);
  382. sk->sk_prot = &tcp_prot;
  383. sk->sk_prot->destroy(sk);
  384. }
  385. static void reset_listen_child(struct sock *child)
  386. {
  387. struct chtls_sock *csk = rcu_dereference_sk_user_data(child);
  388. struct sk_buff *skb;
  389. skb = alloc_ctrl_skb(csk->txdata_skb_cache,
  390. sizeof(struct cpl_abort_req));
  391. chtls_send_reset(child, CPL_ABORT_SEND_RST, skb);
  392. sock_orphan(child);
  393. INC_ORPHAN_COUNT(child);
  394. if (child->sk_state == TCP_CLOSE)
  395. inet_csk_destroy_sock(child);
  396. }
  397. static void chtls_disconnect_acceptq(struct sock *listen_sk)
  398. {
  399. struct request_sock **pprev;
  400. pprev = ACCEPT_QUEUE(listen_sk);
  401. while (*pprev) {
  402. struct request_sock *req = *pprev;
  403. if (req->rsk_ops == &chtls_rsk_ops) {
  404. struct sock *child = req->sk;
  405. *pprev = req->dl_next;
  406. sk_acceptq_removed(listen_sk);
  407. reqsk_put(req);
  408. sock_hold(child);
  409. local_bh_disable();
  410. bh_lock_sock(child);
  411. release_tcp_port(child);
  412. reset_listen_child(child);
  413. bh_unlock_sock(child);
  414. local_bh_enable();
  415. sock_put(child);
  416. } else {
  417. pprev = &req->dl_next;
  418. }
  419. }
  420. }
  421. static int listen_hashfn(const struct sock *sk)
  422. {
  423. return ((unsigned long)sk >> 10) & (LISTEN_INFO_HASH_SIZE - 1);
  424. }
  425. static struct listen_info *listen_hash_add(struct chtls_dev *cdev,
  426. struct sock *sk,
  427. unsigned int stid)
  428. {
  429. struct listen_info *p = kmalloc(sizeof(*p), GFP_KERNEL);
  430. if (p) {
  431. int key = listen_hashfn(sk);
  432. p->sk = sk;
  433. p->stid = stid;
  434. spin_lock(&cdev->listen_lock);
  435. p->next = cdev->listen_hash_tab[key];
  436. cdev->listen_hash_tab[key] = p;
  437. spin_unlock(&cdev->listen_lock);
  438. }
  439. return p;
  440. }
  441. static int listen_hash_find(struct chtls_dev *cdev,
  442. struct sock *sk)
  443. {
  444. struct listen_info *p;
  445. int stid = -1;
  446. int key;
  447. key = listen_hashfn(sk);
  448. spin_lock(&cdev->listen_lock);
  449. for (p = cdev->listen_hash_tab[key]; p; p = p->next)
  450. if (p->sk == sk) {
  451. stid = p->stid;
  452. break;
  453. }
  454. spin_unlock(&cdev->listen_lock);
  455. return stid;
  456. }
  457. static int listen_hash_del(struct chtls_dev *cdev,
  458. struct sock *sk)
  459. {
  460. struct listen_info *p, **prev;
  461. int stid = -1;
  462. int key;
  463. key = listen_hashfn(sk);
  464. prev = &cdev->listen_hash_tab[key];
  465. spin_lock(&cdev->listen_lock);
  466. for (p = *prev; p; prev = &p->next, p = p->next)
  467. if (p->sk == sk) {
  468. stid = p->stid;
  469. *prev = p->next;
  470. kfree(p);
  471. break;
  472. }
  473. spin_unlock(&cdev->listen_lock);
  474. return stid;
  475. }
  476. static void cleanup_syn_rcv_conn(struct sock *child, struct sock *parent)
  477. {
  478. struct request_sock *req;
  479. struct chtls_sock *csk;
  480. csk = rcu_dereference_sk_user_data(child);
  481. req = csk->passive_reap_next;
  482. reqsk_queue_removed(&inet_csk(parent)->icsk_accept_queue, req);
  483. __skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
  484. chtls_reqsk_free(req);
  485. csk->passive_reap_next = NULL;
  486. }
  487. static void chtls_reset_synq(struct listen_ctx *listen_ctx)
  488. {
  489. struct sock *listen_sk = listen_ctx->lsk;
  490. while (!skb_queue_empty(&listen_ctx->synq)) {
  491. struct chtls_sock *csk =
  492. container_of((struct synq *)skb_peek
  493. (&listen_ctx->synq), struct chtls_sock, synq);
  494. struct sock *child = csk->sk;
  495. cleanup_syn_rcv_conn(child, listen_sk);
  496. sock_hold(child);
  497. local_bh_disable();
  498. bh_lock_sock(child);
  499. release_tcp_port(child);
  500. reset_listen_child(child);
  501. bh_unlock_sock(child);
  502. local_bh_enable();
  503. sock_put(child);
  504. }
  505. }
  506. int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
  507. {
  508. struct net_device *ndev;
  509. struct listen_ctx *ctx;
  510. struct adapter *adap;
  511. struct port_info *pi;
  512. int stid;
  513. int ret;
  514. if (sk->sk_family != PF_INET)
  515. return -EAGAIN;
  516. rcu_read_lock();
  517. ndev = chtls_ipv4_netdev(cdev, sk);
  518. rcu_read_unlock();
  519. if (!ndev)
  520. return -EBADF;
  521. pi = netdev_priv(ndev);
  522. adap = pi->adapter;
  523. if (!(adap->flags & FULL_INIT_DONE))
  524. return -EBADF;
  525. if (listen_hash_find(cdev, sk) >= 0) /* already have it */
  526. return -EADDRINUSE;
  527. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  528. if (!ctx)
  529. return -ENOMEM;
  530. __module_get(THIS_MODULE);
  531. ctx->lsk = sk;
  532. ctx->cdev = cdev;
  533. ctx->state = T4_LISTEN_START_PENDING;
  534. skb_queue_head_init(&ctx->synq);
  535. stid = cxgb4_alloc_stid(cdev->tids, sk->sk_family, ctx);
  536. if (stid < 0)
  537. goto free_ctx;
  538. sock_hold(sk);
  539. if (!listen_hash_add(cdev, sk, stid))
  540. goto free_stid;
  541. ret = cxgb4_create_server(ndev, stid,
  542. inet_sk(sk)->inet_rcv_saddr,
  543. inet_sk(sk)->inet_sport, 0,
  544. cdev->lldi->rxq_ids[0]);
  545. if (ret > 0)
  546. ret = net_xmit_errno(ret);
  547. if (ret)
  548. goto del_hash;
  549. return 0;
  550. del_hash:
  551. listen_hash_del(cdev, sk);
  552. free_stid:
  553. cxgb4_free_stid(cdev->tids, stid, sk->sk_family);
  554. sock_put(sk);
  555. free_ctx:
  556. kfree(ctx);
  557. module_put(THIS_MODULE);
  558. return -EBADF;
  559. }
  560. void chtls_listen_stop(struct chtls_dev *cdev, struct sock *sk)
  561. {
  562. struct listen_ctx *listen_ctx;
  563. int stid;
  564. stid = listen_hash_del(cdev, sk);
  565. if (stid < 0)
  566. return;
  567. listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
  568. chtls_reset_synq(listen_ctx);
  569. cxgb4_remove_server(cdev->lldi->ports[0], stid,
  570. cdev->lldi->rxq_ids[0], 0);
  571. chtls_disconnect_acceptq(sk);
  572. }
  573. static int chtls_pass_open_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
  574. {
  575. struct cpl_pass_open_rpl *rpl = cplhdr(skb) + RSS_HDR;
  576. unsigned int stid = GET_TID(rpl);
  577. struct listen_ctx *listen_ctx;
  578. listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
  579. if (!listen_ctx)
  580. return CPL_RET_BUF_DONE;
  581. if (listen_ctx->state == T4_LISTEN_START_PENDING) {
  582. listen_ctx->state = T4_LISTEN_STARTED;
  583. return CPL_RET_BUF_DONE;
  584. }
  585. if (rpl->status != CPL_ERR_NONE) {
  586. pr_info("Unexpected PASS_OPEN_RPL status %u for STID %u\n",
  587. rpl->status, stid);
  588. } else {
  589. cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
  590. sock_put(listen_ctx->lsk);
  591. kfree(listen_ctx);
  592. module_put(THIS_MODULE);
  593. }
  594. return CPL_RET_BUF_DONE;
  595. }
  596. static int chtls_close_listsrv_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
  597. {
  598. struct cpl_close_listsvr_rpl *rpl = cplhdr(skb) + RSS_HDR;
  599. struct listen_ctx *listen_ctx;
  600. unsigned int stid;
  601. void *data;
  602. stid = GET_TID(rpl);
  603. data = lookup_stid(cdev->tids, stid);
  604. listen_ctx = (struct listen_ctx *)data;
  605. if (rpl->status != CPL_ERR_NONE) {
  606. pr_info("Unexpected CLOSE_LISTSRV_RPL status %u for STID %u\n",
  607. rpl->status, stid);
  608. } else {
  609. cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
  610. sock_put(listen_ctx->lsk);
  611. kfree(listen_ctx);
  612. module_put(THIS_MODULE);
  613. }
  614. return CPL_RET_BUF_DONE;
  615. }
  616. static void chtls_purge_wr_queue(struct sock *sk)
  617. {
  618. struct sk_buff *skb;
  619. while ((skb = dequeue_wr(sk)) != NULL)
  620. kfree_skb(skb);
  621. }
  622. static void chtls_release_resources(struct sock *sk)
  623. {
  624. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  625. struct chtls_dev *cdev = csk->cdev;
  626. unsigned int tid = csk->tid;
  627. struct tid_info *tids;
  628. if (!cdev)
  629. return;
  630. tids = cdev->tids;
  631. kfree_skb(csk->txdata_skb_cache);
  632. csk->txdata_skb_cache = NULL;
  633. if (csk->wr_credits != csk->wr_max_credits) {
  634. chtls_purge_wr_queue(sk);
  635. chtls_reset_wr_list(csk);
  636. }
  637. if (csk->l2t_entry) {
  638. cxgb4_l2t_release(csk->l2t_entry);
  639. csk->l2t_entry = NULL;
  640. }
  641. cxgb4_remove_tid(tids, csk->port_id, tid, sk->sk_family);
  642. sock_put(sk);
  643. }
  644. static void chtls_conn_done(struct sock *sk)
  645. {
  646. if (sock_flag(sk, SOCK_DEAD))
  647. chtls_purge_receive_queue(sk);
  648. sk_wakeup_sleepers(sk, 0);
  649. tcp_done(sk);
  650. }
  651. static void do_abort_syn_rcv(struct sock *child, struct sock *parent)
  652. {
  653. /*
  654. * If the server is still open we clean up the child connection,
  655. * otherwise the server already did the clean up as it was purging
  656. * its SYN queue and the skb was just sitting in its backlog.
  657. */
  658. if (likely(parent->sk_state == TCP_LISTEN)) {
  659. cleanup_syn_rcv_conn(child, parent);
  660. /* Without the below call to sock_orphan,
  661. * we leak the socket resource with syn_flood test
  662. * as inet_csk_destroy_sock will not be called
  663. * in tcp_done since SOCK_DEAD flag is not set.
  664. * Kernel handles this differently where new socket is
  665. * created only after 3 way handshake is done.
  666. */
  667. sock_orphan(child);
  668. percpu_counter_inc((child)->sk_prot->orphan_count);
  669. chtls_release_resources(child);
  670. chtls_conn_done(child);
  671. } else {
  672. if (csk_flag(child, CSK_RST_ABORTED)) {
  673. chtls_release_resources(child);
  674. chtls_conn_done(child);
  675. }
  676. }
  677. }
  678. static void pass_open_abort(struct sock *child, struct sock *parent,
  679. struct sk_buff *skb)
  680. {
  681. do_abort_syn_rcv(child, parent);
  682. kfree_skb(skb);
  683. }
  684. static void bl_pass_open_abort(struct sock *lsk, struct sk_buff *skb)
  685. {
  686. pass_open_abort(skb->sk, lsk, skb);
  687. }
  688. static void chtls_pass_open_arp_failure(struct sock *sk,
  689. struct sk_buff *skb)
  690. {
  691. const struct request_sock *oreq;
  692. struct chtls_sock *csk;
  693. struct chtls_dev *cdev;
  694. struct sock *parent;
  695. void *data;
  696. csk = rcu_dereference_sk_user_data(sk);
  697. cdev = csk->cdev;
  698. /*
  699. * If the connection is being aborted due to the parent listening
  700. * socket going away there's nothing to do, the ABORT_REQ will close
  701. * the connection.
  702. */
  703. if (csk_flag(sk, CSK_ABORT_RPL_PENDING)) {
  704. kfree_skb(skb);
  705. return;
  706. }
  707. oreq = csk->passive_reap_next;
  708. data = lookup_stid(cdev->tids, oreq->ts_recent);
  709. parent = ((struct listen_ctx *)data)->lsk;
  710. bh_lock_sock(parent);
  711. if (!sock_owned_by_user(parent)) {
  712. pass_open_abort(sk, parent, skb);
  713. } else {
  714. BLOG_SKB_CB(skb)->backlog_rcv = bl_pass_open_abort;
  715. __sk_add_backlog(parent, skb);
  716. }
  717. bh_unlock_sock(parent);
  718. }
  719. static void chtls_accept_rpl_arp_failure(void *handle,
  720. struct sk_buff *skb)
  721. {
  722. struct sock *sk = (struct sock *)handle;
  723. sock_hold(sk);
  724. process_cpl_msg(chtls_pass_open_arp_failure, sk, skb);
  725. sock_put(sk);
  726. }
  727. static unsigned int chtls_select_mss(const struct chtls_sock *csk,
  728. unsigned int pmtu,
  729. struct cpl_pass_accept_req *req)
  730. {
  731. struct chtls_dev *cdev;
  732. struct dst_entry *dst;
  733. unsigned int tcpoptsz;
  734. unsigned int iphdrsz;
  735. unsigned int mtu_idx;
  736. struct tcp_sock *tp;
  737. unsigned int mss;
  738. struct sock *sk;
  739. mss = ntohs(req->tcpopt.mss);
  740. sk = csk->sk;
  741. dst = __sk_dst_get(sk);
  742. cdev = csk->cdev;
  743. tp = tcp_sk(sk);
  744. tcpoptsz = 0;
  745. iphdrsz = sizeof(struct iphdr) + sizeof(struct tcphdr);
  746. if (req->tcpopt.tstamp)
  747. tcpoptsz += round_up(TCPOLEN_TIMESTAMP, 4);
  748. tp->advmss = dst_metric_advmss(dst);
  749. if (USER_MSS(tp) && tp->advmss > USER_MSS(tp))
  750. tp->advmss = USER_MSS(tp);
  751. if (tp->advmss > pmtu - iphdrsz)
  752. tp->advmss = pmtu - iphdrsz;
  753. if (mss && tp->advmss > mss)
  754. tp->advmss = mss;
  755. tp->advmss = cxgb4_best_aligned_mtu(cdev->lldi->mtus,
  756. iphdrsz + tcpoptsz,
  757. tp->advmss - tcpoptsz,
  758. 8, &mtu_idx);
  759. tp->advmss -= iphdrsz;
  760. inet_csk(sk)->icsk_pmtu_cookie = pmtu;
  761. return mtu_idx;
  762. }
  763. static unsigned int select_rcv_wnd(struct chtls_sock *csk)
  764. {
  765. unsigned int rcvwnd;
  766. unsigned int wnd;
  767. struct sock *sk;
  768. sk = csk->sk;
  769. wnd = tcp_full_space(sk);
  770. if (wnd < MIN_RCV_WND)
  771. wnd = MIN_RCV_WND;
  772. rcvwnd = MAX_RCV_WND;
  773. csk_set_flag(csk, CSK_UPDATE_RCV_WND);
  774. return min(wnd, rcvwnd);
  775. }
  776. static unsigned int select_rcv_wscale(int space, int wscale_ok, int win_clamp)
  777. {
  778. int wscale = 0;
  779. if (space > MAX_RCV_WND)
  780. space = MAX_RCV_WND;
  781. if (win_clamp && win_clamp < space)
  782. space = win_clamp;
  783. if (wscale_ok) {
  784. while (wscale < 14 && (65535 << wscale) < space)
  785. wscale++;
  786. }
  787. return wscale;
  788. }
  789. static void chtls_pass_accept_rpl(struct sk_buff *skb,
  790. struct cpl_pass_accept_req *req,
  791. unsigned int tid)
  792. {
  793. struct cpl_t5_pass_accept_rpl *rpl5;
  794. struct cxgb4_lld_info *lldi;
  795. const struct tcphdr *tcph;
  796. const struct tcp_sock *tp;
  797. struct chtls_sock *csk;
  798. unsigned int len;
  799. struct sock *sk;
  800. u32 opt2, hlen;
  801. u64 opt0;
  802. sk = skb->sk;
  803. tp = tcp_sk(sk);
  804. csk = sk->sk_user_data;
  805. csk->tid = tid;
  806. lldi = csk->cdev->lldi;
  807. len = roundup(sizeof(*rpl5), 16);
  808. rpl5 = __skb_put_zero(skb, len);
  809. INIT_TP_WR(rpl5, tid);
  810. OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
  811. csk->tid));
  812. csk->mtu_idx = chtls_select_mss(csk, dst_mtu(__sk_dst_get(sk)),
  813. req);
  814. opt0 = TCAM_BYPASS_F |
  815. WND_SCALE_V((tp)->rx_opt.rcv_wscale) |
  816. MSS_IDX_V(csk->mtu_idx) |
  817. L2T_IDX_V(csk->l2t_entry->idx) |
  818. NAGLE_V(!(tp->nonagle & TCP_NAGLE_OFF)) |
  819. TX_CHAN_V(csk->tx_chan) |
  820. SMAC_SEL_V(csk->smac_idx) |
  821. DSCP_V(csk->tos >> 2) |
  822. ULP_MODE_V(ULP_MODE_TLS) |
  823. RCV_BUFSIZ_V(min(tp->rcv_wnd >> 10, RCV_BUFSIZ_M));
  824. opt2 = RX_CHANNEL_V(0) |
  825. RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
  826. if (!is_t5(lldi->adapter_type))
  827. opt2 |= RX_FC_DISABLE_F;
  828. if (req->tcpopt.tstamp)
  829. opt2 |= TSTAMPS_EN_F;
  830. if (req->tcpopt.sack)
  831. opt2 |= SACK_EN_F;
  832. hlen = ntohl(req->hdr_len);
  833. tcph = (struct tcphdr *)((u8 *)(req + 1) +
  834. T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
  835. if (tcph->ece && tcph->cwr)
  836. opt2 |= CCTRL_ECN_V(1);
  837. opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
  838. opt2 |= T5_ISS_F;
  839. opt2 |= T5_OPT_2_VALID_F;
  840. rpl5->opt0 = cpu_to_be64(opt0);
  841. rpl5->opt2 = cpu_to_be32(opt2);
  842. rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
  843. set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
  844. t4_set_arp_err_handler(skb, sk, chtls_accept_rpl_arp_failure);
  845. cxgb4_l2t_send(csk->egress_dev, skb, csk->l2t_entry);
  846. }
  847. static void inet_inherit_port(struct inet_hashinfo *hash_info,
  848. struct sock *lsk, struct sock *newsk)
  849. {
  850. local_bh_disable();
  851. __inet_inherit_port(lsk, newsk);
  852. local_bh_enable();
  853. }
  854. static int chtls_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  855. {
  856. if (skb->protocol) {
  857. kfree_skb(skb);
  858. return 0;
  859. }
  860. BLOG_SKB_CB(skb)->backlog_rcv(sk, skb);
  861. return 0;
  862. }
  863. static struct sock *chtls_recv_sock(struct sock *lsk,
  864. struct request_sock *oreq,
  865. void *network_hdr,
  866. const struct cpl_pass_accept_req *req,
  867. struct chtls_dev *cdev)
  868. {
  869. struct adapter *adap = pci_get_drvdata(cdev->pdev);
  870. const struct tcphdr *tcph;
  871. struct inet_sock *newinet;
  872. const struct iphdr *iph;
  873. struct net_device *ndev;
  874. struct chtls_sock *csk;
  875. struct dst_entry *dst;
  876. struct neighbour *n;
  877. struct tcp_sock *tp;
  878. struct sock *newsk;
  879. bool found = false;
  880. u16 port_id;
  881. int rxq_idx;
  882. int step, i;
  883. iph = (const struct iphdr *)network_hdr;
  884. newsk = tcp_create_openreq_child(lsk, oreq, cdev->askb);
  885. if (!newsk)
  886. goto free_oreq;
  887. dst = inet_csk_route_child_sock(lsk, newsk, oreq);
  888. if (!dst)
  889. goto free_sk;
  890. tcph = (struct tcphdr *)(iph + 1);
  891. n = dst_neigh_lookup(dst, &iph->saddr);
  892. if (!n || !n->dev)
  893. goto free_dst;
  894. ndev = n->dev;
  895. if (is_vlan_dev(ndev))
  896. ndev = vlan_dev_real_dev(ndev);
  897. for_each_port(adap, i)
  898. if (cdev->ports[i] == ndev)
  899. found = true;
  900. if (!found)
  901. goto free_dst;
  902. port_id = cxgb4_port_idx(ndev);
  903. csk = chtls_sock_create(cdev);
  904. if (!csk)
  905. goto free_dst;
  906. csk->l2t_entry = cxgb4_l2t_get(cdev->lldi->l2t, n, ndev, 0);
  907. if (!csk->l2t_entry)
  908. goto free_csk;
  909. newsk->sk_user_data = csk;
  910. newsk->sk_backlog_rcv = chtls_backlog_rcv;
  911. tp = tcp_sk(newsk);
  912. newinet = inet_sk(newsk);
  913. newinet->inet_daddr = iph->saddr;
  914. newinet->inet_rcv_saddr = iph->daddr;
  915. newinet->inet_saddr = iph->daddr;
  916. oreq->ts_recent = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  917. sk_setup_caps(newsk, dst);
  918. newsk->sk_prot_creator = lsk->sk_prot_creator;
  919. csk->sk = newsk;
  920. csk->passive_reap_next = oreq;
  921. csk->tx_chan = cxgb4_port_chan(ndev);
  922. csk->port_id = port_id;
  923. csk->egress_dev = ndev;
  924. csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
  925. csk->ulp_mode = ULP_MODE_TLS;
  926. step = cdev->lldi->nrxq / cdev->lldi->nchan;
  927. csk->rss_qid = cdev->lldi->rxq_ids[port_id * step];
  928. rxq_idx = port_id * step;
  929. csk->txq_idx = (rxq_idx < cdev->lldi->ntxq) ? rxq_idx :
  930. port_id * step;
  931. csk->sndbuf = newsk->sk_sndbuf;
  932. csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi->adapter_type,
  933. cxgb4_port_viid(ndev));
  934. tp->rcv_wnd = select_rcv_wnd(csk);
  935. RCV_WSCALE(tp) = select_rcv_wscale(tcp_full_space(newsk),
  936. WSCALE_OK(tp),
  937. tp->window_clamp);
  938. neigh_release(n);
  939. inet_inherit_port(&tcp_hashinfo, lsk, newsk);
  940. csk_set_flag(csk, CSK_CONN_INLINE);
  941. bh_unlock_sock(newsk); /* tcp_create_openreq_child ->sk_clone_lock */
  942. return newsk;
  943. free_csk:
  944. chtls_sock_release(&csk->kref);
  945. free_dst:
  946. if (n)
  947. neigh_release(n);
  948. dst_release(dst);
  949. free_sk:
  950. inet_csk_prepare_forced_close(newsk);
  951. tcp_done(newsk);
  952. free_oreq:
  953. chtls_reqsk_free(oreq);
  954. return NULL;
  955. }
  956. /*
  957. * Populate a TID_RELEASE WR. The skb must be already propely sized.
  958. */
  959. static void mk_tid_release(struct sk_buff *skb,
  960. unsigned int chan, unsigned int tid)
  961. {
  962. struct cpl_tid_release *req;
  963. unsigned int len;
  964. len = roundup(sizeof(struct cpl_tid_release), 16);
  965. req = (struct cpl_tid_release *)__skb_put(skb, len);
  966. memset(req, 0, len);
  967. set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
  968. INIT_TP_WR_CPL(req, CPL_TID_RELEASE, tid);
  969. }
  970. static int chtls_get_module(struct sock *sk)
  971. {
  972. struct inet_connection_sock *icsk = inet_csk(sk);
  973. if (!try_module_get(icsk->icsk_ulp_ops->owner))
  974. return -1;
  975. return 0;
  976. }
  977. static void chtls_pass_accept_request(struct sock *sk,
  978. struct sk_buff *skb)
  979. {
  980. struct cpl_t5_pass_accept_rpl *rpl;
  981. struct cpl_pass_accept_req *req;
  982. struct listen_ctx *listen_ctx;
  983. struct request_sock *oreq;
  984. struct sk_buff *reply_skb;
  985. struct chtls_sock *csk;
  986. struct chtls_dev *cdev;
  987. struct tcphdr *tcph;
  988. struct sock *newsk;
  989. struct ethhdr *eh;
  990. struct iphdr *iph;
  991. void *network_hdr;
  992. unsigned int stid;
  993. unsigned int len;
  994. unsigned int tid;
  995. req = cplhdr(skb) + RSS_HDR;
  996. tid = GET_TID(req);
  997. cdev = BLOG_SKB_CB(skb)->cdev;
  998. newsk = lookup_tid(cdev->tids, tid);
  999. stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  1000. if (newsk) {
  1001. pr_info("tid (%d) already in use\n", tid);
  1002. return;
  1003. }
  1004. len = roundup(sizeof(*rpl), 16);
  1005. reply_skb = alloc_skb(len, GFP_ATOMIC);
  1006. if (!reply_skb) {
  1007. cxgb4_remove_tid(cdev->tids, 0, tid, sk->sk_family);
  1008. kfree_skb(skb);
  1009. return;
  1010. }
  1011. if (sk->sk_state != TCP_LISTEN)
  1012. goto reject;
  1013. if (inet_csk_reqsk_queue_is_full(sk))
  1014. goto reject;
  1015. if (sk_acceptq_is_full(sk))
  1016. goto reject;
  1017. oreq = inet_reqsk_alloc(&chtls_rsk_ops, sk, true);
  1018. if (!oreq)
  1019. goto reject;
  1020. oreq->rsk_rcv_wnd = 0;
  1021. oreq->rsk_window_clamp = 0;
  1022. oreq->cookie_ts = 0;
  1023. oreq->mss = 0;
  1024. oreq->ts_recent = 0;
  1025. eh = (struct ethhdr *)(req + 1);
  1026. iph = (struct iphdr *)(eh + 1);
  1027. if (iph->version != 0x4)
  1028. goto free_oreq;
  1029. network_hdr = (void *)(eh + 1);
  1030. tcph = (struct tcphdr *)(iph + 1);
  1031. tcp_rsk(oreq)->tfo_listener = false;
  1032. tcp_rsk(oreq)->rcv_isn = ntohl(tcph->seq);
  1033. chtls_set_req_port(oreq, tcph->source, tcph->dest);
  1034. inet_rsk(oreq)->ecn_ok = 0;
  1035. chtls_set_req_addr(oreq, iph->daddr, iph->saddr);
  1036. if (req->tcpopt.wsf <= 14) {
  1037. inet_rsk(oreq)->wscale_ok = 1;
  1038. inet_rsk(oreq)->snd_wscale = req->tcpopt.wsf;
  1039. }
  1040. inet_rsk(oreq)->ir_iif = sk->sk_bound_dev_if;
  1041. newsk = chtls_recv_sock(sk, oreq, network_hdr, req, cdev);
  1042. if (!newsk)
  1043. goto reject;
  1044. if (chtls_get_module(newsk))
  1045. goto reject;
  1046. inet_csk_reqsk_queue_added(sk);
  1047. reply_skb->sk = newsk;
  1048. chtls_install_cpl_ops(newsk);
  1049. cxgb4_insert_tid(cdev->tids, newsk, tid, newsk->sk_family);
  1050. csk = rcu_dereference_sk_user_data(newsk);
  1051. listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
  1052. csk->listen_ctx = listen_ctx;
  1053. __skb_queue_tail(&listen_ctx->synq, (struct sk_buff *)&csk->synq);
  1054. chtls_pass_accept_rpl(reply_skb, req, tid);
  1055. kfree_skb(skb);
  1056. return;
  1057. free_oreq:
  1058. chtls_reqsk_free(oreq);
  1059. reject:
  1060. mk_tid_release(reply_skb, 0, tid);
  1061. cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
  1062. kfree_skb(skb);
  1063. }
  1064. /*
  1065. * Handle a CPL_PASS_ACCEPT_REQ message.
  1066. */
  1067. static int chtls_pass_accept_req(struct chtls_dev *cdev, struct sk_buff *skb)
  1068. {
  1069. struct cpl_pass_accept_req *req = cplhdr(skb) + RSS_HDR;
  1070. struct listen_ctx *ctx;
  1071. unsigned int stid;
  1072. unsigned int tid;
  1073. struct sock *lsk;
  1074. void *data;
  1075. stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  1076. tid = GET_TID(req);
  1077. data = lookup_stid(cdev->tids, stid);
  1078. if (!data)
  1079. return 1;
  1080. ctx = (struct listen_ctx *)data;
  1081. lsk = ctx->lsk;
  1082. if (unlikely(tid >= cdev->tids->ntids)) {
  1083. pr_info("passive open TID %u too large\n", tid);
  1084. return 1;
  1085. }
  1086. BLOG_SKB_CB(skb)->cdev = cdev;
  1087. process_cpl_msg(chtls_pass_accept_request, lsk, skb);
  1088. return 0;
  1089. }
  1090. /*
  1091. * Completes some final bits of initialization for just established connections
  1092. * and changes their state to TCP_ESTABLISHED.
  1093. *
  1094. * snd_isn here is the ISN after the SYN, i.e., the true ISN + 1.
  1095. */
  1096. static void make_established(struct sock *sk, u32 snd_isn, unsigned int opt)
  1097. {
  1098. struct tcp_sock *tp = tcp_sk(sk);
  1099. tp->pushed_seq = snd_isn;
  1100. tp->write_seq = snd_isn;
  1101. tp->snd_nxt = snd_isn;
  1102. tp->snd_una = snd_isn;
  1103. inet_sk(sk)->inet_id = prandom_u32();
  1104. assign_rxopt(sk, opt);
  1105. if (tp->rcv_wnd > (RCV_BUFSIZ_M << 10))
  1106. tp->rcv_wup -= tp->rcv_wnd - (RCV_BUFSIZ_M << 10);
  1107. smp_mb();
  1108. tcp_set_state(sk, TCP_ESTABLISHED);
  1109. }
  1110. static void chtls_abort_conn(struct sock *sk, struct sk_buff *skb)
  1111. {
  1112. struct sk_buff *abort_skb;
  1113. abort_skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
  1114. if (abort_skb)
  1115. chtls_send_reset(sk, CPL_ABORT_SEND_RST, abort_skb);
  1116. }
  1117. static struct sock *reap_list;
  1118. static DEFINE_SPINLOCK(reap_list_lock);
  1119. /*
  1120. * Process the reap list.
  1121. */
  1122. DECLARE_TASK_FUNC(process_reap_list, task_param)
  1123. {
  1124. spin_lock_bh(&reap_list_lock);
  1125. while (reap_list) {
  1126. struct sock *sk = reap_list;
  1127. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  1128. reap_list = csk->passive_reap_next;
  1129. csk->passive_reap_next = NULL;
  1130. spin_unlock(&reap_list_lock);
  1131. sock_hold(sk);
  1132. bh_lock_sock(sk);
  1133. chtls_abort_conn(sk, NULL);
  1134. sock_orphan(sk);
  1135. if (sk->sk_state == TCP_CLOSE)
  1136. inet_csk_destroy_sock(sk);
  1137. bh_unlock_sock(sk);
  1138. sock_put(sk);
  1139. spin_lock(&reap_list_lock);
  1140. }
  1141. spin_unlock_bh(&reap_list_lock);
  1142. }
  1143. static DECLARE_WORK(reap_task, process_reap_list);
  1144. static void add_to_reap_list(struct sock *sk)
  1145. {
  1146. struct chtls_sock *csk = sk->sk_user_data;
  1147. local_bh_disable();
  1148. release_tcp_port(sk); /* release the port immediately */
  1149. spin_lock(&reap_list_lock);
  1150. csk->passive_reap_next = reap_list;
  1151. reap_list = sk;
  1152. if (!csk->passive_reap_next)
  1153. schedule_work(&reap_task);
  1154. spin_unlock(&reap_list_lock);
  1155. local_bh_enable();
  1156. }
  1157. static void add_pass_open_to_parent(struct sock *child, struct sock *lsk,
  1158. struct chtls_dev *cdev)
  1159. {
  1160. struct request_sock *oreq;
  1161. struct chtls_sock *csk;
  1162. if (lsk->sk_state != TCP_LISTEN)
  1163. return;
  1164. csk = child->sk_user_data;
  1165. oreq = csk->passive_reap_next;
  1166. csk->passive_reap_next = NULL;
  1167. reqsk_queue_removed(&inet_csk(lsk)->icsk_accept_queue, oreq);
  1168. __skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
  1169. if (sk_acceptq_is_full(lsk)) {
  1170. chtls_reqsk_free(oreq);
  1171. add_to_reap_list(child);
  1172. } else {
  1173. refcount_set(&oreq->rsk_refcnt, 1);
  1174. inet_csk_reqsk_queue_add(lsk, oreq, child);
  1175. lsk->sk_data_ready(lsk);
  1176. }
  1177. }
  1178. static void bl_add_pass_open_to_parent(struct sock *lsk, struct sk_buff *skb)
  1179. {
  1180. struct sock *child = skb->sk;
  1181. skb->sk = NULL;
  1182. add_pass_open_to_parent(child, lsk, BLOG_SKB_CB(skb)->cdev);
  1183. kfree_skb(skb);
  1184. }
  1185. static int chtls_pass_establish(struct chtls_dev *cdev, struct sk_buff *skb)
  1186. {
  1187. struct cpl_pass_establish *req = cplhdr(skb) + RSS_HDR;
  1188. struct chtls_sock *csk;
  1189. struct sock *lsk, *sk;
  1190. unsigned int hwtid;
  1191. hwtid = GET_TID(req);
  1192. sk = lookup_tid(cdev->tids, hwtid);
  1193. if (!sk)
  1194. return (CPL_RET_UNKNOWN_TID | CPL_RET_BUF_DONE);
  1195. bh_lock_sock(sk);
  1196. if (unlikely(sock_owned_by_user(sk))) {
  1197. kfree_skb(skb);
  1198. } else {
  1199. unsigned int stid;
  1200. void *data;
  1201. csk = sk->sk_user_data;
  1202. csk->wr_max_credits = 64;
  1203. csk->wr_credits = 64;
  1204. csk->wr_unacked = 0;
  1205. make_established(sk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
  1206. stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  1207. sk->sk_state_change(sk);
  1208. if (unlikely(sk->sk_socket))
  1209. sk_wake_async(sk, 0, POLL_OUT);
  1210. data = lookup_stid(cdev->tids, stid);
  1211. if (!data) {
  1212. /* listening server close */
  1213. kfree_skb(skb);
  1214. goto unlock;
  1215. }
  1216. lsk = ((struct listen_ctx *)data)->lsk;
  1217. bh_lock_sock(lsk);
  1218. if (unlikely(skb_queue_empty(&csk->listen_ctx->synq))) {
  1219. /* removed from synq */
  1220. bh_unlock_sock(lsk);
  1221. kfree_skb(skb);
  1222. goto unlock;
  1223. }
  1224. if (likely(!sock_owned_by_user(lsk))) {
  1225. kfree_skb(skb);
  1226. add_pass_open_to_parent(sk, lsk, cdev);
  1227. } else {
  1228. skb->sk = sk;
  1229. BLOG_SKB_CB(skb)->cdev = cdev;
  1230. BLOG_SKB_CB(skb)->backlog_rcv =
  1231. bl_add_pass_open_to_parent;
  1232. __sk_add_backlog(lsk, skb);
  1233. }
  1234. bh_unlock_sock(lsk);
  1235. }
  1236. unlock:
  1237. bh_unlock_sock(sk);
  1238. return 0;
  1239. }
  1240. /*
  1241. * Handle receipt of an urgent pointer.
  1242. */
  1243. static void handle_urg_ptr(struct sock *sk, u32 urg_seq)
  1244. {
  1245. struct tcp_sock *tp = tcp_sk(sk);
  1246. urg_seq--;
  1247. if (tp->urg_data && !after(urg_seq, tp->urg_seq))
  1248. return; /* duplicate pointer */
  1249. sk_send_sigurg(sk);
  1250. if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
  1251. !sock_flag(sk, SOCK_URGINLINE) &&
  1252. tp->copied_seq != tp->rcv_nxt) {
  1253. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  1254. tp->copied_seq++;
  1255. if (skb && tp->copied_seq - ULP_SKB_CB(skb)->seq >= skb->len)
  1256. chtls_free_skb(sk, skb);
  1257. }
  1258. tp->urg_data = TCP_URG_NOTYET;
  1259. tp->urg_seq = urg_seq;
  1260. }
  1261. static void check_sk_callbacks(struct chtls_sock *csk)
  1262. {
  1263. struct sock *sk = csk->sk;
  1264. if (unlikely(sk->sk_user_data &&
  1265. !csk_flag_nochk(csk, CSK_CALLBACKS_CHKD)))
  1266. csk_set_flag(csk, CSK_CALLBACKS_CHKD);
  1267. }
  1268. /*
  1269. * Handles Rx data that arrives in a state where the socket isn't accepting
  1270. * new data.
  1271. */
  1272. static void handle_excess_rx(struct sock *sk, struct sk_buff *skb)
  1273. {
  1274. if (!csk_flag(sk, CSK_ABORT_SHUTDOWN))
  1275. chtls_abort_conn(sk, skb);
  1276. kfree_skb(skb);
  1277. }
  1278. static void chtls_recv_data(struct sock *sk, struct sk_buff *skb)
  1279. {
  1280. struct cpl_rx_data *hdr = cplhdr(skb) + RSS_HDR;
  1281. struct chtls_sock *csk;
  1282. struct tcp_sock *tp;
  1283. csk = rcu_dereference_sk_user_data(sk);
  1284. tp = tcp_sk(sk);
  1285. if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
  1286. handle_excess_rx(sk, skb);
  1287. return;
  1288. }
  1289. ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
  1290. ULP_SKB_CB(skb)->psh = hdr->psh;
  1291. skb_ulp_mode(skb) = ULP_MODE_NONE;
  1292. skb_reset_transport_header(skb);
  1293. __skb_pull(skb, sizeof(*hdr) + RSS_HDR);
  1294. if (!skb->data_len)
  1295. __skb_trim(skb, ntohs(hdr->len));
  1296. if (unlikely(hdr->urg))
  1297. handle_urg_ptr(sk, tp->rcv_nxt + ntohs(hdr->urg));
  1298. if (unlikely(tp->urg_data == TCP_URG_NOTYET &&
  1299. tp->urg_seq - tp->rcv_nxt < skb->len))
  1300. tp->urg_data = TCP_URG_VALID |
  1301. skb->data[tp->urg_seq - tp->rcv_nxt];
  1302. if (unlikely(hdr->dack_mode != csk->delack_mode)) {
  1303. csk->delack_mode = hdr->dack_mode;
  1304. csk->delack_seq = tp->rcv_nxt;
  1305. }
  1306. tcp_hdr(skb)->fin = 0;
  1307. tp->rcv_nxt += skb->len;
  1308. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1309. if (!sock_flag(sk, SOCK_DEAD)) {
  1310. check_sk_callbacks(csk);
  1311. sk->sk_data_ready(sk);
  1312. }
  1313. }
  1314. static int chtls_rx_data(struct chtls_dev *cdev, struct sk_buff *skb)
  1315. {
  1316. struct cpl_rx_data *req = cplhdr(skb) + RSS_HDR;
  1317. unsigned int hwtid = GET_TID(req);
  1318. struct sock *sk;
  1319. sk = lookup_tid(cdev->tids, hwtid);
  1320. if (unlikely(!sk)) {
  1321. pr_err("can't find conn. for hwtid %u.\n", hwtid);
  1322. return -EINVAL;
  1323. }
  1324. skb_dst_set(skb, NULL);
  1325. process_cpl_msg(chtls_recv_data, sk, skb);
  1326. return 0;
  1327. }
  1328. static void chtls_recv_pdu(struct sock *sk, struct sk_buff *skb)
  1329. {
  1330. struct cpl_tls_data *hdr = cplhdr(skb);
  1331. struct chtls_sock *csk;
  1332. struct chtls_hws *tlsk;
  1333. struct tcp_sock *tp;
  1334. csk = rcu_dereference_sk_user_data(sk);
  1335. tlsk = &csk->tlshws;
  1336. tp = tcp_sk(sk);
  1337. if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
  1338. handle_excess_rx(sk, skb);
  1339. return;
  1340. }
  1341. ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
  1342. ULP_SKB_CB(skb)->flags = 0;
  1343. skb_ulp_mode(skb) = ULP_MODE_TLS;
  1344. skb_reset_transport_header(skb);
  1345. __skb_pull(skb, sizeof(*hdr));
  1346. if (!skb->data_len)
  1347. __skb_trim(skb,
  1348. CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd)));
  1349. if (unlikely(tp->urg_data == TCP_URG_NOTYET && tp->urg_seq -
  1350. tp->rcv_nxt < skb->len))
  1351. tp->urg_data = TCP_URG_VALID |
  1352. skb->data[tp->urg_seq - tp->rcv_nxt];
  1353. tcp_hdr(skb)->fin = 0;
  1354. tlsk->pldlen = CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd));
  1355. __skb_queue_tail(&tlsk->sk_recv_queue, skb);
  1356. }
  1357. static int chtls_rx_pdu(struct chtls_dev *cdev, struct sk_buff *skb)
  1358. {
  1359. struct cpl_tls_data *req = cplhdr(skb);
  1360. unsigned int hwtid = GET_TID(req);
  1361. struct sock *sk;
  1362. sk = lookup_tid(cdev->tids, hwtid);
  1363. if (unlikely(!sk)) {
  1364. pr_err("can't find conn. for hwtid %u.\n", hwtid);
  1365. return -EINVAL;
  1366. }
  1367. skb_dst_set(skb, NULL);
  1368. process_cpl_msg(chtls_recv_pdu, sk, skb);
  1369. return 0;
  1370. }
  1371. static void chtls_set_hdrlen(struct sk_buff *skb, unsigned int nlen)
  1372. {
  1373. struct tlsrx_cmp_hdr *tls_cmp_hdr = cplhdr(skb);
  1374. skb->hdr_len = ntohs((__force __be16)tls_cmp_hdr->length);
  1375. tls_cmp_hdr->length = ntohs((__force __be16)nlen);
  1376. }
  1377. static void chtls_rx_hdr(struct sock *sk, struct sk_buff *skb)
  1378. {
  1379. struct tlsrx_cmp_hdr *tls_hdr_pkt;
  1380. struct cpl_rx_tls_cmp *cmp_cpl;
  1381. struct sk_buff *skb_rec;
  1382. struct chtls_sock *csk;
  1383. struct chtls_hws *tlsk;
  1384. struct tcp_sock *tp;
  1385. cmp_cpl = cplhdr(skb);
  1386. csk = rcu_dereference_sk_user_data(sk);
  1387. tlsk = &csk->tlshws;
  1388. tp = tcp_sk(sk);
  1389. ULP_SKB_CB(skb)->seq = ntohl(cmp_cpl->seq);
  1390. ULP_SKB_CB(skb)->flags = 0;
  1391. skb_reset_transport_header(skb);
  1392. __skb_pull(skb, sizeof(*cmp_cpl));
  1393. tls_hdr_pkt = (struct tlsrx_cmp_hdr *)skb->data;
  1394. if (tls_hdr_pkt->res_to_mac_error & TLSRX_HDR_PKT_ERROR_M)
  1395. tls_hdr_pkt->type = CONTENT_TYPE_ERROR;
  1396. if (!skb->data_len)
  1397. __skb_trim(skb, TLS_HEADER_LENGTH);
  1398. tp->rcv_nxt +=
  1399. CPL_RX_TLS_CMP_PDULENGTH_G(ntohl(cmp_cpl->pdulength_length));
  1400. ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_TLS_HDR;
  1401. skb_rec = __skb_dequeue(&tlsk->sk_recv_queue);
  1402. if (!skb_rec) {
  1403. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1404. } else {
  1405. chtls_set_hdrlen(skb, tlsk->pldlen);
  1406. tlsk->pldlen = 0;
  1407. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1408. __skb_queue_tail(&sk->sk_receive_queue, skb_rec);
  1409. }
  1410. if (!sock_flag(sk, SOCK_DEAD)) {
  1411. check_sk_callbacks(csk);
  1412. sk->sk_data_ready(sk);
  1413. }
  1414. }
  1415. static int chtls_rx_cmp(struct chtls_dev *cdev, struct sk_buff *skb)
  1416. {
  1417. struct cpl_rx_tls_cmp *req = cplhdr(skb);
  1418. unsigned int hwtid = GET_TID(req);
  1419. struct sock *sk;
  1420. sk = lookup_tid(cdev->tids, hwtid);
  1421. if (unlikely(!sk)) {
  1422. pr_err("can't find conn. for hwtid %u.\n", hwtid);
  1423. return -EINVAL;
  1424. }
  1425. skb_dst_set(skb, NULL);
  1426. process_cpl_msg(chtls_rx_hdr, sk, skb);
  1427. return 0;
  1428. }
  1429. static void chtls_timewait(struct sock *sk)
  1430. {
  1431. struct tcp_sock *tp = tcp_sk(sk);
  1432. tp->rcv_nxt++;
  1433. tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
  1434. tp->srtt_us = 0;
  1435. tcp_time_wait(sk, TCP_TIME_WAIT, 0);
  1436. }
  1437. static void chtls_peer_close(struct sock *sk, struct sk_buff *skb)
  1438. {
  1439. struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
  1440. sk->sk_shutdown |= RCV_SHUTDOWN;
  1441. sock_set_flag(sk, SOCK_DONE);
  1442. switch (sk->sk_state) {
  1443. case TCP_SYN_RECV:
  1444. case TCP_ESTABLISHED:
  1445. tcp_set_state(sk, TCP_CLOSE_WAIT);
  1446. break;
  1447. case TCP_FIN_WAIT1:
  1448. tcp_set_state(sk, TCP_CLOSING);
  1449. break;
  1450. case TCP_FIN_WAIT2:
  1451. chtls_release_resources(sk);
  1452. if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
  1453. chtls_conn_done(sk);
  1454. else
  1455. chtls_timewait(sk);
  1456. break;
  1457. default:
  1458. pr_info("cpl_peer_close in bad state %d\n", sk->sk_state);
  1459. }
  1460. if (!sock_flag(sk, SOCK_DEAD)) {
  1461. sk->sk_state_change(sk);
  1462. /* Do not send POLL_HUP for half duplex close. */
  1463. if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
  1464. sk->sk_state == TCP_CLOSE)
  1465. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
  1466. else
  1467. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  1468. }
  1469. kfree_skb(skb);
  1470. }
  1471. static void chtls_close_con_rpl(struct sock *sk, struct sk_buff *skb)
  1472. {
  1473. struct cpl_close_con_rpl *rpl = cplhdr(skb) + RSS_HDR;
  1474. struct chtls_sock *csk;
  1475. struct tcp_sock *tp;
  1476. csk = rcu_dereference_sk_user_data(sk);
  1477. tp = tcp_sk(sk);
  1478. tp->snd_una = ntohl(rpl->snd_nxt) - 1; /* exclude FIN */
  1479. switch (sk->sk_state) {
  1480. case TCP_CLOSING:
  1481. chtls_release_resources(sk);
  1482. if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
  1483. chtls_conn_done(sk);
  1484. else
  1485. chtls_timewait(sk);
  1486. break;
  1487. case TCP_LAST_ACK:
  1488. chtls_release_resources(sk);
  1489. chtls_conn_done(sk);
  1490. break;
  1491. case TCP_FIN_WAIT1:
  1492. tcp_set_state(sk, TCP_FIN_WAIT2);
  1493. sk->sk_shutdown |= SEND_SHUTDOWN;
  1494. if (!sock_flag(sk, SOCK_DEAD))
  1495. sk->sk_state_change(sk);
  1496. else if (tcp_sk(sk)->linger2 < 0 &&
  1497. !csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN))
  1498. chtls_abort_conn(sk, skb);
  1499. break;
  1500. default:
  1501. pr_info("close_con_rpl in bad state %d\n", sk->sk_state);
  1502. }
  1503. kfree_skb(skb);
  1504. }
  1505. static struct sk_buff *get_cpl_skb(struct sk_buff *skb,
  1506. size_t len, gfp_t gfp)
  1507. {
  1508. if (likely(!skb_is_nonlinear(skb) && !skb_cloned(skb))) {
  1509. WARN_ONCE(skb->len < len, "skb alloc error");
  1510. __skb_trim(skb, len);
  1511. skb_get(skb);
  1512. } else {
  1513. skb = alloc_skb(len, gfp);
  1514. if (skb)
  1515. __skb_put(skb, len);
  1516. }
  1517. return skb;
  1518. }
  1519. static void set_abort_rpl_wr(struct sk_buff *skb, unsigned int tid,
  1520. int cmd)
  1521. {
  1522. struct cpl_abort_rpl *rpl = cplhdr(skb);
  1523. INIT_TP_WR_CPL(rpl, CPL_ABORT_RPL, tid);
  1524. rpl->cmd = cmd;
  1525. }
  1526. static void send_defer_abort_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
  1527. {
  1528. struct cpl_abort_req_rss *req = cplhdr(skb);
  1529. struct sk_buff *reply_skb;
  1530. reply_skb = alloc_skb(sizeof(struct cpl_abort_rpl),
  1531. GFP_KERNEL | __GFP_NOFAIL);
  1532. __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
  1533. set_abort_rpl_wr(reply_skb, GET_TID(req),
  1534. (req->status & CPL_ABORT_NO_RST));
  1535. set_wr_txq(reply_skb, CPL_PRIORITY_DATA, req->status >> 1);
  1536. cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
  1537. kfree_skb(skb);
  1538. }
  1539. /*
  1540. * Add an skb to the deferred skb queue for processing from process context.
  1541. */
  1542. static void t4_defer_reply(struct sk_buff *skb, struct chtls_dev *cdev,
  1543. defer_handler_t handler)
  1544. {
  1545. DEFERRED_SKB_CB(skb)->handler = handler;
  1546. spin_lock_bh(&cdev->deferq.lock);
  1547. __skb_queue_tail(&cdev->deferq, skb);
  1548. if (skb_queue_len(&cdev->deferq) == 1)
  1549. schedule_work(&cdev->deferq_task);
  1550. spin_unlock_bh(&cdev->deferq.lock);
  1551. }
  1552. static void chtls_send_abort_rpl(struct sock *sk, struct sk_buff *skb,
  1553. struct chtls_dev *cdev,
  1554. int status, int queue)
  1555. {
  1556. struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
  1557. struct sk_buff *reply_skb;
  1558. struct chtls_sock *csk;
  1559. unsigned int tid;
  1560. csk = rcu_dereference_sk_user_data(sk);
  1561. tid = GET_TID(req);
  1562. reply_skb = get_cpl_skb(skb, sizeof(struct cpl_abort_rpl), gfp_any());
  1563. if (!reply_skb) {
  1564. req->status = (queue << 1) | status;
  1565. t4_defer_reply(skb, cdev, send_defer_abort_rpl);
  1566. return;
  1567. }
  1568. set_abort_rpl_wr(reply_skb, tid, status);
  1569. set_wr_txq(reply_skb, CPL_PRIORITY_DATA, queue);
  1570. if (csk_conn_inline(csk)) {
  1571. struct l2t_entry *e = csk->l2t_entry;
  1572. if (e && sk->sk_state != TCP_SYN_RECV) {
  1573. cxgb4_l2t_send(csk->egress_dev, reply_skb, e);
  1574. return;
  1575. }
  1576. }
  1577. cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
  1578. kfree_skb(skb);
  1579. }
  1580. /*
  1581. * This is run from a listener's backlog to abort a child connection in
  1582. * SYN_RCV state (i.e., one on the listener's SYN queue).
  1583. */
  1584. static void bl_abort_syn_rcv(struct sock *lsk, struct sk_buff *skb)
  1585. {
  1586. struct chtls_sock *csk;
  1587. struct sock *child;
  1588. int queue;
  1589. child = skb->sk;
  1590. csk = rcu_dereference_sk_user_data(child);
  1591. queue = csk->txq_idx;
  1592. skb->sk = NULL;
  1593. chtls_send_abort_rpl(child, skb, BLOG_SKB_CB(skb)->cdev,
  1594. CPL_ABORT_NO_RST, queue);
  1595. do_abort_syn_rcv(child, lsk);
  1596. }
  1597. static int abort_syn_rcv(struct sock *sk, struct sk_buff *skb)
  1598. {
  1599. const struct request_sock *oreq;
  1600. struct listen_ctx *listen_ctx;
  1601. struct chtls_sock *csk;
  1602. struct chtls_dev *cdev;
  1603. struct sock *psk;
  1604. void *ctx;
  1605. csk = sk->sk_user_data;
  1606. oreq = csk->passive_reap_next;
  1607. cdev = csk->cdev;
  1608. if (!oreq)
  1609. return -1;
  1610. ctx = lookup_stid(cdev->tids, oreq->ts_recent);
  1611. if (!ctx)
  1612. return -1;
  1613. listen_ctx = (struct listen_ctx *)ctx;
  1614. psk = listen_ctx->lsk;
  1615. bh_lock_sock(psk);
  1616. if (!sock_owned_by_user(psk)) {
  1617. int queue = csk->txq_idx;
  1618. chtls_send_abort_rpl(sk, skb, cdev, CPL_ABORT_NO_RST, queue);
  1619. do_abort_syn_rcv(sk, psk);
  1620. } else {
  1621. skb->sk = sk;
  1622. BLOG_SKB_CB(skb)->backlog_rcv = bl_abort_syn_rcv;
  1623. __sk_add_backlog(psk, skb);
  1624. }
  1625. bh_unlock_sock(psk);
  1626. return 0;
  1627. }
  1628. static void chtls_abort_req_rss(struct sock *sk, struct sk_buff *skb)
  1629. {
  1630. const struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
  1631. struct chtls_sock *csk = sk->sk_user_data;
  1632. int rst_status = CPL_ABORT_NO_RST;
  1633. int queue = csk->txq_idx;
  1634. if (is_neg_adv(req->status)) {
  1635. kfree_skb(skb);
  1636. return;
  1637. }
  1638. csk_reset_flag(csk, CSK_ABORT_REQ_RCVD);
  1639. if (!csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) &&
  1640. !csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
  1641. struct tcp_sock *tp = tcp_sk(sk);
  1642. if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
  1643. WARN_ONCE(1, "send_tx_flowc error");
  1644. csk_set_flag(csk, CSK_TX_DATA_SENT);
  1645. }
  1646. csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
  1647. if (!csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
  1648. sk->sk_err = ETIMEDOUT;
  1649. if (!sock_flag(sk, SOCK_DEAD))
  1650. sk->sk_error_report(sk);
  1651. if (sk->sk_state == TCP_SYN_RECV && !abort_syn_rcv(sk, skb))
  1652. return;
  1653. }
  1654. chtls_send_abort_rpl(sk, skb, csk->cdev, rst_status, queue);
  1655. chtls_release_resources(sk);
  1656. chtls_conn_done(sk);
  1657. }
  1658. static void chtls_abort_rpl_rss(struct sock *sk, struct sk_buff *skb)
  1659. {
  1660. struct cpl_abort_rpl_rss *rpl = cplhdr(skb) + RSS_HDR;
  1661. struct chtls_sock *csk;
  1662. struct chtls_dev *cdev;
  1663. csk = rcu_dereference_sk_user_data(sk);
  1664. cdev = csk->cdev;
  1665. if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
  1666. csk_reset_flag(csk, CSK_ABORT_RPL_PENDING);
  1667. if (!csk_flag_nochk(csk, CSK_ABORT_REQ_RCVD)) {
  1668. if (sk->sk_state == TCP_SYN_SENT) {
  1669. cxgb4_remove_tid(cdev->tids,
  1670. csk->port_id,
  1671. GET_TID(rpl),
  1672. sk->sk_family);
  1673. sock_put(sk);
  1674. }
  1675. chtls_release_resources(sk);
  1676. chtls_conn_done(sk);
  1677. }
  1678. }
  1679. kfree_skb(skb);
  1680. }
  1681. static int chtls_conn_cpl(struct chtls_dev *cdev, struct sk_buff *skb)
  1682. {
  1683. struct cpl_peer_close *req = cplhdr(skb) + RSS_HDR;
  1684. void (*fn)(struct sock *sk, struct sk_buff *skb);
  1685. unsigned int hwtid = GET_TID(req);
  1686. struct sock *sk;
  1687. u8 opcode;
  1688. opcode = ((const struct rss_header *)cplhdr(skb))->opcode;
  1689. sk = lookup_tid(cdev->tids, hwtid);
  1690. if (!sk)
  1691. goto rel_skb;
  1692. switch (opcode) {
  1693. case CPL_PEER_CLOSE:
  1694. fn = chtls_peer_close;
  1695. break;
  1696. case CPL_CLOSE_CON_RPL:
  1697. fn = chtls_close_con_rpl;
  1698. break;
  1699. case CPL_ABORT_REQ_RSS:
  1700. fn = chtls_abort_req_rss;
  1701. break;
  1702. case CPL_ABORT_RPL_RSS:
  1703. fn = chtls_abort_rpl_rss;
  1704. break;
  1705. default:
  1706. goto rel_skb;
  1707. }
  1708. process_cpl_msg(fn, sk, skb);
  1709. return 0;
  1710. rel_skb:
  1711. kfree_skb(skb);
  1712. return 0;
  1713. }
  1714. static void chtls_rx_ack(struct sock *sk, struct sk_buff *skb)
  1715. {
  1716. struct cpl_fw4_ack *hdr = cplhdr(skb) + RSS_HDR;
  1717. struct chtls_sock *csk = sk->sk_user_data;
  1718. struct tcp_sock *tp = tcp_sk(sk);
  1719. u32 credits = hdr->credits;
  1720. u32 snd_una;
  1721. snd_una = ntohl(hdr->snd_una);
  1722. csk->wr_credits += credits;
  1723. if (csk->wr_unacked > csk->wr_max_credits - csk->wr_credits)
  1724. csk->wr_unacked = csk->wr_max_credits - csk->wr_credits;
  1725. while (credits) {
  1726. struct sk_buff *pskb = csk->wr_skb_head;
  1727. u32 csum;
  1728. if (unlikely(!pskb)) {
  1729. if (csk->wr_nondata)
  1730. csk->wr_nondata -= credits;
  1731. break;
  1732. }
  1733. csum = (__force u32)pskb->csum;
  1734. if (unlikely(credits < csum)) {
  1735. pskb->csum = (__force __wsum)(csum - credits);
  1736. break;
  1737. }
  1738. dequeue_wr(sk);
  1739. credits -= csum;
  1740. kfree_skb(pskb);
  1741. }
  1742. if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
  1743. if (unlikely(before(snd_una, tp->snd_una))) {
  1744. kfree_skb(skb);
  1745. return;
  1746. }
  1747. if (tp->snd_una != snd_una) {
  1748. tp->snd_una = snd_una;
  1749. tp->rcv_tstamp = tcp_time_stamp(tp);
  1750. if (tp->snd_una == tp->snd_nxt &&
  1751. !csk_flag_nochk(csk, CSK_TX_FAILOVER))
  1752. csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
  1753. }
  1754. }
  1755. if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_CH) {
  1756. unsigned int fclen16 = roundup(failover_flowc_wr_len, 16);
  1757. csk->wr_credits -= fclen16;
  1758. csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
  1759. csk_reset_flag(csk, CSK_TX_FAILOVER);
  1760. }
  1761. if (skb_queue_len(&csk->txq) && chtls_push_frames(csk, 0))
  1762. sk->sk_write_space(sk);
  1763. kfree_skb(skb);
  1764. }
  1765. static int chtls_wr_ack(struct chtls_dev *cdev, struct sk_buff *skb)
  1766. {
  1767. struct cpl_fw4_ack *rpl = cplhdr(skb) + RSS_HDR;
  1768. unsigned int hwtid = GET_TID(rpl);
  1769. struct sock *sk;
  1770. sk = lookup_tid(cdev->tids, hwtid);
  1771. if (unlikely(!sk)) {
  1772. pr_err("can't find conn. for hwtid %u.\n", hwtid);
  1773. return -EINVAL;
  1774. }
  1775. process_cpl_msg(chtls_rx_ack, sk, skb);
  1776. return 0;
  1777. }
  1778. chtls_handler_func chtls_handlers[NUM_CPL_CMDS] = {
  1779. [CPL_PASS_OPEN_RPL] = chtls_pass_open_rpl,
  1780. [CPL_CLOSE_LISTSRV_RPL] = chtls_close_listsrv_rpl,
  1781. [CPL_PASS_ACCEPT_REQ] = chtls_pass_accept_req,
  1782. [CPL_PASS_ESTABLISH] = chtls_pass_establish,
  1783. [CPL_RX_DATA] = chtls_rx_data,
  1784. [CPL_TLS_DATA] = chtls_rx_pdu,
  1785. [CPL_RX_TLS_CMP] = chtls_rx_cmp,
  1786. [CPL_PEER_CLOSE] = chtls_conn_cpl,
  1787. [CPL_CLOSE_CON_RPL] = chtls_conn_cpl,
  1788. [CPL_ABORT_REQ_RSS] = chtls_conn_cpl,
  1789. [CPL_ABORT_RPL_RSS] = chtls_conn_cpl,
  1790. [CPL_FW4_ACK] = chtls_wr_ack,
  1791. };