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 | |
| 28 | /** |
| 29 | * struct llc_station - LLC station component |
| 30 | * |
| 31 | * SAP and connection resource manager, one per adapter. |
| 32 | * |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | struct llc_station { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct { |
| 40 | struct sk_buff_head list; |
| 41 | spinlock_t lock; |
| 42 | } ev_q; |
| 43 | struct sk_buff_head mac_pdu_q; |
| 44 | }; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | typedef int (*llc_station_ev_t)(struct sk_buff *skb); |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | typedef int (*llc_station_action_t)(struct sk_buff *skb); |
| 49 | |
| 50 | /* Station component state table structure */ |
| 51 | struct llc_station_state_trans { |
| 52 | llc_station_ev_t ev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | llc_station_action_t *ev_actions; |
| 54 | }; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct llc_station llc_main_station; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static int llc_stat_ev_rx_null_dsap_xid_c(struct sk_buff *skb) |
| 59 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); |
| 61 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 62 | return LLC_PDU_IS_CMD(pdu) && /* command PDU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */ |
| 64 | LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_XID && |
| 65 | !pdu->dsap ? 0 : 1; /* NULL DSAP value */ |
| 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int llc_stat_ev_rx_null_dsap_test_c(struct sk_buff *skb) |
| 69 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb); |
| 71 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 72 | return LLC_PDU_IS_CMD(pdu) && /* command PDU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | LLC_PDU_TYPE_IS_U(pdu) && /* U type PDU */ |
| 74 | LLC_U_PDU_CMD(pdu) == LLC_1_PDU_CMD_TEST && |
| 75 | !pdu->dsap ? 0 : 1; /* NULL DSAP */ |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /** |
| 79 | * llc_station_send_pdu - queues PDU to send |
| 80 | * @skb: Address of the PDU |
| 81 | * |
| 82 | * Queues a PDU to send to the MAC layer. |
| 83 | */ |
| 84 | static void llc_station_send_pdu(struct sk_buff *skb) |
| 85 | { |
| 86 | skb_queue_tail(&llc_main_station.mac_pdu_q, skb); |
| 87 | while ((skb = skb_dequeue(&llc_main_station.mac_pdu_q)) != NULL) |
| 88 | if (dev_queue_xmit(skb)) |
| 89 | break; |
| 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static int llc_station_ac_send_xid_r(struct sk_buff *skb) |
| 93 | { |
| 94 | u8 mac_da[ETH_ALEN], dsap; |
| 95 | int rc = 1; |
Joonwoo Park | f83f176 | 2008-03-31 21:02:47 -0700 | [diff] [blame] | 96 | struct sk_buff *nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, |
| 97 | sizeof(struct llc_xid_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | if (!nskb) |
| 100 | goto out; |
| 101 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | llc_pdu_decode_sa(skb, mac_da); |
| 103 | llc_pdu_decode_ssap(skb, &dsap); |
| 104 | llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, 0, dsap, LLC_PDU_RSP); |
| 105 | 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] | 106 | 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] | 107 | if (unlikely(rc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | goto free; |
| 109 | llc_station_send_pdu(nskb); |
| 110 | out: |
| 111 | return rc; |
| 112 | free: |
Sorin Dumitru | 91d27a8 | 2012-08-06 02:35:58 +0000 | [diff] [blame] | 113 | kfree_skb(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | static int llc_station_ac_send_test_r(struct sk_buff *skb) |
| 118 | { |
| 119 | u8 mac_da[ETH_ALEN], dsap; |
| 120 | int rc = 1; |
Joonwoo Park | f83f176 | 2008-03-31 21:02:47 -0700 | [diff] [blame] | 121 | u32 data_size; |
| 122 | struct sk_buff *nskb; |
| 123 | |
| 124 | /* The test request command is type U (llc_len = 3) */ |
| 125 | data_size = ntohs(eth_hdr(skb)->h_proto) - 3; |
| 126 | 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] | 127 | |
| 128 | if (!nskb) |
| 129 | goto out; |
| 130 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | llc_pdu_decode_sa(skb, mac_da); |
| 132 | llc_pdu_decode_ssap(skb, &dsap); |
| 133 | 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] | 134 | llc_pdu_init_as_test_rsp(nskb, skb); |
Joonwoo Park | a5a0481 | 2008-03-28 16:28:36 -0700 | [diff] [blame] | 135 | 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] | 136 | if (unlikely(rc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | goto free; |
| 138 | llc_station_send_pdu(nskb); |
| 139 | out: |
| 140 | return rc; |
| 141 | free: |
Sorin Dumitru | 91d27a8 | 2012-08-06 02:35:58 +0000 | [diff] [blame] | 142 | kfree_skb(nskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | goto out; |
| 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* state transition for LLC_STATION_EV_RX_NULL_DSAP_XID_C event */ |
| 147 | static llc_station_action_t llc_stat_up_state_actions_2[] = { |
| 148 | [0] = llc_station_ac_send_xid_r, |
| 149 | [1] = NULL, |
| 150 | }; |
| 151 | |
| 152 | static struct llc_station_state_trans llc_stat_up_state_trans_2 = { |
| 153 | .ev = llc_stat_ev_rx_null_dsap_xid_c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | .ev_actions = llc_stat_up_state_actions_2, |
| 155 | }; |
| 156 | |
| 157 | /* state transition for LLC_STATION_EV_RX_NULL_DSAP_TEST_C event */ |
| 158 | static llc_station_action_t llc_stat_up_state_actions_3[] = { |
| 159 | [0] = llc_station_ac_send_test_r, |
| 160 | [1] = NULL, |
| 161 | }; |
| 162 | |
| 163 | static struct llc_station_state_trans llc_stat_up_state_trans_3 = { |
| 164 | .ev = llc_stat_ev_rx_null_dsap_test_c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .ev_actions = llc_stat_up_state_actions_3, |
| 166 | }; |
| 167 | |
| 168 | /* array of pointers; one to each transition */ |
| 169 | static struct llc_station_state_trans *llc_stat_up_state_trans [] = { |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 170 | &llc_stat_up_state_trans_2, |
| 171 | &llc_stat_up_state_trans_3, |
| 172 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * llc_exec_station_trans_actions - executes actions for transition |
| 177 | * @trans: Address of the transition |
| 178 | * @skb: Address of the event that caused the transition |
| 179 | * |
| 180 | * Executes actions of a transition of the station state machine. Returns |
| 181 | * 0 if all actions complete successfully, nonzero otherwise. |
| 182 | */ |
| 183 | static u16 llc_exec_station_trans_actions(struct llc_station_state_trans *trans, |
| 184 | struct sk_buff *skb) |
| 185 | { |
| 186 | u16 rc = 0; |
| 187 | llc_station_action_t *next_action = trans->ev_actions; |
| 188 | |
| 189 | for (; next_action && *next_action; next_action++) |
| 190 | if ((*next_action)(skb)) |
| 191 | rc = 1; |
| 192 | return rc; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * llc_find_station_trans - finds transition for this event |
| 197 | * @skb: Address of the event |
| 198 | * |
| 199 | * Search thru events of the current state of the station until list |
| 200 | * exhausted or it's obvious that the event is not valid for the current |
| 201 | * state. Returns the address of the transition if cound, %NULL otherwise. |
| 202 | */ |
| 203 | static struct llc_station_state_trans * |
| 204 | llc_find_station_trans(struct sk_buff *skb) |
| 205 | { |
| 206 | int i = 0; |
| 207 | struct llc_station_state_trans *rc = NULL; |
| 208 | struct llc_station_state_trans **next_trans; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 210 | for (next_trans = llc_stat_up_state_trans; next_trans[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (!next_trans[i]->ev(skb)) { |
| 212 | rc = next_trans[i]; |
| 213 | break; |
| 214 | } |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * llc_station_free_ev - frees an event |
| 220 | * @skb: Address of the event |
| 221 | * |
| 222 | * Frees an event. |
| 223 | */ |
| 224 | static void llc_station_free_ev(struct sk_buff *skb) |
| 225 | { |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 226 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * llc_station_next_state - processes event and goes to the next state |
| 231 | * @skb: Address of the event |
| 232 | * |
| 233 | * Processes an event, executes any transitions related to that event and |
| 234 | * updates the state of the station. |
| 235 | */ |
| 236 | static u16 llc_station_next_state(struct sk_buff *skb) |
| 237 | { |
| 238 | u16 rc = 1; |
| 239 | struct llc_station_state_trans *trans; |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | trans = llc_find_station_trans(skb); |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 242 | if (trans) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | /* got the state to which we next transition; perform the |
| 244 | * actions associated with this transition before actually |
| 245 | * transitioning to the next state |
| 246 | */ |
| 247 | rc = llc_exec_station_trans_actions(trans, skb); |
Ben Hutchings | 025e363 | 2012-09-15 17:11:18 +0000 | [diff] [blame^] | 248 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* event not recognized in current state; re-queue it for |
| 250 | * processing again at a later time; return failure |
| 251 | */ |
| 252 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | llc_station_free_ev(skb); |
| 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * llc_station_service_events - service events in the queue |
| 259 | * |
| 260 | * Get an event from the station event queue (if any); attempt to service |
| 261 | * the event; if event serviced, get the next event (if any) on the event |
| 262 | * queue; if event not service, re-queue the event on the event queue and |
| 263 | * attempt to service the next event; when serviced all events in queue, |
| 264 | * finished; if don't transition to different state, just service all |
| 265 | * events once; if transition to new state, service all events again. |
| 266 | * Caller must hold llc_main_station.ev_q.lock. |
| 267 | */ |
| 268 | static void llc_station_service_events(void) |
| 269 | { |
| 270 | struct sk_buff *skb; |
| 271 | |
| 272 | while ((skb = skb_dequeue(&llc_main_station.ev_q.list)) != NULL) |
| 273 | llc_station_next_state(skb); |
| 274 | } |
| 275 | |
| 276 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 277 | * llc_station_state_process - queue event and try to process queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | * @skb: Address of the event |
| 279 | * |
| 280 | * Queues an event (on the station event queue) for handling by the |
| 281 | * station state machine and attempts to process any queued-up events. |
| 282 | */ |
| 283 | static void llc_station_state_process(struct sk_buff *skb) |
| 284 | { |
| 285 | spin_lock_bh(&llc_main_station.ev_q.lock); |
| 286 | skb_queue_tail(&llc_main_station.ev_q.list, skb); |
| 287 | llc_station_service_events(); |
| 288 | spin_unlock_bh(&llc_main_station.ev_q.lock); |
| 289 | } |
| 290 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 291 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * llc_station_rcv - send received pdu to the station state machine |
| 293 | * @skb: received frame. |
| 294 | * |
| 295 | * Sends data unit to station state machine. |
| 296 | */ |
| 297 | static void llc_station_rcv(struct sk_buff *skb) |
| 298 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | llc_station_state_process(skb); |
| 300 | } |
| 301 | |
Ben Hutchings | 6024935 | 2012-08-13 02:49:59 +0000 | [diff] [blame] | 302 | void __init llc_station_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | skb_queue_head_init(&llc_main_station.mac_pdu_q); |
| 305 | skb_queue_head_init(&llc_main_station.ev_q.list); |
| 306 | spin_lock_init(&llc_main_station.ev_q.lock); |
Ben Hutchings | aadf31d | 2012-08-13 02:50:55 +0000 | [diff] [blame] | 307 | llc_set_station_handler(llc_station_rcv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Ben Hutchings | f4f8720 | 2012-08-13 02:50:43 +0000 | [diff] [blame] | 310 | void llc_station_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | llc_set_station_handler(NULL); |
| 313 | } |