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