llc_conn.c 25 KB

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