llc_conn.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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", __func__);
  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. __func__);
  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", __func__);
  126. kfree_skb(skb);
  127. break;
  128. default:
  129. if (ev->ind_prim) {
  130. printk(KERN_INFO "%s: received unknown %d prim!\n",
  131. __func__, 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", __func__);
  172. break;
  173. default:
  174. if (ev->cfm_prim) {
  175. printk(KERN_INFO "%s: received unknown %d prim!\n",
  176. __func__, 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. kfree_skb(skb);
  312. nbr_acked++;
  313. }
  314. out:
  315. *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
  316. return nbr_acked;
  317. }
  318. /**
  319. * llc_conn_send_pdus - Sends queued PDUs
  320. * @sk: active connection
  321. *
  322. * Sends queued pdus to MAC layer for transmission.
  323. */
  324. static void llc_conn_send_pdus(struct sock *sk)
  325. {
  326. struct sk_buff *skb;
  327. while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
  328. struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
  329. if (LLC_PDU_TYPE_IS_I(pdu) &&
  330. !(skb->dev->flags & IFF_LOOPBACK)) {
  331. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  332. skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
  333. if (!skb2)
  334. break;
  335. skb = skb2;
  336. }
  337. dev_queue_xmit(skb);
  338. }
  339. }
  340. /**
  341. * llc_conn_service - finds transition and changes state of connection
  342. * @sk: connection
  343. * @skb: happened event
  344. *
  345. * This function finds transition that matches with happened event, then
  346. * executes related actions and finally changes state of connection.
  347. * Returns 0 for success, 1 for failure.
  348. */
  349. static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
  350. {
  351. int rc = 1;
  352. struct llc_sock *llc = llc_sk(sk);
  353. struct llc_conn_state_trans *trans;
  354. if (llc->state > NBR_CONN_STATES)
  355. goto out;
  356. rc = 0;
  357. trans = llc_qualify_conn_ev(sk, skb);
  358. if (trans) {
  359. rc = llc_exec_conn_trans_actions(sk, trans, skb);
  360. if (!rc && trans->next_state != NO_STATE_CHANGE) {
  361. llc->state = trans->next_state;
  362. if (!llc_data_accept_state(llc->state))
  363. sk->sk_state_change(sk);
  364. }
  365. }
  366. out:
  367. return rc;
  368. }
  369. /**
  370. * llc_qualify_conn_ev - finds transition for event
  371. * @sk: connection
  372. * @skb: happened event
  373. *
  374. * This function finds transition that matches with happened event.
  375. * Returns pointer to found transition on success, %NULL otherwise.
  376. */
  377. static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  378. struct sk_buff *skb)
  379. {
  380. struct llc_conn_state_trans **next_trans;
  381. llc_conn_ev_qfyr_t *next_qualifier;
  382. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  383. struct llc_sock *llc = llc_sk(sk);
  384. struct llc_conn_state *curr_state =
  385. &llc_conn_state_table[llc->state - 1];
  386. /* search thru events for this state until
  387. * list exhausted or until no more
  388. */
  389. for (next_trans = curr_state->transitions +
  390. llc_find_offset(llc->state - 1, ev->type);
  391. (*next_trans)->ev; next_trans++) {
  392. if (!((*next_trans)->ev)(sk, skb)) {
  393. /* got POSSIBLE event match; the event may require
  394. * qualification based on the values of a number of
  395. * state flags; if all qualifications are met (i.e.,
  396. * if all qualifying functions return success, or 0,
  397. * then this is THE event we're looking for
  398. */
  399. for (next_qualifier = (*next_trans)->ev_qualifiers;
  400. next_qualifier && *next_qualifier &&
  401. !(*next_qualifier)(sk, skb); next_qualifier++)
  402. /* nothing */;
  403. if (!next_qualifier || !*next_qualifier)
  404. /* all qualifiers executed successfully; this is
  405. * our transition; return it so we can perform
  406. * the associated actions & change the state
  407. */
  408. return *next_trans;
  409. }
  410. }
  411. return NULL;
  412. }
  413. /**
  414. * llc_exec_conn_trans_actions - executes related actions
  415. * @sk: connection
  416. * @trans: transition that it's actions must be performed
  417. * @skb: event
  418. *
  419. * Executes actions that is related to happened event. Returns 0 for
  420. * success, 1 to indicate failure of at least one action.
  421. */
  422. static int llc_exec_conn_trans_actions(struct sock *sk,
  423. struct llc_conn_state_trans *trans,
  424. struct sk_buff *skb)
  425. {
  426. int rc = 0;
  427. llc_conn_action_t *next_action;
  428. for (next_action = trans->ev_actions;
  429. next_action && *next_action; next_action++) {
  430. int rc2 = (*next_action)(sk, skb);
  431. if (rc2 == 2) {
  432. rc = rc2;
  433. break;
  434. } else if (rc2)
  435. rc = 1;
  436. }
  437. return rc;
  438. }
  439. /**
  440. * __llc_lookup_established - Finds connection for the remote/local sap/mac
  441. * @sap: SAP
  442. * @daddr: address of remote LLC (MAC + SAP)
  443. * @laddr: address of local LLC (MAC + SAP)
  444. *
  445. * Search connection list of the SAP and finds connection using the remote
  446. * mac, remote sap, local mac, and local sap. Returns pointer for
  447. * connection found, %NULL otherwise.
  448. * Caller has to make sure local_bh is disabled.
  449. */
  450. static struct sock *__llc_lookup_established(struct llc_sap *sap,
  451. struct llc_addr *daddr,
  452. struct llc_addr *laddr)
  453. {
  454. struct sock *rc;
  455. struct hlist_node *node;
  456. read_lock(&sap->sk_list.lock);
  457. sk_for_each(rc, node, &sap->sk_list.list) {
  458. struct llc_sock *llc = llc_sk(rc);
  459. if (llc->laddr.lsap == laddr->lsap &&
  460. llc->daddr.lsap == daddr->lsap &&
  461. llc_mac_match(llc->laddr.mac, laddr->mac) &&
  462. llc_mac_match(llc->daddr.mac, daddr->mac)) {
  463. sock_hold(rc);
  464. goto found;
  465. }
  466. }
  467. rc = NULL;
  468. found:
  469. read_unlock(&sap->sk_list.lock);
  470. return rc;
  471. }
  472. struct sock *llc_lookup_established(struct llc_sap *sap,
  473. struct llc_addr *daddr,
  474. struct llc_addr *laddr)
  475. {
  476. struct sock *sk;
  477. local_bh_disable();
  478. sk = __llc_lookup_established(sap, daddr, laddr);
  479. local_bh_enable();
  480. return sk;
  481. }
  482. /**
  483. * llc_lookup_listener - Finds listener for local MAC + SAP
  484. * @sap: SAP
  485. * @laddr: address of local LLC (MAC + SAP)
  486. *
  487. * Search connection list of the SAP and finds connection listening on
  488. * local mac, and local sap. Returns pointer for parent socket found,
  489. * %NULL otherwise.
  490. * Caller has to make sure local_bh is disabled.
  491. */
  492. static struct sock *llc_lookup_listener(struct llc_sap *sap,
  493. struct llc_addr *laddr)
  494. {
  495. struct sock *rc;
  496. struct hlist_node *node;
  497. read_lock(&sap->sk_list.lock);
  498. sk_for_each(rc, node, &sap->sk_list.list) {
  499. struct llc_sock *llc = llc_sk(rc);
  500. if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
  501. llc->laddr.lsap == laddr->lsap &&
  502. (llc_mac_match(llc->laddr.mac, laddr->mac) ||
  503. llc_mac_null(llc->laddr.mac))) {
  504. sock_hold(rc);
  505. goto found;
  506. }
  507. }
  508. rc = NULL;
  509. found:
  510. read_unlock(&sap->sk_list.lock);
  511. return rc;
  512. }
  513. static struct sock *__llc_lookup(struct llc_sap *sap,
  514. struct llc_addr *daddr,
  515. struct llc_addr *laddr)
  516. {
  517. struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
  518. return sk ? : llc_lookup_listener(sap, laddr);
  519. }
  520. /**
  521. * llc_data_accept_state - designates if in this state data can be sent.
  522. * @state: state of connection.
  523. *
  524. * Returns 0 if data can be sent, 1 otherwise.
  525. */
  526. u8 llc_data_accept_state(u8 state)
  527. {
  528. return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
  529. state != LLC_CONN_STATE_REJ;
  530. }
  531. /**
  532. * llc_find_next_offset - finds offset for next category of transitions
  533. * @state: state table.
  534. * @offset: start offset.
  535. *
  536. * Finds offset of next category of transitions in transition table.
  537. * Returns the start index of next category.
  538. */
  539. static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
  540. {
  541. u16 cnt = 0;
  542. struct llc_conn_state_trans **next_trans;
  543. for (next_trans = state->transitions + offset;
  544. (*next_trans)->ev; next_trans++)
  545. ++cnt;
  546. return cnt;
  547. }
  548. /**
  549. * llc_build_offset_table - builds offset table of connection
  550. *
  551. * Fills offset table of connection state transition table
  552. * (llc_offset_table).
  553. */
  554. void __init llc_build_offset_table(void)
  555. {
  556. struct llc_conn_state *curr_state;
  557. int state, ev_type, next_offset;
  558. for (state = 0; state < NBR_CONN_STATES; state++) {
  559. curr_state = &llc_conn_state_table[state];
  560. next_offset = 0;
  561. for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
  562. llc_offset_table[state][ev_type] = next_offset;
  563. next_offset += llc_find_next_offset(curr_state,
  564. next_offset) + 1;
  565. }
  566. }
  567. }
  568. /**
  569. * llc_find_offset - finds start offset of category of transitions
  570. * @state: state of connection
  571. * @ev_type: type of happened event
  572. *
  573. * Finds start offset of desired category of transitions. Returns the
  574. * desired start offset.
  575. */
  576. static int llc_find_offset(int state, int ev_type)
  577. {
  578. int rc = 0;
  579. /* at this stage, llc_offset_table[..][2] is not important. it is for
  580. * init_pf_cycle and I don't know what is it.
  581. */
  582. switch (ev_type) {
  583. case LLC_CONN_EV_TYPE_PRIM:
  584. rc = llc_offset_table[state][0]; break;
  585. case LLC_CONN_EV_TYPE_PDU:
  586. rc = llc_offset_table[state][4]; break;
  587. case LLC_CONN_EV_TYPE_SIMPLE:
  588. rc = llc_offset_table[state][1]; break;
  589. case LLC_CONN_EV_TYPE_P_TMR:
  590. case LLC_CONN_EV_TYPE_ACK_TMR:
  591. case LLC_CONN_EV_TYPE_REJ_TMR:
  592. case LLC_CONN_EV_TYPE_BUSY_TMR:
  593. rc = llc_offset_table[state][3]; break;
  594. }
  595. return rc;
  596. }
  597. /**
  598. * llc_sap_add_socket - adds a socket to a SAP
  599. * @sap: SAP
  600. * @sk: socket
  601. *
  602. * This function adds a socket to sk_list of a SAP.
  603. */
  604. void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  605. {
  606. llc_sap_hold(sap);
  607. write_lock_bh(&sap->sk_list.lock);
  608. llc_sk(sk)->sap = sap;
  609. sk_add_node(sk, &sap->sk_list.list);
  610. write_unlock_bh(&sap->sk_list.lock);
  611. }
  612. /**
  613. * llc_sap_remove_socket - removes a socket from SAP
  614. * @sap: SAP
  615. * @sk: socket
  616. *
  617. * This function removes a connection from sk_list.list of a SAP if
  618. * the connection was in this list.
  619. */
  620. void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
  621. {
  622. write_lock_bh(&sap->sk_list.lock);
  623. sk_del_node_init(sk);
  624. write_unlock_bh(&sap->sk_list.lock);
  625. llc_sap_put(sap);
  626. }
  627. /**
  628. * llc_conn_rcv - sends received pdus to the connection state machine
  629. * @sk: current connection structure.
  630. * @skb: received frame.
  631. *
  632. * Sends received pdus to the connection state machine.
  633. */
  634. static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
  635. {
  636. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  637. ev->type = LLC_CONN_EV_TYPE_PDU;
  638. ev->reason = 0;
  639. return llc_conn_state_process(sk, skb);
  640. }
  641. static struct sock *llc_create_incoming_sock(struct sock *sk,
  642. struct net_device *dev,
  643. struct llc_addr *saddr,
  644. struct llc_addr *daddr)
  645. {
  646. struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
  647. sk->sk_prot);
  648. struct llc_sock *newllc, *llc = llc_sk(sk);
  649. if (!newsk)
  650. goto out;
  651. newllc = llc_sk(newsk);
  652. memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
  653. memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
  654. newllc->dev = dev;
  655. dev_hold(dev);
  656. llc_sap_add_socket(llc->sap, newsk);
  657. llc_sap_hold(llc->sap);
  658. out:
  659. return newsk;
  660. }
  661. void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
  662. {
  663. struct llc_addr saddr, daddr;
  664. struct sock *sk;
  665. llc_pdu_decode_sa(skb, saddr.mac);
  666. llc_pdu_decode_ssap(skb, &saddr.lsap);
  667. llc_pdu_decode_da(skb, daddr.mac);
  668. llc_pdu_decode_dsap(skb, &daddr.lsap);
  669. sk = __llc_lookup(sap, &saddr, &daddr);
  670. if (!sk)
  671. goto drop;
  672. bh_lock_sock(sk);
  673. /*
  674. * This has to be done here and not at the upper layer ->accept
  675. * method because of the way the PROCOM state machine works:
  676. * it needs to set several state variables (see, for instance,
  677. * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
  678. * the originator of the new connection, and this state has to be
  679. * in the newly created struct sock private area. -acme
  680. */
  681. if (unlikely(sk->sk_state == TCP_LISTEN)) {
  682. struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
  683. &saddr, &daddr);
  684. if (!newsk)
  685. goto drop_unlock;
  686. skb_set_owner_r(skb, newsk);
  687. } else {
  688. /*
  689. * Can't be skb_set_owner_r, this will be done at the
  690. * llc_conn_state_process function, later on, when we will use
  691. * skb_queue_rcv_skb to send it to upper layers, this is
  692. * another trick required to cope with how the PROCOM state
  693. * machine works. -acme
  694. */
  695. skb->sk = sk;
  696. }
  697. if (!sock_owned_by_user(sk))
  698. llc_conn_rcv(sk, skb);
  699. else {
  700. dprintk("%s: adding to backlog...\n", __func__);
  701. llc_set_backlog_type(skb, LLC_PACKET);
  702. sk_add_backlog(sk, skb);
  703. }
  704. out:
  705. bh_unlock_sock(sk);
  706. sock_put(sk);
  707. return;
  708. drop:
  709. kfree_skb(skb);
  710. return;
  711. drop_unlock:
  712. kfree_skb(skb);
  713. goto out;
  714. }
  715. #undef LLC_REFCNT_DEBUG
  716. #ifdef LLC_REFCNT_DEBUG
  717. static atomic_t llc_sock_nr;
  718. #endif
  719. /**
  720. * llc_backlog_rcv - Processes rx frames and expired timers.
  721. * @sk: LLC sock (p8022 connection)
  722. * @skb: queued rx frame or event
  723. *
  724. * This function processes frames that has received and timers that has
  725. * expired during sending an I pdu (refer to data_req_handler). frames
  726. * queue by llc_rcv function (llc_mac.c) and timers queue by timer
  727. * callback functions(llc_c_ac.c).
  728. */
  729. static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  730. {
  731. int rc = 0;
  732. struct llc_sock *llc = llc_sk(sk);
  733. if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
  734. if (likely(llc->state > 1)) /* not closed */
  735. rc = llc_conn_rcv(sk, skb);
  736. else
  737. goto out_kfree_skb;
  738. } else if (llc_backlog_type(skb) == LLC_EVENT) {
  739. /* timer expiration event */
  740. if (likely(llc->state > 1)) /* not closed */
  741. rc = llc_conn_state_process(sk, skb);
  742. else
  743. goto out_kfree_skb;
  744. } else {
  745. printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
  746. goto out_kfree_skb;
  747. }
  748. out:
  749. return rc;
  750. out_kfree_skb:
  751. kfree_skb(skb);
  752. goto out;
  753. }
  754. /**
  755. * llc_sk_init - Initializes a socket with default llc values.
  756. * @sk: socket to initialize.
  757. *
  758. * Initializes a socket with default llc values.
  759. */
  760. static void llc_sk_init(struct sock* sk)
  761. {
  762. struct llc_sock *llc = llc_sk(sk);
  763. llc->state = LLC_CONN_STATE_ADM;
  764. llc->inc_cntr = llc->dec_cntr = 2;
  765. llc->dec_step = llc->connect_step = 1;
  766. setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
  767. (unsigned long)sk);
  768. llc->ack_timer.expire = sysctl_llc2_ack_timeout;
  769. setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
  770. (unsigned long)sk);
  771. llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
  772. setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
  773. (unsigned long)sk);
  774. llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
  775. setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
  776. (unsigned long)sk);
  777. llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
  778. llc->n2 = 2; /* max retransmit */
  779. llc->k = 2; /* tx win size, will adjust dynam */
  780. llc->rw = 128; /* rx win size (opt and equal to
  781. * tx_win of remote LLC) */
  782. skb_queue_head_init(&llc->pdu_unack_q);
  783. sk->sk_backlog_rcv = llc_backlog_rcv;
  784. }
  785. /**
  786. * llc_sk_alloc - Allocates LLC sock
  787. * @family: upper layer protocol family
  788. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  789. *
  790. * Allocates a LLC sock and initializes it. Returns the new LLC sock
  791. * or %NULL if there's no memory available for one
  792. */
  793. struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
  794. {
  795. struct sock *sk = sk_alloc(net, family, priority, prot);
  796. if (!sk)
  797. goto out;
  798. llc_sk_init(sk);
  799. sock_init_data(NULL, sk);
  800. #ifdef LLC_REFCNT_DEBUG
  801. atomic_inc(&llc_sock_nr);
  802. printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
  803. __func__, atomic_read(&llc_sock_nr));
  804. #endif
  805. out:
  806. return sk;
  807. }
  808. /**
  809. * llc_sk_free - Frees a LLC socket
  810. * @sk - socket to free
  811. *
  812. * Frees a LLC socket
  813. */
  814. void llc_sk_free(struct sock *sk)
  815. {
  816. struct llc_sock *llc = llc_sk(sk);
  817. llc->state = LLC_CONN_OUT_OF_SVC;
  818. /* Stop all (possibly) running timers */
  819. llc_conn_ac_stop_all_timers(sk, NULL);
  820. #ifdef DEBUG_LLC_CONN_ALLOC
  821. printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
  822. skb_queue_len(&llc->pdu_unack_q),
  823. skb_queue_len(&sk->sk_write_queue));
  824. #endif
  825. skb_queue_purge(&sk->sk_receive_queue);
  826. skb_queue_purge(&sk->sk_write_queue);
  827. skb_queue_purge(&llc->pdu_unack_q);
  828. #ifdef LLC_REFCNT_DEBUG
  829. if (atomic_read(&sk->sk_refcnt) != 1) {
  830. printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
  831. sk, __func__, atomic_read(&sk->sk_refcnt));
  832. printk(KERN_DEBUG "%d LLC sockets are still alive\n",
  833. atomic_read(&llc_sock_nr));
  834. } else {
  835. atomic_dec(&llc_sock_nr);
  836. printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
  837. __func__, atomic_read(&llc_sock_nr));
  838. }
  839. #endif
  840. sock_put(sk);
  841. }
  842. /**
  843. * llc_sk_reset - resets a connection
  844. * @sk: LLC socket to reset
  845. *
  846. * Resets a connection to the out of service state. Stops its timers
  847. * and frees any frames in the queues of the connection.
  848. */
  849. void llc_sk_reset(struct sock *sk)
  850. {
  851. struct llc_sock *llc = llc_sk(sk);
  852. llc_conn_ac_stop_all_timers(sk, NULL);
  853. skb_queue_purge(&sk->sk_write_queue);
  854. skb_queue_purge(&llc->pdu_unack_q);
  855. llc->remote_busy_flag = 0;
  856. llc->cause_flag = 0;
  857. llc->retry_count = 0;
  858. llc_conn_set_p_flag(sk, 0);
  859. llc->f_flag = 0;
  860. llc->s_flag = 0;
  861. llc->ack_pf = 0;
  862. llc->first_pdu_Ns = 0;
  863. llc->ack_must_be_send = 0;
  864. llc->dec_step = 1;
  865. llc->inc_cntr = 2;
  866. llc->dec_cntr = 2;
  867. llc->X = 0;
  868. llc->failed_data_req = 0 ;
  869. llc->last_nr = 0;
  870. }