llc_station.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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/config.h>
  15. #include <linux/init.h>
  16. #include <linux/module.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. * @state - state of station
  33. * @xid_r_count - XID response PDU counter
  34. * @mac_sa - MAC source address
  35. * @sap_list - list of related SAPs
  36. * @ev_q - events entering state mach.
  37. * @mac_pdu_q - PDUs ready to send to MAC
  38. */
  39. struct llc_station {
  40. u8 state;
  41. u8 xid_r_count;
  42. struct timer_list ack_timer;
  43. u8 retry_count;
  44. u8 maximum_retry;
  45. struct {
  46. struct sk_buff_head list;
  47. spinlock_t lock;
  48. } ev_q;
  49. struct sk_buff_head mac_pdu_q;
  50. };
  51. #define LLC_STATION_ACK_TIME (3 * HZ)
  52. int sysctl_llc_station_ack_timeout = LLC_STATION_ACK_TIME;
  53. /* Types of events (possible values in 'ev->type') */
  54. #define LLC_STATION_EV_TYPE_SIMPLE 1
  55. #define LLC_STATION_EV_TYPE_CONDITION 2
  56. #define LLC_STATION_EV_TYPE_PRIM 3
  57. #define LLC_STATION_EV_TYPE_PDU 4 /* command/response PDU */
  58. #define LLC_STATION_EV_TYPE_ACK_TMR 5
  59. #define LLC_STATION_EV_TYPE_RPT_STATUS 6
  60. /* Events */
  61. #define LLC_STATION_EV_ENABLE_WITH_DUP_ADDR_CHECK 1
  62. #define LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK 2
  63. #define LLC_STATION_EV_ACK_TMR_EXP_LT_RETRY_CNT_MAX_RETRY 3
  64. #define LLC_STATION_EV_ACK_TMR_EXP_EQ_RETRY_CNT_MAX_RETRY 4
  65. #define LLC_STATION_EV_RX_NULL_DSAP_XID_C 5
  66. #define LLC_STATION_EV_RX_NULL_DSAP_0_XID_R_XID_R_CNT_EQ 6
  67. #define LLC_STATION_EV_RX_NULL_DSAP_1_XID_R_XID_R_CNT_EQ 7
  68. #define LLC_STATION_EV_RX_NULL_DSAP_TEST_C 8
  69. #define LLC_STATION_EV_DISABLE_REQ 9
  70. struct llc_station_state_ev {
  71. u8 type;
  72. u8 prim;
  73. u8 prim_type;
  74. u8 reason;
  75. struct list_head node; /* node in station->ev_q.list */
  76. };
  77. static __inline__ struct llc_station_state_ev *
  78. llc_station_ev(struct sk_buff *skb)
  79. {
  80. return (struct llc_station_state_ev *)skb->cb;
  81. }
  82. typedef int (*llc_station_ev_t)(struct sk_buff *skb);
  83. #define LLC_STATION_STATE_DOWN 1 /* initial state */
  84. #define LLC_STATION_STATE_DUP_ADDR_CHK 2
  85. #define LLC_STATION_STATE_UP 3
  86. #define LLC_NBR_STATION_STATES 3 /* size of state table */
  87. typedef int (*llc_station_action_t)(struct sk_buff *skb);
  88. /* Station component state table structure */
  89. struct llc_station_state_trans {
  90. llc_station_ev_t ev;
  91. u8 next_state;
  92. llc_station_action_t *ev_actions;
  93. };
  94. struct llc_station_state {
  95. u8 curr_state;
  96. struct llc_station_state_trans **transitions;
  97. };
  98. static struct llc_station llc_main_station;
  99. static int llc_stat_ev_enable_with_dup_addr_check(struct sk_buff *skb)
  100. {
  101. struct llc_station_state_ev *ev = llc_station_ev(skb);
  102. return ev->type == LLC_STATION_EV_TYPE_SIMPLE &&
  103. ev->prim_type ==
  104. LLC_STATION_EV_ENABLE_WITH_DUP_ADDR_CHECK ? 0 : 1;
  105. }
  106. static int llc_stat_ev_enable_without_dup_addr_check(struct sk_buff *skb)
  107. {
  108. struct llc_station_state_ev *ev = llc_station_ev(skb);
  109. return ev->type == LLC_STATION_EV_TYPE_SIMPLE &&
  110. ev->prim_type ==
  111. LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK ? 0 : 1;
  112. }
  113. static int llc_stat_ev_ack_tmr_exp_lt_retry_cnt_max_retry(struct sk_buff *skb)
  114. {
  115. struct llc_station_state_ev *ev = llc_station_ev(skb);
  116. return ev->type == LLC_STATION_EV_TYPE_ACK_TMR &&
  117. llc_main_station.retry_count <
  118. llc_main_station.maximum_retry ? 0 : 1;
  119. }
  120. static int llc_stat_ev_ack_tmr_exp_eq_retry_cnt_max_retry(struct sk_buff *skb)
  121. {
  122. struct llc_station_state_ev *ev = llc_station_ev(skb);
  123. return ev->type == LLC_STATION_EV_TYPE_ACK_TMR &&
  124. llc_main_station.retry_count ==
  125. llc_main_station.maximum_retry ? 0 : 1;
  126. }
  127. static int llc_stat_ev_rx_null_dsap_xid_c(struct sk_buff *skb)
  128. {
  129. struct llc_station_state_ev *ev = llc_station_ev(skb);
  130. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  131. return ev->type == LLC_STATION_EV_TYPE_PDU &&
  132. LLC_PDU_IS_CMD(pdu) && /* command PDU */
  133. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  134. LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_XID &&
  135. !pdu->dsap ? 0 : 1; /* NULL DSAP value */
  136. }
  137. static int llc_stat_ev_rx_null_dsap_0_xid_r_xid_r_cnt_eq(struct sk_buff *skb)
  138. {
  139. struct llc_station_state_ev *ev = llc_station_ev(skb);
  140. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  141. return ev->type == LLC_STATION_EV_TYPE_PDU &&
  142. LLC_PDU_IS_RSP(pdu) && /* response PDU */
  143. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  144. LLC_U_PDU_RSP(pdu) == LLC_1_PDU_CMD_XID &&
  145. !pdu->dsap && /* NULL DSAP value */
  146. !llc_main_station.xid_r_count ? 0 : 1;
  147. }
  148. static int llc_stat_ev_rx_null_dsap_1_xid_r_xid_r_cnt_eq(struct sk_buff *skb)
  149. {
  150. struct llc_station_state_ev *ev = llc_station_ev(skb);
  151. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  152. return ev->type == LLC_STATION_EV_TYPE_PDU &&
  153. LLC_PDU_IS_RSP(pdu) && /* response PDU */
  154. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  155. LLC_U_PDU_RSP(pdu) == LLC_1_PDU_CMD_XID &&
  156. !pdu->dsap && /* NULL DSAP value */
  157. llc_main_station.xid_r_count == 1 ? 0 : 1;
  158. }
  159. static int llc_stat_ev_rx_null_dsap_test_c(struct sk_buff *skb)
  160. {
  161. struct llc_station_state_ev *ev = llc_station_ev(skb);
  162. struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
  163. return ev->type == LLC_STATION_EV_TYPE_PDU &&
  164. LLC_PDU_IS_CMD(pdu) && /* command PDU */
  165. LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */
  166. LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_TEST &&
  167. !pdu->dsap ? 0 : 1; /* NULL DSAP */
  168. }
  169. static int llc_stat_ev_disable_req(struct sk_buff *skb)
  170. {
  171. struct llc_station_state_ev *ev = llc_station_ev(skb);
  172. return ev->type == LLC_STATION_EV_TYPE_PRIM &&
  173. ev->prim == LLC_DISABLE_PRIM &&
  174. ev->prim_type == LLC_PRIM_TYPE_REQ ? 0 : 1;
  175. }
  176. /**
  177. * llc_station_send_pdu - queues PDU to send
  178. * @skb: Address of the PDU
  179. *
  180. * Queues a PDU to send to the MAC layer.
  181. */
  182. static void llc_station_send_pdu(struct sk_buff *skb)
  183. {
  184. skb_queue_tail(&llc_main_station.mac_pdu_q, skb);
  185. while ((skb = skb_dequeue(&llc_main_station.mac_pdu_q)) != NULL)
  186. if (dev_queue_xmit(skb))
  187. break;
  188. }
  189. static int llc_station_ac_start_ack_timer(struct sk_buff *skb)
  190. {
  191. mod_timer(&llc_main_station.ack_timer,
  192. jiffies + sysctl_llc_station_ack_timeout);
  193. return 0;
  194. }
  195. static int llc_station_ac_set_retry_cnt_0(struct sk_buff *skb)
  196. {
  197. llc_main_station.retry_count = 0;
  198. return 0;
  199. }
  200. static int llc_station_ac_inc_retry_cnt_by_1(struct sk_buff *skb)
  201. {
  202. llc_main_station.retry_count++;
  203. return 0;
  204. }
  205. static int llc_station_ac_set_xid_r_cnt_0(struct sk_buff *skb)
  206. {
  207. llc_main_station.xid_r_count = 0;
  208. return 0;
  209. }
  210. static int llc_station_ac_inc_xid_r_cnt_by_1(struct sk_buff *skb)
  211. {
  212. llc_main_station.xid_r_count++;
  213. return 0;
  214. }
  215. static int llc_station_ac_send_null_dsap_xid_c(struct sk_buff *skb)
  216. {
  217. int rc = 1;
  218. struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev);
  219. if (!nskb)
  220. goto out;
  221. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, 0, LLC_PDU_CMD);
  222. llc_pdu_init_as_xid_cmd(nskb, LLC_XID_NULL_CLASS_2, 127);
  223. rc = llc_mac_hdr_init(nskb, llc_station_mac_sa, llc_station_mac_sa);
  224. if (unlikely(rc))
  225. goto free;
  226. llc_station_send_pdu(nskb);
  227. out:
  228. return rc;
  229. free:
  230. kfree_skb(skb);
  231. goto out;
  232. }
  233. static int llc_station_ac_send_xid_r(struct sk_buff *skb)
  234. {
  235. u8 mac_da[ETH_ALEN], dsap;
  236. int rc = 1;
  237. struct sk_buff* nskb = llc_alloc_frame(NULL, skb->dev);
  238. if (!nskb)
  239. goto out;
  240. rc = 0;
  241. llc_pdu_decode_sa(skb, mac_da);
  242. llc_pdu_decode_ssap(skb, &dsap);
  243. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  244. llc_pdu_init_as_xid_rsp(nskb, LLC_XID_NULL_CLASS_2, 127);
  245. rc = llc_mac_hdr_init(nskb, llc_station_mac_sa, mac_da);
  246. if (unlikely(rc))
  247. goto free;
  248. llc_station_send_pdu(nskb);
  249. out:
  250. return rc;
  251. free:
  252. kfree_skb(skb);
  253. goto out;
  254. }
  255. static int llc_station_ac_send_test_r(struct sk_buff *skb)
  256. {
  257. u8 mac_da[ETH_ALEN], dsap;
  258. int rc = 1;
  259. struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev);
  260. if (!nskb)
  261. goto out;
  262. rc = 0;
  263. llc_pdu_decode_sa(skb, mac_da);
  264. llc_pdu_decode_ssap(skb, &dsap);
  265. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  266. llc_pdu_init_as_test_rsp(nskb, skb);
  267. rc = llc_mac_hdr_init(nskb, llc_station_mac_sa, mac_da);
  268. if (unlikely(rc))
  269. goto free;
  270. llc_station_send_pdu(nskb);
  271. out:
  272. return rc;
  273. free:
  274. kfree_skb(skb);
  275. goto out;
  276. }
  277. static int llc_station_ac_report_status(struct sk_buff *skb)
  278. {
  279. return 0;
  280. }
  281. /* COMMON STATION STATE transitions */
  282. /* dummy last-transition indicator; common to all state transition groups
  283. * last entry for this state
  284. * all members are zeros, .bss zeroes it
  285. */
  286. static struct llc_station_state_trans llc_stat_state_trans_end;
  287. /* DOWN STATE transitions */
  288. /* state transition for LLC_STATION_EV_ENABLE_WITH_DUP_ADDR_CHECK event */
  289. static llc_station_action_t llc_stat_down_state_actions_1[] = {
  290. [0] = llc_station_ac_start_ack_timer,
  291. [1] = llc_station_ac_set_retry_cnt_0,
  292. [2] = llc_station_ac_set_xid_r_cnt_0,
  293. [3] = llc_station_ac_send_null_dsap_xid_c,
  294. [4] = NULL,
  295. };
  296. static struct llc_station_state_trans llc_stat_down_state_trans_1 = {
  297. .ev = llc_stat_ev_enable_with_dup_addr_check,
  298. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  299. .ev_actions = llc_stat_down_state_actions_1,
  300. };
  301. /* state transition for LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK event */
  302. static llc_station_action_t llc_stat_down_state_actions_2[] = {
  303. [0] = llc_station_ac_report_status, /* STATION UP */
  304. [1] = NULL,
  305. };
  306. static struct llc_station_state_trans llc_stat_down_state_trans_2 = {
  307. .ev = llc_stat_ev_enable_without_dup_addr_check,
  308. .next_state = LLC_STATION_STATE_UP,
  309. .ev_actions = llc_stat_down_state_actions_2,
  310. };
  311. /* array of pointers; one to each transition */
  312. static struct llc_station_state_trans *llc_stat_dwn_state_trans[] = {
  313. [0] = &llc_stat_down_state_trans_1,
  314. [1] = &llc_stat_down_state_trans_2,
  315. [2] = &llc_stat_state_trans_end,
  316. };
  317. /* UP STATE transitions */
  318. /* state transition for LLC_STATION_EV_DISABLE_REQ event */
  319. static llc_station_action_t llc_stat_up_state_actions_1[] = {
  320. [0] = llc_station_ac_report_status, /* STATION DOWN */
  321. [1] = NULL,
  322. };
  323. static struct llc_station_state_trans llc_stat_up_state_trans_1 = {
  324. .ev = llc_stat_ev_disable_req,
  325. .next_state = LLC_STATION_STATE_DOWN,
  326. .ev_actions = llc_stat_up_state_actions_1,
  327. };
  328. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */
  329. static llc_station_action_t llc_stat_up_state_actions_2[] = {
  330. [0] = llc_station_ac_send_xid_r,
  331. [1] = NULL,
  332. };
  333. static struct llc_station_state_trans llc_stat_up_state_trans_2 = {
  334. .ev = llc_stat_ev_rx_null_dsap_xid_c,
  335. .next_state = LLC_STATION_STATE_UP,
  336. .ev_actions = llc_stat_up_state_actions_2,
  337. };
  338. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_TEST_C event */
  339. static llc_station_action_t llc_stat_up_state_actions_3[] = {
  340. [0] = llc_station_ac_send_test_r,
  341. [1] = NULL,
  342. };
  343. static struct llc_station_state_trans llc_stat_up_state_trans_3 = {
  344. .ev = llc_stat_ev_rx_null_dsap_test_c,
  345. .next_state = LLC_STATION_STATE_UP,
  346. .ev_actions = llc_stat_up_state_actions_3,
  347. };
  348. /* array of pointers; one to each transition */
  349. static struct llc_station_state_trans *llc_stat_up_state_trans [] = {
  350. [0] = &llc_stat_up_state_trans_1,
  351. [1] = &llc_stat_up_state_trans_2,
  352. [2] = &llc_stat_up_state_trans_3,
  353. [3] = &llc_stat_state_trans_end,
  354. };
  355. /* DUP ADDR CHK STATE transitions */
  356. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_0_XID_R_XID_R_CNT_EQ
  357. * event
  358. */
  359. static llc_station_action_t llc_stat_dupaddr_state_actions_1[] = {
  360. [0] = llc_station_ac_inc_xid_r_cnt_by_1,
  361. [1] = NULL,
  362. };
  363. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_1 = {
  364. .ev = llc_stat_ev_rx_null_dsap_0_xid_r_xid_r_cnt_eq,
  365. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  366. .ev_actions = llc_stat_dupaddr_state_actions_1,
  367. };
  368. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_1_XID_R_XID_R_CNT_EQ
  369. * event
  370. */
  371. static llc_station_action_t llc_stat_dupaddr_state_actions_2[] = {
  372. [0] = llc_station_ac_report_status, /* DUPLICATE ADDRESS FOUND */
  373. [1] = NULL,
  374. };
  375. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_2 = {
  376. .ev = llc_stat_ev_rx_null_dsap_1_xid_r_xid_r_cnt_eq,
  377. .next_state = LLC_STATION_STATE_DOWN,
  378. .ev_actions = llc_stat_dupaddr_state_actions_2,
  379. };
  380. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */
  381. static llc_station_action_t llc_stat_dupaddr_state_actions_3[] = {
  382. [0] = llc_station_ac_send_xid_r,
  383. [1] = NULL,
  384. };
  385. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_3 = {
  386. .ev = llc_stat_ev_rx_null_dsap_xid_c,
  387. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  388. .ev_actions = llc_stat_dupaddr_state_actions_3,
  389. };
  390. /* state transition for LLC_STATION_EV_ACK_TMR_EXP_LT_RETRY_CNT_MAX_RETRY
  391. * event
  392. */
  393. static llc_station_action_t llc_stat_dupaddr_state_actions_4[] = {
  394. [0] = llc_station_ac_start_ack_timer,
  395. [1] = llc_station_ac_inc_retry_cnt_by_1,
  396. [2] = llc_station_ac_set_xid_r_cnt_0,
  397. [3] = llc_station_ac_send_null_dsap_xid_c,
  398. [4] = NULL,
  399. };
  400. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_4 = {
  401. .ev = llc_stat_ev_ack_tmr_exp_lt_retry_cnt_max_retry,
  402. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  403. .ev_actions = llc_stat_dupaddr_state_actions_4,
  404. };
  405. /* state transition for LLC_STATION_EV_ACK_TMR_EXP_EQ_RETRY_CNT_MAX_RETRY
  406. * event
  407. */
  408. static llc_station_action_t llc_stat_dupaddr_state_actions_5[] = {
  409. [0] = llc_station_ac_report_status, /* STATION UP */
  410. [1] = NULL,
  411. };
  412. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_5 = {
  413. .ev = llc_stat_ev_ack_tmr_exp_eq_retry_cnt_max_retry,
  414. .next_state = LLC_STATION_STATE_UP,
  415. .ev_actions = llc_stat_dupaddr_state_actions_5,
  416. };
  417. /* state transition for LLC_STATION_EV_DISABLE_REQ event */
  418. static llc_station_action_t llc_stat_dupaddr_state_actions_6[] = {
  419. [0] = llc_station_ac_report_status, /* STATION DOWN */
  420. [1] = NULL,
  421. };
  422. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_6 = {
  423. .ev = llc_stat_ev_disable_req,
  424. .next_state = LLC_STATION_STATE_DOWN,
  425. .ev_actions = llc_stat_dupaddr_state_actions_6,
  426. };
  427. /* array of pointers; one to each transition */
  428. static struct llc_station_state_trans *llc_stat_dupaddr_state_trans[] = {
  429. [0] = &llc_stat_dupaddr_state_trans_6, /* Request */
  430. [1] = &llc_stat_dupaddr_state_trans_4, /* Timer */
  431. [2] = &llc_stat_dupaddr_state_trans_5,
  432. [3] = &llc_stat_dupaddr_state_trans_1, /* Receive frame */
  433. [4] = &llc_stat_dupaddr_state_trans_2,
  434. [5] = &llc_stat_dupaddr_state_trans_3,
  435. [6] = &llc_stat_state_trans_end,
  436. };
  437. static struct llc_station_state
  438. llc_station_state_table[LLC_NBR_STATION_STATES] = {
  439. [LLC_STATION_STATE_DOWN - 1] = {
  440. .curr_state = LLC_STATION_STATE_DOWN,
  441. .transitions = llc_stat_dwn_state_trans,
  442. },
  443. [LLC_STATION_STATE_DUP_ADDR_CHK - 1] = {
  444. .curr_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  445. .transitions = llc_stat_dupaddr_state_trans,
  446. },
  447. [LLC_STATION_STATE_UP - 1] = {
  448. .curr_state = LLC_STATION_STATE_UP,
  449. .transitions = llc_stat_up_state_trans,
  450. },
  451. };
  452. /**
  453. * llc_exec_station_trans_actions - executes actions for transition
  454. * @trans: Address of the transition
  455. * @skb: Address of the event that caused the transition
  456. *
  457. * Executes actions of a transition of the station state machine. Returns
  458. * 0 if all actions complete successfully, nonzero otherwise.
  459. */
  460. static u16 llc_exec_station_trans_actions(struct llc_station_state_trans *trans,
  461. struct sk_buff *skb)
  462. {
  463. u16 rc = 0;
  464. llc_station_action_t *next_action = trans->ev_actions;
  465. for (; next_action && *next_action; next_action++)
  466. if ((*next_action)(skb))
  467. rc = 1;
  468. return rc;
  469. }
  470. /**
  471. * llc_find_station_trans - finds transition for this event
  472. * @skb: Address of the event
  473. *
  474. * Search thru events of the current state of the station until list
  475. * exhausted or it's obvious that the event is not valid for the current
  476. * state. Returns the address of the transition if cound, %NULL otherwise.
  477. */
  478. static struct llc_station_state_trans *
  479. llc_find_station_trans(struct sk_buff *skb)
  480. {
  481. int i = 0;
  482. struct llc_station_state_trans *rc = NULL;
  483. struct llc_station_state_trans **next_trans;
  484. struct llc_station_state *curr_state =
  485. &llc_station_state_table[llc_main_station.state - 1];
  486. for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
  487. if (!next_trans[i]->ev(skb)) {
  488. rc = next_trans[i];
  489. break;
  490. }
  491. return rc;
  492. }
  493. /**
  494. * llc_station_free_ev - frees an event
  495. * @skb: Address of the event
  496. *
  497. * Frees an event.
  498. */
  499. static void llc_station_free_ev(struct sk_buff *skb)
  500. {
  501. struct llc_station_state_ev *ev = llc_station_ev(skb);
  502. if (ev->type == LLC_STATION_EV_TYPE_PDU)
  503. kfree_skb(skb);
  504. }
  505. /**
  506. * llc_station_next_state - processes event and goes to the next state
  507. * @skb: Address of the event
  508. *
  509. * Processes an event, executes any transitions related to that event and
  510. * updates the state of the station.
  511. */
  512. static u16 llc_station_next_state(struct sk_buff *skb)
  513. {
  514. u16 rc = 1;
  515. struct llc_station_state_trans *trans;
  516. if (llc_main_station.state > LLC_NBR_STATION_STATES)
  517. goto out;
  518. trans = llc_find_station_trans(skb);
  519. if (trans) {
  520. /* got the state to which we next transition; perform the
  521. * actions associated with this transition before actually
  522. * transitioning to the next state
  523. */
  524. rc = llc_exec_station_trans_actions(trans, skb);
  525. if (!rc)
  526. /* transition station to next state if all actions
  527. * execute successfully; done; wait for next event
  528. */
  529. llc_main_station.state = trans->next_state;
  530. } else
  531. /* event not recognized in current state; re-queue it for
  532. * processing again at a later time; return failure
  533. */
  534. rc = 0;
  535. out:
  536. llc_station_free_ev(skb);
  537. return rc;
  538. }
  539. /**
  540. * llc_station_service_events - service events in the queue
  541. *
  542. * Get an event from the station event queue (if any); attempt to service
  543. * the event; if event serviced, get the next event (if any) on the event
  544. * queue; if event not service, re-queue the event on the event queue and
  545. * attempt to service the next event; when serviced all events in queue,
  546. * finished; if don't transition to different state, just service all
  547. * events once; if transition to new state, service all events again.
  548. * Caller must hold llc_main_station.ev_q.lock.
  549. */
  550. static void llc_station_service_events(void)
  551. {
  552. struct sk_buff *skb;
  553. while ((skb = skb_dequeue(&llc_main_station.ev_q.list)) != NULL)
  554. llc_station_next_state(skb);
  555. }
  556. /**
  557. * llc_station_state_process: queue event and try to process queue.
  558. * @skb: Address of the event
  559. *
  560. * Queues an event (on the station event queue) for handling by the
  561. * station state machine and attempts to process any queued-up events.
  562. */
  563. static void llc_station_state_process(struct sk_buff *skb)
  564. {
  565. spin_lock_bh(&llc_main_station.ev_q.lock);
  566. skb_queue_tail(&llc_main_station.ev_q.list, skb);
  567. llc_station_service_events();
  568. spin_unlock_bh(&llc_main_station.ev_q.lock);
  569. }
  570. static void llc_station_ack_tmr_cb(unsigned long timeout_data)
  571. {
  572. struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
  573. if (skb) {
  574. struct llc_station_state_ev *ev = llc_station_ev(skb);
  575. ev->type = LLC_STATION_EV_TYPE_ACK_TMR;
  576. llc_station_state_process(skb);
  577. }
  578. }
  579. /*
  580. * llc_station_rcv - send received pdu to the station state machine
  581. * @skb: received frame.
  582. *
  583. * Sends data unit to station state machine.
  584. */
  585. static void llc_station_rcv(struct sk_buff *skb)
  586. {
  587. struct llc_station_state_ev *ev = llc_station_ev(skb);
  588. ev->type = LLC_STATION_EV_TYPE_PDU;
  589. ev->reason = 0;
  590. llc_station_state_process(skb);
  591. }
  592. int __init llc_station_init(void)
  593. {
  594. u16 rc = -ENOBUFS;
  595. struct sk_buff *skb;
  596. struct llc_station_state_ev *ev;
  597. skb_queue_head_init(&llc_main_station.mac_pdu_q);
  598. skb_queue_head_init(&llc_main_station.ev_q.list);
  599. spin_lock_init(&llc_main_station.ev_q.lock);
  600. init_timer(&llc_main_station.ack_timer);
  601. llc_main_station.ack_timer.data = (unsigned long)&llc_main_station;
  602. llc_main_station.ack_timer.function = llc_station_ack_tmr_cb;
  603. llc_main_station.ack_timer.expires = jiffies +
  604. sysctl_llc_station_ack_timeout;
  605. skb = alloc_skb(0, GFP_ATOMIC);
  606. if (!skb)
  607. goto out;
  608. rc = 0;
  609. llc_set_station_handler(llc_station_rcv);
  610. ev = llc_station_ev(skb);
  611. memset(ev, 0, sizeof(*ev));
  612. llc_main_station.maximum_retry = 1;
  613. llc_main_station.state = LLC_STATION_STATE_DOWN;
  614. ev->type = LLC_STATION_EV_TYPE_SIMPLE;
  615. ev->prim_type = LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK;
  616. rc = llc_station_next_state(skb);
  617. out:
  618. return rc;
  619. }
  620. void __exit llc_station_exit(void)
  621. {
  622. llc_set_station_handler(NULL);
  623. }