blob: 3477624a4906ec3fc65957a1eb1567f0864153f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * llc_conn.c - Driver routines for connection component.
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
15#include <linux/init.h>
16#include <net/llc_sap.h>
17#include <net/llc_conn.h>
18#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070019#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/llc_c_ev.h>
21#include <net/llc_c_ac.h>
22#include <net/llc_c_st.h>
23#include <net/llc_pdu.h>
24
25#if 0
26#define dprintk(args...) printk(KERN_DEBUG args)
27#else
28#define dprintk(args...)
29#endif
30
31static int llc_find_offset(int state, int ev_type);
32static void llc_conn_send_pdus(struct sock *sk);
33static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
34static int llc_exec_conn_trans_actions(struct sock *sk,
35 struct llc_conn_state_trans *trans,
36 struct sk_buff *ev);
37static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
38 struct sk_buff *skb);
39
40/* Offset table on connection states transition diagram */
41static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
42
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -030043int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
44int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
45int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
46int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * llc_conn_state_process - sends event to connection state machine
50 * @sk: connection
51 * @skb: occurred event
52 *
53 * Sends an event to connection state machine. After processing event
54 * (executing it's actions and changing state), upper layer will be
55 * indicated or confirmed, if needed. Returns 0 for success, 1 for
56 * failure. The socket lock has to be held before calling this function.
57 */
58int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
59{
60 int rc;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030061 struct llc_sock *llc = llc_sk(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
63
64 /*
65 * We have to hold the skb, because llc_conn_service will kfree it in
66 * the sending path and we need to look at the skb->cb, where we encode
67 * llc_conn_state_ev.
68 */
69 skb_get(skb);
70 ev->ind_prim = ev->cfm_prim = 0;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -030071 /*
72 * Send event to state machine
73 */
74 rc = llc_conn_service(skb->sk, skb);
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030075 if (unlikely(rc != 0)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -080076 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 goto out_kfree_skb;
78 }
79
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030080 if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* indicate or confirm not required */
David S. Miller8728b832005-08-09 19:25:21 -070082 /* XXX this is not very pretty, perhaps we should store
83 * XXX indicate/confirm-needed state in the llc_conn_state_ev
84 * XXX control block of the SKB instead? -DaveM
85 */
86 if (!skb->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto out_kfree_skb;
88 goto out_skb_put;
89 }
90
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -030091 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 skb_get(skb);
93
94 switch (ev->ind_prim) {
95 case LLC_DATA_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -030096 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
97 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /*
99 * shouldn't happen
100 */
101 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800102 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 kfree_skb(skb);
104 }
105 break;
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300106 case LLC_CONN_PRIM:
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300107 /*
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300108 * Can't be sock_queue_rcv_skb, because we have to leave the
109 * skb->sk pointing to the newly created struct sock in
110 * llc_conn_handler. -acme
Arnaldo Carvalho de Melo04e42232005-09-22 04:40:59 -0300111 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300112 skb_queue_tail(&sk->sk_receive_queue, skb);
113 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115 case LLC_DISC_PRIM:
116 sock_hold(sk);
117 if (sk->sk_type == SOCK_STREAM &&
118 sk->sk_state == TCP_ESTABLISHED) {
119 sk->sk_shutdown = SHUTDOWN_MASK;
120 sk->sk_socket->state = SS_UNCONNECTED;
121 sk->sk_state = TCP_CLOSE;
122 if (!sock_flag(sk, SOCK_DEAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sock_set_flag(sk, SOCK_DEAD);
Arnaldo Carvalho de Melo8420e1b2005-09-22 08:29:08 -0300124 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
127 kfree_skb(skb);
128 sock_put(sk);
129 break;
130 case LLC_RESET_PRIM:
131 /*
132 * FIXME:
133 * RESET is not being notified to upper layers for now
134 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800135 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 kfree_skb(skb);
137 break;
138 default:
139 if (ev->ind_prim) {
140 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800141 __func__, ev->ind_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 kfree_skb(skb);
143 }
144 /* No indication */
145 break;
146 }
147
148 switch (ev->cfm_prim) {
149 case LLC_DATA_PRIM:
150 if (!llc_data_accept_state(llc->state))
151 sk->sk_write_space(sk);
152 else
153 rc = llc->failed_data_req = 1;
154 break;
155 case LLC_CONN_PRIM:
156 if (sk->sk_type == SOCK_STREAM &&
157 sk->sk_state == TCP_SYN_SENT) {
158 if (ev->status) {
159 sk->sk_socket->state = SS_UNCONNECTED;
160 sk->sk_state = TCP_CLOSE;
161 } else {
162 sk->sk_socket->state = SS_CONNECTED;
163 sk->sk_state = TCP_ESTABLISHED;
164 }
165 sk->sk_state_change(sk);
166 }
167 break;
168 case LLC_DISC_PRIM:
169 sock_hold(sk);
170 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
171 sk->sk_socket->state = SS_UNCONNECTED;
172 sk->sk_state = TCP_CLOSE;
173 sk->sk_state_change(sk);
174 }
175 sock_put(sk);
176 break;
177 case LLC_RESET_PRIM:
178 /*
179 * FIXME:
180 * RESET is not being notified to upper layers for now
181 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800182 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 default:
185 if (ev->cfm_prim) {
186 printk(KERN_INFO "%s: received unknown %d prim!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800187 __func__, ev->cfm_prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189 }
190 goto out_skb_put; /* No confirmation */
191 }
192out_kfree_skb:
193 kfree_skb(skb);
194out_skb_put:
195 kfree_skb(skb);
196 return rc;
197}
198
199void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
200{
201 /* queue PDU to send to MAC layer */
202 skb_queue_tail(&sk->sk_write_queue, skb);
203 llc_conn_send_pdus(sk);
204}
205
206/**
207 * llc_conn_rtn_pdu - sends received data pdu to upper layer
208 * @sk: Active connection
209 * @skb: Received data frame
210 *
211 * Sends received data pdu to upper layer (by using indicate function).
212 * Prepares service parameters (prim and prim_data). calling indication
213 * function will be done in llc_conn_state_process.
214 */
215void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
216{
217 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
218
219 ev->ind_prim = LLC_DATA_PRIM;
220}
221
222/**
223 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
224 * @sk: active connection
225 * @nr: NR
226 * @first_p_bit: p_bit value of first pdu
227 *
228 * Resend all unacknowledged I PDUs, starting with the NR; send first as
229 * command PDU with P bit equal first_p_bit; if more than one send
230 * subsequent as command PDUs with P bit equal zero (0).
231 */
232void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
233{
234 struct sk_buff *skb;
235 struct llc_pdu_sn *pdu;
236 u16 nbr_unack_pdus;
237 struct llc_sock *llc;
238 u8 howmany_resend = 0;
239
240 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
241 if (!nbr_unack_pdus)
242 goto out;
243 /*
244 * Process unack PDUs only if unack queue is not empty; remove
245 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
246 */
247 llc = llc_sk(sk);
248
249 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
250 pdu = llc_pdu_sn_hdr(skb);
251 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
252 llc_pdu_set_pf_bit(skb, first_p_bit);
253 skb_queue_tail(&sk->sk_write_queue, skb);
254 first_p_bit = 0;
255 llc->vS = LLC_I_GET_NS(pdu);
256 howmany_resend++;
257 }
258 if (howmany_resend > 0)
259 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
260 /* any PDUs to re-send are queued up; start sending to MAC */
261 llc_conn_send_pdus(sk);
262out:;
263}
264
265/**
266 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
267 * @sk: active connection.
268 * @nr: NR
269 * @first_f_bit: f_bit value of first pdu.
270 *
271 * Resend all unacknowledged I PDUs, starting with the NR; send first as
272 * response PDU with F bit equal first_f_bit; if more than one send
273 * subsequent as response PDUs with F bit equal zero (0).
274 */
275void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
276{
277 struct sk_buff *skb;
278 u16 nbr_unack_pdus;
279 struct llc_sock *llc = llc_sk(sk);
280 u8 howmany_resend = 0;
281
282 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
283 if (!nbr_unack_pdus)
284 goto out;
285 /*
286 * Process unack PDUs only if unack queue is not empty; remove
287 * appropriate PDUs, fix them up, and put them on mac_pdu_q
288 */
289 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
290 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
291
292 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
293 llc_pdu_set_pf_bit(skb, first_f_bit);
294 skb_queue_tail(&sk->sk_write_queue, skb);
295 first_f_bit = 0;
296 llc->vS = LLC_I_GET_NS(pdu);
297 howmany_resend++;
298 }
299 if (howmany_resend > 0)
300 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
301 /* any PDUs to re-send are queued up; start sending to MAC */
302 llc_conn_send_pdus(sk);
303out:;
304}
305
306/**
307 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
308 * @sk: active connection
309 * nr: NR
310 * how_many_unacked: size of pdu_unack_q after removing acked pdus
311 *
312 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
313 * the number of pdus that removed from queue.
314 */
315int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
316{
317 int pdu_pos, i;
318 struct sk_buff *skb;
319 struct llc_pdu_sn *pdu;
320 int nbr_acked = 0;
321 struct llc_sock *llc = llc_sk(sk);
322 int q_len = skb_queue_len(&llc->pdu_unack_q);
323
324 if (!q_len)
325 goto out;
326 skb = skb_peek(&llc->pdu_unack_q);
327 pdu = llc_pdu_sn_hdr(skb);
328
329 /* finding position of last acked pdu in queue */
330 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
331 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
332
333 for (i = 0; i < pdu_pos && i < q_len; i++) {
334 skb = skb_dequeue(&llc->pdu_unack_q);
Wei Yongjunc3431ea2009-02-25 00:42:22 +0000335 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 nbr_acked++;
337 }
338out:
339 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
340 return nbr_acked;
341}
342
343/**
344 * llc_conn_send_pdus - Sends queued PDUs
345 * @sk: active connection
346 *
347 * Sends queued pdus to MAC layer for transmission.
348 */
349static void llc_conn_send_pdus(struct sock *sk)
350{
351 struct sk_buff *skb;
352
353 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
354 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
355
356 if (LLC_PDU_TYPE_IS_I(pdu) &&
357 !(skb->dev->flags & IFF_LOOPBACK)) {
358 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
359
360 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
361 if (!skb2)
362 break;
363 skb = skb2;
364 }
365 dev_queue_xmit(skb);
366 }
367}
368
369/**
370 * llc_conn_service - finds transition and changes state of connection
371 * @sk: connection
372 * @skb: happened event
373 *
374 * This function finds transition that matches with happened event, then
375 * executes related actions and finally changes state of connection.
376 * Returns 0 for success, 1 for failure.
377 */
378static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
379{
380 int rc = 1;
381 struct llc_sock *llc = llc_sk(sk);
382 struct llc_conn_state_trans *trans;
383
384 if (llc->state > NBR_CONN_STATES)
385 goto out;
386 rc = 0;
387 trans = llc_qualify_conn_ev(sk, skb);
388 if (trans) {
389 rc = llc_exec_conn_trans_actions(sk, trans, skb);
390 if (!rc && trans->next_state != NO_STATE_CHANGE) {
391 llc->state = trans->next_state;
392 if (!llc_data_accept_state(llc->state))
393 sk->sk_state_change(sk);
394 }
395 }
396out:
397 return rc;
398}
399
400/**
401 * llc_qualify_conn_ev - finds transition for event
402 * @sk: connection
403 * @skb: happened event
404 *
405 * This function finds transition that matches with happened event.
406 * Returns pointer to found transition on success, %NULL otherwise.
407 */
408static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
409 struct sk_buff *skb)
410{
411 struct llc_conn_state_trans **next_trans;
412 llc_conn_ev_qfyr_t *next_qualifier;
413 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
414 struct llc_sock *llc = llc_sk(sk);
415 struct llc_conn_state *curr_state =
416 &llc_conn_state_table[llc->state - 1];
417
418 /* search thru events for this state until
419 * list exhausted or until no more
420 */
421 for (next_trans = curr_state->transitions +
422 llc_find_offset(llc->state - 1, ev->type);
423 (*next_trans)->ev; next_trans++) {
424 if (!((*next_trans)->ev)(sk, skb)) {
425 /* got POSSIBLE event match; the event may require
426 * qualification based on the values of a number of
427 * state flags; if all qualifications are met (i.e.,
428 * if all qualifying functions return success, or 0,
429 * then this is THE event we're looking for
430 */
431 for (next_qualifier = (*next_trans)->ev_qualifiers;
432 next_qualifier && *next_qualifier &&
433 !(*next_qualifier)(sk, skb); next_qualifier++)
434 /* nothing */;
435 if (!next_qualifier || !*next_qualifier)
436 /* all qualifiers executed successfully; this is
437 * our transition; return it so we can perform
438 * the associated actions & change the state
439 */
440 return *next_trans;
441 }
442 }
443 return NULL;
444}
445
446/**
447 * llc_exec_conn_trans_actions - executes related actions
448 * @sk: connection
449 * @trans: transition that it's actions must be performed
450 * @skb: event
451 *
452 * Executes actions that is related to happened event. Returns 0 for
453 * success, 1 to indicate failure of at least one action.
454 */
455static int llc_exec_conn_trans_actions(struct sock *sk,
456 struct llc_conn_state_trans *trans,
457 struct sk_buff *skb)
458{
459 int rc = 0;
460 llc_conn_action_t *next_action;
461
462 for (next_action = trans->ev_actions;
463 next_action && *next_action; next_action++) {
464 int rc2 = (*next_action)(sk, skb);
465
466 if (rc2 == 2) {
467 rc = rc2;
468 break;
469 } else if (rc2)
470 rc = 1;
471 }
472 return rc;
473}
474
475/**
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300476 * __llc_lookup_established - Finds connection for the remote/local sap/mac
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * @sap: SAP
478 * @daddr: address of remote LLC (MAC + SAP)
479 * @laddr: address of local LLC (MAC + SAP)
480 *
481 * Search connection list of the SAP and finds connection using the remote
482 * mac, remote sap, local mac, and local sap. Returns pointer for
483 * connection found, %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300484 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300486static struct sock *__llc_lookup_established(struct llc_sap *sap,
487 struct llc_addr *daddr,
488 struct llc_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct sock *rc;
491 struct hlist_node *node;
492
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300493 read_lock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sk_for_each(rc, node, &sap->sk_list.list) {
495 struct llc_sock *llc = llc_sk(rc);
496
497 if (llc->laddr.lsap == laddr->lsap &&
498 llc->daddr.lsap == daddr->lsap &&
499 llc_mac_match(llc->laddr.mac, laddr->mac) &&
500 llc_mac_match(llc->daddr.mac, daddr->mac)) {
501 sock_hold(rc);
502 goto found;
503 }
504 }
505 rc = NULL;
506found:
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300507 read_unlock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return rc;
509}
510
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300511struct sock *llc_lookup_established(struct llc_sap *sap,
512 struct llc_addr *daddr,
513 struct llc_addr *laddr)
514{
515 struct sock *sk;
516
517 local_bh_disable();
518 sk = __llc_lookup_established(sap, daddr, laddr);
519 local_bh_enable();
520 return sk;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/**
524 * llc_lookup_listener - Finds listener for local MAC + SAP
525 * @sap: SAP
526 * @laddr: address of local LLC (MAC + SAP)
527 *
528 * Search connection list of the SAP and finds connection listening on
529 * local mac, and local sap. Returns pointer for parent socket found,
530 * %NULL otherwise.
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300531 * Caller has to make sure local_bh is disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533static struct sock *llc_lookup_listener(struct llc_sap *sap,
534 struct llc_addr *laddr)
535{
536 struct sock *rc;
537 struct hlist_node *node;
538
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300539 read_lock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 sk_for_each(rc, node, &sap->sk_list.list) {
541 struct llc_sock *llc = llc_sk(rc);
542
543 if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
544 llc->laddr.lsap == laddr->lsap &&
545 (llc_mac_match(llc->laddr.mac, laddr->mac) ||
546 llc_mac_null(llc->laddr.mac))) {
547 sock_hold(rc);
548 goto found;
549 }
550 }
551 rc = NULL;
552found:
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300553 read_unlock(&sap->sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return rc;
555}
556
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300557static struct sock *__llc_lookup(struct llc_sap *sap,
558 struct llc_addr *daddr,
559 struct llc_addr *laddr)
560{
561 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
562
563 return sk ? : llc_lookup_listener(sap, laddr);
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/**
567 * llc_data_accept_state - designates if in this state data can be sent.
568 * @state: state of connection.
569 *
570 * Returns 0 if data can be sent, 1 otherwise.
571 */
572u8 llc_data_accept_state(u8 state)
573{
574 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
575 state != LLC_CONN_STATE_REJ;
576}
577
578/**
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300579 * llc_find_next_offset - finds offset for next category of transitions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * @state: state table.
581 * @offset: start offset.
582 *
583 * Finds offset of next category of transitions in transition table.
584 * Returns the start index of next category.
585 */
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300586static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 u16 cnt = 0;
589 struct llc_conn_state_trans **next_trans;
590
591 for (next_trans = state->transitions + offset;
592 (*next_trans)->ev; next_trans++)
593 ++cnt;
594 return cnt;
595}
596
597/**
598 * llc_build_offset_table - builds offset table of connection
599 *
600 * Fills offset table of connection state transition table
601 * (llc_offset_table).
602 */
603void __init llc_build_offset_table(void)
604{
605 struct llc_conn_state *curr_state;
606 int state, ev_type, next_offset;
607
608 for (state = 0; state < NBR_CONN_STATES; state++) {
609 curr_state = &llc_conn_state_table[state];
610 next_offset = 0;
611 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
612 llc_offset_table[state][ev_type] = next_offset;
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300613 next_offset += llc_find_next_offset(curr_state,
614 next_offset) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 }
617}
618
619/**
620 * llc_find_offset - finds start offset of category of transitions
621 * @state: state of connection
622 * @ev_type: type of happened event
623 *
624 * Finds start offset of desired category of transitions. Returns the
625 * desired start offset.
626 */
627static int llc_find_offset(int state, int ev_type)
628{
629 int rc = 0;
630 /* at this stage, llc_offset_table[..][2] is not important. it is for
631 * init_pf_cycle and I don't know what is it.
632 */
633 switch (ev_type) {
634 case LLC_CONN_EV_TYPE_PRIM:
635 rc = llc_offset_table[state][0]; break;
636 case LLC_CONN_EV_TYPE_PDU:
637 rc = llc_offset_table[state][4]; break;
638 case LLC_CONN_EV_TYPE_SIMPLE:
639 rc = llc_offset_table[state][1]; break;
640 case LLC_CONN_EV_TYPE_P_TMR:
641 case LLC_CONN_EV_TYPE_ACK_TMR:
642 case LLC_CONN_EV_TYPE_REJ_TMR:
643 case LLC_CONN_EV_TYPE_BUSY_TMR:
644 rc = llc_offset_table[state][3]; break;
645 }
646 return rc;
647}
648
649/**
650 * llc_sap_add_socket - adds a socket to a SAP
651 * @sap: SAP
652 * @sk: socket
653 *
654 * This function adds a socket to sk_list of a SAP.
655 */
656void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
657{
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300658 llc_sap_hold(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 write_lock_bh(&sap->sk_list.lock);
660 llc_sk(sk)->sap = sap;
661 sk_add_node(sk, &sap->sk_list.list);
662 write_unlock_bh(&sap->sk_list.lock);
663}
664
665/**
666 * llc_sap_remove_socket - removes a socket from SAP
667 * @sap: SAP
668 * @sk: socket
669 *
670 * This function removes a connection from sk_list.list of a SAP if
671 * the connection was in this list.
672 */
673void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
674{
675 write_lock_bh(&sap->sk_list.lock);
676 sk_del_node_init(sk);
677 write_unlock_bh(&sap->sk_list.lock);
Arnaldo Carvalho de Melo6e2144b2005-09-22 04:43:05 -0300678 llc_sap_put(sap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/**
682 * llc_conn_rcv - sends received pdus to the connection state machine
683 * @sk: current connection structure.
684 * @skb: received frame.
685 *
686 * Sends received pdus to the connection state machine.
687 */
688static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
689{
690 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ev->type = LLC_CONN_EV_TYPE_PDU;
693 ev->reason = 0;
694 return llc_conn_state_process(sk, skb);
695}
696
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300697static struct sock *llc_create_incoming_sock(struct sock *sk,
698 struct net_device *dev,
699 struct llc_addr *saddr,
700 struct llc_addr *daddr)
701{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900702 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300703 sk->sk_prot);
704 struct llc_sock *newllc, *llc = llc_sk(sk);
705
706 if (!newsk)
707 goto out;
708 newllc = llc_sk(newsk);
709 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
710 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
711 newllc->dev = dev;
712 dev_hold(dev);
713 llc_sap_add_socket(llc->sap, newsk);
714 llc_sap_hold(llc->sap);
715out:
716 return newsk;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
720{
721 struct llc_addr saddr, daddr;
722 struct sock *sk;
723
724 llc_pdu_decode_sa(skb, saddr.mac);
725 llc_pdu_decode_ssap(skb, &saddr.lsap);
726 llc_pdu_decode_da(skb, daddr.mac);
727 llc_pdu_decode_dsap(skb, &daddr.lsap);
728
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300729 sk = __llc_lookup(sap, &saddr, &daddr);
730 if (!sk)
731 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 bh_lock_sock(sk);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300734 /*
735 * This has to be done here and not at the upper layer ->accept
736 * method because of the way the PROCOM state machine works:
737 * it needs to set several state variables (see, for instance,
738 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
739 * the originator of the new connection, and this state has to be
740 * in the newly created struct sock private area. -acme
741 */
742 if (unlikely(sk->sk_state == TCP_LISTEN)) {
743 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
744 &saddr, &daddr);
745 if (!newsk)
746 goto drop_unlock;
747 skb_set_owner_r(skb, newsk);
748 } else {
749 /*
750 * Can't be skb_set_owner_r, this will be done at the
751 * llc_conn_state_process function, later on, when we will use
752 * skb_queue_rcv_skb to send it to upper layers, this is
753 * another trick required to cope with how the PROCOM state
754 * machine works. -acme
755 */
756 skb->sk = sk;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!sock_owned_by_user(sk))
759 llc_conn_rcv(sk, skb);
760 else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800761 dprintk("%s: adding to backlog...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 llc_set_backlog_type(skb, LLC_PACKET);
763 sk_add_backlog(sk, skb);
764 }
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300765out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 bh_unlock_sock(sk);
767 sock_put(sk);
768 return;
769drop:
770 kfree_skb(skb);
Arnaldo Carvalho de Melod3894242005-09-22 07:57:21 -0300771 return;
772drop_unlock:
773 kfree_skb(skb);
774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777#undef LLC_REFCNT_DEBUG
778#ifdef LLC_REFCNT_DEBUG
779static atomic_t llc_sock_nr;
780#endif
781
782/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * llc_backlog_rcv - Processes rx frames and expired timers.
784 * @sk: LLC sock (p8022 connection)
785 * @skb: queued rx frame or event
786 *
787 * This function processes frames that has received and timers that has
788 * expired during sending an I pdu (refer to data_req_handler). frames
789 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
790 * callback functions(llc_c_ac.c).
791 */
792static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
793{
794 int rc = 0;
795 struct llc_sock *llc = llc_sk(sk);
796
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300797 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
798 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 rc = llc_conn_rcv(sk, skb);
800 else
801 goto out_kfree_skb;
802 } else if (llc_backlog_type(skb) == LLC_EVENT) {
803 /* timer expiration event */
Arnaldo Carvalho de Meloaf426d32005-09-22 03:59:22 -0300804 if (likely(llc->state > 1)) /* not closed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rc = llc_conn_state_process(sk, skb);
806 else
807 goto out_kfree_skb;
808 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800809 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto out_kfree_skb;
811 }
812out:
813 return rc;
814out_kfree_skb:
815 kfree_skb(skb);
816 goto out;
817}
818
819/**
820 * llc_sk_init - Initializes a socket with default llc values.
821 * @sk: socket to initialize.
822 *
823 * Initializes a socket with default llc values.
824 */
825static void llc_sk_init(struct sock* sk)
826{
827 struct llc_sock *llc = llc_sk(sk);
828
829 llc->state = LLC_CONN_STATE_ADM;
830 llc->inc_cntr = llc->dec_cntr = 2;
831 llc->dec_step = llc->connect_step = 1;
832
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800833 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
834 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300835 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800837 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
838 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300839 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800841 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
842 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300843 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800845 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
846 (unsigned long)sk);
Arnaldo Carvalho de Melo590232a2005-09-22 04:30:44 -0300847 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 llc->n2 = 2; /* max retransmit */
850 llc->k = 2; /* tx win size, will adjust dynam */
851 llc->rw = 128; /* rx win size (opt and equal to
YOSHIFUJI Hideakid57b1862007-02-09 23:25:01 +0900852 * tx_win of remote LLC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 skb_queue_head_init(&llc->pdu_unack_q);
854 sk->sk_backlog_rcv = llc_backlog_rcv;
855}
856
857/**
858 * llc_sk_alloc - Allocates LLC sock
859 * @family: upper layer protocol family
860 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
861 *
862 * Allocates a LLC sock and initializes it. Returns the new LLC sock
863 * or %NULL if there's no memory available for one
864 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700865struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700867 struct sock *sk = sk_alloc(net, family, priority, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (!sk)
870 goto out;
871 llc_sk_init(sk);
872 sock_init_data(NULL, sk);
873#ifdef LLC_REFCNT_DEBUG
874 atomic_inc(&llc_sock_nr);
875 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800876 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#endif
878out:
879 return sk;
880}
881
882/**
883 * llc_sk_free - Frees a LLC socket
884 * @sk - socket to free
885 *
886 * Frees a LLC socket
887 */
888void llc_sk_free(struct sock *sk)
889{
890 struct llc_sock *llc = llc_sk(sk);
891
892 llc->state = LLC_CONN_OUT_OF_SVC;
893 /* Stop all (possibly) running timers */
894 llc_conn_ac_stop_all_timers(sk, NULL);
895#ifdef DEBUG_LLC_CONN_ALLOC
Harvey Harrison0dc47872008-03-05 20:47:47 -0800896 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 skb_queue_len(&llc->pdu_unack_q),
898 skb_queue_len(&sk->sk_write_queue));
899#endif
900 skb_queue_purge(&sk->sk_receive_queue);
901 skb_queue_purge(&sk->sk_write_queue);
902 skb_queue_purge(&llc->pdu_unack_q);
903#ifdef LLC_REFCNT_DEBUG
904 if (atomic_read(&sk->sk_refcnt) != 1) {
905 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800906 sk, __func__, atomic_read(&sk->sk_refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
908 atomic_read(&llc_sock_nr));
909 } else {
910 atomic_dec(&llc_sock_nr);
911 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800912 __func__, atomic_read(&llc_sock_nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914#endif
915 sock_put(sk);
916}
917
918/**
919 * llc_sk_reset - resets a connection
920 * @sk: LLC socket to reset
921 *
922 * Resets a connection to the out of service state. Stops its timers
923 * and frees any frames in the queues of the connection.
924 */
925void llc_sk_reset(struct sock *sk)
926{
927 struct llc_sock *llc = llc_sk(sk);
928
929 llc_conn_ac_stop_all_timers(sk, NULL);
930 skb_queue_purge(&sk->sk_write_queue);
931 skb_queue_purge(&llc->pdu_unack_q);
932 llc->remote_busy_flag = 0;
933 llc->cause_flag = 0;
934 llc->retry_count = 0;
935 llc_conn_set_p_flag(sk, 0);
936 llc->f_flag = 0;
937 llc->s_flag = 0;
938 llc->ack_pf = 0;
939 llc->first_pdu_Ns = 0;
940 llc->ack_must_be_send = 0;
941 llc->dec_step = 1;
942 llc->inc_cntr = 2;
943 llc->dec_cntr = 2;
944 llc->X = 0;
945 llc->failed_data_req = 0 ;
946 llc->last_nr = 0;
947}