llc_conn.c 24 KB

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