llc_conn.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. int slot = llc_sk_laddr_hashfn(sap, laddr);
  464. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  465. rcu_read_lock();
  466. again:
  467. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  468. if (llc_estab_match(sap, daddr, laddr, rc)) {
  469. /* Extra checks required by SLAB_DESTROY_BY_RCU */
  470. if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
  471. goto again;
  472. if (unlikely(llc_sk(rc)->sap != sap ||
  473. !llc_estab_match(sap, daddr, laddr, rc))) {
  474. sock_put(rc);
  475. continue;
  476. }
  477. goto found;
  478. }
  479. }
  480. rc = NULL;
  481. /*
  482. * if the nulls value we got at the end of this lookup is
  483. * not the expected one, we must restart lookup.
  484. * We probably met an item that was moved to another chain.
  485. */
  486. if (unlikely(get_nulls_value(node) != slot))
  487. goto again;
  488. found:
  489. rcu_read_unlock();
  490. return rc;
  491. }
  492. struct sock *llc_lookup_established(struct llc_sap *sap,
  493. struct llc_addr *daddr,
  494. struct llc_addr *laddr)
  495. {
  496. struct sock *sk;
  497. local_bh_disable();
  498. sk = __llc_lookup_established(sap, daddr, laddr);
  499. local_bh_enable();
  500. return sk;
  501. }
  502. static inline bool llc_listener_match(const struct llc_sap *sap,
  503. const struct llc_addr *laddr,
  504. const struct sock *sk)
  505. {
  506. struct llc_sock *llc = llc_sk(sk);
  507. return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
  508. llc->laddr.lsap == laddr->lsap &&
  509. llc_mac_match(llc->laddr.mac, laddr->mac);
  510. }
  511. static struct sock *__llc_lookup_listener(struct llc_sap *sap,
  512. struct llc_addr *laddr)
  513. {
  514. struct sock *rc;
  515. struct hlist_nulls_node *node;
  516. int slot = llc_sk_laddr_hashfn(sap, laddr);
  517. struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
  518. rcu_read_lock();
  519. again:
  520. sk_nulls_for_each_rcu(rc, node, laddr_hb) {
  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. /*
  535. * if the nulls value we got at the end of this lookup is
  536. * not the expected one, we must restart lookup.
  537. * We probably met an item that was moved to another chain.
  538. */
  539. if (unlikely(get_nulls_value(node) != slot))
  540. goto again;
  541. found:
  542. rcu_read_unlock();
  543. return rc;
  544. }
  545. /**
  546. * llc_lookup_listener - Finds listener for local MAC + SAP
  547. * @sap: SAP
  548. * @laddr: address of local LLC (MAC + SAP)
  549. *
  550. * Search connection list of the SAP and finds connection listening on
  551. * local mac, and local sap. Returns pointer for parent socket found,
  552. * %NULL otherwise.
  553. * Caller has to make sure local_bh is disabled.
  554. */
  555. static struct sock *llc_lookup_listener(struct llc_sap *sap,
  556. struct llc_addr *laddr)
  557. {
  558. static struct llc_addr null_addr;
  559. struct sock *rc = __llc_lookup_listener(sap, laddr);
  560. if (!rc)
  561. rc = __llc_lookup_listener(sap, &null_addr);
  562. return rc;
  563. }
  564. static struct sock *__llc_lookup(struct llc_sap *sap,
  565. struct llc_addr *daddr,
  566. struct llc_addr *laddr)
  567. {
  568. struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
  569. return sk ? : llc_lookup_listener(sap, laddr);
  570. }
  571. /**
  572. * llc_data_accept_state - designates if in this state data can be sent.
  573. * @state: state of connection.
  574. *
  575. * Returns 0 if data can be sent, 1 otherwise.
  576. */
  577. u8 llc_data_accept_state(u8 state)
  578. {
  579. return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
  580. state != LLC_CONN_STATE_REJ;
  581. }
  582. /**
  583. * llc_find_next_offset - finds offset for next category of transitions
  584. * @state: state table.
  585. * @offset: start offset.
  586. *
  587. * Finds offset of next category of transitions in transition table.
  588. * Returns the start index of next category.
  589. */
  590. static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
  591. {
  592. u16 cnt = 0;
  593. struct llc_conn_state_trans **next_trans;
  594. for (next_trans = state->transitions + offset;
  595. (*next_trans)->ev; next_trans++)
  596. ++cnt;
  597. return cnt;
  598. }
  599. /**
  600. * llc_build_offset_table - builds offset table of connection
  601. *
  602. * Fills offset table of connection state transition table
  603. * (llc_offset_table).
  604. */
  605. void __init llc_build_offset_table(void)
  606. {
  607. struct llc_conn_state *curr_state;
  608. int state, ev_type, next_offset;
  609. for (state = 0; state < NBR_CONN_STATES; state++) {
  610. curr_state = &llc_conn_state_table[state];
  611. next_offset = 0;
  612. for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
  613. llc_offset_table[state][ev_type] = next_offset;
  614. next_offset += llc_find_next_offset(curr_state,
  615. next_offset) + 1;
  616. }
  617. }
  618. }
  619. /**
  620. * llc_find_offset - finds start offset of category of transitions
  621. * @state: state of connection
  622. * @ev_type: type of happened event
  623. *
  624. * Finds start offset of desired category of transitions. Returns the
  625. * desired start offset.
  626. */
  627. static int llc_find_offset(int state, int ev_type)
  628. {
  629. int rc = 0;
  630. /* at this stage, llc_offset_table[..][2] is not important. it is for
  631. * init_pf_cycle and I don't know what is it.
  632. */
  633. switch (ev_type) {
  634. case LLC_CONN_EV_TYPE_PRIM:
  635. rc = llc_offset_table[state][0]; break;
  636. case LLC_CONN_EV_TYPE_PDU:
  637. rc = llc_offset_table[state][4]; break;
  638. case LLC_CONN_EV_TYPE_SIMPLE:
  639. rc = llc_offset_table[state][1]; break;
  640. case LLC_CONN_EV_TYPE_P_TMR:
  641. case LLC_CONN_EV_TYPE_ACK_TMR:
  642. case LLC_CONN_EV_TYPE_REJ_TMR:
  643. case LLC_CONN_EV_TYPE_BUSY_TMR:
  644. rc = llc_offset_table[state][3]; break;
  645. }
  646. return rc;
  647. }
  648. /**
  649. * llc_sap_add_socket - adds a socket to a SAP
  650. * @sap: SAP
  651. * @sk: socket
  652. *
  653. * This function adds a socket to the hash tables of a SAP.
  654. */
  655. void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  656. {
  657. struct llc_sock *llc = llc_sk(sk);
  658. struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
  659. struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
  660. llc_sap_hold(sap);
  661. llc_sk(sk)->sap = sap;
  662. spin_lock_bh(&sap->sk_lock);
  663. sap->sk_count++;
  664. sk_nulls_add_node_rcu(sk, laddr_hb);
  665. hlist_add_head(&llc->dev_hash_node, dev_hb);
  666. spin_unlock_bh(&sap->sk_lock);
  667. }
  668. /**
  669. * llc_sap_remove_socket - removes a socket from SAP
  670. * @sap: SAP
  671. * @sk: socket
  672. *
  673. * This function removes a connection from the hash tables of a SAP if
  674. * the connection was in this list.
  675. */
  676. void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
  677. {
  678. struct llc_sock *llc = llc_sk(sk);
  679. spin_lock_bh(&sap->sk_lock);
  680. sk_nulls_del_node_init_rcu(sk);
  681. hlist_del(&llc->dev_hash_node);
  682. sap->sk_count--;
  683. spin_unlock_bh(&sap->sk_lock);
  684. llc_sap_put(sap);
  685. }
  686. /**
  687. * llc_conn_rcv - sends received pdus to the connection state machine
  688. * @sk: current connection structure.
  689. * @skb: received frame.
  690. *
  691. * Sends received pdus to the connection state machine.
  692. */
  693. static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
  694. {
  695. struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  696. ev->type = LLC_CONN_EV_TYPE_PDU;
  697. ev->reason = 0;
  698. return llc_conn_state_process(sk, skb);
  699. }
  700. static struct sock *llc_create_incoming_sock(struct sock *sk,
  701. struct net_device *dev,
  702. struct llc_addr *saddr,
  703. struct llc_addr *daddr)
  704. {
  705. struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
  706. sk->sk_prot);
  707. struct llc_sock *newllc, *llc = llc_sk(sk);
  708. if (!newsk)
  709. goto out;
  710. newllc = llc_sk(newsk);
  711. memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
  712. memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
  713. newllc->dev = dev;
  714. dev_hold(dev);
  715. llc_sap_add_socket(llc->sap, newsk);
  716. llc_sap_hold(llc->sap);
  717. out:
  718. return newsk;
  719. }
  720. void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
  721. {
  722. struct llc_addr saddr, daddr;
  723. struct sock *sk;
  724. llc_pdu_decode_sa(skb, saddr.mac);
  725. llc_pdu_decode_ssap(skb, &saddr.lsap);
  726. llc_pdu_decode_da(skb, daddr.mac);
  727. llc_pdu_decode_dsap(skb, &daddr.lsap);
  728. sk = __llc_lookup(sap, &saddr, &daddr);
  729. if (!sk)
  730. goto drop;
  731. bh_lock_sock(sk);
  732. /*
  733. * This has to be done here and not at the upper layer ->accept
  734. * method because of the way the PROCOM state machine works:
  735. * it needs to set several state variables (see, for instance,
  736. * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
  737. * the originator of the new connection, and this state has to be
  738. * in the newly created struct sock private area. -acme
  739. */
  740. if (unlikely(sk->sk_state == TCP_LISTEN)) {
  741. struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
  742. &saddr, &daddr);
  743. if (!newsk)
  744. goto drop_unlock;
  745. skb_set_owner_r(skb, newsk);
  746. } else {
  747. /*
  748. * Can't be skb_set_owner_r, this will be done at the
  749. * llc_conn_state_process function, later on, when we will use
  750. * skb_queue_rcv_skb to send it to upper layers, this is
  751. * another trick required to cope with how the PROCOM state
  752. * machine works. -acme
  753. */
  754. skb->sk = sk;
  755. }
  756. if (!sock_owned_by_user(sk))
  757. llc_conn_rcv(sk, skb);
  758. else {
  759. dprintk("%s: adding to backlog...\n", __func__);
  760. llc_set_backlog_type(skb, LLC_PACKET);
  761. if (sk_add_backlog(sk, skb))
  762. goto drop_unlock;
  763. }
  764. out:
  765. bh_unlock_sock(sk);
  766. sock_put(sk);
  767. return;
  768. drop:
  769. kfree_skb(skb);
  770. return;
  771. drop_unlock:
  772. kfree_skb(skb);
  773. goto out;
  774. }
  775. #undef LLC_REFCNT_DEBUG
  776. #ifdef LLC_REFCNT_DEBUG
  777. static atomic_t llc_sock_nr;
  778. #endif
  779. /**
  780. * llc_backlog_rcv - Processes rx frames and expired timers.
  781. * @sk: LLC sock (p8022 connection)
  782. * @skb: queued rx frame or event
  783. *
  784. * This function processes frames that has received and timers that has
  785. * expired during sending an I pdu (refer to data_req_handler). frames
  786. * queue by llc_rcv function (llc_mac.c) and timers queue by timer
  787. * callback functions(llc_c_ac.c).
  788. */
  789. static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  790. {
  791. int rc = 0;
  792. struct llc_sock *llc = llc_sk(sk);
  793. if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
  794. if (likely(llc->state > 1)) /* not closed */
  795. rc = llc_conn_rcv(sk, skb);
  796. else
  797. goto out_kfree_skb;
  798. } else if (llc_backlog_type(skb) == LLC_EVENT) {
  799. /* timer expiration event */
  800. if (likely(llc->state > 1)) /* not closed */
  801. rc = llc_conn_state_process(sk, skb);
  802. else
  803. goto out_kfree_skb;
  804. } else {
  805. printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
  806. goto out_kfree_skb;
  807. }
  808. out:
  809. return rc;
  810. out_kfree_skb:
  811. kfree_skb(skb);
  812. goto out;
  813. }
  814. /**
  815. * llc_sk_init - Initializes a socket with default llc values.
  816. * @sk: socket to initialize.
  817. *
  818. * Initializes a socket with default llc values.
  819. */
  820. static void llc_sk_init(struct sock* sk)
  821. {
  822. struct llc_sock *llc = llc_sk(sk);
  823. llc->state = LLC_CONN_STATE_ADM;
  824. llc->inc_cntr = llc->dec_cntr = 2;
  825. llc->dec_step = llc->connect_step = 1;
  826. setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
  827. (unsigned long)sk);
  828. llc->ack_timer.expire = sysctl_llc2_ack_timeout;
  829. setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
  830. (unsigned long)sk);
  831. llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
  832. setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
  833. (unsigned long)sk);
  834. llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
  835. setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
  836. (unsigned long)sk);
  837. llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
  838. llc->n2 = 2; /* max retransmit */
  839. llc->k = 2; /* tx win size, will adjust dynam */
  840. llc->rw = 128; /* rx win size (opt and equal to
  841. * tx_win of remote LLC) */
  842. skb_queue_head_init(&llc->pdu_unack_q);
  843. sk->sk_backlog_rcv = llc_backlog_rcv;
  844. }
  845. /**
  846. * llc_sk_alloc - Allocates LLC sock
  847. * @family: upper layer protocol family
  848. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  849. *
  850. * Allocates a LLC sock and initializes it. Returns the new LLC sock
  851. * or %NULL if there's no memory available for one
  852. */
  853. struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
  854. {
  855. struct sock *sk = sk_alloc(net, family, priority, prot);
  856. if (!sk)
  857. goto out;
  858. llc_sk_init(sk);
  859. sock_init_data(NULL, sk);
  860. #ifdef LLC_REFCNT_DEBUG
  861. atomic_inc(&llc_sock_nr);
  862. printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
  863. __func__, atomic_read(&llc_sock_nr));
  864. #endif
  865. out:
  866. return sk;
  867. }
  868. /**
  869. * llc_sk_free - Frees a LLC socket
  870. * @sk - socket to free
  871. *
  872. * Frees a LLC socket
  873. */
  874. void llc_sk_free(struct sock *sk)
  875. {
  876. struct llc_sock *llc = llc_sk(sk);
  877. llc->state = LLC_CONN_OUT_OF_SVC;
  878. /* Stop all (possibly) running timers */
  879. llc_conn_ac_stop_all_timers(sk, NULL);
  880. #ifdef DEBUG_LLC_CONN_ALLOC
  881. printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
  882. skb_queue_len(&llc->pdu_unack_q),
  883. skb_queue_len(&sk->sk_write_queue));
  884. #endif
  885. skb_queue_purge(&sk->sk_receive_queue);
  886. skb_queue_purge(&sk->sk_write_queue);
  887. skb_queue_purge(&llc->pdu_unack_q);
  888. #ifdef LLC_REFCNT_DEBUG
  889. if (atomic_read(&sk->sk_refcnt) != 1) {
  890. printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
  891. sk, __func__, atomic_read(&sk->sk_refcnt));
  892. printk(KERN_DEBUG "%d LLC sockets are still alive\n",
  893. atomic_read(&llc_sock_nr));
  894. } else {
  895. atomic_dec(&llc_sock_nr);
  896. printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
  897. __func__, atomic_read(&llc_sock_nr));
  898. }
  899. #endif
  900. sock_put(sk);
  901. }
  902. /**
  903. * llc_sk_reset - resets a connection
  904. * @sk: LLC socket to reset
  905. *
  906. * Resets a connection to the out of service state. Stops its timers
  907. * and frees any frames in the queues of the connection.
  908. */
  909. void llc_sk_reset(struct sock *sk)
  910. {
  911. struct llc_sock *llc = llc_sk(sk);
  912. llc_conn_ac_stop_all_timers(sk, NULL);
  913. skb_queue_purge(&sk->sk_write_queue);
  914. skb_queue_purge(&llc->pdu_unack_q);
  915. llc->remote_busy_flag = 0;
  916. llc->cause_flag = 0;
  917. llc->retry_count = 0;
  918. llc_conn_set_p_flag(sk, 0);
  919. llc->f_flag = 0;
  920. llc->s_flag = 0;
  921. llc->ack_pf = 0;
  922. llc->first_pdu_Ns = 0;
  923. llc->ack_must_be_send = 0;
  924. llc->dec_step = 1;
  925. llc->inc_cntr = 2;
  926. llc->dec_cntr = 2;
  927. llc->X = 0;
  928. llc->failed_data_req = 0 ;
  929. llc->last_nr = 0;
  930. }