llc_station.c 21 KB

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