llc_conn.c 26 KB

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