llc_conn.c 28 KB

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