llc_conn.c 27 KB

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