llc_station.c 21 KB

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