llc_conn.c 25 KB

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