llc_conn.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * llc_conn.c - Driver routines for connection component.
  3. *
  4. * Copyright (c) 1997 by Procom Technology, Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program can be redistributed or modified under the terms of the
  8. * GNU General Public License as published by the Free Software Foundation.
  9. * This program is distributed without any warranty or implied warranty
  10. * of merchantability or fitness for a particular purpose.
  11. *
  12. * See the GNU General Public License for more details.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <net/llc_sap.h>
  17. #include <net/llc_conn.h>
  18. #include <net/sock.h>
  19. #include <net/tcp_states.h>
  20. #include <net/llc_c_ev.h>
  21. #include <net/llc_c_ac.h>
  22. #include <net/llc_c_st.h>
  23. #include <net/llc_pdu.h>
  24. #if 0
  25. #define dprintk(args...) printk(KERN_DEBUG args)
  26. #else
  27. #define dprintk(args...)
  28. #endif
  29. static int llc_find_offset(int state, int ev_type);
  30. static void llc_conn_send_pdus(struct sock *sk);
  31. static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
  32. static int llc_exec_conn_trans_actions(struct sock *sk,
  33. struct llc_conn_state_trans *trans,
  34. struct sk_buff *ev);
  35. static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  36. struct sk_buff *skb);
  37. /* Offset table on connection states transition diagram */
  38. static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
  39. int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
  40. int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
  41. int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
  42. int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
  43. /**
  44. * llc_conn_state_process - sends event to connection state machine
  45. * @sk: connection
  46. * @skb: occurred event
  47. *
  48. * Sends an event to connection state machine. After processing event
  49. * (executing it's actions and changing state), upper layer will be
  50. * indicated or confirmed, if needed. Returns 0 for success, 1 for
  51. * failure. The socket lock has to be held before calling this function.
  52. *
  53. * This function always consumes a reference to the skb.
  54. */
  55. int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
  56. {
  57. int rc;
  58. struct llc_sock *llc = llc_sk(skb->sk);
  59. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  60. ev->ind_prim = ev->cfm_prim = 0;
  61. /*
  62. * Send event to state machine
  63. */
  64. rc = llc_conn_service(skb->sk, skb);
  65. if (unlikely(rc != 0)) {
  66. printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
  67. goto out_skb_put;
  68. }
  69. switch (ev->ind_prim) {
  70. case LLC_DATA_PRIM:
  71. skb_get(skb);
  72. llc_save_primitive(sk, skb, LLC_DATA_PRIM);
  73. if (unlikely(sock_queue_rcv_skb(sk, skb))) {
  74. /*
  75. * shouldn't happen
  76. */
  77. printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
  78. __func__);
  79. kfree_skb(skb);
  80. }
  81. break;
  82. case LLC_CONN_PRIM:
  83. /*
  84. * Can't be sock_queue_rcv_skb, because we have to leave the
  85. * skb->sk pointing to the newly created struct sock in
  86. * llc_conn_handler. -acme
  87. */
  88. skb_get(skb);
  89. skb_queue_tail(&sk->sk_receive_queue, skb);
  90. sk->sk_state_change(sk);
  91. break;
  92. case LLC_DISC_PRIM:
  93. sock_hold(sk);
  94. if (sk->sk_type == SOCK_STREAM &&
  95. sk->sk_state == TCP_ESTABLISHED) {
  96. sk->sk_shutdown = SHUTDOWN_MASK;
  97. sk->sk_socket->state = SS_UNCONNECTED;
  98. sk->sk_state = TCP_CLOSE;
  99. if (!sock_flag(sk, SOCK_DEAD)) {
  100. sock_set_flag(sk, SOCK_DEAD);
  101. sk->sk_state_change(sk);
  102. }
  103. }
  104. sock_put(sk);
  105. break;
  106. case LLC_RESET_PRIM:
  107. /*
  108. * FIXME:
  109. * RESET is not being notified to upper layers for now
  110. */
  111. printk(KERN_INFO "%s: received a reset ind!\n", __func__);
  112. break;
  113. default:
  114. if (ev->ind_prim)
  115. printk(KERN_INFO "%s: received unknown %d prim!\n",
  116. __func__, ev->ind_prim);
  117. /* No indication */
  118. break;
  119. }
  120. switch (ev->cfm_prim) {
  121. case LLC_DATA_PRIM:
  122. if (!llc_data_accept_state(llc->state))
  123. sk->sk_write_space(sk);
  124. else
  125. rc = llc->failed_data_req = 1;
  126. break;
  127. case LLC_CONN_PRIM:
  128. if (sk->sk_type == SOCK_STREAM &&
  129. sk->sk_state == TCP_SYN_SENT) {
  130. if (ev->status) {
  131. sk->sk_socket->state = SS_UNCONNECTED;
  132. sk->sk_state = TCP_CLOSE;
  133. } else {
  134. sk->sk_socket->state = SS_CONNECTED;
  135. sk->sk_state = TCP_ESTABLISHED;
  136. }
  137. sk->sk_state_change(sk);
  138. }
  139. break;
  140. case LLC_DISC_PRIM:
  141. sock_hold(sk);
  142. if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
  143. sk->sk_socket->state = SS_UNCONNECTED;
  144. sk->sk_state = TCP_CLOSE;
  145. sk->sk_state_change(sk);
  146. }
  147. sock_put(sk);
  148. break;
  149. case LLC_RESET_PRIM:
  150. /*
  151. * FIXME:
  152. * RESET is not being notified to upper layers for now
  153. */
  154. printk(KERN_INFO "%s: received a reset conf!\n", __func__);
  155. break;
  156. default:
  157. if (ev->cfm_prim)
  158. printk(KERN_INFO "%s: received unknown %d prim!\n",
  159. __func__, ev->cfm_prim);
  160. /* No confirmation */
  161. break;
  162. }
  163. out_skb_put:
  164. kfree_skb(skb);
  165. return rc;
  166. }
  167. void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
  168. {
  169. /* queue PDU to send to MAC layer */
  170. skb_queue_tail(&sk->sk_write_queue, skb);
  171. llc_conn_send_pdus(sk);
  172. }
  173. /**
  174. * llc_conn_rtn_pdu - sends received data pdu to upper layer
  175. * @sk: Active connection
  176. * @skb: Received data frame
  177. *
  178. * Sends received data pdu to upper layer (by using indicate function).
  179. * Prepares service parameters (prim and prim_data). calling indication
  180. * function will be done in llc_conn_state_process.
  181. */
  182. void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
  183. {
  184. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  185. ev->ind_prim = LLC_DATA_PRIM;
  186. }
  187. /**
  188. * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
  189. * @sk: active connection
  190. * @nr: NR
  191. * @first_p_bit: p_bit value of first pdu
  192. *
  193. * Resend all unacknowledged I PDUs, starting with the NR; send first as
  194. * command PDU with P bit equal first_p_bit; if more than one send
  195. * subsequent as command PDUs with P bit equal zero (0).
  196. */
  197. void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
  198. {
  199. struct sk_buff *skb;
  200. struct llc_pdu_sn *pdu;
  201. u16 nbr_unack_pdus;
  202. struct llc_sock *llc;
  203. u8 howmany_resend = 0;
  204. llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
  205. if (!nbr_unack_pdus)
  206. goto out;
  207. /*
  208. * Process unack PDUs only if unack queue is not empty; remove
  209. * appropriate PDUs, fix them up, and put them on mac_pdu_q.
  210. */
  211. llc = llc_sk(sk);
  212. while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
  213. pdu = llc_pdu_sn_hdr(skb);
  214. llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
  215. llc_pdu_set_pf_bit(skb, first_p_bit);
  216. skb_queue_tail(&sk->sk_write_queue, skb);
  217. first_p_bit = 0;
  218. llc->vS = LLC_I_GET_NS(pdu);
  219. howmany_resend++;
  220. }
  221. if (howmany_resend > 0)
  222. llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
  223. /* any PDUs to re-send are queued up; start sending to MAC */
  224. llc_conn_send_pdus(sk);
  225. out:;
  226. }
  227. /**
  228. * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
  229. * @sk: active connection.
  230. * @nr: NR
  231. * @first_f_bit: f_bit value of first pdu.
  232. *
  233. * Resend all unacknowledged I PDUs, starting with the NR; send first as
  234. * response PDU with F bit equal first_f_bit; if more than one send
  235. * subsequent as response PDUs with F bit equal zero (0).
  236. */
  237. void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
  238. {
  239. struct sk_buff *skb;
  240. u16 nbr_unack_pdus;
  241. struct llc_sock *llc = llc_sk(sk);
  242. u8 howmany_resend = 0;
  243. llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
  244. if (!nbr_unack_pdus)
  245. goto out;
  246. /*
  247. * Process unack PDUs only if unack queue is not empty; remove
  248. * appropriate PDUs, fix them up, and put them on mac_pdu_q
  249. */
  250. while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
  251. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  252. llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
  253. llc_pdu_set_pf_bit(skb, first_f_bit);
  254. skb_queue_tail(&sk->sk_write_queue, skb);
  255. first_f_bit = 0;
  256. llc->vS = LLC_I_GET_NS(pdu);
  257. howmany_resend++;
  258. }
  259. if (howmany_resend > 0)
  260. llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
  261. /* any PDUs to re-send are queued up; start sending to MAC */
  262. llc_conn_send_pdus(sk);
  263. out:;
  264. }
  265. /**
  266. * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
  267. * @sk: active connection
  268. * nr: NR
  269. * how_many_unacked: size of pdu_unack_q after removing acked pdus
  270. *
  271. * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
  272. * the number of pdus that removed from queue.
  273. */
  274. int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
  275. {
  276. int pdu_pos, i;
  277. struct sk_buff *skb;
  278. struct llc_pdu_sn *pdu;
  279. int nbr_acked = 0;
  280. struct llc_sock *llc = llc_sk(sk);
  281. int q_len = skb_queue_len(&llc->pdu_unack_q);
  282. if (!q_len)
  283. goto out;
  284. skb = skb_peek(&llc->pdu_unack_q);
  285. pdu = llc_pdu_sn_hdr(skb);
  286. /* finding position of last acked pdu in queue */
  287. pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
  288. (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
  289. for (i = 0; i < pdu_pos && i < q_len; i++) {
  290. skb = skb_dequeue(&llc->pdu_unack_q);
  291. kfree_skb(skb);
  292. nbr_acked++;
  293. }
  294. out:
  295. *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
  296. return nbr_acked;
  297. }
  298. /**
  299. * llc_conn_send_pdus - Sends queued PDUs
  300. * @sk: active connection
  301. *
  302. * Sends queued pdus to MAC layer for transmission.
  303. */
  304. static void llc_conn_send_pdus(struct sock *sk)
  305. {
  306. struct sk_buff *skb;
  307. while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
  308. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  309. if (LLC_PDU_TYPE_IS_I(pdu) &&
  310. !(skb->dev->flags & IFF_LOOPBACK)) {
  311. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  312. skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
  313. if (!skb2)
  314. break;
  315. skb = skb2;
  316. }
  317. dev_queue_xmit(skb);
  318. }
  319. }
  320. /**
  321. * llc_conn_service - finds transition and changes state of connection
  322. * @sk: connection
  323. * @skb: happened event
  324. *
  325. * This function finds transition that matches with happened event, then
  326. * executes related actions and finally changes state of connection.
  327. * Returns 0 for success, 1 for failure.
  328. */
  329. static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
  330. {
  331. int rc = 1;
  332. struct llc_sock *llc = llc_sk(sk);
  333. struct llc_conn_state_trans *trans;
  334. if (llc->state > NBR_CONN_STATES)
  335. goto out;
  336. rc = 0;
  337. trans = llc_qualify_conn_ev(sk, skb);
  338. if (trans) {
  339. rc = llc_exec_conn_trans_actions(sk, trans, skb);
  340. if (!rc && trans->next_state != NO_STATE_CHANGE) {
  341. llc->state = trans->next_state;
  342. if (!llc_data_accept_state(llc->state))
  343. sk->sk_state_change(sk);
  344. }
  345. }
  346. out:
  347. return rc;
  348. }
  349. /**
  350. * llc_qualify_conn_ev - finds transition for event
  351. * @sk: connection
  352. * @skb: happened event
  353. *
  354. * This function finds transition that matches with happened event.
  355. * Returns pointer to found transition on success, %NULL otherwise.
  356. */
  357. static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  358. struct sk_buff *skb)
  359. {
  360. struct llc_conn_state_trans **next_trans;
  361. const llc_conn_ev_qfyr_t *next_qualifier;
  362. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  363. struct llc_sock *llc = llc_sk(sk);
  364. struct llc_conn_state *curr_state =
  365. &llc_conn_state_table[llc->state - 1];
  366. /* search thru events for this state until
  367. * list exhausted or until no more
  368. */
  369. for (next_trans = curr_state->transitions +
  370. llc_find_offset(llc->state - 1, ev->type);
  371. (*next_trans)->ev; next_trans++) {
  372. if (!((*next_trans)->ev)(sk, skb)) {
  373. /* got POSSIBLE event match; the event may require
  374. * qualification based on the values of a number of
  375. * state flags; if all qualifications are met (i.e.,
  376. * if all qualifying functions return success, or 0,
  377. * then this is THE event we're looking for
  378. */
  379. for (next_qualifier = (*next_trans)->ev_qualifiers;
  380. next_qualifier && *next_qualifier &&
  381. !(*next_qualifier)(sk, skb); next_qualifier++)
  382. /* nothing */;
  383. if (!next_qualifier || !*next_qualifier)
  384. /* all qualifiers executed successfully; this is
  385. * our transition; return it so we can perform
  386. * the associated actions & change the state
  387. */
  388. return *next_trans;
  389. }
  390. }
  391. return NULL;
  392. }
  393. /**
  394. * llc_exec_conn_trans_actions - executes related actions
  395. * @sk: connection
  396. * @trans: transition that it's actions must be performed
  397. * @skb: event
  398. *
  399. * Executes actions that is related to happened event. Returns 0 for
  400. * success, 1 to indicate failure of at least one action.
  401. */
  402. static int llc_exec_conn_trans_actions(struct sock *sk,
  403. struct llc_conn_state_trans *trans,
  404. struct sk_buff *skb)
  405. {
  406. int rc = 0;
  407. const llc_conn_action_t *next_action;
  408. for (next_action = trans->ev_actions;
  409. next_action && *next_action; next_action++) {
  410. int rc2 = (*next_action)(sk, skb);
  411. if (rc2 == 2) {
  412. rc = rc2;
  413. break;
  414. } else if (rc2)
  415. rc = 1;
  416. }
  417. return rc;
  418. }
  419. static inline bool llc_estab_match(const struct llc_sap *sap,
  420. const struct llc_addr *daddr,
  421. const struct llc_addr *laddr,
  422. const struct sock *sk)
  423. {
  424. struct llc_sock *llc = llc_sk(sk);
  425. return llc->laddr.lsap == laddr->lsap &&
  426. llc->daddr.lsap == daddr->lsap &&
  427. ether_addr_equal(llc->laddr.mac, laddr->mac) &&
  428. ether_addr_equal(llc->daddr.mac, daddr->mac);
  429. }
  430. /**
  431. * __llc_lookup_established - Finds connection for the remote/local sap/mac
  432. * @sap: SAP
  433. * @daddr: address of remote LLC (MAC + SAP)
  434. * @laddr: address of local LLC (MAC + SAP)
  435. *
  436. * Search connection list of the SAP and finds connection using the remote
  437. * mac, remote sap, local mac, and local sap. Returns pointer for
  438. * connection found, %NULL otherwise.
  439. * Caller has to make sure local_bh is disabled.
  440. */
  441. static struct sock *__llc_lookup_established(struct llc_sap *sap,
  442. struct llc_addr *daddr,
  443. struct llc_addr *laddr)
  444. {
  445. struct sock *rc;
  446. struct hlist_nulls_node *node;
  447. int slot = llc_sk_laddr_hashfn(sap, laddr);
  448. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  449. rcu_read_lock();
  450. again:
  451. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  452. if (llc_estab_match(sap, daddr, laddr, rc)) {
  453. /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
  454. if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
  455. goto again;
  456. if (unlikely(llc_sk(rc)->sap != sap ||
  457. !llc_estab_match(sap, daddr, laddr, rc))) {
  458. sock_put(rc);
  459. continue;
  460. }
  461. goto found;
  462. }
  463. }
  464. rc = NULL;
  465. /*
  466. * if the nulls value we got at the end of this lookup is
  467. * not the expected one, we must restart lookup.
  468. * We probably met an item that was moved to another chain.
  469. */
  470. if (unlikely(get_nulls_value(node) != slot))
  471. goto again;
  472. found:
  473. rcu_read_unlock();
  474. return rc;
  475. }
  476. struct sock *llc_lookup_established(struct llc_sap *sap,
  477. struct llc_addr *daddr,
  478. struct llc_addr *laddr)
  479. {
  480. struct sock *sk;
  481. local_bh_disable();
  482. sk = __llc_lookup_established(sap, daddr, laddr);
  483. local_bh_enable();
  484. return sk;
  485. }
  486. static inline bool llc_listener_match(const struct llc_sap *sap,
  487. const struct llc_addr *laddr,
  488. const struct sock *sk)
  489. {
  490. struct llc_sock *llc = llc_sk(sk);
  491. return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
  492. llc->laddr.lsap == laddr->lsap &&
  493. ether_addr_equal(llc->laddr.mac, laddr->mac);
  494. }
  495. static struct sock *__llc_lookup_listener(struct llc_sap *sap,
  496. struct llc_addr *laddr)
  497. {
  498. struct sock *rc;
  499. struct hlist_nulls_node *node;
  500. int slot = llc_sk_laddr_hashfn(sap, laddr);
  501. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  502. rcu_read_lock();
  503. again:
  504. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  505. if (llc_listener_match(sap, laddr, rc)) {
  506. /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
  507. if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
  508. goto again;
  509. if (unlikely(llc_sk(rc)->sap != sap ||
  510. !llc_listener_match(sap, laddr, rc))) {
  511. sock_put(rc);
  512. continue;
  513. }
  514. goto found;
  515. }
  516. }
  517. rc = NULL;
  518. /*
  519. * if the nulls value we got at the end of this lookup is
  520. * not the expected one, we must restart lookup.
  521. * We probably met an item that was moved to another chain.
  522. */
  523. if (unlikely(get_nulls_value(node) != slot))
  524. goto again;
  525. found:
  526. rcu_read_unlock();
  527. return rc;
  528. }
  529. /**
  530. * llc_lookup_listener - Finds listener for local MAC + SAP
  531. * @sap: SAP
  532. * @laddr: address of local LLC (MAC + SAP)
  533. *
  534. * Search connection list of the SAP and finds connection listening on
  535. * local mac, and local sap. Returns pointer for parent socket found,
  536. * %NULL otherwise.
  537. * Caller has to make sure local_bh is disabled.
  538. */
  539. static struct sock *llc_lookup_listener(struct llc_sap *sap,
  540. struct llc_addr *laddr)
  541. {
  542. static struct llc_addr null_addr;
  543. struct sock *rc = __llc_lookup_listener(sap, laddr);
  544. if (!rc)
  545. rc = __llc_lookup_listener(sap, &null_addr);
  546. return rc;
  547. }
  548. static struct sock *__llc_lookup(struct llc_sap *sap,
  549. struct llc_addr *daddr,
  550. struct llc_addr *laddr)
  551. {
  552. struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
  553. return sk ? : llc_lookup_listener(sap, laddr);
  554. }
  555. /**
  556. * llc_data_accept_state - designates if in this state data can be sent.
  557. * @state: state of connection.
  558. *
  559. * Returns 0 if data can be sent, 1 otherwise.
  560. */
  561. u8 llc_data_accept_state(u8 state)
  562. {
  563. return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
  564. state != LLC_CONN_STATE_REJ;
  565. }
  566. /**
  567. * llc_find_next_offset - finds offset for next category of transitions
  568. * @state: state table.
  569. * @offset: start offset.
  570. *
  571. * Finds offset of next category of transitions in transition table.
  572. * Returns the start index of next category.
  573. */
  574. static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
  575. {
  576. u16 cnt = 0;
  577. struct llc_conn_state_trans **next_trans;
  578. for (next_trans = state->transitions + offset;
  579. (*next_trans)->ev; next_trans++)
  580. ++cnt;
  581. return cnt;
  582. }
  583. /**
  584. * llc_build_offset_table - builds offset table of connection
  585. *
  586. * Fills offset table of connection state transition table
  587. * (llc_offset_table).
  588. */
  589. void __init llc_build_offset_table(void)
  590. {
  591. struct llc_conn_state *curr_state;
  592. int state, ev_type, next_offset;
  593. for (state = 0; state < NBR_CONN_STATES; state++) {
  594. curr_state = &llc_conn_state_table[state];
  595. next_offset = 0;
  596. for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
  597. llc_offset_table[state][ev_type] = next_offset;
  598. next_offset += llc_find_next_offset(curr_state,
  599. next_offset) + 1;
  600. }
  601. }
  602. }
  603. /**
  604. * llc_find_offset - finds start offset of category of transitions
  605. * @state: state of connection
  606. * @ev_type: type of happened event
  607. *
  608. * Finds start offset of desired category of transitions. Returns the
  609. * desired start offset.
  610. */
  611. static int llc_find_offset(int state, int ev_type)
  612. {
  613. int rc = 0;
  614. /* at this stage, llc_offset_table[..][2] is not important. it is for
  615. * init_pf_cycle and I don't know what is it.
  616. */
  617. switch (ev_type) {
  618. case LLC_CONN_EV_TYPE_PRIM:
  619. rc = llc_offset_table[state][0]; break;
  620. case LLC_CONN_EV_TYPE_PDU:
  621. rc = llc_offset_table[state][4]; break;
  622. case LLC_CONN_EV_TYPE_SIMPLE:
  623. rc = llc_offset_table[state][1]; break;
  624. case LLC_CONN_EV_TYPE_P_TMR:
  625. case LLC_CONN_EV_TYPE_ACK_TMR:
  626. case LLC_CONN_EV_TYPE_REJ_TMR:
  627. case LLC_CONN_EV_TYPE_BUSY_TMR:
  628. rc = llc_offset_table[state][3]; break;
  629. }
  630. return rc;
  631. }
  632. /**
  633. * llc_sap_add_socket - adds a socket to a SAP
  634. * @sap: SAP
  635. * @sk: socket
  636. *
  637. * This function adds a socket to the hash tables of a SAP.
  638. */
  639. void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  640. {
  641. struct llc_sock *llc = llc_sk(sk);
  642. struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
  643. struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
  644. llc_sap_hold(sap);
  645. llc_sk(sk)->sap = sap;
  646. spin_lock_bh(&sap->sk_lock);
  647. sock_set_flag(sk, SOCK_RCU_FREE);
  648. sap->sk_count++;
  649. sk_nulls_add_node_rcu(sk, laddr_hb);
  650. hlist_add_head(&llc->dev_hash_node, dev_hb);
  651. spin_unlock_bh(&sap->sk_lock);
  652. }
  653. /**
  654. * llc_sap_remove_socket - removes a socket from SAP
  655. * @sap: SAP
  656. * @sk: socket
  657. *
  658. * This function removes a connection from the hash tables of a SAP if
  659. * the connection was in this list.
  660. */
  661. void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
  662. {
  663. struct llc_sock *llc = llc_sk(sk);
  664. spin_lock_bh(&sap->sk_lock);
  665. sk_nulls_del_node_init_rcu(sk);
  666. hlist_del(&llc->dev_hash_node);
  667. sap->sk_count--;
  668. spin_unlock_bh(&sap->sk_lock);
  669. llc_sap_put(sap);
  670. }
  671. /**
  672. * llc_conn_rcv - sends received pdus to the connection state machine
  673. * @sk: current connection structure.
  674. * @skb: received frame.
  675. *
  676. * Sends received pdus to the connection state machine.
  677. */
  678. static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
  679. {
  680. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  681. ev->type = LLC_CONN_EV_TYPE_PDU;
  682. ev->reason = 0;
  683. return llc_conn_state_process(sk, skb);
  684. }
  685. static struct sock *llc_create_incoming_sock(struct sock *sk,
  686. struct net_device *dev,
  687. struct llc_addr *saddr,
  688. struct llc_addr *daddr)
  689. {
  690. struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
  691. sk->sk_prot, 0);
  692. struct llc_sock *newllc, *llc = llc_sk(sk);
  693. if (!newsk)
  694. goto out;
  695. newllc = llc_sk(newsk);
  696. memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
  697. memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
  698. newllc->dev = dev;
  699. dev_hold(dev);
  700. llc_sap_add_socket(llc->sap, newsk);
  701. llc_sap_hold(llc->sap);
  702. out:
  703. return newsk;
  704. }
  705. void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
  706. {
  707. struct llc_addr saddr, daddr;
  708. struct sock *sk;
  709. llc_pdu_decode_sa(skb, saddr.mac);
  710. llc_pdu_decode_ssap(skb, &saddr.lsap);
  711. llc_pdu_decode_da(skb, daddr.mac);
  712. llc_pdu_decode_dsap(skb, &daddr.lsap);
  713. sk = __llc_lookup(sap, &saddr, &daddr);
  714. if (!sk)
  715. goto drop;
  716. bh_lock_sock(sk);
  717. /*
  718. * This has to be done here and not at the upper layer ->accept
  719. * method because of the way the PROCOM state machine works:
  720. * it needs to set several state variables (see, for instance,
  721. * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
  722. * the originator of the new connection, and this state has to be
  723. * in the newly created struct sock private area. -acme
  724. */
  725. if (unlikely(sk->sk_state == TCP_LISTEN)) {
  726. struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
  727. &saddr, &daddr);
  728. if (!newsk)
  729. goto drop_unlock;
  730. skb_set_owner_r(skb, newsk);
  731. } else {
  732. /*
  733. * Can't be skb_set_owner_r, this will be done at the
  734. * llc_conn_state_process function, later on, when we will use
  735. * skb_queue_rcv_skb to send it to upper layers, this is
  736. * another trick required to cope with how the PROCOM state
  737. * machine works. -acme
  738. */
  739. skb_orphan(skb);
  740. sock_hold(sk);
  741. skb->sk = sk;
  742. skb->destructor = sock_efree;
  743. }
  744. if (!sock_owned_by_user(sk))
  745. llc_conn_rcv(sk, skb);
  746. else {
  747. dprintk("%s: adding to backlog...\n", __func__);
  748. llc_set_backlog_type(skb, LLC_PACKET);
  749. if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
  750. goto drop_unlock;
  751. }
  752. out:
  753. bh_unlock_sock(sk);
  754. sock_put(sk);
  755. return;
  756. drop:
  757. kfree_skb(skb);
  758. return;
  759. drop_unlock:
  760. kfree_skb(skb);
  761. goto out;
  762. }
  763. #undef LLC_REFCNT_DEBUG
  764. #ifdef LLC_REFCNT_DEBUG
  765. static atomic_t llc_sock_nr;
  766. #endif
  767. /**
  768. * llc_backlog_rcv - Processes rx frames and expired timers.
  769. * @sk: LLC sock (p8022 connection)
  770. * @skb: queued rx frame or event
  771. *
  772. * This function processes frames that has received and timers that has
  773. * expired during sending an I pdu (refer to data_req_handler). frames
  774. * queue by llc_rcv function (llc_mac.c) and timers queue by timer
  775. * callback functions(llc_c_ac.c).
  776. */
  777. static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  778. {
  779. int rc = 0;
  780. struct llc_sock *llc = llc_sk(sk);
  781. if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
  782. if (likely(llc->state > 1)) /* not closed */
  783. rc = llc_conn_rcv(sk, skb);
  784. else
  785. goto out_kfree_skb;
  786. } else if (llc_backlog_type(skb) == LLC_EVENT) {
  787. /* timer expiration event */
  788. if (likely(llc->state > 1)) /* not closed */
  789. rc = llc_conn_state_process(sk, skb);
  790. else
  791. goto out_kfree_skb;
  792. } else {
  793. printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
  794. goto out_kfree_skb;
  795. }
  796. out:
  797. return rc;
  798. out_kfree_skb:
  799. kfree_skb(skb);
  800. goto out;
  801. }
  802. /**
  803. * llc_sk_init - Initializes a socket with default llc values.
  804. * @sk: socket to initialize.
  805. *
  806. * Initializes a socket with default llc values.
  807. */
  808. static void llc_sk_init(struct sock *sk)
  809. {
  810. struct llc_sock *llc = llc_sk(sk);
  811. llc->state = LLC_CONN_STATE_ADM;
  812. llc->inc_cntr = llc->dec_cntr = 2;
  813. llc->dec_step = llc->connect_step = 1;
  814. setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
  815. (unsigned long)sk);
  816. llc->ack_timer.expire = sysctl_llc2_ack_timeout;
  817. setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
  818. (unsigned long)sk);
  819. llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
  820. setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
  821. (unsigned long)sk);
  822. llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
  823. setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
  824. (unsigned long)sk);
  825. llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
  826. llc->n2 = 2; /* max retransmit */
  827. llc->k = 2; /* tx win size, will adjust dynam */
  828. llc->rw = 128; /* rx win size (opt and equal to
  829. * tx_win of remote LLC) */
  830. skb_queue_head_init(&llc->pdu_unack_q);
  831. sk->sk_backlog_rcv = llc_backlog_rcv;
  832. }
  833. /**
  834. * llc_sk_alloc - Allocates LLC sock
  835. * @family: upper layer protocol family
  836. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  837. *
  838. * Allocates a LLC sock and initializes it. Returns the new LLC sock
  839. * or %NULL if there's no memory available for one
  840. */
  841. struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
  842. {
  843. struct sock *sk = sk_alloc(net, family, priority, prot, kern);
  844. if (!sk)
  845. goto out;
  846. llc_sk_init(sk);
  847. sock_init_data(NULL, sk);
  848. #ifdef LLC_REFCNT_DEBUG
  849. atomic_inc(&llc_sock_nr);
  850. printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
  851. __func__, atomic_read(&llc_sock_nr));
  852. #endif
  853. out:
  854. return sk;
  855. }
  856. void llc_sk_stop_all_timers(struct sock *sk, bool sync)
  857. {
  858. struct llc_sock *llc = llc_sk(sk);
  859. if (sync) {
  860. del_timer_sync(&llc->pf_cycle_timer.timer);
  861. del_timer_sync(&llc->ack_timer.timer);
  862. del_timer_sync(&llc->rej_sent_timer.timer);
  863. del_timer_sync(&llc->busy_state_timer.timer);
  864. } else {
  865. del_timer(&llc->pf_cycle_timer.timer);
  866. del_timer(&llc->ack_timer.timer);
  867. del_timer(&llc->rej_sent_timer.timer);
  868. del_timer(&llc->busy_state_timer.timer);
  869. }
  870. llc->ack_must_be_send = 0;
  871. llc->ack_pf = 0;
  872. }
  873. /**
  874. * llc_sk_free - Frees a LLC socket
  875. * @sk - socket to free
  876. *
  877. * Frees a LLC socket
  878. */
  879. void llc_sk_free(struct sock *sk)
  880. {
  881. struct llc_sock *llc = llc_sk(sk);
  882. llc->state = LLC_CONN_OUT_OF_SVC;
  883. /* Stop all (possibly) running timers */
  884. llc_sk_stop_all_timers(sk, true);
  885. #ifdef DEBUG_LLC_CONN_ALLOC
  886. printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
  887. skb_queue_len(&llc->pdu_unack_q),
  888. skb_queue_len(&sk->sk_write_queue));
  889. #endif
  890. skb_queue_purge(&sk->sk_receive_queue);
  891. skb_queue_purge(&sk->sk_write_queue);
  892. skb_queue_purge(&llc->pdu_unack_q);
  893. #ifdef LLC_REFCNT_DEBUG
  894. if (refcount_read(&sk->sk_refcnt) != 1) {
  895. printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
  896. sk, __func__, refcount_read(&sk->sk_refcnt));
  897. printk(KERN_DEBUG "%d LLC sockets are still alive\n",
  898. atomic_read(&llc_sock_nr));
  899. } else {
  900. atomic_dec(&llc_sock_nr);
  901. printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
  902. __func__, atomic_read(&llc_sock_nr));
  903. }
  904. #endif
  905. sock_put(sk);
  906. }
  907. /**
  908. * llc_sk_reset - resets a connection
  909. * @sk: LLC socket to reset
  910. *
  911. * Resets a connection to the out of service state. Stops its timers
  912. * and frees any frames in the queues of the connection.
  913. */
  914. void llc_sk_reset(struct sock *sk)
  915. {
  916. struct llc_sock *llc = llc_sk(sk);
  917. llc_conn_ac_stop_all_timers(sk, NULL);
  918. skb_queue_purge(&sk->sk_write_queue);
  919. skb_queue_purge(&llc->pdu_unack_q);
  920. llc->remote_busy_flag = 0;
  921. llc->cause_flag = 0;
  922. llc->retry_count = 0;
  923. llc_conn_set_p_flag(sk, 0);
  924. llc->f_flag = 0;
  925. llc->s_flag = 0;
  926. llc->ack_pf = 0;
  927. llc->first_pdu_Ns = 0;
  928. llc->ack_must_be_send = 0;
  929. llc->dec_step = 1;
  930. llc->inc_cntr = 2;
  931. llc->dec_cntr = 2;
  932. llc->X = 0;
  933. llc->failed_data_req = 0 ;
  934. llc->last_nr = 0;
  935. }