llc_conn.c 27 KB

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