llc_station.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. * @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, LLC_PDU_TYPE_U,
  219. sizeof(struct llc_xid_info));
  220. if (!nskb)
  221. goto out;
  222. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, 0, LLC_PDU_CMD);
  223. llc_pdu_init_as_xid_cmd(nskb, LLC_XID_NULL_CLASS_2, 127);
  224. rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, skb->dev->dev_addr);
  225. if (unlikely(rc))
  226. goto free;
  227. llc_station_send_pdu(nskb);
  228. out:
  229. return rc;
  230. free:
  231. kfree_skb(skb);
  232. goto out;
  233. }
  234. static int llc_station_ac_send_xid_r(struct sk_buff *skb)
  235. {
  236. u8 mac_da[ETH_ALEN], dsap;
  237. int rc = 1;
  238. struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U,
  239. sizeof(struct llc_xid_info));
  240. if (!nskb)
  241. goto out;
  242. rc = 0;
  243. llc_pdu_decode_sa(skb, mac_da);
  244. llc_pdu_decode_ssap(skb, &dsap);
  245. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  246. llc_pdu_init_as_xid_rsp(nskb, LLC_XID_NULL_CLASS_2, 127);
  247. rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da);
  248. if (unlikely(rc))
  249. goto free;
  250. llc_station_send_pdu(nskb);
  251. out:
  252. return rc;
  253. free:
  254. kfree_skb(skb);
  255. goto out;
  256. }
  257. static int llc_station_ac_send_test_r(struct sk_buff *skb)
  258. {
  259. u8 mac_da[ETH_ALEN], dsap;
  260. int rc = 1;
  261. u32 data_size;
  262. struct sk_buff *nskb;
  263. /* The test request command is type U (llc_len = 3) */
  264. data_size = ntohs(eth_hdr(skb)->h_proto) - 3;
  265. nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, data_size);
  266. if (!nskb)
  267. goto out;
  268. rc = 0;
  269. llc_pdu_decode_sa(skb, mac_da);
  270. llc_pdu_decode_ssap(skb, &dsap);
  271. llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP);
  272. llc_pdu_init_as_test_rsp(nskb, skb);
  273. rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da);
  274. if (unlikely(rc))
  275. goto free;
  276. llc_station_send_pdu(nskb);
  277. out:
  278. return rc;
  279. free:
  280. kfree_skb(skb);
  281. goto out;
  282. }
  283. static int llc_station_ac_report_status(struct sk_buff *skb)
  284. {
  285. return 0;
  286. }
  287. /* COMMON STATION STATE transitions */
  288. /* dummy last-transition indicator; common to all state transition groups
  289. * last entry for this state
  290. * all members are zeros, .bss zeroes it
  291. */
  292. static struct llc_station_state_trans llc_stat_state_trans_end;
  293. /* DOWN STATE transitions */
  294. /* state transition for LLC_STATION_EV_ENABLE_WITH_DUP_ADDR_CHECK event */
  295. static llc_station_action_t llc_stat_down_state_actions_1[] = {
  296. [0] = llc_station_ac_start_ack_timer,
  297. [1] = llc_station_ac_set_retry_cnt_0,
  298. [2] = llc_station_ac_set_xid_r_cnt_0,
  299. [3] = llc_station_ac_send_null_dsap_xid_c,
  300. [4] = NULL,
  301. };
  302. static struct llc_station_state_trans llc_stat_down_state_trans_1 = {
  303. .ev = llc_stat_ev_enable_with_dup_addr_check,
  304. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  305. .ev_actions = llc_stat_down_state_actions_1,
  306. };
  307. /* state transition for LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK event */
  308. static llc_station_action_t llc_stat_down_state_actions_2[] = {
  309. [0] = llc_station_ac_report_status, /* STATION UP */
  310. [1] = NULL,
  311. };
  312. static struct llc_station_state_trans llc_stat_down_state_trans_2 = {
  313. .ev = llc_stat_ev_enable_without_dup_addr_check,
  314. .next_state = LLC_STATION_STATE_UP,
  315. .ev_actions = llc_stat_down_state_actions_2,
  316. };
  317. /* array of pointers; one to each transition */
  318. static struct llc_station_state_trans *llc_stat_dwn_state_trans[] = {
  319. [0] = &llc_stat_down_state_trans_1,
  320. [1] = &llc_stat_down_state_trans_2,
  321. [2] = &llc_stat_state_trans_end,
  322. };
  323. /* UP STATE transitions */
  324. /* state transition for LLC_STATION_EV_DISABLE_REQ event */
  325. static llc_station_action_t llc_stat_up_state_actions_1[] = {
  326. [0] = llc_station_ac_report_status, /* STATION DOWN */
  327. [1] = NULL,
  328. };
  329. static struct llc_station_state_trans llc_stat_up_state_trans_1 = {
  330. .ev = llc_stat_ev_disable_req,
  331. .next_state = LLC_STATION_STATE_DOWN,
  332. .ev_actions = llc_stat_up_state_actions_1,
  333. };
  334. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */
  335. static llc_station_action_t llc_stat_up_state_actions_2[] = {
  336. [0] = llc_station_ac_send_xid_r,
  337. [1] = NULL,
  338. };
  339. static struct llc_station_state_trans llc_stat_up_state_trans_2 = {
  340. .ev = llc_stat_ev_rx_null_dsap_xid_c,
  341. .next_state = LLC_STATION_STATE_UP,
  342. .ev_actions = llc_stat_up_state_actions_2,
  343. };
  344. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_TEST_C event */
  345. static llc_station_action_t llc_stat_up_state_actions_3[] = {
  346. [0] = llc_station_ac_send_test_r,
  347. [1] = NULL,
  348. };
  349. static struct llc_station_state_trans llc_stat_up_state_trans_3 = {
  350. .ev = llc_stat_ev_rx_null_dsap_test_c,
  351. .next_state = LLC_STATION_STATE_UP,
  352. .ev_actions = llc_stat_up_state_actions_3,
  353. };
  354. /* array of pointers; one to each transition */
  355. static struct llc_station_state_trans *llc_stat_up_state_trans [] = {
  356. [0] = &llc_stat_up_state_trans_1,
  357. [1] = &llc_stat_up_state_trans_2,
  358. [2] = &llc_stat_up_state_trans_3,
  359. [3] = &llc_stat_state_trans_end,
  360. };
  361. /* DUP ADDR CHK STATE transitions */
  362. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_0_XID_R_XID_R_CNT_EQ
  363. * event
  364. */
  365. static llc_station_action_t llc_stat_dupaddr_state_actions_1[] = {
  366. [0] = llc_station_ac_inc_xid_r_cnt_by_1,
  367. [1] = NULL,
  368. };
  369. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_1 = {
  370. .ev = llc_stat_ev_rx_null_dsap_0_xid_r_xid_r_cnt_eq,
  371. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  372. .ev_actions = llc_stat_dupaddr_state_actions_1,
  373. };
  374. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_1_XID_R_XID_R_CNT_EQ
  375. * event
  376. */
  377. static llc_station_action_t llc_stat_dupaddr_state_actions_2[] = {
  378. [0] = llc_station_ac_report_status, /* DUPLICATE ADDRESS FOUND */
  379. [1] = NULL,
  380. };
  381. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_2 = {
  382. .ev = llc_stat_ev_rx_null_dsap_1_xid_r_xid_r_cnt_eq,
  383. .next_state = LLC_STATION_STATE_DOWN,
  384. .ev_actions = llc_stat_dupaddr_state_actions_2,
  385. };
  386. /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */
  387. static llc_station_action_t llc_stat_dupaddr_state_actions_3[] = {
  388. [0] = llc_station_ac_send_xid_r,
  389. [1] = NULL,
  390. };
  391. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_3 = {
  392. .ev = llc_stat_ev_rx_null_dsap_xid_c,
  393. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  394. .ev_actions = llc_stat_dupaddr_state_actions_3,
  395. };
  396. /* state transition for LLC_STATION_EV_ACK_TMR_EXP_LT_RETRY_CNT_MAX_RETRY
  397. * event
  398. */
  399. static llc_station_action_t llc_stat_dupaddr_state_actions_4[] = {
  400. [0] = llc_station_ac_start_ack_timer,
  401. [1] = llc_station_ac_inc_retry_cnt_by_1,
  402. [2] = llc_station_ac_set_xid_r_cnt_0,
  403. [3] = llc_station_ac_send_null_dsap_xid_c,
  404. [4] = NULL,
  405. };
  406. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_4 = {
  407. .ev = llc_stat_ev_ack_tmr_exp_lt_retry_cnt_max_retry,
  408. .next_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  409. .ev_actions = llc_stat_dupaddr_state_actions_4,
  410. };
  411. /* state transition for LLC_STATION_EV_ACK_TMR_EXP_EQ_RETRY_CNT_MAX_RETRY
  412. * event
  413. */
  414. static llc_station_action_t llc_stat_dupaddr_state_actions_5[] = {
  415. [0] = llc_station_ac_report_status, /* STATION UP */
  416. [1] = NULL,
  417. };
  418. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_5 = {
  419. .ev = llc_stat_ev_ack_tmr_exp_eq_retry_cnt_max_retry,
  420. .next_state = LLC_STATION_STATE_UP,
  421. .ev_actions = llc_stat_dupaddr_state_actions_5,
  422. };
  423. /* state transition for LLC_STATION_EV_DISABLE_REQ event */
  424. static llc_station_action_t llc_stat_dupaddr_state_actions_6[] = {
  425. [0] = llc_station_ac_report_status, /* STATION DOWN */
  426. [1] = NULL,
  427. };
  428. static struct llc_station_state_trans llc_stat_dupaddr_state_trans_6 = {
  429. .ev = llc_stat_ev_disable_req,
  430. .next_state = LLC_STATION_STATE_DOWN,
  431. .ev_actions = llc_stat_dupaddr_state_actions_6,
  432. };
  433. /* array of pointers; one to each transition */
  434. static struct llc_station_state_trans *llc_stat_dupaddr_state_trans[] = {
  435. [0] = &llc_stat_dupaddr_state_trans_6, /* Request */
  436. [1] = &llc_stat_dupaddr_state_trans_4, /* Timer */
  437. [2] = &llc_stat_dupaddr_state_trans_5,
  438. [3] = &llc_stat_dupaddr_state_trans_1, /* Receive frame */
  439. [4] = &llc_stat_dupaddr_state_trans_2,
  440. [5] = &llc_stat_dupaddr_state_trans_3,
  441. [6] = &llc_stat_state_trans_end,
  442. };
  443. static struct llc_station_state
  444. llc_station_state_table[LLC_NBR_STATION_STATES] = {
  445. [LLC_STATION_STATE_DOWN - 1] = {
  446. .curr_state = LLC_STATION_STATE_DOWN,
  447. .transitions = llc_stat_dwn_state_trans,
  448. },
  449. [LLC_STATION_STATE_DUP_ADDR_CHK - 1] = {
  450. .curr_state = LLC_STATION_STATE_DUP_ADDR_CHK,
  451. .transitions = llc_stat_dupaddr_state_trans,
  452. },
  453. [LLC_STATION_STATE_UP - 1] = {
  454. .curr_state = LLC_STATION_STATE_UP,
  455. .transitions = llc_stat_up_state_trans,
  456. },
  457. };
  458. /**
  459. * llc_exec_station_trans_actions - executes actions for transition
  460. * @trans: Address of the transition
  461. * @skb: Address of the event that caused the transition
  462. *
  463. * Executes actions of a transition of the station state machine. Returns
  464. * 0 if all actions complete successfully, nonzero otherwise.
  465. */
  466. static u16 llc_exec_station_trans_actions(struct llc_station_state_trans *trans,
  467. struct sk_buff *skb)
  468. {
  469. u16 rc = 0;
  470. llc_station_action_t *next_action = trans->ev_actions;
  471. for (; next_action && *next_action; next_action++)
  472. if ((*next_action)(skb))
  473. rc = 1;
  474. return rc;
  475. }
  476. /**
  477. * llc_find_station_trans - finds transition for this event
  478. * @skb: Address of the event
  479. *
  480. * Search thru events of the current state of the station until list
  481. * exhausted or it's obvious that the event is not valid for the current
  482. * state. Returns the address of the transition if cound, %NULL otherwise.
  483. */
  484. static struct llc_station_state_trans *
  485. llc_find_station_trans(struct sk_buff *skb)
  486. {
  487. int i = 0;
  488. struct llc_station_state_trans *rc = NULL;
  489. struct llc_station_state_trans **next_trans;
  490. struct llc_station_state *curr_state =
  491. &llc_station_state_table[llc_main_station.state - 1];
  492. for (next_trans = curr_state->transitions; next_trans[i]->ev; i++)
  493. if (!next_trans[i]->ev(skb)) {
  494. rc = next_trans[i];
  495. break;
  496. }
  497. return rc;
  498. }
  499. /**
  500. * llc_station_free_ev - frees an event
  501. * @skb: Address of the event
  502. *
  503. * Frees an event.
  504. */
  505. static void llc_station_free_ev(struct sk_buff *skb)
  506. {
  507. struct llc_station_state_ev *ev = llc_station_ev(skb);
  508. if (ev->type == LLC_STATION_EV_TYPE_PDU)
  509. kfree_skb(skb);
  510. }
  511. /**
  512. * llc_station_next_state - processes event and goes to the next state
  513. * @skb: Address of the event
  514. *
  515. * Processes an event, executes any transitions related to that event and
  516. * updates the state of the station.
  517. */
  518. static u16 llc_station_next_state(struct sk_buff *skb)
  519. {
  520. u16 rc = 1;
  521. struct llc_station_state_trans *trans;
  522. if (llc_main_station.state > LLC_NBR_STATION_STATES)
  523. goto out;
  524. trans = llc_find_station_trans(skb);
  525. if (trans) {
  526. /* got the state to which we next transition; perform the
  527. * actions associated with this transition before actually
  528. * transitioning to the next state
  529. */
  530. rc = llc_exec_station_trans_actions(trans, skb);
  531. if (!rc)
  532. /* transition station to next state if all actions
  533. * execute successfully; done; wait for next event
  534. */
  535. llc_main_station.state = trans->next_state;
  536. } else
  537. /* event not recognized in current state; re-queue it for
  538. * processing again at a later time; return failure
  539. */
  540. rc = 0;
  541. out:
  542. llc_station_free_ev(skb);
  543. return rc;
  544. }
  545. /**
  546. * llc_station_service_events - service events in the queue
  547. *
  548. * Get an event from the station event queue (if any); attempt to service
  549. * the event; if event serviced, get the next event (if any) on the event
  550. * queue; if event not service, re-queue the event on the event queue and
  551. * attempt to service the next event; when serviced all events in queue,
  552. * finished; if don't transition to different state, just service all
  553. * events once; if transition to new state, service all events again.
  554. * Caller must hold llc_main_station.ev_q.lock.
  555. */
  556. static void llc_station_service_events(void)
  557. {
  558. struct sk_buff *skb;
  559. while ((skb = skb_dequeue(&llc_main_station.ev_q.list)) != NULL)
  560. llc_station_next_state(skb);
  561. }
  562. /**
  563. * llc_station_state_process: queue event and try to process queue.
  564. * @skb: Address of the event
  565. *
  566. * Queues an event (on the station event queue) for handling by the
  567. * station state machine and attempts to process any queued-up events.
  568. */
  569. static void llc_station_state_process(struct sk_buff *skb)
  570. {
  571. spin_lock_bh(&llc_main_station.ev_q.lock);
  572. skb_queue_tail(&llc_main_station.ev_q.list, skb);
  573. llc_station_service_events();
  574. spin_unlock_bh(&llc_main_station.ev_q.lock);
  575. }
  576. static void llc_station_ack_tmr_cb(unsigned long timeout_data)
  577. {
  578. struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
  579. if (skb) {
  580. struct llc_station_state_ev *ev = llc_station_ev(skb);
  581. ev->type = LLC_STATION_EV_TYPE_ACK_TMR;
  582. llc_station_state_process(skb);
  583. }
  584. }
  585. /*
  586. * llc_station_rcv - send received pdu to the station state machine
  587. * @skb: received frame.
  588. *
  589. * Sends data unit to station state machine.
  590. */
  591. static void llc_station_rcv(struct sk_buff *skb)
  592. {
  593. struct llc_station_state_ev *ev = llc_station_ev(skb);
  594. ev->type = LLC_STATION_EV_TYPE_PDU;
  595. ev->reason = 0;
  596. llc_station_state_process(skb);
  597. }
  598. int __init llc_station_init(void)
  599. {
  600. int rc = -ENOBUFS;
  601. struct sk_buff *skb;
  602. struct llc_station_state_ev *ev;
  603. skb_queue_head_init(&llc_main_station.mac_pdu_q);
  604. skb_queue_head_init(&llc_main_station.ev_q.list);
  605. spin_lock_init(&llc_main_station.ev_q.lock);
  606. setup_timer(&llc_main_station.ack_timer, llc_station_ack_tmr_cb,
  607. (unsigned long)&llc_main_station);
  608. llc_main_station.ack_timer.expires = jiffies +
  609. sysctl_llc_station_ack_timeout;
  610. skb = alloc_skb(0, GFP_ATOMIC);
  611. if (!skb)
  612. goto out;
  613. rc = 0;
  614. llc_set_station_handler(llc_station_rcv);
  615. ev = llc_station_ev(skb);
  616. memset(ev, 0, sizeof(*ev));
  617. llc_main_station.maximum_retry = 1;
  618. llc_main_station.state = LLC_STATION_STATE_DOWN;
  619. ev->type = LLC_STATION_EV_TYPE_SIMPLE;
  620. ev->prim_type = LLC_STATION_EV_ENABLE_WITHOUT_DUP_ADDR_CHECK;
  621. rc = llc_station_next_state(skb);
  622. out:
  623. return rc;
  624. }
  625. void __exit llc_station_exit(void)
  626. {
  627. llc_set_station_handler(NULL);
  628. }