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