llc_station.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * llc_station.c - station component of LLC
  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/module.h>
  16. #include <linux/slab.h>
  17. #include <net/llc.h>
  18. #include <net/llc_sap.h>
  19. #include <net/llc_conn.h>
  20. #include <net/llc_c_ac.h>
  21. #include <net/llc_s_ac.h>
  22. #include <net/llc_c_ev.h>
  23. #include <net/llc_c_st.h>
  24. #include <net/llc_s_ev.h>
  25. #include <net/llc_s_st.h>
  26. #include <net/llc_pdu.h>
  27. /**
  28. * struct llc_station - LLC station component
  29. *
  30. * SAP and connection resource manager, one per adapter.
  31. *
  32. * @mac_sa: MAC source address
  33. * @sap_list: list of related SAPs
  34. * @mac_pdu_q: PDUs ready to send to MAC
  35. */
  36. struct llc_station {
  37. struct sk_buff_head mac_pdu_q;
  38. };
  39. typedef int (*llc_station_ev_t)(struct sk_buff *skb);
  40. typedef int (*llc_station_action_t)(struct sk_buff *skb);
  41. /* Station component state table structure */
  42. struct llc_station_state_trans {
  43. llc_station_ev_t ev;
  44. llc_station_action_t *ev_actions;
  45. };
  46. static struct llc_station llc_main_station;
  47. static int llc_stat_ev_rx_null_dsap_xid_c(struct sk_buff *skb)
  48. {
  49. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  50. return LLC_PDU_IS_CMD(pdu) && /* command PDU */
  51. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  52. LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_XID &&
  53. !pdu->dsap ? 0 : 1; /* NULL DSAP value */
  54. }
  55. static int llc_stat_ev_rx_null_dsap_test_c(struct sk_buff *skb)
  56. {
  57. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  58. return LLC_PDU_IS_CMD(pdu) && /* command PDU */
  59. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  60. LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_TEST &&
  61. !pdu->dsap ? 0 : 1; /* NULL DSAP */
  62. }
  63. /**
  64. * llc_station_send_pdu - queues PDU to send
  65. * @skb: Address of the PDU
  66. *
  67. * Queues a PDU to send to the MAC layer.
  68. */
  69. static void llc_station_send_pdu(struct sk_buff *skb)
  70. {
  71. skb_queue_tail(&llc_main_station.mac_pdu_q, skb);
  72. while ((skb = skb_dequeue(&llc_main_station.mac_pdu_q)) != NULL)
  73. if (dev_queue_xmit(skb))
  74. break;
  75. }
  76. static int llc_station_ac_send_xid_r(struct sk_buff *skb)
  77. {
  78. u8 mac_da[ETH_ALEN], dsap;
  79. int rc = 1;
  80. struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U,
  81. sizeof(struct llc_xid_info));
  82. if (!nskb)
  83. goto out;
  84. rc = 0;
  85. llc_pdu_decode_sa(skb, mac_da);
  86. llc_pdu_decode_ssap(skb, &dsap);
  87. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  88. llc_pdu_init_as_xid_rsp(nskb, LLC_XID_NULL_CLASS_2, 127);
  89. rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da);
  90. if (unlikely(rc))
  91. goto free;
  92. llc_station_send_pdu(nskb);
  93. out:
  94. return rc;
  95. free:
  96. kfree_skb(nskb);
  97. goto out;
  98. }
  99. static int llc_station_ac_send_test_r(struct sk_buff *skb)
  100. {
  101. u8 mac_da[ETH_ALEN], dsap;
  102. int rc = 1;
  103. u32 data_size;
  104. struct sk_buff *nskb;
  105. /* The test request command is type U (llc_len = 3) */
  106. data_size = ntohs(eth_hdr(skb)->h_proto) - 3;
  107. nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, data_size);
  108. if (!nskb)
  109. goto out;
  110. rc = 0;
  111. llc_pdu_decode_sa(skb, mac_da);
  112. llc_pdu_decode_ssap(skb, &dsap);
  113. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  114. llc_pdu_init_as_test_rsp(nskb, skb);
  115. rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da);
  116. if (unlikely(rc))
  117. goto free;
  118. llc_station_send_pdu(nskb);
  119. out:
  120. return rc;
  121. free:
  122. kfree_skb(nskb);
  123. goto out;
  124. }
  125. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */
  126. static llc_station_action_t llc_stat_up_state_actions_2[] = {
  127. [0] = llc_station_ac_send_xid_r,
  128. [1] = NULL,
  129. };
  130. static struct llc_station_state_trans llc_stat_up_state_trans_2 = {
  131. .ev = llc_stat_ev_rx_null_dsap_xid_c,
  132. .ev_actions = llc_stat_up_state_actions_2,
  133. };
  134. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_TEST_C event */
  135. static llc_station_action_t llc_stat_up_state_actions_3[] = {
  136. [0] = llc_station_ac_send_test_r,
  137. [1] = NULL,
  138. };
  139. static struct llc_station_state_trans llc_stat_up_state_trans_3 = {
  140. .ev = llc_stat_ev_rx_null_dsap_test_c,
  141. .ev_actions = llc_stat_up_state_actions_3,
  142. };
  143. /* array of pointers; one to each transition */
  144. static struct llc_station_state_trans *llc_stat_up_state_trans [] = {
  145. &llc_stat_up_state_trans_2,
  146. &llc_stat_up_state_trans_3,
  147. NULL,
  148. };
  149. /**
  150. * llc_exec_station_trans_actions - executes actions for transition
  151. * @trans: Address of the transition
  152. * @skb: Address of the event that caused the transition
  153. *
  154. * Executes actions of a transition of the station state machine. Returns
  155. * 0 if all actions complete successfully, nonzero otherwise.
  156. */
  157. static u16 llc_exec_station_trans_actions(struct llc_station_state_trans *trans,
  158. struct sk_buff *skb)
  159. {
  160. u16 rc = 0;
  161. llc_station_action_t *next_action = trans->ev_actions;
  162. for (; next_action && *next_action; next_action++)
  163. if ((*next_action)(skb))
  164. rc = 1;
  165. return rc;
  166. }
  167. /**
  168. * llc_find_station_trans - finds transition for this event
  169. * @skb: Address of the event
  170. *
  171. * Search thru events of the current state of the station until list
  172. * exhausted or it's obvious that the event is not valid for the current
  173. * state. Returns the address of the transition if cound, %NULL otherwise.
  174. */
  175. static struct llc_station_state_trans *
  176. llc_find_station_trans(struct sk_buff *skb)
  177. {
  178. int i = 0;
  179. struct llc_station_state_trans *rc = NULL;
  180. struct llc_station_state_trans **next_trans;
  181. for (next_trans = llc_stat_up_state_trans; next_trans[i]; i++)
  182. if (!next_trans[i]->ev(skb)) {
  183. rc = next_trans[i];
  184. break;
  185. }
  186. return rc;
  187. }
  188. /**
  189. * llc_station_rcv - send received pdu to the station state machine
  190. * @skb: received frame.
  191. *
  192. * Sends data unit to station state machine.
  193. */
  194. static void llc_station_rcv(struct sk_buff *skb)
  195. {
  196. struct llc_station_state_trans *trans;
  197. trans = llc_find_station_trans(skb);
  198. if (trans)
  199. llc_exec_station_trans_actions(trans, skb);
  200. kfree_skb(skb);
  201. }
  202. void __init llc_station_init(void)
  203. {
  204. skb_queue_head_init(&llc_main_station.mac_pdu_q);
  205. llc_set_station_handler(llc_station_rcv);
  206. }
  207. void llc_station_exit(void)
  208. {
  209. llc_set_station_handler(NULL);
  210. }