llc_conn.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * llc_conn.c - Driver routines for connection component.
  3. *
  4. * Copyright (c) 1997 by Procom Technology, Inc.
  5. * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program can be redistributed or modified under the terms of the
  8. * GNU General Public License as published by the Free Software Foundation.
  9. * This program is distributed without any warranty or implied warranty
  10. * of merchantability or fitness for a particular purpose.
  11. *
  12. * See the GNU General Public License for more details.
  13. */
  14. #include <linux/init.h>
  15. #include <net/llc_sap.h>
  16. #include <net/llc_conn.h>
  17. #include <net/sock.h>
  18. #include <net/tcp_states.h>
  19. #include <net/llc_c_ev.h>
  20. #include <net/llc_c_ac.h>
  21. #include <net/llc_c_st.h>
  22. #include <net/llc_pdu.h>
  23. #if 0
  24. #define dprintk(args...) printk(KERN_DEBUG args)
  25. #else
  26. #define dprintk(args...)
  27. #endif
  28. static int llc_find_offset(int state, int ev_type);
  29. static void llc_conn_send_pdus(struct sock *sk);
  30. static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
  31. static int llc_exec_conn_trans_actions(struct sock *sk,
  32. struct llc_conn_state_trans *trans,
  33. struct sk_buff *ev);
  34. static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  35. struct sk_buff *skb);
  36. /* Offset table on connection states transition diagram */
  37. static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
  38. int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
  39. int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
  40. int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
  41. int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
  42. /**
  43. * llc_conn_state_process - sends event to connection state machine
  44. * @sk: connection
  45. * @skb: occurred event
  46. *
  47. * Sends an event to connection state machine. After processing event
  48. * (executing it's actions and changing state), upper layer will be
  49. * indicated or confirmed, if needed. Returns 0 for success, 1 for
  50. * failure. The socket lock has to be held before calling this function.
  51. */
  52. int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
  53. {
  54. int rc;
  55. struct llc_sock *llc = llc_sk(skb->sk);
  56. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  57. /*
  58. * We have to hold the skb, because llc_conn_service will kfree it in
  59. * the sending path and we need to look at the skb->cb, where we encode
  60. * llc_conn_state_ev.
  61. */
  62. skb_get(skb);
  63. ev->ind_prim = ev->cfm_prim = 0;
  64. /*
  65. * Send event to state machine
  66. */
  67. rc = llc_conn_service(skb->sk, skb);
  68. if (unlikely(rc != 0)) {
  69. printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
  70. goto out_kfree_skb;
  71. }
  72. if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
  73. /* indicate or confirm not required */
  74. /* XXX this is not very pretty, perhaps we should store
  75. * XXX indicate/confirm-needed state in the llc_conn_state_ev
  76. * XXX control block of the SKB instead? -DaveM
  77. */
  78. if (!skb->next)
  79. goto out_kfree_skb;
  80. goto out_skb_put;
  81. }
  82. if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
  83. skb_get(skb);
  84. switch (ev->ind_prim) {
  85. case LLC_DATA_PRIM:
  86. llc_save_primitive(sk, skb, LLC_DATA_PRIM);
  87. if (unlikely(sock_queue_rcv_skb(sk, skb))) {
  88. /*
  89. * shouldn't happen
  90. */
  91. printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
  92. __FUNCTION__);
  93. kfree_skb(skb);
  94. }
  95. break;
  96. case LLC_CONN_PRIM:
  97. /*
  98. * Can't be sock_queue_rcv_skb, because we have to leave the
  99. * skb->sk pointing to the newly created struct sock in
  100. * llc_conn_handler. -acme
  101. */
  102. skb_queue_tail(&sk->sk_receive_queue, skb);
  103. sk->sk_state_change(sk);
  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. sock_set_flag(sk, SOCK_DEAD);
  114. sk->sk_state_change(sk);
  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. * Caller has to make sure local_bh is disabled.
  450. */
  451. static struct sock *__llc_lookup_established(struct llc_sap *sap,
  452. struct llc_addr *daddr,
  453. struct llc_addr *laddr)
  454. {
  455. struct sock *rc;
  456. struct hlist_node *node;
  457. read_lock(&sap->sk_list.lock);
  458. sk_for_each(rc, node, &sap->sk_list.list) {
  459. struct llc_sock *llc = llc_sk(rc);
  460. if (llc->laddr.lsap == laddr->lsap &&
  461. llc->daddr.lsap == daddr->lsap &&
  462. llc_mac_match(llc->laddr.mac, laddr->mac) &&
  463. llc_mac_match(llc->daddr.mac, daddr->mac)) {
  464. sock_hold(rc);
  465. goto found;
  466. }
  467. }
  468. rc = NULL;
  469. found:
  470. read_unlock(&sap->sk_list.lock);
  471. return rc;
  472. }
  473. struct sock *llc_lookup_established(struct llc_sap *sap,
  474. struct llc_addr *daddr,
  475. struct llc_addr *laddr)
  476. {
  477. struct sock *sk;
  478. local_bh_disable();
  479. sk = __llc_lookup_established(sap, daddr, laddr);
  480. local_bh_enable();
  481. return sk;
  482. }
  483. /**
  484. * llc_lookup_listener - Finds listener for local MAC + SAP
  485. * @sap: SAP
  486. * @laddr: address of local LLC (MAC + SAP)
  487. *
  488. * Search connection list of the SAP and finds connection listening on
  489. * local mac, and local sap. Returns pointer for parent socket found,
  490. * %NULL otherwise.
  491. * Caller has to make sure local_bh is disabled.
  492. */
  493. static struct sock *llc_lookup_listener(struct llc_sap *sap,
  494. struct llc_addr *laddr)
  495. {
  496. struct sock *rc;
  497. struct hlist_node *node;
  498. read_lock(&sap->sk_list.lock);
  499. sk_for_each(rc, node, &sap->sk_list.list) {
  500. struct llc_sock *llc = llc_sk(rc);
  501. if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
  502. llc->laddr.lsap == laddr->lsap &&
  503. (llc_mac_match(llc->laddr.mac, laddr->mac) ||
  504. llc_mac_null(llc->laddr.mac))) {
  505. sock_hold(rc);
  506. goto found;
  507. }
  508. }
  509. rc = NULL;
  510. found:
  511. read_unlock(&sap->sk_list.lock);
  512. return rc;
  513. }
  514. static struct sock *__llc_lookup(struct llc_sap *sap,
  515. struct llc_addr *daddr,
  516. struct llc_addr *laddr)
  517. {
  518. struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
  519. return sk ? : llc_lookup_listener(sap, laddr);
  520. }
  521. /**
  522. * llc_data_accept_state - designates if in this state data can be sent.
  523. * @state: state of connection.
  524. *
  525. * Returns 0 if data can be sent, 1 otherwise.
  526. */
  527. u8 llc_data_accept_state(u8 state)
  528. {
  529. return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
  530. state != LLC_CONN_STATE_REJ;
  531. }
  532. /**
  533. * llc_find_next_offset - finds offset for next category of transitions
  534. * @state: state table.
  535. * @offset: start offset.
  536. *
  537. * Finds offset of next category of transitions in transition table.
  538. * Returns the start index of next category.
  539. */
  540. static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
  541. {
  542. u16 cnt = 0;
  543. struct llc_conn_state_trans **next_trans;
  544. for (next_trans = state->transitions + offset;
  545. (*next_trans)->ev; next_trans++)
  546. ++cnt;
  547. return cnt;
  548. }
  549. /**
  550. * llc_build_offset_table - builds offset table of connection
  551. *
  552. * Fills offset table of connection state transition table
  553. * (llc_offset_table).
  554. */
  555. void __init llc_build_offset_table(void)
  556. {
  557. struct llc_conn_state *curr_state;
  558. int state, ev_type, next_offset;
  559. for (state = 0; state < NBR_CONN_STATES; state++) {
  560. curr_state = &llc_conn_state_table[state];
  561. next_offset = 0;
  562. for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
  563. llc_offset_table[state][ev_type] = next_offset;
  564. next_offset += llc_find_next_offset(curr_state,
  565. next_offset) + 1;
  566. }
  567. }
  568. }
  569. /**
  570. * llc_find_offset - finds start offset of category of transitions
  571. * @state: state of connection
  572. * @ev_type: type of happened event
  573. *
  574. * Finds start offset of desired category of transitions. Returns the
  575. * desired start offset.
  576. */
  577. static int llc_find_offset(int state, int ev_type)
  578. {
  579. int rc = 0;
  580. /* at this stage, llc_offset_table[..][2] is not important. it is for
  581. * init_pf_cycle and I don't know what is it.
  582. */
  583. switch (ev_type) {
  584. case LLC_CONN_EV_TYPE_PRIM:
  585. rc = llc_offset_table[state][0]; break;
  586. case LLC_CONN_EV_TYPE_PDU:
  587. rc = llc_offset_table[state][4]; break;
  588. case LLC_CONN_EV_TYPE_SIMPLE:
  589. rc = llc_offset_table[state][1]; break;
  590. case LLC_CONN_EV_TYPE_P_TMR:
  591. case LLC_CONN_EV_TYPE_ACK_TMR:
  592. case LLC_CONN_EV_TYPE_REJ_TMR:
  593. case LLC_CONN_EV_TYPE_BUSY_TMR:
  594. rc = llc_offset_table[state][3]; break;
  595. }
  596. return rc;
  597. }
  598. /**
  599. * llc_sap_add_socket - adds a socket to a SAP
  600. * @sap: SAP
  601. * @sk: socket
  602. *
  603. * This function adds a socket to sk_list of a SAP.
  604. */
  605. void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  606. {
  607. llc_sap_hold(sap);
  608. write_lock_bh(&sap->sk_list.lock);
  609. llc_sk(sk)->sap = sap;
  610. sk_add_node(sk, &sap->sk_list.list);
  611. write_unlock_bh(&sap->sk_list.lock);
  612. }
  613. /**
  614. * llc_sap_remove_socket - removes a socket from SAP
  615. * @sap: SAP
  616. * @sk: socket
  617. *
  618. * This function removes a connection from sk_list.list of a SAP if
  619. * the connection was in this list.
  620. */
  621. void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
  622. {
  623. write_lock_bh(&sap->sk_list.lock);
  624. sk_del_node_init(sk);
  625. write_unlock_bh(&sap->sk_list.lock);
  626. llc_sap_put(sap);
  627. }
  628. /**
  629. * llc_conn_rcv - sends received pdus to the connection state machine
  630. * @sk: current connection structure.
  631. * @skb: received frame.
  632. *
  633. * Sends received pdus to the connection state machine.
  634. */
  635. static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
  636. {
  637. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  638. ev->type = LLC_CONN_EV_TYPE_PDU;
  639. ev->reason = 0;
  640. return llc_conn_state_process(sk, skb);
  641. }
  642. static struct sock *llc_create_incoming_sock(struct sock *sk,
  643. struct net_device *dev,
  644. struct llc_addr *saddr,
  645. struct llc_addr *daddr)
  646. {
  647. struct sock *newsk = llc_sk_alloc(sk->sk_family, GFP_ATOMIC,
  648. sk->sk_prot);
  649. struct llc_sock *newllc, *llc = llc_sk(sk);
  650. if (!newsk)
  651. goto out;
  652. newllc = llc_sk(newsk);
  653. memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
  654. memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
  655. newllc->dev = dev;
  656. dev_hold(dev);
  657. llc_sap_add_socket(llc->sap, newsk);
  658. llc_sap_hold(llc->sap);
  659. out:
  660. return newsk;
  661. }
  662. void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
  663. {
  664. struct llc_addr saddr, daddr;
  665. struct sock *sk;
  666. llc_pdu_decode_sa(skb, saddr.mac);
  667. llc_pdu_decode_ssap(skb, &saddr.lsap);
  668. llc_pdu_decode_da(skb, daddr.mac);
  669. llc_pdu_decode_dsap(skb, &daddr.lsap);
  670. sk = __llc_lookup(sap, &saddr, &daddr);
  671. if (!sk)
  672. goto drop;
  673. bh_lock_sock(sk);
  674. /*
  675. * This has to be done here and not at the upper layer ->accept
  676. * method because of the way the PROCOM state machine works:
  677. * it needs to set several state variables (see, for instance,
  678. * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
  679. * the originator of the new connection, and this state has to be
  680. * in the newly created struct sock private area. -acme
  681. */
  682. if (unlikely(sk->sk_state == TCP_LISTEN)) {
  683. struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
  684. &saddr, &daddr);
  685. if (!newsk)
  686. goto drop_unlock;
  687. skb_set_owner_r(skb, newsk);
  688. } else {
  689. /*
  690. * Can't be skb_set_owner_r, this will be done at the
  691. * llc_conn_state_process function, later on, when we will use
  692. * skb_queue_rcv_skb to send it to upper layers, this is
  693. * another trick required to cope with how the PROCOM state
  694. * machine works. -acme
  695. */
  696. skb->sk = sk;
  697. }
  698. if (!sock_owned_by_user(sk))
  699. llc_conn_rcv(sk, skb);
  700. else {
  701. dprintk("%s: adding to backlog...\n", __FUNCTION__);
  702. llc_set_backlog_type(skb, LLC_PACKET);
  703. sk_add_backlog(sk, skb);
  704. }
  705. out:
  706. bh_unlock_sock(sk);
  707. sock_put(sk);
  708. return;
  709. drop:
  710. kfree_skb(skb);
  711. return;
  712. drop_unlock:
  713. kfree_skb(skb);
  714. goto out;
  715. }
  716. #undef LLC_REFCNT_DEBUG
  717. #ifdef LLC_REFCNT_DEBUG
  718. static atomic_t llc_sock_nr;
  719. #endif
  720. /**
  721. * llc_backlog_rcv - Processes rx frames and expired timers.
  722. * @sk: LLC sock (p8022 connection)
  723. * @skb: queued rx frame or event
  724. *
  725. * This function processes frames that has received and timers that has
  726. * expired during sending an I pdu (refer to data_req_handler). frames
  727. * queue by llc_rcv function (llc_mac.c) and timers queue by timer
  728. * callback functions(llc_c_ac.c).
  729. */
  730. static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  731. {
  732. int rc = 0;
  733. struct llc_sock *llc = llc_sk(sk);
  734. if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
  735. if (likely(llc->state > 1)) /* not closed */
  736. rc = llc_conn_rcv(sk, skb);
  737. else
  738. goto out_kfree_skb;
  739. } else if (llc_backlog_type(skb) == LLC_EVENT) {
  740. /* timer expiration event */
  741. if (likely(llc->state > 1)) /* not closed */
  742. rc = llc_conn_state_process(sk, skb);
  743. else
  744. goto out_kfree_skb;
  745. } else {
  746. printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
  747. goto out_kfree_skb;
  748. }
  749. out:
  750. return rc;
  751. out_kfree_skb:
  752. kfree_skb(skb);
  753. goto out;
  754. }
  755. /**
  756. * llc_sk_init - Initializes a socket with default llc values.
  757. * @sk: socket to initialize.
  758. *
  759. * Initializes a socket with default llc values.
  760. */
  761. static void llc_sk_init(struct sock* sk)
  762. {
  763. struct llc_sock *llc = llc_sk(sk);
  764. llc->state = LLC_CONN_STATE_ADM;
  765. llc->inc_cntr = llc->dec_cntr = 2;
  766. llc->dec_step = llc->connect_step = 1;
  767. init_timer(&llc->ack_timer.timer);
  768. llc->ack_timer.expire = sysctl_llc2_ack_timeout;
  769. llc->ack_timer.timer.data = (unsigned long)sk;
  770. llc->ack_timer.timer.function = llc_conn_ack_tmr_cb;
  771. init_timer(&llc->pf_cycle_timer.timer);
  772. llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
  773. llc->pf_cycle_timer.timer.data = (unsigned long)sk;
  774. llc->pf_cycle_timer.timer.function = llc_conn_pf_cycle_tmr_cb;
  775. init_timer(&llc->rej_sent_timer.timer);
  776. llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
  777. llc->rej_sent_timer.timer.data = (unsigned long)sk;
  778. llc->rej_sent_timer.timer.function = llc_conn_rej_tmr_cb;
  779. init_timer(&llc->busy_state_timer.timer);
  780. llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
  781. llc->busy_state_timer.timer.data = (unsigned long)sk;
  782. llc->busy_state_timer.timer.function = llc_conn_busy_tmr_cb;
  783. llc->n2 = 2; /* max retransmit */
  784. llc->k = 2; /* tx win size, will adjust dynam */
  785. llc->rw = 128; /* rx win size (opt and equal to
  786. * tx_win of remote LLC) */
  787. skb_queue_head_init(&llc->pdu_unack_q);
  788. sk->sk_backlog_rcv = llc_backlog_rcv;
  789. }
  790. /**
  791. * llc_sk_alloc - Allocates LLC sock
  792. * @family: upper layer protocol family
  793. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  794. *
  795. * Allocates a LLC sock and initializes it. Returns the new LLC sock
  796. * or %NULL if there's no memory available for one
  797. */
  798. struct sock *llc_sk_alloc(int family, gfp_t priority, struct proto *prot)
  799. {
  800. struct sock *sk = sk_alloc(family, priority, prot, 1);
  801. if (!sk)
  802. goto out;
  803. llc_sk_init(sk);
  804. sock_init_data(NULL, sk);
  805. #ifdef LLC_REFCNT_DEBUG
  806. atomic_inc(&llc_sock_nr);
  807. printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
  808. __FUNCTION__, atomic_read(&llc_sock_nr));
  809. #endif
  810. out:
  811. return sk;
  812. }
  813. /**
  814. * llc_sk_free - Frees a LLC socket
  815. * @sk - socket to free
  816. *
  817. * Frees a LLC socket
  818. */
  819. void llc_sk_free(struct sock *sk)
  820. {
  821. struct llc_sock *llc = llc_sk(sk);
  822. llc->state = LLC_CONN_OUT_OF_SVC;
  823. /* Stop all (possibly) running timers */
  824. llc_conn_ac_stop_all_timers(sk, NULL);
  825. #ifdef DEBUG_LLC_CONN_ALLOC
  826. printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
  827. skb_queue_len(&llc->pdu_unack_q),
  828. skb_queue_len(&sk->sk_write_queue));
  829. #endif
  830. skb_queue_purge(&sk->sk_receive_queue);
  831. skb_queue_purge(&sk->sk_write_queue);
  832. skb_queue_purge(&llc->pdu_unack_q);
  833. #ifdef LLC_REFCNT_DEBUG
  834. if (atomic_read(&sk->sk_refcnt) != 1) {
  835. printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
  836. sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
  837. printk(KERN_DEBUG "%d LLC sockets are still alive\n",
  838. atomic_read(&llc_sock_nr));
  839. } else {
  840. atomic_dec(&llc_sock_nr);
  841. printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
  842. __FUNCTION__, atomic_read(&llc_sock_nr));
  843. }
  844. #endif
  845. sock_put(sk);
  846. }
  847. /**
  848. * llc_sk_reset - resets a connection
  849. * @sk: LLC socket to reset
  850. *
  851. * Resets a connection to the out of service state. Stops its timers
  852. * and frees any frames in the queues of the connection.
  853. */
  854. void llc_sk_reset(struct sock *sk)
  855. {
  856. struct llc_sock *llc = llc_sk(sk);
  857. llc_conn_ac_stop_all_timers(sk, NULL);
  858. skb_queue_purge(&sk->sk_write_queue);
  859. skb_queue_purge(&llc->pdu_unack_q);
  860. llc->remote_busy_flag = 0;
  861. llc->cause_flag = 0;
  862. llc->retry_count = 0;
  863. llc_conn_set_p_flag(sk, 0);
  864. llc->f_flag = 0;
  865. llc->s_flag = 0;
  866. llc->ack_pf = 0;
  867. llc->first_pdu_Ns = 0;
  868. llc->ack_must_be_send = 0;
  869. llc->dec_step = 1;
  870. llc->inc_cntr = 2;
  871. llc->dec_cntr = 2;
  872. llc->X = 0;
  873. llc->failed_data_req = 0 ;
  874. llc->last_nr = 0;
  875. }