llc_conn.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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(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. rc = llc_conn_service(sk, skb); /* sending event to state machine */
  65. if (unlikely(rc != 0)) {
  66. printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
  67. goto out_kfree_skb;
  68. }
  69. if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
  70. /* indicate or confirm not required */
  71. /* XXX this is not very pretty, perhaps we should store
  72. * XXX indicate/confirm-needed state in the llc_conn_state_ev
  73. * XXX control block of the SKB instead? -DaveM
  74. */
  75. if (!skb->next)
  76. goto out_kfree_skb;
  77. goto out_skb_put;
  78. }
  79. if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
  80. skb_get(skb);
  81. switch (ev->ind_prim) {
  82. case LLC_DATA_PRIM:
  83. llc_save_primitive(skb, LLC_DATA_PRIM);
  84. if (sock_queue_rcv_skb(sk, skb)) {
  85. /*
  86. * shouldn't happen
  87. */
  88. printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
  89. __FUNCTION__);
  90. kfree_skb(skb);
  91. }
  92. break;
  93. case LLC_CONN_PRIM: {
  94. struct sock *parent = skb->sk;
  95. skb->sk = sk;
  96. skb_queue_tail(&parent->sk_receive_queue, skb);
  97. sk->sk_state_change(parent);
  98. }
  99. break;
  100. case LLC_DISC_PRIM:
  101. sock_hold(sk);
  102. if (sk->sk_type == SOCK_STREAM &&
  103. sk->sk_state == TCP_ESTABLISHED) {
  104. sk->sk_shutdown = SHUTDOWN_MASK;
  105. sk->sk_socket->state = SS_UNCONNECTED;
  106. sk->sk_state = TCP_CLOSE;
  107. if (!sock_flag(sk, SOCK_DEAD)) {
  108. sk->sk_state_change(sk);
  109. sock_set_flag(sk, SOCK_DEAD);
  110. }
  111. }
  112. kfree_skb(skb);
  113. sock_put(sk);
  114. break;
  115. case LLC_RESET_PRIM:
  116. /*
  117. * FIXME:
  118. * RESET is not being notified to upper layers for now
  119. */
  120. printk(KERN_INFO "%s: received a reset ind!\n", __FUNCTION__);
  121. kfree_skb(skb);
  122. break;
  123. default:
  124. if (ev->ind_prim) {
  125. printk(KERN_INFO "%s: received unknown %d prim!\n",
  126. __FUNCTION__, ev->ind_prim);
  127. kfree_skb(skb);
  128. }
  129. /* No indication */
  130. break;
  131. }
  132. switch (ev->cfm_prim) {
  133. case LLC_DATA_PRIM:
  134. if (!llc_data_accept_state(llc->state))
  135. sk->sk_write_space(sk);
  136. else
  137. rc = llc->failed_data_req = 1;
  138. break;
  139. case LLC_CONN_PRIM:
  140. if (sk->sk_type == SOCK_STREAM &&
  141. sk->sk_state == TCP_SYN_SENT) {
  142. if (ev->status) {
  143. sk->sk_socket->state = SS_UNCONNECTED;
  144. sk->sk_state = TCP_CLOSE;
  145. } else {
  146. sk->sk_socket->state = SS_CONNECTED;
  147. sk->sk_state = TCP_ESTABLISHED;
  148. }
  149. sk->sk_state_change(sk);
  150. }
  151. break;
  152. case LLC_DISC_PRIM:
  153. sock_hold(sk);
  154. if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
  155. sk->sk_socket->state = SS_UNCONNECTED;
  156. sk->sk_state = TCP_CLOSE;
  157. sk->sk_state_change(sk);
  158. }
  159. sock_put(sk);
  160. break;
  161. case LLC_RESET_PRIM:
  162. /*
  163. * FIXME:
  164. * RESET is not being notified to upper layers for now
  165. */
  166. printk(KERN_INFO "%s: received a reset conf!\n", __FUNCTION__);
  167. break;
  168. default:
  169. if (ev->cfm_prim) {
  170. printk(KERN_INFO "%s: received unknown %d prim!\n",
  171. __FUNCTION__, ev->cfm_prim);
  172. break;
  173. }
  174. goto out_skb_put; /* No confirmation */
  175. }
  176. out_kfree_skb:
  177. kfree_skb(skb);
  178. out_skb_put:
  179. kfree_skb(skb);
  180. return rc;
  181. }
  182. void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
  183. {
  184. /* queue PDU to send to MAC layer */
  185. skb_queue_tail(&sk->sk_write_queue, skb);
  186. llc_conn_send_pdus(sk);
  187. }
  188. /**
  189. * llc_conn_rtn_pdu - sends received data pdu to upper layer
  190. * @sk: Active connection
  191. * @skb: Received data frame
  192. *
  193. * Sends received data pdu to upper layer (by using indicate function).
  194. * Prepares service parameters (prim and prim_data). calling indication
  195. * function will be done in llc_conn_state_process.
  196. */
  197. void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
  198. {
  199. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  200. ev->ind_prim = LLC_DATA_PRIM;
  201. }
  202. /**
  203. * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
  204. * @sk: active connection
  205. * @nr: NR
  206. * @first_p_bit: p_bit value of first pdu
  207. *
  208. * Resend all unacknowledged I PDUs, starting with the NR; send first as
  209. * command PDU with P bit equal first_p_bit; if more than one send
  210. * subsequent as command PDUs with P bit equal zero (0).
  211. */
  212. void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
  213. {
  214. struct sk_buff *skb;
  215. struct llc_pdu_sn *pdu;
  216. u16 nbr_unack_pdus;
  217. struct llc_sock *llc;
  218. u8 howmany_resend = 0;
  219. llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
  220. if (!nbr_unack_pdus)
  221. goto out;
  222. /*
  223. * Process unack PDUs only if unack queue is not empty; remove
  224. * appropriate PDUs, fix them up, and put them on mac_pdu_q.
  225. */
  226. llc = llc_sk(sk);
  227. while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
  228. pdu = llc_pdu_sn_hdr(skb);
  229. llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
  230. llc_pdu_set_pf_bit(skb, first_p_bit);
  231. skb_queue_tail(&sk->sk_write_queue, skb);
  232. first_p_bit = 0;
  233. llc->vS = LLC_I_GET_NS(pdu);
  234. howmany_resend++;
  235. }
  236. if (howmany_resend > 0)
  237. llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
  238. /* any PDUs to re-send are queued up; start sending to MAC */
  239. llc_conn_send_pdus(sk);
  240. out:;
  241. }
  242. /**
  243. * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
  244. * @sk: active connection.
  245. * @nr: NR
  246. * @first_f_bit: f_bit value of first pdu.
  247. *
  248. * Resend all unacknowledged I PDUs, starting with the NR; send first as
  249. * response PDU with F bit equal first_f_bit; if more than one send
  250. * subsequent as response PDUs with F bit equal zero (0).
  251. */
  252. void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
  253. {
  254. struct sk_buff *skb;
  255. u16 nbr_unack_pdus;
  256. struct llc_sock *llc = llc_sk(sk);
  257. u8 howmany_resend = 0;
  258. llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
  259. if (!nbr_unack_pdus)
  260. goto out;
  261. /*
  262. * Process unack PDUs only if unack queue is not empty; remove
  263. * appropriate PDUs, fix them up, and put them on mac_pdu_q
  264. */
  265. while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
  266. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  267. llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
  268. llc_pdu_set_pf_bit(skb, first_f_bit);
  269. skb_queue_tail(&sk->sk_write_queue, skb);
  270. first_f_bit = 0;
  271. llc->vS = LLC_I_GET_NS(pdu);
  272. howmany_resend++;
  273. }
  274. if (howmany_resend > 0)
  275. llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
  276. /* any PDUs to re-send are queued up; start sending to MAC */
  277. llc_conn_send_pdus(sk);
  278. out:;
  279. }
  280. /**
  281. * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
  282. * @sk: active connection
  283. * nr: NR
  284. * how_many_unacked: size of pdu_unack_q after removing acked pdus
  285. *
  286. * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
  287. * the number of pdus that removed from queue.
  288. */
  289. int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
  290. {
  291. int pdu_pos, i;
  292. struct sk_buff *skb;
  293. struct llc_pdu_sn *pdu;
  294. int nbr_acked = 0;
  295. struct llc_sock *llc = llc_sk(sk);
  296. int q_len = skb_queue_len(&llc->pdu_unack_q);
  297. if (!q_len)
  298. goto out;
  299. skb = skb_peek(&llc->pdu_unack_q);
  300. pdu = llc_pdu_sn_hdr(skb);
  301. /* finding position of last acked pdu in queue */
  302. pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
  303. (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
  304. for (i = 0; i < pdu_pos && i < q_len; i++) {
  305. skb = skb_dequeue(&llc->pdu_unack_q);
  306. if (skb)
  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. */
  445. struct sock *llc_lookup_established(struct llc_sap *sap, struct llc_addr *daddr,
  446. struct llc_addr *laddr)
  447. {
  448. struct sock *rc;
  449. struct hlist_node *node;
  450. read_lock_bh(&sap->sk_list.lock);
  451. sk_for_each(rc, node, &sap->sk_list.list) {
  452. struct llc_sock *llc = llc_sk(rc);
  453. if (llc->laddr.lsap == laddr->lsap &&
  454. llc->daddr.lsap == daddr->lsap &&
  455. llc_mac_match(llc->laddr.mac, laddr->mac) &&
  456. llc_mac_match(llc->daddr.mac, daddr->mac)) {
  457. sock_hold(rc);
  458. goto found;
  459. }
  460. }
  461. rc = NULL;
  462. found:
  463. read_unlock_bh(&sap->sk_list.lock);
  464. return rc;
  465. }
  466. /**
  467. * llc_lookup_listener - Finds listener for local MAC + SAP
  468. * @sap: SAP
  469. * @laddr: address of local LLC (MAC + SAP)
  470. *
  471. * Search connection list of the SAP and finds connection listening on
  472. * local mac, and local sap. Returns pointer for parent socket found,
  473. * %NULL otherwise.
  474. */
  475. static struct sock *llc_lookup_listener(struct llc_sap *sap,
  476. struct llc_addr *laddr)
  477. {
  478. struct sock *rc;
  479. struct hlist_node *node;
  480. read_lock_bh(&sap->sk_list.lock);
  481. sk_for_each(rc, node, &sap->sk_list.list) {
  482. struct llc_sock *llc = llc_sk(rc);
  483. if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
  484. llc->laddr.lsap == laddr->lsap &&
  485. (llc_mac_match(llc->laddr.mac, laddr->mac) ||
  486. llc_mac_null(llc->laddr.mac))) {
  487. sock_hold(rc);
  488. goto found;
  489. }
  490. }
  491. rc = NULL;
  492. found:
  493. read_unlock_bh(&sap->sk_list.lock);
  494. return rc;
  495. }
  496. /**
  497. * llc_data_accept_state - designates if in this state data can be sent.
  498. * @state: state of connection.
  499. *
  500. * Returns 0 if data can be sent, 1 otherwise.
  501. */
  502. u8 llc_data_accept_state(u8 state)
  503. {
  504. return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
  505. state != LLC_CONN_STATE_REJ;
  506. }
  507. /**
  508. * llc_find_next_offset - finds offset for next category of transitions
  509. * @state: state table.
  510. * @offset: start offset.
  511. *
  512. * Finds offset of next category of transitions in transition table.
  513. * Returns the start index of next category.
  514. */
  515. static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
  516. {
  517. u16 cnt = 0;
  518. struct llc_conn_state_trans **next_trans;
  519. for (next_trans = state->transitions + offset;
  520. (*next_trans)->ev; next_trans++)
  521. ++cnt;
  522. return cnt;
  523. }
  524. /**
  525. * llc_build_offset_table - builds offset table of connection
  526. *
  527. * Fills offset table of connection state transition table
  528. * (llc_offset_table).
  529. */
  530. void __init llc_build_offset_table(void)
  531. {
  532. struct llc_conn_state *curr_state;
  533. int state, ev_type, next_offset;
  534. for (state = 0; state < NBR_CONN_STATES; state++) {
  535. curr_state = &llc_conn_state_table[state];
  536. next_offset = 0;
  537. for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
  538. llc_offset_table[state][ev_type] = next_offset;
  539. next_offset += llc_find_next_offset(curr_state,
  540. next_offset) + 1;
  541. }
  542. }
  543. }
  544. /**
  545. * llc_find_offset - finds start offset of category of transitions
  546. * @state: state of connection
  547. * @ev_type: type of happened event
  548. *
  549. * Finds start offset of desired category of transitions. Returns the
  550. * desired start offset.
  551. */
  552. static int llc_find_offset(int state, int ev_type)
  553. {
  554. int rc = 0;
  555. /* at this stage, llc_offset_table[..][2] is not important. it is for
  556. * init_pf_cycle and I don't know what is it.
  557. */
  558. switch (ev_type) {
  559. case LLC_CONN_EV_TYPE_PRIM:
  560. rc = llc_offset_table[state][0]; break;
  561. case LLC_CONN_EV_TYPE_PDU:
  562. rc = llc_offset_table[state][4]; break;
  563. case LLC_CONN_EV_TYPE_SIMPLE:
  564. rc = llc_offset_table[state][1]; break;
  565. case LLC_CONN_EV_TYPE_P_TMR:
  566. case LLC_CONN_EV_TYPE_ACK_TMR:
  567. case LLC_CONN_EV_TYPE_REJ_TMR:
  568. case LLC_CONN_EV_TYPE_BUSY_TMR:
  569. rc = llc_offset_table[state][3]; break;
  570. }
  571. return rc;
  572. }
  573. /**
  574. * llc_sap_add_socket - adds a socket to a SAP
  575. * @sap: SAP
  576. * @sk: socket
  577. *
  578. * This function adds a socket to sk_list of a SAP.
  579. */
  580. void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  581. {
  582. write_lock_bh(&sap->sk_list.lock);
  583. llc_sk(sk)->sap = sap;
  584. sk_add_node(sk, &sap->sk_list.list);
  585. write_unlock_bh(&sap->sk_list.lock);
  586. }
  587. /**
  588. * llc_sap_remove_socket - removes a socket from SAP
  589. * @sap: SAP
  590. * @sk: socket
  591. *
  592. * This function removes a connection from sk_list.list of a SAP if
  593. * the connection was in this list.
  594. */
  595. void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
  596. {
  597. write_lock_bh(&sap->sk_list.lock);
  598. sk_del_node_init(sk);
  599. write_unlock_bh(&sap->sk_list.lock);
  600. }
  601. /**
  602. * llc_conn_rcv - sends received pdus to the connection state machine
  603. * @sk: current connection structure.
  604. * @skb: received frame.
  605. *
  606. * Sends received pdus to the connection state machine.
  607. */
  608. static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
  609. {
  610. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  611. struct llc_sock *llc = llc_sk(sk);
  612. if (!llc->dev)
  613. llc->dev = skb->dev;
  614. ev->type = LLC_CONN_EV_TYPE_PDU;
  615. ev->reason = 0;
  616. return llc_conn_state_process(sk, skb);
  617. }
  618. void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
  619. {
  620. struct llc_addr saddr, daddr;
  621. struct sock *sk;
  622. llc_pdu_decode_sa(skb, saddr.mac);
  623. llc_pdu_decode_ssap(skb, &saddr.lsap);
  624. llc_pdu_decode_da(skb, daddr.mac);
  625. llc_pdu_decode_dsap(skb, &daddr.lsap);
  626. sk = llc_lookup_established(sap, &saddr, &daddr);
  627. if (!sk) {
  628. /*
  629. * Didn't find an active connection; verify if there
  630. * is a listening socket for this llc addr
  631. */
  632. struct llc_sock *llc;
  633. struct sock *parent = llc_lookup_listener(sap, &daddr);
  634. if (!parent) {
  635. dprintk("llc_lookup_listener failed!\n");
  636. goto drop;
  637. }
  638. sk = llc_sk_alloc(parent->sk_family, GFP_ATOMIC, parent->sk_prot);
  639. if (!sk) {
  640. sock_put(parent);
  641. goto drop;
  642. }
  643. llc = llc_sk(sk);
  644. memcpy(&llc->laddr, &daddr, sizeof(llc->laddr));
  645. memcpy(&llc->daddr, &saddr, sizeof(llc->daddr));
  646. llc_sap_add_socket(sap, sk);
  647. sock_hold(sk);
  648. sock_put(parent);
  649. skb->sk = parent;
  650. } else
  651. skb->sk = sk;
  652. bh_lock_sock(sk);
  653. if (!sock_owned_by_user(sk))
  654. llc_conn_rcv(sk, skb);
  655. else {
  656. dprintk("%s: adding to backlog...\n", __FUNCTION__);
  657. llc_set_backlog_type(skb, LLC_PACKET);
  658. sk_add_backlog(sk, skb);
  659. }
  660. bh_unlock_sock(sk);
  661. sock_put(sk);
  662. return;
  663. drop:
  664. kfree_skb(skb);
  665. }
  666. #undef LLC_REFCNT_DEBUG
  667. #ifdef LLC_REFCNT_DEBUG
  668. static atomic_t llc_sock_nr;
  669. #endif
  670. /**
  671. * llc_release_sockets - releases all sockets in a sap
  672. * @sap: sap to release its sockets
  673. *
  674. * Releases all connections of a sap. Returns 0 if all actions complete
  675. * successfully, nonzero otherwise
  676. */
  677. int llc_release_sockets(struct llc_sap *sap)
  678. {
  679. int rc = 0;
  680. struct sock *sk;
  681. struct hlist_node *node;
  682. write_lock_bh(&sap->sk_list.lock);
  683. sk_for_each(sk, node, &sap->sk_list.list) {
  684. llc_sk(sk)->state = LLC_CONN_STATE_TEMP;
  685. if (llc_send_disc(sk))
  686. rc = 1;
  687. }
  688. write_unlock_bh(&sap->sk_list.lock);
  689. return rc;
  690. }
  691. /**
  692. * llc_backlog_rcv - Processes rx frames and expired timers.
  693. * @sk: LLC sock (p8022 connection)
  694. * @skb: queued rx frame or event
  695. *
  696. * This function processes frames that has received and timers that has
  697. * expired during sending an I pdu (refer to data_req_handler). frames
  698. * queue by llc_rcv function (llc_mac.c) and timers queue by timer
  699. * callback functions(llc_c_ac.c).
  700. */
  701. static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  702. {
  703. int rc = 0;
  704. struct llc_sock *llc = llc_sk(sk);
  705. if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
  706. if (likely(llc->state > 1)) /* not closed */
  707. rc = llc_conn_rcv(sk, skb);
  708. else
  709. goto out_kfree_skb;
  710. } else if (llc_backlog_type(skb) == LLC_EVENT) {
  711. /* timer expiration event */
  712. if (likely(llc->state > 1)) /* not closed */
  713. rc = llc_conn_state_process(sk, skb);
  714. else
  715. goto out_kfree_skb;
  716. } else {
  717. printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
  718. goto out_kfree_skb;
  719. }
  720. out:
  721. return rc;
  722. out_kfree_skb:
  723. kfree_skb(skb);
  724. goto out;
  725. }
  726. /**
  727. * llc_sk_init - Initializes a socket with default llc values.
  728. * @sk: socket to initialize.
  729. *
  730. * Initializes a socket with default llc values.
  731. */
  732. static void llc_sk_init(struct sock* sk)
  733. {
  734. struct llc_sock *llc = llc_sk(sk);
  735. llc->state = LLC_CONN_STATE_ADM;
  736. llc->inc_cntr = llc->dec_cntr = 2;
  737. llc->dec_step = llc->connect_step = 1;
  738. init_timer(&llc->ack_timer.timer);
  739. llc->ack_timer.expire = sysctl_llc2_ack_timeout;
  740. llc->ack_timer.timer.data = (unsigned long)sk;
  741. llc->ack_timer.timer.function = llc_conn_ack_tmr_cb;
  742. init_timer(&llc->pf_cycle_timer.timer);
  743. llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
  744. llc->pf_cycle_timer.timer.data = (unsigned long)sk;
  745. llc->pf_cycle_timer.timer.function = llc_conn_pf_cycle_tmr_cb;
  746. init_timer(&llc->rej_sent_timer.timer);
  747. llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
  748. llc->rej_sent_timer.timer.data = (unsigned long)sk;
  749. llc->rej_sent_timer.timer.function = llc_conn_rej_tmr_cb;
  750. init_timer(&llc->busy_state_timer.timer);
  751. llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
  752. llc->busy_state_timer.timer.data = (unsigned long)sk;
  753. llc->busy_state_timer.timer.function = llc_conn_busy_tmr_cb;
  754. llc->n2 = 2; /* max retransmit */
  755. llc->k = 2; /* tx win size, will adjust dynam */
  756. llc->rw = 128; /* rx win size (opt and equal to
  757. * tx_win of remote LLC) */
  758. skb_queue_head_init(&llc->pdu_unack_q);
  759. sk->sk_backlog_rcv = llc_backlog_rcv;
  760. }
  761. /**
  762. * llc_sk_alloc - Allocates LLC sock
  763. * @family: upper layer protocol family
  764. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  765. *
  766. * Allocates a LLC sock and initializes it. Returns the new LLC sock
  767. * or %NULL if there's no memory available for one
  768. */
  769. struct sock *llc_sk_alloc(int family, int priority, struct proto *prot)
  770. {
  771. struct sock *sk = sk_alloc(family, priority, prot, 1);
  772. if (!sk)
  773. goto out;
  774. llc_sk_init(sk);
  775. sock_init_data(NULL, sk);
  776. #ifdef LLC_REFCNT_DEBUG
  777. atomic_inc(&llc_sock_nr);
  778. printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
  779. __FUNCTION__, atomic_read(&llc_sock_nr));
  780. #endif
  781. out:
  782. return sk;
  783. }
  784. /**
  785. * llc_sk_free - Frees a LLC socket
  786. * @sk - socket to free
  787. *
  788. * Frees a LLC socket
  789. */
  790. void llc_sk_free(struct sock *sk)
  791. {
  792. struct llc_sock *llc = llc_sk(sk);
  793. llc->state = LLC_CONN_OUT_OF_SVC;
  794. /* Stop all (possibly) running timers */
  795. llc_conn_ac_stop_all_timers(sk, NULL);
  796. #ifdef DEBUG_LLC_CONN_ALLOC
  797. printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
  798. skb_queue_len(&llc->pdu_unack_q),
  799. skb_queue_len(&sk->sk_write_queue));
  800. #endif
  801. skb_queue_purge(&sk->sk_receive_queue);
  802. skb_queue_purge(&sk->sk_write_queue);
  803. skb_queue_purge(&llc->pdu_unack_q);
  804. #ifdef LLC_REFCNT_DEBUG
  805. if (atomic_read(&sk->sk_refcnt) != 1) {
  806. printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
  807. sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
  808. printk(KERN_DEBUG "%d LLC sockets are still alive\n",
  809. atomic_read(&llc_sock_nr));
  810. } else {
  811. atomic_dec(&llc_sock_nr);
  812. printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
  813. __FUNCTION__, atomic_read(&llc_sock_nr));
  814. }
  815. #endif
  816. sock_put(sk);
  817. }
  818. /**
  819. * llc_sk_reset - resets a connection
  820. * @sk: LLC socket to reset
  821. *
  822. * Resets a connection to the out of service state. Stops its timers
  823. * and frees any frames in the queues of the connection.
  824. */
  825. void llc_sk_reset(struct sock *sk)
  826. {
  827. struct llc_sock *llc = llc_sk(sk);
  828. llc_conn_ac_stop_all_timers(sk, NULL);
  829. skb_queue_purge(&sk->sk_write_queue);
  830. skb_queue_purge(&llc->pdu_unack_q);
  831. llc->remote_busy_flag = 0;
  832. llc->cause_flag = 0;
  833. llc->retry_count = 0;
  834. llc_conn_set_p_flag(sk, 0);
  835. llc->f_flag = 0;
  836. llc->s_flag = 0;
  837. llc->ack_pf = 0;
  838. llc->first_pdu_Ns = 0;
  839. llc->ack_must_be_send = 0;
  840. llc->dec_step = 1;
  841. llc->inc_cntr = 2;
  842. llc->dec_cntr = 2;
  843. llc->X = 0;
  844. llc->failed_data_req = 0 ;
  845. llc->last_nr = 0;
  846. }