blob: 1f659e56d8e2cf2f9e4a5b29ed3e1416a02a340b [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
43/**
44 * llc_conn_state_process - sends event to connection state machine
45 * @sk: connection
46 * @skb: occurred event
47 *
48 * Sends an event to connection state machine. After processing event
49 * (executing it's actions and changing state), upper layer will be
50 * indicated or confirmed, if needed. Returns 0 for success, 1 for
51 * failure. The socket lock has to be held before calling this function.
52 */
53int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
54{
55 int rc;
56 struct llc_sock *llc = llc_sk(sk);
57 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
58
59 /*
60 * We have to hold the skb, because llc_conn_service will kfree it in
61 * the sending path and we need to look at the skb->cb, where we encode
62 * llc_conn_state_ev.
63 */
64 skb_get(skb);
65 ev->ind_prim = ev->cfm_prim = 0;
66 rc = llc_conn_service(sk, skb); /* sending event to state machine */
67 if (rc) {
68 printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
69 goto out_kfree_skb;
70 }
71
72 if (!ev->ind_prim && !ev->cfm_prim) {
73 /* indicate or confirm not required */
David S. Miller8728b832005-08-09 19:25:21 -070074 /* XXX this is not very pretty, perhaps we should store
75 * XXX indicate/confirm-needed state in the llc_conn_state_ev
76 * XXX control block of the SKB instead? -DaveM
77 */
78 if (!skb->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 goto out_kfree_skb;
80 goto out_skb_put;
81 }
82
83 if (ev->ind_prim && ev->cfm_prim) /* Paranoia */
84 skb_get(skb);
85
86 switch (ev->ind_prim) {
87 case LLC_DATA_PRIM:
88 llc_save_primitive(skb, LLC_DATA_PRIM);
89 if (sock_queue_rcv_skb(sk, skb)) {
90 /*
91 * shouldn't happen
92 */
93 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
94 __FUNCTION__);
95 kfree_skb(skb);
96 }
97 break;
98 case LLC_CONN_PRIM: {
99 struct sock *parent = skb->sk;
100
101 skb->sk = sk;
102 skb_queue_tail(&parent->sk_receive_queue, skb);
103 sk->sk_state_change(parent);
104 }
105 break;
106 case LLC_DISC_PRIM:
107 sock_hold(sk);
108 if (sk->sk_type == SOCK_STREAM &&
109 sk->sk_state == TCP_ESTABLISHED) {
110 sk->sk_shutdown = SHUTDOWN_MASK;
111 sk->sk_socket->state = SS_UNCONNECTED;
112 sk->sk_state = TCP_CLOSE;
113 if (!sock_flag(sk, SOCK_DEAD)) {
114 sk->sk_state_change(sk);
115 sock_set_flag(sk, SOCK_DEAD);
116 }
117 }
118 kfree_skb(skb);
119 sock_put(sk);
120 break;
121 case LLC_RESET_PRIM:
122 /*
123 * FIXME:
124 * RESET is not being notified to upper layers for now
125 */
126 printk(KERN_INFO "%s: received a reset ind!\n", __FUNCTION__);
127 kfree_skb(skb);
128 break;
129 default:
130 if (ev->ind_prim) {
131 printk(KERN_INFO "%s: received unknown %d prim!\n",
132 __FUNCTION__, ev->ind_prim);
133 kfree_skb(skb);
134 }
135 /* No indication */
136 break;
137 }
138
139 switch (ev->cfm_prim) {
140 case LLC_DATA_PRIM:
141 if (!llc_data_accept_state(llc->state))
142 sk->sk_write_space(sk);
143 else
144 rc = llc->failed_data_req = 1;
145 break;
146 case LLC_CONN_PRIM:
147 if (sk->sk_type == SOCK_STREAM &&
148 sk->sk_state == TCP_SYN_SENT) {
149 if (ev->status) {
150 sk->sk_socket->state = SS_UNCONNECTED;
151 sk->sk_state = TCP_CLOSE;
152 } else {
153 sk->sk_socket->state = SS_CONNECTED;
154 sk->sk_state = TCP_ESTABLISHED;
155 }
156 sk->sk_state_change(sk);
157 }
158 break;
159 case LLC_DISC_PRIM:
160 sock_hold(sk);
161 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
162 sk->sk_socket->state = SS_UNCONNECTED;
163 sk->sk_state = TCP_CLOSE;
164 sk->sk_state_change(sk);
165 }
166 sock_put(sk);
167 break;
168 case LLC_RESET_PRIM:
169 /*
170 * FIXME:
171 * RESET is not being notified to upper layers for now
172 */
173 printk(KERN_INFO "%s: received a reset conf!\n", __FUNCTION__);
174 break;
175 default:
176 if (ev->cfm_prim) {
177 printk(KERN_INFO "%s: received unknown %d prim!\n",
178 __FUNCTION__, ev->cfm_prim);
179 break;
180 }
181 goto out_skb_put; /* No confirmation */
182 }
183out_kfree_skb:
184 kfree_skb(skb);
185out_skb_put:
186 kfree_skb(skb);
187 return rc;
188}
189
190void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
191{
192 /* queue PDU to send to MAC layer */
193 skb_queue_tail(&sk->sk_write_queue, skb);
194 llc_conn_send_pdus(sk);
195}
196
197/**
198 * llc_conn_rtn_pdu - sends received data pdu to upper layer
199 * @sk: Active connection
200 * @skb: Received data frame
201 *
202 * Sends received data pdu to upper layer (by using indicate function).
203 * Prepares service parameters (prim and prim_data). calling indication
204 * function will be done in llc_conn_state_process.
205 */
206void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
207{
208 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
209
210 ev->ind_prim = LLC_DATA_PRIM;
211}
212
213/**
214 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
215 * @sk: active connection
216 * @nr: NR
217 * @first_p_bit: p_bit value of first pdu
218 *
219 * Resend all unacknowledged I PDUs, starting with the NR; send first as
220 * command PDU with P bit equal first_p_bit; if more than one send
221 * subsequent as command PDUs with P bit equal zero (0).
222 */
223void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
224{
225 struct sk_buff *skb;
226 struct llc_pdu_sn *pdu;
227 u16 nbr_unack_pdus;
228 struct llc_sock *llc;
229 u8 howmany_resend = 0;
230
231 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
232 if (!nbr_unack_pdus)
233 goto out;
234 /*
235 * Process unack PDUs only if unack queue is not empty; remove
236 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
237 */
238 llc = llc_sk(sk);
239
240 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
241 pdu = llc_pdu_sn_hdr(skb);
242 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
243 llc_pdu_set_pf_bit(skb, first_p_bit);
244 skb_queue_tail(&sk->sk_write_queue, skb);
245 first_p_bit = 0;
246 llc->vS = LLC_I_GET_NS(pdu);
247 howmany_resend++;
248 }
249 if (howmany_resend > 0)
250 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
251 /* any PDUs to re-send are queued up; start sending to MAC */
252 llc_conn_send_pdus(sk);
253out:;
254}
255
256/**
257 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
258 * @sk: active connection.
259 * @nr: NR
260 * @first_f_bit: f_bit value of first pdu.
261 *
262 * Resend all unacknowledged I PDUs, starting with the NR; send first as
263 * response PDU with F bit equal first_f_bit; if more than one send
264 * subsequent as response PDUs with F bit equal zero (0).
265 */
266void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
267{
268 struct sk_buff *skb;
269 u16 nbr_unack_pdus;
270 struct llc_sock *llc = llc_sk(sk);
271 u8 howmany_resend = 0;
272
273 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
274 if (!nbr_unack_pdus)
275 goto out;
276 /*
277 * Process unack PDUs only if unack queue is not empty; remove
278 * appropriate PDUs, fix them up, and put them on mac_pdu_q
279 */
280 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
281 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
282
283 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
284 llc_pdu_set_pf_bit(skb, first_f_bit);
285 skb_queue_tail(&sk->sk_write_queue, skb);
286 first_f_bit = 0;
287 llc->vS = LLC_I_GET_NS(pdu);
288 howmany_resend++;
289 }
290 if (howmany_resend > 0)
291 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
292 /* any PDUs to re-send are queued up; start sending to MAC */
293 llc_conn_send_pdus(sk);
294out:;
295}
296
297/**
298 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
299 * @sk: active connection
300 * nr: NR
301 * how_many_unacked: size of pdu_unack_q after removing acked pdus
302 *
303 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
304 * the number of pdus that removed from queue.
305 */
306int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
307{
308 int pdu_pos, i;
309 struct sk_buff *skb;
310 struct llc_pdu_sn *pdu;
311 int nbr_acked = 0;
312 struct llc_sock *llc = llc_sk(sk);
313 int q_len = skb_queue_len(&llc->pdu_unack_q);
314
315 if (!q_len)
316 goto out;
317 skb = skb_peek(&llc->pdu_unack_q);
318 pdu = llc_pdu_sn_hdr(skb);
319
320 /* finding position of last acked pdu in queue */
321 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
322 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
323
324 for (i = 0; i < pdu_pos && i < q_len; i++) {
325 skb = skb_dequeue(&llc->pdu_unack_q);
326 if (skb)
327 kfree_skb(skb);
328 nbr_acked++;
329 }
330out:
331 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
332 return nbr_acked;
333}
334
335/**
336 * llc_conn_send_pdus - Sends queued PDUs
337 * @sk: active connection
338 *
339 * Sends queued pdus to MAC layer for transmission.
340 */
341static void llc_conn_send_pdus(struct sock *sk)
342{
343 struct sk_buff *skb;
344
345 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
346 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
347
348 if (LLC_PDU_TYPE_IS_I(pdu) &&
349 !(skb->dev->flags & IFF_LOOPBACK)) {
350 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
351
352 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
353 if (!skb2)
354 break;
355 skb = skb2;
356 }
357 dev_queue_xmit(skb);
358 }
359}
360
361/**
362 * llc_conn_service - finds transition and changes state of connection
363 * @sk: connection
364 * @skb: happened event
365 *
366 * This function finds transition that matches with happened event, then
367 * executes related actions and finally changes state of connection.
368 * Returns 0 for success, 1 for failure.
369 */
370static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
371{
372 int rc = 1;
373 struct llc_sock *llc = llc_sk(sk);
374 struct llc_conn_state_trans *trans;
375
376 if (llc->state > NBR_CONN_STATES)
377 goto out;
378 rc = 0;
379 trans = llc_qualify_conn_ev(sk, skb);
380 if (trans) {
381 rc = llc_exec_conn_trans_actions(sk, trans, skb);
382 if (!rc && trans->next_state != NO_STATE_CHANGE) {
383 llc->state = trans->next_state;
384 if (!llc_data_accept_state(llc->state))
385 sk->sk_state_change(sk);
386 }
387 }
388out:
389 return rc;
390}
391
392/**
393 * llc_qualify_conn_ev - finds transition for event
394 * @sk: connection
395 * @skb: happened event
396 *
397 * This function finds transition that matches with happened event.
398 * Returns pointer to found transition on success, %NULL otherwise.
399 */
400static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
401 struct sk_buff *skb)
402{
403 struct llc_conn_state_trans **next_trans;
404 llc_conn_ev_qfyr_t *next_qualifier;
405 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
406 struct llc_sock *llc = llc_sk(sk);
407 struct llc_conn_state *curr_state =
408 &llc_conn_state_table[llc->state - 1];
409
410 /* search thru events for this state until
411 * list exhausted or until no more
412 */
413 for (next_trans = curr_state->transitions +
414 llc_find_offset(llc->state - 1, ev->type);
415 (*next_trans)->ev; next_trans++) {
416 if (!((*next_trans)->ev)(sk, skb)) {
417 /* got POSSIBLE event match; the event may require
418 * qualification based on the values of a number of
419 * state flags; if all qualifications are met (i.e.,
420 * if all qualifying functions return success, or 0,
421 * then this is THE event we're looking for
422 */
423 for (next_qualifier = (*next_trans)->ev_qualifiers;
424 next_qualifier && *next_qualifier &&
425 !(*next_qualifier)(sk, skb); next_qualifier++)
426 /* nothing */;
427 if (!next_qualifier || !*next_qualifier)
428 /* all qualifiers executed successfully; this is
429 * our transition; return it so we can perform
430 * the associated actions & change the state
431 */
432 return *next_trans;
433 }
434 }
435 return NULL;
436}
437
438/**
439 * llc_exec_conn_trans_actions - executes related actions
440 * @sk: connection
441 * @trans: transition that it's actions must be performed
442 * @skb: event
443 *
444 * Executes actions that is related to happened event. Returns 0 for
445 * success, 1 to indicate failure of at least one action.
446 */
447static int llc_exec_conn_trans_actions(struct sock *sk,
448 struct llc_conn_state_trans *trans,
449 struct sk_buff *skb)
450{
451 int rc = 0;
452 llc_conn_action_t *next_action;
453
454 for (next_action = trans->ev_actions;
455 next_action && *next_action; next_action++) {
456 int rc2 = (*next_action)(sk, skb);
457
458 if (rc2 == 2) {
459 rc = rc2;
460 break;
461 } else if (rc2)
462 rc = 1;
463 }
464 return rc;
465}
466
467/**
468 * llc_lookup_established - Finds connection for the remote/local sap/mac
469 * @sap: SAP
470 * @daddr: address of remote LLC (MAC + SAP)
471 * @laddr: address of local LLC (MAC + SAP)
472 *
473 * Search connection list of the SAP and finds connection using the remote
474 * mac, remote sap, local mac, and local sap. Returns pointer for
475 * connection found, %NULL otherwise.
476 */
477struct sock *llc_lookup_established(struct llc_sap *sap, struct llc_addr *daddr,
478 struct llc_addr *laddr)
479{
480 struct sock *rc;
481 struct hlist_node *node;
482
483 read_lock_bh(&sap->sk_list.lock);
484 sk_for_each(rc, node, &sap->sk_list.list) {
485 struct llc_sock *llc = llc_sk(rc);
486
487 if (llc->laddr.lsap == laddr->lsap &&
488 llc->daddr.lsap == daddr->lsap &&
489 llc_mac_match(llc->laddr.mac, laddr->mac) &&
490 llc_mac_match(llc->daddr.mac, daddr->mac)) {
491 sock_hold(rc);
492 goto found;
493 }
494 }
495 rc = NULL;
496found:
497 read_unlock_bh(&sap->sk_list.lock);
498 return rc;
499}
500
501/**
502 * llc_lookup_listener - Finds listener for local MAC + SAP
503 * @sap: SAP
504 * @laddr: address of local LLC (MAC + SAP)
505 *
506 * Search connection list of the SAP and finds connection listening on
507 * local mac, and local sap. Returns pointer for parent socket found,
508 * %NULL otherwise.
509 */
510static struct sock *llc_lookup_listener(struct llc_sap *sap,
511 struct llc_addr *laddr)
512{
513 struct sock *rc;
514 struct hlist_node *node;
515
516 read_lock_bh(&sap->sk_list.lock);
517 sk_for_each(rc, node, &sap->sk_list.list) {
518 struct llc_sock *llc = llc_sk(rc);
519
520 if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
521 llc->laddr.lsap == laddr->lsap &&
522 (llc_mac_match(llc->laddr.mac, laddr->mac) ||
523 llc_mac_null(llc->laddr.mac))) {
524 sock_hold(rc);
525 goto found;
526 }
527 }
528 rc = NULL;
529found:
530 read_unlock_bh(&sap->sk_list.lock);
531 return rc;
532}
533
534/**
535 * llc_data_accept_state - designates if in this state data can be sent.
536 * @state: state of connection.
537 *
538 * Returns 0 if data can be sent, 1 otherwise.
539 */
540u8 llc_data_accept_state(u8 state)
541{
542 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
543 state != LLC_CONN_STATE_REJ;
544}
545
546/**
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300547 * llc_find_next_offset - finds offset for next category of transitions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * @state: state table.
549 * @offset: start offset.
550 *
551 * Finds offset of next category of transitions in transition table.
552 * Returns the start index of next category.
553 */
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300554static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 u16 cnt = 0;
557 struct llc_conn_state_trans **next_trans;
558
559 for (next_trans = state->transitions + offset;
560 (*next_trans)->ev; next_trans++)
561 ++cnt;
562 return cnt;
563}
564
565/**
566 * llc_build_offset_table - builds offset table of connection
567 *
568 * Fills offset table of connection state transition table
569 * (llc_offset_table).
570 */
571void __init llc_build_offset_table(void)
572{
573 struct llc_conn_state *curr_state;
574 int state, ev_type, next_offset;
575
576 for (state = 0; state < NBR_CONN_STATES; state++) {
577 curr_state = &llc_conn_state_table[state];
578 next_offset = 0;
579 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
580 llc_offset_table[state][ev_type] = next_offset;
Arnaldo Carvalho de Melo0eb80172005-09-22 03:57:55 -0300581 next_offset += llc_find_next_offset(curr_state,
582 next_offset) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
585}
586
587/**
588 * llc_find_offset - finds start offset of category of transitions
589 * @state: state of connection
590 * @ev_type: type of happened event
591 *
592 * Finds start offset of desired category of transitions. Returns the
593 * desired start offset.
594 */
595static int llc_find_offset(int state, int ev_type)
596{
597 int rc = 0;
598 /* at this stage, llc_offset_table[..][2] is not important. it is for
599 * init_pf_cycle and I don't know what is it.
600 */
601 switch (ev_type) {
602 case LLC_CONN_EV_TYPE_PRIM:
603 rc = llc_offset_table[state][0]; break;
604 case LLC_CONN_EV_TYPE_PDU:
605 rc = llc_offset_table[state][4]; break;
606 case LLC_CONN_EV_TYPE_SIMPLE:
607 rc = llc_offset_table[state][1]; break;
608 case LLC_CONN_EV_TYPE_P_TMR:
609 case LLC_CONN_EV_TYPE_ACK_TMR:
610 case LLC_CONN_EV_TYPE_REJ_TMR:
611 case LLC_CONN_EV_TYPE_BUSY_TMR:
612 rc = llc_offset_table[state][3]; break;
613 }
614 return rc;
615}
616
617/**
618 * llc_sap_add_socket - adds a socket to a SAP
619 * @sap: SAP
620 * @sk: socket
621 *
622 * This function adds a socket to sk_list of a SAP.
623 */
624void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
625{
626 write_lock_bh(&sap->sk_list.lock);
627 llc_sk(sk)->sap = sap;
628 sk_add_node(sk, &sap->sk_list.list);
629 write_unlock_bh(&sap->sk_list.lock);
630}
631
632/**
633 * llc_sap_remove_socket - removes a socket from SAP
634 * @sap: SAP
635 * @sk: socket
636 *
637 * This function removes a connection from sk_list.list of a SAP if
638 * the connection was in this list.
639 */
640void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
641{
642 write_lock_bh(&sap->sk_list.lock);
643 sk_del_node_init(sk);
644 write_unlock_bh(&sap->sk_list.lock);
645}
646
647/**
648 * llc_conn_rcv - sends received pdus to the connection state machine
649 * @sk: current connection structure.
650 * @skb: received frame.
651 *
652 * Sends received pdus to the connection state machine.
653 */
654static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
655{
656 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
657 struct llc_sock *llc = llc_sk(sk);
658
659 if (!llc->dev)
660 llc->dev = skb->dev;
661 ev->type = LLC_CONN_EV_TYPE_PDU;
662 ev->reason = 0;
663 return llc_conn_state_process(sk, skb);
664}
665
666void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
667{
668 struct llc_addr saddr, daddr;
669 struct sock *sk;
670
671 llc_pdu_decode_sa(skb, saddr.mac);
672 llc_pdu_decode_ssap(skb, &saddr.lsap);
673 llc_pdu_decode_da(skb, daddr.mac);
674 llc_pdu_decode_dsap(skb, &daddr.lsap);
675
676 sk = llc_lookup_established(sap, &saddr, &daddr);
677 if (!sk) {
678 /*
679 * Didn't find an active connection; verify if there
680 * is a listening socket for this llc addr
681 */
682 struct llc_sock *llc;
683 struct sock *parent = llc_lookup_listener(sap, &daddr);
684
685 if (!parent) {
686 dprintk("llc_lookup_listener failed!\n");
687 goto drop;
688 }
689
690 sk = llc_sk_alloc(parent->sk_family, GFP_ATOMIC, parent->sk_prot);
691 if (!sk) {
692 sock_put(parent);
693 goto drop;
694 }
695 llc = llc_sk(sk);
696 memcpy(&llc->laddr, &daddr, sizeof(llc->laddr));
697 memcpy(&llc->daddr, &saddr, sizeof(llc->daddr));
698 llc_sap_add_socket(sap, sk);
699 sock_hold(sk);
700 sock_put(parent);
701 skb->sk = parent;
702 } else
703 skb->sk = sk;
704 bh_lock_sock(sk);
705 if (!sock_owned_by_user(sk))
706 llc_conn_rcv(sk, skb);
707 else {
708 dprintk("%s: adding to backlog...\n", __FUNCTION__);
709 llc_set_backlog_type(skb, LLC_PACKET);
710 sk_add_backlog(sk, skb);
711 }
712 bh_unlock_sock(sk);
713 sock_put(sk);
714 return;
715drop:
716 kfree_skb(skb);
717}
718
719#undef LLC_REFCNT_DEBUG
720#ifdef LLC_REFCNT_DEBUG
721static atomic_t llc_sock_nr;
722#endif
723
724/**
725 * llc_release_sockets - releases all sockets in a sap
726 * @sap: sap to release its sockets
727 *
728 * Releases all connections of a sap. Returns 0 if all actions complete
729 * successfully, nonzero otherwise
730 */
731int llc_release_sockets(struct llc_sap *sap)
732{
733 int rc = 0;
734 struct sock *sk;
735 struct hlist_node *node;
736
737 write_lock_bh(&sap->sk_list.lock);
738
739 sk_for_each(sk, node, &sap->sk_list.list) {
740 llc_sk(sk)->state = LLC_CONN_STATE_TEMP;
741
742 if (llc_send_disc(sk))
743 rc = 1;
744 }
745
746 write_unlock_bh(&sap->sk_list.lock);
747 return rc;
748}
749
750/**
751 * llc_backlog_rcv - Processes rx frames and expired timers.
752 * @sk: LLC sock (p8022 connection)
753 * @skb: queued rx frame or event
754 *
755 * This function processes frames that has received and timers that has
756 * expired during sending an I pdu (refer to data_req_handler). frames
757 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
758 * callback functions(llc_c_ac.c).
759 */
760static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
761{
762 int rc = 0;
763 struct llc_sock *llc = llc_sk(sk);
764
765 if (llc_backlog_type(skb) == LLC_PACKET) {
766 if (llc->state > 1) /* not closed */
767 rc = llc_conn_rcv(sk, skb);
768 else
769 goto out_kfree_skb;
770 } else if (llc_backlog_type(skb) == LLC_EVENT) {
771 /* timer expiration event */
772 if (llc->state > 1) /* not closed */
773 rc = llc_conn_state_process(sk, skb);
774 else
775 goto out_kfree_skb;
776 } else {
777 printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
778 goto out_kfree_skb;
779 }
780out:
781 return rc;
782out_kfree_skb:
783 kfree_skb(skb);
784 goto out;
785}
786
787/**
788 * llc_sk_init - Initializes a socket with default llc values.
789 * @sk: socket to initialize.
790 *
791 * Initializes a socket with default llc values.
792 */
793static void llc_sk_init(struct sock* sk)
794{
795 struct llc_sock *llc = llc_sk(sk);
796
797 llc->state = LLC_CONN_STATE_ADM;
798 llc->inc_cntr = llc->dec_cntr = 2;
799 llc->dec_step = llc->connect_step = 1;
800
801 init_timer(&llc->ack_timer.timer);
802 llc->ack_timer.expire = LLC_ACK_TIME;
803 llc->ack_timer.timer.data = (unsigned long)sk;
804 llc->ack_timer.timer.function = llc_conn_ack_tmr_cb;
805
806 init_timer(&llc->pf_cycle_timer.timer);
807 llc->pf_cycle_timer.expire = LLC_P_TIME;
808 llc->pf_cycle_timer.timer.data = (unsigned long)sk;
809 llc->pf_cycle_timer.timer.function = llc_conn_pf_cycle_tmr_cb;
810
811 init_timer(&llc->rej_sent_timer.timer);
812 llc->rej_sent_timer.expire = LLC_REJ_TIME;
813 llc->rej_sent_timer.timer.data = (unsigned long)sk;
814 llc->rej_sent_timer.timer.function = llc_conn_rej_tmr_cb;
815
816 init_timer(&llc->busy_state_timer.timer);
817 llc->busy_state_timer.expire = LLC_BUSY_TIME;
818 llc->busy_state_timer.timer.data = (unsigned long)sk;
819 llc->busy_state_timer.timer.function = llc_conn_busy_tmr_cb;
820
821 llc->n2 = 2; /* max retransmit */
822 llc->k = 2; /* tx win size, will adjust dynam */
823 llc->rw = 128; /* rx win size (opt and equal to
824 * tx_win of remote LLC) */
825 skb_queue_head_init(&llc->pdu_unack_q);
826 sk->sk_backlog_rcv = llc_backlog_rcv;
827}
828
829/**
830 * llc_sk_alloc - Allocates LLC sock
831 * @family: upper layer protocol family
832 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
833 *
834 * Allocates a LLC sock and initializes it. Returns the new LLC sock
835 * or %NULL if there's no memory available for one
836 */
837struct sock *llc_sk_alloc(int family, int priority, struct proto *prot)
838{
839 struct sock *sk = sk_alloc(family, priority, prot, 1);
840
841 if (!sk)
842 goto out;
843 llc_sk_init(sk);
844 sock_init_data(NULL, sk);
845#ifdef LLC_REFCNT_DEBUG
846 atomic_inc(&llc_sock_nr);
847 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
848 __FUNCTION__, atomic_read(&llc_sock_nr));
849#endif
850out:
851 return sk;
852}
853
854/**
855 * llc_sk_free - Frees a LLC socket
856 * @sk - socket to free
857 *
858 * Frees a LLC socket
859 */
860void llc_sk_free(struct sock *sk)
861{
862 struct llc_sock *llc = llc_sk(sk);
863
864 llc->state = LLC_CONN_OUT_OF_SVC;
865 /* Stop all (possibly) running timers */
866 llc_conn_ac_stop_all_timers(sk, NULL);
867#ifdef DEBUG_LLC_CONN_ALLOC
868 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
869 skb_queue_len(&llc->pdu_unack_q),
870 skb_queue_len(&sk->sk_write_queue));
871#endif
872 skb_queue_purge(&sk->sk_receive_queue);
873 skb_queue_purge(&sk->sk_write_queue);
874 skb_queue_purge(&llc->pdu_unack_q);
875#ifdef LLC_REFCNT_DEBUG
876 if (atomic_read(&sk->sk_refcnt) != 1) {
877 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
878 sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
879 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
880 atomic_read(&llc_sock_nr));
881 } else {
882 atomic_dec(&llc_sock_nr);
883 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
884 __FUNCTION__, atomic_read(&llc_sock_nr));
885 }
886#endif
887 sock_put(sk);
888}
889
890/**
891 * llc_sk_reset - resets a connection
892 * @sk: LLC socket to reset
893 *
894 * Resets a connection to the out of service state. Stops its timers
895 * and frees any frames in the queues of the connection.
896 */
897void llc_sk_reset(struct sock *sk)
898{
899 struct llc_sock *llc = llc_sk(sk);
900
901 llc_conn_ac_stop_all_timers(sk, NULL);
902 skb_queue_purge(&sk->sk_write_queue);
903 skb_queue_purge(&llc->pdu_unack_q);
904 llc->remote_busy_flag = 0;
905 llc->cause_flag = 0;
906 llc->retry_count = 0;
907 llc_conn_set_p_flag(sk, 0);
908 llc->f_flag = 0;
909 llc->s_flag = 0;
910 llc->ack_pf = 0;
911 llc->first_pdu_Ns = 0;
912 llc->ack_must_be_send = 0;
913 llc->dec_step = 1;
914 llc->inc_cntr = 2;
915 llc->dec_cntr = 2;
916 llc->X = 0;
917 llc->failed_data_req = 0 ;
918 llc->last_nr = 0;
919}