Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | typedef int (*llc_station_ev_t)(struct sk_buff *skb); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | typedef int (*llc_station_action_t)(struct sk_buff *skb); |
| 31 | |
| 32 | /* Station component state table structure */ |
| 33 | struct llc_station_state_trans { |
| 34 | llc_station_ev_t ev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | llc_station_action_t *ev_actions; |
| 36 | }; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static int llc_stat_ev_rx_null_dsap_xid_c(struct sk_buff *skb) |
| 39 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); |
| 41 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame] | 42 | return LLC_PDU_IS_CMD(pdu) && /* command PDU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */ |
| 44 | LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_XID && |
| 45 | !pdu->dsap ? 0 : 1; /* NULL DSAP value */ |
| 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int llc_stat_ev_rx_null_dsap_test_c(struct sk_buff *skb) |
| 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); |
| 51 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame] | 52 | return LLC_PDU_IS_CMD(pdu) && /* command PDU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */ |
| 54 | LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_TEST && |
| 55 | !pdu->dsap ? 0 : 1; /* NULL DSAP */ |
| 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static int llc_station_ac_send_xid_r(struct sk_buff *skb) |
| 59 | { |
| 60 | u8 mac_da[ETH_ALEN], dsap; |
| 61 | int rc = 1; |
Joonwoo Park | f83f176 | 2008-03-31 21:02:47 -0700 | [diff] [blame] | 62 | struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, |
| 63 | sizeof(struct llc_xid_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | if (!nskb) |
| 66 | goto out; |
| 67 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | llc_pdu_decode_sa(skb, mac_da); |
| 69 | llc_pdu_decode_ssap(skb, &dsap); |
| 70 | llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP); |
| 71 | llc_pdu_init_as_xid_rsp(nskb, LLC_XID_NULL_CLASS_2, 127); |
Joonwoo Park | a5a0481 | 2008-03-28 16:28:36 -0700 | [diff] [blame] | 72 | rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da); |
Arnaldo Carvalho de Melo | 249ff1c | 2005-09-22 04:32:10 -0300 | [diff] [blame] | 73 | if (unlikely(rc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | goto free; |
Ben Hutchings | 5ecf9ee | 2012-09-15 17:11:32 +0000 | [diff] [blame^] | 75 | dev_queue_xmit(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | out: |
| 77 | return rc; |
| 78 | free: |
Sorin Dumitru | 91d27a8 | 2012-08-06 02:35:58 +0000 | [diff] [blame] | 79 | kfree_skb(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | static int llc_station_ac_send_test_r(struct sk_buff *skb) |
| 84 | { |
| 85 | u8 mac_da[ETH_ALEN], dsap; |
| 86 | int rc = 1; |
Joonwoo Park | f83f176 | 2008-03-31 21:02:47 -0700 | [diff] [blame] | 87 | u32 data_size; |
| 88 | struct sk_buff *nskb; |
| 89 | |
| 90 | /* The test request command is type U (llc_len = 3) */ |
| 91 | data_size = ntohs(eth_hdr(skb)->h_proto) - 3; |
| 92 | nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, data_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | if (!nskb) |
| 95 | goto out; |
| 96 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | llc_pdu_decode_sa(skb, mac_da); |
| 98 | llc_pdu_decode_ssap(skb, &dsap); |
| 99 | llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP); |
YOSHIFUJI Hideaki | d57b186 | 2007-02-09 23:25:01 +0900 | [diff] [blame] | 100 | llc_pdu_init_as_test_rsp(nskb, skb); |
Joonwoo Park | a5a0481 | 2008-03-28 16:28:36 -0700 | [diff] [blame] | 101 | rc = llc_mac_hdr_init(nskb, skb->dev->dev_addr, mac_da); |
Arnaldo Carvalho de Melo | 249ff1c | 2005-09-22 04:32:10 -0300 | [diff] [blame] | 102 | if (unlikely(rc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | goto free; |
Ben Hutchings | 5ecf9ee | 2012-09-15 17:11:32 +0000 | [diff] [blame^] | 104 | dev_queue_xmit(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | out: |
| 106 | return rc; |
| 107 | free: |
Sorin Dumitru | 91d27a8 | 2012-08-06 02:35:58 +0000 | [diff] [blame] | 108 | kfree_skb(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | goto out; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */ |
| 113 | static llc_station_action_t llc_stat_up_state_actions_2[] = { |
| 114 | [0] = llc_station_ac_send_xid_r, |
| 115 | [1] = NULL, |
| 116 | }; |
| 117 | |
| 118 | static struct llc_station_state_trans llc_stat_up_state_trans_2 = { |
| 119 | .ev = llc_stat_ev_rx_null_dsap_xid_c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .ev_actions = llc_stat_up_state_actions_2, |
| 121 | }; |
| 122 | |
| 123 | /* state transition for LLC_STATION_EV_RX_NULL_DSAP_TEST_C event */ |
| 124 | static llc_station_action_t llc_stat_up_state_actions_3[] = { |
| 125 | [0] = llc_station_ac_send_test_r, |
| 126 | [1] = NULL, |
| 127 | }; |
| 128 | |
| 129 | static struct llc_station_state_trans llc_stat_up_state_trans_3 = { |
| 130 | .ev = llc_stat_ev_rx_null_dsap_test_c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .ev_actions = llc_stat_up_state_actions_3, |
| 132 | }; |
| 133 | |
| 134 | /* array of pointers; one to each transition */ |
| 135 | static struct llc_station_state_trans *llc_stat_up_state_trans [] = { |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame] | 136 | &llc_stat_up_state_trans_2, |
| 137 | &llc_stat_up_state_trans_3, |
| 138 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * llc_exec_station_trans_actions - executes actions for transition |
| 143 | * @trans: Address of the transition |
| 144 | * @skb: Address of the event that caused the transition |
| 145 | * |
| 146 | * Executes actions of a transition of the station state machine. Returns |
| 147 | * 0 if all actions complete successfully, nonzero otherwise. |
| 148 | */ |
| 149 | static u16 llc_exec_station_trans_actions(struct llc_station_state_trans *trans, |
| 150 | struct sk_buff *skb) |
| 151 | { |
| 152 | u16 rc = 0; |
| 153 | llc_station_action_t *next_action = trans->ev_actions; |
| 154 | |
| 155 | for (; next_action && *next_action; next_action++) |
| 156 | if ((*next_action)(skb)) |
| 157 | rc = 1; |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * llc_find_station_trans - finds transition for this event |
| 163 | * @skb: Address of the event |
| 164 | * |
| 165 | * Search thru events of the current state of the station until list |
| 166 | * exhausted or it's obvious that the event is not valid for the current |
| 167 | * state. Returns the address of the transition if cound, %NULL otherwise. |
| 168 | */ |
| 169 | static struct llc_station_state_trans * |
| 170 | llc_find_station_trans(struct sk_buff *skb) |
| 171 | { |
| 172 | int i = 0; |
| 173 | struct llc_station_state_trans *rc = NULL; |
| 174 | struct llc_station_state_trans **next_trans; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame] | 176 | for (next_trans = llc_stat_up_state_trans; next_trans[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (!next_trans[i]->ev(skb)) { |
| 178 | rc = next_trans[i]; |
| 179 | break; |
| 180 | } |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * llc_station_rcv - send received pdu to the station state machine |
| 186 | * @skb: received frame. |
| 187 | * |
| 188 | * Sends data unit to station state machine. |
| 189 | */ |
| 190 | static void llc_station_rcv(struct sk_buff *skb) |
| 191 | { |
Ben Hutchings | 04d191c | 2012-09-15 17:11:25 +0000 | [diff] [blame] | 192 | struct llc_station_state_trans *trans; |
| 193 | |
| 194 | trans = llc_find_station_trans(skb); |
| 195 | if (trans) |
| 196 | llc_exec_station_trans_actions(trans, skb); |
| 197 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ben Hutchings | 6024935 | 2012-08-13 02:49:59 +0000 | [diff] [blame] | 200 | void __init llc_station_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Ben Hutchings | aadf31d | 2012-08-13 02:50:55 +0000 | [diff] [blame] | 202 | llc_set_station_handler(llc_station_rcv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ben Hutchings | f4f8720 | 2012-08-13 02:50:43 +0000 | [diff] [blame] | 205 | void llc_station_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
| 207 | llc_set_station_handler(NULL); |
| 208 | } |