llc_conn.c 24 KB

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